Made some improvements to fileapprove utility

This commit is contained in:
Andrew Pamment 2016-12-07 12:46:55 +10:00
parent 145005129c
commit 990759ea84
2 changed files with 94 additions and 18 deletions

View File

@ -1,21 +1,24 @@
. . . .__ .__ __. . . . .__ .__ __.
|\/| _. _ * _.;_/ _. [__)[__)(__ |\/| _. _ * _.;_/ _. [__)[__)(__
| |(_](_]|(_.| \(_] [__)[__).__) v0.4a | |(_](_]|(_.| \(_] [__)[__).__) v0.5a
-------._|---------------------------------- -------._|----------------------------------
Magicka BBS is a Free BBS System for Linux Magicka BBS is a Free BBS System for Linux
and FreeBSD. While Still in the early stages and FreeBSD. While Still in the early stages
of development, we have most of the features of development, we have most of the features
you find in modern BBS software. you find in modern BBS software.
* FTN / WWIVnet Network Support * FTN Network Support
* Zmodem & Long filename Support * Zmodem & Long filename Support
* SSH Server & Telnet Server * SSH Server & Telnet Server
* External Editor Support * External Editor Support
* Compiles and runs on Raspberry Pi * Compiles and runs on Raspberry Pi
* LUA Scripting Support * LUA Scripting Support
* Optional WWW server. * Optional WWW server.
* Bluewave Support
* TIC file processor
THIS IS ALPHA SOFTWARE! If you're looking WWIVnet Support is known not to work, and
for something different, this might be for will be removed. Don't use it!
you. If you're looking for something
established, try Mystic, WWIV or Synchronet. THIS IS ALPHA SOFTWARE! Use at your own
risk!

View File

@ -1,5 +1,5 @@
#include <curses.h> #include <curses.h>
#include <cdk/cdk.h> #include <cdk.h>
#include <sqlite3.h> #include <sqlite3.h>
#include <stdio.h> #include <stdio.h>
#include <libgen.h> #include <libgen.h>
@ -17,6 +17,57 @@ int fcount = 0;
sqlite3 *db; sqlite3 *db;
CDKSCREEN *cdkscreen = 0; CDKSCREEN *cdkscreen = 0;
static int deleteFile(EObjectType cdktype, void *object, void *clientData, chtype input) {
int i;
CDKSCROLL *s = (CDKSCROLL *)object;
sqlite3_stmt *res;
int rc;
struct stat st;
char sql_delete[] = "DELETE FROM files WHERE filename LIKE ?";
int index = getCDKScrollCurrent(s);
if (index >= fcount) {
return FALSE;
}
rc = sqlite3_prepare_v2(db, sql_delete, -1, &res, 0);
if (rc != SQLITE_OK) {
fprintf(stderr, "Error deleting file! : %s\r\n", sqlite3_errmsg(db));
return FALSE;
}
sqlite3_bind_text(res, 1, f[index]->name, -1, 0);
sqlite3_step(res);
sqlite3_finalize(res);
if (stat(f[index]->name, &st) == 0) {
remove(f[index]->name);
}
for (i=index;i<fcount-1;i++) {
filenames[i] = filenames[i+1];
f[i] = f[i+1];
}
free(filenames[fcount-1]);
free(f[fcount-1]);
fcount--;
if (fcount == 0) {
free(filenames);
free(f);
filenames = NULL;
f = NULL;
} else {
filenames = realloc(filenames, sizeof(char *) * fcount);
f = realloc(f, sizeof(struct files) * fcount);
setCDKScrollItems(s, filenames, fcount, FALSE);
refreshCDKScreen(cdkscreen);
}
return FALSE;
}
static void doApprove(int index) { static void doApprove(int index) {
char sql_approve[] = "UPDATE files SET approved=1 WHERE filename LIKE ?"; char sql_approve[] = "UPDATE files SET approved=1 WHERE filename LIKE ?";
sqlite3_stmt *res; sqlite3_stmt *res;
@ -25,7 +76,7 @@ static void doApprove(int index) {
if (stat(f[index]->name, &st) == 0) { if (stat(f[index]->name, &st) == 0) {
f[index]->approved = 1; f[index]->approved = 1;
sprintf(filenames[index], "</24>%s<!24>", basename(f[index]->name)); sprintf(filenames[index], "</24>%s (approved)<!24>", basename(f[index]->name));
rc = sqlite3_prepare_v2(db, sql_approve, -1, &res, 0); rc = sqlite3_prepare_v2(db, sql_approve, -1, &res, 0);
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
fprintf(stderr, "Error approving file! : %s\r\n", sqlite3_errmsg(db)); fprintf(stderr, "Error approving file! : %s\r\n", sqlite3_errmsg(db));
@ -43,8 +94,13 @@ static void doDisapprove(int index) {
char sql_approve[] = "UPDATE files SET approved=0 WHERE filename LIKE ?"; char sql_approve[] = "UPDATE files SET approved=0 WHERE filename LIKE ?";
sqlite3_stmt *res; sqlite3_stmt *res;
int rc; int rc;
struct stat s;
f[index]->approved = 0; f[index]->approved = 0;
sprintf(filenames[index], "</32>%s<!32>", basename(f[index]->name)); if (stat(f[index]->name, &s) != 0) {
sprintf(filenames[index], "</16>%s (missing)<!16>", basename(f[index]->name));
} else {
sprintf(filenames[index], "</32>%s (unapproved)<!32>", basename(f[index]->name));
}
rc = sqlite3_prepare_v2(db, sql_approve, -1, &res, 0); rc = sqlite3_prepare_v2(db, sql_approve, -1, &res, 0);
if (rc != SQLITE_OK) { if (rc != SQLITE_OK) {
fprintf(stderr, "Error approving file! : %s\r\n", sqlite3_errmsg(db)); fprintf(stderr, "Error approving file! : %s\r\n", sqlite3_errmsg(db));
@ -62,6 +118,9 @@ static int approveFile(EObjectType cdktype, void *object, void *clientData, chty
int index = getCDKScrollCurrent(s); int index = getCDKScrollCurrent(s);
if (index >= fcount) {
return FALSE;
}
if (f[index]->approved == 1) { if (f[index]->approved == 1) {
doDisapprove(index); doDisapprove(index);
} else { } else {
@ -69,14 +128,15 @@ static int approveFile(EObjectType cdktype, void *object, void *clientData, chty
} }
setCDKScrollItems(s, filenames, fcount, FALSE); setCDKScrollItems(s, filenames, fcount, FALSE);
refreshCDKScreen(cdkscreen); refreshCDKScreen(cdkscreen);
return FALSE;
} }
int main(int argc, char **argv) { int main(int argc, char **argv) {
CDKSCROLL *scrollList = 0; CDKSCROLL *scrollList = 0;
CDKLABEL *instructions = 0;
WINDOW *cursesWin = 0; WINDOW *cursesWin = 0;
char *message[] = {"Press A to toggle Activation", "Press D to Delete file", "Press ESC to exit"};
char sql_read[] = "SELECT filename, description, approved FROM files"; char sql_read[] = "SELECT filename, description, approved FROM files";
CDK_PARAMS params; CDK_PARAMS params;
@ -96,11 +156,20 @@ int main(int argc, char **argv) {
sprintf(title, "</48>%s<!48>",basename(CDKparamString (&params, 'd'))); sprintf(title, "</48>%s<!48>",basename(CDKparamString (&params, 'd')));
instructions = newCDKLabel (
cdkscreen,
40,
1,
message,
3,
TRUE,
TRUE);
drawCDKLabel(instructions, TRUE);
scrollList = newCDKScroll(cdkscreen, scrollList = newCDKScroll(cdkscreen,
2, 2,
1, 1,
1, 1,
23, 36,
36, 36,
title, title,
NULL, NULL,
@ -108,7 +177,7 @@ int main(int argc, char **argv) {
FALSE, FALSE,
A_REVERSE, A_REVERSE,
TRUE, TRUE,
FALSE); TRUE);
if (scrollList == 0) { if (scrollList == 0) {
fprintf(stderr, "Unable to make scrolllist!"); fprintf(stderr, "Unable to make scrolllist!");
destroyCDKScreen(cdkscreen); destroyCDKScreen(cdkscreen);
@ -136,6 +205,9 @@ int main(int argc, char **argv) {
exit(1); exit(1);
} }
f = NULL;
filenames = NULL;
while(sqlite3_step(res) == SQLITE_ROW) { while(sqlite3_step(res) == SQLITE_ROW) {
if (fcount == 0) { if (fcount == 0) {
f = (struct files **)malloc(sizeof(struct files *)); f = (struct files **)malloc(sizeof(struct files *));
@ -156,17 +228,17 @@ int main(int argc, char **argv) {
f[fcount]->description = strdup((char *)sqlite3_column_text(res, 1)); f[fcount]->description = strdup((char *)sqlite3_column_text(res, 1));
f[fcount]->approved = sqlite3_column_int(res, 2); f[fcount]->approved = sqlite3_column_int(res, 2);
filenames[fcount] = (char *)malloc(strlen(basename(f[fcount]->name) + 9)); filenames[fcount] = (char *)malloc(strlen(basename(f[fcount]->name) + 30));
if (stat(f[fcount]->name, &s) != 0) { if (stat(f[fcount]->name, &s) != 0) {
sprintf(filenames[fcount], "</16>%s<!16>", basename(f[fcount]->name)); sprintf(filenames[fcount], "</16>%s (missing)<!16>", basename(f[fcount]->name));
if (f[fcount]->approved == 1) { if (f[fcount]->approved == 1) {
// unapprove missing file // unapprove missing file
doDisapprove(fcount); doDisapprove(fcount);
} }
} else if (f[fcount]->approved) { } else if (f[fcount]->approved) {
sprintf(filenames[fcount], "</24>%s<!24>", basename(f[fcount]->name)); sprintf(filenames[fcount], "</24>%s (approved)<!24>", basename(f[fcount]->name));
} else { } else {
sprintf(filenames[fcount], "</32>%s<!32>", basename(f[fcount]->name)); sprintf(filenames[fcount], "</32>%s (unapproved)<!32>", basename(f[fcount]->name));
} }
fcount++; fcount++;
} }
@ -175,6 +247,7 @@ int main(int argc, char **argv) {
setCDKScrollItems(scrollList, filenames, fcount, FALSE); setCDKScrollItems(scrollList, filenames, fcount, FALSE);
bindCDKObject (vSCROLL, scrollList, 'a', approveFile, NULL); bindCDKObject (vSCROLL, scrollList, 'a', approveFile, NULL);
bindCDKObject (vSCROLL, scrollList, 'd', deleteFile, NULL);
while(1) { while(1) {
selection = activateCDKScroll(scrollList, 0); selection = activateCDKScroll(scrollList, 0);
@ -191,7 +264,7 @@ int main(int argc, char **argv) {
free(f); free(f);
free(filenames); free(filenames);
destroyCDKLabel(instructions);
destroyCDKScroll(scrollList); destroyCDKScroll(scrollList);
destroyCDKScreen(cdkscreen); destroyCDKScreen(cdkscreen);
sqlite3_close(db); sqlite3_close(db);