class MucRoomUser: """ Describes a user of a MUC room. The attributes of this object should not be changed directly. :Ivariables: - `presence`: last presence stanza received for the user. - `role`: user's role. - `affiliation`: user's affiliation. - `room_jid`: user's room jid. - `real_jid`: user's real jid or None if not available. - `nick`: user's nick (resource part of `room_jid`) :Types: - `presence`: `MucPresence` - `role`: `str` - `affiliation`: `str` - `room_jid`: `JID` - `real_jid`: `JID` - `nick`: `unicode` """ def __init__(self,presence_or_user_or_jid): """ Initialize a `MucRoomUser` object. :Parameters: - `presence_or_user_or_jid`: a MUC presence stanza with user information, a user object to copy or a room JID of a user. :Types: - `presence_or_user_or_jid`: `MucPresence` or `MucRoomUser` or `JID` When `presence_or_user_or_jid` is a JID user's role and affiliation are set to "none". """ if isinstance(presence_or_user_or_jid,MucRoomUser): self.presence=presence_or_user_or_jid.presence self.role=presence_or_user_or_jid.role self.affiliation=presence_or_user_or_jid.affiliation self.room_jid=presence_or_user_or_jid.room_jid self.real_jid=presence_or_user_or_jid.real_jid self.nick=presence_or_user_or_jid.nick self.new_nick=None else: self.affiliation="none" self.presence=None self.real_jid=None self.new_nick=None if isinstance(presence_or_user_or_jid,JID): self.nick=presence_or_user_or_jid.resource self.room_jid=presence_or_user_or_jid self.role="none" elif isinstance(presence_or_user_or_jid,Presence): self.nick=None self.room_jid=None self.role="participant" self.update_presence(presence_or_user_or_jid) else: raise TypeError,"Bad argument type for MucRoomUser constructor" def update_presence(self,presence): """ Update user information. :Parameters: - `presence`: a presence stanza with user information update. :Types: - `presence`: `MucPresence` """ self.presence=MucPresence(presence) t=presence.get_type() if t=="unavailable": self.role="none" self.affiliation="none" self.room_jid=self.presence.get_from() self.nick=self.room_jid.resource mc=self.presence.get_muc_child() if isinstance(mc,MucUserX): items=mc.get_items() for item in items: if not isinstance(item,MucItem): continue if item.role: self.role=item.role if item.affiliation: self.affiliation=item.affiliation if item.jid: self.real_jid=item.jid if item.nick: self.new_nick=item.nick break def same_as(self,other): """Check if two `MucRoomUser` objects describe the same user in the same room. :Parameters: - `other`: the user object to compare `self` with. :Types: - `other`: `MucRoomUser` :return: `True` if the two object describe the same user. :returntype: `bool`""" return self.room_jid==other.room_jid
class MucRoomUser: """ Describes a user of a MUC room. The attributes of this object should not be changed directly. :Ivariables: - `presence`: last presence stanza received for the user. - `role`: user's role. - `affiliation`: user's affiliation. - `room_jid`: user's room jid. - `real_jid`: user's real jid or None if not available. - `nick`: user's nick (resource part of `room_jid`) :Types: - `presence`: `MucPresence` - `role`: `str` - `affiliation`: `str` - `room_jid`: `JID` - `real_jid`: `JID` - `nick`: `unicode` """ def __init__(self, presence_or_user_or_jid): """ Initialize a `MucRoomUser` object. :Parameters: - `presence_or_user_or_jid`: a MUC presence stanza with user information, a user object to copy or a room JID of a user. :Types: - `presence_or_user_or_jid`: `MucPresence` or `MucRoomUser` or `JID` When `presence_or_user_or_jid` is a JID user's role and affiliation are set to "none". """ if isinstance(presence_or_user_or_jid, MucRoomUser): self.presence = presence_or_user_or_jid.presence self.role = presence_or_user_or_jid.role self.affiliation = presence_or_user_or_jid.affiliation self.room_jid = presence_or_user_or_jid.room_jid self.real_jid = presence_or_user_or_jid.real_jid self.nick = presence_or_user_or_jid.nick self.new_nick = None else: self.affiliation = "none" self.presence = None self.real_jid = None self.new_nick = None if isinstance(presence_or_user_or_jid, JID): self.nick = presence_or_user_or_jid.resource self.room_jid = presence_or_user_or_jid self.role = "none" elif isinstance(presence_or_user_or_jid, Presence): self.nick = None self.room_jid = None self.role = "participant" self.update_presence(presence_or_user_or_jid) else: raise TypeError, "Bad argument type for MucRoomUser constructor" def update_presence(self, presence): """ Update user information. :Parameters: - `presence`: a presence stanza with user information update. :Types: - `presence`: `MucPresence` """ self.presence = MucPresence(presence) t = presence.get_type() if t == "unavailable": self.role = "none" self.affiliation = "none" self.room_jid = self.presence.get_from() self.nick = self.room_jid.resource mc = self.presence.get_muc_child() if isinstance(mc, MucUserX): items = mc.get_items() for item in items: if not isinstance(item, MucItem): continue if item.role: self.role = item.role if item.affiliation: self.affiliation = item.affiliation if item.jid: self.real_jid = item.jid if item.nick: self.new_nick = item.nick break def same_as(self, other): """Check if two `MucRoomUser` objects describe the same user in the same room. :Parameters: - `other`: the user object to compare `self` with. :Types: - `other`: `MucRoomUser` :return: `True` if the two object describe the same user. :returntype: `bool`""" return self.room_jid == other.room_jid