From 52b00f6d6f089883281f6c92426dab9e8b19351b Mon Sep 17 00:00:00 2001 From: Goffi Date: Wed, 4 Apr 2012 00:06:44 +0200 Subject: [PATCH] core: exceptions moved to core plugin group blog: group blog now manage public microblogs --- frontends/src/bridge/DBus.py | 4 +- .../dbus_frontend_template.py | 4 +- src/{ => core}/exceptions.py | 0 src/core/sat_main.py | 3 +- src/core/xmpp.py | 2 +- src/plugins/plugin_misc_groupblog.py | 81 +++++++++++-------- src/plugins/plugin_misc_maildir.py | 4 +- 7 files changed, 57 insertions(+), 41 deletions(-) rename src/{ => core}/exceptions.py (100%) diff --git a/frontends/src/bridge/DBus.py b/frontends/src/bridge/DBus.py index 8d4fba5c..bf35d1a6 100644 --- a/frontends/src/bridge/DBus.py +++ b/frontends/src/bridge/DBus.py @@ -243,8 +243,8 @@ class DBusBridgeFrontend(BridgeFrontend): def getLastMicroblogs(self, jid, max_items, profile_key, callback=None, errback=None): return self.db_plugin_iface.getLastMicroblogs(jid, max_items, profile_key, reply_handler=callback, error_handler=errback) - def sendGroupBlog(self, groups, message, profile_key): - return self.db_plugin_iface.sendGroupBlog(groups, message, profile_key) + def sendGroupBlog(self, access_type, access_list, message, profile_key='@DEFAULT@'): + return self.db_plugin_iface.sendGroupBlog(access_type, access_list, message, profile_key) def getLastGroupBlogs(self, jid, max_items, profile_key, callback=None, errback=None): return self.db_plugin_iface.getLastGroupBlogs(jid, max_items, profile_key, reply_handler=callback, error_handler=errback) diff --git a/src/bridge/bridge_constructor/dbus_frontend_template.py b/src/bridge/bridge_constructor/dbus_frontend_template.py index f53ff964..16b7f27e 100644 --- a/src/bridge/bridge_constructor/dbus_frontend_template.py +++ b/src/bridge/bridge_constructor/dbus_frontend_template.py @@ -136,8 +136,8 @@ class DBusBridgeFrontend(BridgeFrontend): def getLastMicroblogs(self, jid, max_items, profile_key, callback=None, errback=None): return self.db_plugin_iface.getLastMicroblogs(jid, max_items, profile_key, reply_handler=callback, error_handler=errback) - def sendGroupBlog(self, groups, message, profile_key): - return self.db_plugin_iface.sendGroupBlog(groups, message, profile_key) + def sendGroupBlog(self, access_type, access_list, message, profile_key='@DEFAULT@'): + return self.db_plugin_iface.sendGroupBlog(access_type, access_list, message, profile_key) def getLastGroupBlogs(self, jid, max_items, profile_key, callback=None, errback=None): return self.db_plugin_iface.getLastGroupBlogs(jid, max_items, profile_key, reply_handler=callback, error_handler=errback) diff --git a/src/exceptions.py b/src/core/exceptions.py similarity index 100% rename from src/exceptions.py rename to src/core/exceptions.py diff --git a/src/core/sat_main.py b/src/core/sat_main.py index 1f462f79..15c476f4 100644 --- a/src/core/sat_main.py +++ b/src/core/sat_main.py @@ -43,6 +43,7 @@ import sys import os.path from sat.core import xmpp +from sat.core.exceptions import ProfileUnknownError from sat.memory.memory import Memory from sat.tools.xml_tools import tupleList2dataForm from sat.tools.misc import TriggerManager @@ -213,7 +214,7 @@ class SAT(service.Service): profile = self.memory.getProfileName(profile_key) if not profile: error (_('Trying to connect a non-exsitant profile')) - return defer.fail() + raise ProfileUnknownError(profile_key) if (self.isConnected(profile)): info(_("already connected !")) diff --git a/src/core/xmpp.py b/src/core/xmpp.py index c18f6bbf..62d33683 100644 --- a/src/core/xmpp.py +++ b/src/core/xmpp.py @@ -187,7 +187,7 @@ class SatRosterProtocol(xmppim.RosterClientProtocol): bare_jid = item.jid.userhost() self._jids[bare_jid] = item for group in item.groups: - self._groups.get(group,set()).add(bare_jid) + self._groups.setdefault(group,set()).add(bare_jid) self.host.bridge.newContact(item.jid.full(), self.getAttributes(item), item.groups, self.parent.profile) def onRosterRemove(self, entity): diff --git a/src/plugins/plugin_misc_groupblog.py b/src/plugins/plugin_misc_groupblog.py index 971f1d53..dcafa25e 100755 --- a/src/plugins/plugin_misc_groupblog.py +++ b/src/plugins/plugin_misc_groupblog.py @@ -62,6 +62,12 @@ PLUGIN_INFO = { class NoCompatiblePubSubServerFound(Exception): pass +class BadAccessTypeError(Exception): + pass + +class BadAccessListError(Exception): + pass + class GroupBlog(): """This class use a SàT PubSub Service to manage access on microblog""" @@ -69,24 +75,13 @@ class GroupBlog(): info(_("Group blog plugin initialization")) self.host = host - host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='asss', out_sign='', - method=self.sendGroupBlog, - doc = { 'summary':"Send a microblog to a list of groups", - 'param_0':'list of groups which can read the microblog', - 'param_1':'text to send', - 'param_2':'%(doc_profile)s' - }) + host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='sasss', out_sign='', + method=self.sendGroupBlog) host.bridge.addMethod("getLastGroupBlogs", ".plugin", in_sign='sis', out_sign='aa{ss}', method=self.getLastGroupBlogs, - async = True, - doc = { 'summary':'retrieve items', - 'param_0':'jid: publisher of wanted microblog', - 'param_1':'max_items: see XEP-0060 #6.5.7', - 'param_2':'%(doc_profile)s', - 'return':'list of microblog data (dict)' - }) + async = True) host.bridge.addMethod("getMassiveLastGroupBlogs", ".plugin", in_sign='sasis', out_sign='a{saa{ss}}', @@ -129,40 +124,61 @@ class GroupBlog(): defer.returnValue((profile, client)) - def _publishMblog(self, service, groups, message, client): + def _publishMblog(self, service, client, access_type, access_list, message): """Actually publish the message on the group blog @param service: jid of the item-access pubsub service - @param groups: set of groups allowed to see the item + @param client: SatXMPPClient of the published + @param access_type: one of "PUBLIC", "GROUP", "JID" + @param access_list: set of entities (empty list for all, groups or jids) allowed to see the item @param message: message to publish - @param client: SatXMPPClient of the published""" + """ mblog_item = self.host.plugins["XEP-0277"].data2entry({'content':message}, client.profile) form = data_form.Form('submit', formNamespace=NS_PUBSUB_ITEM_CONFIG) - field = data_form.Field('list-multi', OPT_ROSTER_GROUPS_ALLOWED, values=groups) - form.addField(field) - mblog_item.addChild(form.toElement()) + if access_type == "PUBLIC": + if access_list: + raise BadAccessListError("access_list must be empty for PUBLIC access") + elif access_type == "GROUP": + field = data_form.Field('list-multi', OPT_ROSTER_GROUPS_ALLOWED, values=access_list) + form.addField(field) + mblog_item.addChild(form.toElement()) + elif access_type == "JID": + raise NotImplementedError + else: + error(_("Unknown access_type")) + raise BadAccessTypeError defer_blog = self.host.plugins["XEP-0060"].publish(service, client.jid.userhost(), items=[mblog_item], profile_key=client.profile) defer_blog.addErrback(self._mblogPublicationFailed) def _mblogPublicationFailed(self, failure): #TODO - pass + return failure - def sendGroupBlog(self, groups, message, profile_key='@DEFAULT@'): - """Publish a microblog to the node associated to the groups - If the node doesn't exist, it is created, then the message is posted - @param groups: list of groups allowed to retrieve the microblog + def sendGroupBlog(self, access_type, access_list, message, profile_key='@DEFAULT@'): + """Publish a microblog with given item access + @param access_type: one of "PUBLIC", "GROUP", "JID" + @param access_list: list of authorized entity (empty list for PUBLIC ACCESS, + list of groups or list of jids) for this item @param message: microblog @profile_key: %(doc_profile)s """ print "sendGroupBlog" - def initialised(result): profile, client = result - _groups = set(groups).intersection(client.roster.getGroups()) #We only keep group which actually exist - #TODO: send an error signal if user want to post to non existant groups + if access_type == "PUBLIC": + if access_list: + raise Exception("Publishers list must be empty when getting microblogs for all contacts") + self._publishMblog(client.item_access_pubsub, client, "PUBLIC", [], message) + elif access_type == "GROUP": + _groups = set(access_list).intersection(client.roster.getGroups()) #We only keep group which actually exist + if not _groups: + raise BadAccessListError("No valid group") + self._publishMblog(client.item_access_pubsub, client, "GROUP", _groups, message) + elif access_type == "JID": + raise NotImplementedError + else: + error(_("Unknown access type")) + raise BadAccessTypeError - self._publishMblog(client.item_access_pubsub, _groups, message, client) - self.initialise(profile_key).addCallback(initialised) @@ -170,10 +186,9 @@ class GroupBlog(): def getLastGroupBlogs(self, pub_jid, max_items=10, profile_key='@DEFAULT@'): """Get the last published microblogs @param pub_jid: jid of the publisher - @param max_items: how many microblogs we want to get + @param max_items: how many microblogs we want to get (see XEP-0060 #6.5.7) @param profile_key: profile key - @param callback: used for the async answer - @param errback: used for the async answer + @return: list of microblog data (dict) """ def initialised(result): diff --git a/src/plugins/plugin_misc_maildir.py b/src/plugins/plugin_misc_maildir.py index db13193a..9fcbbc22 100755 --- a/src/plugins/plugin_misc_maildir.py +++ b/src/plugins/plugin_misc_maildir.py @@ -33,7 +33,7 @@ import os,os.path from cStringIO import StringIO from twisted.internet import reactor import pdb -from sat.exceptions import * +from sat.core.exceptions import ProfileUnknownError from sat.memory.persistent import PersistentBinaryDict @@ -107,7 +107,7 @@ class MaildirBox(): @param observer: method to call when a NewMessage arrive""" profile = self.host.memory.getProfileName(profile_key) if not profile: - raise ProfileUnknownError + raise ProfileUnknownError(profile_key) if not self.__mailboxes[profile].has_key(boxname): self.__mailboxes[profile][boxname]=MailboxUser(self, boxname, observer, profile=profile) else: