If I'm understanding the requirements properly, it seems that if the lightning encounters a barrier, it goes around it on both sides, then merges automatically if possible on the other side.
First method:
I'd say fire something like 3 entities. They should stay vertically aligned at all times. If one of them hits a wall and the other 2 keep flying, it should either disappear or try to align itself with the others and keep flying.
Or you could fire a spray of entities that stay vertical like this, and when this hit a surface at a certain angle that kills the entity, but the others continue on without it. If they hit at an angle that won't kill them, instead of slowing down it reapplies its velocity and/or sets its origin to that of the sister entity that is farthest away from the original starting position. The entities that can see each other push each other away or otherwise try to space themselves out appropriately.
In any case, the entities that can't see any other sisters will fire their lightning independently; those that can see each other will pick one to do the lightning. The lightning is done by tracelining to the ceiling, tracelining from the ceiling to the floor, and finally using that traceline to draw the lightning. Entities that realize they're inside a wall do not apply lightning, and set their movetype to noclip until they get out of the wall.
This method is riddled with problems though when you shoot at stairs or other non-smooth geometry.
---
Second method:
Another method is to fire a noclip entity out that acts like the black hole mod I saw once. Their black hold gun would traceline to the wall, then incrementally step forward through it and check if the contents was solid. When/if it found a location that was not solid, it knew it had breached to the other side and tracelined back toward the origin to find where that edge was. Then it creates a portal on both sides of the wall, and if you step into one you teleport to the portal on the other side.
Thus this noclip entity just flies merrily along, alone. When not in a wall, it tracelines to the ceiling, then tracelines from the ceiling to the floor for the lightning. When it's in a solid, it does the black hole gun trick for both up and down directions. If it finds empty space above it, it does its ceiling to floor lightning there, and the same for if it finds empty space below it.
After it hits its timeout (say, 10 seconds), it dies.
---
Method 3:
Sort of a hybrid method. When this noclip entity enters a wall, it does its black hole gun traceline trick once for up and once for down. For each direction it finds empty space, it spawns a new entity like itself and then deletes itself (or moves itself if it only finds one direction). This way, it continues to split into new children for each time the geometry segments.
Each segment probably inherits its base timer from its parent, so that only 10 seconds of zappiness max happens no matter how often it has to split.
For safety, you could even set a limit on how many generations it can produce.
Some extra tracelines when you spawn a child or move the parent out of the wall may be desirable, in case a slope or stairs are going to make it keep entering the wall. Firing a spread of tracelines from its origin to its destination a couple seconds away with varying Z offsets could help it choose a height/direction that will maximize how long it flies before entering a wall again.
---
Method 4:
Get the Hexen code, and see how they did it.