diff options
author | Wilmer van der Gaast <wilmer@gaast.net> | 2006-01-15 21:31:59 +0100 |
---|---|---|
committer | Wilmer van der Gaast <wilmer@gaast.net> | 2006-01-15 21:31:59 +0100 |
commit | f4a59408250b76173418fad090d4623e5300c90f (patch) | |
tree | 1eb79bd250b9c5f5c4ed2de8ffd5202f0575b819 | |
parent | 6fda35070cadc83a6b78c85896fcaf85299e5ad8 (diff) |
Added REHASH command, IPC emulation in daemon (non-forked) mode.
-rw-r--r-- | commands.h | 2 | ||||
-rw-r--r-- | conf.c | 8 | ||||
-rw-r--r-- | ipc.c | 91 | ||||
-rw-r--r-- | ipc.h | 2 | ||||
-rw-r--r-- | irc_commands.c | 12 |
5 files changed, 105 insertions, 10 deletions
@@ -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 @@ -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 ) ); } @@ -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 ); + } } @@ -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 } }; |