summaryrefslogtreecommitdiff
path: root/src/plugins/relay/weechat/relay-weechat-nicklist.c
blob: 5449663e1d9557c8300dabcabefcd10d4b45db37 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * relay-weechat-nicklist.c - nicklist functions for WeeChat protocol
 *
 * Copyright (C) 2003-2023 Sébastien Helleu <flashcode@flashtux.org>
 *
 * This file is part of WeeChat, the extensible chat client.
 *
 * WeeChat is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * WeeChat is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with WeeChat.  If not, see <https://www.gnu.org/licenses/>.
 */

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

#include "../../weechat-plugin.h"
#include "../relay.h"
#include "relay-weechat.h"
#include "relay-weechat-nicklist.h"


/*
 * Builds a new nicklist structure (to store nicklist diffs).
 *
 * Returns pointer to new nicklist structure, NULL if error.
 */

struct t_relay_weechat_nicklist *
relay_weechat_nicklist_new ()
{
    struct t_relay_weechat_nicklist *new_nicklist;

    new_nicklist = malloc (sizeof (*new_nicklist));
    if (!new_nicklist)
        return NULL;

    new_nicklist->nicklist_count = 0;
    new_nicklist->items_count = 0;
    new_nicklist->items = NULL;

    return new_nicklist;
}

/*
 * Adds a nicklist item in nicklist structure.
 */

void
relay_weechat_nicklist_add_item (struct t_relay_weechat_nicklist *nicklist,
                                 char diff, struct t_gui_nick_group *group,
                                 struct t_gui_nick *nick)
{
    struct t_relay_weechat_nicklist_item *new_items, *ptr_item;
    struct t_hdata *hdata;
    const char *str;
    int i;

    /*
     * check if the last "parent_group" (with diff = '^') of items is the same
     * as this one: if yes, don't add this parent group
     */
    if ((diff == RELAY_WEECHAT_NICKLIST_DIFF_PARENT)
        && (nicklist->items_count > 0))
    {
        for (i = nicklist->items_count - 1; i >= 0; i--)
        {
            if (nicklist->items[i].diff == RELAY_WEECHAT_NICKLIST_DIFF_PARENT)
            {
                if (nicklist->items[i].pointer == group)
                    return;
                break;
            }
        }
    }

    new_items = realloc (nicklist->items,
                         (nicklist->items_count + 1) * sizeof (new_items[0]));
    if (!new_items)
        return;

    nicklist->items = new_items;
    ptr_item = &(nicklist->items[nicklist->items_count]);
    if (group)
    {
        hdata = weechat_hdata_get ("nick_group");
        ptr_item->pointer = group;
    }
    else
    {
        hdata = weechat_hdata_get ("nick");
        ptr_item->pointer = nick;
    }
    ptr_item->diff = diff;
    ptr_item->group = (group) ? 1 : 0;
    ptr_item->visible = weechat_hdata_integer (hdata, ptr_item->pointer, "visible");
    ptr_item->level = (group) ? weechat_hdata_integer (hdata, ptr_item->pointer, "level") : 0;
    str = weechat_hdata_string (hdata, ptr_item->pointer, "name");
    ptr_item->name = (str) ? strdup (str) : NULL;
    str = weechat_hdata_string (hdata, ptr_item->pointer, "color");
    ptr_item->color = (str) ? strdup (str) : NULL;
    str = weechat_hdata_string (hdata, ptr_item->pointer, "prefix");
    ptr_item->prefix = (str) ? strdup (str) : NULL;
    str = weechat_hdata_string (hdata, ptr_item->pointer, "prefix_color");
    ptr_item->prefix_color = (str) ? strdup (str) : NULL;

    nicklist->items_count++;
}

/*
 * Frees a nicklist_item structure.
 */

void
relay_weechat_nicklist_item_free (struct t_relay_weechat_nicklist_item *item)
{
    if (!item)
        return;

    if (item->name)
        free (item->name);
    if (item->color)
        free (item->color);
    if (item->prefix)
        free (item->prefix);
    if (item->prefix_color)
        free (item->prefix_color);
}

/*
 * Frees a new nicklist structure.
 */

void
relay_weechat_nicklist_free (struct t_relay_weechat_nicklist *nicklist)
{
    int i;

    if (!nicklist)
        return;

    /* free items */
    if (nicklist->items_count > 0)
    {
        for (i = 0; i < nicklist->items_count; i++)
        {
            relay_weechat_nicklist_item_free (&(nicklist->items[i]));
        }
        free (nicklist->items);
    }

    free (nicklist);
}