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 -*-
""" Common functions for urlbot """
import html.parser
import logging
import os
@@ -8,7 +9,6 @@ import sys
import time
import urllib.request
from collections import namedtuple
from threading import Lock
from local_config import conf
RATE_NO_LIMIT = 0x00
@@ -22,16 +22,13 @@ RATE_FUN = 0x40
BUFSIZ = 8192
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'''
basedir = '.'
if 2 == len(sys.argv):
basedir = sys.argv[1]
USER_AGENT = 'Mozilla/5.0 (X11; Linux x86_64; rv:31.0) ' \
'Gecko/20100101 Firefox/31.0 Iceweasel/31.0'
def conf_save(obj):
with open(conf('persistent_storage'), 'wb') as fd:
return pickle.dump(obj, fd)
with open(conf('persistent_storage'), 'wb') as config_file:
return pickle.dump(obj, config_file)
def conf_load():
@@ -90,7 +87,10 @@ def rate_limit(rate_class=RATE_GLOBAL):
now = time.time()
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):
# print("blocked")

View File

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

View File

@@ -1,21 +1,19 @@
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import random
import re
"""
The URLBot - ready to strive for desaster in YOUR jabber MUC
"""
import sys
from common import (
conf_load, conf_save,
extract_title,
rate_limit_classes,
RATE_GLOBAL,
RATE_CHAT,
RATE_NO_SILENCE,
RATE_EVENT,
# rate_limited,
rate_limit,
RATE_URL, conf_set)
conf_set
)
from idlebot import IdleBot, start
from plugins import (
plugins as plugin_storage,
@@ -34,14 +32,17 @@ except ImportError:
%s site specific configurations. Rename local_config.py.skel and
%s adjust to your needs.
'''[1:] % (
sys.argv[0],
' ' * len(sys.argv[0]),
' ' * len(sys.argv[0])
))
sys.argv[0],
' ' * len(sys.argv[0]),
' ' * len(sys.argv[0])
))
sys.exit(1)
class UrlBot(IdleBot):
"""
The URLBot, doing things the IdleBot wouldn't dare to.
"""
def __init__(self, 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.priority = 100
for r in self.rooms:
self.add_event_handler('muc::%s::got_online' % r, self.muc_online)
for room in self.rooms:
self.add_event_handler('muc::%s::got_online' % room, self.muc_online)
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)
def message(self, msg_obj):
if 'groupchat' == msg_obj['type']:
"""
General message hook
:param msg_obj:
"""
if msg_obj['type'] == 'groupchat':
return
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):
"""
@@ -86,19 +96,18 @@ class UrlBot(IdleBot):
mto=msg_obj['from'].bare,
mbody='%s, there %s %d message%s for you:\n%s' % (
arg_user,
'is' if 1 == len(records) else 'are',
'is' if len(records) == 1 else 'are',
len(records),
'' if 1 == len(records) else 's',
'' if len(records) == 1 else 's',
'\n'.join(records)
),
mtype='groupchat'
)
self.logger.info('sent %d offline records to room %s' % (
len(records), msg_obj['from'].bare
))
self.logger.info('sent %d offline records to room %s',
len(records), msg_obj['from'].bare)
if conf('persistent_locked'):
self.logger.warn("couldn't get exclusive lock")
self.logger.warning("couldn't get exclusive lock")
return False
set_conf('persistent_locked', True)
@@ -119,7 +128,7 @@ class UrlBot(IdleBot):
Send a reply to a message
"""
if self.show:
self.logger.warn("I'm muted! (status: %s)" % self.show)
self.logger.warning("I'm muted! (status: %s)", self.show)
return
set_conf('request_counter', conf('request_counter') + 1)
@@ -184,12 +193,10 @@ class UrlBot(IdleBot):
:returns: nothing
"""
global got_hangup
data = msg_obj['body']
words = data.split()
if 2 > len(words): # need at least two words
if len(words) < 2: # need at least two words
return None
# don't reply if beginning of the text matches bot_user
@@ -197,13 +204,14 @@ class UrlBot(IdleBot):
return None
if 'hangup' in data:
self.logger.warn('received hangup: ' + data)
got_hangup = True
self.logger.warning('received hangup: ' + data)
self.hangup()
sys.exit(1)
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
for plugin in plugin_storage[ptypes_COMMAND]:
@@ -278,5 +286,5 @@ class UrlBot(IdleBot):
# self.reconnect(wait=True)
if '__main__' == __name__:
if __name__ == '__main__':
start(UrlBot, True)