quote-bot/events.c

236 lines
6.0 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 "libircclient.h"
#include "data.h"
#include "events.h"
2021-03-11 20:48:32 -05:00
#include "strings.h"
2021-03-09 01:39:19 -05:00
#include "threads.h"
2021-03-08 23:16:14 -05:00
#ifndef UNUSED
#define UNUSED(x) (void)(x)
#endif
char F_SPAM_THREADS = STOPPED;
2021-03-11 20:48:32 -05:00
char say_hi = 1;
2021-03-08 23:16:14 -05:00
THREAD_FUNCTION(gen_spam) {
struct spam_params_t *sp = (struct spam_params_t *)arg;
while (RUNNING == F_SPAM_THREADS) {
2021-03-08 23:16:14 -05:00
if (irc_cmd_msg(sp->session, sp->channel, sp->phrase))
break;
if (sp->timer > 0)
sleep((unsigned int)sp->timer);
2021-03-08 23:16:14 -05:00
}
return 0;
}
EVENT_SIGNATURE(start_spam) {
struct irc_ctx_t *ctx;
static struct spam_params_t spam1;
static struct spam_params_t spam2;
static struct spam_params_t spam3;
thread_id_t tid;
2021-03-11 20:48:32 -05:00
UNUSED(count);
2021-03-08 23:16:14 -05:00
UNUSED(event);
UNUSED(origin);
UNUSED(params);
2021-03-11 20:48:32 -05:00
ctx = (struct irc_ctx_t *)irc_get_ctx(session);
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;
2021-03-11 20:48:32 -05:00
F_SPAM_THREADS = RUNNING;
if (CREATE_THREAD(&tid, gen_spam, &spam1)
2021-03-11 20:48:32 -05:00
|| CREATE_THREAD(&tid, gen_spam, &spam2)
|| CREATE_THREAD(&tid, gen_spam, &spam3))
printf("CREATE_THREAD failed: %s\n", strerror(errno));
else
2021-03-11 20:48:32 -05:00
printf(string_spam_success);
2021-03-08 23:16:14 -05:00
}
void dump_event(irc_session_t * session, const char *event, const char *origin, const char **params, unsigned int count) {
char buf[1024];
unsigned int cnt;
2021-03-08 23:16:14 -05:00
UNUSED(session);
buf[0] = '\0';
memset(buf, 0, sizeof buf);
for (cnt = 0; cnt < count; cnt++) {
if (cnt)
strcat(buf, "|");
strcat(buf, params[cnt]);
}
2021-03-11 20:48:32 -05:00
printf("[%s] %s : [%d] %s\n", event, origin ? origin : "(null)", cnt, buf);
}
EVENT_SIGNATURE(event_connect) {
struct irc_ctx_t *ctx;
UNUSED(event);
2021-03-08 23:16:14 -05:00
UNUSED(origin);
UNUSED(params);
UNUSED(count);
ctx = (struct irc_ctx_t *)irc_get_ctx(session);
2021-03-11 20:48:32 -05:00
printf(string_connect_success, ctx->server, ctx->port, ctx->nick, ctx->channel);
irc_cmd_join(session, ctx->channel, 0);
}
EVENT_NUMERIC_SIGNATURE(event_numeric) {
char buf[24];
snprintf(buf, sizeof buf, "%d", event);
2021-03-11 20:48:32 -05:00
dump_event(session, buf, origin, params, count);
2021-03-08 23:16:14 -05:00
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] : "");
}
}
2021-03-11 20:48:32 -05:00
int IsAdmin(const char *name) {
return !strcmp(name, "Bubblegumdrop");
}
2021-03-08 23:16:14 -05:00
EVENT_SIGNATURE(event_channel) {
struct irc_ctx_t *ctx;
2021-03-08 23:16:14 -05:00
char nickbuf[128];
UNUSED(event);
UNUSED(params);
UNUSED(count);
2021-03-11 20:48:32 -05:00
dump_event(session, event, origin, params, count);
2021-03-08 23:16:14 -05:00
if (NULL == origin || count < 2)
2021-03-08 23:16:14 -05:00
return;
irc_target_get_nick(origin, nickbuf, sizeof(nickbuf));
2021-03-11 20:48:32 -05:00
if (!IsAdmin(nickbuf)) {
return;
}
ctx = (struct irc_ctx_t *)irc_get_ctx(session);
2021-03-08 23:16:14 -05:00
UNUSED(ctx);
if (!strcmp(params[1], ".stop")) {
irc_cmd_msg(session, params[0], ":x");
F_SPAM_THREADS = STOPPED;
}
if (!strcmp(params[1], ".start")) {
irc_cmd_msg(session, params[0], ":v");
F_SPAM_THREADS = RUNNING;
2021-03-11 20:48:32 -05:00
start_spam(session, ctx->nick, origin, params, count);
}
if (!strcmp(params[1], ".quit")) {
F_MAIN_THREAD = STOPPED;
F_IRC_THREAD = STOPPED;
F_MAIN_THREAD = STOPPED;
irc_cmd_quit(session, "Bye!");
}
2021-03-08 23:16:14 -05:00
#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) {
2021-03-11 20:48:32 -05:00
char buf[128];
struct irc_ctx_t *ctx;
2021-03-08 23:16:14 -05:00
UNUSED(count);
UNUSED(event);
2021-03-08 23:16:14 -05:00
if (!origin)
return;
ctx = (struct irc_ctx_t *)irc_get_ctx(session);
2021-03-08 23:16:14 -05:00
/*
* 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)) {
2021-03-11 20:48:32 -05:00
if (say_hi)
irc_cmd_msg(session, params[0], "Hi");
/*
printf("We just joined the channel %s; starting the spam threads\n", params[0]);
start_spam (session, ctx->nick, origin, params, count);
*/
} else if (say_hi) {
memset(&buf, 0, sizeof buf);
snprintf(buf, (sizeof buf) - 1, "Hey, %s, hi!", origin);
irc_cmd_msg(session, params[0], buf);
2021-03-08 23:16:14 -05:00
}
}
EVENT_SIGNATURE(event_nick) {
char nickbuf[128];
struct irc_ctx_t *ctx;
2021-03-08 23:16:14 -05:00
UNUSED(event);
UNUSED(params);
if (!origin || count != 1)
return;
irc_target_get_nick(origin, nickbuf, sizeof(nickbuf));
ctx = (struct irc_ctx_t *)irc_get_ctx(session);
2021-03-08 23:16:14 -05:00
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
}