# HG changeset patch # User Santeri Piippo # Date 1402751115 -10800 # Node ID e843c08ee51e535afa16bb9baa40bf829645c485 # Parent 90851b22ab882381b751221ac9758e95fcc1e568 - added mantisbt new ticket announcing - added admin commands addchan, delchan and chanattr diff -r 90851b22ab88 -r e843c08ee51e cobalt.py --- a/cobalt.py Fri Jun 13 01:43:47 2014 +0300 +++ b/cobalt.py Sat Jun 14 16:05:15 2014 +0300 @@ -68,6 +68,60 @@ except Exception: pass +btannounce_active = False +btannounce_timeout = 0 + +def save_config(): + with open ('cobalt.json', 'w') as fp: + json.dump (g_config, fp, sort_keys = True, indent = 4) + +def cfg (key, default): + if not hasattr (g_config, key): + g_config[key] = default + save_config() + return default + return g_config[key] + +def bt_updatechecktimeout(): + global btannounce_timeout + btannounce_timeout = time.time() + (cfg ('btlatest_checkinterval', 5) * 60) + +if suds_active: + try: + btannounce_id = suds_client.service.mc_issue_get_biggest_id (g_config['trackeruser'], g_config ['trackerpassword'], 0) - 1 + btannounce_active = True + bt_updatechecktimeout() + print "Latest ticket on tracker: %d" % btannounce_id + except Exception as e: + pass + +def bt_getissue(ticket): + global suds_client + global g_config + return suds_client.service.mc_issue_get (g_config['trackeruser'], g_config['trackerpassword'], ticket) + +def bt_checklatest(): + global btannounce_timeout + global btannounce_id + + if time.time() >= btannounce_timeout: + bt_updatechecktimeout() + newid = btannounce_id + try: + newid = suds_client.service.mc_issue_get_biggest_id (g_config['trackeruser'], g_config ['trackerpassword'], 0) + except Exception as e: + pass + + while newid > btannounce_id: + try: + btannounce_id += 1 + data = bt_getissue (btannounce_id) + + for client in g_clients: + client.announce_ticket (data) + except Exception as e: + pass + # # irc_client flags # @@ -145,7 +199,7 @@ self.umode = cfg['umode'] if 'umode' in cfg else '' self.cfg = cfg self.mynick = '' - self.verbose = cfg['verbose'] if 'verbose' in cfg else False + self.verbose = g_config['verbose'] if 'verbose' in g_config else False self.commandprefix = g_config['commandprefix'][0] if 'commandprefix' in g_config else '.' if not 'conflictsuffix' in self.cfg: @@ -229,6 +283,9 @@ self.mynick = '%s%s' % (self.mynick, self.cfg['conflictsuffix']) self.write ("NICK %s" % self.mynick) + # Check for new issues on the bugtracker + bt_checklatest() + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # Handle a PRIVMSG line from the IRC server @@ -264,6 +321,13 @@ # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + # Get the URL for a specified ticket + # + def get_ticket_url (self, ticket): + return 'https://%s/view.php?id=%s' % (g_config['trackerurl'], ticket) + + # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # + # # Retrieve a ticket from mantisbt # def get_ticket_data (self, replyto, ticket, withlink): @@ -272,7 +336,7 @@ data = {} try: - data = suds_client.service.mc_issue_get (g_config['trackeruser'], g_config ['trackerpassword'], ticket) + data = bt_getissue (ticket) except Exception, e: self.privmsg (replyto, "Failed to get info for issue %s: %s" % (ticket, `e`)) @@ -286,7 +350,7 @@ data.resolution.name)) if withlink: - self.privmsg (replyto, "Read all about it here: https://%s/view.php?id=%s" % (g_config['trackerurl'], ticket)) + self.privmsg (replyto, "Read all about it here: " + self.get_ticket_url (ticket)) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # @@ -305,6 +369,11 @@ if len(args) != 1: raise logical_exception ("usage: .%s " % command) self.get_ticket_data (replyto, args[0], True) + elif command == 'testannounce': + check_admin (sender, ident, host, command) + if len(args) != 1: + raise logical_exception ("usage: .%s " % command) + self.announce_ticket (bt_getissue (args[0])) elif command == 'idgames': try: if len(args) < 1: @@ -359,12 +428,84 @@ self.privmsg (replyto, 'Up to date at %s.' % r2) except hgapi.HgException as e: raise logical_exception ('Search failed: %s' % `e`) + elif command == 'addchan': + check_admin (sender, ident, host, command) + if len(args) != 1: + raise logical_exception ("usage: .%s " % command) + + for channel in self.channels: + if channel['name'].upper() == args[0].upper(): + raise logical_exception ('I already know of %s!' % args[0]) + + chan = {} + chan['name'] = args[0] + self.channels.append (chan) + self.write ('JOIN ' + chan['name']) + save_config() + elif command == 'delchan': + check_admin (sender, ident, host, command) + if len(args) != 1: + raise logical_exception ("usage: .%s " % command) + + for channel in self.channels: + if channel['name'].upper() == args[0].upper(): + break; + else: + raise logical_exception ('unknown channel ' + args[0]) + + self.channels.remove (channel) + self.write ('PART ' + args[0]) + save_config() + elif command == 'chanattr': + check_admin (sender, ident, host, command) + + if len(args) < 2: + raise logical_exception ("usage: .%s " % command) + + for channel in self.channels: + if channel['name'] == replyto: + break + else: + raise logical_exception ('I don\'t know of a channel named ' + replyto) + + key = args[0] + value = ' '.join (args[1:]) + + if key == 'name': + self.write ('PART ' + channel['name']) + channel['name'] = value + self.write ('JOIN ' + channel['name'] + ' ' + (channel['password'] if hasattr (channel, 'password') else '')) + elif key == 'password': + channel['password'] = value + elif key == 'btannounce': + if value != 'true' and value != 'false': + raise logical_exception ('expected true or false for value') + channel['btannounce'] = True if value == 'true' else False + else: + raise logical_exception ('unknown key ' + key) + + save_config() elif command == 'die': check_admin (sender, ident, host, command) quit() # else: # raise logical_exception ("unknown command `.%s`" % command) + # + # Print a ticket announce to appropriate channels + # + def announce_ticket (self, data): + idstring = "%d" % data.id + while len(idstring) < 7: + idstring = "0" + idstring + + reporter = data['reporter']['name'] if hasattr (data['reporter'], 'name') else '' + + for channel in self.cfg['channels']: + if 'btannounce' in channel and channel['btannounce'] == True: + self.write ("PRIVMSG %s :New issue %s, reported by %s: %s: %s" % \ + (channel['name'], idstring, reporter, data['summary'], self.get_ticket_url (idstring))) + def handle_error(self): excepterm (traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))