irctk

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

commit cf4c6994ec82a24d455a02eaba625104b0712cdf
parent 2674a6a59be9637454e0e4953fc14567f7b46acc
Author: Antoine Amarilli <ant.amarilli@free.fr>
Date:   Sat, 15 Jan 2011 19:28:00 +0100

argument parsing is ok

Diffstat:
irctk.c | 114+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 68 insertions(+), 46 deletions(-)

diff --git a/irctk.c b/irctk.c @@ -25,6 +25,11 @@ * $Id: irctest.c 73 2009-01-03 11:14:59Z gyunaev $ */ +/* 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> #include <string.h> @@ -43,22 +48,24 @@ #define _GNU_SOURCE -const char *argp_program_version = "irccat 0.1"; +const char *argp_program_version = "irctk 0.1"; const char *argp_program_bug_address = "<a3nm@a3nm.net>"; /* Program documentation. */ static char doc[] = - "Irccat -- an IRC toolkit"; + "irctk -- an IRC toolkit"; /* A description of the arguments we accept. */ -static char args_doc[] = "SERVER[:PORT] NICK [CHANNEL]..."; +static char args_doc[] = "[NICK@]SERVER[:PORT] [CHANNEL]..."; /* The options we understand. */ static struct argp_option options[] = { {"verbose", 'v', 0, 0, - "Produce verbose output" }, + "Produce debug output on stderr" }, + {"no-command", 0, 0, 0, + "Don't interpret commands like \"/quit\", \"/me\"..." }, /* TODO */ {"interval", 'i', "secs", 0, - "Wait secs seconds between multiple lines (default: 1)" }, + "Wait delay between posted messages (default: 1)" }, { 0 } }; @@ -71,8 +78,11 @@ struct arguments int n_channels; int interval; int verbose; + int command; char * nick; char * server; + int port; + char * raw_dest; char ready; }; @@ -86,6 +96,7 @@ 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)) { @@ -113,9 +124,25 @@ parse_opt (int key, char *arg, struct argp_state *state) case ARGP_KEY_ARG: printf("saw an arg\n"); if (state->arg_num == 0) + { + i = -1; + arguments->raw_dest = arg; arguments->server = arg; - else if (state->arg_num == 1) - arguments->nick = 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) { @@ -127,7 +154,7 @@ parse_opt (int key, char *arg, struct argp_state *state) break; case ARGP_KEY_END: - if (state->arg_num < 2) + if (state->arg_num < 1) /* Not enough arguments. */ argp_usage (state); break; @@ -357,20 +384,12 @@ void event_numeric (irc_session_t * session, unsigned int event, const char * or } -int main (int argc, char **argv) +int start (struct arguments * args) { irc_callbacks_t callbacks; irc_ctx_t ctx; irc_session_t * s; - if ( argc < 4 ) - { - printf ("Usage: %s <server> <nick> <channel>\n", argv[0]); - return 1; - } - - - memset (&callbacks, 0, sizeof(callbacks)); callbacks.event_connect = event_connect; @@ -401,42 +420,20 @@ int main (int argc, char **argv) fprintf (stderr, "Could not create session\n"); return 1; } - + ctx.ready = 0; - ctx.n_channels = argc - 3; - ctx.nick = argv[2]; + ctx.n_channels = 0; + ctx.nick = "foo"; irc_set_ctx (s, &ctx); - - - struct arguments arguments; - - /* Default values. */ - arguments.n_channels = 0; - arguments.channels = (char**) NULL; - arguments.verbose = 0; - arguments.interval = 1; - - /* Parse our arguments; every option seen by parse_opt will - be reflected in arguments. */ - argp_parse (&argp, argc, argv, 0, 0, &arguments); - - printf ("ARG1 = %s\nARG2 = %s\nINTERVAL = %d\nNCHAN = %d\n" - "VERBOSE = %s", - arguments.channels[0], arguments.channels[1], - arguments.interval, arguments.n_channels, - arguments.verbose ? "yes" : "no"); - - //irc_option_set (s, LIBIRC_OPTION_STRIPNICKS); - - if ( irc_connect (s, argv[1], 6667, 0, argv[2], 0, 0) ) + + if ( irc_connect (s, "localhost", 6667, 0, "irctk", 0, 0) ) { fprintf (stderr, "Could not connect: %s\n", irc_strerror (irc_errno(s))); return 1; } - - thread_id_t tid; + if ( CREATE_THREAD (&tid, irc_listen, s) ) { fprintf (stderr, "CREATE_THREAD failed: %s\n", strerror(errno)); @@ -469,9 +466,34 @@ int main (int argc, char **argv) irc_disconnect(s); free(line); - + //pthread_join(tid, value_ptr); return 1; } + +int main (int argc, char **argv) +{ + struct arguments args; + + /* Default values. */ + args.n_channels = 0; + args.channels = (char**) NULL; + args.verbose = 0; + args.interval = 1; + args.nick = "irctk"; + args.port = 6667; + + /* Parse our arguments; every option seen by parse_opt will + be reflected in arguments. */ + argp_parse (&argp, argc, argv, 0, 0, &args); + + if(args.verbose) + { + fprintf (stderr, "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(&args); +}