Commands work.

This commit is contained in:
Tomasz Melcer 2006-07-17 17:52:37 +00:00
parent de23ba2a15
commit 5740d226e6
2 changed files with 38 additions and 27 deletions

View File

@ -22,7 +22,12 @@ class AdHocCommand:
commandfeatures = (xmpp.NS_DATA,) commandfeatures = (xmpp.NS_DATA,)
@staticmethod @staticmethod
def isVisibleFor(jid): return True def isVisibleFor(samejid):
''' This returns True if that command should be visible and invokable
for others.
samejid - True when command is invoked by an entity with the same bare jid.
'''
return True
def __init__(self, conn, jid, sessionid): def __init__(self, conn, jid, sessionid):
self.connection = conn self.connection = conn
@ -48,15 +53,23 @@ class AdHocCommand:
cmd.addChild('actions', attrs, actions) cmd.addChild('actions', attrs, actions)
return response, cmd return response, cmd
def badRequest(self):
self.connection.connection.send(xmpp.Error(xmpp.NS_STANZAS+' bad-request'))
def cancel(self, request): def cancel(self, request):
response, cmd = self.buildResponse(request, status='canceled') response, cmd = self.buildResponse(request, status='canceled')
self.connection.send(response) self.connection.connection.send(response)
return False # finish the session return False # finish the session
class ChangeStatusCommand(AdHocCommand): class ChangeStatusCommand(AdHocCommand):
commandnode = 'change-status' commandnode = 'change-status'
commandname = 'Change status information' commandname = 'Change status information'
@staticmethod
def isVisibleFor(samejid):
''' Change status is visible only if the entity has the same bare jid. '''
return samejid
def execute(self, request): def execute(self, request):
# first query... # first query...
response, cmd = self.buildResponse(request, defaultaction='execute', actions=['execute']) response, cmd = self.buildResponse(request, defaultaction='execute', actions=['execute'])
@ -79,7 +92,7 @@ class ChangeStatusCommand(AdHocCommand):
dataforms.DataField('text-multi', 'presence-desc', dataforms.DataField('text-multi', 'presence-desc',
label='Presence description:')])) label='Presence description:')]))
self.connection.send(response) self.connection.connection.send(response)
# for next invocation # for next invocation
self.execute = self.changestatus self.execute = self.changestatus
@ -91,16 +104,17 @@ class ChangeStatusCommand(AdHocCommand):
try: try:
form=dataforms.DataForm(node=request.getTag('command').getTag('x')) form=dataforms.DataForm(node=request.getTag('command').getTag('x'))
except TypeError: except TypeError:
self.badRequest()
return False return False
try: try:
presencetype = form['presence-type'] presencetype = form['presence-type']
if not presencetype in ('free-for-chat', 'online', 'away', 'xa', 'dnd', 'offline'): if not presencetype in ('free-for-chat', 'online', 'away', 'xa', 'dnd', 'offline'):
#raise BadSomething self.badRequest()
return return False
except KeyError: except KeyError:
# raise BadSomething self.badRequest()
return return False
try: try:
presencedesc = form['presence-desc'] presencedesc = form['presence-desc']
@ -110,16 +124,10 @@ class ChangeStatusCommand(AdHocCommand):
response, cmd = self.buildResponse(request, status='completed') response, cmd = self.buildResponse(request, status='completed')
cmd.addChild('note', {}, 'The status has been changed.') cmd.addChild('note', {}, 'The status has been changed.')
self.connection.send(response) self.connection.connection.send(response)
# looking for account name... # send new status
accname = None gajim.interface.roster.send_status(self.connection.name, presencetype, presencedesc)
for acc in gajim.connections.iterkeys():
if self.connection is gajim.connections[acc]:
accname=acc
assert accname is not None
gajim.interface.roster.send_status(accname, presencetype, presencedesc)
return False # finish the session return False # finish the session
@ -134,16 +142,23 @@ class ConnectionCommands:
# a list of sessions; keys are tuples (jid, sessionid, node) # a list of sessions; keys are tuples (jid, sessionid, node)
self.__sessions = {} self.__sessions = {}
def getOurBareJID(self):
return gajim.get_jid_from_account(self.name)
def isSameJID(self, jid):
''' Tests if the bare jid given is the same as our bare jid. '''
return xmpp.JID(jid).getStripped() == self.getOurBareJID()
def commandListQuery(self, con, iq_obj): def commandListQuery(self, con, iq_obj):
iq = iq_obj.buildReply('result') iq = iq_obj.buildReply('result')
jid = helpers.get_full_jid_from_iq(iq_obj) jid = helpers.get_full_jid_from_iq(iq_obj)
q = iq.getTag('query') q = iq.getTag('query')
for node, cmd in self.__commands.iteritems(): for node, cmd in self.__commands.iteritems():
if cmd.isVisibleFor(jid): if cmd.isVisibleFor(self.isSameJID(jid)):
q.addChild('item', { q.addChild('item', {
# TODO: find the jid # TODO: find the jid
'jid': 'our-jid', 'jid': self.getOurBareJID()+u'/'+self.server_resource,
'node': node, 'node': node,
'name': cmd.commandname}) 'name': cmd.commandname})
@ -158,7 +173,7 @@ class ConnectionCommands:
if node not in self.__commands: return False if node not in self.__commands: return False
cmd = self.__commands[node] cmd = self.__commands[node]
if cmd.isVisibleFor(jid): if cmd.isVisibleFor(self.isSameJID(jid)):
iq = iq_obj.buildReply('result') iq = iq_obj.buildReply('result')
q = iq.getTag('query') q = iq.getTag('query')
q.addChild('identity', attrs = {'type': 'command-node', q.addChild('identity', attrs = {'type': 'command-node',
@ -186,18 +201,18 @@ class ConnectionCommands:
if sessionid is None: if sessionid is None:
# we start a new command session... only if we are visible for the jid # we start a new command session... only if we are visible for the jid
newcmd = self.__commands[node] newcmd = self.__commands[node]
if not newcmd.isVisibleFor(jid): if not newcmd.isVisibleFor(self.isSameJID(jid)):
return return
# generate new sessionid # generate new sessionid
sessionid = self.connection.getAnID() sessionid = self.connection.getAnID()
# create new instance and run it # create new instance and run it
obj = newcmd(conn=self.connection, jid=jid, sessionid=sessionid) obj = newcmd(conn=self, jid=jid, sessionid=sessionid)
rc = obj.execute(iq_obj) rc = obj.execute(iq_obj)
if rc: if rc:
self.__sessions[(jid, sessionid, node)] = obj self.__sessions[(jid, sessionid, node)] = obj
raise NodeProcessed raise xmpp.NodeProcessed
else: else:
# the command is already running, check for it # the command is already running, check for it
magictuple = (jid, sessionid, node) magictuple = (jid, sessionid, node)
@ -216,8 +231,7 @@ class ConnectionCommands:
elif action == 'complete': rc = obj.complete(iq_obj) elif action == 'complete': rc = obj.complete(iq_obj)
else: else:
# action is wrong. stop the session, send error # action is wrong. stop the session, send error
del self.__sessions[magictuple] raise AttributeError
return
except AttributeError: except AttributeError:
# the command probably doesn't handle invoked action... # the command probably doesn't handle invoked action...
# stop the session, return error # stop the session, return error

View File

@ -310,11 +310,8 @@ class DataFormWidget(gtk.Alignment, object):
model[path][0]=newtext model[path][0]=newtext
values = field.value values = field.value
print values
values[values.index(old)]=newtext values[values.index(old)]=newtext
field.value = values field.value = values
print values
print field.value
def on_jid_multi_add_button_clicked(self, widget, treeview, model, field): def on_jid_multi_add_button_clicked(self, widget, treeview, model, field):
iter = model.insert(999999, ("new@jabber.id",)) iter = model.insert(999999, ("new@jabber.id",))