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

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

View File

@@ -285,6 +285,10 @@ class bot(ClientXMPP):
print('msg from %s: %s' % (msg_obj['from'].bare, msg_obj)) print('msg from %s: %s' % (msg_obj['from'].bare, msg_obj))
# def set_presence(self, msg):
# for room in self.rooms:
# self.send_presence(pto=room, pstatus=msg)
if '__main__' == __name__: if '__main__' == __name__:
import plugins import plugins