Esempio n. 1
0
def add_warning(target: Union[str, users.User],
                amount: int,
                actor: users.User,
                reason: str,
                notes: str = None,
                expires=None,
                sanctions=None):
    if isinstance(target, users.User):
        tacc = target.account
        if tacc is None:
            return False
    else:
        tacc = target

    reason = reason.format()
    sacc = actor.account

    # Turn expires into a datetime if we were passed a string; note that no error checking is performed here
    if not isinstance(expires, datetime):
        expires = _parse_expires(expires)

    # determine if we need to automatically add any sanctions
    if sanctions is None:
        sanctions = {}
    prev = db.get_warning_points(tacc)
    cur = prev + amount
    if amount > 0:
        _get_auto_sanctions(sanctions, prev, cur)

    sid = db.add_warning(tacc, sacc, amount, reason, notes, expires)
    if "stasis" in sanctions:
        db.add_warning_sanction(sid, "stasis", sanctions["stasis"])
    if "deny" in sanctions:
        for cmd in sanctions["deny"]:
            db.add_warning_sanction(sid, "deny command", cmd)
    if "tempban" in sanctions:
        # this inserts into the bantrack table too
        (acclist, hmlist) = db.add_warning_sanction(sid, "tempban",
                                                    sanctions["tempban"])
        cmodes = []
        for acc in acclist:
            cmodes.append(("+b", "{0}{1}".format(var.ACCOUNT_PREFIX, acc)))
        channels.Main.mode(*cmodes)
        for user in channels.Main.users:
            if user.account in acclist:
                channels.Main.kick(
                    user,
                    messages["tempban_kick"].format(nick=user,
                                                    botnick=users.Bot.nick,
                                                    reason=reason))

    # Update any tracking vars that may have changed due to this
    db.init_vars()

    return sid
Esempio n. 2
0
def add_warning(cli,
                target,
                amount,
                actor,
                reason,
                notes=None,
                expires=None,
                sanctions=None):
    tacc, thm = parse_warning_target(target)
    if tacc is None and thm is None:
        return False

    if actor not in var.USERS and actor != users.Bot.nick:
        return False
    sacc = None
    shm = None
    if actor in var.USERS:
        sacc = var.USERS[actor]["account"]
        shm = actor + "!" + var.USERS[actor]["ident"] + "@" + var.USERS[actor][
            "host"]

    # Turn expires into a datetime if we were passed a string; note that no error checking is performed here
    if isinstance(expires, str):
        exp_suffix = expires[-1]
        exp_amount = int(expires[:-1])

        if exp_suffix == "d":
            expires = datetime.utcnow() + timedelta(days=exp_amount)
        elif exp_suffix == "h":
            expires = datetime.utcnow() + timedelta(hours=exp_amount)
        elif exp_suffix == "m":
            expires = datetime.utcnow() + timedelta(minutes=exp_amount)
        else:
            raise ValueError("Invalid expiration string")
    elif isinstance(expires, int):
        expires = datetime.utcnow() + timedelta(days=expires)

    # Round expires to the nearest minute (30s rounds up)
    if isinstance(expires, datetime):
        round_add = 0
        if expires.second >= 30:
            round_add = 1
        expires -= timedelta(seconds=expires.second,
                             microseconds=expires.microsecond)
        expires += timedelta(minutes=round_add)

    # determine if we need to automatically add any sanctions
    if sanctions is None:
        sanctions = {}
    prev = db.get_warning_points(tacc, thm)
    cur = prev + amount
    if amount > 0:
        _get_auto_sanctions(sanctions, prev, cur)

    sid = db.add_warning(tacc, thm, sacc, shm, amount, reason, notes, expires)
    if "stasis" in sanctions:
        db.add_warning_sanction(sid, "stasis", sanctions["stasis"])
    if "deny" in sanctions:
        for cmd in sanctions["deny"]:
            db.add_warning_sanction(sid, "deny command", cmd)
    if "tempban" in sanctions:
        # this inserts into the bantrack table too
        (acclist, hmlist) = db.add_warning_sanction(sid, "tempban",
                                                    sanctions["tempban"])
        cmodes = []
        for acc in acclist:
            cmodes.append(("+b", "{0}{1}".format(var.ACCOUNT_PREFIX, acc)))
        for hm in hmlist:
            cmodes.append(("+b", "*!*@{0}".format(hm.split("@")[1])))
        channels.Main.mode(*cmodes)
        for user in channels.Main.users:
            if user.account in acclist:
                channels.Main.kick(
                    user,
                    messages["tempban_kick"].format(nick=user,
                                                    botnick=users.Bot.nick,
                                                    reason=reason))
            elif user.host in hmlist:
                channels.Main.kick(
                    user,
                    messages["tempban_kick"].format(nick=user,
                                                    botnick=users.Bot.nick,
                                                    reason=reason))

    # Update any tracking vars that may have changed due to this
    db.init_vars()

    return sid
Esempio n. 3
0
def add_warning(cli, target, amount, actor, reason, notes=None, expires=None, sanctions=None):
    tacc, thm = parse_warning_target(target)
    if tacc is None and thm is None:
        return False

    if actor not in var.USERS and actor != users.Bot.nick:
        return False
    sacc = None
    shm = None
    if actor in var.USERS:
        sacc = var.USERS[actor]["account"]
        shm = actor + "!" + var.USERS[actor]["ident"] + "@" + var.USERS[actor]["host"]

    # Turn expires into a datetime if we were passed a string; note that no error checking is performed here
    if isinstance(expires, str):
        exp_suffix = expires[-1]
        exp_amount = int(expires[:-1])

        if exp_suffix == "d":
            expires = datetime.utcnow() + timedelta(days=exp_amount)
        elif exp_suffix == "h":
            expires = datetime.utcnow() + timedelta(hours=exp_amount)
        elif exp_suffix == "m":
            expires = datetime.utcnow() + timedelta(minutes=exp_amount)
        else:
            raise ValueError("Invalid expiration string")
    elif isinstance(expires, int):
        expires = datetime.utcnow() + timedelta(days=expires)

    # Round expires to the nearest minute (30s rounds up)
    if isinstance(expires, datetime):
        round_add = 0
        if expires.second >= 30:
            round_add = 1
        expires -= timedelta(seconds=expires.second, microseconds=expires.microsecond)
        expires += timedelta(minutes=round_add)

    # determine if we need to automatically add any sanctions
    if sanctions is None:
        sanctions = {}
    prev = db.get_warning_points(tacc, thm)
    cur = prev + amount
    if amount > 0:
        _get_auto_sanctions(sanctions, prev, cur)

    sid = db.add_warning(tacc, thm, sacc, shm, amount, reason, notes, expires)
    if "stasis" in sanctions:
        db.add_warning_sanction(sid, "stasis", sanctions["stasis"])
    if "deny" in sanctions:
        for cmd in sanctions["deny"]:
            db.add_warning_sanction(sid, "deny command", cmd)
    if "tempban" in sanctions:
        # this inserts into the bantrack table too
        (acclist, hmlist) = db.add_warning_sanction(sid, "tempban", sanctions["tempban"])
        cmodes = []
        for acc in acclist:
            cmodes.append(("+b", "{0}{1}".format(var.ACCOUNT_PREFIX, acc)))
        for hm in hmlist:
            cmodes.append(("+b", "*!*@{0}".format(hm.split("@")[1])))
        channels.Main.mode(*cmodes)
        for user in channels.Main.users:
            if user.account in acclist:
                channels.Main.kick(user, messages["tempban_kick"].format(nick=user, botnick=users.Bot.nick, reason=reason))
            elif user.host in hmlist:
                channels.Main.kick(user, messages["tempban_kick"].format(nick=user, botnick=users.Bot.nick, reason=reason))

    # Update any tracking vars that may have changed due to this
    db.init_vars()

    return sid
Esempio n. 4
0
def add_warning(cli, target, amount, actor, reason, notes=None, expires=None, sanctions=None):
    # make 0-point warnings no-op successfully, otherwise we add warnings when things like PART_PENALTY is 0
    if amount == 0:
        return False

    tacc, thm = parse_warning_target(target)
    if tacc is None and thm is None:
        return False

    if actor not in var.USERS and actor != botconfig.NICK:
        return False
    sacc = None
    shm = None
    if actor in var.USERS:
        sacc = var.USERS[actor]["account"]
        shm = actor + "!" + var.USERS[actor]["ident"] + "@" + var.USERS[actor]["host"]

    # Turn expires into a datetime if we were passed a string; note that no error checking is performed here
    if isinstance(expires, str):
        exp_suffix = expires[-1]
        exp_amount = int(expires[:-1])

        if exp_suffix == "d":
            expires = datetime.utcnow() + timedelta(days=exp_amount)
        elif exp_suffix == "h":
            expires = datetime.utcnow() + timedelta(hours=exp_amount)
        elif exp_suffix == "m":
            expires = datetime.utcnow() + timedelta(minutes=exp_amount)
        else:
            raise ValueError("Invalid expiration string")
    elif isinstance(expires, int):
        expires = datetime.utcnow() + timedelta(days=expires)

    # Round expires to the nearest minute (30s rounds up)
    if isinstance(expires, datetime):
        round_add = 0
        if expires.second >= 30:
            round_add = 1
        expires -= timedelta(seconds=expires.second, microseconds=expires.microsecond)
        expires += timedelta(minutes=round_add)

    # determine if we need to automatically add any sanctions
    if sanctions is None:
        sanctions = {}
    prev = db.get_warning_points(tacc, thm)
    cur = prev + amount
    for (mn, mx, sanc) in var.AUTO_SANCTION:
        if (prev < mn and cur >= mn) or (prev >= mn and prev <= mx and cur <= mx):
            if "stasis" in sanc:
                if "stasis" not in sanctions:
                    sanctions["stasis"] = sanc["stasis"]
                else:
                    sanctions["stasis"] = max(sanctions["stasis"], sanc["stasis"])
            if "scalestasis" in sanc:
                (a, b, c) = sanc["scalestasis"]
                amt = (a * cur * cur) + (b * cur) + c
                if "stasis" not in sanctions:
                    sanctions["stasis"] = amt
                else:
                    sanctions["stasis"] = max(sanctions["stasis"], amt)
            if "deny" in sanc:
                if "deny" not in sanctions:
                    sanctions["deny"] = set(sanc["deny"])
                else:
                    sanctions["deny"].update(sanc["deny"])
            if "tempban" in sanc:
                # tempban's param can either be a fixed expiry time or a number
                # which indicates the warning point threshold that the ban will be lifted at
                # if two are set at once, the threshold takes precedence over set times
                # within each category, a larger set time or a lower threshold takes precedence
                exp = None
                ths = None
                if isinstance(sanc["tempban"], str) and sanc["tempban"][-1] in ("d", "h", "m"):
                    amt = int(sanc["tempban"][:-1])
                    dur = sanc["tempban"][-1]
                    if dur == "d":
                        exp = datetime.utcnow() + timedelta(days=amt)
                    elif dur == "h":
                        exp = datetime.utcnow() + timedelta(hours=amt)
                    elif dur == "m":
                        exp = datetime.utcnow() + timedelta(minutes=amt)
                else:
                    ths = int(sanc["tempban"])

                if "tempban" in sanctions:
                    if isinstance(sanctions["tempban"], datetime):
                        if ths is not None:
                            sanctions["tempban"] = ths
                        else:
                            sanctions["tempban"] = max(sanctions["tempban"], exp)
                    elif ths is not None:
                        sanctions["tempban"] = min(sanctions["tempban"], ths)
                elif ths is not None:
                    sanctions["tempban"] = ths
                else:
                    sanctions["tempban"] = exp

    sid = db.add_warning(tacc, thm, sacc, shm, amount, reason, notes, expires)
    if "stasis" in sanctions:
        db.add_warning_sanction(sid, "stasis", sanctions["stasis"])
    if "deny" in sanctions:
        for cmd in sanctions["deny"]:
            db.add_warning_sanction(sid, "deny command", cmd)
    if "tempban" in sanctions:
        # this inserts into the bantrack table too
        (acclist, hmlist) = db.add_warning_sanction(sid, "tempban", sanctions["tempban"])
        cmodes = []
        for acc in acclist:
            cmodes.append(("+b", "{0}{1}".format(var.ACCOUNT_PREFIX, acc)))
        for hm in hmlist:
            cmodes.append(("+b", "*!*@{0}".format(hm)))
        mass_mode(cli, cmodes, [])
        for (nick, user) in var.USERS.items():
            if user["account"] in acclist:
                cli.kick(botconfig.CHANNEL, nick, messages["tempban_kick"].format(nick=nick, botnick=botconfig.NICK, reason=reason))
            elif user["host"] in hmlist:
                cli.kick(botconfig.CHANNEL, nick, messages["tempban_kick"].format(nick=nick, botnick=botconfig.NICK, reason=reason))

    # Update any tracking vars that may have changed due to this
    db.init_vars()

    return sid
Esempio n. 5
0
def add_warning(cli, target, amount, actor, reason, notes=None, expires=None, sanctions=None):
    # make 0-point warnings no-op successfully, otherwise we add warnings when things like PART_PENALTY is 0
    if amount == 0:
        return False

    tacc, thm = parse_warning_target(target)
    if tacc is None and thm is None:
        return False

    if actor not in var.USERS and actor != botconfig.NICK:
        return False
    sacc = None
    shm = None
    if actor in var.USERS:
        sacc = var.USERS[actor]["account"]
        shm = actor + "!" + var.USERS[actor]["ident"] + "@" + var.USERS[actor]["host"]

    # Turn expires into a datetime if we were passed a string; note that no error checking is performed here
    if isinstance(expires, str):
        exp_suffix = expires[-1]
        exp_amount = int(expires[:-1])

        if exp_suffix == "d":
            expires = datetime.utcnow() + timedelta(days=exp_amount)
        elif exp_suffix == "h":
            expires = datetime.utcnow() + timedelta(hours=exp_amount)
        elif exp_suffix == "m":
            expires = datetime.utcnow() + timedelta(minutes=exp_amount)
        else:
            raise ValueError("Invalid expiration string")
    elif isinstance(expires, int):
        expires = datetime.utcnow() + timedelta(days=expires)

    # Round expires to the nearest minute (30s rounds up)
    if isinstance(expires, datetime):
        round_add = 0
        if expires.second >= 30:
            round_add = 1
        expires -= timedelta(seconds=expires.second, microseconds=expires.microsecond)
        expires += timedelta(minutes=round_add)

    # determine if we need to automatically add any sanctions
    if sanctions is None:
        sanctions = {}
    prev = db.get_warning_points(tacc, thm)
    cur = prev + amount
    for (mn, mx, sanc) in var.AUTO_SANCTION:
        if (prev < mn and cur >= mn) or (prev >= mn and prev <= mx and cur <= mx):
            if "stasis" in sanc:
                if "stasis" not in sanctions:
                    sanctions["stasis"] = sanc["stasis"]
                else:
                    sanctions["stasis"] = max(sanctions["stasis"], sanc["stasis"])
            if "scalestasis" in sanc:
                (a, b, c) = sanc["scalestasis"]
                amt = (a * cur * cur) + (b * cur) + c
                if "stasis" not in sanctions:
                    sanctions["stasis"] = amt
                else:
                    sanctions["stasis"] = max(sanctions["stasis"], amt)
            if "deny" in sanc:
                if "deny" not in sanctions:
                    sanctions["deny"] = set(sanc["deny"])
                else:
                    sanctions["deny"].update(sanc["deny"])
            if "tempban" in sanc:
                # tempban's param can either be a fixed expiry time or a number
                # which indicates the warning point threshold that the ban will be lifted at
                # if two are set at once, the threshold takes precedence over set times
                # within each category, a larger set time or a lower threshold takes precedence
                exp = None
                ths = None
                if sanc["tempban"][-1] in ("d", "h", "m"):
                    amt = int(sanc["tempban"][:-1])
                    dur = sanc["tempban"][-1]
                    if dur == "d":
                        exp = datetime.utcnow() + timedelta(days=amt)
                    elif dur == "h":
                        exp = datetime.utcnow() + timedelta(hours=amt)
                    elif dur == "m":
                        exp = datetime.utcnow() + timedelta(minutes=amt)
                else:
                    ths = int(sanc["tempban"])

                if "tempban" in sanctions:
                    if isinstance(sanctions["tempban"], datetime):
                        if ths is not None:
                            sanctions["tempban"] = ths
                        else:
                            sanctions["tempban"] = max(sanctions["tempban"], exp)
                    elif ths is not None:
                        sanctions["tempban"] = min(sanctions["tempban"], ths)
                elif ths is not None:
                    sanctions["tempban"] = ths
                else:
                    sanctions["tempban"] = exp

    sid = db.add_warning(tacc, thm, sacc, shm, amount, reason, notes, expires)
    if "stasis" in sanctions:
        db.add_warning_sanction(sid, "stasis", sanctions["stasis"])
    if "deny" in sanctions:
        for cmd in sanctions["deny"]:
            db.add_warning_sanction(sid, "deny command", cmd)
    if "tempban" in sanctions:
        # this inserts into the bantrack table too
        (acclist, hmlist) = db.add_warning_sanction(sid, "tempban", sanctions["tempban"])
        cmodes = []
        for acc in acclist:
            cmodes.append(("+b", "{0}{1}".format(var.ACCOUNT_PREFIX, acc)))
        for hm in hmlist:
            cmodes.append(("+b", "*!*@{0}".format(hm)))
        mass_mode(cli, cmodes, [])
        for (nick, user) in var.USERS.items():
            if user["account"] in acclist:
                cli.kick(botconfig.CHANNEL, nick, messages["tempban_kick"].format(nick=nick, botnick=botconfig.NICK, reason=reason))
            elif user["host"] in hmlist:
                cli.kick(botconfig.CHANNEL, nick, messages["tempban_kick"].format(nick=nick, botnick=botconfig.NICK, reason=reason))

    # Update any tracking vars that may have changed due to this
    db.init_vars()

    return sid