File: cvtbatch.c

package info (click to toggle)
inn2 2.5.2-2~squeeze1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 11,072 kB
  • ctags: 8,521
  • sloc: ansic: 91,418; sh: 13,249; perl: 12,311; makefile: 2,928; yacc: 868; python: 342; lex: 266
file content (138 lines) | stat: -rw-r--r-- 2,834 bytes parent folder | download | duplicates (3)
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
/*  $Id: cvtbatch.c 8788 2009-11-15 09:27:11Z iulius $
**
**  Read file list on standard input and spew out batch files.
*/

#include "config.h"
#include "clibrary.h"

#include "inn/innconf.h"
#include "inn/messages.h"
#include "inn/qio.h"
#include "inn/wire.h"
#include "inn/libinn.h"
#include "inn/paths.h"
#include "inn/storage.h"


int
main(int ac, char *av[]) {
    int		i;
    QIOSTATE	*qp;
    char	*line;
    const char	*text;
    char	*format;
    char	*p, *q;
    const char	*r;
    bool	Dirty;
    TOKEN	token;
    ARTHANDLE	*art;
    size_t	len;
    time_t      arrived;

    /* First thing, set up our identity. */
    message_program_name = "cvtbatch";
    if (!innconf_read(NULL))
        exit(1);

    /* Parse JCL. */
    format = xstrdup("nm");
    while ((i = getopt(ac, av, "w:")) != EOF)
	switch (i) {
	default:
            die("usage error");
            break;
	case 'w':
            free(format);
	    for (p = format = optarg; *p; p++) {
		switch (*p) {
		case FEED_BYTESIZE:
                case FEED_TIMERECEIVED:
		case FEED_FULLNAME:
		case FEED_MESSAGEID:
		case FEED_NAME:
		    continue;
		}
                warn("ignoring %c in -w flag", *p);
	    }
	}
    ac -= optind;
    av += optind;
    if (ac)
	die("usage error");

    if (!SMinit())
        die("cannot initialize storage manager: %s", SMerrorstr);

    /* Loop over all input. */
    qp = QIOfdopen((int)fileno(stdin));
    while ((line = QIOread(qp)) != NULL) {
	for (p = line; *p; p++)
	    if (ISWHITE(*p)) {
		*p = '\0';
		break;
	    }

	if (!IsToken(line))
	    continue;
	token = TextToToken(line);
	if ((art = SMretrieve(token, RETR_ALL)) == NULL)
	    continue;
        text = wire_findheader(art->data, art->len, "Message-ID", true);
	if (text == NULL) {
	    SMfreearticle(art);
	    continue;
	}
	len = art->len;
        arrived = art->arrived;

	for (r = text; r < art->data + art->len; r++) {
	    if (*r == '\r' || *r == '\n')
		break;
	}
	if (r == art->data + art->len) {
	    SMfreearticle(art);
	    continue;
	}
	q = xmalloc(r - text + 1);
	memcpy(q, text, r - text);
	SMfreearticle(art);
	q[r - text] = '\0';

	/* Write the desired info. */
	for (Dirty = false, p = format; *p; p++) {
	    switch (*p) {
	    default:
		continue;
	    case FEED_BYTESIZE:
		if (Dirty)
		    putchar(' ');
		printf("%lu", (unsigned long)len);
		break;
	    case FEED_FULLNAME:
	    case FEED_NAME:
		if (Dirty)
		    putchar(' ');
		printf("%s", line);
		break;
	    case FEED_MESSAGEID:
		if (Dirty)
		    putchar(' ');
		printf("%s", q);
		break;
            case FEED_TIMERECEIVED:
                if (Dirty)
                    putchar(' ');
                printf("%lu", (unsigned long) arrived);
                break;
	    }
	    Dirty = true;
	}
	free(q);
	if (Dirty)
	    putchar('\n');
    }

    exit(0);
    /* NOTREACHED */
}