<commit_before>import asyncio
import random
import string

import plugins


_externals = { "authorisation": False }


def _initialise(bot):
    plugins.register_admin_command(["addme", "addusers", "createconversation", "refresh"])


def addusers(bot, event, *args):
    """adds user(s) into a chat
    Usage: /bot addusers
    <user id(s)>
    [into <chat id>]"""
    list_add = []
    target_conv = event.conv_id

    state = ["adduser"]

    for parameter in args:
        if parameter == "into":
            state.append("targetconv")
        else:
            if state[-1] == "adduser":
                list_add.append(parameter)
            elif state[-1] == "targetconv":
                target_conv = parameter
                state.pop()
            else:
                raise ValueError("UNKNOWN STATE: {}".format(state[-1]))

    list_add = list(set(list_add))
    print("addusers: {} into conversation {}".format(list_add, target_conv))
    if len(list_add) > 0:
        yield from bot._client.adduser(target_conv, list_add)


def addme(bot, event, *args):
    """add yourself into a chat
    Usage: /bot addme <conv id>"""
    if len(args) == 1:
        target_conv = args[0]
        yield from addusers(bot, event, *[event.user.id_.chat_id, "into", target_conv])


def createconversation(bot, event, *args):
    """create a new conversation with the bot and the specified user(s)
    Usage: /bot createconversation <user id(s)>"""
    parameters = list(args)

    force_group = False # default: defer to hangups client decision

    if "group" in parameters:
        parameters.remove("group")
        force_group = True

    user_ids = list(set(parameters))
    print("createconversation: {}".format(user_ids))

    response = yield from bot._client.createconversation(user_ids, force_group)
    new_conversation_id = response['conversation']['id']['id']
    bot.send_html_to_conversation(new_conversation_id, "<i>conversation created</i>")


def refresh(bot, event, *args):
    """refresh a chat
    Usage: /bot refresh
    conversation <chat id>
    [without <user id(s)>]
    [add <user id(s)>] [quietly]"""
    parameters = list(args)

    if _externals["authorisation"] not in parameters:
        _externals["authorisation"] = False
        initiator_1on1 = bot.get_1on1_conversation(event.user.id_.chat_id)
        if initiator_1on1:
            _externals["authorisation"] = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(8))
            bot.send_html_to_conversation(initiator_1on1, "<i>are you sure? execute the command again and append this key: {}</i>".format(_externals["authorisation"]))
        else:
            bot.send_html_to_conversation(event.conv_id, "<i>you must have a 1on1 with the bot first</i>")
    else:
        parameters.remove(_externals["authorisation"])
        _externals["authorisation"] = False

        quietly = False
        target_conv = False
        list_remove = []
        list_add = []

        state = ["adduser"]

        print("refresh: {}".format(parameters))

        for parameter in parameters:
            if parameter == "without":
                state.append("removeuser")
            elif parameter == "add":
                state.append("adduser")<commit_msg>convtools plugin: bot.get_1on1(), part of #290<commit_after>import asyncio
import random
import string

import plugins


_externals = { "authorisation": False }


def _initialise(bot):
    plugins.register_admin_command(["addme", "addusers", "createconversation", "refresh"])


def addusers(bot, event, *args):
    """adds user(s) into a chat
    Usage: /bot addusers
    <user id(s)>
    [into <chat id>]"""
    list_add = []
    target_conv = event.conv_id

    state = ["adduser"]

    for parameter in args:
        if parameter == "into":
            state.append("targetconv")
        else:
            if state[-1] == "adduser":
                list_add.append(parameter)
            elif state[-1] == "targetconv":
                target_conv = parameter
                state.pop()
            else:
                raise ValueError("UNKNOWN STATE: {}".format(state[-1]))

    list_add = list(set(list_add))
    print("addusers: {} into conversation {}".format(list_add, target_conv))
    if len(list_add) > 0:
        yield from bot._client.adduser(target_conv, list_add)


def addme(bot, event, *args):
    """add yourself into a chat
    Usage: /bot addme <conv id>"""
    if len(args) == 1:
        target_conv = args[0]
        yield from addusers(bot, event, *[event.user.id_.chat_id, "into", target_conv])


def createconversation(bot, event, *args):
    """create a new conversation with the bot and the specified user(s)
    Usage: /bot createconversation <user id(s)>"""
    parameters = list(args)

    force_group = False # default: defer to hangups client decision

    if "group" in parameters:
        parameters.remove("group")
        force_group = True

    user_ids = list(set(parameters))
    print("createconversation: {}".format(user_ids))

    response = yield from bot._client.createconversation(user_ids, force_group)
    new_conversation_id = response['conversation']['id']['id']
    bot.send_html_to_conversation(new_conversation_id, "<i>conversation created</i>")


def refresh(bot, event, *args):
    """refresh a chat
    Usage: /bot refresh
    conversation <chat id>
    [without <user id(s)>]
    [add <user id(s)>] [quietly]"""
    parameters = list(args)

    if _externals["authorisation"] not in parameters:
        _externals["authorisation"] = False
        initiator_1on1 = yield from bot.get_1to1(event.user.id_.chat_id)
        if initiator_1on1:
            _externals["authorisation"] = ''.join(random.SystemRandom().choice(string.ascii_uppercase + string.digits) for _ in range(8))
            bot.send_html_to_conversation(initiator_1on1, "<i>are you sure? execute the command again and append this key: {}</i>".format(_externals["authorisation"]))
        else:
            bot.send_html_to_conversation(event.conv_id, "<i>you must have a 1on1 with the bot first</i>")
    else:
        parameters.remove(_externals["authorisation"])
        _externals["authorisation"] = False

        quietly = False
        target_conv = False
        list_remove = []
        list_add = []

        state = ["adduser"]

        print("refresh: {}".format(parameters))

        for parameter in parameters:
            if parameter == "without":
                state.append("removeuser")
            elif parameter == "add":
                state.append("adduser")
{'fraction_non_alphanumeric': 0.1284738041002278, 'fraction_numerical': 0.00592255125284738, 'mean_word_length': 2.8718400940623163, 'pattern_counts': {'":': 12, '<': 29, '<?xml version=': 0, '>': 31, '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': '4602676', 'n_tokens_mistral': 2021, 'n_tokens_neox': 1944, 'n_words': 492}