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/bbs_list.c

237 lines
4.7 KiB
C
Raw Normal View History

2016-03-25 10:21:28 +00:00
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlite3.h>
#include <ctype.h>
#include "bbs.h"
extern struct bbs_config conf;
extern int mynode;
2016-08-06 04:36:36 +00:00
void add_bbs(struct user_record *user) {
2016-03-25 10:21:28 +00:00
char *create_sql = "CREATE TABLE IF NOT EXISTS bbslist ("
"id INTEGER PRIMARY KEY,"
"bbsname TEXT,"
"sysop TEXT,"
"telnet TEXT,"
"owner INTEGER);";
2016-03-25 10:21:28 +00:00
char *insert_sql = "INSERT INTO bbslist (bbsname, sysop, telnet, owner) VALUES(?,?, ?, ?)";
char bbsname[19];
char sysop[17];
char telnet[39];
char buffer[256];
char c;
char *err_msg = 0;
2016-03-25 10:21:28 +00:00
sqlite3 *db;
sqlite3_stmt *res;
int rc;
s_printf(get_string(28));
2016-08-06 04:36:36 +00:00
s_readstring(bbsname, 18);
s_printf(get_string(29));
2016-08-06 04:36:36 +00:00
s_readstring(sysop, 16);
s_printf(get_string(30));
2016-08-06 04:36:36 +00:00
s_readstring(telnet, 38);
s_printf(get_string(31));
s_printf(get_string(32));
s_printf(get_string(33), bbsname);
s_printf(get_string(34), sysop);
s_printf(get_string(35), telnet);
s_printf(get_string(36));
s_printf(get_string(37));
2016-08-06 04:36:36 +00:00
c = s_getc();
2016-03-25 10:21:28 +00:00
if (tolower(c) == 'y') {
sprintf(buffer, "%s/bbslist.sq3", conf.bbs_path);
2016-03-25 10:21:28 +00:00
rc = sqlite3_open(buffer, &db);
2017-03-22 01:51:04 +00:00
2016-03-25 10:21:28 +00:00
if (rc != SQLITE_OK) {
dolog("Cannot open database: %s", sqlite3_errmsg(db));
2016-03-25 10:21:28 +00:00
sqlite3_close(db);
2016-03-25 10:21:28 +00:00
exit(1);
}
2017-03-22 01:51:04 +00:00
sqlite3_busy_timeout(db, 5000);
2016-03-25 10:21:28 +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-03-25 10:21:28 +00:00
sqlite3_close(db);
2016-03-25 10:21:28 +00:00
return;
}
2016-03-25 10:21:28 +00:00
rc = sqlite3_prepare_v2(db, insert_sql, -1, &res, 0);
if (rc == SQLITE_OK) {
2016-03-25 10:21:28 +00:00
sqlite3_bind_text(res, 1, bbsname, -1, 0);
sqlite3_bind_text(res, 2, sysop, -1, 0);
sqlite3_bind_text(res, 3, telnet, -1, 0);
sqlite3_bind_int(res, 4, user->id);
2016-03-25 10:21:28 +00:00
} else {
dolog("Failed to execute statement: %s", sqlite3_errmsg(db));
2016-03-25 10:21:28 +00:00
}
2016-03-25 10:21:28 +00:00
rc = sqlite3_step(res);
2016-03-25 10:21:28 +00:00
if (rc != SQLITE_DONE) {
dolog("execution failed: %s", sqlite3_errmsg(db));
sqlite3_close(db);
2016-03-25 10:21:28 +00:00
exit(1);
}
sqlite3_finalize(res);
sqlite3_close(db);
s_printf(get_string(38));
2016-03-25 10:21:28 +00:00
} else {
s_printf(get_string(39));
2016-03-25 10:21:28 +00:00
}
}
2016-08-06 04:36:36 +00:00
void delete_bbs(struct user_record *user) {
2016-03-25 10:21:28 +00:00
char buffer[256];
sqlite3 *db;
2016-08-06 04:36:36 +00:00
sqlite3_stmt *res;
int rc;
char *sql = "SELECT bbsname FROM bbslist WHERE id=? and owner=?";
char *dsql = "DELETE FROM bbslist WHERE id=?";
int i;
char c;
s_printf(get_string(40));
2016-08-06 04:36:36 +00:00
s_readstring(buffer, 5);
i = atoi(buffer);
2016-08-06 04:36:36 +00:00
sprintf(buffer, "%s/bbslist.sq3", conf.bbs_path);
2016-08-06 04:36:36 +00:00
rc = sqlite3_open(buffer, &db);
2017-03-22 01:51:04 +00:00
2016-03-25 10:21:28 +00:00
if (rc != SQLITE_OK) {
return;
}
2017-03-22 01:51:04 +00:00
sqlite3_busy_timeout(db, 5000);
2016-08-06 04:36:36 +00:00
rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
2016-03-25 10:21:28 +00:00
if (rc == SQLITE_OK) {
sqlite3_bind_int(res, 1, i);
sqlite3_bind_int(res, 2, user->id);
} else {
sqlite3_close(db);
s_printf(get_string(41));
2016-03-25 10:21:28 +00:00
return;
}
if (sqlite3_step(res) == SQLITE_ROW) {
s_printf(get_string(42), sqlite3_column_text(res, 0));
2016-03-25 10:21:28 +00:00
sqlite3_finalize(res);
2016-08-06 04:36:36 +00:00
c = s_getc();
2016-03-25 10:21:28 +00:00
if (tolower(c) == 'y') {
rc = sqlite3_prepare_v2(db, dsql, -1, &res, 0);
if (rc == SQLITE_OK) {
sqlite3_bind_int(res, 1, i);
} else {
sqlite3_close(db);
s_printf(get_string(41));
2016-03-25 10:21:28 +00:00
return;
}
sqlite3_step(res);
s_printf(get_string(43));
2016-03-25 10:21:28 +00:00
sqlite3_finalize(res);
} else {
s_printf(get_string(39));
2016-03-25 10:21:28 +00:00
}
} else {
sqlite3_finalize(res);
s_printf(get_string(44));
2016-03-25 10:21:28 +00:00
}
sqlite3_close(db);
}
2016-08-06 04:36:36 +00:00
void list_bbses() {
2016-03-25 10:21:28 +00:00
char buffer[256];
sqlite3 *db;
2016-08-06 04:36:36 +00:00
sqlite3_stmt *res;
int rc;
char *sql = "SELECT id,bbsname,sysop,telnet FROM bbslist";
int i;
2016-08-06 04:36:36 +00:00
sprintf(buffer, "%s/bbslist.sq3", conf.bbs_path);
2016-08-06 04:36:36 +00:00
rc = sqlite3_open(buffer, &db);
2017-03-22 01:51:04 +00:00
2016-03-25 10:21:28 +00:00
if (rc != SQLITE_OK) {
dolog("Cannot open database: %s", sqlite3_errmsg(db));
2016-03-25 10:21:28 +00:00
sqlite3_close(db);
exit(1);
}
2017-03-22 01:51:04 +00:00
sqlite3_busy_timeout(db, 5000);
2016-03-25 10:21:28 +00:00
rc = sqlite3_prepare_v2(db, sql, -1, &res, 0);
if (rc != SQLITE_OK) {
sqlite3_close(db);
s_printf(get_string(41));
2016-03-25 10:21:28 +00:00
return;
}
i = 0;
s_printf(get_string(45));
2016-03-25 10:21:28 +00:00
while (sqlite3_step(res) == SQLITE_ROW) {
s_printf(get_string(46), sqlite3_column_int(res, 0), sqlite3_column_text(res, 1), sqlite3_column_text(res, 2), sqlite3_column_text(res, 3));
2016-03-25 10:21:28 +00:00
i++;
if (i == 20) {
s_printf(get_string(6));
2016-08-06 04:36:36 +00:00
s_getc();
2016-03-25 10:21:28 +00:00
i = 0;
}
2016-03-25 10:21:28 +00:00
}
s_printf(get_string(47));
2016-08-06 04:36:36 +00:00
sqlite3_finalize(res);
sqlite3_close(db);
s_printf(get_string(6));
2016-08-06 04:36:36 +00:00
s_getc();
2016-03-25 10:21:28 +00:00
}
2016-08-06 04:36:36 +00:00
void bbs_list(struct user_record *user) {
2016-03-25 10:21:28 +00:00
int doquit = 0;
char c;
2016-03-25 10:21:28 +00:00
while(!doquit) {
s_printf(get_string(48));
2016-08-06 04:36:36 +00:00
c = s_getc();
2016-03-25 10:21:28 +00:00
switch(tolower(c)) {
case 27:
{
c = s_getc();
if (c == 91) {
c = s_getc();
}
}
break;
2016-03-25 10:21:28 +00:00
case 'l':
2016-08-06 04:36:36 +00:00
list_bbses();
2016-03-25 10:21:28 +00:00
break;
case 'a':
2016-08-06 04:36:36 +00:00
add_bbs(user);
2016-03-25 10:21:28 +00:00
break;
case 'd':
2016-08-06 04:36:36 +00:00
delete_bbs(user);
2016-03-25 10:21:28 +00:00
break;
case 'q':
doquit = 1;
break;
}
}
}