summaryrefslogtreecommitdiff
path: root/src/plugins/irc/irc-mode.c
blob: 24664e9d0df87d87a5bf15c6ca98eff85cc16b2c (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
/*
 * 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-mode.c: IRC channel/user modes management
 */

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

#include "../weechat-plugin.h"
#include "irc.h"
#include "irc-mode.h"
#include "irc-server.h"
#include "irc-channel.h"
#include "irc-nick.h"


/*
 * irc_mode_channel_set_nick: set a mode for a nick on a channel
 */

void
irc_mode_channel_set_nick (struct t_irc_server *server,
                           struct t_irc_channel *channel, const char *nick,
                           char set_flag, int flag)
{
    struct t_irc_nick *ptr_nick;
    
    if (nick)
    {
        ptr_nick = irc_nick_search (channel, nick);
        if (ptr_nick)
            irc_nick_set (server, channel, ptr_nick, (set_flag == '+'), flag);
    }
}

/*
 * irc_mode_channel_set: set channel modes
 *                       return: 1 if channel modes are updated
 *                               0 if channel modes are NOT updated
 *                                 (no update or on nicks only)
 */

int
irc_mode_channel_set (struct t_irc_server *server,
                      struct t_irc_channel *channel, const char *modes)
{
    char *pos_args, *str_modes, set_flag, **argv, *pos, *ptr_arg;
    int mode, modes_count, channel_modes_updated, argc, current_arg;
    
    if (!server || !channel || !modes)
        return 0;
    
    channel_modes_updated = 0;
    
    argc = 0;
    argv = NULL;
    pos_args = strchr (modes, ' ');
    if (pos_args)
    {
        str_modes = weechat_strndup (modes, pos_args - modes);
        if (!str_modes)
            return 0;
        pos_args++;
        while (pos_args[0] == ' ')
            pos_args++;
        argv = weechat_string_split (pos_args, " ", 0, 0, &argc);
    }
    else
    {
        str_modes = strdup (modes);
        if (!str_modes)
            return 0;
    }

    /* count number of mode chars */
    modes_count = 0;
    pos = str_modes;
    while (pos && pos[0])
    {
        if ((pos[0] != ':') && (pos[0] != ' ') && (pos[0] != '+')
            && (pos[0] != '-'))
        {
            modes_count++;
        }
        pos++;
    }
    current_arg = argc - modes_count;
    
    if (str_modes && str_modes[0])
    {
        set_flag = '+';
        pos = str_modes;
        while (pos && pos[0])
        {
            switch (pos[0])
            {
                case ':':
                case ' ':
                    break;
                case '+':
                    set_flag = '+';
                    break;
                case '-':
                    set_flag = '-';
                    break;
                case 'a': /* channel admin (unrealircd specific flag) */
                    ptr_arg = ((current_arg >= 0) && (current_arg < argc)) ?
                        argv[current_arg] : NULL;
                    mode = irc_mode_get_nick_attr (server, "a", '~');
                    if (mode >= 0)
                    {
                        irc_mode_channel_set_nick (server, channel, ptr_arg,
                                                   set_flag, mode);
                    }
                    current_arg++;
                    break;
                case 'b': /* ban (ignored) */
                    ptr_arg = ((current_arg >= 0) && (current_arg < argc)) ?
                        argv[current_arg] : NULL;
                    current_arg++;
                    break;
                case 'h': /* half-op */
                    ptr_arg = ((current_arg >= 0) && (current_arg < argc)) ?
                        argv[current_arg] : NULL;
                    mode = irc_mode_get_nick_attr (server, "h", '%');
                    if (mode >= 0)
                    {
                        irc_mode_channel_set_nick (server, channel, ptr_arg,
                                                   set_flag, mode);
                    }
                    current_arg++;
                    break;
                case 'k': /* channel key */
                    if (channel->key)
                    {
                        free (channel->key);
                        channel->key = NULL;
                    }
                    if (set_flag == '+')
                    {
                        ptr_arg = ((current_arg >= 0) && (current_arg < argc)) ?
                            argv[current_arg] : NULL;
                        if (ptr_arg)
                            channel->key = strdup (ptr_arg);
                    }
                    channel_modes_updated = 1;
                    current_arg++;
                    break;
                case 'l': /* channel limit */
                    if (set_flag == '-')
                        channel->limit = 0;
                    if (set_flag == '+')
                    {
                        ptr_arg = ((current_arg >= 0) && (current_arg < argc)) ?
                            argv[current_arg] : NULL;
                        if (ptr_arg)
                            channel->limit = atoi (ptr_arg);
                    }
                    channel_modes_updated = 1;
                    current_arg++;
                    break;
                case 'o': /* op */
                    ptr_arg = ((current_arg >= 0) && (current_arg < argc)) ?
                        argv[current_arg] : NULL;
                    mode = irc_mode_get_nick_attr (server, "o", '@');
                    if (mode >= 0)
                    {
                        irc_mode_channel_set_nick (server, channel, ptr_arg,
                                                   set_flag, mode);
                    }
                    current_arg++;
                    break;
                case 'q': /* channel owner (unrealircd specific flag) */
                    ptr_arg = ((current_arg >= 0) && (current_arg < argc)) ?
                        argv[current_arg] : NULL;
                    mode = irc_mode_get_nick_attr (server, "q", '~');
                    if (mode >= 0)
                    {
                        irc_mode_channel_set_nick (server, channel, ptr_arg,
                                                   set_flag, mode);
                    }
                    current_arg++;
                    break;
                case 'u': /* channel user */
                    ptr_arg = ((current_arg >= 0) && (current_arg < argc)) ?
                        argv[current_arg] : NULL;
                    mode = irc_mode_get_nick_attr (server, "u", '-');
                    if (mode >= 0)
                    {
                        irc_mode_channel_set_nick (server, channel, ptr_arg,
                                                   set_flag, mode);
                    }
                    current_arg++;
                    break;
                case 'v': /* voice */
                    ptr_arg = ((current_arg >= 0) && (current_arg < argc)) ?
                        argv[current_arg] : NULL;
                    mode = irc_mode_get_nick_attr (server, "v", '+');
                    if (mode >= 0)
                    {
                        irc_mode_channel_set_nick (server, channel, ptr_arg,
                                                   set_flag, mode);
                    }
                    current_arg++;
                    break;
                default:
                    current_arg++;
                    channel_modes_updated = 1;
                    break;
            }
            pos++;
        }
    }
    
    if (str_modes)
        free (str_modes);
    if (argv)
        weechat_string_free_split (argv);
    
    weechat_bar_item_update ("buffer_name");
    
    return channel_modes_updated;
}

/*
 * irc_mode_user_add: add a user mode
 */

void
irc_mode_user_add (struct t_irc_server *server, char mode)
{
    char str_mode[2];

    str_mode[0] = mode;
    str_mode[1] = '\0';
    
    if (server->nick_modes)
    {
        if (!strchr (server->nick_modes, mode))
        {
            server->nick_modes = realloc (server->nick_modes,
                                          strlen (server->nick_modes) + 1 + 1);
            strcat (server->nick_modes, str_mode);
            weechat_bar_item_update ("input_prompt");
        }
    }
    else
    {
        server->nick_modes = malloc (2);
        strcpy (server->nick_modes, str_mode);
        weechat_bar_item_update ("input_prompt");
    }
}

/*
 * irc_mode_user_remove: remove a user mode
 */

void
irc_mode_user_remove (struct t_irc_server *server, char mode)
{
    char *pos;
    int new_size;
    
    if (server->nick_modes)
    {
        pos = strchr (server->nick_modes, mode);
        if (pos)
        {
            new_size = strlen (server->nick_modes);
            memmove (pos, pos + 1, strlen (pos + 1) + 1);
            server->nick_modes = realloc (server->nick_modes, new_size);
            weechat_bar_item_update ("input_prompt");
        }
    }
}

/*
 * irc_mode_user_set: set user modes
 */

void
irc_mode_user_set (struct t_irc_server *server, const char *modes)
{
    char set_flag;
    
    set_flag = '+';
    while (modes && modes[0])
    {
        switch (modes[0])
        {
            case ':':
            case ' ':
                break;
            case '+':
                set_flag = '+';
                break;
            case '-':
                set_flag = '-';
                break;
            default:
                if (set_flag == '+')
                    irc_mode_user_add (server, modes[0]);
                else
                    irc_mode_user_remove (server, modes[0]);
                break;
        }
        modes++;
    }
}

/*
 * irc_mode_prefix_to_nick_attr: get nick attribute for prefix
 *                               return -1 if prefix is unknown
 */

int
irc_mode_prefix_to_nick_attr (char prefix)
{
    switch (prefix)
    {
        case '@': /* op */
            return IRC_NICK_OP;
        case '~': /* channel owner */
            return IRC_NICK_CHANOWNER;
        case '*': /* channel owner */
            return IRC_NICK_CHANOWNER;
        case '&': /* channel admin */
            return IRC_NICK_CHANADMIN;
        case '!': /* channel admin (2) */
            return IRC_NICK_CHANADMIN2;
        case '%': /* half-op */
            return IRC_NICK_HALFOP;
        case '+': /* voice */
            return IRC_NICK_VOICE;
        case '-': /* channel user */
            return IRC_NICK_CHANUSER;
    }
    
    return -1;
}

/*
 * irc_mode_get_nick_attr: return nick attribute for mode, if allowed by server
 *                         return -1 if not allowed
 *                         for example :
 *                           IRC:  005 (...) PREFIX=(ohv)@%+
 *                         => allowed modes: ohv
 */

int
irc_mode_get_nick_attr (struct t_irc_server *server, char *mode,
                        char prefix)
{
    char str[2], *pos, *ptr_prefixes, *pos_mode;
    int index;
    
    /* if server did not send any prefix info, then use default prefixes */
    if (!server->prefix)
    {
        str[0] = prefix;
        str[1] = '\0';
        pos = strpbrk (str, IRC_MODE_NICK_DEFAULT_PREFIXES_LIST);
        if (!pos)
            return -1;
        return irc_mode_prefix_to_nick_attr (pos[0]);
    }
    
    /* find start of prefixes, after "(...)" */
    ptr_prefixes = strchr (server->prefix, ')');
    if (ptr_prefixes)
        ptr_prefixes++;
    
    /* we check if mode is in list and return prefix found */
    if (mode && ptr_prefixes)
    {
        pos_mode = strchr (server->prefix + 1, mode[0]);
        if (pos_mode && (pos_mode < ptr_prefixes))
        {
            index = pos_mode - server->prefix - 1;
            if (pos_mode && (index < (int)strlen (ptr_prefixes)))
            {
                return irc_mode_prefix_to_nick_attr (ptr_prefixes[index]);
            }
        }
    }
    
    if (!ptr_prefixes)
        ptr_prefixes = server->prefix;
    
    pos = strchr (ptr_prefixes, prefix);
    if (!pos)
        return -1;
    
    return irc_mode_prefix_to_nick_attr (pos[0]);
}

/*
 * irc_mode_get_prefix: return prefix for mode
 *                      for example if prefixes are:
 *                        IRC:  005 (...) PREFIX=(ohv)@%+
 *                      'o' will return '@'
 *                      'h' will return '%'
 *                      'v' will return '+'
 */

char
irc_mode_get_prefix (struct t_irc_server *server, char mode,
                     char default_prefix)
{
    char *ptr_prefixes, *pos_mode;
    int index;
    
    if (!server->prefix)
        return default_prefix;
    
    /* find start of prefixes, after "(...)" */
    ptr_prefixes = strchr (server->prefix, ')');
    if (ptr_prefixes)
        ptr_prefixes++;
    
    if (!ptr_prefixes || !ptr_prefixes[0])
        return default_prefix;
    
    /* search prefix for mode */
    pos_mode = strchr (server->prefix + 1, mode);
    if (pos_mode && (pos_mode < ptr_prefixes))
    {
        index = pos_mode - server->prefix - 1;
        if (pos_mode && (index < (int)strlen (ptr_prefixes)))
        {
            return ptr_prefixes[index];
        }
    }
    
    return default_prefix;
}