sync 098claws
[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 <netinet/in.h>
15 #include <netdb.h>
16 #include <stdio.h>
17
18 #define EX_NOTSPAM                0
19 #define EX_ISSPAM                 1
20 #define EX_TOOBIG               866
21
22 /* Aug 14, 2002 bj: Bitflags instead of lots of bool parameters */
23 #define SPAMC_MODE_MASK      1
24 #define SPAMC_RAW_MODE       0
25 #define SPAMC_BSMTP_MODE     1
26
27 #define SPAMC_USE_SSL        (1<<27)
28 #define SPAMC_SAFE_FALLBACK  (1<<28)
29 #define SPAMC_CHECK_ONLY     (1<<29)
30
31 /* Jan 30, 2003 ym: added reporting options */
32 #define SPAMC_REPORT         (1<<26)
33 #define SPAMC_REPORT_IFSPAM  (1<<25)
34
35 /* Feb  1 2003 jm: might as well fix bug 191 as well */
36 #define SPAMC_SYMBOLS        (1<<24)
37
38 /* 2003/04/16 SJF: randomize hostname order (quasi load balancing) */
39 #define SPAMC_RANDOMIZE_HOSTS (1<<23)
40
41
42 /* Aug 14, 2002 bj: A struct for storing a message-in-progress */
43 typedef enum {
44     MESSAGE_NONE,
45     MESSAGE_ERROR,
46     MESSAGE_RAW,
47     MESSAGE_BSMTP,
48     MAX_MESSAGE_TYPE
49 } message_type_t;
50
51 struct libspamc_private_message;
52
53 struct message {
54     /* Set before passing the struct on! */
55     int max_len;  /* messages larger than this will return EX_TOOBIG */
56     int timeout;  /* timeout for read() system calls */
57
58     /* Filled in by message_read */
59     message_type_t type;
60     char *raw; int raw_len;   /* Raw message buffer */
61     char *pre; int pre_len;   /* Pre-message data (e.g. SMTP commands) */
62     char *msg; int msg_len;   /* The message */
63     char *post; int post_len; /* Post-message data (e.g. SMTP commands) */
64     int content_length;
65
66     /* Filled in by filter_message */
67     int is_spam;              /* EX_ISSPAM if the message is spam, EX_NOTSPAM
68                                  if not */
69     float score, threshold;   /* score and threshold */
70     char *out; int out_len;   /* Output from spamd. Either the filtered
71                                  message, or the check-only response. Or else,
72                                  a pointer to msg above. */
73
74     /* these members added in SpamAssassin version 2.60: */
75     struct libspamc_private_message *priv;
76 };
77
78 /*------------------------------------------------------------------------
79  * TRANSPORT (2004/04/16 - SJF)
80  *
81  * The code to connect with the daemon has gotten more complicated: support
82  * for SSL, fallback to multiple hosts, and using UNIX domain sockets. The
83  * code has gotten ugly with way too many parameters being passed all around.
84  *
85  * So we've created this object to hold all the info required to connect with
86  * the remote site, including a self-contained list of all the IP addresses
87  * in the event this is using TCP sockets. These multiple IPs can be obtained
88  * only from DNS returning more than one A record for a single name, and
89  * this allows for fallback.
90  *
91  * We also allow a kind of quasi-load balancing, where we take the list of
92  * A records from DNS and randomize them before starting out - this lets
93  * us spread the load out among multiple servers if desired. The idea for
94  * load balancing goes to Jeremy Zawodny.
95  *
96  * By putting all our data here, we remove "fallback" from being a special
97  * case. We may find ourselves with several IP addresses, but if the user
98  * disables fallback, we set the IP address count to one. Now the connect
99  * code just loops over that same address.
100  */
101 #define TRANSPORT_LOCALHOST 0x01        /* TCP to localhost only */
102 #define TRANSPORT_TCP       0x02        /* standard TCP socket   */
103 #define TRANSPORT_UNIX      0x03        /* UNIX domain socket    */
104
105 #define TRANSPORT_MAX_HOSTS 256         /* max hosts we can failover between */
106
107 struct transport {
108         int             type;
109
110         const char      *socketpath;    /* for UNIX dommain socket      */
111         const char      *hostname;      /* for TCP sockets              */
112
113         unsigned short  port;           /* for TCP sockets              */
114
115         struct in_addr  hosts[TRANSPORT_MAX_HOSTS];
116         int             nhosts;
117 };
118
119 extern void transport_init(struct transport *tp);
120 extern int  transport_setup(struct transport *tp, int flags);
121
122 /* Aug 14, 2002 bj: New interface functions */
123
124 /* Read in a message from the fd, with the mode specified in the flags.
125  * Returns EX_OK on success, EX_otherwise on failure. On failure, m may be
126  * either MESSAGE_NONE or MESSAGE_ERROR. */
127 int message_read(int in_fd, int flags, struct message *m);
128
129 /* Write out a message to the fd, as specified by m->type. Note that
130  * MESSAGE_NONE messages have nothing to write. Also note that if you ran the
131  * message through message_filter with SPAMC_CHECK_ONLY, it will only output
132  * the "score/threshold" line. */
133 long message_write(int out_fd, struct message *m);
134
135 /* Process the message through the spamd filter, making as many connection
136  * attempts as are implied by the transport structure. To make this do
137  * failover, more than one host is defined, but if there is only one there,
138  * no failover is done.
139  */
140 int message_filter(struct transport *tp, const char *username,
141                 int flags, struct message *m);
142
143 /* Dump the message. If there is any data in the message (typically, m->type
144  * will be MESSAGE_ERROR) it will be message_writed. Then, fd_in will be piped
145  * to fd_out intol EOF. This is particularly useful if you get back an
146  * EX_TOOBIG. */
147 void message_dump(int in_fd, int out_fd, struct message *m);
148
149 /* Do a message_read->message_filter->message_write sequence, handling errors
150  * appropriately with dump_message or appropriate CHECK_ONLY output. Returns
151  * EX_OK or EX_ISSPAM/EX_NOTSPAM on success, some error EX on error. */
152 int message_process(struct transport *trans, char *username, int max_size, int in_fd, int out_fd, const int flags);
153
154 /* Cleanup the resources we allocated for storing the message. Call after
155  * you're done processing. */
156 void message_cleanup(struct message *m);
157
158 /* Aug 14, 2002 bj: This is now legacy, don't use it. */
159 int process_message(struct transport *tp, char *username, 
160                     int max_size, int in_fd, int out_fd,
161                     const int check_only, const int safe_fallback);
162
163 #endif
164