summaryrefslogtreecommitdiff
path: root/src/irc/irc-channel.c
blob: 86028f952a2c7f5803b612c32744f7451bb28aaa (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
/*
 * Copyright (c) 2003-2005 by FlashCode <flashcode@flashtux.org>
 * See README for License detail, AUTHORS for developers list.
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

/* irc-channel.c: manages a chat (channel or private chat) */


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

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

#include "../common/weechat.h"
#include "irc.h"
#include "../gui/gui.h"


char *channel_modes = "iklmnst";


/*
 * channel_new: allocate a new channel for a server and add it to the server queue
 */

t_irc_channel *
channel_new (t_irc_server *server, int channel_type, char *channel_name,
             int switch_to_buffer)
{
    t_irc_channel *new_channel;
    
    /* alloc memory for new channel */
    if ((new_channel = (t_irc_channel *) malloc (sizeof (t_irc_channel))) == NULL)
    {
        fprintf (stderr, _("%s cannot allocate new channel"), WEECHAT_ERROR);
        return NULL;
    }

    /* initialize new channel */
    new_channel->type = channel_type;
    new_channel->dcc_chat = NULL;
    new_channel->name = strdup (channel_name);
    new_channel->topic = NULL;
    memset (new_channel->modes, ' ', sizeof (new_channel->modes));
    new_channel->modes[sizeof (new_channel->modes) - 1] = '\0';
    new_channel->limit = 0;
    new_channel->key = NULL;
    new_channel->nicks_count = 0;
    new_channel->checking_away = 0;
    new_channel->nicks = NULL;
    new_channel->last_nick = NULL;

    /* add new channel to queue */
    new_channel->prev_channel = server->last_channel;
    new_channel->next_channel = NULL;
    if (server->channels)
        server->last_channel->next_channel = new_channel;
    else
        server->channels = new_channel;
    server->last_channel = new_channel;

    gui_buffer_new (gui_current_window, server, new_channel, 0, switch_to_buffer);
    
    /* all is ok, return address of new channel */
    return new_channel;
}

/*
 * channel_free: free a channel and remove it from channels queue
 */

void
channel_free (t_irc_server *server, t_irc_channel *channel)
{
    t_irc_channel *new_channels;
    
    /* close DCC CHAT */
    if ((t_irc_dcc *)(channel->dcc_chat) &&
        (!DCC_ENDED(((t_irc_dcc *)(channel->dcc_chat))->status)))
    {
        dcc_close ((t_irc_dcc *)(channel->dcc_chat), DCC_ABORTED);
        dcc_redraw (1);
    }
    
    /* remove channel from queue */
    if (server->last_channel == channel)
        server->last_channel = channel->prev_channel;
    if (channel->prev_channel)
    {
        (channel->prev_channel)->next_channel = channel->next_channel;
        new_channels = server->channels;
    }
    else
        new_channels = channel->next_channel;
    
    if (channel->next_channel)
        (channel->next_channel)->prev_channel = channel->prev_channel;
    
    /* free data */
    if (channel->name)
        free (channel->name);
    if (channel->topic)
        free (channel->topic);
    nick_free_all (channel);
    free (channel);
    server->channels = new_channels;
}

/*
 * channel_free_all: free all allocated channels
 */

void
channel_free_all (t_irc_server *server)
{
    /* remove all channels for the server */
    while (server->channels)
        channel_free (server, server->channels);
}

/*
 * channel_search: returns pointer on a channel with name
 */

t_irc_channel *
channel_search (t_irc_server *server, char *channel_name)
{
    t_irc_channel *ptr_channel;
    
    if (!server || !channel_name)
        return NULL;
    
    for (ptr_channel = server->channels; ptr_channel;
         ptr_channel = ptr_channel->next_channel)
    {
        if (ascii_strcasecmp (ptr_channel->name, channel_name) == 0)
            return ptr_channel;
    }
    return NULL;
}

/*
 * string_is_channel: returns 1 if string is channel
 */

int
string_is_channel (char *string)
{
    char first_char[2];
    
    if (!string)
        return 0;
    
    first_char[0] = string[0];
    first_char[1] = '\0';
    return (strpbrk (first_char, CHANNEL_PREFIX)) ? 1 : 0;    
}

/*
 * channel_remove_away: remove away for all nicks on a channel
 */

void
channel_remove_away (t_irc_channel *channel)
{
    t_irc_nick *ptr_nick;
    
    if (channel->type == CHAT_CHANNEL)
    {
        for (ptr_nick = channel->nicks; ptr_nick; ptr_nick = ptr_nick->next_nick)
        {
            ptr_nick->is_away = 0;
        }
        gui_draw_buffer_nick (channel->buffer, 0);
    }
}

/*
 * channel_check_away: check for away on a channel
 */

void
channel_check_away (t_irc_server *server, t_irc_channel *channel)
{
    if (channel->type == CHAT_CHANNEL)
    {
        channel->checking_away++;
        server_sendf (server, "WHO %s\r\n", channel->name);
    }
}

/*
 * channel_set_away: set/unset away status for a channel
 */

void
channel_set_away (t_irc_channel *channel, char *nick, int is_away)
{
    t_irc_nick *ptr_nick;
    
    if (channel->type == CHAT_CHANNEL)
    {
        ptr_nick = nick_search (channel, nick);
        if (ptr_nick)
            nick_set_away (channel, ptr_nick, is_away);
    }
}

/*
 * channel_create_dcc: create DCC CHAT channel
 */

int
channel_create_dcc (t_irc_dcc *ptr_dcc)
{
    t_irc_channel *ptr_channel;
    
    ptr_channel = channel_search (ptr_dcc->server, ptr_dcc->nick);
    if (!ptr_channel)
        ptr_channel = channel_new (ptr_dcc->server, CHAT_PRIVATE,
                                   ptr_dcc->nick, 0);
    if (!ptr_channel)
        return 0;
    
    if (ptr_channel->dcc_chat &&
        (!DCC_ENDED(((t_irc_dcc *)(ptr_channel->dcc_chat))->status)))
        return 0;
    
    ptr_channel->dcc_chat = ptr_dcc;
    ptr_dcc->channel = ptr_channel;
    gui_redraw_buffer (ptr_channel->buffer);
    return 1;
}

/*
 * channel_remove_dcc: remove a DCC CHAT
 */

void
channel_remove_dcc (t_irc_dcc *ptr_dcc)
{
    t_irc_channel *ptr_channel;
    
    for (ptr_channel = ptr_dcc->server->channels; ptr_channel;
        ptr_channel = ptr_channel->next_channel)
    {
        if ((t_irc_dcc *)(ptr_channel->dcc_chat) == ptr_dcc)
        {
            ptr_channel->dcc_chat = NULL;
            gui_redraw_buffer (ptr_channel->buffer);
        }
    }
}

/*
 * channel_get_notify_level: get channel notify level
 */

int
channel_get_notify_level (t_irc_server *server, t_irc_channel *channel)
{
    char *name, *pos, *pos2, notify;
    
    if ((!server) || (!channel))
        return NOTIFY_LEVEL_DEFAULT;
    
    if ((!server->notify_levels) || (!server->notify_levels[0]))
        return NOTIFY_LEVEL_DEFAULT;
    
    name = (char *) malloc (strlen (channel->name) + 2);
    strcpy (name, channel->name);
    strcat (name, ":");
    pos = strstr (server->notify_levels, name);
    free (name);
    if (!pos)
        return NOTIFY_LEVEL_DEFAULT;
    
    pos2 = pos + strlen (channel->name);
    if (pos2[0] != ':')
        return NOTIFY_LEVEL_DEFAULT;
    pos2++;
    if (!pos2[0])
        return NOTIFY_LEVEL_DEFAULT;
    
    notify = (int)(pos2[0] - '0');
    if ((notify < NOTIFY_LEVEL_MIN) || (notify > NOTIFY_LEVEL_MAX))
        return NOTIFY_LEVEL_DEFAULT;
    else
        return notify;
}

/*
 * server_remove_notify_level: remove channel notify from list
 */

void
channel_remove_notify_level (t_irc_server *server, t_irc_channel *channel)
{
    char *name, *pos, *pos2;
    
    if ((!server) || (!channel))
        return;
    
    name = (char *) malloc (strlen (channel->name) + 2);
    strcpy (name, channel->name);
    strcat (name, ":");
    pos = strstr (server->notify_levels, name);
    free (name);
    if (pos)
    {
        pos2 = pos + strlen (channel->name);
        if (pos2[0] == ':')
        {
            pos2++;
            if (pos2[0])
            {
                pos2++;
                if (pos2[0] == ',')
                    pos2++;
                if (!pos2[0] && (pos != server->notify_levels))
                    pos--;
                strcpy (pos, pos2);
                server->notify_levels = (char *) realloc (server->notify_levels,
                                                          strlen (server->notify_levels) + 1);
            }
        }
    }
}

/*
 * server_set_notify_level: set channel notify level
 */

void
channel_set_notify_level (t_irc_server *server, t_irc_channel *channel, int notify)
{
    char *name, *pos, *pos2, level_string[2];
    
    if ((!server) || (!channel))
        return;
    
    if (notify == NOTIFY_LEVEL_DEFAULT)
    {
        channel_remove_notify_level (server, channel);
        return;
    }
    
    if (!server->notify_levels)
    {
        server->notify_levels = (char *) malloc (strlen (channel->name) + 3);
        server->notify_levels[0] = '\0';
    }
    else
    {
        name = (char *) malloc (strlen (channel->name) + 2);
        strcpy (name, channel->name);
        strcat (name, ":");
        pos = strstr (server->notify_levels, name);
        free (name);
        if (pos)
        {
            pos2 = pos + strlen (channel->name) + 1;
            if (pos2[0])
            {
                pos2[0] = '0' + notify;
                return;
            }
        }
        /* realloc notify list to add channel */
        server->notify_levels = (char *) realloc (server->notify_levels,
                                                  strlen (server->notify_levels) + 1 +
                                                  strlen (channel->name) + 2 + 1);
    }
    
    /* channel not in notify list => add it */
    if (server->notify_levels[0])
        strcat (server->notify_levels, ",");
    strcat (server->notify_levels, channel->name);
    strcat (server->notify_levels, ":");
    level_string[0] = notify + '0';
    level_string[1] = '\0';
    strcat (server->notify_levels, level_string);
}

/*
 * channel_print_log: print channel infos in log (usually for crash dump)
 */

void
channel_print_log (t_irc_channel *channel)
{
    wee_log_printf ("=> channel %s (addr:0x%X)]\n", channel->name, channel);
    wee_log_printf ("     type . . . . : %d\n",     channel->type);
    wee_log_printf ("     dcc_chat . . : 0x%X\n",   channel->dcc_chat);
    wee_log_printf ("     topic. . . . : '%s'\n",   channel->topic);
    wee_log_printf ("     modes. . . . : '%s'\n",   channel->modes);
    wee_log_printf ("     limit. . . . : %d\n",     channel->limit);
    wee_log_printf ("     key. . . . . : '%s'\n",   channel->key);
    wee_log_printf ("     checking_away: %d\n",     channel->checking_away);
    wee_log_printf ("     nicks. . . . : 0x%X\n",   channel->nicks);
    wee_log_printf ("     last_nick. . : 0x%X\n",   channel->last_nick);
    wee_log_printf ("     buffer . . . : 0x%X\n",   channel->buffer);
    wee_log_printf ("     prev_channel : 0x%X\n",   channel->prev_channel);
    wee_log_printf ("     next_channel : 0x%X\n",   channel->next_channel);
}