-
Notifications
You must be signed in to change notification settings - Fork 1
/
jep0133.py
332 lines (294 loc) · 17 KB
/
jep0133.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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# Service administration commands Jep-0133 for the xmpppy based transports written by Mike Albon
import xmpp, string
from xmpp.protocol import *
import xmpp.commands
import config
from xml.dom.minidom import parse
"""This file is the JEP-0133 commands that are applicable to the transports.
Implemented commands as follows:
4.18. Registered_Users_Command: Return a list of Registered Users
4.20. Online_Users_Command: Return a list of Online Users
4.21. Active_Users_Command: Return a list of Active Users
4.29. Edit_Admin_List_Command: Edit the Administrators list
4.30. Restart_Service_Command: Restarts the Service
4.31. Shutdown_Service_Command: Shuts down the Service
"""
class Online_Users_Command(xmpp.commands.Command_Handler_Prototype):
"""This is the online users command as documented in section 4.20 of JEP-0133.
At the current time, no provision is made for splitting the userlist into sections"""
name = NS_ADMIN_ONLINE_USERS_LIST
description = 'Get List of Online Users'
discofeatures = [xmpp.commands.NS_COMMANDS,xmpp.NS_DATA]
def __init__(self,users,jid=''):
"""Initialise the command object"""
xmpp.commands.Command_Handler_Prototype.__init__(self,jid)
self.initial = { 'execute':self.cmdFirstStage }
self.users = users
def _DiscoHandler(self,conn,request,type):
"""The handler for discovery events"""
if request.getFrom().getStripped() in config.admins:
return xmpp.commands.Command_Handler_Prototype._DiscoHandler(self,conn,request,type)
else:
return None
def cmdFirstStage(self,conn,request):
"""Build the reply to complete the request"""
if request.getFrom().getStripped() in config.admins:
reply = request.buildReply('result')
form = DataForm(typ='result',data=[DataField(typ='hidden',name='FORM_TYPE',value=NS_ADMIN),DataField(desc='The list of online users',name='onlineuserjids',value=self.users.keys(),typ='jid-multi')])
reply.addChild(name='command',namespace=NS_COMMANDS,attrs={'node':request.getTagAttr('command','node'),'sessionid':self.getSessionID(),'status':'completed'},payload=[form])
self._owner.send(reply)
else:
self._owner.send(Error(request,ERR_FORBIDDEN))
raise NodeProcessed
class Active_Users_Command(xmpp.commands.Command_Handler_Prototype):
"""This is the active users command as documented in section 4.21 of JEP-0133.
At the current time, no provision is made for splitting the userlist into sections"""
name = NS_ADMIN_ACTIVE_USERS_LIST
description = 'Get List of Active Users'
discofeatures = [xmpp.commands.NS_COMMANDS,xmpp.NS_DATA]
def __init__(self,users,jid=''):
"""Initialise the command object"""
xmpp.commands.Command_Handler_Prototype.__init__(self,jid)
self.initial = { 'execute':self.cmdFirstStage }
self.users = users
def _DiscoHandler(self,conn,request,type):
"""The handler for discovery events"""
if request.getFrom().getStripped() in config.admins:
return xmpp.commands.Command_Handler_Prototype._DiscoHandler(self,conn,request,type)
else:
return None
def cmdFirstStage(self,conn,request):
"""Build the reply to complete the request"""
if request.getFrom().getStripped() in config.admins:
reply = request.buildReply('result')
form = DataForm(typ='result',data=[DataField(typ='hidden',name='FORM_TYPE',value=NS_ADMIN),DataField(desc='The list of active users',name='activeuserjids',value=self.users.keys(),typ='jid-multi')])
reply.addChild(name='command',namespace=NS_COMMANDS,attrs={'node':request.getTagAttr('command','node'),'sessionid':self.getSessionID(),'status':'completed'},payload=[form])
self._owner.send(reply)
else:
self._owner.send(Error(request,ERR_FORBIDDEN))
raise NodeProcessed
class Registered_Users_Command(xmpp.commands.Command_Handler_Prototype):
"""This is the active users command as documented in section 4.18 of JEP-0133.
At the current time, no provision is made for splitting the userlist into sections"""
name = NS_ADMIN_REGISTERED_USERS_LIST
description = 'Get List of Registered Users'
discofeatures = [xmpp.commands.NS_COMMANDS,xmpp.NS_DATA]
def __init__(self,userfile,jid=''):
"""Initialise the command object"""
xmpp.commands.Command_Handler_Prototype.__init__(self,jid)
self.initial = { 'execute':self.cmdFirstStage }
self.userfile = userfile
def _DiscoHandler(self,conn,request,type):
"""The handler for discovery events"""
if request.getFrom().getStripped() in config.admins:
return xmpp.commands.Command_Handler_Prototype._DiscoHandler(self,conn,request,type)
else:
return None
def cmdFirstStage(self,conn,request):
"""Build the reply to complete the request"""
if request.getFrom().getStripped() in config.admins:
reply = request.buildReply('result')
form = DataForm(typ='result',data=[DataField(typ='hidden',name='FORM_TYPE',value=NS_ADMIN),DataField(desc='The list of registered users',name='registereduserjids',value=self.userfile.keys(),typ='jid-multi')])
reply.addChild(name='command',namespace=NS_COMMANDS,attrs={'node':request.getTagAttr('command','node'),'sessionid':self.getSessionID(),'status':'completed'},payload=[form])
self._owner.send(reply)
else:
self._owner.send(Error(request,ERR_FORBIDDEN))
raise NodeProcessed
class Edit_Admin_List_Command(xmpp.commands.Command_Handler_Prototype):
"""This command enables the editing of the administrators list as documented in section 4.29 of JEP-0133.
(the users of JEP-0133 commands in this case)"""
name = NS_ADMIN_EDIT_ADMIN
description = 'Edit Admin List'
discofeatures = [xmpp.commands.NS_COMMANDS, xmpp.NS_DATA]
def __init__(self,jid=''):
"""Initialise the command object"""
xmpp.commands.Command_Handler_Prototype.__init__(self,jid)
self.initial = {'execute':self.cmdFirstStage }
def _DiscoHandler(self,conn,request,type):
"""The handler for discovery events"""
if request.getFrom().getStripped() in config.admins:
return xmpp.commands.Command_Handler_Prototype._DiscoHandler(self,conn,request,type)
else:
return None
def cmdFirstStage(self,conn,request):
"""Set the session ID, and return the form containing the current administrators"""
if request.getFrom().getStripped() in config.admins:
# Setup session ready for form reply
session = self.getSessionID()
self.sessions[session] = {'jid':request.getFrom(),'actions':{'cancel':self.cmdCancel,'next':self.cmdSecondStage,'execute':self.cmdSecondStage}}
# Setup form with existing data in
reply = request.buildReply('result')
form = DataForm(title='Editing the Admin List',data=['Fill out this form to edit the list of entities who have administrative privileges', DataField(typ='hidden',name='FORM_TYPE',value=NS_ADMIN),DataField(desc='The Admin List', typ='jid-multi', name='adminjids',value=config.admins)])
replypayload = [Node('actions',attrs={'execute':'next'},payload=[Node('next')]),form]
reply.addChild(name='command',namespace=NS_COMMANDS,attrs={'node':request.getTagAttr('command','node'),'sessionid':session,'status':'executing'},payload=replypayload)
self._owner.send(reply)
else:
self._owner.send(Error(request,ERR_FORBIDDEN))
raise NodeProcessed
def cmdSecondStage(self,conn,request):
"""Apply and save the config"""
form = DataForm(node=request.getTag(name='command').getTag(name='x',namespace=NS_DATA))
session = request.getTagAttr('command','sessionid')
if self.sessions.has_key(session):
if self.sessions[session]['jid'] == request.getFrom():
config.admins = form.getField('adminjids').getValues()
if len(config.admins) == 1 and len(config.admins[0]) == 0:
config.admins = []
doc = parse(config.configFile)
admins = doc.getElementsByTagName('admins')[0]
for el in [x for x in admins.childNodes]:
admins.removeChild(el)
el.unlink()
for admin in config.admins:
txt = doc.createTextNode('\n ')
admins.appendChild(txt)
txt = doc.createTextNode(admin)
el = doc.createElement('jid')
el.appendChild(txt)
admins.appendChild(el)
txt = doc.createTextNode('\n ')
admins.appendChild(txt)
attrs={'node':request.getTagAttr('command','node'),'sessionid':session,'status':'completed'}
payload=[]
try:
f = open(config.configFile,'w')
doc.writexml(f)
f.close()
except IOError, (errno, strerror):
# attrs['status'] = 'canceled' # Psi doesn't display the form if we cancel the command
form = DataForm(typ='result',data=[DataField(value="I/O error(%s): %s" % (errno, strerror),typ='fixed')])
payload.append(form)
doc.unlink()
reply = request.buildReply('result')
reply.addChild(name='command',namespace=NS_COMMANDS,attrs=attrs,payload=payload)
self._owner.send(reply)
else:
self._owner.send(Error(request,ERR_BAD_REQUEST))
else:
self._owner.send(Error(request,ERR_BAD_REQUEST))
raise NodeProcessed
def cmdCancel(self,conn,request):
session = request.getTagAttr('command','sessionid')
if self.sessions.has_key(session):
del self.sessions[session]
reply = request.buildReply('result')
reply.addChild(name='command',namespace=NS_COMMANDS,attrs={'node':request.getTagAttr('command','node'),'sessionid':session,'status':'canceled'})
self._owner.send(reply)
else:
self._owner.send(Error(request,ERR_BAD_REQUEST))
raise NodeProcessed
class Restart_Service_Command(xmpp.commands.Command_Handler_Prototype):
"""This is the restart service command as documented in section 4.30 of JEP-0133."""
name = NS_ADMIN_RESTART
description = 'Restart Service'
discofeatures = [xmpp.commands.NS_COMMANDS, xmpp.NS_DATA]
def __init__(self,transport,jid=''):
"""Initialise the command object"""
xmpp.commands.Command_Handler_Prototype.__init__(self,jid)
self.initial = {'execute':self.cmdFirstStage }
self.transport = transport
def _DiscoHandler(self,conn,request,type):
"""The handler for discovery events"""
if request.getFrom().getStripped() in config.admins:
return xmpp.commands.Command_Handler_Prototype._DiscoHandler(self,conn,request,type)
else:
return None
def cmdFirstStage(self,conn,request):
"""Set the session ID, and return the form containing the restart reason"""
if request.getFrom().getStripped() in config.admins:
# Setup session ready for form reply
session = self.getSessionID()
self.sessions[session] = {'jid':request.getFrom(),'actions':{'cancel':self.cmdCancel,'next':self.cmdSecondStage,'execute':self.cmdSecondStage}}
# Setup form with existing data in
reply = request.buildReply('result')
form = DataForm(title='Restarting the Service',data=['Fill out this form to restart the service', DataField(typ='hidden',name='FORM_TYPE',value=NS_ADMIN),DataField(desc='Announcement', typ='text-multi', name='announcement')])
replypayload = [Node('actions',attrs={'execute':'next'},payload=[Node('next')]),form]
reply.addChild(name='command',namespace=NS_COMMANDS,attrs={'node':request.getTagAttr('command','node'),'sessionid':session,'status':'executing'},payload=replypayload)
self._owner.send(reply)
else:
self._owner.send(Error(request,ERR_FORBIDDEN))
raise NodeProcessed
def cmdSecondStage(self,conn,request):
"""Apply and save the config"""
form = DataForm(node=request.getTag(name='command').getTag(name='x',namespace=NS_DATA))
session = request.getTagAttr('command','sessionid')
if self.sessions.has_key(session):
if self.sessions[session]['jid'] == request.getFrom():
self.transport.offlinemsg = '\n'.join(form.getField('announcement').getValues())
self.transport.restart = 1
self.transport.online = 0
reply = request.buildReply('result')
reply.addChild(name='command',namespace=NS_COMMANDS,attrs={'node':request.getTagAttr('command','node'),'sessionid':session,'status':'completed'})
self._owner.send(reply)
else:
self._owner.send(Error(request,ERR_BAD_REQUEST))
else:
self._owner.send(Error(request,ERR_BAD_REQUEST))
raise NodeProcessed
def cmdCancel(self,conn,request):
session = request.getTagAttr('command','sessionid')
if self.sessions.has_key(session):
del self.sessions[session]
reply = request.buildReply('result')
reply.addChild(name='command',namespace=NS_COMMANDS,attrs={'node':request.getTagAttr('command','node'),'sessionid':session,'status':'canceled'})
self._owner.send(reply)
else:
self._owner.send(Error(request,ERR_BAD_REQUEST))
raise NodeProcessed
class Shutdown_Service_Command(xmpp.commands.Command_Handler_Prototype):
"""This is the shutdown service command as documented in section 4.31 of JEP-0133."""
name = NS_ADMIN_SHUTDOWN
description = 'Shut Down Service'
discofeatures = [xmpp.commands.NS_COMMANDS, xmpp.NS_DATA]
def __init__(self,transport,jid=''):
"""Initialise the command object"""
xmpp.commands.Command_Handler_Prototype.__init__(self,jid)
self.initial = {'execute':self.cmdFirstStage }
self.transport = transport
def _DiscoHandler(self,conn,request,type):
"""The handler for discovery events"""
if request.getFrom().getStripped() in config.admins:
return xmpp.commands.Command_Handler_Prototype._DiscoHandler(self,conn,request,type)
else:
return None
def cmdFirstStage(self,conn,request):
"""Set the session ID, and return the form containing the shutdown reason"""
if request.getFrom().getStripped() in config.admins:
# Setup session ready for form reply
session = self.getSessionID()
self.sessions[session] = {'jid':request.getFrom(),'actions':{'cancel':self.cmdCancel,'next':self.cmdSecondStage,'execute':self.cmdSecondStage}}
# Setup form with existing data in
reply = request.buildReply('result')
form = DataForm(title='Shutting Down the Service',data=['Fill out this form to shut down the service', DataField(typ='hidden',name='FORM_TYPE',value=NS_ADMIN),DataField(desc='Announcement', typ='text-multi', name='announcement')])
replypayload = [Node('actions',attrs={'execute':'next'},payload=[Node('next')]),form]
reply.addChild(name='command',namespace=NS_COMMANDS,attrs={'node':request.getTagAttr('command','node'),'sessionid':session,'status':'executing'},payload=replypayload)
self._owner.send(reply)
else:
self._owner.send(Error(request,ERR_FORBIDDEN))
raise NodeProcessed
def cmdSecondStage(self,conn,request):
"""Apply and save the config"""
form = DataForm(node=request.getTag(name='command').getTag(name='x',namespace=NS_DATA))
session = request.getTagAttr('command','sessionid')
if self.sessions.has_key(session):
if self.sessions[session]['jid'] == request.getFrom():
self.transport.offlinemsg = '\n'.join(form.getField('announcement').getValues())
self.transport.online = 0
reply = request.buildReply('result')
reply.addChild(name='command',namespace=NS_COMMANDS,attrs={'node':request.getTagAttr('command','node'),'sessionid':session,'status':'completed'})
self._owner.send(reply)
else:
self._owner.send(Error(request,ERR_BAD_REQUEST))
else:
self._owner.send(Error(request,ERR_BAD_REQUEST))
raise NodeProcessed
def cmdCancel(self,conn,request):
session = request.getTagAttr('command','sessionid')
if self.sessions.has_key(session):
del self.sessions[session]
reply = request.buildReply('result')
reply.addChild(name='command',namespace=NS_COMMANDS,attrs={'node':request.getTagAttr('command','node'),'sessionid':session,'status':'canceled'})
self._owner.send(reply)
else:
self._owner.send(Error(request,ERR_BAD_REQUEST))
raise NodeProcessed