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

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
/* A simple N-gram lexer. */

/* Copyright (C) 1997 Andrew McCallum

   Written by:  Andrew Kachites McCallum <mccallum@cs.cmu.edu>

   This file is part of the Bag-Of-Words Library, `libbow'.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public License
   as published by the Free Software Foundation, version 2.
   
   This library 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
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA */

#ifdef _AIX
#pragma alloca
#endif

#include <ifile.h>

#if HAVE_ALLOCA_H
#include <alloca.h>
#endif

#define SELF ((ifile_lexer_email*)self)
#define LEX ((ifile_lex_email*)lex)

/* String compare function used to compare headers.  Ignores case distinctions
   1 => strings don't match.  0 => strings do match. */
int
strcmp_ignore_case (char *str1, char *str2)
{
  int pos = 0;

  assert(str1 != NULL);  assert(str2 != NULL);

  while (tolower(str1[pos]) == tolower(str2[pos]))
    {
      if (str1[pos] == '\0') { return 0; }
      pos++;
    }

  return 1;
}


/* Given a lex which is pointing to the first character of a header line,
 * determines whether the header should be kept and extracts the portion
 * which should be kept.  Returns NULL if the header should be tossed.
 * Otherwise, returns a pointer to a newly allocated string which is the
 * portion of the header which should be kept.  Modifies
 * lex->document_position so that it points at the first character of the line
 * immediately following the header. */
/* NOTE: function makes some modifications to lex->document */
/* written by Jason Rennie <jrennie@ai.mit.edu> for ifile */
char *
ifile_lexer_email_parse_header (ifile_lex *lex, ifile_lexer *self)
{
  int keep_header;         /* boolean */
  int start_header_name = lex->document_position;
  int start_header;        /* first character after the colon */
  char byte, next_byte;
  char *ret;               /* header string to return */
  int ret_size;
  int i;

  /*
  char buf[80];
  strncpy(buf, &lex->document[lex->document_position], 79);
  buf[79] = '\0';
  printf("ifile_lexer_email_parse_header (lex=%p, self=%p)\n%s\n",
	 lex, self, buf);
	 */

  /* find the colon, indicating the end of the header name */
  do
    {
      byte = lex->document[lex->document_position++];
    }
  while (((byte >= 33 && byte <= 57) || (byte >= 59 && byte <= 126))
	 && lex->document_position < lex->document_length);

  start_header = lex->document_position;
  keep_header = FALSE;
  /* check to see if we should be keeping this header */
  if (byte == 58)
    {
      lex->document[lex->document_position-1] = '\0';
      ifile_verbosify(ifile_debug, "Checking \"%s\"... ",
		      &lex->document[start_header_name]);
      for (i = 0; !keep_header && SELF->headers_to_keep[i]; i++)
	if (strcmp_ignore_case (SELF->headers_to_keep[i],
				&lex->document[start_header_name]) == 0)
	  {
	    keep_header = TRUE;
	    ifile_verbosify(ifile_debug, "okay.\n");
	  }
    }
  else
    {
      /* this doesn't appear to be a header */
      ifile_verbosify(ifile_debug, "not a header.\n");
      return (char *) ERROR;
    }

  /* search forward until the end of the header is reached */
  next_byte = lex->document[lex->document_position];
  do
    {
      byte = next_byte;
      next_byte = lex->document[++lex->document_position];
    }
  while ((byte != '\n'
	  || next_byte == 32
	  || next_byte == 9)
	 && byte != '\0'
	 && lex->document_position < lex->document_length);

  if (keep_header == FALSE)
    {
      ifile_verbosify(ifile_debug, "tossed.\n");
      return NULL;
    }

  /* make a new string out of the portion of the header we want to keep */
  ret_size = lex->document_position - start_header;
  ret = malloc(sizeof(char) * (ret_size+1));
  strncpy(ret, &lex->document[start_header], ret_size);
  ret[ret_size] = '\0';

  return ret;

}

/* Does a pre-lexing of the headers, removing certain headers and eliminating
 * the tags from others */
/* written by Jason Rennie <jrennie@ai.mit.edu> for ifile */
int
ifile_lexer_email_prelex_header (ifile_lex *lex, ifile_lexer *self)
{
  int begin_unfinished;  /* first character of the unfinished section */
  char *header, *body;
  int body_size;

  /*
  printf("ifile_lexer_email_prelex_header(lex=%p, self=%p)\n", lex, self);
  */

  if (SELF->headers_to_keep == NULL
      || SELF->headers_to_keep == (char **) -1)
    return 0;
  if (lex->document_position != 0)
    return ERROR;

  begin_unfinished = 0;

  /* check each header, removing any unwanted portions from the document */
  while (lex->document_position < lex->document_length
	 && lex->document[lex->document_position] != '\n')
    {
      if ((header = ifile_lexer_email_parse_header (lex, self)))
	{
	  if (header == (char *) ERROR) break;
	  strcpy (&lex->document[begin_unfinished], header);
	  begin_unfinished += strlen (header);
	  free (header);
	}
    }

  body_size = lex->document_length - lex->document_position;
  body = (char *) malloc (sizeof(char) * (body_size + 1));
  strncpy (body, &lex->document[lex->document_position], body_size);
  body[body_size] = '\0';
  strcpy (&lex->document[begin_unfinished], body);
  lex->document_length = begin_unfinished + body_size - 1;

  lex->document_position = 0;
  return 0;
}

ifile_lex *
ifile_lexer_email_open_text_fp (ifile_lexer *self, FILE *fp)
{
  ifile_lex *lex;

  /* to toss all headers, don't begin document until the first blank line */
  if (SELF->headers_to_keep == NULL)
    self->document_start_pattern = "\n\n";
  lex = ifile_lexer_indirect_open_text_fp (self, fp);
  if (lex == NULL)
    return NULL;
  LEX->gram_size_this_time = SELF->gram_size;
  if (ifile_lexer_email_prelex_header (lex, self))
    ifile_verbosify(ifile_progress, "Did not pre-lex e-mail headers properly!\n");
  
  return lex;
}

int
ifile_lexer_email_get_word (ifile_lexer *self, ifile_lex *lex, 
			 char *buf, int buflen)
{
  int i;
  char **tokens;
  int s;
  int len;
  
  tokens = alloca (sizeof (char*) * LEX->gram_size_this_time);
  for (i = 0; i < LEX->gram_size_this_time; i++)
    tokens[i] = alloca (IFILE_MAX_WORD_LENGTH);

  /* Remember where we started. */
  s = LEX->lex.document_position;

  /* Get the first token. */
  if (SELF->indirect_lexer.underlying_lexer->get_word 
      (SELF->indirect_lexer.underlying_lexer, lex,
       tokens[0], IFILE_MAX_WORD_LENGTH)
      == 0)
    return 0;

  /* Get the next n-1 tokens. */
  for (i = 1; i < LEX->gram_size_this_time; i++)
    if (SELF->indirect_lexer.underlying_lexer->get_word
	(SELF->indirect_lexer.underlying_lexer, lex,
	 tokens[i], IFILE_MAX_WORD_LENGTH)
	== 0)
      *(tokens[i]) = '\0';

  /* Make sure it will fit. */
  for (i = 0, len = 0; i < LEX->gram_size_this_time; i++)
    len += strlen (tokens[i]) + 1;
  assert (len < IFILE_MAX_WORD_LENGTH);

  /* Fill buf with the tokens concatenated. */
  strcpy (buf, tokens[0]);
  for (i = 1; i < LEX->gram_size_this_time; i++)
    {
      strcat (buf, ";");
      strcat (buf, tokens[i]);
    }

  /* Put us back to the second token so we can get it with the next call */
  if (LEX->gram_size_this_time > 1)
    LEX->lex.document_position = s;

  if (LEX->gram_size_this_time == 1)
    LEX->gram_size_this_time = SELF->gram_size;
  else
    LEX->gram_size_this_time--;

  return strlen (buf);
}

/* This is declared in lex-simple.c */
extern ifile_lexer_simple _ifile_alpha_lexer;

const ifile_lexer_email _ifile_email_lexer =
{
  {
    {
      sizeof (ifile_lex_email),
      ifile_lexer_email_open_text_fp,
      ifile_lexer_email_get_word,
      ifile_lexer_indirect_close,
      "",			/* document start pattern begins right away */
      NULL			/* document end pattern goes to end */
    },
    (ifile_lexer*)&_ifile_alpha_lexer, /* default UNDERLYING_LEXER */
  },
  (char **) -1,                 /* keep all headers by default */
  1				/* default gram-size is 1 */
};
const ifile_lexer_email *ifile_email_lexer = &_ifile_email_lexer;