Import ezmlm-idx 0.40
[ezmlm] / qmail.c
1 #include "substdio.h"
2 #include "readwrite.h"
3 #include "wait.h"
4 #include "exit.h"
5 #include "fork.h"
6 #include "fd.h"
7 #include "qmail.h"
8 #include "auto_qmail.h"
9 #include "alloc.h"
10 #include "stralloc.h"
11 #include "idx.h"
12
13 static char *binqqargs[2] = { PROG_QMAIL_QUEUE, 0 } ;
14
15 int qmail_open(qq,sa)
16 struct qmail *qq;
17 stralloc *sa;
18 {
19 int pim[2];
20 int pie[2];
21 unsigned i,j;
22 char **cpp;
23
24 qq->msgbytes = 0L;
25 if (pipe(pim) == -1) return -1;
26 if (pipe(pie) == -1) { close(pim[0]); close(pim[1]); return -1; }
27
28 switch(qq->pid = vfork()) {
29 case -1:
30 close(pim[0]); close(pim[1]);
31 close(pie[0]); close(pie[1]);
32 return -1;
33 case 0:
34 close(pim[1]);
35 close(pie[1]);
36 if (fd_move(0,pim[0]) == -1) _exit(120);
37 if (fd_move(1,pie[0]) == -1) _exit(120);
38 if (chdir(auto_qmail) == -1) _exit(61);
39 j = 2; /* empty sa - qmqpc c control args */
40 if (sa) { /* count args */
41 for (i = 0; i + 1 < sa->len; i++) {
42 if (sa->s[i] == '\0') j++;
43 } /* make space */
44 if (!(cpp = (char **) alloc(j * sizeof (char *)))) _exit(51);
45 cpp[0] = PROG_QMAIL_QMQPC;
46 cpp[j - 1] = (char *) 0;
47 if (sa->len) cpp[1] = sa->s;
48 j = 2;
49 for (i = 0; i + 1 < sa->len; i++) {
50 if (sa->s[i] == '\0')
51 cpp[j++] = sa->s + i + 1; /* build args */
52 }
53 execv(*cpp,cpp);
54 } else
55 execv(*binqqargs,binqqargs);
56 _exit(120);
57 }
58
59 qq->fdm = pim[1]; close(pim[0]);
60 qq->fde = pie[1]; close(pie[0]);
61 substdio_fdbuf(&qq->ss,write,qq->fdm,qq->buf,sizeof(qq->buf));
62 qq->flagerr = 0;
63 return 0;
64 }
65
66 unsigned long qmail_qp(qq) struct qmail *qq;
67 {
68 return qq->pid;
69 }
70
71 void qmail_fail(qq) struct qmail *qq;
72 {
73 qq->flagerr = 1;
74 }
75
76 void qmail_put(qq,s,len) struct qmail *qq; char *s; int len;
77 {
78 if (!qq->flagerr) if (substdio_put(&qq->ss,s,len) == -1) qq->flagerr = 1;
79 qq->msgbytes += len;
80 }
81
82 void qmail_puts(qq,s) struct qmail *qq; char *s;
83 {
84 register int len;
85 if (!qq->flagerr) {
86 len = str_len(s);
87 if (substdio_put(&qq->ss,s,len) == -1) qq->flagerr = 1;
88 }
89 qq->msgbytes += len;
90 }
91
92 void qmail_from(qq,s) struct qmail *qq; char *s;
93 {
94 if (substdio_flush(&qq->ss) == -1) qq->flagerr = 1;
95 close(qq->fdm);
96 substdio_fdbuf(&qq->ss,write,qq->fde,qq->buf,sizeof(qq->buf));
97 qmail_put(qq,"F",1);
98 qmail_puts(qq,s);
99 qmail_put(qq,"",1);
100 }
101
102 void qmail_to(qq,s) struct qmail *qq; char *s;
103 {
104 qmail_put(qq,"T",1);
105 qmail_puts(qq,s);
106 qmail_put(qq,"",1);
107 }
108
109 char *qmail_close(qq)
110 struct qmail *qq;
111 {
112 int wstat;
113 int exitcode;
114
115 qmail_put(qq,"",1);
116 if (!qq->flagerr) if (substdio_flush(&qq->ss) == -1) qq->flagerr = 1;
117 close(qq->fde);
118
119 if (wait_pid(&wstat,qq->pid) != qq->pid)
120 return "Zqq waitpid surprise (#4.3.0)";
121 if (wait_crashed(wstat))
122 return "Zqq crashed (#4.3.0)";
123 exitcode = wait_exitcode(wstat);
124
125 switch(exitcode) {
126 case 115: /* compatibility */
127 case 11: return "Denvelope address too long for qq (#5.1.3)";
128 case 31: return "Dmail server permanently rejected message (#5.3.0)";
129 case 51: return "Zqq out of memory (#4.3.0)";
130 case 52: return "Zqq timeout (#4.3.0)";
131 case 53: return "Zqq write error or disk full (#4.3.0)";
132 case 0: if (!qq->flagerr) return ""; /* fall through */
133 case 54: return "Zqq read error (#4.3.0)";
134 case 55: return "Zqq unable to read configuration (#4.3.0)";
135 case 56: return "Zqq trouble making network connection (#4.3.0)";
136 case 61: return "Zqq trouble in home directory (#4.3.0)";
137 case 63:
138 case 64:
139 case 65:
140 case 66:
141 case 62: return "Zqq trouble creating files in queue (#4.3.0)";
142 case 71: return "Zmail server temporarily rejected message (#4.3.0)";
143 case 72: return "Zconnection to mail server timed out (#4.4.1)";
144 case 73: return "Zconnection to mail server rejected (#4.4.1)";
145 case 74: return "Zcommunication with mail server failed (#4.4.2)";
146 case 91: /* fall through */
147 case 81: return "Zqq internal bug (#4.3.0)";
148 case 120: return "Zunable to exec qq (#4.3.0)";
149 default:
150 if ((exitcode >= 11) && (exitcode <= 40))
151 return "Dqq permanent problem (#5.3.0)";
152 return "Zqq temporary problem (#4.3.0)";
153 }
154 }