From b6fdcbeb3ea50e0051749dc552ffb7a736d3c8e1 Mon Sep 17 00:00:00 2001 From: Dirk Engling Date: Sun, 5 Jan 2014 21:23:39 +0100 Subject: Dict handling to allow custom completion from user dictionary --- vchat-commands.c | 29 +++++++++++++++++++---------- vchat-help.h | 2 ++ vchat-user.c | 38 +++++++++++++++++++++++++++++++++++--- vchat-user.h | 3 +++ 4 files changed, 59 insertions(+), 13 deletions(-) diff --git a/vchat-commands.c b/vchat-commands.c index e4f1d99..2c3d68b 100755 --- a/vchat-commands.c +++ b/vchat-commands.c @@ -48,6 +48,7 @@ COMMAND_FORMAT, COMMAND_KEYS, COMMAND_QUIT, COMMAND_USER, +COMMAND_DICT, COMMAND_FLT, COMMAND_PM, COMMAND_ACTION, @@ -73,6 +74,7 @@ static void command_rmflt ( char *tail); static void command_none ( char *line); static void command_query ( char *tail); static void command_reconnect ( char *tail); +static void command_dict ( char *tail); static void output_default ( char *tail); @@ -92,6 +94,7 @@ commandtable[] = { { COMMAND_QUERY, "QUERY", 5, command_query, NULL, NULL }, { COMMAND_QUIT, "QUIT", 4, command_quit, SHORT_HELPTEXT_QUIT, LONG_HELPTEXT_QUIT }, { COMMAND_USER, "USER", 4, command_user, SHORT_HELPTEXT_USER, LONG_HELPTEXT_USER }, +{ COMMAND_DICT, "DICT", 4, command_dict, SHORT_HELPTEXT_DICT, LONG_HELPTEXT_DICT }, { COMMAND_FLT, "FLT", 3, command_flt, NULL, LONG_HELPTEXT_FLT }, { COMMAND_PM, "MSG", 3, command_pm, SHORT_HELPTEXT_MSG, LONG_HELPTEXT_MSG }, { COMMAND_ACTION, "ME", 2, command_action, SHORT_HELPTEXT_ME, LONG_HELPTEXT_ME }, @@ -131,7 +134,7 @@ translatecommand( char **cmd) /* ... whose start may be affected by abbrevation */ if( commandtable[result].number != COMMAND_NONE ) (*cmd) -= cut; - + return result; } @@ -140,11 +143,11 @@ static void dothink( char *tail, char nice ) { while( *tail == ' ' ) tail++; - + /* send users message to server */ snprintf (tmpstr, TMPSTRSIZE, ".%c %s", nice, tail); networkoutput (tmpstr); - + /* show action in channel window */ snprintf (tmpstr, TMPSTRSIZE, nice == 'O' ? getformatstr(FS_TXPUBNTHOUGHT) : getformatstr(FS_TXPUBTHOUGHT), tail); writechan (tmpstr); @@ -261,7 +264,7 @@ handleline (char *line) break; default: output_default( line ); - break; + break; } } @@ -276,7 +279,7 @@ output_default(char *line ) { /* output message to channel window */ writechan (tmpstr); } - + /* handle a "/user " request */ static void command_user(char *tail) @@ -372,7 +375,7 @@ command_none(char *line) { snprintf(tmpstr, TMPSTRSIZE, " Unknown client command: %s ", line); msgout(tmpstr); } - + /* handle a "/flt " request */ static void command_flt(char *tail){ @@ -383,7 +386,7 @@ command_flt(char *tail){ if( colour && *tail) { addfilter( colour, tail); } -} +} /* handle a "/clflt " request */ static void @@ -391,20 +394,20 @@ command_clflt (char *tail) { while( *tail == ' ') tail++; clearfilters( *tail ); } - + /* handle a "/rmflt " request */ static void command_rmflt (char *tail) { while( *tail == ' ') tail++; removefilter( tail ); } - + /* list filters */ static void command_lsflt (char *tail) { listfilters(); } - + /* handle a "/me " action */ static void command_action(char *tail) @@ -469,3 +472,9 @@ command_query(char *tail) // Do the ui stuff for query handlequery( tail ); } + +void +command_dict(char *tail) +{ + ul_add_to_dict(tail); +} diff --git a/vchat-help.h b/vchat-help.h index aec0fcf..a7964fd 100755 --- a/vchat-help.h +++ b/vchat-help.h @@ -55,6 +55,8 @@ #define LONG_HELPTEXT_QUIT NULL #define SHORT_HELPTEXT_USER "/USER REGEX Lists all users matching regex REGEX" #define LONG_HELPTEXT_USER NULL +#define SHORT_HELPTEXT_DICT "/DICT ITEM [...] Add space separated items to the user completion dict" +#define LONG_HELPTEXT_DICT NULL #define SHORT_HELPTEXT_MSG "/M[SG] USER MESSAGE Send private message to user USER" #define LONG_HELPTEXT_MSG NULL #define SHORT_HELPTEXT_ME "/ME ACTION Let the user do an action" diff --git a/vchat-user.c b/vchat-user.c index ce36220..d7261d6 100755 --- a/vchat-user.c +++ b/vchat-user.c @@ -30,6 +30,9 @@ static char *g_nick; //< own nick static int g_channel; //< own channel unsigned int ul_case_first = 0; +static char **g_dict; +static size_t g_dict_len; + static int ul_nick_lookup( const char *nick, int *exact_match ) { int i; @@ -206,6 +209,15 @@ void ul_public_action(char *name) { g_users[base].last_public = ul_now(); } +void ul_add_to_dict(char *dict_items) { + char *i; + for(i=strtok(dict_items," ");i;i=strtok(0," ")) { + g_dict = realloc( g_dict, sizeof(char*) * ( 1 + g_dict_len ) ); + if( !g_dict ) exit(1); + g_dict[g_dict_len++] = strdup(i); + } +} + /* Finding users ul_finduser? */ char * ul_match_user(char *regex) { char *dest = tmpstr; @@ -327,15 +339,21 @@ static int ul_compare_middle_case( const void *a, const void *b ) { /* Nick completion function for readline */ char **ul_complete_user(char *text, int start, int end ) { char **result = 0; - int i, result_count = 0; + int i, result_count = 0, dict_result_count = 0; /* Never want readline to complete filenames */ rl_attempted_completion_over = 1; + /* Check for amount of custom dict matches */ + if( end && ( start != end ) ) + for( i=0; i