Files
urlbot-native/idlebot.py

123 lines
3.4 KiB
Python
Raw Normal View History

2015-06-17 10:53:11 +02:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import logging
import time
import sys
from common import VERSION, EVENTLOOP_DELAY, conf_load
2015-06-17 10:53:11 +02:00
try:
2015-11-30 19:17:40 +01:00
from local_config import conf, set_conf
2015-06-17 10:53:11 +02:00
except ImportError:
2015-11-30 19:17:40 +01:00
sys.stderr.write('''
2015-06-17 10:53:11 +02:00
%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:] % (
2015-11-30 19:17:40 +01:00
sys.argv[0],
' ' * len(sys.argv[0]),
' ' * len(sys.argv[0])
))
sys.exit(1)
2015-06-17 10:53:11 +02:00
from sleekxmpp import ClientXMPP
2015-06-20 15:13:12 +02:00
got_hangup = False
class IdleBot(ClientXMPP):
2015-11-30 19:17:40 +01:00
def __init__(self, jid, password, rooms, nick):
ClientXMPP.__init__(self, jid, password)
self.rooms = rooms
self.nick = nick
self.add_event_handler('session_start', self.session_start)
self.add_event_handler('groupchat_message', self.muc_message)
self.priority = 0
self.status = None
self.show = None
self.logger = logging.getLogger(__name__)
def session_start(self, _):
self.get_roster()
self.send_presence(ppriority=self.priority, pstatus=self.status, pshow=self.show)
for room in self.rooms:
self.logger.info('%s: joining' % room)
ret = self.plugin['xep_0045'].joinMUC(
room,
self.nick,
wait=True
)
self.logger.info('%s: joined with code %s' % (room, ret))
def muc_message(self, msg_obj):
"""
Handle muc messages, return if irrelevant content or die by hangup.
:param msg_obj:
:return:
"""
# don't talk to yourself
if msg_obj['mucnick'] == self.nick or 'groupchat' != msg_obj['type']:
return False
elif msg_obj['body'].startswith(conf('bot_user')) and 'hangup' in msg_obj['body']:
self.logger.warn("got 'hangup' from '%s': '%s'" % (
msg_obj['mucnick'], msg_obj['body']
))
global got_hangup
got_hangup = True
return False
elif msg_obj['mucnick'] in conf_load().get("other_bots", ()):
# not talking to the other bot.
return False
else:
return True
2015-06-20 15:13:12 +02:00
def start(botclass, active=False):
2015-11-30 19:17:40 +01:00
logging.basicConfig(
level=conf('loglevel', logging.INFO),
format=sys.argv[0] + ' %(asctime)s %(levelname).1s %(funcName)-15s %(message)s'
)
logger = logging.getLogger(__name__)
logger.info(VERSION)
jid = conf('jid')
if '/' not in jid:
jid = '%s/%s' % (jid, botclass.__name__)
bot = botclass(
jid=jid,
password=conf('password'),
rooms=conf('rooms'),
nick=conf('bot_user')
)
import plugins
if active:
plugins.register_all()
if plugins.plugin_enabled_get(plugins.command_dsa_watcher):
# first result is lost.
plugins.command_dsa_watcher(['dsa-watcher', 'crawl'])
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)
time.sleep(EVENTLOOP_DELAY)
except KeyboardInterrupt:
print('')
exit(130)
if '__main__' == __name__:
2015-11-30 19:17:40 +01:00
start(IdleBot)