File: example.c

package info (click to toggle)
dspam 3.10.1+dfsg-11
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,656 kB
  • sloc: ansic: 26,034; sh: 12,546; perl: 5,469; makefile: 690; sql: 379
file content (332 lines) | stat: -rw-r--r-- 9,957 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* $Id: example.c,v 1.11 2011/06/28 00:13:48 sbajic Exp $ */

/*
 DSPAM
 COPYRIGHT (C) 2002-2011 DSPAM PROJECT

 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU Affero General Public License as
 published by the Free Software Foundation, either version 3 of the
 License, or (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU Affero General Public License for more details.

 You should have received a copy of the GNU Affero General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/

/*
 * example.c - example of libdspam implementation
 *
 * DESCRIPTION
 *
 * 
 * compile with:
 * gcc -o example example.c -ldspam -L.libs -I. -DHAVE_CONFIG_H \
 *   -DCONFIG_DEFAULT=/usr/local/etc/dspam.conf
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <libdspam.h>

#define USERNAME	"testuser"

int
main (int argc, char **argv)
{
  char dspam_home[] = "/var/dspam";     /* Our dspam data home */
  char buffer[1024];
  char *message = malloc (1);
  long len = 1;

  DSPAM_CTX *CTX;               	/* DSPAM Context */
  struct _ds_spam_signature SIG;        /* Example signature */

  /*
     Performs any driver-specific startup functions (such as initializing 
     internal lock tables).  Call this only once when your application
     starts
  */

  dspam_init_driver (NULL);

  /* read in the message from stdin */
  message[0] = 0;
  while (fgets (buffer, sizeof (buffer), stdin) != NULL)
  {
    len += strlen (buffer);
    message = realloc (message, len);
    if (message == NULL)
    {
      fprintf (stderr, "out of memory!");
      exit (EXIT_FAILURE);
    }
    strcat (message, buffer);
  }

  /* MESSAGE PROCESSING */

  /* initialize dspam 

    Operating Modes:

    DSM_PROCESS:	Process message
    DSM_CLASSIFY:	Classify message only (do not write changes)

    Flags:

    DSF_SIGNATURE	Signature Mode (Use a signature)
    DSF_NOISE		Use Bayesian Noise Reduction
    DSF_WHITELIST	Use Automatic Whitelisting

    Tokenizers:

    DSZ_WORD		Use WORD tokenizer
    DSZ_CHAIN		Use CHAIN tokenizer
    DSZ_SBPH		Use SBPH tokenizer
    DSZ_OSB		Use OSB tokenizer

    Training Modes:

    DST_TEFT		Train Everything
    DST_TOE		Train-on-Error
    DST_TUM		Train-until-Mature

    Classifications:

    Used to tell libdspam the message has already been classified, and should
    be processed in such a way that the result will match the classification.

    DSR_ISSPAM		Message is spam (learn as spam)
    DSR_ISINNOCENT	Message is innocent (learn as innocent)
    DSR_NONE		No predetermined classification (classify message)

    Sources:

    Used to tell libdspam the source of the specified classification (if any).

    DSS_ERROR		Misclassification by libdspam
    DSS_CORPUS		Corpusfed message
    DSS_INOCULATION	Message inoculation
    DSS_NONE		No classification source (use only with DSR_NONE)

    NOTE: When using DSS_ERROR, a DSPAM signature should be provided, OR
          the original message in PRISTINE form (without any DSPAM headers and
          with the original message's headers).

  */ 

   /* --------------------------- EXAMPLE 1 ----------------------------*/
                      /* STANDARD INBOUND PROCESSING */

  /* In this example, DSF_SIGNATURE is specified to request that libdspam
     generate a signature, which can be stored internally for future retraining.
     This is useful if the original message in pristine form won't be available
     server-side should the user want to reclassify the message.
  */

  /* Initialize the DSPAM context */
  CTX = dspam_init (USERNAME, NULL, dspam_home, DSM_PROCESS,
                    DSF_SIGNATURE | DSF_NOISE);

  if (CTX == NULL)
  {
    fprintf (stderr, "ERROR: dspam_init failed!\n");
    exit (EXIT_FAILURE);
  }

  /* Use graham and robinson algorithms, graham's p-values */
  CTX->algorithms = DSA_GRAHAM | DSA_BURTON | DSP_GRAHAM;

  /* Use CHAIN tokenizer */
  CTX->tokenizer = DSZ_CHAIN;

  /* Call DSPAM's processor with the message text */
  if (dspam_process (CTX, message) != 0)
  {
    fprintf (stderr, "ERROR: dspam_process failed");
    exit (EXIT_FAILURE);
  }

  /* Print processing results */
  printf ("Probability: %2.4f Confidence: %2.4f, Result: %s\n",
          CTX->probability,
          CTX->confidence,
          (CTX->result == DSR_ISSPAM) ? "Spam" : "Innocent");

  /* Manage signature */
  if (CTX->signature == NULL)
  {
    printf ("No signature provided\n");
  }
  else
  {
    /* Copy to a safe place */

    SIG.data = malloc (CTX->signature->length);
    if (SIG.data != NULL)
      memcpy (SIG.data, CTX->signature->data, CTX->signature->length);
  }
  SIG.length = CTX->signature->length;

  /* Destroy the context */
  dspam_destroy(CTX);

   /* --------------------------- EXAMPLE 2 ----------------------------*/
                 /* SPAM REPORTING (AS MISCLASSIFICATION) */

  /* We call everything just like before, with these exceptions:
     - We set the classification to DSR_ISSPAM
     - We set the source to DSS_ERROR

     This example will use the original message in pristine form instead of
     a signature.  See the next example (false positives) for an example using
     a signature.
  */

  /* Initialize the DSPAM context */
  CTX = dspam_init (USERNAME, NULL, dspam_home, DSM_PROCESS, 0);
  if (CTX == NULL)
  {
    fprintf (stderr, "ERROR: dspam_init failed!\n");
    exit (EXIT_FAILURE);
  }

  /* Set up the context for error correction as spam */
  CTX->classification = DSR_ISSPAM;
  CTX->source         = DSS_ERROR;

  /* Use graham and robinson algorithms, graham's p-values */
  CTX->algorithms = DSA_GRAHAM | DSA_BURTON | DSP_GRAHAM;

  /* Use CHAIN tokenizer */
  CTX->tokenizer = DSZ_CHAIN;

  /* Call DSPAM */
  if (dspam_process(CTX, message) != 0)
  {
    fprintf (stderr, "ERROR: dspam_process failed\n");
    exit (EXIT_FAILURE);
  }

  /* Destroy the context */
  dspam_destroy (CTX);

  printf("Spam retrained successfully.\n");

   /* --------------------------- EXAMPLE 3 ----------------------------*/
                        /* FALSE POSITIVE REPORTING */

  /* Here we submit the message's signature for retraining as a false positive.
     We make the following changes from our original example: 
     - We set the classification to DSR_ISINNOCENT
     - We set the source to DSS_ERROR
     - We attach the binary signature to the context, and pass in a NULL text
  */

  /* Initialize DSPAM context */

  CTX = dspam_init (USERNAME, NULL, dspam_home, DSM_PROCESS, DSF_SIGNATURE);
  if (CTX == NULL)
  {
    fprintf (stderr, "ERROR: dspam_init failed!\n");
    exit (EXIT_FAILURE);
  }

  /* Set up the context for error correction as innocent */
  CTX->classification = DSR_ISINNOCENT;
  CTX->source         = DSS_ERROR;

  /* Use graham and robinson algorithms, graham's p-values */
  CTX->algorithms = DSA_GRAHAM | DSA_BURTON | DSP_GRAHAM;

  /* Use CHAIN tokenizer */

  CTX->tokenizer = DSZ_CHAIN;

  /* Attach the signature to the context */
  CTX->signature = &SIG;

  /* Call DSPAM */
  if (dspam_process(CTX, NULL) != 0)
  {
    fprintf (stderr, "ERROR: dspam_process failed\n");
    exit (EXIT_FAILURE);
  }

  /* Destroy the context */
  dspam_destroy(CTX); 

  printf("False positive retrained successfully.\n");

  /* --------------------------- EXAMPLE 4 ----------------------------*/
             /* USE OF CREATE AND ATTACH WITH ATTRIBUTES API */
                                                                                
  /* If we want to provide libdspam with a set of storage driver attributes
     so that it doesn't have to go looking for a [driver].data file to get
     SQL-server information from, we can use the create/attach mode of
     preparing a context, rather than calling dspam_init()
  */
                                                                                
  /* Create the DSPAM context; called just like dspam_init() */

  CTX = dspam_create (USERNAME, NULL,  dspam_home, DSM_PROCESS, DSF_SIGNATURE);
  if (CTX == NULL)
  {
    fprintf (stderr, "ERROR: dspam_create failed!\n");
    exit (EXIT_FAILURE);
  }

  /* Now we have a context but it is not attached to the storage driver. The
     next step is to set up our set of attributes to tell the driver how to
     connect. In this example, we'll assume we're using a MySQL backend. 
  */

  dspam_addattribute(CTX, "MySQLServer", "127.0.0.1");
  dspam_addattribute(CTX, "MySQLPort", "3306");
  dspam_addattribute(CTX, "MySQLUser", "example");
  dspam_addattribute(CTX, "MySQLPass", "1234");
  dspam_addattribute(CTX, "MySQLDb", "dspam"); 

  /* Use graham and robinson algorithms, graham's p-values */
  CTX->algorithms = DSA_GRAHAM | DSA_BURTON | DSP_GRAHAM;

  /* Use CHAIN tokenizer */
  CTX->tokenizer = DSZ_CHAIN;

  /* Here, we can also pass in any other attributes used by libdspam
     (see dspam.conf). We can also do this after the attach, since they are
     not used until we process a message.
  */

  dspam_addattribute(CTX, "IgnoreHeader", "X-Virus-Scanner-Result");

  /* Now a call to dspam_attach() will connect our context to the storage 
     driver interface and establish a connection. Alternatively, if you have
     an open database handle you may pass it in as the second parameter and
     avoid opening a new database connection */

  if (dspam_attach(CTX, NULL)) {
    fprintf (stderr, "ERROR: dspam_attach failed!\n");
    exit(EXIT_FAILURE);
  }

  /* Then proceed like normal and when we're done, destroy the context like
     we normally do */

  dspam_destroy(CTX); 

  printf("Create/attach performed successfully.\n");

  /* Performs any driver-specific shutdown functions */
  dspam_shutdown_driver (NULL);

  exit (EXIT_SUCCESS);
}