2015-06-17 10:53:11 +02:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
# -*- coding: utf-8 -*-
|
2015-11-20 21:07:48 +01:00
|
|
|
import logging
|
|
|
|
|
import time
|
2015-06-17 10:53:11 +02:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
from common import VERSION, EVENTLOOP_DELAY
|
2015-06-17 10:53:11 +02:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from local_config import conf, set_conf
|
|
|
|
|
except ImportError:
|
|
|
|
|
sys.stderr.write('''
|
|
|
|
|
%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:] % (
|
|
|
|
|
sys.argv[0],
|
|
|
|
|
' ' * len(sys.argv[0]),
|
|
|
|
|
' ' * len(sys.argv[0])
|
|
|
|
|
)
|
|
|
|
|
)
|
2015-11-20 21:07:48 +01:00
|
|
|
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
|
|
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
|
|
|
|
|
class IdleBot(ClientXMPP):
|
2015-06-17 10:53:11 +02: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)
|
2015-06-20 15:13:12 +02:00
|
|
|
self.add_event_handler('groupchat_message', self.muc_message)
|
2015-06-17 10:53:11 +02:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
self.logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
def session_start(self, _):
|
2015-06-17 10:53:11 +02:00
|
|
|
self.get_roster()
|
|
|
|
|
self.send_presence()
|
|
|
|
|
|
|
|
|
|
for room in self.rooms:
|
2015-11-20 21:07:48 +01:00
|
|
|
self.logger.info('%s: joining' % room)
|
2015-11-14 11:06:07 +01:00
|
|
|
ret = self.plugin['xep_0045'].joinMUC(
|
2015-06-17 10:53:11 +02:00
|
|
|
room,
|
|
|
|
|
self.nick,
|
|
|
|
|
wait=True
|
|
|
|
|
)
|
2015-11-20 21:07:48 +01:00
|
|
|
self.logger.info('%s: joined with code %s' % (room, ret))
|
2015-06-17 10:53:11 +02:00
|
|
|
|
2015-06-20 15:13:12 +02:00
|
|
|
def muc_message(self, msg_obj):
|
2015-11-20 21:07:48 +01:00
|
|
|
"""
|
|
|
|
|
Handle muc messages, return if irrelevant content or die by hangup.
|
|
|
|
|
:param msg_obj:
|
|
|
|
|
:return:
|
|
|
|
|
"""
|
2015-06-20 15:13:12 +02:00
|
|
|
# don't talk to yourself
|
|
|
|
|
if msg_obj['mucnick'] == self.nick:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if 'groupchat' != msg_obj['type']:
|
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
if msg_obj['body'].startswith(conf('bot_user')) and 'hangup' in msg_obj['body']:
|
2015-11-20 21:07:48 +01:00
|
|
|
self.logger.warn("got 'hangup' from '%s': '%s'" % (
|
2015-06-20 15:13:12 +02:00
|
|
|
msg_obj['mucnick'], msg_obj['body']
|
|
|
|
|
))
|
2015-11-20 21:07:48 +01:00
|
|
|
global got_hangup
|
2015-06-20 15:13:12 +02:00
|
|
|
got_hangup = True
|
2015-11-20 21:07:48 +01:00
|
|
|
return
|
2015-06-20 15:13:12 +02:00
|
|
|
|
|
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
def start(botclass, active=False):
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
level=logging.INFO,
|
|
|
|
|
format=sys.argv[0] + ' %(asctime)s %(levelname).1s %(funcName)-15s %(message)s'
|
|
|
|
|
)
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
logger.info(VERSION)
|
2015-06-17 10:53:11 +02:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
bot = botclass(
|
2015-06-17 10:53:11 +02:00
|
|
|
jid=conf('jid'),
|
|
|
|
|
password=conf('password'),
|
|
|
|
|
rooms=conf('rooms'),
|
|
|
|
|
nick=conf('bot_user')
|
|
|
|
|
)
|
2015-11-20 21:07:48 +01:00
|
|
|
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'])
|
2015-06-17 10:53:11 +02:00
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
bot.connect()
|
|
|
|
|
bot.register_plugin('xep_0045')
|
|
|
|
|
bot.process()
|
2015-06-17 10:53:11 +02:00
|
|
|
|
|
|
|
|
while 1:
|
|
|
|
|
try:
|
2015-11-20 21:07:48 +01:00
|
|
|
if not plugins.event_trigger():
|
|
|
|
|
bot.disconnect()
|
2015-06-20 15:13:12 +02:00
|
|
|
sys.exit(1)
|
2015-06-17 10:53:11 +02:00
|
|
|
|
2015-08-21 23:40:14 +02:00
|
|
|
time.sleep(EVENTLOOP_DELAY)
|
2015-06-17 10:53:11 +02:00
|
|
|
except KeyboardInterrupt:
|
|
|
|
|
print('')
|
|
|
|
|
exit(130)
|
2015-11-20 21:07:48 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
if '__main__' == __name__:
|
|
|
|
|
start(IdleBot)
|