/[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.45 - (show annotations)
Tue Nov 4 14:58:56 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.44: +1 -1 lines
Content type: text/x-csrc
Refine file header information comments

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

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