1 | /* $Id: example.c,v 1.11 2011/06/28 00:13:48 sbajic Exp $ */ |
---|
2 | |
---|
3 | /* |
---|
4 | DSPAM |
---|
5 | COPYRIGHT (C) 2002-2012 DSPAM PROJECT |
---|
6 | |
---|
7 | This program is free software: you can redistribute it and/or modify |
---|
8 | it under the terms of the GNU Affero General Public License as |
---|
9 | published by the Free Software Foundation, either version 3 of the |
---|
10 | License, or (at your option) any later version. |
---|
11 | |
---|
12 | This program is distributed in the hope that it will be useful, |
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
15 | GNU Affero General Public License for more details. |
---|
16 | |
---|
17 | You should have received a copy of the GNU Affero General Public License |
---|
18 | along with this program. If not, see <http://www.gnu.org/licenses/>. |
---|
19 | |
---|
20 | */ |
---|
21 | |
---|
22 | /* |
---|
23 | * example.c - example of libdspam implementation |
---|
24 | * |
---|
25 | * DESCRIPTION |
---|
26 | * |
---|
27 | * |
---|
28 | * compile with: |
---|
29 | * gcc -o example example.c -ldspam -L.libs -I. -DHAVE_CONFIG_H \ |
---|
30 | * -DCONFIG_DEFAULT=/usr/local/etc/dspam.conf |
---|
31 | */ |
---|
32 | |
---|
33 | #include <stdio.h> |
---|
34 | #include <stdlib.h> |
---|
35 | #include <string.h> |
---|
36 | #include <libdspam.h> |
---|
37 | |
---|
38 | #define USERNAME "testuser" |
---|
39 | |
---|
40 | int |
---|
41 | main (int argc, char **argv) |
---|
42 | { |
---|
43 | char dspam_home[] = "/var/dspam"; /* Our dspam data home */ |
---|
44 | char buffer[1024]; |
---|
45 | char *message = malloc (1); |
---|
46 | long len = 1; |
---|
47 | |
---|
48 | DSPAM_CTX *CTX; /* DSPAM Context */ |
---|
49 | struct _ds_spam_signature SIG; /* Example signature */ |
---|
50 | |
---|
51 | /* |
---|
52 | Performs any driver-specific startup functions (such as initializing |
---|
53 | internal lock tables). Call this only once when your application |
---|
54 | starts |
---|
55 | */ |
---|
56 | |
---|
57 | dspam_init_driver (NULL); |
---|
58 | |
---|
59 | /* read in the message from stdin */ |
---|
60 | message[0] = 0; |
---|
61 | while (fgets (buffer, sizeof (buffer), stdin) != NULL) |
---|
62 | { |
---|
63 | len += strlen (buffer); |
---|
64 | message = realloc (message, len); |
---|
65 | if (message == NULL) |
---|
66 | { |
---|
67 | fprintf (stderr, "out of memory!"); |
---|
68 | exit (EXIT_FAILURE); |
---|
69 | } |
---|
70 | strcat (message, buffer); |
---|
71 | } |
---|
72 | |
---|
73 | /* MESSAGE PROCESSING */ |
---|
74 | |
---|
75 | /* initialize dspam |
---|
76 | |
---|
77 | Operating Modes: |
---|
78 | |
---|
79 | DSM_PROCESS: Process message |
---|
80 | DSM_CLASSIFY: Classify message only (do not write changes) |
---|
81 | |
---|
82 | Flags: |
---|
83 | |
---|
84 | DSF_SIGNATURE Signature Mode (Use a signature) |
---|
85 | DSF_NOISE Use Bayesian Noise Reduction |
---|
86 | DSF_WHITELIST Use Automatic Whitelisting |
---|
87 | |
---|
88 | Tokenizers: |
---|
89 | |
---|
90 | DSZ_WORD Use WORD tokenizer |
---|
91 | DSZ_CHAIN Use CHAIN tokenizer |
---|
92 | DSZ_SBPH Use SBPH tokenizer |
---|
93 | DSZ_OSB Use OSB tokenizer |
---|
94 | |
---|
95 | Training Modes: |
---|
96 | |
---|
97 | DST_TEFT Train Everything |
---|
98 | DST_TOE Train-on-Error |
---|
99 | DST_TUM Train-until-Mature |
---|
100 | |
---|
101 | Classifications: |
---|
102 | |
---|
103 | Used to tell libdspam the message has already been classified, and should |
---|
104 | be processed in such a way that the result will match the classification. |
---|
105 | |
---|
106 | DSR_ISSPAM Message is spam (learn as spam) |
---|
107 | DSR_ISINNOCENT Message is innocent (learn as innocent) |
---|
108 | DSR_NONE No predetermined classification (classify message) |
---|
109 | |
---|
110 | Sources: |
---|
111 | |
---|
112 | Used to tell libdspam the source of the specified classification (if any). |
---|
113 | |
---|
114 | DSS_ERROR Misclassification by libdspam |
---|
115 | DSS_CORPUS Corpusfed message |
---|
116 | DSS_INOCULATION Message inoculation |
---|
117 | DSS_NONE No classification source (use only with DSR_NONE) |
---|
118 | |
---|
119 | NOTE: When using DSS_ERROR, a DSPAM signature should be provided, OR |
---|
120 | the original message in PRISTINE form (without any DSPAM headers and |
---|
121 | with the original message's headers). |
---|
122 | |
---|
123 | */ |
---|
124 | |
---|
125 | /* --------------------------- EXAMPLE 1 ----------------------------*/ |
---|
126 | /* STANDARD INBOUND PROCESSING */ |
---|
127 | |
---|
128 | /* In this example, DSF_SIGNATURE is specified to request that libdspam |
---|
129 | generate a signature, which can be stored internally for future retraining. |
---|
130 | This is useful if the original message in pristine form won't be available |
---|
131 | server-side should the user want to reclassify the message. |
---|
132 | */ |
---|
133 | |
---|
134 | /* Initialize the DSPAM context */ |
---|
135 | CTX = dspam_init (USERNAME, NULL, dspam_home, DSM_PROCESS, |
---|
136 | DSF_SIGNATURE | DSF_NOISE); |
---|
137 | |
---|
138 | if (CTX == NULL) |
---|
139 | { |
---|
140 | fprintf (stderr, "ERROR: dspam_init failed!\n"); |
---|
141 | exit (EXIT_FAILURE); |
---|
142 | } |
---|
143 | |
---|
144 | /* Use graham and robinson algorithms, graham's p-values */ |
---|
145 | CTX->algorithms = DSA_GRAHAM | DSA_BURTON | DSP_GRAHAM; |
---|
146 | |
---|
147 | /* Use CHAIN tokenizer */ |
---|
148 | CTX->tokenizer = DSZ_CHAIN; |
---|
149 | |
---|
150 | /* Call DSPAM's processor with the message text */ |
---|
151 | if (dspam_process (CTX, message) != 0) |
---|
152 | { |
---|
153 | fprintf (stderr, "ERROR: dspam_process failed"); |
---|
154 | exit (EXIT_FAILURE); |
---|
155 | } |
---|
156 | |
---|
157 | /* Print processing results */ |
---|
158 | printf ("Probability: %2.4f Confidence: %2.4f, Result: %s\n", |
---|
159 | CTX->probability, |
---|
160 | CTX->confidence, |
---|
161 | (CTX->result == DSR_ISSPAM) ? "Spam" : "Innocent"); |
---|
162 | |
---|
163 | /* Manage signature */ |
---|
164 | if (CTX->signature == NULL) |
---|
165 | { |
---|
166 | printf ("No signature provided\n"); |
---|
167 | } |
---|
168 | else |
---|
169 | { |
---|
170 | /* Copy to a safe place */ |
---|
171 | |
---|
172 | SIG.data = malloc (CTX->signature->length); |
---|
173 | if (SIG.data != NULL) |
---|
174 | memcpy (SIG.data, CTX->signature->data, CTX->signature->length); |
---|
175 | } |
---|
176 | SIG.length = CTX->signature->length; |
---|
177 | |
---|
178 | /* Destroy the context */ |
---|
179 | dspam_destroy(CTX); |
---|
180 | |
---|
181 | /* --------------------------- EXAMPLE 2 ----------------------------*/ |
---|
182 | /* SPAM REPORTING (AS MISCLASSIFICATION) */ |
---|
183 | |
---|
184 | /* We call everything just like before, with these exceptions: |
---|
185 | - We set the classification to DSR_ISSPAM |
---|
186 | - We set the source to DSS_ERROR |
---|
187 | |
---|
188 | This example will use the original message in pristine form instead of |
---|
189 | a signature. See the next example (false positives) for an example using |
---|
190 | a signature. |
---|
191 | */ |
---|
192 | |
---|
193 | /* Initialize the DSPAM context */ |
---|
194 | CTX = dspam_init (USERNAME, NULL, dspam_home, DSM_PROCESS, 0); |
---|
195 | if (CTX == NULL) |
---|
196 | { |
---|
197 | fprintf (stderr, "ERROR: dspam_init failed!\n"); |
---|
198 | exit (EXIT_FAILURE); |
---|
199 | } |
---|
200 | |
---|
201 | /* Set up the context for error correction as spam */ |
---|
202 | CTX->classification = DSR_ISSPAM; |
---|
203 | CTX->source = DSS_ERROR; |
---|
204 | |
---|
205 | /* Use graham and robinson algorithms, graham's p-values */ |
---|
206 | CTX->algorithms = DSA_GRAHAM | DSA_BURTON | DSP_GRAHAM; |
---|
207 | |
---|
208 | /* Use CHAIN tokenizer */ |
---|
209 | CTX->tokenizer = DSZ_CHAIN; |
---|
210 | |
---|
211 | /* Call DSPAM */ |
---|
212 | if (dspam_process(CTX, message) != 0) |
---|
213 | { |
---|
214 | fprintf (stderr, "ERROR: dspam_process failed\n"); |
---|
215 | exit (EXIT_FAILURE); |
---|
216 | } |
---|
217 | |
---|
218 | /* Destroy the context */ |
---|
219 | dspam_destroy (CTX); |
---|
220 | |
---|
221 | printf("Spam retrained successfully.\n"); |
---|
222 | |
---|
223 | /* --------------------------- EXAMPLE 3 ----------------------------*/ |
---|
224 | /* FALSE POSITIVE REPORTING */ |
---|
225 | |
---|
226 | /* Here we submit the message's signature for retraining as a false positive. |
---|
227 | We make the following changes from our original example: |
---|
228 | - We set the classification to DSR_ISINNOCENT |
---|
229 | - We set the source to DSS_ERROR |
---|
230 | - We attach the binary signature to the context, and pass in a NULL text |
---|
231 | */ |
---|
232 | |
---|
233 | /* Initialize DSPAM context */ |
---|
234 | |
---|
235 | CTX = dspam_init (USERNAME, NULL, dspam_home, DSM_PROCESS, DSF_SIGNATURE); |
---|
236 | if (CTX == NULL) |
---|
237 | { |
---|
238 | fprintf (stderr, "ERROR: dspam_init failed!\n"); |
---|
239 | exit (EXIT_FAILURE); |
---|
240 | } |
---|
241 | |
---|
242 | /* Set up the context for error correction as innocent */ |
---|
243 | CTX->classification = DSR_ISINNOCENT; |
---|
244 | CTX->source = DSS_ERROR; |
---|
245 | |
---|
246 | /* Use graham and robinson algorithms, graham's p-values */ |
---|
247 | CTX->algorithms = DSA_GRAHAM | DSA_BURTON | DSP_GRAHAM; |
---|
248 | |
---|
249 | /* Use CHAIN tokenizer */ |
---|
250 | |
---|
251 | CTX->tokenizer = DSZ_CHAIN; |
---|
252 | |
---|
253 | /* Attach the signature to the context */ |
---|
254 | CTX->signature = &SIG; |
---|
255 | |
---|
256 | /* Call DSPAM */ |
---|
257 | if (dspam_process(CTX, NULL) != 0) |
---|
258 | { |
---|
259 | fprintf (stderr, "ERROR: dspam_process failed\n"); |
---|
260 | exit (EXIT_FAILURE); |
---|
261 | } |
---|
262 | |
---|
263 | /* Destroy the context */ |
---|
264 | dspam_destroy(CTX); |
---|
265 | |
---|
266 | printf("False positive retrained successfully.\n"); |
---|
267 | |
---|
268 | /* --------------------------- EXAMPLE 4 ----------------------------*/ |
---|
269 | /* USE OF CREATE AND ATTACH WITH ATTRIBUTES API */ |
---|
270 | |
---|
271 | /* If we want to provide libdspam with a set of storage driver attributes |
---|
272 | so that it doesn't have to go looking for a [driver].data file to get |
---|
273 | SQL-server information from, we can use the create/attach mode of |
---|
274 | preparing a context, rather than calling dspam_init() |
---|
275 | */ |
---|
276 | |
---|
277 | /* Create the DSPAM context; called just like dspam_init() */ |
---|
278 | |
---|
279 | CTX = dspam_create (USERNAME, NULL, dspam_home, DSM_PROCESS, DSF_SIGNATURE); |
---|
280 | if (CTX == NULL) |
---|
281 | { |
---|
282 | fprintf (stderr, "ERROR: dspam_create failed!\n"); |
---|
283 | exit (EXIT_FAILURE); |
---|
284 | } |
---|
285 | |
---|
286 | /* Now we have a context but it is not attached to the storage driver. The |
---|
287 | next step is to set up our set of attributes to tell the driver how to |
---|
288 | connect. In this example, we'll assume we're using a MySQL backend. |
---|
289 | */ |
---|
290 | |
---|
291 | dspam_addattribute(CTX, "MySQLServer", "127.0.0.1"); |
---|
292 | dspam_addattribute(CTX, "MySQLPort", "3306"); |
---|
293 | dspam_addattribute(CTX, "MySQLUser", "example"); |
---|
294 | dspam_addattribute(CTX, "MySQLPass", "1234"); |
---|
295 | dspam_addattribute(CTX, "MySQLDb", "dspam"); |
---|
296 | |
---|
297 | /* Use graham and robinson algorithms, graham's p-values */ |
---|
298 | CTX->algorithms = DSA_GRAHAM | DSA_BURTON | DSP_GRAHAM; |
---|
299 | |
---|
300 | /* Use CHAIN tokenizer */ |
---|
301 | CTX->tokenizer = DSZ_CHAIN; |
---|
302 | |
---|
303 | /* Here, we can also pass in any other attributes used by libdspam |
---|
304 | (see dspam.conf). We can also do this after the attach, since they are |
---|
305 | not used until we process a message. |
---|
306 | */ |
---|
307 | |
---|
308 | dspam_addattribute(CTX, "IgnoreHeader", "X-Virus-Scanner-Result"); |
---|
309 | |
---|
310 | /* Now a call to dspam_attach() will connect our context to the storage |
---|
311 | driver interface and establish a connection. Alternatively, if you have |
---|
312 | an open database handle you may pass it in as the second parameter and |
---|
313 | avoid opening a new database connection */ |
---|
314 | |
---|
315 | if (dspam_attach(CTX, NULL)) { |
---|
316 | fprintf (stderr, "ERROR: dspam_attach failed!\n"); |
---|
317 | exit(EXIT_FAILURE); |
---|
318 | } |
---|
319 | |
---|
320 | /* Then proceed like normal and when we're done, destroy the context like |
---|
321 | we normally do */ |
---|
322 | |
---|
323 | dspam_destroy(CTX); |
---|
324 | |
---|
325 | printf("Create/attach performed successfully.\n"); |
---|
326 | |
---|
327 | /* Performs any driver-specific shutdown functions */ |
---|
328 | dspam_shutdown_driver (NULL); |
---|
329 | |
---|
330 | exit (EXIT_SUCCESS); |
---|
331 | } |
---|
332 | |
---|