summaryrefslogtreecommitdiff
path: root/src/plugins/irc/irc-info.c
blob: db788d5c297880ed11a38842dc4502896b006d5a (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
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
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
/*
 * Copyright (C) 2003-2010 Sebastien 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 <http://www.gnu.org/licenses/>.
 */

/*
 * irc-info.c: info and infolist hooks for IRC plugin
 */

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

#include "../weechat-plugin.h"
#include "irc.h"
#include "irc-channel.h"
#include "irc-config.h"
#include "irc-ignore.h"
#include "irc-message.h"
#include "irc-nick.h"
#include "irc-notify.h"
#include "irc-protocol.h"
#include "irc-server.h"


/*
 * irc_info_create_string_with_pointer: create a string with a pointer inside
 *                                      an IRC structure
 */

void
irc_info_create_string_with_pointer (char **string, void *pointer)
{
    if (*string)
    {
        free (*string);
        *string = NULL;
    }
    if (pointer)
    {
        *string = malloc (64);
        if (*string)
        {
            snprintf (*string, 64 - 1, "0x%lx", (long unsigned int)pointer);
        }
    }
}

/*
 * irc_info_get_info_cb: callback called when IRC info is asked
 */

const char *
irc_info_get_info_cb (void *data, const char *info_name,
                      const char *arguments)
{
    char *pos_comma, *pos_comma2, *server, *channel, *host;
    const char *nick, *isupport_value;
    static char str_true[2] = "1";
    struct t_irc_server *ptr_server;
    struct t_irc_channel *ptr_channel;
    
    /* make C compiler happy */
    (void) data;
    
    if (weechat_strcasecmp (info_name, "irc_is_channel") == 0)
    {
        if (irc_channel_is_channel (arguments))
            return str_true;
        return NULL;
    }
    else if (weechat_strcasecmp (info_name, "irc_is_nick") == 0)
    {
        if (irc_nick_is_nick (arguments))
            return str_true;
        return NULL;
    }
    else if (weechat_strcasecmp (info_name, "irc_nick") == 0)
    {
        ptr_server = irc_server_search (arguments);
        if (ptr_server)
            return ptr_server->nick;
        return NULL;
    }
    else if (weechat_strcasecmp (info_name, "irc_nick_from_host") == 0)
    {
        return irc_message_get_nick_from_host (arguments);
    }
    else if (weechat_strcasecmp (info_name, "irc_nick_color") == 0)
    {
        return irc_nick_find_color (arguments);
    }
    else if (weechat_strcasecmp (info_name, "irc_nick_color_name") == 0)
    {
        return irc_nick_find_color_name (arguments);
    }
    else if (weechat_strcasecmp (info_name, "irc_buffer") == 0)
    {
        if (arguments && arguments[0])
        {
            server = NULL;
            channel = NULL;
            host = NULL;
            ptr_server = NULL;
            ptr_channel = NULL;
            
            pos_comma = strchr (arguments, ',');
            if (pos_comma)
            {
                server = weechat_strndup (arguments, pos_comma - arguments);
                pos_comma2 = strchr (pos_comma + 1, ',');
                if (pos_comma2)
                {
                    channel = weechat_strndup (pos_comma + 1,
                                               pos_comma2 - pos_comma - 1);
                    host = strdup (pos_comma2 + 1);
                }
                else
                    channel = strdup (pos_comma + 1);
            }
            else
            {
                if (irc_channel_is_channel (arguments))
                    channel = strdup (arguments);
                else
                    server = strdup (arguments);
            }
            
            /*
             * replace channel by nick in host if channel is not a channel
             * (private ?)
             */
            if (channel && host)
            {
                if (!irc_channel_is_channel (channel))
                {
                    free (channel);
                    channel = NULL;
                    nick = irc_message_get_nick_from_host (host);
                    if (nick)
                        channel = strdup (nick);
                    
                }
            }
            
            /* search for server or channel buffer */
            if (server)
            {
                ptr_server = irc_server_search (server);
                if (ptr_server && channel)
                    ptr_channel = irc_channel_search (ptr_server, channel);
            }
            
            if (server)
                free (server);
            if (channel)
                free (channel);
            if (host)
                free (host);
            
            if (ptr_channel)
            {
                irc_info_create_string_with_pointer (&ptr_channel->buffer_as_string,
                                                     ptr_channel->buffer);
                return ptr_channel->buffer_as_string;
            }
            if (ptr_server)
            {
                irc_info_create_string_with_pointer (&ptr_server->buffer_as_string,
                                                     ptr_server->buffer);
                return ptr_server->buffer_as_string;
            }
        }
    }
    else if (weechat_strcasecmp (info_name, "irc_server_isupport") == 0)
    {
        isupport_value = NULL;
        pos_comma = strchr (arguments, ',');
        if (pos_comma)
        {
            server = weechat_strndup (arguments, pos_comma - arguments);
            if (server)
            {
                ptr_server = irc_server_search (server);
                if (ptr_server)
                {
                    isupport_value = irc_server_get_isupport_value (ptr_server,
                                                                    pos_comma + 1);
                }
            }
        }
        if (isupport_value)
            return str_true;
        return NULL;
    }
    else if (weechat_strcasecmp (info_name, "irc_server_isupport_value") == 0)
    {
        isupport_value = NULL;
        pos_comma = strchr (arguments, ',');
        if (pos_comma)
        {
            server = weechat_strndup (arguments, pos_comma - arguments);
            if (server)
            {
                ptr_server = irc_server_search (server);
                if (ptr_server)
                {
                    isupport_value = irc_server_get_isupport_value (ptr_server,
                                                                    pos_comma + 1);
                }
            }
        }
        return isupport_value;
    }
    
    return NULL;
}

/*
 * irc_info_get_info_hashtable_cb: callback called when IRC info_hashtable is
 *                                 asked
 */

struct t_hashtable *
irc_info_get_info_hashtable_cb (void *data, const char *info_name,
                                struct t_hashtable *hashtable)
{
    const char *message;
    struct t_hashtable *value;
    
    /* make C compiler happy */
    (void) data;
    
    if (weechat_strcasecmp (info_name, "irc_parse_message") == 0)
    {
        if (hashtable)
        {
            message = (const char *)weechat_hashtable_get (hashtable,
                                                           "message");
            if (message)
            {
                value = irc_message_parse_to_hashtable (message);
                return value;
            }
        }
    }
    
    return NULL;
}

/*
 * irc_info_get_infolist_cb: callback called when IRC infolist is asked
 */

struct t_infolist *
irc_info_get_infolist_cb (void *data, const char *infolist_name,
                          void *pointer, const char *arguments)
{
    struct t_infolist *ptr_infolist;
    struct t_irc_server *ptr_server;
    struct t_irc_channel *ptr_channel;
    struct t_irc_nick *ptr_nick;
    struct t_irc_ignore *ptr_ignore;
    struct t_irc_notify *ptr_notify;
    char **argv;
    int argc;
    
    /* make C compiler happy */
    (void) data;
    
    if (!infolist_name || !infolist_name[0])
        return NULL;
    
    if (weechat_strcasecmp (infolist_name, "irc_server") == 0)
    {
        if (pointer && !irc_server_valid (pointer))
            return NULL;
        
        ptr_infolist = weechat_infolist_new ();
        if (ptr_infolist)
        {
            if (pointer)
            {
                /* build list with only one server */
                if (!irc_server_add_to_infolist (ptr_infolist, pointer))
                {
                    weechat_infolist_free (ptr_infolist);
                    return NULL;
                }
                return ptr_infolist;
            }
            else
            {
                /* build list with all servers matching arguments */
                for (ptr_server = irc_servers; ptr_server;
                     ptr_server = ptr_server->next_server)
                {
                    if (!arguments || !arguments[0]
                        || weechat_string_match (ptr_server->name, arguments, 0))
                    {
                        if (!irc_server_add_to_infolist (ptr_infolist, ptr_server))
                        {
                            weechat_infolist_free (ptr_infolist);
                            return NULL;
                        }
                    }
                }
                return ptr_infolist;
            }
        }
    }
    else if (weechat_strcasecmp (infolist_name, "irc_channel") == 0)
    {
        if (arguments && arguments[0])
        {
            ptr_server = NULL;
            ptr_channel = NULL;
            argv = weechat_string_split (arguments, ",", 0, 0, &argc);
            if (argv)
            {
                if (argc >= 1)
                {
                    ptr_server = irc_server_search (argv[0]);
                    if (!ptr_server)
                    {
                        weechat_string_free_split (argv);
                        return NULL;
                    }
                    if (!pointer && (argc >= 2))
                    {
                        pointer = irc_channel_search (ptr_server, argv[1]);
                        if (!pointer)
                        {
                            weechat_string_free_split (argv);
                            return NULL;
                        }
                    }
                }
                weechat_string_free_split (argv);
                if (ptr_server)
                {
                    if (pointer && !irc_channel_valid (ptr_server, pointer))
                        return NULL;
                    
                    ptr_infolist = weechat_infolist_new ();
                    if (ptr_infolist)
                    {
                        if (pointer)
                        {
                            /* build list with only one channel */
                            if (!irc_channel_add_to_infolist (ptr_infolist,
                                                              pointer))
                            {
                                weechat_infolist_free (ptr_infolist);
                                return NULL;
                            }
                            return ptr_infolist;
                        }
                        else
                        {
                            /* build list with all channels of server */
                            for (ptr_channel = ptr_server->channels;
                                 ptr_channel;
                                 ptr_channel = ptr_channel->next_channel)
                            {
                                if (!irc_channel_add_to_infolist (ptr_infolist,
                                                                  ptr_channel))
                                {
                                    weechat_infolist_free (ptr_infolist);
                                    return NULL;
                                }
                            }
                            return ptr_infolist;
                        }
                    }
                }
            }
        }
    }
    else if (weechat_strcasecmp (infolist_name, "irc_nick") == 0)
    {
        if (arguments && arguments[0])
        {
            ptr_server = NULL;
            ptr_channel = NULL;
            argv = weechat_string_split (arguments, ",", 0, 0, &argc);
            if (argv)
            {
                if (argc >= 2)
                {
                    ptr_server = irc_server_search (argv[0]);
                    if (!ptr_server)
                    {
                        weechat_string_free_split (argv);
                        return NULL;
                    }
                    ptr_channel = irc_channel_search (ptr_server, argv[1]);
                    if (!ptr_channel)
                    {
                        weechat_string_free_split (argv);
                        return NULL;
                    }
                    if (!pointer && (argc >= 3))
                    {
                        pointer = irc_nick_search (ptr_channel, argv[2]);
                        if (!pointer)
                        {
                            weechat_string_free_split (argv);
                            return NULL;
                        }
                    }
                }
                weechat_string_free_split (argv);
                if (ptr_server && ptr_channel)
                {
                    if (pointer && !irc_nick_valid (ptr_channel, pointer))
                        return NULL;
                    
                    ptr_infolist = weechat_infolist_new ();
                    if (ptr_infolist)
                    {
                        if (pointer)
                        {
                            /* build list with only one nick */
                            if (!irc_nick_add_to_infolist (ptr_infolist,
                                                           pointer))
                            {
                                weechat_infolist_free (ptr_infolist);
                                return NULL;
                            }
                            return ptr_infolist;
                        }
                        else
                        {
                            /* build list with all nicks of channel */
                            for (ptr_nick = ptr_channel->nicks; ptr_nick;
                                 ptr_nick = ptr_nick->next_nick)
                            {
                                if (!irc_nick_add_to_infolist (ptr_infolist,
                                                               ptr_nick))
                                {
                                    weechat_infolist_free (ptr_infolist);
                                    return NULL;
                                }
                            }
                            return ptr_infolist;
                        }
                    }
                }
            }
        }
    }
    else if (weechat_strcasecmp (infolist_name, "irc_ignore") == 0)
    {
        if (pointer && !irc_ignore_valid (pointer))
            return NULL;
        
        ptr_infolist = weechat_infolist_new ();
        if (ptr_infolist)
        {
            if (pointer)
            {
                /* build list with only one ignore */
                if (!irc_ignore_add_to_infolist (ptr_infolist, pointer))
                {
                    weechat_infolist_free (ptr_infolist);
                    return NULL;
                }
                return ptr_infolist;
            }
            else
            {
                /* build list with all ignore */
                for (ptr_ignore = irc_ignore_list; ptr_ignore;
                     ptr_ignore = ptr_ignore->next_ignore)
                {
                    if (!irc_ignore_add_to_infolist (ptr_infolist, ptr_ignore))
                    {
                        weechat_infolist_free (ptr_infolist);
                        return NULL;
                    }
                }
                return ptr_infolist;
            }
        }
    }
    else if (weechat_strcasecmp (infolist_name, "irc_notify") == 0)
    {
        if (pointer && !irc_notify_valid (NULL, pointer))
            return NULL;
        
        ptr_infolist = weechat_infolist_new ();
        if (ptr_infolist)
        {
            if (pointer)
            {
                /* build list with only one notify */
                if (!irc_notify_add_to_infolist (ptr_infolist, pointer))
                {
                    weechat_infolist_free (ptr_infolist);
                    return NULL;
                }
                return ptr_infolist;
            }
            else
            {
                /* build list with notify list of all servers matchin arguments */
                for (ptr_server = irc_servers; ptr_server;
                     ptr_server = ptr_server->next_server)
                {
                    if (!arguments || !arguments[0]
                        || weechat_string_match (ptr_server->name, arguments, 0))
                    {
                        for (ptr_notify = ptr_server->notify_list; ptr_notify;
                             ptr_notify = ptr_notify->next_notify)
                        {
                            if (!irc_notify_add_to_infolist (ptr_infolist,
                                                             ptr_notify))
                            {
                                weechat_infolist_free (ptr_infolist);
                                return NULL;
                            }
                        }
                    }
                }
                return ptr_infolist;
            }
        }
    }
    
    return NULL;
}

/*
 * irc_info_init: initialize info and infolist hooks for IRC plugin
 */

void
irc_info_init ()
{
    /* info hooks */
    weechat_hook_info ("irc_is_channel",
                       N_("1 if string is a valid IRC channel name"),
                       N_("channel name"),
                       &irc_info_get_info_cb, NULL);
    weechat_hook_info ("irc_is_nick",
                       N_("1 if string is a valid IRC nick name"),
                       N_("nickname"),
                       &irc_info_get_info_cb, NULL);
    weechat_hook_info ("irc_nick",
                       N_("get current nick on a server"),
                       N_("server name"),
                       &irc_info_get_info_cb, NULL);
    weechat_hook_info ("irc_nick_from_host",
                       N_("get nick from IRC host"),
                       N_("IRC host (like `:nick!name@server.com`)"),
                       &irc_info_get_info_cb, NULL);
    weechat_hook_info ("irc_nick_color",
                       N_("get nick color code"),
                       N_("nickname"),
                       &irc_info_get_info_cb, NULL);
    weechat_hook_info ("irc_nick_color_name",
                       N_("get nick color name"),
                       N_("nickname"),
                       &irc_info_get_info_cb, NULL);
    weechat_hook_info ("irc_buffer",
                       N_("get buffer pointer for an IRC server/channel/nick"),
                       N_("server,channel,nick (channel and nicks are optional)"),
                       &irc_info_get_info_cb, NULL);
    weechat_hook_info ("irc_server_isupport",
                       N_("1 if server supports this feature (from IRC message 005)"),
                       N_("server,feature"),
                       &irc_info_get_info_cb, NULL);
    weechat_hook_info ("irc_server_isupport_value",
                       N_("value of feature, if supported by server (from IRC message 005)"),
                       N_("server,feature"),
                       &irc_info_get_info_cb, NULL);
    
    /* info_hashtable hooks */
    weechat_hook_info_hashtable ("irc_parse_message",
                                 N_("parse an IRC message"),
                                 N_("\"message\": IRC message"),
                                 /* TRANSLATORS: please do not translate key names (enclosed by quotes) */
                                 N_("\"nick\": nick, \"host\": host, "
                                    "\"command\": command, \"channel\": channel, "
                                    "\"arguments\": arguments (includes channel)"),
                                 &irc_info_get_info_hashtable_cb, NULL);
    
    /* infolist hooks */
    weechat_hook_infolist ("irc_server",
                           N_("list of IRC servers"),
                           N_("server pointer (optional)"),
                           N_("server name (can start or end with \"*\" as wildcard) (optional)"),
                           &irc_info_get_infolist_cb, NULL);
    weechat_hook_infolist ("irc_channel",
                           N_("list of channels for an IRC server"),
                           N_("channel pointer (optional)"),
                           N_("server,channel (channel is optional)"),
                           &irc_info_get_infolist_cb, NULL);
    weechat_hook_infolist ("irc_nick",
                           N_("list of nicks for an IRC channel"),
                           N_("nick pointer (optional)"),
                           N_("server,channel,nick (channel and nick are optional)"),
                           &irc_info_get_infolist_cb, NULL);
    weechat_hook_infolist ("irc_ignore",
                           N_("list of IRC ignores"),
                           N_("ignore pointer (optional)"),
                           NULL,
                           &irc_info_get_infolist_cb, NULL);
    weechat_hook_infolist ("irc_notify",
                           N_("list of notify"),
                           N_("notify pointer (optional)"),
                           N_("server name (can start or end with \"*\" as wildcard) (optional)"),
                           &irc_info_get_infolist_cb, NULL);
}