Example #1
0
def initialize_bridge(bot: Bot, update: Updater, user_data: dict):

    session = Session.get_from(user_data)

    plugin = Plugin("plugins/_bridge_initialization")

    name, ip, stdout, stderr = next(plugin.run(session))
    view.plugin_output(name,
                       ip,
                       "Bridge Initialization",
                       stdout,
                       stderr,
                       hide_header=True).reply(update)
Example #2
0
def update_computers_status(user_data: dict):

    session = Session.get_from(user_data)

    plugin = Plugin("plugins/_computers_status")
    _, _, stdout, _ = next(plugin.run(session))
    # The stdout has the format `{ip} is {status}`

    # TODO: Remove n^2 loop by adding dicts to the computers_json structure
    for line in stdout.splitlines():
        ip, _, status = line.split()

        for computer in session.computers:
            if ip == computer.ip:
                computer.status = status
Example #3
0
def start_plugin_from_callback(bot: Bot, update: Updater, user_data: dict):
    query = update.callback_query

    # Plugin path in server
    plugin_path_server = re.search(State.START_PLUGIN, query.data).group(1)

    user_data["plugin"] = Plugin(plugin_path_server)

    view.plugin_start(user_data["plugin"].name).edit(update)

    return collect_arguments(bot, update, user_data)
Example #4
0
def update_ips(bot: Bot, update: Updater, user_data: dict):
    """
    Get all the macs and its associated ips from the local network. Then,
    iterate through all the computers in :obj:`Computers`. If one of the
    computer macs match with one of the local macs, update its associated ip to
    the new value.

    Args:
        bot (:obj:`telegram.bot.Bot`): The telegram bot instance.
        update (:obj:`telegram.ext.update.Updater`): The Updater associated to
            the bot.
        user_data (:obj:`dict`): The dictionary with user variables.
    """
    session = Session.get_from(user_data)

    # Get all the local ips for every local mac
    plugin = Plugin("plugins/_local_arp_scan")

    # Change view to executing
    view.plugin_start(plugin.name).edit(update)

    _, _, stdout, _ = next(plugin.run(session))

    local_ips = {}
    for line in stdout.splitlines():
        ip, mac = line.strip().split()
        local_ips[mac] = ip

    for computer in session.computers.get_included_computers():
        if computer.mac.lower() in local_ips:
            last_ip = computer.ip
            computer.ip = local_ips[computer.mac.lower()]

            view.update_ip_output(computer, last_ip).reply(update)

    session.computers.save()

    menu.new_main(bot, update, user_data)
Example #5
0
def main(bot: Bot, update: Updater, user_data: dict):
    """
    Edit the last message to show the main menu. Depending on if the user is
    connected or disconnected the view may change.

    Args:
        bot (:obj:`telegram.bot.Bot`): The telegram bot instance.
        update (:obj:`telegram.ext.update.Updater`): The Updater associated to
            the bot.
        user_data (:obj:`dict`): The dictionary with user variables.
    """

    session = Session.get_from(user_data)

    # View
    if not session.connected:
        view.not_connected().edit(update)
    else:
        view.connected(session, Plugin.get_local_plugins()).edit(update)
Example #6
0
def start_plugin_from_download(bot: Bot, update: Updater, user_data: dict):

    session = Session.get_from(user_data)
    message = update.message

    if not session.connected:
        message.reply_text(
            "You must be connected to a bridge computer before sending files!")
        return ConversationHandler.END

    # Download the file
    file_object = message.document.get_file()
    download_path = f"{states.config_file.server_tmp_dir}/{message.document.file_name}"

    file_object.download(download_path)

    # Make downloaded file executable by the user
    os.chmod(download_path, 0o764)

    user_data["plugin"] = Plugin(download_path)

    view.plugin_start(user_data["plugin"].name).reply(update)

    return collect_arguments(bot, update, user_data)