summaryrefslogtreecommitdiffstats
path: root/util/angel.c
blob: 4f7b0a0cf5aeeb82a32c1f7b56e55c156cc7edf9 (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
/* $Id$ */
#include "bbs.h"

#ifndef PLAY_ANGEL
int main(){ return 0; }
#else

int total[MAX_USERS + 1];
unsigned char reject[MAX_USERS + 1];
int nReject[4];
int rej_question;
int double_rej;
int (*list)[2];
int *rej_list;
int nReport = 50;
int count;
char* mailto = "SYSOP";

int ListCmp(const void * a, const void * b){
    return *(int*)b - *(int*)a;
}

int RejCmp(const void * a, const void * b){
    return strcasecmp(SHM->userid[*(int*)a - 1], SHM->userid[*(int*)b - 1]);
}

void readData();
void sendResult();
void slurp(FILE* to, FILE* from);

int main(int argc, char* argv[]){
    if (argc > 1)
    mailto = argv[1];
    if (argc > 2)
    nReport = atoi(argv[2]);

    readData();
    sendResult();
    return 0;
}

void readData(){
    int i, j;
    int k;
    userec_t user;
    FILE* fp;

    attach_SHM();

    fp = fopen(BBSHOME "/.Angel", "rb");
    if (fp != 0) {
    fread(reject, 1, sizeof(reject), fp);
    fclose(fp);
    }

    fp = fopen(BBSHOME "/.PASSWDS", "rb");
    j = count = 0;
    while (fread(&user, sizeof(userec_t), 1, fp) == 1) {
    ++j; /* j == uid */
    if (user.myangel[0]) {
        i = searchuser(user.myangel, NULL);
        if (i)
        ++total[i];
    }
    if (user.userlevel & PERM_ANGEL) {
        ++count;
        ++nReject[((user.uflag2 & ANGEL_MASK) >> 12)];
        ++total[j]; /* make all angel have total > 0 */
        if (user.uflag2 & REJ_QUESTION) {
        ++rej_question;
        if (++reject[j] >= 2)
            ++double_rej;
        } else
        reject[j] = 0;
    } else { /* don't have PERM_ANGEL */
        total[j] = INT_MIN;
        reject[j] = 0;
    }
    }
    fclose(fp);

    fp = fopen(BBSHOME "/.Angel", "wb");
    if (fp != NULL) {
    fwrite(reject, sizeof(reject), 1, fp);
    fclose(fp);
    }

    if(nReport > count)
    nReport = count;

    list = (int(*)[2]) malloc(count * sizeof(int[2]));
    rej_list = (int*) malloc(double_rej * sizeof(int));
    k = j = 0;
    for (i = 1; i <= MAX_USERS; ++i)
    if (total[i] > 0) {
        list[j][0] = total[i] - 1;
        list[j][1] = i;
        ++j;
        if (reject[i] >= 2)
        rej_list[k++] = i;
    }

    qsort(list, count, sizeof(int[2]), ListCmp);
    qsort(rej_list, double_rej, sizeof(int), RejCmp);
}

int mailalertuser(char* userid)
{
    userinfo_t *uentp=NULL;
    if (userid[0] && (uentp = search_ulist_userid(userid)))
         uentp->alerts|=ALERT_NEW_MAIL;
    return 0;
}

void sendResult(){
    int i;
    FILE* fp;
    time4_t t;
    fileheader_t header;
    struct stat st;
    char filename[512];

    sprintf(filename, BBSHOME "/home/%c/%s", mailto[0], mailto);
    if (stat(filename, &st) == -1) {
    if (mkdir(filename, 0755) == -1) {
        fprintf(stderr, "mail box create error %s \n", filename);
        return;
    }
    }
    else if (!(st.st_mode & S_IFDIR)) {
    fprintf(stderr, "mail box error\n");
    return;
    }

    stampfile(filename, &header);
    fp = fopen(filename, "w");
    if (fp == NULL) {
    fprintf(stderr, "Cannot open file %s\n", filename);
    return;
    }

    t = time(NULL);
    fprintf(fp, "作者: " BBSMNAME " 站方統計\n"
        "標題: 小天使統計資料\n"
        "時間: %s\n"
        "\n現在全站小天使有 %d 位\n"
        "\n小主人人數最多的 %d 位小天使:\n",
        ctime4(&t), count, nReport);
    for (i = 0; i < nReport; ++i)
    fprintf(fp, "%15s %5d 人\n", SHM->userid[list[i][1] - 1], list[i][0]);
    fprintf(fp, "\n現在男女皆收的小天使有 %d 位\n"
        "只收女生的有 %d 位  (共 %d 位收女生)\n"
        "只收男生的有 %d 位  (共 %d 位收男生)\n"
        "不接受新小天使的有 %d 位\n",
        nReject[0], nReject[1], nReject[1] + nReject[0],
        nReject[2], nReject[2] + nReject[0], nReject[3]);
    fprintf(fp, "\n現在開放小主人問問題的小天使有 %d 位\n"
        "不開放的有 %d 位\n"
        "連續兩次統計都不開放的有 %d 位:\n",
        count - rej_question, rej_question, double_rej);
    for (i = 0; i < double_rej; ++i) {
    fprintf(fp, "%13s %3d 次", SHM->userid[rej_list[i] - 1],
        reject[rej_list[i]]);
    if (i % 4 == 3) fputc('\n', fp);
    }
    if (i % 4 != 0) fputc('\n', fp);

    {
    FILE* changefp = fopen(BBSHOME "/log/changeangel.log", "r");
    if (changefp) {
        remove(BBSHOME "/log/changeangel.log");

        fputs("\n== 本周更換小天使紀錄 ==\n", fp);
        slurp(fp, changefp);
        fclose(changefp);
    }
    }

    fputs("\n--\n\n  本資料由 angel 程式產生\n\n", fp);
    fclose(fp);

    strcpy(header.title, "小天使統計資料");
    strcpy(header.owner, "站方統計");
    sprintf(filename, BBSHOME "/home/%c/%s/.DIR", mailto[0], mailto);
    append_record(filename, &header, sizeof(header));
    mailalertuser(mailto);
}

void slurp(FILE* to, FILE* from)
{
    char buf[4096]; // 4K block
    int count;

    while ((count = fread(buf, 1, sizeof(buf), from)) > 0) {
    char * p = buf;
    while (count > 0) {
        int i = fwrite(p, 1, count, to);

        if (i <= 0) return;

        p += i;
        count -= i;
    }
    }
}

#endif /* defined PLAY_ANGEL */