irctk

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

commit a8f47feea9525af8d8e402f83443d6b74f155b20
parent 335ceef725d8147175c6c947d422031ac49860a4
Author: Antoine Amarilli <a3nm@a3nm.net>
Date:   Sun, 17 Jun 2012 02:38:43 +0200

fixes to fifo_in, it now works (but we still need to limit per channel)

Diffstat:
irctk.c | 57++++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 40 insertions(+), 17 deletions(-)

diff --git a/irctk.c b/irctk.c @@ -450,8 +450,9 @@ void debug_args() typedef struct line { char* line; - char* last_in; - char* last_out; + char last_chan_in[MAX_LEN]; + char last_chans_out[MAX_LEN]; + char last_nick_in[MAX_LEN]; } line; typedef struct fifo { @@ -486,6 +487,14 @@ line pop(fifo *f) { // now fifo isn't empty was_full = full(f); result = f->queue[f->hd]; + /*debug("state 0:%s 1:%s 2:%s 3:%s 4:%s\n", + f->queue[0].line, + f->queue[1].line, + f->queue[2].line, + f->queue[3].line, + f->queue[4].line + );*/ + debug("popped %s at %d", result.line, f->hd); f->hd++; f->hd %= LINE_BUFFER; if (was_full) @@ -494,7 +503,7 @@ line pop(fifo *f) { return result; } -void push(fifo *f, char* l, char* li, char* lo) { +void push(fifo *f, char* l, char* li, char* lni, char* lo) { int was_empty; pthread_mutex_lock(&f->mutex); while (full(f)) @@ -502,8 +511,17 @@ void push(fifo *f, char* l, char* li, char* lo) { // now fifo isn't full was_empty = empty(f); f->queue[f->tl].line = l; - f->queue[f->tl].last_in = li; - f->queue[f->tl].last_out = lo; + strncpy(f->queue[f->tl].last_chan_in, li, MAX_LEN-1); + strncpy(f->queue[f->tl].last_nick_in, lni, MAX_LEN-1); + strncpy(f->queue[f->tl].last_chans_out, lo, MAX_LEN-1); + debug("pushed %s at %d\n", l, f->tl); + /*debug("state 0:%s 1:%s 2:%s 3:%s 4:%s\n", + f->queue[0].line, + f->queue[1].line, + f->queue[2].line, + f->queue[3].line, + f->queue[4].line + );*/ f->tl++; f->tl %= LINE_BUFFER; if (was_empty) @@ -639,7 +657,7 @@ int cmd_msg_chan(irc_session_t *s, char *target, char* line) int cont = 1; char tmp; char *one_target; - int rsl; + int rsl = 0; debug("cmd_msg_chan %s %s\n", target, line); one_target = target; @@ -670,7 +688,7 @@ int cmd_msg(irc_session_t *s, char* target, line l) int ret; char *msg[2*MAX_LEN+2]; - debug("cmd_msg %s %s, last_in %s", target, l.line, l.last_in); + debug("cmd_msg %s %s, last_in %s", target, l.line, l.last_chan_in); /* Manage the fact that target may be "" */ if (!target[0]) @@ -681,24 +699,25 @@ int cmd_msg(irc_session_t *s, char* target, line l) return cmd_msg_chan(s, first_chan(), l.line); case DEFAULT_LAST_IN: - if (args.show_nick_prefix && args.last_nick_in[0] && args.last_chan_in[0] == '#') + if (args.show_nick_prefix && l.last_nick_in[0] + && l.last_chan_in[0] == '#') { if (l.line[0] != '\n') { msg[0] = 0; - strncat((char*) msg, args.last_nick_in, MAX_LEN-1); + strncat((char*) msg, l.last_nick_in, MAX_LEN-1); strcat((char*) msg, ": "); strncat((char*) msg, l.line, MAX_LEN-1); l.line = (char*) msg; } else { - args.last_nick_in[0] = 0; + l.last_nick_in[0] = 0; } } - debug("will cmd_msg_chan %s %s\n", l.last_in, l.line); - return cmd_msg_chan(s, l.last_in, l.line); + debug("will cmd_msg_chan %s %s\n", l.last_chan_in, l.line); + return cmd_msg_chan(s, l.last_chan_in, l.line); case DEFAULT_LAST_OUT: - return cmd_msg_chan(s, l.last_out, l.line); + return cmd_msg_chan(s, l.last_chans_out, l.line); case DEFAULT_ALL: //TODO also for chans joined at runtime @@ -1010,10 +1029,13 @@ static void* fifo_in_thread (void *arg) { while ((res = getline((char**) &line, (size_t*) &size, stdin)) != -1) { debug("i read and push %s\n", line); - push(&fifo_in, line, args.last_chan_in, args.last_chans_out); + push(&fifo_in, line, args.last_chan_in, args.last_nick_in, + args.last_chans_out); + // reallocate line, the popper will free the line + line = (char*) malloc(size+1); } // sentinel - push(&fifo_in, NULL, NULL, NULL); + push(&fifo_in, NULL, NULL, NULL, NULL); free(line); return 0; } @@ -1110,17 +1132,18 @@ int start (int max_wait) l.line = msg; cmd_msg(s, target, l); l.line = tmp; - strncpy(l.last_out, target, MAX_LEN-1); + strncpy(l.last_chans_out, target, MAX_LEN-1); } else { /* No target specified, we attempt the default */ cmd_msg(s, "", l); } if (args.show_inferred && args.default_destination == DEFAULT_LAST_OUT) - fprintf(stderr, "[%s] ", l.last_out); + fprintf(stderr, "[%s] ", l.last_chans_out); usleep(args.interval); debug("endloop"); + free(l.line); } debug("exiting");