quote-bot/main.c

189 lines
5.9 KiB
C
Raw Normal View History

/*
* code adapted from:
* <https://gist.github.com/enile8/2424514>
* <https://www.rosettacode.org/wiki/Write_entire_file#C>
* <https://rosettacode.org/wiki/Read_entire_file#C>
* libircclient-1.10/examples/spammer.c
*
* Copyright (C) 2004-2009 Georgy Yunaev gyunaev@ulduzsoft.com
* Copyright (C) 2021 Bubblegumdrop <Bubblegumdrop@lain.church>
*
* 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.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* Version 2, December 2004
*
* Copyright (C) 2004 Sam Hocevar <sam@hocevar.net>
* Copyright (C) 2021 Bubblegumdrop <Bubblegumdrop@lain.church>
*
* Everyone is permitted to copy and distribute verbatim or modified
* copies of this license document, and changing it is allowed as long
* as the name is changed.
*
* DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
* TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
*
* 0. You just DO WHAT THE FUCK YOU WANT TO.
*
* Quote bot. WIP.
*/
#include <string.h>
#include <assert.h> /* assert */
#include <errno.h>
#include <stdio.h> /* size_t */
#include <unistd.h> /* getopt */
#include <getopt.h> /* getopt */
#include "libircclient.h"
#include "data.h"
#include "db.h"
#include "events.h"
#include "levenshtein.h"
#include "threads.h"
#ifndef UNUSED
#define UNUSED(x) (void)(x)
#endif
char F_IRC_THREAD = STOPPED;
char F_MAIN_THREAD = STOPPED;
THREAD_FUNCTION(irc_thread) {
const char *server;
unsigned short port = 6667;
irc_callbacks_t callbacks;
irc_session_t *s;
irc_ctx_t *ctx;
ctx = (irc_ctx_t *) arg;
assert(NULL != ctx);
server = ctx->server;
assert(NULL != server);
assert(NULL != ctx->nick);
assert(NULL != ctx->channel);
while (RUNNING == F_IRC_THREAD) {
/* Initialize the callbacks */
memset(&callbacks, 0, sizeof(callbacks));
/* Set up the callbacks we will use */
callbacks.event_channel = event_channel;
callbacks.event_connect = event_connect;
callbacks.event_join = event_join;
callbacks.event_nick = event_nick;
callbacks.event_numeric = event_numeric;
/* And create the IRC session; 0 means error */
s = irc_create_session(&callbacks);
if (!s) {
printf("Could not create IRC session\n");
return (void *)-1;
}
irc_set_ctx(s, &ctx);
irc_option_set(s, LIBIRC_OPTION_STRIPNICKS);
/* If the port number is specified in the server string, use the port 0 so it gets parsed */
if (strchr(server, ':') != 0)
port = 0;
/*
* To handle the "SSL certificate verify failed" from command line we allow passing ## in front
* of the server name, and in this case tell libircclient not to verify the cert
*/
if (server[0] == '#' && server[0] == '#') {
/* Skip the first character as libircclient needs only one # for SSL support, i.e. #irc.freenode.net */
server++;
irc_option_set(s, LIBIRC_OPTION_SSL_NO_VERIFY);
}
/* Initiate the IRC server connection */
if (irc_connect(s, server, port, 0, ctx->nick, 0, 0)) {
printf("Could not connect: %s\n", irc_strerror(irc_errno(s)));
return (void *)-1;
}
/* and run into forever loop, generating events */
if (irc_run(s)) {
printf("Could not connect or I/O error: %s\n", irc_strerror(irc_errno(s)));
return (void *)-1;
}
}
return (void *)0;
}
int main(int argc, char **argv) {
int rc, opt;
const char *db_name = NULL;
const char *script_filename = NULL;
const char *query_str = NULL;
irc_ctx_t ctx;
sqlite3 *db = NULL;
thread_id_t tid;
#if 0
const char *s1 = "rosettacode";
const char *s2 = "raisethysword";
printf("distance between `%s' and `%s': %d\n", s1, s2, levenshtein(s1, s2));
#endif
db_name = "familyGuy.sqlite3";
while ((opt = getopt(argc, argv, "c:d:s:h")) != -1) {
switch (opt) {
case 'c':
query_str = optarg;
break;
case 'd':
db_name = optarg;
break;
case 's':
script_filename = optarg;
break;
case 'h':
default:
fprintf(stderr, "Usage: %s [-h]\n", argv[0]);
return -1;
break;
}
}
if (NULL != db_name) {
rc = sqlite3_open(db_name, &db);
if (SQLITE_OK != rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
return -1;
}
} else {
printf("Open database successfully: ");
sqlite_version(db);
}
run_one(db, "create table if not exists quotedb (date_added text not null, added_by text not null, channel text not null, subject text not null, words text not null)");
if (query_str) {
run_one(db, query_str);
} else if (script_filename) {
run_script(db, script_filename);
} else if (argc == 4) {
ctx.server = argv[1];
ctx.nick = argv[2];
ctx.channel = argv[3];
F_IRC_THREAD = F_MAIN_THREAD = RUNNING;
if (CREATE_THREAD(&tid, irc_thread, &ctx)) {
printf("CREATE_THREAD failed: %s\n", strerror(errno));
} else {
printf("IRC thread was started successfully.\n");
}
while (RUNNING == F_MAIN_THREAD) {
sleep(1);
}
} else {
printf("Usage: %s <server> <nick> <channel>\n", argv[0]);
}
F_IRC_THREAD = STOPPED;
sqlite3_close(db);
return 0;
}