comparison src/plugins/plugin_xep_0249.py @ 689:78bf4ed37574

plugin XEP-249: added parameter Misc / Auto-join MUC on invitation
author souliane <souliane@mailoo.org>
date Tue, 05 Nov 2013 21:08:31 +0100
parents 84a6e83157c2
children 3344f1d8a232
comparison
equal deleted inserted replaced
688:f7878ad3c846 689:78bf4ed37574
33 from wokkel.subprotocols import XMPPHandler 33 from wokkel.subprotocols import XMPPHandler
34 34
35 MESSAGE = '/message' 35 MESSAGE = '/message'
36 NS_DIRECT_MUC_INVITATION = 'jabber:x:conference' 36 NS_DIRECT_MUC_INVITATION = 'jabber:x:conference'
37 DIRECT_MUC_INVITATION_REQUEST = MESSAGE + '/x[@xmlns="' + NS_DIRECT_MUC_INVITATION + '"]' 37 DIRECT_MUC_INVITATION_REQUEST = MESSAGE + '/x[@xmlns="' + NS_DIRECT_MUC_INVITATION + '"]'
38 AUTOJOIN_KEY = "Misc"
39 AUTOJOIN_NAME = "Auto-join MUC on invitation"
40 AUTOJOIN_VALUES = ["ask", "always", "never"]
38 41
39 PLUGIN_INFO = { 42 PLUGIN_INFO = {
40 "name": "XEP 0249 Plugin", 43 "name": "XEP 0249 Plugin",
41 "import_name": "XEP-0249", 44 "import_name": "XEP-0249",
42 "type": "XEP", 45 "type": "XEP",
48 } 51 }
49 52
50 53
51 class XEP_0249(object): 54 class XEP_0249(object):
52 55
56 params = """
57 <params>
58 <individual>
59 <category name="%(category_name)s" label="%(category_label)s">
60 <param name="%(param_name)s" label="%(param_label)s" value="%(param_default)s" type="list" security="0">
61 %(param_options)s
62 </param>
63 </category>
64 </individual>
65 </params>
66 """ % {
67 'category_name': AUTOJOIN_KEY,
68 'category_label': _("Misc"),
69 'param_name': AUTOJOIN_NAME,
70 'param_label': _("Auto-join MUC on invitation"),
71 'param_default': AUTOJOIN_VALUES[0],
72 'param_options': ['<option value="%s"/>' % value for value in AUTOJOIN_VALUES]
73 }
74
53 def __init__(self, host): 75 def __init__(self, host):
54 info(_("Plugin XEP_0249 initialization")) 76 info(_("Plugin XEP_0249 initialization"))
55 self.host = host 77 self.host = host
78 host.memory.updateParams(self.params)
56 host.bridge.addMethod("inviteMUC", ".plugin", in_sign='sssa{ss}s', out_sign='', method=self._invite) 79 host.bridge.addMethod("inviteMUC", ".plugin", in_sign='sssa{ss}s', out_sign='', method=self._invite)
57 80
58 def getHandler(self, profile): 81 def getHandler(self, profile):
59 return XEP_0249_handler(self) 82 return XEP_0249_handler(self)
60 83
87 @param profile_key: %(doc_profile_key)s 110 @param profile_key: %(doc_profile_key)s
88 """ 111 """
89 #TODO: check parameters validity 112 #TODO: check parameters validity
90 self.invite(jid.JID(target), jid.JID("%s@%s" % (roomId, service)), options, profile_key) 113 self.invite(jid.JID(target), jid.JID("%s@%s" % (roomId, service)), options, profile_key)
91 114
115 def _accept(self, room, profile_key='@DEFAULT@'):
116 """
117 Accept the invitation to join a MUC
118 @param room: room jid as string
119 """
120 profile = self.host.memory.getProfileName(profile_key)
121 if not profile:
122 error(_("Profile doesn't exists !"))
123 return
124 info(_('Invitation accepted for room %(room)s [%(profile)s]') % {'room': room, 'profile': profile})
125 _jid, xmlstream = self.host.getJidNStream(profile)
126 d = self.host.plugins["XEP-0045"].join(jid.JID(room), _jid.user, {}, profile)
127
92 def onInvitation(self, message, profile): 128 def onInvitation(self, message, profile):
93 """ 129 """
94 called when an invitation is received 130 called when an invitation is received
95 @param message: message element 131 @param message: message element
96 @profile: %(doc_profile)s 132 @profile: %(doc_profile)s
97 """ 133 """
98 info(_('Invitation received for room %(room)s [%(profile)s]') % {'room': '', 'profile': profile})
99 try: 134 try:
100 room = jid.JID(message.firstChildElement()['jid']) 135 room = message.firstChildElement()['jid']
136 info(_('Invitation received for room %(room)s [%(profile)s]') % {'room': room, 'profile': profile})
101 except: 137 except:
102 error(_('Error while parsing invitation')) 138 error(_('Error while parsing invitation'))
103 return 139 return
104 _jid, xmlstream = self.host.getJidNStream(profile) 140 autojoin = self.host.memory.getParamA(AUTOJOIN_NAME, AUTOJOIN_KEY, profile_key=profile)
105 #TODO: we always autojoin so far, we need to add a parameter to autojoin/ignore invitations or let user choose to follow it 141 from_ = message["from"]
106 d = self.host.plugins["XEP-0045"].join(room, _jid.user, {}, profile) 142
143 def accept_cb(conf_id, accepted, data, profile):
144 if conf_id == room and accepted:
145 self._accept(room, profile)
146
147 if autojoin == "always":
148 self._accept(room, profile)
149 elif autojoin == "ask":
150 data = {"message": _("You have been invited by %s to join the room %s. Do you accept?") % (from_, room), "title": _("MUC invitation")}
151 self.host.askConfirmation(room, "YES/NO", data, accept_cb, profile)
152 else:
153 self.host.bridge.newAlert(_("An invitation from %s to join the room %s has been declined according to your personal settings.") % (from_, room), _("MUC invitation"), "INFO", profile)
107 154
108 155
109 class XEP_0249_handler(XMPPHandler): 156 class XEP_0249_handler(XMPPHandler):
110 implements(iwokkel.IDisco) 157 implements(iwokkel.IDisco)
111 158