annotate src/plugins/plugin_xep_0045.py @ 384:785420cd63f7

plugins: In-Band Bytestreams (XEP-0047) implementation
author Goffi <goffi@goffi.org>
date Thu, 29 Sep 2011 12:05:45 +0200
parents f964dcec1611
children 10b4f577d0c0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
1 #!/usr/bin/python
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
2 # -*- coding: utf-8 -*-
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
3
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
4 """
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
5 SAT plugin for managing xep-0045
228
b1794cbb88e5 2011 copyright upgrade
Goffi <goffi@goffi.org>
parents: 223
diff changeset
6 Copyright (C) 2009, 2010, 2011 Jérôme Poisson (goffi@goffi.org)
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
7
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
8 This program is free software: you can redistribute it and/or modify
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
9 it under the terms of the GNU General Public License as published by
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
10 the Free Software Foundation, either version 3 of the License, or
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
11 (at your option) any later version.
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
12
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
13 This program is distributed in the hope that it will be useful,
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
16 GNU General Public License for more details.
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
17
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
18 You should have received a copy of the GNU General Public License
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
20 """
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
21
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
22 from logging import debug, info, warning, error
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
23 from twisted.words.xish import domish
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
24 from twisted.internet import protocol, defer, threads, reactor
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
25 from twisted.words.protocols.jabber import client, jid, xmlstream
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
26 from twisted.words.protocols.jabber import error as jab_error
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
27 from twisted.words.protocols.jabber.xmlstream import IQ
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
28 import os.path
319
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
29 import uuid
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
30
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
31 from zope.interface import implements
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
32
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
33 from wokkel import disco, iwokkel, muc
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
34
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
35 from base64 import b64decode
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
36 from hashlib import sha1
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
37 from time import sleep
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
38
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
39 try:
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
40 from twisted.words.protocols.xmlstream import XMPPHandler
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
41 except ImportError:
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
42 from wokkel.subprotocols import XMPPHandler
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
43
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
44 PLUGIN_INFO = {
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
45 "name": "XEP 0045 Plugin",
291
7c79d4a8c9e6 plugins: fixed bad import names
Goffi <goffi@goffi.org>
parents: 279
diff changeset
46 "import_name": "XEP-0045",
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
47 "type": "XEP",
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
48 "protocols": ["XEP-0045"],
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
49 "dependencies": [],
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
50 "main": "XEP_0045",
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
51 "handler": "yes",
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
52 "description": _("""Implementation of Multi-User Chat""")
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
53 }
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
54
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
55 class XEP_0045():
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
56
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
57 def __init__(self, host):
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
58 info(_("Plugin XEP_0045 initialization"))
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
59 self.host = host
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
60 self.clients={}
372
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 350
diff changeset
61 host.bridge.addMethod("joinMUC", ".plugin", in_sign='ssss', out_sign='', method=self._join)
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 350
diff changeset
62 host.bridge.addMethod("getRoomJoined", ".plugin", in_sign='s', out_sign='a(ssass)', method=self.getRoomJoined)
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 350
diff changeset
63 host.bridge.addMethod("getRoomSubjects", ".plugin", in_sign='s', out_sign='a(sss)', method=self.getRoomSubjects)
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 350
diff changeset
64 host.bridge.addMethod("getUniqueRoomName", ".plugin", in_sign='s', out_sign='s', method=self.getUniqueName)
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 350
diff changeset
65 host.bridge.addSignal("roomJoined", ".plugin", signature='ssasss') #args: room_id, room_service, room_nicks, user_nick, profile
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 350
diff changeset
66 host.bridge.addSignal("roomUserJoined", ".plugin", signature='sssa{ss}s') #args: room_id, room_service, user_nick, user_data, profile
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 350
diff changeset
67 host.bridge.addSignal("roomUserLeft", ".plugin", signature='sssa{ss}s') #args: room_id, room_service, user_nick, user_data, profile
f964dcec1611 core: plugins refactored according to bridge + updatedValue now use profile
Goffi <goffi@goffi.org>
parents: 350
diff changeset
68 host.bridge.addSignal("roomNewSubject", ".plugin", signature='ssss') #args: room_id, room_service, subject, profile
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
69
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
70 def __check_profile(self, profile):
75
7322a41f8a8e Basic user joined/left management
Goffi <goffi@goffi.org>
parents: 74
diff changeset
71 """check if profile is used and connected
7322a41f8a8e Basic user joined/left management
Goffi <goffi@goffi.org>
parents: 74
diff changeset
72 if profile known but disconnected, remove it from known profiles
7322a41f8a8e Basic user joined/left management
Goffi <goffi@goffi.org>
parents: 74
diff changeset
73 @param profile: profile to check
7322a41f8a8e Basic user joined/left management
Goffi <goffi@goffi.org>
parents: 74
diff changeset
74 @return: True if the profile is known and connected, else False"""
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
75 if not profile or not self.clients.has_key(profile) or not self.host.isConnected(profile):
91
39c672544593 Tarot: bidding phase
Goffi <goffi@goffi.org>
parents: 78
diff changeset
76 error (_('Unknown or disconnected profile (%s)') % profile)
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
77 if self.clients.has_key(profile):
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
78 del self.clients[profile]
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
79 return False
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
80 return True
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
81
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
82 def __room_joined(self, room, profile):
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
83 """Called when the user is in the requested room"""
319
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
84 def _sendBridgeSignal(ignore=None):
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
85 self.host.bridge.roomJoined(room.roomIdentifier, room.service, [user.nick for user in room.roster.values()], room.nick, profile)
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
86
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
87 room_jid = room.roomIdentifier+'@'+room.service
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
88 self.clients[profile].joined_rooms[room_jid] = room
319
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
89 if room.status == '201':
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
90 #FIXME: the current behaviour is to create an instant room
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
91 #and send the signal only when the room is unlocked
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
92 #a proper configuration management should be done
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
93 #TODO: wokkel's muc currently doesn't manage correctly message from the room
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
94 # service (without resource) in room.getUser
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
95 self.clients[profile].configure(room_jid).addCallbacks(_sendBridgeSignal, lambda x: error(_('Error while configuring the room')))
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
96 else:
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
97 _sendBridgeSignal()
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
98 return room
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
99
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
100
75
7322a41f8a8e Basic user joined/left management
Goffi <goffi@goffi.org>
parents: 74
diff changeset
101 def __err_joining_room(self, failure, profile):
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
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
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
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
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 91
diff changeset
117 def getRoomNick(self, room_jid, profile_key='@DEFAULT@'):
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 91
diff changeset
118 """return nick used in room by user
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 91
diff changeset
119 @param room_jid: unicode room id
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 91
diff changeset
120 @profile_key: profile
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 91
diff changeset
121 @return: nick or empty string in case of error"""
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 91
diff changeset
122 profile = self.host.memory.getProfileName(profile_key)
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 91
diff changeset
123 if not self.__check_profile(profile) or not self.clients[profile].joined_rooms.has_key(room_jid):
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 91
diff changeset
124 return ''
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 91
diff changeset
125 return self.clients[profile].joined_rooms[room_jid].nick
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 91
diff changeset
126
2f87651a5ad8 Tarot game: basic trick
Goffi <goffi@goffi.org>
parents: 91
diff changeset
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
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
135 def getUniqueName(self, profile_key='@DEFAULT@'):
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
136 """Return unique name for room, avoiding collision"""
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
137 #TODO: we should use #RFC-0045 10.1.4 when available here
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
138 #TODO: we should be able to select the MUC service here
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
139 return uuid.uuid1()
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
140
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
141 def join(self, service, roomId, nick, profile_key='@DEFAULT@'):
350
abe08fcb42d7 plugin XEP-0045: added error callback to join's deferred, and a callback is created if join fail before calling MUCClient's join
Goffi <goffi@goffi.org>
parents: 319
diff changeset
142 def _errDeferred(exc_obj = Exception, txt='Error while joining room'):
abe08fcb42d7 plugin XEP-0045: added error callback to join's deferred, and a callback is created if join fail before calling MUCClient's join
Goffi <goffi@goffi.org>
parents: 319
diff changeset
143 d = defer.Deferred()
abe08fcb42d7 plugin XEP-0045: added error callback to join's deferred, and a callback is created if join fail before calling MUCClient's join
Goffi <goffi@goffi.org>
parents: 319
diff changeset
144 d.errback(exc_obj(txt))
abe08fcb42d7 plugin XEP-0045: added error callback to join's deferred, and a callback is created if join fail before calling MUCClient's join
Goffi <goffi@goffi.org>
parents: 319
diff changeset
145
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
146 profile = self.host.memory.getProfileName(profile_key)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
147 if not self.__check_profile(profile):
350
abe08fcb42d7 plugin XEP-0045: added error callback to join's deferred, and a callback is created if join fail before calling MUCClient's join
Goffi <goffi@goffi.org>
parents: 319
diff changeset
148 return _errDeferred()
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
149 room_jid = roomId+'@'+service
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
150 if self.clients[profile].joined_rooms.has_key(room_jid):
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
151 warning(_('%(profile)s is already in room %(room_jid)s') % {'profile':profile, 'room_jid':room_jid})
350
abe08fcb42d7 plugin XEP-0045: added error callback to join's deferred, and a callback is created if join fail before calling MUCClient's join
Goffi <goffi@goffi.org>
parents: 319
diff changeset
152 return _errDeferred()
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
153 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
154 try:
319
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
155 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
156 except:
c5274bf5e18b plugin xep 0045: workaround for MUCClient exceptions
Goffi <goffi@goffi.org>
parents: 198
diff changeset
157 #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
158 #FIXME: must be removed when MUCClient manage this better
350
abe08fcb42d7 plugin XEP-0045: added error callback to join's deferred, and a callback is created if join fail before calling MUCClient's join
Goffi <goffi@goffi.org>
parents: 319
diff changeset
159 d = _errDeferred(txt="ugly workaround")
319
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
160 d.addErrback(self.__err_joining_room, profile)
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
161 return d
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
162
319
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
163 def _join(self, service, roomId, nick, profile_key='@DEFAULT@'):
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
164 """join method used by bridge: use the _join method, but doesn't return any deferred"""
350
abe08fcb42d7 plugin XEP-0045: added error callback to join's deferred, and a callback is created if join fail before calling MUCClient's join
Goffi <goffi@goffi.org>
parents: 319
diff changeset
165 d = self.join(service, roomId, nick, profile_key)
abe08fcb42d7 plugin XEP-0045: added error callback to join's deferred, and a callback is created if join fail before calling MUCClient's join
Goffi <goffi@goffi.org>
parents: 319
diff changeset
166 d.addErrback(lambda x: warning(_('Error while joining room'))) #TODO: error management + signal in bridge
319
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
167
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
168 def getHandler(self, profile):
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
169 self.clients[profile] = SatMUCClient(self)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
170 return self.clients[profile]
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
171
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
172
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
173
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
174 class SatMUCClient (muc.MUCClient):
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
175 #implements(iwokkel.IDisco)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
176
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
177 def __init__(self, plugin_parent):
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
178 self.plugin_parent = plugin_parent
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
179 self.host = plugin_parent.host
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
180 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
181 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
182 self.rec_subjects = {}
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
183 print "init SatMUCClient OK"
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
184
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
185 def receivedGroupChat(self, room, user, body):
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
186 debug('receivedGroupChat: room=%s user=%s body=%s', room, user, body)
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
187
74
6e3a06b4dd36 plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents: 73
diff changeset
188 def userJoinedRoom(self, room, user):
6e3a06b4dd36 plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents: 73
diff changeset
189 debug (_("user %(nick)s has joined room (%(room_id)s)") % {'nick':user.nick, 'room_id':room.occupantJID.userhost()})
319
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
190 if not self.host.trigger.point("MUC user joined", room, user, self.parent.profile):
5bb1cfc105d0 plugin xep-0045: misc improvments
Goffi <goffi@goffi.org>
parents: 291
diff changeset
191 return
74
6e3a06b4dd36 plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents: 73
diff changeset
192 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
193 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
194
6e3a06b4dd36 plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents: 73
diff changeset
195 def userLeftRoom(self, room, user):
6e3a06b4dd36 plugin xep-0045: added roomUserJoined and roomUserLeft signals
Goffi <goffi@goffi.org>
parents: 73
diff changeset
196 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
197 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
198 self.host.bridge.roomUserLeft(room.roomIdentifier, room.service, user.nick, user_data, self.parent.profile)
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
199
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
200 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
201 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
202 #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
203
198
3d7a06fe3997 plugin XEP-0045: fixed receivedSubject
Goffi <goffi@goffi.org>
parents: 183
diff changeset
204 def receivedSubject(self, occupantJID, subject):
3d7a06fe3997 plugin XEP-0045: fixed receivedSubject
Goffi <goffi@goffi.org>
parents: 183
diff changeset
205 room = self._getRoom(occupantJID)
76
8becde8a967c MUC: added subject management
Goffi <goffi@goffi.org>
parents: 75
diff changeset
206 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
207 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
208 self.rec_subjects[room_jid] = (room.roomIdentifier, room.service, subject)
76
8becde8a967c MUC: added subject management
Goffi <goffi@goffi.org>
parents: 75
diff changeset
209 self.host.bridge.roomNewSubject(room.roomIdentifier, room.service, subject, self.parent.profile)
8becde8a967c MUC: added subject management
Goffi <goffi@goffi.org>
parents: 75
diff changeset
210
72
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
211 #def connectionInitialized(self):
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
212 #pass
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
213
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
214 #def getDiscoInfo(self, requestor, target, nodeIdentifier=''):
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
215 #return [disco.DiscoFeature(NS_VCARD)]
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
216
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
217 #def getDiscoItems(self, requestor, target, nodeIdentifier=''):
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
218 #return []
f271fff3a713 MUC implementation: first draft
Goffi <goffi@goffi.org>
parents:
diff changeset
219