forked from postalsys/imapflow
/
namespace.ts
105 lines (95 loc) · 3.57 KB
/
namespace.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
// Requests NAMESPACE info from server
export const NAMESPACE = async connection => {
if (![connection.states.AUTHENTICATED, connection.states.SELECTED].includes(connection.state)) {
// nothing to do here
return;
}
if (!connection.capabilities.has('NAMESPACE')) {
// try to derive from listing
let { prefix, delimiter } = await getListPrefix(connection);
if (delimiter && prefix && prefix.charAt(prefix.length - 1) !== delimiter) {
prefix += delimiter;
}
let map = {
personal: [{ prefix: prefix || '', delimiter }],
other: false,
shared: false
};
connection.namespaces = map;
connection.namespace = connection.namespaces.personal[0];
return connection.namespace;
}
let response;
try {
let map = {};
response = await connection.exec('NAMESPACE', false, {
untagged: {
NAMESPACE: async untagged => {
if (!untagged.attributes || !untagged.attributes.length) {
return;
}
map.personal = getNamsepaceInfo(untagged.attributes[0]);
map.other = getNamsepaceInfo(untagged.attributes[1]);
map.shared = getNamsepaceInfo(untagged.attributes[2]);
}
}
});
connection.namespaces = map;
// make sure that we have the first personal namespace always set
if (!connection.namespaces.personal[0]) {
connection.namespaces.personal[0] = { prefix: '', delimiter: '.' };
}
connection.namespaces.personal[0].prefix = connection.namespaces.personal[0].prefix || '';
response.next();
connection.namespace = connection.namespaces.personal[0];
return connection.namespace;
} catch (err) {
connection.log.warn({ err, cid: connection.id });
return {
error: true,
status: err.responseStatus,
text: err.responseText
};
}
};
async function getListPrefix(connection) {
let response;
try {
let map = {};
response = await connection.exec('LIST', ['', ''], {
untagged: {
LIST: async untagged => {
if (!untagged.attributes || !untagged.attributes.length) {
return;
}
map.flags = new Set(untagged.attributes[0].map(entry => entry.value));
map.delimiter = untagged.attributes[1] && untagged.attributes[1].value;
map.prefix = (untagged.attributes[2] && untagged.attributes[2].value) || '';
if (map.delimiter && map.prefix.charAt(0) === map.delimiter) {
map.prefix = map.prefix.slice(1);
}
}
}
});
response.next();
return map;
} catch (err) {
connection.log.warn({ err, cid: connection.id });
return {};
}
}
function getNamsepaceInfo(attribute) {
if (!attribute || !attribute.length) {
return false;
}
return attribute
.filter(entry => entry.length >= 2 && typeof entry[0].value === 'string' && typeof entry[1].value === 'string')
.map(entry => {
let prefix = entry[0].value;
let delimiter = entry[1].value;
if (delimiter && prefix && prefix.charAt(prefix.length - 1) !== delimiter) {
prefix += delimiter;
}
return { prefix, delimiter };
});
}