File: maildir2mbox.c

package info (click to toggle)
qmail 1.01-5
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 2,188 kB
  • ctags: 1,711
  • sloc: ansic: 13,993; makefile: 1,914; perl: 448; sh: 377
file content (162 lines) | stat: -rw-r--r-- 4,551 bytes parent folder | download | duplicates (11)
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
#include "readwrite.h"
#include "prioq.h"
#include "env.h"
#include "stralloc.h"
#include "subfd.h"
#include "substdio.h"
#include "getln.h"
#include "error.h"
#include "open.h"
#include "lock.h"
#include "gfrom.h"
#include "str.h"
#include "exit.h"
#include "myctime.h"
#include "maildir.h"

char *mbox;
char *mboxtmp;

stralloc filenames = {0};
prioq pq = {0};
prioq pq2 = {0};

stralloc line = {0};

stralloc ufline = {0};

char inbuf[SUBSTDIO_INSIZE];
char outbuf[SUBSTDIO_OUTSIZE];

#define FATAL "maildir2mbox: fatal: "
#define WARNING "maildir2mbox: warning: "

void die_nomem() { strerr_die2x(111,FATAL,"out of memory"); }

void main()
{
 substdio ssin;
 substdio ssout;
 struct prioq_elt pe;
 int fdoldmbox;
 int fdnewmbox;
 int fd;
 int match;
 int fdlock;

 umask(077);

 mbox = env_get("MAIL");
 if (!mbox) strerr_die2x(111,FATAL,"MAIL not set");
 mboxtmp = env_get("MAILTMP");
 if (!mboxtmp) strerr_die2x(111,FATAL,"MAILTMP not set");

 if (maildir_chdir() == -1)
   strerr_die1(111,FATAL,&maildir_chdir_err);
 maildir_clean(&filenames);
 if (maildir_scan(&pq,&filenames,1,1) == -1)
   strerr_die1(111,FATAL,&maildir_scan_err);

 if (!prioq_min(&pq,&pe)) _exit(0); /* nothing new */

 fdlock = open_append(mbox);
 if (fdlock == -1)
   strerr_die4sys(111,FATAL,"unable to lock ",mbox,": ");
 if (lock_ex(fdlock) == -1)
   strerr_die4sys(111,FATAL,"unable to lock ",mbox,": ");

 fdoldmbox = open_read(mbox);
 if (fdoldmbox == -1)
   strerr_die4sys(111,FATAL,"unable to read ",mbox,": ");

 fdnewmbox = open_trunc(mboxtmp);
 if (fdnewmbox == -1)
   strerr_die4sys(111,FATAL,"unable to create ",mboxtmp,": ");

 substdio_fdbuf(&ssin,read,fdoldmbox,inbuf,sizeof(inbuf));
 substdio_fdbuf(&ssout,write,fdnewmbox,outbuf,sizeof(outbuf));

 switch(substdio_copy(&ssout,&ssin))
  {
   case -2: strerr_die4sys(111,FATAL,"unable to read ",mbox,": ");
   case -3: strerr_die4sys(111,FATAL,"unable to write to ",mboxtmp,": ");
  }

 while (prioq_min(&pq,&pe))
  {
   prioq_delmin(&pq);
   if (!prioq_insert(&pq2,&pe)) die_nomem();

   fd = open_read(filenames.s + pe.id);
   if (fd == -1)
     strerr_die4sys(111,FATAL,"unable to read $MAILDIR/",filenames.s + pe.id,": ");
   substdio_fdbuf(&ssin,read,fd,inbuf,sizeof(inbuf));

   if (getln(&ssin,&line,&match,'\n') != 0)
     strerr_die4sys(111,FATAL,"unable to read $MAILDIR/",filenames.s + pe.id,": ");
   
   if (!stralloc_copys(&ufline,"From XXX ")) die_nomem();
   if (match)
     if (stralloc_starts(&line,"Return-Path: <"))
      {
       if (line.s[14] == '>')
	{
         if (!stralloc_copys(&ufline,"From MAILER-DAEMON ")) die_nomem();
	}
       else
	{
	 int i;
         if (!stralloc_ready(&ufline,line.len)) die_nomem();
         if (!stralloc_copys(&ufline,"From ")) die_nomem();
	 for (i = 14;i < line.len - 2;++i)
	   if ((line.s[i] == ' ') || (line.s[i] == '\t'))
	     ufline.s[ufline.len++] = '-';
	   else
	     ufline.s[ufline.len++] = line.s[i];
         if (!stralloc_cats(&ufline," ")) die_nomem();
	}
      }
   if (!stralloc_cats(&ufline,myctime(pe.dt))) die_nomem();
   if (substdio_put(&ssout,ufline.s,ufline.len) == -1)
     strerr_die4sys(111,FATAL,"unable to write to ",mboxtmp,": ");

   while (match && line.len)
    {
     if (gfrom(line.s,line.len))
       if (substdio_puts(&ssout,">") == -1)
         strerr_die4sys(111,FATAL,"unable to write to ",mboxtmp,": ");
     if (substdio_put(&ssout,line.s,line.len) == -1)
       strerr_die4sys(111,FATAL,"unable to write to ",mboxtmp,": ");
     if (!match)
      {
       if (substdio_puts(&ssout,"\n") == -1)
         strerr_die4sys(111,FATAL,"unable to write to ",mboxtmp,": ");
       break;
      }
     if (getln(&ssin,&line,&match,'\n') != 0)
       strerr_die4sys(111,FATAL,"unable to read $MAILDIR/",filenames.s + pe.id,": ");
    }
   if (substdio_puts(&ssout,"\n"))
     strerr_die4sys(111,FATAL,"unable to write to ",mboxtmp,": ");

   close(fd);
  }

 if (substdio_flush(&ssout) == -1)
   strerr_die4sys(111,FATAL,"unable to write to ",mboxtmp,": ");
 if (fsync(fdnewmbox) == -1)
   strerr_die4sys(111,FATAL,"unable to write to ",mboxtmp,": ");
 if (close(fdnewmbox) == -1) /* NFS dorks */
   strerr_die4sys(111,FATAL,"unable to write to ",mboxtmp,": ");
 if (rename(mboxtmp,mbox) == -1)
   strerr_die6(111,FATAL,"unable to move ",mboxtmp," to ",mbox,": ",&strerr_sys);
 
 while (prioq_min(&pq2,&pe))
  {
   prioq_delmin(&pq2);
   if (unlink(filenames.s + pe.id) == -1)
     strerr_warn4(WARNING,"$MAILDIR/",filenames.s + pe.id," will be delivered twice; unable to unlink: ",&strerr_sys);
  }

 _exit(0);
}