s/qmail 4.2.29a
Next generation secure email transport
Loading...
Searching...
No Matches
qmail-qread.c
Go to the documentation of this file.
1#include <unistd.h>
2#include <sys/types.h>
3#include <sys/stat.h>
4#include "stralloc.h"
5#include "fmt.h"
6#include "str.h"
7#include "getln.h"
8#include "fmtqfn.h"
9#include "readsubdir.h"
10#include "auto_qmail.h"
11#include "open.h"
12#include "datetime.h"
13#include "date822fmt.h"
14#include "error.h"
15#include "exit.h"
16
18
19void die(int n) { buffer_flush(buffer_1); _exit(n); }
20
21void warn(char *s1,char *s2)
22{
23 char *x;
24 x = error_str(errno);
25 buffer_puts(buffer_1,s1);
26 buffer_puts(buffer_1,s2);
27 buffer_puts(buffer_1,": ");
28 buffer_puts(buffer_1,x);
29 buffer_puts(buffer_1,"\n");
30}
31
32void die_nomem() { buffer_puts(buffer_1,"fatal: out of memory\n"); die(111); }
33void die_chdir() { warn("fatal: unable to chdir",""); die(110); }
34void die_opendir(fn) char *fn; { warn("fatal: unable to opendir ",fn); die(110); }
35
36void err(unsigned long id)
37{
38 char foo[FMT_ULONG];
39 foo[fmt_ulong(foo,id)] = 0;
40 warn("warning: trouble with #",foo);
41}
42
48
49char inbuf[1024];
50stralloc sender = {0};
51
52unsigned long id;
55unsigned long size;
56
57unsigned int fmtstats(char *s)
58{
59 struct datetime dt;
60 unsigned int len;
61 unsigned int i;
62
63 len = 0;
65 i = date822fmt(s,&dt) - 7/*XXX*/; len += i; if (s) s += i;
66 i = fmt_str(s," GMT #"); len += i; if (s) s += i;
67 i = fmt_ulong(s,id); len += i; if (s) s += i;
68 i = fmt_str(s," "); len += i; if (s) s += i;
69 i = fmt_ulong(s,size); len += i; if (s) s += i;
70 i = fmt_str(s," <"); len += i; if (s) s += i;
71 i = fmt_str(s,sender.s + 1); len += i; if (s) s += i;
72 i = fmt_str(s,"> "); len += i; if (s) s += i;
73 if (flagbounce) {
74 i = fmt_str(s," bouncing"); len += i; if (s) s += i;
75 }
76
77 return len;
78}
79
80stralloc stats = {0};
81
82void out(char *s,unsigned int n)
83{
84 while (n > 0) {
85 buffer_put(buffer_1,((*s >= 32) && (*s <= 126)) ? s : "_",1);
86 --n;
87 ++s;
88 }
89}
90void outs(char *s) { out(s,str_len(s)); }
91void outok(char *s) { buffer_puts(buffer_1,s); }
92
94{
95 if (!stralloc_ready(&stats,fmtstats(FMT_LEN))) die_nomem();
96 stats.len = fmtstats(stats.s);
97 out(stats.s,stats.len);
98 outok("\n");
99}
100
101stralloc line = {0};
102
103int main()
104{
105 int channel;
106 int match;
107 struct stat st;
108 int fd;
109 buffer b;
110 int x;
111
112 if (chdir(auto_qmail) == -1) die_chdir();
113 if (chdir("queue") == -1) die_chdir();
115
116 while ((x = readsubdir_next(&rs,&id)))
117 if (x > 0) {
118 fmtqfn(fnmess,"mess/",id,1);
119 fmtqfn(fninfo,"info/",id,1);
120 fmtqfn(fnlocal,"local/",id,1);
121 fmtqfn(fnremote,"remote/",id,1);
122 fmtqfn(fnbounce,"bounce/",id,0);
123
124 if (stat(fnmess,&st) == -1) { err(id); continue; }
125 size = st.st_size;
126 flagbounce = !stat(fnbounce,&st);
127
128 fd = open_read(fninfo);
129 if (fd == -1) { err(id); continue; }
130 buffer_init(&b,read,fd,inbuf,sizeof(inbuf));
131 if (getln(&b,&sender,&match,0) == -1) die_nomem();
132 if (fstat(fd,&st) == -1) { close(fd); err(id); continue; }
133 close(fd);
134 qtime = st.st_mtime;
135
136 putstats();
137
138 for (channel = 0; channel < 2; ++channel) {
139 fd = open_read(channel ? fnremote : fnlocal);
140 if (fd == -1) {
141 if (errno != ENOENT) err(id);
142 } else {
143 for (;;) {
144 if (getln(&b,&line,&match,0) == -1) die_nomem();
145 if (!match) break;
146 switch (line.s[0]) {
147 case 'D':
148 outok(" done");
149 case 'T':
150 outok(channel ? "\tremote\t" : "\tlocal\t");
151 outs(line.s + 1);
152 outok("\n");
153 break;
154 }
155 }
156 close(fd);
157 }
158 }
159 }
160
161 die(0);
162}
buffer b
Definition: auto-gid.c:10
char auto_qmail[]
unsigned int date822fmt(char *s, struct datetime *dt)
Definition: date822fmt.c:9
long datetime_sec
Definition: datetime.h:15
void datetime_tai()
stralloc out
Definition: dnscname.c:12
void _exit()
unsigned int fmtqfn(char *s, char *dirslash, unsigned long id, int flagsplit)
Definition: fmtqfn.c:5
#define FMTQFN
Definition: fmtqfn.h:6
int match
Definition: matchup.c:195
int fd
stralloc foo
Definition: qmail-local.c:74
void die()
Definition: qmail-pop3d.c:24
stralloc fn
Definition: qmail-qmaint.c:483
char fnlocal[FMTQFN]
Definition: qmail-qread.c:45
unsigned long size
Definition: qmail-qread.c:55
datetime_sec qtime
Definition: qmail-qread.c:53
void die_nomem()
Definition: qmail-qread.c:32
void die_opendir(char *fn)
Definition: qmail-qread.c:34
unsigned int fmtstats(char *s)
Definition: qmail-qread.c:57
char inbuf[1024]
Definition: qmail-qread.c:49
char fninfo[FMTQFN]
Definition: qmail-qread.c:44
int flagbounce
Definition: qmail-qread.c:54
stralloc sender
Definition: qmail-qread.c:50
char fnbounce[FMTQFN]
Definition: qmail-qread.c:47
void putstats()
Definition: qmail-qread.c:93
char fnremote[FMTQFN]
Definition: qmail-qread.c:46
void err(unsigned long id)
Definition: qmail-qread.c:36
void warn(char *s1, char *s2)
Definition: qmail-qread.c:21
stralloc line
Definition: qmail-qread.c:101
void outok(char *s)
Definition: qmail-qread.c:91
void die_chdir()
Definition: qmail-qread.c:33
stralloc stats
Definition: qmail-qread.c:80
readsubdir rs
Definition: qmail-qread.c:17
char fnmess[FMTQFN]
Definition: qmail-qread.c:43
int main()
Definition: qmail-qread.c:103
unsigned long id
Definition: qmail-qread.c:52
void outs(char *s)
Definition: qmail-qread.c:90
struct datetime dt
Definition: qmail-queue.c:33
int readsubdir_next()
void readsubdir_init()