From cb712d96451d51ae80fb456fd965196d9669d7bf Mon Sep 17 00:00:00 2001 From: urlbot Date: Tue, 2 Dec 2014 15:22:02 +0100 Subject: [PATCH] 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()