cobalt.py

changeset 5
b6d2b7de0a6d
parent 4
bd9508f8a10f
child 6
2c6265a99d32
equal deleted inserted replaced
4:bd9508f8a10f 5:b6d2b7de0a6d
34 import traceback 34 import traceback
35 import re 35 import re
36 import json 36 import json
37 import urllib 37 import urllib
38 import urllib2 38 import urllib2
39 import hgapi
40 import os
39 from suds.xsd.doctor import Import 41 from suds.xsd.doctor import Import
40 from suds.xsd.doctor import ImportDoctor 42 from suds.xsd.doctor import ImportDoctor
41 from suds.client import Client 43 from suds.client import Client
42 44
43 try: 45 try:
102 for segment in data: 104 for segment in data:
103 for line in segment.splitlines(): 105 for line in segment.splitlines():
104 print line 106 print line
105 control (line) 107 control (line)
106 for client in g_clients: 108 for client in g_clients:
107 client.exceptdie() 109 if len(data) > 0:
108 quit() 110 client.exceptdie()
111 else:
112 client.quit_irc()
113 restart_self()
114 #quit()
109 115
110 sys.excepthook = handle_exception 116 sys.excepthook = handle_exception
111 117
112 def check_admin (sender, ident, host, command): 118 def check_admin (sender, ident, host, command):
113 if not "%s!%s@%s" % (sender, ident, host) in g_admins: 119 if not "%s!%s@%s" % (sender, ident, host) in g_admins:
116 class logical_exception (Exception): 122 class logical_exception (Exception):
117 def __init__ (self, value): 123 def __init__ (self, value):
118 self.value = value 124 self.value = value
119 def __str__ (self): 125 def __str__ (self):
120 return self.value 126 return self.value
127
128 # from http://www.daniweb.com/software-development/python/code/260268/restart-your-python-program
129 def restart_self():
130 python = sys.executable
131 os.execl (python, python, * sys.argv)
121 132
122 # 133 #
123 # Main IRC client class 134 # Main IRC client class
124 # 135 #
125 class irc_client (asyncore.dispatcher): 136 class irc_client (asyncore.dispatcher):
193 self.write ('MODE %s %s' % (self.mynick, self.cfg['umode'])) 204 self.write ('MODE %s %s' % (self.mynick, self.cfg['umode']))
194 205
195 if len(words) >= 2 and words[1] == "PRIVMSG": 206 if len(words) >= 2 and words[1] == "PRIVMSG":
196 self.handle_privmsg (line) 207 self.handle_privmsg (line)
197 208
209 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
210 #
211 # Handle a PRIVMSG line from the IRC server
212 #
198 def handle_privmsg (self, line): 213 def handle_privmsg (self, line):
199 rex = re.compile (r'^:([^!]+)!([^@]+)@([^ ]+) PRIVMSG ([^ ]+) :(.+)$') 214 rex = re.compile (r'^:([^!]+)!([^@]+)@([^ ]+) PRIVMSG ([^ ]+) :(.+)$')
200 match = rex.match (line) 215 match = rex.match (line)
201 if match: 216 if match:
202 sender = match.group (1) 217 sender = match.group (1)
222 elif http_match: 237 elif http_match:
223 self.get_ticket_data (replyto, http_match.group (2), False) 238 self.get_ticket_data (replyto, http_match.group (2), False)
224 else: 239 else:
225 control ("Recieved bad PRIVMSG: %s" % line) 240 control ("Recieved bad PRIVMSG: %s" % line)
226 241
242 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
243 #
244 # Retrieve a ticket from mantisbt
245 #
227 def get_ticket_data (self, replyto, ticket, withlink): 246 def get_ticket_data (self, replyto, ticket, withlink):
228 if suds_active == False: 247 if suds_active == False:
229 return 248 return
230 249
231 data = {} 250 data = {}
244 data.resolution.name)) 263 data.resolution.name))
245 264
246 if withlink: 265 if withlink:
247 self.privmsg (replyto, "Read all about it here: https://%s/view.php?id=%s" % (g_config['trackerurl'], ticket)) 266 self.privmsg (replyto, "Read all about it here: https://%s/view.php?id=%s" % (g_config['trackerurl'], ticket))
248 267
268 # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #
269 #
270 # Process an IRC command
271 #
249 def handle_command (self, sender, ident, host, replyto, command, args): 272 def handle_command (self, sender, ident, host, replyto, command, args):
250 if command == "raw": 273 if command == "raw":
251 check_admin (sender, ident, host, command) 274 check_admin (sender, ident, host, command)
252 self.write (" ".join (args)) 275 self.write (" ".join (args))
253 elif command == "msg": 276 elif command == "msg":
292 raise logical_exception ("Incomplete JSON response from doomworld.com/idgames") 315 raise logical_exception ("Incomplete JSON response from doomworld.com/idgames")
293 except logical_exception as e: 316 except logical_exception as e:
294 raise e 317 raise e
295 except Exception as e: 318 except Exception as e:
296 raise logical_exception ('Search failed: %s' % `e`) 319 raise logical_exception ('Search failed: %s' % `e`)
297 else: 320 elif command == 'restart':
298 raise logical_exception ("unknown command `.%s`" % command) 321 check_admin (sender, ident, host, command)
322 excepterm('')
323 elif command == 'update':
324 check_admin (sender, ident, host, command)
325
326 try:
327 repo = hgapi.Repo ('.')
328 r1 = repo.hg_id()
329 repo.hg_pull()
330 repo.hg_update('tip', True)
331 r2 = repo.hg_id()
332 self.privmsg (replyto, '%s <-> %s' % (r1, r2))
333 if r1 != r2:
334 self.privmsg (replyto, 'Updated to %s, restarting...' % r2)
335 excepterm('')
336 else:
337 self.privmsg (replyto, 'Up to date at %s.' % r2)
338 except hgapi.HgException as e:
339 raise logical_exception ('Search failed: %s' % `e`)
340 # else:
341 # raise logical_exception ("unknown command `.%s`" % command)
299 342
300 def handle_error(self): 343 def handle_error(self):
301 excepterm (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback)) 344 excepterm (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
302 345
303 def privmsg (self, channel, msg): 346 def privmsg (self, channel, msg):
304 self.write ("PRIVMSG %s :%s" % (channel, msg)) 347 self.write ("PRIVMSG %s :%s" % (channel, msg))
348
349 def quit_irc (self):
350 if self.flags & CLIF_CONNECTED:
351 self.write ("QUIT :Leaving")
352 self.send_all_now()
353 self.close()
305 354
306 def exceptdie (self): 355 def exceptdie (self):
307 if self.flags & CLIF_CONNECTED: 356 if self.flags & CLIF_CONNECTED:
308 self.write ("QUIT :Caught exception") 357 self.write ("QUIT :Caught exception")
309 self.send_all_now() 358 self.send_all_now()

mercurial