annotate src/plugins/plugin_xep_0249.py @ 331:0a8eb0461f31

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