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

Annotation of /lbbs/src/article_del.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.10 - (hide annotations)
Wed Nov 5 01:04:05 2025 UTC (4 months, 1 week ago) by sysadm
Branch: MAIN
Changes since 1.9: +1 -1 lines
Content type: text/x-csrc
Use enum instead of macro define for constants in bbs.h

1 sysadm 1.8 /* SPDX-License-Identifier: GPL-3.0-or-later */
2     /*
3     * article_del
4     * - user interactive feature to delete article
5     *
6 sysadm 1.9 * Copyright (C) 2004-2025 Leaflet <leaflet@leafok.com>
7 sysadm 1.8 */
8 sysadm 1.1
9     #include "article_del.h"
10     #include "database.h"
11 sysadm 1.5 #include "io.h"
12 sysadm 1.1 #include "log.h"
13     #include "screen.h"
14     #include "user_priv.h"
15 sysadm 1.2 #include <ctype.h>
16 sysadm 1.1 #include <stdlib.h>
17    
18     int article_del(const SECTION_LIST *p_section, const ARTICLE *p_article)
19     {
20     MYSQL *db = NULL;
21     MYSQL_RES *rs = NULL;
22     MYSQL_ROW row;
23     char sql[SQL_BUFFER_LEN];
24     int ret = 0;
25     int ch;
26     int article_visible = 0;
27     int article_excerption = 0;
28     int exp_change = 0;
29    
30     if (p_section == NULL || p_article == NULL)
31     {
32     log_error("NULL pointer error\n");
33     }
34    
35 sysadm 1.3 if (p_article->excerption) // Delete is not allowed
36     {
37     clearscr();
38     moveto(1, 1);
39 sysadm 1.6 prints("该文章无法被删除,请联系版主。");
40 sysadm 1.3 press_any_key();
41    
42     return 0;
43     }
44    
45 sysadm 1.1 clearscr();
46     moveto(1, 1);
47 sysadm 1.6 prints("真的要删除文章?(Y)是, (N)否 [N]: ");
48 sysadm 1.1 iflush();
49    
50 sysadm 1.10 for (ch = 0; !SYS_server_exit; ch = igetch_t(BBS_max_user_idle_time))
51 sysadm 1.1 {
52     switch (toupper(ch))
53     {
54 sysadm 1.4 case KEY_NULL:
55     case KEY_TIMEOUT:
56     goto cleanup;
57 sysadm 1.1 case CR:
58     case KEY_ESC:
59     case 'N':
60 sysadm 1.3 return 0;
61 sysadm 1.1 case 'Y':
62     break;
63     default: // Invalid selection
64     continue;
65     }
66    
67     break;
68     }
69    
70 sysadm 1.4 if (SYS_server_exit) // Do not save data on shutdown
71     {
72     goto cleanup;
73     }
74    
75 sysadm 1.1 db = db_open();
76     if (db == NULL)
77     {
78     log_error("db_open() error: %s\n", mysql_error(db));
79     ret = -1;
80     goto cleanup;
81     }
82    
83     // Begin transaction
84     if (mysql_query(db, "SET autocommit=0") != 0)
85     {
86     log_error("SET autocommit=0 error: %s\n", mysql_error(db));
87     ret = -1;
88     goto cleanup;
89     }
90    
91     if (mysql_query(db, "BEGIN") != 0)
92     {
93     log_error("Begin transaction error: %s\n", mysql_error(db));
94     ret = -1;
95     goto cleanup;
96     }
97    
98     snprintf(sql, sizeof(sql),
99     "SELECT visible, excerption FROM bbs WHERE AID = %d FOR UPDATE",
100     p_article->aid);
101    
102     if (mysql_query(db, sql) != 0)
103     {
104     log_error("Query article status error: %s\n", mysql_error(db));
105     ret = -1;
106     goto cleanup;
107     }
108     if ((rs = mysql_use_result(db)) == NULL)
109     {
110     log_error("Get article status data failed\n");
111     ret = -1;
112     goto cleanup;
113     }
114    
115     if ((row = mysql_fetch_row(rs)))
116     {
117     article_visible = atoi(row[0]);
118     article_excerption = atoi(row[1]);
119     }
120     mysql_free_result(rs);
121     rs = NULL;
122    
123     if (!article_visible) // Already deleted
124     {
125     mysql_close(db);
126     db = NULL;
127    
128     clearscr();
129     moveto(1, 1);
130 sysadm 1.6 prints("该文章已被删除,请稍后刷新列表。");
131 sysadm 1.1 press_any_key();
132    
133     goto cleanup;
134     }
135    
136     if (article_excerption) // Delete is not allowed
137     {
138     clearscr();
139     moveto(1, 1);
140 sysadm 1.6 prints("该文章无法被删除,请联系版主。");
141 sysadm 1.1 press_any_key();
142    
143     goto cleanup;
144     }
145    
146     // Update article(s)
147     snprintf(sql, sizeof(sql),
148     "UPDATE bbs SET visible = 0, reply_count = 0, m_del = %d "
149     "WHERE (AID = %d OR TID = %d) AND visible",
150     (p_article->uid == BBS_priv.uid ? 0 : 1),
151     p_article->aid, p_article->aid);
152    
153     if (mysql_query(db, sql) != 0)
154     {
155     log_error("Update article status error: %s\n", mysql_error(db));
156     ret = -1;
157     goto cleanup;
158     }
159    
160     // Update exp
161     if (p_article->uid == BBS_priv.uid)
162     {
163     exp_change = (p_article->tid == 0 ? -20 : -5);
164     }
165     else
166     {
167     exp_change = (p_article->tid == 0 ? -50 : -15);
168     }
169    
170     snprintf(sql, sizeof(sql),
171     "UPDATE user_pubinfo SET exp = exp + %d WHERE UID = %d",
172     exp_change, BBS_priv.uid);
173    
174     if (mysql_query(db, sql) != 0)
175     {
176     log_error("Update exp error: %s\n", mysql_error(db));
177     ret = -1;
178     goto cleanup;
179     }
180    
181     // Add log
182     snprintf(sql, sizeof(sql),
183     "INSERT INTO bbs_article_op(AID, UID, type, op_dt, op_ip)"
184     "VALUES(%d, %d, '%c', NOW(), '%s')",
185     p_article->aid, BBS_priv.uid,
186     (p_article->uid == BBS_priv.uid ? 'D' : 'X'),
187     hostaddr_client);
188    
189     if (mysql_query(db, sql) != 0)
190     {
191     log_error("Add log error: %s\n", mysql_error(db));
192     ret = -1;
193     goto cleanup;
194     }
195    
196     // Set reply count
197     if (p_article->tid != 0)
198     {
199     snprintf(sql, sizeof(sql),
200     "UPDATE bbs SET reply_count = reply_count - 1 WHERE AID = %d",
201     p_article->tid);
202    
203     if (mysql_query(db, sql) != 0)
204     {
205     log_error("Update article error: %s\n", mysql_error(db));
206     ret = -1;
207     goto cleanup;
208     }
209     }
210    
211     // Commit transaction
212     if (mysql_query(db, "COMMIT") != 0)
213     {
214     log_error("Commit transaction error: %s\n", mysql_error(db));
215     ret = -1;
216     goto cleanup;
217     }
218    
219     mysql_close(db);
220     db = NULL;
221    
222     clearscr();
223     moveto(1, 1);
224 sysadm 1.6 prints("删除成功,请在%d秒后刷新列表。", BBS_section_list_load_interval);
225 sysadm 1.1 press_any_key();
226     ret = 1; // Success
227    
228     cleanup:
229     mysql_free_result(rs);
230     mysql_close(db);
231    
232     return ret;
233     }

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