| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
bbs_net.c - description |
bbs_net.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 18 2004 |
Copyright : (C) 2004-2025 by Leaflet |
| 5 |
copyright : (C) 2004 by Leaflet |
Email : leaflet@leafok.com |
|
email : leaflet@leafok.com |
|
| 6 |
***************************************************************************/ |
***************************************************************************/ |
| 7 |
|
|
| 8 |
/*************************************************************************** |
/*************************************************************************** |
| 9 |
* * |
* * |
| 10 |
* This program is free software; you can redistribute it and/or modify * |
* This program is free software; you can redistribute it and/or modify * |
| 11 |
* it under the terms of the GNU General Public License as published by * |
* it under the terms of the GNU General Public License as published by * |
| 12 |
* the Free Software Foundation; either version 2 of the License, or * |
* the Free Software Foundation; either version 3 of the License, or * |
| 13 |
* (at your option) any later version. * |
* (at your option) any later version. * |
| 14 |
* * |
* * |
| 15 |
***************************************************************************/ |
***************************************************************************/ |
| 41 |
char host1[20]; |
char host1[20]; |
| 42 |
char host2[40]; |
char host2[40]; |
| 43 |
char ip[40]; |
char ip[40]; |
| 44 |
int port; |
in_port_t port; |
| 45 |
} bbsnet_conf[MAXSTATION]; |
} bbsnet_conf[MAXSTATION]; |
| 46 |
|
|
| 47 |
MENU_SET bbsnet_menu; |
MENU_SET bbsnet_menu; |
| 51 |
FILE *fp; |
FILE *fp; |
| 52 |
MENU *p_menu; |
MENU *p_menu; |
| 53 |
MENU_ITEM *p_menuitem; |
MENU_ITEM *p_menuitem; |
| 54 |
char t[256], *t1, *t2, *t3, *t4; |
char t[256], *t1, *t2, *t3, *t4, *saveptr; |
| 55 |
int item_count = 0; |
int item_count = 0; |
| 56 |
|
|
| 57 |
fp = fopen(file_config, "r"); |
fp = fopen(file_config, "r"); |
| 58 |
if (fp == NULL) |
if (fp == NULL) |
| 59 |
|
{ |
| 60 |
return -1; |
return -1; |
| 61 |
|
} |
| 62 |
|
|
| 63 |
p_menu = bbsnet_menu.p_menu[0] = malloc(sizeof(MENU)); |
p_menu = bbsnet_menu.p_menu[0] = malloc(sizeof(MENU)); |
| 64 |
strcpy(p_menu->name, "BBSNET"); |
strncpy(p_menu->name, "BBSNET", sizeof(p_menu->name) - 1); |
| 65 |
|
p_menu->name[sizeof(p_menu->name) - 1] = '\0'; |
| 66 |
p_menu->title.show = 0; |
p_menu->title.show = 0; |
| 67 |
p_menu->screen.show = 0; |
p_menu->screen.show = 0; |
| 68 |
|
|
| 69 |
while (fgets(t, 255, fp) && item_count < MAXSTATION) |
while (fgets(t, 255, fp) && item_count < MAXSTATION) |
| 70 |
{ |
{ |
| 71 |
t1 = strtok(t, " \t"); |
t1 = strtok_r(t, " \t", &saveptr); |
| 72 |
t2 = strtok(NULL, " \t\n"); |
t2 = strtok_r(NULL, " \t\n", &saveptr); |
| 73 |
t3 = strtok(NULL, " \t\n"); |
t3 = strtok_r(NULL, " \t\n", &saveptr); |
| 74 |
t4 = strtok(NULL, " \t\n"); |
t4 = strtok_r(NULL, " \t\n", &saveptr); |
| 75 |
|
|
| 76 |
if (t1[0] == '#' || t1[0] == '*' || t1 == NULL || t2 == NULL || t3 == NULL) |
if (t1 == NULL || t2 == NULL || t3 == NULL || t[0] == '#' || t[0] == '*') |
| 77 |
|
{ |
| 78 |
continue; |
continue; |
| 79 |
strncpy(bbsnet_conf[item_count].host1, t2, 18); |
} |
| 80 |
bbsnet_conf[item_count].host1[18] = 0; |
|
| 81 |
strncpy(bbsnet_conf[item_count].host2, t1, 36); |
strncpy(bbsnet_conf[item_count].host1, t2, sizeof(bbsnet_conf[item_count].host1) - 1); |
| 82 |
bbsnet_conf[item_count].host2[36] = 0; |
bbsnet_conf[item_count].host1[sizeof(bbsnet_conf[item_count].host1) - 1] = '\0'; |
| 83 |
strncpy(bbsnet_conf[item_count].ip, t3, 36); |
strncpy(bbsnet_conf[item_count].host2, t1, sizeof(bbsnet_conf[item_count].host2) - 1); |
| 84 |
bbsnet_conf[item_count].ip[36] = 0; |
bbsnet_conf[item_count].host2[sizeof(bbsnet_conf[item_count].host2) - 1] = '\0'; |
| 85 |
bbsnet_conf[item_count].port = t4 ? atoi(t4) : 23; |
strncpy(bbsnet_conf[item_count].ip, t3, sizeof(bbsnet_conf[item_count].ip) - 1); |
| 86 |
|
bbsnet_conf[item_count].ip[sizeof(bbsnet_conf[item_count].ip) - 1] = '\0'; |
| 87 |
|
bbsnet_conf[item_count].port = (in_port_t)(t4 ? atoi(t4) : 23); |
| 88 |
|
|
| 89 |
p_menuitem = p_menu->items[item_count] = malloc(sizeof(MENU_ITEM)); |
p_menuitem = p_menu->items[item_count] = malloc(sizeof(MENU_ITEM)); |
| 90 |
p_menuitem->row = 2 + item_count / STATION_PER_LINE; |
p_menuitem->row = 2 + item_count / STATION_PER_LINE; |
| 91 |
p_menuitem->col = 5 + item_count % STATION_PER_LINE * 20; |
p_menuitem->col = 5 + item_count % STATION_PER_LINE * 20; |
| 92 |
sprintf(p_menuitem->action, "%d", item_count); |
snprintf(p_menuitem->action, sizeof(p_menuitem->action), "%d", item_count); |
| 93 |
p_menuitem->submenu = 0; |
p_menuitem->submenu = 0; |
| 94 |
p_menuitem->priv = 0; |
p_menuitem->priv = 0; |
| 95 |
p_menuitem->level = 0; |
p_menuitem->level = 0; |
| 96 |
p_menuitem->display = 0; |
p_menuitem->display = 0; |
| 97 |
p_menuitem->name[0] = |
p_menuitem->name[0] = |
| 98 |
(item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count); |
(char)(item_count < MAXSTATION / 2 ? 'A' + item_count : 'a' + item_count); |
| 99 |
p_menuitem->name[1] = '\0'; |
p_menuitem->name[1] = '\0'; |
| 100 |
sprintf(p_menuitem->text, "[1;36m%c.[m %s", |
snprintf(p_menuitem->text, sizeof(p_menuitem->text), "[1;36m%c.[m %s", |
| 101 |
p_menuitem->name[0], bbsnet_conf[item_count].host1); |
p_menuitem->name[0], bbsnet_conf[item_count].host1); |
| 102 |
|
|
| 103 |
item_count++; |
item_count++; |
| 104 |
} |
} |
| 114 |
return 0; |
return 0; |
| 115 |
} |
} |
| 116 |
|
|
| 117 |
static void |
static void process_bar(int n, int len) |
|
process_bar(int n, int len) |
|
| 118 |
{ |
{ |
| 119 |
char buf[256]; |
char buf[256]; |
| 120 |
char buf2[256]; |
char buf2[256]; |
| 123 |
char *ptr3; |
char *ptr3; |
| 124 |
|
|
| 125 |
moveto(4, 0); |
moveto(4, 0); |
| 126 |
prints("┌───────────────┐\r\n"); |
prints(" ------------------------------ \r\n"); |
| 127 |
sprintf(buf2, " %3d%% ", n * 100 / len); |
snprintf(buf2, sizeof(buf2), " %3d%% ", n * 100 / len); |
| 128 |
ptr = buf; |
ptr = buf; |
| 129 |
ptr2 = buf2; |
ptr2 = buf2; |
| 130 |
ptr3 = buf + n; |
ptr3 = buf + n; |
| 138 |
while (*ptr2 != '\0') |
while (*ptr2 != '\0') |
| 139 |
*ptr++ = *ptr2++; |
*ptr++ = *ptr2++; |
| 140 |
*ptr++ = '\0'; |
*ptr++ = '\0'; |
| 141 |
prints("│\033[46m%s\033[m│\r\n", buf); |
prints("|\033[46m%s\033[m|\r\n", buf); |
| 142 |
prints("└───────────────┘\r\n"); |
prints(" ------------------------------ \r\n"); |
| 143 |
iflush(); |
iflush(); |
| 144 |
} |
} |
| 145 |
|
|
| 146 |
int bbsnet_connect(int n) |
int bbsnet_connect(int n) |
| 147 |
{ |
{ |
| 148 |
int sock, ch, result, len, loop; |
int sock, result, loop; |
| 149 |
|
ssize_t len; |
| 150 |
struct sockaddr_in sin; |
struct sockaddr_in sin; |
| 151 |
char buf[256]; |
char buf[256]; |
| 152 |
fd_set inputs, testfds; |
fd_set inputs, testfds; |
| 153 |
struct timeval timeout; |
struct timeval timeout; |
| 154 |
struct hostent *pHost = NULL; |
struct hostent *pHost = NULL; |
| 155 |
int rc, rv, tos = 020, i; |
int rv, tos = 020, i; |
| 156 |
char remote_addr[256]; |
char remote_addr[256]; |
| 157 |
int remote_port; |
int remote_port; |
| 158 |
time_t t_used; |
time_t t_used; |
| 187 |
|
|
| 188 |
sin.sin_family = AF_INET; |
sin.sin_family = AF_INET; |
| 189 |
sin.sin_addr.s_addr = |
sin.sin_addr.s_addr = |
| 190 |
(strlen(hostaddr_server) > 0 ? inet_addr(hostaddr_server) : INADDR_ANY); |
(strnlen(hostaddr_server, sizeof(hostaddr_server)) > 0 ? inet_addr(hostaddr_server) : INADDR_ANY); |
| 191 |
sin.sin_port = 0; |
sin.sin_port = 0; |
| 192 |
|
|
| 193 |
if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) < 0) |
| 202 |
sin.sin_addr = *(struct in_addr *)pHost->h_addr_list[0]; |
sin.sin_addr = *(struct in_addr *)pHost->h_addr_list[0]; |
| 203 |
sin.sin_port = htons(bbsnet_conf[n].port); |
sin.sin_port = htons(bbsnet_conf[n].port); |
| 204 |
|
|
| 205 |
strcpy(remote_addr, inet_ntoa(sin.sin_addr)); |
strncpy(remote_addr, inet_ntoa(sin.sin_addr), sizeof(remote_addr) - 1); |
| 206 |
|
remote_addr[sizeof(remote_addr) - 1] = '\0'; |
| 207 |
remote_port = ntohs(sin.sin_port); |
remote_port = ntohs(sin.sin_port); |
| 208 |
|
|
| 209 |
prints("\033[1;32m穿梭进度条提示您当前已使用的时间。\033[m\r\n"); |
prints("\033[1;32m穿梭进度条提示您当前已使用的时间。\033[m\r\n"); |
| 224 |
continue; |
continue; |
| 225 |
} |
} |
| 226 |
else if (rv == 0) |
else if (rv == 0) |
| 227 |
|
{ |
| 228 |
break; |
break; |
| 229 |
|
} |
| 230 |
else |
else |
| 231 |
{ |
{ |
| 232 |
prints("\033[1;31m连接失败!\033[m\r\n"); |
prints("\033[1;31m连接失败!\033[m\r\n"); |
| 283 |
{ |
{ |
| 284 |
loop = 0; |
loop = 0; |
| 285 |
} |
} |
| 286 |
write(sock, buf, len); |
write(sock, buf, (size_t)len); |
| 287 |
} |
} |
| 288 |
if (FD_ISSET(sock, &testfds)) |
if (FD_ISSET(sock, &testfds)) |
| 289 |
{ |
{ |
| 292 |
{ |
{ |
| 293 |
loop = 0; |
loop = 0; |
| 294 |
} |
} |
| 295 |
write(1, buf, len); |
write(1, buf, (size_t)len); |
| 296 |
} |
} |
| 297 |
BBS_last_access_tm = time(0); |
BBS_last_access_tm = time(0); |
| 298 |
} |
} |
| 316 |
static int |
static int |
| 317 |
bbsnet_refresh() |
bbsnet_refresh() |
| 318 |
{ |
{ |
|
int i; |
|
|
|
|
| 319 |
clearscr(); |
clearscr(); |
| 320 |
moveto(1, 0); |
moveto(1, 0); |
| 321 |
prints("╭══════════════════════════════════════╮"); |
prints(" ----------------------------------------------------------------------------- "); |
| 322 |
for (i = 2; i < 19; i++) |
for (int i = 2; i < 19; i++) |
| 323 |
{ |
{ |
| 324 |
moveto(i, 0); |
moveto(i, 0); |
| 325 |
prints("║"); |
prints("|"); |
| 326 |
moveto(i, 79); |
moveto(i, 79); |
| 327 |
prints("║"); |
prints("|"); |
| 328 |
} |
} |
| 329 |
moveto(19, 0); |
moveto(19, 0); |
| 330 |
prints("║——————————————————————————————————————║"); |
prints("|-----------------------------------------------------------------------------|"); |
| 331 |
moveto(22, 0); |
moveto(22, 0); |
| 332 |
prints("╰══════════════════════════════════════╯"); |
prints(" ----------------------------------------------------------------------------- "); |
| 333 |
moveto(23, 0); |
moveto(23, 0); |
| 334 |
prints(" [\x1b[1;32mCtrl+C\x1b[m]退出"); |
prints(" [\x1b[1;32mCtrl+C\x1b[m]退出"); |
| 335 |
|
|
| 342 |
{ |
{ |
| 343 |
moveto(20, 0); |
moveto(20, 0); |
| 344 |
clrtoeol(); |
clrtoeol(); |
| 345 |
prints("║\x1b[1m单位:\x1b[1;33m%-18s\x1b[m 站名:\x1b[1;33m%s\x1b[m", |
prints("|\x1b[1m单位:\x1b[1;33m%-18s\x1b[m 站名:\x1b[1;33m%s\x1b[m", |
| 346 |
bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1); |
bbsnet_conf[new_pos].host2, bbsnet_conf[new_pos].host1); |
| 347 |
moveto(20, 79); |
moveto(20, 79); |
| 348 |
prints("║"); |
prints("|"); |
| 349 |
moveto(21, 0); |
moveto(21, 0); |
| 350 |
clrtoeol(); |
clrtoeol(); |
| 351 |
prints("║\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip); |
prints("|\x1b[1m连往:\x1b[1;33m%-20s", bbsnet_conf[new_pos].ip); |
| 352 |
if (bbsnet_conf[new_pos].port != 23) |
if (bbsnet_conf[new_pos].port != 23) |
| 353 |
|
{ |
| 354 |
prints(" %d", bbsnet_conf[new_pos].port); |
prints(" %d", bbsnet_conf[new_pos].port); |
| 355 |
|
} |
| 356 |
prints("\x1b[m"); |
prints("\x1b[m"); |
| 357 |
moveto(21, 79); |
moveto(21, 79); |
| 358 |
prints("║"); |
prints("|"); |
| 359 |
iflush(); |
iflush(); |
| 360 |
|
|
| 361 |
return 0; |
return 0; |
| 364 |
int bbs_net() |
int bbs_net() |
| 365 |
{ |
{ |
| 366 |
int ch, pos, i; |
int ch, pos, i; |
|
char file_config[256]; |
|
| 367 |
|
|
| 368 |
strcpy(file_config, app_home_dir); |
load_bbsnet_conf(CONF_BBSNET); |
|
strcat(file_config, "conf/bbsnet.conf"); |
|
|
|
|
|
load_bbsnet_conf(file_config); |
|
| 369 |
|
|
| 370 |
BBS_last_access_tm = time(0); |
BBS_last_access_tm = time(0); |
| 371 |
|
|
| 398 |
break; |
break; |
| 399 |
case KEY_UP: |
case KEY_UP: |
| 400 |
for (i = 0; i < STATION_PER_LINE; i++) |
for (i = 0; i < STATION_PER_LINE; i++) |
| 401 |
|
{ |
| 402 |
menu_control(&bbsnet_menu, KEY_UP); |
menu_control(&bbsnet_menu, KEY_UP); |
| 403 |
|
} |
| 404 |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
| 405 |
bbsnet_selchange(pos); |
bbsnet_selchange(pos); |
| 406 |
break; |
break; |
| 407 |
case KEY_DOWN: |
case KEY_DOWN: |
| 408 |
for (i = 0; i < STATION_PER_LINE; i++) |
for (i = 0; i < STATION_PER_LINE; i++) |
| 409 |
|
{ |
| 410 |
menu_control(&bbsnet_menu, KEY_DOWN); |
menu_control(&bbsnet_menu, KEY_DOWN); |
| 411 |
|
} |
| 412 |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
pos = bbsnet_menu.p_menu[0]->item_cur_pos; |
| 413 |
bbsnet_selchange(pos); |
bbsnet_selchange(pos); |
| 414 |
break; |
break; |