1
0
mirror of http://aero2k.de/t/repos/urlbot-native.git synced 2017-09-06 15:25:38 +02:00
Files
urlbot-native-trex/idlebot.py

129 lines
3.7 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__)
2015-12-26 21:03:09 +01:00
for room in self.rooms:
self.add_event_handler('muc::%s::got_offline' % room, self.muc_offline)
2015-11-30 19:17:40 +01:00
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
# elif msg_obj['mucnick'] in config.runtimeconf_get("other_bots", ()):
# self.logger.debug("not talking to the other bot named {}".format( msg_obj['mucnick']))
# return False
2015-11-30 19:17:40 +01:00
else:
return True
2015-06-20 15:13:12 +02:00
2015-12-26 21:03:09 +01:00
def muc_offline(self, msg_obj):
room = msg_obj.values['muc']['room']
user = msg_obj.values['muc']['nick']
if user == config.conf_get('bot_nickname'):
self.logger.warn("Left my room, rejoin")
self.plugin['xep_0045'].joinMUC(
room,
self.nick,
wait=True
)
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()
2015-12-21 00:12:10 +01:00
config.runtimeconf_set('start_time', -time.time())
while 1:
try:
# print("hangup: %s" % got_hangup)
if not plugins.event_trigger():
bot.hangup()
if bot.state.current_state() == 'disconnected':
exit(0)
time.sleep(EVENTLOOP_DELAY)
except KeyboardInterrupt:
print('')
exit(130)
if '__main__' == __name__:
2015-11-30 19:17:40 +01:00
start(IdleBot)