annotate src/plugins/plugin_misc_groupblog.py @ 462:d9456d94cd12

plugin groupblog: next-gen group blog first draft
author Goffi <goffi@goffi.org>
date Wed, 14 Mar 2012 23:45:01 +0100
parents cf005701624b
children 78e67a59d51d
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 SAT plugin for microbloging with roster access
459
cf005701624b copyleft date update
Goffi <goffi@goffi.org>
parents: 372
diff changeset
6 Copyright (C) 2009, 2010, 2011, 2012 Jérôme Poisson (goffi@goffi.org)
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from logging import debug, info, error
308
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
23 from twisted.internet import protocol, defer
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.words.protocols.jabber import jid
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import error as jab_error
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 import twisted.internet.error
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.words.xish import domish
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 from sat.tools.xml_tools import ElementParser
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
29
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
30 from wokkel import disco, pubsub, data_form
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from feed.atom import Entry, Author
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32 import uuid
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from time import time
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
35 NS_PUBSUB = 'http://jabber.org/protocol/pubsub'
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
36 #NS_PUBSUB_EXP = 'http://goffi.org/protocol/pubsub' #for non official features
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
37 NS_PUBSUB_EXP = NS_PUBSUB #XXX: we can't use custom namespace as Wokkel's PubSubService use official NS
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
38 NS_PUBSUB_ITEM_ACCESS = NS_PUBSUB_EXP + "#item-access"
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
39 NS_PUBSUB_CREATOR_JID_CHECK = NS_PUBSUB_EXP + "#creator-jid-check"
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
40 NS_PUBSUB_ITEM_CONFIG = NS_PUBSUB_EXP + "#item-config"
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
41 NS_PUBSUB_AUTO_CREATE = NS_PUBSUB + "#auto-create"
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
42 OPT_ROSTER_GROUPS_ALLOWED = 'pubsub#roster_groups_allowed'
310
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
43 OPT_ACCESS_MODEL = 'pubsub#access_model'
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
44 OPT_PERSIST_ITEMS = 'pubsub#persist_items'
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
45 OPT_MAX_ITEMS = 'pubsub#max_items'
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
46 OPT_NODE_TYPE = 'pubsub#node_type'
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
47 OPT_SUBSCRIPTION_TYPE = 'pubsub#subscription_type'
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
48 OPT_SUBSCRIPTION_DEPTH = 'pubsub#subscription_depth'
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 TYPE_COLLECTION = 'collection'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 PLUGIN_INFO = {
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 "name": "Group blogging throught collections",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 "import_name": "groupblog",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54 "type": "MISC",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 "protocols": [],
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56 "dependencies": ["XEP-0277"],
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 "main": "GroupBlog",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 "handler": "no",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 "description": _("""Implementation of microblogging with roster access""")
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 }
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
61
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
62 class NodeCreationError(Exception):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
63 pass
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
64
308
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
65 class NodeDeletionError(Exception):
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
66 pass
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
67
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
68 class GroupBlog():
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
69 """This class use a SàT PubSub Service to manage access on microblog"""
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
71 def __init__(self, host):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
72 info(_("Group blog plugin initialization"))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
73 self.host = host
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
74
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 340
diff changeset
75 host.bridge.addMethod("sendGroupBlog", ".plugin", in_sign='asss', out_sign='',
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
76 method=self.sendGroupBlog,
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 doc = { 'summary':"Send a microblog to a list of groups",
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 'param_0':'list of groups which can read the microblog',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 'param_1':'text to send',
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80 'param_2':'%(doc_profile)s'
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81 })
310
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
82
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
83 def _publishMblog(self, service, groups, message, client):
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
84 """Actually publish the message on the group blog
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
85 @param service: jid of the item-access pubsub service
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
86 @param groups: set of groups allowed to see the item
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 @param message: message to publish
311
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
88 @param client: SatXMPPClient of the published"""
0aa6ca6cdbdd plugin group blog: group blog now use PEP to take profit of autosubscribe
Goffi <goffi@goffi.org>
parents: 310
diff changeset
89 mblog_item = self.host.plugins["XEP-0277"].data2entry({'content':message}, client.profile)
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
90 form = data_form.Form('submit', formNamespace=NS_PUBSUB_ITEM_CONFIG)
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
91 field = data_form.Field('list-multi', OPT_ROSTER_GROUPS_ALLOWED, values=groups)
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
92 form.addField(field)
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
93 mblog_item.addChild(form.toElement())
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
94 defer_blog = self.host.plugins["XEP-0060"].publish(service, client.jid.userhost(), items=[mblog_item], profile_key=client.profile)
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
95 defer_blog.addErrback(self._mblogPublicationFailed)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
96
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
97 def _mblogPublicationFailed(self, failure):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
98 #TODO
340
2572351d875a plugin groupblog: removed breakpoints, but error handling still need work
Goffi <goffi@goffi.org>
parents: 326
diff changeset
99 pass
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
101 @defer.inlineCallbacks
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
102 def sendGroupBlog(self, groups, message, profile_key='@DEFAULT@'):
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
103 """Publish a microblog to the node associated to the groups
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
104 If the node doesn't exist, it is created, then the message is posted
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
105 @param groups: list of groups allowed to retrieve the microblog
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
106 @param message: microblog
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
107 @profile_key: %(doc_profile)s
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
108 """
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
109 print "sendGroupBlog"
307
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
110 profile = self.host.memory.getProfileName(profile_key)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
111 if not profile:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
112 error(_("Unknown profile"))
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
113 return
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
114
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
115 client = self.host.getClient(profile)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
116 if not client:
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
117 error(_('No client for this profile key: %s') % profile_key)
1e4575e12581 Group blog first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
118 return
308
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
119
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
120 yield client.client_initialized #we want to be sure that the client is initialized
308
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
121
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
122 #we first check that we have a item-access pubsub server
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
123 if not hasattr(client,"item_access_pubsub"):
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
124 debug(_('Looking for item-access power pubsub server'))
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
125 #we don't have any pubsub server featuring item access yet
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
126 test = self.host.memory.getServerServiceEntities("pubsub", "service", profile)
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
127 client.item_access_pubsub = None
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
128 for entity in self.host.memory.getServerServiceEntities("pubsub", "service", profile):
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
129 disco = yield client.disco.requestInfo(entity)
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
130 if set([NS_PUBSUB_ITEM_ACCESS, NS_PUBSUB_AUTO_CREATE, NS_PUBSUB_CREATOR_JID_CHECK]).issubset(disco.features):
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
131 info(_("item-access powered pubsub service found: [%s]") % entity.full())
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
132 client.item_access_pubsub = entity
308
ce3607b7198d plugin group blog: blog collection cleaning
Goffi <goffi@goffi.org>
parents: 307
diff changeset
133
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
134 if not client.item_access_pubsub:
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
135 error(_("No item-access powered pubsub server found, can't post group blog"))
310
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
136 return
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
137
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
138 _groups = set(groups).intersection(client.roster.getGroups()) #We only keep group which actually exist
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
139 #TODO: send an error signal if user want to post to non existant groups
310
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
140
462
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
141 self._publishMblog(client.item_access_pubsub, _groups, message, client)
d9456d94cd12 plugin groupblog: next-gen group blog first draft
Goffi <goffi@goffi.org>
parents: 459
diff changeset
142
310
53adec87d1d7 plugin group blog: group blog subscription
Goffi <goffi@goffi.org>
parents: 308
diff changeset
143