-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathttvChat.py
85 lines (68 loc) · 2.18 KB
/
ttvChat.py
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
#!/usr/bin/python
# === internal module ===
import threading
import os
# === external module ===
from twisted.internet import reactor
# === project module ===
from configLoader import ConfigLoader
from ui import TtvChatAppUI
from bot import IrcBotFactory
# load ini
print 'loading setting....'
p = ConfigLoader()
settings = p.load('settings.ini')
# init UI thread
print 'preparing UI thread....'
class uiThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.appUI = TtvChatAppUI(None)
self.appUI.title('moo Chat')
self.start()
def run(self):
print '\n===UI thread online===\n'
# block here
self.appUI.mainloop()
# end the main thread when UI is closed
os._exit(1)
uiThread = uiThread()
# load parsing bot
print 'preparing irc bot...'
f = IrcBotFactory(settings)
reactor.connectTCP(settings['ircServer'], settings['ircPort'], f)
# send bot msg queue to UI
print 'preparing recv thread...'
# improve thread safe
class recvMsgThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.start()
def run(self):
print '\n===recv thread online===\n'
if uiThread.appUI:
while 1:
# pass received msg from bot to UI
botRecvMsg = f.botRecvMsgQueue.get()
uiThread.appUI.uiRecvMsgQueue.put(botRecvMsg)
recvMsgThread = recvMsgThread()
# send UI msg queue to bot
print 'preparing send thread...'
class sendMsgThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.start()
def run(self):
print '\n===send thread online===\n'
if uiThread.appUI:
while 1:
# pass ui sent msg to bot
uiSentMsg = uiThread.appUI.uiSentMsgQueue.get()
if uiSentMsg:
f.protocol.say(settings['channel'] , uiSentMsg['msg'])
#send pure msg to IRC server
#f.protocol.sendLine(uiSentMsg['msg'])
sendMsgThread = sendMsgThread()
# bot main loop
print 'parsing bot online'
reactor.run()