/*
 * SPDX-FileCopyrightText: 2003-2026 Sébastien Helleu <flashcode@flashtux.org>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

/* Completion for relay command */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "../weechat-plugin.h"
#include "relay.h"
#include "relay-remote.h"
#include "relay-server.h"


/*
 * Add protocol and name to completion list.
 */

int
relay_completion_protocol_name_cb (const void *pointer, void *data,
                                   const char *completion_item,
                                   struct t_gui_buffer *buffer,
                                   struct t_gui_completion *completion)
{
    struct t_infolist *infolist;
    char protocol_name[1024];

    /* Make C compiler happy. */
    (void) pointer;
    (void) data;
    (void) buffer;
    (void) completion_item;

    /* Relay "irc" */
    infolist = weechat_infolist_get ("irc_server", NULL, NULL);
    if (infolist)
    {
        while (weechat_infolist_next (infolist))
        {
            /* TCP socket */
            snprintf (protocol_name, sizeof (protocol_name), "irc.%s",
                      weechat_infolist_string (infolist, "name"));
            weechat_completion_list_add (completion, protocol_name,
                                         0, WEECHAT_LIST_POS_SORT);
            snprintf (protocol_name, sizeof (protocol_name), "tls.irc.%s",
                      weechat_infolist_string (infolist, "name"));
            weechat_completion_list_add (completion, protocol_name,
                                         0, WEECHAT_LIST_POS_SORT);
            /* UNIX domain socket */
            snprintf (protocol_name, sizeof (protocol_name), "unix.irc.%s",
                      weechat_infolist_string (infolist, "name"));
            weechat_completion_list_add (completion, protocol_name,
                                         0, WEECHAT_LIST_POS_SORT);
            snprintf (protocol_name, sizeof (protocol_name), "unix.tls.irc.%s",
                      weechat_infolist_string (infolist, "name"));
            weechat_completion_list_add (completion, protocol_name,
                                         0, WEECHAT_LIST_POS_SORT);
        }
        weechat_infolist_free (infolist);
    }

    /* Relay "api" */
    weechat_completion_list_add (completion, "api",
                                 0, WEECHAT_LIST_POS_SORT);
    weechat_completion_list_add (completion, "tls.api",
                                 0, WEECHAT_LIST_POS_SORT);
    weechat_completion_list_add (completion, "unix.api",
                                 0, WEECHAT_LIST_POS_SORT);
    weechat_completion_list_add (completion, "unix.tls.api",
                                 0, WEECHAT_LIST_POS_SORT);

    /* Relay "weechat" */
    weechat_completion_list_add (completion, "weechat",
                                 0, WEECHAT_LIST_POS_SORT);
    weechat_completion_list_add (completion, "tls.weechat",
                                 0, WEECHAT_LIST_POS_SORT);
    weechat_completion_list_add (completion, "unix.weechat",
                                 0, WEECHAT_LIST_POS_SORT);
    weechat_completion_list_add (completion, "unix.tls.weechat",
                                 0, WEECHAT_LIST_POS_SORT);

    return WEECHAT_RC_OK;
}

/*
 * Add protocol and name of current relays to completion list.
 */

int
relay_completion_relays_cb (const void *pointer, void *data,
                            const char *completion_item,
                            struct t_gui_buffer *buffer,
                            struct t_gui_completion *completion)
{
    struct t_relay_server *ptr_server;

    /* Make C compiler happy. */
    (void) pointer;
    (void) data;
    (void) buffer;
    (void) completion_item;

    for (ptr_server = relay_servers; ptr_server;
         ptr_server = ptr_server->next_server)
    {
        weechat_completion_list_add (completion,
                                     ptr_server->protocol_string,
                                     0, WEECHAT_LIST_POS_SORT);
    }

    return WEECHAT_RC_OK;
}

/*
 * Add free ports to completion list.
 */

int
relay_completion_free_port_cb (const void *pointer, void *data,
                               const char *completion_item,
                               struct t_gui_buffer *buffer,
                               struct t_gui_completion *completion)
{
    struct t_relay_server *ptr_server;
    char str_port[16];
    int port_max;

    /* Make C compiler happy. */
    (void) pointer;
    (void) data;
    (void) buffer;
    (void) completion_item;

    port_max = -1;
    for (ptr_server = relay_servers; ptr_server;
         ptr_server = ptr_server->next_server)
    {
        if (ptr_server->port > port_max)
            port_max = ptr_server->port;
    }
    if (port_max < 0)
        port_max = 8000 - 1;

    snprintf (str_port, sizeof (str_port), "%d", port_max + 1);
    weechat_completion_list_add (completion, str_port,
                                 0, WEECHAT_LIST_POS_SORT);

    return WEECHAT_RC_OK;
}

/*
 * Add relay remotes to completion list.
 */

int
relay_completion_remotes_cb (const void *pointer, void *data,
                             const char *completion_item,
                             struct t_gui_buffer *buffer,
                             struct t_gui_completion *completion)
{
    struct t_relay_remote *ptr_remote;

    /* Make C compiler happy. */
    (void) pointer;
    (void) data;
    (void) buffer;
    (void) completion_item;

    for (ptr_remote = relay_remotes; ptr_remote;
         ptr_remote = ptr_remote->next_remote)
    {
        weechat_completion_list_add (completion,
                                     ptr_remote->name,
                                     0, WEECHAT_LIST_POS_SORT);
    }

    return WEECHAT_RC_OK;
}

/*
 * Hook completions.
 */

void
relay_completion_init (void)
{
    weechat_hook_completion ("relay_protocol_name",
                             N_("all possible protocol.name for relay plugin"),
                             &relay_completion_protocol_name_cb, NULL, NULL);
    weechat_hook_completion ("relay_relays",
                             N_("protocol.name of current relays for relay "
                                "plugin"),
                             &relay_completion_relays_cb, NULL, NULL);
    weechat_hook_completion ("relay_free_port",
                             N_("first free port for relay plugin"),
                             &relay_completion_free_port_cb, NULL, NULL);
    weechat_hook_completion ("relay_remotes",
                             N_("relay remotes"),
                             &relay_completion_remotes_cb, NULL, NULL);
}
