2014-12-02 13:55:11 +01:00
|
|
|
#!/usr/bin/python3
|
|
|
|
|
|
2014-12-02 14:00:43 +01:00
|
|
|
import logging
|
|
|
|
|
|
2014-12-02 13:55:11 +01:00
|
|
|
from sleekxmpp import ClientXMPP
|
2014-12-02 14:26:41 +01:00
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
from local_config import conf
|
|
|
|
|
except ImportError:
|
|
|
|
|
import sys
|
|
|
|
|
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(10)
|
2014-12-02 13:55:11 +01:00
|
|
|
|
|
|
|
|
import time
|
|
|
|
|
t = -time.time()
|
|
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
|
|
|
|
|
class Bot(ClientXMPP):
|
2014-12-02 13:55:11 +01:00
|
|
|
def __init__(self, jid, password, room, nick):
|
|
|
|
|
ClientXMPP.__init__(self, jid, password)
|
|
|
|
|
|
|
|
|
|
self.room = room
|
|
|
|
|
self.nick = nick
|
|
|
|
|
|
|
|
|
|
self.add_event_handler('session_start', self.session_start)
|
2014-12-02 14:49:12 +01:00
|
|
|
self.add_event_handler('groupchat_message', self.muc_message)
|
2014-12-02 13:55:11 +01:00
|
|
|
|
|
|
|
|
def session_start(self, event):
|
|
|
|
|
self.get_roster()
|
|
|
|
|
self.send_presence()
|
|
|
|
|
|
|
|
|
|
self.plugin['xep_0045'].joinMUC(
|
|
|
|
|
self.room,
|
|
|
|
|
self.nick,
|
|
|
|
|
wait=True
|
|
|
|
|
)
|
|
|
|
|
|
2014-12-02 14:49:12 +01:00
|
|
|
def muc_message(self, msg):
|
|
|
|
|
print(msg['mucnick'])
|
|
|
|
|
print(msg['body'])
|
2014-12-02 15:22:02 +01:00
|
|
|
print((msg['from'], msg['from'].bare))
|
|
|
|
|
|
|
|
|
|
print(conf('room') == msg['from'].bare)
|
2014-12-02 13:55:11 +01:00
|
|
|
|
2014-12-02 14:49:12 +01:00
|
|
|
# don't talk to yourself
|
|
|
|
|
if msg['mucnick'] == self.nick:
|
|
|
|
|
return
|
2014-12-02 13:55:11 +01:00
|
|
|
|
2014-12-02 14:49:12 +01:00
|
|
|
self.send_message(
|
|
|
|
|
mto=msg['from'].bare,
|
|
|
|
|
mbody='got[%s]' % msg['body'],
|
|
|
|
|
mtype='groupchat'
|
|
|
|
|
)
|
2014-12-02 13:55:11 +01:00
|
|
|
|
|
|
|
|
if '__main__' == __name__:
|
2014-12-02 14:00:43 +01:00
|
|
|
logging.basicConfig(
|
|
|
|
|
level=logging.DEBUG,
|
|
|
|
|
format='%(levelname)-8s %(message)s'
|
|
|
|
|
)
|
|
|
|
|
|
2015-11-20 21:07:48 +01:00
|
|
|
xmpp = Bot(
|
2014-12-02 13:55:11 +01:00
|
|
|
jid=conf('jid'),
|
|
|
|
|
password=conf('password'),
|
|
|
|
|
room=conf('room'),
|
|
|
|
|
nick=conf('bot_user')
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
xmpp.connect()
|
|
|
|
|
xmpp.register_plugin('xep_0045')
|
2014-12-02 14:49:12 +01:00
|
|
|
xmpp.process(threaded=False)
|