summaryrefslogtreecommitdiffstats
path: root/mbbsd/brc.c
blob: 636b8dbe01be8f93f22424ab0328ec094e2e99d8 (plain) (blame)
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
#include "bbs.h"

#ifndef BRC_MAXNUM
#define BRC_MAXSIZE     24576   /* Effective size of brc rc file, 8192 * 3 */
#define BRC_ITEMSIZE    ((BRC_MAXNUM + 2) * sizeof( int ))
    /* Maximum size of each record */
#define BRC_MAXNUM      80      /* Upper bound of brc_num, size of brc_list  */
#endif

/* brc rc file form:
 * board_name     15 bytes
 * brc_num         1 byte, binary integer
 * brc_list       brc_num * sizeof(int) bytes, brc_num binary integer(s) */

static time_t   brc_expire_time;
 /* Will be set to the time one year before login. All the files created
  * before then will be recognized read. */

static int      brc_changed = 0;
/* The below two will be filled by read_brc_buf() and brc_update() */
static int     *brc_buf = NULL;
static int      brc_size;

static char * const fn_oldboardrc = ".boardrc";
static char * const fn_brc = ".brc2";

static int    *
brc_getrecord(int *ptr, int *endp, int *bid, int *pnum, int *list)
{
    int  num;
    int *tmp;

    if (ptr + 2 > endp)
    return endp + 1; /* dangling, ignoring it */
    *bid = *ptr++;       /* bid */
    num = *ptr++;          /* brc_num */
    tmp = ptr + num;     /* end of this record */
    if (tmp <= endp){
    memcpy(list, ptr, num * sizeof(int)); /* brc_list */
    if (num > BRC_MAXNUM)
        num = BRC_MAXNUM;
    *pnum = num;
    }
    return tmp;
}

static int *
brc_putrecord(int *ptr, int *endp, int bid, int num, const int *list)
{
    if (num > 0 && list[0] > brc_expire_time &&
        ptr + 2 <= endp) {
    if (num > BRC_MAXNUM)
        num = BRC_MAXNUM;

    while (num > 1 && list[num - 1] < brc_expire_time)
        num--; /* don't write the times before brc_expire_time */

    if( num == 0 ) return ptr;

    *ptr++ = bid;         /* write in bid */
    *ptr++ = num;         /* write in brc_num */
    if (ptr + num <= endp)
        memcpy(ptr, list, num * sizeof(int)); /* write in brc_list */
    ptr += num;
    }
    return ptr;
}

#error "Below not changed yet"
static inline void
brc_insert_record(const char* board, int num, int* list)
{
    int            *ptr, *endp, *tmpp = 0;
    char            tmp_buf[BRC_ITEMSIZE];
    char            tmp_name[BRC_STRLEN];
    int             tmp_list[BRC_MAXNUM];
    int             new_size, end_size, tmp_num;
    int             found = 0;

    ptr = brc_buf;
    endp = &brc_buf[brc_size];
    while (ptr < endp && (*ptr >= ' ' && *ptr <= 'z')) {
    /* for each available records */
    tmpp = brc_getrecord(ptr, endp, tmp_name, &tmp_num, tmp_list);

    if ( tmpp > endp ){
        /* dangling, ignore the trailing data */
        brc_size = (int)(ptr - brc_buf);
        break;
    }
    if ( strncmp(tmp_name, board, BRC_STRLEN) == 0 ){
        found = 1;
        break;
    }
    ptr = tmpp;
    }

    if( ! found ){
    /* put on the beginning */
    ptr = brc_putrecord(tmp_buf, tmp_buf + BRC_MAXSIZE,
        board, num, list);
    new_size = (int)(ptr - tmp_buf);
    if( new_size ){
        brc_size += new_size;
        if ( brc_size > BRC_MAXSIZE )
        brc_size = BRC_MAXSIZE;
        memmove(brc_buf + new_size, brc_buf, brc_size);
        memmove(brc_buf, tmp_buf, new_size);
    }
    }else{
    /* ptr points to the old current brc list.
     * tmpp is the end of it (exclusive).       */
    end_size = endp - tmpp;
    new_size = (int)(brc_putrecord(tmp_buf, tmp_buf + BRC_ITEMSIZE,
            board, num, list) - tmp_buf);
    if( new_size ){
        brc_size += new_size - (tmpp - ptr);
        if ( brc_size > BRC_MAXSIZE ){
        end_size -= brc_size - BRC_MAXSIZE;
        brc_size = BRC_MAXSIZE;
        }
        if ( end_size > 0 && ptr + new_size != tmpp )
        memmove(ptr + new_size, tmpp, end_size);
        memmove(ptr, tmp_buf, new_size);
    }else
        memmove(ptr, tmpp, brc_size - (tmpp - brc_buf));
    }

    brc_changed = 0;
}

void
brc_update(){
    if (brc_changed && cuser.userlevel && brc_num > 0)
    brc_insert_record(currboard, brc_num, brc_list);
}

inline static void
read_old_brc()
{
    /* XXX: read from old brc file */
}

inline static void
read_brc_buf()
{
    if( brc_buf == NULL ){
    char            dirfile[STRLEN];
    int             fd;

    brc_buf = malloc(BRC_MAXSIZE);
    setuserfile(dirfile, fn_brc);
    if ((fd = open(dirfile, O_RDONLY)) != -1) {
        brc_size = read(fd, brc_buf, BRC_MAXSIZE);
        close(fd);
    } else {
        read_old_brc();
    }
    }
}

void
brc_finalize(){
    char brcfile[STRLEN];
    int fd;
    brc_update();
    setuserfile(brcfile, fn_boardrc);
    if (brc_buf != NULL &&
    (fd = open(brcfile, O_WRONLY | O_CREAT | O_TRUNC, 0644)) != -1) {
    write(fd, brc_buf, brc_size);
    close(fd);
    }
}

void
brc_initialize(){
    static char done = 0;
    if( done )
    return;
    done = 1;
    brc_expire_time = login_start_time - 365 * 86400;
    read_brc_buf();
}

int
brc_read_record(const char* bname, int* num, int* list){
    char            *ptr;
    char            *endp;
    char            tmp_name[BRC_STRLEN];
    ptr = brc_buf;
    endp = &brc_buf[brc_size];
    while (ptr < endp && (*ptr >= ' ' && *ptr <= 'z')) {
    /* for each available records */
    ptr = brc_getrecord(ptr, endp, tmp_name, num, list);
    if (strncmp(tmp_name, bname, BRC_STRLEN) == 0)
        return *num;
    }
    *num = list[0] = 1;
    return 0;
}

int
brc_initial_board(const char *boardname)
{
    brc_initialize();

    if (strcmp(currboard, boardname) == 0) {
    return brc_num;
    }

    brc_update(); /* write back first */
    currbid = getbnum(boardname);
    currboard = bcache[currbid - 1].brdname;
    currbrdattr = bcache[currbid - 1].brdattr;

    return brc_read_record(boardname, &brc_num, brc_list);
}

void
brc_trunc(const char* brdname, int ftime){
    brc_insert_record(brdname, 1, &ftime);
    if (strncmp(brdname, currboard, BRC_STRLEN) == 0){
    brc_num = 1;
    brc_list[0] = ftime;
    brc_changed = 0;
    }
}

void
brc_addlist(const char *fname)
{
    int             ftime, n, i;

    if (!cuser.userlevel)
    return;

    ftime = atoi(&fname[2]);
    if (ftime <= brc_expire_time /* too old, don't do any thing  */
     /* || fname[0] != 'M' || fname[1] != '.' */ ) {
    return;
    }
    if (brc_num <= 0) { /* uninitialized */
    brc_list[0] = ftime;
    brc_num = 1;
    brc_changed = 1;
    return;
    }
    if ((brc_num == 1) && (ftime < brc_list[0])) /* most when after 'v' */
    return;
    for (n = 0; n < brc_num; n++) { /* using linear search */
    if (ftime == brc_list[n]) {
        return;
    } else if (ftime > brc_list[n]) {
        if (brc_num < BRC_MAXNUM)
        brc_num++;
        /* insert ftime in to brc_list */
        for (i = brc_num - 1; --i >= n; brc_list[i + 1] = brc_list[i]);
        brc_list[n] = ftime;
        brc_changed = 1;
        return;
    }
    }
}

int
brc_unread_time(time_t ftime, int bnum, const int *blist)
{
    int             n;

    if (ftime <= brc_expire_time) /* too old */
    return 0;

    if (brc_num <= 0)
    return 1;
    for (n = 0; n < bnum; n++) { /* using linear search */
    if (ftime > blist[n])
        return 1;
    else if (ftime == blist[n])
        return 0;
    }
    return 0;
}

int
brc_unread(const char *fname, int bnum, const int *blist)
{
    int             ftime, n;

    ftime = atoi(&fname[2]); /* this will get the time of the file created */

    if (ftime <= brc_expire_time) /* too old */
    return 0;

    if (brc_num <= 0)
    return 1;
    for (n = 0; n < bnum; n++) { /* using linear search */
    if (ftime > blist[n])
        return 1;
    else if (ftime == blist[n])
        return 0;
    }
    return 0;
}