Moving things around.
This commit is contained in:
parent
61ba8a692f
commit
eb77e1ac0e
30
db.c
30
db.c
@ -15,7 +15,7 @@
|
||||
|
||||
#include "io.h"
|
||||
|
||||
static int callback(void *NotUsed, int argc, char **argv, char **azColName) {
|
||||
static int callback_run_script(void *NotUsed, int argc, char **argv, char **azColName) {
|
||||
int i;
|
||||
UNUSED(NotUsed);
|
||||
for (i = 0; i < argc; i++) {
|
||||
@ -24,51 +24,45 @@ static int callback(void *NotUsed, int argc, char **argv, char **azColName) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
int test_db(char *script_filename) {
|
||||
char *orig_statements;
|
||||
char *saveptr1;
|
||||
char *statements;
|
||||
char *str1, *token;
|
||||
char *zErrMsg;
|
||||
const char *db_name;
|
||||
int j;
|
||||
int rc;
|
||||
int run_script(const char *db_name, const char *script_filename) {
|
||||
int j, rc;
|
||||
char *copy_statements, *orig_statements, *saveptr1, *str1, *token, *zErrMsg;
|
||||
sqlite3 *db;
|
||||
|
||||
zErrMsg = 0;
|
||||
orig_statements = file_read(script_filename);
|
||||
if (orig_statements) {
|
||||
statements = strndup(orig_statements, 4096);
|
||||
copy_statements = strndup(orig_statements, 4096);
|
||||
} else {
|
||||
fprintf(stderr, "Error reading file: %s\n", script_filename);
|
||||
return -1;
|
||||
}
|
||||
|
||||
db_name = "familyGuy.db";
|
||||
rc = sqlite3_open(db_name, &db);
|
||||
if (rc) {
|
||||
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
||||
return -1;
|
||||
} else {
|
||||
fprintf(stderr, "Open database successfully\n");
|
||||
}
|
||||
|
||||
for (j = 1, str1 = statements;; j++, str1 = NULL) {
|
||||
for (j = 1, str1 = copy_statements;; j++, str1 = NULL) {
|
||||
token = strtok_r(str1, "\n", &saveptr1);
|
||||
if (token == NULL)
|
||||
break;
|
||||
if (strlen(token) > 2 && token[0] == '-' && token[1] == '-')
|
||||
continue;
|
||||
continue; /* Comment line; TODO multiline comment */
|
||||
printf("%d: %s\n", j, token);
|
||||
rc = sqlite3_exec(db, token, callback, 0, &zErrMsg);
|
||||
rc = sqlite3_exec(db, token, callback_run_script, 0, &zErrMsg);
|
||||
if (rc != SQLITE_OK) {
|
||||
fprintf(stderr, "SQL error: %s\n", sqlite3_errmsg(db));
|
||||
fprintf(stderr, " !! SQL error: %s\n", sqlite3_errmsg(db));
|
||||
sqlite3_free(zErrMsg);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
free(copy_statements);
|
||||
free(orig_statements);
|
||||
free(statements);
|
||||
sqlite3_close(db);
|
||||
fprintf(stderr, "Bye!\n");
|
||||
|
||||
@ -84,6 +78,6 @@ int main(int argc, char **argv) {
|
||||
fprintf(stderr, "Usage: %s <script>\n", argv[0]);
|
||||
return -1;
|
||||
}
|
||||
return test_db(script_filename);
|
||||
return run_script("familyGuy.sqlite3", script_filename);
|
||||
}
|
||||
#endif
|
||||
|
||||
2
db.h
2
db.h
@ -1,6 +1,6 @@
|
||||
#ifndef DB_H
|
||||
#define DB_H
|
||||
|
||||
int test_db(char *script_filename);
|
||||
int run_script(const char *,const char *);
|
||||
|
||||
#endif /* DB_H */
|
||||
|
||||
@ -1,65 +0,0 @@
|
||||
void event_channel (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
|
||||
{
|
||||
irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
|
||||
|
||||
if ( !origin || count != 2 )
|
||||
return;
|
||||
|
||||
if ( strstr (params[1], "fuck") == 0 )
|
||||
return;
|
||||
|
||||
char nickbuf[128], text[256];
|
||||
|
||||
irc_target_get_nick (origin, nickbuf, sizeof(nickbuf));
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
void event_nick (irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
|
||||
{
|
||||
char nickbuf[128];
|
||||
|
||||
irc_ctx_t * ctx = (irc_ctx_t *) irc_get_ctx (session);
|
||||
|
||||
if ( !origin || count != 1 )
|
||||
return;
|
||||
|
||||
irc_target_get_nick (origin, nickbuf, sizeof(nickbuf));
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
15
events.c
15
events.c
@ -4,24 +4,11 @@
|
||||
#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"
|
||||
#include "threads.h"
|
||||
|
||||
#ifndef UNUSED
|
||||
#define UNUSED(x) (void)(x)
|
||||
|
||||
46
levenshtein.c
Normal file
46
levenshtein.c
Normal file
@ -0,0 +1,46 @@
|
||||
#define _POSIX_C_SOURCE 200809L /* strtok_r, strndup */
|
||||
#include <string.h>
|
||||
#include <stdlib.h> /* malloc */
|
||||
|
||||
#include "levenshtein.h"
|
||||
|
||||
/* <https://rosettacode.org/wiki/Levenshtein_distance#C>
|
||||
* TODO: use this for quote duplicate removal */
|
||||
static int levenshtein_dist(const char *s, const char *t, int *d, int ls, int lt, int i, int j) {
|
||||
int x, y;
|
||||
int *n = d + i * ls + j;
|
||||
|
||||
if (*n >= 0)
|
||||
return *n;
|
||||
|
||||
if (i == ls)
|
||||
x = lt - j;
|
||||
else if (j == lt)
|
||||
x = ls - i;
|
||||
else if (s[i] == t[j])
|
||||
x = levenshtein_dist(s, t, d, ls, lt, i + 1, j + 1);
|
||||
else {
|
||||
x = levenshtein_dist(s, t, d, ls, lt, i + 1, j + 1);
|
||||
|
||||
if ((y = levenshtein_dist(s, t, d, ls, lt, i, j + 1)) < x)
|
||||
x = y;
|
||||
if ((y = levenshtein_dist(s, t, d, ls, lt, i + 1, j)) < x)
|
||||
x = y;
|
||||
x++;
|
||||
}
|
||||
return *n = x;
|
||||
}
|
||||
|
||||
int levenshtein(const char *s, const char *t) {
|
||||
int i, j, n;
|
||||
int ls = (int)strnlen(s, 128), lt = (int)strnlen(t, 128);
|
||||
int *d;
|
||||
d = (int*)malloc(sizeof(int) * (unsigned long)((ls + 1) * (lt + 1)));
|
||||
for (i = 0; i <= ls; i++)
|
||||
for (j = 0; j <= lt; j++)
|
||||
*(d + i * ls + j) = -1;
|
||||
n = levenshtein_dist(s, t, d, ls, lt, 0, 0);
|
||||
free(d);
|
||||
d = NULL;
|
||||
return n;
|
||||
}
|
||||
6
levenshtein.h
Normal file
6
levenshtein.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef LEVENSHTEIN_H
|
||||
#define LEVENSHTEIN_H
|
||||
|
||||
int levenshtein(const char *s, const char *t);
|
||||
|
||||
#endif /* LEVENSHTEIN_H */
|
||||
6
makefile
6
makefile
@ -23,9 +23,11 @@ CFLAGS += -g -O -Wall -Weffc++ -pedantic \
|
||||
#-ggdb -O0 -std=c89 -W -Wall -Wextra -pedantic
|
||||
LDFLAGS += -lpthread -lircclient -lsqlite3
|
||||
|
||||
OBJ = db.o events.o io.o levenshtein.o spammer.o
|
||||
|
||||
all: spammer
|
||||
|
||||
spammer: spammer.o io.o db.o events.o
|
||||
spammer: $(OBJ)
|
||||
$(CC) $(CFLAGS) $^ -o $@ $(LDFLAGS)
|
||||
|
||||
# TODO: client side / offline access
|
||||
@ -33,4 +35,4 @@ db: db.o io.o
|
||||
$(CC) -DTEST_DB $(CFLAGS) $^ -o $@ $(LDFLAGS)
|
||||
|
||||
clean:
|
||||
$(RM) -rf spammer db a.out *.o *.db
|
||||
$(RM) -rf spammer db a.out *.o *.db *.sqlite3
|
||||
|
||||
51
spammer.c
51
spammer.c
@ -32,71 +32,30 @@
|
||||
* Quote bot. WIP.
|
||||
*/
|
||||
|
||||
#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 */
|
||||
|
||||
#include "libircclient.h"
|
||||
|
||||
#include "data.h"
|
||||
#include "db.h"
|
||||
#include "events.h"
|
||||
#include "levenshtein.h"
|
||||
|
||||
#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;
|
||||
int *n = d + i * ls + j;
|
||||
|
||||
if (*n >= 0)
|
||||
return *n;
|
||||
|
||||
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++;
|
||||
}
|
||||
return *n = x;
|
||||
}
|
||||
|
||||
int levenshtein(const char *s, const char *t) {
|
||||
int i, j, n;
|
||||
int ls = (int)strnlen(s, 128), lt = (int)strnlen(t, 128);
|
||||
int *d;
|
||||
d = (int*)malloc(sizeof(int) * (unsigned long)((ls + 1) * (lt + 1)));
|
||||
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) {
|
||||
int dirty_exit;
|
||||
char *script_filename;
|
||||
unsigned short port = 6667;
|
||||
const char *db_name = "familyGuy.sqlite3";
|
||||
const 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";
|
||||
@ -104,7 +63,7 @@ int main(int argc, char **argv) {
|
||||
|
||||
if (argc == 2) {
|
||||
script_filename = argv[1];
|
||||
return test_db(script_filename);
|
||||
return run_script(db_name, script_filename);
|
||||
} else {
|
||||
if (argc != 4) {
|
||||
printf("Usage: %s <server> <nick> <channel>\n", argv[0]);
|
||||
|
||||
18
threads.h
Normal file
18
threads.h
Normal file
@ -0,0 +1,18 @@
|
||||
#ifndef THREADS_H
|
||||
#define THREADS_H
|
||||
|
||||
#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
|
||||
|
||||
#endif /* THREADS_H */
|
||||
Loading…
Reference in New Issue
Block a user