~ubuntu-branches/ubuntu/raring/ifile/raring

« back to all changes in this revision

Viewing changes to ifile.c

  • Committer: Bazaar Package Importer
  • Author(s): Jens Peter Secher
  • Date: 2004-11-19 23:30:24 UTC
  • Revision ID: james.westby@ubuntu.com-20041119233024-3s7sqpy963jx22eu
Tags: upstream-1.3.4
ImportĀ upstreamĀ versionĀ 1.3.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ifile - intelligent mail filter for EXMH/MH
 
2
   ifile is Copyright (C) 1997  Jason Rennie <jrennie@ai.mit.edu>
 
3
 
 
4
   This program is free software; you can redistribute it and/or
 
5
   modify it under the terms of the GNU General Public License
 
6
   as published by the Free Software Foundation; either version 2
 
7
   of the License, or (at your option) any later version.
 
8
   
 
9
   This program is distributed in the hope that it will be useful,
 
10
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
   GNU General Public License for more details.
 
13
   
 
14
   You should have received a copy of the GNU General Public License
 
15
   along with this program (see file 'COPYING'); if not, write to the Free
 
16
   Software Foundation, Inc., 59 Temple Place - Suite 330,
 
17
   Boston, MA  02111-1307, USA.
 
18
   */
 
19
  
 
20
#include <sys/types.h>
 
21
#include <sys/ipc.h>
 
22
#include <sys/sem.h>
 
23
 
 
24
#include <locale.h>
 
25
#include <time.h>
 
26
#include <ifile.h>        /* standard ifile library */
 
27
 
 
28
#define SEMKEY  10439838
 
29
 
 
30
int semid;
 
31
struct sembuf sops;
 
32
 
 
33
arguments args;
 
34
extern struct argp argp;
 
35
int msgs_read;        /* number of messages actually read in */
 
36
 
 
37
/* variables for keeping track of time/speed of ifile */
 
38
clock_t DMZ_start, DMZ_end, DMZ2_start;
 
39
 
 
40
/* ifilter specific function prototypes */
 
41
int cmp(const void *e1, const void *e2);
 
42
 
 
43
/* Main program */
 
44
/* written by Jason Rennie <jrennie@ai.mit.edu> */
 
45
int 
 
46
main (int argc, char **argv)
 
47
{
 
48
  char *data_file = NULL;   /* full path of idata file */
 
49
  char *home_dir = NULL;    /* full path of user's home directory */
 
50
  FILE *MSG = NULL;         /* file pointer for a message */
 
51
  category_rating * ratings;
 
52
  ifile_db idata;
 
53
  htable *message = NULL;
 
54
  int i;
 
55
  int db_read_result = 0, db_write_result = 0;
 
56
  char *file_name;
 
57
  int trimmed_words;
 
58
 
 
59
  setlocale(LC_ALL, "" );
 
60
 
 
61
  /* Harry's semaphore stuff to protect two ifile jobs from stepping
 
62
   * on each other */
 
63
  /* Find the Semaphore id */
 
64
  if ((semid = semget(SEMKEY, 1, 0666)) < 0)
 
65
    if ((semid = semget(SEMKEY, 1, 0666|IPC_CREAT|IPC_EXCL)) < 0)
 
66
      {
 
67
        perror("semget");
 
68
        exit (-1);
 
69
      }
 
70
 
 
71
  /* Wait for Semaphore to clear */
 
72
  sops.sem_num = 0;
 
73
  sops.sem_op = 0;
 
74
  sops.sem_flg = 0;
 
75
 
 
76
  if (semop(semid, &sops, 1))
 
77
    {
 
78
      perror("semop");
 
79
      exit (-1);
 
80
    }
 
81
  
 
82
  /* Set the Semaphore to clear on exit */
 
83
  sops.sem_num = 0;
 
84
  sops.sem_op = 1;
 
85
  sops.sem_flg = SEM_UNDO;
 
86
 
 
87
  if (semop(semid, &sops, 1))
 
88
    {
 
89
      perror("semop");
 
90
      exit (-1);
 
91
    }
 
92
  
 
93
  ifile_init_args(&args);
 
94
  argp_parse (&argp, argc, argv, 0, 0, &args);
 
95
  
 
96
  ifile_verbosify(ifile_verbose, "%d file(s) passed\n", args.num_files);
 
97
  for (i=0; i < args.num_files; i++)
 
98
    ifile_verbosify(ifile_verbose, "file #%d: %s\n", i,
 
99
                    EXT_ARRAY_GET(args.file, char *, i));
 
100
 
 
101
  /* Get home directory */
 
102
  home_dir = getenv("HOME");
 
103
  if (home_dir == NULL)
 
104
    ifile_error("Fatal: HOME environment variable not defined!\n");
 
105
  ifile_verbosify(ifile_verbose, "home directory = %s\n", home_dir);
 
106
 
 
107
  /* Get the database file name */
 
108
  if (args.db_file != NULL)
 
109
    data_file = ifile_strdup (args.db_file);
 
110
  else
 
111
    data_file = ifile_sprintf("%s/%s", home_dir, DEFAULT_DB_FILE);
 
112
 
 
113
  /* remove the .idata file if requested */
 
114
  if (args.reset_data)
 
115
    {
 
116
      ifile_verbosify(ifile_progress, "Removing %s...\n", data_file);
 
117
      system(ifile_sprintf("rm %s", data_file));
 
118
    }
 
119
 
 
120
  ifile_db_init(&idata);
 
121
  ifile_open_log(argc, argv);
 
122
  ifile_default_lexer_init();
 
123
 
 
124
  /* argument variables that still need to be handled:
 
125
   * skip_header, minus_folder, plus_folder */
 
126
 
 
127
  /* Read the idata database */
 
128
  if ((args.read_db == TRUE) && (!args.print_tokens))
 
129
    db_read_result = ifile_read_db(data_file, &idata);
 
130
 
 
131
  /* If doing update, print warning & die if folder doesn't exist */
 
132
  if (args.plus_folder && !args.create_folder) {
 
133
    int match = FALSE;
 
134
    int i=0;
 
135
    if (!db_read_result)
 
136
      for (; i < idata.num_folders; ++i)
 
137
        if (strcmp(EXT_ARRAY_GET(idata.folder_name,char*,i), args.plus_folder) == 0)
 
138
          match = TRUE;
 
139
    if (!match)
 
140
      ifile_error("Folder does not exist: %s\n", args.plus_folder);
 
141
  }
 
142
 
 
143
  /* read and lex the message(s) */
 
144
  if (args.read_message != TRUE)
 
145
    exit(0);
 
146
 
 
147
  msgs_read = 0;
 
148
  i = 0;
 
149
  DMZ_start = clock();
 
150
  do {
 
151
    if (args.num_files != 0) {
 
152
      {
 
153
        ifile_verbosify(ifile_verbose, "Reading message %d...\n",i);
 
154
        file_name = EXT_ARRAY_GET(args.file, char *, i);
 
155
        MSG = fopen(file_name, "r");
 
156
        if (MSG == NULL)
 
157
          {
 
158
            ifile_verbosify(ifile_quiet,
 
159
                            "Not able to open %s!  No action taken.\n",
 
160
                            file_name);
 
161
            if( message != NULL )
 
162
              {
 
163
                htable_free(message,free,NULL);
 
164
                message = NULL;
 
165
              }
 
166
          }
 
167
        else
 
168
          {
 
169
            message = ifile_read_message(MSG);
 
170
            if (args.occur == TRUE)
 
171
              ifile_bitify_document(message);
 
172
            if (message != NULL)
 
173
              msgs_read++;
 
174
          }
 
175
        if (MSG && (args.verbosity >= ifile_debug || args.print_tokens))
 
176
          ifile_print_message(message);
 
177
        if (MSG) fclose(MSG);
 
178
      }
 
179
    }
 
180
    else
 
181
      {
 
182
        ifile_verbosify(ifile_quiet, "Reading message from standard input...\n");
 
183
        message = ifile_read_message(stdin);
 
184
        msgs_read++;
 
185
        if (args.verbosity >= ifile_debug || args.print_tokens)
 
186
          ifile_print_message(message);
 
187
      }
 
188
 
 
189
    /* Don't do anything else if we are printing tokens */
 
190
    if (args.print_tokens)
 
191
      continue;
 
192
 
 
193
    /* Do LOOCV queries if requested */
 
194
    if (args.loocv_folder != NULL)
 
195
      {
 
196
        if (db_read_result)
 
197
          ifile_error("Not able to perform LOOCV: not able to open database\n");
 
198
        if (message == NULL)
 
199
          continue;
 
200
 
 
201
        ifile_del_db(args.loocv_folder, message, &idata);
 
202
        ratings = ifile_rate_categories(message, &idata);
 
203
        qsort(ratings, idata.num_folders, sizeof(category_rating), cmp);
 
204
 
 
205
        if (args.concise)
 
206
          {
 
207
            file_name = EXT_ARRAY_GET(args.file, char *, i);
 
208
            ifile_concise_ratings(file_name, stdout, ratings, &idata,
 
209
                                  args.thresh);
 
210
          }
 
211
        else
 
212
          ifile_print_ratings(stdout, ratings, &idata, args.thresh);
 
213
 
 
214
        ifile_free_categories(ratings,&idata);
 
215
        ifile_add_db(args.loocv_folder, message, &idata, args.create_folder);
 
216
      }
 
217
 
 
218
    /* if a query was requested, make the calculations and output the results */
 
219
    if (args.query == TRUE)
 
220
      {
 
221
        if (db_read_result)
 
222
          ifile_error("Not able to perform query: not able to open database\n");
 
223
        if (message != NULL)
 
224
          {
 
225
            ratings = ifile_rate_categories(message, &idata);
 
226
            qsort(ratings, idata.num_folders, sizeof(category_rating), cmp);
 
227
 
 
228
            if (args.concise)
 
229
              {
 
230
                file_name = EXT_ARRAY_GET(args.file, char *, i);
 
231
                ifile_concise_ratings(file_name, stdout, ratings, &idata,
 
232
                                      args.thresh);
 
233
              }
 
234
            else
 
235
              ifile_print_ratings(stdout, ratings, &idata, args.thresh);
 
236
 
 
237
            if (args.query_insert)
 
238
              ifile_add_db(ratings[0].category, message, &idata, args.create_folder);
 
239
            ifile_free_categories(ratings,&idata);
 
240
          }
 
241
      }
 
242
 
 
243
    if (args.write_db == TRUE)
 
244
      {
 
245
        if (args.plus_folder != NULL)
 
246
          if (message != NULL)
 
247
            ifile_add_db(args.plus_folder, message, &idata, args.create_folder);
 
248
 
 
249
        if (args.minus_folder != NULL)
 
250
          if (message != NULL)
 
251
            ifile_del_db(args.minus_folder, message, &idata);
 
252
 
 
253
      }
 
254
 
 
255
    if (message) {
 
256
      htable_free(message, free, NULL);
 
257
      message = NULL;
 
258
    }
 
259
  } while (++i < args.num_files);
 
260
 
 
261
  DMZ_end = clock();
 
262
  ifile_verbosify(ifile_progress,
 
263
                  "Read %d message(s).  Time used: %.3f sec\n", msgs_read,
 
264
                  ((float)(DMZ_end-DMZ_start))/CLOCKS_PER_SECOND);
 
265
 
 
266
  if (args.write_db == TRUE) {
 
267
    if ((args.plus_folder != NULL || args.query_insert == TRUE) &&
 
268
        args.minus_folder == NULL)
 
269
      {
 
270
        trimmed_words = ifile_age_words(&idata, msgs_read);
 
271
        ifile_verbosify(ifile_progress,
 
272
                        "Trimmed %d words due to lack of frequency\n",
 
273
                        trimmed_words);
 
274
      }
 
275
    db_write_result = ifile_write_db(data_file, &idata);
 
276
    if (db_read_result != 0 && db_write_result == 0)
 
277
      {
 
278
        ifile_verbosify(ifile_quiet, "Created new %s file.\n", data_file);
 
279
        /* set proper permissions */
 
280
        system(ifile_sprintf("chmod 0600 %s\n", data_file));
 
281
      } 
 
282
  }
 
283
 
 
284
  ifile_close_log();
 
285
 
 
286
#ifdef DMALLOC  
 
287
  /* if we're debugging, clean up after malloc;  if not, don't bother
 
288
     spending the computrons since we're exiting anyhow. */
 
289
  ifile_stoplist_free();
 
290
  ifile_db_free(&idata);
 
291
#endif
 
292
 
 
293
  return 0;
 
294
}
 
295
 
 
296
 
 
297
/* a comparison function for sorting */
 
298
/* Written by Jason Rennie <jrennie@ai.mit.edu> for ifile */
 
299
int cmp (const void *e1, const void *e2)
 
300
{
 
301
  if (((category_rating *)e1)->rating > (((category_rating *)e2)->rating))
 
302
    return -1;
 
303
  else if (((category_rating *)e1)->rating < (((category_rating *)e2)->rating))
 
304
    return 1;
 
305
  else
 
306
    return 0;
 
307
}
 
308
 
 
309
 
 
310