Changeset 153:bf4b940f3547 for wokkel/muc.py
- Timestamp:
- Sep 2, 2011, 9:56:49 AM (11 years ago)
- Branch:
- wokkel-muc-client-support-24
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
wokkel/muc.py
r152 r153 356 356 """ 357 357 358 statusCode = None 358 affiliation = None 359 role = None 360 entity = None 361 nick = None 362 363 statusCodes = None 359 364 360 365 childParsers = {(NS_MUC_USER, 'x'): '_childParser_mucUser'} 361 366 362 367 def _childParser_mucUser(self, element): 368 statusCodes = set() 369 363 370 for child in element.elements(): 364 371 if child.uri != NS_MUC_USER: 365 372 continue 373 366 374 elif child.name == 'status': 367 self.statusCode = child.getAttribute('code') 368 # TODO: item, destroy 369 375 try: 376 statusCode = int(child.getAttribute('code')) 377 except (TypeError, ValueError): 378 continue 379 380 statusCodes.add(statusCode) 381 382 elif child.name == 'item': 383 if child.hasAttribute('jid'): 384 self.entity = jid.JID(child['jid']) 385 386 self.nick = child.getAttribute('nick') 387 self.affiliation = child.getAttribute('affiliation') 388 self.role = child.getAttribute('role') 389 390 for reason in child.elements(NS_MUC_ADMIN, 'reason'): 391 self.reason = unicode(reason) 392 393 # TODO: destroy 394 395 if statusCodes: 396 self.statusCodes = statusCodes 370 397 371 398 … … 496 523 query = "/presence[@from='%s' or (@from='%s' and @type='error')]" % ( 497 524 stanza.recipient.full(), stanza.recipient.userhost()) 498 self.xmlstream.addOnetimeObserver(query, onResponse, priority= 1)525 self.xmlstream.addOnetimeObserver(query, onResponse, priority=-1) 499 526 self.xmlstream.send(stanza.toElement()) 500 527 return d … … 1041 1068 a client. 1042 1069 1043 @ivar roomIdentifier: The Room ID of the MUC room. 1044 @type roomIdentifier: C{unicode} 1045 1046 @ivar service: The server where the MUC room is located. 1047 @type service: C{unicode} 1070 @ivar roomJID: The Room JID of the MUC room. 1071 @type roomJID: L{JID} 1048 1072 1049 1073 @ivar nick: The nick name for the client in this room. … … 1054 1078 1055 1079 @ivar occupantJID: The JID of the occupant in the room. Generated from 1056 room Identifier, service,and nick.1080 roomJID and nick. 1057 1081 @type occupantJID: L{jid.JID} 1058 1082 """ 1059 1083 1060 1084 1061 def __init__(self, room Identifier, service, nick, state=None):1085 def __init__(self, roomJID, nick, state=None): 1062 1086 """ 1063 1087 Initialize the room. 1064 1088 """ 1065 self.roomIdentifier = roomIdentifier 1066 self.service = service 1089 self.roomJID = roomJID 1067 1090 self.setNick(nick) 1068 1091 self.state = state … … 1074 1097 1075 1098 def setNick(self, nick): 1076 self.occupantJID = jid.internJID(u"%s@%s/%s" % (self.roomIdentifier, 1077 self.service, 1078 nick)) 1099 self.occupantJID = jid.internJID(u"%s/%s" % (self.roomJID, nick)) 1079 1100 self.nick = nick 1080 1101 … … 1168 1189 1169 1190 1170 def _removeRoom(self, occupantJID):1191 def _removeRoom(self, roomJID): 1171 1192 """ 1172 1193 Delete a room from the room collection. 1173 1194 """ 1174 roomJID = occupantJID.userhostJID()1175 1195 if roomJID in self._rooms: 1176 1196 del self._rooms[roomJID] 1177 1197 1178 1198 1179 def unavailableReceived(self, presence): 1180 """ 1181 Unavailable presence was received. 1182 1183 If this was received from a MUC room occupant JID, that occupant has 1184 left the room. 1185 """ 1186 1187 occupantJID = presence.sender 1188 1189 if occupantJID: 1190 self._userLeavesRoom(occupantJID) 1191 1192 1193 def errorReceived(self, presence): 1194 """ 1195 Error presence was received. 1196 1197 If this was received from a MUC room occupant JID, we conclude the 1198 occupant has left the room. 1199 """ 1200 occupantJID = presence.sender 1201 1202 if occupantJID: 1203 self._userLeavesRoom(occupantJID) 1204 1205 1206 def _userLeavesRoom(self, occupantJID): 1199 def _getRoomUser(self, stanza): 1200 """ 1201 Lookup the room and user associated with the stanza's sender. 1202 """ 1203 occupantJID = stanza.sender 1204 1205 if not occupantJID: 1206 return None, None 1207 1207 1208 # when a user leaves a room we need to update it 1208 1209 room = self._getRoom(occupantJID.userhostJID()) 1209 1210 if room is None: 1210 1211 # not in the room yet 1212 return None, None 1213 1214 # Check if user is in roster 1215 nick = occupantJID.resource 1216 user = room.getUser(nick) 1217 1218 return room, user 1219 1220 1221 def unavailableReceived(self, presence): 1222 """ 1223 Unavailable presence was received. 1224 1225 If this was received from a MUC room occupant JID, that occupant has 1226 left the room. 1227 """ 1228 1229 room, user = self._getRoomUser(presence) 1230 1231 if room is None or user is None: 1211 1232 return 1212 # check if user is in roster 1213 user = room.getUser(occupantJID.resource) 1233 1234 room.removeUser(user) 1235 self.userLeftRoom(room, user) 1236 1237 1238 def availableReceived(self, presence): 1239 """ 1240 Available presence was received. 1241 """ 1242 1243 room, user = self._getRoomUser(presence) 1244 1245 if room is None: 1246 return 1247 1214 1248 if user is None: 1215 return 1249 nick = presence.sender.resource 1250 user = User(nick, presence.entity) 1251 1252 # Update user status 1253 user.status = presence.status 1254 user.show = presence.show 1255 1216 1256 if room.inRoster(user): 1217 room.removeUser(user) 1218 self.userLeftRoom(room, user) 1219 1220 1221 def availableReceived(self, presence): 1222 """ 1223 Available presence was received. 1224 """ 1225 1226 occupantJID = presence.sender 1227 1228 if not occupantJID: 1229 return 1230 1231 # grab room 1232 room = self._getRoom(occupantJID.userhostJID()) 1233 if room is None: 1234 # not in the room yet 1235 return 1236 1237 user = self._changeUserStatus(room, occupantJID, presence.status, 1238 presence.show) 1239 1240 if room.inRoster(user): 1241 # we changed status or nick 1242 if presence.statusCode: 1243 room.status = presence.statusCode # XXX 1244 else: 1245 self.userUpdatedStatus(room, user, presence.show, 1246 presence.status) 1257 self.userUpdatedStatus(room, user, presence.show, presence.status) 1247 1258 else: 1248 1259 room.addUser(user) … … 1257 1268 L{receivedGroupChat}, L{receivedHistory} or L{receivedHistory}. 1258 1269 """ 1259 occupantJID = message.sender 1260 if not occupantJID: 1270 room, user = self._getRoomUser(message) 1271 1272 if room is None: 1261 1273 return 1262 1263 roomJID = occupantJID.userhostJID()1264 1265 room = self._getRoom(roomJID)1266 if room is None:1267 # not in the room yet1268 return1269 1270 if occupantJID.resource:1271 user = room.getUser(occupantJID.resource)1272 else:1273 # This message is from the room itself.1274 user = None1275 1274 1276 1275 if message.subject: … … 1282 1281 1283 1282 1284 def _joinedRoom(self, presence):1285 """1286 We have presence that says we joined a room.1287 """1288 roomJID = presence.sender.userhostJID()1289 1290 # change the state of the room1291 room = self._getRoom(roomJID)1292 room.state = 'joined'1293 1294 # grab status1295 if presence.statusCode:1296 room.status = presence.statusCode1297 1298 return room1299 1300 1301 def _leftRoom(self, presence):1302 """1303 We have presence that says we left a room.1304 """1305 occupantJID = presence.sender1306 1307 # change the state of the room1308 self._removeRoom(occupantJID)1309 1310 return True1311 1312 1313 1283 def userJoinedRoom(self, room, user): 1314 1284 """ … … 1353 1323 1354 1324 1355 def receivedSubject(self, room, subject): 1356 """ 1325 def receivedSubject(self, room, user, subject): 1326 """ 1327 A (new) room subject has been received. 1328 1357 1329 This method will need to be modified inorder for clients to 1358 1330 do something when this event occurs. … … 1401 1373 def join(self, roomJID, nick, historyOptions=None, 1402 1374 password=None): 1403 room = Room(roomJID.user, roomJID.host, nick, state='joining') 1375 """ 1376 Join a MUC room by sending presence to it. 1377 1378 @param roomJID: The JID of the room the entity is joining. 1379 @type roomJID: L{jid.JID} 1380 1381 @param nick: The nick name for the entitity joining the room. 1382 @type nick: C{unicode} 1383 1384 @param historyOptions: Options for conversation history sent by the 1385 room upon joining. 1386 @type historyOptions: L{HistoryOptions} 1387 1388 @param password: Optional password for the room. 1389 @type password: C{unicode} 1390 1391 @return: A deferred that fires with the room when the entity is in the 1392 room, or with a failure if an error has occurred. 1393 """ 1394 def cb(presence): 1395 """ 1396 We have presence that says we joined a room. 1397 """ 1398 room.state = 'joined' 1399 return room 1400 1401 def eb(failure): 1402 self._removeRoom(roomJID) 1403 return failure 1404 1405 room = Room(roomJID, nick, state='joining') 1404 1406 self._addRoom(room) 1405 1407 1406 1408 d = MUCClientProtocol.join(self, roomJID, nick, historyOptions, 1407 1409 password) 1408 d.addCallback (self._joinedRoom)1410 d.addCallbacks(cb, eb) 1409 1411 return d 1410 1411 1412 def _changeUserStatus(self, room, occupantJID, status, show):1413 """1414 Change the user status in a room.1415 """1416 1417 # check if user is in roster1418 user = room.getUser(occupantJID.resource)1419 if user is None: # create a user that does not exist1420 user = User(occupantJID.resource)1421 1422 if status is not None:1423 user.status = unicode(status)1424 if show is not None:1425 user.show = unicode(show)1426 1427 return user1428 1429 1430 def _changed(self, presence, occupantJID):1431 """1432 Callback for changing the nick and status.1433 """1434 room = self._getRoom(occupantJID.userhostJID())1435 self._changeUserStatus(room, occupantJID, presence.status, presence.show)1436 1437 return room1438 1412 1439 1413 … … 1450 1424 @type nick: C{unicode} 1451 1425 """ 1426 def cb(presence): 1427 # Presence confirmation, change the nickname. 1428 room.setNick(nick) 1429 return room 1430 1452 1431 room = self._getRoom(roomJID) 1453 1432 1454 # Change the nickname1455 room.setNick(nick)1456 1457 1433 d = MUCClientProtocol.nick(self, roomJID, nick) 1458 d.addCallback( self._changed, room.occupantJID)1434 d.addCallback(cb) 1459 1435 return d 1460 1436 … … 1469 1445 @type roomJID: L{jid.JID} 1470 1446 """ 1447 def cb(presence): 1448 self._removeRoom(roomJID) 1449 1471 1450 d = MUCClientProtocol.leave(self, roomJID) 1472 d.addCallback( self._leftRoom)1451 d.addCallback(cb) 1473 1452 return d 1474 1453 … … 1492 1471 room = self._getRoom(roomJID) 1493 1472 d = MUCClientProtocol.status(self, roomJID, show, status) 1494 d.addCallback( self._changed, room.occupantJID)1473 d.addCallback(lambda _: room) 1495 1474 return d 1496 1475
Note: See TracChangeset
for help on using the changeset viewer.