summaryrefslogtreecommitdiffstats
path: root/mbbsd/board.c
blob: a310117501c4b021b4ed4c9007ac5aade3592572 (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
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
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
/* $Id$ */
#include "bbs.h"

/* personal board state
 * 相對於看板的 attr (BRD_* in ../include/pttstruct.h),
 * 這些是用在 user interface 的 flag */
#define NBRD_FAV         1
#define NBRD_BOARD   2
#define NBRD_LINE        4
#define NBRD_FOLDER  8
#define NBRD_TAG    16
#define NBRD_UNREAD     32
#define NBRD_SYMBOLIC   64

#define TITLE_MATCH(bptr, key)  ((key)[0] && !strcasestr((bptr)->title, (key)))

#define B_TOTAL(bptr)        (SHM->total[(bptr)->bid - 1])
#define B_LASTPOSTTIME(bptr) (SHM->lastposttime[(bptr)->bid - 1])
#define B_BH(bptr)           (&bcache[(bptr)->bid - 1])

#define HasFavEditPerm() HasUserPerm(PERM_BASIC)

typedef struct {
    int             bid;
    unsigned char   myattr;
} __attribute__ ((packed)) boardstat_t;

/**
 * class_bid 的意義
 *   class_bid < 0   熱門看板
 *   class_bid = 0   我的最愛
 *   class_bid = 1   分類看板
 *   class_bid > 1   其他目錄
 */
#define IN_HOTBOARD()   (class_bid < 0)
#define IN_FAVORITE()   (class_bid == 0)
#define IN_CLASSROOT()  (class_bid == 1)
#define IN_SUBCLASS()   (class_bid > 1)
#define IN_CLASS()  (class_bid > 0)
static int      class_bid = 0;

static int nbrdsize = 0;
static boardstat_t *nbrd = NULL;
static char choose_board_depth = 0;
static int      brdnum;
static char     yank_flag = 1;

static time4_t   last_save_fav_and_brc;

/* These are all the states yank_flag may be. */
// XXX IS_LISTING_FAV() does not mean we are in favorite.
// That is controlled by IN_FAVORITE().
#define LIST_FAV()         (yank_flag = 0)
#define LIST_BRD()         (yank_flag = 1)
#define IS_LISTING_FAV()   (yank_flag == 0)
#define IS_LISTING_BRD()   (yank_flag == 1)

inline int getbid(const boardheader_t *fh)
{
    return (fh - bcache);
}
inline boardheader_t *getparent(const boardheader_t *fh)
{
    if(fh->parent>0)
    return getbcache(fh->parent);
    else
    return NULL;
}

// 程式中有特別用途所以不得自行 post / 修改的看板
int
is_readonly_board(const char *bname)
{
    return (strcasecmp(bname, BN_SECURITY) == 0 ||
        strcasecmp(bname, BN_ALLPOST ) == 0 );
}

/**
 * @param[in]   boardname   board name, case insensitive
 * @return  0   if success
 *      -1  if not found
 *      -2  permission denied
 *      -3  error
 * @note enter board:
 *  1. setup brc (currbid, currboard, currbrdattr)
 *  2. set currbid, currBM, currmode, currdirect
 *  3. utmp brc_id
 */
int enter_board(const char *boardname)
{
    boardheader_t  *bh;
    int bid;
    char bname[IDLEN+1];
    char bpath[60];
    struct stat     st;

    /* checking ... */
    if (boardname[0] == '\0' || !(bid = getbnum(boardname)))
    return -1;
    assert(0<=bid-1 && bid-1<MAX_BOARD);
    bh = getbcache(bid);
    if (!HasBoardPerm(bh))
    return -2;
    if (IS_GROUP(bh))
    return -1;

    strlcpy(bname, bh->brdname, sizeof(bname));
    if (bname[0] == '\0')
    return -3;

    setbpath(bpath, bname);
    if (stat(bpath, &st) == -1) {
    return -3;
    }

    /* really enter board */
    brc_update();
    brc_initial_board(bname);
    setutmpbid(currbid);

    set_board();
    setbdir(currdirect, currboard);
    curredit &= ~EDIT_MAIL;

    return 0;
}


static void imovefav(int old)
{
    char buf[5];
    int new;
    
    getdata(b_lines - 1, 0, "請輸入新次序:", buf, sizeof(buf), DOECHO);
    new = atoi(buf) - 1;
    if (new < 0 || brdnum <= new){
    vmsg("輸入範圍有誤!");
    return;
    }
    move_in_current_folder(old, new);
}

void
init_brdbuf(void)
{
    if (brc_initialize())
    return;
}

void
save_brdbuf(void)
{
    fav_save();
    fav_free();
}

int
HasBoardPerm(boardheader_t *bptr)
{
    register int    level, brdattr;

    level = bptr->level;
    brdattr = bptr->brdattr;

    if (HasUserPerm(PERM_SYSOP))
    return 1;

    // allow POLICE to enter BM boards
    if ((level & PERM_BM) && 
    (HasUserPerm(PERM_POLICE) || HasUserPerm(PERM_POLICE_MAN)))
    return 1;

    /* 板主 */
    if( is_BM_cache(bptr - bcache + 1) ) /* XXXbid */
    return 1;

    /* 祕密看板:核對首席板主的好友名單 */
    if (brdattr & BRD_HIDE) {   /* 隱藏 */
    if (!is_hidden_board_friend((int)(bptr - bcache) + 1, currutmp->uid)) {
        if (brdattr & BRD_POSTMASK)
        return 0;
        else
        return 2;
    } else
        return 1;
    }

    /* 十八禁看板 */
    if( (brdattr & BRD_OVER18) && !over18 )
    return 0;

    /* 限制閱讀權限 */
    if (level && !(brdattr & BRD_POSTMASK) && !HasUserPerm(level))
    return 0;

    return 1;
}

// board configuration utilities

static int
b_post_note(void)
{
    char            buf[PATHLEN], yn[3];

    // if(!(currmode & MODE_BOARD)) return DONOTHING;
    vs_hdr("自訂注意事項");

    setbfile(buf, currboard, FN_POST_NOTE);
    move(b_lines-2, 0); clrtobot();

    if (more(buf, NA) == -1)
    more("etc/" FN_POST_NOTE, NA);

    getdata(b_lines - 2, 0, "自訂發文注意事項: (y)編輯/(d)刪除/(n)不變? [y/N/d]:",
        yn, sizeof(yn), LCECHO);
    if (yn[0] == 'y')
    veditfile(buf);
    else if (yn[0] == 'd')
    unlink(buf);

    return FULLUPDATE;
}

static int
b_posttype()
{
   boardheader_t  *bp;
   int i, aborted;
   char filepath[PATHLEN], genbuf[60], title[5], posttype_f, posttype[33]="";

   // if(!(currmode & MODE_BOARD)) return DONOTHING;
   
   assert(0<=currbid-1 && currbid-1<MAX_BOARD);
   bp = getbcache(currbid);
   vs_hdr("設定文章類別");

   move(2,0);
   clrtobot();
   posttype_f = bp->posttype_f;
   for( i = 0 ; i < 8 ; ++i ){
       move(2+i,0);
       outs("文章種類:       ");
       strlcpy(genbuf, bp->posttype + i * 4, 5);
       sprintf(title, "%d.", i + 1);
       if( !getdata_buf(2+i, 11, title, genbuf, 5, DOECHO) )
       break;
       sprintf(posttype + i * 4, "%-4.4s", genbuf); 
       if( posttype_f & (1<<i) ){
       if( getdata(2+i, 20, "設定範本格式?(Y/n)", genbuf, 3, LCECHO) &&
           genbuf[0]=='n' ){
           posttype_f &= ~(1<<i);
           continue;
       }
       }
       else if ( !getdata(2+i, 20, "設定範本格式?(y/N)", genbuf, 3, LCECHO) ||
         genbuf[0] != 'y' )
       continue;

       setbnfile(filepath, bp->brdname, "postsample", i);
       aborted = veditfile(filepath);
       if (aborted == -1) {
           clear();
           posttype_f &= ~(1<<i);
           continue;
       }
       posttype_f |= (1<<i);
   }
   bp->posttype_f = posttype_f; 
   strlcpy(bp->posttype, posttype, sizeof(bp->posttype)); /* 這邊應該要防race condition */

   assert(0<=currbid-1 && currbid-1<MAX_BOARD);
   substitute_record(fn_board, bp, sizeof(boardheader_t), currbid);
   return FULLUPDATE;
}

// integrated board config
int
b_config(void)
{
    boardheader_t   *bp=NULL;
    int touched = 0, finished = 0;
    int i = 0, attr = 0, ipostres;
    char isBM = (currmode & MODE_BOARD) || HasUserPerm(PERM_SYSOP);
    // perm cache
    char hasres = 0, 
     cachePostPerm = CheckPostPerm(), 
     cachePostRes  = CheckPostRestriction(currbid);
    char canpost = (cachePostPerm && cachePostRes);

#define LNBOARDINFO (18)
#define LNPOSTRES   (12)
#define COLPOSTRES  (50)

    int ytitle = b_lines - LNBOARDINFO;

    bp = getbcache(currbid); 

#ifdef OLDRECOMMEND
    ytitle ++;
#endif  // OLDRECOMMEND

    grayout(0, ytitle-2, GRAYOUT_DARK);

    // available hotkeys yet:
    // a b d j k p q z
    // 2 3 4 5 6 7 9
    // better not: l 0

#define CANTPOSTMSG ANSI_COLOR(1;31) "(您未達限制)" ANSI_RESET

    while(!finished) {
    // limits
    uint8_t lpost  = bp->post_limit_posts,
        lreg   = bp->post_limit_regtime,
        lbp    = bp->post_limit_badpost;

    move(ytitle-1, 0); clrtobot();
    // outs(MSG_SEPERATOR); // deprecated by grayout
    outs("\n" ANSI_REVERSE); // now (ytitle, 0);
    vbarf(" 《%s》看板設定", bp->brdname);

    move(ytitle +2, 0);
    clrtobot();

    prints(" "ANSI_COLOR(1;36) "b" ANSI_RESET " - 中文敘述: %s\n", bp->title);
    prints("     板主名單: %s\n", (bp->BM[0] > ' ')? bp->BM : "(無)");

    outs(" \n"); // at least one character, for move_ansi.

    prints( " " ANSI_COLOR(1;36) "h" ANSI_RESET 
        " - 公開狀態(是否隱形): %s " ANSI_RESET "\n", 
        (bp->brdattr & BRD_HIDE) ? 
        ANSI_COLOR(1)"隱形":"公開");

    prints( " " ANSI_COLOR(1;36) "g" ANSI_RESET 
        " - 隱板時 %s 進入十大排行榜" ANSI_RESET "\n", 
        (bp->brdattr & BRD_BMCOUNT) ? 
        ANSI_COLOR(1)"可以" ANSI_RESET: "不可");

    prints( " " ANSI_COLOR(1;36) "e" ANSI_RESET 
        " - %s "ANSI_RESET "非板友發文\n", 
        (bp->brdattr & BRD_RESTRICTEDPOST) ? 
        ANSI_COLOR(1)"不開放" : "開放"
        );

    prints( " " ANSI_COLOR(1;36) "y" ANSI_RESET 
        " - %s" ANSI_RESET
        " 回應文章\n",
        (bp->brdattr & BRD_NOREPLY) ? 
        ANSI_COLOR(1)"不開放" : "開放"
        );

    prints( " " ANSI_COLOR(1;36) "r" ANSI_RESET 
        " - %s " ANSI_RESET "推薦文章\n", 
        (bp->brdattr & BRD_NORECOMMEND) ? 
        ANSI_COLOR(31)"不開放":"開放"
        );

#ifndef OLDRECOMMEND
    prints( " " ANSI_COLOR(1;36) "s" ANSI_RESET
            " - %s " ANSI_RESET "噓文\n", 
        ((bp->brdattr & BRD_NORECOMMEND) || (bp->brdattr & BRD_NOBOO))
        ? ANSI_COLOR(1)"不開放":"開放");
#endif
    {
        int d = 0;

        if(bp->brdattr & BRD_NORECOMMEND)
        {
        d = -1;
        } else {
        if ((bp->brdattr & BRD_NOFASTRECMD) &&
            (bp->fastrecommend_pause > 0))
            d = bp->fastrecommend_pause;
        }

        prints( " " ANSI_COLOR(1;36) "f" ANSI_RESET 
            " - %s " ANSI_RESET "快速連推文章", 
            d != 0 ?
             ANSI_COLOR(1)"限制": "開放");
        if(d > 0)
        prints(", 最低間隔時間: %d 秒", d);
        outs("\n");
    }

    prints( " " ANSI_COLOR(1;36) "i" ANSI_RESET 
        " - 推文時 %s" ANSI_RESET " 記錄來源 IP\n", 
        (bp->brdattr & BRD_IPLOGRECMD) ? 
        ANSI_COLOR(1)"自動":"不會");

    prints( " " ANSI_COLOR(1;36) "a" ANSI_RESET 
        " - 推文時 %s" ANSI_RESET " 開頭\n", 
        (bp->brdattr & BRD_ALIGNEDCMT) ? 
        ANSI_COLOR(1)"對齊":"不用對齊");

#ifdef USE_AUTOCPLOG
    prints( " " ANSI_COLOR(1;36) "x" ANSI_RESET 
        " - 轉錄文章 %s " ANSI_RESET "自動記錄,且 %s " 
        ANSI_RESET "發文權限\n", 
        (bp->brdattr & BRD_CPLOG) ? 
        ANSI_COLOR(1)"會" : "不會" ,
        (bp->brdattr & BRD_CPLOG) ? 
        ANSI_COLOR(1)"需要" : "不需"
        );
#endif

    prints( " " ANSI_COLOR(1;36) "L" ANSI_RESET 
        " - 若有轉信則發文時預設 %s " ANSI_RESET "\n", 
        (bp->brdattr & BRD_LOCALSAVE) ? 
        "站內存檔(不轉出)" : ANSI_COLOR(1)"站際存檔(轉出)" );

    // use '8' instead of '1', to prevent 'l'/'1' confusion
    prints( " " ANSI_COLOR(1;36) "8" ANSI_RESET 
        " - %s" ANSI_RESET "未滿十八歲進入\n",
        (bp->brdattr & BRD_OVER18) ? 
        ANSI_COLOR(1) "禁止 " : "允許\ " );

    if (!canpost)
        outs(ANSI_COLOR(1;31)"  ★ 您在此看板無發文或推文權限,"
        "詳細原因請參考上面顯示為紅色的項目。"ANSI_RESET"\n");

    ipostres = b_lines - LNPOSTRES;
    move_ansi(ipostres++, COLPOSTRES-2);

    if (cachePostPerm && cachePostRes)
        outs(ANSI_COLOR(1;32));
    else
        outs(ANSI_COLOR(31));

    if (bp->brdattr & BRD_VOTEBOARD)
        outs("提出連署限制:" ANSI_RESET);
    else
        outs("發文與推文限制:" ANSI_RESET);

#define POSTRESTRICTION(msg,utag) \
    prints(msg, attr ? ANSI_COLOR(1) : "", i, attr ? ANSI_RESET : "")

    if (bp->brdattr & BRD_VOTEBOARD)
    {
        lpost  = bp->vote_limit_posts;
        lreg   = bp->vote_limit_regtime;
        lbp    = bp->vote_limit_badpost;
    }

    if (lpost)
    {
        move_ansi(ipostres++, COLPOSTRES);
        i = (int)lpost * 10;
        attr = (cuser.numposts < i) ? 1 : 0;
        if (attr) outs(ANSI_COLOR(1;31));
        prints("文章篇數 %d 篇以上", i);
        if (attr) outs(ANSI_RESET);
        hasres = 1;
    }

    if (lreg)
    {
        move_ansi(ipostres++, COLPOSTRES);
        i = lreg;
        attr = (cuser.firstlogin > 
            (now - (time4_t)lreg * MONTH_SECONDS)) ? 1 : 0;
        if (attr) outs(ANSI_COLOR(1;31));
        outs("註冊時間 ");
        if (i < 5)
        prints("%d 天以上", i*30);
        else
        prints("%d 個月以上",i);
        if (attr) outs(ANSI_RESET);
        hasres = 1;
    }

    if (lbp)
    {
        move_ansi(ipostres++, COLPOSTRES);
        i = 255 - lbp;
        attr = (cuser.badpost > i) ? 1 : 0;
        if (attr) outs(ANSI_COLOR(1;31));
        prints("劣文篇數 %d 篇以下", i);
        if (attr) outs(ANSI_RESET);
        hasres = 1;
    }

    if (!cachePostPerm)
    {
        const char *msg = postperm_msg(bp->brdname);
        if (msg) // some reasons
        {
        move_ansi(ipostres++, COLPOSTRES);
        outs(ANSI_COLOR(1;31));
        outs(msg);
        outs(ANSI_RESET);
        }
    } 

    if (!hasres && cachePostPerm) 
    {
        move_ansi(ipostres++, COLPOSTRES);
        outs("無特別限制");
    }

    // show BM commands
    {
        const char *aCat = ANSI_COLOR(1;32);
        const char *aHot = ANSI_COLOR(1;36);
        const char *aRst = ANSI_RESET;

        if (!isBM)
        {
        aCat = ANSI_COLOR(1;30;40);
        aHot = "";
        aRst = "";
        }

        ipostres ++;
        move_ansi(ipostres++, COLPOSTRES-2);
        outs(aCat);
        outs("名單編輯與其它:");
        if (!isBM) outs(" (需板主權限)");
        outs(aRst);
        move_ansi(ipostres++, COLPOSTRES);
        prints("%sv%s)可見名單 %sw%s)水桶名單 ", 
            aHot, aRst, aHot, aRst);
        move_ansi(ipostres++, COLPOSTRES);
        prints("%sm%s)舉辦投票 %so%s)投票名單 ",
            aHot, aRst, aHot, aRst);
        move_ansi(ipostres++, COLPOSTRES);
        prints("%sc%s)文章類別 %sn%s)發文注意事項 ",
            aHot, aRst, aHot, aRst);
        outs(ANSI_RESET);
    }

    if (!isBM)
    {
        pressanykey();
        return FULLUPDATE;
    }

#ifdef NOTIFY_NEW_EDIT_BTITLE
    move(b_lines-1, 0);
    outs(ANSI_COLOR(1;33) 
        "▲提醒您修改板標已整入看板設定中,輸入 b 即可修改板標。" 
        ANSI_RESET);
#endif
    switch(vans("請輸入要改變的設定, 其它鍵結束: "))
    {
#ifdef USE_AUTOCPLOG
        case 'x':
        bp->brdattr ^= BRD_CPLOG;
        touched = 1;
        break;
#endif
        case 'l':
        bp->brdattr ^= BRD_LOCALSAVE;
        touched = 1;
        break;
        
        case 'a':
        bp->brdattr ^= BRD_ALIGNEDCMT;
        touched = 1;
        break;

        case 'b':
        {
            char genbuf[BTLEN+1];
            move(b_lines, 0); clrtoeol();
            SOLVE_ANSI_CACHE();
            outs("請輸入看板新中文敘述: ");
            vgetstr(genbuf, BTLEN-16, 0, bp->title + 7);
            if (!genbuf[0] || strcmp(genbuf, bp->title+7) == 0)
            break;
            touched = 1;
            strip_ansi(genbuf, genbuf, STRIP_ALL);
            strlcpy(bp->title + 7, genbuf, sizeof(bp->title) - 7);
            assert(0<=currbid-1 && currbid-1<MAX_BOARD);
            substitute_record(fn_board, bp, sizeof(boardheader_t), currbid);
            log_usies("SetBoard", currboard);
        }
        break;

        case 'e':
        if(HasUserPerm(PERM_SYSOP))
        {
            bp->brdattr ^= BRD_RESTRICTEDPOST;
            touched = 1;
        } else {
            vmsg("此項設定需要站長權限");
        }
        break;

        case 'h':
#ifndef BMCHS
        if (!HasUserPerm(PERM_SYSOP))
        {
            vmsg("此項設定需要站長權限");
            break;
        }
#endif
        if(bp->brdattr & BRD_HIDE)
        {
            bp->brdattr &= ~BRD_HIDE;
            bp->brdattr &= ~BRD_POSTMASK;
            hbflreload(currbid);
        } else {
            bp->brdattr |= BRD_HIDE;
            bp->brdattr |= BRD_POSTMASK;
        }
        bp->perm_reload = now;
        touched = 1;
        break;

        case 'g':
#ifndef BMCHS
        if (!HasUserPerm(PERM_SYSOP))
        {
            vmsg("此項設定需要站長權限");
            break;
        }
#endif
        bp->brdattr ^= BRD_BMCOUNT;
        touched = 1;
        break;

        case 'r':
        bp->brdattr ^= BRD_NORECOMMEND;
        touched = 1;
        break;

        case 'i':
        bp->brdattr ^= BRD_IPLOGRECMD;
        touched = 1;
        break;

        case 'f':
        bp->brdattr &= ~BRD_NORECOMMEND;
        bp->brdattr ^= BRD_NOFASTRECMD;
        touched = 1;

        if(bp->brdattr & BRD_NOFASTRECMD)
        {
            char buf[8] = "";

            if(bp->fastrecommend_pause > 0)
            sprintf(buf, "%d", bp->fastrecommend_pause);
            getdata_str(b_lines-1, 0, 
                "請輸入連推時間限制(單位: 秒) [5~240]: ",
                buf, 4, NUMECHO, buf);
            if(buf[0] >= '0' && buf[0] <= '9')
            bp->fastrecommend_pause = atoi(buf);

            if( bp->fastrecommend_pause < 5 || 
            bp->fastrecommend_pause > 240)
            {
            if(buf[0])
            {
                vmsg("輸入時間無效,請使用 5~240 之間的數字。");
            }
            bp->fastrecommend_pause = 0;
            bp->brdattr &= ~BRD_NOFASTRECMD;
            }
        }
        break;
#ifndef OLDRECOMMEND
        case 's':
        if(bp->brdattr & BRD_NORECOMMEND)
            bp->brdattr |= BRD_NOBOO;
        bp->brdattr ^= BRD_NOBOO;
        touched = 1;
        if (!(bp->brdattr & BRD_NOBOO))
            bp->brdattr &= ~BRD_NORECOMMEND;
        break;
#endif
        case '8':
        if (!over18)
        {
            vmsg("板主本身未滿 18 歲。");
        } else {
            bp->brdattr ^= BRD_OVER18;
            touched = 1;        
        }
        break;

        case 'v':
        clear();
        friend_edit(BOARD_VISABLE);
        assert(0<=currbid-1 && currbid-1<MAX_BOARD);
        hbflreload(currbid);
        clear();
        break;

        case 'w':
        clear();
        friend_edit(BOARD_WATER);
        clear();
        break;

        case 'o':
        clear();
        friend_edit(FRIEND_CANVOTE);
        clear();

        case 'm':
        clear();
        b_vote_maintain();
        clear();
        break;

        case 'n':
        clear();
        b_post_note();
        clear();
        break;

        case 'c':
        clear();
        b_posttype();
        clear();
        break;

        case 'y':
        if (!(HasUserPerm(PERM_SYSOP) || (HasUserPerm(PERM_SYSSUPERSUBOP) && GROUPOP()) ) ) {
            vmsg("此項設定需要群組長或站長權限");
            break;
        }
        bp->brdattr ^= BRD_NOREPLY;
        touched = 1;        
        break;

        default:
        finished = 1;
        break;
    }
    }
    if(touched)
    {
    assert(0<=currbid-1 && currbid-1<MAX_BOARD);
    substitute_record(fn_board, bp, sizeof(boardheader_t), currbid);
    log_usies("SetBoard", bp->brdname);
    vmsg("已儲存新設定");
    }
    else
    vmsg("未改變任何設定");

    return FULLUPDATE;
}

static int
check_newpost(boardstat_t * ptr)
{               /* Ptt 改 */
    time4_t         ftime;

    ptr->myattr &= ~NBRD_UNREAD;
    if (B_BH(ptr)->brdattr & (BRD_GROUPBOARD | BRD_SYMBOLIC) ||
        ptr->myattr & (NBRD_LINE | NBRD_FOLDER))
    return 0;

    if (B_TOTAL(ptr) == 0)
       {
    setbtotal(ptr->bid);
    setbottomtotal(ptr->bid);
       }
    if (B_TOTAL(ptr) == 0)
    return 0;
    ftime = B_LASTPOSTTIME(ptr);

    /* 有些 util, 尤其是 innbbsd, 會用到比較新的 time stamp,
     * 只要不太誇張就 ok */
    if (ftime > now + 10) 
    ftime = B_LASTPOSTTIME(ptr) = now - 1;

    if ( brc_unread_time(ptr->bid, ftime, 0) )
    ptr->myattr |= NBRD_UNREAD;
    
    return 1;
}

static void
load_uidofgid(const int gid, const int type)
{
    boardheader_t  *bptr, *currbptr, *parent;
    int             bid, n, childcount = 0;
    assert(0<=type && type<2);
    assert(0<= gid-1 && gid-1<MAX_BOARD);
    currbptr = parent = &bcache[gid - 1];
    assert(0<=numboards && numboards<=MAX_BOARD);
    for (n = 0; n < numboards; ++n) {
    bid = SHM->bsorted[type][n]+1;
    if( bid<=0 || !(bptr = getbcache(bid)) 
        || bptr->brdname[0] == '\0' )
        continue;
    if (bptr->gid == gid) {
        if (currbptr == parent)
        currbptr->firstchild[type] = bid;
        else {
        currbptr->next[type] = bid;
        currbptr->parent = gid;
        }
        childcount++;
        currbptr = bptr;
    }
    }
    parent->childcount = childcount;
    if (currbptr == parent) // no child
    currbptr->firstchild[type] = -1;
    else // the last child
    currbptr->next[type] = -1;
}

static boardstat_t *
addnewbrdstat(int n, int state)
{
    boardstat_t    *ptr;

    // ptt 2 local modification 
    // XXX maybe some developer discovered signed short issue?
    assert(n != -32769);

    assert(0<=n && n<MAX_BOARD);
    assert(0<=brdnum && brdnum<nbrdsize);
    ptr = &nbrd[brdnum++];
    //boardheader_t  *bptr = &bcache[n];
    //ptr->total = &(SHM->total[n]);
    //ptr->lastposttime = &(SHM->lastposttime[n]);
    
    ptr->bid = n + 1;
    ptr->myattr = state;
    if ((B_BH(ptr)->brdattr & BRD_HIDE) && state == NBRD_BOARD)
    B_BH(ptr)->brdattr |= BRD_POSTMASK;
    if (!IS_LISTING_FAV())
    ptr->myattr &= ~NBRD_FAV;
    check_newpost(ptr);
    return ptr;
}

#if !HOTBOARDCACHE
static int
cmpboardfriends(const void *brd, const void *tmp)
{
#ifdef USE_COOLDOWN
    if ((B_BH((boardstat_t*)tmp)->brdattr & BRD_COOLDOWN) &&
        (B_BH((boardstat_t*)brd)->brdattr & BRD_COOLDOWN))
    return 0;
    else if ( B_BH((boardstat_t*)tmp)->brdattr & BRD_COOLDOWN ) {
    if (B_BH((boardstat_t*)brd)->nuser == 0)
        return 0;
    else
        return 1;
    }
    else if ( B_BH((boardstat_t*)brd)->brdattr & BRD_COOLDOWN ) {
    if (B_BH((boardstat_t*)tmp)->nuser == 0)
        return 0;
    else
        return -1;
    }
#endif
    return ((B_BH((boardstat_t*)tmp)->nuser) -
        (B_BH((boardstat_t*)brd)->nuser));
}
#endif

static void
load_boards(char *key)
{
    int             type = (cuser.uflag & BRDSORT_FLAG) ? 1 : 0;
    int             i;
    int             state;

    // override type in class root, because usually we don't need to sort
    // class root; and there may be out-of-sync in that mode.
    if (IN_CLASSROOT())
    type = 1;

    brdnum = 0;
    if (nbrd) {
        free(nbrd);
    nbrdsize = 0;
    nbrd = NULL;
    }
    if (!IN_CLASS()) {
    if(IS_LISTING_FAV()){
        fav_t   *fav = get_current_fav();
        int     nfav = get_data_number(fav);
        if( nfav == 0 ) {
        nbrdsize = 1;
        nbrd = (boardstat_t *)malloc(sizeof(boardstat_t) * 1);
        addnewbrdstat(0, 0); // dummy
            return;
        }
        nbrdsize = nfav;
        nbrd = (boardstat_t *)malloc(sizeof(boardstat_t) * nfav);
            for( i = 0 ; i < fav->DataTail; ++i ){
        int state;
        if (!(fav->favh[i].attr & FAVH_FAV))
            continue;

        if ( !key[0] ){
            if (get_item_type(&fav->favh[i]) == FAVT_LINE )
            state = NBRD_LINE;
            else if (get_item_type(&fav->favh[i]) == FAVT_FOLDER )
            state = NBRD_FOLDER;
            else {
            state = NBRD_BOARD;
            if (is_set_attr(&fav->favh[i], FAVH_UNREAD))
                state |= NBRD_UNREAD;
            }
        } else {
            if (get_item_type(&fav->favh[i]) == FAVT_LINE )
            continue;
            else if (get_item_type(&fav->favh[i]) == FAVT_FOLDER ){
            if( strcasestr(
                get_folder_title(fav_getid(&fav->favh[i])),
                key)
            )
                state = NBRD_FOLDER;
            else
                continue;
            }else{
            boardheader_t *bptr = getbcache(fav_getid(&fav->favh[i]));
            assert(0<=fav_getid(&fav->favh[i])-1 && fav_getid(&fav->favh[i])-1<MAX_BOARD);
            if (strcasestr(bptr->title, key))
                state = NBRD_BOARD;
            else
                continue;
            if (is_set_attr(&fav->favh[i], FAVH_UNREAD))
                state |= NBRD_UNREAD;
            }
        }

        if (is_set_attr(&fav->favh[i], FAVH_TAG))
            state |= NBRD_TAG;
        if (is_set_attr(&fav->favh[i], FAVH_ADM_TAG))
            state |= NBRD_TAG;
        // 有些人 某些 bid < 0 Orzz // ptt2 local modification
        if (fav_getid(&fav->favh[i]) < 1)
            continue;
        addnewbrdstat(fav_getid(&fav->favh[i]) - 1, NBRD_FAV | state);
        }
    }
#if HOTBOARDCACHE
    else if(IN_HOTBOARD()){
        nbrdsize = SHM->nHOTs;
        if(nbrdsize == 0) {
        nbrdsize = 1;
        nbrd = (boardstat_t *)malloc(sizeof(boardstat_t) * 1);
        addnewbrdstat(0, 0); // dummy
        return;
        }
        assert(0<nbrdsize);
        nbrd = (boardstat_t *)malloc(sizeof(boardstat_t) * nbrdsize);
        for( i = 0 ; i < nbrdsize; ++i ) {
        if(SHM->HBcache[i] == -1)
            continue;
        addnewbrdstat(SHM->HBcache[i], HasBoardPerm(&bcache[SHM->HBcache[i]]));
        }
    }
#endif
    else { // general case
        nbrdsize = numboards;
        assert(0<nbrdsize && nbrdsize<=MAX_BOARD);
        nbrd = (boardstat_t *) malloc(sizeof(boardstat_t) * nbrdsize);
        for (i = 0; i < nbrdsize; i++) {
        int n = SHM->bsorted[type][i];
        boardheader_t *bptr;
        if (n < 0)
            continue;
        bptr = &bcache[n];
        if (bptr == NULL)
            continue;
        if (!bptr->brdname[0] ||
            (bptr->brdattr & (BRD_GROUPBOARD | BRD_SYMBOLIC)) ||
            !((state = HasBoardPerm(bptr)) || GROUPOP()) ||
            TITLE_MATCH(bptr, key)
#if ! HOTBOARDCACHE
            || (IN_HOTBOARD() && bptr->nuser < 5)
#endif
            )
            continue;
        addnewbrdstat(n, state);
        }
    }
#if ! HOTBOARDCACHE
    if (IN_HOTBOARD())
        qsort(nbrd, brdnum, sizeof(boardstat_t), cmpboardfriends);
#endif
    } else { /* load boards of a subclass */
    boardheader_t  *bptr = getbcache(class_bid);
    int childcount; 
    int bid;

    assert(0<=class_bid-1 && class_bid-1<MAX_BOARD);
    if (bptr->firstchild[type] == 0 || bptr->childcount==0)
        load_uidofgid(class_bid, type);

        childcount = bptr->childcount;  // Ptt: child count after load_uidofgid

    nbrdsize = childcount + 5;
    nbrd = (boardstat_t *) malloc((childcount+5) * sizeof(boardstat_t));
        // 預留兩個以免大量開板時掛調
    for (bid = bptr->firstchild[type]; bid > 0 && 
        brdnum < childcount+5; bid = bptr->next[type]) {
        assert(0<=bid-1 && bid-1<MAX_BOARD);
            bptr = getbcache(bid);
        state = HasBoardPerm(bptr);
        if ( !(state || GROUPOP()) || TITLE_MATCH(bptr, key) )
        continue;

        if (bptr->brdattr & BRD_SYMBOLIC) {
        /* Only SYSOP knows a board is symbolic */
        if (HasUserPerm(PERM_SYSOP) || HasUserPerm(PERM_SYSSUPERSUBOP))
            state |= NBRD_SYMBOLIC;
        else {
            bid = BRD_LINK_TARGET(bptr);
            if (bcache[bid - 1].brdname[0] == 0) {
            vmsg("連結已損毀,請至 SYSOP 回報此問題。");
            continue;
            }
        }
        }
        assert(0<=bid-1 && bid-1<MAX_BOARD);
        addnewbrdstat(bid-1, state);
    }
        if(childcount < brdnum) {
        //Ptt: dirty fix fix soon 
        getbcache(class_bid)->childcount = 0;
    }
           
                 
    }
}

static int
search_local_board()
{
    int             num;
    char            genbuf[IDLEN + 2];
    struct Vector namelist;

    move(0, 0);
    clrtoeol();
    Vector_init(&namelist, IDLEN + 1);
    assert(brdnum<=nbrdsize);
    Vector_resize(&namelist, brdnum);
    for (num = 0; num < brdnum; num++)
        if (!IS_LISTING_FAV() ||
            (nbrd[num].myattr & NBRD_BOARD && HasBoardPerm(B_BH(&nbrd[num]))) )
            Vector_add(&namelist, B_BH(&nbrd[num])->brdname);
    namecomplete2(&namelist, ANSI_REVERSE "【 搜尋所在位置看板 】"
        ANSI_RESET "\n請輸入看板名稱(按空白鍵自動搜尋): ", genbuf);
    Vector_delete(&namelist);
            

    for (num = 0; num < brdnum; num++)
        if (!strcasecmp(B_BH(&nbrd[num])->brdname, genbuf))
            return num;
    return -1;
}

static int
search_board(const char *bname)
{
    int num = 0;
    assert(brdnum<=nbrdsize);

    for (num = 0; num < brdnum; num++)
    if (!strcasecmp(B_BH(&nbrd[num])->brdname, bname))
        return num;
    return -1;
}

static int
unread_position(char *dirfile, boardstat_t * ptr)
{
    fileheader_t    fh;
    char            fname[FNLEN];
    register int    num, fd, step, total;

    total = B_TOTAL(ptr);
    num = total + 1;
    if ((ptr->myattr & NBRD_UNREAD) && (fd = open(dirfile, O_RDWR)) > 0) {
    if (!brc_initial_board(B_BH(ptr)->brdname)) {
        num = 1;
    } else {
        num = total - 1;
        step = 4;
        while (num > 0) {
        lseek(fd, (off_t) (num * sizeof(fh)), SEEK_SET);
        if (read(fd, fname, FNLEN) <= 0 ||
            !brc_unread(ptr->bid, fname, 0))
            break;
        num -= step;
        if (step < 32)
            step += step >> 1;
        }
        if (num < 0)
        num = 0;
        while (num < total) {
        lseek(fd, (off_t) (num * sizeof(fh)), SEEK_SET);
        if (read(fd, fname, FNLEN) <= 0 ||
            brc_unread(ptr->bid, fname, 0))
            break;
        num++;
        }
    }
    close(fd);
    }
    if (num < 0)
    num = 0;
    return num;
}

static char
get_fav_type(boardstat_t *ptr)
{
    if (ptr->myattr & NBRD_FOLDER)
    return FAVT_FOLDER;
    else if (ptr->myattr & NBRD_BOARD)
    return FAVT_BOARD;
    else if (ptr->myattr & NBRD_LINE)
    return FAVT_LINE;
    return 0;
}

static void
brdlist_foot(void)
{
    vs_footer("  選擇看板  ",
        IS_LISTING_FAV() ?
        "  (c)新文章模式 (v/V)標為已讀/未讀 (y)列出全部 (m)切換最愛" :
        "  (c)新文章模式 (v/V)標為已讀/未讀 (y)篩選列表 (m)切換最愛");
}


static inline char * 
make_class_color(char *name)
{
    /* 34 is too dark */
    char    *colorset[8] = {"", ANSI_COLOR(32),
    ANSI_COLOR(33), ANSI_COLOR(36), ANSI_COLOR(1;34), 
    ANSI_COLOR(1), ANSI_COLOR(1;32), ANSI_COLOR(1;33)};

    return colorset[(unsigned int)
    (name[0] + name[1] +
     name[2] + name[3]) & 0x7];
}

#define HILIGHT_COLOR   ANSI_COLOR(1;36)

static void
show_brdlist(int head, int clsflag, int newflag)
{
    int             myrow = 2;
    if (unlikely(IN_CLASSROOT())) {
    currstat = CLASS;
    myrow = 6;
    showtitle("分類看板", BBSName);
    movie(0);
    move(1, 0);
    // TODO remove ascii art here
    outs(
        "                                                              "
        "◣  ╭—" ANSI_COLOR(33) "●\n"
        "                                                    ╬—  " ANSI_RESET " "
        "◢█" ANSI_COLOR(47) "⊙" ANSI_COLOR(40) "██◣╤\n"
        "  " ANSI_COLOR(44) "   ︿︿︿︿︿︿︿︿                               "
        ANSI_COLOR(33) "║" ANSI_RESET ANSI_COLOR(44) " ◣◢███▼▼▼║ " ANSI_RESET "\n"
        "  " ANSI_COLOR(44) "                                                  "
        ANSI_COLOR(33) "  " ANSI_RESET ANSI_COLOR(44) " ◤◥███▲▲▲ ║" ANSI_RESET "\n"
        "                                  ︿︿︿︿︿︿︿︿    " ANSI_COLOR(33)
        "│" ANSI_RESET "   ◥████◤ ║\n"
        "                                                      " ANSI_COLOR(33) "╫"
        "——" ANSI_RESET "  ◤      —+" ANSI_RESET);
    } else if (clsflag) {
    showtitle("看板列表", BBSName);
    // [m]加入或移出我的最愛 
    outs("[←][q]主選單 [→][r]閱\讀 [↑↓]選擇 [PgUp][PgDn]翻頁 [S]排序 [/]搜尋  [h]求助\n");

    // boards in Ptt series are very, very large.
    // let's create more space for board numbers,
    // and less space for BM.
    //
    // newflag is not so different now because we use all 5 digits.

    vbarf(ANSI_REVERSE "   %s   看  板       類別 轉信  中   文   敘   述           人氣 板   主",
        newflag ? "總數" : "編號");
    move(b_lines, 0);
    brdlist_foot();
    }
    if (brdnum > 0) {
    boardstat_t    *ptr;
    char    *unread[2] = {ANSI_COLOR(37) "  " ANSI_RESET, ANSI_COLOR(1;31) "ˇ" ANSI_RESET};
 
    if (IS_LISTING_FAV() && brdnum == 1 && get_fav_type(&nbrd[0]) == 0) {

        // (a) or (i) needs HasUserPerm(PERM_LOGINOK)).
        // 3 = first line of empty area
        if (!HasFavEditPerm())
        {
        // TODO actually we cannot use 's' (for PTT)...
        mvouts(3, 10, 
        "--- 註冊的使用者才能新增看板喔 (可按 s 手動選取) ---");
        } else {
        // normal user. tell him what to do.
        mvouts(3, 10, 
        "--- 空目錄,請按 a 新增或用 y 列出全部看板後按 z 增刪 ---");
        }
        return;
    }

    while (++myrow < b_lines) {

        move(myrow, 0);
        clrtoeol();

        if (head < brdnum) {
        assert(0<=head && head<nbrdsize);
        ptr = &nbrd[head++];
        if (ptr->myattr & NBRD_LINE){
            if( !newflag )
            prints("%7d %c ", head, ptr->myattr & NBRD_TAG ? 'D' : ' ');
            else
            prints("%7s   ", "");
                
            if (!(ptr->myattr & NBRD_FAV))
            outs(ANSI_COLOR(1;30));

            outs("------------" 
                "      "
                // "------"
                "------------------------------------------" 
                ANSI_RESET "\n");
            continue;
        }
        else if (ptr->myattr & NBRD_FOLDER){
            char *title = get_folder_title(ptr->bid);
            prints("%7d %c ", 
                newflag ? 
                get_data_number(get_fav_folder(getfolder(ptr->bid))) :
                head, ptr->myattr & NBRD_TAG ? 'D' : ' ');

            // well, what to print with myfav folders?
            // this style is too long and we don't want to 
            // fight with users... 
            // think about new way some otherday.
            prints("%sMyFavFolder" ANSI_RESET "  目錄 □%-34s", 
                !(cuser.uflag2 & FAVNOHILIGHT)?HILIGHT_COLOR  : "",
                title); 
            /*
            if (!(cuser.uflag2 & FAVNOHILIGHT))
            outs(HILIGHT_COLOR);
            prints("%-12s", "[Folder]");
            outs(ANSI_RESET);
            prints(" 目錄 Σ%-34s", title);
            */
            /*
            outs(ANSI_COLOR(0;36));
            prints("Σ%-70.70s", title);
            outs(ANSI_RESET);
            */
            continue;
        }

        if (IN_CLASSROOT())
            outs("          ");
        else {
            if (!GROUPOP() && !HasBoardPerm(B_BH(ptr))) {
            if (newflag) prints("%7s", "");
            else prints("%7d", head);
            prints(" %c Unknown??    隱板 ?這個板是隱板",
                ptr->myattr & NBRD_TAG ? 'D' : ' ');
            continue;
            }
        }

        if (newflag && B_BH(ptr)->brdattr & BRD_GROUPBOARD)
            outs("          ");
        else
            prints("%7d%c%s", 
                newflag ? (int)(B_TOTAL(ptr)) : head,
                !(B_BH(ptr)->brdattr & BRD_HIDE) ? ' ' :
                (B_BH(ptr)->brdattr & BRD_POSTMASK) ? ')' : '-',
                (ptr->myattr & NBRD_TAG) ? "D " :
                (B_BH(ptr)->brdattr & BRD_GROUPBOARD) ? "  " :
                unread[ptr->myattr & NBRD_UNREAD ? 1 : 0]);

        if (!IN_CLASSROOT()) {
            prints("%s%-13s" ANSI_RESET "%s%5.5s" ANSI_COLOR(0;37) 
                "%2.2s" ANSI_RESET "%-34.34s",
                ((!(cuser.uflag2 & FAVNOHILIGHT) &&
                  getboard(ptr->bid) != NULL))? HILIGHT_COLOR : "",
                B_BH(ptr)->brdname,
                make_class_color(B_BH(ptr)->title),
                B_BH(ptr)->title, B_BH(ptr)->title + 5, B_BH(ptr)->title + 7);

#ifdef USE_COOLDOWN
            if (B_BH(ptr)->brdattr & BRD_COOLDOWN)
            outs("靜 ");
            else if (B_BH(ptr)->brdattr & BRD_BAD)
#else
            if (B_BH(ptr)->brdattr & BRD_BAD)
#endif
            outs(" X ");

            else if (B_BH(ptr)->nuser <= 0)
            prints(" %c ", B_BH(ptr)->bvote ? 'V' : ' ');
            else if (B_BH(ptr)->nuser <= 10)
            prints("%2d ", B_BH(ptr)->nuser);
            else if (B_BH(ptr)->nuser <= 50)
            prints(ANSI_COLOR(1;33) "%2d" ANSI_RESET " ", B_BH(ptr)->nuser);
#ifdef EXTRA_HOTBOARD_COLORS
            // piaip 2008/02/04: new colors
            else if (B_BH(ptr)->nuser >= 100000)
            outs(ANSI_COLOR(1;35) "爆!" ANSI_RESET);
            else if (B_BH(ptr)->nuser >= 60000)
            outs(ANSI_COLOR(1;33) "爆!" ANSI_RESET);
            else if (B_BH(ptr)->nuser >= 30000)
            outs(ANSI_COLOR(1;32) "爆!" ANSI_RESET);
            else if (B_BH(ptr)->nuser >= 10000)
            outs(ANSI_COLOR(1;36) "爆!" ANSI_RESET);
#endif
            else if (B_BH(ptr)->nuser >= 5000)
            outs(ANSI_COLOR(1;34) "爆!" ANSI_RESET);
            else if (B_BH(ptr)->nuser >= 2000)
            outs(ANSI_COLOR(1;31) "爆!" ANSI_RESET);
            else if (B_BH(ptr)->nuser >= 1000)
            outs(ANSI_COLOR(1) "爆!" ANSI_RESET);
            else if (B_BH(ptr)->nuser >= 100)
            outs(ANSI_COLOR(1) "HOT" ANSI_RESET);
            else //if (B_BH(ptr)->nuser > 50)
            prints(ANSI_COLOR(1;31) "%2d" ANSI_RESET " ", B_BH(ptr)->nuser);
            prints("%.*s" ANSI_CLRTOEND, t_columns - 68, B_BH(ptr)->BM);
        } else {
            prints("%-40.40s %.*s", B_BH(ptr)->title + 7,
               t_columns - 68, B_BH(ptr)->BM);
        }
        }
        clrtoeol();
    }
    }
}

static void
set_menu_BM(char *BM)
{
    if (!HasUserPerm(PERM_NOCITIZEN) && (HasUserPerm(PERM_ALLBOARD) || is_uBM(BM, cuser.userid))) {
    currmode |= MODE_GROUPOP;
    cuser.userlevel |= PERM_SYSSUBOP | PERM_BM;
    }
}

static void replace_link_by_target(boardstat_t *board)
{
    assert(0<=board->bid-1 && board->bid-1<MAX_BOARD);
    board->bid = BRD_LINK_TARGET(getbcache(board->bid));
    board->myattr &= ~NBRD_SYMBOLIC;
}
static int
paste_taged_brds(int gid)
{
    fav_t *fav;
    int  bid, tmp;

    if (gid == 0  || ! (HasUserPerm(PERM_SYSOP) || GROUPOP()) ||
        vans("貼上標記的看板?(y/N)")!='y') return 0;
    fav = get_fav_root();
    for (tmp = 0; tmp < fav->DataTail; tmp++) {
        boardheader_t  *bh;
        bid = fav_getid(&fav->favh[tmp]);
        assert(0<=bid-1 && bid-1<MAX_BOARD);
        bh = getbcache(bid);
        if( !is_set_attr(&fav->favh[tmp], FAVH_ADM_TAG))
        continue;
        set_attr(&fav->favh[tmp], FAVH_ADM_TAG, FALSE);
        if (bh->gid != gid) {
        bh->gid = gid;
        assert(0<=bid-1 && bid-1<MAX_BOARD);
        substitute_record(FN_BOARD, bh,
                  sizeof(boardheader_t), bid);
        reset_board(bid);
        log_usies("SetBoardGID", bh->brdname);
        }
    }
    sort_bcache();
    return 1;
}

static void
choose_board(int newflag)
{
    static int      num = 0;
    boardstat_t    *ptr;
    int             head = -1, ch = 0, currmodetmp, tmp, tmp1, bidtmp;
    char            keyword[13] = "", buf[PATHLEN];

    setutmpmode(newflag ? READNEW : READBRD);
    if( get_fav_root() == NULL )
    fav_load();
    ++choose_board_depth;
    brdnum = 0;
    if (!cuser.userlevel)   /* guest yank all boards */
    LIST_BRD();

    do {
    if (brdnum <= 0) {
        load_boards(keyword);
        if (brdnum <= 0) {
        if (keyword[0] != 0) {
            vmsg("沒有任何看板標題有此關鍵字");
            keyword[0] = 0;
            brdnum = -1;
            continue;
        }
        if (IS_LISTING_BRD()) {
            if (HasUserPerm(PERM_SYSOP) || GROUPOP()) {
            if (paste_taged_brds(class_bid) || 
                    m_newbrd(class_bid, 0) == -1)
                break;
            brdnum = -1;
            continue;
            } else
            break;
        }
        }
        head = -1;
    }

    /* reset the cursor when out of range */
    if (num < 0)
        num = 0;
    else if (num >= brdnum)
        num = brdnum - 1;

    if (head < 0) {
        if (newflag) {
        tmp = num;
        assert(brdnum<=nbrdsize);
        while (num < brdnum) {
            ptr = &nbrd[num];
            if (ptr->myattr & NBRD_UNREAD)
            break;
            num++;
        }
        if (num >= brdnum)
            num = tmp;
        }
        head = (num / p_lines) * p_lines;
        show_brdlist(head, 1, newflag);
    } else if (num < head || num >= head + p_lines) {
        head = (num / p_lines) * p_lines;
        show_brdlist(head, 0, newflag);
    }
    if (IN_CLASSROOT())
        ch = cursor_key(7 + num - head, 10);
    else
        ch = cursor_key(3 + num - head, 0);

    switch (ch) {
        ///////////////////////////////////////////////////////
        // General Hotkeys
        ///////////////////////////////////////////////////////
        
    case 'h':
        show_helpfile(fn_boardlisthelp);
        show_brdlist(head, 1, newflag);
        break;
    case Ctrl('W'):
        whereami();
        head = -1;
        break;

    case 'c':
        show_brdlist(head, 1, newflag ^= 1);
        break;
    case Ctrl('I'):
        t_idle();
        show_brdlist(head, 1, newflag);
        break;

    // ZA
    case Ctrl('Z'):
        head = -1;
        if (ZA_Select())
        ch = 'q';
        else
        break;
        // if selected, follow q.

    case 'e':
    case KEY_LEFT:
    case EOF:
        ch = 'q';
    case 'q':
        if (keyword[0]) {
        keyword[0] = 0;
        brdnum = -1;
        ch = ' ';
        }
        break;
    case KEY_PGUP:
    case 'P':
    case 'b':
    case Ctrl('B'):
        if (num) {
        num -= p_lines;
        break;
        }
    case KEY_END:
    case '$':
        num = brdnum - 1;
        break;
    case ' ':
    case KEY_PGDN:
    case 'N':
    case Ctrl('F'):
        if (num == brdnum - 1)
        num = 0;
        else
        num += p_lines;
        break;
    case KEY_UP:
    case 'p':
    case 'k':
        if (num-- <= 0)
        num = brdnum - 1;
        break;
    case '*':
        if (IS_LISTING_FAV()) {
        int i = 0;
        assert(brdnum<=nbrdsize);
        for (i = 0; i < brdnum; i++)
        {
            ptr = &nbrd[i];
            if (IS_LISTING_FAV()){
            assert(nbrdsize>0);
            if(get_fav_type(&nbrd[0]) != 0)
                fav_tag(ptr->bid, get_fav_type(ptr), 2);
            }
            ptr->myattr ^= NBRD_TAG;
        }
        head = 9999;
        }
        break;
    case 't':
        assert(0<=num && num<nbrdsize);
        ptr = &nbrd[num];
        if (IS_LISTING_FAV()){
        assert(nbrdsize>0);
        if(get_fav_type(&nbrd[0]) != 0)
            fav_tag(ptr->bid, get_fav_type(ptr), EXCH);
        }
        else if (HasUserPerm(PERM_SYSOP) ||
             HasUserPerm(PERM_SYSSUPERSUBOP) ||
             HasUserPerm(PERM_SYSSUBOP) ||
             HasUserPerm(PERM_BOARD)) {
        /* 站長管理用的 tag */
        if (ptr->myattr & NBRD_TAG)
            set_attr(getadmtag(ptr->bid), FAVH_ADM_TAG, FALSE);
        else
            fav_add_admtag(ptr->bid);
        }
        ptr->myattr ^= NBRD_TAG;
        head = 9999;
    case KEY_DOWN:
    case 'n':
    case 'j':
        if (++num < brdnum)
        break;
    case '0':
    case KEY_HOME:
        num = 0;
        break;
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
        if ((tmp = search_num(ch, brdnum)) >= 0)
        num = tmp;
        brdlist_foot();
        break;

    case '/':
        getdata_buf(b_lines - 1, 0, "請輸入看板中文關鍵字:",
            keyword, sizeof(keyword), DOECHO);
        brdnum = -1;
        break;
    case 'S':
        if(IS_LISTING_FAV()){
        move(b_lines - 2, 0); clrtobot();
        outs("重新排序看板 "
            ANSI_COLOR(1;33) "(注意, 這個動作會覆寫原來設定)" ANSI_RESET " \n");
        tmp = vans("排序方式 (1)按照板名排序 (2)按照類別排序 ==> [0]取消 ");
        if( tmp == '1' )
            fav_sort_by_name();
        else if( tmp == '2' )
            fav_sort_by_class();
        }
        else
        cuser.uflag ^= BRDSORT_FLAG;
        brdnum = -1;
        break;


    case 'v':
    case 'V':
        assert(0<=num && num<nbrdsize);
        ptr = &nbrd[num];
        if(nbrd[num].bid < 0 || !HasBoardPerm(B_BH(ptr)))
        break;
        if (ch == 'v') {
        ptr->myattr &= ~NBRD_UNREAD;
        brc_toggle_all_read(ptr->bid, 1);
        } else {
        brc_toggle_all_read(ptr->bid, 0);
        ptr->myattr |= NBRD_UNREAD;
        }
        show_brdlist(head, 0, newflag);
        break;
    case Ctrl('S'):
        head = -1;
        if ((tmp = search_local_board()) != -1) {
        num = tmp;
        }
        break;
    case 's':
        {
        char bname[IDLEN+1];
        move(0, 0);
        clrtoeol();
        // since now user can use Ctrl-S to get access
        // to folders, let's fallback to boards only here.
        CompleteBoard(ANSI_REVERSE 
            "【 搜尋全站看板 】" ANSI_RESET
            "  (若要限定搜尋範圍為目前列表請改用 Ctrl-S)\n"
            "請輸入看板名稱(按空白鍵自動搜尋): ",
            bname);
        // force refresh
        head = -1;
        if (!*bname)
            break;
        // try to search board
        if ((tmp = search_board(bname)) != -1)
        {
            num = tmp;
            break;
        }
        // try to enter board directly.
        if(enter_board(bname) >= 0)
            Read();
        // restore my mode
        setutmpmode(newflag ? READNEW : READBRD);
        }
        break;

    case KEY_RIGHT:
    case KEY_ENTER:
    case 'r':
    case 'l':
        {
        if (IS_LISTING_FAV()) {
            assert(nbrdsize>0);
            if (get_fav_type(&nbrd[0]) == 0)
            break;
            assert(0<=num && num<nbrdsize);
            ptr = &nbrd[num];
            if (ptr->myattr & NBRD_LINE)
            break;
            if (ptr->myattr & NBRD_FOLDER){
            int t = num;
            num = 0;
            fav_folder_in(ptr->bid);
            choose_board(0);
            fav_folder_out();
            num = t;
            LIST_FAV(); // XXX press 'y' in fav makes yank_flag = LIST_BRD
            brdnum = -1;
            head = 9999;
            break;
            }
        } else {
            assert(0<=num && num<nbrdsize);
            ptr = &nbrd[num];
            if (ptr->myattr & NBRD_SYMBOLIC) {
            replace_link_by_target(ptr);
            }
        }

        assert(0<=ptr->bid-1 && ptr->bid-1<MAX_BOARD);
        if (!(B_BH(ptr)->brdattr & BRD_GROUPBOARD)) {   /* 非sub class */
            if (HasBoardPerm(B_BH(ptr))) {
            brc_initial_board(B_BH(ptr)->brdname);

            if (newflag) {
                setbdir(buf, currboard);
                tmp = unread_position(buf, ptr);
                head = tmp - t_lines / 2;
                getkeep(buf, head > 1 ? head : 1, tmp + 1);
            }
            Read();
            check_newpost(ptr);
            head = -1;
            setutmpmode(newflag ? READNEW : READBRD);
            }
        } else {    /* sub class */
            move(12, 1);
            bidtmp = class_bid;
            currmodetmp = currmode;
            tmp1 = num;
            num = 0;
            if (!(B_BH(ptr)->brdattr & BRD_TOP))
            class_bid = ptr->bid;
            else
            class_bid = -1; /* 熱門群組用 */

            if (!GROUPOP()) /* 如果還沒有小組長權限 */
            set_menu_BM(B_BH(ptr)->BM);

            if (now < B_BH(ptr)->bupdate) {
            int mr = 0;

            setbfile(buf, B_BH(ptr)->brdname, fn_notes);
            mr = more(buf, NA);
            if (mr != -1 && mr != READ_NEXT)
                pressanykey();
            }
            tmp = currutmp->brc_id;
            setutmpbid(ptr->bid);
            free(nbrd);
            nbrd = NULL;
            nbrdsize = 0;
                if (IS_LISTING_FAV()) {
            LIST_BRD();
            choose_board(0);
            LIST_FAV();
                }
            else
            choose_board(0);
            currmode = currmodetmp; /* 離開板板後就把權限拿掉喔 */
            num = tmp1;
            class_bid = bidtmp;
            setutmpbid(tmp);
            brdnum = -1;
        }
        }
        break;
        ///////////////////////////////////////////////////////
        // MyFav Functionality (Require PERM_BASIC)
        ///////////////////////////////////////////////////////
    case 'y':
        if (HasFavEditPerm() && !(IN_CLASS())) {
        if (get_current_fav() != NULL || !IS_LISTING_FAV()){
            yank_flag ^= 1; /* FAV <=> BRD */
        }
        brdnum = -1;
        }
        break;
    case Ctrl('D'):
        if (HasFavEditPerm()) {
        if (vans("刪除所有標記[N]?") == 'y'){
            fav_remove_all_tagged_item();
            brdnum = -1;
        }
        }
        break;
    case Ctrl('A'):
        if (HasFavEditPerm()) {
        fav_add_all_tagged_item();
        brdnum = -1;
        }
        break;
    case Ctrl('T'):
        if (HasFavEditPerm()) {
        fav_remove_all_tag();
        brdnum = -1;
        }
        break;
    case Ctrl('P'):
            if (paste_taged_brds(class_bid))
                brdnum = -1;
            break;

    case 'L':
        if ((HasUserPerm(PERM_SYSOP) ||
            (HasUserPerm(PERM_SYSSUPERSUBOP) && GROUPOP())) && IN_CLASS()) {
        // TODO XXX why need symlink here? Can we remove it?
        if (make_symbolic_link_interactively(class_bid) < 0)
            break;
        brdnum = -1;
        head = 9999;
        }
        else if (HasFavEditPerm() && IS_LISTING_FAV()) {
        if (fav_add_line() == NULL) {
            vmsg("新增失敗,分隔線/總最愛 數量達最大值。");
            break;
        }
        /* done move if it's the first item. */
        assert(nbrdsize>0);
        if (get_fav_type(&nbrd[0]) != 0)
            move_in_current_folder(brdnum, num);
        brdnum = -1;
        head = 9999;
        }
        break;

    case 'd': // why don't we enable 'd'?
    case 'z':
    case 'm':
        if (HasFavEditPerm()) {
        assert(0<=num && num<nbrdsize);
        ptr = &nbrd[num];
        brdnum = -1;
        head = 9999;
        if (IS_LISTING_FAV()) {
            if (ptr->myattr & NBRD_FAV) {
            if (vans("你確定刪除嗎? [N/y]") != 'y')
                break;
            fav_remove_item(ptr->bid, get_fav_type(ptr));
            ptr->myattr &= ~NBRD_FAV;
            }
        }
        else 
        {
            if (getboard(ptr->bid) != NULL) {
            fav_remove_item(ptr->bid, FAVT_BOARD);
            ptr->myattr &= ~NBRD_FAV;
            }
            else if (ch != 'd') // 'd' only deletes something.
            {
            if (fav_add_board(ptr->bid) == NULL)
                vmsg("你的最愛太多了啦 真花心");
            else
                ptr->myattr |= NBRD_FAV;
            }
        }
        }
        break;
    case 'M':
        if (HasFavEditPerm()){
        if (IN_FAVORITE() && IS_LISTING_FAV()){
            imovefav(num);
            brdnum = -1;
            head = 9999;
        }
        }
        break;
    case 'g':
        if (HasFavEditPerm() && IS_LISTING_FAV()) {
        fav_type_t  *ft;
        if (fav_stack_full()){
            vmsg("目錄已達最大層數!!");
            break;
        }
        if ((ft = fav_add_folder()) == NULL) {
            vmsg("新增失敗,目錄/總最愛 數量達最大值。");
            break;
        }
        fav_set_folder_title(ft, "新的目錄");
        /* don't move if it's the first item */
        assert(nbrdsize>0);
        if (get_fav_type(&nbrd[0]) != 0)
            move_in_current_folder(brdnum, num);
        brdnum = -1;
            head = 9999;
        }
        break;
    case 'T':
        assert(0<=num && num<nbrdsize);
        if (HasFavEditPerm() && nbrd[num].myattr & NBRD_FOLDER) {
        fav_type_t *ft = getfolder(nbrd[num].bid);
        strlcpy(buf, get_item_title(ft), sizeof(buf));
        getdata_buf(b_lines-1, 0, "請修改名稱: ", buf, BTLEN+1, DOECHO);
        fav_set_folder_title(ft, buf);
        brdnum = -1;
        }
        break;
    case 'K':
        if (HasFavEditPerm()) {
        char c, fname[80];
        brdnum = -1;
        if (get_current_fav() != get_fav_root()) {
            vmsg("請到我的最愛最上層執行本功\能");
            break;
        }

        c = vans("請選擇 2)備份我的最愛 3)取回最愛備份 [Q]");
        if(!c)
            break;
        if(vans("確定嗎 [y/N] ") != 'y')
            break;
        switch(c){
            case '2':
            fav_save();
            setuserfile(fname, FAV);
            sprintf(buf, "%s.bak", fname);
                        Copy(fname, buf);
            break;
            case '3':
            setuserfile(fname, FAV);
            sprintf(buf, "%s.bak", fname);
            if (!dashf(buf)){
                vmsg("你沒有備份你的最愛喔");
                break;
            }
                        Copy(buf, fname);
            fav_free();
            fav_load();
            break;
        }
        }
        break;

    case 'a':
    case 'i':
        if(IN_FAVORITE() && HasFavEditPerm()){
        char         bname[IDLEN + 1];
        int          bid;
        move(0, 0);
        clrtoeol();
        /* use CompleteBoard or CompleteBoardAndGroup ? */
        CompleteBoard(ANSI_REVERSE "【 增加我的最愛 】" ANSI_RESET "\n"
            "請輸入欲加入的看板名稱(按空白鍵自動搜尋):",
            bname);

        if (bname[0] && (bid = getbnum(bname)) &&
            HasBoardPerm(getbcache(bid))) {
            fav_type_t * ptr = getboard(bid);
            if (ptr != NULL) { // already in fav list
            // move curser to item
            int i;
            for (i = 0; i < nbrdsize; ++i) {
                if (bid == nbrd[i].bid) {
                num = i;
                break;
                }
            }
            if (i == nbrdsize) {
                assert(keyword[0]);
                vmsg("已經在我的最愛裡了, 取消關鍵字就能看到囉");
                keyword[0] = '\0';
                brdnum = -1;
            }
            } else {
            ptr = fav_add_board(bid);

            if (ptr == NULL)
                vmsg("你的最愛太多了啦 真花心");
            else {
                ptr->attr |= NBRD_FAV;

                if (ch == 'i' && get_data_number(get_current_fav()) > 1)
                move_in_current_folder(brdnum, num);
                else
                num = brdnum;
            }
            }
        }
        }
        brdnum = -1;
        head = 9999;
        break;

    case 'w':
        /* allowing save BRC/fav once per 10 minutes */
        if (now - last_save_fav_and_brc > 10 * 60) {
        fav_save();
        brc_finalize();

        last_save_fav_and_brc = now;
        vmsg("已儲存看板閱\讀記錄");
        } else
        vmsgf("間隔時間太近,未儲存看板閱\讀記錄 [請等 %d 秒後再試]", 
            (int)(600 - (now-last_save_fav_and_brc)));
        break;

        ///////////////////////////////////////////////////////
        // Administrator Only
        ///////////////////////////////////////////////////////

    case 'F':
    case 'f':
        if (HasUserPerm(PERM_SYSOP)) {
        getbcache(class_bid)->firstchild[cuser.uflag & BRDSORT_FLAG ? 1 : 0] = 0;
        brdnum = -1;
        }
        break;
    case 'D':
        if (HasUserPerm(PERM_SYSOP) ||
            (HasUserPerm(PERM_SYSSUPERSUBOP) && GROUPOP())) {
        assert(0<=num && num<nbrdsize);
        ptr = &nbrd[num];
        if (ptr->myattr & NBRD_SYMBOLIC) {
            if (vans("確定刪除連結?[N/y]") == 'y')
            delete_symbolic_link(getbcache(ptr->bid), ptr->bid);
        }
        brdnum = -1;
        }
        break;
    case 'E':
        if (HasUserPerm(PERM_SYSOP | PERM_BOARD) || GROUPOP()) {
        assert(0<=num && num<nbrdsize);
        ptr = &nbrd[num];
        move(1, 1);
        clrtobot();
        m_mod_board(B_BH(ptr)->brdname);
        brdnum = -1;
        }
        break;
    case 'R':
        if (HasUserPerm(PERM_SYSOP) || GROUPOP()) {
        m_newbrd(class_bid, 1);
        brdnum = -1;
        }
        break;
    case 'B':
        if (HasUserPerm(PERM_SYSOP) || GROUPOP()) {
        m_newbrd(class_bid, 0);
        brdnum = -1;
        }
        break;
    case 'W':
        if (IN_SUBCLASS() &&
        (HasUserPerm(PERM_SYSOP) || GROUPOP())) {
        setbpath(buf, getbcache(class_bid)->brdname);
        mkdir(buf, 0755);   /* Ptt:開群組目錄 */
        b_note_edit_bname(class_bid);
        brdnum = -1;
        }
        break;

    }
    } while (ch != 'q' && !ZA_Waiting());
    free(nbrd);
    nbrd = NULL;
    nbrdsize = 0;
    --choose_board_depth;
}

int
Class(void)
{
    init_brdbuf();
    class_bid = 1;
    LIST_BRD();
    choose_board(0);
    return 0;
}

int
TopBoards(void)
{
    init_brdbuf();
    class_bid = -1;
    LIST_BRD();
    choose_board(0);
    return 0;
}

int
Favorite(void)
{
    init_brdbuf();
    class_bid = 0;
    LIST_FAV();
    choose_board(0);
    return 0;
}

int
New(void)
{
    int             mode0 = currutmp->mode;
    int             stat0 = currstat;

    class_bid = 0;
    init_brdbuf();
    choose_board(1);
    currutmp->mode = mode0;
    currstat = stat0;
    return 0;
}