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