/** * 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."); }
/** * 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); } } }