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
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
|
/* $Id$ */
#include "bbs.h"
/* copy temp queue operation -------------------------------------- */
typedef struct {
char copyfile[PATHLEN];
char copytitle[TTLEN + 1];
char copyowner[IDLEN + 2];
} CopyQueue ;
static CopyQueue *copyqueue;
static int allocated_copyqueue = 0, used_copyqueue = 0, head_copyqueue = 0;
int copyqueue_testin(CopyQueue *pcq)
{
int i = 0;
for (i = 0; i < used_copyqueue; i++)
if (strcmp(pcq->copyfile, copyqueue[i].copyfile) == 0)
return 1;
return 0;
}
void copyqueue_reset()
{
allocated_copyqueue = 0;
used_copyqueue = 0;
head_copyqueue = 0;
}
int copyqueue_append(CopyQueue *pcq)
{
if(copyqueue_testin(pcq))
return 0;
if(head_copyqueue == used_copyqueue)
{
// empty queue, happy happy reset
head_copyqueue = used_copyqueue = 0;
}
used_copyqueue ++;
if(used_copyqueue > allocated_copyqueue)
{
allocated_copyqueue = used_copyqueue + 10; // half page
copyqueue = (CopyQueue*) realloc (copyqueue,
sizeof(CopyQueue) * allocated_copyqueue);
if(!copyqueue)
{
vmsg("記憶體不足,拷貝失敗");
// try to reset
copyqueue_reset();
if(copyqueue) free(copyqueue);
copyqueue = NULL;
return 0;
}
}
memcpy(&(copyqueue[used_copyqueue-1]), pcq, sizeof(CopyQueue));
return 1;
}
CopyQueue *copyqueue_gethead()
{
if( used_copyqueue <= 0 ||
head_copyqueue >= used_copyqueue)
return NULL;
return &(copyqueue[head_copyqueue++]);
}
int copyqueue_querysize()
{
if( used_copyqueue <= 0 ||
head_copyqueue >= used_copyqueue)
return 0;
return (used_copyqueue - head_copyqueue);
}
/* end copy temp queue operation ----------------------------------- */
void
a_copyitem(const char *fpath, const char *title, const char *owner, int mode)
{
CopyQueue cq;
static int flFirstAlert = 1;
memset(&cq, 0, sizeof(CopyQueue));
strcpy(cq.copyfile, fpath);
strcpy(cq.copytitle, title);
if (owner)
strcpy(cq.copyowner, owner);
copyqueue_append(&cq);
if (mode && flFirstAlert) {
#if 0
move(b_lines-2, 0); clrtoeol();
prints("目前已標記 %d 個檔案。[注意] 拷貝後才能刪除原文!",
copyqueue_querysize());
#else
vmsg("目前已複製 %d 個項目 [注意]貼上(p)或附加(a)後才能刪除原文!",
copyqueue_querysize());
flFirstAlert = 0;
#endif
}
}
#define FHSZ sizeof(fileheader_t)
static void
a_loadname(const menu_t * pm)
{
char buf[PATHLEN];
int len;
setadir(buf, pm->path);
len = get_records(buf, pm->header, FHSZ, pm->page + 1, p_lines);
if (len < p_lines)
bzero(&pm->header[len], FHSZ * (p_lines - len));
}
static void
a_timestamp(char *buf, const time4_t *time)
{
struct tm *pt = localtime4(time);
sprintf(buf, "%02d/%02d/%02d", pt->tm_mon + 1, pt->tm_mday, (pt->tm_year + 1900) % 100);
}
static void
a_showmenu(const menu_t * pm)
{
char *title, *editor;
int n;
fileheader_t *item;
char buf[PATHLEN];
time4_t dtime;
showtitle("精華文章", pm->mtitle);
prints(" " ANSI_COLOR(1;36) "編號 標 題%56s" ANSI_COLOR(0),
"編 選 日 期");
if (pm->num) {
setadir(buf, pm->path);
a_loadname(pm);
for (n = 0; n < p_lines && pm->page + n < pm->num; n++) {
item = &pm->header[n];
title = item->title;
editor = item->owner;
/*
* Ptt 把時間改為取檔案時間 dtime = atoi(&item->filename[2]);
*/
snprintf(buf, sizeof(buf), "%s/%s", pm->path, item->filename);
dtime = dasht(buf);
a_timestamp(buf, &dtime);
prints("\n%6d%c %-47.46s%-13s[%s]", pm->page + n + 1,
(item->filemode & FILE_BM) ? 'X' :
(item->filemode & FILE_HIDE) ? ')' : '.',
title, editor,
buf);
}
} else
outs("\n 《精華區》尚在吸取天地間的日月精華中... :)");
move(b_lines, 1);
if(copyqueue_querysize() > 0)
{ // something in queue
prints(
ANSI_COLOR(34;46) "【已標記 %d 個項目】"
ANSI_COLOR(31;47) " (h)" ANSI_COLOR(30) "說明 "
ANSI_COLOR(31) "(p)" ANSI_COLOR(30) "貼上或重設標記 "
ANSI_COLOR(31) "(a)" ANSI_COLOR(30) "附加至文章後 "
// ANSI_COLOR(31) "[注意]" ANSI_COLOR(30) "拷貝後才能刪除原文!"
ANSI_RESET , copyqueue_querysize());
}
else if(pm->level)
{ // BM
outs(
ANSI_COLOR(34;46) " 【板 主】 "
ANSI_COLOR(31;47) " (h)" ANSI_COLOR(30) "說明 "
ANSI_COLOR(31) "(q/←)" ANSI_COLOR(30) "離開 "
ANSI_COLOR(31) "(n)" ANSI_COLOR(30) "新增文章 "
ANSI_COLOR(31) "(g)" ANSI_COLOR(30) "新增目錄 "
ANSI_COLOR(31) "(e)" ANSI_COLOR(30) "編輯檔案 " ANSI_RESET
);
}
else
{ // normal user
outs(
ANSI_COLOR(34;46) " 【功\能鍵】 "
ANSI_COLOR(31;47) " (h)" ANSI_COLOR(30) "說明 "
ANSI_COLOR(31) "(q/←)" ANSI_COLOR(30) "離開 "
ANSI_COLOR(31) "(k↑j↓)" ANSI_COLOR(30) "移動游標 "
ANSI_COLOR(31) "(enter/→)" ANSI_COLOR(30) "讀取資料 " ANSI_RESET);
}
}
static int
a_searchtitle(menu_t * pm, int rev)
{
static char search_str[40] = "";
int pos;
getdata(b_lines - 1, 1, "[搜尋]關鍵字:", search_str, sizeof(search_str), DOECHO);
if (!*search_str)
return pm->now;
str_lower(search_str, search_str);
rev = rev ? -1 : 1;
pos = pm->now;
do {
pos += rev;
if (pos == pm->num)
pos = 0;
else if (pos < 0)
pos = pm->num - 1;
if (pos < pm->page || pos >= pm->page + p_lines) {
pm->page = pos - pos % p_lines;
a_loadname(pm);
}
if (strcasestr(pm->header[pos - pm->page].title, search_str))
return pos;
} while (pos != pm->now);
return pm->now;
}
enum {
NOBODY, MANAGER, SYSOP
};
static void
a_showhelp(int level)
{
clear();
outs(ANSI_COLOR(36) "【 " BBSNAME "公佈欄使用說明 】" ANSI_RESET "\n\n"
"[←][q] 離開到上一層目錄\n"
"[↑][k] 上一個選項\n"
"[↓][j] 下一個選項\n"
"[→][r][enter] 進入目錄/讀取文章\n"
"[^B][PgUp] 上頁選單\n"
"[^F][PgDn][Spc] 下頁選單\n"
"[##] 移到該選項\n"
"[F][U] 將文章寄回 Internet 郵箱/"
"將文章 uuencode 後寄回郵箱\n");
if (level >= MANAGER) {
outs("\n" ANSI_COLOR(36) "【 板主專用鍵 】" ANSI_RESET "\n"
"[H] 切換為 公開/會員/板主 才能閱\讀\n"
"[n/g/G] 收錄精華文章/開闢目錄/建立連線\n"
"[m/d/D] 移動/刪除文章/刪除一個範圍的文章\n"
"[f/T/e] 編輯標題符號/修改文章標題/內容\n"
"[c/p/a] 精華區內 拷貝/貼上(可多篇)/附加單篇文章\n"
"[^P/^A] 貼上/附加精華區外已用't'標記文章\n");
}
if (level >= SYSOP) {
outs("\n" ANSI_COLOR(36) "【 站長專用鍵 】" ANSI_RESET "\n"
"[l] 建 symbolic link\n"
"[N] 查詢檔名\n");
}
pressanykey();
}
static void
a_forward(const char *path, const fileheader_t * pitem, int mode)
{
fileheader_t fhdr;
strlcpy(fhdr.filename, pitem->filename, sizeof(fhdr.filename));
strlcpy(fhdr.title, pitem->title, sizeof(fhdr.title));
switch (doforward(path, &fhdr, mode)) {
case 0:
outmsg(msg_fwd_ok);
break;
case -1:
outmsg(msg_fwd_err1);
break;
case -2:
outmsg(msg_fwd_err2);
break;
}
}
static void
a_additem(menu_t * pm, const fileheader_t * myheader)
{
char buf[PATHLEN];
setadir(buf, pm->path);
if (append_record(buf, myheader, FHSZ) == -1)
return;
pm->now = pm->num++;
if (pm->now >= pm->page + p_lines) {
pm->page = pm->now - ((pm->page == 10000 && pm->now > p_lines / 2) ?
(p_lines / 2) : (pm->now % p_lines));
}
/* Ptt */
strlcpy(pm->header[pm->now - pm->page].filename,
myheader->filename,
sizeof(pm->header[pm->now - pm->page].filename));
}
#define ADDITEM 0
#define ADDGROUP 1
#define ADDLINK 2
static void
a_newitem(menu_t * pm, int mode)
{
char *mesg[3] = {
"[新增文章] 請輸入標題:", /* ADDITEM */
"[新增目錄] 請輸入標題:", /* ADDGROUP */
"請輸入標題:" /* ADDLINK */
};
char fpath[PATHLEN], buf[PATHLEN], lpath[PATHLEN];
fileheader_t item;
int d;
strlcpy(fpath, pm->path, sizeof(fpath));
switch (mode) {
case ADDITEM:
stampfile(fpath, &item);
strlcpy(item.title, "◇ ", sizeof(item.title)); /* A1BA */
break;
case ADDGROUP:
stampdir(fpath, &item);
strlcpy(item.title, "◆ ", sizeof(item.title)); /* A1BB */
break;
case ADDLINK:
stamplink(fpath, &item);
if (!getdata(b_lines - 2, 1, "新增連線:", buf, 61, DOECHO))
return;
if (invalid_pname(buf)) {
unlink(fpath);
outs("目的地路徑不合法!");
igetch();
return;
}
item.title[0] = 0;
// XXX Is it alright?
// ex: path= "PASSWD"
for (d = 0; d <= 3; d++) {
switch (d) {
case 0:
snprintf(lpath, sizeof(lpath), BBSHOME "/man/boards/%c/%s/%s",
currboard[0], currboard, buf);
break;
case 1:
snprintf(lpath, sizeof(lpath), BBSHOME "/man/boards/%c/%s",
buf[0], buf);
break;
case 2:
snprintf(lpath, sizeof(lpath), BBSHOME "/%s",
buf);
break;
case 3:
snprintf(lpath, sizeof(lpath), BBSHOME "/etc/%s",
buf);
break;
}
if (dashf(lpath)) {
strlcpy(item.title, "☆ ", sizeof(item.title)); /* A1B3 */
break;
} else if (dashd(lpath)) {
strlcpy(item.title, "★ ", sizeof(item.title)); /* A1B4 */
break;
}
if (!HasUserPerm(PERM_BBSADM) && d == 1)
break;
}
if (!item.title[0]) {
unlink(fpath);
outs("目的地路徑不合法!");
igetch();
return;
}
}
if (!getdata(b_lines - 1, 1, mesg[mode], &item.title[3], 55, DOECHO)) {
if (mode == ADDGROUP)
rmdir(fpath);
else
unlink(fpath);
return;
}
switch (mode) {
case ADDITEM:
if (vedit(fpath, 0, NULL) == -1) {
unlink(fpath);
pressanykey();
return;
}
break;
case ADDLINK:
unlink(fpath);
if (symlink(lpath, fpath) == -1) {
outs("無法建立 symbolic link");
igetch();
return;
}
break;
}
strlcpy(item.owner, cuser.userid, sizeof(item.owner));
a_additem(pm, &item);
}
void
a_pasteitem(menu_t * pm, int mode)
{
char newpath[PATHLEN];
char buf[PATHLEN];
char ans[2], skipAll = 0, multiple = 0;
int i, copied = 0;
fileheader_t item;
CopyQueue *cq;
move(b_lines - 1, 0);
if(copyqueue_querysize() <= 0)
{
vmsg("請先執行複製(copy)命令後再貼上(paste)");
return;
}
if(mode && copyqueue_querysize() > 1)
{
multiple = 1;
move(b_lines-2, 0); clrtobot();
outs("c: 對各項目個別確認是否要貼上, z: 全部不貼,同時重設並取消全部標記\n");
snprintf(buf, sizeof(buf),
"確定要貼上全部共 %d 個項目嗎 (c/z/y/N)? ",
copyqueue_querysize());
getdata(b_lines - 1, 0, buf, ans, sizeof(ans), LCECHO);
if(ans[0] == 'y')
skipAll = 1;
else if(ans[0] == 'z')
{
copyqueue_reset();
vmsg("已重設複製記錄。");
return;
}
else if (ans[0] != 'c')
return;
clear();
}
while (copyqueue_querysize() > 0)
{
cq = copyqueue_gethead();
if(!cq->copyfile[0])
continue;
if(mode && multiple)
{
scroll();
move(b_lines-2, 0); clrtobot();
prints("%d. %s\n", ++copied,cq->copytitle);
}
if (dashd(cq->copyfile)) {
for (i = 0; cq->copyfile[i] && cq->copyfile[i] == pm->path[i]; i++);
if (!cq->copyfile[i]) {
vmsg("將目錄拷進自己的子目錄中,會造成無窮迴圈!");
continue;
}
}
if (mode && !skipAll) {
snprintf(buf, sizeof(buf),
"確定要拷貝[%s]嗎(Y/N)?[N] ", cq->copytitle);
getdata(b_lines - 1, 0, buf, ans, sizeof(ans), LCECHO);
} else
ans[0] = 'y';
if (ans[0] == 'y') {
strlcpy(newpath, pm->path, sizeof(newpath));
if (*cq->copyowner) {
char *fname = strrchr(cq->copyfile, '/');
if (fname)
strcat(newpath, fname);
else
return;
if (access(pm->path, X_OK | R_OK | W_OK))
mkdir(pm->path, 0755);
memset(&item, 0, sizeof(fileheader_t));
strlcpy(item.filename, fname + 1, sizeof(item.filename));
memcpy(cq->copytitle, "◎", 2);
Copy(cq->copyfile, newpath);
} else if (dashf(cq->copyfile)) {
stampfile(newpath, &item);
memcpy(cq->copytitle, "◇", 2);
Copy(cq->copyfile, newpath);
} else if (dashd(cq->copyfile)) {
stampdir(newpath, &item);
memcpy(cq->copytitle, "◆", 2);
copy_file(cq->copyfile, newpath);
} else {
copyqueue_reset();
vmsg("無法拷貝!");
return;
}
strlcpy(item.owner, *cq->copyowner ? cq->copyowner : cuser.userid,
sizeof(item.owner));
strlcpy(item.title, cq->copytitle, sizeof(item.title));
a_additem(pm, &item);
cq->copyfile[0] = '\0';
}
}
}
static void
a_appenditem(const menu_t * pm, int isask)
{
char fname[PATHLEN];
char buf[ANSILINELEN];
char ans[2] = "y";
FILE *fp, *fin;
move(b_lines - 1, 1);
if(copyqueue_querysize() <= 0)
{
vmsg("請先執行 copy 命令後再 append");
copyqueue_reset();
return;
}
else
{
CopyQueue *cq = copyqueue_gethead();
if (dashf(cq->copyfile)) {
snprintf(fname, sizeof(fname), "%s/%s", pm->path,
pm->header[pm->now - pm->page].filename);
if (dashf(fname)) {
if (isask) {
snprintf(buf, sizeof(buf),
"確定要將[%s]附加於此嗎(Y/N)?[N] ", cq->copytitle);
getdata(b_lines - 2, 1, buf, ans, sizeof(ans), LCECHO);
}
if (ans[0] == 'y') {
if ((fp = fopen(fname, "a+"))) {
if ((fin = fopen(cq->copyfile, "r"))) {
memset(buf, '-', 74);
buf[74] = '\0';
fprintf(fp, "\n> %s <\n\n", buf);
if (isask)
getdata(b_lines - 1, 1,
"是否收錄簽名檔部份(Y/N)?[Y] ",
ans, sizeof(ans), LCECHO);
while (fgets(buf, sizeof(buf), fin)) {
if ((ans[0] == 'n') &&
!strcmp(buf, "--\n"))
break;
fputs(buf, fp);
}
fclose(fin);
cq->copyfile[0] = '\0';
}
fclose(fp);
}
}
} else {
vmsg("檔案不得附加於此!");
}
} else {
vmsg("目錄不得附加於檔案後!");
}
}
}
static int
a_pastetagpost(menu_t * pm, int mode)
{
fileheader_t fhdr;
boardheader_t *bh = NULL;
int ans = 0, ent = 0, tagnum;
char title[TTLEN + 1] = "◇ ";
char dirname[200], buf[200];
if (TagBoard == 0){
sethomedir(dirname, cuser.userid);
}
else{
bh = getbcache(TagBoard);
setbdir(dirname, bh->brdname);
}
tagnum = TagNum;
if (!tagnum)
return ans;
while (tagnum--) {
EnumTagFhdr(&fhdr, dirname, ent++);
if (TagBoard == 0)
sethomefile(buf, cuser.userid, fhdr.filename);
else
setbfile(buf, bh->brdname, fhdr.filename);
if (dashf(buf)) {
strncpy(title + 3, fhdr.title, TTLEN - 3);
title[TTLEN] = '\0';
a_copyitem(buf, title, 0, 0);
if (mode) {
mode--;
a_pasteitem(pm, 0);
} else
a_appenditem(pm, 0);
++ans;
UnTagger(tagnum);
}
}
return ans;
}
static void
a_moveitem(menu_t * pm)
{
fileheader_t *tmp;
char newnum[4];
int num, max, min;
char buf[PATHLEN];
int fail;
snprintf(buf, sizeof(buf), "請輸入第 %d 選項的新次序:", pm->now + 1);
if (!getdata(b_lines - 1, 1, buf, newnum, sizeof(newnum), DOECHO))
return;
num = (newnum[0] == '$') ? 9999 : atoi(newnum) - 1;
if (num >= pm->num)
num = pm->num - 1;
else if (num < 0)
num = 0;
setadir(buf, pm->path);
min = num < pm->now ? num : pm->now;
max = num > pm->now ? num : pm->now;
tmp = (fileheader_t *) calloc(max + 1, FHSZ);
fail = 0;
if (get_records(buf, tmp, FHSZ, 1, min) != min)
fail = 1;
if (num > pm->now) {
if (get_records(buf, &tmp[min], FHSZ, pm->now + 2, max - min) != max - min)
fail = 1;
if (get_records(buf, &tmp[max], FHSZ, pm->now + 1, 1) != 1)
fail = 1;
} else {
if (get_records(buf, &tmp[min], FHSZ, pm->now + 1, 1) != 1)
fail = 1;
if (get_records(buf, &tmp[min + 1], FHSZ, num + 1, max - min) != max - min)
fail = 1;
}
if (!fail)
substitute_record(buf, tmp, FHSZ * (max + 1), 1);
pm->now = num;
free(tmp);
}
static void
a_delrange(menu_t * pm)
{
char fname[256];
snprintf(fname, sizeof(fname), "%s/.DIR", pm->path);
del_range(0, NULL, fname);
pm->num = get_num_records(fname, FHSZ);
}
static void
a_delete(menu_t * pm)
{
char fpath[PATHLEN], buf[PATHLEN], cmd[PATHLEN];
char ans[4];
fileheader_t backup;
snprintf(fpath, sizeof(fpath),
"%s/%s", pm->path, pm->header[pm->now - pm->page].filename);
setadir(buf, pm->path);
if (pm->header[pm->now - pm->page].filename[0] == 'H' &&
pm->header[pm->now - pm->page].filename[1] == '.') {
getdata(b_lines - 1, 1, "您確定要刪除此精華區連線嗎(Y/N)?[N] ",
ans, sizeof(ans), LCECHO);
if (ans[0] != 'y')
return;
if (delete_record(buf, FHSZ, pm->now + 1) == -1)
return;
} else if (dashl(fpath)) {
getdata(b_lines - 1, 1, "您確定要刪除此 symbolic link 嗎(Y/N)?[N] ",
ans, sizeof(ans), LCECHO);
if (ans[0] != 'y')
return;
if (delete_record(buf, FHSZ, pm->now + 1) == -1)
return;
unlink(fpath);
} else if (dashf(fpath)) {
getdata(b_lines - 1, 1, "您確定要刪除此檔案嗎(Y/N)?[N] ", ans,
sizeof(ans), LCECHO);
if (ans[0] != 'y')
return;
if (delete_record(buf, FHSZ, pm->now + 1) == -1)
return;
setbpath(buf, "deleted");
stampfile(buf, &backup);
strlcpy(backup.owner, cuser.userid, sizeof(backup.owner));
strlcpy(backup.title,
pm->header[pm->now - pm->page].title + 2,
sizeof(backup.title));
snprintf(cmd, sizeof(cmd),
"mv -f %s %s", fpath, buf);
system(cmd);
setbdir(buf, "deleted");
append_record(buf, &backup, sizeof(backup));
} else if (dashd(fpath)) {
getdata(b_lines - 1, 1, "您確定要刪除整個目錄嗎(Y/N)?[N] ", ans,
sizeof(ans), LCECHO);
if (ans[0] != 'y')
return;
if (delete_record(buf, FHSZ, pm->now + 1) == -1)
return;
setapath(buf, "deleted");
stampdir(buf, &backup);
snprintf(cmd, sizeof(cmd),
"rm -rf %s;/bin/mv -f %s %s", buf, fpath, buf);
system(cmd);
strlcpy(backup.owner, cuser.userid, sizeof(backup.owner));
strcpy(backup.title, "◆");
strlcpy(backup.title + 2,
pm->header[pm->now - pm->page].title + 2,
sizeof(backup.title) - 3);
setapath(buf, "deleted");
setadir(buf, buf);
append_record(buf, &backup, sizeof(backup));
} else { /* Ptt 損毀的項目 */
getdata(b_lines - 1, 1, "您確定要刪除此損毀的項目嗎(Y/N)?[N] ",
ans, sizeof(ans), LCECHO);
if (ans[0] != 'y')
return;
if (delete_record(buf, FHSZ, pm->now + 1) == -1)
return;
}
pm->num--;
}
static void
a_newtitle(const menu_t * pm)
{
char buf[PATHLEN];
fileheader_t item;
memcpy(&item, &pm->header[pm->now - pm->page], FHSZ);
strlcpy(buf, item.title + 3, sizeof(buf));
if (getdata_buf(b_lines - 1, 1, "新標題:", buf, 60, DOECHO)) {
strlcpy(item.title + 3, buf, sizeof(item.title) - 3);
setadir(buf, pm->path);
substitute_record(buf, &item, FHSZ, pm->now + 1);
}
}
static void
a_hideitem(const menu_t * pm)
{
fileheader_t *item = &pm->header[pm->now - pm->page];
char buf[PATHLEN];
if (item->filemode & FILE_BM) {
item->filemode &= ~FILE_BM;
item->filemode &= ~FILE_HIDE;
} else if (item->filemode & FILE_HIDE)
item->filemode |= FILE_BM;
else
item->filemode |= FILE_HIDE;
setadir(buf, pm->path);
substitute_record(buf, item, FHSZ, pm->now + 1);
}
static void
a_editsign(const menu_t * pm)
{
char buf[PATHLEN];
fileheader_t item;
memcpy(&item, &pm->header[pm->now - pm->page], FHSZ);
snprintf(buf, sizeof(buf), "%c%c", item.title[0], item.title[1]);
if (getdata_buf(b_lines - 1, 1, "符號", buf, 5, DOECHO)) {
item.title[0] = buf[0] ? buf[0] : ' ';
item.title[1] = buf[1] ? buf[1] : ' ';
item.title[2] = buf[2] ? buf[2] : ' ';
setadir(buf, pm->path);
substitute_record(buf, &item, FHSZ, pm->now + 1);
}
}
static void
a_showname(const menu_t * pm)
{
char buf[PATHLEN];
int len;
int i;
int sym;
move(b_lines - 1, 1);
snprintf(buf, sizeof(buf),
"%s/%s", pm->path, pm->header[pm->now - pm->page].filename);
if (dashl(buf)) {
prints("此 symbolic link 名稱為 %s\n",
pm->header[pm->now - pm->page].filename);
if ((len = readlink(buf, buf, PATHLEN - 1)) >= 0) {
buf[len] = '\0';
for (i = 0; BBSHOME[i] && buf[i] == BBSHOME[i]; i++);
if (!BBSHOME[i] && buf[i] == '/') {
if (HasUserPerm(PERM_BBSADM))
sym = 1;
else {
sym = 0;
for (i++; BBSHOME "/man"[i] && buf[i] == BBSHOME "/man"[i];
i++);
if (!BBSHOME "/man"[i] && buf[i] == '/')
sym = 1;
}
if (sym) {
vmsg("此 symbolic link 指向 %s\n", &buf[i + 1]);
}
}
}
} else if (dashf(buf))
prints("此文章名稱為 %s", pm->header[pm->now - pm->page].filename);
else if (dashd(buf))
prints("此目錄名稱為 %s", pm->header[pm->now - pm->page].filename);
else
outs("此項目已損毀, 建議將其刪除!");
pressanykey();
}
static int
isvisible_man(const menu_t * me)
{
fileheader_t *fhdr = &me->header[me->now - me->page];
if (me->level < MANAGER && ((fhdr->filemode & FILE_BM) ||
((fhdr->filemode & FILE_HIDE) &&
/* board friend only effact when
* in board reading mode */
(currstat == ANNOUNCE ||
hbflcheck(currbid, currutmp->uid))
)))
return 0;
return 1;
}
int
a_menu(const char *maintitle, char *path, int lastlevel, char *trans_buffer)
{
static char Fexit; // 用來跳出 recursion
menu_t me;
char fname[PATHLEN];
int ch, returnvalue = FULLUPDATE;
if(trans_buffer)
trans_buffer[0] = '\0';
Fexit = 0;
me.header = (fileheader_t *) calloc(p_lines, FHSZ);
me.path = path;
strlcpy(me.mtitle, maintitle, sizeof(me.mtitle));
setadir(fname, me.path);
me.num = get_num_records(fname, FHSZ);
/* 精華區-tree 中部份結構屬於 cuser ==> BM */
if (!(me.level = lastlevel)) {
char *ptr;
if ((ptr = strrchr(me.mtitle, '[')))
me.level = is_BM(ptr + 1);
}
me.page = 9999;
me.now = 0;
for (;;) {
if (me.now >= me.num)
me.now = me.num - 1;
if (me.now < 0)
me.now = 0;
if (me.now < me.page || me.now >= me.page + p_lines) {
me.page = me.now - ((me.page == 10000 && me.now > p_lines / 2) ?
(p_lines / 2) : (me.now % p_lines));
a_showmenu(&me);
}
ch = cursor_key(2 + me.now - me.page, 0);
if (ch == 'q' || ch == 'Q' || ch == KEY_LEFT)
break;
if (ch >= '1' && ch <= '9') {
if ((ch = search_num(ch, me.num)) != -1)
me.now = ch;
me.page = 10000;
continue;
}
switch (ch) {
case KEY_UP:
case 'k':
if (--me.now < 0)
me.now = me.num - 1;
break;
case KEY_DOWN:
case 'j':
if (++me.now >= me.num)
me.now = 0;
break;
case KEY_PGUP:
case Ctrl('B'):
if (me.now >= p_lines)
me.now -= p_lines;
else if (me.now > 0)
me.now = 0;
else
me.now = me.num - 1;
break;
case ' ':
case KEY_PGDN:
case Ctrl('F'):
if (me.now < me.num - p_lines)
me.now += p_lines;
else if (me.now < me.num - 1)
me.now = me.num - 1;
else
me.now = 0;
break;
case '0':
me.now = 0;
break;
case '?':
case '/':
me.now = a_searchtitle(&me, ch == '?');
me.page = 9999;
break;
case '$':
me.now = me.num - 1;
break;
case 'h':
a_showhelp(me.level);
me.page = 9999;
break;
case Ctrl('I'):
t_idle();
me.page = 9999;
break;
case 'e':
case 'E':
snprintf(fname, sizeof(fname),
"%s/%s", path, me.header[me.now - me.page].filename);
if (dashf(fname) && me.level >= MANAGER) {
*quote_file = 0;
if (vedit(fname, NA, NULL) != -1) {
char fpath[200];
fileheader_t fhdr;
strlcpy(fpath, path, sizeof(fpath));
stampfile(fpath, &fhdr);
unlink(fpath);
Rename(fname, fpath);
strlcpy(me.header[me.now - me.page].filename,
fhdr.filename,
sizeof(me.header[me.now - me.page].filename));
strlcpy(me.header[me.now - me.page].owner,
cuser.userid,
sizeof(me.header[me.now - me.page].owner));
setadir(fpath, path);
substitute_record(fpath, me.header + me.now - me.page,
sizeof(fhdr), me.now + 1);
}
me.page = 9999;
}
break;
case 'c':
if (me.now < me.num) {
if (!isvisible_man(&me))
break;
snprintf(fname, sizeof(fname), "%s/%s", path,
me.header[me.now - me.page].filename);
/* XXX: dirty fix
應該要改成如果發現該目錄裡面有隱形目錄的話才拒絕.
不過這樣的話須要整個搜一遍, 而且目前判斷該資料是目錄
還是檔案竟然是用 fstat(2) 而不是直接存在 .DIR 內 |||b
須等該資料寫入 .DIR 內再 implement才有效率.
*/
if( !lastlevel && !HasUserPerm(PERM_SYSOP) &&
!is_BM_cache(currbid) && dashd(fname) )
vmsg("只有板主才可以拷貝目錄唷!");
else
a_copyitem(fname, me.header[me.now - me.page].title, 0, 1);
me.page = 9999;
break;
}
case '\n':
case '\r':
case KEY_RIGHT:
case 'r':
if (me.now < me.num) {
fileheader_t *fhdr = &me.header[me.now - me.page];
if (!isvisible_man(&me))
break;
#ifdef DEBUG
vmsg("%s/%s", &path[11], fhdr->filename);;
#endif
snprintf(fname, sizeof(fname), "%s/%s", path, fhdr->filename);
if (*fhdr->filename == 'H' && fhdr->filename[1] == '.') {
vmsg("不再支援 gopher mode, 請使用瀏覽器直接瀏覽");
vmsg("gopher://%s/1/",fhdr->filename+2);
} else if (dashf(fname)) {
int more_result;
while ((more_result = more(fname, YEA))) {
/* Ptt 範本精靈 plugin */
if (trans_buffer &&
(currstat == EDITEXP || currstat == OSONG)) {
char ans[4];
move(22, 0);
clrtoeol();
getdata(22, 1,
currstat == EDITEXP ?
"要把範例 Plugin 到文章嗎?[y/N]" :
"確定要點這首歌嗎?[y/N]",
ans, sizeof(ans), LCECHO);
if (ans[0] == 'y') {
strlcpy(trans_buffer, fname, 256);
Fexit = 1;
if (currstat == OSONG) {
/* XXX: 只選歌未點歌可灌排行榜 */
log_file(FN_USSONG, LOG_CREAT | LOG_VF,
"%s\n", fhdr->title);
}
free(me.header);
return FULLUPDATE;
}
}
if (more_result == READ_PREV) {
if (--me.now < 0) {
me.now = 0;
break;
}
} else if (more_result == READ_NEXT) {
if (++me.now >= me.num) {
me.now = me.num - 1;
break;
}
/* we only load p_lines pages */
if (me.now - me.page >= p_lines)
break;
} else
break;
if (!isvisible_man(&me))
break;
snprintf(fname, sizeof(fname), "%s/%s", path,
me.header[me.now - me.page].filename);
if (!dashf(fname))
break;
}
} else if (dashd(fname)) {
a_menu(me.header[me.now - me.page].title, fname, me.level, trans_buffer);
/* Ptt 強力跳出recursive */
if (Fexit) {
free(me.header);
return FULLUPDATE;
}
}
me.page = 9999;
}
break;
case 'F':
case 'U':
snprintf(fname, sizeof(fname),
"%s/%s", path, me.header[me.now - me.page].filename);
if (me.now < me.num && HasUserPerm(PERM_LOGINOK) && dashf(fname)) {
a_forward(path, &me.header[me.now - me.page], ch /* == 'U' */ );
/* By CharlieL */
} else
vmsg("無法轉寄此項目");
me.page = 9999;
break;
#ifdef BLOG
case 'b':
if( !HasUserPerm(PERM_SYSOP) && !is_BM_cache(currbid) )
vmsg("只有板主才可以用唷!");
else{
char genbuf[128];
snprintf(genbuf, sizeof(genbuf),
"bin/builddb.pl -f -n %d %s", me.now, currboard);
system(genbuf);
vmsg("資料更新完成");
}
me.page = 9999;
break;
case 'B':
if( !HasUserPerm(SYSOP) && !is_BM_cache(currbid) )
vmsg("只有板主才可以用唷!");
else
BlogMain(me.now);
me.page = 9999;
break;
#endif
}
if (me.level >= MANAGER) {
int page0 = me.page;
switch (ch) {
case 'n':
a_newitem(&me, ADDITEM);
me.page = 9999;
break;
case 'g':
a_newitem(&me, ADDGROUP);
me.page = 9999;
break;
case 'p':
a_pasteitem(&me, 1);
me.page = 9999;
break;
case 'f':
a_editsign(&me);
me.page = 9999;
break;
case Ctrl('P'):
a_pastetagpost(&me, -1);
returnvalue = DIRCHANGED;
me.page = 9999;
break;
case Ctrl('A'):
a_pastetagpost(&me, 1);
returnvalue = DIRCHANGED;
me.page = 9999;
break;
case 'a':
a_appenditem(&me, 1);
me.page = 9999;
break;
default:
me.page = page0;
break;
}
if (me.num)
switch (ch) {
case 'm':
a_moveitem(&me);
me.page = 9999;
break;
case 'D':
/* Ptt me.page = -1; */
a_delrange(&me);
me.page = 9999;
break;
case 'd':
a_delete(&me);
me.page = 9999;
break;
case 'H':
a_hideitem(&me);
me.page = 9999;
break;
case 'T':
a_newtitle(&me);
me.page = 9999;
break;
}
}
if (me.level == SYSOP) {
switch (ch) {
case 'l':
a_newitem(&me, ADDLINK);
me.page = 9999;
break;
case 'N':
a_showname(&me);
me.page = 9999;
break;
}
}
}
free(me.header);
return returnvalue;
}
static char *mytitle = BBSNAME "佈告欄";
int
Announce(void)
{
setutmpmode(ANNOUNCE);
a_menu(mytitle, "man",
((HasUserPerm(PERM_SYSOP) ) ? SYSOP : NOBODY),
NULL);
return 0;
}
#ifdef BLOG
#include <mysql/mysql.h>
void BlogMain(int num)
{
int oldmode = currutmp->mode;
char genbuf[128], exit = 0;
//setutmpmode(BLOGGING); /* will crash someone using old program */
sprintf(genbuf, "%s的部落格", currboard);
showtitle("部落格", genbuf);
while( !exit ){
move(1, 0);
outs("請選擇您要執行的重作:\n"
"0.回到上一層\n"
"1.製作部落格樣板格式\n"
" 使用新的 config 目錄下樣板資料\n"
" 通常在第一次使用部落格或樣板更新的時候使用\n"
"\n"
"2.重新製作部落格\n"
" 只在部落格資料整個亂掉的時候才使用\n"
"\n"
"3.將本文加入部落格\n"
" 將游標所在位置的文章加入部落格\n"
"\n"
"4.刪除迴響\n"
"\n"
"5.刪除一篇部落格\n"
"\n"
"C.建立部落格目錄 (您只有第一次時需要)\n"
);
switch( getans("請選擇(0-5,C)?[0]") ){
case '1':
snprintf(genbuf, sizeof(genbuf),
"bin/builddb.pl -c %s", currboard);
system(genbuf);
break;
case '2':
snprintf(genbuf, sizeof(genbuf),
"bin/builddb.pl -a %s", currboard);
system(genbuf);
break;
case '3':
snprintf(genbuf, sizeof(genbuf),
"bin/builddb.pl -f -n %d %s", num, currboard);
system(genbuf);
break;
case '4':{
char hash[33];
int i;
getdata(16, 0, "請輸入該篇的雜湊值: ",
hash, sizeof(hash), DOECHO);
for( i = 0 ; hash[i] != 0 ; ++i ) /* 前面用 getdata() 保證有 \0 */
if( !islower(hash[i]) && !isnumber(hash[i]) )
break;
if( i != 32 ){
vmsg("輸入錯誤");
break;
}
if( hash[0] != 0 &&
getans("請確定刪除(Y/N)?[N] ") == 'y' ){
MYSQL mysql;
char cmd[256];
sprintf(cmd, "delete from comment where "
"hash='%s'&&brdname='%s'", hash, currboard);
#ifdef DEBUG
vmsg(cmd);
#endif
if( !(!mysql_init(&mysql) ||
!mysql_real_connect(&mysql, BLOGDB_HOST, BLOGDB_USER,
BLOGDB_PASSWD, BLOGDB_DB,
BLOGDB_PORT, BLOGDB_SOCK, 0) ||
mysql_query(&mysql, cmd)) )
vmsg("資料刪除完成");
else
vmsg(
#ifdef DEBUG
mysql_error(&mysql)
#else
"database internal error"
#endif
);
mysql_close(&mysql);
}
}
break;
case '5': {
char date[9];
int i;
getdata(16, 0, "請輸入該篇的日期(yyyymmdd): ",
date, sizeof(date), DOECHO);
for( i = 0 ; i < 9 ; ++i )
if( !isdigit(date[i]) )
break;
if( i != 8 ){
vmsg("輸入錯誤");
break;
}
snprintf(genbuf, sizeof(genbuf),
"bin/builddb.pl -D %s %s", date, currboard);
system(genbuf);
}
break;
case 'C': case 'c': {
fileheader_t item;
char fpath[PATHLEN], adir[PATHLEN], buf[256];
setapath(fpath, currboard);
stampdir(fpath, &item);
strlcpy(item.title, "◆ Blog", sizeof(item.title));
strlcpy(item.owner, cuser.userid, sizeof(item.owner));
setapath(adir, currboard);
strcat(adir, "/.DIR");
append_record(adir, &item, FHSZ);
snprintf(buf, sizeof(buf),
"cp -R etc/Blog.Default/.DIR etc/Blog.Default/* %s/",
fpath);
system(buf);
more("etc/README.BLOG", YEA);
}
break;
default:
exit = 1;
break;
}
if( !exit )
vmsg("部落格完成");
}
currutmp->mode = oldmode;
pressanykey();
}
#endif
|