summaryrefslogtreecommitdiff
path: root/injector.py
blob: d84481d0d64621e858f53790121f438f856a31fc (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
import urwid.curses_display
import urwid
import sys,time

class InjectorWindow(object):
	def __init__(self):
		self.column1_edit = urwid.Edit(caption="Send: ")
		self.column1_check = urwid.CheckBox("Link to other",state=True,on_state_change=self.on_link1_changed)
		self.column1_disconnect = urwid.Button("Disconnect", self.on_column1_disconnect)
		self.column1 = urwid.ListBox([
			self.column1_edit, 
			self.column1_check, 
			urwid.AttrWrap(self.column1_disconnect,'button')])
		
		self.column2_edit = urwid.Edit(caption="Send: ")
		self.column2_check = urwid.CheckBox("Link to other",state=True,on_state_change=self.on_link2_changed)
		self.column2_disconnect = urwid.Button("Disconnect", self.on_column1_disconnect)
		self.column2 = urwid.ListBox([
			self.column2_edit,
			self.column2_check,
			urwid.AttrWrap(self.column2_disconnect,'button')])
	
		self.columns = urwid.Columns([self.column1, self.column2])

		self.messages = urwid.SimpleListWalker([urwid.Text("bbalasdasd"), urwid.Text("blasasd")])
		self.textbox = urwid.ListBox(self.messages)
		self.body = urwid.Pile([self.columns,self.textbox])

		self.widget_header = urwid.Text("F2 Toggle Link 1 | F3 Toggle Link 2 |           | F10 - EXIT")

		self.top = urwid.Frame(body=self.body, header=urwid.AttrWrap(self.widget_header,'header'))

		self.focus_ring = [ 
							[(self.body,self.textbox)],
							[(self.body,self.columns), (self.columns, self.column1), (self.column1, 0)], 
							[(self.body,self.columns), (self.columns, self.column1), (self.column1, 1)], 
							[(self.body,self.columns), (self.columns, self.column1), (self.column1, 2)],
							[(self.body,self.columns), (self.columns, self.column2), (self.column2, 0)],
							[(self.body,self.columns), (self.columns, self.column2), (self.column2, 1)],
							[(self.body,self.columns), (self.columns, self.column2), (self.column2, 2)],
		]

	def add_message(self,msg):
		w=urwid.Text(msg)
		self.messages.contents.append(w)
		self.textbox.set_focus(len(self.messages.contents))

	def main(self):
		self.ui = urwid.curses_display.Screen()
		self.ui.register_palette([
			('header', 'black', 'dark cyan', 'standout'),
			('button', 'white', 'dark blue', 'standout'),
			('keylabel', 'white', 'dark cyan', 'standout'),
			])
		self.ui.run_wrapper( self.run )
	
	def run(self):
		size = self.ui.get_cols_rows()
		running=True
		try: 
			while running:
				keys = self.ui.get_input()

				for k in keys:
					if k == "window resize":
						size = self.ui.get_cols_rows()
					elif k == "enter":
						if self.body.get_focus() == self.columns \
							and self.columns.get_focus() == self.column1 \
							and  self.column1.get_focus()[0] == self.column1_edit:
							self.on_column1_send(self.column1_edit.get_text()[0])
						elif self.body.get_focus() == self.columns \
							and self.columns.get_focus() == self.column2 \
							and  self.column2.get_focus()[0] == self.column2_edit:
							self.on_column2_send(self.column2_edit.get_text()[0])
						else:
							self.top.keypress( size, k )
					elif k == "tab":
						self.focus_ring.reverse()
						elem = self.focus_ring.pop()
						for pair in elem:
							pair[0].set_focus(pair[1])
						self.focus_ring.insert(0,elem)
						self.focus_ring.reverse()
					elif k == "f2":
						self.column1_check.toggle_state()
					elif k == "f3":
						self.column2_check.toggle_state()
					elif k == "f10":
						running=False
					else:
						self.top.keypress( size, k )

				self.draw_screen( size )
		except KeyboardInterrupt:
			pass
	
	def draw_screen(self, size):
		canvas = self.top.render( size, focus=True )
		self.ui.draw_screen( size, canvas )

	def on_link1_changed(self,*args):
		self.add_message("DEBUG: on_link1_changed")

	def on_link2_changed(self,a,b):
		self.add_message("DEBUG: on_link2_changed")

	def on_column1_send(self,message):
		self.add_message("DEBUG: on_column1_send message=%s"%message)

	def on_column2_send(self,message):
		self.add_message("DEBUG: on_column2_send message=%s"%message)

	def on_column1_disconnect(self,*args):
		self.add_message("DEBUG: on_column1_disconnect")

	def on_column2_disconnect(self,*args):
		self.add_message("DEBUG: on_column2_disconnect")


from omegle import OmegleChat

class Chat(OmegleChat):
	def __init__(self,injector,name):
		OmegleChat.__init__(self)
		self.injector = injector
		self.name = name
	def on_message(self,msg):
		#XXX handle linking
		injector.add_message("%s %s : %s" % (1, self.name, msg))
class Injector(InjectorWindow):
	def __init__(self):
		InjectorWindow.__init__(self)
		self.omegle1 = Chat(self,"A")
		self.omegle2 = Chat(self,"B")
	def on_column1_send(self,message):
		if self.omegle1.is_confirmed:
			self.omegle1.send(message)
		else:
			self.add_message("Column 1 not connected")
	def on_column2_send(self,message):
		if self.omegle1.is_confirmed:
			self.omegle2.send(message)
		else:
			self.add_message("Column 2 not connected")
	def on_column1_disconnect(self,*args):
		if self.omegle1.is_connected:
			self.omegle1.disconnect()
			self.add_message("Column1 disconnected")
		else:
			self.comegle1.start()
			self.add_message("Connect Column1")
	def on_column2_disconnect(self,*args):
		if self.omegle2.is_connected:
			self.omegle2.disconnect()
			self.add_message("Column2 disconnected")
		else:
			self.comegle2.start()
			self.add_message("Connect Column2")


Injector().main()