quote-bot/events.c

174 lines
4.8 KiB
C
Raw Normal View History

2021-03-08 23:16:14 -05:00
#include <string.h>
#include <assert.h> /* assert */
#include <errno.h>
#include <stdio.h> /* size_t */
#include <stdlib.h> /* malloc */
#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"
#include "data.h"
#include "events.h"
#ifndef UNUSED
#define UNUSED(x) (void)(x)
#endif
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;
if (sp->timer > 0)
sleep((unsigned int)
sp->timer);
}
return 0;
}
EVENT_SIGNATURE(event_connect) {
irc_ctx_t *ctx;
UNUSED(event);
UNUSED(origin);
UNUSED(params);
UNUSED(count);
ctx = (irc_ctx_t *)
irc_get_ctx(session);
irc_cmd_join(session, ctx->channel, 0);
}
EVENT_NUMERIC_SIGNATURE(event_numeric) {
UNUSED(session);
UNUSED(origin);
UNUSED(params);
UNUSED(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] : "");
}
}
EVENT_SIGNATURE(event_channel) {
irc_ctx_t *ctx;
char nickbuf[128];
UNUSED(event);
UNUSED(params);
UNUSED(count);
if (!origin || count != 2)
return;
if (strstr(params[1], "fuck") == 0)
return;
irc_target_get_nick(origin, nickbuf, sizeof(nickbuf));
ctx = (irc_ctx_t *) irc_get_ctx(session);
UNUSED(ctx);
#if 0
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;
}
#endif
}
EVENT_SIGNATURE(event_join) {
irc_ctx_t *ctx;
UNUSED(count);
UNUSED(event);
if (!origin)
return;
ctx = (irc_ctx_t *)
irc_get_ctx(session);
/*
* 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);
}
}
EVENT_SIGNATURE(event_nick) {
char nickbuf[128];
irc_ctx_t *ctx;
UNUSED(event);
UNUSED(params);
if (!origin || count != 1)
return;
irc_target_get_nick(origin, nickbuf, sizeof(nickbuf));
ctx = (irc_ctx_t *) irc_get_ctx(session);
UNUSED(ctx);
#if 0
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);
}
#endif
}