Advertisement
jared314

Untitled

Feb 14th, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.27 KB | None | 0 0
  1. import java.util.Random;
  2. import java.io.*;
  3. import java.net.*;
  4. import java.util.ArrayList;
  5. import org.jibble.pircbot.*;
  6.  
  7. public class Gil extends PircBot {
  8.  
  9.     //random number object
  10.     Random rand = new Random();
  11.    
  12.     //the channel the current message was received from
  13.     String channel;
  14.     //sender of current message
  15.     String sender;
  16.     //array of words in message
  17.     String[] msgArray;
  18.    
  19.     //number of messages on channel sense bot last spoke
  20.     int inactivity = 0;
  21.     //delay in messages before bot looks for opportunity to talk
  22.     int actionDelay = 5;
  23.    
  24.     //ArrayList to store message to send to other users
  25.     ArrayList<Outbox> msgList = new ArrayList<Outbox>();
  26.    
  27.     //constructor
  28.     public Gil() {
  29.         this.setName("tom");
  30.     }
  31.    
  32.     //New message on channel
  33.     public void onMessage(String channel, String sender, String login, String hostname, String message){
  34.         inactivity++;
  35.         this.sender = sender;
  36.         this.channel = channel;
  37.        
  38.         //split msg into words and place in an array
  39.         msgArray = message.split("\\s+");
  40.        
  41.         //if message came from game server, remove <name> from message and edit sender to match
  42.         formatInGameMsg();
  43.        
  44.         //check if sender has a message saved
  45.         checkForMsg();
  46.        
  47.         //check for tell command
  48.         tell();
  49.        
  50.         if(inactivity > 0)
  51.         {
  52.             //if message contains bot name,
  53.             //look for direct replies
  54.             if(checkForName())
  55.                 {
  56.                     try {
  57.                         searchDirectReplies();
  58.                     } catch (IOException e) {
  59.                         // TODO Auto-generated catch block
  60.                         e.printStackTrace();
  61.                     } catch (InterruptedException e) {
  62.                         // TODO Auto-generated catch block
  63.                         e.printStackTrace();
  64.                     }
  65.                 }
  66.             //search for actions related to message
  67.             searchActions();
  68.         }
  69.     }//end onMessage
  70.    
  71.     //check for use of bot name in message
  72.     public boolean checkForName() {
  73.         String[] names = {this.getName(),
  74.                             this.getName()+"?",
  75.                             this.getName()+":",
  76.                             this.getName()+",",
  77.                             "\""+this.getName()+"\"",
  78.                             this.getName()+"!",
  79.                             this.getName()+"."};
  80.         if(matchWords(names))
  81.         {
  82.             return true;
  83.         } else {
  84.             return false;
  85.         }
  86.     }//end check method
  87.    
  88.     //match single words in message from words[] array
  89.     public boolean matchWords(String[] words)
  90.     {
  91.         for(int x=0;x<msgArray.length;x++) {
  92.             for(int y=0;y<words.length;y++){
  93.                 if(msgArray[x].equalsIgnoreCase(words[y]))
  94.                 {
  95.                     return true;
  96.                 }//end if
  97.             }//end nested for
  98.         }//end for
  99.         return false;
  100.     }//end matchWords
  101.    
  102.     //match pairs of words in message from pairs[] array
  103.     public boolean matchPairs(String[][] pairs)
  104.     {
  105.         for(int x=0;x<(msgArray.length);x++) {
  106.             for(int y=0;y<pairs.length;y++){
  107.                 if(msgArray[x].equalsIgnoreCase(pairs[y][0]) && msgArray[x+1].equalsIgnoreCase(pairs[y][1]))
  108.                 {
  109.                     return true;
  110.                 }
  111.             }//end nested for
  112.         }//end for
  113.         return false;
  114.     }//end match pairs
  115.  
  116.     //reply methods
  117.      public void randReply(String[] response) {
  118.             int randomNum = rand.nextInt(response.length);
  119.             String reply = response[randomNum];
  120.            
  121.             try {
  122.                 Thread.sleep(replyDelay(reply));
  123.             } catch(InterruptedException ex) {
  124.                 Thread.currentThread().interrupt();
  125.             }
  126.            
  127.             sendMessage(channel, reply);
  128.             inactivity = 0;
  129.      }//end reply
  130.      
  131.      public void Reply(String response) {
  132.          try {
  133.              Thread.sleep(replyDelay(response));
  134.          } catch(InterruptedException ex) {
  135.              Thread.currentThread().interrupt();
  136.          }
  137.          
  138.             sendMessage(channel, response);
  139.             inactivity = 0;
  140.      }//end reply
  141.      
  142.      public long replyDelay(String reply)
  143.      {
  144.          long delay;
  145.          int length = reply.length();
  146.          
  147.          if(length < 6)
  148.              delay = 2000;
  149.          else if(length < 12)
  150.              delay = 3000;
  151.          else if(length < 22)
  152.              delay = 4000;
  153.          else
  154.              delay = 5000;
  155.          
  156.          return delay;
  157.      }
  158.      
  159.      
  160.      
  161.      //-------------------------------------//
  162.      //                                     //
  163.      //     Responses when bot name used    //
  164.      //                                     //
  165.      //-------------------------------------//
  166.      
  167.      public void searchDirectReplies() throws IOException, InterruptedException
  168.      {
  169.          hello();
  170.          leave();
  171.          whatsUp();
  172.          changeName();
  173.          who();
  174.          changeDelay();
  175.          thanks();
  176.          why();
  177.          ping();
  178.          shutUp();
  179.          tellHelp();
  180.          pageAdmin();
  181.          
  182.          //if bot name is used and no other criteria are met
  183.          if(inactivity > actionDelay)
  184.              Reply("What?");
  185.      }
  186.      
  187.      public void hello() {
  188.             String words[] = {"hello",
  189.                                 "hi",
  190.                                 "welcome",
  191.                                 "hey",
  192.                                 "o/"};
  193.             String response[] = {"hello",
  194.                                 "hi",
  195.                                 "hey",
  196.                                 "Salutations",
  197.                                 "o/"};
  198.            
  199.             if(matchWords(words))
  200.             {
  201.                 randReply(response);
  202.             }
  203.         }//end hello
  204.        
  205.      public void whatsUp() {
  206.             String words[] = {"sup"};
  207.             String pairs[][] = {{"whats", "up"},
  208.                                 {"what", "up"},
  209.                                 {"what's", "up"}};
  210.             String response[] = {"I'm fine",
  211.                                 "I wish I had a brain...",
  212.                                 "punching trees",
  213.                                 "hanging out in Jared's RAM"};
  214.            
  215.             if(matchWords(words) || matchPairs(pairs))
  216.             {
  217.                 randReply(response);
  218.             }
  219.         }//end whats up
  220.      
  221.      public void who() {
  222.             String words[] = {"who",
  223.                                 "whose",
  224.                                 "whos",
  225.                                 "who's"};
  226.             String pairs[][] = {{"who", "is"},
  227.                                 {"who", "are"}};
  228.             String response[] = {"I'm "+this.getName(),
  229.                                 "I'm "+sender,
  230.                                 "who are you?",
  231.                                 "what's it to you?"};
  232.            
  233.             if(matchWords(words) || matchPairs(pairs))
  234.             {
  235.                 randReply(response);
  236.             }
  237.         }//end who
  238.      
  239.         public void leave() {
  240.             String words[] = {"exit",
  241.                             "leave",
  242.                             "stop",
  243.                             "bye",
  244.                             "quit"};
  245.             String pairs[][] = {{"fuck", "off"},
  246.                                 {"go", "away"}};
  247.             String response[] = {"fine",
  248.                                 "I'll be back",
  249.                                 "sure thing eh",
  250.                                 "disconnecting..."};
  251.             if(matchWords(words) || matchPairs(pairs))
  252.             {
  253.                 randReply(response);
  254.                 try {
  255.                     Thread.sleep(1000);
  256.                 } catch(InterruptedException ex) {
  257.                     Thread.currentThread().interrupt();
  258.                 }
  259.                 disconnect();
  260.             }
  261.         }//end leave
  262.        
  263.         public void thanks() {
  264.             String words[] = {"thank",
  265.                                 "thanks",
  266.                                 "lol",
  267.                                 "haha"};
  268.             String response[] = {"np",
  269.                                 "anytime",
  270.                                 "your welcome"};
  271.             if(matchWords(words))
  272.             {
  273.                 randReply(response);
  274.             }
  275.         }//end thanks
  276.        
  277.         public void why() {
  278.             String words[] = {"why"};
  279.             String response[] = {"because",
  280.                                 "that's how it is"};
  281.             if(matchWords(words))
  282.             {
  283.                 randReply(response);
  284.             }
  285.         }//end why
  286.        
  287.         public void changeName() {
  288.             String pairs[][] = {{"change", "name"}};
  289.             String temp = "I was pretty fond of " + this.getName() + "...";
  290.             String response[] = {temp,
  291.                                 "sure thing eh"};
  292.             if(matchPairs(pairs))
  293.             {
  294.                 randReply(response);
  295.                
  296.                 try {
  297.                     Thread.sleep(1000);
  298.                 } catch(InterruptedException ex) {
  299.                     Thread.currentThread().interrupt();
  300.                 }
  301.                
  302.                 String newName = msgArray[msgArray.length - 1];//last word in message
  303.                
  304.                 changeNick(newName);
  305.                 this.setName(newName);
  306.             }
  307.         }//end name change
  308.      
  309.         public void changeDelay() {
  310.             String words[] = {"actionDelay"};
  311.            
  312.             if(matchWords(words))
  313.             {
  314.                 int newDelay=0;
  315.                 for(int x=0;x<msgArray.length;x++)
  316.                 {
  317.                     if(validate(msgArray[x]))
  318.                     {
  319.                         newDelay = Integer.parseInt(msgArray[x]);
  320.                     }
  321.                 }
  322.                
  323.                 if(!(newDelay == 0))
  324.                 {
  325.                     this.actionDelay = newDelay;
  326.                 }
  327.                
  328.                 try {
  329.                     Thread.sleep(1000);
  330.                 } catch(InterruptedException ex) {
  331.                     Thread.currentThread().interrupt();
  332.                 }
  333.                
  334.                 Reply("Setting changed to " + actionDelay + " messages.");
  335.             }
  336.         }//end change delay
  337.        
  338.        
  339.         public void ping() throws IOException, InterruptedException
  340.         {
  341.             String[] words = {"ping"};
  342.             if(matchWords(words))
  343.             {
  344.                 String url = msgArray[msgArray.length-1];
  345.                 if(getStatus(url))
  346.                     Reply("Pong ");
  347.                 else
  348.                     Reply("Offine");
  349.             }
  350.         }
  351.        
  352.        
  353.         public void shutUp() {
  354.             String words[] = {"shutup"};
  355.             String pairs[][] = {{"shut", "up"}};
  356.             String response[] = {"you shut up",
  357.                                 "screw you"};
  358.             if(matchWords(words) || matchPairs(pairs))
  359.             {
  360.                 randReply(response);
  361.             }
  362.         }//end shut up
  363.        
  364.         public void pageAdmin() {
  365.  
  366.             String pairs[][] = {{"page", "a"},
  367.                                 {"page", "an"},
  368.                                 {"get", "help"}};
  369.             String ops = getOps();
  370.             String response[] = {"Paging: " + ops};
  371.             if(matchPairs(pairs))
  372.             {
  373.                 randReply(response);
  374.             }
  375.         }//end page admin
  376.        
  377.        
  378.         //------------User messaging--------------------
  379.        
  380.         public void tell()
  381.         {
  382.             if(msgArray[0].equalsIgnoreCase(this.getName()) && msgArray[1].equalsIgnoreCase("tell"))
  383.             {
  384.                 if(msgArray.length > 3)
  385.                 {
  386.                     String message = "";
  387.                     for(int x=3;x<msgArray.length;x++)
  388.                     {
  389.                         message = message + " " + msgArray[x];
  390.                     }
  391.                     msgList.add(new Outbox(sender, msgArray[2], message));
  392.                     Reply("I will let " + msgArray[2] + " know when I see him/her next.");
  393.                 } else {
  394.                     Reply("Something went wrong");
  395.                 }
  396.             }
  397.         }//end tell
  398.        
  399.         public void tellHelp() {
  400.             if(inactivity > 0)
  401.             {
  402.                 String words[] = {"tell"};
  403.                 String pairs[][] = {{"send", "message"},
  404.                                     {"send", "a"}};
  405.                 if(matchWords(words) || matchPairs(pairs))
  406.                 {
  407.                     Reply("To leave someone a message type: " + this.getName() + " tell <user> <message>");
  408.                 }
  409.             }
  410.         }//end shut up
  411.        
  412.         public void checkForMsg()
  413.         {
  414.             for(int x=0;x<msgList.size();x++)
  415.             {
  416.                 if(msgList.get(x).getTo().equalsIgnoreCase(this.sender))
  417.                 {
  418.                     Reply("Hey " + msgList.get(x).getTo() + ", " + msgList.get(x).getFrom() + " says:" + msgList.get(x).getMessage());
  419.                     msgList.get(x).setTo("sent");
  420.                 }
  421.             }
  422.         }
  423.        
  424.        
  425.          //-------------------------------------//
  426.          //                                     //
  427.          //             Bot Actions             //
  428.          //                                     //
  429.          //-------------------------------------//
  430.        
  431.         public void searchActions()
  432.         {
  433.             //search actions only if bots inactivity is greater then the set delay
  434.             if(inactivity > actionDelay)
  435.             {
  436.                 lmgtfy();
  437.                 randomQuote();
  438.                 wave();
  439.                 bug();
  440.                 aBot();
  441.             }
  442.         }
  443.        
  444.        
  445.         public void aBot() {
  446.             String words[] = {"bot"};
  447.             String response[] = {"I ain't no bot",
  448.                                 "better then a human",
  449.                                 "beep beep, boop boop"};
  450.             if(matchWords(words))
  451.             {
  452.                 randReply(response);
  453.             }
  454.         }//end aBot
  455.        
  456.         public void bug() {
  457.             String words[] = {"bug"};
  458.             if(matchWords(words))
  459.             {
  460.                 Reply("it's not a bug, it's a feature");
  461.             }
  462.         }//end bug
  463.        
  464.        
  465.          public void lmgtfy() {
  466.                  if(msgArray.length > 10)
  467.                  {
  468.                      if(msgArray[msgArray.length-1].endsWith("?"))
  469.                      {
  470.                         String URL = "http://lmgtfy.com/?q=";
  471.    
  472.                         for(int x=0;x<msgArray.length;x++) {
  473.                             if(x == 0) {
  474.                                 URL = URL + msgArray[x];
  475.                             } else {
  476.                                 URL = URL + "+" + msgArray[x];
  477.                             }
  478.                         }//end for
  479.                      Reply(URL);
  480.                     }//end ? if
  481.                  }//end msgArray length
  482.             }//end lmgtfy
  483.          
  484.          
  485.          public void randomQuote()
  486.          {
  487.              //if inactivity is greater then 50 more then set delay
  488.              if(inactivity > (actionDelay + 50))
  489.              {
  490.                  String[] quotes = {"I haven't said anything in awhile",
  491.                                     "beep",
  492.                                     "do bots think?",
  493.                                     "Cats sleep 16 to 18 hours per day.",
  494.                                     "Odontophobia is the fear of teeth.",
  495.                                     "Karoke means 'empty orchestra' in Japanese.",
  496.                                     "The most money ever paid for a cow in an auction was $1.3 million.",
  497.                                     "1 in 5,000 north Atlantic lobsters are born bright blue.",
  498.                                     "Elephants are the only mammals that can't jump.",
  499.                                     "Charlie Brown's father was a barber.",
  500.                                     "The plastic things on the end of shoelaces are called aglets.",
  501.                                     "The elephant is the only animal with 4 knees."};
  502.                  randReply(quotes);
  503.              }//end if
  504.          }//end random quote
  505.        
  506.          public void wave()
  507.          {
  508.                 String[] words = {"o/"};
  509.                 if(matchWords(words))
  510.                     Reply("o/");
  511.          }//end wave
  512.          
  513.          
  514.          
  515.          
  516.          //-------------------------------------//
  517.          //                                     //
  518.          //             IRC Events              //
  519.          //                                     //
  520.          //-------------------------------------//
  521.          
  522.          public void onJoin(String channel, String sender, String login, String hostname)
  523.             {
  524.                  this.sender = sender;
  525.                  checkForMsg();
  526.                 if(inactivity > actionDelay+10)
  527.                 {
  528.                     String[] replies = {"o/",
  529.                             "Hello "+sender,
  530.                             "OMG it's "+sender,
  531.                             "shut up guys, he signed in",
  532.                             "hey",
  533.                             "WELCOME " + sender + "!!"};
  534.                     randReply(replies);
  535.                 }
  536.             }//end on join
  537.            
  538.          
  539.              //when someone quits the IRC channel
  540.             public void onQuit(String sourceNick, String sourceLogin, String sourceHostname, String reason)
  541.             {
  542.                
  543.                
  544.                 //if a Server left the channel
  545.                 String[] servers = {"DONUT",
  546.                                     "BAGEL",
  547.                                     "BB_DEV",
  548.                                     "CREPE",
  549.                                     "BB_UPDATE",
  550.                                     "GRAVY",
  551.                                     "TOAST",
  552.                                     "BACON"};
  553.                
  554.                 for(int x=0;x<servers.length;x++)
  555.                 {
  556.                     if(sourceNick.equals(servers[x]))
  557.                     {
  558.                         String ops = getOps();
  559.                         String[] responses = {"Server down! Server down!" + ops + " HELP!",
  560.                                                 "Who caused that? Better get" + ops + " here.",
  561.                                                 "That's all she wrote folks. Maybe an op can help?" + ops};
  562.                         randReply(responses);
  563.                        
  564.                     } else {
  565.                        
  566.                       //relpy to person quiting
  567.                         if(inactivity > actionDelay+10)
  568.                         {
  569.                             String[] replies = {"seeya "+sourceNick,
  570.                                     "Bye "+sourceNick,
  571.                                     sourceNick +" left :("};
  572.                             randReply(replies);
  573.                         }
  574.                     }
  575.                      
  576.                 }//end for server list
  577.                
  578.                
  579.                
  580.             }//end on quit
  581.            
  582.             protected void onAction(String sender, String login, String hostname, String target, String action)
  583.             {
  584.                 if(inactivity > actionDelay+10)
  585.                     sendAction(channel, action+", also");
  586.             }
  587.          
  588.          
  589.            
  590.            
  591.             //--------Validation--------------------
  592.            
  593.            
  594.             public static boolean validate(String s){
  595.                 boolean valid = true;
  596.                 int i = 0;
  597.                
  598.                 //not valid if length = 0
  599.                 if (s.length() == 0)  
  600.                     valid = false;    
  601.                
  602.                 //not valid if it contains a letter or symbol
  603.                 while (i < s.length())
  604.                 {
  605.                     if (!Character.isDigit(s.charAt(i)))
  606.                     {
  607.                         valid = false;
  608.                         break;
  609.                     }
  610.                     i++;
  611.                 }              
  612.                 //return result
  613.                 return valid;          
  614.         }//end method validate
  615.            
  616.            
  617.         public void formatInGameMsg()
  618.         {
  619.             //if the incoming message came from a server
  620.             //first word is <sender>
  621.             if(msgArray[0].charAt(0) == '<')
  622.             {
  623.                 //set sender
  624.                 String sender = msgArray[0];
  625.                 sender = sender.replace("<", "");
  626.                 sender = sender.replace(">", "");
  627.                 this.sender = sender;
  628.                
  629.                 //remove name from array
  630.                 String newArray[];
  631.                 newArray = new String[msgArray.length-1];
  632.                 for(int x=0;x<msgArray.length-1;x++)
  633.                 {
  634.                     newArray[x] = msgArray[x+1];
  635.                 }
  636.                 msgArray = newArray;
  637.             }
  638.         }
  639.            
  640.         public String getOps()
  641.         {
  642.             String ops = "";
  643.             User[] users = getUsers(channel);
  644.             for(int y=0;y<users.length;y++)
  645.             {
  646.                 if(users[y].isOp())
  647.                 {
  648.                     if(!(users[y].equals("ChanServ") || users[y].equals("OperServ")))
  649.                     {
  650.                         ops = ops + " " + users[y];
  651.                     }
  652.                 }
  653.             }
  654.             return ops;
  655.         }//end get ops
  656.  
  657.         public boolean getStatus(String host) throws IOException, InterruptedException
  658.         {
  659.    
  660.            
  661.             Socket s = new Socket(host, 80);
  662.            
  663.             return false;
  664.            
  665. //           try{
  666. //                  String cmd = "ping -n 1 " + host;
  667. //
  668. //                  Process myProcess = Runtime.getRuntime().exec(cmd);
  669. //                  myProcess.waitFor();
  670. //
  671. //                  if(myProcess.exitValue() == 0) {
  672. //
  673. //                          return true;
  674. //                  } else {
  675. //
  676. //                          return false;
  677. //                  }
  678. //
  679. //          } catch( Exception e ) {
  680. //
  681. //                  e.printStackTrace();
  682. //                  return false;
  683.            // }
  684.                            
  685.            
  686.         }//end get status
  687. }//edn class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement