summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFlorian Bruhin <git@the-compiler.org>2013-08-25 14:36:11 +0200
committerFlorian Bruhin <git@the-compiler.org>2013-08-25 14:36:11 +0200
commitfdf4689c8cfce02256385f9e15373f8ae74c9ee6 (patch)
treec6ecf0402c95f11b7fa7f30f15f7874e1d910279
downloadomegle_eliza-master.tar.gz
omegle_eliza-master.zip
Initial commitHEADmaster
-rwxr-xr-xOmegle.py157
-rwxr-xr-xeliza.pl9
-rwxr-xr-xeliza.sh5
-rwxr-xr-xomegle.sh54
4 files changed, 225 insertions, 0 deletions
diff --git a/Omegle.py b/Omegle.py
new file mode 100755
index 0000000..6eeaa1e
--- /dev/null
+++ b/Omegle.py
@@ -0,0 +1,157 @@
+# Coded by Bobng/Nigg/Lobe/etc
+import urllib
+import urllib2
+import thread
+import simplejson
+import cookielib
+import time
+
+user_agent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-GB; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3"
+
+
+class EventHandler:
+ def fire(self,event,chat,var):
+ ''' Callback class. Var is info relating to the event '''
+ if hasattr(self,event):
+ getattr(self,event)(chat,var)
+
+class OmegleChat:
+ def __init__(self,_id=None,debug=False):
+ self.url = "http://cardassia.omegle.com/"
+ self.id = _id
+ self.failed = False
+ self.connected = False
+ self.in_chat = False
+ self.handlers = []
+ self.terminated = False
+
+ self.debug = debug
+
+ jar = cookielib.CookieJar()
+ processor = urllib2.HTTPCookieProcessor(jar)
+ self.connector = urllib2.build_opener(processor)#,urllib2.HTTPHandler(debuglevel=1))
+ self.connector.addheaders = [
+ ('User-agent',user_agent)
+ ]
+
+ def pausedChat(self,chat,message,pause):
+ ''' Make it look like the bot is typing something '''
+ self.typing()
+ time.sleep(pause)
+ self.stoppedTyping()
+ self.say(message)
+
+ def get_events(self,json=False):
+ ''' Poll the /events/ page and process the response '''
+ #requester = urllib2.Request(self.url+'events',headers={'id':self.id})
+ events = self.connector.open(self.url+'events',data=urllib.urlencode({'id':self.id})).read()
+ if json:
+ return simplejson.loads(events)
+ else:
+ return events
+
+ def connect_events(self, event_handler):
+ ''' Add an event handler '''
+ self.handlers.append(event_handler)
+
+ def fire(self,event,var):
+ for handler in self.handlers:
+ handler.fire(event,self,var)
+
+ def terminate(self):
+ ''' Terminate the thread. Don't call directly, use .disconnect() instead '''
+ self.terminated = True
+
+ def open_page(self,page,data={}):
+ if self.terminated:
+ return
+
+ if not 'id' in data:
+ data['id'] = self.id
+ r = self.connector.open(self.url+page,urllib.urlencode(data)).read()
+ if not r == "win":
+ # Maybe make it except here?
+ if self.debug: print 'Page %s returned %s'%(page,r)
+ return r
+
+ def say(self,message):
+ ''' Send a message from the chat '''
+ if self.debug: print 'Saying message: %s'%message
+ self.open_page('send',{'msg':message})
+
+ def disconnect(self):
+ ''' Close the chat '''
+ self.open_page('disconnect',{})
+ self.terminate()
+
+ def typing(self):
+ ''' Tell the stranger we are typing '''
+ self.open_page('typing')
+
+ def stoppedTyping(self):
+ ''' Tell the stranger we are no longer typing '''
+ self.open_page('stoppedtyping')
+
+ def connect(self,threaded=True):
+ ''' Start a chat session'''
+ if not self.id:
+ self.id = self.connector.open(self.url+'start').read().strip('"')
+
+ self.connected = True
+ if threaded:
+ thread.start_new_thread(self.reactor,())
+ else:
+ self.reactor()
+
+ def waitForTerminate(self):
+ ''' This only returns when .disconnect() or .terminate() is called '''
+ while not self.terminated:
+ time.sleep(0.1)
+ pass
+ return
+
+ def reactor(self):
+ while True:
+ if self.terminated:
+ if self.debug: print "Thread terminating"
+ return
+
+ events = self.get_events(json=True)
+ if not events:
+ continue
+ if self.debug: print events
+ for event in events:
+ if len(event) > 1:
+ self.fire(event[0],event[1:])
+ else:
+ self.fire(event[0],None)
+
+if __name__=='__main__':
+
+ class MyEventHandler(EventHandler):
+ def connected(self,chat,var):
+ print "Connected"
+ chat.in_chat = True
+ chat.say("Hello!")
+
+ def gotMessage(self,chat,message):
+ message = message[0]
+ print "Message recieved: "+message
+
+ def typing(self,chat,var):
+ print "Stranger is typing..."
+
+ def stoppedTyping(self,chat,var):
+ print "Stranger stopped typing!"
+
+ def strangerDisconnected(self,chat,var):
+ print "Stranger disconnected - Terminating"
+ chat.terminate()
+
+ for i in xrange(2):
+ print "Starting chat %s"%i
+ a = OmegleChat()
+ a.connect_events(MyEventHandler())
+ a.connect(True)
+ a.waitForTerminate()
+ raw_input()
diff --git a/eliza.pl b/eliza.pl
new file mode 100755
index 0000000..baacaff
--- /dev/null
+++ b/eliza.pl
@@ -0,0 +1,9 @@
+#!/usr/bin/perl
+use Chatbot::Eliza;
+$bot = new Chatbot::Eliza("Eliza", "");
+print $bot->{initial}->[0] . "\n";
+while (my $message = <STDIN>) {
+ $message = $bot->transform($message);
+ print "$message\n";
+}
+exit;
diff --git a/eliza.sh b/eliza.sh
new file mode 100755
index 0000000..10e02e3
--- /dev/null
+++ b/eliza.sh
@@ -0,0 +1,5 @@
+#!/bin/bash
+mkfifo a b
+./omegle.sh < b > a &
+exec 3>b
+./eliza.pl < a >&3
diff --git a/omegle.sh b/omegle.sh
new file mode 100755
index 0000000..94f146b
--- /dev/null
+++ b/omegle.sh
@@ -0,0 +1,54 @@
+#!/bin/bash
+>pidfile
+trap "exit 0" USR1
+trap 'kill $(<pidfile) 2>/dev/null; exit 0' INT TERM
+oldifs="$IFS"
+send() {
+ message=$(sed -e 's/\:/%3a/g;s/\//%2f/g;s/\?/%3f/g;s/\&/%3d/g;s/\,/%2c/g;s/\[/%5b/g;s/\]/%5d/g;s/ /+/g;s/"/%22/g' <<< "$@")
+ out=$(curl -d "id=$id&msg=$message" omegle.com/send 2>/dev/null)
+ [[ "$out" == fail ]] && echo "[E] error while sending!"
+}
+
+echo "[I] Omegle-Bot by The-Compiler"
+
+id=$(curl -d '' omegle.com/start 2>/dev/null)
+echo "[D] got an id"
+if grep -q '^"[a-zA-Z0-9_-]*"$' <<< "$id"; then
+ id="${id//\"/}"
+else
+ echo "[E] Invalid ID: $id"
+fi
+
+while [[ "$disconnect" != 1 ]]; do
+ events=$(curl -d "id=$id" omegle.com/events 2>/dev/null)
+ echo "events_raw: $events" > debug
+ case "$events" in
+ "["*"]")
+ events=$(sed 's/...//;s/...$//;s/"\], \["/\n/g;s/", "/=/g' <<< "$events")
+ echo "events_afterSed: $events" > debug
+ IFS=$'\n'
+ for event in $events; do
+ case "$event" in
+ connected|typing|stoppedTyping|waiting) echo "[I] $event" ;;
+ gotMessage*)
+ message=$(cut -d '=' -f 2- <<< "$event" | sed 's/\\"/"/g')
+ echo "[M] $message"
+ ;;
+ strangerDisconnected) echo "[I] strangerDisconnected"; disconnect=1 ;;
+ *) echo "[E] unknown status $events [in]" | tee -a debug ;;
+ esac
+ done
+ ;;
+ null) echo "[I] strangerDisconnected"; echo "[E] null"; disconnect=1 ;;
+ "") ;;
+ *) echo "[E] unknown status $events" ;;
+ esac
+done && kill -USR1 $$ &
+echo "$!" > pidfile
+
+while read line; do
+ send "$line"
+done
+
+kill $(<pidfile)
+exit 0