chiark / gitweb /
Merge branches 'idx/verh' and 'idx/qmqpc'
[qmail] / headerbody.c
1 #include "stralloc.h"
2 #include "substdio.h"
3 #include "getln.h"
4 #include "hfield.h"
5 #include "headerbody.h"
6
7 static int getsa(ss,sa,match)
8 substdio *ss;
9 stralloc *sa;
10 int *match;
11 {
12  if (!*match) return 0;
13  if (getln(ss,sa,match,'\n') == -1) return -1;
14  if (*match) return 1;
15  if (!sa->len) return 0;
16  if (!stralloc_append(sa,"\n")) return -1;
17  return 1;
18 }
19
20 static stralloc line = {0};
21 static stralloc nextline = {0};
22
23 int headerbody(ss,dohf,hdone,dobl)
24 substdio *ss;
25 void (*dohf)();
26 void (*hdone)();
27 void (*dobl)();
28 {
29  int match;
30  int flaglineok;
31  match = 1;
32  flaglineok = 0;
33  for (;;)
34   {
35    switch(getsa(ss,&nextline,&match))
36     {
37      case -1:
38        return -1;
39      case 0:
40        if (flaglineok) dohf(&line);
41        hdone();
42        /* no message body; could insert blank line here */
43        return 0;
44     }
45    if (flaglineok)
46     {
47      if ((nextline.s[0] == ' ') || (nextline.s[0] == '\t'))
48       {
49        if (!stralloc_cat(&line,&nextline)) return -1;
50        continue;
51       }
52      dohf(&line);
53     }
54    if (nextline.len == 1)
55     {
56      hdone();
57      dobl(&nextline);
58      break;
59     }
60    if (stralloc_starts(&nextline,"From "))
61     {
62      if (!stralloc_copys(&line,"MBOX-Line: ")) return -1;
63      if (!stralloc_cat(&line,&nextline)) return -1;
64     }
65    else
66      if (hfield_valid(nextline.s,nextline.len))
67       {
68        if (!stralloc_copy(&line,&nextline)) return -1;
69       }
70      else
71       {
72        hdone();
73        if (!stralloc_copys(&line,"\n")) return -1;
74        dobl(&line);
75        dobl(&nextline);
76        break;
77       }
78    flaglineok = 1;
79   }
80  for (;;)
81    switch(getsa(ss,&nextline,&match))
82     {
83      case -1: return -1;
84      case 0: return 0;
85      case 1: dobl(&nextline);
86     }
87 }