/** * Waits for a client to connect to the ident server before making an appropriate response. Note * that this method is started by the class constructor. */ public void run() { try { Socket socket = ss.accept(); socket.setSoTimeout(60000); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())); String line = reader.readLine(); if (line != null) { bot.log("*** Ident request received: " + line); line = line + " : USERID : UNIX : " + login; writer.write(line + "\r\n"); writer.flush(); bot.log("*** Ident reply sent: " + line); writer.close(); } } catch (Exception e) { // We're not really concerned with what went wrong, are we? } try { ss.close(); } catch (Exception e) { // Doesn't really matter... } bot.log("*** The Ident server has been shut down."); }
/** * This method starts the Thread consuming from the outgoing message Queue and sending lines to * the server. */ @Override public void run() { try { while (true) { String line = queue.take(); failIfNotConnected(); if (line != null && bot.isConnected()) sendRawLineNow(line); // Small delay to prevent spamming of the channel Thread.sleep(bot.getMessageDelay()); } } catch (InterruptedException e) { // Just let the method return naturally... } }
/** * Constructs and starts an instance of an IdentServer that will respond to a client with the * provided login. Rather than calling this constructor explicitly from your code, it is * recommended that you use the startIdentServer method in the PircBotX class. * * <p>The ident server will wait for up to 60 seconds before shutting down. Otherwise, it will * shut down as soon as it has responded to an ident request. * * @param bot The PircBotX instance that will be used to log to. * @param login The login that the ident server will respond with. */ IdentServer(PircBotX bot, String login) { this.bot = bot; this.login = login; try { ss = new ServerSocket(113); ss.setSoTimeout(60000); } catch (Exception e) { bot.log("*** Could not start the ident server on port 113."); return; } bot.log("*** Ident server running on port 113 for the next 60 seconds..."); this.setName(this.getClass() + "-Thread"); }
/** * A static method to write a line to a BufferedOutputStream and then pass the line to the log * method of the supplied PircBotX instance. * * @param line The line to be written. "\r\n" is appended to the end. */ public void sendRawLineNow(String line) { if (line.length() > bot.getMaxLineLength() - 2) line = line.substring(0, bot.getMaxLineLength() - 2); synchronized (bwriter) { failIfNotConnected(); try { bwriter.write(line + "\r\n"); bwriter.flush(); bot.log(">>>" + line); } catch (Exception e) { // Not much else we can do, but this requires attention of whatever is calling this throw new RuntimeException("Exception encountered when writing to socket", e); } } }
public static void addBotToMDC(PircBotX bot) { MDC.put("pircbotx.id", String.valueOf(bot.getBotId())); MDC.put( "pircbotx.connectionId", bot.getServerHostname() + "-" + bot.getBotId() + "-" + bot.getConnectionId()); MDC.put("pircbotx.server", StringUtils.defaultString(bot.getServerHostname())); MDC.put("pircbotx.port", String.valueOf(bot.getServerPort())); }
@Override protected void shutdown() { // TODO: Fragile if (shutdownEnabled) super.shutdown(); else log.warn("Shutdown called"); }
@Override public void close() { super.close(); closeCalled = true; }
@Override protected void changeSocket(Socket socket) throws IOException { super.changeSocket(socket); this.inputReader = in; }
/** * Sets bot as identified to nickserv. Needed so {@link PircBotX#setNickservIdentified(boolean) } * can stay protected * * @param bot */ public static void setNickServIdentified(PircBotX bot) { bot.setNickservIdentified(true); }
/** * Sends a raw line to the server. Needed so {@link PircBotX#sendRawLineToServer(java.lang.String) * } can stay protected but still be callable from the org.pircbotx.output package * * @param bot The bot that sends the raw line * @param rawLine The raw line to send */ public static void sendRawLineToServer(PircBotX bot, String rawLine) throws IOException { bot.sendRawLineToServer(rawLine); }
@SuppressWarnings("unchecked") public static void dispatchEvent(PircBotX bot, Event event) { bot.getConfiguration().getListenerManager().onEvent(event); }
protected void failIfNotConnected() throws RuntimeException { if (!bot.isConnected()) throw new RuntimeException("Trying to send message when no longer connected"); }