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

Annotation of /lbbs/src/section_list_loader.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.1 - (hide annotations)
Mon May 26 03:42:45 2025 UTC (9 months, 3 weeks ago) by sysadm
Branch: MAIN
Content type: text/x-csrc
Add section_list_loader

1 sysadm 1.1 /***************************************************************************
2     section_list_loader.c - description
3     -------------------
4     Copyright : (C) 2004-2025 by Leaflet
5     Email : leaflet@leafok.com
6     ***************************************************************************/
7    
8     /***************************************************************************
9     * *
10     * 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 *
12     * the Free Software Foundation; either version 3 of the License, or *
13     * (at your option) any later version. *
14     * *
15     ***************************************************************************/
16    
17     #include "section_list_loader.h"
18     #include "log.h"
19     #include "database.h"
20     #include <stdio.h>
21     #include <string.h>
22     #include <errno.h>
23     #include <stdlib.h>
24    
25     int load_section_config_from_db(MYSQL *db)
26     {
27     MYSQL_RES *rs, *rs2;
28     MYSQL_ROW row, row2;
29     char sql[SQL_BUFFER_LEN];
30     int32_t sid;
31     char master_name[BBS_username_max_len + 1];
32     SECTION_LIST *p_section;
33     int ret;
34    
35     snprintf(sql, sizeof(sql),
36     "SELECT section_config.SID, sname, title, section_config.CID, read_user_level, write_user_level, "
37     "section_config.enable * section_class.enable AS enable "
38     "FROM section_config INNER JOIN section_class ON section_config.CID = sectioN_class.CID "
39     "ORDER BY section_config.SID");
40    
41     if (mysql_query(db, sql) != 0)
42     {
43     log_error("Query section_list error: %s\n", mysql_error(db));
44     return -1;
45     }
46     if ((rs = mysql_store_result(db)) == NULL)
47     {
48     log_error("Get section_list data failed\n");
49     return -1;
50     }
51     while ((row = mysql_fetch_row(rs)))
52     {
53     sid = atoi(row[0]);
54    
55     // Query section master
56     snprintf(sql, sizeof(sql),
57     "SELECT username FROM section_master "
58     "INNER JOIN user_list ON section_master.UID = user_list.UID "
59     "WHERE SID = %d AND section_master.enable AND (NOW() BETWEEN begin_dt AND end_dt) "
60     "ORDER BY major DESC LIMIT 1",
61     sid);
62    
63     if (mysql_query(db, sql) != 0)
64     {
65     log_error("Query section_master error: %s\n", mysql_error(db));
66     return -2;
67     }
68     if ((rs2 = mysql_store_result(db)) == NULL)
69     {
70     log_error("Get section_master data failed\n");
71     return -2;
72     }
73     if ((row2 = mysql_fetch_row(rs2)))
74     {
75     strncpy(master_name, row2[0], sizeof(master_name) - 1);
76     master_name[sizeof(master_name) - 1] = '\0';
77     }
78     else
79     {
80     master_name[0] = '\0';
81     }
82     mysql_free_result(rs2);
83    
84     p_section = section_list_find_by_sid(sid);
85    
86     if (p_section == NULL)
87     {
88     p_section = section_list_create(sid, row[1], row[2], "");
89     if (p_section == NULL)
90     {
91     log_error("load_section_config_from_db() error: load new section sid = %d sname = %s\n", sid, row[1]);
92     break;
93     }
94    
95     // acquire rw lock
96     ret = section_list_rw_lock(p_section);
97     if (ret < 0)
98     {
99     break;
100     }
101     }
102     else
103     {
104     // acquire rw lock
105     ret = section_list_rw_lock(p_section);
106     if (ret < 0)
107     {
108     break;
109     }
110    
111     strncpy(p_section->sname, row[1], sizeof(p_section->sname) - 1);
112     p_section->sname[sizeof(p_section->sname) - 1] = '\0';
113     strncpy(p_section->stitle, row[1], sizeof(p_section->stitle) - 1);
114     p_section->stitle[sizeof(p_section->stitle) - 1] = '\0';
115     strncpy(p_section->master_name, master_name, sizeof(p_section->master_name) - 1);
116     p_section->master_name[sizeof(p_section->master_name) - 1] = '\0';
117     }
118    
119     p_section->class_id = atoi(row[3]);
120     p_section->read_user_level = atoi(row[4]);
121     p_section->write_user_level = atoi(row[5]);
122     p_section->enable = (int8_t)atoi(row[6]);
123    
124     // release rw lock
125     ret = section_list_rw_unlock(p_section);
126     if (ret < 0)
127     {
128     break;
129     }
130     }
131     mysql_free_result(rs);
132    
133     return 0;
134     }

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