-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathimap.js
401 lines (389 loc) · 11.2 KB
/
imap.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
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
/*
* Copyright (c) 2012 Adam Jabłooński
*
* Gmail Notify Extension is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2 of the License, or (at your
* option) any later version.
*
* Gmail Notify Extension 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 General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License along
* with Gnome Documents; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author: Adam Jabłoński <[email protected]>
*
*/
const Extension = imports.misc.extensionUtils.getCurrentExtension();
try {
const Gio = imports.gi.Gio;
}
catch (err) {
global.log("Soup import error:"+err.message);
}
const Signals = imports.signals;
const Lang = imports.lang;
const Unicode = Extension.imports.unicode;
const BigInteger = Extension.imports.biginteger;
const _DEBUG=false;
function ImapMessage () {
this._init();
};
ImapMessage.prototype = {
_init : function () {
this.from="";
this.subject="";
this.date="";
this.id =0;
this.link="";
this.safeid="";
},
set : function (prop,value){
switch (prop.toLowerCase())
{
case "from":
this.from=value;
break;
case "link":
this.link=value;
break;
case "id":
this.id=value;
break;
case "subject":
this.subject=value;
break;
case "safeid":
this.safeid=value;
break;
case "date":
this.date=value;
break;
}
}
};
var Imap = function () {
this._init.apply(this,arguments);
};
Imap.prototype= {
_init : function (conn) {
this._conn=conn;
this.authenticated=false;
this.folders=new Array();
},
_gen_tag: function () {
let tag="";
for (let i=0;i<5;i++) tag+=Math.floor(Math.random()*10).toString();
return tag;
},
_commandOK : function(aResp) {
try {
let ret=false ;
if (aResp !=null) {
for (let i=0;i<aResp.length;i++) {
if (aResp[i].search(/^[0-9]{5} OK.*$/)>-1)
{
ret = true;
break;
}
}
return ret;
}
else {
return false ;
}
}
catch (err) {
global.log('_CommmandOK error:'+err.message);
}
},
_readBuffer : function (tag,is_init,decode,callback,read,i) {
is_init = typeof (is_init) != 'undefined' ? is_init : false;
decode = typeof (decode) != 'undefined' ? decode : true;
if (typeof(read) =='undefined')
{
if (_DEBUG) global.log ("initializing array .. "+i)
read= new Array();
i=0;
}
else {
i++
}
if (_DEBUG) global.log ("Reading buffer .. "+i);
this._conn.inputStream.read_line_async(0,null,Lang.bind(this,function (stream,result) {
if (_DEBUG) global.log ("Finishing read .. ");
let buff=this._conn.inputStream.read_line_finish(result);
if (buff==null) global.log("Buffer null!");
read[i] = (new String(buff[0])).substr(0,buff[0].length-1);
if (decode){
let matches=read[i].match(/&.*-/g);
if (matches != null){
for (let j=0;j<matches.length;j++) {
let dec=GLib.base64_decode(matches[j].substr(1,matches[j].length-2)+"="+(matches[j].length % 2 ==0 ? "=":""));
let us="";
for(let k=0;k<dec.length;k+=2){
us+=String.fromCharCode(dec[k]*256+dec[k+1]);
}
read[i]=read[i].replace(matches[j],us);
}
}
}
if ((tag.length >0 && read[i].substr(0,tag.length) == tag) || (is_init)) {
if (typeof(callback) != 'undefined') {
callback.apply(this,[this,read]);
}
this.emit('buffer-ready',read);
} else {
this._readBuffer(tag,is_init,decode,callback,read,i);
}
}),
null);
},
_logout : function() {
if (this.connected)
{
let tag=this._gen_tag();
this._output_stream.put_string(tag+" LOGOUT"+_newline,null);
this._readBuffer(tag,false,true,Lang.bind(this,function(oImap,resp){
if (_DEBUG) {
for (let i=0;i<resp.length;i++) global.log(resp[i]);
this.emit('logged out',resp);
}
}));
}
},
_command : function (cmd,decode,callback) {
decode = typeof (decode) != 'udefined' ? decode : true;
if (_DEBUG) global.log ("Entering Command ..");
let tag=this._gen_tag();
if (_DEBUG) global.log ("Sending .. "+ tag+" "+cmd);
if (this._conn.outputStream.put_string(tag+" "+cmd+this._conn.newline,null))
{
this._readBuffer(tag,false,decode,Lang.bind(this,function(oImap,resp){
if (_DEBUG) global.log("Entering callback .. ");
if (_DEBUG) {for (let i=0;i<resp.length;i++) global.log("< "+resp[i]);}
if (typeof(callback)!='undefined') {
if (_DEBUG) global.log ("Calling callback .. ");
callback.apply(this,[this,resp]);
}
}));
}
else
{
throw new Error ('Imap command: cannot put command');
}
},
_scanFolder: function (folder,callback) {
try
{
this.folders=new Array();
this._command("EXAMINE "+folder,true,Lang.bind(this,function(oImap,resp) {
if (_DEBUG) {for (let i=0;i<resp.length;i++) global.log("< "+resp[i]);}
//get number of total and unseen
let sTotal=0;
let sUnseen=0;
for (let i=0;i<resp.length;i++) {
let tmatch=resp[i].match (/\* ([0-9]+) EXISTS.*/);
if (tmatch!=null) {
if (_DEBUG) global.log ("-- TOTAL "+tmatch[1]);
sTotal=parseInt(tmatch[1]);
}
}
try {
let messages=new Array();
this._command("SEARCH UNSEEN",true,Lang.bind(this,function(oImap,resp) {
let xmatches=resp[0].match(/(([0-9]+[ ]*)+){1}/g);
if (xmatches!=null)
{
if (_DEBUG) global.log("xmatches"+xmatches);
sUnseen=xmatches[0].split(" ").length;
if (_DEBUG)
{
for (let l=0;l<xmatches.length;l++)
{
global.log (xmatches[l]);
}
}
if (_DEBUG) global.log("FETCH");
this._command("FETCH "+xmatches[0].replace(/ /g,",")+" (FLAGS BODY.PEEK[HEADER.FIELDS (DATE FROM SUBJECT)] X-GM-MSGID)",false, Lang.bind(this,function(oImap,presp)
{ try {
if (_DEBUG)
{
for (let d=0;d<presp.length;d++)
{
global.log ("Line "+d.toString()+": "+presp[d]);
global.log ("char "+d.toString()+": "+presp[d].charCodeAt(0));
}
}
var fline="";
var part="";
var m;
for (let l=0;l<presp.length;l++)
{
if (_DEBUG) global.log (Unicode.unescapeFromMime(presp[l]));
let line = Unicode.unescapeFromMime(presp[l]);
let pmatches=line.match(/^(From|Date|Subject){1}\s*[:]{1}(.*)$/i);
if (pmatches != null)
{
if (_DEBUG) global.log("Match! fline:"+fline);
if (fline!="")
{
m.set(part,fline);
//messages.push(m);
if (_DEBUG) global.log("Push!");
}
part=pmatches[1];
fline=pmatches[2];
}
else
{
if (_DEBUG )
{
global.log("No match!, line:" +line);
global.log("No match!, code:" +line.substr(0,1).charCodeAt(0));
}
if ( presp[l].substr(0,1)==" ")
{
if (_DEBUG) global.log("Space !");
fline+=line;
continue;
}
if ( presp[l].substr(0,1)=="*")
{
if (_DEBUG) global.log("Star!");
if (fline!="")
{
m.set(part,fline);
messages.push(m);
}
m=new ImapMessage();
let idmatches=line.match(/^\*\s([0-9]+)\sFETCH\s+\(X-GM-MSGID\s([0-9]+)\s.+/);
if (idmatches !=null)
{
try {
m.set("id",parseInt(idmatches[1]));
if (_DEBUG) global.log(idmatches[2]);
let ba=BigInteger.BigInteger.parse(idmatches[2]);
m.set("link",'http://mail.google.com/mail?account_id='+this._conn._oAccount.get_account().identity+'&message_id='+ba.toString(16)+'&view=conv&extsrc=atom');
}
catch (err) {
global.log(err.message);
}
}
fline="";
continue;
}
if (fline!="")
{
m.set(part,fline);
messages.push(m);
fline=""
}
}
}
this.folders.push(new Object({name:folder,encoded:folder,messages:sTotal,unseen:sUnseen,list: messages }));
if (typeof (callback)!='undefined')
{
callback.apply(this,[this,messages])
}
this.emit('folder-scanned',folder);
}
catch (err) {
if (typeof (callback)!='undefined')
{
callback.apply(this,[this,null,err])
}
}
})); //FETCH
}
else {
this.folders.push(new Object({name:folder,encoded:folder,messages:sTotal,unseen:sUnseen,list:messages }));
if (typeof (callback)!='undefined')
{
callback.apply(this,[this,messages])
}
this.emit('folder-scanned',folder);
}
}));
}
catch (err) {
if (typeof (callback)!='undefined')
{
callback.apply(this,[this,null,err])
}
}
}));
}
catch (err)
{
if (typeof (callback)!='undefined')
{
callback.apply(this,[this,null,err])
}
}
},
_scan : function (inboxOnly,callback) {
inboxOnly=typeof (inboxOnly) !='undefined' ? inboxOnly:true;
global.log ("scan entry ..");
if (this.authenticated)
{
global.log ("weel authenticated ..");
try
{
this._command("EXAMINE INBOX",true,Lang.bind(this,function(oImap,resp){
if (_DEBUG) {for (let i=0;i<resp.length;i++) global.log("< "+resp[i]);}
this._command("LIST \"\" *",false,Lang.bind(this,function(oImap,resp){
if (_DEBUG) {for (let i=0;i<resp.length;i++) global.log(resp[i]);}
let sumMess=0;
let sumUnseen=0;
// for every folder
for (let i=0;i<resp.length;i++){
let matches=inboxOnly ? resp[i].match(/("INBOX")$/g) :resp[i].match(/("[^".]*")$/g);
if (matches !=null)
{
this._command("STATUS "+matches[0]+" (MESSAGES UNSEEN)",true,Lang.bind(this,function(oImap,cmdstatus){
if (this._commandOK(cmdstatus))
{
for (let k=0;k<cmdstatus.length-1;k++)
{
let cmdmatches=cmdstatus[k].match(/^\* STATUS "(.*)" \(MESSAGES ([0-9]*) UNSEEN ([0-9]*)\)$/);
if (cmdmatches !=null)
{
if (cmdmatches[1].toUpperCase().search(/^\[GMAIL|IMAP\]/)==-1)
{
sumMess+=parseInt(cmdmatches[2]);
sumUnseen+=parseInt(cmdmatches[3]);
let messages=new Array();
if (_DEBUG) global.log("SEARCH");
//SEARCH UNSEEN
}
}
}
}
})); //STATUS MESSAGES UNSEEN
//
if (inboxOnly) break;
}
}
//if (_DEBUG) global.log("MESS in SCAN "+sumMess.toString());
this.numMessages=sumMess;
this.numUnseen=sumUnseen;
})); //LIST
})); //EXAMINE INBOX
}
catch (err){
return [false,err.message];
}
if (typeof(callback) != 'undefined')
{
callback.apply(this,[this]);
}
}
return [false,"Not authenticaed or connected"];
},
};
Signals.addSignalMethods(Imap.prototype);