Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0045.py @ 321:1a990a88bff2
wix: force updating of splitterWindow in ChatWindow (needed when Tarot game is launched)
author | Goffi <goffi@goffi.org> |
---|---|
date | Sun, 08 May 2011 17:09:03 +0200 |
parents | 5bb1cfc105d0 |
children | abe08fcb42d7 |
rev | line source |
---|---|
72 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT plugin for managing xep-0045 | |
228 | 6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org) |
72 | 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, threads, reactor | |
25 from twisted.words.protocols.jabber import client, jid, xmlstream | |
26 from twisted.words.protocols.jabber import error as jab_error | |
27 from twisted.words.protocols.jabber.xmlstream import IQ | |
28 import os.path | |
319 | 29 import uuid |
72 | 30 |
31 from zope.interface import implements | |
32 | |
33 from wokkel import disco, iwokkel, muc | |
34 | |
35 from base64 import b64decode | |
36 from hashlib import sha1 | |
37 from time import sleep | |
38 | |
39 try: | |
40 from twisted.words.protocols.xmlstream import XMPPHandler | |
41 except ImportError: | |
42 from wokkel.subprotocols import XMPPHandler | |
43 | |
44 PLUGIN_INFO = { | |
45 "name": "XEP 0045 Plugin", | |
291 | 46 "import_name": "XEP-0045", |
72 | 47 "type": "XEP", |
48 "protocols": ["XEP-0045"], | |
49 "dependencies": [], | |
50 "main": "XEP_0045", | |
51 "handler": "yes", | |
52 "description": _("""Implementation of Multi-User Chat""") | |
53 } | |
54 | |
55 class XEP_0045(): | |
56 | |
57 def __init__(self, host): | |
58 info(_("Plugin XEP_0045 initialization")) | |
59 self.host = host | |
60 self.clients={} | |
319 | 61 host.bridge.addMethod("joinMUC", ".communication", in_sign='ssss', out_sign='', method=self._join) |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
62 host.bridge.addMethod("getRoomJoined", ".communication", in_sign='s', out_sign='a(ssass)', method=self.getRoomJoined) |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
63 host.bridge.addMethod("getRoomSubjects", ".communication", in_sign='s', out_sign='a(sss)', method=self.getRoomSubjects) |
319 | 64 host.bridge.addMethod("getUniqueRoomName", ".communication", in_sign='s', out_sign='s', method=self.getUniqueName) |
73 | 65 host.bridge.addSignal("roomJoined", ".communication", signature='ssasss') #args: room_id, room_service, room_nicks, user_nick, profile |
74
6e3a06b4dd36
plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents:
73
diff
changeset
|
66 host.bridge.addSignal("roomUserJoined", ".communication", signature='sssa{ss}s') #args: room_id, room_service, user_nick, user_data, profile |
6e3a06b4dd36
plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents:
73
diff
changeset
|
67 host.bridge.addSignal("roomUserLeft", ".communication", signature='sssa{ss}s') #args: room_id, room_service, user_nick, user_data, profile |
76 | 68 host.bridge.addSignal("roomNewSubject", ".communication", signature='ssss') #args: room_id, room_service, subject, profile |
72 | 69 |
70 def __check_profile(self, profile): | |
75 | 71 """check if profile is used and connected |
72 if profile known but disconnected, remove it from known profiles | |
73 @param profile: profile to check | |
74 @return: True if the profile is known and connected, else False""" | |
72 | 75 if not profile or not self.clients.has_key(profile) or not self.host.isConnected(profile): |
91 | 76 error (_('Unknown or disconnected profile (%s)') % profile) |
72 | 77 if self.clients.has_key(profile): |
78 del self.clients[profile] | |
79 return False | |
80 return True | |
81 | |
82 def __room_joined(self, room, profile): | |
83 """Called when the user is in the requested room""" | |
319 | 84 def _sendBridgeSignal(ignore=None): |
85 self.host.bridge.roomJoined(room.roomIdentifier, room.service, [user.nick for user in room.roster.values()], room.nick, profile) | |
86 | |
72 | 87 room_jid = room.roomIdentifier+'@'+room.service |
88 self.clients[profile].joined_rooms[room_jid] = room | |
319 | 89 if room.status == '201': |
90 #FIXME: the current behaviour is to create an instant room | |
91 #and send the signal only when the room is unlocked | |
92 #a proper configuration management should be done | |
93 #TODO: wokkel's muc currently doesn't manage correctly message from the room | |
94 # service (without resource) in room.getUser | |
95 self.clients[profile].configure(room_jid).addCallbacks(_sendBridgeSignal, lambda x: error(_('Error while configuring the room'))) | |
96 else: | |
97 _sendBridgeSignal() | |
98 return room | |
99 | |
72 | 100 |
75 | 101 def __err_joining_room(self, failure, profile): |
72 | 102 """Called when something is going wrong when joining the room""" |
183
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
134
diff
changeset
|
103 mess = _("Error when joining the room") |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
134
diff
changeset
|
104 error (mess) |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
134
diff
changeset
|
105 self.host.bridge.newAlert(mess, _("Group chat error"), "ERROR", profile) |
72 | 106 |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
107 def getRoomJoined(self, profile_key='@DEFAULT@'): |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
108 """Return room where user is""" |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
109 profile = self.host.memory.getProfileName(profile_key) |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
110 result = [] |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
111 if not self.__check_profile(profile): |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
112 return result |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
113 for room in self.clients[profile].joined_rooms.values(): |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
114 result.append((room.roomIdentifier, room.service, [user.nick for user in room.roster.values()], room.nick)) |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
115 return result |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
116 |
93 | 117 def getRoomNick(self, room_jid, profile_key='@DEFAULT@'): |
118 """return nick used in room by user | |
119 @param room_jid: unicode room id | |
120 @profile_key: profile | |
121 @return: nick or empty string in case of error""" | |
122 profile = self.host.memory.getProfileName(profile_key) | |
123 if not self.__check_profile(profile) or not self.clients[profile].joined_rooms.has_key(room_jid): | |
124 return '' | |
125 return self.clients[profile].joined_rooms[room_jid].nick | |
126 | |
127 | |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
128 def getRoomSubjects(self, profile_key='@DEFAULT@'): |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
129 """Return received subjects of rooms""" |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
130 profile = self.host.memory.getProfileName(profile_key) |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
131 if not self.__check_profile(profile): |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
132 return [] |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
133 return self.clients[profile].rec_subjects.values() |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
134 |
319 | 135 def getUniqueName(self, profile_key='@DEFAULT@'): |
136 """Return unique name for room, avoiding collision""" | |
137 #TODO: we should use #RFC-0045 10.1.4 when available here | |
138 #TODO: we should be able to select the MUC service here | |
139 return uuid.uuid1() | |
140 | |
72 | 141 def join(self, service, roomId, nick, profile_key='@DEFAULT@'): |
142 profile = self.host.memory.getProfileName(profile_key) | |
143 if not self.__check_profile(profile): | |
144 return | |
145 room_jid = roomId+'@'+service | |
146 if self.clients[profile].joined_rooms.has_key(room_jid): | |
147 warning(_('%(profile)s is already in room %(room_jid)s') % {'profile':profile, 'room_jid':room_jid}) | |
148 return | |
149 info (_("[%(profile)s] is joining room %(room)s with nick %(nick)s") % {'profile':profile,'room':roomId+'@'+service, 'nick':nick}) | |
220
c5274bf5e18b
plugin xep 0045: workaround for MUCClient exceptions
Goffi <goffi@goffi.org>
parents:
198
diff
changeset
|
150 try: |
319 | 151 return self.clients[profile].join(service, roomId, nick).addCallbacks(self.__room_joined, self.__err_joining_room, callbackKeywords={'profile':profile}, errbackKeywords={'profile':profile}) |
220
c5274bf5e18b
plugin xep 0045: workaround for MUCClient exceptions
Goffi <goffi@goffi.org>
parents:
198
diff
changeset
|
152 except: |
c5274bf5e18b
plugin xep 0045: workaround for MUCClient exceptions
Goffi <goffi@goffi.org>
parents:
198
diff
changeset
|
153 #XXX: this is a ugly workaround as MUCClient thrown an error if there is invalid chars in the room jid (like with the default string) |
c5274bf5e18b
plugin xep 0045: workaround for MUCClient exceptions
Goffi <goffi@goffi.org>
parents:
198
diff
changeset
|
154 #FIXME: must be removed when MUCClient manage this better |
319 | 155 d = defer.Deferred() |
156 d.addErrback(self.__err_joining_room, profile) | |
157 d.errback(Exception("ugly workaround")) | |
158 return d | |
72 | 159 |
319 | 160 def _join(self, service, roomId, nick, profile_key='@DEFAULT@'): |
161 """join method used by bridge: use the _join method, but doesn't return any deferred""" | |
162 self.join(service, roomId, nick, profile_key) | |
163 | |
72 | 164 def getHandler(self, profile): |
165 self.clients[profile] = SatMUCClient(self) | |
166 return self.clients[profile] | |
167 | |
168 | |
169 | |
170 class SatMUCClient (muc.MUCClient): | |
171 #implements(iwokkel.IDisco) | |
172 | |
173 def __init__(self, plugin_parent): | |
174 self.plugin_parent = plugin_parent | |
175 self.host = plugin_parent.host | |
176 muc.MUCClient.__init__(self) | |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
177 self.joined_rooms = {} |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
178 self.rec_subjects = {} |
72 | 179 print "init SatMUCClient OK" |
180 | |
181 def receivedGroupChat(self, room, user, body): | |
182 debug('receivedGroupChat: room=%s user=%s body=%s', room, user, body) | |
183 | |
74
6e3a06b4dd36
plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents:
73
diff
changeset
|
184 def userJoinedRoom(self, room, user): |
6e3a06b4dd36
plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents:
73
diff
changeset
|
185 debug (_("user %(nick)s has joined room (%(room_id)s)") % {'nick':user.nick, 'room_id':room.occupantJID.userhost()}) |
319 | 186 if not self.host.trigger.point("MUC user joined", room, user, self.parent.profile): |
187 return | |
74
6e3a06b4dd36
plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents:
73
diff
changeset
|
188 user_data={'entity':user.entity or '', 'affiliation':user.affiliation, 'role':user.role} |
6e3a06b4dd36
plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents:
73
diff
changeset
|
189 self.host.bridge.roomUserJoined(room.roomIdentifier, room.service, user.nick, user_data, self.parent.profile) |
6e3a06b4dd36
plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents:
73
diff
changeset
|
190 |
6e3a06b4dd36
plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents:
73
diff
changeset
|
191 def userLeftRoom(self, room, user): |
6e3a06b4dd36
plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents:
73
diff
changeset
|
192 debug (_("user %(nick)s left room (%(room_id)s)") % {'nick':user.nick, 'room_id':room.occupantJID.userhost()}) |
6e3a06b4dd36
plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents:
73
diff
changeset
|
193 user_data={'entity':user.entity or '', 'affiliation':user.affiliation, 'role':user.role} |
6e3a06b4dd36
plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents:
73
diff
changeset
|
194 self.host.bridge.roomUserLeft(room.roomIdentifier, room.service, user.nick, user_data, self.parent.profile) |
72 | 195 |
77
1ae680f9682e
wix: MUC groupchat management + short nick shown in chat window instead of full jid when possible
Goffi <goffi@goffi.org>
parents:
76
diff
changeset
|
196 def userUpdatedStatus(self, room, user, show, status): |
1ae680f9682e
wix: MUC groupchat management + short nick shown in chat window instead of full jid when possible
Goffi <goffi@goffi.org>
parents:
76
diff
changeset
|
197 print("FIXME: MUC status not managed yet") |
1ae680f9682e
wix: MUC groupchat management + short nick shown in chat window instead of full jid when possible
Goffi <goffi@goffi.org>
parents:
76
diff
changeset
|
198 #FIXME: gof |
1ae680f9682e
wix: MUC groupchat management + short nick shown in chat window instead of full jid when possible
Goffi <goffi@goffi.org>
parents:
76
diff
changeset
|
199 |
198
3d7a06fe3997
plugin XEP-0045: fixed receivedSubject
Goffi <goffi@goffi.org>
parents:
183
diff
changeset
|
200 def receivedSubject(self, occupantJID, subject): |
3d7a06fe3997
plugin XEP-0045: fixed receivedSubject
Goffi <goffi@goffi.org>
parents:
183
diff
changeset
|
201 room = self._getRoom(occupantJID) |
76 | 202 debug (_("New subject for room (%(room_id)s): %(subject)s") % {'room_id':room.occupantJID.userhost(),'subject':subject}) |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
203 room_jid = room.roomIdentifier+'@'+room.service |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
204 self.rec_subjects[room_jid] = (room.roomIdentifier, room.service, subject) |
76 | 205 self.host.bridge.roomNewSubject(room.roomIdentifier, room.service, subject, self.parent.profile) |
206 | |
72 | 207 #def connectionInitialized(self): |
208 #pass | |
209 | |
210 #def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | |
211 #return [disco.DiscoFeature(NS_VCARD)] | |
212 | |
213 #def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
214 #return [] | |
215 |