summaryrefslogtreecommitdiffstats
path: root/util/rmbadmail.c
blob: 6973e968e171a4e39df10fe01dc72a8624e17da6 (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
/*-------------------------------------------------------*/
/* util/rmbadmail.c      ( YZU WindTop 2000)             */
/*-------------------------------------------------------*/
/* author : visor.bbs@bbs.yzu.edu.tw                     */
/* target : 刪除[信箱|看板]未在 .DIR 裡的信件            */
/* create : 00/08/30                                     */
/* update : 01/02/16                                     */
/*-------------------------------------------------------*/
/* syntax : rmbadmail                                    */
/*-------------------------------------------------------*/


#undef  FAKE_IO
#include "bbs.h"


static int reserve, r_size, ulink, u_size;


static void
reaper(fpath, lowid)
  char *fpath;
  char *lowid;
{
  int fd, size, check;
  char buf[256], *fname, folder[128], *ptr;
  DIR *dirp;
  struct dirent *de;
  HDR *head, *tail, *base;
  struct stat st;
  time_t now;

  now = time(0) - 60;

  printf("> processing account %-20s ", lowid);

  sprintf(buf, "%s/.DIR", fpath);
  
  if ((fd = open(buf, O_RDONLY)) >= 0)
  {
    fstat(fd, &st);
    size = st.st_size / sizeof(HDR);
    
    if (size <= 0)
    {
      base = NULL;
    }
    else
    {
      base = (HDR *) malloc(sizeof(HDR) * size);
      tail = base + size;
      read(fd, base, sizeof(HDR) * size);
    }
    
    close(fd);
  }
  else
  {
    size = 0;
    base = NULL;
  }
  
  printf("total mail : %d\n", size);
  sprintf(folder, "%s/@", fpath);

  if (!(dirp = opendir(folder)))
  {
    if (base)
      free(base);
    return;
  }
  
  ptr = strchr(folder, '@') + 1;
  *ptr++ = '/';

  while (de = readdir(dirp))
  {
    check = 0;
    fname = de->d_name;
    if (fname[0] > ' ' && fname[0] != '.')
    {
      if (base)
      {
    for (head = base; head < tail; head++)
    {
      if (!strcmp(head->xname, fname))
      {
        check = 1;
        break;
      }
    }
      }
      if (!check)
      {
    strcpy(ptr, fname);
    if (!(!stat(folder, &st) && (st.st_atime > now)))
    {
      u_size += st.st_size;
      ulink++;
      printf("file is not in HDR : %s : unlink !\n", fname);
      printf("--> unlinking %s\n", folder);

#ifndef FAKE_IO
      unlink(folder);
#endif
    }
    else
    {
      r_size += st.st_size;
      reserve++;
      printf("file is not in HDR : %s : reserve !\n", fname);
    }
      }
    }
  }
  closedir(dirp);
  free(base);
}


static void
expire(fpath, lowid)
  char *fpath;
  char *lowid;
{
  int fd, size, check;
  char buf[256], *fname, folder[128], *ptr, *str;
  DIR *dirp;
  struct dirent *de;
  HDR *head, *tail, *base;
  struct stat st;
  time_t now;

  now = time(0) - 60;

  printf("> processing board %-20s ", lowid);

  sprintf(buf, "%s/.DIR", fpath);
  if ((fd = open(buf, O_RDONLY)) >= 0)
  {
    fstat(fd, &st);
    size = st.st_size / sizeof(HDR);
    if (size <= 0)
    {
      base = NULL;
    }
    else
    {
      base = (HDR *) malloc(sizeof(HDR) * size);
      tail = base + size;
      read(fd, base, sizeof(HDR) * size);
    }
    close(fd);
  }
  else
  {
    base = NULL;
    size = 0;
  }
  printf("total article : %d\n", size);
  sprintf(folder, "%s/@", fpath);

  str = strchr(folder, '@');
  ptr = str + 1;
  *ptr++ = '/';
  *str = '0';

  while (1)
  {
    if ((dirp = opendir(folder)))
    {
      while (de = readdir(dirp))
      {
    check = 0;
    fname = de->d_name;
    if (fname[0] > ' ' && fname[0] != '.')
    {
      if (base)
      {
        for (head = base; head < tail; head++)
        {
          if (!strcmp(head->xname, fname))
          {
        check = 1;
        break;
          }
        }
      }
      if (!check)
      {
        strcpy(ptr, fname);
        if (!(!stat(folder, &st) && (st.st_atime > now)))
        {
          u_size += st.st_size;
          ulink++;
          printf("file is not in HDR : %s : unlink !\n", fname);
          printf("--> unlinking %s\n", folder);

#ifndef FAKE_IO
          unlink(folder);
#endif
        }
        else
        {
          r_size += st.st_size;
          reserve++;
          printf("file is not in HDR : %s : reserve !\n", fname);
        }
      }
    }
      }
      closedir(dirp);
    }
    if (++(*str) == ('9' + 1))
      *str = 'A';
    if ((*str) == 'W')
      break;
  }
  free(base);
}


static void
traverse(fpath, mode)
  char *fpath;
  int mode;
{
  DIR *dirp;
  struct dirent *de;
  char *fname, *str;

  if (!(dirp = opendir(fpath)))
  {
    return;
  }
  for (str = fpath; *str; str++);
  *str++ = '/';

  while (de = readdir(dirp))
  {
    fname = de->d_name;
    if (fname[0] > ' ' && fname[0] != '.')
    {
      strcpy(str, fname);
      if (mode == 1)
    reaper(fpath, fname);
      else if (mode == 2)
    expire(fpath, fname);
    }
  }
  closedir(dirp);
}


int
main(argc, argv)
  int argc;
  char *argv[];
{
  int ch;
  char *fname, fpath[256], buf[32];

  chdir(BBSHOME);

  if (argc > 1 && !strncmp(argv[1], "-a", 2))
  {
    if (argc > 2)
    {
      str_lower(buf, argv[2]);
      sprintf(fpath, "usr/%c/%s", *buf, buf);
      if (!access(fpath, 0))
    reaper(fpath, buf);
      else
    printf("error open account %s\n", buf);
    }
    else
    {
      strcpy(fname = fpath, "usr/@");
      fname = (char *) strchr(fname, '@');
      for (ch = 'a'; ch <= 'z'; ch++)
      {
    fname[0] = ch;
    fname[1] = '\0';
    traverse(fpath, 1);
      }
      for (ch = '0'; ch <= '9'; ch++)
      {
    fname[0] = ch;
    fname[1] = '\0';
    traverse(fpath, 1);
      }
    }
  }
  else if (argc > 1 && !strncmp(argv[1], "-b", 2))
  {
    if (argc > 2)
    {
      strcpy(buf, argv[2]);
      sprintf(fpath, "brd/%s", buf);
      if (!access(fpath, 0))
    expire(fpath, buf);
      else
    printf("error open board %s\n", buf);
    }
    else
    {
      strcpy(fpath, "brd");
      traverse(fpath, 2);
    }
  }
  else
  {
    printf("syntax : rmbadmail [-a|-b] [account|board]\n");
  }
  printf("total unlink  %10d  unlink  size : %10d\n", ulink, u_size);
  printf("total reserve %10d  reserve size : %10d\n", reserve, r_size);
  return 0;
}