commit 2bdf6384ca3104c891e6f386fd984daacb57708c
parent c1a4b653a468ce26d8cd83ca0e5d05daedbb7c18
Author: Antoine Amarilli <ant.amarilli@free.fr>
Date: Thu, 13 Jan 2011 14:12:42 +0100
sorta working iirc
Diffstat:
irctest.c | | | 285 | ++++++++++++++++++++++++++++++++----------------------------------------------- |
1 file changed, 116 insertions(+), 169 deletions(-)
diff --git a/irctest.c b/irctest.c
@@ -47,26 +47,14 @@
*/
typedef struct
{
- char * channel;
+ char ** channels;
+ int n_channels;
char * nick;
+ char ready;
} irc_ctx_t;
-
-/*
- * Params that we give to our threads.
- */
-typedef struct
-{
- irc_session_t * session;
- const char * phrase;
- const char * channel;
- int timer;
-
-} spam_params_t;
-
-
THREAD_FUNCTION(irc_listen)
{
irc_session_t * sp = (irc_session_t *) arg;
@@ -76,39 +64,6 @@ THREAD_FUNCTION(irc_listen)
return 0;
}
-THREAD_FUNCTION(gen_spam)
-{
- /*spam_params_t * sp = (spam_params_t *) arg;
- int res;
-
- printf("FINI %d\n", res);*/
-
- return 0;
-}
-
-
-void addlog (const char * fmt, ...)
-{
- FILE * fp;
- char buf[1024];
- va_list va_alist;
-
- va_start (va_alist, fmt);
-#if defined (WIN32)
- _vsnprintf (buf, sizeof(buf), fmt, va_alist);
-#else
- vsnprintf (buf, sizeof(buf), fmt, va_alist);
-#endif
- va_end (va_alist);
-
- printf ("%s\n", buf);
-
- if ( (fp = fopen ("irctest.log", "ab")) != 0 )
- {
- fprintf (fp, "%s\n", buf);
- fclose (fp);
- }
-}
void dump_event (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
@@ -127,7 +82,7 @@ void dump_event (irc_session_t * session, const char * event, const char * origi
}
- addlog ("Event \"%s\", origin: \"%s\", params: %d [%s]", event, origin ? origin : "NULL", cnt, buf);
+ fprintf (stderr, "Event \"%s\", origin: \"%s\", params: %d [%s]\n", event, origin ? origin : "NULL", cnt, buf);
}
@@ -135,41 +90,13 @@ 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");
- irc_cmd_msg (session, params[0], "Hi all");
- irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
+ printf("joined\n");
+ /*irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
if ( !origin )
- return;
-
- // We need to know whether WE are joining the channel, or someone else.
- // To do this, we compare the origin with our nick.
- // Note that we have set LIBIRC_OPTION_STRIPNICKS to obtain 'parsed' nicks.
- if ( !strcmp(origin, ctx->nick) )
- {
- static spam_params_t spam1;
- //thread_id_t tid;
-
- spam1.session = session;
- spam1.channel = ctx->channel;
-
- spam1.phrase = "HEHE";
-
- spam1.timer = 2;
+ return;*/
-/* printf ("We just joined the channel %s; starting the spam threads\n", params[1]);
-
- if ( CREATE_THREAD (&tid, gen_spam, &spam1) )
- printf ("CREATE_THREAD failed: %s\n", strerror(errno));
- else
- printf ("Spammer thread was started successfully.\n");*/
- }
- else
- {
- char textbuf[168];
- sprintf (textbuf, "Hey, %s, hi!", origin);
- irc_cmd_msg (session, params[0], textbuf);
- }
}
@@ -178,7 +105,12 @@ void event_connect (irc_session_t * session, const char * event, const char * or
irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
dump_event (session, event, origin, params, count);
- irc_cmd_join (session, ctx->channel, 0);
+ 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);
}
@@ -186,9 +118,9 @@ void event_privmsg (irc_session_t * session, const char * event, const char * or
{
dump_event (session, event, origin, params, count);
- printf ("'%s' said me (%s): %s\n",
+ printf ("[ %s ] < %s > %s\n", params[0],
origin ? origin : "someone",
- params[0], params[1] );
+ params[1] );
}
@@ -251,19 +183,18 @@ void dcc_file_recv_callback (irc_session_t * session, irc_dcc_t id, int status,
void event_channel (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
- char nickbuf[128];
+ //char nickbuf[128];
if ( count != 2 )
return;
- printf ("'%s' said in channel %s: %s\n",
- origin ? origin : "someone",
- params[0], params[1] );
+ printf ("[ %s ] < %s > %s\n", params[0],
+ origin ? origin : "someone", params[1] );
if ( !origin )
return;
- irc_target_get_nick (origin, nickbuf, sizeof(nickbuf));
+ /*irc_target_get_nick (origin, nickbuf, sizeof(nickbuf));
if ( !strcmp (params[1], "quit") )
irc_cmd_quit (session, "of course, Master!");
@@ -307,7 +238,7 @@ void event_channel (irc_session_t * session, const char * event, const char * or
irc_cmd_nick (session, params[1] + 5);
if ( strstr (params[1], "whois ") == params[1] )
- irc_cmd_whois (session, params[1] + 5);
+ irc_cmd_whois (session, params[1] + 5);*/
}
@@ -341,84 +272,100 @@ void event_numeric (irc_session_t * session, unsigned int event, const char * or
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;
-
- //callbacks.event_dcc_chat_req = irc_event_dcc_chat;
- //callbacks.event_dcc_send_req = irc_event_dcc_send;
-
- s = irc_create_session (&callbacks);
-
- if ( !s )
- {
- printf ("Could not create session\n");
- return 1;
- }
-
- ctx.channel = argv[3];
- ctx.nick = argv[2];
-
- irc_set_ctx (s, &ctx);
-
- irc_option_set (s, LIBIRC_OPTION_STRIPNICKS);
-
- if ( irc_connect (s, argv[1], 6667, 0, argv[2], 0, 0) )
- {
- printf ("Could not connect: %s\n", irc_strerror (irc_errno(s)));
- return 1;
- }
-
-
- thread_id_t tid;
- if ( CREATE_THREAD (&tid, irc_listen, s) )
- printf ("CREATE_THREAD failed: %s\n", strerror(errno));
- else
- printf ("Listener thread was started successfully.\n");
-
- void **value_ptr;
- char *line = NULL;
- int res, size=100;
-
- line = (char*) malloc(size+1);
-
- while ( (res = getline((char**) &line, (size_t*) &size, stdin)) != -1 )
- {
- if ( irc_cmd_msg (s, ctx.channel, line) )
- break;
-
- line = NULL;
- }
-
- free(line);
-
- //pthread_join(tid, value_ptr);
-
- return 1;
+ 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;
+
+ //callbacks.event_dcc_chat_req = irc_event_dcc_chat;
+ //callbacks.event_dcc_send_req = irc_event_dcc_send;
+
+ s = irc_create_session (&callbacks);
+
+ if ( !s )
+ {
+ sprintf (stderr, "Could not create session\n");
+ return 1;
+ }
+
+ ctx.channels = argv;
+ ctx.ready = 0;
+ ctx.n_channels = argc - 3;
+ ctx.nick = argv[2];
+
+ irc_set_ctx (s, &ctx);
+
+ //irc_option_set (s, LIBIRC_OPTION_STRIPNICKS);
+
+ if ( irc_connect (s, argv[1], 6667, 0, argv[2], 0, 0) )
+ {
+ sprintf (stderr, "Could not connect: %s\n", irc_strerror (irc_errno(s)));
+ return 1;
+ }
+
+
+ thread_id_t tid;
+ if ( CREATE_THREAD (&tid, irc_listen, s) )
+ {
+ sprintf (stderr, "CREATE_THREAD failed: %s\n", strerror(errno));
+ return 42;
+ }
+ /*else
+ printf ("Listener thread was started successfully.\n");*/
+
+ void **value_ptr;
+ 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;
}