summaryrefslogtreecommitdiffstats
path: root/mbbsd/voteboard.c
blob: f8be3d27f1161b796a9b2ca60e95ae2781260223 (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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/* $Id: voteboard.c,v 1.6 2002/05/25 11:18:11 ptt Exp $ */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/file.h>
#include "config.h"
#include "pttstruct.h"
#include "modes.h"
#include "common.h"
#include "perm.h"
#include "proto.h"

#define VOTEBOARD "NewBoard"

extern char currboard[];
extern int currbid;
extern boardheader_t *bcache;
extern int currmode;
extern userec_t cuser;
extern time_t now;

void do_voteboardreply(fileheader_t *fhdr){
    char genbuf[1024];
    char reason[50];
    char fpath[80];
    char oldfpath[80];
    char opnion[10];
    char *ptr;
    FILE *fo, *fp;
    fileheader_t votefile;
    int len;
    int i, j;
    int fd;
    time_t endtime;
    int hastime = 0;
   

    clear();
    if(!(currmode & MODE_POST)) {
    move(5, 10);
    outs("對不起,您目前無法在此發表文章!");
    pressanykey();
    return;
    }   
    
    setbpath(fpath, currboard);
    stampfile(fpath, &votefile);

    setbpath(oldfpath, currboard);
   
    strcat(oldfpath, "/");
    strcat(oldfpath, fhdr->filename);
   
    fp = fopen(oldfpath, "r");
   
    len = strlen(cuser.userid);
   
    while(fgets(genbuf, 1024, fp)){
    if (!strncmp(genbuf, "連署結束時間", 12)){
        hastime = 1;
        ptr = strchr(genbuf, '(');
        sscanf(ptr+1, "%ld", &endtime);
        if (endtime < now){
        prints("連署時間已過");
        pressanykey();
        fclose(fp);
        return;
        }
    }
    if (!strncmp(genbuf+4, cuser.userid, len)){
        move(5, 10);
        prints("您已經連署過本篇了");
        opnion[0] = 'n';
        getdata(7, 0, "要修改您之前的連署嗎?(Y/N) [N]", opnion, 3, LCECHO);
        if (opnion[0] != 'y'){
        fclose(fp);
        return;
        }
        strcpy(reason, genbuf+19);
    }
    }
    fclose(fp);                  
   
    if((fd = open(oldfpath, O_RDONLY)) == -1)
    return;
    flock(fd, LOCK_EX);
   
    fo = fopen(fpath, "w");
   
    if (!fo)
    return;
    i = 0;
    while(fo){
    j = 0;
    do{
        if (read(fd, genbuf+j, 1)<=0){
        flock(fd, LOCK_UN);
        close(fd);
        fclose(fo);
        unlink(fpath);
        return;
        }
        j++;
    }while(genbuf[j-1] !='\n');
    genbuf[j] = '\0';        
    i++;
    if (!strncmp("----------", genbuf, 10))
        break;
    if (i > 3)
        prints(genbuf);
    fprintf(fo, "%s", genbuf);    
    }
    if (!hastime){
    now += 14*24*60*60;
    fprintf(fo, "連署結束時間: (%ld)%s", now, ctime(&now));
    now -= 14*24*60*60;
    }
   
    fprintf(fo, "%s", genbuf);

    do{
    if (!getdata(18, 0, "請問您 (Y)支持 (N)反對 這個議題:", opnion, 3, LCECHO)){
        flock(fd, LOCK_UN);
        close(fd);           
        fclose(fo);
        unlink(fpath);
        return;
    }
    }while(opnion[0] != 'y' && opnion[0] != 'n');      
   
    if (!getdata(20, 0, "請問您與這個議題的關係或連署理由為何:",
         reason, sizeof(reason), DOECHO)){
    flock(fd, LOCK_UN);
    close(fd);           
    fclose(fo);
    unlink(fpath);
    return;   
    }
   
    i = 0;
   
    while(fo){
    i++;
    j = 0;
    do{
        if (read(fd, genbuf+j, 1)<=0){
        flock(fd, LOCK_UN);
        close(fd);
        fclose(fo);
        unlink(fpath);
        return;
        }      
        j++;
    }while(genbuf[j-1] !='\n');   
    genbuf[j] = '\0';
    if (!strncmp("----------", genbuf, 10))
        break;
    if (strncmp(genbuf+4, cuser.userid, len))
        fprintf(fo, "%3d.%s", i, genbuf+4);
    else
        i--;
    }
    if (opnion[0] == 'y')
    fprintf(fo, "%3d.%-15s%-34s 來源:%s\n", i, cuser.userid, reason,cuser.lasthost);
    i = 0;
    fprintf(fo, "%s", genbuf);
    while(fo){
    i++;
    j = 0;
    do{
        if (!read(fd, genbuf+j, 1))
        break;
        j++;
    }while(genbuf[j-1] !='\n');
    genbuf[j] = '\0';
    if (j <= 3)
        break;         
    if (strncmp(genbuf+4, cuser.userid, len))
        fprintf(fo, "%3d.%s", i, genbuf+4);
    else
        i--;
    }
    if (opnion[0] == 'n')
    fprintf(fo, "%3d.%-15s%-34s 來源:%s\n", i, cuser.userid, reason,cuser.lasthost);
    flock(fd, LOCK_UN);  
    close(fd);
    fclose(fo);
    unlink(oldfpath);
    rename(fpath, oldfpath);
#ifdef MDCACHE
    close(updatemdcache(NULL, oldfpath));
#endif   
}

int do_voteboard() {
    fileheader_t votefile;
    char topic[100];
    char title[80];
    char genbuf[1024];
    char fpath[80];
    FILE* fp;
    int temp, i;

    clear();
    if(!(currmode & MODE_POST)) {
    move(5, 10);
    outs("對不起,您目前無法在此發表文章!");
    pressanykey();
    return FULLUPDATE;
    }       
    
    move(0, 0);
    clrtobot();
    prints("您正在使用 PTT 的連署系統\n");
    prints("本連署系統將詢問您一些問題,請小心回答才能開始連署\n");
    prints("任意提出連署案者,將被列入本系統不受歡迎使用者喔\n");
    pressanykey();
    move(0, 0);
    clrtobot();    
    prints("(1)申請新版 (2)廢除舊版 (3)連署版主 (4)罷免版主\n");
    if (!strcmp(currboard, VOTEBOARD))
    prints("(5)連署小組長 (6)罷免小組長 ");
    if (!strcmp(currboard, VOTEBOARD) && HAS_PERM(PERM_SYSOP))
    prints("(7)站民公投");
    prints("(8)申請新群組");
       
    do{       
    getdata(3, 0, "請輸入連署類別:", topic, 3, DOECHO);
    temp = atoi(topic);
    }while(temp <= 0 && temp >= 9);
    
    switch(temp){
    case 1:    
    do{
        if (!getdata(4, 0, "請輸入看版英文名稱:", topic, IDLEN+1, DOECHO))
        return FULLUPDATE;
        else if (invalid_brdname(topic))
        outs("不是正確的看版名稱");
        else if (getbnum(topic) > 0)
        outs("本名稱已經存在");
        else
        break;
    }while(temp > 0);          
    sprintf(title, "[申請新版] %s", topic);
    sprintf(genbuf, "%s\n\n%s%s\n%s","申請新版", "英文名稱: ", topic, "中文名稱: ");
       
    if (!getdata(5, 0, "請輸入看版中文名稱:", topic, 20, DOECHO))
        return FULLUPDATE;
    strcat(genbuf, topic);
    strcat(genbuf, "\n看版類別: ");
    if (!getdata(6, 0, "請輸入看版類別:", topic, 20, DOECHO))
        return FULLUPDATE;
    strcat(genbuf, topic);       
    strcat(genbuf, "\n版主名單: ");
    getdata(7, 0, "請輸入版主名單:", topic, IDLEN * 3 + 3, DOECHO);
    strcat(genbuf, topic);
    strcat(genbuf, "\n申請原因: \n");
    outs("請輸入申請原因(至多五行),要清楚填寫不然不會核准喔");
    for(i= 8;i<13;i++){
        if (!getdata(i, 0, ":", topic, 60, DOECHO))
        break;
        strcat(genbuf, topic);
        strcat(genbuf, "\n");
    }
    if (i==8)
        return FULLUPDATE;
    break;
    case 2:
    do{
        if (!getdata(4, 0, "請輸入看版英文名稱:", topic, IDLEN+1, DOECHO))
        return FULLUPDATE;
        else if (getbnum(topic) <= 0)
        outs("本名稱並不存在");
        else
        break;
    }while(temp > 0);          
    sprintf(title, "[廢除舊版] %s", topic);
    sprintf(genbuf, "%s\n\n%s%s\n","廢除舊版", "英文名稱: ", topic);
    strcat(genbuf, "\n廢除原因: \n");
    outs("請輸入廢除原因(至多五行),要清楚填寫不然不會核准喔");
    for(i= 8;i<13;i++){
        if (!getdata(i, 0, ":", topic, 60, DOECHO))
        break;
        strcat(genbuf, topic);
        strcat(genbuf, "\n");
    }
    if (i==8)
        return FULLUPDATE;
          
    break;
    case 3:    
    do{
        if (!getdata(4, 0, "請輸入看版英文名稱:", topic, IDLEN+1, DOECHO))
        return FULLUPDATE;
        else if (getbnum(topic) <= 0)
        outs("本名稱並不存在");
        else
        break;
    }while(temp > 0);          
    sprintf(title, "[連署版主] %s", topic);
    sprintf(genbuf, "%s\n\n%s%s\n%s%s","連署版主", "英文名稱: ", topic, "申請 ID : ", cuser.userid);
    strcat(genbuf, "\n申請政見: \n");
    outs("請輸入申請政見(至多五行),要清楚填寫不然不會核准喔");
    for(i= 8;i<13;i++){
        if (!getdata(i, 0, ":", topic, 60, DOECHO))
        break;
        strcat(genbuf, topic);
        strcat(genbuf, "\n");
    }
    if (i==8)
        return FULLUPDATE;       
    break;       
    case 4:    
    do{
        if (!getdata(4, 0, "請輸入看版英文名稱:", topic, IDLEN+1, DOECHO))
        return FULLUPDATE;
        else if ((i = getbnum(topic)) <= 0)
        outs("本名稱並不存在");
        else
        break;
    }while(temp > 0);          
    sprintf(title, "[罷免版主] %s", topic);
    sprintf(genbuf, "%s\n\n%s%s\n%s","罷免版主", "英文名稱: ", topic, "版主 ID : ");
    do{
        if (!getdata(6, 0, "請輸入版主ID:", topic, IDLEN + 1, DOECHO))
        return FULLUPDATE;
        else if (!userid_is_BM(topic, bcache[i-1].BM))
        outs("不是該版的版主");
        else
        break;
    }while(temp > 0);
    strcat(genbuf, topic);
    strcat(genbuf, "\n罷免原因: \n");
    outs("請輸入罷免原因(至多五行),要清楚填寫不然不會核准喔");
    for(i= 8;i<13;i++){
        if (!getdata(i, 0, ":", topic, 60, DOECHO))
        break;
        strcat(genbuf, topic);
        strcat(genbuf, "\n");
    }
    if (i==8)
        return FULLUPDATE;
    break;       
    case 5:    
    if (!getdata(4, 0, "請輸入小組中英文名稱:", topic, 30, DOECHO))
        return FULLUPDATE;
    sprintf(title, "[連署小組長] %s", topic);
    sprintf(genbuf, "%s\n\n%s%s\n%s%s","連署小組長", "小組名稱: ", topic, "申請 ID : ", cuser.userid);
    strcat(genbuf, "\n申請政見: \n");
    outs("請輸入申請政見(至多五行),要清楚填寫不然不會核准喔");
    for(i= 8;i<13;i++){
        if (!getdata(i, 0, ":", topic, 60, DOECHO))
        break;
        strcat(genbuf, topic);
        strcat(genbuf, "\n");
    }
    if (i==8)
        return FULLUPDATE;
    break;
    case 6:
           
    if (!getdata(4, 0, "請輸入小組中英文名稱:", topic, 30, DOECHO))
        return FULLUPDATE;
    sprintf(title, "[罷免小組長] %s", topic);
    sprintf(genbuf, "%s\n\n%s%s\n%s","罷免小組長", "小組名稱: ", topic, "小組長 ID : ");
    if (!getdata(6, 0, "請輸入小組長ID:", topic, IDLEN + 1, DOECHO))
        return FULLUPDATE;
    strcat(genbuf, topic);
    strcat(genbuf, "\n罷免原因: \n");
    outs("請輸入罷免原因(至多五行),要清楚填寫不然不會核准喔");
    for(i= 8;i<13;i++){
        if (!getdata(i, 0, ":", topic, 60, DOECHO))
        break;
        strcat(genbuf, topic);
        strcat(genbuf, "\n");
    }
    if (i==8)
        return FULLUPDATE;
    break;       
    case 7:    
    if (!HAS_PERM(PERM_SYSOP))
        return FULLUPDATE;
    if (!getdata(4, 0, "請輸入公投主題:", topic, 30, DOECHO))
        return FULLUPDATE;
    sprintf(title, "%s %s", "[站民公投]", topic);
    sprintf(genbuf, "%s\n\n%s%s\n","站民公投", "公投主題: ", topic);
    strcat(genbuf, "\n公投原因: \n");
    outs("請輸入公投原因(至多五行),要清楚填寫不然不會核准喔");
    for(i= 8;i<13;i++){
        if (!getdata(i, 0, ":", topic, 60, DOECHO))
        break;
        strcat(genbuf, topic);
        strcat(genbuf, "\n");
    }
    if (i==8)
        return FULLUPDATE;
    break;              
    case 8:
    if(!getdata(4, 0, "請輸入群組中英文名稱:", topic, 30, DOECHO))
        return FULLUPDATE;
    sprintf(title, "[申請新群組] %s", topic);
    sprintf(genbuf, "%s\n\n%s%s\n%s%s","申請群組", "群組名稱: ", topic, "申請 ID : ", cuser.userid);
    strcat(genbuf, "\n申請政見: \n");
    outs("請輸入申請政見(至多五行),要清楚填寫不然不會核准喔");
    for(i= 8;i<13;i++){
        if (!getdata(i, 0, ":", topic, 60, DOECHO))
        break;
        strcat(genbuf, topic);
        strcat(genbuf, "\n");
    }
    if (i==8)
        return FULLUPDATE;
    break;
    default:
    return FULLUPDATE;
    }
    strcat(genbuf, "連署結束時間: ");
    now += 14*24*60*60;    
    sprintf(topic, "(%ld)", now);
    strcat(genbuf, topic);
    strcat(genbuf, ctime(&now));
    now -= 14*24*60*60;
    strcat(genbuf, "----------支持----------\n");
    strcat(genbuf, "----------反對----------\n");    
    outs("開始連署嘍");
    setbpath(fpath, currboard);
    stampfile(fpath, &votefile);
    
    if (!(fp = fopen(fpath, "w"))){
    outs("開檔失敗,請稍候重來一次");
    return FULLUPDATE;
    }
    fprintf(fp, "%s%s %s%s\n%s%s\n%s%s", "作者: ", cuser.userid,
        "看板: ", currboard, 
        "標題: ", title,
        "時間: ", ctime(&now));
    fprintf(fp, "%s\n", genbuf);
    fclose(fp);
    strcpy(votefile.owner, cuser.userid);
    strcpy(votefile.title, title);
    setbdir(genbuf, currboard);
    if(append_record(genbuf, &votefile, sizeof(votefile)) != -1)
    setbtotal(currbid);
    do_voteboardreply(&votefile);
    return FULLUPDATE;
}