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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
|
/*
* irc-modelist.c - channel mode list management for IRC plugin
*
* Copyright (C) 2015 Simmo Saan <simmo.saan@gmail.com>
* Copyright (C) 2018-2019 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 <stddef.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "../weechat-plugin.h"
#include "irc.h"
#include "irc-channel.h"
#include "irc-modelist.h"
/*
* Checks if a modelist pointer is valid for a channel.
*
* Returns:
* 1: modelist exists for channel
* 0: modelist does not exist for channel
*/
int
irc_modelist_valid (struct t_irc_channel *channel,
struct t_irc_modelist *modelist)
{
struct t_irc_modelist *ptr_modelist;
if (!channel || !modelist)
return 0;
for (ptr_modelist = channel->modelists; ptr_modelist;
ptr_modelist = ptr_modelist->next_modelist)
{
if (ptr_modelist == modelist)
return 1;
}
/* modelist not found */
return 0;
}
/*
* Searches for a modelist by type.
*
* Returns pointer to modelist found, NULL if not found.
*/
struct t_irc_modelist *
irc_modelist_search (struct t_irc_channel *channel, char type)
{
struct t_irc_modelist *ptr_modelist;
if (!channel)
return NULL;
for (ptr_modelist = channel->modelists; ptr_modelist;
ptr_modelist = ptr_modelist->next_modelist)
{
if (ptr_modelist->type == type)
return ptr_modelist;
}
return NULL;
}
/*
* Creates a new modelist in a channel.
*
* Returns pointer to new modelist, NULL if error.
*/
struct t_irc_modelist *
irc_modelist_new (struct t_irc_channel *channel, char type)
{
struct t_irc_modelist *new_modelist;
/* alloc memory for new modelist */
if ((new_modelist = malloc (sizeof (*new_modelist))) == NULL)
{
weechat_printf (NULL,
_("%s%s: cannot allocate new modelist"),
weechat_prefix ("error"), IRC_PLUGIN_NAME);
return NULL;
}
/* initialize new modelist */
new_modelist->type = type;
new_modelist->state = IRC_MODELIST_STATE_EMPTY;
new_modelist->items = NULL;
new_modelist->last_item = NULL;
/* add new modelist to channel */
new_modelist->prev_modelist = channel->last_modelist;
new_modelist->next_modelist = NULL;
if (channel->modelists)
(channel->last_modelist)->next_modelist = new_modelist;
else
channel->modelists = new_modelist;
channel->last_modelist = new_modelist;
/* all is OK, return address of new modelist */
return new_modelist;
}
/*
* Frees a modelist and remove it from channel.
*/
void
irc_modelist_free (struct t_irc_channel *channel,
struct t_irc_modelist *modelist)
{
struct t_irc_modelist *new_modelists;
if (!channel || !modelist)
return;
/* remove modelist from channel modelists */
if (channel->last_modelist == modelist)
channel->last_modelist = modelist->prev_modelist;
if (modelist->prev_modelist)
{
(modelist->prev_modelist)->next_modelist = modelist->next_modelist;
new_modelists = channel->modelists;
}
else
new_modelists = modelist->next_modelist;
if (modelist->next_modelist)
(modelist->next_modelist)->prev_modelist = modelist->prev_modelist;
/* free linked lists */
irc_modelist_item_free_all (modelist);
free (modelist);
channel->modelists = new_modelists;
}
/*
* Frees all modelists for a channel.
*/
void
irc_modelist_free_all (struct t_irc_channel *channel)
{
while (channel->modelists)
{
irc_modelist_free (channel, channel->modelists);
}
}
/*
* Checks if a modelist item pointer is valid for a modelist.
*
* Returns:
* 1: item exists for modelist
* 0: item does not exist for modelist
*/
int
irc_modelist_item_valid (struct t_irc_modelist *modelist,
struct t_irc_modelist_item *item)
{
struct t_irc_modelist_item *ptr_item;
if (!modelist || !item)
return 0;
for (ptr_item = modelist->items; ptr_item;
ptr_item = ptr_item->next_item)
{
if (ptr_item == item)
return 1;
}
/* item not found */
return 0;
}
/*
* Searches for an item by mask.
*
* Returns pointer to item found, NULL if not found.
*/
struct t_irc_modelist_item *
irc_modelist_item_search_mask (struct t_irc_modelist *modelist, const char *mask)
{
struct t_irc_modelist_item *ptr_item;
if (!modelist || !mask)
return NULL;
for (ptr_item = modelist->items; ptr_item;
ptr_item = ptr_item->next_item)
{
if (strcmp (ptr_item->mask, mask) == 0)
return ptr_item;
}
return NULL;
}
/*
* Searches for an item by number.
*
* Returns pointer to item found, NULL if not found.
*/
struct t_irc_modelist_item *
irc_modelist_item_search_number (struct t_irc_modelist *modelist, int number)
{
struct t_irc_modelist_item *ptr_item;
if (!modelist)
return NULL;
for (ptr_item = modelist->items; ptr_item;
ptr_item = ptr_item->next_item)
{
if (ptr_item->number == number)
return ptr_item;
}
return NULL;
}
/*
* Creates a new item in a modelist.
*
* Returns pointer to new item, NULL if error.
*/
struct t_irc_modelist_item *
irc_modelist_item_new (struct t_irc_modelist *modelist,
const char *mask, const char *setter, time_t datetime)
{
struct t_irc_modelist_item *new_item;
if (!mask)
return NULL;
/* alloc memory for new item */
if ((new_item = malloc (sizeof (*new_item))) == NULL)
{
weechat_printf (NULL,
_("%s%s: cannot allocate new modelist item"),
weechat_prefix ("error"), IRC_PLUGIN_NAME);
return NULL;
}
/* initialize new item */
new_item->number = (modelist->last_item) ?
modelist->last_item->number + 1 : 0;
new_item->mask = strdup (mask);
new_item->setter = (setter) ? strdup (setter) : NULL;
new_item->datetime = datetime;
/* add new item to modelist */
new_item->prev_item = modelist->last_item;
new_item->next_item = NULL;
if (modelist->items)
(modelist->last_item)->next_item = new_item;
else
modelist->items = new_item;
modelist->last_item = new_item;
if ((modelist->state == IRC_MODELIST_STATE_EMPTY) ||
(modelist->state == IRC_MODELIST_STATE_RECEIVED))
{
modelist->state = IRC_MODELIST_STATE_MODIFIED;
}
/* all is OK, return address of new item */
return new_item;
}
/*
* Frees an item and remove it from modelist.
*/
void
irc_modelist_item_free (struct t_irc_modelist *modelist,
struct t_irc_modelist_item *item)
{
struct t_irc_modelist_item *new_items;
if (!modelist || !item)
return;
/* remove item from modelist list */
if (modelist->last_item == item)
modelist->last_item = item->prev_item;
if (item->prev_item)
{
(item->prev_item)->next_item = item->next_item;
new_items = modelist->items;
}
else
new_items = item->next_item;
if (item->next_item)
(item->next_item)->prev_item = item->prev_item;
/* free item data */
if (item->mask)
free (item->mask);
if (item->setter)
free (item->setter);
free (item);
modelist->items = new_items;
if (modelist->state == IRC_MODELIST_STATE_RECEIVED)
modelist->state = IRC_MODELIST_STATE_MODIFIED;
}
/*
* Frees all items for a modelist.
*/
void
irc_modelist_item_free_all (struct t_irc_modelist *modelist)
{
while (modelist->items)
{
irc_modelist_item_free (modelist, modelist->items);
}
modelist->state = IRC_MODELIST_STATE_EMPTY;
}
/*
* Returns hdata for modelist item.
*/
struct t_hdata *
irc_modelist_hdata_item_cb (const void *pointer, void *data,
const char *hdata_name)
{
struct t_hdata *hdata;
/* make C compiler happy */
(void) pointer;
(void) data;
hdata = weechat_hdata_new (hdata_name, "prev_item", "next_item",
0, 0, NULL, NULL);
if (hdata)
{
WEECHAT_HDATA_VAR(struct t_irc_modelist_item, number, INTEGER, 0, NULL, NULL);
WEECHAT_HDATA_VAR(struct t_irc_modelist_item, mask, STRING, 0, NULL, NULL);
WEECHAT_HDATA_VAR(struct t_irc_modelist_item, setter, STRING, 0, NULL, NULL);
WEECHAT_HDATA_VAR(struct t_irc_modelist_item, datetime, TIME, 0, NULL, NULL);
WEECHAT_HDATA_VAR(struct t_irc_modelist_item, prev_item, POINTER, 0, NULL, hdata_name);
WEECHAT_HDATA_VAR(struct t_irc_modelist_item, next_item, POINTER, 0, NULL, hdata_name);
}
return hdata;
}
/*
* Returns hdata for modelist.
*/
struct t_hdata *
irc_modelist_hdata_modelist_cb (const void *pointer, void *data, const char *hdata_name)
{
struct t_hdata *hdata;
/* make C compiler happy */
(void) pointer;
(void) data;
hdata = weechat_hdata_new (hdata_name, "prev_modelist", "next_modelist",
0, 0, NULL, NULL);
if (hdata)
{
WEECHAT_HDATA_VAR(struct t_irc_modelist, type, CHAR, 0, NULL, NULL);
WEECHAT_HDATA_VAR(struct t_irc_modelist, state, INTEGER, 0, NULL, NULL);
WEECHAT_HDATA_VAR(struct t_irc_modelist, items, POINTER, 0, NULL, "irc_modelist_item");
WEECHAT_HDATA_VAR(struct t_irc_modelist, last_item, POINTER, 0, NULL, "irc_modelist_item");
WEECHAT_HDATA_VAR(struct t_irc_modelist, prev_modelist, POINTER, 0, NULL, hdata_name);
WEECHAT_HDATA_VAR(struct t_irc_modelist, next_modelist, POINTER, 0, NULL, hdata_name);
}
return hdata;
}
/*
* Adds a modelist item in an infolist.
*
* Returns:
* 1: OK
* 0: error
*/
int
irc_modelist_item_add_to_infolist (struct t_infolist *infolist,
struct t_irc_modelist_item *item)
{
struct t_infolist_item *ptr_item;
if (!infolist || !item)
return 0;
ptr_item = weechat_infolist_new_item (infolist);
if (!ptr_item)
return 0;
if (!weechat_infolist_new_var_integer (ptr_item, "number", item->number))
return 0;
if (!weechat_infolist_new_var_string (ptr_item, "mask", item->mask))
return 0;
if (!weechat_infolist_new_var_string (ptr_item, "setter", item->setter))
return 0;
if (!weechat_infolist_new_var_time (ptr_item, "datetime", item->datetime))
return 0;
return 1;
}
/*
* Adds a modelist in an infolist.
*
* Returns:
* 1: OK
* 0: error
*/
int
irc_modelist_add_to_infolist (struct t_infolist *infolist,
struct t_irc_modelist *modelist)
{
struct t_infolist_item *ptr_item;
char str_type[2];
if (!infolist || !modelist)
return 0;
ptr_item = weechat_infolist_new_item (infolist);
if (!ptr_item)
return 0;
str_type[0] = modelist->type;
str_type[1] = '\0';
if (!weechat_infolist_new_var_string (ptr_item, "type", str_type))
return 0;
if (!weechat_infolist_new_var_integer (ptr_item, "state", modelist->state))
return 0;
return 1;
}
/*
* Prints modelist item infos in WeeChat log file (usually for crash dump).
*/
void
irc_modelist_item_print_log (struct t_irc_modelist_item *item)
{
weechat_log_printf ("");
weechat_log_printf (" => modelist item %d (addr:0x%lx):", item->number, item);
weechat_log_printf (" mask . . . . . . . . . . : '%s'", item->mask);
weechat_log_printf (" setter . . . . . . . . . : '%s'", item->setter);
weechat_log_printf (" datetime . . . . . . . . : %lld",
(long long)(item->datetime));
weechat_log_printf (" prev_item . . . . . . . : 0x%lx", item->prev_item);
weechat_log_printf (" next_item . . . . . . . : 0x%lx", item->next_item);
}
/*
* Prints modelist infos in WeeChat log file (usually for crash dump).
*/
void
irc_modelist_print_log (struct t_irc_modelist *modelist)
{
struct t_irc_modelist_item *ptr_item;
weechat_log_printf ("");
weechat_log_printf (" => modelist \"%c\" (addr:0x%lx):", modelist->type, modelist);
weechat_log_printf (" state. . . . . . . . . . : %d", modelist->state);
weechat_log_printf (" prev_modelist . . . . . : 0x%lx", modelist->prev_modelist);
weechat_log_printf (" next_modelist . . . . . : 0x%lx", modelist->next_modelist);
for (ptr_item = modelist->items; ptr_item; ptr_item = ptr_item->next_item)
{
irc_modelist_item_print_log (ptr_item);
}
}
|