Advertisement
globalbus

Untitled

Nov 30th, 2012
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.88 KB | None | 0 0
  1. import org.schwering.irc.lib.*;
  2. import java.io.BufferedReader;
  3. import java.io.InputStreamReader;
  4. import java.io.IOException;
  5. import java.util.Hashtable;
  6.  
  7. /**
  8.  * A simple command line client based on IRClib.
  9.  */
  10. public class IRC extends Thread {
  11.  
  12.   /** Reads input from the console. */
  13.   private BufferedReader in;
  14.  
  15.   /** The IRC connection. */
  16.   private IRCConnection conn;
  17.  
  18.   /** The current default target of PRIVMSGs (a channel or nickname). */
  19.   private String target;
  20.  
  21.   /**
  22.    * Parses the arguments and starts the client.
  23.    */
  24.   public static void main(String[] args) {
  25.     Hashtable ht = null;
  26.     try {
  27.       ht = getHashtable(args);
  28.     } catch (IllegalArgumentException exc) {
  29.       printHelp();
  30.       return;
  31.     }
  32.     String host = (String)ht.get("host");
  33.     int port = new Integer((String)ht.get("port")).intValue();
  34.     String pass = (String)ht.get("pass");
  35.     String nick = (String)ht.get("nick");
  36.     String user = (String)ht.get("user");
  37.     String name = (String)ht.get("name");
  38.     boolean ssl = ((Boolean)ht.get("ssl")).booleanValue();
  39.     try {
  40.       new IRC(host, port, pass, nick, user, name, ssl);
  41.     } catch (IOException exc) {
  42.       printHelp();
  43.     }
  44.   }
  45.  
  46.   /**
  47.    * Returns a hashtable with settings like host, port, nick etc..
  48.    */
  49.   private static Hashtable getHashtable(String[] args) {
  50.     Hashtable ht = new Hashtable();
  51.     String serverPort = (String)getParam(args, "server");
  52.     int colon = serverPort.indexOf(':');
  53.     ht.put("host", serverPort.substring(0, colon));
  54.     ht.put("port", serverPort.substring(colon + 1));
  55.     ht.put("pass", getParam(args, "pass", ""));
  56.     ht.put("nick", getParam(args, "nick"));
  57.     ht.put("user", getParam(args, "user", ht.get("nick")));
  58.     ht.put("name", getParam(args, "name", ht.get("user")));
  59.     ht.put("ssl", getParam(args, "ssl", new Boolean(false)));
  60.     return ht;
  61.   }
  62.  
  63.   /**
  64.    * Returns a value of a key in the arguments.
  65.    */
  66.   private static Object getParam(String[] args, Object key) {
  67.     return getParam(args, key, null);
  68.   }
  69.  
  70.   /**
  71.    * Returns a value of a key in the arguments. If a key without a value is
  72.    * found, a Boolean object with true is returned. If no key is found, the
  73.    * default value is returned.
  74.    */
  75.   private static Object getParam(String[] args, Object key, Object def) {
  76.     for (int i = 0; i < args.length; i++) {
  77.       if (args[i].equalsIgnoreCase("-"+ key)) {
  78.         if (i + 1 < args.length) {
  79.           String value = args[i + 1];
  80.           if (value.charAt(0) == '-')
  81.             return new Boolean(true);
  82.           else
  83.             return value;
  84.         } else {
  85.           return new Boolean(true);
  86.         }
  87.       }
  88.     }
  89.     if (def != null)
  90.       return def;
  91.     else
  92.       throw new IllegalArgumentException("No value for "+ key +" found.");
  93.   }
  94.  
  95.   /**
  96.    * Prints some help.
  97.    */
  98.   private static void printHelp() {
  99.     print("A simple command-line IRC client based on IRClib.");
  100.     print("");
  101.     print("Use it as follows:");
  102.     print("java IRC -server <server:port> [-pass <server-password<] -nick "+
  103.         "<nickname> [-user <username>] [-name <realname>] [-ssl]");
  104.     print("");
  105.     print("Note that you need the IRClib classes in your classpath.");
  106.     print("You can get IRClib from http://moepii.sourceforge.net.");
  107.     print("");
  108.     print("Copyright (C) 2003, 2004, 2005, 2006 Christoph Schwering");
  109.   }
  110.  
  111.   /**
  112.    * A shorthand for the System.out.println method.
  113.    */
  114.   private static void print(Object o) {
  115.     System.out.println(o);
  116.   }
  117.  
  118.   /**
  119.    * Checks wether a string starts with another string (case insensitive).
  120.    */
  121.   private static boolean startsWith(String s1, String s2) {
  122.     return (s1.length() >= s2.length()) ?
  123.         s1.substring(0, s2.length()).equalsIgnoreCase(s2) : false;
  124.   }
  125.  
  126.   /**
  127.    * Creates a new IRCConnection instance and starts the thread.
  128.    *
  129.    * If you get confused by the two setDaemon()s: The conn.setDaemon(false) marks the
  130.    * IRCConnection thread as user thread and the setDaemon(true) marks this class's thread
  131.    * (which just listens for keyboard input) as daemon thread. Thus, if the IRCConnection
  132.    * breaks, this console application shuts down, because due to the setDaemon(true) it
  133.    * will no longer wait for keyboard input (no input would make sense without being
  134.    * connected to a server).
  135.    */
  136.   public IRC(String host, int port, String pass, String nick, String user,
  137.       String name, boolean ssl) throws IOException {
  138.     in = new BufferedReader(new InputStreamReader(System.in));
  139.     if (!ssl) {
  140.       conn = new IRCConnection(host, new int[] { port }, pass, nick, user,
  141.           name);
  142.     } else {
  143.       conn = new SSLIRCConnection(host, new int[] { port }, pass, nick, user,
  144.           name);
  145.       ((SSLIRCConnection)conn).addTrustManager(new SSLDefaultTrustManager());
  146.     }
  147.     conn.addIRCEventListener(new Listener());
  148.     conn.setPong(true);
  149.     conn.setDaemon(false);
  150.     conn.setColors(false);
  151.     conn.connect();
  152.     setDaemon(true);
  153.     start();
  154.   }
  155.  
  156.   /**
  157.    * The thread waits for input.
  158.    */
  159.   public void run() {
  160.     while (true) {
  161.       try {
  162.         shipInput();
  163.       } catch (Exception exc) {
  164.         exc.printStackTrace();
  165.       }
  166.     }
  167.   }
  168.  
  169.   /**
  170.    * Parses the input and sends it to the IRC server.
  171.    */
  172.   public void shipInput() throws Exception {
  173.     String input = in.readLine();
  174.     if (input == null || input.length() == 0)
  175.       return;
  176.    
  177.     if (input.charAt(0) == '/') {
  178.       if (startsWith(input, "/TARGET")) {
  179.         target = input.substring(8);
  180.         return;
  181.       } else if (startsWith(input, "/JOIN")) {
  182.         target = input.substring(6);
  183.       }
  184.       input = input.substring(1);
  185.       print("Exec: "+ input);
  186.       conn.send(input);
  187.     } else {
  188.       conn.doPrivmsg(target, input);
  189.       print(target +"> "+ input);
  190.     }
  191.   }
  192.  
  193.   /**
  194.    * Treats IRC events. The most of them are just printed.
  195.    */
  196.   public class Listener extends IRCEventAdapter implements IRCEventListener {
  197.  
  198.     public void onRegistered() {
  199.       print("Connected");
  200.     }
  201.    
  202.     public void onDisconnected() {
  203.       print("Disconnected");
  204.     }
  205.  
  206.     public void onError(String msg) {
  207.       print("Error: "+ msg);
  208.     }
  209.    
  210.     public void onError(int num, String msg) {
  211.       print("Error #"+ num +": "+ msg);
  212.     }
  213.  
  214.     public void onInvite(String chan, IRCUser u, String nickPass) {
  215.       print(chan +"> "+ u.getNick() +" invites "+ nickPass);
  216.     }
  217.  
  218.     public void onJoin(String chan, IRCUser u) {
  219.       print(chan +"> "+ u.getNick() +" joins");
  220.     }
  221.    
  222.     public void onKick(String chan, IRCUser u, String nickPass, String msg) {
  223.       print(chan +"> "+ u.getNick() +" kicks "+ nickPass);
  224.     }
  225.  
  226.     public void onMode(IRCUser u, String nickPass, String mode) {
  227.       print("Mode: "+ u.getNick() +" sets modes "+ mode +" "+
  228.           nickPass);
  229.     }
  230.  
  231.     public void onMode(IRCUser u, String chan, IRCModeParser mp) {
  232.       print(chan +"> "+ u.getNick() +" sets mode: "+ mp.getLine());
  233.     }
  234.  
  235.     public void onNick(IRCUser u, String nickNew) {
  236.       print("Nick: "+ u.getNick() +" is now known as "+ nickNew);
  237.     }
  238.  
  239.     public void onNotice(String target, IRCUser u, String msg) {
  240.       print(target +"> "+ u.getNick() +" (notice): "+ msg);
  241.     }
  242.  
  243.     public void onPart(String chan, IRCUser u, String msg) {
  244.       print(chan +"> "+ u.getNick() +" parts");
  245.     }
  246.    
  247.     public void onPrivmsg(String chan, IRCUser u, String msg) {
  248.       print(chan +"> "+ u.getNick() +": "+ msg);
  249.     }
  250.  
  251.     public void onQuit(IRCUser u, String msg) {
  252.       print("Quit: "+ u.getNick());
  253.     }
  254.  
  255.     public void onReply(int num, String value, String msg) {
  256.       print("Reply #"+ num +": "+ value +" "+ msg);
  257.     }
  258.  
  259.     public void onTopic(String chan, IRCUser u, String topic) {
  260.       print(chan +"> "+ u.getNick() +" changes topic into: "+ topic);
  261.     }
  262.  
  263.   }
  264.  
  265. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement