irctk

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

commit 4b8fff2f21ac86258630fe7b6bc58ba52121b89a
parent 01d7656a4b49aae5789f16db53b6dfe5695d4618
Author: Antoine Amarilli <ant.amarilli@free.fr>
Date:   Sun, 16 Jan 2011 04:30:41 +0100

done default targets and stderr prefix

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

diff --git a/irctk.c b/irctk.c @@ -76,7 +76,7 @@ static struct argp_option options[] = { "Translate server events to messages with \"/quit\", \"/me\"..." }, /* TODO */ {"event-to-message", 'm', 0, 0, "Translate server events to a human-readable description" }, /* TODO */ - {"with-host", 'h', 0, 0, + {"with-host", 'w', 0, 0, "Keep the host info in pseudos" }, /* TODO, and make compatible with --own */ {"own", 'o', 0, 0, @@ -90,7 +90,7 @@ static struct argp_option options[] = { "Post messages to the last active channel by default" }, {"default-last-posted", 'p', 0, 0, "Post messages to the last posted channel by default" }, - {"show-inferred", 'I', 0, 0, + {"show-inferred", 'P', 0, 0, "When inferring a next chan with --default-last-posted, display it on stderr" }, /* TODO for this option, it would be cool to display the channel name on stderr, for the poor man's client*/ {"synchronous", 's', 0, 0, @@ -123,6 +123,7 @@ struct arguments char last_chan_in[MAX_LEN]; char last_chans_out[MAX_LEN]; int default_destination; + int show_prefix; }; @@ -142,6 +143,8 @@ void initialize_args() args.port = 6667; args.last_chan_in[0] = 0; args.last_chans_out[0] = 0; + args.default_destination = DEFAULT_LAST_IN; + args.show_prefix = 0; } void die(int val, const char *err, ...) @@ -167,82 +170,170 @@ void debug(const char *err, ...) } } +char* first_chan() +{ + if (args.n_channels) + return args.channels[0]; + else return args.nick; +} + +int do_cmd_msg(irc_session_t *s, const char* chan, const char* line) +{ + int rsl; + /* TODO ask the server to notify, if possible */ + /* TODO find a way to get real host name (part after !) */ + + rsl = irc_cmd_msg (s, chan, line); + /* TODO manage errors */ + if (rsl) + return rsl; + if (args.own) + { + printf("[%s] <%s> %s", chan, args.nick, line); + fflush(stdout); + } + + return 0; +} + +int cmd_msg(irc_session_t *s, char* target, const char* line) +{ + int i = 0; + int cont = 1; + char tmp; + char *one_target; + int rsl; + + /* Manage the fact that target may be "" */ + if (!target[0]) + { + switch(args.default_destination) + { + case DEFAULT_FIRST: + target = first_chan(); + break; + + case DEFAULT_LAST_IN: + target = args.last_chan_in; + break; + + case DEFAULT_LAST_OUT: + target = args.last_chans_out; + break; + } + } + + one_target = target; + + while (cont) + { + if (target[i] == ',' || !target[i]) + { + if (!target[i]) + cont = 0; + tmp = target[i]; + target[i] = 0; + /* TODO manage errors correctly! */ + rsl = do_cmd_msg(s, one_target, line); + target[i] = tmp; + one_target = target + i + 1; + } + i++; + } + + return rsl; +} + + + /* Parse a single option. */ -static error_t + static error_t parse_opt (int key, char *arg, struct argp_state *state) { - /* Get the input argument from argp_parse, which we - know is a pointer to our arguments structure. */ - struct arguments *arguments = state->input; - int i; - - if (key != ARGP_KEY_ARG && key != ARGP_KEY_END && (! arguments->channels)) - { - switch (key) - { - case 'v': - arguments->verbose = 1; - break; - case 'i': - // TODO use float, and be more robust - debug ("seen i, nchan is %d", arguments->n_channels); - arguments->interval = atoi(arg); - break; - case 'o': - arguments->own = 1; - break; - - case ARGP_KEY_NO_ARGS: - argp_usage (state); - - default: - return ARGP_ERR_UNKNOWN; - } + /* Get the input argument from argp_parse, which we + know is a pointer to our arguments structure. */ + struct arguments *arguments = state->input; + int i; + + if (key != ARGP_KEY_ARG && key != ARGP_KEY_END && (! arguments->channels)) + { + switch (key) + { + case 'v': + arguments->verbose = 1; + break; + case 'i': + // TODO use float, and be more robust + debug ("seen i, nchan is %d", arguments->n_channels); + arguments->interval = atoi(arg); + break; + case 'o': + arguments->own = 1; + break; + case 'f': + arguments->default_destination = DEFAULT_FIRST; + break; + case 'a': + arguments->default_destination = DEFAULT_LAST_IN; + break; + case 'p': + arguments->default_destination = DEFAULT_LAST_OUT; + break; + case 'P': + arguments->show_prefix = 1; + break; + + case ARGP_KEY_NO_ARGS: + argp_usage (state); + + default: + return ARGP_ERR_UNKNOWN; + } } else { switch(key) { /* TODO option exclusion doesn't work! */ - case ARGP_KEY_ARG: - debug("saw an arg"); - if (state->arg_num == 0) - { - i = -1; - arguments->raw_dest = arg; - arguments->server = arg; - while (arguments->raw_dest[++i]) - { - if (arguments->raw_dest[i] == '@') - { - arguments->raw_dest[i] = 0; - arguments->nick = arguments->raw_dest; - arguments->server = arguments->raw_dest + i + 1; - } - if (arguments->raw_dest[i] == ':') - { - arguments->raw_dest[i] = 0; - arguments->port = atoi(arguments->raw_dest + i + 1); - } - } - } - 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++; - } - break; - - case ARGP_KEY_END: - if (state->arg_num < 1) - /* Not enough arguments. */ - argp_usage (state); - break; + case ARGP_KEY_ARG: + debug("saw an arg"); + if (state->arg_num == 0) + { + i = -1; + arguments->raw_dest = arg; + arguments->server = arg; + while (arguments->raw_dest[++i]) + { + if (arguments->raw_dest[i] == '@') + { + arguments->raw_dest[i] = 0; + arguments->nick = arguments->raw_dest; + arguments->server = arguments->raw_dest + i + 1; + } + if (arguments->raw_dest[i] == ':') + { + arguments->raw_dest[i] = 0; + arguments->port = atoi(arguments->raw_dest + i + 1); + } + } + } + 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++; + } + break; + + case ARGP_KEY_END: + if (state->arg_num < 1) + /* Not enough arguments. */ + argp_usage (state); + break; } } - return 0; + return 0; } /* Our argp parser. */ @@ -528,6 +619,9 @@ int start () line = (char*) malloc(size+1); + if (args.show_prefix && args.default_destination == DEFAULT_LAST_OUT) + fprintf(stderr, "[%s] ", args.last_chans_out); + while ( (res = getline((char**) &line, (size_t*) &size, stdin)) != -1 ) { while (!args.ready) @@ -539,7 +633,8 @@ int start () } debug("got %s", line); - if (line[0] == '[' && line[1] != ']') + + if (line[0] == '[') { int i=0; char *msg; @@ -549,51 +644,15 @@ int start () while (line[i++] != ']'); msg = line + i + 1; line[i-1] = 0; - strcpy(args.last_chans_out, line); - line[i-1] = ']'; - - i = 0; - - int cont = 1; - - while (cont) - { - if (line[i] == ',' || line[i] == ']') - { - if (line[i] == ']') cont = 0; - line[i] = 0; - // TODO delay? - // TODO don't do that each time! - irc_cmd_join (s, target, 0); - if ( irc_cmd_msg (s, target, msg) ) - break; // <- TODO NO - if (args.own) /* TODO ask the server to notify? or at least refactor and get the real pseudo with '!' and chan etc.*/ - printf("[%s] <%s> %s", target, args.nick, msg); - target = line + i + 1; - } - i++; - } + cmd_msg(s, target, msg); + strcpy(args.last_chans_out, target); } else { /* No target specified, we attempt the default */ - /* TODO WRITE BETTER DEFAULT */ - if (line[0] == '[') - line += 2; // we start with '[]' - /* - if (args.n_channels) - { - if ( irc_cmd_msg (s, args.channels[0], line) ) - break; - if (args.own) // TODO ask the server to notify? or at least refactor and get the real pseudo with '!' and chan etc - printf("[%s] <%s> %s", args.channels[0], args.nick, line); - } - else debug("No channel to send to!"); - */ - if ( irc_cmd_msg (s, args.last_chan_in, line) ) - break; - if (args.own) // TODO ask the server to notify? or at least refactor and get the real pseudo with '!' and chan etc - printf("[%s] <%s> %s", args.last_chan_in, args.nick, line); + cmd_msg(s, "", line); } + if (args.show_prefix && args.default_destination == DEFAULT_LAST_OUT) + fprintf(stderr, "[%s] ", args.last_chans_out); sleep(args.interval); } @@ -611,23 +670,14 @@ int main (int argc, char **argv) initialize_args(); debug ("isatty? %s\n", isatty(fileno(stdin))?"yes":"no"); - /* Parse our arguments; every option seen by parse_opt will - be reflected in arguments. */ + argp_parse (&argp, argc, argv, 0, 0, &args); - if (!args.n_channels) - { - strcpy(args.last_chan_in, args.nick); - strcpy(args.last_chans_out, args.nick); - } else { - strcpy(args.last_chan_in, args.channels[0]); - strcpy(args.last_chans_out, args.channels[0]); - } - if(args.verbose) - { - fprintf (stderr, "n_channels = %d\ninterval = %d\nnick = %s\nserver = %s\nport = %d\n", + strcpy(args.last_chan_in, first_chan()); + strcpy(args.last_chans_out, first_chan()); + + debug("n_channels = %d\ninterval = %d\nnick = %s\nserver = %s\nport = %d\n", args.n_channels, args.interval, args.nick, args.server, args.port); - } return start(); }