Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Random;
- import java.io.*;
- import java.net.*;
- import java.util.ArrayList;
- import org.jibble.pircbot.*;
- public class Gil extends PircBot {
- //random number object
- Random rand = new Random();
- //the channel the current message was received from
- String channel;
- //sender of current message
- String sender;
- //array of words in message
- String[] msgArray;
- //number of messages on channel sense bot last spoke
- int inactivity = 0;
- //delay in messages before bot looks for opportunity to talk
- int actionDelay = 5;
- //ArrayList to store message to send to other users
- ArrayList<Outbox> msgList = new ArrayList<Outbox>();
- //constructor
- public Gil() {
- this.setName("tom");
- }
- //New message on channel
- public void onMessage(String channel, String sender, String login, String hostname, String message){
- inactivity++;
- this.sender = sender;
- this.channel = channel;
- //split msg into words and place in an array
- msgArray = message.split("\\s+");
- //if message came from game server, remove <name> from message and edit sender to match
- formatInGameMsg();
- //check if sender has a message saved
- checkForMsg();
- //check for tell command
- tell();
- if(inactivity > 0)
- {
- //if message contains bot name,
- //look for direct replies
- if(checkForName())
- {
- try {
- searchDirectReplies();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (InterruptedException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- //search for actions related to message
- searchActions();
- }
- }//end onMessage
- //check for use of bot name in message
- public boolean checkForName() {
- String[] names = {this.getName(),
- this.getName()+"?",
- this.getName()+":",
- this.getName()+",",
- "\""+this.getName()+"\"",
- this.getName()+"!",
- this.getName()+"."};
- if(matchWords(names))
- {
- return true;
- } else {
- return false;
- }
- }//end check method
- //match single words in message from words[] array
- public boolean matchWords(String[] words)
- {
- for(int x=0;x<msgArray.length;x++) {
- for(int y=0;y<words.length;y++){
- if(msgArray[x].equalsIgnoreCase(words[y]))
- {
- return true;
- }//end if
- }//end nested for
- }//end for
- return false;
- }//end matchWords
- //match pairs of words in message from pairs[] array
- public boolean matchPairs(String[][] pairs)
- {
- for(int x=0;x<(msgArray.length);x++) {
- for(int y=0;y<pairs.length;y++){
- if(msgArray[x].equalsIgnoreCase(pairs[y][0]) && msgArray[x+1].equalsIgnoreCase(pairs[y][1]))
- {
- return true;
- }
- }//end nested for
- }//end for
- return false;
- }//end match pairs
- //reply methods
- public void randReply(String[] response) {
- int randomNum = rand.nextInt(response.length);
- String reply = response[randomNum];
- try {
- Thread.sleep(replyDelay(reply));
- } catch(InterruptedException ex) {
- Thread.currentThread().interrupt();
- }
- sendMessage(channel, reply);
- inactivity = 0;
- }//end reply
- public void Reply(String response) {
- try {
- Thread.sleep(replyDelay(response));
- } catch(InterruptedException ex) {
- Thread.currentThread().interrupt();
- }
- sendMessage(channel, response);
- inactivity = 0;
- }//end reply
- public long replyDelay(String reply)
- {
- long delay;
- int length = reply.length();
- if(length < 6)
- delay = 2000;
- else if(length < 12)
- delay = 3000;
- else if(length < 22)
- delay = 4000;
- else
- delay = 5000;
- return delay;
- }
- //-------------------------------------//
- // //
- // Responses when bot name used //
- // //
- //-------------------------------------//
- public void searchDirectReplies() throws IOException, InterruptedException
- {
- hello();
- leave();
- whatsUp();
- changeName();
- who();
- changeDelay();
- thanks();
- why();
- ping();
- shutUp();
- tellHelp();
- pageAdmin();
- //if bot name is used and no other criteria are met
- if(inactivity > actionDelay)
- Reply("What?");
- }
- public void hello() {
- String words[] = {"hello",
- "hi",
- "welcome",
- "hey",
- "o/"};
- String response[] = {"hello",
- "hi",
- "hey",
- "Salutations",
- "o/"};
- if(matchWords(words))
- {
- randReply(response);
- }
- }//end hello
- public void whatsUp() {
- String words[] = {"sup"};
- String pairs[][] = {{"whats", "up"},
- {"what", "up"},
- {"what's", "up"}};
- String response[] = {"I'm fine",
- "I wish I had a brain...",
- "punching trees",
- "hanging out in Jared's RAM"};
- if(matchWords(words) || matchPairs(pairs))
- {
- randReply(response);
- }
- }//end whats up
- public void who() {
- String words[] = {"who",
- "whose",
- "whos",
- "who's"};
- String pairs[][] = {{"who", "is"},
- {"who", "are"}};
- String response[] = {"I'm "+this.getName(),
- "I'm "+sender,
- "who are you?",
- "what's it to you?"};
- if(matchWords(words) || matchPairs(pairs))
- {
- randReply(response);
- }
- }//end who
- public void leave() {
- String words[] = {"exit",
- "leave",
- "stop",
- "bye",
- "quit"};
- String pairs[][] = {{"fuck", "off"},
- {"go", "away"}};
- String response[] = {"fine",
- "I'll be back",
- "sure thing eh",
- "disconnecting..."};
- if(matchWords(words) || matchPairs(pairs))
- {
- randReply(response);
- try {
- Thread.sleep(1000);
- } catch(InterruptedException ex) {
- Thread.currentThread().interrupt();
- }
- disconnect();
- }
- }//end leave
- public void thanks() {
- String words[] = {"thank",
- "thanks",
- "lol",
- "haha"};
- String response[] = {"np",
- "anytime",
- "your welcome"};
- if(matchWords(words))
- {
- randReply(response);
- }
- }//end thanks
- public void why() {
- String words[] = {"why"};
- String response[] = {"because",
- "that's how it is"};
- if(matchWords(words))
- {
- randReply(response);
- }
- }//end why
- public void changeName() {
- String pairs[][] = {{"change", "name"}};
- String temp = "I was pretty fond of " + this.getName() + "...";
- String response[] = {temp,
- "sure thing eh"};
- if(matchPairs(pairs))
- {
- randReply(response);
- try {
- Thread.sleep(1000);
- } catch(InterruptedException ex) {
- Thread.currentThread().interrupt();
- }
- String newName = msgArray[msgArray.length - 1];//last word in message
- changeNick(newName);
- this.setName(newName);
- }
- }//end name change
- public void changeDelay() {
- String words[] = {"actionDelay"};
- if(matchWords(words))
- {
- int newDelay=0;
- for(int x=0;x<msgArray.length;x++)
- {
- if(validate(msgArray[x]))
- {
- newDelay = Integer.parseInt(msgArray[x]);
- }
- }
- if(!(newDelay == 0))
- {
- this.actionDelay = newDelay;
- }
- try {
- Thread.sleep(1000);
- } catch(InterruptedException ex) {
- Thread.currentThread().interrupt();
- }
- Reply("Setting changed to " + actionDelay + " messages.");
- }
- }//end change delay
- public void ping() throws IOException, InterruptedException
- {
- String[] words = {"ping"};
- if(matchWords(words))
- {
- String url = msgArray[msgArray.length-1];
- if(getStatus(url))
- Reply("Pong ");
- else
- Reply("Offine");
- }
- }
- public void shutUp() {
- String words[] = {"shutup"};
- String pairs[][] = {{"shut", "up"}};
- String response[] = {"you shut up",
- "screw you"};
- if(matchWords(words) || matchPairs(pairs))
- {
- randReply(response);
- }
- }//end shut up
- public void pageAdmin() {
- String pairs[][] = {{"page", "a"},
- {"page", "an"},
- {"get", "help"}};
- String ops = getOps();
- String response[] = {"Paging: " + ops};
- if(matchPairs(pairs))
- {
- randReply(response);
- }
- }//end page admin
- //------------User messaging--------------------
- public void tell()
- {
- if(msgArray[0].equalsIgnoreCase(this.getName()) && msgArray[1].equalsIgnoreCase("tell"))
- {
- if(msgArray.length > 3)
- {
- String message = "";
- for(int x=3;x<msgArray.length;x++)
- {
- message = message + " " + msgArray[x];
- }
- msgList.add(new Outbox(sender, msgArray[2], message));
- Reply("I will let " + msgArray[2] + " know when I see him/her next.");
- } else {
- Reply("Something went wrong");
- }
- }
- }//end tell
- public void tellHelp() {
- if(inactivity > 0)
- {
- String words[] = {"tell"};
- String pairs[][] = {{"send", "message"},
- {"send", "a"}};
- if(matchWords(words) || matchPairs(pairs))
- {
- Reply("To leave someone a message type: " + this.getName() + " tell <user> <message>");
- }
- }
- }//end shut up
- public void checkForMsg()
- {
- for(int x=0;x<msgList.size();x++)
- {
- if(msgList.get(x).getTo().equalsIgnoreCase(this.sender))
- {
- Reply("Hey " + msgList.get(x).getTo() + ", " + msgList.get(x).getFrom() + " says:" + msgList.get(x).getMessage());
- msgList.get(x).setTo("sent");
- }
- }
- }
- //-------------------------------------//
- // //
- // Bot Actions //
- // //
- //-------------------------------------//
- public void searchActions()
- {
- //search actions only if bots inactivity is greater then the set delay
- if(inactivity > actionDelay)
- {
- lmgtfy();
- randomQuote();
- wave();
- bug();
- aBot();
- }
- }
- public void aBot() {
- String words[] = {"bot"};
- String response[] = {"I ain't no bot",
- "better then a human",
- "beep beep, boop boop"};
- if(matchWords(words))
- {
- randReply(response);
- }
- }//end aBot
- public void bug() {
- String words[] = {"bug"};
- if(matchWords(words))
- {
- Reply("it's not a bug, it's a feature");
- }
- }//end bug
- public void lmgtfy() {
- if(msgArray.length > 10)
- {
- if(msgArray[msgArray.length-1].endsWith("?"))
- {
- String URL = "http://lmgtfy.com/?q=";
- for(int x=0;x<msgArray.length;x++) {
- if(x == 0) {
- URL = URL + msgArray[x];
- } else {
- URL = URL + "+" + msgArray[x];
- }
- }//end for
- Reply(URL);
- }//end ? if
- }//end msgArray length
- }//end lmgtfy
- public void randomQuote()
- {
- //if inactivity is greater then 50 more then set delay
- if(inactivity > (actionDelay + 50))
- {
- String[] quotes = {"I haven't said anything in awhile",
- "beep",
- "do bots think?",
- "Cats sleep 16 to 18 hours per day.",
- "Odontophobia is the fear of teeth.",
- "Karoke means 'empty orchestra' in Japanese.",
- "The most money ever paid for a cow in an auction was $1.3 million.",
- "1 in 5,000 north Atlantic lobsters are born bright blue.",
- "Elephants are the only mammals that can't jump.",
- "Charlie Brown's father was a barber.",
- "The plastic things on the end of shoelaces are called aglets.",
- "The elephant is the only animal with 4 knees."};
- randReply(quotes);
- }//end if
- }//end random quote
- public void wave()
- {
- String[] words = {"o/"};
- if(matchWords(words))
- Reply("o/");
- }//end wave
- //-------------------------------------//
- // //
- // IRC Events //
- // //
- //-------------------------------------//
- public void onJoin(String channel, String sender, String login, String hostname)
- {
- this.sender = sender;
- checkForMsg();
- if(inactivity > actionDelay+10)
- {
- String[] replies = {"o/",
- "Hello "+sender,
- "OMG it's "+sender,
- "shut up guys, he signed in",
- "hey",
- "WELCOME " + sender + "!!"};
- randReply(replies);
- }
- }//end on join
- //when someone quits the IRC channel
- public void onQuit(String sourceNick, String sourceLogin, String sourceHostname, String reason)
- {
- //if a Server left the channel
- String[] servers = {"DONUT",
- "BAGEL",
- "BB_DEV",
- "CREPE",
- "BB_UPDATE",
- "GRAVY",
- "TOAST",
- "BACON"};
- for(int x=0;x<servers.length;x++)
- {
- if(sourceNick.equals(servers[x]))
- {
- String ops = getOps();
- String[] responses = {"Server down! Server down!" + ops + " HELP!",
- "Who caused that? Better get" + ops + " here.",
- "That's all she wrote folks. Maybe an op can help?" + ops};
- randReply(responses);
- } else {
- //relpy to person quiting
- if(inactivity > actionDelay+10)
- {
- String[] replies = {"seeya "+sourceNick,
- "Bye "+sourceNick,
- sourceNick +" left :("};
- randReply(replies);
- }
- }
- }//end for server list
- }//end on quit
- protected void onAction(String sender, String login, String hostname, String target, String action)
- {
- if(inactivity > actionDelay+10)
- sendAction(channel, action+", also");
- }
- //--------Validation--------------------
- public static boolean validate(String s){
- boolean valid = true;
- int i = 0;
- //not valid if length = 0
- if (s.length() == 0)
- valid = false;
- //not valid if it contains a letter or symbol
- while (i < s.length())
- {
- if (!Character.isDigit(s.charAt(i)))
- {
- valid = false;
- break;
- }
- i++;
- }
- //return result
- return valid;
- }//end method validate
- public void formatInGameMsg()
- {
- //if the incoming message came from a server
- //first word is <sender>
- if(msgArray[0].charAt(0) == '<')
- {
- //set sender
- String sender = msgArray[0];
- sender = sender.replace("<", "");
- sender = sender.replace(">", "");
- this.sender = sender;
- //remove name from array
- String newArray[];
- newArray = new String[msgArray.length-1];
- for(int x=0;x<msgArray.length-1;x++)
- {
- newArray[x] = msgArray[x+1];
- }
- msgArray = newArray;
- }
- }
- public String getOps()
- {
- String ops = "";
- User[] users = getUsers(channel);
- for(int y=0;y<users.length;y++)
- {
- if(users[y].isOp())
- {
- if(!(users[y].equals("ChanServ") || users[y].equals("OperServ")))
- {
- ops = ops + " " + users[y];
- }
- }
- }
- return ops;
- }//end get ops
- public boolean getStatus(String host) throws IOException, InterruptedException
- {
- Socket s = new Socket(host, 80);
- return false;
- // try{
- // String cmd = "ping -n 1 " + host;
- //
- // Process myProcess = Runtime.getRuntime().exec(cmd);
- // myProcess.waitFor();
- //
- // if(myProcess.exitValue() == 0) {
- //
- // return true;
- // } else {
- //
- // return false;
- // }
- //
- // } catch( Exception e ) {
- //
- // e.printStackTrace();
- // return false;
- // }
- }//end get status
- }//edn class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement