-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuser.py
187 lines (170 loc) · 5.06 KB
/
user.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
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
# -*- coding: utf-8 -*-
# Copyright 2008, 2009 Mr.Z-man, bjweeks
# This file is part of wikitools.
# wikitools is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# wikitools is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
# You should have received a copy of the GNU General Public License
# along with wikitools. If not, see <http://www.gnu.org/licenses/>.
import wiki
import page
import api
import socket
class User:
"""A user on the wiki"""
def __init__(self, site, name, check=True):
"""
wiki - A wiki object
name - The username, as a string
check - Checks for existence, normalizes name
"""
self.site = site
self.name = name
if not isinstance(self.name, unicode):
self.name = unicode(self.name, 'utf8')
self.exists = True # If we're not going to check, assume it does
self.blocked = None # So we can tell the difference between blocked/not blocked/haven't checked
self.editcount = -1
self.groups = []
if check:
self.setUserInfo()
self.isIP = False
try:
s = socket.inet_aton(self.name.replace(' ', '_'))
if socket.inet_ntoa(s) == self.name:
self.isIP = True
self.exists = False
except:
pass
self.page = page.Page(self.site, ':'.join([self.site.namespaces[2]['*'], self.name]), check=check, followRedir=False)
def setUserInfo(self):
"""Sets basic user info"""
params = {
'action': 'query',
'list': 'users',
'ususers':self.name,
'usprop':'blockinfo|groups|editcount'
}
req = api.APIRequest(self.site, params)
response = req.query()
user = response['query']['users'][0]
self.name = user['name']
if 'missing' in user or 'invalid' in user:
self.exists = False
return
self.editcount = int(user['editcount'])
if 'groups' in user:
self.groups = user['groups']
if 'blockedby' in user:
self.blocked = True
else:
self.blocked = False
return self
def isBlocked(self, force=False):
"""Determine if a user is blocked"""
if self.blocked is not None and not force:
return self.blocked
params = {'action':'query',
'list':'blocks',
'bkusers':self.name,
'bkprop':'id'
}
req = api.APIRequest(self.site, params)
res = req.query(False)
if len(res['query']['blocks']) > 0:
self.blocked = True
else:
self.blocked = False
return self.blocked
def block(self, reason=False, expiry=False, anononly=False, nocreate=False, autoblock=False, noemail=False, hidename=False, allowusertalk=False, reblock=False):
"""Block the user
Params are the same as the API
reason - block reason
expiry - block expiration
anononly - block anonymous users only
nocreate - disable account creation
autoblock - block IP addresses used by the user
noemail - block user from sending email through the site
hidename - hide the username from the log (requires hideuser right)
allowusertalk - allow the user to edit their talk page
reblock - overwrite existing block
"""
params = {'action':'block',
'user':self.name,
'gettoken':''
}
req = api.APIRequest(self.site, params)
res = req.query()
token = res['block']['blocktoken']
params = {'action':'block',
'user':self.name,
'token':token
}
if reason:
params['reason'] = reason
if expiry:
params['expiry'] = expiry
if anononly:
params['anononly'] = ''
if nocreate:
params['nocreate'] = ''
if autoblock:
params['autoblock'] = ''
if noemail:
params['noemail'] = ''
if hidename:
params['hidename'] = ''
if allowusertalk:
params['allowusertalk'] = ''
if reblock:
params['reblock'] = ''
req = api.APIRequest(self.site, params, write=False)
res = req.query()
if 'block' in res:
self.blocked = True
return res
def unblock(self, reason=False):
"""Unblock the user
reason - reason for the log
"""
params = {
'action': 'unblock',
'user': self.name,
'gettoken': ''
}
req = api.APIRequest(self.site, params)
res = req.query()
token = res['unblock']['unblocktoken']
params = {
'action': 'unblock',
'user': self.name,
'token': token
}
if reason:
params['reason'] = reason
req = api.APIRequest(self.site, params, write=False)
res = req.query()
if 'unblock' in res:
self.blocked = False
return res
def __eq__(self, other):
if not isinstance(other, User):
return False
if self.name == other.name and self.site == other.wiki:
return True
return False
def __ne__(self, other):
if not isinstance(other, User):
return True
if self.name == other.name and self.site == other.wiki:
return False
return True
def __str__(self):
return self.__class__.__name__ + ' ' + repr(self.name) + " on " + repr(self.site.domain)
def __repr__(self):
return "<"+self.__module__+'.'+self.__class__.__name__+" "+repr(self.name)+" on "+repr(self.site.apibase)+">"