diff options
author | Wilmer van der Gaast <wilmer@gaast.net> | 2006-01-15 02:49:49 +0100 |
---|---|---|
committer | Wilmer van der Gaast <wilmer@gaast.net> | 2006-01-15 02:49:49 +0100 |
commit | 0431ea17870f5c382fe5fb692c2852ae80432737 (patch) | |
tree | aac66b136740754f56d643df75e2be45dfbce548 | |
parent | 9d6b229674144d2a48348aeb120604164f371904 (diff) | |
parent | de3e100b7fa676bb628ead4cca2b8cee91fc3a84 (diff) |
Imported irc_command branch and used this addition for parsing IPC commands. (Implemented WALLOP and a very evil DIE.)
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | bitlbee.c | 93 | ||||
-rw-r--r-- | commands.c | 82 | ||||
-rw-r--r-- | commands.h | 28 | ||||
-rw-r--r-- | ipc.c | 207 | ||||
-rw-r--r-- | ipc.h | 40 | ||||
-rw-r--r-- | irc.c | 730 | ||||
-rw-r--r-- | irc.h | 5 | ||||
-rw-r--r-- | irc_commands.c | 654 |
9 files changed, 1058 insertions, 783 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,21 +28,13 @@ #include "commands.h" #include "protocols/nogaim.h" #include "help.h" +#include "ipc.h" #include <signal.h> #include <stdio.h> #include <errno.h> -struct bitlbee_child -{ - 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 @@ -231,9 +223,6 @@ 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 ); @@ -263,35 +252,38 @@ gboolean bitlbee_io_new_client( GIOChannel *source, GIOCondition condition, gpoi 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->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, bitlbee_io_child_ipc_read, NULL ); + global.listen_watch_source_id = gaim_input_add( fds[1], GAIM_INPUT_READ, ipc_child_read, irc ); close( fds[0] ); } } - - if( client_pid == 0 ) + else { 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; } @@ -305,60 +297,3 @@ 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; -} @@ -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,19 @@ #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 + #endif @@ -0,0 +1,207 @@ + /********************************************************************\ + * 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_die( irc_t *data, char **cmd ) +{ + exit( 0 ); +} + +static int ipc_master_cmd_wallop( 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 ) ) + return 0; + + for( l = child_list; l; l = l->next ) + { + struct bitlbee_child *c = l->data; + write( c->ipc_fd, msg_buf, msg_len ); + } + + return 1; +} + +static const command_t ipc_master_commands[] = { + { "die", 0, ipc_master_cmd_die, 0 }, + { "wallop", 1, ipc_master_cmd_wallop, 1 }, + { NULL } +}; + +static int ipc_child_cmd_wallop( irc_t *data, char **cmd ) +{ + irc_t *irc = data; + + if( strchr( irc->umode, 'w' ) ) + irc_write( irc, ":%s WALLOP :%s", irc->myhost, cmd[1] ); + + return 1; +} + +static const command_t ipc_child_commands[] = { + { "wallop", 1, ipc_child_cmd_wallop, 1 }, + { 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 ) + { + 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 + 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 ) + { + close( c->ipc_fd ); + gaim_input_remove( c->ipc_inpa ); + g_free( c ); + + child_list = g_slist_remove( child_list, l ); + + 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 ) + { + 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 ++; + + 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" ); + + write( global.listen_socket, s, len ); + } +} @@ -0,0 +1,40 @@ + /********************************************************************\ + * 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" + +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 ); + +struct bitlbee_child +{ + pid_t pid; + int ipc_fd; + gint ipc_inpa; +}; + +extern GSList *child_list; @@ -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( 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,566 +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( 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 - { - 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 ); + line[i] = '\0'; + cmd[++j] = line + i + 1; - 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( line[i+1] == ':' ) { - 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] ) + cmd[j] ++; 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( 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:" ); - for( i = 0; cmd[i]; i ++ ) irc_usermsg( irc, "%s", cmd[i] ); - irc_usermsg( irc, "\002--------------------" ); - } - return( 1 ); + return cmd; } void irc_reply( irc_t *irc, int code, char *format, ... ) @@ -1043,35 +589,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 ) @@ -1183,34 +720,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 @@ -1240,47 +749,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 ); @@ -1334,22 +802,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" @@ -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( char *line ); void irc_vawrite( irc_t *irc, char *format, va_list params ); void irc_write( irc_t *irc, char *format, ... ); @@ -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 new file mode 100644 index 00000000..3ef5566e --- /dev/null +++ b/irc_commands.c @@ -0,0 +1,654 @@ + /********************************************************************\ + * 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 ); +} + +/* 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 }, + { "die", 0, NULL, IRC_CMD_OPER_ONLY | IRC_CMD_TO_MASTER }, + { "wallop", 0, 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_to_master( cmd ); + else + return irc_commands[i].execute( irc, cmd ); + } + + return( 1 ); +} |