From 5c577bd5e2ee33dbe7389df6f4baf659c34de365 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Fri, 13 Jan 2006 23:10:29 +0100 Subject: IPC code (by no means final) --- bitlbee.c | 160 ++++++++++++++++++++++++++++++++++++++++++++++++++------------ bitlbee.h | 3 +- irc.c | 4 ++ 3 files changed, 136 insertions(+), 31 deletions(-) diff --git a/bitlbee.c b/bitlbee.c index 6e36bd12..0e90b912 100644 --- a/bitlbee.c +++ b/bitlbee.c @@ -32,39 +32,17 @@ #include #include -gboolean bitlbee_io_new_client( GIOChannel *source, GIOCondition condition, gpointer data ) +struct bitlbee_child { - 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 ); - } - - return TRUE; -} - + pid_t pid; + int ipc_fd; + gint ipc_inpa; +}; +static GSList *child_list = NULL; +gboolean bitlbee_io_new_client( GIOChannel *source, GIOCondition condition, gpointer data ); + int bitlbee_daemon_init() { #ifdef IPV6 @@ -277,6 +255,71 @@ gboolean bitlbee_io_current_client_write( GIOChannel *source, GIOCondition condi } } +gboolean bitlbee_io_master_ipc_read( gpointer data, gint source, GaimInputCondition cond ); +gboolean bitlbee_io_child_ipc_read( gpointer data, gint source, GaimInputCondition cond ); + +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, bitlbee_io_master_ipc_read, child ); + child_list = g_slist_append( child_list, child ); + + close( fds[1] ); + } + else if( client_pid == 0 ) + { + /* Close the listening socket, we're a client. */ + close( global.listen_socket ); + g_source_remove( global.listen_watch_source_id ); + + /* 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, bitlbee_io_child_ipc_read, NULL ); + + close( fds[0] ); + } + } + + if( client_pid == 0 ) + { + log_message( LOGLVL_INFO, "Creating new connection with fd %d.", new_socket ); + irc_new( new_socket ); + } + else + { + /* We don't need this one, only the client does. */ + close( new_socket ); + } + + return TRUE; +} + void bitlbee_shutdown( gpointer data ) { /* Try to save data for all active connections (if desired). */ @@ -286,3 +329,60 @@ void bitlbee_shutdown( gpointer data ) /* We'll only reach this point when not running in inetd mode: */ g_main_quit( global.loop ); } + +gboolean bitlbee_io_master_ipc_read( gpointer data, gint source, GaimInputCondition cond ) +{ + struct bitlbee_child *child = data; + char buf[513], *eol; + int size; + + size = recv( child->ipc_fd, buf, sizeof( buf ) - 1, MSG_PEEK ); + + if( size < 0 || ( size < 0 && !sockerr_again() ) ) + goto error_abort; + else + buf[size] = 0; + + eol = strstr( buf, "\r\n" ); + if( eol == NULL ) + goto error_abort; + + size = recv( child->ipc_fd, buf, eol - buf + 2, 0 ); + buf[size] = 0; + + if( strcmp( buf, "DIE\r\n" ) == 0 ) + { + printf( "Bye...\n" ); + exit( 0 ); + } + + return TRUE; + +error_abort: + { + GSList *l; + struct bitlbee_child *c; + + for( l = child_list; l; l = l->next ) + { + c = l->data; + if( c->ipc_fd == source ) + { + close( c->ipc_fd ); + gaim_input_remove( c->ipc_inpa ); + g_free( c ); + + child_list = g_slist_remove( child_list, l ); + + break; + } + } + + return FALSE; + } +} + +gboolean bitlbee_io_child_ipc_read( gpointer data, gint source, GaimInputCondition cond ) +{ + return TRUE; +} diff --git a/bitlbee.h b/bitlbee.h index be58ad07..0bd7c90e 100644 --- a/bitlbee.h +++ b/bitlbee.h @@ -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; diff --git a/irc.c b/irc.c index 8842ec41..66469f68 100644 --- a/irc.c +++ b/irc.c @@ -873,6 +873,10 @@ int irc_exec( irc_t *irc, char **cmd ) irc_privmsg( irc, u, "NOTICE", irc->nick, "COMPLETIONS ", "END" ); } + else if( g_strcasecmp( cmd[0], "DIE" ) == 0 ) + { + printf( "%d %d\n", global.listen_socket, write( global.listen_socket, "DIE\r\n", 5 ) ); + } else if( set_getint( irc, "debug" ) ) { irc_usermsg( irc, "\002--- Unknown command:" ); -- cgit v1.2.3 From 0298d1195bb76c1e400695916b4cf65480b0dd76 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sat, 14 Jan 2006 00:51:21 +0100 Subject: Moved all IRC commands to separate functions in irc_commands.c. At least the PASS command doesn't work yet in this form. --- Makefile | 2 +- commands.c | 82 ++++---- commands.h | 27 +-- irc.c | 581 --------------------------------------------------- irc_commands.c | 646 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 694 insertions(+), 644 deletions(-) create mode 100644 irc_commands.c diff --git a/Makefile b/Makefile index 2e0d0faa..c9c396ee 100644 --- a/Makefile +++ b/Makefile @@ -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 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 diff --git a/commands.c b/commands.c index 82143168..fbe378f6 100644 --- a/commands.c +++ b/commands.c @@ -31,29 +31,6 @@ #include -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 } +}; diff --git a/commands.h b/commands.h index 806126e6..69514a67 100644 --- a/commands.h +++ b/commands.h @@ -28,33 +28,18 @@ #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 + #endif diff --git a/irc.c b/irc.c index 32dadb7c..2339af71 100644 --- a/irc.c +++ b/irc.c @@ -418,471 +418,6 @@ int irc_process_line( irc_t *irc, char *line ) 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)->auth_pass ) == 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], "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 ) - { - 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]; @@ -1039,37 +574,6 @@ 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 ) -{ - 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; @@ -1179,34 +683,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 @@ -1236,47 +712,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 ); @@ -1330,22 +765,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; diff --git a/irc_commands.c b/irc_commands.c new file mode 100644 index 00000000..b6eeab37 --- /dev/null +++ b/irc_commands.c @@ -0,0 +1,646 @@ + /********************************************************************\ + * 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" + +static int irc_cmd_pass( irc_t *irc, char **cmd ) +{ + if( strcmp( cmd[1], (global.conf)->auth_pass ) == 0 ) + { + irc->status = USTATUS_AUTHORIZED; + } + else + { + irc_reply( irc, 464, ":Nope, maybe you should try it again..." ); + } + + return( 1 ); +} + +static int irc_cmd_user( irc_t *irc, char **cmd ) +{ + 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 ); +} + +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] ); + if( irc->user ) irc_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 ); +} + +/* + if( !irc->user || !irc->nick ) + { + irc_reply( irc, 451, ":Register first" ); + return( 1 ); + } +*/ + +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( strcmp( cmd[2], global.conf->oper_pass ) == 0 ) + irc_umode_set( irc, "+o", 1 ); + // else + /* FIXME/TODO: Find out which reply to send now. */ + + 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 ); +} + +/* TODO: Attach this one to NOTICE too! */ +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 ); +} + +/* TODO: Alias to NS */ +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 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 }, + { 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_AUTHORIZED ) + continue; + if( irc_commands[i].flags & IRC_CMD_LOGGED_IN && irc->status < USTATUS_LOGGED_IN ) + continue; + if( irc_commands[i].flags & IRC_CMD_OPER_ONLY && !strchr( irc->umode, 'o' ) ) + continue; + + 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 ); + } + + return irc_commands[i].execute( irc, cmd ); + } + + return( 1 ); +} -- cgit v1.2.3 From c22c210f560aee8a43b9fbff63c3dc408b434094 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sat, 14 Jan 2006 18:48:29 +0100 Subject: Checks if there's an OPER password set before checking it, to prevent crashes. --- irc_commands.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/irc_commands.c b/irc_commands.c index b6eeab37..37d530ea 100644 --- a/irc_commands.c +++ b/irc_commands.c @@ -106,7 +106,7 @@ static int irc_cmd_ping( irc_t *irc, char **cmd ) static int irc_cmd_oper( irc_t *irc, char **cmd ) { - if( strcmp( cmd[2], global.conf->oper_pass ) == 0 ) + if( global.conf->oper_pass && strcmp( cmd[2], global.conf->oper_pass ) == 0 ) irc_umode_set( irc, "+o", 1 ); // else /* FIXME/TODO: Find out which reply to send now. */ -- cgit v1.2.3 From edf965753380a165ec615b79d45251931ac3ea62 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sat, 14 Jan 2006 19:25:00 +0100 Subject: Fixed the PASS-command, added error messages for invalid commands to irc_exec(). --- irc.c | 22 ++++++++++++++++++++++ irc.h | 1 + irc_commands.c | 47 ++++++++++++++++++++++------------------------- 3 files changed, 45 insertions(+), 25 deletions(-) diff --git a/irc.c b/irc.c index 2339af71..da6a70c7 100644 --- a/irc.c +++ b/irc.c @@ -574,6 +574,28 @@ void irc_names( irc_t *irc, char *channel ) irc_reply( irc, 366, "%s :End of /NAMES list", channel ); } +int irc_check_login( irc_t *irc ) +{ + if( irc->user && irc->nick ) + { + if( global.conf->authmode == AUTHMODE_CLOSED && irc->status < USTATUS_AUTHORIZED ) + { + irc_reply( irc, 464, ":This server is password-protected." ); + return 0; + } + else + { + irc_login( irc ); + return 1; + } + } + else + { + /* More information needed. */ + return 0; + } +} + void irc_login( irc_t *irc ) { user_t *u; diff --git a/irc.h b/irc.h index 32b19642..69d103b0 100644 --- a/irc.h +++ b/irc.h @@ -117,6 +117,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 index 37d530ea..696c45d4 100644 --- a/irc_commands.c +++ b/irc_commands.c @@ -28,13 +28,14 @@ static int irc_cmd_pass( irc_t *irc, char **cmd ) { - if( strcmp( cmd[1], (global.conf)->auth_pass ) == 0 ) + if( global.conf->auth_pass && strcmp( cmd[1], global.conf->auth_pass ) == 0 ) { irc->status = USTATUS_AUTHORIZED; + irc_check_login(); } else { - irc_reply( irc, 464, ":Nope, maybe you should try it again..." ); + irc_reply( irc, 464, ":Incorrect password." ); } return( 1 ); @@ -42,16 +43,10 @@ static int irc_cmd_pass( irc_t *irc, char **cmd ) static int irc_cmd_user( irc_t *irc, char **cmd ) { - 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 ); - } + irc->user = g_strdup( cmd[1] ); + irc->realname = g_strdup( cmd[4] ); + + irc_check_login( irc ); return( 1 ); } @@ -75,7 +70,8 @@ static int irc_cmd_nick( irc_t *irc, char **cmd ) else { irc->nick = g_strdup( cmd[1] ); - if( irc->user ) irc_login( irc ); + + irc_check_login( irc ); } return( 1 ); @@ -89,14 +85,6 @@ static int irc_cmd_quit( irc_t *irc, char **cmd ) return( 0 ); } -/* - if( !irc->user || !irc->nick ) - { - irc_reply( irc, 451, ":Register first" ); - return( 1 ); - } -*/ - 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 ); @@ -625,12 +613,21 @@ int irc_exec( irc_t *irc, char *cmd[] ) 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_AUTHORIZED ) - continue; + 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 ) - continue; + { + irc_reply( irc, 451, ":Register first" ); + return( 1 ); + } if( irc_commands[i].flags & IRC_CMD_OPER_ONLY && !strchr( irc->umode, 'o' ) ) - continue; + { + 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] ) -- cgit v1.2.3 From b23c5c758097e7c7e9e0141ba9467177c0c32114 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sat, 14 Jan 2006 20:12:48 +0100 Subject: Added correct responses for OPER command, stripped some unnecessary periods. --- irc_commands.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/irc_commands.c b/irc_commands.c index 696c45d4..81b3d60b 100644 --- a/irc_commands.c +++ b/irc_commands.c @@ -35,7 +35,7 @@ static int irc_cmd_pass( irc_t *irc, char **cmd ) } else { - irc_reply( irc, 464, ":Incorrect password." ); + irc_reply( irc, 464, ":Incorrect password" ); } return( 1 ); @@ -95,9 +95,14 @@ static int irc_cmd_ping( irc_t *irc, char **cmd ) 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 ); - // else - /* FIXME/TODO: Find out which reply to send now. */ + irc_reply( irc, 381, ":Password accepted" ); + } + else + { + irc_reply( irc, 432, ":Incorrect password" ); + } return( 1 ); } @@ -310,7 +315,7 @@ static int irc_cmd_who( irc_t *irc, char **cmd ) 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:"**" ); + irc_reply( irc, 315, "%s :End of /WHO list", channel?channel:"**" ); return( 1 ); } -- cgit v1.2.3 From de3e100b7fa676bb628ead4cca2b8cee91fc3a84 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sat, 14 Jan 2006 21:31:59 +0100 Subject: Separated the IRC line splitting/parsing code (to use it for IPC too), and improved the splitting to deal with empty arguments. --- irc.c | 149 +++++++++++++++++++++++++++++++-------------------------- irc.h | 2 +- irc_commands.c | 2 +- 3 files changed, 84 insertions(+), 69 deletions(-) diff --git a/irc.c b/irc.c index da6a70c7..86bcba35 100644 --- a/irc.c +++ b/irc.c @@ -292,30 +292,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( irc, 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; } @@ -325,97 +340,97 @@ 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( 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; + 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; + cmd[0] = line; + for( i = 0, j = 0; line[i] != '\0'; i ++ ) + { + if( line[i] == ' ' ) + { + line[i] = '\0'; + cmd[++j] = line + i + 1; + + if( line[i+1] == ':' ) + { + cmd[j] ++; + break; + } } } - i=irc_exec(irc, cmd); - g_free(cmd); - - return(i); + return cmd; } void irc_reply( irc_t *irc, int code, char *format, ... ) diff --git a/irc.h b/irc.h index 69d103b0..e5b90ea5 100644 --- a/irc.h +++ b/irc.h @@ -107,7 +107,7 @@ 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( irc_t *irc, char *line ); void irc_vawrite( irc_t *irc, char *format, va_list params ); void irc_write( irc_t *irc, char *format, ... ); diff --git a/irc_commands.c b/irc_commands.c index 81b3d60b..a7cb9963 100644 --- a/irc_commands.c +++ b/irc_commands.c @@ -31,7 +31,7 @@ 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_check_login( irc ); } else { -- cgit v1.2.3 From e0ca412a709c80cc7fe26db0576c2bc38be4a45a Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sun, 15 Jan 2006 12:33:54 +0100 Subject: s/WALLOP/WALLOPS/, added LILO command. --- ipc.c | 24 ++++++++++++++++++------ irc_commands.c | 3 ++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/ipc.c b/ipc.c index 81ef4ce8..a60a1c0c 100644 --- a/ipc.c +++ b/ipc.c @@ -32,16 +32,17 @@ GSList *child_list = NULL; static int ipc_master_cmd_die( irc_t *data, char **cmd ) { + /* This shouldn't really be the final implementation... */ exit( 0 ); } -static int ipc_master_cmd_wallop( irc_t *data, char **cmd ) +static int ipc_master_cmd_wallops( irc_t *data, char **cmd ) { GSList *l; char msg_buf[513]; int msg_len; - if( ( msg_len = g_snprintf( msg_buf, sizeof( msg_buf ) - 1, "WALLOP :%s\r\n", cmd[1] ) ) > ( sizeof( msg_buf ) - 1 ) ) + if( ( msg_len = g_snprintf( msg_buf, sizeof( msg_buf ) - 1, "%s :%s\r\n", cmd[0], cmd[1] ) ) > ( sizeof( msg_buf ) - 1 ) ) return 0; for( l = child_list; l; l = l->next ) @@ -55,22 +56,33 @@ static int ipc_master_cmd_wallop( irc_t *data, char **cmd ) static const command_t ipc_master_commands[] = { { "die", 0, ipc_master_cmd_die, 0 }, - { "wallop", 1, ipc_master_cmd_wallop, 1 }, + { "wallops", 1, ipc_master_cmd_wallops, 1 }, + { "lilo", 1, ipc_master_cmd_wallops, 1 }, { NULL } }; -static int ipc_child_cmd_wallop( irc_t *data, char **cmd ) +static int ipc_child_cmd_wallops( irc_t *data, char **cmd ) { irc_t *irc = data; if( strchr( irc->umode, 'w' ) ) - irc_write( irc, ":%s WALLOP :%s", irc->myhost, cmd[1] ); + irc_write( irc, ":%s WALLOPS :%s", irc->myhost, cmd[1] ); + + return 1; +} + +static int ipc_child_cmd_lilo( irc_t *data, char **cmd ) +{ + irc_t *irc = data; + + irc_write( irc, ":%s NOTICE %s :%s", irc->myhost, irc->nick, cmd[1] ); return 1; } static const command_t ipc_child_commands[] = { - { "wallop", 1, ipc_child_cmd_wallop, 1 }, + { "wallops", 1, ipc_child_cmd_wallops, 1 }, + { "lilo", 1, ipc_child_cmd_lilo, 1 }, { NULL } }; diff --git a/irc_commands.c b/irc_commands.c index 3ef5566e..b2bde17b 100644 --- a/irc_commands.c +++ b/irc_commands.c @@ -607,7 +607,8 @@ static const command_t irc_commands[] = { { "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 }, - { "wallop", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, + { "wallops", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, + { "lilo", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, { NULL } }; -- cgit v1.2.3 From 13caf0aa5d1e5575b74221e0cd9e4ff9f4cd79a8 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sun, 15 Jan 2006 15:16:49 +0100 Subject: Better implementation of /DIE --- ipc.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/ipc.c b/ipc.c index a60a1c0c..03c44827 100644 --- a/ipc.c +++ b/ipc.c @@ -32,8 +32,7 @@ GSList *child_list = NULL; static int ipc_master_cmd_die( irc_t *data, char **cmd ) { - /* This shouldn't really be the final implementation... */ - exit( 0 ); + bitlbee_shutdown( NULL ); } static int ipc_master_cmd_wallops( irc_t *data, char **cmd ) -- cgit v1.2.3 From 74c119dd1b066329eba59d057935ba7ec7249555 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sun, 15 Jan 2006 16:42:20 +0100 Subject: Better DIE implementation, added SO_REUSEADDR to listening socket. --- bitlbee.c | 4 +++ conf.c | 12 +++++---- ipc.c | 86 +++++++++++++++++++++++++++++++++++---------------------------- ipc.h | 2 ++ irc.c | 31 +++++++++++++++++++++++ irc.h | 1 + 6 files changed, 93 insertions(+), 43 deletions(-) diff --git a/bitlbee.c b/bitlbee.c index f6605552..dc6b8261 100644 --- a/bitlbee.c +++ b/bitlbee.c @@ -55,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 ); diff --git a/conf.c b/conf.c index 6ec20015..dd165869 100644 --- a/conf.c +++ b/conf.c @@ -91,15 +91,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 +107,8 @@ conf_t *conf_load( int argc, char *argv[] ) g_free( CONF_FILE ); CONF_FILE = g_strdup( optarg ); g_free( conf ); + /* Re-evaluate arguments. */ + optind = 1; return( conf_load( argc, argv ) ); } } diff --git a/ipc.c b/ipc.c index 03c44827..f7435776 100644 --- a/ipc.c +++ b/ipc.c @@ -30,36 +30,39 @@ GSList *child_list = NULL; + 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; } static int ipc_master_cmd_wallops( irc_t *data, char **cmd ) { - GSList *l; - char msg_buf[513]; - int msg_len; - - if( ( msg_len = g_snprintf( msg_buf, sizeof( msg_buf ) - 1, "%s :%s\r\n", cmd[0], cmd[1] ) ) > ( sizeof( msg_buf ) - 1 ) ) - return 0; - - for( l = child_list; l; l = l->next ) - { - struct bitlbee_child *c = l->data; - write( c->ipc_fd, msg_buf, msg_len ); - } + ipc_to_children( cmd ); return 1; } static const command_t ipc_master_commands[] = { { "die", 0, ipc_master_cmd_die, 0 }, - { "wallops", 1, ipc_master_cmd_wallops, 1 }, - { "lilo", 1, ipc_master_cmd_wallops, 1 }, + { "wallops", 1, ipc_master_cmd_wallops, 0 }, + { "lilo", 1, ipc_master_cmd_wallops, 0 }, { NULL } }; + +static int ipc_child_cmd_die( irc_t *data, char **cmd ) +{ + bitlbee_shutdown( NULL ); + + return 1; +} + static int ipc_child_cmd_wallops( irc_t *data, char **cmd ) { irc_t *irc = data; @@ -74,17 +77,20 @@ static int ipc_child_cmd_lilo( irc_t *data, char **cmd ) { irc_t *irc = data; - irc_write( irc, ":%s NOTICE %s :%s", irc->myhost, irc->nick, cmd[1] ); + if( strchr( irc->umode, 's' ) ) + irc_write( irc, ":%s NOTICE %s :%s", irc->myhost, irc->nick, cmd[1] ); return 1; } static const command_t ipc_child_commands[] = { - { "wallops", 1, ipc_child_cmd_wallops, 1 }, - { "lilo", 1, ipc_child_cmd_lilo, 1 }, + { "die", 0, ipc_child_cmd_die, 0 }, + { "wallops", 1, ipc_child_cmd_wallops, 0 }, + { "lilo", 1, ipc_child_cmd_lilo, 0 }, { NULL } }; + static void ipc_command_exec( void *data, char **cmd, const command_t *commands ) { int i; @@ -190,29 +196,33 @@ void ipc_to_master( char **cmd ) { if( global.conf->runmode == RUNMODE_FORKDAEMON ) { - int i, len; - char *s; - - len = 1; - for( i = 0; cmd[i]; i ++ ) - len += strlen( cmd[i] ) + 1; - - if( strchr( cmd[i-1], ' ' ) != NULL ) - len ++; + char *s = irc_build_line( cmd ); + write( global.listen_socket, s, strlen( s ) ); + g_free( s ); + } +} + +void ipc_to_children( char **cmd ) +{ + if( global.conf->runmode == RUNMODE_FORKDAEMON ) + { + char *msg_buf = irc_build_line( cmd ); + ipc_to_children_str( msg_buf ); + g_free( msg_buf ); + } +} + +void ipc_to_children_str( char *msg_buf ) +{ + if( global.conf->runmode == RUNMODE_FORKDAEMON ) + { + int msg_len = strlen( msg_buf ); + GSList *l; - s = g_new0( char, len + 1 ); - for( i = 0; cmd[i]; i ++ ) + for( l = child_list; l; l = l->next ) { - if( cmd[i+1] == NULL && strchr( cmd[i], ' ' ) != NULL ) - strcat( s, ":" ); - - strcat( s, cmd[i] ); - - if( cmd[i+1] ) - strcat( s, " " ); + struct bitlbee_child *c = l->data; + write( c->ipc_fd, msg_buf, msg_len ); } - strcat( s, "\r\n" ); - - write( global.listen_socket, s, len ); } } diff --git a/ipc.h b/ipc.h index 8c48147c..393cb0aa 100644 --- a/ipc.h +++ b/ipc.h @@ -29,6 +29,8 @@ void ipc_master_read( gpointer data, gint source, GaimInputCondition cond ); void ipc_child_read( gpointer data, gint source, GaimInputCondition cond ); void ipc_to_master( char **cmd ); +void ipc_to_children( char **cmd ); +void ipc_to_children_str( char *msg_buf ); struct bitlbee_child { diff --git a/irc.c b/irc.c index a00c4192..a0ba6e53 100644 --- a/irc.c +++ b/irc.c @@ -433,6 +433,37 @@ char **irc_parse_line( char *line ) return cmd; } +char *irc_build_line( char **cmd ) +{ + int i, len; + char *s; + + if( cmd[0] == NULL ) + return NULL; + + 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 ++ ) + { + if( cmd[i+1] == NULL && strchr( cmd[i], ' ' ) != NULL ) + strcat( s, ":" ); + + strcat( s, cmd[i] ); + + if( cmd[i+1] ) + strcat( s, " " ); + } + strcat( s, "\r\n" ); + + return s; +} + void irc_reply( irc_t *irc, int code, char *format, ... ) { char text[IRC_MAX_LINE]; diff --git a/irc.h b/irc.h index 8517bb7e..c8246c16 100644 --- a/irc.h +++ b/irc.h @@ -108,6 +108,7 @@ void irc_free( irc_t *irc ); int irc_exec( irc_t *irc, char **cmd ); int irc_process( irc_t *irc ); 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, ... ); -- cgit v1.2.3 From 6fda35070cadc83a6b78c85896fcaf85299e5ad8 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sun, 15 Jan 2006 17:41:55 +0100 Subject: Fixed a bug in url.c that interpreted all http-urls as https. --- url.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/url.c b/url.c index 816f4ef3..e4deac78 100644 --- a/url.c +++ b/url.c @@ -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 ) -- cgit v1.2.3 From f4a59408250b76173418fad090d4623e5300c90f Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Sun, 15 Jan 2006 21:31:59 +0100 Subject: Added REHASH command, IPC emulation in daemon (non-forked) mode. --- commands.h | 2 ++ conf.c | 8 ++++-- ipc.c | 91 ++++++++++++++++++++++++++++++++++++++++++++++++++++++---- ipc.h | 2 ++ irc_commands.c | 12 ++++++-- 5 files changed, 105 insertions(+), 10 deletions(-) diff --git a/commands.h b/commands.h index e43d275a..a881499a 100644 --- a/commands.h +++ b/commands.h @@ -43,4 +43,6 @@ extern const command_t commands[]; #define IRC_CMD_OPER_ONLY 4 #define IRC_CMD_TO_MASTER 8 +#define IPC_CMD_TO_CHILDREN 1 + #endif diff --git a/conf.c b/conf.c index dd165869..ae4b77a2 100644 --- a/conf.c +++ b/conf.c @@ -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' ) { @@ -107,7 +109,9 @@ conf_t *conf_load( int argc, char *argv[] ) g_free( CONF_FILE ); CONF_FILE = g_strdup( optarg ); g_free( conf ); - /* Re-evaluate arguments. */ + /* 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 ) ); } diff --git a/ipc.c b/ipc.c index f7435776..5d6790f0 100644 --- a/ipc.c +++ b/ipc.c @@ -41,17 +41,32 @@ static int ipc_master_cmd_die( irc_t *data, char **cmd ) return 1; } -static int ipc_master_cmd_wallops( irc_t *data, char **cmd ) +static int ipc_master_cmd_rehash( irc_t *data, char **cmd ) { - ipc_to_children( 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[] = { { "die", 0, ipc_master_cmd_die, 0 }, - { "wallops", 1, ipc_master_cmd_wallops, 0 }, - { "lilo", 1, ipc_master_cmd_wallops, 0 }, + { "wallops", 1, NULL, IPC_CMD_TO_CHILDREN }, + { "lilo", 1, NULL, IPC_CMD_TO_CHILDREN }, + { "rehash", 0, ipc_master_cmd_rehash, 0 }, { NULL } }; @@ -83,10 +98,25 @@ static int ipc_child_cmd_lilo( irc_t *data, char **cmd ) return 1; } +static int ipc_child_cmd_rehash( irc_t *data, 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 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 }, + { "rehash", 0, ipc_child_cmd_rehash, 0 }, { NULL } }; @@ -101,7 +131,11 @@ static void ipc_command_exec( void *data, char **cmd, const command_t *commands for( i = 0; commands[i].command; i ++ ) if( g_strcasecmp( commands[i].command, cmd[0] ) == 0 ) { - commands[i].execute( data, cmd ); + if( commands[i].flags & IPC_CMD_TO_CHILDREN ) + ipc_to_children( cmd ); + else + commands[i].execute( data, cmd ); + return; } } @@ -197,7 +231,32 @@ void ipc_to_master( char **cmd ) if( global.conf->runmode == RUNMODE_FORKDAEMON ) { char *s = irc_build_line( cmd ); - write( global.listen_socket, s, strlen( s ) ); + ipc_to_master_str( 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 *msg_buf ) +{ + if( global.conf->runmode == RUNMODE_FORKDAEMON ) + { + write( global.listen_socket, msg_buf, strlen( msg_buf ) ); + } + else if( global.conf->runmode == RUNMODE_DAEMON ) + { + char *s, **cmd; + + /* irc_parse_line() wants a read-write string, so get it one: */ + s = g_strdup( msg_buf ); + cmd = irc_parse_line( s ); + + ipc_command_exec( NULL, cmd, ipc_master_commands ); + + g_free( cmd ); g_free( s ); } } @@ -210,6 +269,13 @@ void ipc_to_children( char **cmd ) ipc_to_children_str( 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 *msg_buf ) @@ -225,4 +291,17 @@ void ipc_to_children_str( char *msg_buf ) write( c->ipc_fd, msg_buf, msg_len ); } } + else if( global.conf->runmode == RUNMODE_DAEMON ) + { + char *s, **cmd; + + /* irc_parse_line() wants a read-write string, so get it one: */ + s = g_strdup( msg_buf ); + cmd = irc_parse_line( s ); + + ipc_to_children( cmd ); + + g_free( cmd ); + g_free( s ); + } } diff --git a/ipc.h b/ipc.h index 393cb0aa..57037d61 100644 --- a/ipc.h +++ b/ipc.h @@ -28,7 +28,9 @@ void ipc_master_read( gpointer data, gint source, GaimInputCondition cond ); void ipc_child_read( gpointer data, gint source, GaimInputCondition cond ); + void ipc_to_master( char **cmd ); +void ipc_to_master_str( char *msg_buf ); void ipc_to_children( char **cmd ); void ipc_to_children_str( char *msg_buf ); diff --git a/irc_commands.c b/irc_commands.c index b2bde17b..5f666702 100644 --- a/irc_commands.c +++ b/irc_commands.c @@ -232,7 +232,6 @@ static int irc_cmd_invite( irc_t *irc, char **cmd ) return( 1 ); } -/* TODO: Attach this one to NOTICE too! */ static int irc_cmd_privmsg( irc_t *irc, char **cmd ) { if ( !cmd[2] ) @@ -529,7 +528,6 @@ static int irc_cmd_whowas( irc_t *irc, char **cmd ) return( 1 ); } -/* TODO: Alias to NS */ static int irc_cmd_nickserv( irc_t *irc, char **cmd ) { /* [SH] This aliases the NickServ command to PRIVMSG root */ @@ -579,6 +577,15 @@ static int irc_cmd_completions( irc_t *irc, char **cmd ) return( 1 ); } +static int irc_cmd_rehash( irc_t *irc, char **cmd ) +{ + 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 }, @@ -609,6 +616,7 @@ static const command_t irc_commands[] = { { "die", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, { "wallops", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, { "lilo", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, + { "rehash", 0, irc_cmd_rehash, IRC_CMD_OPER_ONLY }, { NULL } }; -- cgit v1.2.3 From daa9e027e8c8d1bc91e104dba985d5d44fef8c77 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Tue, 17 Jan 2006 19:05:22 +0100 Subject: LILO/WALLOPS commands now check if the receiving user logged in yet. --- ipc.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/ipc.c b/ipc.c index 5d6790f0..5e7b7821 100644 --- a/ipc.c +++ b/ipc.c @@ -82,6 +82,9 @@ static int ipc_child_cmd_wallops( irc_t *data, char **cmd ) { irc_t *irc = data; + if( irc->status < USTATUS_LOGGED_IN ) + return 1; + if( strchr( irc->umode, 'w' ) ) irc_write( irc, ":%s WALLOPS :%s", irc->myhost, cmd[1] ); @@ -92,6 +95,9 @@ static int ipc_child_cmd_lilo( irc_t *data, char **cmd ) { irc_t *irc = data; + 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] ); -- cgit v1.2.3 From 48721c328e574e0ff76c41734b78aab217edbf65 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Tue, 17 Jan 2006 22:15:42 +0100 Subject: A KILL command. Unfortunately the user doesn't see the KILL message yet. :-( --- ipc.c | 34 +++++++++++++++++++++++----------- irc_commands.c | 5 +++-- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/ipc.c b/ipc.c index 5e7b7821..cd30eb52 100644 --- a/ipc.c +++ b/ipc.c @@ -31,7 +31,7 @@ GSList *child_list = NULL; -static int ipc_master_cmd_die( irc_t *data, char **cmd ) +static int ipc_master_cmd_die( irc_t *irc, char **cmd ) { if( global.conf->runmode == RUNMODE_FORKDAEMON ) ipc_to_children_str( "DIE\r\n" ); @@ -41,7 +41,7 @@ static int ipc_master_cmd_die( irc_t *data, char **cmd ) return 1; } -static int ipc_master_cmd_rehash( irc_t *data, char **cmd ) +static int ipc_master_cmd_rehash( irc_t *irc, char **cmd ) { runmode_t oldmode; @@ -67,21 +67,20 @@ static const command_t ipc_master_commands[] = { { "wallops", 1, NULL, IPC_CMD_TO_CHILDREN }, { "lilo", 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 *data, char **cmd ) +static int ipc_child_cmd_die( irc_t *irc, char **cmd ) { bitlbee_shutdown( NULL ); return 1; } -static int ipc_child_cmd_wallops( irc_t *data, char **cmd ) +static int ipc_child_cmd_wallops( irc_t *irc, char **cmd ) { - irc_t *irc = data; - if( irc->status < USTATUS_LOGGED_IN ) return 1; @@ -91,10 +90,8 @@ static int ipc_child_cmd_wallops( irc_t *data, char **cmd ) return 1; } -static int ipc_child_cmd_lilo( irc_t *data, char **cmd ) +static int ipc_child_cmd_lilo( irc_t *irc, char **cmd ) { - irc_t *irc = data; - if( irc->status < USTATUS_LOGGED_IN ) return 1; @@ -104,7 +101,7 @@ static int ipc_child_cmd_lilo( irc_t *data, char **cmd ) return 1; } -static int ipc_child_cmd_rehash( irc_t *data, char **cmd ) +static int ipc_child_cmd_rehash( irc_t *irc, char **cmd ) { runmode_t oldmode; @@ -118,11 +115,26 @@ static int ipc_child_cmd_rehash( irc_t *data, char **cmd ) 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] ); + 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 }, - { "rehash", 0, ipc_child_cmd_rehash, 0 }, + { "rehash", 0, ipc_child_cmd_rehash, 0 }, + { "kill", 2, ipc_child_cmd_kill, 0 }, { NULL } }; diff --git a/irc_commands.c b/irc_commands.c index 5f666702..2e225702 100644 --- a/irc_commands.c +++ b/irc_commands.c @@ -614,9 +614,10 @@ static const command_t irc_commands[] = { { "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", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, - { "lilo", 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 } }; -- cgit v1.2.3 From 1ea13be2cf335a471f85ea54d610fb91b7d14564 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Wed, 18 Jan 2006 19:14:35 +0100 Subject: Fixed a bad mistake in ipc_readline() error handling. --- ipc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/ipc.c b/ipc.c index cd30eb52..dfde24bd 100644 --- a/ipc.c +++ b/ipc.c @@ -173,6 +173,8 @@ static char *ipc_readline( int fd ) 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; -- cgit v1.2.3 From bd9b00f4fed3560eab98f15cf9923aed13467d5d Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Thu, 19 Jan 2006 18:07:47 +0100 Subject: Fixes for single-process daemon mode, changed value of USTATUS_SHUTDOWN. If this still causes problems, shutting down should be an extra flag instead of a status code. --- ipc.c | 21 +++++++++++++++------ irc.c | 4 +++- irc.h | 4 ++-- 3 files changed, 20 insertions(+), 9 deletions(-) diff --git a/ipc.c b/ipc.c index 5fda047c..4777113a 100644 --- a/ipc.c +++ b/ipc.c @@ -35,12 +35,15 @@ static int ipc_master_cmd_client( irc_t *data, char **cmd ) { struct bitlbee_child *child = (void*) data; - child->host = g_strdup( cmd[1] ); - child->nick = g_strdup( cmd[2] ); - child->realname = g_strdup( cmd[3] ); + 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->pid, child->nick, child->host, child->realname ); + child ? child->pid : -1, cmd[2], cmd[1], cmd[3] ); return 1; } @@ -307,7 +310,10 @@ void ipc_to_master_str( char *format, ... ) } else if( global.conf->runmode == RUNMODE_DAEMON ) { - char **cmd; + 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 ); @@ -360,7 +366,10 @@ void ipc_to_children_str( char *format, ... ) } else if( global.conf->runmode == RUNMODE_DAEMON ) { - char **cmd; + char **cmd, *s; + + if( ( s = strchr( msg_buf, '\r' ) ) ) + *s = 0; cmd = irc_parse_line( msg_buf ); ipc_to_children( cmd ); diff --git a/irc.c b/irc.c index aedbbc83..2f2937cc 100644 --- a/irc.c +++ b/irc.c @@ -187,6 +187,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 ); @@ -693,7 +695,7 @@ void irc_login( irc_t *irc ) 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 ) + 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; diff --git a/irc.h b/irc.h index 093de7c9..309f79e6 100644 --- a/irc.h +++ b/irc.h @@ -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 -- cgit v1.2.3 From 5424c76c7813f82e2f98546f6a46b73d80181877 Mon Sep 17 00:00:00 2001 From: Wilmer van der Gaast Date: Thu, 19 Jan 2006 18:52:19 +0100 Subject: Rehash command now also works in inetd mode. Other "IPC" commands only make sense in daemon mode. --- ipc.c | 2 +- ipc.h | 6 ++++++ irc_commands.c | 7 ++++++- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/ipc.c b/ipc.c index 4777113a..e0bf1319 100644 --- a/ipc.c +++ b/ipc.c @@ -58,7 +58,7 @@ static int ipc_master_cmd_die( irc_t *data, char **cmd ) return 1; } -static int ipc_master_cmd_rehash( irc_t *data, char **cmd ) +int ipc_master_cmd_rehash( irc_t *data, char **cmd ) { runmode_t oldmode; diff --git a/ipc.h b/ipc.h index 56449a7c..308b6e4b 100644 --- a/ipc.h +++ b/ipc.h @@ -26,6 +26,7 @@ #define BITLBEE_CORE #include "bitlbee.h" + struct bitlbee_child { pid_t pid; @@ -37,6 +38,7 @@ struct bitlbee_child char *realname; }; + void ipc_master_read( gpointer data, gint source, GaimInputCondition cond ); void ipc_child_read( gpointer data, gint source, GaimInputCondition cond ); @@ -48,4 +50,8 @@ 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; diff --git a/irc_commands.c b/irc_commands.c index 152df767..e31a92e8 100644 --- a/irc_commands.c +++ b/irc_commands.c @@ -579,7 +579,10 @@ static int irc_cmd_completions( irc_t *irc, char **cmd ) static int irc_cmd_rehash( irc_t *irc, char **cmd ) { - ipc_to_master( 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 ); @@ -655,6 +658,8 @@ int irc_exec( irc_t *irc, char *cmd[] ) } 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 ); -- cgit v1.2.3