File: email.c

package info (click to toggle)
bow 19991122-4
  • links: PTS
  • area: main
  • in suites: woody
  • size: 2,544 kB
  • ctags: 2,987
  • sloc: ansic: 38,660; lisp: 1,072; makefile: 594; perl: 492; yacc: 149; sh: 91
file content (412 lines) | stat: -rw-r--r-- 12,566 bytes parent folder | download | duplicates (4)
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/* Functions for parsing email files. */

/* 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 */

#include <bow/libbow.h>
#include <assert.h>
#include <ctype.h>		/* for tolower() */
#include <string.h>

/* Read characters from the file pointer FP until the string STRING is
   found or EOF if reached.  Return 0 if EOF was reached, 1 otherwise.
   The search is case-insensitive.  If 1 is returned, the file pointer
   will be at the character after the last character in STRING.  If
   ONELINE is non-zero, insist that the string appear before a newline
   character. */
static inline int
_scan_fp_for_string (FILE *fp, const char *string, int oneline)
{
  int byte;			/* character read from the FP */
  const char *string_ptr;	/* a placeholder into STRING */

  if (!string || !string[0])
    return 0;

  /* Read forward until we find the first character of STRING. */
  /* Make an initial newline in STRING match the beginning of the file. */
  if (!(ftell (fp) == 0 && string[0] == '\n'))
    {
    again:
      do
	{
	  byte = fgetc (fp);
	  if (byte == EOF || (string[0] != '\n' && oneline && byte == '\n'))
	    return 0;
	}
      while (tolower (byte) != tolower (string[0]));
    }

  /* Step through the characters in STRING, starting all over again
     if we encounter a mismatch. */
  for (string_ptr = string+1; *string_ptr; string_ptr++)
    {
      byte = fgetc (fp);
      if (byte == EOF || (oneline && byte == '\n'))
	return 0;
      if (tolower (byte) != tolower (*string_ptr))
	/* A mismatch; start the search again. */
	goto again;
    }

  /* Success!  We found the string. */
  return 1;
}

#if 0
/* Read characters from FP into BUF until the string STOPSTR is
   reached.  On success, returns the number of characters read.  If
   EOF is reached before reading the STOPSTR, return the negative of
   the number of characters read.  If BUFLEN is reached before reading
   the STOPCHAR, return 0. */
static inline int
_scan_fp_into_buffer_until_string (FILE *fp, char *buf, int buflen,
				   char* stopstr)
{
  int byte;			/* character read from the FP */
  const char *stopstr_ptr;	/* a placeholder into STOPSTR */
  char *buf_ptr;		/* a placeholder into BUF */
  int count;			/* the number of characters added to BUF */

  if (!stopstr || !stopstr[0])
    return 0;

  count = 0;
  buf_ptr = buf;

  /* xxx The structure of this function is a bit repetative... */

again:
  /* Read forward until we find the first character of STRING. */
  do
    {
      byte = fgetc (fp);
      if (byte == EOF)
	{
	  buf[count] = '\0';
	  return -count;
	}
      *buf_ptr++ = byte;
      if (++count >= buflen)
	{
	  buf[buflen-1] = '\0';
	  return 0;
	}
    }
  while (tolower (byte) != tolower (stopstr[0]));

  /* Step through the characters in STRING, starting all over again
     if we encounter a mismatch. */
  for (stopstr_ptr = stopstr+1; *stopstr_ptr; stopstr_ptr++)
    {
      byte = fgetc (fp);
      if (byte == EOF)
	{
	  buf[count] = '\0';
	  return -count;
	}
      *buf_ptr++ = byte;
      if (++count >= buflen)
	{
	  buf[buflen-1] = '\0';
	  return 0;
	}
      if (tolower (byte) != tolower (*stopstr_ptr))
	/* A mismatch; start the search again. */
	goto again;
    }

  /* Success!  We found the stopstr. */
  count =- strlen (stopstr);
  buf[count] = '\0';
  return count;
}
#endif


/* Read characters from FP into BUF until the character STOPCHAR is
   reached.  On success, returns the number of characters read.  If
   EOF is reached before reading the STOPCHAR, return the negative of
   the number of characters read.  If BUFLEN is reached before reading
   the STOPCHAR, return 0.  If NEGFLAG is 1, the sense of the test is
   reversed. */
static inline int
_scan_fp_into_buffer_until_char (FILE *fp, char *buf, int buflen,
				      char stopchar, int negflag)
{
  int byte;
  int count = 0;

  assert (buflen > 0 && buf);
  while (buflen--)
    {
      byte = fgetc (fp);
      if (byte == EOF)
	{
	  buf[count] = '\0';
	  return -count;
	}
      if (negflag ? (byte != stopchar) : (byte == stopchar))
	{
	  fseek (fp, -1, SEEK_CUR);
	  buf[count] = '\0';
	  return count;
	}
      buf[count++] = byte;
    }
  buf[count-1] = '\0';
  return 0;
}

/* Read characters from FP into BUF until any of the characters in the
   string STOPCHARS is reached.  On success, returns the number of
   characters read.  If EOF is reached before reading any of the
   STOPCHARS, return the negative of the number of characters read.
   If BUFLEN is reached before reading the STOPCHAR, return 0. 
   If NEGFLAG is 1, the sense of the test is reversed. */
static inline int
_scan_fp_into_buffer_until_chars (FILE *fp, char *buf, int buflen,
				  const char *stopchars, int negflag)
{
  int byte;
  int count = 0;

  assert (buflen > 0 && buf);
  while (buflen--)
    {
      byte = fgetc (fp);
      if (byte == EOF)
	{
	  buf[count] = '\0';
	  return -count;
	}
      if (negflag
	  ? (strchr (stopchars, byte) == 0)
	  : (strchr (stopchars, byte) != 0))
	{
	  fseek (fp, -1, SEEK_CUR);
	  buf[count] = '\0';
	  return count;
	}
      buf[count++] = byte;
    }
  buf[count-1] = '\0';
  return 0;
}


int
bow_email_get_headers (FILE *fp, char *buf, int buflen)
{
  return 0;
}

/* Read in BUF the characters inside the `<>' of the `Message-Id:'
   field of the email message contain in the file pointer FP.  Return
   the number of characters placed in BUF.  Signal an error if more
   than BUFLEN characters are necessary.  Return -1 if no matching
   field is found. */
int
bow_email_get_msgid (FILE *fp, char *buf, int buflen)
{
  int len;
  if (!_scan_fp_for_string (fp, "\nMessage-Id: <", 0))
    return -1;
  if ((len = _scan_fp_into_buffer_until_char (fp, buf, buflen, '>', 0)) <= 0)
    bow_error ("%s: No Message-Id: `>' terminator found.",
	       __PRETTY_FUNCTION__);
  buf[len] = '\0';
  return len;
}

/* Read in BUF the characters inside the `<>' of the `References:'
   field of the news message contain in the file pointer FP.  Return
   the number of characters placed in BUF.  Signal an error if more
   than BUFLEN characters are necessary.  Return -1 if no matching
   field is found. */
int
bow_email_get_references (FILE *fp, char *buf, int buflen)
{
  int len;
  if (!_scan_fp_for_string (fp, "\nReferences: <", 0))
    return -1;
  if ((len = _scan_fp_into_buffer_until_char (fp, buf, buflen, '>', 0)) <= 0)
    bow_error ("%s: No Message-Id: `>' terminator found.",
	       __PRETTY_FUNCTION__);
  buf[len] = '\0';
  return len;
}

static inline int
_bow_email_get_email_address (FILE *fp, char *buf, int buflen)
{
  int len;
  int fpos;
  /* Grab the email address of the sender from inside the `<...>' */
  fpos = ftell (fp);
  if ((len = _scan_fp_for_string (fp, "<", 1)))
    {
      if ((len = _scan_fp_into_buffer_until_chars (fp, buf, buflen, "> ", 0))
	  <= 0)
	bow_error ("No email address `>' terminator found.");
      buf[len] = '\0';
      return len;
    }
  /* There wasn't a `<' in the From: header.  Just grab the text up to
     the next white space character. */
  fseek (fp, fpos, SEEK_SET);
  /* Chop off whitespace at beginning. */
  len = _scan_fp_into_buffer_until_chars (fp, buf, buflen, " \n\t", 1);
  len = _scan_fp_into_buffer_until_chars (fp, buf, buflen, " \n\t", 0);
  assert (len > 0);
  buf[len] = '\0';
  return len;
}

/* Read into BUF the characters inside the `<>' of the `From:' field
   of the email message contain in the file pointer FP.  Return the
   number of characters placed in BUF.  Signal an error if more than
   BUFLEN characters are necessary.  Return -1 if no matching field is
   found. */
int
bow_email_get_sender (FILE *fp, char *buf, int buflen)
{
  int fpos;
  fpos = ftell (fp);
  if (!_scan_fp_for_string (fp, "\nFrom: ", 0))
    {
      fseek (fp, fpos, SEEK_SET);
      if (!_scan_fp_for_string (fp, "\nReturn-Path: ", 0))
	return -1;
    }
  return _bow_email_get_email_address (fp, buf, buflen);
}

/* Read into BUF the characters inside the `<>' of the `To:' field
   of the email message contain in the file pointer FP.  Return the
   number of characters placed in BUF.  Signal an error if more than
   BUFLEN characters are necessary.  Return -1 if no matching field is
   found. */
int
bow_email_get_recipient (FILE *fp, char *buf, int buflen)
{
  if (!_scan_fp_for_string (fp, "\nTo: ", 0))
    return -1;
  return _bow_email_get_email_address (fp, buf, buflen);
}


/* Read into BUF the day, month and year of the `Date:' field of the
   email message contain in the file pointer FP.  The format is
   something like `21 Jul 1996'.  Return the number of characters
   placed in BUF.  Signal an error if more than BUFLEN characters are
   necessary.  Return -1 if no matching field is found. */
int
bow_email_get_date (FILE *fp, char *buf, int buflen)
{
  int len;
  int byte;

  if (!_scan_fp_for_string (fp, "\nDate: ", 0))
    return -1;
  /* Scan up until the comma separator. */
  if (!_scan_fp_for_string (fp, ", ", 1))
    return -1;
  /* Scan into BUF the next 7 characters, which should contain
     something like `19 Jun '. */
  assert (buflen >= 7);
  for (len = 0; len < 7; len++)
    {
      byte = fgetc (fp);
      assert (byte != EOF);
      buf[len] = byte;
    }
  /* Scan into buf the year, which may be something like `96' or `1996'. 
     Just keep scanning until whitespace is found. */
  len = _scan_fp_into_buffer_until_chars (fp, buf+7, buflen-7, " \t\n", 0);
  if (len <= 0)
    return 0;
  len += 7;
  buf[len] = '\0';
  return len;
}

/* Read in BUF the characters between the `Received: from ' and the
   following space, and the characters between the ` id ' and the
   following `;' in the file pointer FP.  Return the number of
   characters placed in BUF.  Signal an error if more than BUFLEN
   characters are necessary.  Return -1 if no matching field is
   found. */
int
bow_email_get_receivedid (FILE *fp, char *buf, int buflen)
{
  int len;
  if (!_scan_fp_for_string (fp, "\nReceived: from ", 0))
    return -1;
  if ((len = _scan_fp_into_buffer_until_char (fp, buf, buflen, ' ', 0)) <= 0)
    bow_error ("%s: No `Received: from' ` ' terminator found.",
	       __PRETTY_FUNCTION__);
  if (!_scan_fp_for_string (fp, " id ", 1))
    return -1;
  if ((len += _scan_fp_into_buffer_until_char (fp, buf+len, buflen, ';', 0))
      <= 0)
    bow_error ("%s: No `Received: from id' `;' terminator found.",
	       __PRETTY_FUNCTION__);
  buf[len] = '\0';
  return len;
}


/* Read in BUF the characters inside the `<>' of the `In-Reply-To:'
   field of the email message contain in the file pointer FP.  Return
   the number of characters placed in BUF.  Signal an error if more
   than BUFLEN characters are necessary.  Return -1 if no matching
   field is found. */
int
bow_email_get_replyid (FILE *fp, char *buf, int buflen)
{
  int len;
  if (!_scan_fp_for_string (fp, "\nIn-Reply-To: ", 0))
    return -1;
  if (!_scan_fp_for_string (fp, "<", 0))
    return -1;
  if ((len = _scan_fp_into_buffer_until_char (fp, buf, buflen, '>', 0)) <= 0)
    bow_error ("%s: No Message-Id: `>' terminator found.",
	       __PRETTY_FUNCTION__);
  buf[len] = '\0';
  return len;
}

/* Read in BUF the characters inside the `<>' of the
   `Resent-Message-Id:' field of the email message contain in the file
   pointer FP.  Return the number of characters placed in BUF.  Signal
   an error if more than BUFLEN characters are necessary.  Return -1
   if no matching field is found. */
int
bow_email_get_resent_msgid (FILE *fp, char *buf, int buflen)
{
  int len;
  if (!_scan_fp_for_string (fp, "\nResent-Message-Id: <", 0))
    return -1;
  if ((len = _scan_fp_into_buffer_until_char (fp, buf, buflen, '>', 0)) <= 0)
    bow_error ("%s: No Message-Id: `>' terminator found.",
	       __PRETTY_FUNCTION__);
  buf[len] = '\0';
  return len;
}