From c04a8621abde66c598569c0e1177b10fdbfe52eb Mon Sep 17 00:00:00 2001 From: yvesf Date: Fri, 29 May 2009 18:58:02 +0000 Subject: cleanup git-svn-id: http://xapek.org/svn/common/omegle@1048 d0e8fea9-7529-0410-93fb-d39fd5b9c1dd --- gui.py | 120 ----------------------------------------------------------- injector.py | 122 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ omegle.py | 28 +++++++++++--- proxy.py | 11 ++---- 4 files changed, 148 insertions(+), 133 deletions(-) delete mode 100644 gui.py create mode 100644 injector.py diff --git a/gui.py b/gui.py deleted file mode 100644 index 2af9f62..0000000 --- a/gui.py +++ /dev/null @@ -1,120 +0,0 @@ -import urwid.curses_display -import urwid -import sys,time - -class Conversation(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") - -Conversation().main() diff --git a/injector.py b/injector.py new file mode 100644 index 0000000..0934b1b --- /dev/null +++ b/injector.py @@ -0,0 +1,122 @@ +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") + +class Injector(InjectorWindow): + pass +Injector().main() diff --git a/omegle.py b/omegle.py index a5a07c1..ab64c09 100644 --- a/omegle.py +++ b/omegle.py @@ -37,6 +37,7 @@ class Timer(Thread): self.event.set() class RESTClient(object): + """Some routines used for calling the webservice""" headers = { "Host" : "omegle.com", "Content-type": "application/x-www-form-urlencoded; charset=utf-8", @@ -52,20 +53,21 @@ class RESTClient(object): self.conn.request(method,path,params,RESTClient.headers) resp = self.conn.getresponse() self.conn_lock.release() - return resp class OmegleChat(RESTClient): def __init__(self,poll_interval=0.5,name=""): RESTClient.__init__(self,'www.omegle.com') - self.timer = Timer(self.events, poll_interval) + self.timer = Timer(self._events, poll_interval) self.logger = logging.getLogger(__name__ + "." + self.__class__.__name__ + name) self.is_confirmed = False #after getting a connection confirm self.is_connected = False #after having self.id def start(self): + """Starts a chat session. You have to wait until + is_confirmed is set (or use on_connect)""" self.logger.debug("/start") if self.is_connected: self.logger.error("Already connected") @@ -76,7 +78,7 @@ class OmegleChat(RESTClient): if id.__len__() == 3: self.id = id[1] self.logger.info("Connected id=%s" % self.id) - #Poll events + #start /events polling self.timer.start() self.is_connected = True else: @@ -84,11 +86,13 @@ class OmegleChat(RESTClient): raise Exception("Bad response: %s" % body) def disconnect(self): + """Close a chat session.""" self.logger.debug("disconnect()") self.is_connected = False self.timer.stop() if self.is_confirmed: self.is_confirmed = False + self.id = None r = RESTClient.request(self,"POST", "/disconnect", urllib.urlencode({'id': self.id})) body = r.read() self.logger.debug("/disconnect sent. Read: %s" % body) @@ -99,7 +103,7 @@ class OmegleChat(RESTClient): self.is_confirmed = True raise Exception("/disconnect; Bad response: %s" % body) - def events(self): + def _events(self): """does use its own "HTTPConnection" because its called async from a thread""" conn = httplib.HTTPConnection('www.omegle.com') conn.request("POST", @@ -108,10 +112,14 @@ class OmegleChat(RESTClient): OmegleChat.headers) r=conn.getresponse() body=r.read() - self.dispatch_event(conn,body) + self._dispatch_event(conn,body) conn.close() def send(self,msg): + """Send a message to the Stranger + Arguments: + msg -- the message - in utf8 + """ #if a raw string given, assume its utf8 if msg.__class__ == str: msg = msg.decode("utf8") @@ -125,6 +133,7 @@ class OmegleChat(RESTClient): raise Exception("/send; Bad response %s" % body) def typing(self): + """Notify the Stranger that youre typing a message""" r = RESTClient.request(self, "POST", "/typing", urllib.urlencode({'id':self.id})) body = r.read() if body != "win": @@ -132,13 +141,14 @@ class OmegleChat(RESTClient): raise Exception("/typing; Bad response %s" % body) def stopped_typing(self): + """Notify the Stranger that youre stopped typing""" r = RESTClient.request(self, "POST", "/stoppedtyping", urllib.urlencode({'id':self.id})) body = r.read() if body != "win": self.logger.error("/stoppedtyping; Bad response %s" % body) raise Exception("/stoppedtyping; Bad response %s" % body) - def dispatch_event(self,conn,body): + def _dispatch_event(self,conn,body): try: data_set=json.read(body.decode("utf8")) if not data_set: @@ -167,16 +177,22 @@ class OmegleChat(RESTClient): self.logger.error("Json ReadException. Body: %s" % body) def on_message(self,message): + """To be overwritten""" self.logger.info("<<< %s" % message) def on_connect(self): + """To be overwritten""" self.logger.info("Connection confirmed") def on_typing(self): + """To be overwritten""" self.logger.info("Stranger is typing") def on_stopped_typing(self): + """To be overwritten""" self.logger.info("Stranger stopped typing") def on_disconnect(self): + """To be overwritten""" self.logger.info("Stranger Disconnectet") def on_wait(self): + """To be overwritten""" self.logger.info("Server sent [\"waiting\"]") def get_count(): diff --git a/proxy.py b/proxy.py index 957b116..2ea2798 100644 --- a/proxy.py +++ b/proxy.py @@ -8,12 +8,6 @@ from omegle import OmegleChat __all__ = ['OmegleProxyChat'] -colors = {"default":0, "black":30, "red":31, "green":32, "yellow":33, - "blue":34,"magenta":35, "cyan":36, "white":37, "black":38, "black":39} - - - - class OmegleProxyChat(OmegleChat): """Omegle class with link to other "partner" instance""" def __init__(self,disconnect_event,name,color=0): @@ -58,8 +52,11 @@ class OmegleProxyChat(OmegleChat): 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" + logging.getLogger().info("press ctrl-c to abort") event = Event() A=OmegleProxyChat(event,"A",colors['red']) -- cgit v1.2.1