Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.DataOutputStream;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.io.UnsupportedEncodingException;
- import java.net.URL;
- import java.net.URLConnection;
- import java.net.URLEncoder;
- import java.util.Iterator;
- import java.util.LinkedList;
- import java.util.List;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import org.json.simple.*;
- import org.jibble.pircbot.*;
- public class OmeagleBot extends PircBot {
- private String id;
- private boolean connected;
- private WatcherThread thread;
- private boolean fullChan = false;
- private String prompt = ".";
- private boolean stranger = true;
- public enum actions {
- SAY, CONNECT, DISCONNECT, ABOUT, HELP, TOGGLEFULLCHAN, TOGGLESTRANGER, SETPROMPT
- };
- public OmeagleBot(String name) {
- this.setName(name);
- thread = new WatcherThread();
- }
- public void doAction(String action) {
- doAction(action, "");
- }
- public void debug(Object text) {
- System.out.println(text);
- }
- public void doAction(String action, String message) {
- debug(action);
- try {
- switch (actions.valueOf(action.toUpperCase())) {
- case SAY:
- say(message);
- break;
- case CONNECT:
- sendMessage(getChannels()[0], "Connecting...");
- id = connect();
- if (!thread.isAlive())
- thread.start();
- break;
- case DISCONNECT:
- disconnectChat();
- break;
- case TOGGLEFULLCHAN:
- fullChan = !fullChan;
- String chanText = (fullChan == true) ? "no " : "now ";
- sendMessage(getChannels()[0], "Toggling full channel mode, "
- + chanText + "need to prepend " + prompt + "say");
- break;
- case HELP:
- sendMessage(
- getChannels()[0],
- "Commands are: "
- + prompt
- + "connect, finds a new partner. "
- + prompt
- + "disconnect, leaves current partner. "
- + prompt
- + "say, sends text to partner. "
- + prompt
- + "togglefullchan, turns on/off full channel mode, which removes the need to prepend "
- + prompt
- + "say to messages. "
- + prompt
- + "setprompt, changes what the prompt symbol is.");
- break;
- case ABOUT:
- sendMessage(getChannels()[0],
- "Coded by UberMouse, idea by Widdershin. Type "
- + prompt + "help for commands");
- break;
- case SETPROMPT:
- Pattern regxp = Pattern.compile("[a-zA-Z0-9]");
- Matcher match = regxp.matcher(message.replace(prompt
- + "setprompt", ""));
- message = match.replaceAll("");
- System.out.println(message);
- prompt = message.replace(" ", "");
- break;
- case TOGGLESTRANGER:
- stranger = !stranger;
- sendMessage(getChannels()[0], "Toggling prepending of stranger");
- break;
- }
- } catch (Exception e) {
- }
- }
- public void onPrivateMessage(String sender, String login, String hostname,
- String message) {
- if (message.toLowerCase().contains(prompt + "kill")) {
- System.exit(0);
- }
- if (message.substring(0, 1).equals(prompt)) {
- message = message.replaceFirst(prompt, "");
- if (message.contains(" ")) {
- String[] split;
- split = message.split(" ");
- doAction(split[0], split[1]);
- } else
- doAction(message);
- }
- }
- public void onJoin(String channel, String sender, String login,
- String hostname) {
- if (sender.equals(this.getName())) {
- sendMessage(channel, "Omegle IRC bot, type " + prompt
- + "help for commands");
- }
- }
- public void onMessage(String channel, String sender, String login,
- String hostname, String message) {
- if (!message.substring(0, 1).equals(prompt) && fullChan) {
- try {
- say(message);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- if (message.substring(0, 1).equals(prompt)) {
- message = message.replaceFirst(prompt, "");
- if (message.contains(" ")) {
- String[] split;
- split = message.split(" ");
- doAction(split[0], split[1]);
- } else
- doAction(message);
- }
- }
- public void say(String message) throws IOException {
- if (!connected)
- return;
- debug("IRC: " + message);
- sendData(
- "http://bajor.omegle.com/send",
- "id=" + URLEncoder.encode(id, "UTF-8") + "&msg="
- + URLEncoder.encode(message, "UTF-8"));
- }
- public String connect() throws IOException {
- debug("Connecting");
- String input = sendData("http://bajor.omegle.com/start", "rcs=1&spid=");
- if (input == null) {
- return "NOIDRECV";
- } else {
- input = input.replaceAll("\n", "");
- connected = true;
- return input.replaceAll("\"", "");
- }
- }
- public void disconnectChat() {
- debug("Disconnected");
- connected = false;
- sendMessage(getChannels()[0], "Disconnected");
- }
- public String sendData(String URL, String message) throws IOException {
- URL url = new URL(URL);
- URLConnection connection = url.openConnection();
- connection.setDoOutput(true);
- DataOutputStream printout = new DataOutputStream(
- connection.getOutputStream());
- printout.writeBytes(message);
- printout.flush();
- printout.close();
- BufferedReader in = new BufferedReader(new InputStreamReader(
- connection.getInputStream()));
- String returnData = "";
- String temp;
- while ((temp = in.readLine()) != null) {
- returnData += temp + "\n";
- }
- return returnData;
- }
- public void getEvents() throws UnsupportedEncodingException, IOException {
- String event_json = sendData("http://bajor.omegle.com/events", "id="
- + URLEncoder.encode(id, "UTF-8"));
- if (event_json != null) {
- JSONArray events = (JSONArray) JSONValue.parse(event_json);
- handleEvents(events);
- }
- }
- public void handleEvents(JSONArray events)
- throws UnsupportedEncodingException, IOException {
- if (events == null)
- return;
- Iterator iter = events.iterator();
- while (iter.hasNext()) {
- String message = iter.next().toString();
- if (message.toLowerCase().contains("reload the page")) {
- disconnectChat();
- }
- if (message.toLowerCase().contains("typing")) {
- continue;
- }
- if (message.toLowerCase().contains("waiting")) {
- continue;
- }
- if (message.toLowerCase().contains("gotmessage")) {
- message = message.split("Message\",\"")[1].split("\"]")[0]
- .trim();
- message = message.replaceAll("\"", "");
- message = message.replaceAll(",", "");
- message = message.replaceAll("\\[", "");
- message = message.replaceAll("\\]", "");
- Pattern regx = Pattern.compile("[\\r\\n]+");
- Matcher matcher = regx.matcher(message);
- message = message.toLowerCase();
- if (matcher.matches()) {
- debug("new line");
- String[] splitMessage = message.split("[\\r\\n]+");
- for (String messages : splitMessage) {
- debug(messages);
- sendMessage(getChannels()[0], "Stranger: " + messages);
- }
- continue;
- }
- debug("Stranger: " + message);
- sendMessage(getChannels()[0], (stranger) ? "Stranger: " + message : message);
- }
- if (message.toLowerCase().contains("strangerdisconnected")) {
- connected = false;
- sendMessage(getChannels()[0], "Stranger disconnected");
- id = connect();
- debug("Stranger disconnected");
- continue;
- }
- if (message.toLowerCase().contains("connected")) {
- debug("Found new stranger");
- sendMessage(getChannels()[0], "Found a stranger to talk to!");
- }
- }
- }
- class WatcherThread extends Thread {
- public void run() {
- while (true) {
- try {
- if (connected) {
- getEvents();
- }
- Thread.sleep(1000);
- } catch (Exception e) {
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement