Advertisement
UberMouse

Untitled

Nov 10th, 2011
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.52 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.DataOutputStream;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. import java.io.UnsupportedEncodingException;
  6. import java.net.URL;
  7. import java.net.URLConnection;
  8. import java.net.URLEncoder;
  9. import java.util.Iterator;
  10. import java.util.LinkedList;
  11. import java.util.List;
  12. import java.util.regex.Matcher;
  13. import java.util.regex.Pattern;
  14.  
  15. import org.json.simple.*;
  16.  
  17. import org.jibble.pircbot.*;
  18.  
  19. public class OmeagleBot extends PircBot {
  20.     private String id;
  21.     private boolean connected;
  22.     private WatcherThread thread;
  23.     private boolean fullChan = false;
  24.     private String prompt = ".";
  25.     private boolean stranger = true;
  26.  
  27.     public enum actions {
  28.         SAY, CONNECT, DISCONNECT, ABOUT, HELP, TOGGLEFULLCHAN, TOGGLESTRANGER, SETPROMPT
  29.     };
  30.  
  31.     public OmeagleBot(String name) {
  32.         this.setName(name);
  33.         thread = new WatcherThread();
  34.     }
  35.  
  36.     public void doAction(String action) {
  37.         doAction(action, "");
  38.     }
  39.  
  40.     public void debug(Object text) {
  41.         System.out.println(text);
  42.     }
  43.  
  44.     public void doAction(String action, String message) {
  45.         debug(action);
  46.         try {
  47.             switch (actions.valueOf(action.toUpperCase())) {
  48.             case SAY:
  49.                 say(message);
  50.                 break;
  51.             case CONNECT:
  52.                 sendMessage(getChannels()[0], "Connecting...");
  53.                 id = connect();
  54.                 if (!thread.isAlive())
  55.                     thread.start();
  56.                 break;
  57.             case DISCONNECT:
  58.                 disconnectChat();
  59.                 break;
  60.             case TOGGLEFULLCHAN:
  61.                 fullChan = !fullChan;
  62.                 String chanText = (fullChan == true) ? "no " : "now ";
  63.                 sendMessage(getChannels()[0], "Toggling full channel mode, "
  64.                         + chanText + "need to prepend " + prompt + "say");
  65.                 break;
  66.             case HELP:
  67.                 sendMessage(
  68.                         getChannels()[0],
  69.                         "Commands are: "
  70.                                 + prompt
  71.                                 + "connect, finds a new partner. "
  72.                                 + prompt
  73.                                 + "disconnect, leaves current partner. "
  74.                                 + prompt
  75.                                 + "say, sends text to partner. "
  76.                                 + prompt
  77.                                 + "togglefullchan, turns on/off full channel mode, which removes the need to prepend "
  78.                                 + prompt
  79.                                 + "say to messages. "
  80.                                 + prompt
  81.                                 + "setprompt, changes what the prompt symbol is.");
  82.                 break;
  83.             case ABOUT:
  84.                 sendMessage(getChannels()[0],
  85.                         "Coded by UberMouse, idea by Widdershin. Type "
  86.                                 + prompt + "help for commands");
  87.                 break;
  88.             case SETPROMPT:
  89.                 Pattern regxp = Pattern.compile("[a-zA-Z0-9]");
  90.                 Matcher match = regxp.matcher(message.replace(prompt
  91.                         + "setprompt", ""));
  92.                 message = match.replaceAll("");
  93.                 System.out.println(message);
  94.                 prompt = message.replace(" ", "");
  95.                 break;
  96.             case TOGGLESTRANGER:
  97.                     stranger = !stranger;
  98.                     sendMessage(getChannels()[0], "Toggling prepending of stranger");
  99.                 break;
  100.             }
  101.         } catch (Exception e) {
  102.         }
  103.     }
  104.  
  105.     public void onPrivateMessage(String sender, String login, String hostname,
  106.             String message) {
  107.         if (message.toLowerCase().contains(prompt + "kill")) {
  108.             System.exit(0);
  109.         }
  110.         if (message.substring(0, 1).equals(prompt)) {
  111.             message = message.replaceFirst(prompt, "");
  112.             if (message.contains(" ")) {
  113.                 String[] split;
  114.                 split = message.split(" ");
  115.                 doAction(split[0], split[1]);
  116.             } else
  117.                 doAction(message);
  118.         }
  119.     }
  120.  
  121.     public void onJoin(String channel, String sender, String login,
  122.             String hostname) {
  123.         if (sender.equals(this.getName())) {
  124.             sendMessage(channel, "Omegle IRC bot, type " + prompt
  125.                     + "help for commands");
  126.         }
  127.     }
  128.  
  129.     public void onMessage(String channel, String sender, String login,
  130.             String hostname, String message) {
  131.         if (!message.substring(0, 1).equals(prompt) && fullChan) {
  132.             try {
  133.                 say(message);
  134.             } catch (IOException e) {
  135.                 // TODO Auto-generated catch block
  136.                 e.printStackTrace();
  137.             }
  138.         }
  139.         if (message.substring(0, 1).equals(prompt)) {
  140.             message = message.replaceFirst(prompt, "");
  141.             if (message.contains(" ")) {
  142.                 String[] split;
  143.                 split = message.split(" ");
  144.                 doAction(split[0], split[1]);
  145.             } else
  146.                 doAction(message);
  147.  
  148.         }
  149.     }
  150.  
  151.     public void say(String message) throws IOException {
  152.         if (!connected)
  153.             return;
  154.         debug("IRC: " + message);
  155.         sendData(
  156.                 "http://bajor.omegle.com/send",
  157.                 "id=" + URLEncoder.encode(id, "UTF-8") + "&msg="
  158.                         + URLEncoder.encode(message, "UTF-8"));
  159.     }
  160.  
  161.     public String connect() throws IOException {
  162.         debug("Connecting");
  163.         String input = sendData("http://bajor.omegle.com/start", "rcs=1&spid=");
  164.         if (input == null) {
  165.             return "NOIDRECV";
  166.         } else {
  167.             input = input.replaceAll("\n", "");
  168.             connected = true;
  169.             return input.replaceAll("\"", "");
  170.         }
  171.     }
  172.  
  173.     public void disconnectChat() {
  174.         debug("Disconnected");
  175.         connected = false;
  176.         sendMessage(getChannels()[0], "Disconnected");
  177.     }
  178.  
  179.     public String sendData(String URL, String message) throws IOException {
  180.         URL url = new URL(URL);
  181.         URLConnection connection = url.openConnection();
  182.         connection.setDoOutput(true);
  183.         DataOutputStream printout = new DataOutputStream(
  184.                 connection.getOutputStream());
  185.         printout.writeBytes(message);
  186.         printout.flush();
  187.         printout.close();
  188.         BufferedReader in = new BufferedReader(new InputStreamReader(
  189.                 connection.getInputStream()));
  190.         String returnData = "";
  191.         String temp;
  192.         while ((temp = in.readLine()) != null) {
  193.             returnData += temp + "\n";
  194.         }
  195.         return returnData;
  196.     }
  197.  
  198.     public void getEvents() throws UnsupportedEncodingException, IOException {
  199.         String event_json = sendData("http://bajor.omegle.com/events", "id="
  200.                 + URLEncoder.encode(id, "UTF-8"));
  201.         if (event_json != null) {
  202.             JSONArray events = (JSONArray) JSONValue.parse(event_json);
  203.             handleEvents(events);
  204.         }
  205.     }
  206.  
  207.     public void handleEvents(JSONArray events)
  208.             throws UnsupportedEncodingException, IOException {
  209.         if (events == null)
  210.             return;
  211.         Iterator iter = events.iterator();
  212.         while (iter.hasNext()) {
  213.             String message = iter.next().toString();
  214.             if (message.toLowerCase().contains("reload the page")) {
  215.                 disconnectChat();
  216.             }
  217.             if (message.toLowerCase().contains("typing")) {
  218.                 continue;
  219.             }
  220.             if (message.toLowerCase().contains("waiting")) {
  221.                 continue;
  222.             }
  223.             if (message.toLowerCase().contains("gotmessage")) {
  224.                 message = message.split("Message\",\"")[1].split("\"]")[0]
  225.                         .trim();
  226.                 message = message.replaceAll("\"", "");
  227.                 message = message.replaceAll(",", "");
  228.                 message = message.replaceAll("\\[", "");
  229.                 message = message.replaceAll("\\]", "");
  230.                 Pattern regx = Pattern.compile("[\\r\\n]+");
  231.                 Matcher matcher = regx.matcher(message);
  232.                 message = message.toLowerCase();
  233.                 if (matcher.matches()) {
  234.                     debug("new line");
  235.                     String[] splitMessage = message.split("[\\r\\n]+");
  236.                     for (String messages : splitMessage) {
  237.                         debug(messages);
  238.                         sendMessage(getChannels()[0], "Stranger: " + messages);
  239.                     }
  240.                     continue;
  241.                 }
  242.                 debug("Stranger: " + message);
  243.                 sendMessage(getChannels()[0], (stranger) ? "Stranger: " + message : message);
  244.             }
  245.             if (message.toLowerCase().contains("strangerdisconnected")) {
  246.                 connected = false;
  247.                 sendMessage(getChannels()[0], "Stranger disconnected");
  248.                 id = connect();
  249.                 debug("Stranger disconnected");
  250.                 continue;
  251.             }
  252.             if (message.toLowerCase().contains("connected")) {
  253.                 debug("Found new stranger");
  254.                 sendMessage(getChannels()[0], "Found a stranger to talk to!");
  255.             }
  256.         }
  257.     }
  258.  
  259.     class WatcherThread extends Thread {
  260.         public void run() {
  261.             while (true) {
  262.                 try {
  263.                     if (connected) {
  264.                         getEvents();
  265.                     }
  266.                     Thread.sleep(1000);
  267.                 } catch (Exception e) {
  268.                 }
  269.             }
  270.         }
  271.     }
  272. }
  273.  
  274.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement