Sat, 21 Jun 2014 16:42:03 +0300
- control connections turned into log channels
cobalt.py | file | annotate | diff | comparison | revisions |
--- a/cobalt.py Sat Jun 21 16:09:32 2014 +0300 +++ b/cobalt.py Sat Jun 21 16:42:03 2014 +0300 @@ -82,6 +82,11 @@ global btannounce_timeout btannounce_timeout = time.time() + (cfg ('btlatest_checkinterval', 5) * 60) +def bool_from_string (value): + if value != 'true' and value != 'false': + raise logical_exception ('expected true or false for value') + return True if value == 'true' else False + if suds_active: try: btannounce_id = suds_client.service.mc_issue_get_biggest_id (g_config['trackeruser'], g_config ['trackerpassword'], 0) @@ -121,7 +126,6 @@ # # irc_client flags # -CLIF_CONTROL = (1 << 0) CLIF_CONNECTED = (1 << 1) # @@ -137,12 +141,16 @@ self.name = name # -# Prints a line to control channel(s) +# Prints a line to log channel(s) # -def control (line): +def chanlog (line): for client in g_clients: - if client.flags & (CLIF_CONTROL|CLIF_CONNECTED) == (CLIF_CONTROL|CLIF_CONNECTED): - client.write ("PRIVMSG %s :%s" % (client.channels[0]['name'], line)) + if not client.flags & CLIF_CONNECTED: + continue + + for channel in client.channels: + if channel['logchannel']: + client.write ("PRIVMSG %s :%s" % (channel['name'], line)) # # Exception handling @@ -154,12 +162,14 @@ for segment in data: for line in segment.splitlines(): print line - control (line) + chanlog (line) + for client in g_clients: if len(data) > 0: client.exceptdie() else: client.quit_irc() + restart_self() #quit() @@ -198,6 +208,10 @@ self.verbose = g_config['verbose'] if 'verbose' in g_config else False self.commandprefix = g_config['commandprefix'][0] if 'commandprefix' in g_config else '.' + for channel in self.channels: + if not 'logchannel' in channel: + channel['logchannel'] = False + if not 'conflictsuffix' in self.cfg: self.cfg['conflictsuffix'] = '`' @@ -319,7 +333,7 @@ elif http_match: self.get_ticket_data (replyto, http_match.group (2), False) else: - control ("Recieved bad PRIVMSG: %s" % line) + chanlog ("Recieved bad PRIVMSG: %s" % line) # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # @@ -441,6 +455,7 @@ chan = {} chan['name'] = args[0] + chan['logchannel'] = False self.channels.append (chan) self.write ('JOIN ' + chan['name']) save_config() @@ -461,8 +476,8 @@ elif command == 'chanattr': check_admin (sender, ident, host, command) - if len(args) < 2: - raise logical_exception ("usage: .%s <attribute> <value...>" % command) + if len(args) < 1: + raise logical_exception ("usage: .%s <attribute> [value...]" % command) for channel in self.channels: if channel['name'] == replyto: @@ -471,25 +486,41 @@ raise logical_exception ('I don\'t know of a channel named ' + replyto) key = args[0] - value = ' '.join (args[1:]) + + if len(args) < 2: + try: + self.privmsg (replyto, '%s = %s' % (key, channel[key])) + except KeyError: + self.privmsg (replyto, 'attribute %s is not set' % key) + else: + value = " ".join (args[1:]) + if key == 'name': + if replyto == channel['name']: + replyto = value - 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) + 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': + channel['btannounce'] = bool_from_string (value) + elif key == 'logchannel': + channel['logchannel'] = bool_from_string (value) + else: + raise logical_exception ('unknown key ' + key) + + self.privmsg (replyto, '%s is now %s' % (key, channel[key])) save_config() elif command == 'die': check_admin (sender, ident, host, command) quit() + elif command == 'testlog': + check_admin (sender, ident, host, command) + if len(args) < 1: + raise logical_exception ("usage: .%s <message...>" % command) + chanlog (" ".join (args)) # else: # raise logical_exception ("unknown command `.%s`" % command) @@ -538,7 +569,7 @@ try: for conndata in g_config['connections']: if conndata['name'] in g_config['autoconnect']: - irc_client (conndata, CLIF_CONTROL if conndata['control'] else 0) + irc_client (conndata, 0) asyncore.loop() except KeyboardInterrupt: for client in g_clients: