-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadhoc.py
151 lines (134 loc) · 7.61 KB
/
adhoc.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
# $Id: adhoc.py,v 1.1 2008-01-08 04:23:52 norman Exp $
import sys, xmpp
from xmpp.protocol import *
import config
from xep0133 import *
from myspace_helpers import MS_msg_action_zap_mappings, MS_msg_action_zap_prefix
class AdHocCommands:
def __init__(self, userfile):
self.userfile = userfile
def PlugIn(self, transport):
self.commands = xmpp.commands.Commands(transport.disco)
self.commands.PlugIn(transport.jabber)
# jep-0133 commands:
transport.cmdonlineusers = Online_Users_Command(transport.userlist,jid=config.jid)
transport.cmdonlineusers.plugin(self.commands)
transport.cmdactiveusers = Active_Users_Command(transport.userlist,jid=config.jid)
transport.cmdactiveusers.plugin(self.commands)
transport.cmdregisteredusers = Registered_Users_Command(self.userfile,jid=config.jid)
transport.cmdregisteredusers.plugin(self.commands)
transport.cmdeditadminusers = Edit_Admin_List_Command(jid=config.jid)
transport.cmdeditadminusers.plugin(self.commands)
transport.cmdrestartservice = Restart_Service_Command(transport,jid=config.jid)
transport.cmdrestartservice.plugin(self.commands)
transport.cmdshutdownservice = Shutdown_Service_Command(transport,jid=config.jid)
transport.cmdshutdownservice.plugin(self.commands)
# transport wide commands:
transport.cmdconnectusers = Connect_Registered_Users_Command(self.userfile)
transport.cmdconnectusers.plugin(self.commands)
# per contact commands:
transport.cmdzapcontact = ZAP_Contact_Command(transport)
transport.cmdzapcontact.plugin(self.commands)
class Connect_Registered_Users_Command(xmpp.commands.Command_Handler_Prototype):
"""This is the register users command"""
name = "connect-users"
description = 'Connect all registered users'
discofeatures = [xmpp.commands.NS_COMMANDS]
def __init__(self,userfile):
"""Initialise the command object"""
xmpp.commands.Command_Handler_Prototype.__init__(self,config.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:
for each in self.userfile.keys():
conn.send(Presence(to=each, frm = config.jid, typ = 'probe'))
if self.userfile[each].has_key('servers'):
for server in self.userfile[each]['servers']:
conn.send(Presence(to=each, frm = '%s@%s'%(server,config.jid), typ = 'probe'))
reply = request.buildReply('result')
form = DataForm(typ='result',data=[DataField(value='Command completed.',typ='fixed')])
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 ZAP_Contact_Command(xmpp.commands.Command_Handler_Prototype):
"""This is the ZAP contact command"""
name = "zap-contact"
description = 'Send a zap'
discofeatures = [xmpp.commands.NS_COMMANDS]
zaplist = [x[0] for x in MS_msg_action_zap_mappings]
zapindexes = {}
for i in range(len(zaplist)):
zapindexes[zaplist[i]] = i
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,event,type):
"""The handler for discovery events"""
fromstripped = event.getFrom().getStripped().encode('utf8')
if self.transport.userlist.has_key(fromstripped):
return xmpp.commands.Command_Handler_Prototype._DiscoHandler(self,conn,event,type)
else:
return None
def cmdFirstStage(self,conn,request):
"""Set the session ID, and return the form containing the zap options"""
fromstripped = request.getFrom().getStripped().encode('utf8')
if self.transport.userlist.has_key(fromstripped):
# 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='Zap',data=['Pick a zap', DataField(desc='Zap List', typ='list-single', name='zap',options=self.zaplist)])
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():
fromstripped = request.getFrom().getStripped().encode('utf8')
zap = form.getField('zap').getValue()
if self.transport.userlist.has_key(fromstripped) and self.zapindexes.has_key(zap):
msid = request.getTo().getNode()
msidenc = msid.encode('utf-8')
msg = MS_msg_action_zap_prefix + str(self.zapindexes[zap])
msobj = self.transport.userlist[fromstripped]
msobj.msmsg_send_action(msidenc, msg)
reply = request.buildReply('result')
form = DataForm(typ='result',data=[DataField(value='Command completed.',typ='fixed')])
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_BAD_REQUEST))
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