nightmaremail

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

headerbody.c (1701B)


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