summaryrefslogtreecommitdiffstats
path: root/util/deluserfile.c
blob: 73f3750abfe5add7da40476b5fb2a41dd797189e (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
/* $Id$ */
/* 自動砍user目錄檔案程式 */

#include "bbs.h"

#define HOLDWRITELOG
#define DELZEROFILE
#define USERHOME BBSHOME "/home"

int bad_user_id(char *userid)
{
    register char ch;

    if (strlen(userid) < 2)
    return 1;

    if (!isalpha(*userid))
    return 1;

    if (!strcasecmp(userid, "new"))
    return 1;

    while ((ch = *(++userid)))
    if (!isalnum(ch))
        return 1;
    return 0;
}

void del_file(char *userid)
{
    char buf[200], buf1[200];
    struct dirent *de;
    DIR *dirp;
    char *ptr;

    sprintf(buf, BBSHOME "/home/%c/%s", userid[0], userid);

    if (chdir(buf) == -1)
    return;

    if (!(dirp = opendir(buf)))
    return;

    while ((de = readdir(dirp)))
    {
    ptr = de->d_name;
    if (ptr[0] > ' ' && ptr[0] != '.')
    {
        if (strstr(ptr, "writelog"))
#ifdef HOLDWRITELOG
        {
        fileheader_t mymail;

        stampfile(buf, &mymail);
        mymail.filemode = FILE_READ|FILE_HOLD;
        strcpy(mymail.owner, userid);
        strcpy(mymail.title, "熱線記錄");
        sprintf(buf1, BBSHOME "/home/%c/%s/writelog",
            userid[0], userid);
        rename(buf1, buf);
        sprintf(buf1, BBSHOME "/home/%c/%s/.DIR", userid[0], userid);
        append_record(buf1, &mymail, sizeof(mymail));
        }
#else
        unlink(ptr);
#endif
        else if (strstr(ptr, "chat_"))
        unlink(ptr);
        else if (strstr(ptr, "ve_"))
        unlink(ptr);
        else if (strstr(ptr, "SR."))
        unlink(ptr);
        else if (strstr(ptr, ".old"))
        unlink(ptr);
        else if (strstr(ptr, "talk_"))
        unlink(ptr);
    }
    }
    closedir(dirp);
}

void mv_user_home(char *ptr)
{
    char buf[200];

    printf("move user %s to tmp\n", ptr);
    sprintf(buf, "cp -R " BBSHOME "/home/%c/%s " BBSHOME "/tmp", ptr[0], ptr);
//      sprintf(buf,"rm -rf " BBSHOME "/home/%c/%s",ptr[0],ptr);
    if (!system(buf))
    {               //Copy success

    sprintf(buf, "rm -rf " BBSHOME "/home/%c/%s", ptr[0], ptr);
    system(buf);
    }
}

int main(int argc, char **argv)
{
    struct dirent *de;
    DIR *dirp;
    char *ptr, buf[200], ch;
    int count = 0;
/* visit all users  */

    printf("new version, deleting\n");

    for (ch = 'A'; ch <= 'z'; ch++)
    {
    if(ch > 'Z' && ch < 'a')
        continue;
    printf("Cleaning %c\n", ch);
    sprintf(buf, USERHOME "/%c", ch);
    if (!(dirp = opendir(buf)))
    {
        printf("unable to open %s\n", buf);
        continue;
    }

    while ((de = readdir(dirp)))
    {
        ptr = de->d_name;

        /* 預防錯誤 */
        if (!bad_user_id(ptr))
        {
        if (!(count++ % 300))
            printf(".\n");
        if (!searchuser(ptr))
            mv_user_home(ptr);
        else
            del_file(ptr);
        }
    }
    closedir(dirp);
    }
    return 0;
}