Filter returned ad-hoc command depending on the destination jid (root, account type or account)

darcs-hash:20071024060954-86b55-bc9ebf1f1cec930caeabeafd91101de59d69e709.gz
This commit is contained in:
David Rousselie
2007-10-24 08:09:54 +02:00
parent 73bf2ff246
commit 444325996a
3 changed files with 399 additions and 324 deletions

View File

@@ -48,6 +48,10 @@ STATUS_COMPLETED = "completed"
STATUS_EXECUTING = "executing" STATUS_EXECUTING = "executing"
STATUS_CANCELED = "canceled" STATUS_CANCELED = "canceled"
root_node_re = re.compile("^[^@/]+$")
account_type_node_re = re.compile("^[^@/]+/.*$")
account_node_re = re.compile("^[^@/]+@[^/]+/.*$")
class FieldNoType(Field): class FieldNoType(Field):
def complete_xml_element(self, xmlnode, doc): def complete_xml_element(self, xmlnode, doc):
result = Field.complete_xml_element(self, xmlnode, doc) result = Field.complete_xml_element(self, xmlnode, doc)
@@ -84,12 +88,13 @@ class CommandManager(object):
command_desc = short_command_name command_desc = short_command_name
return command_desc return command_desc
def list_commands(self, jid, disco_items, lang_class): def list_commands(self, jid, to_jid, disco_items, lang_class):
"""Return DiscoItem for each supported commands""" """Return DiscoItem for each supported commands"""
for command_name in self.commands.keys(): for command_name in self.commands.keys():
must_be_admin = self.commands[command_name] (must_be_admin, to_jid_re) = self.commands[command_name]
if not must_be_admin or \ if to_jid_re.match(unicode(to_jid)) and \
(must_be_admin and self.component.is_admin(jid)): (not must_be_admin or
(must_be_admin and self.component.is_admin(jid))):
command_desc = self.get_command_desc(command_name, command_desc = self.get_command_desc(command_name,
lang_class) lang_class)
DiscoItem(disco_items, DiscoItem(disco_items,
@@ -110,7 +115,7 @@ class CommandManager(object):
def apply_command_action(self, info_query, command_name, action): def apply_command_action(self, info_query, command_name, action):
"""Apply action on command""" """Apply action on command"""
if self.commands.has_key(command_name): if self.commands.has_key(command_name):
must_be_admin = self.commands[command_name] (must_be_admin, to_jid_re) = self.commands[command_name]
if not must_be_admin or \ if not must_be_admin or \
(must_be_admin and (must_be_admin and
self.component.is_admin(info_query.get_from())): self.component.is_admin(info_query.get_from())):
@@ -265,30 +270,52 @@ class JCLCommandManager(CommandManager):
""" """
CommandManager.__init__(self, component, account_manager) CommandManager.__init__(self, component, account_manager)
self.__logger = logging.getLogger("jcl.jabber.command.JCLCommandManager") self.__logger = logging.getLogger("jcl.jabber.command.JCLCommandManager")
self.commands["http://jabber.org/protocol/admin#add-user"] = False self.commands["http://jabber.org/protocol/admin#add-user"] = \
self.commands["http://jabber.org/protocol/admin#delete-user"] = True (False, root_node_re)
self.commands["http://jabber.org/protocol/admin#disable-user"] = True self.commands["http://jabber.org/protocol/admin#delete-user"] = \
self.commands["http://jabber.org/protocol/admin#reenable-user"] = True (True, root_node_re)
self.commands["http://jabber.org/protocol/admin#end-user-session"] = True self.commands["http://jabber.org/protocol/admin#disable-user"] = \
(True, root_node_re)
self.commands["http://jabber.org/protocol/admin#reenable-user"] = \
(True, root_node_re)
self.commands["http://jabber.org/protocol/admin#end-user-session"] = \
(True, root_node_re)
#self.commands["http://jabber.org/protocol/admin#get-user-password"] = True #self.commands["http://jabber.org/protocol/admin#get-user-password"] = True
#self.commands["http://jabber.org/protocol/admin#change-user-password"] = True #self.commands["http://jabber.org/protocol/admin#change-user-password"] = True
self.commands["http://jabber.org/protocol/admin#get-user-roster"] = True self.commands["http://jabber.org/protocol/admin#get-user-roster"] = \
self.commands["http://jabber.org/protocol/admin#get-user-lastlogin"] = True (True, root_node_re)
self.commands["http://jabber.org/protocol/admin#get-registered-users-num"] = True self.commands["http://jabber.org/protocol/admin#get-user-lastlogin"] = \
self.commands["http://jabber.org/protocol/admin#get-disabled-users-num"] = True (True, root_node_re)
self.commands["http://jabber.org/protocol/admin#get-online-users-num"] = True self.commands["http://jabber.org/protocol/admin#get-registered-users-num"] = \
self.commands["http://jabber.org/protocol/admin#get-registered-users-list"] = True (True, root_node_re)
self.commands["http://jabber.org/protocol/admin#get-disabled-users-list"] = True self.commands["http://jabber.org/protocol/admin#get-disabled-users-num"] = \
self.commands["http://jabber.org/protocol/admin#get-online-users-list"] = True (True, root_node_re)
self.commands["http://jabber.org/protocol/admin#announce"] = True self.commands["http://jabber.org/protocol/admin#get-online-users-num"] = \
self.commands["http://jabber.org/protocol/admin#set-motd"] = True (True, root_node_re)
self.commands["http://jabber.org/protocol/admin#edit-motd"] = True self.commands["http://jabber.org/protocol/admin#get-registered-users-list"] = \
self.commands["http://jabber.org/protocol/admin#delete-motd"] = True (True, root_node_re)
self.commands["http://jabber.org/protocol/admin#set-welcome"] = True self.commands["http://jabber.org/protocol/admin#get-disabled-users-list"] = \
self.commands["http://jabber.org/protocol/admin#delete-welcome"] = True (True, root_node_re)
self.commands["http://jabber.org/protocol/admin#edit-admin"] = True self.commands["http://jabber.org/protocol/admin#get-online-users-list"] = \
self.commands["http://jabber.org/protocol/admin#restart"] = True (True, root_node_re)
self.commands["http://jabber.org/protocol/admin#shutdown"] = True self.commands["http://jabber.org/protocol/admin#announce"] = \
(True, root_node_re)
self.commands["http://jabber.org/protocol/admin#set-motd"] = \
(True, root_node_re)
self.commands["http://jabber.org/protocol/admin#edit-motd"] = \
(True, root_node_re)
self.commands["http://jabber.org/protocol/admin#delete-motd"] = \
(True, root_node_re)
self.commands["http://jabber.org/protocol/admin#set-welcome"] = \
(True, root_node_re)
self.commands["http://jabber.org/protocol/admin#delete-welcome"] = \
(True, root_node_re)
self.commands["http://jabber.org/protocol/admin#edit-admin"] = \
(True, root_node_re)
self.commands["http://jabber.org/protocol/admin#restart"] = \
(True, root_node_re)
self.commands["http://jabber.org/protocol/admin#shutdown"] = \
(True, root_node_re)
def get_name_and_jid(self, mixed_name_and_jid): def get_name_and_jid(self, mixed_name_and_jid):
return mixed_name_and_jid.split("/", 1)[:2] return mixed_name_and_jid.split("/", 1)[:2]
@@ -1226,6 +1253,7 @@ class CommandDiscoGetItemsHandler(DiscoHandler):
if not disco_obj: if not disco_obj:
disco_obj = DiscoItems() disco_obj = DiscoItems()
return [command_manager.list_commands(jid=info_query.get_from(), return [command_manager.list_commands(jid=info_query.get_from(),
to_jid=info_query.get_to(),
disco_items=disco_obj, disco_items=disco_obj,
lang_class=lang_class)] lang_class=lang_class)]

File diff suppressed because it is too large Load Diff

View File

@@ -45,6 +45,7 @@ import jcl.model as model
import jcl.model.account as account import jcl.model.account as account
from jcl.model.account import Account, LegacyJID, User from jcl.model.account import Account, LegacyJID, User
from jcl.lang import Lang from jcl.lang import Lang
import jcl.jabber.command as command
from jcl.model.tests.account import ExampleAccount, Example2Account from jcl.model.tests.account import ExampleAccount, Example2Account
from jcl.tests import JCLTestCase from jcl.tests import JCLTestCase
@@ -757,7 +758,7 @@ class JCLComponent_TestCase(JCLTestCase):
disco_items = self.comp.disco_get_items("Example2/account1", info_query) disco_items = self.comp.disco_get_items("Example2/account1", info_query)
self.assertEquals(disco_items, None) self.assertEquals(disco_items, None)
def test_disco_get_items_list_commands(self): def test_disco_root_get_items_list_commands(self):
self.comp.stream = MockStream() self.comp.stream = MockStream()
self.comp.stream_class = MockStream self.comp.stream_class = MockStream
config_file = tempfile.mktemp(".conf", "jcltest", jcl.tests.DB_DIR) config_file = tempfile.mktemp(".conf", "jcltest", jcl.tests.DB_DIR)
@@ -765,6 +766,10 @@ class JCLComponent_TestCase(JCLTestCase):
self.comp.config_file = config_file self.comp.config_file = config_file
self.comp.config.read(config_file) self.comp.config.read(config_file)
self.comp.set_admins(["admin@test.com"]) self.comp.set_admins(["admin@test.com"])
command.command_manager.commands["accounttype_command"] = \
(True, command.account_type_node_re)
command.command_manager.commands["account_command"] = \
(True, command.account_node_re)
info_query = Iq(stanza_type="get", info_query = Iq(stanza_type="get",
from_jid="admin@test.com", from_jid="admin@test.com",
to_jid="jcl.test.com") to_jid="jcl.test.com")