summaryrefslogtreecommitdiff
path: root/omegle_jabber.py
blob: 51d7ba3bcf18971387e62c4343ac7a7c17f321b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import xmpp
from omegle import OmegleChat
import Queue

def reply(message,text):
	msg = xmpp.Message(to=message.getFrom(),body=text)
	if message.getThread():
		msg.setThread( message.getThread() )
	msg.setType( message.getType() )
	return msg

class OmegleXMPP(OmegleChat):
	def __init__(self,xmpp,last_message):
		self.xmpp = xmpp
		self.last_message = last_message
		OmegleChat.__init__(self,name="(%s)"%last_message.getFrom())

	def on_message(self,message):
		print "%s: omegle got message %s" % (self.last_message.getFrom(),message)
		msg = reply(self.last_message, message)
		msg.addChild(name="active",namespace="http://jabber.org/protocol/chatstates")
		self.xmpp._myqueue.put_nowait(msg)


	def on_typing(self):
		msg = xmpp.Message(to=self.last_message.getFrom(),typ=self.last_message.getType())
		if self.last_message.getThread():
			msg.setThread(self.last_message.getThread())
		msg.addChild(name="composing",namespace="http://jabber.org/protocol/chatstates")
		self.xmpp._myqueue.put_nowait(msg)
		print "begin typing"

	def on_stopped_typing(self):
		msg = xmpp.Message(to=self.last_message.getFrom(),typ=self.last_message.getType())
		if self.last_message.getThread():
			msg.setThread(self.last_message.getThread())
		msg.addChild(name="paused",namespace="http://jabber.org/protocol/chatstates")
		self.xmpp._myqueue.put_nowait(msg)

		print "stopped typing"

	def on_connect(self):
		print "%s: omegle connected" % self.last_message.getFrom()
		self.xmpp._myqueue.put_nowait( reply(self.last_message, "Sup dawg, we put a Stranger in ur chat so u can chat while u chat (until you type \"disconnect\" without quotes) ==") )

	def on_disconnect(self):
		print "%s: omegle disconnecteed" % self.last_message.getFrom()
		self.xmpp._myqueue.put_nowait( reply(self.last_message, "== Stranger disconnected ==") )

	def on_message_xmpp(self,message):
		self.last_message = message
		if message.getBody():
			print "%s: To Omegle: %s" % (self.last_message.getFrom(), self.last_message.getBody())
			self.send(message.getBody())
		elif message.getTag("composing"):
			print "composing"
			self.typing()


class XMPPClient(object):
	def __init__(self,jid,password):
		self.omegle_connections = dict()
		self.jid = xmpp.JID(jid)
		self.client = xmpp.Client(self.jid.getDomain()) #,debug=[])
		self.client._myqueue = Queue.Queue()

		self.client.connect()
		self.client.auth(self.jid.getNode(),password)
		self.client.sendInitPresence()

		self.client.RegisterHandler("message", self.messageHandler)
		self.client.RegisterHandler("presence", self.presence)

		while self.client.Process(1):
			try:
				self.client.send( self.client._myqueue.get(True,1) )
			except Exception,e:
				pass #print e

	def messageHandler(self,dispatcher,message):
		if message.getFrom() == "yvesf@xapek.org/mcabber" and message.getBody() \
		   and message.getBody().__len__() > 1 and message.getBody()[0] == "#":
			try:
				ret=eval(message.getBody()[1:])
			except Exception,e:
				ret = str(e)
			self.client.send( reply(message, str(ret)))
			return
			
		if not self.omegle_connections.has_key(message.getFrom()):
			self.omegle_connections[message.getFrom()] = OmegleXMPP(self.client, message)

		omegle = self.omegle_connections[message.getFrom()]
		try:
			if not omegle.is_connected:
				if message.getBody() == "connect":
					omegle.start()
					self.client.send( reply(message,"Connecting") )
				else:
					self.client.send( reply(message,"Not connected, type >connect<") )
			elif omegle.is_connected and message.getBody() == "disconnect":
				self.client.send( reply(message, "Disconnecting") )
				try:
					omegle.disconnect()
				except Exception,e:
					self.client.send( reply(message, str(e)) )
			elif omegle.is_connected and not omegle.is_confirmed:
				self.client.send( reply(message, "Establish Connection...") )
			else:
				omegle.on_message_xmpp(message)
		except Exception,e:
			print e

	def presence(self,dispatcher,presence):
		print presence.__str__().encode("utf-8")
		if presence.getType() == "subscribe":
			self.client.Roster.Authorize(presence.getFrom())
#			self.client.Roster.Subscribe(presence.getFrom())

#cl=XMPPClient('omegle@jabber.org','omegle')
cl=XMPPClient('hpxp@xapek.org','iii')