This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
idlebot.py
executable file
·130 lines (106 loc) · 3.83 KB
/
idlebot.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import logging
import sys
import time
import _ssl
from sleekxmpp import ClientXMPP
import config
import events
from common import VERSION
class IdleBot(ClientXMPP):
def __init__(self, jid, password, rooms, nick):
ClientXMPP.__init__(self, jid, password)
self.ssl_version = _ssl.PROTOCOL_TLSv1_2
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.add_event_handler('disconnected', self.disconnected)
self.add_event_handler('presence_error', self.disconnected)
self.add_event_handler('session_end', self.disconnected)
self.priority = 0
self.status = None
self.show = None
self.logger = logging.getLogger(__name__)
for room in self.rooms:
self.add_event_handler('muc::%s::got_offline' % room, self.muc_offline)
def talked_to_me(self, text):
return text[:len(self.nick)].lower() == self.nick.lower()
def disconnected(self, _):
self.logger.warn("Disconnected! dbg: {}".format(str(_)))
self.disconnect(wait=True)
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
elif self.talked_to_me(msg_obj['body']) and 'hangup' in msg_obj['body']:
self.logger.warn("got 'hangup' from '%s': '%s'" % (
msg_obj['mucnick'], msg_obj['body']
))
self.hangup()
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
else:
return True
def muc_offline(self, msg_obj):
if 'muc' in msg_obj.values:
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
)
def hangup(self):
"""
disconnect and exit
"""
self.disconnect(wait=True)
def start(botclass, active=False):
logging.basicConfig(
level=config.conf_get('loglevel'),
format=sys.argv[0] + ' %(asctime)s %(levelname).1s %(funcName)-15s %(message)s'
)
logger = logging.getLogger(__name__)
logger.info(VERSION)
jid = config.conf_get('jid')
if '/' not in jid:
jid = '%s/%s' % (jid, botclass.__name__)
bot = botclass(
jid=jid,
password=config.conf_get('password'),
rooms=config.conf_get('rooms'),
nick=config.conf_get('bot_nickname')
)
bot.connect()
bot.register_plugin('xep_0045')
bot.register_plugin('xep_0199', {'keepalive': True})
bot.register_plugin('xep_0308')
bot.process()
config.runtimeconf_set('start_time', -time.time())
if active:
pass
events.event_loop.start()
if '__main__' == __name__:
start(IdleBot)