forked from d-dd/Yukari
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtacfile.py
299 lines (259 loc) · 10.4 KB
/
tacfile.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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
# Standard Library
from collections import deque
import importlib
import os
import subprocess
import sys
import textwrap
import time
# Twisted Library
from twisted.application import service
from twisted.conch import manhole_tap
from twisted.internet import reactor, defer
from twisted.internet.defer import Deferred
# Yukari
# add home directory to sys.path
sys.path.append(os.getcwd())
from connections.cytube.cyClient import WSService
import connections.cytube.cyProfileChange as cyProfileChange
from connections.ircClient import IrcService
from conf import config
import database, tools
from tools import clog
sys = 'Yukari'
def importPlugins(path):
try:
files = os.listdir(path)
except(OSError):
clog.error('Error importing plugins. Invalid path.', sys)
importPath = path.replace('/', '.')
moduleNames = [importPath + i[:-3] for i in files
if not i.startswith('_') and i.endswith('.py') and not i.startswith('test')]
modules = map(importlib.import_module, moduleNames)
return modules
class Yukari(service.MultiService):
""" Handles connections to a Cytube server and IRC, as well as
any communication between them."""
def __init__(self):
super(Yukari, self).__init__()
# import plugins
self._importPlugins()
# False = Offline, True = Online, None = has shutdown
self.irc = False
self.cy = False
self.cyUserdict = {}
self.inIrcChan = False
self.inIrcNp = False
self.inIrcStatus = False
# Wether to restart when disconnected
self.ircRestart = True
self.cyRestart = True
self.cyLastConnect = 0
self.startTime = time.time()
# Remember the git-hash when this instance is created (non-atomic)
self.version = subprocess.check_output(['git', 'rev-parse',
'--short', 'HEAD']).strip()
# Users in IRC chat channel
self.ircUserCount = 0
self.ircChan = str(config['irc']['channel'])
if self.ircChan and not self.ircChan.startswith('#'):
self.ircChan = '#' + self.ircChan
self.cyName = str(config['Cytube']['username'])
self.lastIrcChat = 0
def _importPlugins(self):
modules = importPlugins('plugins/')
self.triggers = {'commands':{}}
for module in modules:
instance = module.setup()
for method in dir(instance):
# commands in cytube chat
if method.startswith('_com_'):
trigger = '%s' % method[5:]
self.triggers['commands'][trigger] = getattr(instance, method)
clog.info('Imported %s!' % trigger, sys)
def cyChangeProfile(self):
""" Change Yukari's profile picture and text on CyTube """
d = database.getCurrentAndMaxProfileId()
d.addCallback(self.cbChangeProfile)
return d
def cbChangeProfile(self, res):
#clog.debug('(cbChangeProfile) %s' % res, sys)
if len(res) < 2: # no flagged row
clog.error('(cbChangeProfile) CyProfile table incorrect.', sys)
return defer.fail(None)
currentRow = res[0][0]
maxRow = res[1][0]
if currentRow == maxRow:
nextRow = 1
else:
nextRow = currentRow + 1
d = database.getProfile(nextRow)
d.addCallback(self.setNewProfile, currentRow, nextRow)
return d
def setNewProfile(self, res, currentRow, nextRow):
clog.debug('(setNewProfile) %s' % res, sys)
name = config['Cytube']['username']
password = config['Cytube']['password']
text = res[0][1]
imgurl = res[0][2]
d = cyProfileChange.changeProfileInfo(text, imgurl)
d.addCallback(self.setProfileFlags, currentRow, nextRow)
return d
def setProfileFlags(self, ignored, currentRow, nextRow):
# set/unset flags only after setNewProfile succeeds
database.setProfileFlag(currentRow, 0) # unset current profile flag
database.setProfileFlag(nextRow, 1) # set next profile flag
def cyPostErr(self, err):
clog.error(err, sys)
return err
def recIrcMsg(self, user, channel, msg, modifier=None):
self.lastIrcChat = time.time()
user = user.split('!', 1)[0] # takes out the extra info in the name
# cytube char limit per line is 244
pre = ''
if self.cy:
max_width = 244 - (len(user) + len('[..]')*2 + 10)
msgd = deque(textwrap.wrap(msg, max_width))
while msgd:
cont = '[..]' if len(msgd) > 1 else ''
line = '(%s) %s %s %s' % (user, pre, msgd.popleft(), cont)
if modifier:
line = '_%s _' % line
self.wsFactory.prot.relayToCyChat(line)
pre = '[..]'
# don't process commands from action (/me) messages
if not modifier:
if self.irc:
prot = self.ircFactory.prot
self.processCommand('irc', user, tools.returnUnicode(msg),
prot=prot)
def recCyMsg(self, source, user, msg, needProcessing, action=False):
if self.inIrcChan and user != self.cyName and source != 'pm':
clog.debug('recCyMsg: %s' % msg, sys)
if not action:
cleanMsg = '(%s) %s' % (user, msg)
else:
cleanMsg = '( * %s) %s' % (user, msg)
self.sendToIrc(cleanMsg)
if needProcessing and not action and self.cy:
self.processCommand(source, user, msg, prot=self.wsFactory.prot)
def recCyChangeMedia(self, media):
if self.inIrcNp and media:
mType, mId, title = media
if mType == 'yt':
link = 'https://youtu.be/%s' % mId
msg = '[Now Playing]: %s %s' % (title, link)
else:
msg = '[Now Playing]: %s (%s, %s)' % (title, mType, mId)
msg = tools.returnStr(msg)
self.ircFactory.prot.sayNowPlaying(msg)
def recCyUserlist(self, userdict):
# called from cyClient, when cyCall userlist
self.cyUserdict = userdict
if self.inIrcStatus:
self.ircFactory.prot.sendCyNames()
def recCyUserJoin(self, user, rank):
if self.inIrcStatus:
self.ircFactory.prot.sendCyUserJoin(user, rank)
def recCyUserLeave(self, user):
if self.inIrcStatus:
self.ircFactory.prot.sendCyUserLeave(user)
def processCommand(self, source, user, msg, prot):
if msg.startswith('$'):
msg = tools.returnUnicode(msg)
#msg = msg.encode('utf-8')
command = msg.split()[0][1:]
argsList = msg.split(' ', 1)
if len(argsList) == 2:
args = argsList[1]
else:
args = None
if command in self.triggers['commands']:
clog.info('triggered command: [%s] args: [%s]' %
(command, args), sys)
self.triggers['commands'][command](self, user, args, source,
prot=prot)
def reply(self, msg, source, username, modflair=False, action=False):
# public chat: send to both
if source == 'chat' or source == 'irc':
self.sendChats(msg, modflair, action)
elif source == 'pm' and self.cy:
if action:
# no /me in Cytube PM
msg = '_* %s_' % msg
self.wsFactory.prot.doSendPm(msg, username)
def sendToIrc(self, msg, action=False):
if not self.inIrcChan:
return
self.ircFactory.prot.sendChat(msg, action)
def sendToCy(self, msg, modflair=False):
if self.cy:
self.wsFactory.prot.relayToCyChat(msg, modflair)
def sendChats(self, msg, modflair=False, action=False):
self.sendToIrc(msg, action)
if action:
self.sendToCy('/me %s' % msg, modflair)
else:
self.sendToCy(msg, modflair)
def cyAnnounceLeftRoom(self):
msg = ('[status] Left Cytube channel.')
self.sendToIrc(msg)
if self.irc:
self.ircFactory.prot.setOfflineNick()
def cyAnnounceConnect(self):
msg = ('[status] Connected to Cytube.')
self.sendToIrc(msg)
if self.irc:
self.ircFactory.prot.setOnlineNick()
def cleanup(self):
""" Prepares for shutdown """
# Starts pre-shutdown cleanup
clog.info('(cleanup) Cleaning up for shutdown!', sys)
self.done = Deferred()
#self.done.callback(None)
# return self.done
# if nothing has started, shutdown immediately.
if not self.irc and not self.cy:
clog.info('(cleanup) Nothing to clean!', sys)
self.done.callback(None)
if self.irc:
self.ircFactory.prot.partLeave('Shutting down.')
if self.cy:
self.cyRestart = False
self.wsFactory.prot.sendClose()
self.wsFactory.stopTrying()
return self.done
def doneCleanup(self, protocol):
""" Fires the done deferred, which unpauses the shutdown sequence """
# If the application is stuck after Ctrl+C due to a bug,
# use telnet(manhole) to manually fire the 'done' deferred.
clog.warning('(doneCleanup) CLEANUP FROM %s' % protocol, sys)
if protocol == 'irc':
self.irc = None
clog.info('(doneCleanup) Done shutting down IRC.', sys)
elif protocol == 'cy':
self.cy = None
clog.info('(doneCleanup) Done shutting down Cy.', sys)
if not self.irc and not self.cy:
self.done.callback(None)
application = service.Application("app")
yukService = Yukari()
yukService.setServiceParent(application)
from twisted.conch import manhole_tap
manhole_service = manhole_tap.makeService({
"telnetPort": "tcp:{}".format(config['telnet']['port']),
"sshPort": None,
"namespace": {"y": yukService},
"passwd": "telnet.pw",
})
manhole_service.setName("manhole")
manhole_service.setServiceParent(yukService)
# cytube
ws_service = WSService()
ws_service.setName("cy")
ws_service.setServiceParent(yukService)
# irc
irc_service = IrcService()
irc_service.setName("irc")
irc_service.setServiceParent(yukService)
reactor.addSystemEventTrigger('before', 'shutdown', yukService.cleanup)