~ubuntu-branches/ubuntu/precise/dbacl/precise

« back to all changes in this revision

Viewing changes to src/hmine.c

  • Committer: Bazaar Package Importer
  • Author(s): Clint Adams
  • Date: 2005-05-07 12:59:53 UTC
  • Revision ID: james.westby@ubuntu.com-20050507125953-xzy2bwkb2qamglwm
Tags: upstream-1.9
ImportĀ upstreamĀ versionĀ 1.9

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* 
 
2
 * Copyright (C) 2002 Laird Breyer
 
3
 *  
 
4
 * This program is free software; you can redistribute it and/or modify
 
5
 * it under the terms of the GNU General Public License as published by
 
6
 * the Free Software Foundation; either version 2 of the License, or
 
7
 * (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; if not, write to the Free Software
 
16
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
17
 * 
 
18
 * Author:   Laird Breyer <laird@lbreyer.com>
 
19
 */
 
20
#ifdef HAVE_CONFIG_H
 
21
#include "config.h"
 
22
#endif
 
23
 
 
24
#include <locale.h>
 
25
 
 
26
#if defined HAVE_LANGINFO_H
 
27
#include <langinfo.h>
 
28
#if !defined CODESET
 
29
/* on OpenBSD, CODESET doesn't seem to be defined - 
 
30
   we use 3, which should be US-ASCII, but it's not ideal... */
 
31
#define CODESET 3
 
32
#endif
 
33
#endif
 
34
 
 
35
#include <stdlib.h>
 
36
#include <stdio.h>
 
37
#include <string.h>
 
38
#include <ctype.h>
 
39
#include <sys/stat.h>
 
40
#include <fcntl.h>
 
41
 
 
42
#if defined HAVE_UNISTD_H
 
43
#include <unistd.h> 
 
44
#endif
 
45
 
 
46
#include "dbacl.h"
 
47
#include "util.h"
 
48
#include "hmine.h"
 
49
 
 
50
extern HEADER_State head;
 
51
 
 
52
extern char *textbuf;
 
53
extern charbuf_len_t textbuf_len;
 
54
 
 
55
extern options_t u_options;
 
56
 
 
57
extern char *progname;
 
58
extern char *inputfile;
 
59
extern long inputline;
 
60
 
 
61
extern int cmd;
 
62
extern long system_pagesize;
 
63
 
 
64
int exit_code = 0; /* default */
 
65
 
 
66
/***********************************************************
 
67
 * FUNNY FUNCTIONS                                         *
 
68
 ***********************************************************/
 
69
void print_summary() {
 
70
/*   int i; */
 
71
/*   time_t numsec = (time_t)-1; */
 
72
/*   for(i = 0; i < head.hstack.top; i++) { */
 
73
/*     if( head.hstack.hlines[i].tag == hltRCV ) { */
 
74
/*       if( head.hstack.hlines[i].state & (1<<H_STATE_BAD_DATA) ) { */
 
75
/*      break; */
 
76
/*       } else { */
 
77
/*      if( numsec == (time_t)-1 ) {  */
 
78
/*        numsec = head.hstack.hlines[i].data.rcv.numsec; */
 
79
/*      } */
 
80
/*      printf("%ld ", head.hstack.hlines[i].data.rcv.numsec - numsec);  */
 
81
/*      numsec = head.hstack.hlines[i].data.rcv.numsec; */
 
82
/*       } */
 
83
/*     } */
 
84
/*   } */
 
85
/*   printf("\n"); */
 
86
 
 
87
  printf("I have nothing to say.\n");
 
88
 
 
89
}
 
90
 
 
91
/***********************************************************
 
92
 * MAIN FUNCTIONS                                          *
 
93
 ***********************************************************/
 
94
 
 
95
static void usage(char **argv) {
 
96
  fprintf(stderr, 
 
97
          "\n");
 
98
  fprintf(stderr, 
 
99
          "hmine [-vD] [FILE]\n");
 
100
  fprintf(stderr, 
 
101
          "\n");
 
102
  fprintf(stderr, 
 
103
          "      checks FILE or STDIN for RFC822 header forgery.\n");
 
104
  fprintf(stderr, 
 
105
          "\n");
 
106
  fprintf(stderr, 
 
107
          "hmine -V\n");
 
108
  fprintf(stderr, 
 
109
          "\n");
 
110
  fprintf(stderr, 
 
111
          "      prints program version.\n");
 
112
}
 
113
 
 
114
int hset_option(int op, char *optarg) {
 
115
  int c = 0;
 
116
  switch(op) {
 
117
  case 'D':
 
118
    u_options |= (1<<U_OPTION_DEBUG);
 
119
    break;
 
120
  case 'V':
 
121
    fprintf(stdout, "hmine version %s\n", VERSION);
 
122
    fprintf(stdout, COPYBLURB, "hmine");
 
123
    exit(1);
 
124
    break;
 
125
  default:
 
126
    c--;
 
127
    break;
 
128
  }
 
129
  return c;
 
130
}
 
131
 
 
132
 
 
133
void hprocess_file(FILE *input, HEADER_State *head) {
 
134
  char *pptextbuf = NULL;
 
135
  bool_t in_header = 1;
 
136
  int extra_lines = 0;
 
137
  hline_t *h = NULL;
 
138
 
 
139
  set_iobuf_mode(input);
 
140
  inputline = 0;
 
141
 
 
142
  /* now start processing */
 
143
  while( fill_textbuf(input, &extra_lines) ) {
 
144
    inputline++;
 
145
 
 
146
    if( u_options & (1<<U_OPTION_FILTER) ) {
 
147
        fprintf(stdout, "%s", textbuf);
 
148
    }
 
149
 
 
150
    if( in_header ) {
 
151
      if( (textbuf[0] == '\0') || 
 
152
          (textbuf[0] == '\n') ||
 
153
          ((textbuf[0] == '\r') && (textbuf[1] == '\n')) ) {
 
154
        in_header = 0;
 
155
      } else {
 
156
        if( pptextbuf && !isblank(*textbuf) ) {
 
157
          h = head_push_header(head, pptextbuf);
 
158
        }
 
159
        pptextbuf = head_append_hline(head, textbuf);
 
160
      }
 
161
    } else {
 
162
      /* body of email */
 
163
    }
 
164
 
 
165
 
 
166
  }
 
167
}
 
168
 
 
169
void init_header_handling() {
 
170
  init_buffers();
 
171
  init_head_filter(&head);
 
172
}
 
173
 
 
174
void cleanup_header_handling() {
 
175
  free_head_filter(&head);
 
176
  cleanup_buffers();
 
177
}
 
178
 
 
179
int main(int argc, char **argv) {
 
180
 
 
181
  FILE *input;
 
182
  signed char op;
 
183
 
 
184
  void (*preprocess_fun)(void) = NULL;
 
185
  void (*postprocess_fun)(void) = print_summary;
 
186
 
 
187
  progname = "hmine";
 
188
  inputfile = "stdin";
 
189
  inputline = 0;
 
190
 
 
191
  /* set up internationalization */
 
192
  if( !setlocale(LC_ALL, "") ) {
 
193
    errormsg(E_WARNING,
 
194
            "could not set locale, internationalization disabled\n");
 
195
  } else {
 
196
    if( u_options & (1<<U_OPTION_VERBOSE) ) {
 
197
      errormsg(E_WARNING,
 
198
              "international locales not supported\n");
 
199
    }
 
200
  }
 
201
 
 
202
#if defined(HAVE_GETPAGESIZE)
 
203
  system_pagesize = getpagesize();
 
204
#endif
 
205
  if( system_pagesize == -1 ) { system_pagesize = BUFSIZ; }
 
206
 
 
207
  init_signal_handling();
 
208
 
 
209
  /* parse the options */
 
210
  while( (op = getopt(argc, argv, 
 
211
                      "vVD")) > -1 ) {
 
212
    hset_option(op, optarg);
 
213
  }
 
214
 
 
215
 
 
216
  /* set up callbacks */
 
217
 
 
218
 
 
219
  if( preprocess_fun ) { (*preprocess_fun)(); }
 
220
 
 
221
 
 
222
  init_header_handling();
 
223
 
 
224
  /* now process only the first file on the command line,
 
225
     or if none provided read stdin */
 
226
  if( (optind > -1) && *(argv + optind) ) {
 
227
    /* if it's a filename, process it */
 
228
    input = fopen(argv[optind], "r");
 
229
    if( input ) {
 
230
      inputfile = argv[optind];
 
231
 
 
232
      u_options |= (1<<U_OPTION_STDIN);
 
233
 
 
234
      if( (u_options & (1<<U_OPTION_VERBOSE)) && 
 
235
          !(u_options & (1<<U_OPTION_CLASSIFY))) {
 
236
        fprintf(stdout, "processing file %s\n", argv[optind]);
 
237
      }
 
238
 
 
239
      /* set some initial options */
 
240
      hprocess_file(input, &head);
 
241
 
 
242
      fclose(input);
 
243
 
 
244
    } else { /* unrecognized file name */
 
245
 
 
246
      errormsg(E_ERROR, "couldn't open %s\n", argv[optind]);
 
247
      usage(argv);
 
248
      return 0;
 
249
 
 
250
    }
 
251
 
 
252
  }
 
253
  /* in case no files were specified, get input from stdin */
 
254
  if( !(u_options & (1<<U_OPTION_STDIN)) ) {
 
255
 
 
256
    if( (u_options & (1<<U_OPTION_VERBOSE)) && 
 
257
        !(u_options & (1<<U_OPTION_CLASSIFY)) ) {
 
258
      fprintf(stdout, "taking input from stdin\n");
 
259
    }
 
260
 
 
261
    hprocess_file(stdin, &head);
 
262
 
 
263
    /* must close before freeing in_iobuf, in case setvbuf was called */
 
264
    fclose(stdin); 
 
265
 
 
266
  }
 
267
  
 
268
  if( postprocess_fun ) { (*postprocess_fun)(); }
 
269
 
 
270
  cleanup_header_handling();
 
271
 
 
272
  cleanup_signal_handling();
 
273
 
 
274
  return exit_code;
 
275
}