/[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.49 - (show annotations)
Fri Nov 7 12:02:25 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.48: +1 -0 lines
Content type: text/x-csrc
Fix bug

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

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