/* * code adapted from: * * * * libircclient-1.10/examples/spammer.c * * Copyright (C) 2004-2009 Georgy Yunaev gyunaev@ulduzsoft.com * Copyright (C) 2021 Bubblegumdrop * * 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 * Copyright (C) 2021 Bubblegumdrop * * 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. */ #define _POSIX_C_SOURCE 200809L /* strtok_r, strndup */ #include #include /* assert */ #include #include /* getopt */ #include /* size_t */ #include /* 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 extern void install_signal_handler(void); char F_IRC_THREAD = STOPPED; char F_MAIN_THREAD = STOPPED; irc_session_t *irc_session = NULL; THREAD_FUNCTION(irc_thread) { const char *server; irc_callbacks_t callbacks; struct irc_ctx_t *ctx; ctx = (struct irc_ctx_t *)arg; assert(NULL != ctx); server = ctx->server; assert(NULL != server); assert(NULL != ctx->nick); assert(NULL != ctx->channel); printf("Server: %s\nPort: %d\nNick: %s\nChannel: %s\n", ctx->server, ctx->port, ctx->nick, 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 */ irc_session = irc_create_session(&callbacks); if (!irc_session) { fprintf(stderr, "Could not create IRC session\n"); F_IRC_THREAD = STOPPED; } irc_set_ctx(irc_session, ctx); irc_option_set(irc_session, 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) ctx->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(irc_session, LIBIRC_OPTION_SSL_NO_VERIFY); } /* Initiate the IRC server connection */ if (irc_connect(irc_session, server, ctx->port, 0, ctx->nick, 0, 0)) { fprintf(stderr, "Could not connect: %s\n", irc_strerror(irc_errno(irc_session))); F_IRC_THREAD = STOPPED; } /* and run into forever loop, generating events */ if (irc_run(irc_session)) { fprintf(stderr, "Could not connect or I/O error: %s\n", irc_strerror(irc_errno(irc_session))); F_IRC_THREAD = STOPPED; } } F_MAIN_THREAD = STOPPED; return NULL; } int main(int argc, char *argv[]) { int rc, opt; const char *db_name = NULL; const char *script_filename = NULL; const char *query_str = NULL; struct irc_ctx_t ctx; sqlite3 *db = NULL; thread_id_t tid; size_t maxlen = 1024; install_signal_handler(); #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"; memset(&ctx, 0, sizeof(struct irc_ctx_t)); ctx.port = 6667; while ((opt = getopt(argc, argv, "q:d:hp:f:")) != -1) { switch (opt) { case 'q': query_str = optarg; break; case 'd': db_name = optarg; break; case 'p': ctx.port = atoi(optarg); break; case 'f': script_filename = optarg; break; case 'h': default: fprintf(stderr, "Usage: %s [-q query] [-d sqlite db] [-h] [-p port] [-f script file]\n", argv[0]); return -1; break; } } if (NULL != db_name) { if (SQLITE_OK == (rc = sqlite3_open(db_name, &db))) { printf("Open database successfully: "); sqlite_version(db); } } else { fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db)); return -1; } 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 = strndup(argv[1], maxlen); ctx.nick = strndup(argv[2], maxlen); ctx.channel = strndup(argv[3], maxlen); F_IRC_THREAD = RUNNING; 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); } free(ctx.server); free(ctx.nick); free(ctx.channel); } else { printf("Usage: %s \n", argv[0]); } F_IRC_THREAD = STOPPED; F_SPAM_THREADS = STOPPED; sqlite3_close(db); return 0; }