Files
urlbot-native/idlebot.py

112 lines
3.1 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
2015-12-20 15:24:42 +01:00
from common import VERSION, EVENTLOOP_DELAY
import config
2015-06-17 10:53:11 +02:00
from sleekxmpp import ClientXMPP
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
2015-12-20 15:24:42 +01:00
elif msg_obj['body'].startswith(config.conf_get('bot_nickname')) and 'hangup' in msg_obj['body']:
2015-11-30 19:17:40 +01:00
self.logger.warn("got 'hangup' from '%s': '%s'" % (
msg_obj['mucnick'], msg_obj['body']
))
2015-11-30 19:50:11 +01:00
self.hangup()
2015-11-30 19:17:40 +01:00
return False
2015-12-20 15:24:42 +01:00
elif msg_obj['mucnick'] in config.runtime_config_store["other_bots"]:
2015-11-30 19:17:40 +01:00
# not talking to the other bot.
return False
else:
return True
2015-06-20 15:13:12 +02:00
2015-11-30 19:50:11 +01:00
def hangup(self):
"""
disconnect and exit
"""
self.disconnect()
sys.exit(1)
2015-06-20 15:13:12 +02:00
def start(botclass, active=False):
2015-11-30 19:17:40 +01:00
logging.basicConfig(
2015-12-20 15:24:42 +01:00
level=config.conf_get('loglevel'),
2015-11-30 19:17:40 +01:00
format=sys.argv[0] + ' %(asctime)s %(levelname).1s %(funcName)-15s %(message)s'
)
logger = logging.getLogger(__name__)
logger.info(VERSION)
2015-12-20 15:24:42 +01:00
jid = config.conf_get('jid')
2015-11-30 19:17:40 +01:00
if '/' not in jid:
jid = '%s/%s' % (jid, botclass.__name__)
bot = botclass(
jid=jid,
2015-12-20 15:24:42 +01:00
password=config.conf_get('password'),
rooms=config.conf_get('rooms'),
nick=config.conf_get('bot_nickname')
2015-11-30 19:17:40 +01:00
)
import plugins
if active:
plugins.register_all()
2015-12-20 15:24:42 +01:00
# if plugins.plugin_enabled_get(plugins.command_dsa_watcher):
2015-11-30 19:17:40 +01:00
# first result is lost.
2015-12-20 15:24:42 +01:00
# plugins.command_dsa_watcher(['dsa-watcher', 'crawl'])
2015-11-30 19:17:40 +01:00
bot.connect()
bot.register_plugin('xep_0045')
bot.process()
while 1:
try:
# print("hangup: %s" % got_hangup)
2015-11-30 19:50:11 +01:00
if not plugins.event_trigger():
bot.hangup()
2015-11-30 19:17:40 +01:00
time.sleep(EVENTLOOP_DELAY)
except KeyboardInterrupt:
print('')
exit(130)
if '__main__' == __name__:
2015-11-30 19:17:40 +01:00
start(IdleBot)