예제 #1
0
파일: IRC.py 프로젝트: r0lan/Legobot
    def handle(self, message):
        logger.info(message)

        target = message['metadata']['opts']['target']

        for split_line in Utilities.tokenize(message['text']):
            for truncated_line in Utilities.truncate(split_line):
                self.botThread.connection.privmsg(target, truncated_line)
                # Delay to prevent floods
                time.sleep(0.25)
예제 #2
0
파일: Slack.py 프로젝트: bbriggs/Legobot
    def handle(self, message):
        '''Attempts to send a message to the specified destination in Slack.
        Extends Legobot.Lego.handle()

        Args:
            message (Legobot.Message): message w/ metadata to send.
        '''

        logger.debug(message)
        if Utilities.isNotEmpty(message['metadata']['opts']):
            target = message['metadata']['opts']['target']
            thread = message['metadata']['opts'].get('thread')
            # pattern = re.compile('@([a-zA-Z0-9._-]+)')
            pattern = re.compile('^@([a-zA-Z0-9._-]+)|\s@([a-zA-Z0-9._-]+)')
            matches = re.findall(pattern, message['text'])
            matches = set(matches)
            logger.debug('MATCHES!!!!   {}'.format(matches))
            for match in matches:
                if isinstance(match, tuple):
                    if match[0] != '':
                        match = match[0]
                    else:
                        match = match[1]
                if not match.startswith('@'):
                    match = '@' + match
                message['text'] = message['text'].replace(
                    match,
                    '<{}>'.format(match)
                )

            pattern = re.compile('#([A-Za-z0-9-]+)')
            matches = re.findall(pattern, message['text'])
            matches = set(matches)
            for match in matches:
                channel_id = self.botThread.get_channel_id_by_name(match)
                if channel_id:
                    message['text'] = message['text'].replace(
                        '#' + match,
                        '<#{}|{}>'.format(
                            channel_id,
                            match
                        )
                    )

            if (message['text'].find('<<@') != -1
                    or message['text'].find('<<#') != -1):
                message['text'] = message['text'].replace('<<', '<')
                message['text'] = message['text'].replace('>>', '>')

            if target.startswith('U'):
                target = self.botThread.get_dm_channel(target)
            attachment = message['metadata']['opts'].get('attachment')
            if attachment:
                text = message['metadata']['opts'].get('fallback')
                attachment = self.build_attachment(
                    text, target, attachment, thread)
                self.botThread.post_attachment(attachment)
            else:
                self.botThread.slack_client.rtm_send_message(
                    target, message['text'], thread=thread)
예제 #3
0
파일: Help.py 프로젝트: r0lan/Legobot
 def listening_for(message):
     if Utilities.isNotEmpty(message['text']):
         try:
             return message['text'].split()[0] == '!help'
         except Exception as e:
             logger.error(
                 'Help lego failed to check message text: {0!s}'.format(e))
             return False
예제 #4
0
파일: Help.py 프로젝트: bbriggs/Legobot
 def listening_for(message):
     if Utilities.isNotEmpty(message['text']):
         try:
             return message['text'].split()[0] == '!help'
         except Exception as e:
             logger.error(
                 'Help lego failed to check message text: {0!s}'.format(e))
             return False
예제 #5
0
    def handle(self, message):
        '''
        Attempts to send a message to the specified destination in IRC
        Extends Legobot.Lego.handle()

        Args:
            message (Legobot.Message): message w/ metadata to send.
        '''

        logger.debug(message)
        if Utilities.isNotEmpty(message['metadata']['opts']):
            target = message['metadata']['opts']['target']

            for split_line in Utilities.tokenize(message['text']):
                for truncated_line in Utilities.truncate(split_line):
                    self.botThread.connection.privmsg(target, truncated_line)
                    # Delay to prevent floods
                    time.sleep(0.25)
예제 #6
0
    def handle(self, message):
        """Attempts to send a message to the specified destination in Slack.

        Extends Legobot.Lego.handle()

        Args:
            message (Legobot.Message): message w/ metadata to send.
        """

        logger.debug(message)
        if Utilities.isNotEmpty(message['metadata']['opts']):
            target = message['metadata']['opts']['target']
            thread = message['metadata']['opts'].get('thread')
            # pattern = re.compile('@([a-zA-Z0-9._-]+)')
            pattern = re.compile(r'^@([a-zA-Z0-9._-]+)|\s@([a-zA-Z0-9._-]+)')
            matches = re.findall(pattern, message['text'])
            matches = set(matches)
            logger.debug('MATCHES!!!!   {}'.format(matches))
            for match in matches:
                if isinstance(match, tuple):
                    if match[0] != '':
                        match = match[0]
                    else:
                        match = match[1]
                if not match.startswith('@'):
                    match = '@' + match
                message['text'] = message['text'].replace(
                    match, '<{}>'.format(match))

            pattern = re.compile('#([A-Za-z0-9-]+)')
            matches = re.findall(pattern, message['text'])
            matches = set(matches)
            for match in matches:
                channel_id = self.botThread.get_channel_id_by_name(match)
                if channel_id:
                    message['text'] = message['text'].replace(
                        '#' + match, '<#{}|{}>'.format(channel_id, match))

            if (message['text'].find('<<@') != -1
                    or message['text'].find('<<#') != -1):
                message['text'] = message['text'].replace('<<', '<')
                message['text'] = message['text'].replace('>>', '>')

            if target.startswith('U'):
                target = self.botThread.get_dm_channel(target)
            attachment = message['metadata']['opts'].get('attachment')
            if attachment:
                text = message['metadata']['opts'].get('fallback')
                attachment = self.build_attachment(text, target, attachment,
                                                   thread)
                self.botThread.post_attachment(attachment)
            else:
                self.botThread.slack_client.rtm_send_message(target,
                                                             message['text'],
                                                             thread=thread)
예제 #7
0
파일: Discord.py 프로젝트: 5l1v3r1/Legobot
    def handle(self, message):
        '''
        Attempts to send a message to the specified destination in Discord.
        Extends Legobot.Lego.handle()

        Args:
            message (Legobot.Message): message w/ metadata to send.
        '''

        logger.debug(message)
        if Utilities.isNotEmpty(message['metadata']['opts']):
            target = message['metadata']['opts']['target']
            self.botThread.create_message(target, message['text'])
예제 #8
0
파일: Discord.py 프로젝트: bbriggs/Legobot
    def handle(self, message):
        '''
        Attempts to send a message to the specified destination in Discord.
        Extends Legobot.Lego.handle()

        Args:
            message (Legobot.Message): message w/ metadata to send.
        '''

        logger.debug(message)
        if Utilities.isNotEmpty(message['metadata']['opts']):
            target = message['metadata']['opts']['target']
            self.botThread.create_message(target, message['text'])
예제 #9
0
파일: Slack.py 프로젝트: r0lan/Legobot
    def handle(self, message):
        '''Attempts to send a message to the specified destination in Slack.
        Extends Legobot.Lego.handle()

        Args:
            message (Legobot.Message): message w/ metadata to send.
        '''

        if Utilities.isNotEmpty(message['metadata']['opts']):
            target = message['metadata']['opts']['target']
            if target.startswith('U'):
                target = self.botThread.get_dm_channel(target)
            self.botThread.slack_client.rtm_send_message(
                target, message['text'])
예제 #10
0
파일: IRC.py 프로젝트: r0lan/Legobot
    def on_welcome(self, c, e):
        """
        This function runs when the bot successfully connects to the IRC server
        """
        for channel in self.my_channels:
            logger.debug('Attempting to join {0!s}'.format(channel))
            c.join(channel)

        if self.nickserv:
            if Utilities.isNotEmpty(self.nickserv_pass):
                self.identify(c, e, self.nickserv_pass)
            else:
                logger.error('If nickserv is enabled, you must supply'
                             ' a password')

        if self.nickserv is False and self.nickserv_pass is not None:
            logger.warn('It appears you provided a nickserv password but '
                        'did not enable nickserv authentication')
예제 #11
0
    def on_welcome(self, c, e):
        """
        This function runs when the bot successfully connects to the IRC server
        """
        self.backoff = 1  # Assume we had a good connection. Reset backoff.
        if self.nickserv:
            if Utilities.isNotEmpty(self.nickserv_pass):
                self.identify(c, e, self.nickserv_pass)
                time.sleep(3)  # Make sure Nickserv really sees us
            else:
                logger.error('If nickserv is enabled, you must supply'
                             ' a password')

        if self.nickserv is False and self.nickserv_pass is not None:
            logger.warn('It appears you provided a nickserv password but '
                        'did not enable nickserv authentication')

        for channel in self.my_channels:
            logger.debug('Attempting to join {0!s}'.format(channel))
            c.join(channel)