core: exceptions moved to core

plugin group blog: group blog now manage public microblogs
This commit is contained in:
Goffi 2012-04-04 00:06:44 +02:00
commit 52b00f6d6f
7 changed files with 57 additions and 41 deletions

View file

@ -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)

View file

@ -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)

23
src/core/exceptions.py Normal file
View file

@ -0,0 +1,23 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
SàT Exceptions
Copyright (C) 2011 Jérôme Poisson (goffi@goffi.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
class ProfileUnknownError(Exception):
pass

View file

@ -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 !"))

View file

@ -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):

View file

@ -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):

View file

@ -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: