<commit_before>import os
import re
import sys
import time
import logging
import telegram
import yaml
import threading

from lytebot import config, config_dir
from lytebot.errors import CommandError

class LyteBot:
    paths = {
        'ignored': os.path.join(config_dir, 'ignored.yml'),
        'disabled': os.path.join(config_dir, 'disabled.yml'),
        'blacklisted': os.path.join(config_dir, 'blacklisted.yml'),
    }

    def __init__(self, prefix='/'):
        self._last_id = None
        self.ignored = {}
        self.commands = {}
        self.disabled = []
        self.previous = {}
        self.blacklisted = []
        self.prefix = prefix
        self._bot = telegram.Bot(token=config['telegram']['token'])
        # Disable Telegram API's logger to prevent spam
        self._bot.logger.disabled = True

        for n, f in self.paths.items():
            try:
                with open(f, 'r') as f:
                    setattr(self, n, yaml.load(f.read()))
            except FileNotFoundError:
                pass
            except Exception as e:
                logging.warning('Couldn\'t load {} data: {}'.format(n, e))
            else:
                logging.info('Loaded {} data'.format(n))

    def _set_previous(self, func, args):
        '''
        Save previous command per chat

        :param func: Command function
        :param args: Arguments given to command
        '''
        self.previous[args.chat_id] = {'func': func, 'args': args}

    def _handle_msg(self, update):
        '''Handles all messages sent in all chats (that the bot can see)'''
        # Ignore stickers, pictures and other non-text messages
        if not update.message['text']:
            return

        # Is the user who sent the message ignored?
        if update.message.chat_id in self.ignored and \
           update.message.from_user.username in self.ignored[update.message.chat_id]:
            return

        message = update.message.text[1::]
        prefix = update.message.text[0]
        command = self.get_command(message)
        user = update.message.from_user.username or update.message.from_user.first_name

        if command and prefix == self.prefix and not self._is_disabled(command):
            # Check if the user is an owner if he calls an admin command
            if command['admin'] and update.message.from_user.username not in config['telegram']['owners']:
                text = '@{} You can\'t do that!'.format(user)
            else:
                text = command['func'](update.message, user)

            t = threading.Thread(target=self._bot.sendMessage, kwargs={
                'chat_id': update.message.chat_id,
                'text': text
            })
            t.start()

            self._last_id = update.update_id + 1

            if not message.startswith('!!'):
                self._set_previous(command['func'], update.message)

    def is_command(self, command):
        return command['func'].__name__ in self.commands

    def blacklist(self, sub):
        '''Blacklist a sub from the /r command'''
        if sub not in self.blacklisted:
            self.blacklisted.append(sub)
            self.save_data(self.paths['blacklisted'], self.blacklisted)

    def whitelist(self, sub):
        '''Whitelist a sub from the /r command'''
        try:
            self.blacklisted.remove(sub)
        except ValueError:
            return
<commit_msg>whitelist/blacklist now case insensitive<commit_after>import os
import re
import sys
import time
import logging
import telegram
import yaml
import threading

from lytebot import config, config_dir
from lytebot.errors import CommandError

class LyteBot:
    paths = {
        'ignored': os.path.join(config_dir, 'ignored.yml'),
        'disabled': os.path.join(config_dir, 'disabled.yml'),
        'blacklisted': os.path.join(config_dir, 'blacklisted.yml'),
    }

    def __init__(self, prefix='/'):
        self._last_id = None
        self.ignored = {}
        self.commands = {}
        self.disabled = []
        self.previous = {}
        self.blacklisted = []
        self.prefix = prefix
        self._bot = telegram.Bot(token=config['telegram']['token'])
        # Disable Telegram API's logger to prevent spam
        self._bot.logger.disabled = True

        for n, f in self.paths.items():
            try:
                with open(f, 'r') as f:
                    setattr(self, n, yaml.load(f.read()))
            except FileNotFoundError:
                pass
            except Exception as e:
                logging.warning('Couldn\'t load {} data: {}'.format(n, e))
            else:
                logging.info('Loaded {} data'.format(n))

    def _set_previous(self, func, args):
        '''
        Save previous command per chat

        :param func: Command function
        :param args: Arguments given to command
        '''
        self.previous[args.chat_id] = {'func': func, 'args': args}

    def _handle_msg(self, update):
        '''Handles all messages sent in all chats (that the bot can see)'''
        # Ignore stickers, pictures and other non-text messages
        if not update.message['text']:
            return

        # Is the user who sent the message ignored?
        if update.message.chat_id in self.ignored and \
           update.message.from_user.username in self.ignored[update.message.chat_id]:
            return

        message = update.message.text[1::]
        prefix = update.message.text[0]
        command = self.get_command(message)
        user = update.message.from_user.username or update.message.from_user.first_name

        if command and prefix == self.prefix and not self._is_disabled(command):
            # Check if the user is an owner if he calls an admin command
            if command['admin'] and update.message.from_user.username not in config['telegram']['owners']:
                text = '@{} You can\'t do that!'.format(user)
            else:
                text = command['func'](update.message, user)

            t = threading.Thread(target=self._bot.sendMessage, kwargs={
                'chat_id': update.message.chat_id,
                'text': text
            })
            t.start()

            self._last_id = update.update_id + 1

            if not message.startswith('!!'):
                self._set_previous(command['func'], update.message)

    def is_command(self, command):
        return command['func'].__name__ in self.commands

    def blacklist(self, sub):
        '''Blacklist a sub from the /r command'''
        if sub not in self.blacklisted:
            self.blacklisted.append(sub.lower())
            self.save_data(self.paths['blacklisted'], self.blacklisted)

    def whitelist(self, sub):
        '''Whitelist a sub from the /r command'''
        try:
            self.blacklisted.remove(sub.lower())
        except ValueError:
            return

{'fraction_non_alphanumeric': 0.11169208424110384, 'fraction_numerical': 0.0008714596949891067, 'mean_word_length': 2.083743842364532, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4067476', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1870, 'n_words': 521}