summaryrefslogtreecommitdiff
path: root/proxy.py
blob: d37d9dfab357e044d8b667ba5f962652908745d6 (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
import sys
import time
import logging
from threading import Event
from omegle import OmegleChat


__all__ = ['OmegleProxyChat']


class OmegleProxyChat(OmegleChat):
	"""Omegle class  with link to other "partner" instance"""
	def __init__(self,disconnect_event,name,color=0):
		OmegleChat.__init__(self,name="(" + name + ")")
		self.disconnect_event = disconnect_event
		self.name = name
		self.color = color
		self.idlecount = 0
		self.partner = None

	def c(self,str,bold=False):
		if bold:
			return "\033[1m\033[%sm%s\033[0m\033[0m" %(self.color, str)
		else:
			return "\033[%sm%s\033[0m" %(self.color, str)

	def set_partner(self,partner):
		self.partner = partner

	def on_idle(self):
		self.idlecount += 1
		if self.idlecount > 8:
			self.logger.info("Idle > 8. disconnect()")
			try:
				self.disconnect()
			except:
				self.disconnect_event.set()
		else:
			self.logger.info("Idle count %s/8" % self.idlecount)

	def on_message(self,message):
		self.idlecount = 0
		self.partner.idlecount = 0
		print self.c("%s [MSG] %s: %s" %(time.strftime("%H:%M:%S"), self.name, message.encode("utf-8")),bold=True)
		if self.partner.is_connected:
			try:
				self.partner.send(message)
			except Exception,e:
				print e
				try:
					self.disconnect()
				except:
					pass
				self.disconnect_event.set()
				

	def on_connect(self):
		print self.c("%s [EVT] %s Connection confirmed" % (time.strftime("%H:%M:%S"), self.name))

	def on_typing(self):
		self.idlecount = 0
		self.partner.idlecount = 0
		print self.c("%s [EVT] %s is typing" % (time.strftime("%H:%M:%S"), self.name))
		if self.partner.is_connected:
			self.partner.typing()

	def on_stopped_typing(self):
		print self.c("%s [EVT] %s stopped typing" % (time.strftime("%H:%M:%S"), self.name))
		if self.partner.is_connected:
			self.partner.stopped_typing()

	def on_disconnect(self):
		print self.c("%s [EVT] %s disconnect" % (time.strftime("%H:%M:%S"), self.name))
		if self.partner.is_connected:
			self.partner.disconnect()
		self.disconnect_event.set()



if __name__ == "__main__":
	colors = {"default":0, "black":30, "red":31, "green":32, "yellow":33,
		  "blue":34,"magenta":35, "cyan":36, "white":37, "black":38, "black":39}

	logging.basicConfig(level=logging.DEBUG)
	print "press ctrl-c to abort"
	
	event = Event()
	A=OmegleProxyChat(event,"A",colors['red'])
	B=OmegleProxyChat(event,"B",colors['blue'])

	A.set_partner(B)
	B.set_partner(A)

	try:
		A.start()
		B.start()
	except:
		sys.exit(255)

	try:
		while True:
			event.wait(0.5)
			if event.isSet():
				print "One part disconnected, exiting"
				break
	except KeyboardInterrupt:
		print "CTRL-C pressed, exiting"
	try:
		if A.is_connected: A.disconnect()
	except:
		pass

	try:
		 if B.is_connected: B.disconnect()
	except:
		 pass