/[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.12 - (hide annotations)
Fri Dec 19 06:16:26 2025 UTC (2 months, 3 weeks ago) by sysadm
Branch: MAIN
Changes since 1.11: +11 -11 lines
Content type: text/x-csrc
Append \n to the end of logging message by log_...()
Remove ending \n from each logging message

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

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