Files
urlbot-native/idlebot.py

90 lines
1.8 KiB
Python
Raw Normal View History

2015-06-17 10:53:11 +02:00
#!/usr/bin/python3
# -*- coding: utf-8 -*-
from common import *
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])
)
)
from sleekxmpp import ClientXMPP
2015-06-20 15:13:12 +02:00
got_hangup = False
2015-06-17 10:53:11 +02:00
class bot(ClientXMPP):
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
def session_start(self, event):
self.get_roster()
self.send_presence()
for room in self.rooms:
2015-11-14 11:06:07 +01:00
log.info('%s: joining' % room)
ret = self.plugin['xep_0045'].joinMUC(
2015-06-17 10:53:11 +02:00
room,
self.nick,
wait=True
)
2015-11-14 11:06:07 +01:00
log.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):
global got_hangup
# 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']:
log.warn("got 'hangup' from '%s': '%s'" % (
2015-06-20 15:13:12 +02:00
msg_obj['mucnick'], msg_obj['body']
))
got_hangup = True
sys.exit(1)
2015-06-17 10:53:11 +02:00
if '__main__' == __name__:
log.info(VERSION)
2015-06-17 10:53:11 +02:00
xmpp = bot(
jid=conf('jid'),
password=conf('password'),
rooms=conf('rooms'),
nick=conf('bot_user')
)
xmpp.connect()
xmpp.register_plugin('xep_0045')
xmpp.process()
while 1:
try:
# do nothing here, just idle
2015-06-20 15:13:12 +02:00
if got_hangup:
xmpp.disconnect()
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)