From 7d3cf217cb590429284ba854f6e78612de8bde70 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Mon, 11 Apr 2016 17:18:33 +1000 Subject: [PATCH] Added some utiltites, filepost and mgpost filepost posts a file to a filebase mgpost posts a message to a msgbase --- utils/filepost/Makefile | 15 +++++ utils/filepost/filepost.c | 96 +++++++++++++++++++++++++++++ utils/mgpost/Makefile | 15 +++++ utils/mgpost/mgpost.c | 125 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 251 insertions(+) create mode 100644 utils/filepost/Makefile create mode 100644 utils/filepost/filepost.c create mode 100644 utils/mgpost/Makefile create mode 100644 utils/mgpost/mgpost.c diff --git a/utils/filepost/Makefile b/utils/filepost/Makefile new file mode 100644 index 0000000..d53ebc8 --- /dev/null +++ b/utils/filepost/Makefile @@ -0,0 +1,15 @@ +CC=cc +CFLAGS=-I/usr/local/include +DEPS = filepost.c + +OBJ = filepost.o +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +filepost: $(OBJ) + $(CC) -o filepost -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 + +.PHONY: clean + +clean: + rm -f $(OBJ) filepost diff --git a/utils/filepost/filepost.c b/utils/filepost/filepost.c new file mode 100644 index 0000000..a94cd11 --- /dev/null +++ b/utils/filepost/filepost.c @@ -0,0 +1,96 @@ +#include +#include +#include +#include +#include + +int main(int argc, char **argv) { + int i; + char *create_sql = "CREATE TABLE IF NOT EXISTS files (" + "Id INTEGER PRIMARY KEY," + "filename TEXT," + "description TEXT," + "size INTEGER," + "dlcount INTEGER," + "approved INTEGER);"; + char *sql = "INSERT INTO files (filename, description, size, dlcount, approved) VALUES(?, ?, ?, 0, 1)"; + sqlite3 *db; + sqlite3_stmt *res; + int rc; + struct stat s; + char *err_msg = NULL; + FILE *fptr; + char *body; + int totlen; + int len; + char buffer[256]; + + if (argc < 4) { + printf("Usage:\n"); + printf("%s filename desc_file database\n", argv[0]); + exit(1); + } + + fptr = fopen(argv[2], "r"); + if (!fptr) { + body = strdup("No description."); + } else { + body = NULL; + totlen = 0; + + len = fread(buffer, 1, 256, fptr); + while (len > 0) { + totlen += len; + if (body == NULL) { + body = (char *)malloc(totlen + 1); + } else { + body = (char *)realloc(body, totlen + 1); + } + memcpy(&body[totlen - len], buffer, len); + body[totlen] = '\0'; + len = fread(buffer, 1, 256, fptr); + } + fclose(fptr); + } + + + rc = sqlite3_open(argv[3], &db); + + if (rc != SQLITE_OK) { + printf("Cannot open database: %s\n", sqlite3_errmsg(db)); + exit(1); + } + rc = sqlite3_exec(db, create_sql, 0, 0, &err_msg); + if (rc != SQLITE_OK ) { + printf("SQL error: %s\n", err_msg); + sqlite3_free(err_msg); + sqlite3_close(db); + exit(1); + } + rc = sqlite3_prepare_v2(db, sql, -1, &res, 0); + + if (rc == SQLITE_OK) { + stat(argv[1], &s); + + sqlite3_bind_text(res, 1, argv[1], -1, 0); + sqlite3_bind_text(res, 2, body, -1, 0); + sqlite3_bind_int(res, 3, s.st_size); + } else { + fprintf(stderr, "Failed to execute statement: %s\n", sqlite3_errmsg(db)); + sqlite3_finalize(res); + sqlite3_close(db); + exit(1); + } + + rc = sqlite3_step(res); + + if (rc != SQLITE_DONE) { + printf("execution failed: %s", sqlite3_errmsg(db)); + sqlite3_finalize(res); + sqlite3_close(db); + exit(1); + } + sqlite3_finalize(res); + sqlite3_close(db); + return 0; +} diff --git a/utils/mgpost/Makefile b/utils/mgpost/Makefile new file mode 100644 index 0000000..0d59091 --- /dev/null +++ b/utils/mgpost/Makefile @@ -0,0 +1,15 @@ +CC=cc +CFLAGS=-I/usr/local/include +DEPS = mgpost.c +JAMLIB = ../../jamlib/jamlib.a +OBJ = mgpost.o +%.o: %.c $(DEPS) + $(CC) -c -o $@ $< $(CFLAGS) + +mgpost: $(OBJ) + $(CC) -o mgpost -o $@ $^ $(CFLAGS) -L/usr/local/lib $(JAMLIB) + +.PHONY: clean + +clean: + rm -f $(OBJ) mgpost diff --git a/utils/mgpost/mgpost.c b/utils/mgpost/mgpost.c new file mode 100644 index 0000000..f407d75 --- /dev/null +++ b/utils/mgpost/mgpost.c @@ -0,0 +1,125 @@ +#include +#include +#include +#include +#include +#include "../../jamlib/jam.h" + +s_JamBase *open_jam_base(char *path) { + int ret; + s_JamBase *jb; + + ret = JAM_OpenMB((char *)path, &jb); + + if (ret != 0) { + if (ret == JAM_IO_ERROR) { + free(jb); + ret = JAM_CreateMB((char *)path, 1, &jb); + if (ret != 0) { + free(jb); + return NULL; + } + } + } + return jb; +} + +int main(int argc, char **argv) { + char buffer[256]; + char *body; + char *subject; + char *from; + FILE *fptr; + int len; + int totlen; + time_t thetime; + int z; + + s_JamBase *jb; + s_JamMsgHeader jmh; + s_JamSubPacket* jsp; + s_JamSubfield jsf; + + if (argc < 5) { + printf("Usage:\n"); + printf("%s filename jambase from subject\n", argv[0]); + exit(1); + } + fptr = fopen(argv[1], "r"); + + if (!fptr) { + printf("Unable to open %s\n", argv[1]); + exit(1); + } + body = NULL; + totlen = 0; + + len = fread(buffer, 1, 256, fptr); + while (len > 0) { + totlen += len; + if (body == NULL) { + body = (char *)malloc(totlen + 1); + } else { + body = (char *)realloc(body, totlen + 1); + } + memcpy(&body[totlen - len], buffer, len); + body[totlen] = '\0'; + len = fread(buffer, 1, 256, fptr); + } + + fclose(fptr); + + jb = open_jam_base(argv[2]); + if (!jb) { + printf("Unable to open JAM base %s\n", argv[2]); + exit(1); + } + thetime = time(NULL); + + JAM_ClearMsgHeader( &jmh ); + jmh.DateWritten = thetime; + + jmh.Attribute |= MSG_TYPELOCAL; + + jsp = JAM_NewSubPacket(); + jsf.LoID = JAMSFLD_SENDERNAME; + jsf.HiID = 0; + jsf.DatLen = strlen(argv[3]); + jsf.Buffer = (char *)argv[3]; + JAM_PutSubfield(jsp, &jsf); + + jsf.LoID = JAMSFLD_RECVRNAME; + jsf.HiID = 0; + jsf.DatLen = 3; + jsf.Buffer = "ALL"; + JAM_PutSubfield(jsp, &jsf); + + jsf.LoID = JAMSFLD_SUBJECT; + jsf.HiID = 0; + jsf.DatLen = strlen(argv[4]); + jsf.Buffer = (char *)argv[4]; + JAM_PutSubfield(jsp, &jsf); + + while (1) { + z = JAM_LockMB(jb, 100); + if (z == 0) { + break; + } else if (z == JAM_LOCK_FAILED) { + sleep(1); + } else { + printf("Failed to lock msg base!\n"); + break; + } + } + if (z == 0) { + if (JAM_AddMessage(jb, &jmh, jsp, (char *)body, strlen(body))) { + printf("Failed to add message\n"); + } + + JAM_UnlockMB(jb); + JAM_DelSubPacket(jsp); + } + JAM_CloseMB(jb); + + return 0; +}