summaryrefslogtreecommitdiffstats
path: root/util/toplazyBM.c
blob: e2bc0a6bc568f6fde1cc5fb7f9027fb435eec000 (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
/* $Id$ */
#define _UTIL_C_
#include "bbs.h"
#define OUTFILE  BBSHOME "/etc/toplazyBM"
#define FIREFILE BBSHOME "/etc/firelazyBM"
extern boardheader_t *bcache;
extern int numboards;

boardheader_t allbrd[MAX_BOARD];
typedef struct lostbm {
    char  bmname[IDLEN + 1];
    char *title;
    char *ctitle;
    int lostdays;
} lostbm;
lostbm lostbms[MAX_BOARD];

typedef struct BMarray{
    char bmname[IDLEN + 1];
    int  flag;
}  BMArray;
BMArray bms[5];


int bmlostdays_cmp(const void *va, const void *vb)
{
    lostbm *a=(lostbm *)va, *b=(lostbm *)vb;
    if (a->lostdays > b->lostdays) return -1;
    else if (a->lostdays == b->lostdays) return 0;
    else return 1;
}

int main(int argc, char *argv[])
{
    int bmid, i, j=0;
    FILE *inf, *firef;
    time4_t now=time(NULL); 
    attach_SHM();
    resolve_boards();

    if(passwd_init())
    exit(1);      

    memcpy(allbrd,bcache,numboards*sizeof(boardheader_t));

    /* write out the target file */
    inf   = fopen(OUTFILE, "w+");
    if (inf == NULL) {
    printf("open file error : %s\n", OUTFILE);
    exit(1);
    }

    firef = fopen(FIREFILE, "w+");
    if (firef == NULL) {
    printf("open file error : %s\n", FIREFILE);
    exit(1);
    }

    fprintf(inf, "警告: 板主若於兩個月未上站,將予於免職\n");
    fprintf(inf,
        "看板名稱                                      "
        "    板主        幾天沒來啦\n"
        "---------------------------------------------------"
        "-------------------\n");

    fprintf(firef, "免職板主\n");
    fprintf(firef,
        "看板名稱                                      "
        "    板主        幾天沒來啦\n"
        "---------------------------------------------------"
        "-------------------\n"); 


    j = 0;
    for (i = 0; i < numboards; i++) {
    char *p, bmbuf[IDLEN * 3 + 3];
    int   index = 0, flag = 0, k, n;
    userec_t xuser;
    p = allbrd[i].BM;

    if (*p == '[') p++;
    if (allbrd[i].brdname[0] == '\0' ||
        !isalpha(allbrd[i].brdname[0])
       ) continue;

    p = strtok(p,"/ ]");
    for(index=0; p && index<5; index++) {
        int diff;
        // XXX what if bmid is invalid?
        if(!p[0] || (bmid = passwd_load_user(p, &xuser)) < 1) {
        index--;
        p=strtok(NULL,"/ ]");
        continue;
        }
        strlcpy(bms[index].bmname, p, sizeof(bms[index].bmname));
        bms[index].flag = 0;

        diff = now - xuser.lastlogin;
        if (diff < 0)
        diff = 0;

        if (diff >= 45 * 86400
            && !(xuser.userlevel & PERM_SYSOPHIDE)
            && !(xuser.userlevel & PERM_SYSOP)) {
        strlcpy(lostbms[j].bmname, p, sizeof(bms[index].bmname));
        lostbms[j].title = allbrd[i].brdname;
        lostbms[j].ctitle = allbrd[i].title;
        lostbms[j].lostdays = diff / 86400;

        //超過90天 免職
        if (lostbms[j].lostdays > 90) {
            xuser.userlevel &= ~PERM_BM;
            bms[index].flag = 1;
            flag = 1;
            passwd_update(bmid, &xuser);
        }
        j++;
        }
        p = strtok(NULL,"/ ]");
    }

    if (flag == 1) {
        bmbuf[0] = '\0';
        for (k = 0, n = 0; k < index; k++) {
        if (!bms[k].flag) {
            if (n++ != 0) strcat(bmbuf, "/");
            strcat(bmbuf, bms[k].bmname);
        }
        }
        strcpy(allbrd[i].BM, bmbuf);
        if (substitute_record(BBSHOME"/"FN_BOARD, &allbrd[i], 
            sizeof(boardheader_t), i+1) == -1) {
        printf("Update Board Faile : %s\n", allbrd[i].brdname);
        }
        reset_board(i+1);
    }
    }
    qsort(lostbms, j, sizeof(lostbm), bmlostdays_cmp);

    //write to the etc/toplazyBM
    for (i = 0; i < j; i++) {
    if (lostbms[i].lostdays > 90) {
        fprintf(firef, "%-*.*s%-*.*s%-*.*s%3d天沒上站\n",
            IDLEN, IDLEN, lostbms[i].title, BTLEN-10,
            BTLEN-10, lostbms[i].ctitle, IDLEN,IDLEN,
            lostbms[i].bmname,lostbms[i].lostdays);
    } else {
        fprintf(inf, "%-*.*s%-*.*s%-*.*s%3d天沒上站\n",
            IDLEN, IDLEN, lostbms[i].title, BTLEN-10,
            BTLEN-10, lostbms[i].ctitle, IDLEN,IDLEN,
            lostbms[i].bmname,lostbms[i].lostdays);
    }
    }
    fclose(inf);
    fclose(firef);

    //printf("Total %d boards.\n", count);

    //mail to the users
    for (i=0; i<j; i++) {
    fileheader_t mymail;
    char    genbuf[200];
    int lostdays;

    lostdays = lostbms[i].lostdays;

    if (lostdays != 45 && lostdays != 60 && lostdays!=75 &&(lostdays <= 90))
        continue;

    sprintf(genbuf, BBSHOME "/home/%c/%s", 
        lostbms[i].bmname[0], lostbms[i].bmname);
    stampfile(genbuf, &mymail);

    strcpy(mymail.owner, "[" BBSMNAME "警察局]");
    if (lostdays <= 90)
        sprintf(mymail.title,
            "\033[32m版主通知\033[m %s版版主%s",
            lostbms[i].title, lostbms[i].bmname);
    else
        sprintf(mymail.title,
            "\033[32m版主自動免職通知\033[m %s 版主 %s",
            lostbms[i].title, lostbms[i].bmname);

    unlink(genbuf);
    if (lostdays <= 90)
        Link(OUTFILE, genbuf);
    else
        Link(FIREFILE, genbuf);

    sprintf(genbuf, BBSHOME "/home/%c/%s/.DIR", 
        lostbms[i].bmname[0], lostbms[i].bmname);
    append_record(genbuf, &mymail, sizeof(mymail));     
    }
    return 0;
}