minor cleanup

This commit is contained in:
Thorsten
2015-11-30 19:50:11 +01:00
parent a7b53d855a
commit f40add54b5
3 changed files with 57 additions and 45 deletions

View File

@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
""" Common functions for urlbot """
import html.parser import html.parser
import logging import logging
import os import os
@@ -8,7 +9,6 @@ import sys
import time import time
import urllib.request import urllib.request
from collections import namedtuple from collections import namedtuple
from threading import Lock
from local_config import conf from local_config import conf
RATE_NO_LIMIT = 0x00 RATE_NO_LIMIT = 0x00
@@ -22,16 +22,13 @@ RATE_FUN = 0x40
BUFSIZ = 8192 BUFSIZ = 8192
EVENTLOOP_DELAY = 0.100 # seconds EVENTLOOP_DELAY = 0.100 # seconds
USER_AGENT = '''Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Firefox/31.0 Iceweasel/31.0''' USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) ' \
'Gecko/20100101 Firefox/31.0 Iceweasel/31.0'
basedir = '.'
if 2 == len(sys.argv):
basedir = sys.argv[1]
def conf_save(obj): def conf_save(obj):
with open(conf('persistent_storage'), 'wb') as fd: with open(conf('persistent_storage'), 'wb') as config_file:
return pickle.dump(obj, fd) return pickle.dump(obj, config_file)
def conf_load(): def conf_load():
@@ -90,7 +87,10 @@ def rate_limit(rate_class=RATE_GLOBAL):
now = time.time() now = time.time()
bucket = buckets[rate_class] bucket = buckets[rate_class]
logging.getLogger(__name__).debug("[ratelimit][bucket=%x][time=%s]%s" % (rate_class, now, bucket.history)) logging.getLogger(__name__).debug(
"[ratelimit][bucket=%x][time=%s]%s",
rate_class, now, bucket.history
)
if len(bucket.history) >= bucket.max_hist_len and bucket.history[0] > (now - bucket.period): if len(bucket.history) >= bucket.max_hist_len and bucket.history[0] > (now - bucket.period):
# print("blocked") # print("blocked")

View File

@@ -21,7 +21,7 @@ except ImportError:
from sleekxmpp import ClientXMPP from sleekxmpp import ClientXMPP
got_hangup = False # got_hangup = False
class IdleBot(ClientXMPP): class IdleBot(ClientXMPP):
@@ -65,8 +65,7 @@ class IdleBot(ClientXMPP):
self.logger.warn("got 'hangup' from '%s': '%s'" % ( self.logger.warn("got 'hangup' from '%s': '%s'" % (
msg_obj['mucnick'], msg_obj['body'] msg_obj['mucnick'], msg_obj['body']
)) ))
global got_hangup self.hangup()
got_hangup = True
return False return False
elif msg_obj['mucnick'] in conf_load().get("other_bots", ()): elif msg_obj['mucnick'] in conf_load().get("other_bots", ()):
# not talking to the other bot. # not talking to the other bot.
@@ -74,6 +73,13 @@ class IdleBot(ClientXMPP):
else: else:
return True return True
def hangup(self):
"""
disconnect and exit
"""
self.disconnect()
sys.exit(1)
def start(botclass, active=False): def start(botclass, active=False):
logging.basicConfig( logging.basicConfig(
@@ -103,14 +109,12 @@ def start(botclass, active=False):
bot.connect() bot.connect()
bot.register_plugin('xep_0045') bot.register_plugin('xep_0045')
bot.process() bot.process()
global got_hangup
while 1: while 1:
try: try:
# print("hangup: %s" % got_hangup) # print("hangup: %s" % got_hangup)
if got_hangup or not plugins.event_trigger(): if not plugins.event_trigger():
bot.disconnect() bot.hangup()
sys.exit(1)
time.sleep(EVENTLOOP_DELAY) time.sleep(EVENTLOOP_DELAY)
except KeyboardInterrupt: except KeyboardInterrupt:

View File

@@ -1,21 +1,19 @@
#!/usr/bin/python3 #!/usr/bin/python3
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
"""
import random The URLBot - ready to strive for desaster in YOUR jabber MUC
import re """
import sys import sys
from common import ( from common import (
conf_load, conf_save, conf_load, conf_save,
extract_title,
rate_limit_classes, rate_limit_classes,
RATE_GLOBAL, RATE_GLOBAL,
RATE_CHAT, RATE_CHAT,
RATE_NO_SILENCE,
RATE_EVENT, RATE_EVENT,
# rate_limited,
rate_limit, rate_limit,
RATE_URL, conf_set) conf_set
)
from idlebot import IdleBot, start from idlebot import IdleBot, start
from plugins import ( from plugins import (
plugins as plugin_storage, plugins as plugin_storage,
@@ -42,6 +40,9 @@ except ImportError:
class UrlBot(IdleBot): class UrlBot(IdleBot):
"""
The URLBot, doing things the IdleBot wouldn't dare to.
"""
def __init__(self, jid, password, rooms, nick): def __init__(self, jid, password, rooms, nick):
super(UrlBot, self).__init__(jid, password, rooms, nick) super(UrlBot, self).__init__(jid, password, rooms, nick)
@@ -51,17 +52,26 @@ class UrlBot(IdleBot):
self.add_event_handler('message', self.message) self.add_event_handler('message', self.message)
self.priority = 100 self.priority = 100
for r in self.rooms: for room in self.rooms:
self.add_event_handler('muc::%s::got_online' % r, self.muc_online) self.add_event_handler('muc::%s::got_online' % room, self.muc_online)
def muc_message(self, msg_obj): def muc_message(self, msg_obj):
"""
Handle muc messages, return if irrelevant content or die by hangup.
:param msg_obj:
:return:
"""
return super(UrlBot, self).muc_message(msg_obj) and self.handle_msg(msg_obj) return super(UrlBot, self).muc_message(msg_obj) and self.handle_msg(msg_obj)
def message(self, msg_obj): def message(self, msg_obj):
if 'groupchat' == msg_obj['type']: """
General message hook
:param msg_obj:
"""
if msg_obj['type'] == 'groupchat':
return return
else: else:
self.logger.info("Got the following PM: %s" % str(msg_obj)) self.logger.info("Got the following PM: %s", str(msg_obj))
def muc_online(self, msg_obj): def muc_online(self, msg_obj):
""" """
@@ -86,19 +96,18 @@ class UrlBot(IdleBot):
mto=msg_obj['from'].bare, mto=msg_obj['from'].bare,
mbody='%s, there %s %d message%s for you:\n%s' % ( mbody='%s, there %s %d message%s for you:\n%s' % (
arg_user, arg_user,
'is' if 1 == len(records) else 'are', 'is' if len(records) == 1 else 'are',
len(records), len(records),
'' if 1 == len(records) else 's', '' if len(records) == 1 else 's',
'\n'.join(records) '\n'.join(records)
), ),
mtype='groupchat' mtype='groupchat'
) )
self.logger.info('sent %d offline records to room %s' % ( self.logger.info('sent %d offline records to room %s',
len(records), msg_obj['from'].bare len(records), msg_obj['from'].bare)
))
if conf('persistent_locked'): if conf('persistent_locked'):
self.logger.warn("couldn't get exclusive lock") self.logger.warning("couldn't get exclusive lock")
return False return False
set_conf('persistent_locked', True) set_conf('persistent_locked', True)
@@ -119,7 +128,7 @@ class UrlBot(IdleBot):
Send a reply to a message Send a reply to a message
""" """
if self.show: if self.show:
self.logger.warn("I'm muted! (status: %s)" % self.show) self.logger.warning("I'm muted! (status: %s)", self.show)
return return
set_conf('request_counter', conf('request_counter') + 1) set_conf('request_counter', conf('request_counter') + 1)
@@ -184,12 +193,10 @@ class UrlBot(IdleBot):
:returns: nothing :returns: nothing
""" """
global got_hangup
data = msg_obj['body'] data = msg_obj['body']
words = data.split() words = data.split()
if 2 > len(words): # need at least two words if len(words) < 2: # need at least two words
return None return None
# don't reply if beginning of the text matches bot_user # don't reply if beginning of the text matches bot_user
@@ -197,13 +204,14 @@ class UrlBot(IdleBot):
return None return None
if 'hangup' in data: if 'hangup' in data:
self.logger.warn('received hangup: ' + data) self.logger.warning('received hangup: ' + data)
got_hangup = True self.hangup()
sys.exit(1) sys.exit(1)
reply_user = msg_obj['mucnick'] reply_user = msg_obj['mucnick']
# TODO: check how several commands/plugins in a single message behave (also with rate limiting) # TODO: check how several commands/plugins
# in a single message behave (also with rate limiting)
reacted = False reacted = False
for plugin in plugin_storage[ptypes_COMMAND]: for plugin in plugin_storage[ptypes_COMMAND]:
@@ -278,5 +286,5 @@ class UrlBot(IdleBot):
# self.reconnect(wait=True) # self.reconnect(wait=True)
if '__main__' == __name__: if __name__ == '__main__':
start(UrlBot, True) start(UrlBot, True)