-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetBlockedRecipient.ts
executable file
·66 lines (59 loc) · 1.86 KB
/
getBlockedRecipient.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
#!/usr/bin/env -S yarn exec ts-node
// Before running, make sure to source the EXCHANGE_CONFIG environtment variable dependency.
// E.g. `source ~/.gns/credentials`.
import * as ews from "ews-javascript-api";
import { withEwsConnection } from "./ews-connect";
const thisSystem = "exchange";
const findContactGroup = async (
service: ews.ExchangeService,
name: string
): Promise<ews.ContactGroup | undefined> => {
const filter = new ews.SearchFilter.SearchFilterCollection(
ews.LogicalOperator.And,
[
new ews.SearchFilter.IsEqualTo(ews.ContactGroupSchema.DisplayName, name),
new ews.SearchFilter.IsEqualTo(
ews.ContactGroupSchema.ItemClass,
"IPM.DistList"
),
]
);
const found = await service.FindItems(
ews.WellKnownFolderName.Contacts,
filter,
new ews.ItemView(2)
);
if (found.Items.length > 1) {
console.error({
system: thisSystem,
message: `Found more than one contact group named ${name}`,
});
return undefined;
} else if (found.Items.length === 1) {
return ews.ContactGroup.Bind(service, found.Items[0].Id);
}
return undefined;
};
function collectionToArray<T extends ews.ComplexProperty>(
collection: ews.ComplexPropertyCollection<T>
): ReadonlyArray<T> {
const result = Array.from({ length: collection.Count }).map((n, i) =>
collection._getItem(i)
);
return result;
}
const getBlockedRecipients = async (
service: ews.ExchangeService
): Promise<Set<string>> => {
const contactGroup = await findContactGroup(service, "Blocked Recipients");
const blockedEmails = contactGroup
? collectionToArray(contactGroup.Members).map(
(m) => m.AddressInformation.Address
)
: [];
const blockedEmailsSet = new Set(blockedEmails);
return blockedEmailsSet;
};
withEwsConnection(async (service) => {
console.log(await getBlockedRecipients(service));
});