/[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.7 - (hide annotations)
Fri Oct 17 01:25:08 2025 UTC (4 months, 4 weeks ago) by sysadm
Branch: MAIN
Changes since 1.6: +0 -1 lines
Content type: text/x-csrc
No longer use igetch_reset() to skip remaining \n after \r
\r\n -> \r and \n -> \r conversions have already been implemented in igetch()

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

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