Example #1
0
 def _on_invite(self, c, e):
     """[Internal]"""
     oMatrix = CNBMatrix.getInstance()
     oMsg = self._getMsgFromEvent(e, 'Invite')
     if self._botConfig.get('bot', 'username') != oMsg.replyTo:
         replies = oMatrix.processIrcMod(oMsg)
         if replies != None:
             for r in replies:
                 c.privmsg(oMsg.replyTo, r)
                 sleep(self.IRC_REPLY_SLEEP)
Example #2
0
 def _on_invite(self, c, e):
     """[Internal]"""
     oMatrix = CNBMatrix.getInstance()
     oMsg = self._getMsgFromEvent(e, 'Invite')
     if self._botConfig.get('bot', 'username') != oMsg.replyTo:
         replies = oMatrix.processIrcMod(oMsg)
         if replies != None:
             for r in replies:
                 c.privmsg(oMsg.replyTo, r)
                 sleep(self.IRC_REPLY_SLEEP)
Example #3
0
File: cnbCore.py Project: OSSSP/cnb
    def _die(self):
        """
        This method is called when the bot is killed
        """
        self.log.info('Killing Connector Manager')
        #del oConMgr
        self.oConMgr.killAll()

        #self.log.info('Killing Session Manager')
        #oSesMgr.kill()

        self.log.info('Killing Matrix')
        oMatrix = CNBMatrix.getInstance()
        #del oMatrix
        oMatrix.freeMM()

        self.log.info('Killing Console')
        #del oConsole
        self.oConsole.killConsole()
Example #4
0
File: cnbCore.py Project: OSSSP/cnb
    def __init__(self):
        Thread.__init__(self)
        oConfig = CNBConfig.getInstance()
        if oConfig.get('global', 'daemon'):
            self._bEnableShellConsole = False
        self.log = logging.getLogger(self.__class__.__name__)
        self._configLogs(oConfig)

        oMgr = CNBManager.getInstance()
        oMgr.setCNB(self)

        self.oConMgr = CNBConnectorManager()
        self.oConMgr.loadDefault()

        if self._bEnableShellConsole:
            self.oConsole = CNBConsole()
            self.oConsole.start()

        oMatrix = CNBMatrix.getInstance()
Example #5
0
    def _die(self):
        """
        This method is called when the bot is killed
        """
        self.log.info('Killing Connector Manager')
        #del oConMgr
        self.oConMgr.killAll()
        
        #self.log.info('Killing Session Manager')
        #oSesMgr.kill()

        self.log.info('Killing Matrix')
        oMatrix = CNBMatrix.getInstance()
        #del oMatrix
        oMatrix.freeMM()

        self.log.info('Killing Console')
        #del oConsole
        self.oConsole.killConsole()
Example #6
0
    def __init__(self):
        Thread.__init__(self)
        oConfig = CNBConfig.getInstance()
        if oConfig.get('global', 'daemon'):
            self._bEnableShellConsole = False
        self.log = logging.getLogger(self.__class__.__name__)
        self._configLogs(oConfig)

        oMgr = CNBManager.getInstance()
        oMgr.setCNB(self)

        self.oConMgr = CNBConnectorManager()
        self.oConMgr.loadDefault()

        if self._bEnableShellConsole:
            self.oConsole = CNBConsole()
            self.oConsole.start()

        oMatrix = CNBMatrix.getInstance()
Example #7
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)
Example #8
0
            if subscription not in ('to', 'both'):
                self.roster.Subscribe(jid)

            if subscription in (None, 'none'):
                self.send(jid, self.MSG_AUTHORIZE_ME)
        elif type_ == 'subscribed':
            # Authorize any pending requests for that JID
            self.roster.Authorize(jid)
        elif type_ == 'unsubscribed':
            # Authorization was not granted
            self.send(jid, self.MSG_NOT_AUTHORIZED)
            self.roster.Unauthorize(jid)

        # Process any module for stats or more
        if oMsg.presType in ['None', self.AVAILABLE, self.AWAY, self.CHAT, self.DND, self.XA, self.OFFLINE]:
            oMatrix = CNBMatrix.getInstance()
            oMatrix.processXmppMod(oMsg)

    def _callback_iq(self, conn, iq_node):
        """
        IQ callback function. A IQ query is a way to query a xmpp server for some informations (ex: get list of members in a room). 
        IQ Responses are handled here.
        @param conn: Connection handle
        @type conn: Object
        @param iq_node: IQ Node
        @type iq_node: Object
        """
        node = str(iq_node)

        #we've got an online users list
        if 'http://jabber.org/protocol/disco#items' in node: