예제 #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
예제 #2
0
파일: cnbIrcCon.py 프로젝트: hackfestca/cnb
    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
예제 #3
0
파일: cnbXmppCon.py 프로젝트: OSSSP/cnb
    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)