]>
diplodocus.org Git - mdeliver/blob - processor.c
2c015c68e69a465219c6435e5a99bce9bd85a7dd
1 static char ident
[] = "$Id$";
4 #define T_TYPE TRANSPORT_LOCALHOST
5 #define T_SOCK NULL /* for TRANSPORT_UNIX */
6 #define T_HOST NULL /* for TRANSPORT_TCP */
7 #define T_PORT 7783 /* for TRANSPORT_{LOCALHOST,TCP} */
8 #define M_TIME 30 /* call it ham if no answer in 30 seconds */
10 #include <sys/types.h>
27 static int flags
= SPAMC_CHECK_ONLY
| SPAMC_RAW_MODE
;
29 #define err(n, ...) _err(n, true, __FILE__, __LINE__, __VA_ARGS__)
30 #define errx(n, ...) _err(n, false, __FILE__, __LINE__, __VA_ARGS__)
33 _err(int exitcode
, bool want_strerror
, char *fn
, int lineno
, char *format
, ...)
38 /* If anything fails in here, we're fucked, so just exit. If
39 * exitcode is greather than 0 (i.e. the caller is trying to exit
40 * with a failure code), use that, else use EX_IOERR. */
42 if ((fp
= fopen(errfn
, "a")) == NULL
) {
43 exit(exitcode
> 0 ? exitcode
: EX_IOERR
);
46 if (fprintf(fp
, ".mdeliver-processor:%s:%d:", fn
, lineno
) < 0) {
47 exit(exitcode
> 0 ? exitcode
: EX_IOERR
);
51 if (vfprintf(fp
, format
, ap
) < 0) {
52 exit(exitcode
> 0 ? exitcode
: EX_IOERR
);
57 if (fputs(": ", fp
) == EOF
) {
58 exit(exitcode
> 0 ? exitcode
: EX_IOERR
);
60 if (fputs(strerror(errno
), fp
) == EOF
) {
61 exit(exitcode
> 0 ? exitcode
: EX_IOERR
);
64 if (fputs("\n", fp
) == EOF
) {
65 exit(exitcode
> 0 ? exitcode
: EX_IOERR
);
68 if (fclose(fp
) == EOF
) {
69 exit(exitcode
> 0 ? exitcode
: EX_IOERR
);
83 if ((logname
= getenv("LOGNAME")) != NULL
) {
88 if ((p
= getpwuid(getuid())) == NULL
) {
90 err(EX_OSERR
, "getpwuid(getuid())");
92 errx(EX_NOUSER
, "Who are you? What do you want? Why are you here? Where are you going?");
99 get_transport(struct transport
*t
)
104 t
->socketpath
= T_SOCK
;
105 t
->hostname
= T_HOST
;
108 if ((status
= transport_setup(t
, 0)) != EX_OK
) {
109 errx(status
, "transport_setup");
114 get_message(char *fn
, struct message
*m
)
120 /* message_filter stupidly allocates a buffer a little bigger than
121 * max_len for reading the processed message back from spamd even
122 * when told not to. It never uses the buffer. */
125 if ((fd
= open(fn
, O_RDONLY
)) == -1) {
126 err(EX_IOERR
, "open(%s)", fn
);
129 if ((status
= message_read(fd
, flags
, m
)) != EX_OK
) {
130 errx(status
, "message_read");
135 is_spam(char *message
)
142 get_message(message
, &m
);
144 if ((status
= message_filter(&t
, logname(), flags
, &m
)) != EX_OK
) {
145 errx(status
, "message_filter");
148 if (m
.is_spam
== EX_ISSPAM
) {
150 } else if (m
.is_spam
== EX_NOTSPAM
) {
154 errx(m
.is_spam
, "message_filter");
162 size_t len
= strlen(tmp
);
163 errfn
= malloc(len
+ 1);
164 /* 5 for spam/ and 1 for terminating null */
165 char *new = malloc(len
+ 5 + 1);
167 strcpy(errfn
, "err");
168 strcat(errfn
, tmp
+ 3);
172 strcpy(new, "spam/");
175 strcat(new, tmp
+ 3);
177 if (rename(tmp
, new) == -1) {
178 err(EX_OSERR
, "rename(%s, %s)", tmp
, new);
186 main(int argc
, char *argv
[])
192 if (argv
[1][0] == '-' && argv
[1][1] == 'v') {
197 return process(argv
[1]);