-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathreadbits.c
323 lines (265 loc) · 6.73 KB
/
readbits.c
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
Eagles Bulletin Board System
Copyright (C) 1995, Ray Rocker, rocker@datasync.com
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "server.h"
#include <time.h>
/* readbits file format:
bname :ordr:stampxxx:10101010...
if bname is empty, the readbits are for Mail.
*/
#define BIT_BNAME_OFFSET 0
#define BIT_ORDER_OFFSET (BIT_BNAME_OFFSET+NAMELEN+1)
#define BIT_STAMP_OFFSET (BIT_ORDER_OFFSET+5)
#define BIT_DATA_OFFSET (BIT_STAMP_OFFSET+9)
#define READBITFILE "readbits"
get_readbit_file(userid, buf)
char *userid;
char *buf;
{
get_home_directory(userid, buf);
strcat(buf, "/");
strcat(buf, READBITFILE);
}
expand_bitent(bitent, ba)
char *bitent; /* The readbit data */
char *ba; /* char array of size READBITSIZE, filled */
{
char *eob;
char currval;
int currct = 0;
char scratch[10];
for (eob = ba+READBITSIZE-1; *eob == '0' && eob > ba; eob--);
eob++;
for (currval = *ba; ba <= eob; ba++) {
if (*ba == currval) currct++;
else {
if (currct <= 4) {
memset(scratch, 0, sizeof scratch);
for (; currct; currct--) scratch[currct-1] = currval;
}
else sprintf(scratch, "%c[%d]", currval, currct);
strcpy(bitent, scratch);
bitent += strlen(scratch);
currct = 1;
currval = *ba;
}
}
*bitent = '\0';
}
compress_bitent(bitent, ba)
char *bitent; /* The readbit data */
char *ba; /* char array of size READBITSIZE, empty */
{
int ct;
char *rbrkt;
memset(ba, '0', READBITSIZE);
while (*bitent && *bitent != '\n') {
if (*(bitent+1) == '[') {
if (rbrkt = strchr(bitent, ']')) *rbrkt = '\0';
for (ct = atoi(bitent+2); ct; ct--) *ba++ = *bitent;
if (rbrkt) {
*rbrkt = ']';
bitent = rbrkt;
}
}
else {
*ba++ = *bitent;
}
bitent++;
}
}
parse_readinfo(rec, rinfo)
char *rec;
READINFO *rinfo;
{
rinfo->stamp = hex2LONG(rec+BIT_STAMP_OFFSET);
compress_bitent(rec+BIT_DATA_OFFSET, rinfo->bits);
return S_OK;
}
parse_readorder(rec, order)
char *rec;
SHORT *order;
{
*order = hex2SHORT(rec+BIT_ORDER_OFFSET);
return S_OK;
}
get_bitfile_ent(bname, rinfo)
char *bname;
READINFO *rinfo;
{
int rc;
PATH bitfile;
get_readbit_file(my_userid(), bitfile);
memset(rinfo->bits, '0', READBITSIZE);
rinfo->stamp = 0;
rc = _record_find(bitfile, _match_first, bname, parse_readinfo, rinfo);
return rc;
}
struct _bitsetstruct {
char *bname;
SHORT *order;
READINFO *rinfo;
};
format_readinfo(rec, info)
char *rec;
struct _bitsetstruct *info;
{
if (info->bname) {
memset(rec+BIT_BNAME_OFFSET, ' ', NAMELEN);
memcpy(rec+BIT_BNAME_OFFSET, info->bname, strlen(info->bname));
rec[BIT_ORDER_OFFSET-1] = ':';
}
if (info->order) {
SHORTcpy(rec+BIT_ORDER_OFFSET, *(info->order));
rec[BIT_STAMP_OFFSET-1] = ':';
}
if (info->rinfo) {
LONGcpy(rec+BIT_STAMP_OFFSET, info->rinfo->stamp);
rec[BIT_DATA_OFFSET-1] = ':';
expand_bitent(rec+BIT_DATA_OFFSET, info->rinfo->bits);
strcat(rec, "\n");
}
return S_OK;
}
/* This can GO AWAY if I ever fix _record_replace to only pass two args */
update_readinfo(newrec, oldrec, info)
char *newrec;
char *oldrec;
struct _bitsetstruct *info;
{
strcpy(newrec, oldrec);
return (format_readinfo(newrec, info));
}
set_bitfile_ent(bname, rinfo)
char *bname;
READINFO *rinfo;
{
int rc;
struct _bitsetstruct bs;
PATH bitfile;
if (my_flag(FLG_SHARED)) return 0;
get_readbit_file(my_userid(), bitfile);
bs.bname = NULL;
bs.order = NULL;
bs.rinfo = rinfo;
rc = _record_replace(bitfile, _match_first, bname, update_readinfo, &bs);
if (rc == S_NOSUCHREC) {
SHORT order = READ_ORDER_UNSET;
bs.bname = bname;
bs.order = ℴ
rc = _record_add(bitfile, _match_first, bname, format_readinfo, &bs);
}
return rc;
}
set_readbit(rinfo, fileid)
READINFO *rinfo;
SHORT fileid;
{
if (fileid <= 0 || fileid > READBITSIZE) return S_OUTOFRANGE;
rinfo->bits[fileid-1] = '1';
return S_OK;
}
clear_all_readbits(rinfo)
READINFO *rinfo;
{
memset(rinfo->bits, '0', sizeof(rinfo->bits));
return S_OK;
}
test_readbit(rinfo, fileid)
READINFO *rinfo;
SHORT fileid;
{
if (fileid <= 0 || fileid > READBITSIZE) return 0;
return (rinfo->bits[fileid-1] == '1');
}
get_read_order(bname, order)
char *bname;
SHORT *order;
{
int rc;
PATH bitfile;
get_readbit_file(my_userid(), bitfile);
*order = READ_ORDER_UNSET;
rc = _record_find(bitfile, _match_first, bname, parse_readorder, order);
return rc;
}
set_read_order(bname, order)
char *bname;
SHORT order;
{
int rc;
struct _bitsetstruct bs;
PATH bitfile;
if (my_flag(FLG_SHARED)) return 0;
get_readbit_file(my_userid(), bitfile);
bs.bname = NULL;
bs.order = ℴ
bs.rinfo = NULL;
rc = _record_replace(bitfile, _match_first, bname, update_readinfo, &bs);
if (rc == S_NOSUCHREC) {
READINFO rinfo;
rinfo.stamp = 0;
clear_all_readbits(&rinfo);
bs.bname = bname;
bs.rinfo = &rinfo;
rc = _record_add(bitfile, _match_first, bname, format_readinfo, &bs);
}
return rc;
}
/*ARGSUSED*/
fix_readbit_entry(indx, rec, ncs)
int indx;
char *rec;
struct namechange *ncs;
{
int rc;
PATH bitfile;
NAME userid;
struct _bitsetstruct bs;
/* Sleazy way to get the userid out of the passfile record */
memset(userid, 0, sizeof userid);
strncpy(userid, rec, NAMELEN);
strip_trailing_space(userid);
get_readbit_file(userid, bitfile);
if (ncs->newname == NULL)
rc = _record_delete(bitfile, _match_first, ncs->oldname);
else {
bs.rinfo = NULL;
bs.order = NULL;
bs.bname = ncs->newname;
rc = _record_replace(bitfile, _match_first, ncs->oldname,
update_readinfo, &bs);
}
return rc;
}
#define NOZAP_FILE "nozap"
local_bbs_zap_board(bname, dozap)
char *bname;
SHORT dozap;
{
int rc;
PATH nzfile;
FILE *fp;
SHORT neworder = dozap ? READ_ORDER_ZAPPED : READ_ORDER_UNSET;
get_board_directory(bname, nzfile);
strcat(nzfile, "/");
strcat(nzfile, NOZAP_FILE);
if (dozap && ((fp = fopen(nzfile, "r")) != NULL)) {
fclose(fp);
return S_CANNOTZAP;
}
rc = set_read_order(bname, neworder);
return (rc == S_OK ? S_OK : S_NOSUCHBOARD);
}