Пример #1
0
Файл: s.py Проект: chizu/fishbot
def bang(pipein, arguments, event):
    # This regexp is to split correctly on only unescaped / slashes like
    # 'asjdf/as[ad/c]df/as\/df' needs.
    # Returns ['asjdf', 'as[ad/c]df', 'as\\/df']
    unescaped_slash = re.compile(r"(?<!\\)(?<!\[)/(?!.?\])")
    if len(unescaped_slash.split(arguments)) == 4:
        # Assumed to start with s/
        (pattern, repl, options) = unescaped_slash.split(arguments)[1:]
        # Number of lines to search
        lines = 20
        # Number of lines to match
        if re.search("([1-9])", options):
            lines_returned = int(re.search("([1-9])", options).group(0)) + 1
        else:
            lines_returned = 1
        # Search globally, or just the user executing the command
        if "m" in options or "M" in options or "g" in options or "G" in options:
            search_domain = fishapi.backend.last(type(event), target=event.target, count=lines)[1:]
        else:
            search_domain = fishapi.backend.last(type(event), source=event.source, target=event.target, count=lines)[1:]
        for past_event in search_domain:
            past_event.arguments = past_event.arguments.replace("\001ACTION", fishapi.getnick(past_event.source))
            past_event.arguments = past_event.arguments.replace("\001", "")
        try:
            option_values = {
                "I": re.I,
                "L": re.L,
                "M": re.M,
                "S": re.S,
                "U": re.U,
                "X": re.X,
                "G": re.M,
                "i": re.I,
                "m": re.M,
                "g": re.M,
            }
            # eval, be careful changing this as to not allow arbitrary
            # python to be executed
            compiled = re.compile(pattern, options and eval("|".join(options), None, option_values) or 0)
            returned = []
            for pevent in search_domain:
                if compiled.search(pevent.arguments):
                    returned.append(
                        "%s meant to say: %s"
                        % (fishapi.getnick(pevent.source), compiled.sub(repl, pevent.arguments).replace("\/", "/"))
                    )
            if len(returned) > lines_returned:
                returned = returned[:lines_returned]
            return (returned, None)
        except:
            raise
    else:
        return ("Wrong number of /'s.", None)
Пример #2
0
def karma(self, event):
    sql_session = get_session()
    before, string, operator = re.search(expression[0], event.arguments, re.UNICODE).groups()
    string = string.strip().lower()
    nick = getnick(event.source)
    # Find this string or add the row
    item = sql_session.query(Karma).\
        filter_by(string=string, nick=nick).first()
    if not item:
        item = Karma(string=string, nick=nick)
        sql_session.add(item)
    # Count +/- and alter the score within the range of 5 +/-
    if operator[0] == '+':
        if item.score < 7 - len(operator):
            item.score += len(operator) - 1
        else:
            item.score = 5
    elif operator[0] == '-':
        if item.score > -7 + len(operator):
            item.score -= len(operator) - 1
        else:
            item.score = -5
    else:
        print "karma.py: Something odd has happened."
    sql_session.commit()
Пример #3
0
def karma(self, event):
    sql_session = get_session()
    before, string, operator = re.search(expression[0], event.arguments,
                                         re.UNICODE).groups()
    string = string.strip().lower()
    nick = getnick(event.source)
    # Find this string or add the row
    item = sql_session.query(Karma).\
        filter_by(string=string, nick=nick).first()
    if not item:
        item = Karma(string=string, nick=nick)
        sql_session.add(item)
    # Count +/- and alter the score within the range of 5 +/-
    if operator[0] == '+':
        if item.score < 7 - len(operator):
            item.score += len(operator) - 1
        else:
            item.score = 5
    elif operator[0] == '-':
        if item.score > -7 + len(operator):
            item.score -= len(operator) - 1
        else:
            item.score = -5
    else:
        print "karma.py: Something odd has happened."
    sql_session.commit()
Пример #4
0
def bang(self, event):
    import re
    import importer
    pipes = [string.strip(i) for i in event.arguments.split('|')]
    pipein = ""
    for pipe in pipes:
        if pipe:
            postpipe = re.search(expression[0], pipe)
            if not postpipe:
                # invalid bang command syntax
                print "bang: invalid syntax - '" + pipe + "'"
                return
            name = postpipe.group(1)
            arguments = string.strip(postpipe.group(2))
            try:
                module = importer.__import__(name, globals(), locals(),
                                             'plugins.bang')
                if hasattr(module, 'bang'):
                    # New API
                    respond = self.respond_to(event.source, event.target)
                    (pubmsg, action) = module.bang(pipein, arguments, event)
                    if pubmsg and pipe is pipes[-1]:
                        if isinstance(pubmsg, (list, tuple)):
                            lines = list()
                            for line in pubmsg:
                                lines += line.split('\n')
                        else:
                            lines = pubmsg.split('\n')
                        if len(lines) > 13:
                            # Private message
                            respond = fishapi.getnick(event.source)
                        for each in lines:
                            if each != '':
                                event.server.message(respond, each)
                    elif pubmsg:
                        if isinstance(pubmsg, (list, tuple)):
                            pipein = string.join(pubmsg, '\n')
                        else:
                            pipein = pubmsg
                    if action:
                        if isinstance(action, (list, tuple)):
                            for each in action:
                                event.server.action(respond, each)
                        else:
                            event.server.action(respond, action)

                elif hasattr(module, 'handle_say'):
                    # Old API
                    module.handle_say(self, event.source, event.target,
                                      event.arguments)
            except ImportError:
                return
            except:
                raise
Пример #5
0
Файл: s.py Проект: chizu/fishbot
def bang(pipein, arguments, event):
    # This regexp is to split correctly on only unescaped / slashes like
    # 'asjdf/as[ad/c]df/as\/df' needs.
    # Returns ['asjdf', 'as[ad/c]df', 'as\\/df']
    unescaped_slash = re.compile(r"(?<!\\)(?<!\[)/(?!.?\])")
    if len(unescaped_slash.split(arguments)) == 4:
        # Assumed to start with s/
        (pattern, repl, options) = unescaped_slash.split(arguments)[1:]
        # Number of lines to search
        lines = 20
        # Number of lines to match
        if re.search('([1-9])', options):
            lines_returned = int(re.search('([1-9])', options).group(0)) + 1
        else:
            lines_returned = 1
        # Search globally, or just the user executing the command
        if 'm' in options or 'M' in options or 'g' in options or 'G' in options:
            search_domain = fishapi.backend.last(type(event), target=event.target, count=lines)[1:]
        else:
            search_domain = fishapi.backend.last(type(event), source=event.source, target=event.target, count=lines)[1:]
        for past_event in search_domain:
            past_event.arguments = past_event.arguments.replace('\001ACTION', fishapi.getnick(past_event.source))
            past_event.arguments = past_event.arguments.replace('\001', '')
        try:
            option_values = {'I':re.I, 'L':re.L, 'M':re.M, 'S':re.S, 'U':re.U, 'X':re.X, 'G':re.M,
                             'i':re.I,             'm':re.M,                               'g':re.M}
            # eval, be careful changing this as to not allow arbitrary
            # python to be executed
            compiled = re.compile(pattern, options and eval('|'.join(options), None, option_values) or 0)
            returned = []
            for pevent in search_domain:
                if compiled.search(pevent.arguments):
                    returned.append("%s meant to say: %s" % (fishapi.getnick(pevent.source), compiled.sub(repl, pevent.arguments).replace("\/", "/")))
            if len(returned) > lines_returned:
                returned = returned[:lines_returned]
            return (returned, None)
        except:
            raise
    else:
        return ("Wrong number of /'s.", None)
Пример #6
0
def bang(pipein, arguments, event):
    sql_session = backend.get_session()
    token = arguments.strip().lower()
    if not token:
        token = fishapi.getnick(event.source).strip().lower()
        arguments = token
    tokens = token.split()
    if tokens[0] == 'stats':
        results = sql_session.query("string", "score").\
            from_statement(stats_sql).all()
        sql_session.commit()
        results = sorted(results, key=itemgetter(1))
        if len(tokens) >= 3 and tokens[2].isdigit() and int(tokens[2]) <= 15:
            amount = int(tokens[2])
        else:
            amount = 3
        if len(tokens) >= 2:
            if tokens[1] == 'top':
                return ("Karma top %s -> %s" % (amount, ", ".join(
                    [":".join([str(y) for y in x])
                     for x in results[-amount:]])), None)
            elif tokens[1] == 'bottom':
                return ("Karma bottom %s -> %s" % (amount, ", ".join(
                    [":".join([str(y) for y in x])
                     for x in results[:amount]])), None)
            elif tokens[1] == 'middle':
                mid_point = int(len(results) / 2)
                return ("Karma middle %s -> %s" % (amount, ", ".join([
                    ":".join([str(y) for y in x])
                    for x in results[mid_point -
                                     int(amount / 2 + 1):mid_point +
                                     int(amount / 2 + 1)]
                ])), None)
        return ("Highest score: %s (%s) - Lowest score: %s (%s)" %
                (results[-1][0], results[-1][1], results[0][0], results[0][1]),
                None)
    else:
        things = sql_session.query(Karma).filter_by(string=token)
        sql_session.commit()
        score = calckarma(things)
        if score:
            return ("'%s' has a score of: %s" % (arguments, score), None)
        else:
            return ("'%s' has neutral karma." % (arguments), None)
Пример #7
0
def bang(pipein, arguments, event):
    sql_session = backend.get_session()
    token = arguments.strip().lower()
    if not token:
        token = fishapi.getnick(event.source).strip().lower()
        arguments = token
    tokens = token.split()
    if tokens[0] == 'stats':
        results = sql_session.query("string", "score").\
            from_statement(stats_sql).all()
        sql_session.commit()
        results = sorted(results, key=itemgetter(1))
        if len(tokens) >= 3 and tokens[2].isdigit() and int(tokens[2]) <= 15:
            amount = int(tokens[2])
        else:
            amount = 3
        if len(tokens) >= 2:
            if tokens[1] == 'top':
                return ("Karma top %s -> %s" %
                        (amount, ", ".join([":".join([str(y) for y in x]) for x in results[-amount:]])), None)
            elif tokens[1] == 'bottom':
                return ("Karma bottom %s -> %s" %
                        (amount, ", ".join([":".join([str(y) for y in x]) for x in results[:amount]])), None)
            elif tokens[1] == 'middle':
                mid_point = int(len(results) / 2)
                return ("Karma middle %s -> %s" %
                        (amount, ", ".join([":".join([str(y) for y in x]) for x in results[mid_point - int(amount / 2 + 1):mid_point + int(amount / 2 + 1)]])), None)
        return ("Highest score: %s (%s) - Lowest score: %s (%s)" % (results[-1][0], results[-1][1], results[0][0], results[0][1]), None)
    else:
        things = sql_session.query(Karma).filter_by(string=token)
        sql_session.commit()
        score = calckarma(things)
        if score:
            return ("'%s' has a score of: %s" % (arguments, score), None)
        else:
            return ("'%s' has neutral karma." % (arguments), None)