diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | bitlbee.c | 109 | ||||
-rw-r--r-- | bitlbee.h | 3 | ||||
-rw-r--r-- | commands.c | 82 | ||||
-rw-r--r-- | commands.h | 30 | ||||
-rw-r--r-- | conf.c | 18 | ||||
-rw-r--r-- | ipc.c | 402 | ||||
-rw-r--r-- | ipc.h | 57 | ||||
-rw-r--r-- | irc.c | 756 | ||||
-rw-r--r-- | irc.h | 10 | ||||
-rw-r--r-- | irc_commands.c | 669 | ||||
-rw-r--r-- | url.c | 6 |
12 files changed, 1399 insertions, 745 deletions
@@ -9,7 +9,7 @@ -include Makefile.settings # Program variables -objects = account.o bitlbee.o commands.o conf.o crypting.o help.o ini.o irc.o log.o nick.o query.o set.o storage.o storage_text.o unix.o url.o user.o util.o +objects = account.o bitlbee.o commands.o conf.o crypting.o help.o ini.o ipc.o irc.o irc_commands.o log.o nick.o query.o set.o storage.o storage_text.o unix.o url.o user.o util.o subdirs = protocols # Expansion of variables @@ -28,46 +28,12 @@ #include "commands.h" #include "protocols/nogaim.h" #include "help.h" +#include "ipc.h" #include <signal.h> #include <stdio.h> #include <errno.h> -gboolean bitlbee_io_new_client( GIOChannel *source, GIOCondition condition, gpointer data ) -{ - size_t size = sizeof( struct sockaddr_in ); - struct sockaddr_in conn_info; - int new_socket = accept( global.listen_socket, (struct sockaddr *) &conn_info, &size ); - pid_t client_pid = 0; - - if( global.conf->runmode == RUNMODE_FORKDAEMON ) - client_pid = fork(); - - if( client_pid == 0 ) - { - log_message( LOGLVL_INFO, "Creating new connection with fd %d.", new_socket ); - irc_new( new_socket ); - - if( global.conf->runmode == RUNMODE_FORKDAEMON ) - { - /* Close the listening socket, we're a client. */ - close( global.listen_socket ); - g_source_remove( global.listen_watch_source_id ); - } - } - else - { - /* We don't need this one, only the client does. */ - close( new_socket ); - - /* Or maybe we didn't even get a child process... */ - if( client_pid == -1 ) - log_message( LOGLVL_ERROR, "Failed to fork() subprocess for client: %s", strerror( errno ) ); - } - - return TRUE; -} - - +gboolean bitlbee_io_new_client( GIOChannel *source, GIOCondition condition, gpointer data ); int bitlbee_daemon_init() { @@ -89,6 +55,10 @@ int bitlbee_daemon_init() return( -1 ); } + /* TIME_WAIT (?) sucks.. */ + i = 1; + setsockopt( global.listen_socket, SOL_SOCKET, SO_REUSEADDR, &i, sizeof( i ) ); + #ifdef IPV6 listen_addr.sin6_family = AF_INETx; listen_addr.sin6_port = htons( global.conf->port ); @@ -256,6 +226,73 @@ gboolean bitlbee_io_current_client_write( GIOChannel *source, GIOCondition condi } } +gboolean bitlbee_io_new_client( GIOChannel *source, GIOCondition condition, gpointer data ) +{ + size_t size = sizeof( struct sockaddr_in ); + struct sockaddr_in conn_info; + int new_socket = accept( global.listen_socket, (struct sockaddr *) &conn_info, &size ); + pid_t client_pid = 0; + + if( global.conf->runmode == RUNMODE_FORKDAEMON ) + { + int fds[2]; + + if( socketpair( AF_UNIX, SOCK_STREAM, 0, fds ) == -1 ) + { + log_message( LOGLVL_WARNING, "Could not create IPC socket for client: %s", strerror( errno ) ); + fds[0] = fds[1] = -1; + } + + sock_make_nonblocking( fds[0] ); + sock_make_nonblocking( fds[1] ); + + client_pid = fork(); + + if( client_pid > 0 && fds[0] != -1 ) + { + struct bitlbee_child *child; + + child = g_new0( struct bitlbee_child, 1 ); + child->pid = client_pid; + child->ipc_fd = fds[0]; + child->ipc_inpa = gaim_input_add( child->ipc_fd, GAIM_INPUT_READ, ipc_master_read, child ); + child_list = g_slist_append( child_list, child ); + + log_message( LOGLVL_INFO, "Creating new subprocess with pid %d.", client_pid ); + + /* Close some things we don't need in the parent process. */ + close( new_socket ); + close( fds[1] ); + } + else if( client_pid == 0 ) + { + irc_t *irc; + + /* Close the listening socket, we're a client. */ + close( global.listen_socket ); + g_source_remove( global.listen_watch_source_id ); + + /* Make the connection. */ + irc = irc_new( new_socket ); + + /* We can store the IPC fd there now. */ + global.listen_socket = fds[1]; + global.listen_watch_source_id = gaim_input_add( fds[1], GAIM_INPUT_READ, ipc_child_read, irc ); + + close( fds[0] ); + + ipc_master_free_all(); + } + } + else + { + log_message( LOGLVL_INFO, "Creating new connection with fd %d.", new_socket ); + irc_new( new_socket ); + } + + return TRUE; +} + void bitlbee_shutdown( gpointer data ) { /* Try to save data for all active connections (if desired). */ @@ -111,7 +111,8 @@ extern char *CONF_FILE; #include "query.h" #include "sock.h" -typedef struct global_t { +typedef struct global { + /* In forked mode, child processes store the fd of the IPC socket here. */ int listen_socket; gint listen_watch_source_id; help_t *help; @@ -31,29 +31,6 @@ #include <string.h> -const command_t commands[] = { - { "help", 0, cmd_help }, - { "identify", 1, cmd_identify }, - { "register", 1, cmd_register }, - { "drop", 1, cmd_drop }, - { "account", 1, cmd_account }, - { "add", 2, cmd_add }, - { "info", 1, cmd_info }, - { "rename", 2, cmd_rename }, - { "remove", 1, cmd_remove }, - { "block", 1, cmd_block }, - { "allow", 1, cmd_allow }, - { "save", 0, cmd_save }, - { "set", 0, cmd_set }, - { "yes", 0, cmd_yesno }, - { "no", 0, cmd_yesno }, - { "blist", 0, cmd_blist }, - { "nick", 1, cmd_nick }, - { "import_buddies", 1, cmd_import_buddies }, - { "qlist", 0, cmd_qlist }, - { NULL } -}; - int root_command_string( irc_t *irc, user_t *u, char *command, int flags ) { char *cmd[IRC_MAX_ARGS]; @@ -113,7 +90,7 @@ int root_command( irc_t *irc, char *cmd[] ) return( 1 ); } -int cmd_help( irc_t *irc, char **cmd ) +static int cmd_help( irc_t *irc, char **cmd ) { char param[80]; int i; @@ -142,7 +119,7 @@ int cmd_help( irc_t *irc, char **cmd ) } } -int cmd_identify( irc_t *irc, char **cmd ) +static int cmd_identify( irc_t *irc, char **cmd ) { storage_status_t status = storage_load( irc->nick, cmd[1], irc ); @@ -165,7 +142,7 @@ int cmd_identify( irc_t *irc, char **cmd ) return( 0 ); } -int cmd_register( irc_t *irc, char **cmd ) +static int cmd_register( irc_t *irc, char **cmd ) { if( global.conf->authmode == AUTHMODE_REGISTERED ) { @@ -192,7 +169,7 @@ int cmd_register( irc_t *irc, char **cmd ) return( 0 ); } -int cmd_drop( irc_t *irc, char **cmd ) +static int cmd_drop( irc_t *irc, char **cmd ) { storage_status_t status; @@ -216,7 +193,7 @@ int cmd_drop( irc_t *irc, char **cmd ) } } -int cmd_account( irc_t *irc, char **cmd ) +static int cmd_account( irc_t *irc, char **cmd ) { account_t *a; @@ -376,7 +353,7 @@ int cmd_account( irc_t *irc, char **cmd ) return( 1 ); } -int cmd_add( irc_t *irc, char **cmd ) +static int cmd_add( irc_t *irc, char **cmd ) { account_t *a; @@ -416,7 +393,7 @@ int cmd_add( irc_t *irc, char **cmd ) return( 0 ); } -int cmd_info( irc_t *irc, char **cmd ) +static int cmd_info( irc_t *irc, char **cmd ) { struct gaim_connection *gc; account_t *a; @@ -453,7 +430,7 @@ int cmd_info( irc_t *irc, char **cmd ) return( 0 ); } -int cmd_rename( irc_t *irc, char **cmd ) +static int cmd_rename( irc_t *irc, char **cmd ) { user_t *u; @@ -494,7 +471,7 @@ int cmd_rename( irc_t *irc, char **cmd ) return( 0 ); } -int cmd_remove( irc_t *irc, char **cmd ) +static int cmd_remove( irc_t *irc, char **cmd ) { user_t *u; char *s; @@ -516,7 +493,7 @@ int cmd_remove( irc_t *irc, char **cmd ) return( 0 ); } -int cmd_block( irc_t *irc, char **cmd ) +static int cmd_block( irc_t *irc, char **cmd ) { struct gaim_connection *gc; account_t *a; @@ -557,7 +534,7 @@ int cmd_block( irc_t *irc, char **cmd ) return( 0 ); } -int cmd_allow( irc_t *irc, char **cmd ) +static int cmd_allow( irc_t *irc, char **cmd ) { struct gaim_connection *gc; account_t *a; @@ -599,7 +576,7 @@ int cmd_allow( irc_t *irc, char **cmd ) return( 0 ); } -int cmd_yesno( irc_t *irc, char **cmd ) +static int cmd_yesno( irc_t *irc, char **cmd ) { query_t *q = NULL; int numq = 0; @@ -639,7 +616,7 @@ int cmd_yesno( irc_t *irc, char **cmd ) return( 1 ); } -int cmd_set( irc_t *irc, char **cmd ) +static int cmd_set( irc_t *irc, char **cmd ) { if( cmd[1] && cmd[2] ) { @@ -665,7 +642,7 @@ int cmd_set( irc_t *irc, char **cmd ) return( 0 ); } -int cmd_save( irc_t *irc, char **cmd ) +static int cmd_save( irc_t *irc, char **cmd ) { if( storage_save( irc, TRUE ) == STORAGE_OK ) irc_usermsg( irc, "Configuration saved" ); @@ -675,7 +652,7 @@ int cmd_save( irc_t *irc, char **cmd ) return( 0 ); } -int cmd_blist( irc_t *irc, char **cmd ) +static int cmd_blist( irc_t *irc, char **cmd ) { int online = 0, away = 0, offline = 0; user_t *u; @@ -721,7 +698,7 @@ int cmd_blist( irc_t *irc, char **cmd ) return( 0 ); } -int cmd_nick( irc_t *irc, char **cmd ) +static int cmd_nick( irc_t *irc, char **cmd ) { account_t *a; @@ -757,7 +734,7 @@ int cmd_nick( irc_t *irc, char **cmd ) return( 1 ); } -int cmd_qlist( irc_t *irc, char **cmd ) +static int cmd_qlist( irc_t *irc, char **cmd ) { query_t *q = irc->queries; int num; @@ -779,7 +756,7 @@ int cmd_qlist( irc_t *irc, char **cmd ) return( 0 ); } -int cmd_import_buddies( irc_t *irc, char **cmd ) +static int cmd_import_buddies( irc_t *irc, char **cmd ) { struct gaim_connection *gc; account_t *a; @@ -831,3 +808,26 @@ int cmd_import_buddies( irc_t *irc, char **cmd ) return( 0 ); } + +const command_t commands[] = { + { "help", 0, cmd_help, 0 }, + { "identify", 1, cmd_identify, 0 }, + { "register", 1, cmd_register, 0 }, + { "drop", 1, cmd_drop, 0 }, + { "account", 1, cmd_account, 0 }, + { "add", 2, cmd_add, 0 }, + { "info", 1, cmd_info, 0 }, + { "rename", 2, cmd_rename, 0 }, + { "remove", 1, cmd_remove, 0 }, + { "block", 1, cmd_block, 0 }, + { "allow", 1, cmd_allow, 0 }, + { "save", 0, cmd_save, 0 }, + { "set", 0, cmd_set, 0 }, + { "yes", 0, cmd_yesno, 0 }, + { "no", 0, cmd_yesno, 0 }, + { "blist", 0, cmd_blist, 0 }, + { "nick", 1, cmd_nick, 0 }, + { "import_buddies", 1, cmd_import_buddies, 0 }, + { "qlist", 0, cmd_qlist, 0 }, + { NULL } +}; @@ -28,33 +28,21 @@ #include "bitlbee.h" -typedef struct command_t +typedef struct command { char *command; int required_parameters; int (*execute)(irc_t *, char **args); + int flags; } command_t; -int cmd_account( irc_t *irc, char **cmd ); -int cmd_help( irc_t *irc, char **args); -int cmd_info( irc_t *irc, char **args); -int cmd_add( irc_t *irc, char **args) ; -int cmd_rename( irc_t *irc, char **args ); -int cmd_remove( irc_t *irc, char **args ); -int cmd_block( irc_t *irc, char **args ); -int cmd_allow( irc_t *irc, char **args ); -int cmd_save( irc_t *irc, char **args ); -int cmd_set( irc_t *irc, char **args ); -int cmd_yesno( irc_t *irc, char **args ); -int cmd_identify( irc_t *irc, char **args ); -int cmd_register( irc_t *irc, char **args ); -int cmd_drop( irc_t *irc, char **args ); -int cmd_blist( irc_t *irc, char **cmd ); -int cmd_nick( irc_t *irc, char **cmd ); -int cmd_qlist( irc_t *irc, char **cmd ); -int cmd_import_buddies( irc_t *irc, char **cmd ); -int cmd_dump( irc_t *irc, char **cmd ); - extern const command_t commands[]; +#define IRC_CMD_PRE_LOGIN 1 +#define IRC_CMD_LOGGED_IN 2 +#define IRC_CMD_OPER_ONLY 4 +#define IRC_CMD_TO_MASTER 8 + +#define IPC_CMD_TO_CHILDREN 1 + #endif @@ -63,6 +63,7 @@ conf_t *conf_load( int argc, char *argv[] ) conf->motdfile = g_strdup( ETCDIR "/motd.txt" ); conf->ping_interval = 180; conf->ping_timeout = 300; + proxytype = 0; i = conf_loadini( conf, CONF_FILE ); if( i == 0 ) @@ -75,7 +76,8 @@ conf_t *conf_load( int argc, char *argv[] ) fprintf( stderr, "Warning: Unable to read configuration file `%s'.\n", CONF_FILE ); } - while( ( opt = getopt( argc, argv, "i:p:nvIDFc:d:h" ) ) >= 0 ) + while( argc > 0 && ( opt = getopt( argc, argv, "i:p:nvIDFc:d:h" ) ) >= 0 ) + /* ^^^^ Just to make sure we skip this step from the REHASH handler. */ { if( opt == 'i' ) { @@ -91,15 +93,15 @@ conf_t *conf_load( int argc, char *argv[] ) conf->port = i; } else if( opt == 'n' ) - conf->nofork=1; + conf->nofork = 1; else if( opt == 'v' ) - conf->verbose=1; + conf->verbose = 1; else if( opt == 'I' ) - conf->runmode=RUNMODE_INETD; + conf->runmode = RUNMODE_INETD; else if( opt == 'D' ) - conf->runmode=RUNMODE_DAEMON; + conf->runmode = RUNMODE_DAEMON; else if( opt == 'F' ) - conf->runmode=RUNMODE_FORKDAEMON; + conf->runmode = RUNMODE_FORKDAEMON; else if( opt == 'c' ) { if( strcmp( CONF_FILE, optarg ) != 0 ) @@ -107,6 +109,10 @@ conf_t *conf_load( int argc, char *argv[] ) g_free( CONF_FILE ); CONF_FILE = g_strdup( optarg ); g_free( conf ); + /* Re-evaluate arguments. Don't use this option twice, + you'll end up in an infinite loop! Hope this trick + works with all libcs BTW.. */ + optind = 1; return( conf_load( argc, argv ) ); } } @@ -0,0 +1,402 @@ + /********************************************************************\ + * BitlBee -- An IRC to other IM-networks gateway * + * * + * Copyright 2002-2004 Wilmer van der Gaast and others * + \********************************************************************/ + +/* IPC - communication between BitlBee processes */ + +/* + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License with + the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, + Suite 330, Boston, MA 02111-1307 USA +*/ + +#define BITLBEE_CORE +#include "bitlbee.h" +#include "ipc.h" +#include "commands.h" + +GSList *child_list = NULL; + + +static int ipc_master_cmd_client( irc_t *data, char **cmd ) +{ + struct bitlbee_child *child = (void*) data; + + if( child ) + { + child->host = g_strdup( cmd[1] ); + child->nick = g_strdup( cmd[2] ); + child->realname = g_strdup( cmd[3] ); + } + + ipc_to_children_str( "OPERMSG :Client connecting (PID=%d): %s@%s (%s)\r\n", + child ? child->pid : -1, cmd[2], cmd[1], cmd[3] ); + + return 1; +} + +static int ipc_master_cmd_die( irc_t *data, char **cmd ) +{ + if( global.conf->runmode == RUNMODE_FORKDAEMON ) + ipc_to_children_str( "DIE\r\n" ); + + bitlbee_shutdown( NULL ); + + return 1; +} + +int ipc_master_cmd_rehash( irc_t *data, char **cmd ) +{ + runmode_t oldmode; + + oldmode = global.conf->runmode; + + g_free( global.conf ); + global.conf = conf_load( 0, NULL ); + + if( global.conf->runmode != oldmode ) + { + log_message( LOGLVL_WARNING, "Can't change RunMode setting at runtime, restoring original setting" ); + global.conf->runmode = oldmode; + } + + if( global.conf->runmode == RUNMODE_FORKDAEMON ) + ipc_to_children( cmd ); + + return 1; +} + +static const command_t ipc_master_commands[] = { + { "client", 3, ipc_master_cmd_client, 0 }, + { "die", 0, ipc_master_cmd_die, 0 }, + { "wallops", 1, NULL, IPC_CMD_TO_CHILDREN }, + { "lilo", 1, NULL, IPC_CMD_TO_CHILDREN }, + { "opermsg", 1, NULL, IPC_CMD_TO_CHILDREN }, + { "rehash", 0, ipc_master_cmd_rehash, 0 }, + { "kill", 2, NULL, IPC_CMD_TO_CHILDREN }, + { NULL } +}; + + +static int ipc_child_cmd_die( irc_t *irc, char **cmd ) +{ + if( irc->status >= USTATUS_LOGGED_IN ) + irc_write( irc, "ERROR :Operator requested server shutdown, bye bye!" ); + + irc_abort( irc ); + + return 1; +} + +static int ipc_child_cmd_wallops( irc_t *irc, char **cmd ) +{ + if( irc->status < USTATUS_LOGGED_IN ) + return 1; + + if( strchr( irc->umode, 'w' ) ) + irc_write( irc, ":%s WALLOPS :%s", irc->myhost, cmd[1] ); + + return 1; +} + +static int ipc_child_cmd_lilo( irc_t *irc, char **cmd ) +{ + if( irc->status < USTATUS_LOGGED_IN ) + return 1; + + if( strchr( irc->umode, 's' ) ) + irc_write( irc, ":%s NOTICE %s :%s", irc->myhost, irc->nick, cmd[1] ); + + return 1; +} + +static int ipc_child_cmd_opermsg( irc_t *irc, char **cmd ) +{ + if( irc->status < USTATUS_LOGGED_IN ) + return 1; + + if( strchr( irc->umode, 'o' ) ) + irc_write( irc, ":%s NOTICE %s :*** OperMsg *** %s", irc->myhost, irc->nick, cmd[1] ); + + return 1; +} + +static int ipc_child_cmd_rehash( irc_t *irc, char **cmd ) +{ + runmode_t oldmode; + + oldmode = global.conf->runmode; + + g_free( global.conf ); + global.conf = conf_load( 0, NULL ); + + global.conf->runmode = oldmode; + + return 1; +} + +static int ipc_child_cmd_kill( irc_t *irc, char **cmd ) +{ + if( irc->status < USTATUS_LOGGED_IN ) + return 1; + + if( nick_cmp( cmd[1], irc->nick ) != 0 ) + return 1; /* It's not for us. */ + + irc_write( irc, ":%s!%s@%s KILL %s :%s", irc->mynick, irc->mynick, irc->myhost, irc->nick, cmd[2] ); + irc_abort( irc ); + /* g_io_channel_close( irc->io_channel ); */ + + return 0; +} + +static const command_t ipc_child_commands[] = { + { "die", 0, ipc_child_cmd_die, 0 }, + { "wallops", 1, ipc_child_cmd_wallops, 0 }, + { "lilo", 1, ipc_child_cmd_lilo, 0 }, + { "opermsg", 1, ipc_child_cmd_opermsg, 0 }, + { "rehash", 0, ipc_child_cmd_rehash, 0 }, + { "kill", 2, ipc_child_cmd_kill, 0 }, + { NULL } +}; + + +static void ipc_command_exec( void *data, char **cmd, const command_t *commands ) +{ + int i; + + if( !cmd[0] ) + return; + + for( i = 0; commands[i].command; i ++ ) + if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 ) + { + if( commands[i].flags & IPC_CMD_TO_CHILDREN ) + ipc_to_children( cmd ); + else + commands[i].execute( data, cmd ); + + return; + } +} + +static char *ipc_readline( int fd ) +{ + char *buf, *eol; + int size; + + buf = g_new0( char, 513 ); + + /* Because this is internal communication, it should be pretty safe + to just peek at the message, find its length (by searching for the + end-of-line) and then just read that message. With internal + sockets and limites message length, messages should always be + complete. Saves us quite a lot of code and buffering. */ + size = recv( fd, buf, 512, MSG_PEEK ); + if( size == 0 || ( size < 0 && !sockerr_again() ) ) + return NULL; + else if( size < 0 ) /* && sockerr_again() */ + return( g_strdup( "" ) ); + else + buf[size] = 0; + + eol = strstr( buf, "\r\n" ); + if( eol == NULL ) + return NULL; + else + size = eol - buf + 2; + + g_free( buf ); + buf = g_new0( char, size + 1 ); + + if( recv( fd, buf, size, 0 ) != size ) + return NULL; + else + buf[size-2] = 0; + + return buf; +} + +void ipc_master_read( gpointer data, gint source, GaimInputCondition cond ) +{ + char *buf, **cmd; + + if( ( buf = ipc_readline( source ) ) ) + { + cmd = irc_parse_line( buf ); + if( cmd ) + ipc_command_exec( data, cmd, ipc_master_commands ); + } + else + { + GSList *l; + struct bitlbee_child *c; + + for( l = child_list; l; l = l->next ) + { + c = l->data; + if( c->ipc_fd == source ) + { + ipc_master_free_one( c ); + child_list = g_slist_remove( child_list, c ); + break; + } + } + } +} + +void ipc_child_read( gpointer data, gint source, GaimInputCondition cond ) +{ + char *buf, **cmd; + + if( ( buf = ipc_readline( source ) ) ) + { + cmd = irc_parse_line( buf ); + if( cmd ) + ipc_command_exec( data, cmd, ipc_child_commands ); + } + else + { + gaim_input_remove( global.listen_watch_source_id ); + close( global.listen_socket ); + + global.listen_socket = -1; + } +} + +void ipc_to_master( char **cmd ) +{ + if( global.conf->runmode == RUNMODE_FORKDAEMON ) + { + char *s = irc_build_line( cmd ); + ipc_to_master_str( "%s", s ); + g_free( s ); + } + else if( global.conf->runmode == RUNMODE_DAEMON ) + { + ipc_command_exec( NULL, cmd, ipc_master_commands ); + } +} + +void ipc_to_master_str( char *format, ... ) +{ + char *msg_buf; + va_list params; + + va_start( params, format ); + msg_buf = g_strdup_vprintf( format, params ); + va_end( params ); + + if( strlen( msg_buf ) > 512 ) + { + /* Don't send it, it's too long... */ + } + else if( global.conf->runmode == RUNMODE_FORKDAEMON ) + { + write( global.listen_socket, msg_buf, strlen( msg_buf ) ); + } + else if( global.conf->runmode == RUNMODE_DAEMON ) + { + char **cmd, *s; + + if( ( s = strchr( msg_buf, '\r' ) ) ) + *s = 0; + + cmd = irc_parse_line( msg_buf ); + ipc_command_exec( NULL, cmd, ipc_master_commands ); + g_free( cmd ); + } + + g_free( msg_buf ); +} + +void ipc_to_children( char **cmd ) +{ + if( global.conf->runmode == RUNMODE_FORKDAEMON ) + { + char *msg_buf = irc_build_line( cmd ); + ipc_to_children_str( "%s", msg_buf ); + g_free( msg_buf ); + } + else if( global.conf->runmode == RUNMODE_DAEMON ) + { + GSList *l; + + for( l = irc_connection_list; l; l = l->next ) + ipc_command_exec( l->data, cmd, ipc_child_commands ); + } +} + +void ipc_to_children_str( char *format, ... ) +{ + char *msg_buf; + va_list params; + + va_start( params, format ); + msg_buf = g_strdup_vprintf( format, params ); + va_end( params ); + + if( strlen( msg_buf ) > 512 ) + { + /* Don't send it, it's too long... */ + } + else if( global.conf->runmode == RUNMODE_FORKDAEMON ) + { + int msg_len = strlen( msg_buf ); + GSList *l; + + for( l = child_list; l; l = l->next ) + { + struct bitlbee_child *c = l->data; + write( c->ipc_fd, msg_buf, msg_len ); + } + } + else if( global.conf->runmode == RUNMODE_DAEMON ) + { + char **cmd, *s; + + if( ( s = strchr( msg_buf, '\r' ) ) ) + *s = 0; + + cmd = irc_parse_line( msg_buf ); + ipc_to_children( cmd ); + g_free( cmd ); + } + + g_free( msg_buf ); +} + +void ipc_master_free_one( struct bitlbee_child *c ) +{ + gaim_input_remove( c->ipc_inpa ); + closesocket( c->ipc_fd ); + + g_free( c->host ); + g_free( c->nick ); + g_free( c->realname ); + g_free( c ); +} + +void ipc_master_free_all() +{ + GSList *l; + + for( l = child_list; l; l = l->next ) + ipc_master_free_one( l->data ); + + g_slist_free( child_list ); + child_list = NULL; +} @@ -0,0 +1,57 @@ + /********************************************************************\ + * BitlBee -- An IRC to other IM-networks gateway * + * * + * Copyright 2002-2004 Wilmer van der Gaast and others * + \********************************************************************/ + +/* IPC - communication between BitlBee processes */ + +/* + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License with + the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, + Suite 330, Boston, MA 02111-1307 USA +*/ + +#define BITLBEE_CORE +#include "bitlbee.h" + + +struct bitlbee_child +{ + pid_t pid; + int ipc_fd; + gint ipc_inpa; + + char *host; + char *nick; + char *realname; +}; + + +void ipc_master_read( gpointer data, gint source, GaimInputCondition cond ); +void ipc_child_read( gpointer data, gint source, GaimInputCondition cond ); + +void ipc_master_free_one( struct bitlbee_child *child ); +void ipc_master_free_all(); + +void ipc_to_master( char **cmd ); +void ipc_to_master_str( char *format, ... ); +void ipc_to_children( char **cmd ); +void ipc_to_children_str( char *format, ... ); + +/* We need this function in inetd mode, so let's just make it non-static. */ +int ipc_master_cmd_rehash( irc_t *data, char **cmd ); + + +extern GSList *child_list; @@ -26,6 +26,7 @@ #define BITLBEE_CORE #include "bitlbee.h" #include "crypting.h" +#include "ipc.h" static gboolean irc_userping( gpointer _irc ); @@ -203,7 +204,7 @@ static gboolean irc_free_userhash( gpointer key, gpointer value, gpointer data ) } /* Because we have no garbage collection, this is quite annoying */ -void irc_free( irc_t * irc ) +void irc_free(irc_t * irc) { account_t *account, *accounttmp; user_t *user, *usertmp; @@ -217,6 +218,8 @@ void irc_free( irc_t * irc ) if( storage_save( irc, TRUE ) != STORAGE_OK ) irc_usermsg( irc, "Error while saving settings!" ); + closesocket( irc->fd ); + if( irc->ping_source_id > 0 ) g_source_remove( irc->ping_source_id ); g_source_remove( irc->r_watch_source_id ); @@ -337,30 +340,45 @@ void irc_setpass (irc_t *irc, const char *pass) int irc_process( irc_t *irc ) { - char **lines, *temp; + char **lines, *temp, **cmd; int i; - if( irc->readbuffer != NULL ) { - lines = irc_tokenize(irc->readbuffer ); - for( i = 0; *lines[i] != '\0'; i++ ) { - if( lines[i+1] == NULL ) { + 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++; + i ++; break; } - if (!irc_process_line(irc, lines[i])) { + + if( ( cmd = irc_parse_line( lines[i] ) ) == NULL ) + continue; + if( !irc_exec( irc, cmd ) ) + { + g_free( cmd ); g_free( lines ); return 0; } + + g_free( cmd ); } - if(lines[i]!=NULL) { - g_free(irc->readbuffer); - irc->readbuffer=NULL; + + if( lines[i] != NULL ) + { + g_free( irc->readbuffer ); + irc->readbuffer = NULL; } + g_free( lines ); } + return 1; } @@ -370,562 +388,128 @@ char **irc_tokenize( char *buffer ) 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++; + 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); + lines = g_new( char *, j + 1 ); /* NULL terminate our list. */ - lines[j]=NULL; + lines[j] = NULL; - lines[0]=buffer; + 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; + for( i = 0, j = 0; buffer[i] != '\0'; i ++) + { + if( buffer[i] == '\n' ) + { + buffer[i] = '\0'; + + if( i > 0 && buffer[i-1] == '\r' ) + buffer[i-1] = '\0'; + if( buffer[i+1] != '\r' && buffer[i+1] != '\n' ) + lines[++j] = buffer + i + 1; } } - - return(lines); + + return( lines ); } -int irc_process_line( irc_t *irc, char *line ) +char **irc_parse_line( 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; + if( line[0] == ':' ) + { + for( i = 0; line[i] != ' '; i ++ ); + line = line + i; } - for(i=0; line[i]==32; i++); - line=line+i; - + for( i = 0; line[i] == ' '; 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; - } + if( line[0] == '\0') + return NULL; + + /* Count the number of char **cmd elements we're going to need. */ + j = 1; + for( i = 0; line[i] != '\0'; i ++ ) + { + if( line[i] == ' ' ) + { + j ++; + if( line[i+1] == ':' ) + break; + } } /* Allocate the space we need. */ - cmd=g_new(char *, j+1); - cmd[j]=NULL; + 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 ) + cmd[0] = line; + for( i = 0, j = 0; line[i] != '\0'; i ++ ) { - if( g_strcasecmp( cmd[0], "PASS" ) == 0 ) + if( line[i] == ' ' ) { - if( !cmd[1] ) - { - irc_reply( irc, 461, "%s :Need more parameters", cmd[0] ); - } - else if( strcmp( cmd[1], (global.conf)->auth_pass ) == 0 ) - { - irc->status = USTATUS_AUTHORIZED; - } - else + line[i] = '\0'; + cmd[++j] = line + i + 1; + + if( line[i+1] == ':' ) { - irc_reply( irc, 464, ":Nope, maybe you should try it again..." ); + cmd[j] ++; + break; } } - 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 ); - } + return cmd; +} + +char *irc_build_line( char **cmd ) +{ + int i, len; + char *s; - if( !irc->user || !irc->nick ) - { - irc_reply( irc, 451, ":Register first" ); - return( 1 ); - } + if( cmd[0] == NULL ) + return NULL; - 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], "OPER" ) == 0 ) - { - if( !cmd[2] ) - irc_reply( irc, 461, "%s :Need more parameters", cmd[0] ); - else if( strcmp( cmd[2], global.conf->oper_pass ) == 0 ) - irc_umode_set( irc, "+o", 1 ); - // else - /* FIXME/TODO: Find out which reply to send now. */ - } - 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, cmd[2], 0 ); - } - 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 ) + len = 1; + for( i = 0; cmd[i]; i ++ ) + len += strlen( cmd[i] ) + 1; + + if( strchr( cmd[i-1], ' ' ) != NULL ) + len ++; + + s = g_new0( char, len + 1 ); + for( i = 0; cmd[i]; i ++ ) { - 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 ); + if( cmd[i+1] == NULL && strchr( cmd[i], ' ' ) != NULL ) + strcat( s, ":" ); - for( s = irc->set; s; s = s->next ) - irc_privmsg( irc, u, "NOTICE", irc->nick, "COMPLETIONS set ", s->key ); + strcat( s, cmd[i] ); - 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--------------------" ); + if( cmd[i+1] ) + strcat( s, " " ); } + strcat( s, "\r\n" ); - return( 1 ); + return s; } void irc_reply( irc_t *irc, int code, char *format, ... ) @@ -1084,35 +668,26 @@ void irc_names( irc_t *irc, char *channel ) irc_reply( irc, 366, "%s :End of /NAMES list", channel ); } -void irc_who( irc_t *irc, char *channel ) +int irc_check_login( irc_t *irc ) { - 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( irc->user && irc->nick ) + { + if( global.conf->authmode == AUTHMODE_CLOSED && irc->status < USTATUS_AUTHORIZED ) { - 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; + irc_reply( irc, 464, ":This server is password-protected." ); + return 0; } - else if( ( c = conv_findchannel( channel ) ) ) - for( l = c->in_room; l; l = l->next ) + else { - 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 ); + irc_login( irc ); + return 1; } - 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:"**" ); + } + else + { + /* More information needed. */ + return 0; + } } void irc_login( irc_t *irc ) @@ -1147,11 +722,13 @@ void irc_login( irc_t *irc ) 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." ); + if( global.conf->runmode == RUNMODE_FORKDAEMON || global.conf->runmode == RUNMODE_DAEMON ) + ipc_to_master_str( "CLIENT %s %s :%s\r\n", irc->host, irc->nick, irc->realname ); + irc->status = USTATUS_LOGGED_IN; } @@ -1224,34 +801,6 @@ void irc_topic( irc_t *irc, char *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 *s, int allow_priv ) { /* allow_priv: Set to 0 if s contains user input, 1 if you want @@ -1281,47 +830,6 @@ void irc_umode_set( irc_t *irc, char *s, int allow_priv ) 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 ); @@ -1375,22 +883,6 @@ void irc_kill( irc_t *irc, user_t *u ) 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; @@ -32,7 +32,7 @@ #define IRC_LOGIN_TIMEOUT 60 #define IRC_PING_STRING "PinglBee" -#define UMODES "ias" +#define UMODES "iasw" #define UMODES_PRIV "Ro" #define CMODES "nt" #define CMODE "t" @@ -40,11 +40,11 @@ typedef enum { - USTATUS_OFFLINE, + USTATUS_OFFLINE = 0, USTATUS_AUTHORIZED, USTATUS_LOGGED_IN, USTATUS_IDENTIFIED, - USTATUS_SHUTDOWN + USTATUS_SHUTDOWN = -1 } irc_status_t; typedef struct channel @@ -109,7 +109,8 @@ void irc_free( irc_t *irc ); int irc_exec( irc_t *irc, char **cmd ); int irc_process( irc_t *irc ); -int irc_process_line( irc_t *irc, char *line ); +char **irc_parse_line( char *line ); +char *irc_build_line( char **cmd ); void irc_vawrite( irc_t *irc, char *format, va_list params ); void irc_write( irc_t *irc, char *format, ... ); @@ -119,6 +120,7 @@ G_MODULE_EXPORT int irc_usermsg( irc_t *irc, char *format, ... ); char **irc_tokenize( char *buffer ); void irc_login( irc_t *irc ); +int irc_check_login( irc_t *irc ); void irc_motd( irc_t *irc ); void irc_names( irc_t *irc, char *channel ); void irc_topic( irc_t *irc, char *channel ); diff --git a/irc_commands.c b/irc_commands.c new file mode 100644 index 00000000..e31a92e8 --- /dev/null +++ b/irc_commands.c @@ -0,0 +1,669 @@ + /********************************************************************\ + * BitlBee -- An IRC to other IM-networks gateway * + * * + * Copyright 2002-2006 Wilmer van der Gaast and others * + \********************************************************************/ + +/* IRC commands */ + +/* + This program is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License with + the Debian GNU/Linux distribution in /usr/share/common-licenses/GPL; + if not, write to the Free Software Foundation, Inc., 59 Temple Place, + Suite 330, Boston, MA 02111-1307 USA +*/ + +#define BITLBEE_CORE +#include "bitlbee.h" +#include "ipc.h" + +static int irc_cmd_pass( irc_t *irc, char **cmd ) +{ + if( global.conf->auth_pass && strcmp( cmd[1], global.conf->auth_pass ) == 0 ) + { + irc->status = USTATUS_AUTHORIZED; + irc_check_login( irc ); + } + else + { + irc_reply( irc, 464, ":Incorrect password" ); + } + + return( 1 ); +} + +static int irc_cmd_user( irc_t *irc, char **cmd ) +{ + irc->user = g_strdup( cmd[1] ); + irc->realname = g_strdup( cmd[4] ); + + irc_check_login( irc ); + + return( 1 ); +} + +static int irc_cmd_nick( irc_t *irc, char **cmd ) +{ + 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] ); + + irc_check_login( irc ); + } + + return( 1 ); +} + +static int irc_cmd_quit( irc_t *irc, char **cmd ) +{ + irc_write( irc, "ERROR :%s%s", cmd[1]?"Quit: ":"", cmd[1]?cmd[1]:"Client Quit" ); + /* g_io_channel_close( irc->io_channel ); */ + + return( 0 ); +} + +static int irc_cmd_ping( irc_t *irc, char **cmd ) +{ + irc_write( irc, ":%s PONG %s :%s", irc->myhost, irc->myhost, cmd[1]?cmd[1]:irc->myhost ); + + return( 1 ); +} + +static int irc_cmd_oper( irc_t *irc, char **cmd ) +{ + if( global.conf->oper_pass && strcmp( cmd[2], global.conf->oper_pass ) == 0 ) + { + irc_umode_set( irc, "+o", 1 ); + irc_reply( irc, 381, ":Password accepted" ); + } + else + { + irc_reply( irc, 432, ":Incorrect password" ); + } + + return( 1 ); +} + +static int irc_cmd_mode( irc_t *irc, char **cmd ) +{ + 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, cmd[2], 0 ); + } + else + irc_reply( irc, 502, ":Don't touch their modes" ); + } + + return( 1 ); +} + +static int irc_cmd_names( irc_t *irc, char **cmd ) +{ + irc_names( irc, cmd[1]?cmd[1]:irc->channel ); + + return( 1 ); +} + +static int irc_cmd_part( irc_t *irc, char **cmd ) +{ + struct conversation *c; + + 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] ); + } + + return( 1 ); +} + +static int irc_cmd_join( irc_t *irc, char **cmd ) +{ + 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 if( u ) + { + irc_reply( irc, 403, "%s :Groupchats are not possible with %s", cmd[1], cmd[1]+1 ); + } + else + { + irc_reply( irc, 403, "%s :No such nick", cmd[1] ); + } + } + else + { + irc_reply( irc, 403, "%s :No such channel", cmd[1] ); + } + } + + return( 1 ); +} + +static int irc_cmd_invite( irc_t *irc, char **cmd ) +{ + char *nick = cmd[1], *channel = cmd[2]; + 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( 1 ); + } + + irc_reply( irc, 482, "%s :Invite impossible; User/Channel non-existent or incompatible", channel ); + + return( 1 ); +} + +static int irc_cmd_privmsg( irc_t *irc, char **cmd ) +{ + 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 ); + } + + return( 1 ); +} + +static int irc_cmd_who( irc_t *irc, char **cmd ) +{ + char *channel = cmd[1]; + 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:"**" ); + + return( 1 ); +} + +static int irc_cmd_userhost( irc_t *irc, char **cmd ) +{ + user_t *u; + int i; + + /* [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 ); + } + + return( 1 ); +} + +static int irc_cmd_ison( irc_t *irc, char **cmd ) +{ + user_t *u; + char buff[IRC_MAX_LINE]; + int lenleft, i; + + 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'; + + irc_reply( irc, 303, ":%s", buff ); + + return( 1 ); +} + +static int irc_cmd_watch( irc_t *irc, char **cmd ) +{ + int i; + + /* 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" ); + } + } + } + + return( 1 ); +} + +static int irc_cmd_topic( irc_t *irc, char **cmd ) +{ + if( cmd[2] ) + irc_reply( irc, 482, "%s :Cannot change topic", cmd[1] ); + else + irc_topic( irc, cmd[1] ); + + return( 1 ); +} + +static int irc_cmd_away( irc_t *irc, char **cmd ) +{ + user_t *u = user_find( irc, irc->nick ); + GSList *c = get_connections(); + char *away = cmd[1]; + + if( !u ) return( 1 ); + + 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 ); +} + +static int irc_cmd_whois( irc_t *irc, char **cmd ) +{ + char *nick = cmd[1]; + 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 ); + } + + return( 1 ); +} + +static int irc_cmd_whowas( irc_t *irc, char **cmd ) +{ + /* 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 */ + + irc_reply( irc, 406, "%s :Nick does not exist", cmd[1] ); + irc_reply( irc, 369, "%s :End of WHOWAS", cmd[1] ); + + return( 1 ); +} + +static int irc_cmd_nickserv( irc_t *irc, char **cmd ) +{ + /* [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 ); + + return( 1 ); +} + +static int irc_cmd_motd( irc_t *irc, char **cmd ) +{ + irc_motd( irc ); + + return( 1 ); +} + +static int irc_cmd_pong( irc_t *irc, char **cmd ) +{ + /* 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; + + return( 1 ); +} + +static int irc_cmd_completions( irc_t *irc, char **cmd ) +{ + 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" ); + + return( 1 ); +} + +static int irc_cmd_rehash( irc_t *irc, char **cmd ) +{ + if( global.conf->runmode == RUNMODE_INETD ) + ipc_master_cmd_rehash( NULL, NULL ); + else + ipc_to_master( cmd ); + + irc_reply( irc, 382, "%s :Rehashing", CONF_FILE ); + + return( 1 ); +} + +static const command_t irc_commands[] = { + { "pass", 1, irc_cmd_pass, IRC_CMD_PRE_LOGIN }, + { "user", 4, irc_cmd_user, IRC_CMD_PRE_LOGIN }, + { "nick", 1, irc_cmd_nick, 0 }, + { "quit", 0, irc_cmd_quit, 0 }, + { "ping", 0, irc_cmd_ping, 0 }, + { "oper", 2, irc_cmd_oper, IRC_CMD_LOGGED_IN }, + { "mode", 1, irc_cmd_mode, IRC_CMD_LOGGED_IN }, + { "names", 0, irc_cmd_names, IRC_CMD_LOGGED_IN }, + { "part", 1, irc_cmd_part, IRC_CMD_LOGGED_IN }, + { "join", 1, irc_cmd_join, IRC_CMD_LOGGED_IN }, + { "invite", 2, irc_cmd_invite, IRC_CMD_LOGGED_IN }, + { "privmsg", 1, irc_cmd_privmsg, IRC_CMD_LOGGED_IN }, + { "notice", 1, irc_cmd_privmsg, IRC_CMD_LOGGED_IN }, + { "who", 0, irc_cmd_who, IRC_CMD_LOGGED_IN }, + { "userhost", 1, irc_cmd_userhost, IRC_CMD_LOGGED_IN }, + { "ison", 1, irc_cmd_ison, IRC_CMD_LOGGED_IN }, + { "watch", 1, irc_cmd_watch, IRC_CMD_LOGGED_IN }, + { "topic", 1, irc_cmd_topic, IRC_CMD_LOGGED_IN }, + { "away", 0, irc_cmd_away, IRC_CMD_LOGGED_IN }, + { "whois", 1, irc_cmd_whois, IRC_CMD_LOGGED_IN }, + { "whowas", 1, irc_cmd_whowas, IRC_CMD_LOGGED_IN }, + { "nickserv", 1, irc_cmd_nickserv, IRC_CMD_LOGGED_IN }, + { "ns", 1, irc_cmd_nickserv, IRC_CMD_LOGGED_IN }, + { "motd", 0, irc_cmd_motd, IRC_CMD_LOGGED_IN }, + { "pong", 0, irc_cmd_pong, IRC_CMD_LOGGED_IN }, + { "completions", 0, irc_cmd_completions, IRC_CMD_LOGGED_IN }, + { "die", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, + { "wallops", 1, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, + { "lilo", 1, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, + { "rehash", 0, irc_cmd_rehash, IRC_CMD_OPER_ONLY }, + { "kill", 2, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, + { NULL } +}; + +int irc_exec( irc_t *irc, char *cmd[] ) +{ + int i, j; + + if( !cmd[0] ) + return( 1 ); + + for( i = 0; irc_commands[i].command; i++ ) + if( g_strcasecmp( irc_commands[i].command, cmd[0] ) == 0 ) + { + if( irc_commands[i].flags & IRC_CMD_PRE_LOGIN && irc->status >= USTATUS_LOGGED_IN ) + { + irc_reply( irc, 462, ":Only allowed before logging in" ); + return( 1 ); + } + if( irc_commands[i].flags & IRC_CMD_LOGGED_IN && irc->status < USTATUS_LOGGED_IN ) + { + irc_reply( irc, 451, ":Register first" ); + return( 1 ); + } + if( irc_commands[i].flags & IRC_CMD_OPER_ONLY && !strchr( irc->umode, 'o' ) ) + { + irc_reply( irc, 481, ":Permission denied - You're not an IRC operator" ); + return( 1 ); + } + + for( j = 1; j <= irc_commands[i].required_parameters; j ++ ) + if( !cmd[j] ) + { + irc_reply( irc, 461, "%s :Need more parameters", cmd[0] ); + return( 1 ); + } + + if( irc_commands[i].flags & IRC_CMD_TO_MASTER ) + /* IPC doesn't make sense in inetd mode, + but the function will catch that. */ + ipc_to_master( cmd ); + else + return irc_commands[i].execute( irc, cmd ); + } + + return( 1 ); +} @@ -39,10 +39,10 @@ int url_set( url_t *url, char *set_url ) } else { - if( g_strncasecmp( set_url, "https", i - set_url ) == 0 ) - url->proto = PROTO_HTTPS; - else if( g_strncasecmp( set_url, "http", i - set_url ) == 0 ) + if( g_strncasecmp( set_url, "http", i - set_url ) == 0 ) url->proto = PROTO_HTTP; + else if( g_strncasecmp( set_url, "https", i - set_url ) == 0 ) + url->proto = PROTO_HTTPS; else if( g_strncasecmp( set_url, "socks4", i - set_url ) == 0 ) url->proto = PROTO_SOCKS4; else if( g_strncasecmp( set_url, "socks5", i - set_url ) == 0 ) |