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
|
/*
* irc-buffer.c - buffer functions for IRC plugin
*
* Copyright (C) 2003-2016 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 <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <limits.h>
#include "../weechat-plugin.h"
#include "irc.h"
#include "irc-buffer.h"
#include "irc-channel.h"
#include "irc-command.h"
#include "irc-config.h"
#include "irc-raw.h"
#include "irc-server.h"
/*
* Gets IRC server and channel pointers with a buffer pointer (buffer may be a
* server or a channel).
*/
void
irc_buffer_get_server_and_channel (struct t_gui_buffer *buffer,
struct t_irc_server **server,
struct t_irc_channel **channel)
{
struct t_irc_server *ptr_server;
struct t_irc_channel *ptr_channel;
if (server)
*server = NULL;
if (channel)
*channel = NULL;
if (!buffer)
return;
/* look for a server or channel using this buffer */
for (ptr_server = irc_servers; ptr_server;
ptr_server = ptr_server->next_server)
{
if (ptr_server->buffer == buffer)
{
if (server)
*server = ptr_server;
return;
}
for (ptr_channel = ptr_server->channels; ptr_channel;
ptr_channel = ptr_channel->next_channel)
{
if (ptr_channel->buffer == buffer)
{
if (server)
*server = ptr_server;
if (channel)
*channel = ptr_channel;
return;
}
}
}
/* no server or channel found */
}
/*
* Builds buffer name with a server and a channel.
*/
const char *
irc_buffer_build_name (const char *server, const char *channel)
{
static char buffer[128];
buffer[0] = '\0';
if (!server && !channel)
return buffer;
if (server && channel)
snprintf (buffer, sizeof (buffer), "%s.%s", server, channel);
else if (server)
snprintf (buffer, sizeof (buffer), "server.%s", server);
else
snprintf (buffer, sizeof (buffer), "%s", channel);
return buffer;
}
/*
* 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_BUFFER_GET_SERVER_CHANNEL(buffer);
/* make C compiler happy */
(void) data;
if (buffer == irc_raw_buffer)
{
irc_raw_buffer = NULL;
}
else
{
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)
{
if (!ptr_server->disconnected)
{
/* send QUIT to server, then disconnect */
irc_command_quit_server (ptr_server, NULL);
irc_server_disconnect (ptr_server, 0, 0);
}
ptr_channel = ptr_server->channels;
while (ptr_channel)
{
next_channel = ptr_channel->next_channel;
if (ptr_channel->buffer != buffer)
weechat_buffer_close (ptr_channel->buffer);
ptr_channel = next_channel;
}
ptr_server->buffer = NULL;
}
}
}
return WEECHAT_RC_OK;
}
/*
* Callback for comparing two nicks in nicklist (called when searching a nick in
* nicklist).
* The "casemapping" of server is used in comparison.
*
* Returns:
* -1: nick1 < nick2
* 0: nick1 == nick2
* 1: nick2 > nick2
*/
int
irc_buffer_nickcmp_cb (void *data,
struct t_gui_buffer *buffer,
const char *nick1,
const char *nick2)
{
struct t_irc_server *server;
if (data)
server = (struct t_irc_server *)data;
else
irc_buffer_get_server_and_channel (buffer, &server, NULL);
if (server)
{
return irc_server_strcasecmp (server, nick1, nick2);
}
else
{
/* default is RFC 1459 casemapping comparison */
return weechat_strcasecmp_range (nick1, nick2, 29);
}
}
/*
* Searches for the server buffer with the lowest number.
*
* Returns pointer to buffer found, NULL if not found.
*/
struct t_gui_buffer *
irc_buffer_search_server_lowest_number ()
{
struct t_gui_buffer *ptr_buffer;
struct t_irc_server *ptr_server;
int number, number_found;
ptr_buffer = NULL;
number_found = INT_MAX;
for (ptr_server = irc_servers; ptr_server;
ptr_server = ptr_server->next_server)
{
if (ptr_server->buffer)
{
number = weechat_buffer_get_integer (ptr_server->buffer, "number");
if (number < number_found)
{
number_found = number;
ptr_buffer = ptr_server->buffer;
}
}
}
return ptr_buffer;
}
/*
* Searches for the private buffer with the lowest number.
* If server is not NULL, searches only for this server.
*
* Returns pointer to buffer found, NULL if not found.
*/
struct t_gui_buffer *
irc_buffer_search_private_lowest_number (struct t_irc_server *server)
{
struct t_gui_buffer *ptr_buffer;
struct t_irc_server *ptr_server;
struct t_irc_channel *ptr_channel;
int number, number_found;
ptr_buffer = NULL;
number_found = INT_MAX;
for (ptr_server = (server) ? server : irc_servers; ptr_server;
ptr_server = ptr_server->next_server)
{
for (ptr_channel = ptr_server->channels; ptr_channel;
ptr_channel = ptr_channel->next_channel)
{
if ((ptr_channel->type == IRC_CHANNEL_TYPE_PRIVATE)
&& ptr_channel->buffer)
{
number = weechat_buffer_get_integer (ptr_channel->buffer,
"number");
if (number < number_found)
{
number_found = number;
ptr_buffer = ptr_channel->buffer;
}
}
}
if (server)
break;
}
return ptr_buffer;
}
|