commit 95dfc49de632fba1f1fec5ab37e6f5e7abcca3f0
parent cf4c6994ec82a24d455a02eaba625104b0712cdf
Author: Antoine Amarilli <ant.amarilli@free.fr>
Date: Sat, 15 Jan 2011 19:53:26 +0100
now sort-of working
Diffstat:
irctk.c | | | 129 | +++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------------- |
1 file changed, 86 insertions(+), 43 deletions(-)
diff --git a/irctk.c b/irctk.c
@@ -47,6 +47,9 @@
#define thread_id_t pthread_t
#define _GNU_SOURCE
+#define E_SESSION 1
+#define E_CONNECT 2
+#define E_THREAD 3
const char *argp_program_version = "irctk 0.1";
const char *argp_program_bug_address = "<a3nm@a3nm.net>";
@@ -62,8 +65,16 @@ static char args_doc[] = "[NICK@]SERVER[:PORT] [CHANNEL]...";
static struct argp_option options[] = {
{"verbose", 'v', 0, 0,
"Produce debug output on stderr" },
- {"no-command", 0, 0, 0,
- "Don't interpret commands like \"/quit\", \"/me\"..." }, /* TODO */
+ {"no-command-to-event", 0, 0, 0,
+ "Don't generate events from messages with \"/quit\", \"/me\"..." }, /* 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 */
+ {"strip-pseudo", 'S', 0, 0,
+ "Remove the post-! part in pseudos" }, /* TODO, and offer possibility to translate them as messages too */
+ {"self", 's', 0, 0,
+ "Generate events for messages posted by self" }, /* TODO, and offer possibility to translate them as messages too */
{"interval", 'i', "secs", 0,
"Wait delay between posted messages (default: 1)" },
{ 0 }
@@ -89,6 +100,33 @@ struct arguments
typedef struct arguments irc_ctx_t;
+/* The arguments */
+struct arguments args;
+
+
+void error(int val, const char *err, ...)
+{
+ va_list ap;
+ va_start(ap, err);
+ fprintf(stderr, "Error: ");
+ vfprintf(stderr, err, ap);
+ fprintf(stderr, "\n");
+ exit(val);
+}
+
+
+void debug(const char *err, ...)
+{
+ va_list ap;
+ va_start(ap, err);
+ if (args.verbose)
+ {
+ fprintf(stderr, "debug: ");
+ vfprintf(stderr, err, ap);
+ fprintf(stderr, "\n");
+ }
+}
+
/* Parse a single option. */
static error_t
parse_opt (int key, char *arg, struct argp_state *state)
@@ -107,7 +145,7 @@ parse_opt (int key, char *arg, struct argp_state *state)
break;
case 'i':
// TODO use float, and be more robust
- printf ("seen i, nchan is %d\n", arguments->n_channels);
+ debug ("seen i, nchan is %d", arguments->n_channels);
arguments->interval = atoi(arg);
break;
@@ -122,7 +160,7 @@ parse_opt (int key, char *arg, struct argp_state *state)
{
/* TODO option exclusion doesn't work! */
case ARGP_KEY_ARG:
- printf("saw an arg\n");
+ debug("saw an arg");
if (state->arg_num == 0)
{
i = -1;
@@ -146,7 +184,7 @@ parse_opt (int key, char *arg, struct argp_state *state)
else {
if (arguments->channels == NULL)
{
- printf("start with %s, at %d\n", arg, state->arg_num);
+ debug("start with %s, at %d", arg, state->arg_num);
arguments->channels = state->argv + (state->next - 1);
}
arguments->n_channels++;
@@ -178,8 +216,6 @@ THREAD_FUNCTION(irc_listen)
return 0;
}
-
-
void dump_event (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
char buf[512];
@@ -196,7 +232,7 @@ void dump_event (irc_session_t * session, const char * event, const char * origi
}
- fprintf (stderr, "Event \"%s\", origin: \"%s\", params: %d [%s]\n", event, origin ? origin : "NULL", cnt, buf);
+ debug("Event \"%s\", origin: \"%s\", params: %d [%s]", event, origin ? origin : "NULL", cnt, buf);
}
@@ -205,7 +241,7 @@ void event_join (irc_session_t * session, const char * event, const char * origi
dump_event (session, event, origin, params, count);
irc_cmd_user_mode (session, "+i");
- printf("joined\n");
+ debug("Joined!");
/*irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
if ( !origin )
@@ -220,11 +256,14 @@ void event_connect (irc_session_t * session, const char * event, const char * or
dump_event (session, event, origin, params, count);
ctx->ready = 1;
- printf("connected\n");
+ debug("Connected!");
int i;
for (i = 0; i < ctx->n_channels; i++)
- irc_cmd_join (session, ctx->channels[i+3], 0);
+ {
+ debug("Attempt to join %s", ctx->channels[i]);
+ irc_cmd_join (session, ctx->channels[i], 0);
+ }
}
@@ -384,11 +423,13 @@ void event_numeric (irc_session_t * session, unsigned int event, const char * or
}
-int start (struct arguments * args)
+int start ()
{
irc_callbacks_t callbacks;
- irc_ctx_t ctx;
irc_session_t * s;
+ thread_id_t tid;
+ char *line = NULL;
+ int res, size=100;
memset (&callbacks, 0, sizeof(callbacks));
@@ -418,65 +459,67 @@ int start (struct arguments * args)
if ( !s )
{
fprintf (stderr, "Could not create session\n");
- return 1;
+ return E_SESSION;
}
- ctx.ready = 0;
- ctx.n_channels = 0;
- ctx.nick = "foo";
-
- irc_set_ctx (s, &ctx);
+ irc_set_ctx (s, &args);
- if ( irc_connect (s, "localhost", 6667, 0, "irctk", 0, 0) )
+ if (args.verbose)
+ fprintf (stderr, "Trying to connect to %s port %d with nick %s...\n",
+ args.server, args.port, args.nick);
+
+ if ( irc_connect (s, args.server, args.port, 0, args.nick, 0, 0) )
{
fprintf (stderr, "Could not connect: %s\n", irc_strerror (irc_errno(s)));
- return 1;
+ return E_CONNECT;
}
- thread_id_t tid;
+ if (args.verbose)
+ fprintf(stderr, "Connection request successful!\n");
+
+
+ if (args.verbose)
+ fprintf(stderr, "Trying to create thread...\n");
if ( CREATE_THREAD (&tid, irc_listen, s) )
{
fprintf (stderr, "CREATE_THREAD failed: %s\n", strerror(errno));
- return 42;
+ return E_THREAD;
}
- /*else
- printf ("Listener thread was started successfully.\n");*/
-
- char *line = NULL;
- int res, size=100;
+ if (args.verbose)
+ fprintf(stderr, "Thread, started!\n");
line = (char*) malloc(size+1);
while ( (res = getline((char**) &line, (size_t*) &size, stdin)) != -1 )
{
- while (!ctx.ready)
+ while (!args.ready)
{
- fprintf(stderr, "Waiting for the connection to be ready...\n");
+ if (args.verbose)
+ fprintf(stderr, "Waiting for the connection to be ready...\n"); // TODO manage timeouts
sleep(1);
}
- fprintf(stderr, "got %s\n", line);
- if ( irc_cmd_msg (s, ctx.channels[3], line) )
- break;
+ if (args.verbose)
+ fprintf(stderr, "got %s\n", line);
+ if (args.n_channels)
+ {
+ if ( irc_cmd_msg (s, args.channels[0], line) )
+ break;
+ }
+ else debug("No channel to send to!");
- sleep(1);
+ sleep(args.interval);
}
line = NULL;
- sleep(1);
+ sleep(args.interval);
irc_disconnect(s);
free(line);
-
-
- //pthread_join(tid, value_ptr);
-
- return 1;
+ return 0;
}
int main (int argc, char **argv)
{
- struct arguments args;
-
/* Default values. */
args.n_channels = 0;
args.channels = (char**) NULL;
@@ -495,5 +538,5 @@ int main (int argc, char **argv)
args.n_channels, args.interval, args.nick, args.server, args.port);
}
- return start(&args);
+ return start();
}