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

/* WeeChat print hook */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

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

#include "../weechat.h"
#include "../core-hook.h"
#include "../core-hdata.h"
#include "../core-infolist.h"
#include "../core-log.h"
#include "../core-string.h"
#include "../../gui/gui-buffer.h"
#include "../../gui/gui-color.h"
#include "../../gui/gui-line.h"
#include "../../plugins/plugin.h"


/*
 * Return description of hook.
 *
 * Note: result must be freed after use.
 */

char *
hook_print_get_description (struct t_hook *hook)
{
    char str_desc[1024];

    if (HOOK_PRINT(hook, buffer))
    {
        snprintf (str_desc, sizeof (str_desc),
                  "buffer: %s, message: %s%s%s",
                  HOOK_PRINT(hook, buffer)->name,
                  (HOOK_PRINT(hook, message)) ? "\"" : "",
                  (HOOK_PRINT(hook, message)) ? HOOK_PRINT(hook, message) : "(none)",
                  (HOOK_PRINT(hook, message)) ? "\"" : "");
    }
    else
    {
        snprintf (str_desc, sizeof (str_desc),
                  "message: %s%s%s",
                  (HOOK_PRINT(hook, message)) ? "\"" : "",
                  (HOOK_PRINT(hook, message)) ? HOOK_PRINT(hook, message) : "(none)",
                  (HOOK_PRINT(hook, message)) ? "\"" : "");
    }

    return strdup (str_desc);
}

/*
 * Hook a message printed by WeeChat.
 *
 * Return pointer to new hook, NULL if error.
 */

struct t_hook *
hook_print (struct t_weechat_plugin *plugin, struct t_gui_buffer *buffer,
            const char *tags, const char *message, int strip_colors,
            t_hook_callback_print *callback,
            const void *callback_pointer,
            void *callback_data)
{
    struct t_hook *new_hook;
    struct t_hook_print *new_hook_print;

    if (!callback)
        return NULL;

    new_hook = malloc (sizeof (*new_hook));
    if (!new_hook)
        return NULL;
    new_hook_print = malloc (sizeof (*new_hook_print));
    if (!new_hook_print)
    {
        free (new_hook);
        return NULL;
    }

    hook_init_data (new_hook, plugin, HOOK_TYPE_PRINT, HOOK_PRIORITY_DEFAULT,
                    callback_pointer, callback_data);

    new_hook->hook_data = new_hook_print;
    new_hook_print->callback = callback;
    new_hook_print->buffer = buffer;
    new_hook_print->tags_array = string_split_tags (tags,
                                                    &new_hook_print->tags_count);
    new_hook_print->message = (message) ? strdup (message) : NULL;
    new_hook_print->strip_colors = strip_colors;

    hook_add_to_list (new_hook);

    return new_hook;
}

/*
 * Execute a print hook.
 */

void
hook_print_exec (struct t_gui_buffer *buffer, struct t_gui_line *line)
{
    struct t_hook *ptr_hook, *next_hook;
    struct t_hook_exec_cb hook_exec_cb;
    char *prefix_no_color, *message_no_color;

    if (!weechat_hooks[HOOK_TYPE_PRINT])
        return;

    if (!line->data->message)
        return;

    prefix_no_color = (line->data->prefix) ?
        gui_color_decode (line->data->prefix, NULL) : NULL;

    message_no_color = gui_color_decode (line->data->message, NULL);
    if (!message_no_color)
    {
        free (prefix_no_color);
        return;
    }

    hook_exec_start ();

    ptr_hook = weechat_hooks[HOOK_TYPE_PRINT];
    while (ptr_hook)
    {
        next_hook = ptr_hook->next_hook;

        if (!ptr_hook->deleted
            && !ptr_hook->running
            && (!HOOK_PRINT(ptr_hook, buffer)
                || (buffer == HOOK_PRINT(ptr_hook, buffer)))
            && (!HOOK_PRINT(ptr_hook, message)
                || !HOOK_PRINT(ptr_hook, message)[0]
                || string_strcasestr (prefix_no_color, HOOK_PRINT(ptr_hook, message))
                || string_strcasestr (message_no_color, HOOK_PRINT(ptr_hook, message)))
            && (!HOOK_PRINT(ptr_hook, tags_array)
                || gui_line_match_tags (line->data,
                                        HOOK_PRINT(ptr_hook, tags_count),
                                        HOOK_PRINT(ptr_hook, tags_array))))
        {
            /* run callback */
            hook_callback_start (ptr_hook, &hook_exec_cb);
            (void) (HOOK_PRINT(ptr_hook, callback))
                (ptr_hook->callback_pointer,
                 ptr_hook->callback_data,
                 buffer,
                 line->data->date,
                 line->data->date_usec,
                 line->data->tags_count,
                 (const char **)line->data->tags_array,
                 (int)line->data->displayed, (int)line->data->highlight,
                 (HOOK_PRINT(ptr_hook, strip_colors)) ? prefix_no_color : line->data->prefix,
                 (HOOK_PRINT(ptr_hook, strip_colors)) ? message_no_color : line->data->message);
            hook_callback_end (ptr_hook, &hook_exec_cb);
        }

        ptr_hook = next_hook;
    }

    free (prefix_no_color);
    free (message_no_color);

    hook_exec_end ();
}

/*
 * Free data in a print hook.
 */

void
hook_print_free_data (struct t_hook *hook)
{
    if (!hook || !hook->hook_data)
        return;

    if (HOOK_PRINT(hook, tags_array))
    {
        string_free_split_tags (HOOK_PRINT(hook, tags_array));
        HOOK_PRINT(hook, tags_array) = NULL;
    }
    if (HOOK_PRINT(hook, message))
    {
        free (HOOK_PRINT(hook, message));
        HOOK_PRINT(hook, message) = NULL;
    }

    free (hook->hook_data);
    hook->hook_data = NULL;
}

/*
 * Return hdata for print hook.
 */

struct t_hdata *
hook_print_hdata_hook_print_cb (const void *pointer, void *data,
                                const char *hdata_name)
{
    struct t_hdata *hdata;

    /* make C compiler happy */
    (void) pointer;
    (void) data;

    hdata = hdata_new (NULL, hdata_name, NULL, NULL, 0, 0, NULL, NULL);
    if (hdata)
    {
        HDATA_VAR(struct t_hook_print, callback, POINTER, 0, NULL, NULL);
        HDATA_VAR(struct t_hook_print, buffer, POINTER, 0, NULL, "buffer");
        HDATA_VAR(struct t_hook_print, tags_count, INTEGER, 0, NULL, NULL);
        HDATA_VAR(struct t_hook_print, tags_array, STRING, 0, "*,tags_count", NULL);
        HDATA_VAR(struct t_hook_print, message, STRING, 0, NULL, NULL);
        HDATA_VAR(struct t_hook_print, strip_colors, INTEGER, 0, NULL, NULL);
    }
    return hdata;
}

/*
 * Add print hook data in the infolist item.
 *
 * Return:
 *   1: OK
 *   0: error
 */

int
hook_print_add_to_infolist (struct t_infolist_item *item,
                            struct t_hook *hook)
{
    if (!item || !hook || !hook->hook_data)
        return 0;

    if (!infolist_new_var_pointer (item, "callback", HOOK_PRINT(hook, callback)))
        return 0;
    if (!infolist_new_var_pointer (item, "buffer", HOOK_PRINT(hook, buffer)))
        return 0;
    if (!infolist_new_var_integer (item, "tags_count", HOOK_PRINT(hook, tags_count)))
        return 0;
    if (!infolist_new_var_pointer (item, "tags_array", HOOK_PRINT(hook, tags_array)))
        return 0;
    if (!infolist_new_var_string (item, "message", HOOK_PRINT(hook, message)))
        return 0;
    if (!infolist_new_var_integer (item, "strip_colors", HOOK_PRINT(hook, strip_colors)))
        return 0;

    return 1;
}

/*
 * Print print hook data in WeeChat log file (usually for crash dump).
 */

void
hook_print_print_log (struct t_hook *hook)
{
    char **ptr_tag;
    int i, j;

    if (!hook || !hook->hook_data)
        return;

    log_printf ("  print data:");
    log_printf ("    callback. . . . . . . : %p", HOOK_PRINT(hook, callback));
    log_printf ("    buffer. . . . . . . . : %p", HOOK_PRINT(hook, buffer));
    log_printf ("    tags_count. . . . . . : %d", HOOK_PRINT(hook, tags_count));
    log_printf ("    tags_array. . . . . . : %p", HOOK_PRINT(hook, tags_array));
    if (HOOK_PRINT(hook, tags_array))
    {
        for (i = 0; i < HOOK_PRINT(hook, tags_count); i++)
        {
            for (ptr_tag = HOOK_PRINT(hook, tags_array)[i], j = 0; *ptr_tag;
                 ptr_tag++, j++)
            {
                log_printf ("      tags_array[%03d][%03d]: '%s'", i, j, *ptr_tag);
            }
        }
    }
    log_printf ("    message . . . . . . . : '%s'", HOOK_PRINT(hook, message));
    log_printf ("    strip_colors. . . . . : %d", HOOK_PRINT(hook, strip_colors));
}
