imapflow 1.0.131 → 1.0.133
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/commands/close.js +1 -0
- package/lib/commands/idle.js +29 -2
- package/lib/commands/select.js +95 -92
- package/lib/imap-flow.js +3 -0
- package/lib/tools.js +9 -1
- package/lib/types.d.ts +2 -0
- package/package.json +1 -1
package/lib/commands/close.js
CHANGED
package/lib/commands/idle.js
CHANGED
|
@@ -138,8 +138,11 @@ module.exports = async (connection, maxIdleTime) => {
|
|
|
138
138
|
}
|
|
139
139
|
|
|
140
140
|
let idleTimer;
|
|
141
|
-
|
|
142
141
|
return new Promise(resolve => {
|
|
142
|
+
if (!connection.currentSelectCommand) {
|
|
143
|
+
return resolve();
|
|
144
|
+
}
|
|
145
|
+
|
|
143
146
|
// no IDLE support, fallback to NOOP'ing
|
|
144
147
|
connection.preCheck = async () => {
|
|
145
148
|
connection.preCheck = false; // unset itself
|
|
@@ -149,8 +152,32 @@ module.exports = async (connection, maxIdleTime) => {
|
|
|
149
152
|
resolve();
|
|
150
153
|
};
|
|
151
154
|
|
|
155
|
+
let selectCommand = connection.currentSelectCommand;
|
|
156
|
+
|
|
152
157
|
let idleCheck = async () => {
|
|
153
|
-
let response
|
|
158
|
+
let response;
|
|
159
|
+
switch (connection.missingIdleCommand) {
|
|
160
|
+
case 'SELECT':
|
|
161
|
+
connection.log.debug({ src: 'c', msg: `Running SELECT to detect changes in folder` });
|
|
162
|
+
response = await connection.exec(selectCommand.command, selectCommand.arguments);
|
|
163
|
+
break;
|
|
164
|
+
|
|
165
|
+
case 'STATUS':
|
|
166
|
+
{
|
|
167
|
+
let statusArgs = [selectCommand.arguments[0], []]; // path
|
|
168
|
+
for (let key of ['MESSAGES', 'UIDNEXT', 'UIDVALIDITY', 'UNSEEN']) {
|
|
169
|
+
statusArgs[1].push({ type: 'ATOM', value: key.toUpperCase() });
|
|
170
|
+
}
|
|
171
|
+
connection.log.debug({ src: 'c', msg: `Running STATUS to detect changes in folder` });
|
|
172
|
+
response = await connection.exec('STATUS', statusArgs);
|
|
173
|
+
}
|
|
174
|
+
break;
|
|
175
|
+
|
|
176
|
+
case 'NOOP':
|
|
177
|
+
default:
|
|
178
|
+
response = await connection.exec('NOOP', false, { comment: 'IDLE not supported' });
|
|
179
|
+
break;
|
|
180
|
+
}
|
|
154
181
|
response.next();
|
|
155
182
|
};
|
|
156
183
|
|
package/lib/commands/select.js
CHANGED
|
@@ -49,105 +49,106 @@ module.exports = async (connection, path, options) => {
|
|
|
49
49
|
|
|
50
50
|
let encodedPath = encodePath(connection, path);
|
|
51
51
|
|
|
52
|
-
|
|
53
|
-
!options.readOnly ? 'SELECT' : 'EXAMINE',
|
|
54
|
-
[{ type: encodedPath.indexOf('&') >= 0 ? 'STRING' : 'ATOM', value: encodedPath }].concat(extraArgs || [])
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
switch (key) {
|
|
73
|
-
case 'highestmodseq':
|
|
74
|
-
key = 'highestModseq';
|
|
75
|
-
if (/^[0-9]+$/.test(value)) {
|
|
76
|
-
value = BigInt(value);
|
|
77
|
-
}
|
|
78
|
-
break;
|
|
79
|
-
|
|
80
|
-
case 'mailboxid':
|
|
81
|
-
key = 'mailboxId';
|
|
82
|
-
if (Array.isArray(value) && value.length) {
|
|
83
|
-
value = value[0];
|
|
84
|
-
}
|
|
85
|
-
break;
|
|
86
|
-
|
|
87
|
-
case 'permanentflags':
|
|
88
|
-
key = 'permanentFlags';
|
|
89
|
-
value = new Set(value);
|
|
90
|
-
break;
|
|
91
|
-
|
|
92
|
-
case 'uidnext':
|
|
93
|
-
key = 'uidNext';
|
|
94
|
-
value = Number(value);
|
|
95
|
-
break;
|
|
96
|
-
|
|
97
|
-
case 'uidvalidity':
|
|
98
|
-
key = 'uidValidity';
|
|
99
|
-
if (/^[0-9]+$/.test(value)) {
|
|
100
|
-
value = BigInt(value);
|
|
101
|
-
}
|
|
102
|
-
break;
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
map[key] = value;
|
|
52
|
+
let selectCommand = {
|
|
53
|
+
command: !options.readOnly ? 'SELECT' : 'EXAMINE',
|
|
54
|
+
arguments: [{ type: encodedPath.indexOf('&') >= 0 ? 'STRING' : 'ATOM', value: encodedPath }].concat(extraArgs || [])
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
response = await connection.exec(selectCommand.command, selectCommand.arguments, {
|
|
58
|
+
untagged: {
|
|
59
|
+
OK: async untagged => {
|
|
60
|
+
if (!untagged.attributes || !untagged.attributes.length) {
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
let section = !untagged.attributes[0].value && untagged.attributes[0].section;
|
|
64
|
+
if (section && section.length > 1 && section[0].type === 'ATOM' && typeof section[0].value === 'string') {
|
|
65
|
+
let key = section[0].value.toLowerCase();
|
|
66
|
+
let value;
|
|
67
|
+
|
|
68
|
+
if (typeof section[1].value === 'string') {
|
|
69
|
+
value = section[1].value;
|
|
70
|
+
} else if (Array.isArray(section[1])) {
|
|
71
|
+
value = section[1].map(entry => (typeof entry.value === 'string' ? entry.value : false)).filter(entry => entry);
|
|
106
72
|
}
|
|
107
73
|
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
74
|
+
switch (key) {
|
|
75
|
+
case 'highestmodseq':
|
|
76
|
+
key = 'highestModseq';
|
|
77
|
+
if (/^[0-9]+$/.test(value)) {
|
|
78
|
+
value = BigInt(value);
|
|
79
|
+
}
|
|
80
|
+
break;
|
|
81
|
+
|
|
82
|
+
case 'mailboxid':
|
|
83
|
+
key = 'mailboxId';
|
|
84
|
+
if (Array.isArray(value) && value.length) {
|
|
85
|
+
value = value[0];
|
|
86
|
+
}
|
|
87
|
+
break;
|
|
88
|
+
|
|
89
|
+
case 'permanentflags':
|
|
90
|
+
key = 'permanentFlags';
|
|
91
|
+
value = new Set(value);
|
|
92
|
+
break;
|
|
93
|
+
|
|
94
|
+
case 'uidnext':
|
|
95
|
+
key = 'uidNext';
|
|
96
|
+
value = Number(value);
|
|
97
|
+
break;
|
|
98
|
+
|
|
99
|
+
case 'uidvalidity':
|
|
100
|
+
key = 'uidValidity';
|
|
101
|
+
if (/^[0-9]+$/.test(value)) {
|
|
102
|
+
value = BigInt(value);
|
|
103
|
+
}
|
|
104
|
+
break;
|
|
129
105
|
}
|
|
130
106
|
|
|
131
|
-
map
|
|
132
|
-
},
|
|
133
|
-
VANISHED: async untagged => {
|
|
134
|
-
await connection.untaggedVanished(
|
|
135
|
-
untagged,
|
|
136
|
-
// mailbox is not yet open, so use a dummy mailbox object
|
|
137
|
-
{ path, uidNext: false, uidValidity: false }
|
|
138
|
-
);
|
|
139
|
-
},
|
|
140
|
-
// we should only get an untagged FETCH for a SELECT/EXAMINE if QRESYNC was asked for
|
|
141
|
-
FETCH: async untagged => {
|
|
142
|
-
await connection.untaggedFetch(
|
|
143
|
-
untagged,
|
|
144
|
-
// mailbox is not yet open, so use a dummy mailbox object
|
|
145
|
-
{ path, uidNext: false, uidValidity: false }
|
|
146
|
-
);
|
|
107
|
+
map[key] = value;
|
|
147
108
|
}
|
|
109
|
+
|
|
110
|
+
if (section && section.length === 1 && section[0].type === 'ATOM' && typeof section[0].value === 'string') {
|
|
111
|
+
let key = section[0].value.toLowerCase();
|
|
112
|
+
switch (key) {
|
|
113
|
+
case 'nomodseq':
|
|
114
|
+
key = 'noModseq';
|
|
115
|
+
map[key] = true;
|
|
116
|
+
break;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
},
|
|
120
|
+
FLAGS: async untagged => {
|
|
121
|
+
if (!untagged.attributes || (!untagged.attributes.length && Array.isArray(untagged.attributes[0]))) {
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
let flags = untagged.attributes[0].map(flag => (typeof flag.value === 'string' ? flag.value : false)).filter(flag => flag);
|
|
125
|
+
map.flags = new Set(flags);
|
|
126
|
+
},
|
|
127
|
+
EXISTS: async untagged => {
|
|
128
|
+
let num = Number(untagged.command);
|
|
129
|
+
if (isNaN(num)) {
|
|
130
|
+
return false;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
map.exists = num;
|
|
134
|
+
},
|
|
135
|
+
VANISHED: async untagged => {
|
|
136
|
+
await connection.untaggedVanished(
|
|
137
|
+
untagged,
|
|
138
|
+
// mailbox is not yet open, so use a dummy mailbox object
|
|
139
|
+
{ path, uidNext: false, uidValidity: false }
|
|
140
|
+
);
|
|
141
|
+
},
|
|
142
|
+
// we should only get an untagged FETCH for a SELECT/EXAMINE if QRESYNC was asked for
|
|
143
|
+
FETCH: async untagged => {
|
|
144
|
+
await connection.untaggedFetch(
|
|
145
|
+
untagged,
|
|
146
|
+
// mailbox is not yet open, so use a dummy mailbox object
|
|
147
|
+
{ path, uidNext: false, uidValidity: false }
|
|
148
|
+
);
|
|
148
149
|
}
|
|
149
150
|
}
|
|
150
|
-
);
|
|
151
|
+
});
|
|
151
152
|
|
|
152
153
|
let section = !response.response.attributes[0].value && response.response.attributes[0].section;
|
|
153
154
|
if (section && section.length && section[0].type === 'ATOM' && typeof section[0].value === 'string') {
|
|
@@ -183,6 +184,7 @@ module.exports = async (connection, path, options) => {
|
|
|
183
184
|
}
|
|
184
185
|
|
|
185
186
|
connection.mailbox = map;
|
|
187
|
+
connection.currentSelectCommand = selectCommand;
|
|
186
188
|
connection.state = connection.states.SELECTED;
|
|
187
189
|
|
|
188
190
|
if (!currentMailbox || currentMailbox.path !== path) {
|
|
@@ -204,6 +206,7 @@ module.exports = async (connection, path, options) => {
|
|
|
204
206
|
let currentMailbox = connection.mailbox;
|
|
205
207
|
|
|
206
208
|
connection.mailbox = false;
|
|
209
|
+
connection.currentSelectCommand = false;
|
|
207
210
|
connection.state = connection.states.AUTHENTICATED;
|
|
208
211
|
|
|
209
212
|
if (currentMailbox) {
|
package/lib/imap-flow.js
CHANGED
|
@@ -130,6 +130,7 @@ class ImapFlow extends EventEmitter {
|
|
|
130
130
|
* @param {String} [options.proxy] Optional proxy URL. Supports HTTP CONNECT (`http://`, `https://`) and SOCKS (`socks://`, `socks4://`, `socks5://`) proxies
|
|
131
131
|
* @param {Boolean} [options.qresync=false] If true, then enables QRESYNC support. EXPUNGE notifications will include `uid` property instead of `seq`
|
|
132
132
|
* @param {Number} [options.maxIdleTime] If set, then breaks and restarts IDLE every maxIdleTime ms
|
|
133
|
+
* @param {String} [options.missingIdleCommand="NOOP"] Which command to use if server does not support IDLE
|
|
133
134
|
* @param {Boolean} [options.disableBinary=false] If true, then ignores the BINARY extension when making FETCH and APPEND calls
|
|
134
135
|
* @param {Boolean} [options.disableAutoEnable] Do not enable supported extensions by default
|
|
135
136
|
* @param {Number} [options.connectionTimeout=90000] how many milliseconds to wait for the connection to establish (default is 90 seconds)
|
|
@@ -247,6 +248,7 @@ class ImapFlow extends EventEmitter {
|
|
|
247
248
|
* @type {MailboxObject|Boolean}
|
|
248
249
|
*/
|
|
249
250
|
this.mailbox = false;
|
|
251
|
+
this.currentSelectCommand = false;
|
|
250
252
|
|
|
251
253
|
/**
|
|
252
254
|
* Is current mailbox idling (`true`) or not (`false`)
|
|
@@ -275,6 +277,7 @@ class ImapFlow extends EventEmitter {
|
|
|
275
277
|
this.idRequested = false;
|
|
276
278
|
|
|
277
279
|
this.maxIdleTime = this.options.maxIdleTime || false;
|
|
280
|
+
this.missingIdleCommand = (this.options.missingIdleCommand || '').toString().toUpperCase().trim() || 'NOOP';
|
|
278
281
|
|
|
279
282
|
this.disableBinary = !!this.options.disableBinary;
|
|
280
283
|
|
package/lib/tools.js
CHANGED
|
@@ -354,6 +354,14 @@ module.exports = {
|
|
|
354
354
|
return map;
|
|
355
355
|
},
|
|
356
356
|
|
|
357
|
+
processName(name) {
|
|
358
|
+
name = (name || '').toString();
|
|
359
|
+
if (name.length > 2 && name.at(0) === '"' && name.at(-1) === '"') {
|
|
360
|
+
name = name.replace(/^"|"$/g, '');
|
|
361
|
+
}
|
|
362
|
+
return name;
|
|
363
|
+
},
|
|
364
|
+
|
|
357
365
|
parseEnvelope(entry) {
|
|
358
366
|
let getStrValue = obj => {
|
|
359
367
|
if (!obj) {
|
|
@@ -370,7 +378,7 @@ module.exports = {
|
|
|
370
378
|
|
|
371
379
|
let processAddresses = function (list) {
|
|
372
380
|
return [].concat(list || []).map(addr => ({
|
|
373
|
-
name: libmime.decodeWords(getStrValue(addr[0])
|
|
381
|
+
name: module.exports.processName(libmime.decodeWords(getStrValue(addr[0]))),
|
|
374
382
|
address: (getStrValue(addr[2]) || '') + '@' + (getStrValue(addr[3]) || '')
|
|
375
383
|
}));
|
|
376
384
|
},
|
package/lib/types.d.ts
CHANGED
|
@@ -29,6 +29,7 @@ declare module "imapflow" {
|
|
|
29
29
|
* @param [options.proxy] - Optional proxy URL. Supports HTTP CONNECT (`http://`, `https://`) and SOCKS (`socks://`, `socks4://`, `socks5://`) proxies
|
|
30
30
|
* @param [options.qresync = false] - If true, then enables QRESYNC support. EXPUNGE notifications will include `uid` property instead of `seq`
|
|
31
31
|
* @param [options.maxIdleTime] - If set, then breaks and restarts IDLE every maxIdleTime ms
|
|
32
|
+
* @param [options.missingIdleCommand = "NOOP"] - Which command to use if server does not support IDLE
|
|
32
33
|
* @param [options.disableBinary = false] - If true, then ignores the BINARY extension when making FETCH and APPEND calls
|
|
33
34
|
* @param [options.disableAutoEnable] - Do not enable supported extensions by default
|
|
34
35
|
* @param [options.connectionTimeout = 90000] - how many milliseconds to wait for the connection to establish (default is 90 seconds)
|
|
@@ -61,6 +62,7 @@ declare module "imapflow" {
|
|
|
61
62
|
proxy?: string;
|
|
62
63
|
qresync?: boolean;
|
|
63
64
|
maxIdleTime?: number;
|
|
65
|
+
missingIdleCommand?: string;
|
|
64
66
|
disableBinary?: boolean;
|
|
65
67
|
disableAutoEnable?: boolean;
|
|
66
68
|
connectionTimeout?: number;
|