1. package ircbotgui;
  2.  
  3. import java.io.FileNotFoundException;
  4. import java.io.IOException;
  5. import java.lang.reflect.InvocationTargetException;
  6. import java.util.logging.Level;
  7. import java.util.logging.Logger;
  8. import java.util.List;
  9. import java.io.BufferedReader;
  10. import java.io.InputStreamReader;
  11. import java.io.FileInputStream;
  12. import java.io.FileWriter;
  13. import java.io.BufferedWriter;
  14. import java.lang.reflect.Method;
  15. import java.util.Random;
  16.  
  17. /**
  18.  * @author Malte
  19.  */
  20. public class BorBot extends BaseBot{
  21.  
  22.     //Password is *snip*
  23.  
  24.     public boolean fooneticBotFlag = false;                                     //Set mode +b on foonetic?
  25.     public boolean borBotIdent = false;                                         //Identified if using nick BorBot?
  26.  
  27.     public String version = "v0.3";                                             //Current version
  28.     public String owner = "Boreeas";                                            //Whoose bot it is
  29.     public String pw = "*snip*";                                                //Password
  30.  
  31.     public String prefix = "-";                                                 //Command prefix: -time
  32.     public String prefixPriv = ">";                                             //Command prefix 2 for privately send commands: ->Boreeas time
  33.  
  34.     private String[] sendable = {"random"};                                     //Sendable Commands
  35.     private String[] unsendable = {"addrandom"};                                //Unsendable Commands
  36.  
  37.                                                                                
  38.     private boolean isPrivate = false;                                          //Will the command be sent to user or channel
  39.     private String[] cmdArgs;                                                   //Command and arguments
  40.  
  41.     public String user, channel, lastSent;                                      //Connecction control variables
  42.  
  43.     String t = "\t", n = "\n", r = "\r", rn = "\r\n";                           //tabs, newlines
  44.  
  45.     List rand;
  46.  
  47.     Random random = new Random();
  48.     BufferedReader rd;
  49.     BufferedWriter w;
  50.  
  51.     public BorBot() {
  52.         try {
  53.             rd = new BufferedReader(new InputStreamReader(new FileInputStream(System.getProperty("user.dir") + "/src/ircbotgui/rand.txt")));
  54.             w = new BufferedWriter(new FileWriter(System.getProperty("user.dir") + "/src/ircbotgui/rand.txt"));
  55.  
  56.             while (true) {
  57.                 String line;
  58.                 if ((line = rd.readLine()) != null) {
  59.  
  60.                     rand.add(line);
  61.                 }
  62.                 else {
  63.                     break;
  64.                 }
  65.             }
  66.         } catch (FileNotFoundException ex) {
  67.             gui.logError(ex);
  68.         } catch (java.io.IOException ioe) {
  69.             gui.logError(ioe);
  70.         }
  71.     }
  72.    
  73.     @Override
  74.     public void read(){
  75.  
  76.         try {
  77.  
  78.             while (true) {
  79.  
  80.                 String line;
  81.  
  82.                 if ((line = reader.readLine()) != null) {
  83.  
  84.                     gui.appendRaw(line);
  85.                     String[] results = parse(line);                             //0 = Sender, 1 = Message [, 2 = Channel]
  86.  
  87.                     updateInformation(results);
  88.                     checkForServerCommands(results);
  89.                    
  90.                     if (results[1].startsWith(prefix)) {
  91.                        
  92.                         if (results[1].startsWith(prefixPriv, 1)) {
  93.                            
  94.                             results[1] = results[1].substring(2);
  95.                             checkForSendableCommands(results, true);
  96.                         }
  97.                         else {
  98.                             results[1] = results[1].substring(1);
  99.                             checkForUnsendableCommands(results);
  100.                             checkForSendableCommands(results, false);
  101.                    
  102.                         }
  103.                     }
  104.                    
  105.                 }
  106.                 else{
  107.                     break;
  108.                 }
  109.  
  110.                 writer.flush();
  111.             }
  112.         } catch (IOException e) {
  113.  
  114.             gui.logError(e);
  115.         }
  116.     }
  117.  
  118.  
  119.     @Override
  120.     //Append messages, etc
  121.     public void updateInformation(String[] args) throws IOException{
  122.  
  123.         //Append to Server
  124.         if(args[0].equalsIgnoreCase("PING")) {
  125.             gui.appendServer("PING:" + args[1] + rn + "PONG:" + args[1]);
  126.         }
  127.  
  128.         else if(args[0].substring(args[0].indexOf(".") + 1).equalsIgnoreCase(server.substring(server.indexOf(".") + 1))) {
  129.  
  130.             gui.appendServer(args[0]+":"+t+t+args[1]);
  131.         }
  132.  
  133.  
  134.         //Append to channel, update control variables
  135.         else if(args.length > 2) {
  136.             user = args[0];
  137.             channel = args[2];
  138.             lastSent = args[1];
  139.             gui.appendChan("[" + gui.getTime("mm:ss") + "][" + channel + "] " + user + ": \t\t"+ lastSent);
  140.         }
  141.     }
  142.  
  143.  
  144.     @Override
  145.     //Commands not issued by persons, e.g. PING
  146.     public void checkForServerCommands(String[] args) throws IOException{
  147.  
  148.         //PING
  149.         if(args[0].equalsIgnoreCase("PING")) {
  150.  
  151.             writer.write("PONG :"+args[1]+rn);
  152.         }
  153.  
  154.         //Foonetic Bot Flag
  155.         if(args[0].contains("foonetic")) {          //Foonetic asks for a +B flag to indicate Bots
  156.  
  157.             if (!fooneticBotFlag) {
  158.                 gui.appendSys("Setting mode +b");
  159.                 fooneticBotFlag = true;
  160.             }
  161.         }
  162.        
  163.     }
  164.  
  165.  
  166.     @Override
  167.     //Commands that don' require a reply, e.g. disconnect
  168.     public void checkForUnsendableCommands(String[] args) throws IOException{
  169.  
  170.         cmdArgs = args[1].split(" ", 1);                        //0 = recipient, 1 = command 2+ = args. If not priv, recipient does not appear, all indices -1
  171.         String cmd = cmdArgs[0];
  172.  
  173.         boolean inList = false;
  174.         for (int i = 0; i < unsendable.length; i++) {
  175.  
  176.             if (unsendable[i].equalsIgnoreCase(cmd)) {
  177.  
  178.                 inList = true;
  179.             }
  180.         }
  181.  
  182.         Method[] m = BorBot.class.getMethods();
  183.  
  184.         for (int i = 0; i < m.length; i++) {
  185.  
  186.             if (m[i].getName().equalsIgnoreCase(cmd) && inList) {
  187.                 try {
  188.                     m[i].invoke(this);
  189.                 } catch (IllegalAccessException ex) {
  190.                     gui.logError(ex);
  191.                 } catch (InvocationTargetException ex) {
  192.                     gui.logError(ex);
  193.                 } catch (IllegalArgumentException ex) {
  194.                     gui.logError(ex);
  195.                 }
  196.             }
  197.         }
  198.     }
  199.  
  200.  
  201.     @Override
  202.     //Commands that require a reply, e.g. time
  203.     public void checkForSendableCommands(String[] args, boolean priv) throws IOException{
  204.  
  205.         isPrivate = priv;
  206.         int index = 1;
  207.  
  208.         if (priv) {
  209.             index += 1;
  210.         }
  211.  
  212.         cmdArgs = args[1].split(" ", index);                        //0 = recipient, 1 = command 2+ = args. If not priv, recipient does not appear, all indices -1
  213.         String cmd = cmdArgs[index - 1];
  214.  
  215.         boolean inList = false;
  216.         for (int i = 0; i < sendable.length; i++) {
  217.  
  218.             if (sendable[i].equalsIgnoreCase(cmd)) {
  219.  
  220.                 inList = true;
  221.             }
  222.         }
  223.  
  224.         Method[] m = BorBot.class.getMethods();
  225.  
  226.         for (int i = 0; i < m.length; i++) {
  227.  
  228.             if (m[i].getName().equalsIgnoreCase(cmd) && inList) {
  229.                 try {
  230.                     gui.appendParseDebug("Invoking command: " + cmd);
  231.  
  232.                     m[i].invoke(this);
  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.getTargetException());
  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() {
  320.  
  321.         System.out.println("Command random triggered");
  322.         String message = (String)rand.get(random.nextInt(rand.size()));
  323.  
  324.         if (isPrivate) {
  325.  
  326.             say(message, (cmdArgs[1]));
  327.         }
  328.         else {
  329.  
  330.             say(message, channel);
  331.         }
  332.     }
  333.  
  334.     public void addrandom() {
  335.  
  336.         try {
  337.             w.write(cmdArgs[1] + n);
  338.             w.flush();
  339.         } catch (IOException ex) {
  340.             gui.logError(ex);
  341.         }
  342.     }
  343.  
  344. }