-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusermgmtfunc.py
273 lines (229 loc) · 6.49 KB
/
usermgmtfunc.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
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
# Account management.
# Supports querying and modifying levels and passwords, as well as adding
# new users.
# TODO: Split the granting/denying logic here into separate functions
def userMgmtFunc(self, msg):
if not(hasPriv(msg.nick, 'acctMgmt', 20)):
return(config.privrejectgeneric)
if len(msg.cmd) == 1:
return('''This function requires more arguments. See 'help user' for details.''')
# Make a copy since we'll be changing this
args = msg.cmd[1:]
# First part: -u for user or -n for nick
# -n can only work when the user is logged in
# -u should be able to work any timet
if args[0][0:2] == '-u':
user = True
elif args[0][0:2] == '-n':
user = False
else:
return('''Incorrect syntax. See 'help user' for details. ''')
if len(args[0]) > 2:
name = args[0][2:]
args = args[1:]
else:
name = args[1]
args = args[2:]
if len(args) < 1:
return('''This function requires more arguments. See 'help user' for details.''')
# Actions: level, privs/priv
if args[0] == 'level':
action = 0
args = args[1:]
elif args[0] == 'privs' or args[0] == 'priv':
action = 1
args = args[1:]
elif args[0] == 'add':
action = 2
else:
return('''Action must be 'add', 'level' or 'privs'.''')
if action == 0:
if len(args) == 0:
return('Not enough arguments.')
elif args[0] == 'get':
if user:
try:
authEntry = userLookup(name)
except UserNotFound:
return('That user was not found')
level = authEntry.level
return('User %s is level %s' % (name, level))
else:
try:
authEntry = getAuth(name)
except UserNotFound:
return('That user was not found')
level = authEntry.level
return('User %s is level %s' % (name, level))
elif args[0] == 'set':
try:
newLevel = int(args[1])
except:
return('Level must be an integer.')
if user:
try:
chgUserLvl(name, newLevel)
except UserNotFound:
return('User not found')
return('''Changed level for user '%s' to %s. ''' % (name, newLevel))
else:
try:
authEntry = getAuth(name)
except UserNotFound:
return('User not found.')
authEntry.level = newLevel
try:
chgUserLvl(authEntry.authName, newLevel)
except:
return('''Changed level for user, but couldn't update file.''')
return('''Changed level for nick '%s' to %s. ''' % (name, str(newLevel)))
else:
return('''Action must be 'get' or 'set'.''')
if action == 1:
if len(args) == 0:
return('Not enough arguments.')
elif args[0] == 'get':
try:
if user:
authEntry = userLookup(name)
else:
authEntry = getAuth(name)
except UserNotFound:
return('User not found.')
return('User %s has privileges: %s. ' % (name, formatPerms(authEntry.grant, authEntry.deny, ', ')))
if args[0] == 'clear':
if args[1:]:
if user:
try:
authEntry = userLookup(name)
except UserNotFound:
return('User not found.')
grant = authEntry.grant
deny = authEntry.deny
for i in args[1:]:
if i in grant:
grant.remove(i)
elif i in deny:
deny.remove(i)
try:
chgUserPrivs(name, grant, deny)
except UserNotFound:
return('User was found initially, but not when updating.')
return('Cleared privilege(s) from user %s.' % name)
else:
try:
authEntry = getAuth(name)
except UserNotFound:
return('User not found.')
grant = authEntry.grant
deny = authEntry.deny
for i in args[1:]:
if i in grant:
grant.remove(i)
elif i in deny:
deny.remove(i)
try:
chgUserPrivs(authEntry.authName, grant, deny)
except UserNotFound:
return('Cleared privilege(s), but could not update file.')
return('''Cleared privilege(s) for nick '%s'.''' % name)
else:
if user:
try:
chgUserPrivs(name, set(), set())
except UserNotFound:
return('User not found.')
return('Cleared special privileges for user %s.' % name)
else:
try:
authEntry = getAuth(name)
except UserNotFound:
return('Could not find that nick.')
authEntry.grant = set()
authEntry.deny = set()
try:
chgUserPrivs(authEntry.authName, set(), set())
except UserNotFound:
return('Cleared special privileges for nick, but could not update file.')
return('Cleared special privileges for nick %s.' % name)
if args[0] == 'grant':
if args[1:]:
if user:
try:
authEntry = userLookup(name)
except UserNotFound:
return('User not found.')
grant = authEntry.grant
deny = authEntry.deny
for i in args[1:]:
if i in deny:
deny.remove(i)
grant.add(i)
try:
chgUserPrivs(name, grant, deny)
except UserNotFound:
return('User was found initially but not while updating')
return('Changed privilege(s) for user %s.' % name)
else:
try:
authEntry = getAuth(name)
except UserNotFound:
return('User not found.')
grant = authEntry.grant
deny = authEntry.deny
for i in args[1:]:
if i in deny:
deny.remove(i)
grant.add(i)
try:
chgUserPrivs(authEntry.authName, grant, deny)
except UserNotFound:
return('Changed privileges, but could not update file.')
return('''Changed privileges for nick '%s'.''' % name)
else:
return('Not enough arguments.')
if args[0] == 'deny':
if args[1:]:
if user:
try:
authEntry = userLookup(name)
except:
return('User not found.')
grant = authEntry.grant
deny = authEntry.deny
for i in args[1:]:
if i in grant:
grant.remove(i)
deny.add(i)
try:
chgUserPrivs(name, grant, deny)
except:
Raise(Exception('User was found initially, but not when updating.'))
return('Changed privilege(s) for user %s.' % name)
else:
try:
authEntry = getAuth(name)
except UserNotFound:
return('User not found.')
grant = authEntry.grant
deny = authEntry.deny
for i in args[1:]:
if i in grant:
grant.remove(i)
deny.add(i)
try:
chgUserPrivs(authEntry.authName, grant, deny)
except UserNotFound:
return('Changed privileges, but could not update file.')
return('''Changed privileges for nick '%s'.''' % name)
else:
return('Not enough arguments.')
if action == 2:
if conn.userAuth:
return('To add users in explicit auth mode, user the register command.')
else:
try:
addUser(name)
except UserAlreadyExistsError:
return('Error: User already exists')
return('Added user %s' % name)