diff --git a/local_config.py.skel b/local_config.py.skel index adbeda0..71c27c2 100644 --- a/local_config.py.skel +++ b/local_config.py.skel @@ -17,7 +17,7 @@ if '__main__' == __name__: config = { 'jid': 'FIXME', 'password': 'FIXME', - 'room': 'FIXME', + 'rooms': ['FIXME'], 'src-url': 'FIXME', diff --git a/plugins.py b/plugins.py index 697a828..4d41c0c 100644 --- a/plugins.py +++ b/plugins.py @@ -105,9 +105,9 @@ def parse_skynet(args): 'msg': '''I'm an independent bot and have nothing to do with other artificial intelligence systems!''' } -def data_parse_other(msg): - data = msg['body'] - reply_user = msg['mucnick'] +def data_parse_other(msg_obj): + data = msg_obj['body'] + reply_user = msg_obj['mucnick'] for p in plugins['parse']: if ratelimit_exceeded(p['ratelimit_class']): @@ -132,7 +132,7 @@ def data_parse_other(msg): if None != ret: if 'msg' in list(ret.keys()): ratelimit_touch(RATE_CHAT) - chat_write(ret['msg']) + send_reply(ret['msg'], msg_obj) def command_command(args): if 'register' == args: @@ -353,7 +353,7 @@ def command_teatimer(args): return { 'name': 'teatimer', 'desc': 'sets a tea timer to $1 or currently %d seconds' % conf('tea_steep_time'), - 'args': ('reply_user', 'argv0', 'argv1'), + 'args': ('reply_user', 'msg_obj', 'argv0', 'argv1'), 'ratelimit_class': RATE_GLOBAL } @@ -379,7 +379,7 @@ def command_teatimer(args): 'msg': args['reply_user'] + ': time format error: ' + str(e) } - register_event(ready, chat_write, args['reply_user'] + ': Your tea is ready!') + register_event(ready, send_reply, (args['reply_user'] + ': Your tea is ready!', args['msg_obj'])) return { 'msg': args['reply_user'] + ': Tea timer set to %s' % time.strftime( @@ -464,8 +464,8 @@ def command_else(args): 'msg': args['reply_user'] + ''': I'm a bot (highlight me with 'info' for more information).''' } -def data_parse_commands(msg): - data = msg['body'] +def data_parse_commands(msg_obj): + data = msg_obj['body'] words = data.split(' ') if 2 > len(words): # need at least two words @@ -480,7 +480,7 @@ def data_parse_commands(msg): sys.exit(1) return None - reply_user = msg['mucnick'] + reply_user = msg_obj['mucnick'] (argv0, argv1) = (None, None) if 1 < len(words): argv0 = words[1] @@ -506,6 +506,8 @@ def data_parse_commands(msg): args['cmd_list'] = cmds elif 'reply_user' == a: args['reply_user'] = reply_user + elif 'msg_obj' == a: + args['msg_obj'] = msg_obj elif 'argv0' == a: args['argv0'] = argv0 elif 'argv1' == a: @@ -522,14 +524,14 @@ def data_parse_commands(msg): if ratelimit_exceeded(RATE_CHAT): return False - chat_write(ret['msg']) + send_reply(ret['msg'], msg_obj) else: for line in ret['msg']: ratelimit_touch(RATE_CHAT) if ratelimit_exceeded(RATE_CHAT): return False - chat_write(line) + send_reply(line, msg_obj) return None @@ -541,9 +543,9 @@ def data_parse_commands(msg): if 'msg' in list(ret.keys()): if list is type(ret['msg']): for m in ret['msg']: - chat_write(m) + send_reply(m, msg_obj) else: - chat_write(ret['msg']) + send_reply(ret['msg'], msg_obj) funcs = {} funcs['parse'] = (parse_mental_ill, parse_skynet, parse_debbug, parse_cve) @@ -556,8 +558,8 @@ funcs['command'] = ( _dir = dir() if debug_enabled(): - def _chat_write(a): - logger('chat_write', a) + def _send_reply(a, msg_obj): + logger('send_reply[%s]' % msg_obj, a) def _conf(a): return 'bot' @@ -569,9 +571,9 @@ if debug_enabled(): return True try: - chat_write + send_reply except NameError: - chat_write = _chat_write + send_reply = _send_reply try: conf diff --git a/urlbot.py b/urlbot.py index 5bb9826..8b575e8 100755 --- a/urlbot.py +++ b/urlbot.py @@ -102,21 +102,21 @@ def extract_title(url): return (-1, 'error') -def chat_write(message): +def send_reply(message, msg_obj): set_conf('request_counter', conf('request_counter') + 1) for m in message: if 0x20 > ord(m): - logger('warn', 'strange char 0x%02x in chat_write(message), skipping' % ord(m)) + logger('warn', 'strange char 0x%02x in send_reply(message), skipping' % ord(m)) return False if debug_enabled(): print(message) else: xmpp.send_message( - mto=conf('room'), + mto=msg_obj['from'].bare, mbody=message, - mtype='groupchat' + mtype=msg_obj['type'] ) def ratelimit_touch(ignored=None): # FIXME: separate counters @@ -134,7 +134,8 @@ def ratelimit_exceeded(ignored=None): # FIXME: separate counters if (time.time() - first) < conf('hist_max_time'): if hist_flag: hist_flag = False - chat_write('(rate limited to %d messages in %d seconds, try again at %s)' %(conf('hist_max_count'), conf('hist_max_time'), time.strftime('%T %Z', time.localtime(hist_ts[0] + conf('hist_max_time'))))) +# FIXME: this is very likely broken now + send_reply('(rate limited to %d messages in %d seconds, try again at %s)' %(conf('hist_max_count'), conf('hist_max_time'), time.strftime('%T %Z', time.localtime(hist_ts[0] + conf('hist_max_time'))))) logger('warn', 'rate limiting exceeded: ' + pickle.dumps(hist_ts)) return True @@ -142,13 +143,13 @@ def ratelimit_exceeded(ignored=None): # FIXME: separate counters hist_flag = True return False -def extract_url(data): +def extract_url(data, msg_obj): ret = None result = re.findall("(https?://[^\s>]+)", data) if result: for url in result: ratelimit_touch() - if ratelimit_exceeded(): + if ratelimit_exceeded(msg_obj): return False flag = False @@ -203,7 +204,7 @@ def extract_url(data): message = message.replace('\n', '\\n') logger('info', 'printing ' + message) - chat_write(message) + send_reply(message, msg_obj) ret = True return ret @@ -215,8 +216,8 @@ def parse_pn(data): logger('warn', 'received PN: ' + data) return False -def handle_msg(msg): - content = msg['body'] +def handle_msg(msg_obj): + content = msg_obj['body'] # FIXME: still needed? if 'has set the subject to:' in content: @@ -230,16 +231,16 @@ def handle_msg(msg): logger('info', 'silenced, this is my own log') return - if True != extract_url(content): - plugins.data_parse_commands(msg) - plugins.data_parse_other(msg) + if True != extract_url(content, msg_obj): + plugins.data_parse_commands(msg_obj) + plugins.data_parse_other(msg_obj) return class bot(ClientXMPP): - def __init__(self, jid, password, room, nick): + def __init__(self, jid, password, rooms, nick): ClientXMPP.__init__(self, jid, password) - self.room = room + self.rooms = rooms self.nick = nick self.add_event_handler('session_start', self.session_start) @@ -249,23 +250,24 @@ class bot(ClientXMPP): self.get_roster() self.send_presence() - self.plugin['xep_0045'].joinMUC( - self.room, - self.nick, - wait=True - ) + for room in self.rooms: + self.plugin['xep_0045'].joinMUC( + room, + self.nick, + wait=True + ) - def muc_message(self, msg): + def muc_message(self, msg_obj): # don't talk to yourself - if msg['mucnick'] == self.nick: + if msg_obj['mucnick'] == self.nick: return - return handle_msg(msg) + return handle_msg(msg_obj) if '__main__' == __name__: import plugins - plugins.chat_write = chat_write + plugins.send_reply = send_reply plugins.ratelimit_exceeded = ratelimit_exceeded plugins.ratelimit_touch = ratelimit_touch @@ -281,7 +283,7 @@ if '__main__' == __name__: xmpp = bot( jid=conf('jid'), password=conf('password'), - room=conf('room'), + rooms=conf('rooms'), nick=conf('bot_user') )