-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocess-ndr-messages.ts
executable file
·435 lines (404 loc) · 12.7 KB
/
process-ndr-messages.ts
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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
#!/usr/bin/env -S yarn exec ts-node
import * as ews from "ews-javascript-api";
import * as _ from "lodash";
import axios from "axios";
import {
collectionToArray,
findOrCreateContactGroup,
getConfigFromEnvironmentVariable,
withEwsConnection,
writeError,
writeProgress,
} from "./ews-connect";
import { createMailjetEvent } from "./mailjet-event";
import { z } from "zod";
type FieldType = "string" | "number" | "date";
function notEmpty<TValue>(value: TValue | null | undefined): value is TValue {
return value !== null && value !== undefined;
}
function isHardBounce(errorCode: string) {
return errorCode.startsWith("5.");
}
/**
* Identity function to get a narrow field name type
*/
const toFieldList = <T extends string>(
...items: ReadonlyArray<[T, number, FieldType]>
) => items;
// https://docs.microsoft.com/en-us/office/client-developer/outlook/mapi/pidtagoriginalmessageclass-canonical-property
// https://docs.microsoft.com/en-us/openspecs/exchange_server_protocols/ms-oxomsg/62366ac9-8c81-45f5-baa9-8b7bfd4db755
const extraFields = toFieldList(
["PidTagOriginalMessageClass", 0x004b, "string"],
["PidTagOriginalSubject", 0x0049, "string"],
["PidTagOriginalSubmitTime", 0x004e, "date"],
["PidTagOriginalMessageId", 0x1046, "string"],
// Would have been nice if the properties below actually existed on NDR items but nope
["PidTagNonDeliveryReportStatusCode", 0x0c20, "number"],
["PidTagNonDeliveryReportReasonCode", 0x0c04, "number"],
["PidTagNonDeliveryReportDiagCode", 0x0c05, "number"]
);
const mapiPropTypes: { [k in FieldType]: ews.MapiPropertyType } = {
date: ews.MapiPropertyType.SystemTime,
number: ews.MapiPropertyType.Integer,
string: ews.MapiPropertyType.String,
};
interface MapiTypes {
date: number;
number: number;
string: string;
}
const mapiProps = extraFields.map(([name, tag, type]) => ({
name,
type,
prop: new ews.ExtendedPropertyDefinition(tag, mapiPropTypes[type]),
}));
const extraProps = [
ews.ItemSchema.Subject,
ews.ItemSchema.DateTimeReceived,
ews.ItemSchema.TextBody,
ews.ItemSchema.Body,
ews.ItemSchema.ItemClass,
ews.EmailMessageSchema.ToRecipients,
...mapiProps.map(({ prop }) => prop),
];
const props = new ews.PropertySet(
ews.BasePropertySet.FirstClassProperties,
extraProps
);
type OutValue<T extends FieldType> = ews.IOutParam<MapiTypes[T]>;
const initOutValue = <T extends FieldType>(t: T): OutValue<T> => {
return { outValue: null } as any; // Typings are a bit weird here but null seems to be expected
};
interface FieldValue {
name: string;
type: FieldType;
value: OutValue<FieldType>;
}
function getItemExtendedProperties(item: ews.Item): ReadonlyArray<FieldValue> {
const values = mapiProps
.map(({ name, type, prop }): FieldValue | undefined => {
const value = initOutValue(type);
if (item.ExtendedProperties.TryGetValue(prop, value)) {
return { name, type, value };
}
})
.filter(notEmpty);
return values;
}
async function fetchItemByMessageId(
service: ews.ExchangeService,
messageId: string
): Promise<ews.EmailMessage | undefined> {
// Seems we can only use a (potentially slow) search filter to find an item based on its message id
const messageIdFilter = new ews.SearchFilter.IsEqualTo(
ews.EmailMessageSchema.InternetMessageId,
_.escape(messageId)
);
const findResult = await service.FindItems(
ews.WellKnownFolderName.SentItems,
messageIdFilter,
new ews.ItemView(10)
);
if (findResult.Items.length === 1) {
const result = await ews.EmailMessage.Bind(service, findResult.Items[0].Id);
return result;
}
return undefined;
}
// It’s pretty stupid that the error code is not a property of the message itself but
// has to be retrieved from the body text
function extractNdrErrorCode(body: string): string | undefined {
const matches = /Remote Server returned '([^']+)'/i.exec(body);
if (matches) {
const remoteResponse = matches[1];
// Sometimes the hash is missing, e.g. 550 5.1.17 SMTPSEND.Utf8RecipientAddress; UTF-8 recipient address not supported.
const codeMatches = /[#\s]([45]\.\d\.\d{1,3})\s/.exec(remoteResponse);
if (codeMatches) {
return codeMatches[1];
}
}
return undefined;
}
async function invokeWebhook({
ndrItem,
originalMessage,
originalInternetMessageId,
errorCode,
config: { webhookUrl, dryRun = false },
webhookNotBefore,
}: {
ndrItem: ews.Item;
originalMessage: ews.Item;
originalInternetMessageId: string;
errorCode: string;
config: Readonly<NdrProcessorConfig>;
webhookNotBefore: Date;
}): Promise<"success" | "failure"> {
// This is where we could create different kinds of payloads
const content = createMailjetEvent(
ndrItem,
originalMessage,
originalInternetMessageId,
errorCode
);
try {
const originalMessageDate =
originalMessage.DateTimeSent.MomentDate.toDate();
const tooOld = webhookNotBefore > originalMessageDate;
if (dryRun) {
writeProgress(
`Would ${
tooOld ? "not " : ""
}have invoked webhook for ${originalInternetMessageId}`
);
} else {
if (tooOld) {
writeProgress("No webhook call, message too old");
} else {
const _result = await axios.post(webhookUrl, content);
}
}
return "success";
} catch (e) {
return "failure";
}
}
async function blockRecipients(
service: ews.ExchangeService,
recipients: ReadonlyArray<ews.EmailAddress>,
config: Readonly<NdrProcessorConfig>
) {
const blockedSendersList = await findOrCreateContactGroup(
service,
config.blockedRecipientsListName ?? "Blocked Recipients"
);
if (blockedSendersList) {
const blockedSenders = collectionToArray(blockedSendersList.Members);
const foundEmailAddresses: ReadonlyArray<string> = blockedSenders.map(
(member) => member.AddressInformation.Address
);
let changed = false;
for (const recipient of recipients) {
if (
recipient.Address &&
!foundEmailAddresses.includes(recipient.Address)
) {
if (config.dryRun) {
writeProgress(
`Would have added blocked contact ${recipient.Address} to contact group`
);
} else {
writeProgress(
`Saving new blocked contact ${recipient.Address} to contact group`
);
blockedSendersList.Members.AddOneOff(
recipient.Name,
recipient.Address
);
changed = true;
}
}
}
if (changed) {
await blockedSendersList.Update(ews.ConflictResolutionMode.AutoResolve);
}
}
}
/**
* Returns "processed" if the ndr item should be move to the Processed folder
* because we are ready with it
*/
async function processOneNdrItem({
service,
item,
values,
config,
webhookNotBefore,
}: {
service: ews.ExchangeService;
item: ews.Item;
values: ReadonlyArray<FieldValue>;
config: Readonly<NdrProcessorConfig>;
webhookNotBefore: Date;
}): Promise<"processed" | "unprocessed"> {
if (
!item.ItemClass.localeCompare("Report.IPM.Note.NDR", undefined, {
sensitivity: "base",
})
) {
writeProgress(`NDR item found with Subject: ${item.Subject}`);
// Load as an email message to get the recipient list for the NDR because only those recipients
// failed
const ndrAsEmail = await ews.EmailMessage.Bind(service, item.Id);
/**
* NDR RFC 3463 code
*/
const errorCode = extractNdrErrorCode(item.TextBody.Text);
if (!errorCode) {
writeError("Could not extract error code");
} else {
const messageId = values.find(
({ name }) => name === "PidTagOriginalMessageId"
)?.value.outValue;
if (messageId && typeof messageId === "string") {
const originalMessage = await fetchItemByMessageId(service, messageId);
if (originalMessage) {
writeProgress(
`Sent on ${originalMessage.DateTimeSent.MomentDate.format(
"YYYY-MM-DD"
)}`
);
const webhookResult = await invokeWebhook({
ndrItem: item,
originalMessage,
originalInternetMessageId: messageId,
errorCode,
config,
webhookNotBefore,
});
if (isHardBounce(errorCode)) {
await blockRecipients(
service,
collectionToArray(ndrAsEmail.ToRecipients),
config
);
}
if (webhookResult === "success") {
return "processed";
}
}
} else {
writeError(
"Could not find PidTagOriginalMessageId for message - will move it anyway"
);
return "processed";
}
}
}
return "unprocessed";
}
async function findOrCreateFolder(service: ews.ExchangeService, name: string) {
const rootFolder = ews.WellKnownFolderName.MsgFolderRoot;
const filter = new ews.SearchFilter.IsEqualTo(
ews.FolderSchema.DisplayName,
name
);
const foundFolders = await service.FindFolders(
rootFolder,
filter,
new ews.FolderView(2)
);
if (foundFolders.Folders.length > 1) {
writeError(`Found more than one folder named ${name}`);
return undefined;
} else if (foundFolders.Folders.length === 1) {
return foundFolders.Folders[0];
}
const createdFolder = new ews.Folder(service);
createdFolder.DisplayName = name;
writeProgress(`Creating folder ${name}`);
await createdFolder.Save(rootFolder);
return createdFolder;
}
const ndrProcessorConfigSchema = z
.object({
processedFolderName: z.string().optional(),
blockedRecipientsListName: z.string().optional(),
webhookUrl: z.string(),
dryRun: z.boolean().optional(),
})
.strict()
.readonly();
type NdrProcessorConfig = z.infer<typeof ndrProcessorConfigSchema>;
/**
* Searching by query may be faster but there seems to be a huge delay (more than 10 minutes, then I gave up) between e.g. moving items between folders in Outlook Web Access
* and when the items actually show up in a search so we use a filter because it seems reliable
*/
async function findItemsByQueryOrFilter(
service: ews.ExchangeService,
query: string,
filter: ews.SearchFilter,
view: ews.ItemView
) {
// Not: const result = service.FindItems(ews.WellKnownFolderName.Inbox, query, view);
const result = await service.FindItems(
ews.WellKnownFolderName.Inbox,
filter,
view
);
return result;
}
async function processNdrMessages(service: ews.ExchangeService) {
// References:
// https://stackoverflow.com/questions/12176360/get-original-message-headers-using-ews-for-bounced-emails
// https://docs.microsoft.com/en-us/previous-versions/office/developer/exchange-server-2010/ee693615%28v%3dexchg.140%29
// kind:Report is not documented - found through trial and error
const query = `kind:report`;
const filter = new ews.SearchFilter.SearchFilterCollection(
ews.LogicalOperator.And,
[
new ews.SearchFilter.IsEqualTo(
ews.ItemSchema.ItemClass,
"REPORT.IPM.Note.NDR"
),
]
);
let offset = 0;
const configRaw = getConfigFromEnvironmentVariable<unknown>(
"NDR_PROCESSOR_CONFIG"
);
if (!configRaw) {
writeError("Error: NDR_PROCESSOR_CONFIG environment variable must be set");
process.exit(4);
}
const parsed = ndrProcessorConfigSchema.safeParse(configRaw);
if (!parsed.success) {
writeError("Error: configuration did not validate");
for (const error of parsed.error.issues) {
writeError(`${error.message} ${error.path}`);
}
process.exit(4);
}
const processorConfig = parsed.data;
const processedFolder = await findOrCreateFolder(
service,
processorConfig.processedFolderName ?? "NDR Processed"
);
if (!processedFolder) {
writeError(
"Could not find or create folder for processed items - aborting"
);
process.exit(2);
}
do {
const webhookNotBefore = new Date();
webhookNotBefore.setMonth(webhookNotBefore.getMonth() - 1);
const view = new ews.ItemView(10, offset);
const found = await findItemsByQueryOrFilter(service, query, filter, view);
if (found.Items.length > 0) {
await service.LoadPropertiesForItems(found.Items, props);
for (const item of found.Items) {
const processResult = await processOneNdrItem({
service,
item,
values: getItemExtendedProperties(item),
config: processorConfig,
webhookNotBefore,
});
if (processResult === "processed") {
if (processorConfig.dryRun) {
writeProgress("Would have moved item to processed documents");
} else {
await item.Move(processedFolder.Id);
}
}
}
}
if (!found.MoreAvailable) {
break;
}
offset = found.NextPageOffset;
} while (true);
}
writeProgress("process-ndr-messages");
withEwsConnection(processNdrMessages);