From f8f923a6d0b7677aff24eea2f8186b4409977c13 Mon Sep 17 00:00:00 2001 From: Andrew Jessup Date: Sat, 28 Jan 2012 14:38:05 +1100 Subject: [PATCH] Addded basic support for APPEND command --- README.md | 2 ++ imap.js | 27 ++++++++++++++++++++++++++- 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 7beaf5e..60ea1ec 100644 --- a/README.md +++ b/README.md @@ -370,6 +370,8 @@ ImapConnection Functions * **move**(Integer/String/Array, String, Function) - _(void)_ - Moves the message(s) with the message ID(s) identified by the first parameter, in the currently open mailbox, to the mailbox specified by the second parameter. The first parameter can either be an Integer for a single message ID, a String for a message ID range (e.g. '2504:2507' or '*' or '2504:*'), or an Array containing any number of the aforementioned Integers and/or Strings. The Function parameter is the callback with one parameter: the error (null if none). **Note:** The message in the destination mailbox will have a new message ID. +* **append**(String, Array, Date, Function) - _(void)_ - Appends a message to selected mailbox with the specified flags and date. The first parameter is a string the message to be appended, which should be a RFC-822 compatible MIME document including any headers and suitably encoded message attachments. The second parameter is should be an array of strings that denote the flags to be applied to the appended message (eg. ['\Seen', '\Flagged']) or null or an empty array if no flags should be set. The third parameter is a Date object that denotes the date the IMAP server should consider the message as being received, or it can be null (in which case the server will determine the received date). The Function parameter is the callback with one parameter: the error (null if none). **Note:** This method only serves to manipulate a connected mailbox, not to actually send mail. Some IMAP servers such as GMail will modify an existing message rather than create a new one if the specified 'Message-ID' header conflicts with a message already stored in that mailbox. + * **addFlags**(Integer/String/Array, String/Array, Function) - _(void)_ - Adds the specified flag(s) to the message(s) identified by the first parameter. The first parameter can either be an Integer for a single message ID, a String for a message ID range (e.g. '2504:2507' or '*' or '2504:*'), or an Array containing any number of the aforementioned Integers and/or Strings. The second parameter can either be a String containing a single flag or can be an Array of flags. The Function parameter is the callback with one parameter: the error (null if none). * **delFlags**(Integer/String/Array, String/Array, Function) - _(void)_ - Removes the specified flag(s) from the message(s) identified by the first parameter. The first parameter can either be an Integer for a single message ID, a String for a message ID range (e.g. '2504:2507' or '*' or '2504:*'), or an Array containing any number of the aforementioned Integers and/or Strings. The second parameter can either be a String containing a single flag or can be an Array of flags. The Function parameter is the callback with one parameter: the error (null if none). diff --git a/imap.js b/imap.js index 2915677..1fba03e 100644 --- a/imap.js +++ b/imap.js @@ -617,7 +617,32 @@ ImapConnection.prototype._search = function(which, options, cb) { + buildSearchQuery(options, this.capabilities), cb); }; -ImapConnection.prototype.seq.fetch = function(seqnos, options) { +ImapConnection.prototype.append = function(mimedata, flags, date, cb) { + if (this._state.status !== STATES.BOXSELECTED) { + throw new Error('No mailbox is currently selected'); + } + cmd = 'APPEND '+this._state.box.name+' '; + if(flags && flags.length) + if (!Array.isArray(flags)) + throw new Error('Expected null or array for flags'); + cmd += "("+flags.join(' ')+") "; + if(date){ + var months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', + 'Oct', 'Nov', 'Dec']; + if (!(date instanceof Date)) + throw new Error('Expected null or Date object for date'); + cmd += '"'+date.getDate()+'-'+months[date.getMonth()]+'-'+date.getFullYear(); + cmd += ' '+('0'+date.getHours().toString()).slice(-2)+':'+('0'+date.getMinutes().toString()).slice(-2)+':'+('0'+date.getSeconds().toString()).slice(-2); + cmd += ((date.getTimezoneOffset() > 0) ? ' -' : ' +' ); + cmd += ('0'+(-date.getTimezoneOffset() / 60).toString()).slice(-2); + cmd += ('0'+(-date.getTimezoneOffset() % 60).toString()).slice(-2); + cmd += '" '; + } + cmd += '{'+mimedata.length+"}\r\n" + mimedata; + this._send(cmd, cb); +} + +ImapConnection.prototype.fetch_seq = function(seqnos, options) { return this._fetch('', seqnos, options); }; ImapConnection.prototype.fetch = function(uids, options) {