s/qmail
4.3.21
Next generation secure email transport
Loading...
Searching...
No Matches
4.3
sqmail-4.3.21
src
maildirwatch.c
Go to the documentation of this file.
1
#include <unistd.h>
2
#include "getln.h"
3
#include "buffer.h"
4
#include "
prioq.h
"
5
#include "stralloc.h"
6
#include "str.h"
7
#include "
exit.h
"
8
#include "
hfield.h
"
9
#include "logmsg.h"
10
#include "open.h"
11
#include "
headerbody.h
"
12
#include "
maildir.h
"
13
14
#define WHO "maildirwatch"
15
16
static
void
die_nomem
() { logmsg(
WHO
,111,FATAL,
"out of memory"
); }
17
18
stralloc
recipient
= {0};
19
stralloc
sender
= {0};
20
stralloc
fromline
= {0};
21
stralloc
text
= {0};
22
23
static
void
addtext(
char
*s,
int
n)
24
{
25
if
(!stralloc_catb(&
text
,s,n))
die_nomem
();
26
if
(
text
.len > 158)
text
.len = 158;
27
}
28
29
static
void
dobody(stralloc *
h
) { addtext(
h
->s,
h
->len); }
30
31
static
void
doheader(stralloc *
h
)
32
{
33
int
i;
34
switch
(
hfield_known
(
h
->s,
h
->len)) {
35
case
H_SUBJECT
:
36
i =
hfield_skipname
(
h
->s,
h
->len);
37
addtext(
h
->s + i,
h
->len - i);
38
break
;
39
case
H_DELIVEREDTO
:
40
i =
hfield_skipname
(
h
->s,
h
->len);
41
if
(i < h->len)
42
if
(!stralloc_copyb(&
recipient
,
h
->s + i,
h
->len - i - 1))
die_nomem
();
43
break
;
44
case
H_RETURNPATH
:
45
i =
hfield_skipname
(
h
->s,
h
->len);
46
if
(i < h->len)
47
if
(!stralloc_copyb(&
sender
,
h
->s + i,
h
->len - i - 1))
die_nomem
();
48
break
;
49
case
H_FROM
:
50
if
(!stralloc_copyb(&
fromline
,
h
->s,
h
->len - 1))
die_nomem
();
51
break
;
52
}
53
}
54
static
void
finishheader
() { ; }
55
56
stralloc
filenames
= {0};
57
prioq
pq
= {0};
58
59
char
inbuf
[BUFFER_INSIZE];
60
buffer
bi
;
61
62
int
main
()
63
{
64
struct
prioq_elt
pe;
65
int
fd
;
66
int
i;
67
68
if
(
maildir_chdir
() == -1)
69
logmsg(
WHO
,111,FATAL,
"Can't change to maildir"
);
70
71
for
(;;) {
72
maildir_clean
(&
filenames
);
73
if
(
maildir_scan
(&
pq
,&
filenames
,1,0) == -1)
74
logmsg(
WHO
,111,FATAL,
"Can't read maildir"
);
75
76
buffer_putsflush(buffer_1,
"\033[;H\033[;J"
);
77
78
while
(
prioq_min
(&
pq
,&pe)) {
79
prioq_delmin
(&
pq
);
80
81
fd
= open_read(
filenames
.s + pe.
id
);
82
if
(
fd
== -1)
continue
;
83
buffer_init(&
bi
,read,
fd
,
inbuf
,
sizeof
(
inbuf
));
84
85
if
(!
stralloc_copys
(&
sender
,
"?"
))
die_nomem
();
86
if
(!
stralloc_copys
(&
recipient
,
"?"
))
die_nomem
();
87
if
(!
stralloc_copys
(&
fromline
,
""
))
die_nomem
();
88
if
(!
stralloc_copys
(&
text
,
""
))
die_nomem
();
89
if
(
headerbody
(&
bi
,doheader,finishheader,dobody) == -1)
90
logmsg(
WHO
,111,FATAL,
"trouble reading new message"
);
91
92
for
(i = 0; i <
fromline
.len; ++i)
93
if
((
fromline
.s[i] < 32) || (
fromline
.s[i] > 126))
94
fromline
.s[i] =
'/'
;
95
for
(i = 0; i <
sender
.len; ++i)
96
if
((
sender
.s[i] < 32) || (
sender
.s[i] > 126))
97
sender
.s[i] =
'?'
;
98
for
(i = 0; i <
recipient
.len; ++i)
99
if
((
recipient
.s[i] < 32) || (
recipient
.s[i] > 126))
100
recipient
.s[i] =
'?'
;
101
for
(i = 0; i <
text
.len; ++i)
102
if
((
text
.s[i] < 32) || (
text
.s[i] > 126))
103
text
.s[i] =
'/'
;
104
buffer_puts(buffer_1,
"FROM "
);
105
buffer_put(buffer_1,
sender
.s,
sender
.len);
106
buffer_puts(buffer_1,
" TO <"
);
107
buffer_put(buffer_1,
recipient
.s,
recipient
.len);
108
buffer_puts(buffer_1,
">\n"
);
109
if
(
fromline
.len) {
110
buffer_puts(buffer_1,
"\033[1m"
);
111
buffer_put(buffer_1,
fromline
.s,
fromline
.len);
112
buffer_puts(buffer_1,
"\033[0m\n"
);
113
}
114
buffer_put(buffer_1,
text
.s,
text
.len);
115
buffer_puts(buffer_1,
"\n\n"
);
116
117
close(
fd
);
118
}
119
120
buffer_flush(buffer_1);
121
sleep(30);
122
}
123
}
inbuf
char inbuf[BUFFER_SMALL]
Definition
auto-gid.c:9
WHO
#define WHO
Definition
bouncesaying.c:8
bi
buffer bi
Definition
condredirect.c:23
stralloc_copys
int stralloc_copys(stralloc *, char const *)
exit.h
sender
stralloc sender
Definition
fastforward.c:71
recipient
stralloc recipient
Definition
fastforward.c:274
headerbody
int headerbody(buffer *b, void(*dohf)(), void(*hdone)(), void(*dobl)())
Definition
headerbody.c:21
headerbody.h
hfield_skipname
unsigned int hfield_skipname(char *s, int len)
Definition
hfield.c:96
hfield_known
int hfield_known(char *s, int len)
Definition
hfield.c:57
hfield.h
H_RETURNPATH
#define H_RETURNPATH
Definition
hfield.h:29
H_SUBJECT
#define H_SUBJECT
Definition
hfield.h:16
H_DELIVEREDTO
#define H_DELIVEREDTO
Definition
hfield.h:30
H_FROM
#define H_FROM
Definition
hfield.h:9
h
void h(char *, char *, int, int, int)
Definition
install.c:15
filenames
stralloc filenames
Definition
maildir2mbox.c:23
pq
prioq pq
Definition
maildir2mbox.c:24
maildir.h
maildir_chdir
int maildir_chdir(void)
Definition
maildir.c:16
maildir_scan
int maildir_scan(prioq *, stralloc *, int, int)
Definition
maildir.c:83
maildir_clean
void maildir_clean(stralloc *)
Definition
maildir.c:27
text
stralloc text
Definition
maildirwatch.c:21
fromline
stralloc fromline
Definition
maildirwatch.c:20
main
int main()
Definition
maildirwatch.c:62
prioq.h
prioq_min
struct prioq_elt *int prioq_min(prioq *, struct prioq_elt *)
Definition
prioq.c:24
prioq_delmin
void prioq_delmin(prioq *)
Definition
prioq.c:32
fd
int fd
Definition
qmail-badloadertypes.c:31
finishheader
void finishheader()
Definition
qreceipt.c:65
die_nomem
void die_nomem(void)
Definition
qreceipt.c:23
prioq_elt
Definition
prioq.h:7
prioq_elt::id
unsigned long id
Definition
prioq.h:7
Generated on
for s/qmail by
1.14.0