summaryrefslogtreecommitdiff
path: root/src/plugins/irc/irc-buffer.c
blob: 3a6c0f97aeac9f14dd3fd76cba3cb94b29459aca (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
/*
 * Copyright (c) 2003-2008 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 3 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, see <http://www.gnu.org/licenses/>.
 */

/* irc-buffer.c: manages buffers for IRC protocol */


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

#include "../weechat-plugin.h"
#include "irc.h"
#include "irc-buffer.h"
#include "irc-channel.h"
#include "irc-command.h"
#include "irc-server.h"


/*
 * irc_buffer_merge_servers: merge server buffers in one buffer
 */

/*
void
irc_buffer_merge_servers (t_gui_window *window)
{
    t_gui_buffer *ptr_buffer_server, *ptr_buffer, *new_ptr_buffer;
    t_irc_server *ptr_server;
    
    // new server buffer is the first server buffer found
    for (ptr_buffer_server = gui_buffers; ptr_buffer_server;
         ptr_buffer_server = ptr_buffer_server->next_buffer)
    {
        if ((ptr_buffer_server->protocol == irc_protocol)
            && (IRC_BUFFER_SERVER(ptr_buffer_server))
            && (!IRC_BUFFER_CHANNEL(ptr_buffer_server)))
            break;
    }
    
    // no server buffer found
    if (!ptr_buffer_server)
        return;
    
    ptr_buffer = gui_buffers;
    while (ptr_buffer)
    {
        if ((ptr_buffer->protocol == irc_protocol)
            && (ptr_buffer != ptr_buffer_server)
            && (IRC_BUFFER_SERVER(ptr_buffer))
            && (!IRC_BUFFER_CHANNEL(ptr_buffer)))
        {
            ptr_server = IRC_BUFFER_SERVER(ptr_buffer);
            
            // add (by pointer artefact) lines from buffer found to server buffer
            if (ptr_buffer->lines)
            {
                if (ptr_buffer_server->lines)
                {
                    ptr_buffer->lines->prev_line =
                        ptr_buffer_server->last_line;
                    ptr_buffer_server->last_line->next_line =
                        ptr_buffer->lines;
                    ptr_buffer_server->last_line =
                        ptr_buffer->last_line;
                }
                else
                {
                    ptr_buffer_server->lines = ptr_buffer->lines;
                    ptr_buffer_server->last_line = ptr_buffer->last_line;
                }
            }
            
            // free buffer but not lines, because they're now used by 
            // our unique server buffer
            new_ptr_buffer = ptr_buffer->next_buffer;
            ptr_buffer->lines = NULL;
            gui_buffer_free (ptr_buffer, 1);
            ptr_buffer = new_ptr_buffer;
            
            // asociate server with new server buffer
            ptr_server->buffer = ptr_buffer_server;
        }
        else
            ptr_buffer = ptr_buffer->next_buffer;
    }
    
    IRC_BUFFER_ALL_SERVERS(ptr_buffer_server) = 1;
    gui_window_redraw_buffer (window->buffer);
}
*/

/*
 * irc_buffer_split_server: split the server buffer into many buffers (one by server)
 */

/*
void
irc_buffer_split_server (t_gui_window *window)
{
    t_gui_buffer *ptr_buffer;
    t_irc_server *ptr_server;
    char *log_filename;
    
    ptr_buffer = gui_buffer_servers_search ();
    
    if (ptr_buffer)
    {
        if (IRC_BUFFER_SERVER(ptr_buffer))
        {
            for (ptr_server = irc_servers; ptr_server;
                 ptr_server = ptr_server->next_server)
            {
                if (ptr_server->buffer
                    && (ptr_server != IRC_BUFFER_SERVER(ptr_buffer))
                    && (ptr_server->buffer == ptr_buffer))
                {
                    ptr_server->buffer = NULL;
                    log_filename = irc_log_get_filename (ptr_server->name,
                                                         NULL,
                                                         0);
                    gui_buffer_new (window, 0,
                                    ptr_server->name,
                                    ptr_server->name,
                                    GUI_BUFFER_ATTRIB_TEXT |
                                    GUI_BUFFER_ATTRIB_INPUT |
                                    GUI_BUFFER_ATTRIB_NICKS,
                                    irc_protocol,
                                    irc_buffer_data_create (ptr_server),
                                    &irc_buffer_data_free,
                                    GUI_NOTIFY_LEVEL_DEFAULT,
                                    NULL, ptr_server->nick,
                                    irc_cfg_log_auto_server, log_filename,
                                    0);
                    if (log_filename)
                        free (log_filename);
                }
            }
        }
        IRC_BUFFER_ALL_SERVERS(ptr_buffer) = 0;
        gui_status_draw (window->buffer, 1);
        gui_input_draw (window->buffer, 1);
    }
}
*/

/*
 * irc_buffer_close_cb: callback called when a buffer is closed
 */

int
irc_buffer_close_cb (void *data, struct t_gui_buffer *buffer)
{
    struct t_irc_channel *next_channel;
    
    IRC_GET_SERVER_CHANNEL(buffer);
    
    /* make C compiler happy */
    (void) data;
    
    if (ptr_channel)
    {
        /* send PART for channel if its buffer is closed */
        if ((ptr_channel->type == IRC_CHANNEL_TYPE_CHANNEL)
            && (ptr_channel->nicks))
        {
            irc_command_part_channel (ptr_server, ptr_channel->name, NULL);
        }
        irc_channel_free (ptr_server, ptr_channel);
    }
    else
    {
        if (ptr_server)
        {
            /* send PART on all channels for server, then disconnect from server */
            ptr_channel = ptr_server->channels;
            while (ptr_channel)
            {
                next_channel = ptr_channel->next_channel;
                weechat_buffer_close (ptr_channel->buffer, 1);
                ptr_channel = next_channel;
            }
            irc_server_disconnect (ptr_server, 0);
            ptr_server->buffer = NULL;
        }
    }
    
    return WEECHAT_RC_OK;
}