irctk

libircclient binding for scripts
git clone https://a3nm.net/git/irctk/
Log | Files | Refs | README

commit 786d71d29b154af49b9e709bf15874d2626eec50
parent e8842bcf19da7c276e44901a078318f79629404d
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Fri,  7 Sep 2012 15:08:14 +0200

strip bad characters from username

Diffstat:
TODO | 2--
irctk.c | 40+++++++++++++++++++++++++++-------------
2 files changed, 27 insertions(+), 15 deletions(-)

diff --git a/TODO b/TODO @@ -1,6 +1,4 @@ - trying to change to an invalid nick after startup should fail -- identify where some symbols (e.g. underscores) are not properly supported in - initial connects to the server - make ssl support work later: diff --git a/irctk.c b/irctk.c @@ -146,7 +146,7 @@ static struct argp_option options[] = { DISPLAY_SELECTION}, {"username", 'U', "name", 0, - "Unix username (default: same as nick)", + "Unix username (default: same as nick, only keeping lowercase letters)", NAME}, {"realname", 'R', "name", 0, "Full name (default: same as nick)", @@ -189,10 +189,10 @@ struct arguments enum track track; int join; - char *username; - int username_set; // to get nick as default username - char *realname; - int realname_set; // to get nick as default username + char username[MAX_NICK_LEN]; + char realname[MAX_NICK_LEN]; + char username_set; + char realname_set; int force_nick; int command; @@ -239,10 +239,10 @@ void initialize_args() args.track = NO; args.join = 1; + strncpy(args.username, "irctk", MAX_NICK_LEN-1); + strncpy(args.realname, "irctk", MAX_NICK_LEN-1); args.username_set = 0; - args.username = args.nick; args.realname_set = 0; - args.realname = args.nick; args.force_nick = 0; args.filter = 0; @@ -252,15 +252,29 @@ void initialize_args() args.last_chans_out[0] = 0; } +/* set the username, removing bad characters */ +void set_username(struct arguments *arguments, char *name) { + int opos = 0, ipos = 0; + while (name[ipos]) { + if (name[ipos] >= 'a' && name[ipos] <= 'z') + arguments->username[opos++] = name[ipos]; + ipos++; + } + arguments->username[opos] = 0; + if (!opos) { + // there were no valid characters to copy, revert to default + strncpy(arguments->username, "irctk", MAX_NICK_LEN-1); + } +} /* set our nick, and names if not already set */ void set_nick_and_names(struct arguments *arguments) { strncpy(arguments->nick, arguments->raw_dest, MAX_NICK_LEN-1); - if (!arguments->username_set) - arguments->username = arguments->raw_dest; - if (!arguments->realname_set) - arguments->realname = arguments->raw_dest; + if (!args.username_set) + set_username(arguments, arguments->raw_dest); + if (!args.realname_set) + strncpy(arguments->realname, arguments->raw_dest, MAX_NICK_LEN-1); } @@ -307,11 +321,11 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state) break; case 'U': arguments->username_set = 1; - arguments->username = arg; + set_username(arguments, arg); break; case 'R': arguments->realname_set = 1; - arguments->realname = arg; + strncpy(arguments->realname, arg, MAX_NICK_LEN-1); break; case 'o': arguments->own = 1;