irctk

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

commit 6b2f7e5c2a6b9e91e2d4d9b7679321e2c09175af
parent c50d55c1d9e83a0794af0bc87040cdaca0edc292
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Sat, 28 Jul 2012 01:15:46 +0200

cleanup, avoid double \n\n's in info messages

Diffstat:
irctk.c | 174+++++++++++++++++++++++++++++++------------------------------------------------
1 file changed, 67 insertions(+), 107 deletions(-)

diff --git a/irctk.c b/irctk.c @@ -402,7 +402,6 @@ static error_t parse_opt (int key, char *arg, struct argp_state *state) } else { if (arguments->channels == NULL) { - //debug("start with %s, at %d", arg, state->arg_num); arguments->channels = state->argv + (state->next - 1); } arguments->n_channels++; @@ -431,7 +430,6 @@ void message(int verbosity, const char *err, va_list ap) fprintf(stderr, "%s ", verbosity > 0 ? "irctk: [debug]" : verbosity >= 0 ? "irctk:" : "irctk: [error]"); vfprintf(stderr, err, ap); - fprintf(stderr, "\n"); } if (verbosity < 0) exit(1-verbosity); @@ -469,14 +467,6 @@ int mprintf(const char *fmt, ...) } -void debug_args() -{ - debug("n_channels = %d\ninterval = %d\nnick = %s\nserver = %s\nport = %d\n" - "pw = %s\n", args.n_channels, args.interval, args.nick, args.server, - args.port, args.password); -} - - typedef struct line { char *line; char *full_line; @@ -585,7 +575,7 @@ int empty_fifo_set(fifo_set *s) { } void realloc_chans(fifo_set *s) { - debug("realloc chans"); + debug("realloc_chans()\n"); s->allocated *= 2; s->chans = realloc(s->chans, s->allocated*sizeof(char*)); assert(s->chans); @@ -617,12 +607,8 @@ int push_chan(fifo_set *s, const char *chan) { int scan_collection(fifo_set *s, const char **collection, const char *chan) { int i; - // debug("asking for ioc for <%s>", chan); for (i=0; i<s->n; i++) { - // debug("compare to %d of %d value %s surface %s", i, s->n, - // s->chans[i], s->surface_name[i]); if (!strcmp(collection[i], chan)) { - // debug("found ioc %d", i); return i; } } @@ -653,13 +639,11 @@ int index_of_chan(fifo_set *s, const char *chan) { int rename_user(fifo_set *s, const char *from, const char *to) { int pos; - debug("rename %s -> %s", from, to); + debug("rename_user %s -> %s\n", from, to); pthread_mutex_lock(&s->ctrl.mutex); pos = scan_collection(s, (const char **) s->surface_name, from); assert(pos >= 0); - debug("found %s at pos %d", from, pos); strncpy(s->surface_name[pos], to, MAX_NICK_LEN-1); - debug("set surface name to %s", to); pthread_mutex_unlock(&s->ctrl.mutex); return pos; } @@ -686,7 +670,6 @@ int register_nick(fifo_set *s, const char *nick) { pthread_mutex_lock(&s->ctrl.mutex); // we assume that nick is not a current surface nick if (scan_collection(s, (const char **) s->chans, nick) < 0) { - debug("No need to generate fresh name."); result = push_chan(s, nick); } else { // nick is already taken, generate a fresh id @@ -696,12 +679,12 @@ int register_nick(fifo_set *s, const char *nick) { strncpy(new, nick, MAX_NICK_LEN/2); sprintf(num, "%d", suffix++); strncat(new, num, MAX_NICK_LEN/2-1); - debug("Trying to register under %s", new); + debug("Trying to register under %s\n", new); if (!nick_exists(s, new)) { break; } } - debug("Register under %s %s", new, nick); + debug("Register under %s %s\n", new, nick); result = push_chan_alias(s, new, nick); } pthread_mutex_unlock(&s->ctrl.mutex); @@ -719,8 +702,8 @@ void push_one(fifo_set *s, char *fl, char *dest, char *l, fifo *f = &(s->fifos[c]); if (s->was_pushed[c]) return; // already pushed - debug("PUSH_ONE \"%s\" (%p), nextdep %d %d ioc %d position %d", l, l, next_dep_fifoidx, next_dep_lineidx, c, - f->tl); + debug("PUSH_ONE \"%s\" (%p), nextdep %d %d ioc %d position %d\n", + l, l, next_dep_fifoidx, next_dep_lineidx, c, f->tl); s->was_pushed[c] = 1; f->queue[f->tl].line = l; f->queue[f->tl].full_line = fl; @@ -730,7 +713,7 @@ void push_one(fifo_set *s, char *fl, char *dest, char *l, f->queue[f->tl].is_sendable = 0; *fifoidx = c; *lineidx = f->tl; - debug("pushed one, returning next dep %d %d", *fifoidx, *lineidx); + debug("pushed one, returning next dep %d %d\n", *fifoidx, *lineidx); f->tl++; f->tl %= STDIN_BUFFER_SIZE; } @@ -738,8 +721,6 @@ void push_one(fifo_set *s, char *fl, char *dest, char *l, void set_last_idxes(fifo_set *s, char *chan, int fifoidx, int lineidx) { int c = index_of_chan(s, chan); fifo *f = &(s->fifos[c]); - debug("set_last_idxes %d %d for \"%s\" ioc %d position %d", fifoidx, lineidx, - f->queue[(f->tl - 1 + STDIN_BUFFER_SIZE) % STDIN_BUFFER_SIZE].line, c, (f->tl - 1 + STDIN_BUFFER_SIZE) % STDIN_BUFFER_SIZE); f->queue[(f->tl - 1 + STDIN_BUFFER_SIZE) % STDIN_BUFFER_SIZE].next_dep_fifoidx = fifoidx; f->queue[(f->tl - 1 + STDIN_BUFFER_SIZE) % STDIN_BUFFER_SIZE].next_dep_lineidx = lineidx; } @@ -759,14 +740,14 @@ void pre_push(fifo_set *s) { void post_push(fifo_set *s, int was_empty) { if (was_empty) { - debug("signal for empty"); + debug("signal for empty\n"); pthread_cond_signal(&s->ctrl.empty); } pthread_mutex_unlock(&s->ctrl.mutex); } void push_fifo_set(fifo_set *s, char *fl, char *dests, char *l) { - debug("push_fifo_set full_line \"%s\" (%p) line \"%s\" (%p) dests %s", + debug("push_fifo_set full_line \"%s\" (%p) line \"%s\" (%p) dests %s\n", fl, fl, l, l, dests); int was_empty; @@ -787,7 +768,7 @@ void push_fifo_set(fifo_set *s, char *fl, char *dests, char *l) { cont = 0; if (i) dests[i] = 0; - debug("will push one \"%s\"", l); + debug("will push one \"%s\"\n", l); // only put the full line for *one* occurrence push_one(s, first_fifoidx < 0?fl:NULL, last_dest, l, fifoidx, lineidx, &nfifoidx, &nlineidx); fifoidx = nfifoidx; @@ -796,23 +777,18 @@ void push_fifo_set(fifo_set *s, char *fl, char *dests, char *l) { first_fifoidx = fifoidx; first_lineidx = lineidx; first_dest = last_dest; - // debug("first_ptr is %p full line %s, first_dest is %s", first_ptr, - // first_ptr->full_line, first_dest); } last_dest = dests + i + 1; } i++; } - //debug("DDD %s", s->fifos[0].queue[0].line); - debug("finished at i:%d", i); - - // debug("first_ptr is %p full line %s, first_dest is %s", first_ptr, - // first_ptr->full_line, first_dest); + set_last_idxes(s, first_dest, fifoidx, lineidx); post_push(s, was_empty); } +/* TODO refactor */ void push_fifo_set_all(fifo_set *s, char *fl, char **dests, int ndests, char *l) { pre_push(s); int was_empty; @@ -827,11 +803,7 @@ void push_fifo_set_all(fifo_set *s, char *fl, char **dests, int ndests, char *l) fifoidx = nfifoidx; lineidx = nlineidx; } - //debug("DDD %s", s->fifos[0].queue[0].line); - debug("finished at i:%d", i); - // debug("first_ptr is %p full line %s, first_dest is %s", first_ptr, - // first_ptr->full_line, first_dest); set_last_idxes(s, dests[0], fifoidx, lineidx); post_push(s, was_empty); @@ -854,9 +826,6 @@ int mark_sendable_line(fifo_set *s, line *l) { if (l->is_sendable) return 1; l->is_sendable = 1; - debug("i am (%p) %s depending on (%d %d)", l, l->line, l->next_dep_fifoidx, - l->next_dep_lineidx); - //assert(l->next_dep == l); line* ptr = &(s->fifos[l->next_dep_fifoidx].queue[l->next_dep_lineidx]); return mark_sendable_line(s, ptr); } @@ -886,10 +855,7 @@ void pop_if_sendable(fifo_set *s, int c, int *n_result, action *result) { f->hd %= STDIN_BUFFER_SIZE; } else { // don't pop sentinel - //debug("don't pop: channel %d position %d was_sendable? %d, line %p", c, f->hd, - // f->queue[f->hd].is_sendable, - // f->queue[f->hd].line); - debug("don't pop channel %d", c); + debug("don't pop channel %d\n", c); } } @@ -899,16 +865,16 @@ int pop_fifo_set(fifo_set *s, action **result) { int i; pthread_mutex_lock(&s->ctrl.mutex); while (empty_fifo_set(s)) { - debug("wait because empty"); + debug("wait because empty\n"); pthread_cond_wait(&s->ctrl.empty, &s->ctrl.mutex); } // now fifo isn't empty - debug("now fifo isn't empty"); + debug("now fifo isn't empty\n"); was_full = full_fifo_set(s); mark_heads(s); for (i=0; i<s->n; i++) { - debug("doing fifo for chan %s", s->chans[i]); - debug("fifo hd %d tl %d", s->fifos[i].hd, s->fifos[i].tl); + debug("doing fifo for chan %s\n", s->chans[i]); + debug("fifo hd %d tl %d\n", s->fifos[i].hd, s->fifos[i].tl); if (!mark_sendable(s, &s->fifos[i])) unmark_sendable(s, &s->fifos[i]); } @@ -919,12 +885,12 @@ int pop_fifo_set(fifo_set *s, action **result) { pop_if_sendable(s, i, &n_result, *result); if (!n_result) { // only thing left is sentinel, return sentinel - debug("only thing to return is sentinel"); + debug("only thing to return is sentinel\n"); n_result = 1; result[0]->line = NULL; result[0]->full_line = NULL; } - debug("prepared %d results to return", n_result); + debug("prepared %d results to return\n", n_result); if (was_full) pthread_cond_signal(&s->ctrl.full); pthread_mutex_unlock(&s->ctrl.mutex); @@ -1077,16 +1043,14 @@ int do_cmd_msg(irc_session_t *s, char *chan, char* line) } if (to_join) { if (args.join) { - debug("join %s", to_join); rsl = join_channel(s, to_join); } else { - info("Will not join because --no-auto-join was set"); + info("Will not join because --no-auto-join was set\n"); } } else { info("Unrecognized command: %s", line); } } else if ((arg = MATCH_CMD(line, "topic"))) { - debug("topic %s", arg); rsl = irc_cmd_topic(s, chan, arg); } else if ((arg = MATCH_CMD0(line, "quit"))) { char *rquit = NULL; @@ -1148,7 +1112,6 @@ int do_cmd_msg(irc_session_t *s, char *chan, char* line) strncpy(lline, line, MAX_NICK_LEN-1); else strncpy(lline, fifos.surface_name[c], MAX_NICK_LEN-1); - debug("CURRENT LINE %s", lline); pthread_mutex_unlock(&fifos.ctrl.mutex); line[seppos] = sep; strncat(lline, line + seppos, MAX_LINE_LEN-1); @@ -1169,7 +1132,7 @@ void cmd_msg(char *full_line, char *target, char *line) { char *msg; - debug("cmd_msg %s \"%s\" (%p), last_in %s", target, line, line, args.last_chan_in); + debug("cmd_msg %s \"%s\" (%p), last_in %s\n", target, line, line, args.last_chan_in); if (args.track != NO) { char lline[MAX_LINE_LEN + MAX_NICK_LEN + 2]; @@ -1195,7 +1158,6 @@ void cmd_msg(char *full_line, char *target, char *line) line[seppos] = sep; if (c >= 0) { strncpy(lline, line + seppos, MAX_LINE_LEN-1); - debug("COPY TO LLINE"); free(full_line); line = lline; full_line = lline; @@ -1233,7 +1195,7 @@ void cmd_msg(char *full_line, char *target, char *line) args.last_nick_in[0] = 0; } } - debug("last_chan_in is %s", args.last_chan_in); + debug("last_chan_in is %s\n", args.last_chan_in); return push_fifo_set(&fifos, full_line, args.last_chan_in[0] ? args.last_chan_in : first_chan(), line); @@ -1259,7 +1221,7 @@ void saw_user(const char *nick) { if (!nick) return; if (!surface_nick_exists(&fifos, nick)) { // unknown user, register him - debug("Register this new user"); + debug("Register this new user\n"); register_nick(&fifos, nick); } } @@ -1280,50 +1242,50 @@ void manage_event (irc_session_t *session, const char *event, const char *origin if (atoi(event) == LIBIRC_RFC_ERR_ERRONEUSNICKNAME) { if (args.force_nick) { - die(E_BADNAME, "Invalid nick"); + die(E_BADNAME, "Invalid nick\n"); } else { // TODO does not work - info("Invalid nick, reverting to \"irctk\""); + info("Invalid nick, reverting to \"irctk\"\n"); strncpy(args.nick, "irctk", MAX_NICK_LEN-1); rsl = irc_cmd_nick(session, "irctk"); } } 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"); + 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"); + 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, args.nick); + info("Requested nick is taken, try %s\n", args.nick); } } } else if (atoi(event) == LIBIRC_RFC_ERR_BADCHANNELKEY) { - info("Cannot join %s: bad password.", params[1]); + info("Cannot join %s: bad password.\n", params[1]); } else if (atoi(event) == LIBIRC_RFC_ERR_NOSUCHCHANNEL) { - info("Cannot join %s: no such channel.", params[1]); + 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.", params[1]); + 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.", params[1]); + 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.", params[1]); + 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.", params[1]); + 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.", params[1]); + info("Cannot send to %s.\n", params[1]); } else if (atoi(event) == ERR_BADCHANNAME) { - info("Could not join %s.", params[1]); + 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.", params[1], params[2], params[1], params[2]); + 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", params[1]); + 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]); @@ -1341,12 +1303,12 @@ void manage_event (irc_session_t *session, const char *event, const char *origin } else if (count == 1) { debug("%s %s: %s\n", origin, event, params[0]); } else { - debug("Event \"%s\", origin: \"%s\", params: %d", event, + debug("Event \"%s\", origin: \"%s\", params: %d\n", event, origin ? origin : "NULL", count); } if (rsl > 0) { int error = irc_errno(session); - info("(%d) %s when managing event: %s", + info("(%d) %s when managing event: %s\n", error, irc_strerror(error), event); } } @@ -1358,7 +1320,6 @@ void event_nick (irc_session_t *session, const char *event, const char *origin, debug("[] <%s> /nick %s\n", origin, params[0]); saw_user(origin); rename_user(&fifos, origin, params[0]); - debug("EVENTS_TO events-to %d", args.event_to); if (args.event_to == COMMAND) mprintf("[] <%s> /nick %s\n", output_nick(origin), params[0]); if (args.event_to == MESSAGE) @@ -1451,10 +1412,10 @@ void event_connect (irc_session_t *session, const char *event, const char *origi manage_event(session, event, origin, params, count); ctx->ready = 1; - debug("Connected!"); + debug("Connected!\n"); for (i = 0; i < ctx->n_channels; i++) { - debug("Attempt to join %s", ctx->channels[i]); + debug("Attempt to join %s\n", ctx->channels[i]); join_channel (session, ctx->channels[i]); } } @@ -1582,19 +1543,18 @@ irc_session_t* do_connect() s = irc_create_session (&callbacks); if (!s) - die(E_SESSION, "Could not create session"); + die(E_SESSION, "Could not create session\n"); if (!args.with_host) irc_option_set (s, LIBIRC_OPTION_STRIPNICKS); irc_set_ctx (s, &args); - debug_args(); - debug("Connecting to %s port %d with nick %s...", + debug("Connecting to %s port %d with nick %s...\n", args.server, args.port, args.nick); if (irc_connect(s, args.server, args.port, args.password, args.nick, args.username, args.realname)) - die(E_CONNECT, "Could not connect: %s", irc_strerror (irc_errno(s))); + die(E_CONNECT, "Could not connect: %s\n", irc_strerror (irc_errno(s))); return s; } @@ -1616,8 +1576,8 @@ static void* fifo_in_thread (void *arg) { size_t size; while ((res = getline(&line, (size_t*) &size, stdin)) != -1) { - debug("[thread_in] READS %s", line); - debug("someone will have to free %p", line); + debug("[thread_in] READS %s\n", line); + debug("someone will have to free %p\n", line); if (line[0] == '[') { int i=0; char *msg; @@ -1628,18 +1588,18 @@ static void* fifo_in_thread (void *arg) { while (line[i] != ']' && line[i]) i++; if (!line[i]) - die(E_BADLINE, "Malformed address prefix"); + die(E_BADLINE, "Malformed address prefix\n"); msg = line + i + 1; if (msg[0] == ' ') msg++; if (i) line[i] = 0; - debug("[thread_in] calling with line target msg %p %p %p", line, target, msg); + debug("[thread_in] calling with line target msg %p %p %p\n", line, target, msg); cmd_msg(line, target, msg); strncpy(args.last_chans_out, target, MAX_CHANS_LEN-1); } else { /* No target specified, we attempt the default */ - debug("[thread_in] calling with line target msg %p %p %p", line, "", line); + debug("[thread_in] calling with line target msg %p %p %p\n", line, "", line); cmd_msg(line, "", line); } @@ -1647,7 +1607,7 @@ static void* fifo_in_thread (void *arg) { line = NULL; } // sentinel - debug("[thread_in] pushed sentinel"); + debug("[thread_in] pushed sentinel\n"); push_fifo_set(&fifos, NULL, "", NULL); free(line); // which wasn't used return 0; @@ -1668,15 +1628,15 @@ int start (int max_wait) s = do_connect(); - debug("Connection request successful!"); + debug("Connection request successful!\n"); - debug("Starting threads..."); + debug("Starting threads...\n"); if (pthread_create (&tid_irc, 0, irc_thread, (void*) s) != 0) - die(E_THREAD, "Could not create thread: %s", strerror(errno)); + 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", strerror(errno)); + die(E_THREAD, "Could not create thread: %s\n", strerror(errno)); - debug("Thread started!"); + debug("Threads started!\n"); if (args.show_inferred && args.default_destination == DEFAULT_LAST_OUT) fprintf(stderr, "[%s] ", args.last_chans_out); @@ -1685,16 +1645,16 @@ int start (int max_wait) if (!args.ready) { waiting = 0; - debug("Waiting for the connection to be ready..."); + debug("Waiting for the connection to be ready...\n"); while (waiting < max_wait && !args.ready) { - debug("waiting (%d)", waiting); + debug("waiting (%d)\n", waiting); sleep(1); waiting++; } if (!args.ready) { debug("nick is %s\n", args.nick); int new_max_wait = max_wait * args.retry_factor; - info("Connection timed out after %d seconds, retrying for %d seconds...", + info("Connection timed out after %d seconds, retrying for %d seconds...\n", max_wait, new_max_wait); irc_disconnect(s); args.ready = 0; @@ -1703,16 +1663,16 @@ int start (int max_wait) } } - debug("[main] Starting loop"); + debug("[main] Starting loop\n"); while (cont) { n_results = pop_fifo_set(&fifos, &results); - debug("[main] I popped %d results", n_results); + debug("[main] I popped %d results\n", n_results); for (i = 0; i < n_results; i++) { if (!results[i].line) { - debug("[main] Sentinel seen, we will exit"); + debug("[main] Sentinel seen, we will exit\n"); cont = 0; // sentinel } else { - debug("[main] manage line %s %s, pointer %p", + debug("[main] manage line %s %s, pointer %p\n", results[i].destination, results[i].line, results[i].full_line ); rsl = do_cmd_msg(s, results[i].destination, results[i].line); if (rsl > 0) { @@ -1731,9 +1691,9 @@ int start (int max_wait) if (results[i].full_line) free(results[i].full_line); free(results); - debug("[main] I managed my lines"); + debug("[main] I managed my lines\n"); if (cont && !args.ready) { - info("Connection lost, reconnecting..."); + info("Connection lost, reconnecting...\n"); return start(max_wait); } @@ -1743,10 +1703,10 @@ int start (int max_wait) if (cont) usleep(args.interval); - debug("[main] endloop"); + debug("[main] endloop\n"); } - debug("[main] exiting"); + debug("[main] exiting\n"); usleep(args.interval_after); irc_disconnect(s);