summaryrefslogtreecommitdiff
path: root/chat.py
blob: cbabc37ef4dcbf5488ddb0a050a3d11ec672d661 (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
import urwid
import urwid.curses_display
import sys,time
from omegle import OmegleChat

class ConversationWindow(object):
	def __init__(self):
		self.edit = urwid.Edit(caption="Send: ",edit_text="Hi!",multiline=True)
                self.editbox = urwid.Filler(self.edit)

		self.messages = urwid.SimpleListWalker([])
		self.textbox = urwid.ListBox(self.messages)
		self.body = urwid.Pile([('weight',1,self.editbox),('fixed',3,urwid.Filler(urwid.Divider('-'))),('weight',10,self.textbox)])

		self.widget_header = urwid.Text("Omegle    |   F2 - Connect/Disconnect  |   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.editbox)]]

	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.editbox:
                                    self.on_send(self.edit.get_edit_text())
                                else:
                                    pass
                            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.on_toggle_connect()
                            elif k == "f10":
                                self.on_exit()
                            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_send(self,message):
		self.add_message("DEBUG: on_column1_send message=%s"%message)
        def on_exit(self):
            sys.exit(0)
        def on_toggle_connect(self):
            pass

class Omegle(OmegleChat):
    def __init__(self,chatwindow,host="omegle.com"):
        OmegleChat.__init__(self,host=host)
        self.chatwindow = chatwindow

    def on_message(self,message):
        self.chatwindow.add_message("%s Stranger: %s"%(time.strftime("%H:%M:%S"), message))
    def on_connect(self):
        self.chatwindow.add_message("Connected")
    def on_typing(self):
        self.chatwindow.add_message("Stranger typing")
    def on_stopped_typing(self):
        self.chatwindow.add_message("Stranger stopped typing")
    def on_disconnect(self):
        self.chatwindow.add_message("Stranger disconnected")
    def on_wait(self):
        self.chatwindow.add_message("Server send Wait")

class Chat(ConversationWindow):
    def __init__(self,host="omegle.com"):
        print "Chat(host=%s)" % (host)
        ConversationWindow.__init__(self)
        self.omegle = Omegle(self,host=host)

    def on_toggle_connect(self):
        if self.omegle.is_connected:
            self.add_message("Initiating disconnect")
            self.omegle.disconnect()
        else:
            self.add_message("Initiating connect")
            self.omegle.start()

    def on_exit(self):
        if self.omegle.is_connected:
            self.omegle.disconnect()
        sys.exit(0)

    def on_send(self,message):
        if self.omegle.is_confirmed:
            self.add_message("%s You: %s"%(time.strftime("%H:%M:%S"), message))
            self.omegle.send(message)
            self.edit.set_edit_text("")
        else:
            self.add_message("Not connected/confirmed")

if __name__ == "__main__":
    import logging
    logging.basicConfig(filename="chat.py.log",level=logging.DEBUG)
    Chat(host="localhost:8082").main()

"""
Unterstuetzt im moment noch nicht das senden des /typing
partner sieht nicht wenn man tippt
"""