Mercurial > libervia-backend
annotate src/plugins/plugin_xep_0045.py @ 224:9c6ee3f9ab29
files reorganisation
author | Goffi <goffi@goffi.org> |
---|---|
date | Tue, 04 Jan 2011 19:30:27 +0100 |
parents | 86d249b6d9b7 |
children | b1794cbb88e5 |
rev | line source |
---|---|
72 | 1 #!/usr/bin/python |
2 # -*- coding: utf-8 -*- | |
3 | |
4 """ | |
5 SAT plugin for managing xep-0045 | |
6 Copyright (C) 2009, 2010 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, 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 | |
29 import pdb | |
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 AVATAR_PATH = "/avatars" | |
45 | |
46 IQ_GET = '/iq[@type="get"]' | |
47 NS_VCARD = 'vcard-temp' | |
48 VCARD_REQUEST = IQ_GET + '/vCard[@xmlns="' + NS_VCARD + '"]' #TODO: manage requests | |
49 | |
50 PRESENCE = '/presence' | |
51 NS_VCARD_UPDATE = 'vcard-temp:x:update' | |
52 VCARD_UPDATE = PRESENCE + '/x[@xmlns="' + NS_VCARD_UPDATE + '"]' | |
53 | |
54 PLUGIN_INFO = { | |
55 "name": "XEP 0045 Plugin", | |
56 "import_name": "XEP_0045", | |
57 "type": "XEP", | |
58 "protocols": ["XEP-0045"], | |
59 "dependencies": [], | |
60 "main": "XEP_0045", | |
61 "handler": "yes", | |
62 "description": _("""Implementation of Multi-User Chat""") | |
63 } | |
64 | |
65 class XEP_0045(): | |
66 | |
67 def __init__(self, host): | |
68 info(_("Plugin XEP_0045 initialization")) | |
69 self.host = host | |
70 self.clients={} | |
71 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
|
72 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
|
73 host.bridge.addMethod("getRoomSubjects", ".communication", in_sign='s', out_sign='a(sss)', method=self.getRoomSubjects) |
73 | 74 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
|
75 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
|
76 host.bridge.addSignal("roomUserLeft", ".communication", signature='sssa{ss}s') #args: room_id, room_service, user_nick, user_data, profile |
76 | 77 host.bridge.addSignal("roomNewSubject", ".communication", signature='ssss') #args: room_id, room_service, subject, profile |
72 | 78 |
79 def __check_profile(self, profile): | |
75 | 80 """check if profile is used and connected |
81 if profile known but disconnected, remove it from known profiles | |
82 @param profile: profile to check | |
83 @return: True if the profile is known and connected, else False""" | |
72 | 84 if not profile or not self.clients.has_key(profile) or not self.host.isConnected(profile): |
91 | 85 error (_('Unknown or disconnected profile (%s)') % profile) |
72 | 86 if self.clients.has_key(profile): |
87 del self.clients[profile] | |
88 return False | |
89 return True | |
90 | |
91 def __room_joined(self, room, profile): | |
92 """Called when the user is in the requested room""" | |
93 room_jid = room.roomIdentifier+'@'+room.service | |
94 self.clients[profile].joined_rooms[room_jid] = room | |
75 | 95 self.host.bridge.roomJoined(room.roomIdentifier, room.service, [user.nick for user in room.roster.values()], room.nick, profile) |
72 | 96 |
75 | 97 def __err_joining_room(self, failure, profile): |
72 | 98 """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
|
99 mess = _("Error when joining the room") |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
134
diff
changeset
|
100 error (mess) |
9ee4a1d0d7fb
Added auto(dis)connect params + misc
Goffi <goffi@goffi.org>
parents:
134
diff
changeset
|
101 self.host.bridge.newAlert(mess, _("Group chat error"), "ERROR", profile) |
72 | 102 |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
103 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
|
104 """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
|
105 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
|
106 result = [] |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
107 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
|
108 return result |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
109 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
|
110 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
|
111 return result |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
112 |
93 | 113 def getRoomNick(self, room_jid, profile_key='@DEFAULT@'): |
114 """return nick used in room by user | |
115 @param room_jid: unicode room id | |
116 @profile_key: profile | |
117 @return: nick or empty string in case of error""" | |
118 profile = self.host.memory.getProfileName(profile_key) | |
119 if not self.__check_profile(profile) or not self.clients[profile].joined_rooms.has_key(room_jid): | |
120 return '' | |
121 return self.clients[profile].joined_rooms[room_jid].nick | |
122 | |
123 | |
78
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
124 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
|
125 """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
|
126 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
|
127 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
|
128 return [] |
ace2af8abc5a
Added method to know which MUC are joined, and which subjects were received.
Goffi <goffi@goffi.org>
parents:
77
diff
changeset
|
129 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
|
130 |
72 | 131 def join(self, service, roomId, nick, profile_key='@DEFAULT@'): |
132 profile = self.host.memory.getProfileName(profile_key) | |
133 if not self.__check_profile(profile): | |
134 return | |
135 room_jid = roomId+'@'+service | |
136 if self.clients[profile].joined_rooms.has_key(room_jid): | |
137 warning(_('%(profile)s is already in room %(room_jid)s') % {'profile':profile, 'room_jid':room_jid}) | |
138 return | |
139 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
|
140 try: |
c5274bf5e18b
plugin xep 0045: workaround for MUCClient exceptions
Goffi <goffi@goffi.org>
parents:
198
diff
changeset
|
141 self.clients[profile].join(service, roomId, nick).addCallbacks(self.__room_joined, self.__err_joining_room, callbackKeywords={'profile':profile}, errbackKeywords={'profile':profile}) |
c5274bf5e18b
plugin xep 0045: workaround for MUCClient exceptions
Goffi <goffi@goffi.org>
parents:
198
diff
changeset
|
142 except: |
c5274bf5e18b
plugin xep 0045: workaround for MUCClient exceptions
Goffi <goffi@goffi.org>
parents:
198
diff
changeset
|
143 #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
|
144 #FIXME: must be removed when MUCClient manage this better |
c5274bf5e18b
plugin xep 0045: workaround for MUCClient exceptions
Goffi <goffi@goffi.org>
parents:
198
diff
changeset
|
145 self.__err_joining_room(None, profile) |
72 | 146 |
147 def getHandler(self, profile): | |
148 #reactor.callLater(15,self.join,"conference.necton2.int", "test", "Goffi \o/", profile) | |
149 self.clients[profile] = SatMUCClient(self) | |
150 return self.clients[profile] | |
151 | |
152 | |
153 | |
154 class SatMUCClient (muc.MUCClient): | |
155 #implements(iwokkel.IDisco) | |
156 | |
157 def __init__(self, plugin_parent): | |
158 self.plugin_parent = plugin_parent | |
159 self.host = plugin_parent.host | |
160 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
|
161 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
|
162 self.rec_subjects = {} |
72 | 163 print "init SatMUCClient OK" |
164 | |
165 def receivedGroupChat(self, room, user, body): | |
166 debug('receivedGroupChat: room=%s user=%s body=%s', room, user, body) | |
167 | |
74
6e3a06b4dd36
plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents:
73
diff
changeset
|
168 def userJoinedRoom(self, room, user): |
6e3a06b4dd36
plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents:
73
diff
changeset
|
169 debug (_("user %(nick)s has joined 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
|
170 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
|
171 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
|
172 |
6e3a06b4dd36
plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents:
73
diff
changeset
|
173 def userLeftRoom(self, room, user): |
6e3a06b4dd36
plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents:
73
diff
changeset
|
174 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
|
175 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
|
176 self.host.bridge.roomUserLeft(room.roomIdentifier, room.service, user.nick, user_data, self.parent.profile) |
72 | 177 |
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
|
178 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
|
179 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
|
180 #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
|
181 |
198
3d7a06fe3997
plugin XEP-0045: fixed receivedSubject
Goffi <goffi@goffi.org>
parents:
183
diff
changeset
|
182 def receivedSubject(self, occupantJID, subject): |
3d7a06fe3997
plugin XEP-0045: fixed receivedSubject
Goffi <goffi@goffi.org>
parents:
183
diff
changeset
|
183 room = self._getRoom(occupantJID) |
76 | 184 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
|
185 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
|
186 self.rec_subjects[room_jid] = (room.roomIdentifier, room.service, subject) |
76 | 187 self.host.bridge.roomNewSubject(room.roomIdentifier, room.service, subject, self.parent.profile) |
188 | |
72 | 189 #def connectionInitialized(self): |
190 #pass | |
191 | |
192 #def getDiscoInfo(self, requestor, target, nodeIdentifier=''): | |
193 #return [disco.DiscoFeature(NS_VCARD)] | |
194 | |
195 #def getDiscoItems(self, requestor, target, nodeIdentifier=''): | |
196 #return [] | |
197 |