idlebot: implement 'hangup'

This commit is contained in:
urlbot
2015-06-20 15:13:12 +02:00
parent 0b63a09077
commit 08df294e87
2 changed files with 29 additions and 1 deletions

View File

@@ -20,6 +20,8 @@ except ImportError:
import logging
from sleekxmpp import ClientXMPP
got_hangup = False
class bot(ClientXMPP):
def __init__(self, jid, password, rooms, nick):
ClientXMPP.__init__(self, jid, password)
@@ -28,19 +30,38 @@ class bot(ClientXMPP):
self.nick = nick
self.add_event_handler('session_start', self.session_start)
self.add_event_handler('groupchat_message', self.muc_message)
def session_start(self, event):
self.get_roster()
self.send_presence()
for room in self.rooms:
print('joining %s' % room)
logger('info', 'joining %s' % room)
self.plugin['xep_0045'].joinMUC(
room,
self.nick,
wait=True
)
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']:
logger('warn', "got 'hangup' from '%s': '%s'" % (
msg_obj['mucnick'], msg_obj['body']
))
got_hangup = True
sys.exit(1)
if '__main__' == __name__:
print(sys.argv[0] + ' ' + VERSION)
@@ -63,6 +84,9 @@ if '__main__' == __name__:
while 1:
try:
# do nothing here, just idle
if got_hangup:
xmpp.disconnect()
sys.exit(1)
time.sleep(delay)
except KeyboardInterrupt: