source: irc.c @ 5674207

Last change on this file since 5674207 was 5674207, checked in by Wilmer van der Gaast <wilmer@…>, at 2009-11-28T19:51:39Z

Reshuffled initialization sequence a little bit. Most important change:
nogaim_init() should be done after fork() to make ForkDaemon mode work
again. Also, doing help_init() earlie makes "help purple" work.

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