3 * $Id: qmail-checkspam.c,v 1.1 2003/10/01 00:08:57 mdw Exp $
5 * Filter messages for spam
7 * (c) 2003 Mark Wooding
10 /*----- Licensing notice --------------------------------------------------*
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27 /*----- Revision history --------------------------------------------------*
29 * $Log: qmail-checkspam.c,v $
30 * Revision 1.1 2003/10/01 00:08:57 mdw
31 * Collection of miscellaneous ill-documented tools.
35 /*----- Header files ------------------------------------------------------*/
43 #include <sys/types.h>
44 #include <sys/unistd.h>
48 /*----- Main code ---------------------------------------------------------*/
50 static const char *strenv(const char *e
, const char *d
)
52 const char *p
= getenv(e
);
57 static double dblenv(const char *e
, double d
)
59 const char *p
= getenv(e
);
66 if (errno
) return (d
);
71 static int intenv(const char *e
, int d
)
73 const char *p
= getenv(e
);
80 if (errno
) return (d
);
82 if (l
< 0 || l
> INT_MAX
) return (d
);
86 int shovel(int from
, int to
)
94 n
= read(from
, buf
, sizeof(buf
));
95 if (n
< 0 && errno
!= EINTR
&& errno
!= EAGAIN
)
103 if (n
<= 0 && errno
!= EINTR
&& errno
!= EAGAIN
)
111 int main(int argc
, char *argv
[])
115 int fd_m
[2], fd_e
[2];
119 m
.max_len
= intenv("QMAIL_CHECKSPAM_MAXLEN", 2 * 1024 * 1024);
120 m
.timeout
= intenv("QMAIL_CHECKSPAM_TIMEOUT", 300);
121 rc
= message_read(0, 0, &m
);
122 if (rc
!= 0 && rc
!= EX_TOOBIG
)
125 if (lookup_host(strenv("QMAIL_CHECKSPAM_SPAMDHOST", "localhost"),
126 intenv("QMAIL_CHECKSPAM_SPAMDPORT", 783),
129 if (message_filter(&sa
, "spamd", 0, &m
))
131 if (m
.score
>= dblenv("QMAIL_CHECKSPAM_THRESH", m
.threshold
))
134 if (pipe(fd_m
) || pipe(fd_e
))
136 if ((kid
= fork()) < 0)
141 if (message_write(fd_m
[1], &m
) < 0)
143 if (rc
== EX_TOOBIG
&& shovel(0, fd_m
[1]))
146 if (shovel(1, fd_e
[1]))
158 execlp(strenv("QMAIL_CHECKSPAM_QUEUE", "/var/qmail/bin/qmail-queue"),
160 fprintf(stderr
, "failed to exec: %s\n", strerror(errno
));
164 /*----- That's all, folks -------------------------------------------------*/