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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#![allow(clippy::result_unit_err)]
use super::*;
pub trait PostFilter {
fn feed<'p, 'list>(
self: Box<Self>,
post: &'p mut Post,
ctx: &'p mut ListContext<'list>,
) -> std::result::Result<(&'p mut Post, &'p mut ListContext<'list>), ()>;
}
pub struct PostRightsCheck;
impl PostFilter for PostRightsCheck {
fn feed<'p, 'list>(
self: Box<Self>,
post: &'p mut Post,
ctx: &'p mut ListContext<'list>,
) -> std::result::Result<(&'p mut Post, &'p mut ListContext<'list>), ()> {
trace!("Running PostRightsCheck filter");
if let Some(ref policy) = ctx.post_policy {
if policy.announce_only {
trace!("post policy is announce_only");
let owner_addresses = ctx
.list_owners
.iter()
.map(|lo| lo.address())
.collect::<Vec<Address>>();
trace!("Owner addresses are: {:#?}", &owner_addresses);
trace!("Envelope from is: {:?}", &post.from);
if !owner_addresses.iter().any(|addr| *addr == post.from) {
trace!("Envelope From does not include any owner");
post.action = PostAction::Reject {
reason: "You are not allowed to post on this list.".to_string(),
};
return Err(());
}
} else if policy.subscription_only {
trace!("post policy is subscription_only");
let email_from = post.from.get_email();
trace!("post from is {:?}", &email_from);
trace!("post subscriptions are {:#?}", &ctx.subscriptions);
if !ctx.subscriptions.iter().any(|lm| lm.address == email_from) {
trace!("Envelope from is not subscribed to this list");
post.action = PostAction::Reject {
reason: "Only subscriptions can post to this list.".to_string(),
};
return Err(());
}
} else if policy.approval_needed {
trace!("post policy says approval_needed");
let email_from = post.from.get_email();
trace!("post from is {:?}", &email_from);
trace!("post subscriptions are {:#?}", &ctx.subscriptions);
if !ctx.subscriptions.iter().any(|lm| lm.address == email_from) {
trace!("Envelope from is not subscribed to this list");
post.action = PostAction::Defer {
reason: "Your posting has been deferred. Approval from the list's \
moderators is required before it is submitted."
.to_string(),
};
return Err(());
}
}
}
Ok((post, ctx))
}
}
pub struct FixCRLF;
impl PostFilter for FixCRLF {
fn feed<'p, 'list>(
self: Box<Self>,
post: &'p mut Post,
ctx: &'p mut ListContext<'list>,
) -> std::result::Result<(&'p mut Post, &'p mut ListContext<'list>), ()> {
trace!("Running FixCRLF filter");
use std::io::prelude::*;
let mut new_vec = Vec::with_capacity(post.bytes.len());
for line in post.bytes.lines() {
new_vec.extend_from_slice(line.unwrap().as_bytes());
new_vec.extend_from_slice(b"\r\n");
}
post.bytes = new_vec;
Ok((post, ctx))
}
}
pub struct AddListHeaders;
impl PostFilter for AddListHeaders {
fn feed<'p, 'list>(
self: Box<Self>,
post: &'p mut Post,
ctx: &'p mut ListContext<'list>,
) -> std::result::Result<(&'p mut Post, &'p mut ListContext<'list>), ()> {
trace!("Running AddListHeaders filter");
let (mut headers, body) = melib::email::parser::mail(&post.bytes).unwrap();
let sender = format!("<{}>", ctx.list.address);
headers.push((&b"Sender"[..], sender.as_bytes()));
let mut subject = format!("[{}] ", ctx.list.id).into_bytes();
if let Some((_, subj_val)) = headers
.iter_mut()
.find(|(k, _)| k.eq_ignore_ascii_case(b"Subject"))
{
subject.extend(subj_val.iter().cloned());
*subj_val = subject.as_slice();
} else {
headers.push((&b"Subject"[..], subject.as_slice()));
}
let list_id = Some(ctx.list.id_header());
let list_help = ctx.list.help_header();
let list_post = ctx.list.post_header(ctx.post_policy.as_deref());
let list_unsubscribe = ctx
.list
.unsubscribe_header(ctx.subscription_policy.as_deref());
let list_subscribe = ctx
.list
.subscribe_header(ctx.subscription_policy.as_deref());
let list_archive = ctx.list.archive_header();
for (hdr, val) in [
(b"List-Id".as_slice(), &list_id),
(b"List-Help".as_slice(), &list_help),
(b"List-Post".as_slice(), &list_post),
(b"List-Unsubscribe".as_slice(), &list_unsubscribe),
(b"List-Subscribe".as_slice(), &list_subscribe),
(b"List-Archive".as_slice(), &list_archive),
] {
if let Some(val) = val {
headers.push((hdr, val.as_bytes()));
}
}
let mut new_vec = Vec::with_capacity(
headers
.iter()
.map(|(h, v)| h.len() + v.len() + ": \r\n".len())
.sum::<usize>()
+ "\r\n\r\n".len()
+ body.len(),
);
for (h, v) in headers {
new_vec.extend_from_slice(h);
new_vec.extend_from_slice(b": ");
new_vec.extend_from_slice(v);
new_vec.extend_from_slice(b"\r\n");
}
new_vec.extend_from_slice(b"\r\n\r\n");
new_vec.extend_from_slice(body);
post.bytes = new_vec;
Ok((post, ctx))
}
}
pub struct ArchivedAtLink;
impl PostFilter for ArchivedAtLink {
fn feed<'p, 'list>(
self: Box<Self>,
post: &'p mut Post,
ctx: &'p mut ListContext<'list>,
) -> std::result::Result<(&'p mut Post, &'p mut ListContext<'list>), ()> {
trace!("Running ArchivedAtLink filter");
Ok((post, ctx))
}
}
pub struct FinalizeRecipients;
impl PostFilter for FinalizeRecipients {
fn feed<'p, 'list>(
self: Box<Self>,
post: &'p mut Post,
ctx: &'p mut ListContext<'list>,
) -> std::result::Result<(&'p mut Post, &'p mut ListContext<'list>), ()> {
trace!("Running FinalizeRecipients filter");
let mut recipients = vec![];
let mut digests = vec![];
let email_from = post.from.get_email();
for subscription in ctx.subscriptions {
trace!("examining subscription {:?}", &subscription);
if subscription.address == email_from {
trace!("subscription is submitter");
}
if subscription.digest {
if subscription.address != email_from || subscription.receive_own_posts {
trace!("Subscription gets digest");
digests.push(subscription.address());
}
continue;
}
if subscription.address != email_from || subscription.receive_own_posts {
trace!("Subscription gets copy");
recipients.push(subscription.address());
}
}
ctx.scheduled_jobs.push(MailJob::Send { recipients });
if !digests.is_empty() {
ctx.scheduled_jobs.push(MailJob::StoreDigest {
recipients: digests,
});
}
post.action = PostAction::Accept;
Ok((post, ctx))
}
}