private void connect() throws UnknownHostException, IOException { IrcServer = new Socket(serverName, 6667); BufferedReader br = new BufferedReader( new InputStreamReader(IrcServer.getInputStream(), BotStats.getInstance().getCharset())); PrintWriter pw = new PrintWriter( new OutputStreamWriter( IrcServer.getOutputStream(), BotStats.getInstance().getCharset()), true); ih = new InputHandler(br); oh = new OutputHandler(pw); ih.start(); oh.start(); new Message("", "NICK", BotStats.getInstance().getBotname(), "").send(); new Message( "", "USER", "goat" + " nowhere.com " + serverName, BotStats.getInstance().getVersion()) .send(); // we sleep until we are connected, don't want to send these next messages too soon while (!connected) { try { sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } } joinChannels(); }
private void joinChannels() { String[] channels = BotStats.getInstance().getChannels(); for (String channel : channels) new Message("", "JOIN", channel, "").send(); }
public void run() { String messageString; long lastActivity = System.currentTimeMillis(); while (keeprunning) { try { if (in.ready()) { messageString = in.readLine(); if (messageString == null) continue; Message m = new Message(messageString); if (m.getCommand().matches("\\d+")) try { int intcommand = Integer.parseInt(m.getCommand()); if (intcommand == Constants.RPL_ENDOFMOTD) { System.err.println("MOTD SEEN, must be connected."); if (connected) joinChannels(); connected = true; } else if (intcommand == Constants.ERR_NICKNAMEINUSE) { System.err.println("NICKNAMEINUSE"); namecount++; BotStats.getInstance().setBotname(botdefaultname + namecount); new Message("", "NICK", BotStats.getInstance().getBotname(), "").send(); new Message( "", "USER", BotStats.getInstance().getBotname() + " nowhere.com " + BotStats.getInstance().getServername(), BotStats.getInstance().getClientName() + " v." + BotStats.getInstance().getVersion()) .send(); System.err.println("Setting nick to:" + botdefaultname + namecount); } } catch (NumberFormatException nfe) { // we ignore this System.err.println("Unknown command:" + m.getCommand()); } if (m.getCommand().equals("PING")) { outqueue.add(new Message("", "PONG", "", m.getTrailing())); if (debug) System.out.println("PUNG at " + new Date()); } else if (BotStats.getInstance().containsIgnoreName(m.getSender())) { if (debug) System.out.println("Ignored: " + m); } else { inqueue.add(m); // add to inqueue if (debug) System.out.println(m.toString()); // System.out.println("Inbuffer: prefix: " + m.prefix + " params: " + m.params + " // trailing:" // + m.trailing + " command:" + m.command + " sender: " + m.sender + "\n " // + "isCTCP:" + m.isCTCP + " isPrivate:" + m.isPrivate + " CTCPCommand:" + // m.CTCPCommand // + " CTCPMessage:" + m.CTCPMessage); } lastActivity = System.currentTimeMillis(); } else { if (System.currentTimeMillis() - lastActivity > 400000) { System.err.println("400 seconds since last activity! Attempting reconnect"); in.close(); oh.disconnect(); keeprunning = false; reconnect(); return; } sleep(100); } } catch (IOException ioe) { System.out.println("EOF on connection: " + ioe.getMessage()); } catch (InterruptedException ie) { System.out.println("Interrupted: " + ie.getMessage()); } catch (Exception e) { System.err.println("Unexpected exception in InputHandler :"); e.printStackTrace(); } } }