From f5a0e7277cab9979398d1dff954ce159848401c5 Mon Sep 17 00:00:00 2001 From: Steve Fenton Date: Sun, 23 Nov 2014 21:26:00 +0000 Subject: [PATCH 1/5] Create imap.d.ts --- node-imap/imap.d.ts | 95 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 node-imap/imap.d.ts diff --git a/node-imap/imap.d.ts b/node-imap/imap.d.ts new file mode 100644 index 000000000..bc1c08cc2 --- /dev/null +++ b/node-imap/imap.d.ts @@ -0,0 +1,95 @@ +declare module "imap" { + interface ImapOptions { + user: string; + password: string; + host: string; + port: number; + tls: boolean; + } + + interface ImapBox { + name: string; + readOnly: boolean; + newKeywords: boolean; + uidvalidity: number; + uidnext: number; + flags: any[]; // TODO + permFlags: any[]; // TODO + persistentUIDs: boolean; + messages: { + total: number; + 'new': number; + unseen: number; + } + } + + interface ImapChunk { + toString(charset: string): string; + length: number; + } + + interface ImapFetch { + once(event: 'end', callback: () => void): void; + once(event: 'error', callback: (error: Error) => void): void; + once(event: string, callback: Function): void; + + on(event: 'message', callback: (msg: ImapMessage, seqno: number) => void): void; + on(event: string, callback: Function): void; + } + + interface ImapBodyStream { + once(event: 'end', callback: () => void): void; + once(event: string, callback: Function): void; + + on(event: 'data', callback: (chunk: ImapChunk) => void): void; + on(event: string, callback: Function): void; + + pipe(stream: any): void; + } + + interface ImapMessage { + // TODO: typeof attributes + once(event: 'attributes', callback: (attributes) => void): void; + once(event: string, callback: Function): void; + + // TODO: typeof info + on(event: 'body', callback: (stream: ImapBodyStream, info) => void): void; + on(event: string, callback: Function): void; + } + + class Imap { + constructor(options: ImapOptions); + + connect(): void; + + //TODO: + // param a + openBox(name: string, a: boolean, callback: (err: Error, box: ImapBox) => void); + + //TODO: + // param a + // param b + once(event: 'end', callback: () => void): void; + once(event: 'error', callback: (error: Error) => void): void; + once(a: string, callback: Function); + + end(): void; + + //TODO: + // return type + parseHeader(header: string): any; + static parseHeader(header: string): any; + + search(searchTerms: any[], callback: Function): void; + + fetch(results: any, options: {}): ImapFetch; + + //TODO: + // type + seq: { + fetch(messageSourceQuery: string, options: {}): ImapFetch; + }; + } + + export = Imap; +} From fe483987be6af028cbe8233d0d5923d1caf05665 Mon Sep 17 00:00:00 2001 From: Steve Fenton Date: Sun, 23 Nov 2014 21:26:25 +0000 Subject: [PATCH 2/5] Create imap-tests.ts --- node-imap/imap-tests.ts | 126 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 126 insertions(+) create mode 100644 node-imap/imap-tests.ts diff --git a/node-imap/imap-tests.ts b/node-imap/imap-tests.ts new file mode 100644 index 000000000..a12f626a1 --- /dev/null +++ b/node-imap/imap-tests.ts @@ -0,0 +1,126 @@ +import Imap = require('imap'); +var inspect = require('util').inspect; + +var imap = new Imap({ + user: 'mygmailname@gmail.com', + password: 'mygmailpassword', + host: 'imap.gmail.com', + port: 993, + tls: true +}); + +imap.once('ready', function () { + imap.openBox('INBOX', true,function (err, box) { + if (err) throw err; + var f = imap.seq.fetch('1:3', { + bodies: 'HEADER.FIELDS (FROM TO SUBJECT DATE)', + struct: true + }); + f.on('message', function (msg, seqno) { + console.log('Message #%d', seqno); + var prefix = '(#' + seqno + ') '; + msg.on('body', function (stream, info) { + var buffer = ''; + stream.on('data', function (chunk) { + buffer += chunk.toString('utf8'); + }); + stream.once('end', function () { + console.log(prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer))); + }); + }); + msg.once('attributes', function (attrs) { + console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8)); + }); + msg.once('end', function () { + console.log(prefix + 'Finished'); + }); + }); + f.once('error', function (err) { + console.log('Fetch error: ' + err); + }); + f.once('end', function () { + console.log('Done fetching all messages!'); + imap.end(); + }); + }); +}); + +imap.once('error', function (err) { + console.log(err); +}); + +imap.once('end', function () { + console.log('Connection ended'); +}); + +imap.connect(); + +imap.openBox('INBOX', true, function (err, box) { + if (err) throw err; + var f = imap.seq.fetch(box.messages.total + ':*', { bodies: ['HEADER.FIELDS (FROM)', 'TEXT'] }); + f.on('message', function (msg, seqno) { + console.log('Message #%d', seqno); + var prefix = '(#' + seqno + ') '; + msg.on('body', function (stream, info) { + if (info.which === 'TEXT') + console.log(prefix + 'Body [%s] found, %d total bytes', inspect(info.which), info.size); + var buffer = '', count = 0; + stream.on('data', function (chunk) { + count += chunk.length; + buffer += chunk.toString('utf8'); + if (info.which === 'TEXT') + console.log(prefix + 'Body [%s] (%d/%d)', inspect(info.which), count, info.size); + }); + stream.once('end', function () { + if (info.which !== 'TEXT') + console.log(prefix + 'Parsed header: %s', inspect(Imap.parseHeader(buffer))); + else + console.log(prefix + 'Body [%s] Finished', inspect(info.which)); + }); + }); + msg.once('attributes', function (attrs) { + console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8)); + }); + msg.once('end', function () { + console.log(prefix + 'Finished'); + }); + }); + f.once('error', function (err) { + console.log('Fetch error: ' + err); + }); + f.once('end', function () { + console.log('Done fetching all messages!'); + imap.end(); + }); +}); + +var fs = require('fs'), fileStream; + +imap.openBox('INBOX', true,function (err, box) { + if (err) throw err; + imap.search(['UNSEEN', ['SINCE', 'May 20, 2010']], function (err, results) { + if (err) throw err; + var f = imap.fetch(results, { bodies: '' }); + f.on('message', function (msg, seqno) { + console.log('Message #%d', seqno); + var prefix = '(#' + seqno + ') '; + msg.on('body', function (stream, info) { + console.log(prefix + 'Body'); + stream.pipe(fs.createWriteStream('msg-' + seqno + '-body.txt')); + }); + msg.once('attributes', function (attrs) { + console.log(prefix + 'Attributes: %s', inspect(attrs, false, 8)); + }); + msg.once('end', function () { + console.log(prefix + 'Finished'); + }); + }); + f.once('error', function (err) { + console.log('Fetch error: ' + err); + }); + f.once('end', function () { + console.log('Done fetching all messages!'); + imap.end(); + }); + }); +}); From 87896ada1f51129b112f14ca30328724a7a21b32 Mon Sep 17 00:00:00 2001 From: Steve Fenton Date: Sun, 23 Nov 2014 21:28:07 +0000 Subject: [PATCH 3/5] Update imap.d.ts --- node-imap/imap.d.ts | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/node-imap/imap.d.ts b/node-imap/imap.d.ts index bc1c08cc2..77ef53f5f 100644 --- a/node-imap/imap.d.ts +++ b/node-imap/imap.d.ts @@ -1,3 +1,10 @@ +// Type definitions for node imap +// Project: https://github.com/mscdex/node-imap +// Definitions by: Steve Fenton +// Definitions: https://github.com/borisyankov/DefinitelyTyped + +/// + declare module "imap" { interface ImapOptions { user: string; From b19efc4fbe4be11ab006291a96ca1f0a4310421d Mon Sep 17 00:00:00 2001 From: Steve Fenton Date: Sun, 23 Nov 2014 22:11:43 +0000 Subject: [PATCH 4/5] Update imap.d.ts Explicit (but temporary) any types. --- node-imap/imap.d.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/node-imap/imap.d.ts b/node-imap/imap.d.ts index 77ef53f5f..2b387136f 100644 --- a/node-imap/imap.d.ts +++ b/node-imap/imap.d.ts @@ -56,11 +56,11 @@ declare module "imap" { interface ImapMessage { // TODO: typeof attributes - once(event: 'attributes', callback: (attributes) => void): void; + once(event: 'attributes', callback: (attributes: any) => void): void; once(event: string, callback: Function): void; // TODO: typeof info - on(event: 'body', callback: (stream: ImapBodyStream, info) => void): void; + on(event: 'body', callback: (stream: ImapBodyStream, info: any) => void): void; on(event: string, callback: Function): void; } @@ -71,14 +71,14 @@ declare module "imap" { //TODO: // param a - openBox(name: string, a: boolean, callback: (err: Error, box: ImapBox) => void); + openBox(name: string, a: boolean, callback: (err: Error, box: ImapBox) => void) : void; //TODO: // param a // param b once(event: 'end', callback: () => void): void; once(event: 'error', callback: (error: Error) => void): void; - once(a: string, callback: Function); + once(a: string, callback: Function) : void; end(): void; From 5e68541a502169461d6fa4d7833e89e50becc723 Mon Sep 17 00:00:00 2001 From: Steve Fenton Date: Sun, 23 Nov 2014 22:12:48 +0000 Subject: [PATCH 5/5] Update imap-tests.ts --- node-imap/imap-tests.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/node-imap/imap-tests.ts b/node-imap/imap-tests.ts index a12f626a1..61f4b2e0c 100644 --- a/node-imap/imap-tests.ts +++ b/node-imap/imap-tests.ts @@ -94,11 +94,11 @@ imap.openBox('INBOX', true, function (err, box) { }); }); -var fs = require('fs'), fileStream; +var fs = require('fs'), fileStream : any; imap.openBox('INBOX', true,function (err, box) { if (err) throw err; - imap.search(['UNSEEN', ['SINCE', 'May 20, 2010']], function (err, results) { + imap.search(['UNSEEN', ['SINCE', 'May 20, 2010']], function (err : Error, results: any) { if (err) throw err; var f = imap.fetch(results, { bodies: '' }); f.on('message', function (msg, seqno) {