diff --git a/group.py b/group.py index ca403a9..c612473 100644 --- a/group.py +++ b/group.py @@ -32,4 +32,4 @@ class Group(): self.joined = False self.nick = "me" - self.participants = { } + self.participants = [] diff --git a/session.py b/session.py index 78e9985..399e861 100644 --- a/session.py +++ b/session.py @@ -484,12 +484,34 @@ class Session(YowsupApp): self.bot.send("You have been added to group: %s@%s (%s)" % (self._shortenGroupId(room), subject, self.backend.spectrum_jid)) + # Called by superclass def onParticipantsAddedToGroup(self, group): self.logger.debug("Participants added to group: %s", group) room = group.getGroupId().split('@')[0] self.groups[room].participants.extend(group.getParticipants()) self._refreshParticipants(room) + # Called by superclass + def onParticipantsRemovedFromGroup(self, room, participants): + self.logger.debug("Participants removed from group: %s, %s", + room, participants) + group = self.groups[room] + for jid in participants: + group.participants.remove(jid) + buddy = jid.split("@")[0] + try: + nick = self.buddies[buddy].nick + except KeyError: + nick = buddy + if nick == "": + nick = buddy + if buddy == self.legacyName: + nick = group.nick + flags = protocol_pb2.PARTICIPANT_FLAG_NONE + self.backend.handleParticipantChanged( + self.user, nick, self._shortenGroupId(room), flags, + protocol_pb2.STATUS_NONE, buddy) + def onPresenceReceived(self, _type, name, jid, lastseen): self.logger.info("Presence received: %s %s %s %s", _type, name, jid, lastseen) buddy = jid.split("@")[0] @@ -643,7 +665,6 @@ class Session(YowsupApp): group = self.groups[room] for jid in group.participants: buddy = jid.split("@")[0] - self.logger.info("Added %s to room %s", buddy, room) try: nick = self.buddies[buddy].nick except KeyError: diff --git a/yowsupwrapper.py b/yowsupwrapper.py index 4de5be9..ef1928b 100644 --- a/yowsupwrapper.py +++ b/yowsupwrapper.py @@ -501,6 +501,15 @@ class YowsupApp(object): """Called when participants have been added to a group""" pass + def onParticipantsRemovedFromGroup(self, group, participants): + """Called when participants have been removed from a group + + Args: + - group: (str) id of the group (e.g. 27831788123-144024456) + - participants: (list) jids of participants that are removed + """ + pass + def sendEntity(self, entity): """Sends an entity down the stack (as if YowsupAppLayer called toLower)""" self.stack.broadcastEvent(YowLayerEvent(YowsupAppLayer.TO_LOWER_EVENT, @@ -596,12 +605,17 @@ class YowsupAppLayer(YowInterfaceLayer): """ Sends ack automatically """ - self.logger.debug("Received notification: %s", entity) + self.logger.debug("Received notification (%s): %s", type(entity), entity) self.toLower(entity.ack()) if isinstance(entity, CreateGroupsNotificationProtocolEntity): self.caller.onAddedToGroup(entity) elif isinstance(entity, AddGroupsNotificationProtocolEntity): self.caller.onParticipantsAddedToGroup(entity) + elif isinstance(entity, RemoveGroupsNotificationProtocolEntity): + self.caller.onParticipantsRemovedFromGroup( + entity.getGroupId().split('@')[0], + entity.getParticipants().keys() + ) @ProtocolEntityCallback('message') def onMessageReceived(self, entity):