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