irctk

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

commit ebfa4cb8f37f3c70e3ee6920421ca3cf92d81f62
parent 762e5970d8baa01d0f6aafbb549c323acc0e406b
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Thu,  6 Aug 2015 22:57:52 +0200

Merge remote-tracking branch 'louis/master'

Diffstat:
irctk.c | 185+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 111 insertions(+), 74 deletions(-)

diff --git a/irctk.c b/irctk.c @@ -42,8 +42,6 @@ enum track {NO, YES, UNIQUE}; // non-standard error for the hybrid ircd #define ERR_BADCHANNAME 479 -#define max(a, b) ((b) > (a) ? (b) : (a)) - const char *argp_program_bug_address = "<a3nm@a3nm.net>"; static char doc[] = "irctk -- an IRC toolkit"; @@ -281,7 +279,9 @@ void initialize_args() void set_username(struct arguments *arguments, char *name) { int opos = 0, ipos = 0; while (name[ipos]) { - if (name[ipos] >= 'a' && name[ipos] <= 'z') + if ( (name[ipos] >= 'a' && name[ipos] <= 'z') || + (name[ipos] >= 'A' && name[ipos] <= 'Z') || + (name[ipos] >= '0' && name[ipos] <= '9') ) arguments->username[opos++] = name[ipos]; ipos++; } @@ -1433,84 +1433,105 @@ void manage_event (irc_session_t *session, const char *event, const char *origin die(E_SERVER_ERROR, "Unspecified error from server\n"); } } - if (atoi(event) == LIBIRC_RFC_ERR_ERRONEUSNICKNAME) { - if (args.force_nick) { - die(E_BADNAME, "Could not set nick\n"); - } else { - if (!((irc_ctx_t *) irc_get_ctx(session))->ready) { - info("Could not set nick, reverting to \"irctk\"\n"); - strncpy(args.nick, "irctk", MAX_NICK_LEN-1); - rsl = irc_cmd_nick(session, "irctk"); + + irc_ctx_t * ctx; + switch(atoi(event)) { + case LIBIRC_RFC_ERR_ERRONEUSNICKNAME: + if (args.force_nick) { + die(E_BADNAME, "Could not set nick\n"); } else { - info("Could not set nick, ignoring\n"); - rsl = 0; + if (!((irc_ctx_t *) irc_get_ctx(session))->ready) { + info("Could not set nick, reverting to \"irctk\"\n"); + strncpy(args.nick, "irctk", MAX_NICK_LEN-1); + rsl = irc_cmd_nick(session, "irctk"); + } else { + info("Could not set nick, ignoring\n"); + rsl = 0; + } } - } - } else if (atoi(event) == LIBIRC_RFC_ERR_NICKCOLLISION - || atoi(event) == LIBIRC_RFC_ERR_NICKNAMEINUSE) { - if (args.force_nick) { - die(E_BADNAME, "Nickname already in use\n"); - } else { - int len = strlen(args.nick); - if (len >= MAX_NICK_LEN-1) { - die(E_BADNAME, "No available nickname found\n"); + break; + case LIBIRC_RFC_ERR_NICKCOLLISION: + case LIBIRC_RFC_ERR_NICKNAMEINUSE: + if (args.force_nick) { + die(E_BADNAME, "Nickname already in use\n"); } else { - args.nick[len] = '_'; - args.nick[len+1] = 0; - // no need to rename self because the surface_name of ourself points - // to args.nick - rsl = irc_cmd_nick(session, args.nick); - info("Requested nick is taken, try %s\n", args.nick); + int len = strlen(args.nick); + if (len >= MAX_NICK_LEN-1) { + die(E_BADNAME, "No available nickname found\n"); + } else { + args.nick[len] = '_'; + args.nick[len+1] = 0; + // no need to rename self because the surface_name of ourself points + // to args.nick + rsl = irc_cmd_nick(session, args.nick); + info("Requested nick is taken, try %s\n", args.nick); + } + } + break ; + case LIBIRC_RFC_ERR_PASSWDMISMATCH: + ctx = (irc_ctx_t *) irc_get_ctx (session); + if (ctx->ready) { + // this could be about a wrong password for /oper, so make it non-fatal + info("Invalid or missing password.\n"); + } else { + // assuming this is about trying to connect to the server + die(E_PASSWORD, "Invalid or missing password when connecting to server.\n"); + } + break; + case LIBIRC_RFC_ERR_BADCHANNELKEY: + info("Cannot join %s: bad password.\n", params[1]); + break; + case LIBIRC_RFC_ERR_NOSUCHCHANNEL: + info("Cannot join %s: no such channel.\n", params[1]); + break ; + case LIBIRC_RFC_ERR_TOOMANYCHANNELS: + info("Cannot join %s: you have joined too many channels.\n", params[1]); + break ; + case LIBIRC_RFC_ERR_INVITEONLYCHAN: + info("Cannot join %s: channel is invite-only.\n", params[1]); + break ; + case LIBIRC_RFC_ERR_NOTONCHANNEL: + info("Cannot perform action in %s: not on channel.\n", params[1]); + break ; + case LIBIRC_RFC_ERR_CHANOPRIVSNEEDED: + info("Cannot perform action in %s: you're not channel operator.\n", params[1]); + break ; + case LIBIRC_RFC_ERR_CANNOTSENDTOCHAN: + info("Cannot send to %s.\n", params[1]); + break ; + case ERR_BADCHANNAME: + info("Could not join %s.\n", params[1]); + break ; + case LIBIRC_RFC_ERR_USERONCHANNEL: + info("Cannot invite %s to %s: %s is already on %s.\n", params[1], params[2], params[1], params[2]); + break ; + case LIBIRC_RFC_ERR_NOSUCHNICK: + info("No such nick/channel: %s\n", params[1]); + break ; + default: + if (!strcmp(event, "INVITE")) { + if (args.event_to == COMMAND) + mprintf("[%s] <%s> /invite %s %s\n", params[1], output_nick(origin), params[0], params[1]); + if (args.event_to == MESSAGE) + mprintf("[%s] -!- %s invited %s to join %s\n", params[1], output_nick(origin), params[0], params[1]); } } - } else if (atoi(event) == LIBIRC_RFC_ERR_PASSWDMISMATCH) { - irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session); - if (ctx->ready) { - // this could be about a wrong password for /oper, so make it non-fatal - info("Invalid or missing password.\n"); - } else { - // assuming this is about trying to connect to the server - die(E_PASSWORD, "Invalid or missing password when connecting to server.\n"); - } - } else if (atoi(event) == LIBIRC_RFC_ERR_BADCHANNELKEY) { - info("Cannot join %s: bad password.\n", params[1]); - } else if (atoi(event) == LIBIRC_RFC_ERR_NOSUCHCHANNEL) { - info("Cannot join %s: no such channel.\n", params[1]); - } else if (atoi(event) == LIBIRC_RFC_ERR_TOOMANYCHANNELS) { - info("Cannot join %s: you have joined too many channels.\n", params[1]); - } else if (atoi(event) == LIBIRC_RFC_ERR_INVITEONLYCHAN) { - info("Cannot join %s: channel is invite-only.\n", params[1]); - } else if (atoi(event) == LIBIRC_RFC_ERR_NOTONCHANNEL) { - info("Cannot perform action in %s: not on channel.\n", params[1]); - } else if (atoi(event) == LIBIRC_RFC_ERR_CHANOPRIVSNEEDED) { - info("Cannot perform action in %s: you're not channel operator.\n", params[1]); - } else if (atoi(event) == LIBIRC_RFC_ERR_CANNOTSENDTOCHAN) { - info("Cannot send to %s.\n", params[1]); - } else if (atoi(event) == ERR_BADCHANNAME) { - info("Could not join %s.\n", params[1]); - } else if (atoi(event) == LIBIRC_RFC_ERR_USERONCHANNEL) { - info("Cannot invite %s to %s: %s is already on %s.\n", params[1], params[2], params[1], params[2]); - } else if (atoi(event) == LIBIRC_RFC_ERR_NOSUCHNICK) { - info("No such nick/channel: %s\n", params[1]); - } else if (!strcmp(event, "INVITE")) { - if (args.event_to == COMMAND) - mprintf("[%s] <%s> /invite %s %s\n", params[1], output_nick(origin), params[0], params[1]); - if (args.event_to == MESSAGE) - mprintf("[%s] -!- %s invited %s to join %s\n", params[1], output_nick(origin), params[0], params[1]); - } - - if (count == 2) { + + switch (count ) { + case 2: debug("%s %s: %s %s\n", origin, event, params[0], params[1]); - } else if (count == 1) { + break; + case 1: debug("%s %s: %s\n", origin, event, params[0]); - } else { + break; + default: debug("Event \"%s\", origin: \"%s\", params: %d\n", event, - origin ? origin : "NULL", count); + origin ? origin : "NULL", count); } if (rsl > 0) { int error = irc_errno(session); info("(%d) %s when managing event: %s\n", - error, irc_strerror(error), event); + error, irc_strerror(error), event); } } @@ -1761,7 +1782,7 @@ irc_session_t* do_connect() irc_session_t *s; irc_callbacks_t callbacks; memset (&callbacks, 0, sizeof(callbacks)); - + args.ready = 0 ; callbacks.event_connect = event_connect; callbacks.event_nick = event_nick; callbacks.event_quit = event_quit; @@ -1885,7 +1906,7 @@ static void* fifo_in_thread (void *arg) { int start (int max_wait) { irc_session_t * s; - pthread_t tid_irc, tid_in; + pthread_t tid_irc; int i; int waiting = 0; int cont = 1; @@ -1900,9 +1921,6 @@ int start (int max_wait) debug("Starting threads...\n"); if (pthread_create (&tid_irc, 0, irc_thread, (void*) s) != 0) die(E_THREAD, "Could not create thread: %s\n", strerror(errno)); - if (pthread_create (&tid_in, 0, fifo_in_thread, (void*) NULL) != 0) - die(E_THREAD, "Could not create thread: %s\n", strerror(errno)); - debug("Threads started!\n"); if (args.show_inferred && args.default_destination == DEFAULT_LAST_OUT) @@ -1939,6 +1957,16 @@ int start (int max_wait) if (args.ready > 0) { debug("[main] Starting loop\n"); + + for (i = 0; i < fifos.n ; i++) { + if( fifos.chans[i][0] == '#') { + debug("[reboot] rejoigning %s\n",fifos.chans[i]); + int pw_pos = get_password(fifos.chans[i]); + rsl = irc_cmd_join(s, fifos.chans[i], pw_pos >= 0 ? fifos.chans[i] + pw_pos + 1 : NULL); + revert_password(fifos.chans[i], pw_pos); + } + } + while (cont) { n_results = pop_fifo_set(&fifos, &results); debug("[main] I popped %d results\n", n_results); @@ -1973,6 +2001,8 @@ int start (int max_wait) pthread_mutex_unlock(&args.mutex); if (cont && val <= 0) { info("Connection lost, reconnecting...\n"); + irc_disconnect(s); + sleep(1); return start(max_wait); } @@ -2035,6 +2065,13 @@ int main (int argc, char **argv) init_fifo_set(&fifos); saw_user(args.nick); + // start stdin thread + pthread_t tid_in ; + if (pthread_create (&tid_in, 0, fifo_in_thread, (void*) NULL) != 0) + die(E_THREAD, "Could not create thread: %s\n", strerror(errno)); + + + //return test_fifo_set(); // start trying to connect with the initial retry interval