1 | /********************************************************************\ |
---|
2 | * BitlBee -- An IRC to other IM-networks gateway * |
---|
3 | * * |
---|
4 | * Copyright 2002-2004 Wilmer van der Gaast and others * |
---|
5 | \********************************************************************/ |
---|
6 | |
---|
7 | /* The IRC-based UI (for now the only one) */ |
---|
8 | |
---|
9 | /* |
---|
10 | This program is free software; you can redistribute it and/or modify |
---|
11 | it under the terms of the GNU General Public License as published by |
---|
12 | the Free Software Foundation; either version 2 of the License, or |
---|
13 | (at your option) any later version. |
---|
14 | |
---|
15 | This program is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License with |
---|
21 | the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; |
---|
22 | if not, write to the Free Software Foundation, Inc., 59 Temple Place, |
---|
23 | Suite 330, Boston, MA 02111-1307 USA |
---|
24 | */ |
---|
25 | |
---|
26 | #include "bitlbee.h" |
---|
27 | #include "ipc.h" |
---|
28 | #include "dcc.h" |
---|
29 | |
---|
30 | GSList *irc_connection_list; |
---|
31 | GSList *irc_plugins; |
---|
32 | |
---|
33 | static gboolean irc_userping( gpointer _irc, gint fd, b_input_condition cond ); |
---|
34 | static char *set_eval_charset( set_t *set, char *value ); |
---|
35 | static char *set_eval_password( set_t *set, char *value ); |
---|
36 | static char *set_eval_bw_compat( set_t *set, char *value ); |
---|
37 | |
---|
38 | irc_t *irc_new( int fd ) |
---|
39 | { |
---|
40 | irc_t *irc; |
---|
41 | struct sockaddr_storage sock; |
---|
42 | socklen_t socklen = sizeof( sock ); |
---|
43 | char *host = NULL, *myhost = NULL; |
---|
44 | irc_user_t *iu; |
---|
45 | GSList *l; |
---|
46 | set_t *s; |
---|
47 | bee_t *b; |
---|
48 | |
---|
49 | irc = g_new0( irc_t, 1 ); |
---|
50 | |
---|
51 | irc->fd = fd; |
---|
52 | sock_make_nonblocking( irc->fd ); |
---|
53 | |
---|
54 | irc->r_watch_source_id = b_input_add( irc->fd, B_EV_IO_READ, bitlbee_io_current_client_read, irc ); |
---|
55 | |
---|
56 | irc->status = USTATUS_OFFLINE; |
---|
57 | irc->last_pong = gettime(); |
---|
58 | |
---|
59 | irc->nick_user_hash = g_hash_table_new( g_str_hash, g_str_equal ); |
---|
60 | irc->watches = g_hash_table_new( g_str_hash, g_str_equal ); |
---|
61 | |
---|
62 | irc->iconv = (GIConv) -1; |
---|
63 | irc->oconv = (GIConv) -1; |
---|
64 | |
---|
65 | if( global.conf->hostname ) |
---|
66 | { |
---|
67 | myhost = g_strdup( global.conf->hostname ); |
---|
68 | } |
---|
69 | else if( getsockname( irc->fd, (struct sockaddr*) &sock, &socklen ) == 0 ) |
---|
70 | { |
---|
71 | char buf[NI_MAXHOST+1]; |
---|
72 | |
---|
73 | if( getnameinfo( (struct sockaddr *) &sock, socklen, buf, |
---|
74 | NI_MAXHOST, NULL, 0, 0 ) == 0 ) |
---|
75 | { |
---|
76 | myhost = g_strdup( ipv6_unwrap( buf ) ); |
---|
77 | } |
---|
78 | } |
---|
79 | |
---|
80 | if( getpeername( irc->fd, (struct sockaddr*) &sock, &socklen ) == 0 ) |
---|
81 | { |
---|
82 | char buf[NI_MAXHOST+1]; |
---|
83 | |
---|
84 | if( getnameinfo( (struct sockaddr *)&sock, socklen, buf, |
---|
85 | NI_MAXHOST, NULL, 0, 0 ) == 0 ) |
---|
86 | { |
---|
87 | host = g_strdup( ipv6_unwrap( buf ) ); |
---|
88 | } |
---|
89 | } |
---|
90 | |
---|
91 | if( host == NULL ) |
---|
92 | host = g_strdup( "localhost.localdomain" ); |
---|
93 | if( myhost == NULL ) |
---|
94 | myhost = g_strdup( "localhost.localdomain" ); |
---|
95 | |
---|
96 | if( global.conf->ping_interval > 0 && global.conf->ping_timeout > 0 ) |
---|
97 | irc->ping_source_id = b_timeout_add( global.conf->ping_interval * 1000, irc_userping, irc ); |
---|
98 | |
---|
99 | irc_connection_list = g_slist_append( irc_connection_list, irc ); |
---|
100 | |
---|
101 | b = irc->b = bee_new(); |
---|
102 | b->ui_data = irc; |
---|
103 | b->ui = &irc_ui_funcs; |
---|
104 | |
---|
105 | s = set_add( &b->set, "allow_takeover", "true", set_eval_bool, irc ); |
---|
106 | s = set_add( &b->set, "away_devoice", "true", set_eval_bw_compat, irc ); |
---|
107 | s->flags |= SET_HIDDEN; |
---|
108 | s = set_add( &b->set, "away_reply_timeout", "3600", set_eval_int, irc ); |
---|
109 | s = set_add( &b->set, "charset", "utf-8", set_eval_charset, irc ); |
---|
110 | s = set_add( &b->set, "default_target", "root", NULL, irc ); |
---|
111 | s = set_add( &b->set, "display_namechanges", "false", set_eval_bool, irc ); |
---|
112 | s = set_add( &b->set, "display_timestamps", "true", set_eval_bool, irc ); |
---|
113 | s = set_add( &b->set, "handle_unknown", "add_channel", NULL, irc ); |
---|
114 | s = set_add( &b->set, "last_version", NULL, NULL, irc ); |
---|
115 | s->flags |= SET_HIDDEN; |
---|
116 | s->value = g_strdup_printf( "%d", BITLBEE_VERSION_CODE ); |
---|
117 | s = set_add( &b->set, "lcnicks", "true", set_eval_bool, irc ); |
---|
118 | s = set_add( &b->set, "nick_format", "%-@nick", NULL, irc ); |
---|
119 | s = set_add( &b->set, "offline_user_quits", "true", set_eval_bool, irc ); |
---|
120 | s = set_add( &b->set, "ops", "both", set_eval_irc_channel_ops, irc ); |
---|
121 | s = set_add( &b->set, "paste_buffer", "false", set_eval_bool, irc ); |
---|
122 | s->old_key = g_strdup( "buddy_sendbuffer" ); |
---|
123 | s = set_add( &b->set, "paste_buffer_delay", "200", set_eval_int, irc ); |
---|
124 | s->old_key = g_strdup( "buddy_sendbuffer_delay" ); |
---|
125 | s = set_add( &b->set, "password", NULL, set_eval_password, irc ); |
---|
126 | s->flags |= SET_NULL_OK; |
---|
127 | s = set_add( &b->set, "private", "true", set_eval_bool, irc ); |
---|
128 | s = set_add( &b->set, "query_order", "lifo", NULL, irc ); |
---|
129 | s = set_add( &b->set, "root_nick", ROOT_NICK, set_eval_root_nick, irc ); |
---|
130 | s->flags |= SET_HIDDEN; |
---|
131 | s = set_add( &b->set, "show_offline", "false", set_eval_bw_compat, irc ); |
---|
132 | s->flags |= SET_HIDDEN; |
---|
133 | s = set_add( &b->set, "simulate_netsplit", "true", set_eval_bool, irc ); |
---|
134 | s = set_add( &b->set, "timezone", "local", set_eval_timezone, irc ); |
---|
135 | s = set_add( &b->set, "to_char", ": ", set_eval_to_char, irc ); |
---|
136 | s = set_add( &b->set, "typing_notice", "false", set_eval_bool, irc ); |
---|
137 | |
---|
138 | irc->root = iu = irc_user_new( irc, ROOT_NICK ); |
---|
139 | iu->host = g_strdup( myhost ); |
---|
140 | iu->fullname = g_strdup( ROOT_FN ); |
---|
141 | iu->f = &irc_user_root_funcs; |
---|
142 | |
---|
143 | iu = irc_user_new( irc, NS_NICK ); |
---|
144 | iu->host = g_strdup( myhost ); |
---|
145 | iu->fullname = g_strdup( ROOT_FN ); |
---|
146 | iu->f = &irc_user_root_funcs; |
---|
147 | |
---|
148 | irc->user = g_new0( irc_user_t, 1 ); |
---|
149 | irc->user->host = g_strdup( host ); |
---|
150 | |
---|
151 | conf_loaddefaults( irc ); |
---|
152 | |
---|
153 | /* Evaluator sets the iconv/oconv structures. */ |
---|
154 | set_eval_charset( set_find( &b->set, "charset" ), set_getstr( &b->set, "charset" ) ); |
---|
155 | |
---|
156 | irc_write( irc, ":%s NOTICE AUTH :%s", irc->root->host, "BitlBee-IRCd initialized, please go on" ); |
---|
157 | if( isatty( irc->fd ) ) |
---|
158 | irc_write( irc, ":%s NOTICE AUTH :%s", irc->root->host, |
---|
159 | "If you read this, you most likely accidentally " |
---|
160 | "started BitlBee in inetd mode on the command line. " |
---|
161 | "You probably want to run it in (Fork)Daemon mode. " |
---|
162 | "See doc/README for more information." ); |
---|
163 | |
---|
164 | g_free( myhost ); |
---|
165 | g_free( host ); |
---|
166 | |
---|
167 | nogaim_init(); |
---|
168 | |
---|
169 | for( l = irc_plugins; l; l = l->next ) |
---|
170 | { |
---|
171 | irc_plugin_t *p = l->data; |
---|
172 | if( p->irc_new ) |
---|
173 | p->irc_new( irc ); |
---|
174 | } |
---|
175 | |
---|
176 | return irc; |
---|
177 | } |
---|
178 | |
---|
179 | /* immed=1 makes this function pretty much equal to irc_free(), except that |
---|
180 | this one will "log". In case the connection is already broken and we |
---|
181 | shouldn't try to write to it. */ |
---|
182 | void irc_abort( irc_t *irc, int immed, char *format, ... ) |
---|
183 | { |
---|
184 | char *reason = NULL; |
---|
185 | |
---|
186 | if( format != NULL ) |
---|
187 | { |
---|
188 | va_list params; |
---|
189 | |
---|
190 | va_start( params, format ); |
---|
191 | reason = g_strdup_vprintf( format, params ); |
---|
192 | va_end( params ); |
---|
193 | } |
---|
194 | |
---|
195 | irc_write( irc, "ERROR :Closing link: %s", reason ? : "" ); |
---|
196 | |
---|
197 | ipc_to_master_str( "OPERMSG :Client exiting: %s@%s [%s]\r\n", |
---|
198 | irc->user->nick ? irc->user->nick : "(NONE)", |
---|
199 | irc->user->host, reason ? : "" ); |
---|
200 | |
---|
201 | g_free( reason ); |
---|
202 | |
---|
203 | irc_flush( irc ); |
---|
204 | if( immed ) |
---|
205 | { |
---|
206 | irc_free( irc ); |
---|
207 | } |
---|
208 | else |
---|
209 | { |
---|
210 | b_event_remove( irc->ping_source_id ); |
---|
211 | irc->ping_source_id = b_timeout_add( 1, (b_event_handler) irc_free, irc ); |
---|
212 | } |
---|
213 | } |
---|
214 | |
---|
215 | static gboolean irc_free_hashkey( gpointer key, gpointer value, gpointer data ); |
---|
216 | |
---|
217 | void irc_free( irc_t * irc ) |
---|
218 | { |
---|
219 | GSList *l; |
---|
220 | |
---|
221 | irc->status |= USTATUS_SHUTDOWN; |
---|
222 | |
---|
223 | log_message( LOGLVL_INFO, "Destroying connection with fd %d", irc->fd ); |
---|
224 | |
---|
225 | if( irc->status & USTATUS_IDENTIFIED && set_getbool( &irc->b->set, "save_on_quit" ) ) |
---|
226 | if( storage_save( irc, NULL, TRUE ) != STORAGE_OK ) |
---|
227 | log_message( LOGLVL_WARNING, "Error while saving settings for user %s", irc->user->nick ); |
---|
228 | |
---|
229 | for( l = irc_plugins; l; l = l->next ) |
---|
230 | { |
---|
231 | irc_plugin_t *p = l->data; |
---|
232 | if( p->irc_free ) |
---|
233 | p->irc_free( irc ); |
---|
234 | } |
---|
235 | |
---|
236 | irc_connection_list = g_slist_remove( irc_connection_list, irc ); |
---|
237 | |
---|
238 | while( irc->queries != NULL ) |
---|
239 | query_del( irc, irc->queries ); |
---|
240 | |
---|
241 | /* This is a little bit messy: bee_free() frees all b->users which |
---|
242 | calls us back to free the corresponding irc->users. So do this |
---|
243 | before we clear the remaining ones ourselves. */ |
---|
244 | bee_free( irc->b ); |
---|
245 | |
---|
246 | while( irc->users ) |
---|
247 | irc_user_free( irc, (irc_user_t *) irc->users->data ); |
---|
248 | |
---|
249 | while( irc->channels ) |
---|
250 | irc_channel_free( irc->channels->data ); |
---|
251 | |
---|
252 | if( irc->ping_source_id > 0 ) |
---|
253 | b_event_remove( irc->ping_source_id ); |
---|
254 | if( irc->r_watch_source_id > 0 ) |
---|
255 | b_event_remove( irc->r_watch_source_id ); |
---|
256 | if( irc->w_watch_source_id > 0 ) |
---|
257 | b_event_remove( irc->w_watch_source_id ); |
---|
258 | |
---|
259 | closesocket( irc->fd ); |
---|
260 | irc->fd = -1; |
---|
261 | |
---|
262 | g_hash_table_foreach_remove( irc->nick_user_hash, irc_free_hashkey, NULL ); |
---|
263 | g_hash_table_destroy( irc->nick_user_hash ); |
---|
264 | |
---|
265 | g_hash_table_foreach_remove( irc->watches, irc_free_hashkey, NULL ); |
---|
266 | g_hash_table_destroy( irc->watches ); |
---|
267 | |
---|
268 | if( irc->iconv != (GIConv) -1 ) |
---|
269 | g_iconv_close( irc->iconv ); |
---|
270 | if( irc->oconv != (GIConv) -1 ) |
---|
271 | g_iconv_close( irc->oconv ); |
---|
272 | |
---|
273 | g_free( irc->sendbuffer ); |
---|
274 | g_free( irc->readbuffer ); |
---|
275 | g_free( irc->password ); |
---|
276 | |
---|
277 | g_free( irc ); |
---|
278 | |
---|
279 | if( global.conf->runmode == RUNMODE_INETD || |
---|
280 | global.conf->runmode == RUNMODE_FORKDAEMON || |
---|
281 | ( global.conf->runmode == RUNMODE_DAEMON && |
---|
282 | global.listen_socket == -1 && |
---|
283 | irc_connection_list == NULL ) ) |
---|
284 | b_main_quit(); |
---|
285 | } |
---|
286 | |
---|
287 | static gboolean irc_free_hashkey( gpointer key, gpointer value, gpointer data ) |
---|
288 | { |
---|
289 | g_free( key ); |
---|
290 | |
---|
291 | return( TRUE ); |
---|
292 | } |
---|
293 | |
---|
294 | /* USE WITH CAUTION! |
---|
295 | Sets pass without checking */ |
---|
296 | void irc_setpass (irc_t *irc, const char *pass) |
---|
297 | { |
---|
298 | g_free (irc->password); |
---|
299 | |
---|
300 | if (pass) { |
---|
301 | irc->password = g_strdup (pass); |
---|
302 | } else { |
---|
303 | irc->password = NULL; |
---|
304 | } |
---|
305 | } |
---|
306 | |
---|
307 | static char *set_eval_password( set_t *set, char *value ) |
---|
308 | { |
---|
309 | irc_t *irc = set->data; |
---|
310 | |
---|
311 | if( irc->status & USTATUS_IDENTIFIED && value ) |
---|
312 | { |
---|
313 | irc_setpass( irc, value ); |
---|
314 | return NULL; |
---|
315 | } |
---|
316 | else |
---|
317 | { |
---|
318 | return SET_INVALID; |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | static char **irc_splitlines( char *buffer ); |
---|
323 | |
---|
324 | void irc_process( irc_t *irc ) |
---|
325 | { |
---|
326 | char **lines, *temp, **cmd; |
---|
327 | int i; |
---|
328 | |
---|
329 | if( irc->readbuffer != NULL ) |
---|
330 | { |
---|
331 | lines = irc_splitlines( irc->readbuffer ); |
---|
332 | |
---|
333 | for( i = 0; *lines[i] != '\0'; i ++ ) |
---|
334 | { |
---|
335 | char *conv = NULL; |
---|
336 | |
---|
337 | /* [WvG] If the last line isn't empty, it's an incomplete line and we |
---|
338 | should wait for the rest to come in before processing it. */ |
---|
339 | if( lines[i+1] == NULL ) |
---|
340 | { |
---|
341 | temp = g_strdup( lines[i] ); |
---|
342 | g_free( irc->readbuffer ); |
---|
343 | irc->readbuffer = temp; |
---|
344 | i ++; |
---|
345 | break; |
---|
346 | } |
---|
347 | |
---|
348 | if( irc->iconv != (GIConv) -1 ) |
---|
349 | { |
---|
350 | gsize bytes_read, bytes_written; |
---|
351 | |
---|
352 | conv = g_convert_with_iconv( lines[i], -1, irc->iconv, |
---|
353 | &bytes_read, &bytes_written, NULL ); |
---|
354 | |
---|
355 | if( conv == NULL || bytes_read != strlen( lines[i] ) ) |
---|
356 | { |
---|
357 | /* GLib can do strange things if things are not in the expected charset, |
---|
358 | so let's be a little bit paranoid here: */ |
---|
359 | if( irc->status & USTATUS_LOGGED_IN ) |
---|
360 | { |
---|
361 | irc_usermsg( irc, "Error: Charset mismatch detected. The charset " |
---|
362 | "setting is currently set to %s, so please make " |
---|
363 | "sure your IRC client will send and accept text in " |
---|
364 | "that charset, or tell BitlBee which charset to " |
---|
365 | "expect by changing the charset setting. See " |
---|
366 | "`help set charset' for more information. Your " |
---|
367 | "message was ignored.", |
---|
368 | set_getstr( &irc->b->set, "charset" ) ); |
---|
369 | |
---|
370 | g_free( conv ); |
---|
371 | conv = NULL; |
---|
372 | } |
---|
373 | else |
---|
374 | { |
---|
375 | irc_write( irc, ":%s NOTICE AUTH :%s", irc->root->host, |
---|
376 | "Warning: invalid characters received at login time." ); |
---|
377 | |
---|
378 | conv = g_strdup( lines[i] ); |
---|
379 | for( temp = conv; *temp; temp ++ ) |
---|
380 | if( *temp & 0x80 ) |
---|
381 | *temp = '?'; |
---|
382 | } |
---|
383 | } |
---|
384 | lines[i] = conv; |
---|
385 | } |
---|
386 | |
---|
387 | if( lines[i] && ( cmd = irc_parse_line( lines[i] ) ) ) |
---|
388 | { |
---|
389 | irc_exec( irc, cmd ); |
---|
390 | g_free( cmd ); |
---|
391 | } |
---|
392 | |
---|
393 | g_free( conv ); |
---|
394 | |
---|
395 | /* Shouldn't really happen, but just in case... */ |
---|
396 | if( !g_slist_find( irc_connection_list, irc ) ) |
---|
397 | { |
---|
398 | g_free( lines ); |
---|
399 | return; |
---|
400 | } |
---|
401 | } |
---|
402 | |
---|
403 | if( lines[i] != NULL ) |
---|
404 | { |
---|
405 | g_free( irc->readbuffer ); |
---|
406 | irc->readbuffer = NULL; |
---|
407 | } |
---|
408 | |
---|
409 | g_free( lines ); |
---|
410 | } |
---|
411 | } |
---|
412 | |
---|
413 | /* Splits a long string into separate lines. The array is NULL-terminated |
---|
414 | and, unless the string contains an incomplete line at the end, ends with |
---|
415 | an empty string. Could use g_strsplit() but this one does it in-place. |
---|
416 | (So yes, it's destructive.) */ |
---|
417 | static char **irc_splitlines( char *buffer ) |
---|
418 | { |
---|
419 | int i, j, n = 3; |
---|
420 | char **lines; |
---|
421 | |
---|
422 | /* Allocate n+1 elements. */ |
---|
423 | lines = g_new( char *, n + 1 ); |
---|
424 | |
---|
425 | lines[0] = buffer; |
---|
426 | |
---|
427 | /* Split the buffer in several strings, and accept any kind of line endings, |
---|
428 | * knowing that ERC on Windows may send something interesting like \r\r\n, |
---|
429 | * and surely there must be clients that think just \n is enough... */ |
---|
430 | for( i = 0, j = 0; buffer[i] != '\0'; i ++ ) |
---|
431 | { |
---|
432 | if( buffer[i] == '\r' || buffer[i] == '\n' ) |
---|
433 | { |
---|
434 | while( buffer[i] == '\r' || buffer[i] == '\n' ) |
---|
435 | buffer[i++] = '\0'; |
---|
436 | |
---|
437 | lines[++j] = buffer + i; |
---|
438 | |
---|
439 | if( j >= n ) |
---|
440 | { |
---|
441 | n *= 2; |
---|
442 | lines = g_renew( char *, lines, n + 1 ); |
---|
443 | } |
---|
444 | |
---|
445 | if( buffer[i] == '\0' ) |
---|
446 | break; |
---|
447 | } |
---|
448 | } |
---|
449 | |
---|
450 | /* NULL terminate our list. */ |
---|
451 | lines[++j] = NULL; |
---|
452 | |
---|
453 | return lines; |
---|
454 | } |
---|
455 | |
---|
456 | /* Split an IRC-style line into little parts/arguments. */ |
---|
457 | char **irc_parse_line( char *line ) |
---|
458 | { |
---|
459 | int i, j; |
---|
460 | char **cmd; |
---|
461 | |
---|
462 | /* Move the line pointer to the start of the command, skipping spaces and the optional prefix. */ |
---|
463 | if( line[0] == ':' ) |
---|
464 | { |
---|
465 | for( i = 0; line[i] && line[i] != ' '; i ++ ); |
---|
466 | line = line + i; |
---|
467 | } |
---|
468 | for( i = 0; line[i] == ' '; i ++ ); |
---|
469 | line = line + i; |
---|
470 | |
---|
471 | /* If we're already at the end of the line, return. If not, we're going to need at least one element. */ |
---|
472 | if( line[0] == '\0') |
---|
473 | return NULL; |
---|
474 | |
---|
475 | /* Count the number of char **cmd elements we're going to need. */ |
---|
476 | j = 1; |
---|
477 | for( i = 0; line[i] != '\0'; i ++ ) |
---|
478 | { |
---|
479 | if( line[i] == ' ' ) |
---|
480 | { |
---|
481 | j ++; |
---|
482 | |
---|
483 | if( line[i+1] == ':' ) |
---|
484 | break; |
---|
485 | } |
---|
486 | } |
---|
487 | |
---|
488 | /* Allocate the space we need. */ |
---|
489 | cmd = g_new( char *, j + 1 ); |
---|
490 | cmd[j] = NULL; |
---|
491 | |
---|
492 | /* Do the actual line splitting, format is: |
---|
493 | * Input: "PRIVMSG #bitlbee :foo bar" |
---|
494 | * Output: cmd[0]=="PRIVMSG", cmd[1]=="#bitlbee", cmd[2]=="foo bar", cmd[3]==NULL |
---|
495 | */ |
---|
496 | |
---|
497 | cmd[0] = line; |
---|
498 | for( i = 0, j = 0; line[i] != '\0'; i ++ ) |
---|
499 | { |
---|
500 | if( line[i] == ' ' ) |
---|
501 | { |
---|
502 | line[i] = '\0'; |
---|
503 | cmd[++j] = line + i + 1; |
---|
504 | |
---|
505 | if( line[i+1] == ':' ) |
---|
506 | { |
---|
507 | cmd[j] ++; |
---|
508 | break; |
---|
509 | } |
---|
510 | } |
---|
511 | } |
---|
512 | |
---|
513 | return cmd; |
---|
514 | } |
---|
515 | |
---|
516 | /* Converts such an array back into a command string. Mainly used for the IPC code right now. */ |
---|
517 | char *irc_build_line( char **cmd ) |
---|
518 | { |
---|
519 | int i, len; |
---|
520 | char *s; |
---|
521 | |
---|
522 | if( cmd[0] == NULL ) |
---|
523 | return NULL; |
---|
524 | |
---|
525 | len = 1; |
---|
526 | for( i = 0; cmd[i]; i ++ ) |
---|
527 | len += strlen( cmd[i] ) + 1; |
---|
528 | |
---|
529 | if( strchr( cmd[i-1], ' ' ) != NULL ) |
---|
530 | len ++; |
---|
531 | |
---|
532 | s = g_new0( char, len + 1 ); |
---|
533 | for( i = 0; cmd[i]; i ++ ) |
---|
534 | { |
---|
535 | if( cmd[i+1] == NULL && strchr( cmd[i], ' ' ) != NULL ) |
---|
536 | strcat( s, ":" ); |
---|
537 | |
---|
538 | strcat( s, cmd[i] ); |
---|
539 | |
---|
540 | if( cmd[i+1] ) |
---|
541 | strcat( s, " " ); |
---|
542 | } |
---|
543 | strcat( s, "\r\n" ); |
---|
544 | |
---|
545 | return s; |
---|
546 | } |
---|
547 | |
---|
548 | void irc_write( irc_t *irc, char *format, ... ) |
---|
549 | { |
---|
550 | va_list params; |
---|
551 | |
---|
552 | va_start( params, format ); |
---|
553 | irc_vawrite( irc, format, params ); |
---|
554 | va_end( params ); |
---|
555 | |
---|
556 | return; |
---|
557 | } |
---|
558 | |
---|
559 | void irc_write_all( int now, char *format, ... ) |
---|
560 | { |
---|
561 | va_list params; |
---|
562 | GSList *temp; |
---|
563 | |
---|
564 | va_start( params, format ); |
---|
565 | |
---|
566 | temp = irc_connection_list; |
---|
567 | while( temp != NULL ) |
---|
568 | { |
---|
569 | irc_t *irc = temp->data; |
---|
570 | |
---|
571 | if( now ) |
---|
572 | { |
---|
573 | g_free( irc->sendbuffer ); |
---|
574 | irc->sendbuffer = g_strdup( "\r\n" ); |
---|
575 | } |
---|
576 | irc_vawrite( temp->data, format, params ); |
---|
577 | if( now ) |
---|
578 | { |
---|
579 | bitlbee_io_current_client_write( irc, irc->fd, B_EV_IO_WRITE ); |
---|
580 | } |
---|
581 | temp = temp->next; |
---|
582 | } |
---|
583 | |
---|
584 | va_end( params ); |
---|
585 | return; |
---|
586 | } |
---|
587 | |
---|
588 | void irc_vawrite( irc_t *irc, char *format, va_list params ) |
---|
589 | { |
---|
590 | int size; |
---|
591 | char line[IRC_MAX_LINE+1]; |
---|
592 | |
---|
593 | /* Don't try to write anything new anymore when shutting down. */ |
---|
594 | if( irc->status & USTATUS_SHUTDOWN ) |
---|
595 | return; |
---|
596 | |
---|
597 | memset( line, 0, sizeof( line ) ); |
---|
598 | g_vsnprintf( line, IRC_MAX_LINE - 2, format, params ); |
---|
599 | strip_newlines( line ); |
---|
600 | |
---|
601 | if( irc->oconv != (GIConv) -1 ) |
---|
602 | { |
---|
603 | gsize bytes_read, bytes_written; |
---|
604 | char *conv; |
---|
605 | |
---|
606 | conv = g_convert_with_iconv( line, -1, irc->oconv, |
---|
607 | &bytes_read, &bytes_written, NULL ); |
---|
608 | |
---|
609 | if( bytes_read == strlen( line ) ) |
---|
610 | strncpy( line, conv, IRC_MAX_LINE - 2 ); |
---|
611 | |
---|
612 | g_free( conv ); |
---|
613 | } |
---|
614 | g_strlcat( line, "\r\n", IRC_MAX_LINE + 1 ); |
---|
615 | |
---|
616 | if( irc->sendbuffer != NULL ) |
---|
617 | { |
---|
618 | size = strlen( irc->sendbuffer ) + strlen( line ); |
---|
619 | irc->sendbuffer = g_renew ( char, irc->sendbuffer, size + 1 ); |
---|
620 | strcpy( ( irc->sendbuffer + strlen( irc->sendbuffer ) ), line ); |
---|
621 | } |
---|
622 | else |
---|
623 | { |
---|
624 | irc->sendbuffer = g_strdup(line); |
---|
625 | } |
---|
626 | |
---|
627 | if( irc->w_watch_source_id == 0 ) |
---|
628 | { |
---|
629 | /* If the buffer is empty we can probably write, so call the write event handler |
---|
630 | immediately. If it returns TRUE, it should be called again, so add the event to |
---|
631 | the queue. If it's FALSE, we emptied the buffer and saved ourselves some work |
---|
632 | in the event queue. */ |
---|
633 | /* Really can't be done as long as the code doesn't do error checking very well: |
---|
634 | if( bitlbee_io_current_client_write( irc, irc->fd, B_EV_IO_WRITE ) ) */ |
---|
635 | |
---|
636 | /* So just always do it via the event handler. */ |
---|
637 | irc->w_watch_source_id = b_input_add( irc->fd, B_EV_IO_WRITE, bitlbee_io_current_client_write, irc ); |
---|
638 | } |
---|
639 | |
---|
640 | return; |
---|
641 | } |
---|
642 | |
---|
643 | /* Flush sendbuffer if you can. If it fails, fail silently and let some |
---|
644 | I/O event handler clean up. */ |
---|
645 | void irc_flush( irc_t *irc ) |
---|
646 | { |
---|
647 | ssize_t n; |
---|
648 | size_t len; |
---|
649 | |
---|
650 | if( irc->sendbuffer == NULL ) |
---|
651 | return; |
---|
652 | |
---|
653 | len = strlen( irc->sendbuffer ); |
---|
654 | if( ( n = send( irc->fd, irc->sendbuffer, len, 0 ) ) == len ) |
---|
655 | { |
---|
656 | g_free( irc->sendbuffer ); |
---|
657 | irc->sendbuffer = NULL; |
---|
658 | |
---|
659 | b_event_remove( irc->w_watch_source_id ); |
---|
660 | irc->w_watch_source_id = 0; |
---|
661 | } |
---|
662 | else if( n > 0 ) |
---|
663 | { |
---|
664 | char *s = g_strdup( irc->sendbuffer + n ); |
---|
665 | g_free( irc->sendbuffer ); |
---|
666 | irc->sendbuffer = s; |
---|
667 | } |
---|
668 | /* Otherwise something went wrong and we don't currently care |
---|
669 | what the error was. We may or may not succeed later, we |
---|
670 | were just trying to flush the buffer immediately. */ |
---|
671 | } |
---|
672 | |
---|
673 | /* Meant for takeover functionality. Transfer an IRC connection to a different |
---|
674 | socket. */ |
---|
675 | void irc_switch_fd( irc_t *irc, int fd ) |
---|
676 | { |
---|
677 | irc_write( irc, "ERROR :Transferring session to a new connection" ); |
---|
678 | irc_flush( irc ); /* Write it now or forget about it forever. */ |
---|
679 | |
---|
680 | if( irc->sendbuffer ) |
---|
681 | { |
---|
682 | b_event_remove( irc->w_watch_source_id ); |
---|
683 | irc->w_watch_source_id = 0; |
---|
684 | g_free( irc->sendbuffer ); |
---|
685 | irc->sendbuffer = NULL; |
---|
686 | } |
---|
687 | |
---|
688 | b_event_remove( irc->r_watch_source_id ); |
---|
689 | closesocket( irc->fd ); |
---|
690 | irc->fd = fd; |
---|
691 | irc->r_watch_source_id = b_input_add( irc->fd, B_EV_IO_READ, bitlbee_io_current_client_read, irc ); |
---|
692 | } |
---|
693 | |
---|
694 | void irc_sync( irc_t *irc ) |
---|
695 | { |
---|
696 | GSList *l; |
---|
697 | |
---|
698 | irc_write( irc, ":%s!%s@%s MODE %s :+%s", irc->user->nick, |
---|
699 | irc->user->user, irc->user->host, irc->user->nick, |
---|
700 | irc->umode ); |
---|
701 | |
---|
702 | for( l = irc->channels; l; l = l->next ) |
---|
703 | { |
---|
704 | irc_channel_t *ic = l->data; |
---|
705 | if( ic->flags & IRC_CHANNEL_JOINED ) |
---|
706 | irc_send_join( ic, irc->user ); |
---|
707 | } |
---|
708 | } |
---|
709 | |
---|
710 | void irc_desync( irc_t *irc ) |
---|
711 | { |
---|
712 | GSList *l; |
---|
713 | |
---|
714 | for( l = irc->channels; l; l = l->next ) |
---|
715 | irc_channel_del_user( l->data, irc->user, IRC_CDU_KICK, |
---|
716 | "Switching to old session" ); |
---|
717 | |
---|
718 | irc_write( irc, ":%s!%s@%s MODE %s :-%s", irc->user->nick, |
---|
719 | irc->user->user, irc->user->host, irc->user->nick, |
---|
720 | irc->umode ); |
---|
721 | } |
---|
722 | |
---|
723 | int irc_check_login( irc_t *irc ) |
---|
724 | { |
---|
725 | if( irc->user->user && irc->user->nick ) |
---|
726 | { |
---|
727 | if( global.conf->authmode == AUTHMODE_CLOSED && !( irc->status & USTATUS_AUTHORIZED ) ) |
---|
728 | { |
---|
729 | irc_send_num( irc, 464, ":This server is password-protected." ); |
---|
730 | return 0; |
---|
731 | } |
---|
732 | else |
---|
733 | { |
---|
734 | irc_channel_t *ic; |
---|
735 | irc_user_t *iu = irc->user; |
---|
736 | |
---|
737 | irc->user = irc_user_new( irc, iu->nick ); |
---|
738 | irc->user->user = iu->user; |
---|
739 | irc->user->host = iu->host; |
---|
740 | irc->user->fullname = iu->fullname; |
---|
741 | irc->user->f = &irc_user_self_funcs; |
---|
742 | g_free( iu->nick ); |
---|
743 | g_free( iu ); |
---|
744 | |
---|
745 | if( global.conf->runmode == RUNMODE_FORKDAEMON || global.conf->runmode == RUNMODE_DAEMON ) |
---|
746 | ipc_to_master_str( "CLIENT %s %s :%s\r\n", irc->user->host, irc->user->nick, irc->user->fullname ); |
---|
747 | |
---|
748 | irc->status |= USTATUS_LOGGED_IN; |
---|
749 | |
---|
750 | irc_send_login( irc ); |
---|
751 | |
---|
752 | irc->umode[0] = '\0'; |
---|
753 | irc_umode_set( irc, "+" UMODE, TRUE ); |
---|
754 | |
---|
755 | ic = irc->default_channel = irc_channel_new( irc, ROOT_CHAN ); |
---|
756 | irc_channel_set_topic( ic, CONTROL_TOPIC, irc->root ); |
---|
757 | set_setstr( &ic->set, "auto_join", "true" ); |
---|
758 | irc_channel_auto_joins( irc, NULL ); |
---|
759 | |
---|
760 | irc->root->last_channel = irc->default_channel; |
---|
761 | |
---|
762 | irc_usermsg( irc, |
---|
763 | "Welcome to the BitlBee gateway!\n\n" |
---|
764 | "If you've never used BitlBee before, please do read the help " |
---|
765 | "information using the \x02help\x02 command. Lots of FAQs are " |
---|
766 | "answered there.\n" |
---|
767 | "If you already have an account on this server, just use the " |
---|
768 | "\x02identify\x02 command to identify yourself." ); |
---|
769 | |
---|
770 | /* This is for bug #209 (use PASS to identify to NickServ). */ |
---|
771 | if( irc->password != NULL ) |
---|
772 | { |
---|
773 | char *send_cmd[] = { "identify", g_strdup( irc->password ), NULL }; |
---|
774 | |
---|
775 | irc_setpass( irc, NULL ); |
---|
776 | root_command( irc, send_cmd ); |
---|
777 | g_free( send_cmd[1] ); |
---|
778 | } |
---|
779 | |
---|
780 | return 1; |
---|
781 | } |
---|
782 | } |
---|
783 | else |
---|
784 | { |
---|
785 | /* More information needed. */ |
---|
786 | return 0; |
---|
787 | } |
---|
788 | } |
---|
789 | |
---|
790 | /* TODO: This is a mess, but this function is a bit too complicated to be |
---|
791 | converted to something more generic. */ |
---|
792 | void irc_umode_set( irc_t *irc, const char *s, gboolean allow_priv ) |
---|
793 | { |
---|
794 | /* allow_priv: Set to 0 if s contains user input, 1 if you want |
---|
795 | to set a "privileged" mode (+o, +R, etc). */ |
---|
796 | char m[128], st = 1; |
---|
797 | const char *t; |
---|
798 | int i; |
---|
799 | char changes[512], *p, st2 = 2; |
---|
800 | char badflag = 0; |
---|
801 | |
---|
802 | memset( m, 0, sizeof( m ) ); |
---|
803 | |
---|
804 | for( t = irc->umode; *t; t ++ ) |
---|
805 | if( *t < sizeof( m ) ) |
---|
806 | m[(int)*t] = 1; |
---|
807 | |
---|
808 | p = changes; |
---|
809 | for( t = s; *t; t ++ ) |
---|
810 | { |
---|
811 | if( *t == '+' || *t == '-' ) |
---|
812 | st = *t == '+'; |
---|
813 | else if( ( st == 0 && ( !strchr( UMODES_KEEP, *t ) || allow_priv ) ) || |
---|
814 | ( st == 1 && strchr( UMODES, *t ) ) || |
---|
815 | ( st == 1 && allow_priv && strchr( UMODES_PRIV, *t ) ) ) |
---|
816 | { |
---|
817 | if( m[(int)*t] != st) |
---|
818 | { |
---|
819 | if( st != st2 ) |
---|
820 | st2 = st, *p++ = st ? '+' : '-'; |
---|
821 | *p++ = *t; |
---|
822 | } |
---|
823 | m[(int)*t] = st; |
---|
824 | } |
---|
825 | else |
---|
826 | badflag = 1; |
---|
827 | } |
---|
828 | *p = '\0'; |
---|
829 | |
---|
830 | memset( irc->umode, 0, sizeof( irc->umode ) ); |
---|
831 | |
---|
832 | for( i = 'A'; i <= 'z' && strlen( irc->umode ) < ( sizeof( irc->umode ) - 1 ); i ++ ) |
---|
833 | if( m[i] ) |
---|
834 | irc->umode[strlen(irc->umode)] = i; |
---|
835 | |
---|
836 | if( badflag ) |
---|
837 | irc_send_num( irc, 501, ":Unknown MODE flag" ); |
---|
838 | if( *changes ) |
---|
839 | irc_write( irc, ":%s!%s@%s MODE %s :%s", irc->user->nick, |
---|
840 | irc->user->user, irc->user->host, irc->user->nick, |
---|
841 | changes ); |
---|
842 | } |
---|
843 | |
---|
844 | /* Returns 0 if everything seems to be okay, a number >0 when there was a |
---|
845 | timeout. The number returned is the number of seconds we received no |
---|
846 | pongs from the user. When not connected yet, we don't ping but drop the |
---|
847 | connection when the user fails to connect in IRC_LOGIN_TIMEOUT secs. */ |
---|
848 | static gboolean irc_userping( gpointer _irc, gint fd, b_input_condition cond ) |
---|
849 | { |
---|
850 | irc_t *irc = _irc; |
---|
851 | int rv = 0; |
---|
852 | |
---|
853 | if( !( irc->status & USTATUS_LOGGED_IN ) ) |
---|
854 | { |
---|
855 | if( gettime() > ( irc->last_pong + IRC_LOGIN_TIMEOUT ) ) |
---|
856 | rv = gettime() - irc->last_pong; |
---|
857 | } |
---|
858 | else |
---|
859 | { |
---|
860 | if( ( gettime() > ( irc->last_pong + global.conf->ping_interval ) ) && !irc->pinging ) |
---|
861 | { |
---|
862 | irc_write( irc, "PING :%s", IRC_PING_STRING ); |
---|
863 | irc->pinging = 1; |
---|
864 | } |
---|
865 | else if( gettime() > ( irc->last_pong + global.conf->ping_timeout ) ) |
---|
866 | { |
---|
867 | rv = gettime() - irc->last_pong; |
---|
868 | } |
---|
869 | } |
---|
870 | |
---|
871 | if( rv > 0 ) |
---|
872 | { |
---|
873 | irc_abort( irc, 0, "Ping Timeout: %d seconds", rv ); |
---|
874 | return FALSE; |
---|
875 | } |
---|
876 | |
---|
877 | return TRUE; |
---|
878 | } |
---|
879 | |
---|
880 | static char *set_eval_charset( set_t *set, char *value ) |
---|
881 | { |
---|
882 | irc_t *irc = (irc_t*) set->data; |
---|
883 | char *test; |
---|
884 | gsize test_bytes = 0; |
---|
885 | GIConv ic, oc; |
---|
886 | |
---|
887 | if( g_strcasecmp( value, "none" ) == 0 ) |
---|
888 | value = g_strdup( "utf-8" ); |
---|
889 | |
---|
890 | if( ( oc = g_iconv_open( value, "utf-8" ) ) == (GIConv) -1 ) |
---|
891 | { |
---|
892 | return NULL; |
---|
893 | } |
---|
894 | |
---|
895 | /* Do a test iconv to see if the user picked an IRC-compatible |
---|
896 | charset (for example utf-16 goes *horribly* wrong). */ |
---|
897 | if( ( test = g_convert_with_iconv( " ", 1, oc, NULL, &test_bytes, NULL ) ) == NULL || |
---|
898 | test_bytes > 1 ) |
---|
899 | { |
---|
900 | g_free( test ); |
---|
901 | g_iconv_close( oc ); |
---|
902 | irc_usermsg( irc, "Unsupported character set: The IRC protocol " |
---|
903 | "only supports 8-bit character sets." ); |
---|
904 | return NULL; |
---|
905 | } |
---|
906 | g_free( test ); |
---|
907 | |
---|
908 | if( ( ic = g_iconv_open( "utf-8", value ) ) == (GIConv) -1 ) |
---|
909 | { |
---|
910 | g_iconv_close( oc ); |
---|
911 | return NULL; |
---|
912 | } |
---|
913 | |
---|
914 | if( irc->iconv != (GIConv) -1 ) |
---|
915 | g_iconv_close( irc->iconv ); |
---|
916 | if( irc->oconv != (GIConv) -1 ) |
---|
917 | g_iconv_close( irc->oconv ); |
---|
918 | |
---|
919 | irc->iconv = ic; |
---|
920 | irc->oconv = oc; |
---|
921 | |
---|
922 | return value; |
---|
923 | } |
---|
924 | |
---|
925 | /* Mostly meant for upgrades. If one of these is set to the non-default, |
---|
926 | set show_users of all channels to something with the same effect. */ |
---|
927 | static char *set_eval_bw_compat( set_t *set, char *value ) |
---|
928 | { |
---|
929 | irc_t *irc = set->data; |
---|
930 | char *val; |
---|
931 | GSList *l; |
---|
932 | |
---|
933 | irc_usermsg( irc, "Setting `%s' is obsolete, use the `show_users' " |
---|
934 | "channel setting instead.", set->key ); |
---|
935 | |
---|
936 | if( strcmp( set->key, "away_devoice" ) == 0 && !bool2int( value ) ) |
---|
937 | val = "online,away"; |
---|
938 | else if( strcmp( set->key, "show_offline" ) == 0 && bool2int( value ) ) |
---|
939 | val = "online@,away+,offline"; |
---|
940 | else |
---|
941 | val = "online+,away"; |
---|
942 | |
---|
943 | for( l = irc->channels; l; l = l->next ) |
---|
944 | { |
---|
945 | irc_channel_t *ic = l->data; |
---|
946 | /* No need to check channel type, if the setting doesn't exist it |
---|
947 | will just be ignored. */ |
---|
948 | set_setstr( &ic->set, "show_users", val ); |
---|
949 | } |
---|
950 | |
---|
951 | return SET_INVALID; |
---|
952 | } |
---|
953 | |
---|
954 | void register_irc_plugin( const struct irc_plugin *p ) |
---|
955 | { |
---|
956 | irc_plugins = g_slist_prepend( irc_plugins, (gpointer) p ); |
---|
957 | } |
---|