From bc588e8636fb696d453d8caee3ae14fd7c5a7cff Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 23 Apr 2017 10:51:40 +1000 Subject: [PATCH 01/14] Experimenting with utf8 --- Makefile.osx | 2 +- Makefile.osx.WWW | 2 +- bbs.c | 83 +++++++++++++++++++++++++++++++++++++----- bbs.h | 1 + config_default/bbs.ini | 1 + main.c | 7 ++++ 6 files changed, 84 insertions(+), 12 deletions(-) diff --git a/Makefile.osx b/Makefile.osx index 07df784..260e560 100644 --- a/Makefile.osx +++ b/Makefile.osx @@ -35,7 +35,7 @@ $(CDK): $(CC) -c -o $@ $< $(CFLAGS) magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${JSMN} - $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/opt/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh + $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/opt/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh -liconv magimail: $(JAMLIB) cd utils/magimail && $(MAKE) linux diff --git a/Makefile.osx.WWW b/Makefile.osx.WWW index 492520e..1c7cd57 100644 --- a/Makefile.osx.WWW +++ b/Makefile.osx.WWW @@ -39,7 +39,7 @@ OBJ = deps/aha/aha.o inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o do $(CC) -c -o $@ $< $(CFLAGS) magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${B64} ${JSMN} - $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/opt/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh $(MICROHTTPD) + $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/opt/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh -liconv $(MICROHTTPD) magimail: $(JAMLIB) cd utils/magimail && $(MAKE) linux diff --git a/bbs.c b/bbs.c index 77dda31..2912c4c 100644 --- a/bbs.c +++ b/bbs.c @@ -14,6 +14,7 @@ #include #include #include +#include #include "bbs.h" #include "lua/lua.h" #include "lua/lualib.h" @@ -200,27 +201,89 @@ void timer_handler(int signum) { void s_printf(char *fmt, ...) { char buffer[512]; - va_list ap; - va_start(ap, fmt); - vsnprintf(buffer, 512, fmt, ap); - va_end(ap); + va_list ap; + va_start(ap, fmt); + vsnprintf(buffer, 512, fmt, ap); + va_end(ap); s_putstring(buffer); } +int should_convert_utf8() { + return conf.codepage; +} + void s_putchar(char c) { - if (sshBBS) { - putchar(c); + iconv_t ic; + char *inbuf; + char *outbuf; + char *ptr1; + char *ptr2; + size_t inc; + size_t ouc; + size_t sz; + + if (!should_convert_utf8()) { + if (sshBBS) { + putchar(c); + } else { + write(gSocket, &c, 1); + } } else { - write(gSocket, &c, 1); + ic = iconv_open("UTF-8", "CP437"); + inbuf = (char *)malloc(3); + outbuf = (char *)malloc(3); + memset(outbuf, 0, 3); + sprintf(inbuf, "%c", c); + inc = 1; + ouc = 3; + ptr1 = outbuf; + ptr2 = inbuf; + sz = iconv(ic, &inbuf, &inc, &outbuf, &ouc); + if (sshBBS) { + fprintf(stdout, "%s", ptr1); + } else { + write(gSocket, ptr1, strlen(ptr1)); + } + iconv_close(ic); + free(ptr1); + free(ptr2); } } void s_putstring(char *c) { - if (sshBBS) { - printf("%s", c); + iconv_t ic; + char *inbuf; + char *outbuf; + size_t inc; + size_t ouc; + size_t sz; + char *ptr1; + char *ptr2; + if (!should_convert_utf8()) { + if (sshBBS) { + puts(c); + } else { + write(gSocket, c, strlen(c)); + } } else { - write(gSocket, c, strlen(c)); + ic = iconv_open("UTF-8", "CP437"); + inc = strlen(c); + inbuf = strdup(c); + outbuf = (char *)malloc(inc * 2); + memset(outbuf, 0, inc *2); + ptr1 = outbuf; + ptr2 = inbuf; + ouc = inc * 2; + sz = iconv(ic, &inbuf, &inc, &outbuf, &ouc); + if (sshBBS) { + fprintf(stdout, "%s", ptr1); + } else { + write(gSocket, ptr1, strlen(ptr1)); + } + iconv_close(ic); + free(ptr1); + free(ptr2); } } diff --git a/bbs.h b/bbs.h index b9fd7fa..01b699e 100644 --- a/bbs.h +++ b/bbs.h @@ -113,6 +113,7 @@ struct ip_address_guard { }; struct bbs_config { + int codepage; char *bbs_name; char *bwave_name; char *sysop_name; diff --git a/config_default/bbs.ini b/config_default/bbs.ini index f11deb4..0117907 100644 --- a/config_default/bbs.ini +++ b/config_default/bbs.ini @@ -1,4 +1,5 @@ [main] +Codepage = cp437 Telnet Port = 2023 BBS Name = Magicka BBS Sysop Name = sysop diff --git a/main.c b/main.c index a3cfbd2..0fd7434 100644 --- a/main.c +++ b/main.c @@ -479,6 +479,12 @@ static int handler(void* user, const char* section, const char* name, conf->ipguard_tries = atoi(value); } else if (strcasecmp(name, "root menu") == 0) { conf->root_menu = strdup(value); + } else if (strcasecmp(name, "codepage") == 0) { + if (strcasecmp(value, "cp437") == 0) { + conf->codepage = 0; + } else if (strcasecmp(value, "utf8") == 0) { + conf->codepage = 1; + } } } else if (strcasecmp(section, "paths") == 0){ if (strcasecmp(name, "ansi path") == 0) { @@ -1154,6 +1160,7 @@ int main(int argc, char **argv) { conf.ipguard_tries = 4; conf.ipguard_timeout = 120; conf.protocol_count = 0; + conf.codepage = 0; // Load BBS data if (ini_parse(argv[1], handler, &conf) <0) { From 6027021d4f055dcc1fd66cec042192e14813f619 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 23 Apr 2017 12:14:14 +1000 Subject: [PATCH 02/14] more work on utf-8 --- Makefile.freebsd | 2 +- Makefile.freebsd.WWW | 2 +- Makefile.linux | 2 +- Makefile.linux.WWW | 2 +- Makefile.netbsd | 2 +- Makefile.netbsd.WWW | 2 +- bbs.h | 6 +++-- config_default/bbs.ini | 3 ++- doors.c | 59 +++++++++++++++++++++++++++++++++++++----- files.c | 4 +-- lua_glue.c | 4 +-- mail_menu.c | 2 +- main.c | 10 ++++++- menus.c | 3 ++- 14 files changed, 81 insertions(+), 22 deletions(-) diff --git a/Makefile.freebsd b/Makefile.freebsd index 8379b14..6c2d050 100644 --- a/Makefile.freebsd +++ b/Makefile.freebsd @@ -34,7 +34,7 @@ OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list. $(CC) -c -o $@ $< $(CFLAGS) magicka: $(OBJ) ${LUA} ${JAMLIB} ${ZMODEM} ${JSMN} - $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -lssl -lcrypto -lssh + $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -lssl -lcrypto -lssh -liconv magimail: $(JAMLIB) cd utils/magimail && $(MAKE) freebsd diff --git a/Makefile.freebsd.WWW b/Makefile.freebsd.WWW index 3b76f56..b600e2d 100644 --- a/Makefile.freebsd.WWW +++ b/Makefile.freebsd.WWW @@ -39,7 +39,7 @@ OBJ = deps/aha/aha.o inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o do $(CC) -c -o $@ $< $(CFLAGS) magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${B64} ${JSMN} - $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -lssl -lcrypto -lssh $(MICROHTTPD) + $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -lssl -lcrypto -lssh -liconv $(MICROHTTPD) magimail: $(JAMLIB) cd utils/magimail && $(MAKE) freebsd diff --git a/Makefile.linux b/Makefile.linux index 8c4b2ad..71ff5c1 100644 --- a/Makefile.linux +++ b/Makefile.linux @@ -35,7 +35,7 @@ OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o $(CC) -c -o $@ $< $(CFLAGS) magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${JSMN} - $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh + $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh -liconv magimail: $(JAMLIB) cd utils/magimail && $(MAKE) linux diff --git a/Makefile.linux.WWW b/Makefile.linux.WWW index 8e53977..d868726 100644 --- a/Makefile.linux.WWW +++ b/Makefile.linux.WWW @@ -39,7 +39,7 @@ OBJ = deps/aha/aha.o inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o do $(CC) -c -o $@ $< $(CFLAGS) magicka: $(OBJ) ${LUA} ${JAMLIB} ${ZMODEM} ${B64} ${JSMN} - $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh $(MICROHTTPD) + $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh -liconv $(MICROHTTPD) magimail: $(JAMLIB) cd utils/magimail && $(MAKE) linux diff --git a/Makefile.netbsd b/Makefile.netbsd index 8ebda68..2e56955 100644 --- a/Makefile.netbsd +++ b/Makefile.netbsd @@ -34,7 +34,7 @@ OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o $(CC) -c -o $@ $< $(CFLAGS) magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${JSMN} - $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/pkg/lib -lsqlite3 -lutil -lm -lssl -lcrypto -lssh + $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/pkg/lib -lsqlite3 -lutil -lm -lssl -lcrypto -lssh -liconv magiedit: $(ODOORS) cd utils/magiedit && $(MAKE) diff --git a/Makefile.netbsd.WWW b/Makefile.netbsd.WWW index abf326e..50ddd12 100644 --- a/Makefile.netbsd.WWW +++ b/Makefile.netbsd.WWW @@ -39,7 +39,7 @@ OBJ = deps/aha/aha.o inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o do $(CC) -c -o $@ $< $(CFLAGS) magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${B64} ${JSMN} - $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/pkg/lib -lsqlite3 -lutil -lm -lssl -lcrypto -lssh $(MICROHTTPD) + $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/pkg/lib -lsqlite3 -lutil -lm -lssl -lcrypto -lssh -liconv $(MICROHTTPD) magiedit: $(ODOORS) cd utils/magiedit && $(MAKE) diff --git a/bbs.h b/bbs.h index 01b699e..58ab727 100644 --- a/bbs.h +++ b/bbs.h @@ -45,6 +45,7 @@ struct door_config { char *name; char *command; int stdio; + char *codepage; }; struct mail_area { @@ -144,6 +145,7 @@ struct bbs_config { char *menu_path; char *external_editor_cmd; int external_editor_stdio; + char *external_editor_codepage; int fork; int nodes; @@ -273,8 +275,8 @@ extern void next_mail_area(struct user_record *user); extern void prev_mail_area(struct user_record *user); extern void post_message(struct user_record *user); -extern void rundoor(struct user_record *user, char *cmd, int stdio); -extern void runexternal(struct user_record *user, char *cmd, int stdio, char **argv, char *cwd, int raw); +extern void rundoor(struct user_record *user, char *cmd, int stdio, char *codepage); +extern void runexternal(struct user_record *user, char *cmd, int stdio, char **argv, char *cwd, int raw, char *codepage); extern void bbs_list(struct user_record *user); diff --git a/config_default/bbs.ini b/config_default/bbs.ini index 0117907..e404c48 100644 --- a/config_default/bbs.ini +++ b/config_default/bbs.ini @@ -1,5 +1,5 @@ [main] -Codepage = cp437 +Codepage = CP437 Telnet Port = 2023 BBS Name = Magicka BBS Sysop Name = sysop @@ -11,6 +11,7 @@ MagiChat BBSTag = Magicka Default Tagline = Brought to you by Another Magicka BBS! External Editor cmd = /home/andrew/MagickaBBS/utils/magiedit/magiedit.sh External Editor stdio = true +External Editor Codepage = CP437 Automessage Write Level = 10 Fork = false Enable WWW = false diff --git a/doors.c b/doors.c index 1505d1b..5dba537 100644 --- a/doors.c +++ b/doors.c @@ -9,6 +9,7 @@ #include #include #include +#include #if defined(linux) # include #elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) @@ -130,7 +131,7 @@ int write_door32sys(struct user_record *user) { -void rundoor(struct user_record *user, char *cmd, int stdio) { +void rundoor(struct user_record *user, char *cmd, int stdio, char *codepage) { char *arguments[4]; int door_out; char buffer[10]; @@ -147,14 +148,14 @@ void rundoor(struct user_record *user, char *cmd, int stdio) { arguments[2] = strdup(buffer); arguments[3] = NULL; - runexternal(user, cmd, stdio, arguments, NULL, 0); + runexternal(user, cmd, stdio, arguments, NULL, 0, codepage); free(arguments[0]); free(arguments[1]); free(arguments[2]); } -void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], char *cwd, int raw) { +void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], char *cwd, int raw, char *codepage) { char buffer[1024]; int pid; @@ -172,7 +173,8 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c int i; int gotiac; int flush; - + iconv_t ic; + struct timeval thetimeout; struct termios oldit; struct termios oldot; @@ -182,6 +184,11 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c char outbuf[512]; int h; int g; + char *ptr1; + char *ptr2; + size_t ouc; + size_t inc; + timeoutpaused = 1; if (write_door32sys(user) != 0) { @@ -302,7 +309,27 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c } } } - write(master, outbuf, g); + if (codepage == NULL || (strcmp(codepage, "CP437") == 0 && conf.codepage == 0) || (strcmp(codepage, "UTF-8") == 0 && conf.codepage == 1)) { + write(master, outbuf, g); + } else { + if (conf.codepage == 0) { + ic = iconv_open("CP437", codepage); + } else { + ic = iconv_open("UTF-8", codepage); + } + ptr1 = outbuf; + ptr2 = (char *)malloc((g + 1) * 2); + memset(ptr2, 0, (g + 1) * 2); + inc = g; + ouc = g * 2; + + iconv(ic, &ptr1, &inc, &ptr2, &ouc); + ptr2 = ptr2 - (g * 2 - ouc); + write(master, ptr2, strlen(ptr2)); + + free(ptr2); + iconv_close(ic); + } } else if (FD_ISSET(master, &fdset)) { len = read(master, inbuf, 256); if (len == 0) { @@ -317,7 +344,27 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c } outbuf[g++] = c; } - write(door_out, outbuf, g); + if (codepage == NULL || (strcmp(codepage, "CP437") == 0 && conf.codepage == 0) || (strcmp(codepage, "UTF-8") == 0 && conf.codepage == 1)) { + write(door_out, outbuf, g); + } else { + if (conf.codepage == 0) { + ic = iconv_open(codepage, "CP437"); + } else { + ic = iconv_open(codepage, "UTF-8"); + } + ptr1 = outbuf; + ptr2 = (char *)malloc((g + 1) * 2); + memset(ptr2, 0, (g + 1) * 2); + inc = g; + ouc = g * 2; + + iconv(ic, &ptr1, &inc, &ptr2, &ouc); + ptr2 = ptr2 - (g * 2 - ouc); + write(door_out, ptr2, strlen(ptr2)); + + free(ptr2); + iconv_close(ic); + } } } else { if (!running_door) { diff --git a/files.c b/files.c index f65aca0..d10fe62 100644 --- a/files.c +++ b/files.c @@ -439,7 +439,7 @@ int do_download(struct user_record *user, char *file) { arguments[0] = download_command; - runexternal(user, download_command, conf.protocols[user->defprotocol - 1]->stdio, arguments, conf.bbs_path, 1); + runexternal(user, download_command, conf.protocols[user->defprotocol - 1]->stdio, arguments, conf.bbs_path, 1, NULL); free(arguments); } @@ -540,7 +540,7 @@ int do_upload(struct user_record *user, char *final_path) { mkdir(upload_path, 0755); - runexternal(user, upload_command, conf.protocols[user->defprotocol - 1]->stdio, arguments, upload_path, 1); + runexternal(user, upload_command, conf.protocols[user->defprotocol - 1]->stdio, arguments, upload_path, 1, NULL); free(arguments); diff --git a/lua_glue.c b/lua_glue.c index 9b468c9..51f9f3a 100644 --- a/lua_glue.c +++ b/lua_glue.c @@ -100,8 +100,8 @@ int l_bbsMailScan(lua_State *L) { int l_bbsRunDoor(lua_State *L) { char *cmd = (char *)lua_tostring(L, 1); int stdio = lua_toboolean(L, 2); - - rundoor(gUser, cmd, stdio); + char *codepage = (char *)lua_tostring(L, 3); + rundoor(gUser, cmd, stdio, codepage); return 0; } diff --git a/mail_menu.c b/mail_menu.c index d872ee4..ff5f9ec 100644 --- a/mail_menu.c +++ b/mail_menu.c @@ -423,7 +423,7 @@ char *external_editor(struct user_record *user, char *to, char *from, char *quot } fclose(fptr); - rundoor(user, conf.external_editor_cmd, conf.external_editor_stdio); + rundoor(user, conf.external_editor_cmd, conf.external_editor_stdio, conf.external_editor_codepage); // readin msgtmp sprintf(buffer, "%s/node%d/MSGTMP", conf.bbs_path, mynode); diff --git a/main.c b/main.c index 0fd7434..dc14ac7 100644 --- a/main.c +++ b/main.c @@ -210,6 +210,8 @@ static int door_config_handler(void* user, const char* section, const char* name } else { conf->doors[i]->stdio = 0; } + } else if (strcasecmp(name, "codepage") == 0) { + conf->doors[i]->codepage = strdup(value); } return 1; } @@ -224,6 +226,7 @@ static int door_config_handler(void* user, const char* section, const char* name conf->doors[conf->door_count] = (struct door_config *)malloc(sizeof(struct door_config)); conf->doors[conf->door_count]->name = strdup(section); + conf->doors[conf->door_count]->codepage = NULL; if (strcasecmp(name, "command") == 0) { conf->doors[conf->door_count]->command = strdup(value); @@ -233,6 +236,8 @@ static int door_config_handler(void* user, const char* section, const char* name } else { conf->doors[conf->door_count]->stdio = 0; } + } else if (strcasecmp(name, "codepage") == 0) { + conf->doors[conf->door_count]->codepage = strdup(value); } conf->door_count++; @@ -434,6 +439,8 @@ static int handler(void* user, const char* section, const char* name, conf->default_tagline = strdup(value); } else if (strcasecmp(name, "external editor cmd") == 0) { conf->external_editor_cmd = strdup(value); + } else if (strcasecmp(name, "external editor codepage") == 0) { + conf->external_editor_codepage = strdup(value); } else if (strcasecmp(name, "external editor stdio") == 0) { if (strcasecmp(value, "true") == 0) { conf->external_editor_stdio = 1; @@ -482,7 +489,7 @@ static int handler(void* user, const char* section, const char* name, } else if (strcasecmp(name, "codepage") == 0) { if (strcasecmp(value, "cp437") == 0) { conf->codepage = 0; - } else if (strcasecmp(value, "utf8") == 0) { + } else if (strcasecmp(value, "utf-8") == 0) { conf->codepage = 1; } } @@ -1143,6 +1150,7 @@ int main(int argc, char **argv) { conf.mgchat_bbstag = NULL; conf.text_file_count = 0; conf.external_editor_cmd = NULL; + conf.external_editor_codepage = NULL; conf.log_path = NULL; conf.script_path = NULL; conf.automsgwritelvl = 10; diff --git a/menus.c b/menus.c index 2cd1930..e2f1d4e 100644 --- a/menus.c +++ b/menus.c @@ -2,6 +2,7 @@ #include #include #include +#include #include "bbs.h" #include "lua/lua.h" #include "lua/lualib.h" @@ -370,7 +371,7 @@ int menu_system(char *menufile) { for (j=0;jdata, conf.doors[j]->name) == 0) { dolog("%s launched door %s, on node %d", gUser->loginname, conf.doors[j]->name, mynode); - rundoor(gUser, conf.doors[j]->command, conf.doors[j]->stdio); + rundoor(gUser, conf.doors[j]->command, conf.doors[j]->stdio, conf.doors[j]->codepage); dolog("%s returned from door %s, on node %d", gUser->loginname, conf.doors[j]->name, mynode); break; } From 8b611fc3febd84809f39f1710ea9eac1662cb1ea Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 23 Apr 2017 12:28:01 +1000 Subject: [PATCH 03/14] More work on utf8 --- bbs.c | 3 +++ bbs.h | 1 + files.c | 4 ++-- magicka.strings | 1 + settings.c | 7 +++++++ users.c | 15 ++++++++++----- 6 files changed, 24 insertions(+), 7 deletions(-) diff --git a/bbs.c b/bbs.c index 2912c4c..2c387ae 100644 --- a/bbs.c +++ b/bbs.c @@ -210,6 +210,9 @@ void s_printf(char *fmt, ...) { } int should_convert_utf8() { + if (gUser != NULL) { + return gUser->codepage; + } return conf.codepage; } diff --git a/bbs.h b/bbs.h index 58ab727..07e14d0 100644 --- a/bbs.h +++ b/bbs.h @@ -202,6 +202,7 @@ struct user_record { int defarchiver; int defprotocol; int nodemsgs; + int codepage; }; struct jam_msg { diff --git a/files.c b/files.c index d10fe62..c30e96b 100644 --- a/files.c +++ b/files.c @@ -443,7 +443,7 @@ int do_download(struct user_record *user, char *file) { free(arguments); } - + return 1; } int do_upload(struct user_record *user, char *final_path) { @@ -571,7 +571,7 @@ int do_upload(struct user_record *user, char *final_path) { } while ((dent = readdir(inb)) != NULL) { if (dent->d_type == DT_REG) { - snprintf(upload_command, 1-24, "%s%s", upload_path, dent->d_name); + 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) { diff --git a/magicka.strings b/magicka.strings index 7e089d4..e34e677 100644 --- a/magicka.strings +++ b/magicka.strings @@ -219,3 +219,4 @@ File exists!\r\n \r\n\e[1;31mInvalid Node!\e[0m\r\n \r\n\e[1;37mEnter your message (256 chars max)...\e[0m\r\n %s on node %d says:\r\n %s\r\n\r\n +\e[0;36mC. \e[1;37mCodepage (\e[1;33m%s\e[1;37m)\r\n diff --git a/settings.c b/settings.c index daf4867..314a198 100644 --- a/settings.c +++ b/settings.c @@ -22,6 +22,7 @@ void settings_menu(struct user_record *user) { s_printf(get_string(205), conf.archivers[user->defarchiver - 1]->name); s_printf(get_string(213), conf.protocols[user->defprotocol - 1]->name); s_printf(get_string(215), (user->nodemsgs ? "TRUE" : "FALSE")); + s_printf(get_string(221), (user->codepage ? "UTF-8" : "CP437")); s_printf(get_string(153)); s_printf(get_string(154)); @@ -118,6 +119,12 @@ void settings_menu(struct user_record *user) { save_user(user); } break; + case 'c': + { + user->codepage = !user->codepage; + save_user(user); + } + break; case 'q': dosettings = 1; break; diff --git a/users.c b/users.c index 06e0c09..5db48d3 100644 --- a/users.c +++ b/users.c @@ -81,7 +81,7 @@ int save_user(struct user_record *user) { int rc; char *update_sql = "UPDATE users SET password=?, salt=?, firstname=?," - "lastname=?, email=?, location=?, sec_level=?, last_on=?, time_left=?, cur_mail_conf=?, cur_mail_area=?, cur_file_dir=?, cur_file_sub=?, times_on=?, bwavepktno=?, archiver=?, protocol=?,nodemsgs=? where loginname LIKE ?"; + "lastname=?, email=?, location=?, sec_level=?, last_on=?, time_left=?, cur_mail_conf=?, cur_mail_area=?, cur_file_dir=?, cur_file_sub=?, times_on=?, bwavepktno=?, archiver=?, protocol=?,nodemsgs=?,codepage=? where loginname LIKE ?"; sprintf(buffer, "%s/users.sq3", conf.bbs_path); @@ -115,7 +115,8 @@ sqlite3_busy_timeout(db, 5000); sqlite3_bind_int(res, 16, user->defarchiver); sqlite3_bind_int(res, 17, user->defprotocol); sqlite3_bind_int(res, 18, user->nodemsgs); - sqlite3_bind_text(res, 19, user->loginname, -1, 0); + sqlite3_bind_int(res, 19, user->codepage); + sqlite3_bind_text(res, 20, user->loginname, -1, 0); } else { dolog("Failed to execute statement: %s", sqlite3_errmsg(db)); } @@ -159,10 +160,11 @@ int inst_user(struct user_record *user) { "bwavepktno INTEGER," "archiver INTEGER," "protocol INTEGER," - "nodemsgs INTEGER);"; + "nodemsgs INTEGER," + "codepage INTEGER);"; char *insert_sql = "INSERT INTO users (loginname, password, salt, firstname," - "lastname, email, location, sec_level, last_on, time_left, cur_mail_conf, cur_mail_area, cur_file_dir, cur_file_sub, times_on, bwavepktno, archiver, protocol, nodemsgs) VALUES(?,?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; + "lastname, email, location, sec_level, last_on, time_left, cur_mail_conf, cur_mail_area, cur_file_dir, cur_file_sub, times_on, bwavepktno, archiver, protocol, nodemsgs, codepage) VALUES(?,?, ?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; char *err_msg = 0; sprintf(buffer, "%s/users.sq3", conf.bbs_path); @@ -209,6 +211,7 @@ sqlite3_busy_timeout(db, 5000); sqlite3_bind_int(res, 17, user->defarchiver); sqlite3_bind_int(res, 18, user->defprotocol); sqlite3_bind_int(res, 19, user->nodemsgs); + sqlite3_bind_int(res, 20, user->codepage); } else { dolog("Failed to execute statement: %s", sqlite3_errmsg(db)); } @@ -236,7 +239,7 @@ struct user_record *check_user_pass(char *loginname, char *password) { sqlite3_stmt *res; int rc; char *sql = "SELECT Id, loginname, password, salt, firstname," - "lastname, email, location, sec_level, last_on, time_left, cur_mail_conf, cur_mail_area, cur_file_dir, cur_file_sub, times_on, bwavepktno, archiver, protocol,nodemsgs FROM users WHERE loginname LIKE ?"; + "lastname, email, location, sec_level, last_on, time_left, cur_mail_conf, cur_mail_area, cur_file_dir, cur_file_sub, times_on, bwavepktno, archiver, protocol,nodemsgs, codepage FROM users WHERE loginname LIKE ?"; char *pass_hash; sprintf(buffer, "%s/users.sq3", conf.bbs_path); @@ -284,6 +287,7 @@ struct user_record *check_user_pass(char *loginname, char *password) { user->defarchiver = sqlite3_column_int(res, 17); user->defprotocol = sqlite3_column_int(res, 18); user->nodemsgs = sqlite3_column_int(res, 19); + user->codepage = sqlite3_column_int(res, 20); pass_hash = hash_sha256(password, user->salt); if (strcmp(pass_hash, user->password) != 0) { @@ -568,6 +572,7 @@ struct user_record *new_user() { user->defprotocol = 1; user->defarchiver = 1; user->nodemsgs = 1; + user->codepage = conf.codepage; inst_user(user); return user; From db59e0e4b25c01fc76c2ff6b61c1b3cb366b9a7b Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 23 Apr 2017 12:31:03 +1000 Subject: [PATCH 04/14] Switch from default to user defined codepage --- doors.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doors.c b/doors.c index 5dba537..1f8d59e 100644 --- a/doors.c +++ b/doors.c @@ -309,10 +309,10 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c } } } - if (codepage == NULL || (strcmp(codepage, "CP437") == 0 && conf.codepage == 0) || (strcmp(codepage, "UTF-8") == 0 && conf.codepage == 1)) { + if (codepage == NULL || (strcmp(codepage, "CP437") == 0 && gUser->codepage == 0) || (strcmp(codepage, "UTF-8") == 0 && gUser->codepage == 1)) { write(master, outbuf, g); } else { - if (conf.codepage == 0) { + if (gUser->codepage == 0) { ic = iconv_open("CP437", codepage); } else { ic = iconv_open("UTF-8", codepage); @@ -344,10 +344,10 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c } outbuf[g++] = c; } - if (codepage == NULL || (strcmp(codepage, "CP437") == 0 && conf.codepage == 0) || (strcmp(codepage, "UTF-8") == 0 && conf.codepage == 1)) { + if (codepage == NULL || (strcmp(codepage, "CP437") == 0 && gUser->codepage == 0) || (strcmp(codepage, "UTF-8") == 0 && gUser->codepage == 1)) { write(door_out, outbuf, g); } else { - if (conf.codepage == 0) { + if (gUser->codepage == 0) { ic = iconv_open(codepage, "CP437"); } else { ic = iconv_open(codepage, "UTF-8"); From d76ac1e1517b93029cf563830c85bc2dea9539c1 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 23 Apr 2017 12:31:51 +1000 Subject: [PATCH 05/14] change guser to user --- doors.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doors.c b/doors.c index 1f8d59e..8a1c216 100644 --- a/doors.c +++ b/doors.c @@ -309,10 +309,10 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c } } } - if (codepage == NULL || (strcmp(codepage, "CP437") == 0 && gUser->codepage == 0) || (strcmp(codepage, "UTF-8") == 0 && gUser->codepage == 1)) { + if (codepage == NULL || (strcmp(codepage, "CP437") == 0 && user->codepage == 0) || (strcmp(codepage, "UTF-8") == 0 && user->codepage == 1)) { write(master, outbuf, g); } else { - if (gUser->codepage == 0) { + if (user->codepage == 0) { ic = iconv_open("CP437", codepage); } else { ic = iconv_open("UTF-8", codepage); @@ -344,10 +344,10 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c } outbuf[g++] = c; } - if (codepage == NULL || (strcmp(codepage, "CP437") == 0 && gUser->codepage == 0) || (strcmp(codepage, "UTF-8") == 0 && gUser->codepage == 1)) { + if (codepage == NULL || (strcmp(codepage, "CP437") == 0 && user->codepage == 0) || (strcmp(codepage, "UTF-8") == 0 && user->codepage == 1)) { write(door_out, outbuf, g); } else { - if (gUser->codepage == 0) { + if (user->codepage == 0) { ic = iconv_open(codepage, "CP437"); } else { ic = iconv_open(codepage, "UTF-8"); From f8b6c4dc89f518ca541ee8a3c3a100d5636e32dc Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 23 Apr 2017 12:37:23 +1000 Subject: [PATCH 06/14] update users.sq3 utiltity --- utils/sql_update/users_sql_update.pl | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/utils/sql_update/users_sql_update.pl b/utils/sql_update/users_sql_update.pl index bd4a7c0..2a025bf 100755 --- a/utils/sql_update/users_sql_update.pl +++ b/utils/sql_update/users_sql_update.pl @@ -116,3 +116,23 @@ if (check_exists("nodemsgs") == 0) { $dbh->do($sql); $dbh->disconnect; } + +if (check_exists("codepage") == 0) { + print "Column \"codepage\" doesn't exist... adding..\n"; + + my ($needed) = @_; + + my $dsn = "dbi:SQLite:dbname=$dbfile"; + my $user = ""; + my $password = ""; + my $dbh = DBI->connect($dsn, $user, $password, { + PrintError => 0, + RaiseError => 1, + AutoCommit => 1, + FetchHashKeyName => 'NAME_lc', + }); + + my $sql = "ALTER TABLE users ADD COLUMN codepage INTEGER DEFAULT 0"; + $dbh->do($sql); + $dbh->disconnect; +} \ No newline at end of file From 0e16613143860998aaed1b43838406a47407683d Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 23 Apr 2017 13:03:37 +1000 Subject: [PATCH 07/14] attempt to fix utf8 conversion on linux --- Makefile.linux | 2 +- Makefile.linux.WWW | 2 +- bbs.c | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/Makefile.linux b/Makefile.linux index 71ff5c1..5d8de24 100644 --- a/Makefile.linux +++ b/Makefile.linux @@ -35,7 +35,7 @@ OBJ = inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o doors.o bbs_list.o $(CC) -c -o $@ $< $(CFLAGS) magicka: $(OBJ) ${LUA} ${ZMODEM} ${JAMLIB} ${JSMN} - $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh -liconv + $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh magimail: $(JAMLIB) cd utils/magimail && $(MAKE) linux diff --git a/Makefile.linux.WWW b/Makefile.linux.WWW index d868726..cbefdfd 100644 --- a/Makefile.linux.WWW +++ b/Makefile.linux.WWW @@ -39,7 +39,7 @@ OBJ = deps/aha/aha.o inih/ini.o bbs.o main.o users.o main_menu.o mail_menu.o do $(CC) -c -o $@ $< $(CFLAGS) magicka: $(OBJ) ${LUA} ${JAMLIB} ${ZMODEM} ${B64} ${JSMN} - $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh -liconv $(MICROHTTPD) + $(CC) -o magicka -o $@ $^ $(CFLAGS) -L/usr/local/lib -lsqlite3 -lutil -lm -ldl -lssl -lcrypto -lssh $(MICROHTTPD) magimail: $(JAMLIB) cd utils/magimail && $(MAKE) linux diff --git a/bbs.c b/bbs.c index 2c387ae..8fa61c2 100644 --- a/bbs.c +++ b/bbs.c @@ -246,7 +246,7 @@ void s_putchar(char c) { if (sshBBS) { fprintf(stdout, "%s", ptr1); } else { - write(gSocket, ptr1, strlen(ptr1)); + write(gSocket, ptr1, outbuf - ptr1); } iconv_close(ic); free(ptr1); @@ -282,7 +282,7 @@ void s_putstring(char *c) { if (sshBBS) { fprintf(stdout, "%s", ptr1); } else { - write(gSocket, ptr1, strlen(ptr1)); + write(gSocket, ptr1, outbuf - ptr1); } iconv_close(ic); free(ptr1); From 5a507cc0e33569c9fcf156b53baa35beb9688736 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 23 Apr 2017 13:07:30 +1000 Subject: [PATCH 08/14] Doors.... --- doors.c | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/doors.c b/doors.c index 8a1c216..98d8473 100644 --- a/doors.c +++ b/doors.c @@ -186,6 +186,7 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c int g; char *ptr1; char *ptr2; + char *ptr2p; size_t ouc; size_t inc; @@ -319,15 +320,15 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c } ptr1 = outbuf; ptr2 = (char *)malloc((g + 1) * 2); + ptr2p = ptr2; memset(ptr2, 0, (g + 1) * 2); inc = g; ouc = g * 2; iconv(ic, &ptr1, &inc, &ptr2, &ouc); - ptr2 = ptr2 - (g * 2 - ouc); - write(master, ptr2, strlen(ptr2)); + write(master, ptr2p, ptr2p - ptr2); - free(ptr2); + free(ptr2p); iconv_close(ic); } } else if (FD_ISSET(master, &fdset)) { @@ -354,15 +355,15 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c } ptr1 = outbuf; ptr2 = (char *)malloc((g + 1) * 2); + ptr2p = ptr2; memset(ptr2, 0, (g + 1) * 2); inc = g; ouc = g * 2; iconv(ic, &ptr1, &inc, &ptr2, &ouc); - ptr2 = ptr2 - (g * 2 - ouc); - write(door_out, ptr2, strlen(ptr2)); + write(door_out, ptr2p, ptr2p - ptr2); - free(ptr2); + free(ptr2p); iconv_close(ic); } } From 7c4b4142997c724e1eadf66a91c2cdae55ea9f35 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 23 Apr 2017 13:09:24 +1000 Subject: [PATCH 09/14] whoops --- doors.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doors.c b/doors.c index 98d8473..213984b 100644 --- a/doors.c +++ b/doors.c @@ -326,7 +326,7 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c ouc = g * 2; iconv(ic, &ptr1, &inc, &ptr2, &ouc); - write(master, ptr2p, ptr2p - ptr2); + write(master, ptr2p, ptr2 - ptr2p); free(ptr2p); iconv_close(ic); @@ -361,7 +361,7 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c ouc = g * 2; iconv(ic, &ptr1, &inc, &ptr2, &ouc); - write(door_out, ptr2p, ptr2p - ptr2); + write(door_out, ptr2p, ptr2 - ptr2p); free(ptr2p); iconv_close(ic); From 10bc57c164e87ebe1fb3465ad9849f6cce2ddfb1 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 23 Apr 2017 13:16:34 +1000 Subject: [PATCH 10/14] around the wrong way? --- doors.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/doors.c b/doors.c index 213984b..d9b3802 100644 --- a/doors.c +++ b/doors.c @@ -314,9 +314,9 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c write(master, outbuf, g); } else { if (user->codepage == 0) { - ic = iconv_open("CP437", codepage); + ic = iconv_open(codepage, "CP437"); } else { - ic = iconv_open("UTF-8", codepage); + ic = iconv_open(codepage, "UTF-8"); } ptr1 = outbuf; ptr2 = (char *)malloc((g + 1) * 2); @@ -349,9 +349,9 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c write(door_out, outbuf, g); } else { if (user->codepage == 0) { - ic = iconv_open(codepage, "CP437"); + ic = iconv_open("CP437", codepage); } else { - ic = iconv_open(codepage, "UTF-8"); + ic = iconv_open("UTF-8", codepage); } ptr1 = outbuf; ptr2 = (char *)malloc((g + 1) * 2); From 0d857ec599c75d326464860bb413d4c777347053 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 23 Apr 2017 13:25:49 +1000 Subject: [PATCH 11/14] debugging --- doors.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/doors.c b/doors.c index d9b3802..012b0cd 100644 --- a/doors.c +++ b/doors.c @@ -189,6 +189,7 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c char *ptr2p; size_t ouc; size_t inc; + size_t sz; timeoutpaused = 1; @@ -360,7 +361,10 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c inc = g; ouc = g * 2; - iconv(ic, &ptr1, &inc, &ptr2, &ouc); + sz = iconv(ic, &ptr1, &inc, &ptr2, &ouc); + if (sz == -1) { + perror("Iconv"); + } write(door_out, ptr2p, ptr2 - ptr2p); free(ptr2p); From 95e5aa1cec5dc79f0ec718149fc35974c228135a Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 23 Apr 2017 13:29:29 +1000 Subject: [PATCH 12/14] more debugging --- doors.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doors.c b/doors.c index 012b0cd..627bd06 100644 --- a/doors.c +++ b/doors.c @@ -324,7 +324,7 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c ptr2p = ptr2; memset(ptr2, 0, (g + 1) * 2); inc = g; - ouc = g * 2; + ouc = (g + 1) * 2; iconv(ic, &ptr1, &inc, &ptr2, &ouc); write(master, ptr2p, ptr2 - ptr2p); @@ -359,7 +359,7 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c ptr2p = ptr2; memset(ptr2, 0, (g + 1) * 2); inc = g; - ouc = g * 2; + ouc = (g + 1) * 2; sz = iconv(ic, &ptr1, &inc, &ptr2, &ouc); if (sz == -1) { From 6c3184cac09d7b7c21dde5f80d3a61385a4d3969 Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 23 Apr 2017 13:33:09 +1000 Subject: [PATCH 13/14] make the buffers bigger --- doors.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/doors.c b/doors.c index 627bd06..0da1b67 100644 --- a/doors.c +++ b/doors.c @@ -320,11 +320,11 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c ic = iconv_open(codepage, "UTF-8"); } ptr1 = outbuf; - ptr2 = (char *)malloc((g + 1) * 2); + ptr2 = (char *)malloc((g + 1) * 4); ptr2p = ptr2; - memset(ptr2, 0, (g + 1) * 2); + memset(ptr2, 0, (g + 1) * 4); inc = g; - ouc = (g + 1) * 2; + ouc = (g + 1) * 4; iconv(ic, &ptr1, &inc, &ptr2, &ouc); write(master, ptr2p, ptr2 - ptr2p); @@ -355,11 +355,11 @@ void runexternal(struct user_record *user, char *cmd, int stdio, char *argv[], c ic = iconv_open("UTF-8", codepage); } ptr1 = outbuf; - ptr2 = (char *)malloc((g + 1) * 2); + ptr2 = (char *)malloc((g + 1) * 4); ptr2p = ptr2; - memset(ptr2, 0, (g + 1) * 2); + memset(ptr2, 0, (g + 1) * 4); inc = g; - ouc = (g + 1) * 2; + ouc = (g + 1) * 4; sz = iconv(ic, &ptr1, &inc, &ptr2, &ouc); if (sz == -1) { From 660963a04da68a4bea153961e2cd9e59b06faaab Mon Sep 17 00:00:00 2001 From: Andrew Pamment Date: Sun, 23 Apr 2017 13:36:54 +1000 Subject: [PATCH 14/14] Buffers should be big enough now --- bbs.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bbs.c b/bbs.c index 8fa61c2..c1d86e2 100644 --- a/bbs.c +++ b/bbs.c @@ -234,12 +234,12 @@ void s_putchar(char c) { } } else { ic = iconv_open("UTF-8", "CP437"); - inbuf = (char *)malloc(3); - outbuf = (char *)malloc(3); - memset(outbuf, 0, 3); + inbuf = (char *)malloc(4); + outbuf = (char *)malloc(4); + memset(outbuf, 0, 4); sprintf(inbuf, "%c", c); inc = 1; - ouc = 3; + ouc = 4; ptr1 = outbuf; ptr2 = inbuf; sz = iconv(ic, &inbuf, &inc, &outbuf, &ouc); @@ -273,11 +273,11 @@ void s_putstring(char *c) { ic = iconv_open("UTF-8", "CP437"); inc = strlen(c); inbuf = strdup(c); - outbuf = (char *)malloc(inc * 2); - memset(outbuf, 0, inc *2); + outbuf = (char *)malloc(inc * 4); + memset(outbuf, 0, inc * 4); ptr1 = outbuf; ptr2 = inbuf; - ouc = inc * 2; + ouc = inc * 4; sz = iconv(ic, &inbuf, &inc, &outbuf, &ouc); if (sshBBS) { fprintf(stdout, "%s", ptr1);