diff options
author | Wilmer van der Gaast <wilmer@gaast.net> | 2010-04-12 02:06:49 +0200 |
---|---|---|
committer | Wilmer van der Gaast <wilmer@gaast.net> | 2010-04-12 02:06:49 +0200 |
commit | 24b8bbb2616d685006a279e46a4bd2e8e7cf6694 (patch) | |
tree | c585d428a08bdd8c7f22b1fdef8e65d758f60d6e | |
parent | e21c0f8b276cc3ca177bcf6217eba9c634d410f7 (diff) |
Start handling CTCPs, in a saner way than before.
-rw-r--r-- | irc.h | 1 | ||||
-rw-r--r-- | irc_commands.c | 29 | ||||
-rw-r--r-- | irc_user.c | 10 | ||||
-rw-r--r-- | lib/misc.c | 45 | ||||
-rw-r--r-- | lib/misc.h | 2 | ||||
-rw-r--r-- | root_commands.c | 43 |
6 files changed, 85 insertions, 45 deletions
@@ -119,6 +119,7 @@ typedef struct irc_user struct irc_user_funcs { gboolean (*privmsg)( irc_user_t *iu, const char *msg ); + gboolean (*ctcp)( irc_user_t *iu, char * const* ctcp ); }; extern const struct irc_user_funcs irc_user_root_funcs; diff --git a/irc_commands.c b/irc_commands.c index bf55e2b8..5387fb4d 100644 --- a/irc_commands.c +++ b/irc_commands.c @@ -254,16 +254,39 @@ static void irc_cmd_privmsg( irc_t *irc, char **cmd ) if( !cmd[2] ) { irc_send_num( irc, 412, ":No text to send" ); + return; } - else if( irc_channel_name_ok( cmd[1] ) && - ( ic = irc_channel_by_name( irc, cmd[1] ) ) ) + + /* Don't treat CTCP actions as real CTCPs, just convert them right now. */ + if( g_strncasecmp( cmd[2], "\001ACTION", 7 ) == 0 ) + { + cmd[2] += 4; + strcpy( cmd[2], "/me" ); + if( cmd[2][strlen(cmd[2])-1] == '\001' ) + cmd[2][strlen(cmd[2])-1] = '\0'; + } + + if( irc_channel_name_ok( cmd[1] ) && + ( ic = irc_channel_by_name( irc, cmd[1] ) ) ) { if( ic->f->privmsg ) ic->f->privmsg( ic, cmd[2] ); } else if( ( iu = irc_user_by_name( irc, cmd[1] ) ) ) { - if( iu->f->privmsg ) + if( cmd[2][0] == '\001' ) + { + char **ctcp; + + if( iu->f->ctcp == NULL ) + return; + if( cmd[2][strlen(cmd[2])-1] == '\001' ) + cmd[2][strlen(cmd[2])-1] = '\0'; + + ctcp = split_command_parts( cmd[2] + 1 ); + iu->f->ctcp( iu, ctcp ); + } + else if( iu->f->privmsg ) iu->f->privmsg( iu, cmd[2] ); } else @@ -146,8 +146,18 @@ static gboolean root_privmsg( irc_user_t *iu, const char *msg ) return TRUE; } +static gboolean root_ctcp( irc_user_t *iu, char * const *ctcp ) +{ + if( g_strcasecmp( ctcp[0], "VERSION" ) == 0 ) + { + } + + return TRUE; +} + const struct irc_user_funcs irc_user_root_funcs = { root_privmsg, + root_ctcp, }; /* Echo to yourself: */ @@ -613,3 +613,48 @@ int md5_verify_password( char *password, char *hash ) return ret; } + +char **split_command_parts( char *command ) +{ + static char *cmd[IRC_MAX_ARGS+1]; + char *s, q = 0; + int k; + + memset( cmd, 0, sizeof( cmd ) ); + cmd[0] = command; + k = 1; + for( s = command; *s && k < IRC_MAX_ARGS; s ++ ) + if( *s == ' ' && !q ) + { + *s = 0; + while( *++s == ' ' ); + if( *s == '"' || *s == '\'' ) + { + q = *s; + s ++; + } + if( *s ) + { + cmd[k++] = s; + s --; + } + else + { + break; + } + } + else if( *s == '\\' && ( ( !q && s[1] ) || ( q && q == s[1] ) ) ) + { + char *cpy; + + for( cpy = s; *cpy; cpy ++ ) + cpy[0] = cpy[1]; + } + else if( *s == q ) + { + q = *s = 0; + } + cmd[k] = NULL; + + return cmd; +} @@ -67,4 +67,6 @@ G_MODULE_EXPORT gboolean ssl_sockerr_again( void *ssl ); G_MODULE_EXPORT int md5_verify_password( char *password, char *hash ); +G_MODULE_EXPORT char **split_command_parts( char *command ); + #endif diff --git a/root_commands.c b/root_commands.c index 3853a73e..7a4c021e 100644 --- a/root_commands.c +++ b/root_commands.c @@ -33,48 +33,7 @@ void root_command_string( irc_t *irc, char *command ) { - char *cmd[IRC_MAX_ARGS]; - char *s; - int k; - char q = 0; - - memset( cmd, 0, sizeof( cmd ) ); - cmd[0] = command; - k = 1; - for( s = command; *s && k < ( IRC_MAX_ARGS - 1 ); s ++ ) - if( *s == ' ' && !q ) - { - *s = 0; - while( *++s == ' ' ); - if( *s == '"' || *s == '\'' ) - { - q = *s; - s ++; - } - if( *s ) - { - cmd[k++] = s; - s --; - } - else - { - break; - } - } - else if( *s == '\\' && ( ( !q && s[1] ) || ( q && q == s[1] ) ) ) - { - char *cpy; - - for( cpy = s; *cpy; cpy ++ ) - cpy[0] = cpy[1]; - } - else if( *s == q ) - { - q = *s = 0; - } - cmd[k] = NULL; - - root_command( irc, cmd ); + root_command( irc, split_command_parts( command ) ); } #define MIN_ARGS( x, y... ) \ |