-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmailonly.js
283 lines (240 loc) · 8.72 KB
/
mailonly.js
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
/**
* Mail-folders-only IMAProxy module
*
* Intercepts LSUB and LIST responses and removes non-mail folders
* from the listing after checking the /vendor/kolab/folder-type annotations.
*
* @author Thomas Bruederli <[email protected]>
*
* Copyright (C) 2014, Thomas Bruederli, Bern, Switzerland
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
"use strict";
var imap = require('../lib/imap.js');
/**
* Mail-folders-only IMAProxy plugin
*/
function Mailonly(proxy)
{
var TYPE_ANNOTATION = "/vendor/kolab/folder-type";
var listening = false;
var capabilities = {};
var metadata = [];
var proc = [];
// public methods
this.init = init;
/**
* Plugin init method
*/
function init()
{
proxy.clientEmitter.on('LSUB', clientList);
proxy.clientEmitter.on('LIST', clientList);
proxy.clientEmitter.on('XLIST', clientList);
proxy.clientEmitter.on('__DISCONNECT__', clientDisconnect);
proxy.serverEmitter.on('OK', OKResponse);
proxy.serverEmitter.once('CAPABILITY', capabilityResponse);
}
/**
* Handle OK responses which might have Capabilities appended
*/
function OKResponse(event, data)
{
var response = imap.parseResponse(data);
if (response.lines[0].match(/\[CAPABILITY\s/)) {
parseCapabilities(response.lines[0].replace(/\sOK/, '').replace(/\[|\]/i, ''));
if (capabilities['SORT'] || capabilities['ANNOTATEMORE']) {
proxy.serverEmitter.removeListener('OK', OKResponse);
}
}
}
/**
* Handle CAPABILITY response
*/
function capabilityResponse(event, data)
{
var response = imap.parseResponse(data);
if (response.status === 'OK') {
parseCapabilities(response.lines[0]);
proxy.serverEmitter.removeListener('OK', OKResponse);
}
}
/**
* Parse IMAP server capabilities
*/
function parseCapabilities(line)
{
var c, i, caps = imap.explodeQuotedString(line, " ");
for (i=2; i < caps.length; i++) {
c = caps[i].split('=');
capabilities[c[0]] = c[1] || true;
}
}
/**
* Handler for client LSUB or LIST commands
*/
function clientList(event, data)
{
// nothing to do here
if (!capabilities['ANNOTATEMORE']) {
proxy.clientEmitter.removeListener('LSUB', clientList);
proxy.clientEmitter.removeListener('LIST', clientList);
proxy.clientEmitter.removeListener('XLIST', clientList);
proxy.clientEmitter.removeListener('__DISCONNECT__', clientDisconnect);
return;
}
// register new LSUB/LIST/XLIST request for this connection
if (!proc[event.state.ID]) {
proc[event.state.ID] = { buffer:'', listings:{}, pending:0 };
}
var i, req, listing, lines = data.toString().trim().split(/\r?\n/);
for (i=0; i < lines.length; i++) {
req = imap.tokenizeData(lines[i], 2);
listing = { seq: req[0], command: req[1], buffer: [] };
proc[event.state.ID].listings['A' + listing.seq] = listing;
proc[event.state.ID].pending++;
}
// listen to server responses
if (!listening) {
proxy.serverEmitter.on('__DATA__', serverResponse);
listening = true;
}
}
/**
* Handler for server responses
*/
function serverResponse(event, data)
{
var req, last, response, id = event.state.ID;
// buffering is active for this connection
if (req = proc[id]) {
event.write = false; // don't forward to client
response = imap.parseResponse(data);
last = response.lines.pop();
// GETANNOTATION completed
if (response.seq && req.listings[response.seq]) {
var i, ann, values, lines = (req.buffer + data.toString()).trim().split(/\r?\n/);
for (i=0; i < lines.length; i++) {
ann = imap.tokenizeData(lines[i], 5);
values = ann[4] || [];
if (metadata[id] === undefined) {
metadata[id] = {};
}
// store folder type in global (per-connection) memory for subsequent requests (e.g. XLIST + LSUB)
if (ann[1] === 'ANNOTATION' && ann[3] === TYPE_ANNOTATION && values.length) {
metadata[id][ann[2]] = (values[1] || values[3] || '').replace(/\..+$/, '');
}
}
// clear buffer
req.buffer = '';
// filter buffered listing and send it to client
sendFilteredList(id, response.seq, event);
}
else {
req.buffer += data.toString();
// command done
if (response.seq) {
// pipe through unrelated results
event.write = !processListing(id, response.seq, req.buffer, event);
// send all buffered data to client
if (event.write && req.buffer) {
event.result = req.buffer;
}
// clear buffer
req.buffer = '';
}
}
}
}
/**
* Process the collected server response on a listing command
*/
function processListing(id, seq, buffer, event)
{
var i, req = proc[id], listing = req.listings['A' + seq],
lines = buffer.trim().split(/\r?\n/);
// tag doesn't match an active listing command or response is empty
if (!listing || lines.length < 2) {
listingDone(id, 'A'+seq);
return false;
}
// remove response line
lines.pop();
// get metadata for every mailbox name
for (i=0; i < lines.length; i++) {
listing.buffer.push(lines[i]);
}
// we already collected all annotations, send the (filtered) response to the client
if (metadata[id]) {
sendFilteredList(id, 'A' + seq, event);
}
else {
// fetch all folder annotations in one go
metadata[id] = {};
event.server.write('A' + seq + ' GETANNOTATION "*" "' + TYPE_ANNOTATION + '" ("value.priv" "value.shared")\r\n');
}
return true;
}
/**
* Filter and send the buffered list for the given sequence tag
*/
function sendFilteredList(id, seq, event)
{
var req, listing;
if ((req = proc[id]) && (listing = req.listings[seq])) {
proxy.config.debug_log && console.log("Mailonly filter:", listing.buffer, metadata[id]);
var i, rec, mbox, type, list = [];
for (i=0; i < listing.buffer.length; i++) {
rec = imap.tokenizeData(listing.buffer[i]);
mbox = rec.pop();
type = metadata[id][mbox];
if (!type || type === 'mail' || type === 'NIL') {
list.push(listing.buffer[i]);
}
}
// send filtered list as response to the client
event.result = list.join("\r\n") + "\r\n" +
listing.seq + " OK Completed (filtered by IMAProxy)\r\n";
// destroy listing job
listingDone(id, seq);
}
}
/**
* Terminate a buffered listing command and remporarily stored data
*/
function listingDone(id, seq)
{
var req;
if ((req = proc[id]) && req.listings[seq]) {
// destroy listing job
delete req.listings[seq];
req.pending--;
// all done for this connection, suspend response capturing
if (req.pending === 0) {
delete proc[id];
}
}
}
/**
* Handler for client disconnect
*/
function clientDisconnect(event)
{
delete proc[event.state.ID];
delete metadata[event.state.ID];
// TODO: remove serverEmitter listeners if no further jobs pending
}
}
module.exports = Mailonly;