aboutsummaryrefslogtreecommitdiffstats
path: root/utils/bitlbeed.c
blob: 82bd08798c8b4662f8030c0685c18af9a41f11b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/****************************************************************\
*                                                                *
*  bitlbeed.c                                                    *
*                                                                *
*  A tiny daemon to allow you to run The Bee as a non-root user  *
*  (without access to /etc/inetd.conf or whatever)               *
*                                                                *
*  Copyright 2002-2004 Wilmer van der Gaast <wilmer@gaast.net>   *
*                                                                *
*  Licensed under the GNU General Public License                 *
*                                                                *
*  Modified by M. Dennis, 20040627                               *
\****************************************************************/

/* 
   ChangeLog:
   
   2004-06-27: Added support for AF_LOCAL (UNIX domain) sockets
               Renamed log to do_log to fix conflict warning
               Changed protocol to 0 (6 is not supported?)
               Added error check for socket()
               Added a no-fork (debug) mode
   2004-05-15: Added rate limiting
   2003-12-26: Added the SO_REUSEADDR sockopt, logging and CPU-time limiting
               for clients using setrlimit(), fixed the execv() call
   2002-11-29: Added the timeout so old child processes clean up faster
   2002-11-28: First version
*/

#define SELECT_TIMEOUT 2
#define MAX_LOG_LEN 128

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <fcntl.h>
#include <stdarg.h>
#include <time.h>

#include <sys/wait.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/resource.h>
#include <sys/stat.h>

#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>

typedef struct settings
{
	char local;
	char debug;
	char *interface;
	signed int port;
	
	unsigned char max_conn;
	int seconds;
	
	int rate_seconds;
	int rate_times;
	int rate_ignore;
	
	char **call;
} settings_t;

typedef struct ipstats
{
	unsigned int ip;
	
	time_t rate_start;
	int rate_times;
	time_t rate_ignore;
	
	struct ipstats *next;
} ipstats_t;

FILE *logfile;
ipstats_t *ipstats;

settings_t *set_load( int argc, char *argv[] );
void do_log( char *fmt, ... );
ipstats_t *ip_get( char *ip_txt );

int main( int argc, char *argv[] )
{
	const int rebind_on = 1;
	settings_t *set;
	
	int serv_fd, serv_len;
	struct sockaddr_in serv_addr;
	struct sockaddr_un local_addr;
	
	pid_t st;
	
	if( !( set = set_load( argc, argv ) ) )
		return( 1 );
	
	if( !logfile )
		if( !( logfile = fopen( "/dev/null", "w" ) ) )
		{
			perror( "fopen" );
			return( 1 );
		}
	
	fcntl( fileno( logfile ), F_SETFD, FD_CLOEXEC );
	
	if( set->local )
		serv_fd = socket( PF_LOCAL, SOCK_STREAM, 0 );
	else
		serv_fd = socket( PF_INET, SOCK_STREAM, 0 );
	if( serv_fd < 0 )
	{
		perror( "socket" );
		return( 1 );
	}
	setsockopt( serv_fd, SOL_SOCKET, SO_REUSEADDR, &rebind_on, sizeof( rebind_on ) );
	fcntl( serv_fd, F_SETFD, FD_CLOEXEC );
	if (set->local) {
		local_addr.sun_family = AF_LOCAL;
		strncpy( local_addr.sun_path, set->interface, sizeof( local_addr.sun_path ) - 1 );
		local_addr.sun_path[sizeof( local_addr.sun_path ) - 1] = '\0';
		
		/* warning - don't let untrusted users run this program if it
		   is setuid/setgid! Arbitrary file deletion risk! */
		unlink( set->interface );
		if( bind( serv_fd, (struct sockaddr *) &local_addr, SUN_LEN( &local_addr ) ) != 0 )
		{
			perror( "bind" );
			return( 1 );
		}
		chmod( set->interface, S_IRWXO|S_IRWXG|S_IRWXU );

	} else {
		serv_addr.sin_family = AF_INET;
		serv_addr.sin_addr.s_addr = inet_addr( set->interface );
		serv_addr.sin_port = htons( set->port );
		serv_len = sizeof( serv_addr );
	
		if( bind( serv_fd, (struct sockaddr *) &serv_addr, serv_len ) != 0 )
		{
			perror( "bind" );
			return( 1 );
		}
	}
	
	if( listen( serv_fd, set->max_conn ) != 0 )
	{
		perror( "listen" );
		return( 1 );
	}
	
	if ( ! set->debug ) {
		st = fork();
		if( st < 0 )
		{
			perror( "fork" );
			return( 1 );
		}
		else if( st > 0 )
		{
			return( 0 );
		}
		
		setsid();
		close( 0 );
		close( 1 );
		close( 2 );
	}
	
	do_log( "bitlbeed running" );
	
	/* The Daemon */
	while( 1 )
	{
		int cli_fd, cli_len, i, st;
		struct sockaddr_in cli_addr;
		struct sockaddr_un cli_local;
		ipstats_t *ip;
		char *cli_txt;
		pid_t child;
		
		static int running = 0;
		
		fd_set rd;
		struct timeval tm;
		
		/* accept() only returns after someone connects. To clean up old
		   processes (by running waitpid()) it's better to use select()
		   with a timeout. */
		FD_ZERO( &rd );
		FD_SET( serv_fd, &rd );
		tm.tv_sec = SELECT_TIMEOUT;
		tm.tv_usec = 0;
		if( select( serv_fd + 1, &rd, NULL, NULL, &tm ) > 0 )
		{
			if (set->local) {
				cli_len = SUN_LEN( &cli_local );
				cli_fd = accept( serv_fd, (struct sockaddr *) &cli_local, &cli_len );
				cli_txt = "127.0.0.1";
			} else {
				cli_len = sizeof( cli_addr );
				cli_fd = accept( serv_fd, (struct sockaddr *) &cli_addr, &cli_len );
				cli_txt = inet_ntoa( cli_addr.sin_addr );
			}
			
			ip = ip_get( cli_txt );
			
			if( set->rate_times == 0 || time( NULL ) > ip->rate_ignore )
			{
				/* We want this socket on stdout and stderr too! */
				dup( cli_fd ); dup( cli_fd );
				
				if( ( child = fork() ) == 0 )
				{
					if( set->seconds )
					{
						struct rlimit li;
						
						li.rlim_cur = (rlim_t) set->seconds;
						li.rlim_max = (rlim_t) set->seconds + 1;
						setrlimit( RLIMIT_CPU, &li );
					}
					execv( set->call[0], set->call );
					do_log( "Error while executing %s!", set->call[0] );
					return( 1 );
				}
				
				running ++;
				close( 0 );
				close( 1 );
				close( 2 );
				
				do_log( "Started child process for client %s (PID=%d), got %d clients now", cli_txt, child, running );
				
				if( time( NULL ) < ( ip->rate_start + set->rate_seconds ) )
				{
					ip->rate_times ++;
					if( ip->rate_times >= set->rate_times )
					{
						do_log( "Client %s crossed the limit; ignoring for the next %d seconds", cli_txt, set->rate_ignore );
						ip->rate_ignore = time( NULL ) + set->rate_ignore;
						ip->rate_start = 0;
					}
				}
				else
				{
					ip->rate_start = time( NULL );
					ip->rate_times = 1;
				}
			}
			else
			{
				do_log( "Ignoring connection from %s", cli_txt );
				close( cli_fd );
			}
		}
		
		/* If the max. number of connection is reached, don't accept
		   new connections until one expires -> Not always WNOHANG
		   
		   Cleaning up child processes is a good idea anyway... :-) */
		while( ( i = waitpid( 0, &st, ( ( running < set->max_conn ) || ( set->max_conn == 0 ) ) ? WNOHANG : 0 ) ) > 0 )
		{
			running --;
			if( WIFEXITED( st ) )
			{
				do_log( "Child process (PID=%d) exited normally with status %d. %d Clients left now",
				     i, WEXITSTATUS( st ), running );
			}
			else if( WIFSIGNALED( st ) )
			{
				do_log( "Child process (PID=%d) killed by signal %d. %d Clients left now",
				     i, WTERMSIG( st ), running );
			}
			else
			{
				/* Should not happen AFAIK... */
				do_log( "Child process (PID=%d) stopped for unknown reason, %d clients left now",
				     i, running );
			}
		}
	}
	
	return( 0 );
}

settings_t *set_load( int argc, char *argv[] )
{
	settings_t *set;
	int opt, i;
	
	set = malloc( sizeof( settings_t ) );
	memset( set, 0, sizeof( settings_t ) );
	set->interface = NULL;		/* will be filled in later */
	set->port = 6667;
	set->local = 0;
	set->debug = 0;
	
	set->rate_seconds = 600;
	set->rate_times = 5;
	set->rate_ignore = 900;
	
	while( ( opt = getopt( argc, argv, "i:p:n:t:l:r:hud" ) ) >= 0 )
	{
		if( opt == 'i' )
		{
			set->interface = strdup( optarg );
		}
		else if( opt == 'p' )
		{
			if( ( sscanf( optarg, "%d", &i ) != 1 ) || ( i <= 0 ) || ( i > 65535 ) )
			{
				fprintf( stderr, "Invalid port number: %s\n", optarg );
				return( NULL );
			}
			set->port = i;
		}
		else if( opt == 'n' )
		{
			if( ( sscanf( optarg, "%d", &i ) != 1 ) || ( i < 0 ) )
			{
				fprintf( stderr, "Invalid number of connections: %s\n", optarg );
				return( NULL );
			}
			set->max_conn = i;
		}
		else if( opt == 't' )
		{
			if( ( sscanf( optarg, "%d", &i ) != 1 ) || ( i < 0 ) || ( i > 600 ) )
			{
				fprintf( stderr, "Invalid number of seconds: %s\n", optarg );
				return( NULL );
			}
			set->seconds = i;
		}
		else if( opt == 'l' )
		{
			if( !( logfile = fopen( optarg, "a" ) ) )
			{
				perror( "fopen" );
				fprintf( stderr, "Error opening logfile, giving up.\n" );
				return( NULL );
			}
			setbuf( logfile, NULL );
		}
		else if( opt == 'r' )
		{
			if( sscanf( optarg, "%d,%d,%d", &set->rate_seconds, &set->rate_times, &set->rate_ignore ) != 3 )
			{
				fprintf( stderr, "Invalid argument to -r.\n" );
				return( NULL );
			}
		}
		else if( opt == 'u' )
			set->local = 1;
		else if( opt == 'd' )
			set->debug = 1;
		else if( opt == 'h' )
		{
			printf( "Usage: %s [-i <interface>] [-p <port>] [-n <num>] [-r x,y,z] ...\n"
			        "          ... <command> <args...>\n"
			        "A simple inetd-like daemon to have a program listening on a TCP socket without\n"
			        "needing root access to the machine\n"
			        "\n"
			        "  -i  Specify the interface (by IP address) to listen on.\n"
			        "      (Default: 0.0.0.0 (any interface))\n"
			        "  -p  Port number to listen on. (Default: 6667)\n"
			        "  -n  Maximum number of connections. (Default: 0 (unlimited))\n"
			        "  -t  Specify the maximum number of CPU seconds per process.\n"
			        "      (Default: 0 (unlimited))\n"
			        "  -l  Specify a logfile. (Default: none)\n"
			        "  -r  Rate limiting: Ignore a host for z seconds when it connects for more\n"
			        "      than y times in x seconds. (Default: 600,5,900. Disable: 0,0,0)\n"
				"  -u  Use a local socket, by default /tmp/bitlbee (override with -i <filename>)\n"
				"  -d  Don't fork for listening (for debugging purposes)\n"
			        "  -h  This information\n", argv[0] );
			return( NULL );
		}
	}
	
	if( set->interface == NULL )
		set->interface = (set->local) ? "/tmp/bitlbee" : "0.0.0.0";
	
	if( optind == argc )
	{
		fprintf( stderr, "Missing program parameter!\n" );
		return( NULL );
	}
	
	/* The remaining arguments are the executable and its arguments */
	set->call = malloc( ( argc - optind + 1 ) * sizeof( char* ) );
	memcpy( set->call, argv + optind, sizeof( char* ) * ( argc - optind ) );
	set->call[argc-optind] = NULL;
	
	return( set );
}

void do_log( char *fmt, ... )
{
	va_list params;
	char line[MAX_LOG_LEN];
	time_t tm;
	int l;
	
	memset( line, 0, MAX_LOG_LEN );
	
	tm = time( NULL );
	strcpy( line, ctime( &tm ) );
	l = strlen( line );
	line[l-1] = ' ';
	
	va_start( params, fmt );
	vsnprintf( line + l, MAX_LOG_LEN - l - 2, fmt, params );
	va_end( params );
	strcat( line, "\n" );
	
	fprintf( logfile, "%s", line );
}

ipstats_t *ip_get( char *ip_txt )
{
	unsigned int ip;
	ipstats_t *l;
	int p[4];
	
	sscanf( ip_txt, "%d.%d.%d.%d", p + 0, p + 1, p + 2, p + 3 );
	ip = ( p[0] << 24 ) | ( p[1] << 16 ) | ( p[2] << 8 ) | ( p[3] );
	
	for( l = ipstats; l; l = l->next )
	{
		if( l->ip == ip )
			return( l );
	}
	
	if( ipstats )
	{
		for( l = ipstats; l->next; l = l->next );
		
		l->next = malloc( sizeof( ipstats_t ) );
		l = l->next;
	}
	else
	{
		l = malloc( sizeof( ipstats_t ) );
		ipstats = l;
	}
	memset( l, 0, sizeof( ipstats_t ) );
	
	l->ip = ip;
	
	return( l );
}
span class="w"> NULL) query_del(irc, irc->queries); if (irc->accounts != NULL) { account = irc->accounts; while (account != NULL) { g_free(account->user); g_free(account->pass); g_free(account->server); accounttmp = account; account = account->next; g_free(accounttmp); } } if (irc->users != NULL) { user = irc->users; while (user != NULL) { g_free(user->nick); g_free(user->away); g_free(user->handle); if(user->user!=user->nick) g_free(user->user); if(user->host!=user->nick) g_free(user->host); if(user->realname!=user->nick) g_free(user->realname); gaim_input_remove(user->sendbuf_timer); usertmp = user; user = user->next; g_free(usertmp); } } g_hash_table_foreach_remove(irc->userhash, irc_free_userhash, NULL); g_hash_table_destroy(irc->userhash); g_hash_table_foreach_remove(irc->watches, irc_free_userhash, NULL); g_hash_table_destroy(irc->watches); if (irc->nicks != NULL) { nick = irc->nicks; while (nick != NULL) { g_free(nick->nick); g_free(nick->handle); nicktmp = nick; nick = nick->next; g_free(nicktmp); } } if (irc->help != NULL) { helpnode = irc->help; while (helpnode != NULL) { g_free(helpnode->string); helpnodetmp = helpnode; helpnode = helpnode->next; g_free(helpnodetmp); } } if (irc->set != NULL) { setnode = irc->set; while (setnode != NULL) { g_free(setnode->key); g_free(setnode->def); g_free(setnode->value); setnodetmp = setnode; setnode = setnode->next; g_free(setnodetmp); } } g_free(irc); if( global.conf->runmode == RUNMODE_INETD ) g_main_quit( global.loop ); } /* USE WITH CAUTION! Sets pass without checking */ void irc_setpass (irc_t *irc, const char *pass) { if (irc->password) g_free (irc->password); if (pass) { irc->password = g_strdup (pass); irc_usermsg (irc, "Password successfully changed"); } else { irc->password = NULL; } } int irc_process( irc_t *irc ) { char **lines, *temp; int i; if( irc->readbuffer != NULL ) { lines = irc_tokenize(irc->readbuffer ); for( i = 0; *lines[i] != '\0'; i++ ) { if( lines[i+1] == NULL ) { temp = g_strdup( lines[i] ); g_free( irc->readbuffer ); irc->readbuffer = temp; i++; break; } if (!irc_process_line(irc, lines[i])) { g_free( lines ); return 0; } } if(lines[i]!=NULL) { g_free(irc->readbuffer); irc->readbuffer=NULL; } g_free( lines ); } return 1; } char **irc_tokenize( char *buffer ) { int i, j; char **lines; /* Count the number of elements we're gonna need. */ for(i=0, j=1; buffer[i]!='\0'; i++ ) { if(buffer[i]=='\n' ) if(buffer[i+1]!='\r' && buffer[i+1]!='\n') j++; } /* Allocate j+1 elements. */ lines=g_new (char *, j+1); /* NULL terminate our list. */ lines[j]=NULL; lines[0]=buffer; /* Split the buffer in several strings, using \r\n as our seperator, where \r is optional. * Although this is not in the RFC, some braindead ircds (newnet's) use this, so some clients might too. */ for( i=0, j=0; buffer[i]!='\0'; i++) { if(buffer[i]=='\n') { buffer[i]='\0'; /* We dont want to read 1 byte before our buffer * and (in rare cases) generate a SIGSEGV. */ if(i!=0) if(buffer[i-1]=='\r') buffer[i-1]='\0'; if(buffer[i+1]!='\r'&&buffer[i+1]!='\n') lines[++j]=buffer+i+1; } } return(lines); } int irc_process_line( irc_t *irc, char *line ) { int i, j; char **cmd; /* Move the line pointer to the start of the command, skipping spaces and the optional prefix. */ if(line[0]==':') { for(i=0; line[i]!=32; i++); line=line+i; } for(i=0; line[i]==32; i++); line=line+i; /* If we're already at the end of the line, return. If not, we're going to need at least one element. */ if(line[0]=='\0') return 1; else j=1; /* Count the number of char **cmd elements we're going to need. */ for(i=0; line[i]!='\0'; i++) { if((line[i]==32) && (line[i+1]!=32) && (line[i+1]!='\0') && (line[i+1]!=':')) j++; else if((line[i]==':') && (line[i+1]!='\0') && (line[i-1]==32)) { j++; break; } } /* Allocate the space we need. */ cmd=g_new(char *, j+1); cmd[j]=NULL; /* Do the actual line splitting, format is: * Input: "PRIVMSG #bitlbee :foo bar" * Output: cmd[0]=="PRIVMSG", cmd[1]=="#bitlbee", cmd[2]=="foo bar", cmd[3]==NULL */ cmd[0]=line; for(i=0, j=0; line[i]!='\0'; i++) { if((line[i]==32)) { line[i]='\0'; if((line[i+1]!=32) && (line[i+1]!='\0') && (line[i+1]!=':')) cmd[++j]=line+i+1; } else if((line[i]==':') && (line[i+1]!='\0') && (line[i-1]=='\0')) { cmd[++j]=line+i+1; break; } } i=irc_exec(irc, cmd); g_free(cmd); return(i); } int irc_exec( irc_t *irc, char **cmd ) { int i; if( (global.conf)->authmode == AUTHMODE_CLOSED && irc->status < USTATUS_AUTHORIZED ) { if( g_strcasecmp( cmd[0], "PASS" ) == 0 ) { if( !cmd[1] ) { irc_reply( irc, 461, "%s :Need more parameters", cmd[0] ); } else if( strcmp( cmd[1], (global.conf)->password ) == 0 ) { irc->status = USTATUS_AUTHORIZED; } else { irc_reply( irc, 464, ":Nope, maybe you should try it again..." ); } } else { irc_reply( irc, 464, ":Uhh, fine, but I want the password first." ); } return( 1 ); } if( g_strcasecmp( cmd[0], "USER" ) == 0 ) { if( !( cmd[1] && cmd[2] && cmd[3] && cmd[4] ) ) { irc_reply( irc, 461, "%s :Need more parameters", cmd[0] ); } else if( irc->user ) { irc_reply( irc, 462, ":You can't change your nick/userinfo" ); } else { irc->user = g_strdup( cmd[1] ); irc->realname = g_strdup( cmd[4] ); if( irc->nick ) irc_login( irc ); } return( 1 ); } else if( g_strcasecmp( cmd[0], "NICK" ) == 0 ) { if( !cmd[1] ) { irc_reply( irc, 461, "%s :Need more parameters", cmd[0] ); } else if( irc->nick ) { irc_reply( irc, 438, ":The hand of the deity is upon thee, thy nick may not change" ); } /* This is not clean, but for now it'll have to be like this... */ else if( ( nick_cmp( cmd[1], irc->mynick ) == 0 ) || ( nick_cmp( cmd[1], NS_NICK ) == 0 ) ) { irc_reply( irc, 433, ":This nick is already in use" ); } else if( !nick_ok( cmd[1] ) ) { /* [SH] Invalid characters. */ irc_reply( irc, 432, ":This nick contains invalid characters" ); } else { irc->nick = g_strdup( cmd[1] ); if( irc->user ) irc_login( irc ); } return( 1 ); } else if( g_strcasecmp( cmd[0], "QUIT" ) == 0 ) { irc_write( irc, "ERROR :%s%s", cmd[1]?"Quit: ":"", cmd[1]?cmd[1]:"Client Quit" ); g_io_channel_close( irc->io_channel ); return( 0 ); } if( !irc->user || !irc->nick ) { irc_reply( irc, 451, ":Register first" ); return( 1 ); } if( g_strcasecmp( cmd[0], "PING" ) == 0 ) { irc_write( irc, ":%s PONG %s :%s", irc->myhost, irc->myhost, cmd[1]?cmd[1]:irc->myhost ); } else if( g_strcasecmp( cmd[0], "MODE" ) == 0 ) { if( !cmd[1] ) { irc_reply( irc, 461, "%s :Need more parameters", cmd[0] ); } else if( *cmd[1] == '#' || *cmd[1] == '&' ) { if( cmd[2] ) { if( *cmd[2] == '+' || *cmd[2] == '-' ) irc_reply( irc, 477, "%s :Can't change channel modes", cmd[1] ); else if( *cmd[2] == 'b' ) irc_reply( irc, 368, "%s :No bans possible", cmd[1] ); } else irc_reply( irc, 324, "%s +%s", cmd[1], CMODE ); } else { if( nick_cmp( cmd[1], irc->nick ) == 0 ) { if( cmd[2] ) irc_umode_set( irc, irc->nick, cmd[2] ); } else irc_reply( irc, 502, ":Don't touch their modes" ); } } else if( g_strcasecmp( cmd[0], "NAMES" ) == 0 ) { irc_names( irc, cmd[1]?cmd[1]:irc->channel ); } else if( g_strcasecmp( cmd[0], "PART" ) == 0 ) { struct conversation *c; if( !cmd[1] ) { irc_reply( irc, 461, "%s :Need more parameters", cmd[0] ); } else if( g_strcasecmp( cmd[1], irc->channel ) == 0 ) { user_t *u = user_find( irc, irc->nick ); /* Not allowed to leave control channel */ irc_part( irc, u, irc->channel ); irc_join( irc, u, irc->channel ); } else if( ( c = conv_findchannel( cmd[1] ) ) ) { user_t *u = user_find( irc, irc->nick ); irc_part( irc, u, c->channel ); if( c->gc && c->gc->prpl ) { c->joined = 0; c->gc->prpl->chat_leave( c->gc, c->id ); } } else { irc_reply( irc, 403, "%s :No such channel", cmd[1] ); } } else if( g_strcasecmp( cmd[0], "JOIN" ) == 0 ) { if( !cmd[1] ) { irc_reply( irc, 461, "%s :Need more parameters", cmd[0] ); } else if( g_strcasecmp( cmd[1], irc->channel ) == 0 ) ; /* Dude, you're already there... RFC doesn't have any reply for that though? */ else if( cmd[1] ) { if( ( cmd[1][0] == '#' || cmd[1][0] == '&' ) && cmd[1][1] ) { user_t *u = user_find( irc, cmd[1] + 1 ); if( u && u->gc && u->gc->prpl && u->gc->prpl->chat_open ) { irc_reply( irc, 403, "%s :Initializing groupchat in a different channel", cmd[1] ); if( !u->gc->prpl->chat_open( u->gc, u->handle ) ) { irc_usermsg( irc, "Could not open a groupchat with %s, maybe you don't have a connection to him/her yet?", u->nick ); } } else { irc_reply( irc, 403, "%s :Groupchats are not possible with %s", cmd[1], cmd[1]+1 ); } } else { irc_reply( irc, 403, "%s :No such channel", cmd[1] ); } } } else if( g_strcasecmp( cmd[0], "INVITE" ) == 0 ) { if( cmd[1] && cmd[2] ) irc_invite( irc, cmd[1], cmd[2] ); else irc_reply( irc, 461, "%s :Need more parameters", cmd[0] ); } else if( g_strcasecmp( cmd[0], "PRIVMSG" ) == 0 || g_strcasecmp( cmd[0], "NOTICE" ) == 0 ) { if( !cmd[1] ) { irc_reply( irc, 461, "%s :Need more parameters", cmd[0] ); } else if ( !cmd[2] ) { irc_reply( irc, 412, ":No text to send" ); } else if ( irc->nick && g_strcasecmp( cmd[1], irc->nick ) == 0 ) { irc_write( irc, ":%s!%s@%s %s %s :%s", irc->nick, irc->user, irc->host, cmd[0], cmd[1], cmd[2] ); } else { if( g_strcasecmp( cmd[1], irc->channel ) == 0 ) { unsigned int i; char *t = set_getstr( irc, "default_target" ); if( g_strcasecmp( t, "last" ) == 0 && irc->last_target ) cmd[1] = irc->last_target; else if( g_strcasecmp( t, "root" ) == 0 ) cmd[1] = irc->mynick; for( i = 0; i < strlen( cmd[2] ); i ++ ) { if( cmd[2][i] == ' ' ) break; if( cmd[2][i] == ':' || cmd[2][i] == ',' ) { cmd[1] = cmd[2]; cmd[2] += i; *cmd[2] = 0; while( *(++cmd[2]) == ' ' ); break; } } irc->is_private = 0; if( cmd[1] != irc->last_target ) { if( irc->last_target ) g_free( irc->last_target ); irc->last_target = g_strdup( cmd[1] ); } } else { irc->is_private = 1; } irc_send( irc, cmd[1], cmd[2], ( g_strcasecmp( cmd[0], "NOTICE" ) == 0 ) ? IM_FLAG_AWAY : 0 ); } } else if( g_strcasecmp( cmd[0], "WHO" ) == 0 ) { irc_who( irc, cmd[1] ); } else if( g_strcasecmp( cmd[0], "USERHOST" ) == 0 ) { user_t *u; if( !cmd[1] ) { irc_reply( irc, 461, "%s :Need more parameters", cmd[0] ); } /* [TV] Usable USERHOST-implementation according to RFC1459. Without this, mIRC shows an error while connecting, and the used way of rejecting breaks standards. */ for( i = 1; cmd[i]; i ++ ) if( ( u = user_find( irc, cmd[i] ) ) ) { if( u->online && u->away ) irc_reply( irc, 302, ":%s=-%s@%s", u->nick, u->user, u->host ); else irc_reply( irc, 302, ":%s=+%s@%s", u->nick, u->user, u->host ); } } else if( g_strcasecmp( cmd[0], "ISON" ) == 0 ) { user_t *u; char buff[IRC_MAX_LINE]; int lenleft; buff[0] = '\0'; /* [SH] Leave room for : and \0 */ lenleft = IRC_MAX_LINE - 2; for( i = 1; cmd[i]; i ++ ) { if( ( u = user_find( irc, cmd[i] ) ) && u->online ) { /* [SH] Make sure we don't use too much buffer space. */ lenleft -= strlen( u->nick ) + 1; if( lenleft < 0 ) { break; } /* [SH] Add the nick to the buffer. Note * that an extra space is always added. Even * if it's the last nick in the list. Who * cares? */ strcat( buff, u->nick ); strcat( buff, " " ); } } /* [WvG] Well, maybe someone cares, so why not remove it? */ if( strlen( buff ) > 0 ) buff[strlen(buff)-1] = '\0'; /* [SH] By the way, that really *was* WvG talking. */ /* [WvG] Really? */ /* [SH] Yeah... But *this* is WvG talking too. ;-P */ /* [WvG] *sigh* */ irc_reply( irc, 303, ":%s", buff ); } else if( g_strcasecmp( cmd[0], "WATCH" ) == 0 ) { /* Obviously we could also mark a user structure as being watched, but what if the WATCH command is sent right after connecting? The user won't exist yet then... */ for( i = 1; cmd[i]; i ++ ) { char *nick; user_t *u; if( !cmd[i][0] || !cmd[i][1] ) break; nick = g_strdup( cmd[i] + 1 ); nick_lc( nick ); u = user_find( irc, nick ); if( cmd[i][0] == '+' ) { if( !g_hash_table_lookup( irc->watches, nick ) ) g_hash_table_insert( irc->watches, nick, nick ); if( u && u->online ) irc_reply( irc, 604, "%s %s %s %d :%s", u->nick, u->user, u->host, time( NULL ), "is online" ); else irc_reply( irc, 605, "%s %s %s %d :%s", nick, "*", "*", time( NULL ), "is offline" ); } else if( cmd[i][0] == '-' ) { gpointer okey, ovalue; if( g_hash_table_lookup_extended( irc->watches, nick, &okey, &ovalue ) ) { g_free( okey ); g_hash_table_remove( irc->watches, okey ); irc_reply( irc, 602, "%s %s %s %d :%s", nick, "*", "*", 0, "Stopped watching" ); } } } } else if( g_strcasecmp( cmd[0], "TOPIC" ) == 0 ) { if( cmd[1] && cmd[2] ) irc_reply( irc, 482, "%s :Cannot change topic", cmd[1] ); else if( cmd[1] ) irc_topic( irc, cmd[1] ); else irc_reply( irc, 461, "%s :Need more parameters", cmd[0] ); } else if( g_strcasecmp( cmd[0], "AWAY" ) == 0 ) { irc_away( irc, cmd[1] ); } else if( g_strcasecmp( cmd[0], "WHOIS" ) == 0 ) { if( cmd[1] ) { irc_whois( irc, cmd[1] ); } else { irc_reply( irc, 461, "%s :Need more parameters", cmd[0] ); } } else if( g_strcasecmp( cmd[0], "WHOWAS" ) == 0 ) { /* For some reason irssi tries a whowas when whois fails. We can ignore this, but then the user never gets a "user not found" message from irssi which is a bit annoying. So just respond with not-found and irssi users will get better error messages */ if( cmd[1] ) { irc_reply( irc, 406, "%s :Nick does not exist", cmd[1] ); irc_reply( irc, 369, "%s :End of WHOWAS", cmd[1] ); } else { irc_reply( irc, 461, "%s :Need more parameters", cmd[0] ); } } else if( ( g_strcasecmp( cmd[0], "NICKSERV" ) == 0 ) || ( g_strcasecmp( cmd[0], "NS" ) == 0 ) ) { /* [SH] This aliases the NickServ command to PRIVMSG root */ /* [TV] This aliases the NS command to PRIVMSG root as well */ root_command( irc, cmd + 1 ); } else if( g_strcasecmp( cmd[0], "MOTD" ) == 0 ) { irc_motd( irc ); } else if( g_strcasecmp( cmd[0], "PONG" ) == 0 ) { /* We could check the value we get back from the user, but in fact we don't care, we're just happy he's still alive. */ irc->last_pong = gettime(); irc->pinging = 0; } else if( g_strcasecmp( cmd[0], "COMPLETIONS" ) == 0 ) { user_t *u = user_find( irc, irc->mynick ); help_t *h; set_t *s; int i; irc_privmsg( irc, u, "NOTICE", irc->nick, "COMPLETIONS ", "OK" ); for( i = 0; commands[i].command; i ++ ) irc_privmsg( irc, u, "NOTICE", irc->nick, "COMPLETIONS ", commands[i].command ); for( h = global.help; h; h = h->next ) irc_privmsg( irc, u, "NOTICE", irc->nick, "COMPLETIONS help ", h->string ); for( s = irc->set; s; s = s->next ) irc_privmsg( irc, u, "NOTICE", irc->nick, "COMPLETIONS set ", s->key ); irc_privmsg( irc, u, "NOTICE", irc->nick, "COMPLETIONS ", "END" ); } else if( set_getint( irc, "debug" ) ) { irc_usermsg( irc, "\002--- Unknown command:" ); for( i = 0; cmd[i]; i ++ ) irc_usermsg( irc, "%s", cmd[i] ); irc_usermsg( irc, "\002--------------------" ); } return( 1 ); } void irc_reply( irc_t *irc, int code, char *format, ... ) { char text[IRC_MAX_LINE]; va_list params; va_start( params, format ); g_vsnprintf( text, IRC_MAX_LINE, format, params ); va_end( params ); irc_write( irc, ":%s %03d %s %s", irc->myhost, code, irc->nick?irc->nick:"*", text ); return; } int irc_usermsg( irc_t *irc, char *format, ... ) { char text[1024]; va_list params; char is_private = 0; user_t *u; u = user_find( irc, irc->mynick ); if( u ) is_private = u->is_private; va_start( params, format ); g_vsnprintf( text, sizeof( text ), format, params ); va_end( params ); return( irc_msgfrom( irc, u->nick, text ) ); } void irc_write( irc_t *irc, char *format, ... ) { va_list params; va_start( params, format ); irc_vawrite( irc, format, params ); va_end( params ); return; } void irc_vawrite( irc_t *irc, char *format, va_list params ) { int size; char line[IRC_MAX_LINE]; if( irc->quit ) return; g_vsnprintf( line, IRC_MAX_LINE - 3, format, params ); strip_newlines( line ); strcat( line, "\r\n" ); if( irc->sendbuffer != NULL ) { size = strlen( irc->sendbuffer ) + strlen( line ); #ifdef FLOOD_SEND if( size > FLOOD_SEND_MAXBUFFER ) { /* Die flooder, die! >:) */ g_free(irc->sendbuffer); /* We need the \r\n at the start because else we might append our string to a half * sent line. A bit hackish, but it works. */ irc->sendbuffer = g_strdup( "\r\nERROR :Sendq Exceeded\r\n" ); irc->quit = 1; return; } #endif irc->sendbuffer = g_renew ( char, irc->sendbuffer, size + 1 ); strcpy( ( irc->sendbuffer + strlen( irc->sendbuffer ) ), line ); } else irc->sendbuffer = g_strdup(line); if( irc->w_watch_source_id == 0 ) { irc->w_watch_source_id = g_io_add_watch( irc->io_channel, G_IO_OUT, bitlbee_io_current_client_write, irc ); } return; } void irc_write_all( int now, char *format, ... ) { va_list params; GSList *temp; va_start( params, format ); temp = irc_connection_list; while( temp != NULL ) { irc_t *irc = temp->data; if( now ) { g_free( irc->sendbuffer ); irc->sendbuffer = g_strdup( "\r\n" ); } irc_vawrite( temp->data, format, params ); if( now ) { bitlbee_io_current_client_write( irc->io_channel, G_IO_OUT, irc ); } temp = temp->next; } va_end( params ); return; } void irc_names( irc_t *irc, char *channel ) { user_t *u = irc->users; char *s; int control = ( g_strcasecmp( channel, irc->channel ) == 0 ); struct conversation *c = NULL; if( !control ) c = conv_findchannel( channel ); /* RFC's say there is no error reply allowed on NAMES, so when the channel is invalid, just give an empty reply. */ if( control || c ) while( u ) { if( u->online ) { if( u->gc && control ) { if( set_getint( irc, "away_devoice" ) && !u->away ) s = "+"; else s = ""; irc_reply( irc, 353, "@ %s :%s%s", channel, s, u->nick ); } else if( !u->gc ) { if( strcmp( u->nick, irc->mynick ) == 0 && ( strcmp( set_getstr( irc, "ops" ), "root" ) == 0 || strcmp( set_getstr( irc, "ops" ), "both" ) == 0 ) ) s = "@"; else if( strcmp( u->nick, irc->nick ) == 0 && ( strcmp( set_getstr( irc, "ops" ), "user" ) == 0 || strcmp( set_getstr( irc, "ops" ), "both" ) == 0 ) ) s = "@"; else s = ""; irc_reply( irc, 353, "@ %s :%s%s", channel, s, u->nick ); } } u = u->next; } /* For non-controlchannel channels (group conversations) only root and you are listed now. Time to show the channel people: */ if( !control && c ) { GList *l; for( l = c->in_room; l; l = l->next ) if( ( u = user_findhandle( c->gc, l->data ) ) ) irc_reply( irc, 353, "@ %s :%s%s", channel, "", u->nick ); } irc_reply( irc, 366, "%s :End of /NAMES list", channel ); } void irc_who( irc_t *irc, char *channel ) { user_t *u = irc->users; struct conversation *c; GList *l; if( !channel || *channel == '0' || *channel == '*' || !*channel ) while( u ) { irc_reply( irc, 352, "%s %s %s %s %s %c :0 %s", u->online ? irc->channel : "*", u->user, u->host, irc->myhost, u->nick, u->online ? ( u->away ? 'G' : 'H' ) : 'G', u->realname ); u = u->next; } else if( g_strcasecmp( channel, irc->channel ) == 0 ) while( u ) { if( u->online ) irc_reply( irc, 352, "%s %s %s %s %s %c :0 %s", channel, u->user, u->host, irc->myhost, u->nick, u->away ? 'G' : 'H', u->realname ); u = u->next; } else if( ( c = conv_findchannel( channel ) ) ) for( l = c->in_room; l; l = l->next ) { if( ( u = user_findhandle( c->gc, l->data ) ) ) irc_reply( irc, 352, "%s %s %s %s %s %c :0 %s", channel, u->user, u->host, irc->myhost, u->nick, u->away ? 'G' : 'H', u->realname ); } else if( ( u = user_find( irc, channel ) ) ) irc_reply( irc, 352, "%s %s %s %s %s %c :0 %s", channel, u->user, u->host, irc->myhost, u->nick, u->online ? ( u->away ? 'G' : 'H' ) : 'G', u->realname ); irc_reply( irc, 315, "%s :End of /WHO list.", channel?channel:"**" ); } void irc_login( irc_t *irc ) { user_t *u; irc_reply( irc, 1, ":Welcome to the BitlBee gateway, %s", irc->nick ); irc_reply( irc, 2, ":Host %s is running BitlBee " BITLBEE_VERSION " " ARCH "/" CPU ".", irc->myhost ); irc_reply( irc, 3, ":%s", IRCD_INFO ); irc_reply( irc, 4, "%s %s %s %s", irc->myhost, BITLBEE_VERSION, UMODES, CMODES ); 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 ); irc_motd( irc ); irc_umode_set( irc, irc->myhost, "+" UMODE ); u = user_add( irc, irc->mynick ); u->host = g_strdup( irc->myhost ); u->realname = g_strdup( ROOT_FN ); u->online = 1; u->send_handler = root_command_string; u->is_private = 0; /* [SH] The channel is root's personal playground. */ irc_spawn( irc, u ); u = user_add( irc, NS_NICK ); u->host = g_strdup( irc->myhost ); u->realname = g_strdup( ROOT_FN ); u->online = 0; u->send_handler = root_command_string; u->is_private = 1; /* [SH] NickServ is not in the channel, so should always /query. */ u = user_add( irc, irc->nick ); u->user = g_strdup( irc->user ); u->host = g_strdup( irc->host ); u->realname = g_strdup( irc->realname ); u->online = 1; // u->send_handler = msg_echo; irc_spawn( irc, u ); 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." ); irc->status = USTATUS_LOGGED_IN; } void irc_motd( irc_t *irc ) { int fd; fd = open( global.conf->motdfile, O_RDONLY ); if( fd == -1 ) { irc_reply( irc, 422, ":We don't need MOTDs." ); } else { char linebuf[80]; /* Max. line length for MOTD's is 79 chars. It's what most IRC networks seem to do. */ char *add, max; int len; linebuf[79] = len = 0; max = sizeof( linebuf ) - 1; irc_reply( irc, 375, ":- %s Message Of The Day - ", irc->myhost ); while( read( fd, linebuf + len, 1 ) == 1 ) { if( linebuf[len] == '\n' || len == max ) { linebuf[len] = 0; irc_reply( irc, 372, ":- %s", linebuf ); len = 0; } else if( linebuf[len] == '%' ) { read( fd, linebuf + len, 1 ); if( linebuf[len] == 'h' ) add = irc->myhost; else if( linebuf[len] == 'v' ) add = BITLBEE_VERSION; else if( linebuf[len] == 'n' ) add = irc->nick; else add = "%"; strncpy( linebuf + len, add, max - len ); while( linebuf[++len] ); } else if( len < max ) { len ++; } } irc_reply( irc, 376, ":End of MOTD" ); closesocket( fd ); } } void irc_topic( irc_t *irc, char *channel ) { if( g_strcasecmp( channel, irc->channel ) == 0 ) { irc_reply( irc, 332, "%s :%s", channel, CONTROL_TOPIC ); } else { struct conversation *c = conv_findchannel( channel ); if( c ) irc_reply( irc, 332, "%s :BitlBee groupchat: \"%s\". Please keep in mind that root-commands won't work here. Have fun!", channel, c->title ); else irc_reply( irc, 331, "%s :No topic for this channel" ); } } void irc_whois( irc_t *irc, char *nick ) { user_t *u = user_find( irc, nick ); if( u ) { irc_reply( irc, 311, "%s %s %s * :%s", u->nick, u->user, u->host, u->realname ); if( u->gc ) irc_reply( irc, 312, "%s %s.%s :%s network", u->nick, u->gc->user->username, *u->gc->user->proto_opt[0] ? u->gc->user->proto_opt[0] : "", u->gc->prpl->name ); else irc_reply( irc, 312, "%s %s :%s", u->nick, irc->myhost, IRCD_INFO ); if( !u->online ) irc_reply( irc, 301, "%s :%s", u->nick, "User is offline" ); else if( u->away ) irc_reply( irc, 301, "%s :%s", u->nick, u->away ); irc_reply( irc, 318, "%s :End of /WHOIS list", nick ); } else { irc_reply( irc, 401, "%s :Nick does not exist", nick ); } } void irc_umode_set( irc_t *irc, char *who, char *s ) { char m[256], st = 1, *t; int i; memset( m, 0, sizeof( m ) ); for( t = irc->umode; *t; t ++ ) m[(int)*t] = 1; for( t = s; *t; t ++ ) { if( *t == '+' || *t == '-' ) st = *t == '+'; else m[(int)*t] = st; } memset( irc->umode, 0, sizeof( irc->umode ) ); for( i = 0; i < 256 && strlen( irc->umode ) < ( sizeof( irc->umode ) - 1 ); i ++ ) if( m[i] && strchr( UMODES, i ) ) irc->umode[strlen(irc->umode)] = i; irc_reply( irc, 221, "+%s", irc->umode ); } int irc_away( irc_t *irc, char *away ) { user_t *u = user_find( irc, irc->nick ); GSList *c = get_connections(); if( !u ) return( 0 ); if( away && *away ) { int i, j; /* Copy away string, but skip control chars. Mainly because Jabber really doesn't like them. */ u->away = g_malloc( strlen( away ) + 1 ); for( i = j = 0; away[i]; i ++ ) if( ( u->away[j] = away[i] ) >= ' ' ) j ++; u->away[j] = 0; irc_reply( irc, 306, ":You're now away: %s", u->away ); /* irc_umode_set( irc, irc->myhost, "+a" ); */ } else { if( u->away ) g_free( u->away ); u->away = NULL; /* irc_umode_set( irc, irc->myhost, "-a" ); */ irc_reply( irc, 305, ":Welcome back" ); } while( c ) { if( ((struct gaim_connection *)c->data)->flags & OPT_LOGGED_IN ) proto_away( c->data, u->away ); c = c->next; } return( 1 ); } void irc_spawn( irc_t *irc, user_t *u ) { irc_join( irc, u, irc->channel ); } void irc_join( irc_t *irc, user_t *u, char *channel ) { char *nick; if( ( g_strcasecmp( channel, irc->channel ) != 0 ) || user_find( irc, irc->nick ) ) irc_write( irc, ":%s!%s@%s JOIN :%s", u->nick, u->user, u->host, channel ); if( nick_cmp( u->nick, irc->nick ) == 0 ) { irc_write( irc, ":%s MODE %s +%s", irc->myhost, channel, CMODE ); irc_names( irc, channel ); irc_topic( irc, channel ); } nick = g_strdup( u->nick ); nick_lc( nick ); if( g_hash_table_lookup( irc->watches, nick ) ) { irc_reply( irc, 600, "%s %s %s %d :%s", u->nick, u->user, u->host, time( NULL ), "logged online" ); } g_free( nick ); } void irc_part( irc_t *irc, user_t *u, char *channel ) { irc_write( irc, ":%s!%s@%s PART %s :%s", u->nick, u->user, u->host, channel, "" ); } void irc_kick( irc_t *irc, user_t *u, char *channel, user_t *kicker ) { irc_write( irc, ":%s!%s@%s KICK %s %s :%s", kicker->nick, kicker->user, kicker->host, channel, u->nick, "" ); } void irc_kill( irc_t *irc, user_t *u ) { char *nick; irc_write( irc, ":%s!%s@%s QUIT :%s", u->nick, u->user, u->host, "Leaving..." ); nick = g_strdup( u->nick ); nick_lc( nick ); if( g_hash_table_lookup( irc->watches, nick ) ) { irc_reply( irc, 601, "%s %s %s %d :%s", u->nick, u->user, u->host, time( NULL ), "logged offline" ); } g_free( nick ); } void irc_invite( irc_t *irc, char *nick, char *channel ) { struct conversation *c = conv_findchannel( channel ); user_t *u = user_find( irc, nick ); if( u && c && ( u->gc == c->gc ) ) if( c->gc && c->gc->prpl && c->gc->prpl->chat_invite ) { c->gc->prpl->chat_invite( c->gc, c->id, "", u->handle ); irc_reply( irc, 341, "%s %s", nick, channel ); return; } irc_reply( irc, 482, "%s :Invite impossible; User/Channel non-existent or incompatible", channel ); } int irc_send( irc_t *irc, char *nick, char *s, int flags ) { struct conversation *c = NULL; user_t *u = NULL; if( *nick == '#' || *nick == '&' ) { if( !( c = conv_findchannel( nick ) ) ) { irc_reply( irc, 403, "%s :Channel does not exist", nick ); return( 0 ); } } else { u = user_find( irc, nick ); if( !u ) { if( irc->is_private ) irc_reply( irc, 401, "%s :Nick does not exist", nick ); else irc_usermsg( irc, "Nick `%s' does not exist!", nick ); return( 0 ); } } if( *s == 1 && s[strlen(s)-1] == 1 ) { if( g_strncasecmp( s + 1, "ACTION", 6 ) == 0 ) { if( s[7] == ' ' ) s ++; s += 3; *(s++) = '/'; *(s++) = 'm'; *(s++) = 'e'; *(s++) = ' '; s -= 4; s[strlen(s)-1] = 0; } else if( g_strncasecmp( s + 1, "VERSION", 7 ) == 0 ) { u = user_find( irc, irc->mynick ); irc_privmsg( irc, u, "NOTICE", irc->nick, "", "\001VERSION BitlBee " BITLBEE_VERSION " " ARCH "/" CPU "\001" ); return( 1 ); } else if( g_strncasecmp( s + 1, "PING", 4 ) == 0 ) { u = user_find( irc, irc->mynick ); irc_privmsg( irc, u, "NOTICE", irc->nick, "", s ); return( 1 ); } else if( g_strncasecmp( s + 1, "TYPING", 6 ) == 0 ) { if( u && u->gc && u->gc->prpl->send_typing && strlen( s ) >= 10 ) { time_t current_typing_notice = time( NULL ); if( current_typing_notice - u->last_typing_notice >= 5 ) { u->gc->prpl->send_typing( u->gc, u->handle, s[8] == '1' ); u->last_typing_notice = current_typing_notice; } } return( 1 ); } else { irc_usermsg( irc, "Non-ACTION CTCP's aren't supported" ); return( 0 ); } } if( u ) { /* For the next message, we probably do have to send new notices... */ u->last_typing_notice = 0; u->is_private = irc->is_private; if( u->is_private ) { if( !u->online ) irc_reply( irc, 301, "%s :%s", u->nick, "User is offline" ); else if( u->away ) irc_reply( irc, 301, "%s :%s", u->nick, u->away ); } if( u->send_handler ) return( u->send_handler( irc, u, s, flags ) ); } else if( c && c->gc && c->gc->prpl ) { return( serv_send_chat( irc, c->gc, c->id, s ) ); } return( 0 ); } gboolean buddy_send_handler_delayed( gpointer data ) { user_t *u = data; u->sendbuf[u->sendbuf_len-2] = 0; /* Cut off the last newline */ serv_send_im( u->gc->irc, u, u->sendbuf, u->sendbuf_flags ); g_free( u->sendbuf ); u->sendbuf = NULL; u->sendbuf_len = 0; u->sendbuf_timer = 0; u->sendbuf_flags = 0; return( FALSE ); } int buddy_send_handler( irc_t *irc, user_t *u, char *msg, int flags ) { if( !u || !u->gc ) return( 0 ); if( set_getint( irc, "buddy_sendbuffer" ) && set_getint( irc, "buddy_sendbuffer_delay" ) > 0 ) { int delay; if( u->sendbuf_len > 0 && u->sendbuf_flags != flags) { //Flush the buffer g_source_remove( u->sendbuf_timer ); buddy_send_handler_delayed( u ); } if( u->sendbuf_len == 0 ) { u->sendbuf_len = strlen( msg ) + 2; u->sendbuf = g_new (char, u->sendbuf_len ); u->sendbuf[0] = 0; u->sendbuf_flags = flags; } else { u->sendbuf_len += strlen( msg ) + 1; u->sendbuf = g_renew ( char, u->sendbuf, u->sendbuf_len ); } strcat( u->sendbuf, msg ); strcat( u->sendbuf, "\n" ); delay = set_getint( irc, "buddy_sendbuffer_delay" ); if( delay <= 5 ) delay *= 1000; if( u->sendbuf_timer > 0 ) g_source_remove( u->sendbuf_timer ); u->sendbuf_timer = g_timeout_add( delay, buddy_send_handler_delayed, u ); return( 1 ); } else { return( serv_send_im( irc, u, msg, flags ) ); } } int irc_privmsg( irc_t *irc, user_t *u, char *type, char *to, char *prefix, char *msg ) { char last = 0; char *s = msg, *line = msg; /* The almighty linesplitter .. woohoo!! */ while( !last ) { if( *s == '\r' && *(s+1) == '\n' ) *(s++) = 0; if( *s == '\n' ) { last = s[1] == 0; *s = 0; } else { last = s[0] == 0; } if( *s == 0 ) { if( g_strncasecmp( line, "/me ", 4 ) == 0 && ( !prefix || !*prefix ) && g_strcasecmp( type, "PRIVMSG" ) == 0 ) { irc_write( irc, ":%s!%s@%s %s %s :\001ACTION %s\001", u->nick, u->user, u->host, type, to, line + 4 ); } else { irc_write( irc, ":%s!%s@%s %s %s :%s%s", u->nick, u->user, u->host, type, to, prefix ? prefix : "", line ); } line = s + 1; } s ++; } return( 1 ); } int irc_msgfrom( irc_t *irc, char *nick, char *msg ) { user_t *u = user_find( irc, nick ); static char *prefix = NULL; if( !u ) return( 0 ); if( prefix && *prefix ) g_free( prefix ); if( !u->is_private && nick_cmp( u->nick, irc->mynick ) != 0 ) { int len = strlen( irc->nick) + 3; prefix = g_new (char, len ); g_snprintf( prefix, len, "%s%s", irc->nick, set_getstr( irc, "to_char" ) ); prefix[len-1] = 0; } else { prefix = ""; } return( irc_privmsg( irc, u, "PRIVMSG", u->is_private ? irc->nick : irc->channel, prefix, msg ) ); } int irc_noticefrom( irc_t *irc, char *nick, char *msg ) { user_t *u = user_find( irc, nick ); if( u ) return( irc_privmsg( irc, u, "NOTICE", irc->nick, "", msg ) ); else return( 0 ); } /* Returns 0 if everything seems to be okay, a number >0 when there was a timeout. The number returned is the number of seconds we received no pongs from the user. When not connected yet, we don't ping but drop the connection when the user fails to connect in IRC_LOGIN_TIMEOUT secs. */ static gboolean irc_userping( gpointer _irc ) { irc_t *irc = _irc; int rv = 0; if( irc->status < USTATUS_LOGGED_IN ) { if( gettime() > ( irc->last_pong + IRC_LOGIN_TIMEOUT ) ) rv = gettime() - irc->last_pong; } else { if( ( gettime() > ( irc->last_pong + global.conf->ping_interval ) ) && !irc->pinging ) { irc_write( irc, "PING :%s", IRC_PING_STRING ); irc->pinging = 1; } else if( gettime() > ( irc->last_pong + global.conf->ping_timeout ) ) { rv = gettime() - irc->last_pong; } } if( rv > 0 ) { irc_write( irc, "ERROR :Closing Link: Ping Timeout: %d seconds", rv ); irc_free( irc ); return FALSE; } return TRUE; }