op.py 2.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
#encoding: utf-8
"""
Channel operator methods.
"""

import re

import config
from logs import Log

class Op():
    def __init__(self):
        # Sequence for op_mode verification (fibonacci)
        self.op_sequence = [1, 2, 3, 5, 8, 13, 21, 34, 55]
        self.op_offset = 0
        self.op_counter = 0

    def need_op_mode(self, me, params):
        """
        Send a message on channel RP_CHANNEL to beg an op mode to each actual operators.
        params is an array with :
            - params[0]:
            - params[1]:
            - params[2]: the channel
            - params[3]: the list of all users on the channel
        """
        Log.debug("Names : %s" % params)
        message = ""
        if params[2]==config.RP_CHANNEL:
            ops = [user[1:] for user in params[3].split() if user[0]=="@"]
            if me not in params[3]:
                # Testing based on fibonacci sequence
                self.op_counter += 1
                Log.debug("op_counter  : %s" % self.op_counter)
                Log.debug("op_offset   : %s" % self.op_offset)
                Log.debug("op_sequence : %s" % self.op_sequence[self.op_offset])
                if self.op_counter>self.op_sequence[self.op_offset]:
38
                    message = "%s j'ai besoin d'être op pour modifier le topic, ne m'oubliez pas s'il vous plait !" % ", ".join(ops)
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
                    # Then reset op_counter
                    self.op_counter = 0
                    # And move the sequence further in order not to spam channel
                    if self.op_offset<len(self.op_sequence)-1:
                        self.op_offset += 1
        return message

    def get_op_mode(self, user, flag_set):
        # Cleaning user name
        user = re.search("([^!]*)!", user).group(1)
        if flag_set:
            # reset counter and sequence
            self.op_counter = 0
            self.op_offset = 0
            # thanks to user
54
            return "Merci %s <3" % user
55 56
        else:
            # bad user ;o(
57
            return "Vilain %s )o;" % user