comparison src/plugins/plugin_xep_0249.py @ 317:f1f7c94278f2

added plugin XEP-0249: direct MUC invitation
author Goffi <goffi@goffi.org>
date Fri, 06 May 2011 15:33:07 +0200
parents
children f964dcec1611
comparison
equal deleted inserted replaced
316:3a21d586dae4 317:f1f7c94278f2
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 SAT plugin for managing xep-0249
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22 from logging import debug, info, warning, error
23 from twisted.words.xish import domish
24 from twisted.internet import protocol, defer
25 from twisted.words.protocols.jabber import client, jid, xmlstream
26
27 from zope.interface import implements
28
29 from wokkel import disco, iwokkel, data_form
30
31
32 try:
33 from twisted.words.protocols.xmlstream import XMPPHandler
34 except ImportError:
35 from wokkel.subprotocols import XMPPHandler
36
37 MESSAGE= '/message'
38 NS_DIRECT_MUC_INVITATION = 'jabber:x:conference'
39 DIRECT_MUC_INVITATION_REQUEST = MESSAGE + '/x[@xmlns="' + NS_DIRECT_MUC_INVITATION + '"]'
40
41 PLUGIN_INFO = {
42 "name": "XEP 0249 Plugin",
43 "import_name": "XEP-0249",
44 "type": "XEP",
45 "protocols": ["XEP-0249"],
46 "dependencies": ["XEP-0045"],
47 "main": "XEP_0249",
48 "handler": "yes",
49 "description": _("""Implementation of Direct MUC Invitations""")
50 }
51
52 class XEP_0249():
53
54 def __init__(self, host):
55 info(_("Plugin XEP_0249 initialization"))
56 self.host = host
57 host.bridge.addMethod("inviteMUC", ".communication", in_sign='sssa{ss}s', out_sign='', method=self._invite)
58
59 def getHandler(self, profile):
60 return XEP_0249_handler(self)
61
62 def invite(self, target, room, options={}, profile_key='@DEFAULT@'):
63 """
64 Invite a user to a room
65 @param target: jid of the user to invite
66 @param room: jid of the room where the user is invited
67 @options: attribute with extra info (reason, password) as in #XEP-0249
68 @profile_key: %(doc_profile_key)s
69 """
70 profile = self.host.memory.getProfileName(profile_key)
71 if not profile:
72 error(_("Profile doesn't exists !"))
73 return
74 message = domish.Element((None,'message'))
75 message["to"] = target.full()
76 x_elt = message.addElement('x',NS_DIRECT_MUC_INVITATION)
77 x_elt['jid'] = room.userhost()
78 for opt in options:
79 x_elt[opt] = options[opt]
80 self.host.profiles[profile].xmlstream.send(message)
81
82 def _invite(self, target, service, roomId, options = {}, profile_key='@DEFAULT@'):
83 """
84 Invite an user to a room
85 @param target: jid of the user to invite
86 @param service: jid of the MUC service
87 @param roomId: name of the room
88 @param profile_key: %(doc_profile_key)s
89 """
90 #TODO: check parameters validity
91 self.invite(jid.JID(target), jid.JID("%s@%s" % (roomId, service)), options, profile_key)
92
93
94 def onInvitation(self, message, profile):
95 """
96 called when an invitation is received
97 @param message: message element
98 @profile: %(doc_profile)s
99 """
100 info(_('Invitation received for room %(room)s [%(profile)s]') % {'room':'','profile':profile})
101 try:
102 room = jid.JID(message.firstChildElement()['jid'])
103 except:
104 error(_('Error while parsing invitation'))
105 return
106 _jid, xmlstream = self.host.getJidNStream(profile)
107 #TODO: we always autojoin so far, we need to add a parameter to autojoin/ignore invitations or let user choose to follow it
108 d = self.host.plugins["XEP-0045"].join(room.host, room.user, _jid.user, profile)
109
110
111 class XEP_0249_handler(XMPPHandler):
112 implements(iwokkel.IDisco)
113
114 def __init__(self, plugin_parent):
115 self.plugin_parent = plugin_parent
116 self.host = plugin_parent.host
117
118 def connectionInitialized(self):
119 self.xmlstream.addObserver(DIRECT_MUC_INVITATION_REQUEST, self.plugin_parent.onInvitation, profile = self.parent.profile)
120
121 def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
122 return [disco.DiscoFeature(NS_DIRECT_MUC_INVITATION)]
123
124 def getDiscoItems(self, requestor, target, nodeIdentifier=''):
125 return []