--- lbbs/src/bbs_net.c 2025/10/18 01:50:48 1.66 +++ lbbs/src/bbs_net.c 2025/10/20 04:17:36 1.73 @@ -47,8 +47,6 @@ #define MAXSTATION 26 * 2 #define STATION_PER_LINE 4 -#define BBS_NET_DEFAULT_CHARSET "UTF-8" - struct _bbsnet_conf { char host1[20]; @@ -209,14 +207,15 @@ int bbsnet_connect(int n) int output_buf_len = 0; int input_buf_offset = 0; int output_buf_offset = 0; - iconv_t input_cd = NULL; char input_conv[LINE_BUFFER_LEN * 2]; char output_conv[LINE_BUFFER_LEN * 2]; int input_conv_len = 0; int output_conv_len = 0; int input_conv_offset = 0; int output_conv_offset = 0; + iconv_t input_cd = NULL; iconv_t output_cd = NULL; + char tocode[32]; struct epoll_event ev, events[MAX_EVENTS]; int nfds, epollfd; int stdin_read_wait = 0; @@ -425,16 +424,21 @@ int bbsnet_connect(int n) log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n", remote_addr, remote_port, local_addr, local_port, BBS_username); - input_cd = iconv_open(bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET); + snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset, + (strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE")); + input_cd = iconv_open(tocode, stdio_charset); if (input_cd == (iconv_t)(-1)) { - log_error("iconv_open(%s->%s) error: %d\n", BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset, errno); + log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno); goto cleanup; } - output_cd = iconv_open(BBS_NET_DEFAULT_CHARSET, bbsnet_conf[n].charset); - if (input_cd == (iconv_t)(-1)) + + snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset, + (strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT")); + output_cd = iconv_open(tocode, bbsnet_conf[n].charset); + if (output_cd == (iconv_t)(-1)) { - log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, BBS_NET_DEFAULT_CHARSET, errno); + log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno); iconv_close(input_cd); goto cleanup; } @@ -532,6 +536,11 @@ int bbsnet_connect(int n) } else if (ret == 0) { + // Send NO-OP to remote server + input_buf[input_buf_len] = '\0'; + input_buf_len++; + BBS_last_access_tm = time(NULL); + stdin_read_wait = 0; break; // Check whether channel is still open } @@ -587,11 +596,28 @@ int bbsnet_connect(int n) { if (input_buf_offset < input_buf_len) { + // For debug +#ifdef _DEBUG + for (int j = input_buf_offset; j < input_buf_len; j++) + { + log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256); + } +#endif + ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len); if (ret < 0) { log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len); + input_buf_len = input_buf_offset; // Discard invalid sequence + } + + // For debug +#ifdef _DEBUG + for (int j = input_conv_offset; j < input_conv_len; j++) + { + log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256); } +#endif } while (input_conv_offset < input_conv_len && !SYS_server_exit) @@ -686,6 +712,7 @@ int bbsnet_connect(int n) if (ret < 0) { log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len); + output_buf_len = output_buf_offset; // Discard invalid sequence } } @@ -775,6 +802,8 @@ cleanup: tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min, tm_used->tm_sec); + BBS_last_access_tm = time(NULL); + return 0; } @@ -843,10 +872,10 @@ int bbs_net() { ch = igetch(100); - if (ch != KEY_NULL && ch != KEY_TIMEOUT) - { - BBS_last_access_tm = time(NULL); - } + if (ch != KEY_NULL && ch != KEY_TIMEOUT) + { + BBS_last_access_tm = time(NULL); + } switch (ch) {