Esempio n. 1
0
    def _getMsgFromEvent(self, e, et = 'Msg'):
        """
        Generate a message object from an event
        @param e: Event
        @type e: Event
        @param et: 
        @type et: String
        """
        oMsg = CNBMessage()
        oMsg.protocol = 'irc'
        oMsg.conId = self._botConfig.get('bot', 'id')
        oMsg.type = str(e.eventtype())
        oMsg.isPrivate = (oMsg.type == 'privmsg' or oMsg.type == 'privnotice' or oMsg.type == 'invite')
        oMsg.source = str(e.source())
        oMsg.target = str(e.target())
        oMsg.text = str(e.arguments()[0]).strip()
        oMsg.domain = self._botConfig.get('bot', 'server')
        oMsg.initCmd()

        if oMsg.isPrivate:
            oMsg.username = str(e.source()).split('!')[0]
            oMsg.replyTo = str(e.source()).split('!')[0]
            if oMsg.type == 'invite':
                oMsg.room = oMsg.text
        else:
            #oMsg.username = str(e.target())
            oMsg.username = str(e.source()).split('!')[0]
            oMsg.replyTo = str(e.target())
            oMsg.room = str(e.target())

        # Logging
        for i in dir(oMsg):
            if i not in ['__init__', '__del__', '__module__', '__doc__']:
                self.log.debug(et + ": oMsg." + str(i) + " = " + str(getattr(oMsg,i)))
        
        return oMsg
Esempio n. 2
0
    def _callback_message(self, conn, mess):
        """
        Changes the behaviour of the JabberBot in order to allow it to answer direct messages. 
        This is used often when it is connected in MUCs (multiple users chatroom). 

        @param conn: Connection handle
        @type conn: Object
        @param mess: Message sent to the bot
        @type mess: Object
        """
        self._lastping = time()
        reply = None

        oMsg = CNBMessage()
        oMsg.protocol = self._botConfig.get('bot', 'type')
        oMsg.conId = self._botConfig.get('bot', 'id')
        oMsg.type = str(mess.getType())
        oMsg.isPrivate = (oMsg.type == self.XMPP_CHAT)
        oMsg.jid = str(mess.getFrom())
        oMsg.error = mess.getError()
        oMsg.nick = mess.getFrom().getResource()
        oMsg.props = mess.getProperties()
        oMsg.email = mess.getFrom().getStripped()
        oMsg.username = self.get_sender_username(mess)
        if len(oMsg.email.split('@')) > 1:
            oMsg.domain = oMsg.email.split('@')[1]
        oMsg.text = smart_str(mess.getBody()).strip()
        oMsg.initCmd()

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

        if oMsg.type == self.XMPP_CHAT:
            oMsg.replyTo = oMsg.username
        else:
            oMsg.replyTo = oMsg.room

        if ' ' in oMsg.text:
            command, args = oMsg.text.split(' ', 1)
        else:
            command, args = oMsg.text, ''
        oMsg.cmd = command.lower()

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

        if oMsg.type not in ("groupchat", "chat", "normal"):
            self.log.debug("unhandled message type: %s" % type)
            return

        # Ignore messages from before we joined
        if xmpp.NS_DELAY in oMsg.props: return

        # Ignore messages from myself
        if self.jid.bareMatch(oMsg.jid): return

        # Logging message
        # Not logging because it is already logged in cnb-matrix.log
        #self.log.info("%s> %s" % (oMsg.jid, oMsg.text))
        #self.log.debug("*** cmd = %s" % oMsg.cmd)

        # If a message format is not supported (eg. encrypted), txt will be None
        if not oMsg.text: return

        # Ignore messages from users not seen by this bot
        #if jid not in self._seen:
        #    self.log.info('Ignoring message from unseen guest: %s' % jid)
        #    self.log.debug("I've seen: %s" % ["%s" % x for x in self._seen.keys()])
        #    return

        # Remember the last-talked-in thread for replies
        self._threads[oMsg.jid] = mess.getThread()

        # In private chat, it's okay for the bot to always respond.
        # In group chat, the bot should silently ignore commands it
        # doesn't understand or aren't handled by _unknown_command().
        if oMsg.type == 'groupchat':
            default_reply = None
        else:
            default_reply = self.MSG_UNKNOWN_COMMAND % {'command': oMsg.cmd}
            reply = self._unknown_command(mess, oMsg.cmd, args)
            
        # Else
        if reply is None:
            reply = default_reply
        else:
            self.send_simple_reply(mess, reply)

        # Process Response if text is not null and sender is not the bot
        if oMsg.text != '' \
           and oMsg.text != 'None' \
           and oMsg.username != self._botConfig.get('bot', 'username').split('@')[0]:
            oMatrix = CNBMatrix.getInstance()
            reply = oMatrix.processXmppMod(oMsg)
            
            # if reply is too big, split into smaller block
            if reply and len(reply) > self.MAX_REPLY_SIZE:
                aReplies = self._splitByLine(reply,self.MAX_REPLY_SIZE)
                self.log.debug('Splitted the reply in ' + str(len(aReplies)) + ' blocks of ' + str(self.MAX_REPLY_SIZE))
                for r in aReplies: 
                    self.send_simple_reply(mess, r)
                    sleep(self.SLEEP_TIME_BETWEEN_REPLY)
            elif reply:
                self.send_simple_reply(mess, reply)
Esempio n. 3
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
Esempio n. 4
0
    def _getMsgFromEvent(self, e, et='Msg'):
        """
        Generate a message object from an event
        @param e: Event
        @type e: Event
        @param et: 
        @type et: String
        """
        oMsg = CNBMessage()
        oMsg.protocol = 'irc'
        oMsg.conId = self._botConfig.get('bot', 'id')
        oMsg.type = str(e.eventtype())
        oMsg.isPrivate = (oMsg.type == 'privmsg' or oMsg.type == 'privnotice'
                          or oMsg.type == 'invite')
        oMsg.source = str(e.source())
        oMsg.target = str(e.target())
        oMsg.text = str(e.arguments()[0]).strip()
        oMsg.domain = self._botConfig.get('bot', 'server')
        oMsg.initCmd()

        if oMsg.isPrivate:
            oMsg.username = str(e.source()).split('!')[0]
            oMsg.replyTo = str(e.source()).split('!')[0]
            if oMsg.type == 'invite':
                oMsg.room = oMsg.text
        else:
            #oMsg.username = str(e.target())
            oMsg.username = str(e.source()).split('!')[0]
            oMsg.replyTo = str(e.target())
            oMsg.room = str(e.target())

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

        return oMsg