source: irc.c @ 0383943

Last change on this file since 0383943 was 0383943, checked in by Wilmer van der Gaast <wilmer@…>, at 2006-08-24T22:06:52Z

Added message on successful creation of accounts and fixed "set password"
command.

  • Property mode set to 100644
File size: 30.8 KB
RevLine 
[b7d3cc34]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 big hairy IRCd part of the project                               */
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#define BITLBEE_CORE
27#include "bitlbee.h"
28#include "crypting.h"
[2face62]29#include "ipc.h"
[b7d3cc34]30
[ba9edaa]31static gboolean irc_userping( gpointer _irc, int fd, b_input_condition cond );
[b7d3cc34]32
33GSList *irc_connection_list = NULL;
34
[0383943]35static char *passchange( set_t *set, char *value )
[c2295f7]36{
[0383943]37        irc_t *irc = set->data;
38       
[88086db]39        irc_setpass( irc, value );
40        irc_usermsg( irc, "Password successfully changed" );
41        return NULL;
[c2295f7]42}
43
[b7d3cc34]44irc_t *irc_new( int fd )
45{
[e4d6271]46        irc_t *irc;
47        struct hostent *peer;
48        unsigned int i;
49        char buf[128];
[b7d3cc34]50#ifdef IPV6
[b0a33a5]51        struct sockaddr_in6 sock6[1];
52        unsigned int i6;
[b7d3cc34]53#endif
[b0a33a5]54        struct sockaddr_in sock[1];
[e4d6271]55       
56        irc = g_new0( irc_t, 1 );
[b7d3cc34]57       
58        irc->fd = fd;
[a0d04d6]59        sock_make_nonblocking( irc->fd );
60       
[ba9edaa]61        irc->r_watch_source_id = b_input_add( irc->fd, GAIM_INPUT_READ, bitlbee_io_current_client_read, irc );
[b7d3cc34]62       
63        irc->status = USTATUS_OFFLINE;
64        irc->last_pong = gettime();
65       
66        irc->userhash = g_hash_table_new( g_str_hash, g_str_equal );
67        irc->watches = g_hash_table_new( g_str_hash, g_str_equal );
68       
69        strcpy( irc->umode, UMODE );
70        irc->mynick = g_strdup( ROOT_NICK );
71        irc->channel = g_strdup( ROOT_CHAN );
72       
73        i = sizeof( *sock );
[b0a33a5]74#ifdef IPV6
75        i6 = sizeof( *sock6 );
76#endif
[e4d6271]77       
[b7d3cc34]78        if( global.conf->hostname )
79                irc->myhost = g_strdup( global.conf->hostname );
[e4d6271]80#ifdef IPV6
[b0a33a5]81        else if( getsockname( irc->fd, (struct sockaddr*) sock6, &i6 ) == 0 && sock6->sin6_family == AF_INET6 )
[b7d3cc34]82        {
[b0a33a5]83                if( ( peer = gethostbyaddr( (char*) &sock6->sin6_addr, sizeof( sock6->sin6_addr ), AF_INET6 ) ) )
[b7d3cc34]84                        irc->myhost = g_strdup( peer->h_name );
[b0a33a5]85                else if( inet_ntop( AF_INET6, &sock6->sin6_addr, buf, sizeof( buf ) - 1 ) != NULL )
[2a6ca4f]86                        irc->myhost = g_strdup( ipv6_unwrap( buf ) );
[b7d3cc34]87        }
[b0a33a5]88#endif
89        else if( getsockname( irc->fd, (struct sockaddr*) sock, &i ) == 0 && sock->sin_family == AF_INET )
[b7d3cc34]90        {
[b0a33a5]91                if( ( peer = gethostbyaddr( (char*) &sock->sin_addr, sizeof( sock->sin_addr ), AF_INET ) ) )
[b7d3cc34]92                        irc->myhost = g_strdup( peer->h_name );
[b0a33a5]93                else if( inet_ntop( AF_INET, &sock->sin_addr, buf, sizeof( buf ) - 1 ) != NULL )
[e4d6271]94                        irc->myhost = g_strdup( buf );
[b7d3cc34]95        }
96       
97        i = sizeof( *sock );
98#ifdef IPV6
[b0a33a5]99        i6 = sizeof( *sock6 );
100        if( getpeername( irc->fd, (struct sockaddr*) sock6, &i6 ) == 0 && sock6->sin6_family == AF_INET6 )
[b7d3cc34]101        {
[b0a33a5]102                if( ( peer = gethostbyaddr( (char*) &sock6->sin6_addr, sizeof( sock6->sin6_addr ), AF_INET6 ) ) )
[b7d3cc34]103                        irc->host = g_strdup( peer->h_name );
[b0a33a5]104                else if( inet_ntop( AF_INET6, &sock6->sin6_addr, buf, sizeof( buf ) - 1 ) != NULL )
[2a6ca4f]105                        irc->host = g_strdup( ipv6_unwrap( buf ) );
[b7d3cc34]106        }
[b0a33a5]107        else
108#endif
109        if( getpeername( irc->fd, (struct sockaddr*) sock, &i ) == 0 && sock->sin_family == AF_INET )
[b7d3cc34]110        {
[b0a33a5]111                if( ( peer = gethostbyaddr( (char*) &sock->sin_addr, sizeof( sock->sin_addr ), AF_INET ) ) )
[b7d3cc34]112                        irc->host = g_strdup( peer->h_name );
[b0a33a5]113                else if( inet_ntop( AF_INET, &sock->sin_addr, buf, sizeof( buf ) - 1 ) != NULL )
[e4d6271]114                        irc->host = g_strdup( buf );
[b7d3cc34]115        }
116       
[2a6ca4f]117        /* Rare, but possible. */
[b7d3cc34]118        if( !irc->host ) irc->host = g_strdup( "localhost." );
119        if( !irc->myhost ) irc->myhost = g_strdup( "localhost." );
120
121        if( global.conf->ping_interval > 0 && global.conf->ping_timeout > 0 )
[ba9edaa]122                irc->ping_source_id = b_timeout_add( global.conf->ping_interval * 1000, irc_userping, irc );
[b7d3cc34]123       
124        irc_write( irc, ":%s NOTICE AUTH :%s", irc->myhost, "BitlBee-IRCd initialized, please go on" );
125
126        irc_connection_list = g_slist_append( irc_connection_list, irc );
127       
[5c9512f]128        set_add( &irc->set, "away_devoice", "true",  set_eval_away_devoice, irc );
129        set_add( &irc->set, "auto_connect", "true", set_eval_bool, irc );
130        set_add( &irc->set, "auto_reconnect", "false", set_eval_bool, irc );
131        set_add( &irc->set, "auto_reconnect_delay", "300", set_eval_int, irc );
132        set_add( &irc->set, "buddy_sendbuffer", "false", set_eval_bool, irc );
133        set_add( &irc->set, "buddy_sendbuffer_delay", "200", set_eval_int, irc );
134        set_add( &irc->set, "charset", "iso8859-1", set_eval_charset, irc );
135        set_add( &irc->set, "debug", "false", set_eval_bool, irc );
136        set_add( &irc->set, "default_target", "root", NULL, irc );
137        set_add( &irc->set, "display_namechanges", "false", set_eval_bool, irc );
138        set_add( &irc->set, "handle_unknown", "root", NULL, irc );
139        set_add( &irc->set, "lcnicks", "true", set_eval_bool, irc );
140        set_add( &irc->set, "ops", "both", set_eval_ops, irc );
141        set_add( &irc->set, "password", NULL, passchange, irc );
142        set_add( &irc->set, "private", "true", set_eval_bool, irc );
143        set_add( &irc->set, "query_order", "lifo", NULL, irc );
144        set_add( &irc->set, "save_on_quit", "true", set_eval_bool, irc );
145        set_add( &irc->set, "strip_html", "true", NULL, irc );
146        set_add( &irc->set, "to_char", ": ", set_eval_to_char, irc );
147        set_add( &irc->set, "typing_notice", "false", set_eval_bool, irc );
[b7d3cc34]148       
149        conf_loaddefaults( irc );
150       
151        return( irc );
152}
153
[f73b969]154/* immed=1 makes this function pretty much equal to irc_free(), except that
155   this one will "log". In case the connection is already broken and we
156   shouldn't try to write to it. */
[fc50d48]157void irc_abort( irc_t *irc, int immed, char *format, ... )
[c1826c6]158{
[fc50d48]159        if( format != NULL )
160        {
[f73b969]161                va_list params;
[fc50d48]162                char *reason;
163               
164                va_start( params, format );
[f73b969]165                reason = g_strdup_vprintf( format, params );
[fc50d48]166                va_end( params );
167               
168                if( !immed )
169                        irc_write( irc, "ERROR :Closing link: %s", reason );
170               
171                ipc_to_master_str( "OPERMSG :Client exiting: %s@%s [%s]\r\n",
[f73b969]172                                   irc->nick ? irc->nick : "(NONE)", irc->host, reason );
[fc50d48]173               
174                g_free( reason );
175        }
176        else
177        {
178                if( !immed )
179                        irc_write( irc, "ERROR :Closing link" );
180               
181                ipc_to_master_str( "OPERMSG :Client exiting: %s@%s [%s]\r\n",
[f73b969]182                                   irc->nick ? irc->nick : "(NONE)", irc->host, "No reason given" );
[fc50d48]183        }
184       
[79e826a]185        irc->status |= USTATUS_SHUTDOWN;
[fc50d48]186        if( irc->sendbuffer && !immed )
[c1826c6]187        {
[fc50d48]188                /* We won't read from this socket anymore. Instead, we'll connect a timer
189                   to it that should shut down the connection in a second, just in case
190                   bitlbee_.._write doesn't do it first. */
191               
[ba9edaa]192                b_event_remove( irc->r_watch_source_id );
193                irc->r_watch_source_id = b_timeout_add( 1000, (b_event_handler) irc_free, irc );
[c1826c6]194        }
195        else
196        {
197                irc_free( irc );
198        }
199}
200
[36fa9bd]201static gboolean irc_free_hashkey( gpointer key, gpointer value, gpointer data )
[b7d3cc34]202{
203        g_free( key );
204       
205        return( TRUE );
206}
207
208/* Because we have no garbage collection, this is quite annoying */
209void irc_free(irc_t * irc)
210{
[5b52a48]211        account_t *account;
[b7d3cc34]212        user_t *user, *usertmp;
213        help_t *helpnode, *helpnodetmp;
214       
215        log_message( LOGLVL_INFO, "Destroying connection with fd %d", irc->fd );
216       
[d5ccd83]217        if( irc->status & USTATUS_IDENTIFIED && set_getbool( &irc->set, "save_on_quit" ) ) 
[b73ac9c]218                if( storage_save( irc, TRUE ) != STORAGE_OK )
[b7d3cc34]219                        irc_usermsg( irc, "Error while saving settings!" );
220       
[bd9b00f]221        closesocket( irc->fd );
222       
[b7d3cc34]223        if( irc->ping_source_id > 0 )
[ba9edaa]224                b_event_remove( irc->ping_source_id );
225        b_event_remove( irc->r_watch_source_id );
[b7d3cc34]226        if( irc->w_watch_source_id > 0 )
[ba9edaa]227                b_event_remove( irc->w_watch_source_id );
[9c62a7c]228       
[b7d3cc34]229        irc_connection_list = g_slist_remove( irc_connection_list, irc );
230       
[55cc2be3]231        for (account = irc->accounts; account; account = account->next) {
[c99af3a]232                if (account->gc) {
233                        account->gc->wants_to_die = TRUE;
234                        signoff(account->gc);
235                } else if (account->reconnect) {
[6adcb6c6]236                        cancel_auto_reconnect(account);
[c99af3a]237                }
[55cc2be3]238        }
[b7d3cc34]239       
240        g_free(irc->sendbuffer);
241        g_free(irc->readbuffer);
242       
243        g_free(irc->nick);
244        g_free(irc->user);
245        g_free(irc->host);
246        g_free(irc->realname);
247        g_free(irc->password);
248       
249        g_free(irc->myhost);
250        g_free(irc->mynick);
251       
252        g_free(irc->channel);
253       
254        while (irc->queries != NULL)
255                query_del(irc, irc->queries);
256       
[5b52a48]257        while (irc->accounts)
258                account_del(irc, irc->accounts);
259       
260        while (irc->set)
261                set_del(&irc->set, irc->set->key);
[b7d3cc34]262       
263        if (irc->users != NULL) {
264                user = irc->users;
265                while (user != NULL) {
266                        g_free(user->nick);
267                        g_free(user->away);
268                        g_free(user->handle);
269                        if(user->user!=user->nick) g_free(user->user);
270                        if(user->host!=user->nick) g_free(user->host);
271                        if(user->realname!=user->nick) g_free(user->realname);
[ba9edaa]272                        b_event_remove(user->sendbuf_timer);
[b7d3cc34]273                                       
274                        usertmp = user;
275                        user = user->next;
276                        g_free(usertmp);
277                }
278        }
279       
[36fa9bd]280        g_hash_table_foreach_remove(irc->userhash, irc_free_hashkey, NULL);
[b7d3cc34]281        g_hash_table_destroy(irc->userhash);
282       
[36fa9bd]283        g_hash_table_foreach_remove(irc->watches, irc_free_hashkey, NULL);
[b7d3cc34]284        g_hash_table_destroy(irc->watches);
285       
286        if (irc->help != NULL) {
287                helpnode = irc->help;
288                while (helpnode != NULL) {
289                        g_free(helpnode->string);
290                       
291                        helpnodetmp = helpnode;
292                        helpnode = helpnode->next;
293                        g_free(helpnodetmp);
294                }
295        }
296        g_free(irc);
297       
[d25f6fc]298        if( global.conf->runmode == RUNMODE_INETD || global.conf->runmode == RUNMODE_FORKDAEMON )
[ba9edaa]299                b_main_quit();
[b7d3cc34]300}
301
[7cad7b4]302/* USE WITH CAUTION!
303   Sets pass without checking */
304void irc_setpass (irc_t *irc, const char *pass) 
305{
[6e1fed7]306        g_free (irc->password);
[7cad7b4]307       
308        if (pass) {
309                irc->password = g_strdup (pass);
310        } else {
311                irc->password = NULL;
312        }
313}
314
[f73b969]315void irc_process( irc_t *irc )
[b7d3cc34]316{
[e27661d]317        char **lines, *temp, **cmd, *cs;
[b7d3cc34]318        int i;
319
[de3e100]320        if( irc->readbuffer != NULL )
321        {
322                lines = irc_tokenize( irc->readbuffer );
323               
324                for( i = 0; *lines[i] != '\0'; i ++ )
325                {
[7d31002]326                        char conv[IRC_MAX_LINE+1];
327                       
[e27661d]328                        /* [WvG] Because irc_tokenize splits at every newline, the lines[] list
329                            should end with an empty string. This is why this actually works.
330                            Took me a while to figure out, Maurits. :-P */
[de3e100]331                        if( lines[i+1] == NULL )
332                        {
[b7d3cc34]333                                temp = g_strdup( lines[i] );
334                                g_free( irc->readbuffer );
335                                irc->readbuffer = temp;
[de3e100]336                                i ++;
[b7d3cc34]337                                break;
[e27661d]338                        }
339                       
[5c9512f]340                        if( ( cs = set_getstr( &irc->set, "charset" ) ) && ( g_strcasecmp( cs, "utf-8" ) != 0 ) )
[e27661d]341                        {
342                                conv[IRC_MAX_LINE] = 0;
343                                if( do_iconv( cs, "UTF-8", lines[i], conv, 0, IRC_MAX_LINE - 2 ) != -1 )
[7d31002]344                                        lines[i] = conv;
[e27661d]345                        }
[de3e100]346                       
[0431ea1]347                        if( ( cmd = irc_parse_line( lines[i] ) ) == NULL )
[de3e100]348                                continue;
[f73b969]349                        irc_exec( irc, cmd );
350                       
351                        g_free( cmd );
352                       
353                        /* Shouldn't really happen, but just in case... */
354                        if( !g_slist_find( irc_connection_list, irc ) )
[de3e100]355                        {
[b7d3cc34]356                                g_free( lines );
[f73b969]357                                return;
[b7d3cc34]358                        }
359                }
[de3e100]360               
361                if( lines[i] != NULL )
362                {
363                        g_free( irc->readbuffer );
[0431ea1]364                        irc->readbuffer = NULL;
[b7d3cc34]365                }
[de3e100]366               
[b7d3cc34]367                g_free( lines );
368        }
369}
370
[e27661d]371/* Splits a long string into separate lines. The array is NULL-terminated and, unless the string
372   contains an incomplete line at the end, ends with an empty string. */
[b7d3cc34]373char **irc_tokenize( char *buffer )
374{
375        int i, j;
376        char **lines;
377
378        /* Count the number of elements we're gonna need. */
[de3e100]379        for( i = 0, j = 1; buffer[i] != '\0'; i ++ )
380        {
381                if( buffer[i] == '\n' )
382                        if( buffer[i+1] != '\r' && buffer[i+1] != '\n' )
383                                j ++;
[b7d3cc34]384        }
385       
386        /* Allocate j+1 elements. */
[de3e100]387        lines = g_new( char *, j + 1 );
[b7d3cc34]388       
389        /* NULL terminate our list. */ 
[de3e100]390        lines[j] = NULL;
[b7d3cc34]391       
[de3e100]392        lines[0] = buffer;
[b7d3cc34]393       
394        /* Split the buffer in several strings, using \r\n as our seperator, where \r is optional.
395         * Although this is not in the RFC, some braindead ircds (newnet's) use this, so some clients might too.
396         */
[de3e100]397        for( i = 0, j = 0; buffer[i] != '\0'; i ++)
398        {
399                if( buffer[i] == '\n' )
400                {
401                        buffer[i] = '\0';
402                       
403                        if( i > 0 && buffer[i-1] == '\r' )
404                                buffer[i-1] = '\0';
405                        if( buffer[i+1] != '\r' && buffer[i+1] != '\n' )
406                                lines[++j] = buffer + i + 1;
[b7d3cc34]407                }
408        }
[de3e100]409       
410        return( lines );
[b7d3cc34]411}
412
[e27661d]413/* Split an IRC-style line into little parts/arguments. */
[0431ea1]414char **irc_parse_line( char *line )
[b7d3cc34]415{
416        int i, j;
417        char **cmd;
418       
419        /* Move the line pointer to the start of the command, skipping spaces and the optional prefix. */
[de3e100]420        if( line[0] == ':' )
421        {
422                for( i = 0; line[i] != ' '; i ++ );
423                line = line + i;
[b7d3cc34]424        }
[de3e100]425        for( i = 0; line[i] == ' '; i ++ );
426        line = line + i;
427       
[b7d3cc34]428        /* If we're already at the end of the line, return. If not, we're going to need at least one element. */
[de3e100]429        if( line[0] == '\0')
430                return NULL;
431       
432        /* Count the number of char **cmd elements we're going to need. */
433        j = 1;
434        for( i = 0; line[i] != '\0'; i ++ )
435        {
436                if( line[i] == ' ' )
437                {
438                        j ++;
[b7d3cc34]439                       
[de3e100]440                        if( line[i+1] == ':' )
441                                break;
442                }
[b7d3cc34]443        }       
444
445        /* Allocate the space we need. */
[de3e100]446        cmd = g_new( char *, j + 1 );
447        cmd[j] = NULL;
[b7d3cc34]448       
449        /* Do the actual line splitting, format is:
450         * Input: "PRIVMSG #bitlbee :foo bar"
451         * Output: cmd[0]=="PRIVMSG", cmd[1]=="#bitlbee", cmd[2]=="foo bar", cmd[3]==NULL
452         */
453
[de3e100]454        cmd[0] = line;
455        for( i = 0, j = 0; line[i] != '\0'; i ++ )
[b7d3cc34]456        {
[de3e100]457                if( line[i] == ' ' )
[b7d3cc34]458                {
[de3e100]459                        line[i] = '\0';
460                        cmd[++j] = line + i + 1;
[b7d3cc34]461                       
[de3e100]462                        if( line[i+1] == ':' )
[b7d3cc34]463                        {
[de3e100]464                                cmd[j] ++;
[b7d3cc34]465                                break;
466                        }
467                }
468        }
469       
[de3e100]470        return cmd;
[b7d3cc34]471}
472
[e27661d]473/* Converts such an array back into a command string. Mainly used for the IPC code right now. */
[74c119d]474char *irc_build_line( char **cmd )
475{
476        int i, len;
477        char *s;
[b7d3cc34]478       
[74c119d]479        if( cmd[0] == NULL )
480                return NULL;
[b7d3cc34]481       
[74c119d]482        len = 1;
483        for( i = 0; cmd[i]; i ++ )
484                len += strlen( cmd[i] ) + 1;
485       
486        if( strchr( cmd[i-1], ' ' ) != NULL )
487                len ++;
488       
489        s = g_new0( char, len + 1 );
490        for( i = 0; cmd[i]; i ++ )
[b7d3cc34]491        {
[74c119d]492                if( cmd[i+1] == NULL && strchr( cmd[i], ' ' ) != NULL )
493                        strcat( s, ":" );
[b7d3cc34]494               
[74c119d]495                strcat( s, cmd[i] );
[b7d3cc34]496               
[74c119d]497                if( cmd[i+1] )
498                        strcat( s, " " );
[b7d3cc34]499        }
[74c119d]500        strcat( s, "\r\n" );
[b7d3cc34]501       
[74c119d]502        return s;
[b7d3cc34]503}
504
505void irc_reply( irc_t *irc, int code, char *format, ... )
506{
507        char text[IRC_MAX_LINE];
508        va_list params;
509       
510        va_start( params, format );
511        g_vsnprintf( text, IRC_MAX_LINE, format, params );
512        va_end( params );
513        irc_write( irc, ":%s %03d %s %s", irc->myhost, code, irc->nick?irc->nick:"*", text );
514       
515        return;
516}
517
518int irc_usermsg( irc_t *irc, char *format, ... )
519{
520        char text[1024];
521        va_list params;
522        char is_private = 0;
523        user_t *u;
524       
525        u = user_find( irc, irc->mynick );
[dd89a55]526        is_private = u->is_private;
[b7d3cc34]527       
528        va_start( params, format );
529        g_vsnprintf( text, sizeof( text ), format, params );
530        va_end( params );
531       
532        return( irc_msgfrom( irc, u->nick, text ) );
533}
534
535void irc_write( irc_t *irc, char *format, ... ) 
536{
537        va_list params;
538
539        va_start( params, format );
540        irc_vawrite( irc, format, params );     
541        va_end( params );
542
543        return;
544
545}
546
547void irc_vawrite( irc_t *irc, char *format, va_list params )
548{
549        int size;
[d783e48]550        char line[IRC_MAX_LINE+1], *cs;
551               
[0356ae3]552        /* Don't try to write anything new anymore when shutting down. */
[5898ef8]553        if( irc->status & USTATUS_SHUTDOWN )
[b7d3cc34]554                return;
[d783e48]555       
556        line[IRC_MAX_LINE] = 0;
557        g_vsnprintf( line, IRC_MAX_LINE - 2, format, params );
558       
[b7d3cc34]559        strip_newlines( line );
[5c9512f]560        if( ( cs = set_getstr( &irc->set, "charset" ) ) && ( g_strcasecmp( cs, "utf-8" ) != 0 ) )
[d783e48]561        {
562                char conv[IRC_MAX_LINE+1];
563               
564                conv[IRC_MAX_LINE] = 0;
565                if( do_iconv( "UTF-8", cs, line, conv, 0, IRC_MAX_LINE - 2 ) != -1 )
566                        strcpy( line, conv );
567        }
[b7d3cc34]568        strcat( line, "\r\n" );
[d783e48]569       
[a0d04d6]570        if( irc->sendbuffer != NULL )
571        {
[b7d3cc34]572                size = strlen( irc->sendbuffer ) + strlen( line );
573                irc->sendbuffer = g_renew ( char, irc->sendbuffer, size + 1 );
574                strcpy( ( irc->sendbuffer + strlen( irc->sendbuffer ) ), line );
575        }
[a0d04d6]576        else
[b7d3cc34]577        {
[a0d04d6]578                irc->sendbuffer = g_strdup(line);
[b7d3cc34]579        }
580       
[a0d04d6]581        if( irc->w_watch_source_id == 0 )
[0356ae3]582        {
583                /* If the buffer is empty we can probably write, so call the write event handler
584                   immediately. If it returns TRUE, it should be called again, so add the event to
585                   the queue. If it's FALSE, we emptied the buffer and saved ourselves some work
586                   in the event queue. */
587                if( bitlbee_io_current_client_write( irc, irc->fd, GAIM_INPUT_WRITE ) )
588                        irc->w_watch_source_id = b_input_add( irc->fd, GAIM_INPUT_WRITE, bitlbee_io_current_client_write, irc );
589        }
[a0d04d6]590       
[b7d3cc34]591        return;
592}
593
[22d41a2]594void irc_write_all( int now, char *format, ... )
[b7d3cc34]595{
596        va_list params;
597        GSList *temp;   
[22d41a2]598       
[b7d3cc34]599        va_start( params, format );
[22d41a2]600       
[b7d3cc34]601        temp = irc_connection_list;
[22d41a2]602        while( temp != NULL )
603        {
604                irc_t *irc = temp->data;
605               
606                if( now )
607                {
608                        g_free( irc->sendbuffer );
609                        irc->sendbuffer = g_strdup( "\r\n" );
610                }
[b7d3cc34]611                irc_vawrite( temp->data, format, params );
[22d41a2]612                if( now )
613                {
[a0d04d6]614                        bitlbee_io_current_client_write( irc, irc->fd, GAIM_INPUT_WRITE );
[22d41a2]615                }
[b7d3cc34]616                temp = temp->next;
617        }
[22d41a2]618       
[b7d3cc34]619        va_end( params );
620        return;
621} 
622
623void irc_names( irc_t *irc, char *channel )
624{
[3f9440d]625        user_t *u;
626        char namelist[385] = "";
[b7d3cc34]627        struct conversation *c = NULL;
[04026d4]628        char *ops = set_getstr( &irc->set, "ops" );
[b7d3cc34]629       
[2f13222]630        /* RFCs say there is no error reply allowed on NAMES, so when the
[b7d3cc34]631           channel is invalid, just give an empty reply. */
632       
[3f9440d]633        if( g_strcasecmp( channel, irc->channel ) == 0 )
[b7d3cc34]634        {
[3f9440d]635                for( u = irc->users; u; u = u->next ) if( u->online )
[b7d3cc34]636                {
[3f9440d]637                        if( strlen( namelist ) + strlen( u->nick ) > sizeof( namelist ) - 4 )
[b7d3cc34]638                        {
[3f9440d]639                                irc_reply( irc, 353, "= %s :%s", channel, namelist );
640                                *namelist = 0;
[b7d3cc34]641                        }
[3f9440d]642                       
[fef6116]643                        if( u->gc && !u->away && set_getbool( &irc->set, "away_devoice" ) )
[3f9440d]644                                strcat( namelist, "+" );
[a93e3c8]645                        else if( ( strcmp( u->nick, irc->mynick ) == 0 && ( strcmp( ops, "root" ) == 0 || strcmp( ops, "both" ) == 0 ) ) ||
646                                 ( strcmp( u->nick, irc->nick ) == 0 && ( strcmp( ops, "user" ) == 0 || strcmp( ops, "both" ) == 0 ) ) )
647                                strcat( namelist, "@" );
[3f9440d]648                       
649                        strcat( namelist, u->nick );
650                        strcat( namelist, " " );
[b7d3cc34]651                }
652        }
[3f9440d]653        else if( ( c = conv_findchannel( channel ) ) )
[b7d3cc34]654        {
655                GList *l;
[3f9440d]656               
657                /* root and the user aren't in the channel userlist but should
658                   show up in /NAMES, so list them first: */
659                sprintf( namelist, "%s%s %s%s ", strcmp( ops, "root" ) == 0 || strcmp( ops, "both" ) ? "@" : "", irc->mynick,
660                                                 strcmp( ops, "user" ) == 0 || strcmp( ops, "both" ) ? "@" : "", irc->nick );
[b7d3cc34]661               
[3f9440d]662                for( l = c->in_room; l; l = l->next ) if( ( u = user_findhandle( c->gc, l->data ) ) )
663                {
664                        if( strlen( namelist ) + strlen( u->nick ) > sizeof( namelist ) - 4 )
665                        {
666                                irc_reply( irc, 353, "= %s :%s", channel, namelist );
667                                *namelist = 0;
668                        }
669                       
670                        strcat( namelist, u->nick );
671                        strcat( namelist, " " );
672                }
[b7d3cc34]673        }
674       
[3f9440d]675        if( *namelist )
676                irc_reply( irc, 353, "= %s :%s", channel, namelist );
677       
[b7d3cc34]678        irc_reply( irc, 366, "%s :End of /NAMES list", channel );
679}
680
[edf9657]681int irc_check_login( irc_t *irc )
[b7d3cc34]682{
[edf9657]683        if( irc->user && irc->nick )
684        {
[3af70b0]685                if( global.conf->authmode == AUTHMODE_CLOSED && !( irc->status & USTATUS_AUTHORIZED ) )
[b7d3cc34]686                {
[edf9657]687                        irc_reply( irc, 464, ":This server is password-protected." );
688                        return 0;
[b7d3cc34]689                }
[edf9657]690                else
[b7d3cc34]691                {
[edf9657]692                        irc_login( irc );
693                        return 1;
[b7d3cc34]694                }
[edf9657]695        }
696        else
697        {
698                /* More information needed. */
699                return 0;
700        }
[b7d3cc34]701}
702
703void irc_login( irc_t *irc )
704{
705        user_t *u;
706       
707        irc_reply( irc,   1, ":Welcome to the BitlBee gateway, %s", irc->nick );
708        irc_reply( irc,   2, ":Host %s is running BitlBee " BITLBEE_VERSION " " ARCH "/" CPU ".", irc->myhost );
709        irc_reply( irc,   3, ":%s", IRCD_INFO );
[238f828]710        irc_reply( irc,   4, "%s %s %s %s", irc->myhost, BITLBEE_VERSION, UMODES UMODES_PRIV, CMODES );
[578d627]711        irc_reply( irc,   5, "PREFIX=(ov)@+ CHANTYPES=#& CHANMODES=,,,%s NICKLEN=%d NETWORK=BitlBee CASEMAPPING=rfc1459 MAXTARGETS=1 WATCH=128 :are supported by this server", CMODES, MAX_NICK_LENGTH - 1 );
[b7d3cc34]712        irc_motd( irc );
[2f13222]713        irc->umode[0] = '\0';
[238f828]714        irc_umode_set( irc, "+" UMODE, 1 );
[b7d3cc34]715
716        u = user_add( irc, irc->mynick );
717        u->host = g_strdup( irc->myhost );
718        u->realname = g_strdup( ROOT_FN );
719        u->online = 1;
720        u->send_handler = root_command_string;
721        u->is_private = 0; /* [SH] The channel is root's personal playground. */
722        irc_spawn( irc, u );
723       
724        u = user_add( irc, NS_NICK );
725        u->host = g_strdup( irc->myhost );
726        u->realname = g_strdup( ROOT_FN );
727        u->online = 0;
728        u->send_handler = root_command_string;
729        u->is_private = 1; /* [SH] NickServ is not in the channel, so should always /query. */
730       
731        u = user_add( irc, irc->nick );
732        u->user = g_strdup( irc->user );
733        u->host = g_strdup( irc->host );
734        u->realname = g_strdup( irc->realname );
735        u->online = 1;
736        irc_spawn( irc, u );
737       
[5c09a59]738        irc_usermsg( irc, "Welcome to the BitlBee gateway!\n\nIf you've never used BitlBee before, please do read the help information using the \x02help\x02 command. Lots of FAQ's are answered there." );
[b7d3cc34]739       
[bd9b00f]740        if( global.conf->runmode == RUNMODE_FORKDAEMON || global.conf->runmode == RUNMODE_DAEMON )
[2face62]741                ipc_to_master_str( "CLIENT %s %s :%s\r\n", irc->host, irc->nick, irc->realname );
742       
[79e826a]743        irc->status |= USTATUS_LOGGED_IN;
[b7d3cc34]744}
745
746void irc_motd( irc_t *irc )
747{
748        int fd;
749       
750        fd = open( global.conf->motdfile, O_RDONLY );
751        if( fd == -1 )
752        {
753                irc_reply( irc, 422, ":We don't need MOTDs." );
754        }
755        else
756        {
757                char linebuf[80];       /* Max. line length for MOTD's is 79 chars. It's what most IRC networks seem to do. */
758                char *add, max;
759                int len;
760               
761                linebuf[79] = len = 0;
762                max = sizeof( linebuf ) - 1;
763               
764                irc_reply( irc, 375, ":- %s Message Of The Day - ", irc->myhost );
765                while( read( fd, linebuf + len, 1 ) == 1 )
766                {
767                        if( linebuf[len] == '\n' || len == max )
768                        {
769                                linebuf[len] = 0;
770                                irc_reply( irc, 372, ":- %s", linebuf );
771                                len = 0;
772                        }
773                        else if( linebuf[len] == '%' )
774                        {
775                                read( fd, linebuf + len, 1 );
776                                if( linebuf[len] == 'h' )
777                                        add = irc->myhost;
778                                else if( linebuf[len] == 'v' )
779                                        add = BITLBEE_VERSION;
780                                else if( linebuf[len] == 'n' )
781                                        add = irc->nick;
782                                else
783                                        add = "%";
784                               
785                                strncpy( linebuf + len, add, max - len );
786                                while( linebuf[++len] );
787                        }
788                        else if( len < max )
789                        {
790                                len ++;
791                        }
792                }
793                irc_reply( irc, 376, ":End of MOTD" );
[d990997]794                close( fd );
[b7d3cc34]795        }
796}
797
798void irc_topic( irc_t *irc, char *channel )
799{
800        if( g_strcasecmp( channel, irc->channel ) == 0 )
801        {
802                irc_reply( irc, 332, "%s :%s", channel, CONTROL_TOPIC );
803        }
804        else
805        {
806                struct conversation *c = conv_findchannel( channel );
807               
808                if( c )
809                        irc_reply( irc, 332, "%s :BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", channel, c->title );
810                else
[fc630f9]811                        irc_reply( irc, 331, "%s :No topic for this channel", channel );
[b7d3cc34]812        }
813}
814
[238f828]815void irc_umode_set( irc_t *irc, char *s, int allow_priv )
[b7d3cc34]816{
[238f828]817        /* allow_priv: Set to 0 if s contains user input, 1 if you want
818           to set a "privileged" mode (+o, +R, etc). */
[b7d3cc34]819        char m[256], st = 1, *t;
820        int i;
[2f13222]821        char changes[512], *p, st2 = 2;
822        char badflag = 0;
[b7d3cc34]823       
824        memset( m, 0, sizeof( m ) );
825       
826        for( t = irc->umode; *t; t ++ )
827                m[(int)*t] = 1;
[2f13222]828
829        p = changes;
[b7d3cc34]830        for( t = s; *t; t ++ )
831        {
832                if( *t == '+' || *t == '-' )
833                        st = *t == '+';
[238f828]834                else if( st == 0 || ( strchr( UMODES, *t ) || ( allow_priv && strchr( UMODES_PRIV, *t ) ) ) )
[2f13222]835                {
836                        if( m[(int)*t] != st)
837                        {
838                                if( st != st2 )
839                                        st2 = st, *p++ = st ? '+' : '-';
840                                *p++ = *t;
841                        }
[b7d3cc34]842                        m[(int)*t] = st;
[2f13222]843                }
844                else
845                        badflag = 1;
[b7d3cc34]846        }
[2f13222]847        *p = '\0';
[b7d3cc34]848       
849        memset( irc->umode, 0, sizeof( irc->umode ) );
850       
851        for( i = 0; i < 256 && strlen( irc->umode ) < ( sizeof( irc->umode ) - 1 ); i ++ )
[238f828]852                if( m[i] )
[b7d3cc34]853                        irc->umode[strlen(irc->umode)] = i;
854       
[2f13222]855        if( badflag )
856                irc_reply( irc, 501, ":Unknown MODE flag" );
857        /* Deliberately no !user@host on the prefix here */
858        if( *changes )
859                irc_write( irc, ":%s MODE %s %s", irc->nick, irc->nick, changes );
[b7d3cc34]860}
861
862void irc_spawn( irc_t *irc, user_t *u )
863{
864        irc_join( irc, u, irc->channel );
865}
866
867void irc_join( irc_t *irc, user_t *u, char *channel )
868{
869        char *nick;
870       
871        if( ( g_strcasecmp( channel, irc->channel ) != 0 ) || user_find( irc, irc->nick ) )
872                irc_write( irc, ":%s!%s@%s JOIN :%s", u->nick, u->user, u->host, channel );
873       
874        if( nick_cmp( u->nick, irc->nick ) == 0 )
875        {
876                irc_write( irc, ":%s MODE %s +%s", irc->myhost, channel, CMODE );
877                irc_names( irc, channel );
878                irc_topic( irc, channel );
879        }
880       
881        nick = g_strdup( u->nick );
882        nick_lc( nick );
883        if( g_hash_table_lookup( irc->watches, nick ) )
884        {
[fc630f9]885                irc_reply( irc, 600, "%s %s %s %d :%s", u->nick, u->user, u->host, (int) time( NULL ), "logged online" );
[b7d3cc34]886        }
887        g_free( nick );
888}
889
890void irc_part( irc_t *irc, user_t *u, char *channel )
891{
892        irc_write( irc, ":%s!%s@%s PART %s :%s", u->nick, u->user, u->host, channel, "" );
893}
894
895void irc_kick( irc_t *irc, user_t *u, char *channel, user_t *kicker )
896{
897        irc_write( irc, ":%s!%s@%s KICK %s %s :%s", kicker->nick, kicker->user, kicker->host, channel, u->nick, "" );
898}
899
900void irc_kill( irc_t *irc, user_t *u )
901{
[fb62f81f]902        char *nick, *s;
[0a3c243]903        char reason[128];
[fb62f81f]904       
905        if( u->gc && u->gc->flags & OPT_LOGGING_OUT )
906        {
[0a3c243]907                if( u->gc->acc->server )
[fb62f81f]908                        g_snprintf( reason, sizeof( reason ), "%s %s", irc->myhost,
[0a3c243]909                                    u->gc->acc->server );
[fb62f81f]910                else if( ( s = strchr( u->gc->username, '@' ) ) )
911                        g_snprintf( reason, sizeof( reason ), "%s %s", irc->myhost,
912                                    s + 1 );
913                else
914                        g_snprintf( reason, sizeof( reason ), "%s %s.%s", irc->myhost,
[0a3c243]915                                    u->gc->acc->prpl->name, irc->myhost );
[fb62f81f]916               
917                /* proto_opt might contain garbage after the : */
918                if( ( s = strchr( reason, ':' ) ) )
919                        *s = 0;
920        }
921        else
922        {
923                strcpy( reason, "Leaving..." );
924        }
[b7d3cc34]925       
[fb62f81f]926        irc_write( irc, ":%s!%s@%s QUIT :%s", u->nick, u->user, u->host, reason );
[b7d3cc34]927       
928        nick = g_strdup( u->nick );
929        nick_lc( nick );
930        if( g_hash_table_lookup( irc->watches, nick ) )
931        {
[fc630f9]932                irc_reply( irc, 601, "%s %s %s %d :%s", u->nick, u->user, u->host, (int) time( NULL ), "logged offline" );
[b7d3cc34]933        }
934        g_free( nick );
935}
936
937int irc_send( irc_t *irc, char *nick, char *s, int flags )
938{
939        struct conversation *c = NULL;
940        user_t *u = NULL;
941       
[94281ef]942        if( *nick == '#' || *nick == '&' )
[b7d3cc34]943        {
944                if( !( c = conv_findchannel( nick ) ) )
945                {
946                        irc_reply( irc, 403, "%s :Channel does not exist", nick );
947                        return( 0 );
948                }
949        }
950        else
951        {
952                u = user_find( irc, nick );
953               
954                if( !u )
955                {
956                        if( irc->is_private )
957                                irc_reply( irc, 401, "%s :Nick does not exist", nick );
958                        else
959                                irc_usermsg( irc, "Nick `%s' does not exist!", nick );
960                        return( 0 );
961                }
962        }
963       
964        if( *s == 1 && s[strlen(s)-1] == 1 )
965        {
966                if( g_strncasecmp( s + 1, "ACTION", 6 ) == 0 )
967                {
968                        if( s[7] == ' ' ) s ++;
969                        s += 3;
970                        *(s++) = '/';
971                        *(s++) = 'm';
972                        *(s++) = 'e';
973                        *(s++) = ' ';
974                        s -= 4;
975                        s[strlen(s)-1] = 0;
976                }
977                else if( g_strncasecmp( s + 1, "VERSION", 7 ) == 0 )
978                {
979                        u = user_find( irc, irc->mynick );
980                        irc_privmsg( irc, u, "NOTICE", irc->nick, "", "\001VERSION BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\001" );
981                        return( 1 );
982                }
983                else if( g_strncasecmp( s + 1, "PING", 4 ) == 0 )
984                {
985                        u = user_find( irc, irc->mynick );
986                        irc_privmsg( irc, u, "NOTICE", irc->nick, "", s );
987                        return( 1 );
988                }
989                else if( g_strncasecmp( s + 1, "TYPING", 6 ) == 0 )
990                {
[0a3c243]991                        if( u && u->gc && u->gc->acc->prpl->send_typing && strlen( s ) >= 10 )
[b7d3cc34]992                        {
993                                time_t current_typing_notice = time( NULL );
994                               
995                                if( current_typing_notice - u->last_typing_notice >= 5 )
996                                {
[0a3c243]997                                        u->gc->acc->prpl->send_typing( u->gc, u->handle, s[8] == '1' );
[b7d3cc34]998                                        u->last_typing_notice = current_typing_notice;
999                                }
1000                        }
1001                        return( 1 );
1002                }
1003                else
1004                {
1005                        irc_usermsg( irc, "Non-ACTION CTCP's aren't supported" );
1006                        return( 0 );
1007                }
1008        }
1009       
1010        if( u )
1011        {
1012                /* For the next message, we probably do have to send new notices... */
1013                u->last_typing_notice = 0;
1014                u->is_private = irc->is_private;
1015               
1016                if( u->is_private )
1017                {
1018                        if( !u->online )
1019                                irc_reply( irc, 301, "%s :%s", u->nick, "User is offline" );
1020                        else if( u->away )
1021                                irc_reply( irc, 301, "%s :%s", u->nick, u->away );
1022                }
1023               
1024                if( u->send_handler )
[f73b969]1025                {
1026                        u->send_handler( irc, u, s, flags );
1027                        return 1;
1028                }
[b7d3cc34]1029        }
[0a3c243]1030        else if( c && c->gc && c->gc->acc && c->gc->acc->prpl )
[b7d3cc34]1031        {
[226fce1]1032                return( bim_chat_msg( c->gc, c->id, s ) );
[b7d3cc34]1033        }
1034       
1035        return( 0 );
1036}
1037
[ba9edaa]1038static gboolean buddy_send_handler_delayed( gpointer data, gint fd, b_input_condition cond )
[b7d3cc34]1039{
1040        user_t *u = data;
1041       
[226fce1]1042        /* Shouldn't happen, but just to be sure. */
1043        if( u->sendbuf_len < 2 )
1044                return FALSE;
1045       
[b7d3cc34]1046        u->sendbuf[u->sendbuf_len-2] = 0; /* Cut off the last newline */
[226fce1]1047        bim_buddy_msg( u->gc, u->handle, u->sendbuf, u->sendbuf_flags );
[b7d3cc34]1048       
1049        g_free( u->sendbuf );
1050        u->sendbuf = NULL;
1051        u->sendbuf_len = 0;
1052        u->sendbuf_timer = 0;
1053        u->sendbuf_flags = 0;
1054       
[ba9edaa]1055        return FALSE;
[b7d3cc34]1056}
1057
[f73b969]1058void buddy_send_handler( irc_t *irc, user_t *u, char *msg, int flags )
[b7d3cc34]1059{
[f73b969]1060        if( !u || !u->gc ) return;
[b7d3cc34]1061       
[d5ccd83]1062        if( set_getbool( &irc->set, "buddy_sendbuffer" ) && set_getint( &irc->set, "buddy_sendbuffer_delay" ) > 0 )
[b7d3cc34]1063        {
[834ff44]1064                int delay;
1065               
[b7d3cc34]1066                if( u->sendbuf_len > 0 && u->sendbuf_flags != flags)
1067                {
[226fce1]1068                        /* Flush the buffer */
[ba9edaa]1069                        b_event_remove( u->sendbuf_timer );
1070                        buddy_send_handler_delayed( u, -1, 0 );
[b7d3cc34]1071                }
1072
1073                if( u->sendbuf_len == 0 )
1074                {
1075                        u->sendbuf_len = strlen( msg ) + 2;
[226fce1]1076                        u->sendbuf = g_new( char, u->sendbuf_len );
[b7d3cc34]1077                        u->sendbuf[0] = 0;
1078                        u->sendbuf_flags = flags;
1079                }
1080                else
1081                {
1082                        u->sendbuf_len += strlen( msg ) + 1;
[226fce1]1083                        u->sendbuf = g_renew( char, u->sendbuf, u->sendbuf_len );
[b7d3cc34]1084                }
1085               
1086                strcat( u->sendbuf, msg );
1087                strcat( u->sendbuf, "\n" );
1088               
[5c9512f]1089                delay = set_getint( &irc->set, "buddy_sendbuffer_delay" );
[834ff44]1090                if( delay <= 5 )
1091                        delay *= 1000;
1092               
[b7d3cc34]1093                if( u->sendbuf_timer > 0 )
[ba9edaa]1094                        b_event_remove( u->sendbuf_timer );
1095                u->sendbuf_timer = b_timeout_add( delay, buddy_send_handler_delayed, u );
[b7d3cc34]1096        }
1097        else
1098        {
[226fce1]1099                bim_buddy_msg( u->gc, u->handle, msg, flags );
[b7d3cc34]1100        }
1101}
1102
1103int irc_privmsg( irc_t *irc, user_t *u, char *type, char *to, char *prefix, char *msg )
1104{
1105        char last = 0;
1106        char *s = msg, *line = msg;
1107       
1108        /* The almighty linesplitter .. woohoo!! */
1109        while( !last )
1110        {
1111                if( *s == '\r' && *(s+1) == '\n' )
1112                        *(s++) = 0;
1113                if( *s == '\n' )
1114                {
1115                        last = s[1] == 0;
1116                        *s = 0;
1117                }
1118                else
1119                {
1120                        last = s[0] == 0;
1121                }
1122                if( *s == 0 )
1123                {
1124                        if( g_strncasecmp( line, "/me ", 4 ) == 0 && ( !prefix || !*prefix ) && g_strcasecmp( type, "PRIVMSG" ) == 0 )
1125                        {
1126                                irc_write( irc, ":%s!%s@%s %s %s :\001ACTION %s\001", u->nick, u->user, u->host,
1127                                           type, to, line + 4 );
1128                        }
1129                        else
1130                        {
1131                                irc_write( irc, ":%s!%s@%s %s %s :%s%s", u->nick, u->user, u->host,
[25d1be7]1132                                           type, to, prefix ? prefix : "", line );
[b7d3cc34]1133                        }
1134                        line = s + 1;
1135                }
1136                s ++;
1137        }
1138       
1139        return( 1 );
1140}
1141
1142int irc_msgfrom( irc_t *irc, char *nick, char *msg )
1143{
1144        user_t *u = user_find( irc, nick );
1145        static char *prefix = NULL;
1146       
1147        if( !u ) return( 0 );
1148        if( prefix && *prefix ) g_free( prefix );
1149       
1150        if( !u->is_private && nick_cmp( u->nick, irc->mynick ) != 0 )
1151        {
1152                int len = strlen( irc->nick) + 3;
1153                prefix = g_new (char, len );
[5c9512f]1154                g_snprintf( prefix, len, "%s%s", irc->nick, set_getstr( &irc->set, "to_char" ) );
[b7d3cc34]1155                prefix[len-1] = 0;
1156        }
1157        else
1158        {
1159                prefix = "";
1160        }
1161       
1162        return( irc_privmsg( irc, u, "PRIVMSG", u->is_private ? irc->nick : irc->channel, prefix, msg ) );
1163}
1164
1165int irc_noticefrom( irc_t *irc, char *nick, char *msg )
1166{
1167        user_t *u = user_find( irc, nick );
1168       
1169        if( u )
1170                return( irc_privmsg( irc, u, "NOTICE", irc->nick, "", msg ) );
1171        else
1172                return( 0 );
1173}
1174
1175/* Returns 0 if everything seems to be okay, a number >0 when there was a
1176   timeout. The number returned is the number of seconds we received no
1177   pongs from the user. When not connected yet, we don't ping but drop the
1178   connection when the user fails to connect in IRC_LOGIN_TIMEOUT secs. */
[ba9edaa]1179static gboolean irc_userping( gpointer _irc, gint fd, b_input_condition cond )
[b7d3cc34]1180{
1181        irc_t *irc = _irc;
1182        int rv = 0;
1183       
[3af70b0]1184        if( !( irc->status & USTATUS_LOGGED_IN ) )
[b7d3cc34]1185        {
1186                if( gettime() > ( irc->last_pong + IRC_LOGIN_TIMEOUT ) )
1187                        rv = gettime() - irc->last_pong;
1188        }
1189        else
1190        {
1191                if( ( gettime() > ( irc->last_pong + global.conf->ping_interval ) ) && !irc->pinging )
1192                {
1193                        irc_write( irc, "PING :%s", IRC_PING_STRING );
1194                        irc->pinging = 1;
1195                }
1196                else if( gettime() > ( irc->last_pong + global.conf->ping_timeout ) )
1197                {
1198                        rv = gettime() - irc->last_pong;
1199                }
1200        }
1201       
1202        if( rv > 0 )
1203        {
[f73b969]1204                irc_abort( irc, 0, "Ping Timeout: %d seconds", rv );
[b7d3cc34]1205                return FALSE;
1206        }
1207       
1208        return TRUE;
1209}
Note: See TracBrowser for help on using the repository browser.