Repository navigation
Expand file tree
/
Copy pathmisc.py
More file actions
308 lines (261 loc) · 7.7 KB
/
Copy pathmisc.py
File metadata and controls
308 lines (261 loc) · 7.7 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#!/usr/bin/env python
import errno
import gettext
import locale
import os
import pickle
import slixmpp
import sys
import time
import re
import config
gettext.install('messages', 'locale')
locale.setlocale(locale.LC_TIME, '')
lctime = lambda t: time.strftime('%c %Z', time.localtime(t))
restarting = False
quiting = False
msg_log = []
cmd_log = []
nick_table = {}
def add_nicktable(xmpp, jid):
nick_table[getnick(xmpp, jid)] = jid
def change_nicktable(xmpp, jid, newnick):
oldnick = getnick(xmpp, jid)
nick_table[newnick] = jid
if oldnick in nick_table:
del nick_table[oldnick]
def del_nicktable(xmpp, jid):
nick = getnick(xmpp, jid)
if nick in nick_table:
del nick_table[nick]
def get_nicktable(xmpp, nick):
if nick in nick_table:
return nick_table[nick]
else:
return None
def getnick(xmpp, nick_or_jid):
if nick_or_jid in nick_table:
return nick_or_jid
elif isjidvalid(nick_or_jid) and nick_or_jid in xmpp.client_roster and xmpp.client_roster[nick_or_jid]['to']:
nick = xmpp.client_roster[nick_or_jid]['name']
if nick:
return nick
else:
return slixmpp.JID(nick_or_jid).user
else:
return None
def getjid(xmpp, nick_or_jid):
if nick_or_jid in nick_table:
return nick_table[nick_or_jid]
elif isjidvalid(nick_or_jid) and nick_or_jid in xmpp.client_roster and xmpp.client_roster[nick_or_jid]['to']:
return nick_or_jid
else:
return None
def isnickvalid(nick):
nick = str(nick)
return bool(nick and (nick[0] not in config.command_prefix) and (nick[0] != '-') and ('@' not in nick) and ('?' not in nick) and ('*' not in nick) and (nick.lower() != 'root') and (nick.lower() != 'admin') and (nick.lower() != 'administrator'))
def isjidvalid(jid):
jid = str(jid)
return bool(jid and 0 < jid.find('@') < len(jid) - 1)
def replace_prefix(s, prefix):
res = ''
lastisslash = False
for i in s:
if lastisslash:
if i == '-':
res += prefix
else:
res += i
lastisslash = False
elif i == '/':
lastisslash = True
else:
res += i
if lastisslash:
res += '/'
return res
def replace_glob_to_regex(glob):
res = ''
for i in glob:
if i == '*':
res += '.*'
elif i == '?':
res += '.'
else:
res += re.escape(i)
return '^' + res + '$'
def replace_globs_to_regex(glob):
if isinstance(glob, str):
return replace_glob_to_regex(glob)
if len(glob) > 0:
return '^(' + '|'.join([replace_glob_to_regex(i)[1:-1] for i in glob]) + ')$'
else:
return '^.*'
def find_users(xmpp, glob, isAdmin):
haveglob = False
regex = []
for i in glob:
if not i or i.startswith('-'):
continue
regex.append(i)
if '?' in i or '*' in i:
haveglob = True
break
regex = re.compile(replace_globs_to_regex(glob))
res = []
for i in xmpp.client_roster:
if xmpp.client_roster[i]['to'] and xmpp.client_roster[i]['subscription'] == 'both':
if isAdmin or not haveglob:
if regex.match(i) or regex.match(getnick(xmpp, i)):
res.append(i)
else:
if regex.match(getnick(xmpp, i)):
res.append(i)
res.sort()
return res
data = {}
def load_data(filename=config.datafile):
global data
try:
f = open(filename, 'rb')
try:
data = pickle.load(f)
except (pickler.PickleError, ValueError):
sys.stderr.write('Error when loading profile. Created an empty one.\n')
finally:
f.close()
except IOError as e:
if e.errno == errno.ENOENT:
sys.stderr.write('Created an empty profile.\n')
else:
raise
def save_data(filename=config.datafile):
save_okay = False
f = open(filename + '~', 'wb')
try:
pickle.dump(data, f)
save_okay = True
finally:
f.close()
if save_okay:
os.rename(filename + '~', filename)
time_unit = {'u': .000001, 'U': .000001, 'z': .001, 'Z': .001, 'S': 1, 's': 1, 'm': 60, 'H': 3600, 'h': 3600, 'D': 86400, 'd': 86400, 'M': 2629743.7710168, 'Y': 31556925.2522016, 'y': 31556925.2522016, 'C': 31556925252.2016, 'c': 31556925252.2016}
time_unit_chars = 'cyMdhms'
class TimeUnit(float):
def __new__(cls, s):
s = str(s)
if not s:
raise ValueError
if s[0] == '-':
isNeg = True
s = s[1:]
elif s[0] == '+':
isNeg = False
s = s[1:]
else:
isNeg = False
res = 0
cur = ''
for i in s:
if i.isdigit() or i == '.':
cur += i
elif i in time_unit:
res += float(cur) * time_unit[i]
cur = ''
elif not i.isspace():
raise ValueError('invalid liternal for %s(): %s' % (cls, repr(s)))
if cur:
res += float(cur)
if isNeg:
res = -res
return float.__new__(cls, res)
def __repr__(self):
return '%s(%s)' % (type(self).__name__, repr(str(self)))
def __str__(self):
value = float(self)
if value < 0:
res = '-'
value = -value
else:
res = ''
for i in time_unit_chars[:-1]:
if value >= time_unit[i]:
times, value = divmod(value, time_unit[i])
times = int(times)
if times > 0:
res += str(times) + i
i = time_unit_chars[-1]
if value + value >= time_unit[i]:
times, value = divmod(value, time_unit[i])
times = int(times)
if value + value >= time_unit[i]:
times += 1
if times > 0:
res += str(times) + i
if not res or res == '-':
res = '0'
return res
def compare_status(a, b):
if not a or a == 'available':
va = 4
elif a == 'chat':
va = 5
elif a == 'dnd' or a == 'busy':
va = 3
elif a == 'away':
va = 2
elif a == 'xa' or a == 'extended away':
va = 1
elif a == 'unavailable':
va = 0
else:
va = 6
if not b or b == 'available':
vb = 4
elif b == 'chat':
vb = 5
elif b == 'dnd' or b == 'busy':
vb = 3
elif b == 'away':
vb = 2
elif b == 'xa' or b == 'extended away':
vb = 1
elif b == 'unavailable':
vb = 0
else:
vb = 6
return va - vb
def get_status_name(s):
if not s or s == 'available':
return _('available')
elif s == 'chat':
return _('want chat')
elif s == 'dnd' or s == 'busy':
return _('do not disturb')
elif s == 'away':
return _('away')
elif s == 'xa' or s == 'extended away':
return _('extended away')
elif s == 'unavailable':
return _('unavailable')
else:
return s
def check_time(xmpp, from_dict, to_jid):
if to_jid in from_dict:
if from_dict[to_jid] is None:
return False
else:
if from_dict[to_jid] > time.time():
return False
else:
del from_dict[to_jid]
save_data()
try:
if to_jid not in data['stop'] and to_jid not in data['quiet']:
xmpp.send_presence(pto=to_jid, pshow='', pstatus=config.group_topic)
except:
pass
return True
else:
return True
# vim: et ft=python sts=4 sw=4 ts=4