chiark / gitweb /
fixes
[inn-innduct.git] / expire / prunehistory.c
1 /*  $Id: prunehistory.c 6124 2003-01-14 06:03:29Z rra $
2 **
3 **  Prune file names from history file.
4 */
5
6 #include "config.h"
7 #include "clibrary.h"
8 #include <errno.h>
9 #include <syslog.h>
10
11 #include "inn/history.h"
12 #include "inn/innconf.h"
13 #include "inn/messages.h"
14 #include "libinn.h"
15 #include "paths.h"
16
17
18 /*
19 **  Print usage message and exit.
20 */
21 static void
22 Usage(void)
23 {
24     fprintf(stderr, "Usage:  prunehistory [-p] [-f file] [input]\n");
25     exit(1);
26 }
27
28
29 int
30 main(int ac, char *av[])
31 {
32     char                *p;
33     int                 i;
34     char                buff[BUFSIZ];
35     const char          *History;
36     bool                Passing;
37     struct history      *history = NULL;
38     int                 rc = 0;
39
40     /* First thing, set up logging and our identity. */
41     openlog("prunehistory", L_OPENLOG_FLAGS | LOG_PID, LOG_INN_PROG);
42     message_program_name = "prunehistory";
43
44     /* Set defaults. */
45     if (!innconf_read(NULL))
46         exit(1);
47
48     History = concatpath(innconf->pathdb, _PATH_HISTORY);
49     Passing = false;
50
51     /* Parse JCL. */
52     while ((i = getopt(ac, av, "f:p")) != EOF)
53         switch (i) {
54         default:
55             Usage();
56             /* NOTREACHED */
57         case 'f':
58             History = optarg;
59             break;
60         case 'p':
61             Passing = true;
62             break;
63         }
64     ac -= optind;
65     av += optind;
66     if (ac) {
67         Usage();
68         rc = 1;
69         goto fail;
70     }
71
72     history = HISopen(History, innconf->hismethod, HIS_RDWR);
73     if (history == NULL) {
74         syswarn("cannot set up %s database", History);
75         rc = 1;
76         goto fail;
77     }
78
79     /* Loop over all input. */
80     while (fgets(buff, sizeof buff, stdin) != NULL) {
81         time_t arrived, posted, expires;
82
83         if ((p = strchr(buff, '\n')) == NULL) {
84             if (Passing)
85                 printf("%s\n", buff);
86             else
87                 warn("line too long, ignored: %.40s", buff);
88             continue;
89         }
90         *p = '\0';
91
92         /* Ignore blank and comment lines. */
93         if (buff[0] == '\0' || buff[0] == '#') {
94             if (Passing)
95                 printf("%s\n", buff);
96             continue;
97         }
98
99         if (buff[0] != '<' || (p = strchr(buff, '>')) == NULL) {
100             if (Passing)
101                 printf("%s\n", buff);
102             else
103                 warn("line doesn't start with a message ID, ignored: %.40s",
104                      buff);
105             continue;
106         }
107         *++p = '\0';
108
109         if (HISlookup(history, buff, &arrived, &posted, &expires, NULL)) {
110             if (!HISreplace(history, buff, arrived, posted, expires, NULL))
111                 syswarn("cannot write new text for %s", buff);
112         } else {
113             syswarn("no entry for %s", buff);
114         }
115     }
116
117  fail:
118     /* Close files; we're done. */
119     if (history != NULL && !HISclose(history)) {
120         syswarn("cannot close %s", History);
121         rc = 1;
122     }
123
124     return rc;
125 }