Repository navigation
Expand file tree
/
Copy pathchatlist.py
More file actions
executable file
·222 lines (203 loc) · 9.6 KB
/
Copy pathchatlist.py
File metadata and controls
executable file
·222 lines (203 loc) · 9.6 KB
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#!/usr/bin/env python
import gettext
import os
import random
import sleekxmpp
import sys
import time
import logging
import signal
import command
import config
import misc
import msgfilters
gettext.install('messages', 'locale')
class XMPPBot(sleekxmpp.ClientXMPP):
def __init__(self, jid, password):
sleekxmpp.ClientXMPP.__init__(self, jid, password)
self.add_event_handler('session_start', self.start)
self.add_event_handler('message', self.message)
self.add_event_handler('presence_subscribe', self.subscribe)
self.add_event_handler('presence_subscribed', self.subscribed)
self.add_event_handler('presence_unsubscribe', self.unsubscribe)
self.add_event_handler('got_online', self.gotonline)
def start(self, event):
self.send_presence(pshow='', pnick=config.group_nick, pstatus=config.group_topic)
self.get_roster()
self.auto_authorize = True
self.auto_subscribe = True
sys.stderr.write('roster = [\n')
for i in self.client_roster:
if self.client_roster[i]['to']:
if self.client_roster[i]['subscription'] == 'both':
sys.stderr.write('\t%s' % i)
misc.add_nicktable(self, i)
if not (misc.check_time(self, misc.data['stop'], i) or misc.check_time(self, misc.data['quiet'], i)):
self.send_presence(pto=i, pshow='dnd', pnick=config.group_nick, pstatus=config.group_topic)
sys.stderr.write('\n')
elif self.client_roster[i]['subscription'] == 'to':
try:
self.del_roster_item(i)
if i in misc.data['stop']:
del misc.data['stop'][i]
except Exception:
pass
sys.stderr.write(']\n')
def gotonline(self, presence):
try:
from_jid = sleekxmpp.JID(presence['from']).bare
if misc.check_time(self, misc.data['stop'], from_jid) and misc.check_time(self, misc.data['quiet'], from_jid):
self.send_presence(pto=presence['from'], pshow='', pnick=config.group_nick, pstatus=config.group_topic)
else:
self.send_presence(pto=presence['from'], pshow='dnd', pnick=config.group_nick, pstatus=config.group_topic)
client_resource = sleekxmpp.JID(presence['from']).resource
if client_resource.startswith('Talk.v') and client_resource[6:9] > '104':
self.send_message(mto=presence['from'], mbody=_('Warning: You are probably using GTalk v%s version, which sends your data unsecurely. Please consider downgrading it to GTalk v104 or try third-party clients such as Pidgin.') % client_resource[6:9], mtype='chat')
except Exception:
pass
def subscribe(self, presence):
sys.stderr.write('%s subscribed me.\n' % presence['from'])
self.send_presence(pto=sleekxmpp.JID(presence['from']).bare, pshow='away', pnick=config.group_nick, pstatus=_('Not accepted subscription yet'))
def subscribed(self, presence):
jid = sleekxmpp.JID(presence['from']).bare
sys.stderr.write('I subcribed %s.\n' % presence['from'])
while True:
to_nick = int(random.random() * 90000 + 10000)
if not misc.getnick(self, to_nick):
break
to_nick += 1
to_nick = 'Guest' + str(to_nick)
self.update_roster(jid, name=to_nick)
misc.add_nicktable(self, jid)
self.send_message(mto=presence['from'], mbody=misc.replace_prefix(config.welcome_message, config.command_prefix[0]), mtype='chat')
self.send_message(mto=presence['from'], mbody=misc.replace_prefix(_('You have been given a random nickname %s, please use /-nick to change your nickname.'), config.command_prefix[0]) % to_nick, mtype='chat')
self.send_message(mto=presence['from'], mbody=misc.replace_prefix(_('For more help, type /-help'), config.command_prefix[0]), mtype='chat')
self.send_except(jid, _('%s has joined this group.') % to_nick)
self.send_presence(pto=jid, pshow='', pnick=config.group_nick, pstatus=config.group_topic)
def unsubscribe(self, presence):
from_jid = sleekxmpp.JID(presence['from']).bare
if from_jid in misc.data['stop'][from_jid]:
del misc.data['stop'][from_jid]
misc.save_data()
from_nick = misc.getnick(self, from_jid)
try:
self.del_roster_item(from_jid)
except Exception:
pass
sys.stderr.write('%s unsubscribed me.\n' % presence['from'])
self.send_except(from_jid, _('%s has quited this group.') % from_nick)
def message(self, msg):
try:
if misc.quiting:
return
if msg['type'] not in ('chat', 'normal'):
return
if not msg['from'].user:
return
from_jid = msg['from'].bare
body = msg['body']
body = body.rstrip()
if not body:
return
sys.stderr.write('%s:\t%s\n' % (from_jid, body))
if from_jid not in self.client_roster or self.client_roster[from_jid]['subscription'] != 'both':
if self.client_roster[from_jid]['subscription'] == 'from':
msg.reply(_('You have not accept the buddy request.')).send()
else:
msg.reply(_('You have not joined this group.')).send()
return
if len(body) > 1 and body[0] in config.command_prefix and len(body.split(None, 1)[0]) > 1 and not (body[0] == '-' and body[1:].isdigit()):
command.trigger(self, msg)
else:
presence_needs_update = False
if from_jid in misc.data['stop']:
del misc.data['stop'][from_jid]
misc.save_data()
presence_needs_update = True
if not misc.check_time(self, misc.data['quiet'], from_jid):
msg.reply(_('You have been quieted.')).send()
return
if presence_needs_update:
self.send_presence(pto=from_jid, pshow='', pnick=config.group_nick, pstatus=config.group_topic)
for msg_filter in msgfilters.msg_filters:
if not msg_filter(self, msg):
return
for l in body.splitlines():
self.dispatch_message(from_jid, l)
except UnicodeError:
pass
except SystemExit:
raise
except Exception as e:
logging.warning('Exception: %s: %s\n' % (type(e).__name__, e))
def dispatch_message(self, from_jid, body):
if from_jid in config.omit_nickname_prefix:
self.send_except(from_jid, body)
else:
self.send_except(from_jid, '%s: %s' % (misc.getnick(self, from_jid), body))
def send_except(self, except_jid, body):
nowtime = time.time()
misc.msg_log.append((nowtime, body))
if len(misc.msg_log) > config.logsize:
misc.msg_log[len(misc.msg_log) - config.logsize:] = []
for i in self.client_roster:
if i != except_jid and self.client_roster[i]['to'] and self.client_roster[i]['subscription'] == 'both' and self.client_roster[i].resources:
misc.check_time(self, misc.data['quiet'], i)
if misc.check_time(self, misc.data['stop'], i) and (i not in misc.data['block'] or except_jid not in misc.data['block'][i]):
try:
self.send_message(mto=i, mbody=body, mtype='chat')
except Exception:
pass
if __name__ == '__main__':
signal.signal(signal.SIGTERM, lambda signal, frame: sys.exit()) # handle SIGTERM, graceful exit
misc.restarting = False
misc.quiting = False
misc.load_data()
logging.basicConfig(level=config.loglevel)
for i in ('stop', 'quiet', 'block'):
if i not in misc.data:
misc.data[i] = {}
if config.store_log:
if 'msg_log' in misc.data:
misc.msg_log = misc.data['msg_log']
else:
misc.data['msg_log'] = misc.msg_log
if 'cmd_log' in misc.data:
misc.cmd_log = misc.data['cmd_log']
else:
misc.data['cmd_log'] = misc.cmd_log
for i in config.root:
if i not in config.admins:
config.admins.append(i)
try:
try:
xmpp = XMPPBot(config.JID, config.password)
xmpp.register_plugin('xep_0030') # Service Discovery
xmpp.register_plugin('xep_0004') # Data Forms
xmpp.register_plugin('xep_0060') # PubSub
xmpp.register_plugin('xep_0199') # XMPP Ping
if xmpp.connect(config.server):
xmpp.process(block=True)
else:
logging.warning('Connection error.')
time.sleep(10)
misc.restarting = True
except Exception as e:
logging.warning('Exception: %s: %s\n' % (type(e).__name__, e))
time.sleep(10)
misc.restarting = True
raise SystemExit
except (SystemExit, KeyboardInterrupt):
sys.stderr.write('Quiting...')
time.sleep(3)
xmpp.disconnect(wait=True)
misc.save_data()
sys.stderr.write('\n')
if misc.restarting:
sys.stderr.write('Restarting.\n')
try:
os.execlp('python3', 'python3', __file__)
except Exception:
os.execlp('python', 'python', __file__)
raise SystemExit
# vim: et ft=python sts=4 sw=4 ts=4