1d7b4d0d0620cd4d75bdb121734e1418c00272f7
[claws.git] / src / plugins / spamassassin / libspamc.h
1 /*
2  * This code is copyright 2001 by Craig Hughes
3  * Conversion to a thread-safe shared library copyright 2002 Liam Widdowson
4  * Portions copyright 2002 by Brad Jorsch
5  * It is licensed under the same license as Perl itself.  The text of this
6  * license is included in the SpamAssassin distribution in the file named
7  * "License".
8  */
9 #ifndef LIBSPAMC_H
10 #define LIBSPAMC_H 1
11
12 #include <sys/types.h>
13 #include <sys/socket.h>
14 #include <netdb.h>
15 #include <stdio.h>
16
17 #define EX_ISSPAM       1
18 #define EX_NOTSPAM      0
19 #define EX_TOOBIG     866
20
21 /* Aug 14, 2002 bj: Bitflags instead of lots of bool parameters */
22 #define SPAMC_MODE_MASK      1
23 #define SPAMC_RAW_MODE       0
24 #define SPAMC_BSMTP_MODE     1
25
26 #define SPAMC_USE_SSL        1<<27
27 #define SPAMC_SAFE_FALLBACK  1<<28
28 #define SPAMC_CHECK_ONLY     1<<29
29
30 /* Aug 14, 2002 bj: A struct for storing a message-in-progress */
31 typedef enum {
32     MESSAGE_NONE,
33     MESSAGE_ERROR,
34     MESSAGE_RAW,
35     MESSAGE_BSMTP,
36     MAX_MESSAGE_TYPE
37 } message_type_t;
38
39 struct message {
40     /* Set before passing the struct on! */
41     int max_len;  /* messages larger than this will return EX_TOOBIG */
42     int timeout;  /* timeout for read() system calls */
43
44     /* Filled in by message_read */
45     message_type_t type;
46     char *raw; int raw_len;   /* Raw message buffer */
47     char *pre; int pre_len;   /* Pre-message data (e.g. SMTP commands) */
48     char *msg; int msg_len;   /* The message */
49     char *post; int post_len; /* Post-message data (e.g. SMTP commands) */
50
51     /* Filled in by filter_message */
52     int is_spam;              /* EX_ISSPAM if the message is spam, EX_NOTSPAM
53                                  if not, EX_TOOBIG if a filtered message is
54                                  returned in out below. */
55     float score, threshold;   /* score and threshold */
56     char *out; int out_len;   /* Output from spamd. Either the filtered
57                                  message, or the check-only response. Or else,
58                                  a pointer to msg above. */
59 };
60
61 /* Aug 14, 2002 bj: New interface functions */
62
63 /* Read in a message from the fd, with the mode specified in the flags.
64  * Returns EX_OK on success, EX_otherwise on failure. On failure, m may be
65  * either MESSAGE_NONE or MESSAGE_ERROR. */
66 int message_read(int in_fd, int flags, struct message *m);
67
68 /* Write out a message to the fd, as specified by m->type. Note that
69  * MESSAGE_NONE messages have nothing to write. Also note that if you ran the
70  * message through message_filter with SPAMC_CHECK_ONLY, it will only output
71  * the "score/threshold" line. */
72 long message_write(int out_fd, struct message *m);
73
74 /* Pass the message through spamd (at addr) as the specified user, with the
75  * given flags. Returns EX_OK on success, or various errors on error. If it was
76  * successful, message_write will print either the CHECK_ONLY output, or the
77  * filtered message in the appropriate output format. */
78 int message_filter(const struct sockaddr *addr, char *username, int flags, struct message *m);
79
80 /* Convert the host/port into a struct sockaddr. Returns EX_OK on success, or
81  * else an error EX. */
82 int lookup_host(const char *hostname, int port, struct sockaddr *a);
83
84 /* Pass the message through one of a set of spamd's. This variant will handle
85  * multiple spamd machines; if a connect failure occurs, it will fail-over to
86  * the next one in the struct hostent. Otherwise identical to message_filter().
87  */
88 int message_filter_with_failover (const struct hostent *hent, int port, char
89     *username, int flags, struct message *m);
90
91 /* Convert the host into a struct hostent, for use with
92  * message_filter_with_failover() above. Returns EX_OK on success, or else an
93  * error EX.  Note that the data filled into hent is from gethostbyname()'s
94  * static storage, so any call to gethostbyname() between
95  * lookup_host_for_failover() and message_filter_with_failover() will overwrite
96  * this.  Take a copy, and use that instead, if you think a call may occur in
97  * your code, or library code that you use (such as syslog()). */
98 int lookup_host_for_failover(const char *hostname, struct hostent *hent);
99
100 /* Dump the message. If there is any data in the message (typically, m->type
101  * will be MESSAGE_ERROR) it will be message_writed. Then, fd_in will be piped
102  * to fd_out intol EOF. This is particularly useful if you get back an
103  * EX_TOOBIG. */
104 void message_dump(int in_fd, int out_fd, struct message *m);
105
106 /* Do a message_read->message_filter->message_write sequence, handling errors
107  * appropriately with dump_message or appropriate CHECK_ONLY output. Returns
108  * EX_OK or EX_ISSPAM/EX_NOTSPAM on success, some error EX on error. */
109 int message_process(const char *hostname, int port, char *username, int max_size, int in_fd, int out_fd, const int flags);
110
111 /* Cleanup the resources we allocated for storing the message. Call after
112  * you're done processing. */
113 void message_cleanup(struct message *m);
114
115 /* Aug 14, 2002 bj: This is now legacy, don't use it. */
116 int process_message(const char *hostname, int port, char *username, 
117                     int max_size, int in_fd, int out_fd,
118                     const int check_only, const int safe_fallback);
119
120 #endif
121