irctk

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

commit c74a79a8092c1bb4ab3e7b7cf7708da50474440c
parent 1650d2203dff86c67aafe675e43b44edc81b6282
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Sat, 16 Jun 2012 16:24:51 +0200

suffix underscores if nick is not available

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

diff --git a/TODO b/TODO @@ -1,7 +1,5 @@ http://www.iagora.com/~espel/sirc/sirc.html -ulminfo:github has long lines which will not be said? wtf? handle nick following -wait for nick to be free, or fail, or suffix as many as needed ssl support password support exit when killed diff --git a/irctk.c b/irctk.c @@ -45,6 +45,7 @@ // TODO get rid of that #define MAX_LEN 4096 +#define MAX_NICK_LEN 4096 // TODO tracking of nicks @@ -147,7 +148,7 @@ static struct argp_option options[] = { "Full name (default: same as nick)", NAME}, {"force-nick", 'N', 0, 0, //TODO - "Don't give up if the nick is invalid (revert to default) or already taken (add trailing underscores)", + "Abort when the intended nick is unavailable or invalid instead of choosing another one", NAME}, { 0 } @@ -158,7 +159,7 @@ static struct argp_option options[] = { */ struct arguments { - char *nick; + char nick[MAX_NICK_LEN]; char *server; int port; char *password; @@ -203,7 +204,7 @@ struct arguments args; void initialize_args() { - args.nick = "irctk"; + strncpy(args.nick, "irctk", MAX_NICK_LEN-1); args.server = NULL; args.password = NULL; args.port = 6667; @@ -335,7 +336,7 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state) { if (arguments->raw_dest[i] == ':' && !saw_pass) { arguments->raw_dest[i] = 0; - arguments->nick = arguments->raw_dest; + strncat(arguments->nick, arguments->raw_dest, MAX_NICK_LEN-1); if (!arguments->username_set) arguments->username = arguments->raw_dest; if (!arguments->realname_set) @@ -349,7 +350,7 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state) if (saw_pass) { // nick was already set } else { - arguments->nick = arguments->raw_dest; + strncat(arguments->nick, arguments->raw_dest, MAX_NICK_LEN-1); if (!arguments->username_set) arguments->username = arguments->raw_dest; if (!arguments->realname_set) @@ -620,25 +621,30 @@ void dump_event (irc_session_t * session, const char * event, const char * origi if (atoi(event) == LIBIRC_RFC_ERR_ERRONEUSNICKNAME) { /* bad nick */ - // TODO only fatal at startup; if changing, just revert if (args.force_nick) { + die(E_BADNAME, "Invalid nick!"); + } else { // TODO does not work info("Invalid nick, reverting to \"irctk\""); - args.nick = "irctk"; + strncpy(args.nick, "irctk", MAX_NICK_LEN-1); rsl = irc_cmd_nick (session, "irctk"); - } else { - // TODO abort - die(E_BADNAME, "Invalid nick!"); } } else if (atoi(event) == LIBIRC_RFC_ERR_NICKCOLLISION || atoi(event) == LIBIRC_RFC_ERR_NICKNAMEINUSE) { /* bad nick */ if (args.force_nick) { - // TODO - } else { - // TODO abort die(E_BADNAME, "Nickname already in use!"); + } else { + int len = strlen(args.nick); + if (len >= MAX_NICK_LEN-1) { + die(E_BADNAME, "No available nickname found."); + } else { + args.nick[len] = '_'; + args.nick[len+1] = NULL; + rsl = irc_cmd_nick (session, args.nick); + info("Requested nick is taken, try %s\n", args.nick, args.nick); + } } } else if (atoi(event) == LIBIRC_RFC_ERR_BADCHANNELKEY) { // TODO report name of channel last joined, die if it was specified on cli