commit 2674a6a59be9637454e0e4953fc14567f7b46acc
parent a30e322cd044564b7d39800749ca53ff28fd8a6d
Author: Antoine Amarilli <ant.amarilli@free.fr>
Date: Sat, 15 Jan 2011 18:58:21 +0100
reordering
Diffstat:
Makefile | | | 28 | ++++++++++++++++++++++++++++ |
irctest.c | | | 477 | ------------------------------------------------------------------------------- |
irctk.c | | | 477 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
3 files changed, 505 insertions(+), 477 deletions(-)
diff --git a/Makefile b/Makefile
@@ -0,0 +1,28 @@
+# $Id: Makefile.in 42 2004-10-10 16:16:15Z gyunaev $
+CC = gcc
+CXX = g++
+CFLAGS = -Wall -O3 -DENABLE_THREADS -D_REENTRANT
+LIBS = ../src/libircclient.a -lpthread -lnsl
+INCLUDES=-I../include
+
+EXAMPLES=irctk
+
+all: irctk
+
+irctk: irctk.o
+ $(CC) -o irctk irctk.o $(LIBS)
+
+clean:
+ -rm -f $(EXAMPLES) *.o *.exe
+
+distclean: clean
+ -rm -f Makefile *.log
+
+
+.c.o:
+ @echo "Compiling $<"
+ @$(CC) $(CFLAGS) $(INCLUDES) -c -o $@ $<
+
+.cpp.o:
+ @echo "Compiling $<"
+ @$(CXX) $(CFLAGS) $(INCLUDES) -c -o $@ $<
diff --git a/irctest.c b/irctest.c
@@ -1,477 +0,0 @@
-/*
- * Copyright (C) 2004-2009 Georgy Yunaev gyunaev@ulduzsoft.com
- *
- * This example is free, and not covered by LGPL license. There is no
- * restriction applied to their modification, redistribution, using and so on.
- * You can study them, modify them, use them in your own program - either
- * completely or partially. By using it you may give me some credits in your
- * program, but you don't have to.
- *
- *
- * This example tests most features of libirc. It can join the specific
- * channel, welcoming all the people there, and react on some messages -
- * 'help', 'quit', 'dcc chat', 'dcc send', 'ctcp'. Also it can reply to
- * CTCP requests, receive DCC files and accept DCC chats.
- *
- * Features used:
- * - nickname parsing;
- * - handling 'channel' event to track the messages;
- * - handling dcc and ctcp events;
- * - using internal ctcp rely procedure;
- * - generating channel messages;
- * - handling dcc send and dcc chat events;
- * - initiating dcc send and dcc chat.
- *
- * $Id: irctest.c 73 2009-01-03 11:14:59Z gyunaev $
- */
-
-#include <stdio.h>
-#include <stdarg.h>
-#include <string.h>
-#include <stdlib.h>
-#include <errno.h>
-#include <argp.h>
-
-#include "libircclient.h"
-
-#include <unistd.h>
-#include <pthread.h>
-
-#define CREATE_THREAD(id,func,param) (pthread_create (id, 0, func, (void *) param) != 0)
-#define THREAD_FUNCTION(funcname) static void * funcname (void * arg)
-#define thread_id_t pthread_t
-#define _GNU_SOURCE
-
-
-const char *argp_program_version = "irccat 0.1";
-const char *argp_program_bug_address = "<a3nm@a3nm.net>";
-
-/* Program documentation. */
-static char doc[] =
- "Irccat -- an IRC toolkit";
-
-/* A description of the arguments we accept. */
-static char args_doc[] = "SERVER[:PORT] NICK [CHANNEL]...";
-
-/* The options we understand. */
-static struct argp_option options[] = {
- {"verbose", 'v', 0, 0,
- "Produce verbose output" },
- {"interval", 'i', "secs", 0,
- "Wait secs seconds between multiple lines (default: 1)" },
- { 0 }
-};
-
-/*
- * We store data in IRC session context.
- */
-struct arguments
-{
- char ** channels;
- int n_channels;
- int interval;
- int verbose;
- char * nick;
- char * server;
- char ready;
-
-};
-
-typedef struct arguments irc_ctx_t;
-
-/* Parse a single option. */
-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;
-
- 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
- printf ("seen i, nchan is %d\n", arguments->n_channels);
- arguments->interval = atoi(arg);
- 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:
- printf("saw an arg\n");
- if (state->arg_num == 0)
- arguments->server = arg;
- else if (state->arg_num == 1)
- arguments->nick = arg;
- else {
- if (arguments->channels == NULL)
- {
- printf("start with %s, at %d\n", arg, state->arg_num);
- arguments->channels = state->argv + (state->next - 1);
- }
- arguments->n_channels++;
- }
- break;
-
- case ARGP_KEY_END:
- if (state->arg_num < 2)
- /* Not enough arguments. */
- argp_usage (state);
- break;
- }
-
- }
- return 0;
-}
-
-/* Our argp parser. */
-static struct argp argp = { options, parse_opt, args_doc, doc };
-
-
-
-THREAD_FUNCTION(irc_listen)
-{
- irc_session_t * sp = (irc_session_t *) arg;
-
- irc_run(sp);
-
- return 0;
-}
-
-
-
-void dump_event (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
-{
- char buf[512];
- int cnt;
-
- buf[0] = '\0';
-
- for ( cnt = 0; cnt < count; cnt++ )
- {
- if ( cnt )
- strcat (buf, "|");
-
- strcat (buf, params[cnt]);
- }
-
-
- fprintf (stderr, "Event \"%s\", origin: \"%s\", params: %d [%s]\n", event, origin ? origin : "NULL", cnt, buf);
-}
-
-
-void event_join (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
-{
- dump_event (session, event, origin, params, count);
- irc_cmd_user_mode (session, "+i");
-
- printf("joined\n");
- /*irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
-
- if ( !origin )
- return;*/
-
-}
-
-
-void event_connect (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
-{
- irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
- dump_event (session, event, origin, params, count);
-
- ctx->ready = 1;
- printf("connected\n");
-
- int i;
- for (i = 0; i < ctx->n_channels; i++)
- irc_cmd_join (session, ctx->channels[i+3], 0);
-}
-
-
-void event_privmsg (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
-{
- dump_event (session, event, origin, params, count);
-
- printf ("[%s] <%s> %s\n", params[0],
- origin ? origin : "someone",
- params[1] );
-}
-
-
-void dcc_recv_callback (irc_session_t * session, irc_dcc_t id, int status, void * ctx, const char * data, unsigned int length)
-{
- static int count = 1;
- char buf[12];
-
- switch (status)
- {
- case LIBIRC_ERR_CLOSED:
- printf ("DCC %d: chat closed\n", id);
- break;
-
- case 0:
- if ( !data )
- {
- printf ("DCC %d: chat connected\n", id);
- irc_dcc_msg (session, id, "Hehe");
- }
- else
- {
- printf ("DCC %d: %s\n", id, data);
- sprintf (buf, "DCC [%d]: %d", id, count++);
- irc_dcc_msg (session, id, buf);
- }
- break;
-
- default:
- printf ("DCC %d: error %s\n", id, irc_strerror(status));
- break;
- }
-}
-
-
-void dcc_file_recv_callback (irc_session_t * session, irc_dcc_t id, int status, void * ctx, const char * data, unsigned int length)
-{
- if ( status == 0 && length == 0 )
- {
- printf ("File sent successfully\n");
-
- if ( ctx )
- fclose ((FILE*) ctx);
- }
- else if ( status )
- {
- printf ("File sent error: %d\n", status);
-
- if ( ctx )
- fclose ((FILE*) ctx);
- }
- else
- {
- if ( ctx )
- fwrite (data, 1, length, (FILE*) ctx);
- printf ("File sent progress: %d\n", length);
- }
-}
-
-
-void event_channel (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
-{
- //char nickbuf[128];
-
- if ( count != 2 )
- return;
-
- printf ("[%s] <%s> %s\n", params[0],
- origin ? origin : "someone", params[1] );
-
- if ( !origin )
- return;
-
- /*irc_target_get_nick (origin, nickbuf, sizeof(nickbuf));
-
- if ( !strcmp (params[1], "quit") )
- irc_cmd_quit (session, "of course, Master!");
-
- if ( !strcmp (params[1], "help") )
- {
- irc_cmd_msg (session, params[0], "quit, help, dcc chat, dcc send, ctcp");
- }
-
- if ( !strcmp (params[1], "ctcp") )
- {
- irc_cmd_ctcp_request (session, nickbuf, "PING 223");
- irc_cmd_ctcp_request (session, nickbuf, "FINGER");
- irc_cmd_ctcp_request (session, nickbuf, "VERSION");
- irc_cmd_ctcp_request (session, nickbuf, "TIME");
- }
-
- if ( !strcmp (params[1], "dcc chat") )
- {
- irc_dcc_t dccid;
- irc_dcc_chat (session, 0, nickbuf, dcc_recv_callback, &dccid);
- printf ("DCC chat ID: %d\n", dccid);
- }
-
- if ( !strcmp (params[1], "dcc send") )
- {
- irc_dcc_t dccid;
- irc_dcc_sendfile (session, 0, nickbuf, "irctest.c", dcc_file_recv_callback, &dccid);
- printf ("DCC send ID: %d\n", dccid);
- }
-
- if ( !strcmp (params[1], "topic") )
- irc_cmd_topic (session, params[0], 0);
- else if ( strstr (params[1], "topic ") == params[1] )
- irc_cmd_topic (session, params[0], params[1] + 6);
-
- if ( strstr (params[1], "mode ") == params[1] )
- irc_cmd_channel_mode (session, params[0], params[1] + 5);
-
- if ( strstr (params[1], "nick ") == params[1] )
- irc_cmd_nick (session, params[1] + 5);
-
- if ( strstr (params[1], "whois ") == params[1] )
- irc_cmd_whois (session, params[1] + 5);*/
-}
-
-
-void irc_event_dcc_chat (irc_session_t * session, const char * nick, const char * addr, irc_dcc_t dccid)
-{
- printf ("DCC chat [%d] requested from '%s' (%s)\n", dccid, nick, addr);
-
- irc_dcc_accept (session, dccid, 0, dcc_recv_callback);
-}
-
-
-void irc_event_dcc_send (irc_session_t * session, const char * nick, const char * addr, const char * filename, unsigned long size, irc_dcc_t dccid)
-{
- FILE * fp;
- printf ("DCC send [%d] requested from '%s' (%s): %s (%lu bytes)\n", dccid, nick, addr, filename, size);
-
- if ( (fp = fopen ("file", "wb")) == 0 )
- abort();
-
- irc_dcc_accept (session, dccid, fp, dcc_file_recv_callback);
-}
-
-void event_numeric (irc_session_t * session, unsigned int event, const char * origin, const char ** params, unsigned int count)
-{
- char buf[24];
- sprintf (buf, "%d", event);
-
- dump_event (session, buf, origin, params, count);
-}
-
-
-int main (int argc, char **argv)
-{
- 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;
- callbacks.event_join = event_join;
- callbacks.event_nick = dump_event;
- callbacks.event_quit = dump_event;
- callbacks.event_part = dump_event;
- callbacks.event_mode = dump_event;
- callbacks.event_topic = dump_event;
- callbacks.event_kick = dump_event;
- callbacks.event_channel = event_channel;
- callbacks.event_privmsg = event_privmsg;
- callbacks.event_notice = dump_event;
- callbacks.event_invite = dump_event;
- callbacks.event_umode = dump_event;
- callbacks.event_ctcp_rep = dump_event;
- callbacks.event_ctcp_action = dump_event;
- callbacks.event_unknown = dump_event;
- callbacks.event_numeric = event_numeric;
-
- //TODO callbacks.event_dcc_chat_req = irc_event_dcc_chat;
- //TODO callbacks.event_dcc_send_req = irc_event_dcc_send;
-
- s = irc_create_session (&callbacks);
-
- if ( !s )
- {
- fprintf (stderr, "Could not create session\n");
- return 1;
- }
-
- ctx.ready = 0;
- ctx.n_channels = argc - 3;
- ctx.nick = argv[2];
-
- 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) )
- {
- 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));
- return 42;
- }
- /*else
- printf ("Listener thread was started successfully.\n");*/
-
- char *line = NULL;
- int res, size=100;
-
- line = (char*) malloc(size+1);
-
- while ( (res = getline((char**) &line, (size_t*) &size, stdin)) != -1 )
- {
- while (!ctx.ready)
- {
- fprintf(stderr, "Waiting for the connection to be ready...\n");
- sleep(1);
- }
- fprintf(stderr, "got %s\n", line);
- if ( irc_cmd_msg (s, ctx.channels[3], line) )
- break;
-
- sleep(1);
- }
-
- line = NULL;
- sleep(1);
- irc_disconnect(s);
- free(line);
-
-
-
- //pthread_join(tid, value_ptr);
-
- return 1;
-}
diff --git a/irctk.c b/irctk.c
@@ -0,0 +1,477 @@
+/*
+ * Copyright (C) 2004-2009 Georgy Yunaev gyunaev@ulduzsoft.com
+ *
+ * This example is free, and not covered by LGPL license. There is no
+ * restriction applied to their modification, redistribution, using and so on.
+ * You can study them, modify them, use them in your own program - either
+ * completely or partially. By using it you may give me some credits in your
+ * program, but you don't have to.
+ *
+ *
+ * This example tests most features of libirc. It can join the specific
+ * channel, welcoming all the people there, and react on some messages -
+ * 'help', 'quit', 'dcc chat', 'dcc send', 'ctcp'. Also it can reply to
+ * CTCP requests, receive DCC files and accept DCC chats.
+ *
+ * Features used:
+ * - nickname parsing;
+ * - handling 'channel' event to track the messages;
+ * - handling dcc and ctcp events;
+ * - using internal ctcp rely procedure;
+ * - generating channel messages;
+ * - handling dcc send and dcc chat events;
+ * - initiating dcc send and dcc chat.
+ *
+ * $Id: irctest.c 73 2009-01-03 11:14:59Z gyunaev $
+ */
+
+#include <stdio.h>
+#include <stdarg.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+#include <argp.h>
+
+#include "libircclient.h"
+
+#include <unistd.h>
+#include <pthread.h>
+
+#define CREATE_THREAD(id,func,param) (pthread_create (id, 0, func, (void *) param) != 0)
+#define THREAD_FUNCTION(funcname) static void * funcname (void * arg)
+#define thread_id_t pthread_t
+#define _GNU_SOURCE
+
+
+const char *argp_program_version = "irccat 0.1";
+const char *argp_program_bug_address = "<a3nm@a3nm.net>";
+
+/* Program documentation. */
+static char doc[] =
+ "Irccat -- an IRC toolkit";
+
+/* A description of the arguments we accept. */
+static char args_doc[] = "SERVER[:PORT] NICK [CHANNEL]...";
+
+/* The options we understand. */
+static struct argp_option options[] = {
+ {"verbose", 'v', 0, 0,
+ "Produce verbose output" },
+ {"interval", 'i', "secs", 0,
+ "Wait secs seconds between multiple lines (default: 1)" },
+ { 0 }
+};
+
+/*
+ * We store data in IRC session context.
+ */
+struct arguments
+{
+ char ** channels;
+ int n_channels;
+ int interval;
+ int verbose;
+ char * nick;
+ char * server;
+ char ready;
+
+};
+
+typedef struct arguments irc_ctx_t;
+
+/* Parse a single option. */
+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;
+
+ 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
+ printf ("seen i, nchan is %d\n", arguments->n_channels);
+ arguments->interval = atoi(arg);
+ 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:
+ printf("saw an arg\n");
+ if (state->arg_num == 0)
+ arguments->server = arg;
+ else if (state->arg_num == 1)
+ arguments->nick = arg;
+ else {
+ if (arguments->channels == NULL)
+ {
+ printf("start with %s, at %d\n", arg, state->arg_num);
+ arguments->channels = state->argv + (state->next - 1);
+ }
+ arguments->n_channels++;
+ }
+ break;
+
+ case ARGP_KEY_END:
+ if (state->arg_num < 2)
+ /* Not enough arguments. */
+ argp_usage (state);
+ break;
+ }
+
+ }
+ return 0;
+}
+
+/* Our argp parser. */
+static struct argp argp = { options, parse_opt, args_doc, doc };
+
+
+
+THREAD_FUNCTION(irc_listen)
+{
+ irc_session_t * sp = (irc_session_t *) arg;
+
+ irc_run(sp);
+
+ return 0;
+}
+
+
+
+void dump_event (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
+{
+ char buf[512];
+ int cnt;
+
+ buf[0] = '\0';
+
+ for ( cnt = 0; cnt < count; cnt++ )
+ {
+ if ( cnt )
+ strcat (buf, "|");
+
+ strcat (buf, params[cnt]);
+ }
+
+
+ fprintf (stderr, "Event \"%s\", origin: \"%s\", params: %d [%s]\n", event, origin ? origin : "NULL", cnt, buf);
+}
+
+
+void event_join (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
+{
+ dump_event (session, event, origin, params, count);
+ irc_cmd_user_mode (session, "+i");
+
+ printf("joined\n");
+ /*irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
+
+ if ( !origin )
+ return;*/
+
+}
+
+
+void event_connect (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
+{
+ irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
+ dump_event (session, event, origin, params, count);
+
+ ctx->ready = 1;
+ printf("connected\n");
+
+ int i;
+ for (i = 0; i < ctx->n_channels; i++)
+ irc_cmd_join (session, ctx->channels[i+3], 0);
+}
+
+
+void event_privmsg (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
+{
+ dump_event (session, event, origin, params, count);
+
+ printf ("[%s] <%s> %s\n", params[0],
+ origin ? origin : "someone",
+ params[1] );
+}
+
+
+void dcc_recv_callback (irc_session_t * session, irc_dcc_t id, int status, void * ctx, const char * data, unsigned int length)
+{
+ static int count = 1;
+ char buf[12];
+
+ switch (status)
+ {
+ case LIBIRC_ERR_CLOSED:
+ printf ("DCC %d: chat closed\n", id);
+ break;
+
+ case 0:
+ if ( !data )
+ {
+ printf ("DCC %d: chat connected\n", id);
+ irc_dcc_msg (session, id, "Hehe");
+ }
+ else
+ {
+ printf ("DCC %d: %s\n", id, data);
+ sprintf (buf, "DCC [%d]: %d", id, count++);
+ irc_dcc_msg (session, id, buf);
+ }
+ break;
+
+ default:
+ printf ("DCC %d: error %s\n", id, irc_strerror(status));
+ break;
+ }
+}
+
+
+void dcc_file_recv_callback (irc_session_t * session, irc_dcc_t id, int status, void * ctx, const char * data, unsigned int length)
+{
+ if ( status == 0 && length == 0 )
+ {
+ printf ("File sent successfully\n");
+
+ if ( ctx )
+ fclose ((FILE*) ctx);
+ }
+ else if ( status )
+ {
+ printf ("File sent error: %d\n", status);
+
+ if ( ctx )
+ fclose ((FILE*) ctx);
+ }
+ else
+ {
+ if ( ctx )
+ fwrite (data, 1, length, (FILE*) ctx);
+ printf ("File sent progress: %d\n", length);
+ }
+}
+
+
+void event_channel (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
+{
+ //char nickbuf[128];
+
+ if ( count != 2 )
+ return;
+
+ printf ("[%s] <%s> %s\n", params[0],
+ origin ? origin : "someone", params[1] );
+
+ if ( !origin )
+ return;
+
+ /*irc_target_get_nick (origin, nickbuf, sizeof(nickbuf));
+
+ if ( !strcmp (params[1], "quit") )
+ irc_cmd_quit (session, "of course, Master!");
+
+ if ( !strcmp (params[1], "help") )
+ {
+ irc_cmd_msg (session, params[0], "quit, help, dcc chat, dcc send, ctcp");
+ }
+
+ if ( !strcmp (params[1], "ctcp") )
+ {
+ irc_cmd_ctcp_request (session, nickbuf, "PING 223");
+ irc_cmd_ctcp_request (session, nickbuf, "FINGER");
+ irc_cmd_ctcp_request (session, nickbuf, "VERSION");
+ irc_cmd_ctcp_request (session, nickbuf, "TIME");
+ }
+
+ if ( !strcmp (params[1], "dcc chat") )
+ {
+ irc_dcc_t dccid;
+ irc_dcc_chat (session, 0, nickbuf, dcc_recv_callback, &dccid);
+ printf ("DCC chat ID: %d\n", dccid);
+ }
+
+ if ( !strcmp (params[1], "dcc send") )
+ {
+ irc_dcc_t dccid;
+ irc_dcc_sendfile (session, 0, nickbuf, "irctest.c", dcc_file_recv_callback, &dccid);
+ printf ("DCC send ID: %d\n", dccid);
+ }
+
+ if ( !strcmp (params[1], "topic") )
+ irc_cmd_topic (session, params[0], 0);
+ else if ( strstr (params[1], "topic ") == params[1] )
+ irc_cmd_topic (session, params[0], params[1] + 6);
+
+ if ( strstr (params[1], "mode ") == params[1] )
+ irc_cmd_channel_mode (session, params[0], params[1] + 5);
+
+ if ( strstr (params[1], "nick ") == params[1] )
+ irc_cmd_nick (session, params[1] + 5);
+
+ if ( strstr (params[1], "whois ") == params[1] )
+ irc_cmd_whois (session, params[1] + 5);*/
+}
+
+
+void irc_event_dcc_chat (irc_session_t * session, const char * nick, const char * addr, irc_dcc_t dccid)
+{
+ printf ("DCC chat [%d] requested from '%s' (%s)\n", dccid, nick, addr);
+
+ irc_dcc_accept (session, dccid, 0, dcc_recv_callback);
+}
+
+
+void irc_event_dcc_send (irc_session_t * session, const char * nick, const char * addr, const char * filename, unsigned long size, irc_dcc_t dccid)
+{
+ FILE * fp;
+ printf ("DCC send [%d] requested from '%s' (%s): %s (%lu bytes)\n", dccid, nick, addr, filename, size);
+
+ if ( (fp = fopen ("file", "wb")) == 0 )
+ abort();
+
+ irc_dcc_accept (session, dccid, fp, dcc_file_recv_callback);
+}
+
+void event_numeric (irc_session_t * session, unsigned int event, const char * origin, const char ** params, unsigned int count)
+{
+ char buf[24];
+ sprintf (buf, "%d", event);
+
+ dump_event (session, buf, origin, params, count);
+}
+
+
+int main (int argc, char **argv)
+{
+ 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;
+ callbacks.event_join = event_join;
+ callbacks.event_nick = dump_event;
+ callbacks.event_quit = dump_event;
+ callbacks.event_part = dump_event;
+ callbacks.event_mode = dump_event;
+ callbacks.event_topic = dump_event;
+ callbacks.event_kick = dump_event;
+ callbacks.event_channel = event_channel;
+ callbacks.event_privmsg = event_privmsg;
+ callbacks.event_notice = dump_event;
+ callbacks.event_invite = dump_event;
+ callbacks.event_umode = dump_event;
+ callbacks.event_ctcp_rep = dump_event;
+ callbacks.event_ctcp_action = dump_event;
+ callbacks.event_unknown = dump_event;
+ callbacks.event_numeric = event_numeric;
+
+ //TODO callbacks.event_dcc_chat_req = irc_event_dcc_chat;
+ //TODO callbacks.event_dcc_send_req = irc_event_dcc_send;
+
+ s = irc_create_session (&callbacks);
+
+ if ( !s )
+ {
+ fprintf (stderr, "Could not create session\n");
+ return 1;
+ }
+
+ ctx.ready = 0;
+ ctx.n_channels = argc - 3;
+ ctx.nick = argv[2];
+
+ 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) )
+ {
+ 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));
+ return 42;
+ }
+ /*else
+ printf ("Listener thread was started successfully.\n");*/
+
+ char *line = NULL;
+ int res, size=100;
+
+ line = (char*) malloc(size+1);
+
+ while ( (res = getline((char**) &line, (size_t*) &size, stdin)) != -1 )
+ {
+ while (!ctx.ready)
+ {
+ fprintf(stderr, "Waiting for the connection to be ready...\n");
+ sleep(1);
+ }
+ fprintf(stderr, "got %s\n", line);
+ if ( irc_cmd_msg (s, ctx.channels[3], line) )
+ break;
+
+ sleep(1);
+ }
+
+ line = NULL;
+ sleep(1);
+ irc_disconnect(s);
+ free(line);
+
+
+
+ //pthread_join(tid, value_ptr);
+
+ return 1;
+}