chiark / gitweb /
WIP inotify configure test
[inn-innduct.git] / contrib / respool.c
1 /*
2 ** Refile articles into the storage manager under the current storage.conf
3 ** rules, deleting articles from their old place in the spool.
4 ** Written 10-09-99 by rmtodd@servalan.servalan.com
5 **
6 ** Note that history and overview will have to be rebuilt for the moved
7 ** articles to be visible after they're moved.
8 */
9
10 /* include foo needed by libinn/storage manager */
11 #include "config.h"
12 #include "clibrary.h"
13 #include <errno.h>
14
15 #include "inn/innconf.h"
16 #include "inn/qio.h"
17 #include "libinn.h"
18 #include "paths.h"
19 #include "storage.h"
20
21 char *ME;
22
23 static void
24 ProcessLine(char *line)
25 {
26     char *tokenptr;
27     int len;
28     ARTHANDLE *art;
29     ARTHANDLE newart;
30     TOKEN token, newtoken;
31     char *arttmp;
32     time_t arrived;
33
34     tokenptr = line;
35     
36     /* zap newline at end of tokenptr, if present. */
37     len = strlen(tokenptr);
38     if (tokenptr[len-1] == '\n') {
39         tokenptr[len-1] = '\0';
40     }
41
42     token = TextToToken(tokenptr);
43     if ((art = SMretrieve(token, RETR_ALL)) == NULL) return;
44
45     len = art->len;
46     arrived = art->arrived;
47     arttmp = xmalloc(len);
48     memcpy(arttmp, art->data, len);
49     SMfreearticle(art);
50     if (!SMcancel(token)) {
51         fprintf(stderr, "%s: cant cancel %s:%s\n", ME, tokenptr, SMerrorstr);
52         return;
53     }
54
55     newart.data = arttmp;
56     newart.len = len;
57     newart.arrived = (time_t) 0; /* set current time */
58     newart.token = (TOKEN *)NULL;
59
60     newtoken = SMstore(newart);
61     if (newtoken.type == TOKEN_EMPTY) {
62         fprintf(stderr, "%s: cant store article:%s\n", ME, SMerrorstr);
63         return;
64     }
65     free(arttmp);
66     printf("refiled %s ",TokenToText(token));
67     printf("to %s\n", TokenToText(newtoken));
68     return;
69 }
70
71 int
72 main(int argc UNUSED, char *argv[])
73 {
74     bool        one = true;
75     char        buff[SMBUF];
76
77     ME = argv[0];
78
79     if (!innconf_read(NULL))
80         exit(1);
81
82     if (!SMsetup(SM_PREOPEN, &one) || !SMsetup(SM_RDWR, (void *)&one)) {
83         fprintf(stderr, "can't init storage manager");
84         exit(1);
85     }
86     if (!SMinit()) {
87         fprintf(stderr, "Can't init storage manager: %s", SMerrorstr);
88     }
89     while (fgets(buff, SMBUF, stdin)) {
90         ProcessLine(buff);
91     }
92     printf("\nYou will now need to rebuild history and overview for the moved"
93            "\narticles to be visible again.\n");
94     exit(0);
95 }