File: bmf.c

package info (click to toggle)
bmf 0.9.4-9
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 388 kB
  • ctags: 428
  • sloc: ansic: 5,517; sh: 322; makefile: 132
file content (339 lines) | stat: -rw-r--r-- 9,634 bytes parent folder | download | duplicates (6)
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
333
334
335
336
337
338
339
/* $Id: bmf.c,v 1.20 2002/10/20 18:19:17 tommy Exp $ */

/*
 * Copyright (c) 2002 Tom Marshall <tommy@tig-grr.com>
 *
 * This program is free software.  It may be distributed under the terms
 * in the file LICENSE, found in the top level of the distribution.
 *
 * bmf.c: top level Bayesian mail filter app.
 */

#include "config.h"
#include "dbg.h"
#include "str.h"
#include "lex.h"
#include "vec.h"
#include "dbh.h"
#include "filt.h"

/* modes of operation (mutually exclusive) */
typedef enum
{
    mode_test,      /* test and produce report */
    mode_normal,    /* test and register result */
    mode_reg_s,     /* register as spam */
    mode_reg_n,     /* register as non-spam */
    mode_n_to_s,    /* undo non-spam registration and register as spam */
    mode_s_to_n     /* undo spam registration and register as non-spam */
} runmode_t;

static void usage( void )
{
    printf( "\n"
            "Usage: " PACKAGE " [mode] [options]\n"
            "\n"
            "Modes of operation (mutually exclusive; the last one specified is used):\n"
            "\t\tRegister message using historical data if no mode is specified.\n"
            "\t-n\tRegister message as non-spam.\n"
            "\t-s\tRegister message as spam.\n"
            "\t-N\tRegister message as non-spam and undo prior registration as spam.\n"
            "\t-S\tRegister message as spam and undo prior registration as non-spam.\n"
            "\t-t\tTest mode, print report and do not save results.\n"
            "\n"
            "Other options:\n"
            "\t-f fmt\tSpecify database format (text|db|mysql).\n"
            "\t-d db\tSpecify database or directory name.\n"
            "\t-i file\tSpecify file to read instead of stdin.\n"
            "\t-k n\tSpecify count of extrema to use (keepers), default is 15.\n"
            "\t-m type\t[DEPRECATED] Specify mail storage format (mbox|maildir)\n"
            "\t-p\tPassthrough mode, like SpamAssassin.\n"
            "\t-v\tIncrease verbosity level.\n"
            "\t-V\tShow version information and exit.\n"
            "\t-h\tShow this message and exit.\n"
            "\n" );
    exit( 2 );
}

static void version( void )
{
    printf( "\n"
            PACKAGE " version " VERSION " - a Bayesian mail filter\n"
            "Copyright (c) 2002 Tom Marshall\n"
            "\n"
            PACKAGE " comes with ABSOLUTELY NO WARRANTY.\n"
            "This is free software.  You are welcome to redistribute it under the terms\n"
            "of the GNU General Public License.  See the file LICENSE in the source\n"
            "distribution, or visit http://www.gnu.org/licenses/gpl.html\n"
            "\n" );
    exit( 2 );
}

int main( int argc, char** argv )
{
    int         ch;
    dbfmt_t     dbfmt = db_db;
    char*       dbname = NULL;
    bool_t      rdonly;

    runmode_t   mode = mode_normal;
    mbox_t      mboxtype = detect;
    bool_t      do_passthru = false;

    dbh_t*      pdb;
    dbt_t*      pblist;
    dbt_t*      pglist;
    dbt_t*      ptable;
    vec_t       mlist;
    stats_t     stats;
    lex_t       lex;
    tok_t       tok;
    bool_t      is_spam;

    int fd = STDIN_FILENO;
    char* infile = NULL;

    srand(time(NULL));
    atexit( dump_alloc_heap );

#ifdef HAVE_LIBDB
    dbfmt = db_db;
#else
    dbfmt = db_text;
#endif

    stats.keepers = DEF_KEEPERS;
    while( (ch = getopt( argc, argv, "NSVd:f:i:hk:m:npstv" )) != EOF )
    {
        switch( ch )
        {
        case 'N':
            mode = mode_s_to_n;
            break;
        case 'S':
            mode = mode_n_to_s;
            break;
        case 'V':
            version();
            break;  /* notreached */
        case 'd':
            free( dbname );
            dbname = strdup( optarg );
            break;
        case 'f':
            if( strcasecmp( optarg, "text" ) == 0 )
            {
                dbfmt = db_text;
            }
            else if( strcasecmp( optarg, "db" ) == 0 )
            {
                dbfmt = db_db;
            }
            else if( strcasecmp( optarg, "mysql" ) == 0 )
            {
                dbfmt = db_mysql;
            }
            else
            {
                usage();
            }
            break;
        case 'h':
            usage();
            break;  /* notreached */
        case 'i':
            free( infile );
            infile = strdup( optarg );
            break;
        case 'k':
            stats.keepers = atoi( optarg );
            break;
        case 'm':
            if( strcasecmp( optarg, "mbox" ) == 0 )
            {
                mboxtype = mbox;
            }
            else if( strcasecmp( optarg, "maildir" ) == 0 )
            {
                mboxtype = maildir;
            }
            else
            {
                usage();
            }
            break;
        case 'n':
            mode = mode_reg_n;
            break;
        case 'p':
            do_passthru = true;
            break;
        case 's':
            mode = mode_reg_s;
            break;
        case 't':
            mode = mode_test;
            break;
        case 'v':
            g_verbose++;
            verbose( 1, "Verbose level now %u\n", g_verbose );
            break;
        default:
            usage();
        }
    }
    stats.extrema = (discrim_t*)malloc( stats.keepers*sizeof(discrim_t) );

    if( infile != NULL )
    {
        fd = open( infile, O_RDONLY );
        if( fd == -1 )
        {
            fprintf( stderr, "%s: cannot open input file '%s': %s\n",
                     argv[0], infile, strerror(errno) );
            exit( 2 );
        }
    }

    pdb = dbh_open( dbfmt, "localhost", dbname, DB_USER, DB_PASS );
    if( pdb == NULL )
    {
        fprintf( stderr, "%s: cannot open database\n", argv[0] );
        exit( 2 );
    }

    lex_create( &lex, mboxtype );
    if( !lex_load( &lex, fd ) )
    {
        fprintf( stderr, "%s: cannot read input\n", argv[0] );
        exit( 2 );
    }
    lex_nexttoken( &lex, &tok );
    if( tok.tt == eof )
    {
        fprintf( stderr, "%s: no input available\n", argv[0] );
        exit( 2 );
    }

    while( tok.tt != eof )
    {
        if( mboxtype == mbox && tok.tt != from )
        {
            fprintf( stderr, "%s: input does not look like an mbox message\n", argv[0] );
            exit( 2 );
        }

        rdonly = (mode == mode_test || mode == mode_reg_n);
        pblist = pdb->opentable( pdb, "spamlist", rdonly );
        if( pblist == NULL )
        {
            fprintf( stderr, "%s: cannot open spamlist\n", argv[0] );
            exit( 2 );
        }

        rdonly = (mode == mode_test || mode == mode_reg_s);
        pglist = pdb->opentable( pdb, "goodlist", rdonly );
        if( pglist == NULL )
        {
            fprintf( stderr, "%s: cannot open goodlist\n", argv[0] );
            exit( 2 );
        }

        vec_create( &mlist );
        bvec_loadmsg( &mlist, &lex, &tok );

        switch( mode )
        {
        case mode_test:
            bayesfilt( pglist, pblist, &mlist, &stats );
            is_spam = (stats.spamicity > SPAM_CUTOFF);
            break;
        case mode_normal:
            bayesfilt( pglist, pblist, &mlist, &stats );
            is_spam = (stats.spamicity > SPAM_CUTOFF);
            ptable = (is_spam ? pblist : pglist);
            svec_sort( &mlist );
            if( !ptable->mergeclose( ptable, &mlist ) )
            {
                fprintf( stderr, "%s: cannot merge/save list\n", argv[0] );
                exit( 2 );
            }
            break;
        case mode_reg_s:
            stats.spamicity = 1.0;
            is_spam = true;
            svec_sort( &mlist );
            if( !pblist->mergeclose( pblist, &mlist ) )
            {
                fprintf( stderr, "%s: cannot merge/save list\n", argv[0] );
                exit( 2 );
            }
            break;
        case mode_reg_n:
            stats.spamicity = 0.0;
            is_spam = false;
            svec_sort( &mlist );
            if( !pglist->mergeclose( pglist, &mlist ) )
            {
                fprintf( stderr, "%s: cannot merge/save list\n", argv[0] );
                exit( 2 );
            }
            break;
        case mode_n_to_s:
            stats.spamicity = 1.0;
            is_spam = true;
            svec_sort( &mlist );
            if( !pblist->mergeclose( pblist, &mlist ) ||
                !pglist->unmergeclose( pglist, &mlist ) )
            {
                fprintf( stderr, "%s: cannot merge/save list\n", argv[0] );
                exit( 2 );
            }
            break;
        case mode_s_to_n:
            stats.spamicity = 0.0;
            is_spam = false;
            svec_sort( &mlist );
            if( !pblist->unmergeclose( pblist, &mlist ) ||
                !pglist->mergeclose( pglist, &mlist ) )
            {
                fprintf( stderr, "%s: cannot merge/save list\n", argv[0] );
                exit( 2 );
            }
            break;
        default:
            usage();
        }

        if( mode == mode_test )
        {
            statdump( &stats, STDOUT_FILENO );
        }

        if( do_passthru )
        {
            lex_passthru( &lex, is_spam, stats.spamicity );
        }

        vec_destroy( &mlist );

        pglist->close( pglist );
        free( pglist );
        pblist->close( pblist );
        free( pblist );
    }

    lex_destroy( &lex );

    pdb->close( pdb );
    free( pdb );

    if( infile != NULL )
    {
        free( infile );
        close( fd );
    }
    free( stats.extrema );

    return ( (do_passthru || is_spam) ? 0 : 1 );
}