- build fix for poor distros who have a hard time with libircclient (now we just include this small lib in the source tree) so no longer required as an external lib

This commit is contained in:
Mark Vejvoda
2010-12-27 09:01:40 +00:00
parent b76cc89b60
commit 8d8afe78b5
873 changed files with 99837 additions and 95 deletions

View File

@@ -0,0 +1,41 @@
# $Id: Makefile.in 42 2004-10-10 16:16:15Z gyunaev $
CC = @CC@
CXX = @CXX@
CFLAGS = -Wall @CFLAGS@
LIBS = ../src/libircclient.a -lpthread @LIBS@
INCLUDES=-I../include
EXAMPLES=spammer censor irctest ircftp colors
all: $(EXAMPLES)
spammer: spammer.o
$(CC) -o spammer spammer.o $(LIBS)
colors: colors.o
$(CXX) -o colors colors.o $(LIBS)
irctest: irctest.o
$(CC) -o irctest irctest.o $(LIBS)
censor: censor.o
$(CXX) -o censor censor.o $(LIBS)
ircftp: ircftp.o
$(CXX) -o ircftp ircftp.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 $@ $<

View File

@@ -0,0 +1,194 @@
/*
* 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 'guards' the channel against using abusive language. When
* someone says a bad word, it takes some action against him/her, taking
* in account the number of times the person uses it. The first time, it just
* warns the person through a private message, the second time it warns him
* publically in channel, and after the second time it will kicks the insolent
* out of the channel.
*
* To keep it simple, this example reacts only on 'fuck' word, however
* is is easy to add more.
*
* Features used:
* - nickname parsing;
* - handling 'channel' event to track the messages;
* - handling 'nick' event to track nickname changes;
* - generating channel and private messages, and kicking.
*/
#include <map>
#include <string>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#if !defined (WIN32)
#include <unistd.h>
#endif
#include "libircclient.h"
/*
* We store data in IRC session context.
*/
typedef struct
{
char * channel;
char * nick;
std::map <std::string, unsigned int> insolents;
} irc_ctx_t;
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);
irc_cmd_join (session, ctx->channel, 0);
}
void event_nick (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
char nickbuf[128];
irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
if ( !origin || count != 1 )
return;
irc_target_get_nick (origin, nickbuf, sizeof(nickbuf));
if ( ctx->insolents.find(nickbuf) != ctx->insolents.end() )
{
printf ("%s has changed its nick to %s to prevent penalties - no way!\n",
nickbuf, params[0]);
ctx->insolents[params[0]] = ctx->insolents[nickbuf];
ctx->insolents.erase (nickbuf);
}
}
void event_channel (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);
if ( !origin || count != 2 )
return;
if ( strstr (params[1], "fuck") == 0 )
return;
char nickbuf[128], text[256];
irc_target_get_nick (origin, nickbuf, sizeof(nickbuf));
if ( ctx->insolents.find(nickbuf) == ctx->insolents.end() )
ctx->insolents[nickbuf] = 0;
ctx->insolents[nickbuf]++;
printf ("'%s' swears in the channel '%s' %d times\n",
nickbuf,
params[1],
ctx->insolents[nickbuf]);
switch (ctx->insolents[nickbuf])
{
case 1:
// Send a private message
sprintf (text, "%s, please do not swear in this channel.", nickbuf);
irc_cmd_msg (session, nickbuf, text);
break;
case 2:
// Send a channel message
sprintf (text, "%s, do not swear in this channel, or you'll leave it.", nickbuf);
irc_cmd_msg (session, params[0], text);
break;
default:
// Send a channel notice, and kick the insolent
sprintf (text, "kicked %s from %s for swearing.", nickbuf, params[0]);
irc_cmd_me (session, params[0], text);
irc_cmd_kick (session, nickbuf, params[0], "swearing");
break;
}
}
void event_numeric (irc_session_t * session, unsigned int event, const char * origin, const char ** params, unsigned int count)
{
if ( event > 400 )
{
std::string fulltext;
for ( unsigned int i = 0; i < count; i++ )
{
if ( i > 0 )
fulltext += " ";
fulltext += params[i];
}
printf ("ERROR %d: %s: %s\n", event, origin ? origin : "?", fulltext.c_str());
}
}
int main (int argc, char **argv)
{
irc_callbacks_t callbacks;
irc_ctx_t ctx;
if ( argc != 4 )
{
printf ("Usage: %s <server> <nick> <channel>\n", argv[0]);
return 1;
}
// Initialize the callbacks
memset (&callbacks, 0, sizeof(callbacks));
// Set up the callbacks we will use
callbacks.event_connect = event_connect;
callbacks.event_channel = event_channel;
callbacks.event_nick = event_nick;
callbacks.event_numeric = event_numeric;
// And create the IRC session; 0 means error
irc_session_t * s = irc_create_session (&callbacks);
if ( !s )
{
printf ("Could not create IRC session\n");
return 1;
}
ctx.channel = argv[3];
ctx.nick = argv[2];
irc_set_ctx (s, &ctx);
// Initiate the IRC server connection
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;
}
// and run into forever loop, generating events
irc_run (s);
return 1;
}

View File

@@ -0,0 +1,143 @@
/*
* 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 program will test automatic color replacement features. It uses the
* colors itself, generates colored string in reaction to 'test' word, and
* dumps the de-colored messages.
*/
#include <string>
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <stdlib.h>
#include "libircclient.h"
/*
* We store data in IRC session context.
*/
typedef struct
{
char * channel;
char * nick;
} irc_ctx_t;
void event_join (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
char * str = irc_color_convert_to_mirc ("[B]Hi[/B] [U]all[/U]");
irc_cmd_msg (session, params[0], str);
free (str);
}
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);
irc_cmd_join (session, ctx->channel, 0);
}
void event_channel (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
printf ("'%s' said in channel %s: %s\n",
origin ? origin : "someone",
params[0], params[1] );
char * stripped = irc_color_strip_from_mirc (params[1]);
irc_cmd_msg (session, params[0], stripped);
free (stripped);
stripped = irc_color_convert_from_mirc (params[1]);
irc_cmd_msg (session, params[0], stripped);
free (stripped);
if ( !strcmp (params[1], "test") )
{
char * str = irc_color_convert_to_mirc ("normal, [B]bold[/B], [I]reverse[/I], [U]underline[/U], [COLOR=RED]red on white[/COLOR], [COLOR=YELLOW/BLACK]yellow on black[/COLOR]");
irc_cmd_msg (session, params[0], str);
free (str);
}
if ( strstr (params[1], "testme") == params[1] )
{
char * str = irc_color_convert_to_mirc (params[1] + 7);
irc_cmd_msg (session, params[0], str);
free (str);
}
}
void event_numeric (irc_session_t * session, unsigned int event, const char * origin, const char ** params, unsigned int count)
{
if ( event > 400 )
{
std::string fulltext;
for ( unsigned int i = 0; i < count; i++ )
{
if ( i > 0 )
fulltext += " ";
fulltext += params[i];
}
printf ("ERROR %d: %s: %s\n", event, origin ? origin : "?", fulltext.c_str());
}
}
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_channel = event_channel;
callbacks.event_numeric = event_numeric;
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);
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;
}
irc_run (s);
return 1;
}

View File

@@ -0,0 +1,172 @@
/*
* 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 emulates a simple file server. Only 'list' and 'get' commands
* are supported.
*
* Features used:
* - automatic nickname parsing using LIBIRC_OPTION_STRIPNICKS;
* - handling privmsg events to parse commands;
* - generating listings and DCC file transfer;
*
* $Id: ircftp.cpp 73 2009-01-03 11:14:59Z gyunaev $
*/
#include <map>
#include <string>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include "libircclient.h"
#define FILES_DIR "."
/*
* We store data in IRC session context.
*/
typedef struct
{
std::string channel;
std::string nick;
} irc_ctx_t;
void dcc_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");
}
else if ( status )
{
printf ("File sent error: %s (%d)\n", irc_strerror(status), status);
}
else
{
printf ("File sent progress: %d\n", length);
}
}
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);
irc_cmd_join (session, ctx->channel.c_str(), 0);
}
void event_privmsg (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
if ( !origin || count != 2 )
return;
if ( !strcasecmp (params[1], "list") )
{
dirent *d;
DIR *dir = opendir(FILES_DIR);
if ( !dir )
return;
while ( (d = readdir (dir)) != 0 )
{
if ( !strcmp (d->d_name, ".") )
continue;
irc_cmd_msg (session, origin, d->d_name);
}
closedir (dir);
}
else if ( strstr (params[1], "get ") == params[1] )
{
irc_dcc_t dccid;
if ( irc_dcc_sendfile (session, 0, origin, params[1] + 4, dcc_callback, &dccid) )
irc_cmd_msg (session, origin, "Could not send this file");
}
else
{
irc_cmd_msg (session, origin, "Commands: send | list");
}
}
void event_numeric (irc_session_t * session, unsigned int event, const char * origin, const char ** params, unsigned int count)
{
if ( event > 400 )
{
std::string fulltext;
for ( unsigned int i = 0; i < count; i++ )
{
if ( i > 0 )
fulltext += " ";
fulltext += params[i];
}
printf ("ERROR %d: %s: %s\n", event, origin ? origin : "?", fulltext.c_str());
}
}
int main (int argc, char **argv)
{
irc_callbacks_t callbacks;
irc_ctx_t ctx;
if ( argc != 4 )
{
printf ("Usage: %s <server> <nick> <channel>\n", argv[0]);
return 1;
}
// Initialize the callbacks
memset (&callbacks, 0, sizeof(callbacks));
// Set up the callbacks we will use
callbacks.event_connect = event_connect;
callbacks.event_privmsg = event_privmsg;
callbacks.event_numeric = event_numeric;
// And create the IRC session; 0 means error
irc_session_t * s = irc_create_session (&callbacks);
if ( !s )
{
printf ("Could not create IRC session\n");
return 1;
}
ctx.channel = argv[3];
ctx.nick = argv[2];
irc_set_ctx (s, &ctx);
irc_option_set (s, LIBIRC_OPTION_STRIPNICKS);
// Initiate the IRC server connection
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;
}
// and run into forever loop, generating events
irc_run (s);
return 1;
}

View File

@@ -0,0 +1,322 @@
/*
* 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 "libircclient.h"
/*
* We store data in IRC session context.
*/
typedef struct
{
char * channel;
char * nick;
} irc_ctx_t;
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)
{
char buf[512];
int cnt;
buf[0] = '\0';
for ( cnt = 0; cnt < count; cnt++ )
{
if ( cnt )
strcat (buf, "|");
strcat (buf, params[cnt]);
}
addlog ("Event \"%s\", origin: \"%s\", params: %d [%s]", 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");
irc_cmd_msg (session, params[0], "Hi all");
}
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);
irc_cmd_join (session, ctx->channel, 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' said me (%s): %s\n",
origin ? origin : "someone",
params[0], 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' said in channel %s: %s\n",
origin ? origin : "someone",
params[0], 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;
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);
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;
}
irc_run (s);
return 1;
}

View File

@@ -0,0 +1,194 @@
/*
* 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 spams the specified channels with words 'HAHA', 'HEHE' and
* 'HUHU' using three threads. Its main purpose is to test multithreading
* support of libircclient.
*/
#include <stdio.h>
#include <errno.h>
#include <string.h>
#if defined (WIN32)
#include <windows.h>
#define CREATE_THREAD(id,func,param) (CreateThread(0, 0, func, param, 0, id) == 0)
#define THREAD_FUNCTION(funcname) static DWORD WINAPI funcname (LPVOID arg)
#define thread_id_t DWORD
#define sleep(a) Sleep (a*1000)
#else
#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
#endif
#include "libircclient.h"
/*
* We store data in IRC session context.
*/
typedef struct
{
char * channel;
char * nick;
} 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(gen_spam)
{
spam_params_t * sp = (spam_params_t *) arg;
while ( 1 )
{
if ( irc_cmd_msg (sp->session, sp->channel, sp->phrase) )
break;
sleep(sp->timer);
}
return 0;
}
void event_join (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);
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;
static spam_params_t spam2;
static spam_params_t spam3;
thread_id_t tid;
spam1.session = spam2.session = spam3.session = session;
spam1.channel = spam2.channel = spam3.channel = ctx->channel;
spam1.phrase = "HEHE";
spam2.phrase = "HAHA";
spam3.phrase = "HUHU";
spam1.timer = 2;
spam2.timer = 3;
spam3.timer = 4;
printf ("We just joined the channel %s; starting the spam threads\n", params[1]);
if ( CREATE_THREAD (&tid, gen_spam, &spam1)
|| CREATE_THREAD (&tid, gen_spam, &spam2)
|| CREATE_THREAD (&tid, gen_spam, &spam3) )
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);
}
}
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);
irc_cmd_join (session, ctx->channel, 0);
}
void event_numeric (irc_session_t * session, unsigned int event, const char * origin, const char ** params, unsigned int count)
{
if ( event > 400 )
{
printf ("ERROR %d: %s: %s %s %s %s\n",
event,
origin ? origin : "unknown",
params[0],
count > 1 ? params[1] : "",
count > 2 ? params[2] : "",
count > 3 ? params[3] : "");
}
}
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;
}
// Initialize the callbacks
memset (&callbacks, 0, sizeof(callbacks));
// Set up the callbacks we will use
callbacks.event_connect = event_connect;
callbacks.event_join = event_join;
callbacks.event_numeric = event_numeric;
ctx.channel = argv[3];
ctx.nick = argv[2];
// And create the IRC session; 0 means error
s = irc_create_session (&callbacks);
if ( !s )
{
printf ("Could not create IRC session\n");
return 1;
}
irc_set_ctx (s, &ctx);
irc_option_set (s, LIBIRC_OPTION_STRIPNICKS);
// Initiate the IRC server connection
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;
}
// and run into forever loop, generating events
irc_run (s);
return 1;
}