-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathannounces.cpp
299 lines (280 loc) · 9.93 KB
/
announces.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/***************************************************************************
announces.cpp - board announces support
-------------------
begin : Mon Feb 24 2003
copyright : (C) 2001 - 2003 by Alexander Bilichenko
email : [email protected]
***************************************************************************/
#include "announces.h"
#include "error.h"
static FILE * CreateAnnounceFile()
{
FILE *f;
f = fopen(F_GLOBAL_ANN, FILE_ACCESS_MODES_CW);
if(f) {
// announces number starting from 1
DWORD x = 1;
// write starting unique id
if(fwrite(&x, 1, sizeof(x), f) != sizeof(x)) {
fclose(f);
f = NULL;
}
else {
fclose(f);
f = fopen(F_GLOBAL_ANN, FILE_ACCESS_MODES_RW);
}
}
return f;
}
int ReadLastAnnounceNumber(DWORD *an)
{
FILE *f;
int ret = 0;
f = fopen(F_GLOBAL_ANN, FILE_ACCESS_MODES_RW);
if(!f) return 0;
if(fread(an, 1, sizeof(DWORD), f) == sizeof(DWORD))
ret = 1;
fclose(f);
return ret;
}
int PostGlobalAnnounce(const char *username, DWORD uniqid, const char *announce, DWORD ttl, DWORD flags)
{
FILE *f;
DWORD siz;
SGlobalAnnounce an;
DWORD id;
if((f = wcfopen(F_GLOBAL_ANN, FILE_ACCESS_MODES_RW)) == NULL) {
if((f = CreateAnnounceFile()) == NULL) {
return ANNOUNCES_RETURN_IO_ERROR;
}
}
// prepare announce structure
strcpy(an.Announce, announce);
strcpy(an.From, username);
an.UIdFrom = uniqid;
an.TTL = ttl;
an.Date = time(NULL);
an.Flags = flags;
memset(an.Reserved, 0, sizeof(an.Reserved));
// write announce
lock_file(f);
if(!fCheckedRead(&id, sizeof(id), f)) {
unlock_file(f);
wcfclose(f);
return ANNOUNCES_RETURN_DB_ERROR;
}
// save number
an.Number = id;
// increment number
id++;
if(wcfseek(f, 0, SEEK_SET) != 0) {
unlock_file(f);
wcfclose(f);
return ANNOUNCES_RETURN_IO_ERROR;
}
if(!fCheckedWrite(&id, sizeof(id), f)) {
unlock_file(f);
wcfclose(f);
return ANNOUNCES_RETURN_IO_ERROR;
}
if(wcfseek(f, 0, SEEK_END) != 0) {
unlock_file(f);
wcfclose(f);
return ANNOUNCES_RETURN_IO_ERROR;
}
// start transcation
siz = wcftell(f);
if(!fCheckedWrite(&an, sizeof(SGlobalAnnounce), f)) {
// rolling back
if (wcfflush(f) || ftruncate(fileno(f), siz))
printhtmlerror();
id--;
if(wcfseek(f, 0, SEEK_END) == 0) {
wcfwrite(&id, 1, sizeof(id), f);
}
unlock_file(f);
wcfclose(f);
return ANNOUNCES_RETURN_IO_ERROR;
}
unlock_file(f);
wcfclose(f);
return ANNOUNCES_RETURN_OK;
}
int ReadGlobalAnnounces(SGlobalAnnounce **ga, DWORD *cnt)
{
FILE *f;
DWORD i = 0, rd;
SGlobalAnnounce *an;
*ga = NULL;
*cnt = 0;
an = (SGlobalAnnounce*)malloc(sizeof(SGlobalAnnounce));
if(!an) return ANNOUNCES_RETURN_LOW_RES;
if((f = fopen(F_GLOBAL_ANN, FILE_ACCESS_MODES_R)) == NULL) {
if((f = CreateAnnounceFile()) == NULL) {
free(an);
return ANNOUNCES_RETURN_IO_ERROR;
}
}
if(wcfseek(f, sizeof(DWORD), SEEK_SET) != 0) {
free(an);
return ANNOUNCES_RETURN_IO_ERROR;
}
while(!feof(f))
{
if((rd = fread(&(an[i]), 1, sizeof(SGlobalAnnounce), f)) != sizeof(SGlobalAnnounce)) {
if(rd != 0) {
free(an);
fclose(f);
return ANNOUNCES_RETURN_DB_ERROR;
}
else break;
}
i++;
an = (SGlobalAnnounce*)realloc(an, (i+1)*sizeof(SGlobalAnnounce));
}
fclose(f);
*cnt = i;
*ga = an;
return ANNOUNCES_RETURN_OK;
}
int DeleteGlobalAnnounce(DWORD id, DWORD uniqid)
{
WCFILE *f;
DWORD pos, fsize, rd;
SGlobalAnnounce *an;
an = (SGlobalAnnounce*)malloc(sizeof(SGlobalAnnounce));
if(!an) return ANNOUNCES_RETURN_LOW_RES;
if((f = wcfopen(F_GLOBAL_ANN, FILE_ACCESS_MODES_RW)) == NULL) {
free(an);
return ANNOUNCES_RETURN_IO_ERROR;
}
lock_file(f);
if(wcfseek(f, sizeof(DWORD), SEEK_SET) != 0) {
free(an);
return ANNOUNCES_RETURN_IO_ERROR;
}
an->Number = 0xffffffff;
pos = 0;
while(!wcfeof(f))
{
pos = wcftell(f);
if((rd = wcfread(an, 1, sizeof(SGlobalAnnounce), f)) != sizeof(SGlobalAnnounce)) {
if(rd != 0) {
free(an);
wcfclose(f);
return ANNOUNCES_RETURN_DB_ERROR;
}
else break;
}
if(an->Number == id && (uniqid == 0 || an->UIdFrom == uniqid)) break;
}
if(an->Number == id) {
// let's delete it
unsigned i = 0;
while(!wcfeof(f))
{
if((rd = wcfread(&(an[i]), 1, sizeof(SGlobalAnnounce), f)) != sizeof(SGlobalAnnounce)) {
if(rd != 0) {
free(an);
wcfclose(f);
return ANNOUNCES_RETURN_DB_ERROR;
}
else break;
}
i++;
an = (SGlobalAnnounce*)realloc(an, (i+1)*sizeof(SGlobalAnnounce));
}
fsize = wcftell(f);
fsize -= sizeof(SGlobalAnnounce);
if(wcfseek(f, pos, SEEK_SET) != 0) {
free(an);
wcfclose(f);
return ANNOUNCES_RETURN_DB_ERROR;
}
if(wcfwrite(an, 1, sizeof(SGlobalAnnounce)*i, f) != sizeof(SGlobalAnnounce)*i) {
free(an);
wcfclose(f);
return ANNOUNCES_RETURN_DB_ERROR;
}
free(an);
// truncate end of file
if (wcfflush(f) || ftruncate(fileno(f), fsize))
printhtmlerror();
unlock_file(f);
wcfclose(f);
}
else {
// announce not found
free(an);
unlock_file(f);
wcfclose(f);
return ANNOUNCES_RETURN_NOT_FOUND;
}
return ANNOUNCES_RETURN_OK;
}
int UpdateGlobalAnnounce(DWORD id, const char *username, DWORD uniqid, const char *announce,
DWORD ttl, DWORD flags, DWORD updateopt)
{
WCFILE *f;
DWORD pos, rd;
SGlobalAnnounce *an;
an = (SGlobalAnnounce*)malloc(sizeof(SGlobalAnnounce));
if(!an) return ANNOUNCES_RETURN_LOW_RES;
if((f = wcfopen(F_GLOBAL_ANN, FILE_ACCESS_MODES_RW)) == NULL) {
free(an);
return ANNOUNCES_RETURN_IO_ERROR;
}
lock_file(f);
if(wcfseek(f, sizeof(DWORD), SEEK_SET) != 0) {
free(an);
return ANNOUNCES_RETURN_IO_ERROR;
}
an->Number = 0xffffffff;
pos = 0;
while(!wcfeof(f))
{
pos = wcftell(f);
if((rd = wcfread(an, 1, sizeof(SGlobalAnnounce), f)) != sizeof(SGlobalAnnounce)) {
if(rd != 0) {
free(an);
wcfclose(f);
return ANNOUNCES_RETURN_DB_ERROR;
}
else break;
}
if(an->Number == id) break;
}
if(an->Number == id) {
// let's update it
strcpy(an->Announce, announce);
if((updateopt & ANNOUNCES_UPDATE_OPT_USER)) {
strcpy(an->From, username);
an->UIdFrom = uniqid;
}
if((updateopt & ANNOUNCES_UPDATE_OPT_TTL)) an->TTL = ttl;
if((updateopt & ANNOUNCES_UPDATE_OPT_TIME)) an->Date = time(NULL);
if((updateopt & ANNOUNCES_UPDATE_OPT_FLAGS)) an->Flags = flags;
memset(an->Reserved, 0, sizeof(an->Reserved));
if(wcfseek(f, pos, SEEK_SET) != 0) {
free(an);
wcfclose(f);
return ANNOUNCES_RETURN_DB_ERROR;
}
if(wcfwrite(an, 1, sizeof(SGlobalAnnounce), f) != sizeof(SGlobalAnnounce)) {
free(an);
wcfclose(f);
return ANNOUNCES_RETURN_DB_ERROR;
}
free(an);
unlock_file(f);
wcfclose(f);
}
else {
// announce not found
free(an);
unlock_file(f);
wcfclose(f);
return ANNOUNCES_RETURN_NOT_FOUND;
}
return ANNOUNCES_RETURN_OK;
}