<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# mib: Modular irc bot
# Copyright <NAME> <<EMAIL>>
# 
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program. If not, see <http://www.gnu.org/licenses/>.
#

from ircsocket import IrcSocket
from parser import parse, IRCMsg
import config

import os
import sys

class Mib:
    """ Main class which handles most of the core functionality.
    """
    def __init__(self):
        """ Initialize variables and read config.
        """
        sys.path.append('plugins')
        self.loaded_plugins = {} # plugin name : module
        self.cmd_callbacks = {} # command : set(function)
        self.privmsg_cmd_callbacks = {} # command : set(function)

        self.plugins = set(config.LOAD_PLUGINS)
        self.cmd_prefixes = set(config.CMD_PREFIXES)
        self.nick = config.NICK
        self.username = config.USERNAME
        self.realname = config.REALNAME
        self.server, self.port = config.SERVER
        self.channels = config.CHANNELS
        self.socket = IrcSocket(self.server, self.port, self.nick,
                                self.username, self.realname)
        self.socket.register_readline_cb(self.parse_line)
        for channel in self.channels:
            self.socket.join(channel)
        for plugin in self.plugins:
            print self.load_plugin(plugin)[1]

    def run(self):
        """ Start socket's main loop.
        """
        self.socket.run()

    def parse_line(self, line):
        """ Parse line and call callbacks registered for command.
        """
        print line
        parsed = parse(line)
        if not parsed:
            print 'Unable to parse line: "%s"' %(line)
            return
        # call registered functions
        for function in self.cmd_callbacks.get(parsed.cmd, ()):
            try:
                function(parsed)
            except Exception, e:
                print 'Error from function', repr(function), ':', e
        # call registered privmsg functions with pre-parsed line
        if parsed.cmd == 'PRIVMSG':
            cmd_prefix = parsed.postfix.split(' ', 1)[0]
            postfix = parsed.postfix[len(cmd_prefix):].lstrip()
            if cmd_prefix in self.cmd_prefixes:
                print 'Found command prefix', cmd_prefix
                cmd = postfix.lstrip().split(' ', 1)[0]
                postfix = postfix[len(cmd):].lstrip()
                stripped_parsed = IRCMsg(parsed.prefix, parsed.cmd,
                                         parsed.params, postfix)
                print 'Searching for command', cmd
                for function in self.privmsg_cmd_callbacks.get(cmd, ()):
                    try:
                        function(stripped_parsed)
                    except Exception, e:
                        print 'Error from function', repr(function), ':', e<commit_msg>Add regular expression lists for limiting users who can run chosen commands<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# mib: Modular irc bot
# Copyright <NAME> <<EMAIL>>
# 
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program. If not, see <http://www.gnu.org/licenses/>.
#

from ircsocket import IrcSocket
from parser import parse, IRCMsg
import config

import os
import sys

class Mib:
    """ Main class which handles most of the core functionality.
    """
    def __init__(self):
        """ Initialize variables and read config.
        """
        sys.path.append('plugins')
        self.loaded_plugins = {} # plugin name : module
        self.cmd_callbacks = {} # command : set(function)
        self.privmsg_cmd_callbacks = {} # command : set(function)
        self.command_masks = {} # command : list(regexp)

        self.plugins = set(config.LOAD_PLUGINS)
        self.cmd_prefixes = set(config.CMD_PREFIXES)
        self.nick = config.NICK
        self.username = config.USERNAME
        self.realname = config.REALNAME
        self.server, self.port = config.SERVER
        self.channels = config.CHANNELS
        self.socket = IrcSocket(self.server, self.port, self.nick,
                                self.username, self.realname)
        self.socket.register_readline_cb(self.parse_line)
        for channel in self.channels:
            self.socket.join(channel)
        for plugin in self.plugins:
            print self.load_plugin(plugin)[1]

    def run(self):
        """ Start socket's main loop.
        """
        self.socket.run()

    def parse_line(self, line):
        """ Parse line and call callbacks registered for command.
        """
        print line
        parsed = parse(line)
        if not parsed:
            print 'Unable to parse line: "%s"' %(line)
            return
        # call registered functions
        for function in self.cmd_callbacks.get(parsed.cmd, ()):
            try:
                function(parsed)
            except Exception, e:
                print 'Error from function', repr(function), ':', e
        # call registered privmsg functions with pre-parsed line
        if parsed.cmd == 'PRIVMSG':
            cmd_prefix = parsed.postfix.split(' ', 1)[0]
            postfix = parsed.postfix[len(cmd_prefix):].lstrip()
            if cmd_prefix in self.cmd_prefixes:
                print 'Found command prefix', cmd_prefix
                cmd = postfix.lstrip().split(' ', 1)[0]
                postfix = postfix[len(cmd):].lstrip()
                stripped_parsed = IRCMsg(parsed.prefix, parsed.cmd,
                                         parsed.params, postfix)
                print "stripped_parsed = ", stripped_parsed
                print 'Searching for command', cmd
                for function in self.privmsg_cmd_callbacks.get(cmd, ()):
                    run = False
                    if cmd not in self.command_masks:
                        run = True
                    else:
                        for regexp in self.command_masks[cmd]:
                            if regexp.match(parsed.prefix):
                                run = True
                                break
                    if run:
                        try:
                            function(stripped_parsed)
                        except Exception, e:
                            print 'Error from function', repr(function), ':', e
{'fraction_non_alphanumeric': 0.09071274298056156, 'fraction_numerical': 0.0018898488120950325, 'mean_word_length': 1.9043512348098786, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7494402', 'n_tokens_mistral': 1948, 'n_tokens_neox': 1909, 'n_words': 621}