commit 4f41cce703d0c934c6f61aa10a59dcdbab292bfb
parent 95dfc49de632fba1f1fec5ab37e6f5e7abcca3f0
Author: Antoine Amarilli <ant.amarilli@free.fr>
Date: Sun, 16 Jan 2011 00:53:49 +0100
ok for multichan
Diffstat:
irctk.c | | | 124 | +++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------- |
1 file changed, 83 insertions(+), 41 deletions(-)
diff --git a/irctk.c b/irctk.c
@@ -63,6 +63,7 @@ static char args_doc[] = "[NICK@]SERVER[:PORT] [CHANNEL]...";
/* The options we understand. */
static struct argp_option options[] = {
+ /* TODO No alphasort */
{"verbose", 'v', 0, 0,
"Produce debug output on stderr" },
{"no-command-to-event", 0, 0, 0,
@@ -71,12 +72,15 @@ 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 */
- {"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,
+ {"with-host", 'h', 0, 0,
+ "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 */
{"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!! */
{ 0 }
};
@@ -93,6 +97,7 @@ struct arguments
char * nick;
char * server;
int port;
+ int own;
char * raw_dest;
char ready;
@@ -104,7 +109,7 @@ typedef struct arguments irc_ctx_t;
struct arguments args;
-void error(int val, const char *err, ...)
+void die(int val, const char *err, ...)
{
va_list ap;
va_start(ap, err);
@@ -148,6 +153,9 @@ parse_opt (int key, char *arg, struct argp_state *state)
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);
@@ -271,9 +279,8 @@ void event_privmsg (irc_session_t * session, const char * event, const char * or
{
dump_event (session, event, origin, params, count);
- printf ("[%s] <%s> %s\n", params[0],
- origin ? origin : "someone",
- params[1] );
+ /* TODO fill that in */
+ printf("PRIVMSG");
}
@@ -338,12 +345,17 @@ void event_channel (irc_session_t * session, const char * event, const char * or
{
//char nickbuf[128];
+ int i=0;
if ( count != 2 )
return;
- printf ("[%s] <%s> %s\n", params[0],
- origin ? origin : "someone", params[1] );
-
+ printf ("[%s] <", params[0]);
+ if (!origin)
+ printf("someone");
+ else
+ /* TODO not robust */
+ while(origin[i++] != '!') putchar(origin[i-1]);
+ printf("> %s\n", params[1] );
if ( !origin )
return;
@@ -456,37 +468,25 @@ int start ()
s = irc_create_session (&callbacks);
- if ( !s )
- {
- fprintf (stderr, "Could not create session\n");
- return E_SESSION;
- }
+ if (!s)
+ die(E_SESSION, "Could not create session");
irc_set_ctx (s, &args);
- if (args.verbose)
- fprintf (stderr, "Trying to connect to %s port %d with nick %s...\n",
+ debug("Trying to connect to %s port %d with nick %s...",
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 E_CONNECT;
- }
+ if (irc_connect (s, args.server, args.port, 0, args.nick, 0, 0))
+ die(E_CONNECT, "Could not connect: %s\n", irc_strerror (irc_errno(s)));
- if (args.verbose)
- fprintf(stderr, "Connection request successful!\n");
+ debug("Connection request successful!");
+ debug("Trying to create thread...");
- 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 E_THREAD;
- }
- if (args.verbose)
- fprintf(stderr, "Thread, started!\n");
+ if (CREATE_THREAD (&tid, irc_listen, s))
+ die(E_THREAD, "CREATE_THREAD failed: %s", strerror(errno)); // TODO spurious \n
+
+ debug("Thread started!");
line = (char*) malloc(size+1);
@@ -494,18 +494,58 @@ int start ()
{
while (!args.ready)
{
- if (args.verbose)
- fprintf(stderr, "Waiting for the connection to be ready...\n"); // TODO manage timeouts
+ debug("Waiting for the connection to be ready...");
+ // TODO manage timeouts, try reconnects, handle errors, etc.
+ // TODO manage duplicate pseudos
sleep(1);
}
- if (args.verbose)
- fprintf(stderr, "got %s\n", line);
- if (args.n_channels)
+ debug("got %s");
+
+ if (line[0] == '[' && line[1] != ']')
{
- if ( irc_cmd_msg (s, args.channels[0], line) )
- break;
+ int i=0;
+ char *msg;
+ char *target = line+1;
+
+ /* TODO that's not robust AT ALL */
+ while (line[i++] != ']');
+ msg = 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++;
+ }
+ } 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!");
}
- else debug("No channel to send to!");
sleep(args.interval);
}
@@ -521,10 +561,12 @@ 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;