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)