11 #include "headerbody.h"
14 #define FATAL "maildirwatch: fatal: "
16 void die_nomem() { strerr_die2x(111,FATAL,"out of memory"); }
18 stralloc recipient = {0};
19 stralloc sender = {0};
20 stralloc fromline = {0};
23 void addtext(s,n) char *s; int n;
25 if (!stralloc_catb(&text,s,n)) die_nomem();
26 if (text.len > 158) text.len = 158;
28 void dobody(h) stralloc *h; { addtext(h->s,h->len); }
29 void doheader(h) stralloc *h;
32 switch(hfield_known(h->s,h->len))
35 i = hfield_skipname(h->s,h->len);
36 addtext(h->s + i,h->len - i);
39 i = hfield_skipname(h->s,h->len);
41 if (!stralloc_copyb(&recipient,h->s + i,h->len - i - 1)) die_nomem();
44 i = hfield_skipname(h->s,h->len);
46 if (!stralloc_copyb(&sender,h->s + i,h->len - i - 1)) die_nomem();
49 if (!stralloc_copyb(&fromline,h->s,h->len - 1)) die_nomem();
53 void finishheader() { ; }
55 stralloc filenames = {0};
58 char inbuf[SUBSTDIO_INSIZE];
67 if (maildir_chdir() == -1)
68 strerr_die1(111,FATAL,&maildir_chdir_err);
72 maildir_clean(&filenames);
73 if (maildir_scan(&pq,&filenames,1,0) == -1)
74 strerr_die1(111,FATAL,&maildir_scan_err);
76 substdio_putsflush(subfdout,"\033[;H\033[;J");
78 while (prioq_min(&pq,&pe))
82 fd = open_read(filenames.s + pe.id);
83 if (fd == -1) continue;
84 substdio_fdbuf(&ssin,read,fd,inbuf,sizeof(inbuf));
86 if (!stralloc_copys(&sender,"?")) die_nomem();
87 if (!stralloc_copys(&recipient,"?")) die_nomem();
88 if (!stralloc_copys(&fromline,"")) die_nomem();
89 if (!stralloc_copys(&text,"")) die_nomem();
90 if (headerbody(&ssin,doheader,finishheader,dobody) == -1)
91 strerr_die2x(111,FATAL,"trouble reading new message");
93 for (i = 0;i < fromline.len;++i)
94 if ((fromline.s[i] < 32) || (fromline.s[i] > 126))
96 for (i = 0;i < sender.len;++i)
97 if ((sender.s[i] < 32) || (sender.s[i] > 126))
99 for (i = 0;i < recipient.len;++i)
100 if ((recipient.s[i] < 32) || (recipient.s[i] > 126))
101 recipient.s[i] = '?';
102 for (i = 0;i < text.len;++i)
103 if ((text.s[i] < 32) || (text.s[i] > 126))
105 substdio_puts(subfdout,"FROM ");
106 substdio_put(subfdout,sender.s,sender.len);
107 substdio_puts(subfdout," TO <");
108 substdio_put(subfdout,recipient.s,recipient.len);
109 substdio_puts(subfdout,">\n");
112 substdio_puts(subfdout,"\033[1m");
113 substdio_put(subfdout,fromline.s,fromline.len);
114 substdio_puts(subfdout,"\033[0m\n");
116 substdio_put(subfdout,text.s,text.len);
117 substdio_puts(subfdout,"\n\n");
122 substdio_flush(subfdout);