From 77a6322d6699e08fbe707b7eda576b3a0654e9d3 Mon Sep 17 00:00:00 2001 From: urlbot Date: Tue, 2 Dec 2014 15:04:53 +0100 Subject: [PATCH 1/2] port urlbot: remove mcabber specific input stuff --- .gitignore | 1 + bot-xmppy.py | 49 --------- bot-sleek.py => bot.py | 0 urlbot-mcabber/common.py => common.py | 1 - ...levenshtein_test.py => levenshtein_test.py | 0 urlbot-mcabber/plugins.py => plugins.py | 0 urlbot-mcabber/strsim.py => strsim.py | 0 .../test_urlbot.py => test_urlbot.py | 0 urlbot-mcabber/.gitignore | 6 -- urlbot-mcabber/local_config.py.skel | 50 --------- urlbot-mcabber/urlbot.py => urlbot.py | 101 ++++++++++++++---- 11 files changed, 79 insertions(+), 129 deletions(-) delete mode 100755 bot-xmppy.py rename bot-sleek.py => bot.py (100%) rename urlbot-mcabber/common.py => common.py (96%) rename urlbot-mcabber/levenshtein_test.py => levenshtein_test.py (100%) rename urlbot-mcabber/plugins.py => plugins.py (100%) rename urlbot-mcabber/strsim.py => strsim.py (100%) rename urlbot-mcabber/test_urlbot.py => test_urlbot.py (100%) delete mode 100644 urlbot-mcabber/.gitignore delete mode 100644 urlbot-mcabber/local_config.py.skel rename urlbot-mcabber/urlbot.py => urlbot.py (81%) diff --git a/.gitignore b/.gitignore index 578d951..5bc7fb6 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .*swp *.pyc local_config.py +urlbot.persistent diff --git a/bot-xmppy.py b/bot-xmppy.py deleted file mode 100755 index 53f4404..0000000 --- a/bot-xmppy.py +++ /dev/null @@ -1,49 +0,0 @@ -#!/usr/bin/python - -import xmpp -from local_config import conf - -import time -t = -time.time() - -def message_handler(connect_object, message_node): - # hopefully the backlog is processed in this time - # FIXME: find a better way. - if (t + time.time() < 1): - return None - - msg_from = message_node.getFrom().getResource() - msg_body = message_node.getBody() - - if not type(msg_body) in [str, unicode]: - return None - - if msg_body.startswith(conf('bot_user')): - connect_object.send( - xmpp.protocol.Message( - to=conf('room'), - body='hello %s!' % msg_from, - typ='groupchat' - ) - ) - - try: - print '%20s: %s' %(msg_from, msg_body) - except Exception as e: - print e - - return None - -jid = xmpp.protocol.JID(conf('jid')) - -client = xmpp.Client(jid.getDomain(), debug=[]) -client.connect() -client.auth(jid.getNode(), conf('password')) -client.RegisterHandler('message', message_handler) - -client.send(xmpp.Presence(to=(conf('room') + '/' + conf('bot_user')))) - -while (t + time.time()) < 30: - client.Process(1) - -client.disconnect() diff --git a/bot-sleek.py b/bot.py similarity index 100% rename from bot-sleek.py rename to bot.py diff --git a/urlbot-mcabber/common.py b/common.py similarity index 96% rename from urlbot-mcabber/common.py rename to common.py index 42c786a..76320a0 100644 --- a/urlbot-mcabber/common.py +++ b/common.py @@ -20,7 +20,6 @@ delay = 0.100 # seconds basedir = '.' if 2 == len(sys.argv): basedir = sys.argv[1] -event_files_dir = os.path.join(basedir, conf('path_event_files')) fifo_path = os.path.join(basedir, conf('path_cmdfifo')) def debug_enabled(): diff --git a/urlbot-mcabber/levenshtein_test.py b/levenshtein_test.py similarity index 100% rename from urlbot-mcabber/levenshtein_test.py rename to levenshtein_test.py diff --git a/urlbot-mcabber/plugins.py b/plugins.py similarity index 100% rename from urlbot-mcabber/plugins.py rename to plugins.py diff --git a/urlbot-mcabber/strsim.py b/strsim.py similarity index 100% rename from urlbot-mcabber/strsim.py rename to strsim.py diff --git a/urlbot-mcabber/test_urlbot.py b/test_urlbot.py similarity index 100% rename from urlbot-mcabber/test_urlbot.py rename to test_urlbot.py diff --git a/urlbot-mcabber/.gitignore b/urlbot-mcabber/.gitignore deleted file mode 100644 index d150401..0000000 --- a/urlbot-mcabber/.gitignore +++ /dev/null @@ -1,6 +0,0 @@ -.*swp -*.pyc -cmdfifo -logs/ -event_files/ -urlbot.persistent diff --git a/urlbot-mcabber/local_config.py.skel b/urlbot-mcabber/local_config.py.skel deleted file mode 100644 index 78a9112..0000000 --- a/urlbot-mcabber/local_config.py.skel +++ /dev/null @@ -1,50 +0,0 @@ -#!/usr/bin/python3 - -import time, sys - -def _logger(a, b): sys.stderr.write('logger: %s::%s\n' %(a, b)) -try: logger -except NameError: logger = _logger - -if '__main__' == __name__: - print('''this is a config file, which is not meant to be executed''') - exit(-1) - -config = { - 'src-url': 'FIXME', - - 'bot_user': 'urlbot', - 'bot_owner': 'FIXME', - - 'hist_max_count': 5, - 'hist_max_time': 10 * 60, - - 'uptime': -time.time(), - 'request_counter': 0, - - 'path_event_files': 'event_files', - 'path_cmdfifo': 'cmdfifo', - 'persistent_storage': 'urlbot.persistent', - - 'url_blacklist': [ - r'^.*heise\.de/[^/]+/meldung/.*$', - r'^.*wikipedia\.org/wiki/.*$' - ], - -# the "dice" feature will use more efficient random data (0) for given users - 'enhanced-random-user': ( 'FIXME', 'FIXME' ), - - 'tea_steep_time': (3*60 + 40), - - 'image_preview': True -} - -def conf(val): - if val in list(config.keys()): - return config[val] - logger('warn', 'conf(): unknown key ' + str(val)) - return None - -def set_conf(key, val): - config[key] = val - return None diff --git a/urlbot-mcabber/urlbot.py b/urlbot.py similarity index 81% rename from urlbot-mcabber/urlbot.py rename to urlbot.py index 5abb4db..aace9ea 100755 --- a/urlbot-mcabber/urlbot.py +++ b/urlbot.py @@ -3,10 +3,30 @@ import sys, os, stat, re, time, pickle, random import urllib.request, urllib.parse, urllib.error, html.parser -from local_config import conf, set_conf from common import * from strsim import str_sim +try: + from local_config import conf, set_conf +except ImportError: + import sys + sys.stderr.write(''' +%s: E: local_config.py isn't tracked because of included secrets and +%s site specific configurations. Rename local_config.py.skel and +%s adjust to you needs. +'''[1:] % ( + sys.argv[0], + ' ' * len(sys.argv[0]), + ' ' * len(sys.argv[0]) + ) + ) + + sys.exit(-1) + +import logging + +from sleekxmpp import ClientXMPP + # rate limiting to 5 messages per 10 minutes hist_ts = [] hist_flag = True @@ -215,29 +235,15 @@ def parse_pn(data): logger('warn', 'received PN: ' + data) return False -def parse_delete(filepath): - try: - fd = open(filepath, 'r') - except IOError as e: - logger('err', 'file has vanished: %s: %s' % (filepath, e)) - return False - - content = fd.read(BUFSIZ) # ignore more than BUFSIZ - fd.close() - os.remove(filepath) # probably better crash here - - if content[1:1+len(conf('bot_user'))] == conf('bot_user'): - return +def handle_msg(msg): + content = msg['body'] +# FIXME: still needed? if 'has set the subject to:' in content: return - - if content.startswith('PRIV#'): - parse_pn(content) - return if 'nospoiler' in content: -# logger('info', "no spoiler for: " + content) + logger('info', "no spoiler for: " + content) return if sys.argv[0] in content: @@ -249,6 +255,42 @@ def parse_delete(filepath): plugins.data_parse_other(content) return +class bot(ClientXMPP): + def __init__(self, jid, password, room, nick): + ClientXMPP.__init__(self, jid, password) + + self.room = room + self.nick = nick + + self.add_event_handler('session_start', self.session_start) + self.add_event_handler('groupchat_message', self.muc_message) + + def session_start(self, event): + self.get_roster() + self.send_presence() + + self.plugin['xep_0045'].joinMUC( + self.room, + self.nick, + wait=True + ) + + def muc_message(self, msg): + print(msg['mucnick']) + print(msg['body']) + + # don't talk to yourself + if msg['mucnick'] == self.nick: + return + +# self.send_message( +# mto=msg['from'].bare, +# mbody='got[%s]' % msg['body'], +# mtype='groupchat' +# ) + + return handle_msg(msg) + if '__main__' == __name__: import plugins @@ -260,6 +302,23 @@ if '__main__' == __name__: print(sys.argv[0] + ' ' + VERSION) + logging.basicConfig( + level=logging.DEBUG, + format='%(levelname)-8s %(message)s' + ) + + xmpp = bot( + jid=conf('jid'), + password=conf('password'), + room=conf('room'), + nick=conf('bot_user') + ) + + xmpp.connect() + xmpp.register_plugin('xep_0045') + xmpp.process(threaded=False) + + if not os.path.exists(fifo_path): logger('error', 'fifo_path "%s" does not exist, exiting' % fifo_path) exit(1) @@ -270,10 +329,6 @@ if '__main__' == __name__: while 1: try: - for f in os.listdir(event_files_dir): - if 'mcabber-' == f[:8]: - parse_delete(os.path.join(event_files_dir, f)) - plugins.event_trigger() time.sleep(delay) From cb712d96451d51ae80fb456fd965196d9669d7bf Mon Sep 17 00:00:00 2001 From: urlbot Date: Tue, 2 Dec 2014 15:22:02 +0100 Subject: [PATCH 2/2] port urlbot: remove mcabber specific output stuff --- bot.py | 3 +++ common.py | 2 -- plugins.py | 4 ++-- urlbot.py | 45 +++++++++++++++------------------------------ 4 files changed, 20 insertions(+), 34 deletions(-) diff --git a/bot.py b/bot.py index c729b5a..f7119e3 100755 --- a/bot.py +++ b/bot.py @@ -47,6 +47,9 @@ class bot(ClientXMPP): def muc_message(self, msg): print(msg['mucnick']) print(msg['body']) + print((msg['from'], msg['from'].bare)) + + print(conf('room') == msg['from'].bare) # don't talk to yourself if msg['mucnick'] == self.nick: diff --git a/common.py b/common.py index 76320a0..86eb7ad 100644 --- a/common.py +++ b/common.py @@ -20,8 +20,6 @@ delay = 0.100 # seconds basedir = '.' if 2 == len(sys.argv): basedir = sys.argv[1] -fifo_path = os.path.join(basedir, conf('path_cmdfifo')) - def debug_enabled(): # return True return False diff --git a/plugins.py b/plugins.py index a66f453..813b8b6 100644 --- a/plugins.py +++ b/plugins.py @@ -5,7 +5,7 @@ if '__main__' == __name__: print('''this is a plugin file, which is not meant to be executed''') exit(-1) -import time, random, unicodedata, re +import time, random, unicodedata, re, sys from local_config import conf from common import * from urlbot import extract_title @@ -488,8 +488,8 @@ def data_parse_commands(data): return None if 'hangup' in data: - chat_write('', prefix='/quit') logger('warn', 'received hangup: ' + data) + sys.exit(1) return None reply_user = get_reply_data(data) diff --git a/urlbot.py b/urlbot.py index aace9ea..7d3da81 100755 --- a/urlbot.py +++ b/urlbot.py @@ -32,6 +32,7 @@ hist_ts = [] hist_flag = True parser = None +xmpp = None def fetch_page(url): logger('info', 'fetching page ' + url) @@ -100,7 +101,7 @@ def extract_title(url): return (-1, 'error') -def chat_write(message, prefix='/say '): +def chat_write(message): set_conf('request_counter', conf('request_counter') + 1) for m in message: @@ -111,22 +112,20 @@ def chat_write(message, prefix='/say '): if debug_enabled(): print(message) else: + # FIXME: somehow, unicode chars can end up inside a message, + # which seems to make both unicode() and ''.encode('utf8') fail. try: - fd = open(fifo_path, 'wb') -# FIXME 2to3 - # FIXME: somehow, unicode chars can end up inside a message, - # which seems to make both unicode() and ''.encode('utf8') fail. - try: - msg = str(prefix) + str(message) + '\n' - msg = msg.encode('utf8') - except UnicodeDecodeError as e: - logger('warn', 'encoding msg failed: ' + str(e)) - msg = prefix + message + '\n' + msg = str(message) + msg = msg.encode('utf8') + except UnicodeDecodeError as e: + logger('warn', 'encoding msg failed: ' + str(e)) + msg = message - fd.write(msg) - fd.close() - except IOError as e: - logger('err', "couldn't print to fifo %s: %s" % (fifo_path, str(e))) + xmpp.send_message( + mto=conf('room'), + mbody=msg, + mtype='groupchat' + ) def ratelimit_touch(ignored=None): # FIXME: separate counters hist_ts.append(time.time()) @@ -229,6 +228,7 @@ def extract_url(data): return ret def parse_pn(data): +# FIXME: changed ## reply_user = data.split(' ')[0].strip('<>') # since we can't determine if a user named 'foo> ' just wrote ' > bar' # or a user 'foo' just wrote '> > bar', we can't safely answer here @@ -283,12 +283,6 @@ class bot(ClientXMPP): if msg['mucnick'] == self.nick: return -# self.send_message( -# mto=msg['from'].bare, -# mbody='got[%s]' % msg['body'], -# mtype='groupchat' -# ) - return handle_msg(msg) if '__main__' == __name__: @@ -318,15 +312,6 @@ if '__main__' == __name__: xmpp.register_plugin('xep_0045') xmpp.process(threaded=False) - - if not os.path.exists(fifo_path): - logger('error', 'fifo_path "%s" does not exist, exiting' % fifo_path) - exit(1) - - if not stat.S_ISFIFO(os.stat(fifo_path).st_mode): - logger('error', 'fifo_path "%s" is not a FIFO, exiting' % fifo_path) - exit(1) - while 1: try: plugins.event_trigger()