-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmagnet_utils.py
109 lines (96 loc) · 2.93 KB
/
magnet_utils.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
# Magnet2 by Grom PE. Public domain.
import xmpp, os, time, cPickle
try:
from HTMLParser import HTMLParser
htmlparser_available = True
except:
htmlparser_available = False
def iq_set_affiliation(room, nick, affiliation, reason=None):
iq = xmpp.Iq('set', xmpp.NS_MUC_ADMIN, {}, room)
item = iq.getTag('query').setTag('item')
item.setAttr('nick', nick)
item.setAttr('affiliation', affiliation)
if reason: item.addChild('reason', {}, reason)
return iq
def iq_set_role(room, nick, role, reason=None):
iq = xmpp.Iq('set', xmpp.NS_MUC_ADMIN, {}, room)
item = iq.getTag('query').setTag('item')
item.setAttr('nick', nick)
item.setAttr('role', role)
if reason: item.addChild('reason', {}, reason)
return iq
def serialize(fname, data):
f = open(fname, 'wb')
result = cPickle.dump(data, f, 2)
f.close()
return result
def unserialize(fname):
if not os.path.exists(fname): return False
f = open(fname, 'rb')
result = cPickle.load(f)
f.close()
return result
def writelog(filename, text):
s = '[%s] %s\n'%(time.strftime('%d %b %Y %H:%M:%S'), text)
f = open(filename, 'a')
f.write(s.encode('utf-8'))
f.close()
def hasbadwords(text):
badwords = ['bitch', 'fuck', 'asshole', 'shit', 'cunt', 'whore', 'slut',
'cocksuck', 'f*ck', 'b*tch', 'sh*t', 'sh!t', 'faggot', 'whor3',
'b!tch', 'phuck', 'sh1t', 'nigger', 'wank', 'goddamn', 'dickhead',
'bollocks', 'bastard', 'my dick', 'dafuq', 'biatch']
textl = text.replace(u'\xad', '').lower()
for word in badwords:
if word in textl: return True
return False
def unhtml(content):
if htmlparser_available:
return HTMLParser().unescape(content)
content = content.replace('<', '<')
content = content.replace('>', '>')
content = content.replace('"', '"')
content = content.replace(''', "'")
return content.replace('&', '&')
def timeformat(s):
s = s//1 # Rounding
days = s//86400
s -= days*86400
hours = s//3600
s -= hours*3600
minutes = s//60
s -= minutes*60
result = ''
limit = 0
if days>0:
result += ' %d day%s'%(days, ('', 's')[days>1])
limit = 2
if hours>0:
result += ' %d hour%s'%(hours, ('', 's')[hours>1])
limit += 1
if limit<2 and minutes>0:
result += ' %d minute%s'%(minutes, ('', 's')[minutes>1])
if limit<1 and s>0:
result += ' %d second%s'%(s, ('', 's')[s>1])
return result[1:]
def separate_target_reason(bot, room, parameters):
target = parameters
reason = None
if not target in bot.roster[room]:
p = len(parameters)
while True:
p = parameters.rfind(' ', 0, p)
if p == -1:
if parameters.find(' ') != -1:
(target, reason) = parameters.split(' ', 1)
break
if parameters[:p] in bot.roster[room]:
target = parameters[:p]
reason = parameters[p+1:]
break
return (target, reason)
def force_directory(dirname):
if not os.path.exists(dirname):
os.makedirs(dirname, 0755)
if __name__ == "__main__":
pass