/*
 * This file is part of TheStig, licensed under the MIT License (MIT).
 *
 * Copyright (c) 2016, Lexteam <http://www.lexteam.xyz/>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package xyz.lexteam.thestig.module.bridge;

import org.kitteh.irc.client.library.event.channel.ChannelMessageEvent;
import org.kitteh.irc.lib.net.engio.mbassy.listener.Handler;
import xyz.lexteam.thestig.Main;
import xyz.lexteam.thestig.module.IModule;

public class BridgeModule implements IModule {

    @Handler
    public void onMessageEvent(ChannelMessageEvent event) {
        if (!event.getActor().getNick().equalsIgnoreCase("TheStig")) { // TODO: this is shit
            if (Main.INSTANCE.getConfig().getBridges().containsKey(
                    event.getClient().getServerInfo().getNetworkName().get() + "-" + event.getChannel().getName())) {
                String secondaryChannel = Main.INSTANCE.getConfig().getBridges().get(event.getClient().getServerInfo
                        ().getNetworkName().get() + "-" + event.getChannel().getName());
                String[] split = secondaryChannel.split("-");
                Main.INSTANCE.getServers().get(split[0]).getChannel(split[1]).get().sendMessage("[" + event.getActor
                        ().getNick() + "] " +event.getMessage());
            }
        }
    }

    @Override
    public String getName() {
        return "bridge";
    }

    @Override
    public void onEnable() {

    }
}

@@ -1250,16 +1250,61 @@
 ridge;%0A%0A
+import org.kitteh.irc.client.library.Client;%0A
 import o
@@ -1687,131 +1687,64 @@
 ase(
-%22TheStig%22)) %7B // TODO: this is shit%0A            if (Main.INSTANCE.getConfig().getBridges().containsKey(%0A                   
+event.getClient().getNick())) %7B%0A            String key =
  eve
@@ -1833,20 +1833,18 @@
 etName()
-)) %7B
+;%0A
 %0A       
@@ -1852,38 +1852,12 @@
     
-    String secondaryChannel = 
+if (
 Main
@@ -1895,130 +1895,116 @@
 s().
-get(event.getClient().getServerInfo%0A                        ().getNetworkName().get() + %22-%22 + event.getChannel().getName()
+containsKey(key)) %7B%0A                String secondaryChannel = Main.INSTANCE.getConfig().getBridges().get(key
 );%0A 
@@ -2064,16 +2064,17 @@
 t(%22-%22);%0A
+%0A
         
@@ -2080,16 +2080,32 @@
         
+ Client client =
  Main.IN
@@ -2141,93 +2141,257 @@
 %5B0%5D)
-.getChannel(split%5B1%5D).get().sendMessage(%22%5B%22 + event.getActor%0A                        
+;%0A                if (client != null) %7B%0A                    if (client.getChannel(split%5B1%5D).isPresent()) %7B%0A                        client.getChannel(split%5B1%5D).get()%0A                                .sendMessage(String.format(%22%5B%25s%5D %25s%22, event.getActor
 ().g
@@ -2402,17 +2402,10 @@
 ck()
+,
  
-+ %22%5D %22 +
 even
@@ -2419,17 +2419,58 @@
 ssage())
-;
+);%0A                    %7D%0A                %7D
 %0A       

Fix bridge module