Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 6th, 2012  |  syntax: None  |  size: 10.53 KB  |  hits: 21  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2.  
  3. package ircbotgui;
  4.  
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7. import java.lang.reflect.InvocationTargetException;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. import java.util.List;
  11. import java.io.BufferedReader;
  12. import java.io.InputStreamReader;
  13. import java.io.FileInputStream;
  14. import java.io.FileWriter;
  15. import java.io.BufferedWriter;
  16. import java.lang.reflect.Method;
  17. import java.util.Random;
  18. /**
  19.  * @author Malte
  20.  */
  21. public class BorBot extends BaseBot{
  22.  
  23.     //Password is w00H==
  24.  
  25.     public boolean fooneticBotFlag = false;                                     //Set mode +b on foonetic?
  26.     public boolean borBotIdent = false;                                         //Identified if using nick BorBot?
  27.  
  28.     public String version = "v0.3";                                             //Current version
  29.     public String owner = "Boreeas";                                            //Whoose bot it is
  30.     public String pw = "w00H==";                                                //Password
  31.  
  32.     public String prefix = "-";                                                 //Command prefix: -time
  33.     public String prefixPriv = ">";                                             //Command prefix 2 for privately send commands: ->Boreeas time
  34.  
  35.     String[] sendable = {"random"};                                             //Sendable Commands
  36.     String[] unsendable = {"addrandom"};                                        //Unsendable Commands
  37.  
  38.     public String user, channel, lastSent;                                      //Connecction control variables
  39.  
  40.     String t = "\t", n = "\n", r = "\r", rn = "\r\n";                           //tabs, newlines
  41.  
  42.     List rand;
  43.  
  44.     Random random = new Random();
  45.     BufferedReader rd;
  46.     BufferedWriter w;
  47.  
  48.     public BorBot() {
  49.         try {
  50.             rd = new BufferedReader(new InputStreamReader(new FileInputStream(System.getProperty("user.dir") + "/src/ircbotgui/rand.txt")));
  51.             w = new BufferedWriter(new FileWriter(System.getProperty("user.dir") + "/src/ircbotgui/rand.txt"));
  52.  
  53.             while (true) {
  54.                 String line;
  55.                 if ((line = rd.readLine()) != null) {
  56.  
  57.                     rand.add(line);
  58.                 }
  59.                 else {
  60.                     break;
  61.                 }
  62.             }
  63.         } catch (FileNotFoundException ex) {
  64.             gui.logError(ex);
  65.         } catch (java.io.IOException ioe) {
  66.             gui.logError(ioe);
  67.         }
  68.     }
  69.    
  70.     @Override
  71.     public void read(){
  72.  
  73.         try {
  74.  
  75.             while (true) {
  76.  
  77.                 String line;
  78.  
  79.                 if ((line = reader.readLine()) != null) {
  80.  
  81.                     gui.appendRaw(line);
  82.                     String[] results = parse(line);                             //0 = Sender, 1 = Message [, 2 = Channel]
  83.  
  84.                     updateInformation(results);
  85.                     checkForServerCommands(results);
  86.                    
  87.                     if (results[1].startsWith(prefix)) {
  88.                        
  89.                         if (results[1].startsWith(prefixPriv, 1)) {
  90.                            
  91.                             results[1] = results[1].substring(2);
  92.                             checkForSendableCommands(results, true);
  93.                         }
  94.                         else {
  95.                             results[1] = results[1].substring(1);
  96.                             checkForUnsendableCommands(results);
  97.                             checkForSendableCommands(results, false);
  98.                    
  99.                         }
  100.                     }
  101.                    
  102.                 }
  103.                 else{
  104.                     break;
  105.                 }
  106.  
  107.                 writer.flush();
  108.             }
  109.         } catch (IOException e) {
  110.  
  111.             gui.logError(e);
  112.         }
  113.     }
  114.  
  115.  
  116.     @Override
  117.     //Append messages, etc
  118.     public void updateInformation(String[] args) throws IOException{
  119.  
  120.         //Append to Server
  121.         if(args[0].equalsIgnoreCase("PING")) {
  122.             gui.appendServer("PING:" + args[1] + rn + "PONG:" + args[1]);
  123.         }
  124.  
  125.         else if(args[0].substring(args[0].indexOf(".") + 1).equalsIgnoreCase(server.substring(server.indexOf(".") + 1))) {
  126.  
  127.             gui.appendServer(args[0]+":"+t+t+args[1]);
  128.         }
  129.  
  130.  
  131.         //Append to channel, update control variables
  132.         else if(args.length > 2) {
  133.             user = args[0];
  134.             channel = args[2];
  135.             lastSent = args[1];
  136.             gui.appendChan("[" + gui.getTime("mm:ss") + "][" + channel + "] " + user + ": \t\t"+ lastSent);
  137.         }
  138.     }
  139.  
  140.  
  141.     @Override
  142.     //Commands not issued by persons, e.g. PING
  143.     public void checkForServerCommands(String[] args) throws IOException{
  144.  
  145.         //PING
  146.         if(args[0].equalsIgnoreCase("PING")) {
  147.  
  148.             writer.write("PONG :"+args[1]+rn);
  149.         }
  150.  
  151.         //Foonetic Bot Flag
  152.         if(args[0].contains("foonetic")) {          //Foonetic asks for a +B flag to indicate Bots
  153.  
  154.             if (!fooneticBotFlag) {
  155.                 gui.appendSys("Setting mode +b");
  156.                 fooneticBotFlag = true;
  157.             }
  158.         }
  159.        
  160.     }
  161.  
  162.  
  163.     @Override
  164.     //Commands that don' require a reply, e.g. disconnect
  165.     public void checkForUnsendableCommands(String[] args) throws IOException{
  166.  
  167.         String[] cmdAndArgs = args[1].split(" ", 1);                        //0 = recipient, 1 = command 2+ = args. If not priv, recipient does not appear, all indices -1
  168.         String cmd = cmdAndArgs[0];
  169.  
  170.         boolean inList = false;
  171.         for (int i = 0; i < unsendable.length; i++) {
  172.  
  173.             if (unsendable[i].equalsIgnoreCase(cmd)) {
  174.  
  175.                 inList = true;
  176.             }
  177.         }
  178.  
  179.         Method[] m = BorBot.class.getMethods();
  180.  
  181.         for (int i = 0; i < m.length; i++) {
  182.  
  183.             if (m[i].getName().equalsIgnoreCase(cmd) && inList) {
  184.                 try {
  185.                     m[i].invoke(this, cmdAndArgs);
  186.                 } catch (IllegalAccessException ex) {
  187.                     gui.logError(ex);
  188.                 } catch (InvocationTargetException ex) {
  189.                     gui.logError(ex);
  190.                 } catch (IllegalArgumentException ex) {
  191.                     //All sendable commands take these arguments, so the exception is ignored
  192.                 }
  193.             }
  194.         }
  195.     }
  196.  
  197.  
  198.     @Override
  199.     //Commands that require a reply, e.g. time
  200.     public void checkForSendableCommands(String[] args, boolean priv) throws IOException{
  201.  
  202.         int index = 1;
  203.  
  204.         if (priv) {
  205.             index += 1;
  206.         }
  207.  
  208.         String[] cmdAndArgs = args[1].split(" ", index);                        //0 = recipient, 1 = command 2+ = args. If not priv, recipient does not appear, all indices -1
  209.         String cmd = cmdAndArgs[index - 1];
  210.  
  211.         boolean inList = false;
  212.         for (int i = 0; i < sendable.length; i++) {
  213.  
  214.             if (sendable[i].equalsIgnoreCase(cmd)) {
  215.  
  216.                 inList = true;
  217.             }
  218.         }
  219.  
  220.         Method[] m = BorBot.class.getMethods();
  221.  
  222.         for (int i = 0; i < m.length; i++) {
  223.  
  224.             if (m[i].getName().equalsIgnoreCase(cmd) && inList) {
  225.                 try {
  226.                     gui.appendParseDebug("Invoking command: " + cmd);
  227.  
  228.                     Object[] arglist = new Object[2];
  229.                     arglist[0] = cmdAndArgs;
  230.                     arglist[1] = priv;
  231.  
  232.                     m[i].invoke(this, arglist);
  233.                    
  234.                     System.out.println("Invoking succesful");
  235.                 } catch (IllegalAccessException ex) {
  236.                     System.out.println("Got exception Access");
  237.                     gui.logError(ex);
  238.                 } catch (InvocationTargetException ex) {
  239.                     System.out.println("Got exception Invocation Target");
  240.                     System.out.println(ex.getCause());
  241.                     gui.logError(ex);
  242.                 } catch (IllegalArgumentException ex) {
  243.                     ex.printStackTrace();
  244.                 }
  245.             }
  246.         }
  247.     }
  248.  
  249.    
  250.  
  251.     @Override
  252.     public String[] parse(String line) {
  253.  
  254.         //Remove leading ":"
  255.         if(line.startsWith(":")) {
  256.             line = line.substring(1);
  257.         }
  258.  
  259.  
  260.         //This can either be PING, the server, or the user
  261.         //If it contains an '@', it is a user - user will be parsed
  262.         String userLoc = line.substring(0, line.indexOf(" "));
  263.         if (userLoc.contains("@")) {
  264.             userLoc = userLoc.substring(0, userLoc.indexOf("!"));
  265.  
  266.         }
  267.  
  268.  
  269.         //Channel will be parsed by the '#', and only if the line does not start with a server name
  270.         //JOINS/PARTS will be caught and ignored as well as a few lines from the server that would lead to an exception
  271.         String channel = null;
  272.         try {
  273.             String garbage = line.substring(0, line.indexOf(":"));
  274.             if (line.contains("#") && !(line.substring(line.indexOf(".")).startsWith(server.substring(server.indexOf("."))))) {
  275.  
  276.                 int index = line.indexOf("#");
  277.                 String tempStr = line.substring(index);
  278.                 gui.appendParseDebug("Got temporary channel string: " + tempStr);
  279.                 channel = tempStr.substring(0, tempStr.indexOf(" "));
  280.             }
  281.         } catch (StringIndexOutOfBoundsException oobe) {
  282.         }
  283.  
  284.  
  285.         //This is the actuall message, starting after the ':' in the line
  286.         String message = line.substring(line.indexOf(":") + 1).trim();
  287.  
  288.         //Since there might be no channel, channel cannot be appended
  289.         String parseOut = "User: " + userLoc + t + t + "Message: " + message;
  290.         gui.appendParseDebug(parseOut);
  291.  
  292.         //Returning the variables
  293.         String[] temp;
  294.         if (channel != null) {
  295.  
  296.             String[] temp2 = {userLoc, message, channel};
  297.             temp = temp2.clone();
  298.         }
  299.         else {
  300.             String[] temp2 = {userLoc, message};
  301.             temp = temp2.clone();
  302.         }
  303.        
  304.         return temp;
  305.     }
  306.  
  307.  
  308.     public void say(String message, String recipient) {
  309.  
  310.         try {
  311.             writer.write("PRIVMSG " + recipient + " :" + message + rn);
  312.             writer.flush();
  313.         } catch (IOException ex) {
  314.             gui.logError(ex);
  315.         }
  316.        
  317.     }
  318.  
  319.     public void random(Object[] args) {
  320.  
  321.         System.out.println("Command random triggered");
  322.         String message = (String)rand.get(random.nextInt(rand.size()));
  323.  
  324.         if ((Boolean)args[1]) {
  325.  
  326.             say(message, ((String[])args[0])[0]);
  327.         }
  328.         else {
  329.  
  330.             say(message, channel);
  331.         }
  332.     }
  333.  
  334.     public void addrandom(String args[]) {
  335.  
  336.         try {
  337.             w.write(args[1] + n);
  338.             w.flush();
  339.         } catch (IOException ex) {
  340.             gui.logError(ex);
  341.         }
  342.     }
  343.  
  344. }