2021-03-08 23:16:14 -05:00
|
|
|
/*
|
|
|
|
* code adapted from:
|
2021-03-08 16:59:27 -05:00
|
|
|
* <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.
|
|
|
|
*/
|
2021-03-08 23:16:14 -05:00
|
|
|
|
2021-03-08 16:59:27 -05:00
|
|
|
#define _POSIX_C_SOURCE 200809L /* strtok_r, strndup */
|
|
|
|
#include <string.h>
|
|
|
|
#include <assert.h> /* assert */
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdio.h> /* size_t */
|
|
|
|
#include <stdlib.h> /* malloc */
|
2021-03-08 23:16:14 -05:00
|
|
|
|
2021-03-08 16:59:27 -05:00
|
|
|
#include "libircclient.h"
|
|
|
|
|
2021-03-08 23:16:14 -05:00
|
|
|
#include "data.h"
|
|
|
|
#include "db.h"
|
|
|
|
#include "events.h"
|
|
|
|
|
2021-03-08 16:59:27 -05:00
|
|
|
#ifndef UNUSED
|
|
|
|
#define UNUSED(x) (void)(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* <https://rosettacode.org/wiki/Levenshtein_distance#C>
|
|
|
|
* TODO: use this for quote duplicate removal */
|
|
|
|
int dist(const char *s, const char *t, int *d, int ls, int lt, int i, int j) {
|
|
|
|
int x, y;
|
2021-03-08 23:16:14 -05:00
|
|
|
int *n = d + i * ls + j;
|
2021-03-08 16:59:27 -05:00
|
|
|
|
2021-03-08 23:16:14 -05:00
|
|
|
if (*n >= 0)
|
|
|
|
return *n;
|
2021-03-08 16:59:27 -05:00
|
|
|
|
|
|
|
if (i == ls)
|
|
|
|
x = lt - j;
|
|
|
|
else if (j == lt)
|
|
|
|
x = ls - i;
|
|
|
|
else if (s[i] == t[j])
|
|
|
|
x = dist(s, t, d, ls, lt, i + 1, j + 1);
|
|
|
|
else {
|
|
|
|
x = dist(s, t, d, ls, lt, i + 1, j + 1);
|
|
|
|
|
|
|
|
if ((y = dist(s, t, d, ls, lt, i, j + 1)) < x)
|
|
|
|
x = y;
|
|
|
|
if ((y = dist(s, t, d, ls, lt, i + 1, j)) < x)
|
|
|
|
x = y;
|
|
|
|
x++;
|
|
|
|
}
|
2021-03-08 23:16:14 -05:00
|
|
|
return *n = x;
|
2021-03-08 16:59:27 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int levenshtein(const char *s, const char *t) {
|
|
|
|
int i, j, n;
|
2021-03-08 23:16:14 -05:00
|
|
|
int ls = (int)strnlen(s, 128), lt = (int)strnlen(t, 128);
|
2021-03-08 16:59:27 -05:00
|
|
|
int *d;
|
2021-03-08 23:16:14 -05:00
|
|
|
d = (int*)malloc(sizeof(int) * (unsigned long)((ls + 1) * (lt + 1)));
|
2021-03-08 16:59:27 -05:00
|
|
|
for (i = 0; i <= ls; i++)
|
|
|
|
for (j = 0; j <= lt; j++)
|
|
|
|
*(d + i * ls + j) = -1;
|
|
|
|
n = dist(s, t, d, ls, lt, 0, 0);
|
|
|
|
free(d);
|
|
|
|
d = NULL;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2021-03-08 23:16:14 -05:00
|
|
|
int dirty_exit;
|
2021-03-08 16:59:27 -05:00
|
|
|
char *script_filename;
|
|
|
|
irc_callbacks_t callbacks;
|
|
|
|
irc_ctx_t ctx;
|
|
|
|
irc_session_t *s;
|
|
|
|
unsigned short port = 6667;
|
|
|
|
|
|
|
|
const char *s1 = "rosettacode";
|
|
|
|
const char *s2 = "raisethysword";
|
|
|
|
printf("distance between `%s' and `%s': %d\n", s1, s2, levenshtein(s1, s2));
|
|
|
|
|
|
|
|
if (argc == 2) {
|
|
|
|
script_filename = argv[1];
|
|
|
|
return test_db(script_filename);
|
|
|
|
} else {
|
|
|
|
if (argc != 4) {
|
|
|
|
printf("Usage: %s <server> <nick> <channel>\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2021-03-08 23:16:14 -05:00
|
|
|
|
2021-03-08 16:59:27 -05:00
|
|
|
/* 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);
|
|
|
|
/* If the port number is specified in the server string, use the port 0 so it gets parsed */
|
|
|
|
if (strchr(argv[1], ':') != 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 (argv[1][0] == '#' && argv[1][1] == '#') {
|
|
|
|
/* Skip the first character as libircclient needs only one # for SSL support, i.e. #irc.freenode.net */
|
|
|
|
argv[1]++;
|
|
|
|
irc_option_set(s, LIBIRC_OPTION_SSL_NO_VERIFY);
|
|
|
|
}
|
|
|
|
/* Initiate the IRC server connection */
|
|
|
|
if (irc_connect(s, argv[1], port, 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 */
|
|
|
|
if (irc_run(s)) {
|
|
|
|
printf("Could not connect or I/O error: %s\n", irc_strerror(irc_errno(s)));
|
|
|
|
return 1;
|
|
|
|
}
|
2021-03-08 23:16:14 -05:00
|
|
|
dirty_exit = 0;
|
|
|
|
return dirty_exit;
|
2021-03-08 16:59:27 -05:00
|
|
|
}
|