irctk

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

commit edddeb9110cb729925e1ebc4bb6a148e0784c1db
parent fc567a03babbed35176d38c92010076b82ba186b
Author: Antoine Amarilli <ant.amarilli@free.fr>
Date:   Sun, 16 Jan 2011 03:23:32 +0100

-pony feature

comments, cleanup

Diffstat:
irctk.c | 120++++++++++++++++++++++++++++++++++++++++----------------------------------------
1 file changed, 60 insertions(+), 60 deletions(-)

diff --git a/irctk.c b/irctk.c @@ -25,11 +25,6 @@ * $Id: irctest.c 73 2009-01-03 11:14:59Z gyunaev $ */ -/* TODO detect if stdin is a tty for delay configuration */ -/* TODO interpret '/' commands or not - * TODO do interpret [] prefix - * TODO default is to last chan till whitespace (or give whitespace to - * notify that we will talk? */ #include <stdio.h> #include <stdarg.h> @@ -67,28 +62,42 @@ static char args_doc[] = "[NICK@]SERVER[:PORT] [CHANNEL]..."; /* The options we understand. */ static struct argp_option options[] = { - /* TODO No alphasort */ + /* TODO Display by theme not by alpha order */ {"verbose", 'v', 0, 0, "Produce debug output on stderr" }, {"no-command-to-event", 0, 0, 0, "Don't generate events from messages with \"/quit\", \"/me\"..." }, /* TODO */ + {"no-destination-prefix", 0, 0, 0, + "Don't look for []" }, /* TODO */ {"event-to-command", 'c', 0, 0, "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, - "Keep the host info in pseudos" }, /* TODO, and make compatible with --own - */ + "Keep the host info in pseudos" }, + /* TODO, and make compatible with --own */ {"own", 'o', 0, 0, - "Generate events for messages posted by self" }, /* TODO, and offer possibility to translate them as messages too */ + "Also display messages posted by self" }, {"interval", 'i', "secs", 0, "Wait delay between posted messages (default: 1)" }, - {"always-first", 'f', 0, 0, - "Always post messages as-is to the first specified channel (default is to post to the last active channel, reseting on blank lines: bot scripts should put a blank line before and after messages)" }, /* TODO oh, and on leading blankline, display the channel name on stderr, for the client, that would be so cool!! */ - /* option to not use a newline to end (ie. reply immediately one-line - * messages */ - /* TODO because you can't get a pony, add support for exec of sth at - * each line (ie. spawn a thread) */ +/* TODO detect if stdin is a tty for delay configuration */ + {"default-always-first", 'f', 0, 0, + "Post messages to the first specified channel by default" }, + {"default-last-active", 'a', 0, 0, + "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, + "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, + "Will wait for a multiline, blank line-terminated answer on stdin to all lines passed on stdout, and will bufferize activity before that" }, + {"filter", 'f', 0, 0, + "Pipe each message to this command" }, /* TODO and support several of them */ + /* TODO and allow to pipe on argv rather than stdin */ + /* TODO for bots, prefix with "nick_of_sender:" on public channels */ + {"reply", 'r', 0, 0, + "When inferring a destination channel automatically, also infer a destination nick, and prefix lines with this nick" }, { 0 } }; @@ -108,8 +117,8 @@ struct arguments int own; char * raw_dest; char ready; - char memo_chan[MAX_LEN]; - char last_chan[MAX_LEN]; // <- TODO last pseudo, for custom resp to pseudo + char last_chan_in[MAX_LEN]; + char last_chans_out[MAX_LEN]; }; @@ -118,6 +127,19 @@ typedef struct arguments irc_ctx_t; /* The arguments */ struct arguments args; +void initialize_args() +{ + /* TODO move to separate fun */ + args.n_channels = 0; + args.channels = (char**) NULL; + args.verbose = 0; + args.interval = 1; + args.own = 0; + args.nick = "irctk"; + args.port = 6667; + args.last_chan_in[0] = 0; + args.last_chans_out[0] = 0; +} void die(int val, const char *err, ...) { @@ -368,11 +390,7 @@ void event_channel (irc_session_t * session, const char * event, const char * or printf("> %s\n", params[1] ); if ( !origin ) return; - if (!args.memo_chan[0]) - { - debug("memorize last chan to %s", params[0]); - strcpy(args.last_chan, params[0]); - } + strcpy(args.last_chan_in, params[0]); fflush(stdout); @@ -527,6 +545,9 @@ int start () /* TODO that's not robust AT ALL */ while (line[i++] != ']'); msg = line + i + 1; + line[i-1] = 0; + strcpy(args.last_chans_out, line); + line[i-1] = ']'; i = 0; @@ -552,36 +573,22 @@ int start () } else { /* No target specified, we attempt the default */ /* TODO WRITE BETTER DEFAULT */ - if (line [0] == '\n' && args.memo_chan[0]) + if (line[0] == '[') + line += 2; // we start with '[]' + /* + if (args.n_channels) { - /* TODO we should have posted, check that! */ - debug("unfix chan which was at %s", args.memo_chan); - args.memo_chan[0] = 0; - } else { - debug("trying to post, last is %s and memo is %s", args.last_chan, args.memo_chan); - if (!args.memo_chan[0]) - { - debug("fix chan to %s", args.last_chan); - strcpy(args.memo_chan, args.last_chan); - } - debug("update trying to post, last is %s and memo is %s", args.last_chan, args.memo_chan); - 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.memo_chan, line) ) + 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.memo_chan, args.nick, line); + 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); } sleep(args.interval); @@ -598,16 +605,7 @@ int start () int main (int argc, char **argv) { /* Default values. */ - /* TODO move to separate fun */ - args.n_channels = 0; - args.channels = (char**) NULL; - args.verbose = 0; - args.interval = 1; - args.own = 0; - args.nick = "irctk"; - args.port = 6667; - args.memo_chan[0] = 0; - args.last_chan[0] = 0; + initialize_args(); /* Parse our arguments; every option seen by parse_opt will be reflected in arguments. */ @@ -615,9 +613,11 @@ int main (int argc, char **argv) if (args.n_channels) { - strcpy(args.last_chan, args.nick); + strcpy(args.last_chan_in, args.nick); + strcpy(args.last_chans_out, args.nick); } else { - strcpy(args.last_chan, args.channels[0]); + strcpy(args.last_chan_in, args.channels[0]); + strcpy(args.last_chans_out, args.channels[0]); } if(args.verbose) {