'edit-motd' ad-hoc command implementation

darcs-hash:20070904063308-86b55-882c836f7f86205a278f03c62b629cd4cecfed5c.gz
This commit is contained in:
David Rousselie
2007-09-04 08:33:08 +02:00
parent c79b80c22d
commit e797b6f637
5 changed files with 189 additions and 29 deletions

View File

@@ -778,7 +778,7 @@ class JCLCommandManager(CommandManager):
return (None, result) return (None, result)
def execute_set_motd_1(self, info_query, session_context, def execute_set_motd_1(self, info_query, session_context,
command_node, lang_class): command_node, lang_class, motd=""):
self.add_actions(command_node, [ACTION_NEXT]) self.add_actions(command_node, [ACTION_NEXT])
result_form = Form(xmlnode_or_type="result", result_form = Form(xmlnode_or_type="result",
title="TODO", title="TODO",
@@ -789,6 +789,7 @@ class JCLCommandManager(CommandManager):
result_form.add_field(name="motd", result_form.add_field(name="motd",
field_type="text-multi", field_type="text-multi",
label="TODO", label="TODO",
value=[motd],
required=True) required=True)
result_form.as_xml(command_node) result_form.as_xml(command_node)
return (result_form, []) return (result_form, [])
@@ -803,6 +804,8 @@ class JCLCommandManager(CommandManager):
Account.q._status != account.OFFLINE), Account.q._status != account.OFFLINE),
distinct=True) distinct=True)
result = [] result = []
motd = self.component.get_motd()
if motd is not None:
for user in users: for user in users:
for _account in user.accounts: for _account in user.accounts:
if _account.status == account.OFFLINE: if _account.status == account.OFFLINE:
@@ -810,17 +813,19 @@ class JCLCommandManager(CommandManager):
else: else:
user.has_received_motd = True user.has_received_motd = True
if user.has_received_motd: if user.has_received_motd:
result.extend(self.component.get_motd(user.jid)) result.append(Message(from_jid=self.component.jid,
to_jid=user.jid,
body=motd))
command_node.setProp("status", STATUS_COMPLETED) command_node.setProp("status", STATUS_COMPLETED)
return (None, result) return (None, result)
def execute_edit_motd_1(self, info_query, session_context, def execute_edit_motd_1(self, info_query, session_context,
command_node, lang_class): command_node, lang_class):
return [] return self.execute_set_motd_1(info_query, session_context,
command_node, lang_class,
self.component.get_motd())
def execute_edit_motd_2(self, info_query, session_context, execute_edit_motd_2 = execute_set_motd_2
command_node, lang_class):
return []
def execute_delete_motd_1(self, info_query, session_context, def execute_delete_motd_1(self, info_query, session_context,
command_node, lang_class): command_node, lang_class):

View File

@@ -557,15 +557,13 @@ class JCLComponent(Component, object):
% (exception, "".join(traceback.format_exception % (exception, "".join(traceback.format_exception
(type, value, stack, 5)))) (type, value, stack, 5))))
def get_motd(self, to_jid): def get_motd(self):
if self.config is not None \ if self.config is not None \
and self.config.has_option("component", "motd"): and self.config.has_option("component", "motd"):
motd = self.config.get("component", "motd") motd = self.config.get("component", "motd")
return [Message(from_jid=self.jid, return motd
to_jid=to_jid,
body=motd)]
else: else:
return [] return None
def set_motd(self, motd): def set_motd(self, motd):
if not self.config.has_section("component"): if not self.config.has_section("component"):
@@ -573,11 +571,15 @@ class JCLComponent(Component, object):
self.config.set("component", "motd", motd) self.config.set("component", "motd", motd)
configFile = open(self.config_file, "w") configFile = open(self.config_file, "w")
self.config.write(configFile) self.config.write(configFile)
configFile.close()
def del_motd(self): def del_motd(self):
if self.config.has_section("component") \ if self.config.has_section("component") \
and self.config.has_option("component", "motd"): and self.config.has_option("component", "motd"):
self.config.remove_option("component", "motd") self.config.remove_option("component", "motd")
configFile = open(self.config_file, "w")
self.config.write(configFile)
configFile.close()
########################################################################### ###########################################################################
# Virtual methods # Virtual methods

View File

@@ -22,6 +22,7 @@
## ##
from pyxmpp.presence import Presence from pyxmpp.presence import Presence
from pyxmpp.message import Message
from jcl.jabber import Handler from jcl.jabber import Handler
from jcl.model import account from jcl.model import account
@@ -114,7 +115,11 @@ class RootPresenceAvailableHandler(RootPresenceHandler, AccountPresenceAvailable
user = account.get_user(unicode(from_jid.bare())) user = account.get_user(unicode(from_jid.bare()))
if not user.has_received_motd: if not user.has_received_motd:
user.has_received_motd = True user.has_received_motd = True
result.extend(self.component.get_motd(from_jid)) motd = self.component.get_motd()
if motd is not None:
result.append(Message(from_jid=self.component.jid,
to_jid=from_jid,
body=motd))
return result return result

View File

@@ -30,6 +30,7 @@ from pyxmpp.jabber.dataforms import Form
from pyxmpp.iq import Iq from pyxmpp.iq import Iq
from pyxmpp.message import Message from pyxmpp.message import Message
import jcl.tests
from jcl.lang import Lang from jcl.lang import Lang
from jcl.jabber.component import JCLComponent from jcl.jabber.component import JCLComponent
import jcl.jabber.command as command import jcl.jabber.command as command
@@ -63,7 +64,7 @@ class JCLCommandManager_TestCase(JCLTestCase):
JCLTestCase.setUp(self, tables=[Account, ExampleAccount, JCLTestCase.setUp(self, tables=[Account, ExampleAccount,
Example2Account, LegacyJID, Example2Account, LegacyJID,
User]) User])
self.config_file = tempfile.mktemp(".conf", "jcltest", "/tmp") self.config_file = tempfile.mktemp(".conf", "jcltest", jcl.tests.DB_DIR)
self.config = ConfigParser() self.config = ConfigParser()
self.config.read(self.config_file) self.config.read(self.config_file)
self.comp = JCLComponent("jcl.test.com", self.comp = JCLComponent("jcl.test.com",
@@ -2318,7 +2319,9 @@ class JCLCommandManager_TestCase(JCLTestCase):
self.assertEquals(len(fields), 2) self.assertEquals(len(fields), 2)
self.assertEquals(fields[1].prop("var"), "motd") self.assertEquals(fields[1].prop("var"), "motd")
self.assertEquals(fields[1].prop("type"), "text-multi") self.assertEquals(fields[1].prop("type"), "text-multi")
self.assertEquals(fields[1].children.name, "required") self.assertEquals(fields[1].children.name, "value")
self.assertEquals(fields[1].children.content, "")
self.assertEquals(fields[1].children.next.name, "required")
# Second step # Second step
info_query = Iq(stanza_type="set", info_query = Iq(stanza_type="set",
@@ -2355,14 +2358,99 @@ class JCLCommandManager_TestCase(JCLTestCase):
self.assertEquals(result[1].get_body(), "Message Of The Day") self.assertEquals(result[1].get_body(), "Message Of The Day")
self.assertFalse(account21.user.has_received_motd) self.assertFalse(account21.user.has_received_motd)
# def test_execute_edit_motd(self): def test_execute_edit_motd(self):
# #TODO : implement command self.comp.account_manager.account_classes = (ExampleAccount,
# info_query = Iq(stanza_type="set", Example2Account)
# from_jid="user1@test.com", config_file = tempfile.mktemp(".conf", "jcltest", jcl.tests.DB_DIR)
# to_jid="jcl.test.com") self.comp.config_file = config_file
# result = self.command_manager.execute_add_user(info_query) self.comp.config = ConfigParser()
# self.assertNotEquals(result, None) self.comp.set_motd("test motd")
# self.assertEquals(len(result), 1) model.db_connect()
user1 = User(jid="test1@test.com")
account11 = ExampleAccount(user=user1,
name="account11",
jid="account11@jcl.test.com")
account11.status = account.ONLINE
account12 = Example2Account(user=user1,
name="account12",
jid="account12@jcl.test.com")
account12.status = "away"
user2 = User(jid="test2@test.com")
account21 = ExampleAccount(user=user2,
name="account21",
jid="account21@jcl.test.com")
account21.status = account.OFFLINE
account22 = ExampleAccount(user=user2,
name="account11",
jid="account11@jcl.test.com")
account22.status = account.OFFLINE
model.db_disconnect()
info_query = Iq(stanza_type="set",
from_jid="user1@test.com",
to_jid="jcl.test.com")
command_node = info_query.set_new_content(command.COMMAND_NS, "command")
command_node.setProp("node",
"http://jabber.org/protocol/admin#edit-motd")
result = self.command_manager.apply_command_action(\
info_query,
"http://jabber.org/protocol/admin#edit-motd",
"execute")
self.assertNotEquals(result, None)
self.assertEquals(len(result), 1)
xml_command = result[0].xpath_eval("c:command",
{"c": "http://jabber.org/protocol/commands"})[0]
self.assertEquals(xml_command.prop("status"), "executing")
self.assertNotEquals(xml_command.prop("sessionid"), None)
self.__check_actions(result[0], ["next"])
fields = result[0].xpath_eval("c:command/data:x/data:field",
{"c": "http://jabber.org/protocol/commands",
"data": "jabber:x:data"})
self.assertEquals(len(fields), 2)
self.assertEquals(fields[1].prop("var"), "motd")
self.assertEquals(fields[1].prop("type"), "text-multi")
self.assertEquals(fields[1].children.name, "value")
self.assertEquals(fields[1].children.content, "test motd")
self.assertEquals(fields[1].children.next.name, "required")
# Second step
info_query = Iq(stanza_type="set",
from_jid="user1@test.com",
to_jid="jcl.test.com")
command_node = info_query.set_new_content(command.COMMAND_NS, "command")
command_node.setProp("node",
"http://jabber.org/protocol/admin#edit-motd")
session_id = xml_command.prop("sessionid")
command_node.setProp("sessionid", session_id)
command_node.setProp("action", "next")
submit_form = Form(xmlnode_or_type="submit")
submit_form.add_field(field_type="text-multi",
name="motd",
value=["Message Of The Day"])
submit_form.as_xml(command_node)
result = self.command_manager.apply_command_action(\
info_query,
"http://jabber.org/protocol/admin#edit-motd",
"execute")
self.assertNotEquals(result, None)
self.assertEquals(len(result), 2)
xml_command = result[0].xpath_eval("c:command",
{"c": "http://jabber.org/protocol/commands"})[0]
self.assertEquals(xml_command.prop("status"), "completed")
self.assertEquals(xml_command.prop("sessionid"), session_id)
self.__check_actions(result[0])
context_session = self.command_manager.sessions[session_id][1]
self.assertEquals(context_session["motd"],
["Message Of The Day"])
self.assertTrue(account11.user.has_received_motd)
self.assertEquals(result[1].get_from(), "jcl.test.com")
self.assertEquals(result[1].get_to(), "test1@test.com")
self.assertEquals(result[1].get_body(), "Message Of The Day")
self.assertFalse(account21.user.has_received_motd)
self.comp.config.read(self.comp.config_file)
self.assertTrue(self.comp.config.has_option("component", "motd"))
self.assertEquals(self.comp.config.get("component", "motd"),
"Message Of The Day")
os.unlink(config_file)
# def test_execute_delete_motd(self): # def test_execute_delete_motd(self):
# #TODO : implement command # #TODO : implement command

View File

@@ -26,6 +26,9 @@ import unittest
import threading import threading
import time import time
import re import re
from ConfigParser import ConfigParser
import tempfile
import os
from pyxmpp.jid import JID from pyxmpp.jid import JID
from pyxmpp.iq import Iq from pyxmpp.iq import Iq
@@ -33,6 +36,7 @@ from pyxmpp.presence import Presence
from pyxmpp.message import Message from pyxmpp.message import Message
from pyxmpp.jabber.dataforms import Form from pyxmpp.jabber.dataforms import Form
import jcl.tests
from jcl.jabber import Handler from jcl.jabber import Handler
from jcl.jabber.component import JCLComponent, AccountManager from jcl.jabber.component import JCLComponent, AccountManager
from jcl.jabber.presence import DefaultSubscribeHandler, \ from jcl.jabber.presence import DefaultSubscribeHandler, \
@@ -2499,6 +2503,62 @@ class JCLComponent_TestCase(JCLTestCase):
self.comp.send_stanzas(None) self.comp.send_stanzas(None)
self.assertEquals(len(self.comp.stream.sent), 0) self.assertEquals(len(self.comp.stream.sent), 0)
def test_get_motd(self):
config_file = tempfile.mktemp(".conf", "jcltest", jcl.tests.DB_DIR)
self.comp.config_file = config_file
self.comp.config = ConfigParser()
self.comp.config.read(self.comp.config_file)
self.comp.config.add_section("component")
self.comp.config.set("component", "motd", "test motd")
self.comp.config.write(open(self.comp.config_file, "w"))
motd = self.comp.get_motd()
self.assertEquals(motd, "test motd")
os.unlink(config_file)
def test_get_no_motd(self):
config_file = tempfile.mktemp(".conf", "jcltest", jcl.tests.DB_DIR)
self.comp.config_file = config_file
self.comp.config = ConfigParser()
self.comp.config.read(self.comp.config_file)
self.comp.config.write(open(self.comp.config_file, "w"))
motd = self.comp.get_motd()
self.assertEquals(motd, None)
os.unlink(config_file)
def test_set_new_motd(self):
config_file = tempfile.mktemp(".conf", "jcltest", jcl.tests.DB_DIR)
self.comp.config_file = config_file
self.comp.config = ConfigParser()
self.comp.set_motd("test motd")
self.comp.config.read(self.comp.config_file)
self.assertTrue(self.comp.config.has_option("component", "motd"))
self.assertEquals(self.comp.config.get("component", "motd"), "test motd")
os.unlink(config_file)
def test_set_motd(self):
config_file = tempfile.mktemp(".conf", "jcltest", jcl.tests.DB_DIR)
self.comp.config_file = config_file
self.comp.config = ConfigParser()
self.comp.config.add_section("component")
self.comp.config.set("component", "motd", "test motd")
self.comp.config.write(open(self.comp.config_file, "w"))
self.comp.set_motd("test new motd")
self.comp.config.read(self.comp.config_file)
self.assertTrue(self.comp.config.has_option("component", "motd"))
self.assertEquals(self.comp.config.get("component", "motd"), "test new motd")
os.unlink(config_file)
def test_del_motd(self):
config_file = tempfile.mktemp(".conf", "jcltest", jcl.tests.DB_DIR)
self.comp.config_file = config_file
self.comp.config = ConfigParser()
self.comp.config.add_section("component")
self.comp.config.set("component", "motd", "test motd")
self.comp.config.write(open(self.comp.config_file, "w"))
self.comp.del_motd()
self.comp.config.read(self.comp.config_file)
self.assertFalse(self.comp.config.has_option("component", "motd"))
os.unlink(config_file)
########################################################################### ###########################################################################
# 'handle_command' tests # 'handle_command' tests