public synchronized boolean connect() { try { connection = new Socket((String) servers.get(currentServer), port); connection.setSoTimeout(10000); receiver = new BufferedReader(new InputStreamReader(connection.getInputStream(), encoding)); sender = new BufferedWriter(new OutputStreamWriter(connection.getOutputStream(), encoding)); sender.write(irc.nick(myNick)); sender.write(irc.user(myNick, mode, realname)); sender.flush(); } catch (SocketTimeoutException e) { log.error("socket timeout while connecting", e); sendErrorEvent("Bot.connect", "SocketTimeoutException", e.getMessage()); return false; } catch (IOException e) { log.error("error while connecting", e); sendErrorEvent("Bot.connect", "IOException", e.getMessage()); return false; } return true; }
public List<IIrcMessage> receive() throws SocketTimeoutException, IOException { List<IIrcMessage> output; if (receiver.ready() == true) { String input = receiver.readLine(); output = irc.parse(input, myNick); return output; } else { output = null; return output; } }
public void respondToIrcEvent(IIrcEvent event) { log.info("Responding to event " + event); IIrcMessage command = event.getIRCMessage(); if (command.getCommand().equalsIgnoreCase("PING")) { consecutiveErrors = 0; log.info("Sending pong"); String self = hostname; if (command.getEscapedParams() != null && command.getEscapedParams().length() > 0) self = command.getEscapedParams(); enqueueCommand(irc.pong(self)); } else if (command.getCommand().equalsIgnoreCase("ERROR")) { consecutiveErrors++; if (consecutiveErrors > 1) { nextServer(); } reconnect("IRC Error!"); } else if (command.getCommand().equals("433")) { // nick collision log.info("Nick collision. Cycling nicks"); currentNick++; if (currentNick == nicks.size()) { currentNick = 0; } myNick = (String) nicks.get(currentNick); enqueueCommand(irc.nick(myNick)); log.info("(Re)Joining channels"); for (IChannel c : channels) { c.join(); } } else if (command.getCommand().equalsIgnoreCase("CTCP_VERSION")) { enqueueCommand(irc.ctcpVersion(command.getPrefixNick(), description, version, environment)); } else if (command.getCommand().equalsIgnoreCase("CTCP_PING")) { enqueueCommand(irc.ctcpPing(command.getPrefixNick(), command.getEscapedParams())); } else if (command.getCommand().equalsIgnoreCase("CTCP_TIME")) { String dateTime = df.format(new Date(System.currentTimeMillis())); enqueueCommand(irc.ctcpTime(command.getPrefixNick(), dateTime)); } }
public synchronized void disconnect(String reason) throws IOException { log.info("Disconnecting"); for (IChannel c : channels) { c.part(); } log.info("Closing server connection"); sender.write(irc.quit(reason)); sender.flush(); sender.close(); receiver.close(); if (!connection.isClosed()) { connection.close(); } }