package jelectrum;

import org.jibble.pircbot.PircBot;
import org.jibble.pircbot.IrcException;


public class IrcBot extends PircBot
{
  private Jelectrum jelly;
  private Object connection_lock = new Object();
  private Config config;

  private long kickWaitTime=0;
  private boolean shouldRun = false;
  private String advert_host = null;
  private String nick = null;

  public IrcBot(Jelectrum jelly, String mode)
  {
    config = jelly.getConfig();

    if (mode == null)
    {

      if (config.isSet("irc_enabled") && config.getBoolean("irc_enabled"))
      {
        config.require("irc_nick");
        config.require("irc_advertise_host");
        String nick = ("E_j_" + config.get("irc_nick"));
        advert_host = config.get("irc_advertise_host");

        setName(nick);
        setLogin(nick);
        shouldRun=true;

      }
    }
    else 
    {
      if (config.isSet("irc_enabled_" + mode) && config.getBoolean("irc_enabled_" + mode))
      {
        config.require("irc_nick_"+mode);
        config.require("irc_advertise_host_"+mode);

        String nick = "E_j_" + config.get("irc_nick_"+mode);
        advert_host = config.get("irc_advertise_host_"+mode);

        setName(nick);
        setLogin(nick);
        shouldRun=true;

      }
 
    }

    this.jelly = jelly;

  }
  public String getAdvertString()
  {
    StringBuilder sb=new StringBuilder();
    sb.append(advert_host);
    sb.append(" v");
    sb.append(StratumConnection.PROTO_VERSION);
    sb.append(" p10000");
    if (config.isSet("tcp_port")) sb.append(" t");
    if (config.isSet("ssl_port")) sb.append(" s");

    return sb.toString();


  }


  public void start()
  {
    if (shouldRun)
    {
      new IrcThread().start();
    }
  }

  @Override
  protected void onDisconnect() 
  {
    synchronized(connection_lock)
    {
      connection_lock.notifyAll();
    }
  }

  @Override
  protected void onKick(String channel, String kickerNick, String kickerLogin, String kickerHostname, String recipientNick, String reason)
  {
    jelly.getEventLog().log("Kicked from " + channel + " by " + kickerNick + " for " + reason);
    jelly.getEventLog().log("Waiting one hour before trying again");


    kickWaitTime = System.currentTimeMillis() + 3600L * 1000L;

    synchronized(connection_lock)
    {
      connection_lock.notifyAll();
    }
  }


  public class IrcThread extends Thread
  {

    public IrcThread()
    {
      setName("IrcThread");
      setDaemon(true);


    }

    public void run()
    {
      while(shouldRun)
      {
        try
        {
          ircRun();

        }
        catch(Throwable t)
        {
          t.printStackTrace();
        }

        try { sleep(600000);} catch(Throwable t){}

      }

    }

    public void ircRun() throws java.io.IOException, IrcException, InterruptedException
    {
      //setVerbose(true);
      
      setVersion(getAdvertString());
      connect("irc.freenode.net");


      joinChannel("#electrum");
      joinChannel("#jelectrum");

      synchronized(connection_lock)
      {
        connection_lock.wait();
      }

      if (System.currentTimeMillis() < kickWaitTime)
      {
        Thread.sleep(15000);
      }


      disconnect();

    }
  }


}

@@ -647,31 +647,24 @@
 %22);%0A        
-String 
 nick = (%22E_j
@@ -1054,23 +1054,16 @@
         
-String 
 nick = %22
@@ -2000,24 +2000,68 @@
 reason)%0A  %7B%0A
+    if (nick.equals(recipientNick))%0A    %7B%0A  
     jelly.ge
@@ -2144,24 +2144,26 @@
 eason);%0A    
+  
 jelly.getEve
@@ -2212,25 +2212,26 @@
 g again%22);%0A%0A
-%0A
+  
     kickWait
@@ -2282,24 +2282,26 @@
 1000L;%0A%0A    
+  
 synchronized
@@ -2314,34 +2314,38 @@
 ction_lock)%0A    
+ 
+ 
 %7B%0A
+  
       connection
@@ -2355,32 +2355,40 @@
 ck.notifyAll();%0A
+      %7D%0A
     %7D%0A  %7D%0A%0A%0A  pu

Fix irc kick bug