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) {