Exemplo n.º 1
0
    def _callback_presence(self, conn, presence):
        """
        Presence callback function. useful to trigger events based on presence
        @param conn: Connection handle
        @type conn: Object
        @param presence: Presence notification
        @type presence: Object
        """
        self._lastping = time()

        oMsg = CNBMessage()
        oMsg.protocol = self._botConfig.get('bot', 'type')
        oMsg.conId = self._botConfig.get('bot', 'id')
        oMsg.jid = str(presence.getFrom())
        oMsg.error = presence.getError()
        oMsg.presType = smart_str(presence.getType())
        oMsg.presShow = smart_str(presence.getShow())
        oMsg.presStatus = smart_str(presence.getStatus())

        if '/' in oMsg.jid:
            oMsg.room = oMsg.jid.split('/')[0]
        else:
            oMsg.room = oMsg.jid

        # Logging
        for i in dir(oMsg):
            if i not in ['__init__', '__del__', '__module__', '__doc__']:
                self.log.debug("Presence: oMsg." + str(i) + " = " + str(getattr(oMsg,i)))

        jid, type_, show, status = presence.getFrom(), \
            presence.getType(), presence.getShow(), \
            presence.getStatus()

        if self.jid.bareMatch(jid):
            # update internal status
            if type_ != self.OFFLINE:
               self._status = status
               self._show = show
            else:
               self._status = ""
               self._show = self.OFFLINE
            if not self._acceptownmsgs:
               # Ignore our own presence messages
               return

        if type_ is None:
            # Keep track of status message and type changes
            old_show, old_status = self._seen.get(jid, (self.OFFLINE, None))
            if old_show != show:
                self._status_type_changed(jid, show)

            if old_status != status:
                self._status_message_changed(jid, status)

            self._seen[jid] = (show, status)
        elif type_ == self.OFFLINE and jid in self._seen:
            # Notify of user offline status change
            del self._seen[jid]
            self._status_type_changed(jid, self.OFFLINE)

        try:
            subscription = self.roster.getSubscription(unicode(jid.__str__()))
        except KeyError, e:
            # User not on our roster
            subscription = None