source: irc.c @ 7db65b7

Last change on this file since 7db65b7 was 65016a6, checked in by Wilmer van der Gaast <wilmer@…>, at 2010-08-04T19:45:18Z

Set channel mode +C for control channels.

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