/[LeafOK_CVS]/lbbs/src/menu_proc.c
ViewVC logotype

Contents of /lbbs/src/menu_proc.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.48 - (show annotations)
Fri Nov 7 09:53:47 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.47: +11 -3 lines
Content type: text/x-csrc
Refine shutdown: add confirmation

1 /* SPDX-License-Identifier: GPL-3.0-or-later */
2 /*
3 * menu_proc
4 * - handler functions of menu commands
5 *
6 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 */
8
9 #include "article_cache.h"
10 #include "article_favor_display.h"
11 #include "article_view_log.h"
12 #include "bbs.h"
13 #include "bbs_cmd.h"
14 #include "common.h"
15 #include "io.h"
16 #include "log.h"
17 #include "login.h"
18 #include "menu.h"
19 #include "section_list_display.h"
20 #include "screen.h"
21 #include "user_info_update.h"
22 #include "user_list_display.h"
23 #include "user_priv.h"
24 #include <dlfcn.h>
25 #include <errno.h>
26 #include <signal.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <time.h>
30 #include <unistd.h>
31 #include <sys/types.h>
32
33 int list_section(void *param)
34 {
35 section_list_display(param, 0);
36
37 return REDRAW;
38 }
39
40 typedef union exec_handler_t
41 {
42 void *p;
43 int (*handler)();
44 } exec_handler;
45
46 int exec_mbem(void *param)
47 {
48 void *hdll;
49 exec_handler func;
50 char *c, *s;
51 char buf[FILE_PATH_LEN];
52
53 strncpy(buf, (const char *)param, sizeof(buf) - 1);
54 buf[sizeof(buf) - 1] = '\0';
55
56 s = strstr(buf, "@mod:");
57 if (s)
58 {
59 c = strstr(s + 5, "#");
60 if (c)
61 {
62 *c = 0;
63 c++;
64 }
65
66 hdll = dlopen(s + 5, RTLD_LAZY);
67
68 if (hdll)
69 {
70 char *error;
71
72 if ((func.p = dlsym(hdll, c ? c : "mod_main")) != NULL)
73 {
74 func.handler();
75 }
76 else if ((error = dlerror()) != NULL)
77 {
78 clearscr();
79 prints("%s\r\n", error);
80 press_any_key();
81 }
82 dlclose(hdll);
83 }
84 else
85 {
86 clearscr();
87 prints("加载库文件 [%s] 失败!!\r\n", s + 5);
88 prints("失败原因:%s\r\n", dlerror());
89 press_any_key();
90 }
91 }
92
93 return REDRAW;
94 }
95
96 int exit_bbs(void *param)
97 {
98 return EXITBBS;
99 }
100
101 int license(void *param)
102 {
103 display_file(DATA_LICENSE, 0);
104
105 return REDRAW;
106 }
107
108 int copyright(void *param)
109 {
110 display_file(DATA_COPYRIGHT, 1);
111
112 return REDRAW;
113 }
114
115 int version(void *param)
116 {
117 display_file(DATA_VERSION, 1);
118
119 return REDRAW;
120 }
121
122 int reload_bbs_conf(void *param)
123 {
124 clearscr();
125
126 if (kill(getppid(), SIGHUP) < 0)
127 {
128 log_error("Send SIGHUP signal failed (%d)\n", errno);
129
130 prints("发送指令失败\r\n");
131 }
132 else
133 {
134 prints("已发送指令\r\n");
135 }
136
137 press_any_key();
138
139 return REDRAW;
140 }
141
142 int shutdown_bbs(void *param)
143 {
144 char buf[2] = "N";
145
146 clearscr();
147 get_data(1, 1, "真的要关闭系统吗[y/N]? ", buf, sizeof(buf), 1);
148
149 if (toupper(buf[0]) == 'Y')
150 {
151 log_common("Notify main process to exit by [%s]\n", BBS_username);
152
153 if (kill(getppid(), SIGTERM) < 0)
154 {
155 log_error("Send SIGTERM signal failed (%d)\n", errno);
156 }
157 }
158
159 return REDRAW;
160 }
161
162 int favor_section_filter(void *param)
163 {
164 MENU_ITEM *p_menu_item = param;
165
166 return (is_favor(&BBS_priv, p_menu_item->priv) && checklevel2(&BBS_priv, p_menu_item->level));
167 }
168
169 static int display_ex_article_key_handler(int *p_key, DISPLAY_CTX *p_ctx)
170 {
171 switch (*p_key)
172 {
173 case 0: // Set msg
174 snprintf(p_ctx->msg, sizeof(p_ctx->msg),
175 "| 返回[\033[32m←\033[33m,\033[32mESC\033[33m] | "
176 "移动[\033[32m↑\033[33m/\033[32m↓\033[33m/\033[32mPgUp\033[33m/\033[32mPgDn\033[33m] | "
177 "帮助[\033[32mh\033[33m] |");
178 break;
179 }
180
181 return 0;
182 }
183
184 int view_ex_article(void *param)
185 {
186 ARTICLE_CACHE cache;
187 ARTICLE *p_article;
188 int32_t aid = atoi(param);
189 int ret;
190
191 (void)ret;
192
193 p_article = article_block_find_by_aid(aid);
194 if (p_article == NULL)
195 {
196 log_error("article_block_find_by_aid(%d) error\n", aid);
197 return NOREDRAW;
198 }
199
200 if (article_cache_load(&cache, VAR_ARTICLE_CACHE_DIR, p_article) < 0)
201 {
202 log_error("article_cache_load(aid=%d, cid=%d) error\n", p_article->aid, p_article->cid);
203 return NOREDRAW;
204 }
205
206 if (user_online_update("VIEW_ARTICLE") < 0)
207 {
208 log_error("user_online_update(VIEW_ARTICLE) error\n");
209 }
210
211 ret = display_data(cache.p_data, cache.line_total, cache.line_offsets, 0,
212 display_ex_article_key_handler, DATA_READ_HELP);
213
214 if (article_cache_unload(&cache) < 0)
215 {
216 log_error("article_cache_unload(aid=%d, cid=%d) error\n", p_article->aid, p_article->cid);
217 }
218
219 // Update article_view_log
220 if (article_view_log_set_viewed(p_article->aid, &BBS_article_view_log) < 0)
221 {
222 log_error("article_view_log_set_viewed(aid=%d) error\n", p_article->aid);
223 }
224
225 return REDRAW;
226 }
227
228 int list_ex_section(void *param)
229 {
230 SECTION_LIST *p_section;
231
232 p_section = section_list_find_by_name(param);
233 if (p_section == NULL)
234 {
235 log_error("Section %s not found\n", (const char *)param);
236 return -1;
237 }
238
239 if (section_list_ex_dir_display(p_section) < 0)
240 {
241 log_error("section_list_ex_dir_display(sid=%d) error\n", p_section->sid);
242 }
243
244 return REDRAW;
245 }
246
247 int show_top10_menu(void *param)
248 {
249 static int show_top10 = 0;
250 int ch = 0;
251
252 if (show_top10)
253 {
254 return NOREDRAW;
255 }
256 show_top10 = 1;
257
258 clearscr();
259 show_top("", BBS_name, "");
260 show_bottom("");
261
262 if (display_menu(&top10_menu) == 0)
263 {
264 while (!SYS_server_exit)
265 {
266 iflush();
267 ch = igetch(100);
268
269 if (ch != KEY_NULL && ch != KEY_TIMEOUT)
270 {
271 BBS_last_access_tm = time(NULL);
272 }
273
274 switch (ch)
275 {
276 case KEY_NULL: // broken pipe
277 log_error("KEY_NULL\n");
278 show_top10 = 0;
279 return 0;
280 case KEY_TIMEOUT:
281 if (time(NULL) - BBS_last_access_tm >= BBS_max_user_idle_time)
282 {
283 log_error("User input timeout\n");
284 show_top10 = 0;
285 return 0;
286 }
287 continue;
288 case CR:
289 default:
290 switch (menu_control(&top10_menu, ch))
291 {
292 case EXITMENU:
293 ch = EXITMENU;
294 break;
295 case REDRAW:
296 clearscr();
297 show_bottom("");
298 display_menu(&top10_menu);
299 break;
300 case NOREDRAW:
301 case UNKNOWN_CMD:
302 default:
303 break;
304 }
305 }
306
307 if (ch == EXITMENU)
308 {
309 break;
310 }
311 }
312 }
313
314 show_top10 = 0;
315 return REDRAW;
316 }
317
318 int locate_article(void *param)
319 {
320 char buf[MAX_MENUITEM_NAME_LENGTH];
321 char *sname, *aid, *saveptr;
322
323 strncpy(buf, param, sizeof(buf) - 1);
324 buf[sizeof(buf) - 1] = '\0';
325
326 sname = strtok_r(buf, "|", &saveptr);
327 aid = strtok_r(NULL, "|", &saveptr);
328
329 if (sname == NULL || aid == NULL)
330 {
331 log_error("top10_locate(%s) error: invalid parameter\n", param);
332 return NOREDRAW;
333 }
334
335 section_list_display(sname, atoi(aid));
336
337 return REDRAW;
338 }
339
340 int favor_topic(void *param)
341 {
342 if (article_favor_display(&BBS_article_favor) < 0)
343 {
344 log_error("article_favor_display() error\n");
345 }
346
347 return REDRAW;
348 }
349
350 int list_user(void *param)
351 {
352 if (user_list_display(0) < 0)
353 {
354 log_error("user_list_display(all_user) error\n");
355 }
356
357 return REDRAW;
358 }
359
360 int list_online_user(void *param)
361 {
362 if (user_list_display(1) < 0)
363 {
364 log_error("user_list_display(online_user) error\n");
365 }
366
367 return REDRAW;
368 }
369
370 int edit_intro(void *param)
371 {
372 if (user_intro_edit(BBS_priv.uid) < 0)
373 {
374 log_error("user_intro_edit(%d) error\n", BBS_priv.uid);
375 }
376
377 return REDRAW;
378 }

webmaster@leafok.com
ViewVC Help
Powered by ViewVC 1.3.0-beta1