source: irc.c @ c608891

Last change on this file since c608891 was c608891, checked in by Wilmer van der Gaast <wilmer@…>, at 2013-04-23T16:20:06Z

Simple (and possibly still fragile) support for UTF-8 nicknames.

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