| 1 |
/*************************************************************************** |
/* SPDX-License-Identifier: GPL-3.0-or-later */ |
| 2 |
bbs_net.c - description |
/* |
| 3 |
------------------- |
* bbs_net |
| 4 |
Copyright : (C) 2004-2025 by Leaflet |
* - user interactive feature of site shuttle |
| 5 |
Email : leaflet@leafok.com |
* |
| 6 |
***************************************************************************/ |
* Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com> |
| 7 |
|
*/ |
|
/*************************************************************************** |
|
|
* * |
|
|
* This program is free software; you can redistribute it and/or modify * |
|
|
* it under the terms of the GNU General Public License as published by * |
|
|
* the Free Software Foundation; either version 3 of the License, or * |
|
|
* (at your option) any later version. * |
|
|
* * |
|
|
***************************************************************************/ |
|
| 8 |
|
|
| 9 |
#include "bbs.h" |
#include "bbs.h" |
| 10 |
#include "common.h" |
#include "common.h" |
| 33 |
#include <sys/socket.h> |
#include <sys/socket.h> |
| 34 |
#include <sys/epoll.h> |
#include <sys/epoll.h> |
| 35 |
|
|
| 36 |
#define MENU_CONF_DELIM " \t\r\n" |
static const char MENU_CONF_DELIM[] = " \t\r\n"; |
| 37 |
|
|
| 38 |
#define MAX_PROCESS_BAR_LEN 30 |
enum _bbs_net_constant_t |
| 39 |
#define MAXSTATION 26 * 2 |
{ |
| 40 |
#define STATION_PER_LINE 4 |
MAX_PROCESS_BAR_LEN = 30, |
| 41 |
|
MAXSTATION = 26 * 2, |
| 42 |
|
STATION_PER_LINE = 4, |
| 43 |
|
}; |
| 44 |
|
|
| 45 |
struct _bbsnet_conf |
struct _bbsnet_conf |
| 46 |
{ |
{ |
| 48 |
char host2[40]; |
char host2[40]; |
| 49 |
char ip[40]; |
char ip[40]; |
| 50 |
in_port_t port; |
in_port_t port; |
| 51 |
char charset[20]; |
char charset[CHARSET_MAX_LEN + 1]; |
| 52 |
} bbsnet_conf[MAXSTATION]; |
} bbsnet_conf[MAXSTATION]; |
| 53 |
|
|
| 54 |
MENU_SET bbsnet_menu; |
static MENU_SET bbsnet_menu; |
| 55 |
|
|
| 56 |
int load_bbsnet_conf(const char *file_config) |
static int load_bbsnet_conf(const char *file_config) |
| 57 |
{ |
{ |
| 58 |
FILE *fp; |
FILE *fp; |
| 59 |
MENU *p_menu; |
MENU *p_menu; |
| 148 |
return 0; |
return 0; |
| 149 |
} |
} |
| 150 |
|
|
| 151 |
void unload_bbsnet_conf(void) |
static void unload_bbsnet_conf(void) |
| 152 |
{ |
{ |
| 153 |
bbsnet_menu.menu_count = 0; |
bbsnet_menu.menu_count = 0; |
| 154 |
bbsnet_menu.menu_item_count = 0; |
bbsnet_menu.menu_item_count = 0; |
| 159 |
bbsnet_menu.p_menu_item_pool = NULL; |
bbsnet_menu.p_menu_item_pool = NULL; |
| 160 |
} |
} |
| 161 |
|
|
| 162 |
void process_bar(int n, int len) |
static void process_bar(int n, int len) |
| 163 |
{ |
{ |
| 164 |
char buf[LINE_BUFFER_LEN]; |
char buf[LINE_BUFFER_LEN]; |
| 165 |
char buf2[LINE_BUFFER_LEN]; |
char buf2[LINE_BUFFER_LEN]; |
| 180 |
moveto(4, 0); |
moveto(4, 0); |
| 181 |
prints(" ------------------------------ \r\n"); |
prints(" ------------------------------ \r\n"); |
| 182 |
snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); |
snprintf(buf, sizeof(buf), " %3d%% ", n * 100 / len); |
| 183 |
strncpy(buf2, buf, (size_t)n); |
memcpy(buf2, buf, (size_t)n); |
| 184 |
buf2[n] = '\0'; |
buf2[n] = '\0'; |
| 185 |
prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n); |
prints("|\033[46m%s\033[44m%s\033[m|\r\n", buf2, buf + n); |
| 186 |
prints(" ------------------------------ \r\n"); |
prints(" ------------------------------ \r\n"); |
| 187 |
iflush(); |
iflush(); |
| 188 |
} |
} |
| 189 |
|
|
| 190 |
int bbsnet_connect(int n) |
static int bbsnet_connect(int n) |
| 191 |
{ |
{ |
| 192 |
int sock, ret, loop, error; |
int sock, ret, loop, error; |
| 193 |
int sock_connected = 0; |
int sock_connected = 0; |
| 210 |
int output_conv_offset = 0; |
int output_conv_offset = 0; |
| 211 |
iconv_t input_cd = NULL; |
iconv_t input_cd = NULL; |
| 212 |
iconv_t output_cd = NULL; |
iconv_t output_cd = NULL; |
| 213 |
|
char tocode[32]; |
| 214 |
struct epoll_event ev, events[MAX_EVENTS]; |
struct epoll_event ev, events[MAX_EVENTS]; |
| 215 |
int nfds, epollfd; |
int nfds, epollfd; |
| 216 |
int stdin_read_wait = 0; |
int stdin_read_wait = 0; |
| 419 |
log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n", |
log_common("BBSNET connect to %s:%d from %s:%d by [%s]\n", |
| 420 |
remote_addr, remote_port, local_addr, local_port, BBS_username); |
remote_addr, remote_port, local_addr, local_port, BBS_username); |
| 421 |
|
|
| 422 |
input_cd = iconv_open(bbsnet_conf[n].charset, stdio_charset); |
snprintf(tocode, sizeof(tocode), "%s%s", bbsnet_conf[n].charset, |
| 423 |
|
(strcasecmp(stdio_charset, bbsnet_conf[n].charset) == 0 ? "" : "//IGNORE")); |
| 424 |
|
input_cd = iconv_open(tocode, stdio_charset); |
| 425 |
if (input_cd == (iconv_t)(-1)) |
if (input_cd == (iconv_t)(-1)) |
| 426 |
{ |
{ |
| 427 |
log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, bbsnet_conf[n].charset, errno); |
log_error("iconv_open(%s->%s) error: %d\n", stdio_charset, tocode, errno); |
| 428 |
goto cleanup; |
goto cleanup; |
| 429 |
} |
} |
| 430 |
output_cd = iconv_open(stdio_charset, bbsnet_conf[n].charset); |
|
| 431 |
|
snprintf(tocode, sizeof(tocode), "%s%s", stdio_charset, |
| 432 |
|
(strcasecmp(bbsnet_conf[n].charset, stdio_charset) == 0 ? "" : "//TRANSLIT")); |
| 433 |
|
output_cd = iconv_open(tocode, bbsnet_conf[n].charset); |
| 434 |
if (output_cd == (iconv_t)(-1)) |
if (output_cd == (iconv_t)(-1)) |
| 435 |
{ |
{ |
| 436 |
log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, stdio_charset, errno); |
log_error("iconv_open(%s->%s) error: %d\n", bbsnet_conf[n].charset, tocode, errno); |
| 437 |
iconv_close(input_cd); |
iconv_close(input_cd); |
| 438 |
goto cleanup; |
goto cleanup; |
| 439 |
} |
} |
| 479 |
} |
} |
| 480 |
else if (nfds == 0) // timeout |
else if (nfds == 0) // timeout |
| 481 |
{ |
{ |
| 482 |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |
| 483 |
{ |
{ |
| 484 |
break; |
break; |
| 485 |
} |
} |
| 531 |
} |
} |
| 532 |
else if (ret == 0) |
else if (ret == 0) |
| 533 |
{ |
{ |
| 534 |
|
// Send NO-OP to remote server |
| 535 |
|
input_buf[input_buf_len] = '\0'; |
| 536 |
|
input_buf_len++; |
| 537 |
|
BBS_last_access_tm = time(NULL); |
| 538 |
|
|
| 539 |
stdin_read_wait = 0; |
stdin_read_wait = 0; |
| 540 |
break; // Check whether channel is still open |
break; // Check whether channel is still open |
| 541 |
} |
} |
| 591 |
{ |
{ |
| 592 |
if (input_buf_offset < input_buf_len) |
if (input_buf_offset < input_buf_len) |
| 593 |
{ |
{ |
| 594 |
|
// For debug |
| 595 |
|
#ifdef _DEBUG |
| 596 |
|
for (int j = input_buf_offset; j < input_buf_len; j++) |
| 597 |
|
{ |
| 598 |
|
log_error("Debug input: <--[%u]\n", (input_buf[j] + 256) % 256); |
| 599 |
|
} |
| 600 |
|
#endif |
| 601 |
|
|
| 602 |
ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len); |
ret = io_buf_conv(input_cd, input_buf, &input_buf_len, &input_buf_offset, input_conv, sizeof(input_conv), &input_conv_len); |
| 603 |
if (ret < 0) |
if (ret < 0) |
| 604 |
{ |
{ |
| 605 |
log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len); |
log_error("io_buf_conv(input, %d, %d, %d) error\n", input_buf_len, input_buf_offset, input_conv_len); |
| 606 |
|
input_buf_len = input_buf_offset; // Discard invalid sequence |
| 607 |
} |
} |
| 608 |
|
|
| 609 |
|
// For debug |
| 610 |
|
#ifdef _DEBUG |
| 611 |
|
for (int j = input_conv_offset; j < input_conv_len; j++) |
| 612 |
|
{ |
| 613 |
|
log_error("Debug input_conv: <--[%u]\n", (input_conv[j] + 256) % 256); |
| 614 |
|
} |
| 615 |
|
#endif |
| 616 |
} |
} |
| 617 |
|
|
| 618 |
while (input_conv_offset < input_conv_len && !SYS_server_exit) |
while (input_conv_offset < input_conv_len && !SYS_server_exit) |
| 707 |
if (ret < 0) |
if (ret < 0) |
| 708 |
{ |
{ |
| 709 |
log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len); |
log_error("io_buf_conv(output, %d, %d, %d) error\n", output_buf_len, output_buf_offset, output_conv_len); |
| 710 |
|
output_buf_len = output_buf_offset; // Discard invalid sequence |
| 711 |
} |
} |
| 712 |
} |
} |
| 713 |
|
|
| 797 |
tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min, |
tm_used->tm_mday - 1, tm_used->tm_hour, tm_used->tm_min, |
| 798 |
tm_used->tm_sec); |
tm_used->tm_sec); |
| 799 |
|
|
| 800 |
|
BBS_last_access_tm = time(NULL); |
| 801 |
|
|
| 802 |
return 0; |
return 0; |
| 803 |
} |
} |
| 804 |
|
|
| 805 |
static int |
static int bbsnet_refresh() |
|
bbsnet_refresh() |
|
| 806 |
{ |
{ |
| 807 |
clearscr(); |
clearscr(); |
| 808 |
moveto(1, 0); |
moveto(1, 0); |
| 826 |
return 0; |
return 0; |
| 827 |
} |
} |
| 828 |
|
|
| 829 |
int bbsnet_selchange() |
static int bbsnet_selchange() |
| 830 |
{ |
{ |
| 831 |
int i = bbsnet_menu.menu_item_pos[0]; |
int i = bbsnet_menu.menu_item_pos[0]; |
| 832 |
|
|
| 851 |
return 0; |
return 0; |
| 852 |
} |
} |
| 853 |
|
|
| 854 |
int bbs_net() |
extern int bbs_net() |
| 855 |
{ |
{ |
| 856 |
int ch, i; |
int ch, i; |
| 857 |
|
|
| 866 |
{ |
{ |
| 867 |
ch = igetch(100); |
ch = igetch(100); |
| 868 |
|
|
| 869 |
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
if (ch != KEY_NULL && ch != KEY_TIMEOUT) |
| 870 |
{ |
{ |
| 871 |
BBS_last_access_tm = time(NULL); |
BBS_last_access_tm = time(NULL); |
| 872 |
} |
} |
| 873 |
|
|
| 874 |
switch (ch) |
switch (ch) |
| 875 |
{ |
{ |
| 877 |
log_error("KEY_NULL\n"); |
log_error("KEY_NULL\n"); |
| 878 |
goto cleanup; |
goto cleanup; |
| 879 |
case KEY_TIMEOUT: |
case KEY_TIMEOUT: |
| 880 |
if (time(NULL) - BBS_last_access_tm >= MAX_DELAY_TIME) |
if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time) |
| 881 |
{ |
{ |
| 882 |
log_error("User input timeout\n"); |
log_error("User input timeout\n"); |
| 883 |
goto cleanup; |
goto cleanup; |