This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
magicka/src/files.c

1273 lines
28 KiB
C
Raw Normal View History

2016-04-02 23:26:17 +00:00
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sqlite3.h>
#include <unistd.h>
#include <sys/stat.h>
#include <libgen.h>
#include <ctype.h>
#include <errno.h>
2016-08-07 06:37:55 +00:00
#include <termios.h>
#include <dirent.h>
2016-12-11 12:14:36 +00:00
#include <fcntl.h>
2017-09-28 07:56:17 +00:00
#include <time.h>
2016-04-02 23:26:17 +00:00
#include "Xmodem/zmodem.h"
#include "bbs.h"
#include "lua/lua.h"
#include "lua/lualib.h"
#include "lua/lauxlib.h"
2016-04-02 23:26:17 +00:00
extern struct bbs_config conf;
2016-08-06 04:36:36 +00:00
extern int gSocket;
extern int sshBBS;
extern int mynode;
2016-12-11 12:14:36 +00:00
extern int bbs_stdin;
extern int bbs_stdout;
extern int bbs_stderr;
2017-09-28 09:11:00 +00:00
extern time_t userlaston;
extern struct user_record *gUser;
2016-04-02 23:26:17 +00:00
struct file_entry {
char *filename;
char *description;
int size;
int dlcount;
2017-09-28 09:11:00 +00:00
time_t uploaddate;
2016-04-02 23:26:17 +00:00
};
char **tagged_files;
int tagged_count = 0;
2016-08-07 06:37:55 +00:00
int ttySetRaw(int fd, struct termios *prevTermios) {
struct termios t;
if (tcgetattr(fd, &t) == -1)
return -1;
if (prevTermios != NULL)
*prevTermios = t;
t.c_lflag &= ~(ICANON | ISIG | IEXTEN | ECHO);
t.c_iflag &= ~(BRKINT | ICRNL | IGNBRK | IGNCR | INLCR | INPCK | ISTRIP | IXON | PARMRK);
t.c_oflag &= ~OPOST;
t.c_cc[VMIN] = 1;
t.c_cc[VTIME] = 0;
if (tcsetattr(fd, TCSAFLUSH, &t) == -1)
return -1;
return 0;
}
2016-04-02 23:26:17 +00:00
int ZXmitStr(u_char *str, int len, ZModem *info) {
int i;
2016-04-02 23:26:17 +00:00
for (i=0;i<len;i++) {
if (str[i] == 255 && !sshBBS) {
2016-04-02 23:26:17 +00:00
if (write(info->ofd, &str[i], 1) == 0) {
return ZmErrSys;
}
}
if (write(info->ofd, &str[i], 1) == 0) {
return ZmErrSys;
}
}
2016-04-02 23:26:17 +00:00
return 0;
}
void ZIFlush(ZModem *info) {
}
void ZOFlush(ZModem *info) {
}
int ZAttn(ZModem *info) {
char *ptr ;
if( info->attn == NULL )
return 0 ;
for(ptr = info->attn; *ptr != '\0'; ++ptr) {
if( *ptr == ATTNBRK ) {
2016-04-02 23:26:17 +00:00
} else if( *ptr == ATTNPSE ) {
sleep(1);
} else {
write(info->ifd, ptr, 1) ;
}
}
return 0;
}
void ZFlowControl(int onoff, ZModem *info) {
}
void ZStatus(int type, int value, char *status) {
}
char *upload_path;
char upload_filename[1024];
FILE *ZOpenFile(char *name, u_long crc, ZModem *info) {
2016-04-02 23:26:17 +00:00
FILE *fptr;
struct stat s;
2016-04-02 23:26:17 +00:00
snprintf(upload_filename, 1023, "%s/%s", upload_path, basename(name));
if (stat(upload_filename, &s) == 0) {
return NULL;
}
2016-04-02 23:26:17 +00:00
fptr = fopen(upload_filename, "wb");
2016-04-02 23:26:17 +00:00
return fptr;
}
int ZWriteFile(u_char *buffer, int len, FILE *file, ZModem *info) {
return fwrite(buffer, 1, len, file) == len ? 0 : ZmErrSys;
}
int ZCloseFile(ZModem *info) {
fclose(info->file);
return 0;
}
void ZIdleStr(u_char *buffer, int len, ZModem *info) {
}
int doIO(ZModem *zm) {
fd_set readfds;
struct timeval timeout;
u_char buffer[2048];
u_char buffer2[1024];
int len;
int pos;
int done = 0;
int i;
int j;
2016-04-02 23:26:17 +00:00
while(!done) {
FD_ZERO(&readfds);
FD_SET(zm->ifd, &readfds) ;
timeout.tv_sec = zm->timeout ;
timeout.tv_usec = 0 ;
i = select(zm->ifd+1, &readfds,NULL,NULL, &timeout) ;
2016-04-02 23:26:17 +00:00
if( i==0 ) {
done = ZmodemTimeout(zm) ;
} else if (i > 0) {
len = read(zm->ifd, buffer, 2048);
if (len == 0) {
2016-08-06 04:36:36 +00:00
disconnect("Socket closed");
2016-04-02 23:26:17 +00:00
}
2016-04-02 23:26:17 +00:00
pos = 0;
for (j=0;j<len;j++) {
2016-08-07 06:49:52 +00:00
if (buffer[j] == 255 && !sshBBS) {
2016-04-02 23:26:17 +00:00
if (buffer[j+1] == 255) {
buffer2[pos] = 255;
pos++;
j++;
} else {
j++;
j++;
}
} else {
buffer2[pos] = buffer[j];
pos++;
}
}
if (pos > 0) {
done = ZmodemRcv(buffer2, pos, zm) ;
}
} else {
// SIG INT catch
if (errno != EINTR) {
dolog("SELECT ERROR %s", strerror(errno));
2016-04-02 23:26:17 +00:00
}
}
}
return done;
}
2016-12-06 07:06:28 +00:00
void upload_zmodem(struct user_record *user, char *upload_p) {
2016-04-02 23:26:17 +00:00
ZModem zm;
2016-12-06 07:06:28 +00:00
struct termios oldit;
struct termios oldot;
if (sshBBS) {
ttySetRaw(STDIN_FILENO, &oldit);
ttySetRaw(STDOUT_FILENO, &oldot);
}
2016-04-02 23:26:17 +00:00
2016-12-06 07:06:28 +00:00
upload_path = upload_p;
2016-04-02 23:26:17 +00:00
zm.attn = NULL;
zm.windowsize = 0;
zm.bufsize = 0;
if (!sshBBS) {
zm.ifd = gSocket;
zm.ofd = gSocket;
} else {
zm.ifd = STDIN_FILENO;
zm.ofd = STDOUT_FILENO;
}
2016-04-02 23:26:17 +00:00
zm.zrinitflags = 0;
zm.zsinitflags = 0;
2016-04-02 23:26:17 +00:00
zm.packetsize = 1024;
2016-08-06 04:55:18 +00:00
ZmodemRInit(&zm);
2016-04-02 23:26:17 +00:00
doIO(&zm);
2016-12-06 07:06:28 +00:00
if (sshBBS) {
tcsetattr(STDIN_FILENO, TCSANOW, &oldit);
tcsetattr(STDOUT_FILENO, TCSANOW, &oldot);
}
2016-04-02 23:26:17 +00:00
}
char *get_file_id_diz(char *filename) {
char *description;
char buffer[1024];
struct stat s;
int bpos;
int i;
FILE *fptr;
int len;
2016-12-08 04:08:04 +00:00
int ext;
int arch;
2016-12-11 12:14:36 +00:00
int stout;
int stin;
int sterr;
2016-12-08 04:08:04 +00:00
ext = 0;
arch = -1;
for (i=strlen(filename)-1;i>=0;i--) {
if (filename[i] == '.') {
ext = i + 1;
break;
}
}
if (ext == 0) {
return NULL;
}
for (i=0;i<conf.archiver_count;i++) {
if (strcasecmp(&filename[ext], conf.archivers[i]->extension) == 0) {
arch = i;
break;
}
}
if (arch == -1) {
return NULL;
}
snprintf(buffer, 1024, "%s/node%d", conf.bbs_path, mynode);
if (stat(buffer, &s) != 0) {
mkdir(buffer, 0755);
}
snprintf(buffer, 1024, "%s/node%d/temp", conf.bbs_path, mynode);
2016-12-07 01:56:33 +00:00
if (stat(buffer, &s) == 0) {
if (recursive_delete(buffer) != 0) {
return NULL;
}
}
mkdir(buffer, 0755);
bpos = 0;
2016-12-08 04:08:04 +00:00
for (i=0;i<strlen(conf.archivers[arch]->unpack);i++) {
if (conf.archivers[arch]->unpack[i] == '*') {
i++;
2016-12-08 04:08:04 +00:00
if (conf.archivers[arch]->unpack[i] == 'a') {
sprintf(&buffer[bpos], "%s", filename);
bpos = strlen(buffer);
2016-12-08 04:08:04 +00:00
} else if (conf.archivers[arch]->unpack[i] == 'd') {
sprintf(&buffer[bpos], "%s/node%d/temp/", conf.bbs_path, mynode);
bpos = strlen(buffer);
2016-12-08 04:08:04 +00:00
} else if (conf.archivers[arch]->unpack[i] == '*') {
buffer[bpos++] = '*';
buffer[bpos] = '\0';
}
} else {
2016-12-08 04:08:04 +00:00
buffer[bpos++] = conf.archivers[arch]->unpack[i];
buffer[bpos] = '\0';
}
}
2016-12-11 12:14:36 +00:00
if (sshBBS) {
stout = dup(STDOUT_FILENO);
stin = dup(STDIN_FILENO);
sterr = dup(STDERR_FILENO);
dup2(bbs_stdout, STDOUT_FILENO);
dup2(bbs_stderr, STDERR_FILENO);
dup2(bbs_stdin, STDIN_FILENO);
}
system(buffer);
2016-12-11 12:14:36 +00:00
if (sshBBS) {
dup2(stout, STDOUT_FILENO);
dup2(sterr, STDERR_FILENO);
dup2(stin, STDIN_FILENO);
close(stin);
close(stout);
close(sterr);
}
snprintf(buffer, 1024, "%s/node%d/temp/FILE_ID.DIZ", conf.bbs_path, mynode);
if (stat(buffer, &s) != 0) {
snprintf(buffer, 1024, "%s/node%d/temp/file_id.diz", conf.bbs_path, mynode);
if (stat(buffer, &s) != 0) {
snprintf(buffer, 1024, "%s/node%d/temp", conf.bbs_path, mynode);
recursive_delete(buffer);
return NULL;
}
}
description = (char *)malloc(s.st_size + 1);
fptr = fopen(buffer, "rb");
fread(description, 1, s.st_size, fptr);
description[s.st_size] = '\0';
fclose(fptr);
bpos = 0;
len = strlen(description);
for (i=0;i<len;i++) {
if (description[i] == '\r') {
continue;
} else {
description[bpos++] = description[i];
}
}
description[bpos] = '\0';
2016-12-07 01:56:33 +00:00
snprintf(buffer, 1024, "%s/node%d/temp", conf.bbs_path, mynode);
recursive_delete(buffer);
return description;
}
int do_download(struct user_record *user, char *file) {
struct termios oldit;
struct termios oldot;
char download_command[1024];
int i;
int argc;
int last_char_space;
char **arguments;
int bpos;
2016-12-10 08:38:55 +00:00
int len;
if (conf.protocols[user->defprotocol - 1]->internal_zmodem) {
if (sshBBS) {
ttySetRaw(STDIN_FILENO, &oldit);
ttySetRaw(STDOUT_FILENO, &oldot);
}
download_zmodem(user, file);
if (sshBBS) {
tcsetattr(STDIN_FILENO, TCSANOW, &oldit);
tcsetattr(STDOUT_FILENO, TCSANOW, &oldot);
}
return 1;
} else {
bpos = 0;
for (i=0;i<strlen(conf.protocols[user->defprotocol - 1]->download);i++) {
if (conf.protocols[user->defprotocol - 1]->download[i] == '*') {
i++;
if (conf.protocols[user->defprotocol - 1]->download[i] == '*') {
download_command[bpos++] = conf.protocols[user->defprotocol - 1]->download[i];
download_command[bpos] = '\0';
continue;
} else if (conf.protocols[user->defprotocol - 1]->download[i] == 'f') {
sprintf(&download_command[bpos], "%s", file);
bpos = strlen(download_command);
continue;
} else if (conf.protocols[user->defprotocol - 1]->download[i] == 's') {
if (!sshBBS) {
sprintf(&download_command[bpos], "%d", gSocket);
bpos = strlen(download_command);
} else {
s_printf(get_string(209), conf.protocols[user->defprotocol - 1]->name);
return 0;
}
}
} else {
download_command[bpos++] = conf.protocols[user->defprotocol - 1]->download[i];
download_command[bpos] = '\0';
}
}
2016-12-10 08:51:23 +00:00
argc = 1;
last_char_space = 0;
for (i=0;i<strlen(download_command);i++) {
if (download_command[i] == ' ') {
if (!last_char_space) {
argc++;
last_char_space = 1;
}
} else {
last_char_space = 0;
}
}
bpos = 1;
arguments = (char **)malloc(sizeof(char *) * (argc + 1));
2016-12-10 08:38:55 +00:00
len = strlen(download_command);
for (i=0;i<len;) {
if (download_command[i] != ' ') {
2016-12-10 08:38:55 +00:00
i++;
continue;
}
download_command[i] = '\0';
i++;
while (download_command[i] == ' ')
i++;
arguments[bpos++] = &download_command[i];
}
arguments[bpos] = NULL;
arguments[0] = download_command;
2017-04-23 02:14:14 +00:00
runexternal(user, download_command, conf.protocols[user->defprotocol - 1]->stdio, arguments, conf.bbs_path, 1, NULL);
free(arguments);
}
2017-04-23 02:28:01 +00:00
return 1;
}
int do_upload(struct user_record *user, char *final_path) {
char upload_path[1024];
char upload_command[1024];
char buffer3[256];
int bpos;
int i;
int argc;
int last_char_space;
char **arguments;
DIR *inb;
struct dirent *dent;
struct stat s;
2016-12-10 08:38:55 +00:00
int len;
if (conf.protocols[user->defprotocol - 1]->internal_zmodem) {
upload_zmodem(user, final_path);
return 1;
} else {
if (conf.protocols[user->defprotocol - 1]->upload_prompt) {
s_printf(get_string(210));
s_readstring(buffer3, 256);
s_printf("\r\n");
}
bpos = 0;
for (i=0;i<strlen(conf.protocols[user->defprotocol - 1]->upload);i++) {
if (conf.protocols[user->defprotocol - 1]->upload[i] == '*') {
i++;
if (conf.protocols[user->defprotocol - 1]->upload[i] == '*') {
upload_command[bpos++] = conf.protocols[user->defprotocol - 1]->upload[i];
upload_command[bpos] = '\0';
continue;
} else if (conf.protocols[user->defprotocol - 1]->upload[i] == 'f') {
if (conf.protocols[user->defprotocol - 1]->upload_prompt) {
sprintf(&upload_command[bpos], "%s", buffer3);
bpos = strlen(upload_command);
}
continue;
} else if (conf.protocols[user->defprotocol - 1]->upload[i] == 's') {
if (!sshBBS) {
sprintf(&upload_command[bpos], "%d", gSocket);
bpos = strlen(upload_command);
} else {
s_printf(get_string(209), conf.protocols[user->defprotocol - 1]->name);
return 0;
}
}
} else {
upload_command[bpos++] = conf.protocols[user->defprotocol - 1]->upload[i];
upload_command[bpos] = '\0';
}
}
argc = 1;
last_char_space = 0;
for (i=0;i<strlen(upload_command);i++) {
if (upload_command[i] == ' ') {
if (!last_char_space) {
argc++;
last_char_space = 1;
}
} else {
last_char_space = 0;
}
}
bpos = 1;
arguments = (char **)malloc(sizeof(char *) * (argc + 1));
2016-12-10 08:38:55 +00:00
len = strlen(upload_command);
for (i=0;i<len;) {
if (upload_command[i] != ' ') {
2016-12-10 08:38:55 +00:00
i++;
continue;
}
upload_command[i] = '\0';
i++;
while (upload_command[i] == ' ')
i++;
arguments[bpos++] = &upload_command[i];
}
arguments[bpos] = NULL;
arguments[0] = upload_command;
snprintf(upload_path, 1024, "%s/node%d/upload/", conf.bbs_path, mynode);
if (stat(upload_path, &s) == 0) {
recursive_delete(upload_path);
}
mkdir(upload_path, 0755);
2017-04-23 02:14:14 +00:00
runexternal(user, upload_command, conf.protocols[user->defprotocol - 1]->stdio, arguments, upload_path, 1, NULL);
free(arguments);
if (conf.protocols[user->defprotocol - 1]->upload_prompt) {
snprintf(upload_command, 1024, "%s%s", upload_path, buffer3);
2016-12-10 08:38:55 +00:00
if (stat(upload_command, &s) != 0) {
recursive_delete(upload_path);
return 0;
}
snprintf(upload_filename, 1024, "%s/%s", final_path, buffer3);
if (stat(upload_filename, &s) == 0) {
recursive_delete(upload_path);
s_printf(get_string(214));
return 0;
}
if (copy_file(upload_command, upload_filename) != 0) {
recursive_delete(upload_path);
return 0;
}
recursive_delete(upload_path);
return 1;
} else {
inb = opendir(upload_path);
if (!inb) {
return 0;
}
while ((dent = readdir(inb)) != NULL) {
if (dent->d_type == DT_REG) {
2017-04-23 02:28:01 +00:00
snprintf(upload_command, 1024, "%s%s", upload_path, dent->d_name);
snprintf(upload_filename, 1024, "%s/%s", final_path, dent->d_name);
if (stat(upload_filename, &s) == 0) {
recursive_delete(upload_path);
s_printf(get_string(214));
closedir(inb);
return 0;
}
if (copy_file(upload_command, upload_filename) != 0) {
recursive_delete(upload_path);
closedir(inb);
return 0;
}
closedir(inb);
recursive_delete(upload_path);
return 1;
}
}
closedir(inb);
return 0;
}
}
}
2016-08-06 04:36:36 +00:00
void upload(struct user_record *user) {
2016-04-02 23:26:17 +00:00
char buffer[331];
char buffer2[66];
char buffer3[256];
2016-04-02 23:26:17 +00:00
int i;
char *create_sql = "CREATE TABLE IF NOT EXISTS files ("
"Id INTEGER PRIMARY KEY,"
"filename TEXT,"
"description TEXT,"
"size INTEGER,"
"dlcount INTEGER,"
2017-09-28 07:56:17 +00:00
"uploaddate INTEGER,"
2016-04-02 23:26:17 +00:00
"approved INTEGER);";
2017-09-28 07:56:17 +00:00
char *sql = "INSERT INTO files (filename, description, size, dlcount, approved, uploaddate) VALUES(?, ?, ?, 0, 0, ?)";
2016-04-02 23:26:17 +00:00
sqlite3 *db;
sqlite3_stmt *res;
int rc;
struct stat s;
char *err_msg = NULL;
char *description;
2017-09-28 07:56:17 +00:00
time_t curtime;
if (!do_upload(user, conf.file_directories[user->cur_file_dir]->file_subs[user->cur_file_sub]->upload_path)) {
s_printf(get_string(211));
return;
}
description = NULL;
s_printf(get_string(198));
description = get_file_id_diz(upload_filename);
if (description == NULL) {
s_printf(get_string(199));
s_printf(get_string(200));
buffer[0] = '\0';
for (i=0;i<5;i++) {
s_printf("\r\n%d: ", i);
s_readstring(buffer2, 65);
if (strlen(buffer2) == 0) {
break;
}
strcat(buffer, buffer2);
strcat(buffer, "\n");
2016-04-02 23:26:17 +00:00
}
} else {
s_printf(get_string(201));
}
2016-04-02 23:26:17 +00:00
sprintf(buffer3, "%s/%s.sq3", conf.bbs_path, conf.file_directories[user->cur_file_dir]->file_subs[user->cur_file_sub]->database);
2016-04-02 23:26:17 +00:00
rc = sqlite3_open(buffer3, &db);
2016-04-02 23:26:17 +00:00
if (rc != SQLITE_OK) {
dolog("Cannot open database: %s", sqlite3_errmsg(db));
2016-04-02 23:26:17 +00:00
sqlite3_close(db);
exit(1);
}
2017-03-22 01:51:04 +00:00
sqlite3_busy_timeout(db, 5000);
2016-04-02 23:26:17 +00:00
rc = sqlite3_exec(db, create_sql, 0, 0, &err_msg);
if (rc != SQLITE_OK ) {
dolog("SQL error: %s", err_msg);
sqlite3_free(err_msg);
2016-04-02 23:26:17 +00:00
sqlite3_close(db);
if (description != NULL) {
free(description);
}
2016-04-02 23:26:17 +00:00
return;
}
2016-04-02 23:26:17 +00:00
rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
if (rc == SQLITE_OK) {
2016-04-02 23:26:17 +00:00
stat(upload_filename, &s);
2016-04-02 23:26:17 +00:00
sqlite3_bind_text(res, 1, upload_filename, -1, 0);
if (description == NULL) {
sqlite3_bind_text(res, 2, buffer, -1, 0);
} else {
sqlite3_bind_text(res, 2, description, -1, 0);
}
sqlite3_bind_int(res, 3, s.st_size);
2017-09-28 07:56:17 +00:00
curtime = time(NULL);
sqlite3_bind_int(res, 4, curtime);
2016-04-02 23:26:17 +00:00
} else {
dolog("Failed to execute statement: %s", sqlite3_errmsg(db));
2016-04-02 23:26:17 +00:00
sqlite3_finalize(res);
sqlite3_close(db);
if (description != NULL) {
free(description);
}
return;
2016-04-02 23:26:17 +00:00
}
2016-04-02 23:26:17 +00:00
rc = sqlite3_step(res);
2016-04-02 23:26:17 +00:00
if (rc != SQLITE_DONE) {
dolog("execution failed: %s", sqlite3_errmsg(db));
2016-04-02 23:26:17 +00:00
sqlite3_finalize(res);
sqlite3_close(db);
if (description != NULL) {
free(description);
}
2016-04-02 23:26:17 +00:00
return;
}
sqlite3_finalize(res);
sqlite3_close(db);
if (description != NULL) {
free(description);
}
s_printf(get_string(202));
s_printf(get_string(6));
s_getc();
2016-04-02 23:26:17 +00:00
}
2016-08-06 04:36:36 +00:00
void download_zmodem(struct user_record *user, char *filename) {
2016-04-02 23:26:17 +00:00
ZModem zm;
int done ;
dolog("Attempting to upload %s", filename);
2016-04-02 23:26:17 +00:00
zm.attn = NULL;
zm.windowsize = 0;
zm.bufsize = 0;
if (!sshBBS) {
zm.ifd = gSocket;
zm.ofd = gSocket;
} else {
zm.ifd = STDIN_FILENO;
zm.ofd = STDOUT_FILENO;
}
2016-04-02 23:26:17 +00:00
zm.zrinitflags = 0;
zm.zsinitflags = 0;
2016-04-02 23:26:17 +00:00
zm.packetsize = 1024;
2016-04-02 23:26:17 +00:00
ZmodemTInit(&zm) ;
done = doIO(&zm);
if ( done != ZmDone ) {
return;
}
2016-04-02 23:26:17 +00:00
done = ZmodemTFile(filename, basename(filename), ZCBIN,0,0,0,0,0, &zm) ;
2016-04-02 23:26:17 +00:00
switch( done ) {
case 0:
2016-04-02 23:26:17 +00:00
break ;
case ZmErrCantOpen:
dolog("cannot open file \"%s\": %s\n", filename, strerror(errno)) ;
2016-04-02 23:26:17 +00:00
return;
case ZmFileTooLong:
dolog("filename \"%s\" too long, skipping...\n", filename) ;
2016-04-02 23:26:17 +00:00
return;
case ZmDone:
return;
default:
return;
}
2016-04-02 23:26:17 +00:00
if (!done) {
done = doIO(&zm);
}
2016-04-02 23:26:17 +00:00
if ( done != ZmDone ) {
return;
}
2016-04-02 23:26:17 +00:00
done = ZmodemTFinish(&zm);
2016-04-02 23:26:17 +00:00
if (!done) {
done = doIO(&zm);
}
}
2016-08-06 04:36:36 +00:00
void download(struct user_record *user) {
2016-04-02 23:26:17 +00:00
int i;
char *ssql = "select dlcount from files where filename like ?";
char *usql = "update files set dlcount=? where filename like ?";
char buffer[256];
int dloads;
sqlite3 *db;
sqlite3_stmt *res;
int rc;
2016-04-02 23:26:17 +00:00
for (i=0;i<tagged_count;i++) {
2016-08-07 06:37:55 +00:00
do_download(user, tagged_files[i]);
2016-04-02 23:26:17 +00:00
sprintf(buffer, "%s/%s.sq3", conf.bbs_path, conf.file_directories[user->cur_file_dir]->file_subs[user->cur_file_sub]->database);
2016-04-02 23:26:17 +00:00
rc = sqlite3_open(buffer, &db);
2016-04-02 23:26:17 +00:00
if (rc != SQLITE_OK) {
dolog("Cannot open database: %s", sqlite3_errmsg(db));
2016-04-02 23:26:17 +00:00
sqlite3_close(db);
exit(1);
}
2017-03-22 01:51:04 +00:00
sqlite3_busy_timeout(db, 5000);
2016-04-02 23:26:17 +00:00
rc = sqlite3_prepare_v2(db, ssql, -1, &res, 0);
if (rc == SQLITE_OK) {
2016-04-02 23:26:17 +00:00
sqlite3_bind_text(res, 1, tagged_files[i], -1, 0);
} else {
dolog("Failed to execute statement: %s", sqlite3_errmsg(db));
2016-04-02 23:26:17 +00:00
}
rc = sqlite3_step(res);
2016-04-02 23:26:17 +00:00
if (rc != SQLITE_ROW) {
dolog("Downloaded a file not in database!!!!!");
2016-04-02 23:26:17 +00:00
sqlite3_finalize(res);
sqlite3_close(db);
exit(1);
}
2016-04-02 23:26:17 +00:00
dloads = sqlite3_column_int(res, 0);
dloads++;
sqlite3_finalize(res);
2016-04-02 23:26:17 +00:00
rc = sqlite3_prepare_v2(db, usql, -1, &res, 0);
if (rc == SQLITE_OK) {
2016-04-02 23:26:17 +00:00
sqlite3_bind_int(res, 1, dloads);
sqlite3_bind_text(res, 2, tagged_files[i], -1, 0);
} else {
dolog("Failed to execute statement: %s", sqlite3_errmsg(db));
2016-04-02 23:26:17 +00:00
}
rc = sqlite3_step(res);
2016-04-02 23:26:17 +00:00
sqlite3_finalize(res);
sqlite3_close(db);
}
2016-08-08 07:21:04 +00:00
2016-04-02 23:26:17 +00:00
for (i=0;i<tagged_count;i++) {
free(tagged_files[i]);
}
free(tagged_files);
tagged_count = 0;
}
2016-08-06 04:36:36 +00:00
void list_files(struct user_record *user) {
2017-09-28 09:11:00 +00:00
char *sql = "select filename, description, size, dlcount, uploaddate from files where approved=1";
char buffer[PATH_MAX];
2016-04-02 23:26:17 +00:00
sqlite3 *db;
sqlite3_stmt *res;
int rc;
int files_c;
int file_size;
char file_unit;
int lines = 0;
int i;
int j;
int z;
int k;
int match;
2016-04-02 23:26:17 +00:00
struct file_entry **files_e;
2017-09-28 09:11:00 +00:00
snprintf(buffer, PATH_MAX, "%s/%s.sq3", conf.bbs_path, conf.file_directories[user->cur_file_dir]->file_subs[user->cur_file_sub]->database);
2016-04-02 23:26:17 +00:00
rc = sqlite3_open(buffer, &db);
if (rc != SQLITE_OK) {
dolog("Cannot open database: %s", sqlite3_errmsg(db));
2016-04-02 23:26:17 +00:00
sqlite3_close(db);
2016-04-02 23:26:17 +00:00
exit(1);
}
2017-03-22 01:51:04 +00:00
sqlite3_busy_timeout(db, 5000);
2016-04-02 23:26:17 +00:00
rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
if (rc != SQLITE_OK) {
2016-04-02 23:26:17 +00:00
sqlite3_finalize(res);
sqlite3_close(db);
s_printf(get_string(68));
return;
2016-04-02 23:26:17 +00:00
}
2016-04-02 23:26:17 +00:00
files_c = 0;
while (sqlite3_step(res) == SQLITE_ROW) {
if (files_c == 0) {
files_e = (struct file_entry **)malloc(sizeof(struct file_entry *));
} else {
files_e = (struct file_entry **)realloc(files_e, sizeof(struct file_entry *) * (files_c + 1));
}
files_e[files_c] = (struct file_entry *)malloc(sizeof(struct file_entry));
files_e[files_c]->filename = strdup((char *)sqlite3_column_text(res, 0));
files_e[files_c]->description = strdup((char *)sqlite3_column_text(res, 1));
files_e[files_c]->size = sqlite3_column_int(res, 2);
files_e[files_c]->dlcount = sqlite3_column_int(res, 3);
2017-09-28 09:11:00 +00:00
files_e[files_c]->uploaddate = sqlite3_column_int(res, 4);
2016-04-02 23:26:17 +00:00
files_c++;
}
sqlite3_finalize(res);
sqlite3_close(db);
2016-04-02 23:26:17 +00:00
if (files_c == 0) {
s_printf(get_string(68));
return;
2016-04-02 23:26:17 +00:00
}
2016-08-06 04:36:36 +00:00
s_printf("\r\n");
2016-04-02 23:26:17 +00:00
for (i=0;i<files_c;i++) {
file_size = files_e[i]->size;
if (file_size > 1024 * 1024 * 1024) {
file_size = file_size / 1024 / 1024 / 1024;
file_unit = 'G';
} else if (file_size > 1024 * 1024) {
file_size = file_size / 1024 / 1024;
file_unit = 'M';
} else if (file_size > 1024) {
file_size = file_size / 1024;
file_unit = 'K';
} else {
file_unit = 'b';
}
2017-09-28 09:11:00 +00:00
if (files_e[i]->uploaddate > userlaston) {
s_printf(get_string(231), i, files_e[i]->dlcount, file_size, file_unit, basename(files_e[i]->filename));
} else {
s_printf(get_string(69), i, files_e[i]->dlcount, file_size, file_unit, basename(files_e[i]->filename));
}
2016-04-03 05:26:25 +00:00
lines+=3;
2016-04-02 23:26:17 +00:00
for (j=0;j<strlen(files_e[i]->description);j++) {
if (files_e[i]->description[j] == '\n') {
2016-08-06 04:36:36 +00:00
s_printf("\r\n");
2016-04-02 23:26:17 +00:00
lines++;
2016-04-03 05:26:25 +00:00
if (lines >= 18) {
lines = 0;
2016-04-02 23:26:17 +00:00
while (1) {
s_printf(get_string(70));
2016-08-06 04:36:36 +00:00
s_readstring(buffer, 5);
2016-04-02 23:26:17 +00:00
if (strlen(buffer) == 0) {
2016-08-06 04:36:36 +00:00
s_printf("\r\n");
2016-04-02 23:26:17 +00:00
break;
} else if (tolower(buffer[0]) == 'q') {
for (z=0;z<files_c;z++) {
free(files_e[z]->filename);
free(files_e[z]->description);
free(files_e[z]);
}
free(files_e);
2016-08-06 04:36:36 +00:00
s_printf("\r\n");
2016-04-02 23:26:17 +00:00
return;
} else {
z = atoi(buffer);
if (z >= 0 && z < files_c) {
if (conf.file_directories[user->cur_file_dir]->file_subs[user->cur_file_sub]->download_sec_level <= user->sec_level) {
match = 0;
for (k=0;k<tagged_count;k++) {
if (strcmp(tagged_files[k], files_e[z]->filename) == 0) {
match = 1;
break;
}
}
if (match == 0) {
if (tagged_count == 0) {
tagged_files = (char **)malloc(sizeof(char *));
} else {
tagged_files = (char **)realloc(tagged_files, sizeof(char *) * (tagged_count + 1));
}
tagged_files[tagged_count] = strdup(files_e[z]->filename);
tagged_count++;
s_printf(get_string(71), basename(files_e[z]->filename));
2016-04-02 23:26:17 +00:00
} else {
s_printf(get_string(72));
2016-04-02 23:26:17 +00:00
}
} else {
s_printf(get_string(73));
2016-04-02 23:26:17 +00:00
}
}
}
}
2017-09-28 07:56:17 +00:00
}
if (strlen(&(files_e[i]->description[j])) > 1) {
s_printf(get_string(74));
2016-04-02 23:26:17 +00:00
}
} else {
2016-08-06 04:36:36 +00:00
s_putchar(files_e[i]->description[j]);
2016-04-02 23:26:17 +00:00
}
}
2016-08-19 10:07:59 +00:00
if (lines >= 18) {
lines = 0;
while (1) {
s_printf(get_string(70));
s_readstring(buffer, 5);
if (strlen(buffer) == 0) {
s_printf("\r\n");
break;
} else if (tolower(buffer[0]) == 'q') {
for (z=0;z<files_c;z++) {
free(files_e[z]->filename);
free(files_e[z]->description);
free(files_e[z]);
}
free(files_e);
s_printf("\r\n");
return;
} else {
z = atoi(buffer);
if (z >= 0 && z < files_c) {
if (conf.file_directories[user->cur_file_dir]->file_subs[user->cur_file_sub]->download_sec_level <= user->sec_level) {
match = 0;
for (k=0;k<tagged_count;k++) {
if (strcmp(tagged_files[k], files_e[z]->filename) == 0) {
match = 1;
break;
}
}
if (match == 0) {
if (tagged_count == 0) {
tagged_files = (char **)malloc(sizeof(char *));
} else {
tagged_files = (char **)realloc(tagged_files, sizeof(char *) * (tagged_count + 1));
}
tagged_files[tagged_count] = strdup(files_e[z]->filename);
tagged_count++;
s_printf(get_string(71), basename(files_e[z]->filename));
} else {
s_printf(get_string(72));
}
} else {
s_printf(get_string(73));
}
}
}
}
}
2016-04-02 23:26:17 +00:00
}
while (1) {
s_printf(get_string(75));
2016-08-06 04:36:36 +00:00
s_readstring(buffer, 5);
2016-04-02 23:26:17 +00:00
if (strlen(buffer) == 0) {
for (z=0;z<files_c;z++) {
free(files_e[z]->filename);
free(files_e[z]->description);
free(files_e[z]);
}
free(files_e);
2016-08-06 04:36:36 +00:00
s_printf("\r\n");
2016-04-02 23:26:17 +00:00
return;
} else {
z = atoi(buffer);
if (z >= 0 && z < files_c) {
if (conf.file_directories[user->cur_file_dir]->file_subs[user->cur_file_sub]->download_sec_level <= user->sec_level) {
match = 0;
for (k=0;k<tagged_count;k++) {
if (strcmp(tagged_files[k], files_e[z]->filename) == 0) {
match = 1;
break;
}
}
if (match == 0) {
if (tagged_count == 0) {
tagged_files = (char **)malloc(sizeof(char *));
} else {
tagged_files = (char **)realloc(tagged_files, sizeof(char *) * (tagged_count + 1));
}
tagged_files[tagged_count] = strdup(files_e[z]->filename);
tagged_count++;
s_printf(get_string(71), basename(files_e[z]->filename));
2016-04-02 23:26:17 +00:00
} else {
s_printf(get_string(72));
2016-04-02 23:26:17 +00:00
}
} else {
s_printf(get_string(73));
2016-04-02 23:26:17 +00:00
}
}
}
2016-04-02 23:26:17 +00:00
}
}
2017-04-12 12:34:08 +00:00
void choose_subdir(struct user_record *user) {
2016-04-02 23:26:17 +00:00
int i;
2017-04-12 12:34:08 +00:00
char c;
2016-04-02 23:26:17 +00:00
int j;
2017-04-12 12:34:08 +00:00
char prompt[6];
s_printf(get_string(81));
for (i=0;i<conf.file_directories[user->cur_file_dir]->file_sub_count;i++) {
s_printf(" %d. %s\r\n", i, conf.file_directories[user->cur_file_dir]->file_subs[i]->name);
if (i != 0 && i % 20 == 0) {
s_printf(get_string(6));
c = s_getc();
}
}
s_printf(get_string(82));
s_readstring(prompt, 5);
if (tolower(prompt[0]) != 'q') {
j = atoi(prompt);
if (j < 0 || j >= conf.file_directories[user->cur_file_dir]->file_sub_count) {
s_printf(get_string(83));
} else {
2017-04-12 12:34:08 +00:00
s_printf("\r\n");
user->cur_file_sub = j;
}
}
2017-04-12 12:34:08 +00:00
}
2017-04-12 12:34:08 +00:00
void choose_directory(struct user_record *user) {
int i;
char c;
int j;
char prompt[6];
s_printf(get_string(77));
for (i=0;i<conf.file_directory_count;i++) {
if (conf.file_directories[i]->sec_level <= user->sec_level) {
s_printf(get_string(78), i, conf.file_directories[i]->name);
}
if (i != 0 && i % 20 == 0) {
s_printf(get_string(6));
2016-08-06 04:36:36 +00:00
c = s_getc();
2017-04-12 12:34:08 +00:00
}
}
s_printf(get_string(79));
s_readstring(prompt, 5);
if (tolower(prompt[0]) != 'q') {
j = atoi(prompt);
if (j < 0 || j >= conf.file_directory_count || conf.file_directories[j]->sec_level > user->sec_level) {
s_printf(get_string(80));
} else {
2017-04-12 12:34:08 +00:00
s_printf("\r\n");
user->cur_file_dir = j;
user->cur_file_sub = 0;
}
2017-04-12 12:34:08 +00:00
}
}
2016-08-08 07:21:04 +00:00
2017-04-12 12:34:08 +00:00
void clear_tagged_files() {
int i;
// Clear tagged files
if (tagged_count > 0) {
for (i=0;i<tagged_count;i++) {
free(tagged_files[i]);
}
free(tagged_files);
tagged_count = 0;
}
}
void next_file_dir(struct user_record *user) {
int i;
for (i=user->cur_file_dir;i<conf.file_directory_count;i++) {
if (i + 1 == conf.file_directory_count) {
i = -1;
}
if (conf.file_directories[i+1]->sec_level <= user->sec_level) {
user->cur_file_dir = i + 1;
user->cur_file_sub = 0;
break;
}
}
}
void prev_file_dir(struct user_record *user) {
int i;
for (i=user->cur_file_dir;i>=0;i--) {
if (i - 1 == -1) {
i = conf.file_directory_count;
}
if (conf.file_directories[i-1]->sec_level <= user->sec_level) {
user->cur_file_dir = i - 1;
user->cur_file_sub = 0;
break;
2016-04-02 23:26:17 +00:00
}
}
2017-04-12 12:34:08 +00:00
}
void next_file_sub(struct user_record *user) {
int i;
i=user->cur_file_sub;
if (i + 1 == conf.file_directories[user->cur_file_dir]->file_sub_count) {
i = -1;
}
user->cur_file_sub = i + 1;
}
void prev_file_sub(struct user_record *user) {
int i;
i=user->cur_file_sub;
if (i - 1 == -1) {
i = conf.file_directories[user->cur_file_dir]->file_sub_count;
}
2017-04-12 12:34:08 +00:00
user->cur_file_sub = i - 1;
2016-04-02 23:26:17 +00:00
}
2017-04-12 12:34:08 +00:00
2017-09-28 09:11:00 +00:00
void file_scan() {
char c;
int i;
int j;
char buffer[PATH_MAX];
char sql[] = "SELECT COUNT(*) FROM files WHERE uploaddate > ? AND approved=1";
2017-09-28 09:11:00 +00:00
int rc;
sqlite3 *db;
sqlite3_stmt *res;
int new_files;
int lines = 0;
s_printf(get_string(232));
c = s_getc();
if (tolower(c) == 'y') {
for (i=0;i<conf.file_directory_count;i++) {
if (conf.file_directories[i]->sec_level > gUser->sec_level) {
continue;
}
s_printf(get_string(140), i, conf.file_directories[i]->name);
lines += 2;
if (lines == 22) {
s_printf(get_string(6));
s_getc();
lines = 0;
}
for (j=0;j<conf.file_directories[i]->file_sub_count;j++) {
if (conf.file_directories[i]->file_subs[j]->download_sec_level > gUser->sec_level) {
continue;
}
snprintf(buffer, PATH_MAX, "%s/%s.sq3", conf.bbs_path, conf.file_directories[i]->file_subs[j]->database);
rc = sqlite3_open(buffer, &db);
if (rc != SQLITE_OK) {
dolog("Cannot open database: %s", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
sqlite3_busy_timeout(db, 5000);
rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
if (rc != SQLITE_OK) {
sqlite3_finalize(res);
sqlite3_close(db);
continue;
}
sqlite3_bind_int(res, 1, userlaston);
if (sqlite3_step(res) != SQLITE_ERROR) {
new_files = sqlite3_column_int(res, 0);
2017-09-28 09:15:33 +00:00
if (new_files > 0) {
s_printf(get_string(141), j, conf.file_directories[i]->file_subs[j]->name, new_files);
lines++;
}
2017-09-28 09:11:00 +00:00
}
sqlite3_finalize(res);
sqlite3_close(db);
if (lines == 22) {
s_printf(get_string(6));
s_getc();
lines = 0;
}
}
}
2017-09-28 09:15:33 +00:00
s_printf(get_string(6));
s_getc();
2017-09-28 09:11:00 +00:00
}
}