'use strict';

// This module converts message structure into an ENVELOPE object

/**
 * Convert a message object to an ENVELOPE object
 *
 * @param {Object} message A parsed mime tree node
 * @return {Object} ENVELOPE compatible object
 */
module.exports = function(header) {
    return [
        header.date || null,
        header.subject || '',
        processAddress(header.from),
        processAddress(header.sender, header.from),
        processAddress(header['reply-to'], header.from),
        processAddress(header.to),
        processAddress(header.cc),
        processAddress(header.bcc),
        // If this is an embedded MESSAGE/RFC822, then Gmail seems to
        // have a bug here, it states ''NIL'' as the value, not 'NIL'
        header['in-reply-to'] || null,
        header['message-id'] || null
    ];
};

/**
 * Converts an address object to a list of arrays
 * [{name: 'User Name', addres:'user@example.com'}] -> [['User Name', null, 'user', 'example.com']]
 *
 * @param {Array} arr An array of address objects
 * @return {Array} A list of addresses
 */
function processAddress(arr, def) {
    arr = [].concat(arr || []);
    if (!arr.length) {
        arr = [].concat(def || []);
    }
    if (!arr.length) {
        return null;
    }
    var result = [];
    arr.forEach(function(addr) {
        if (!addr.group) {
            result.push([
                addr.name || null,
                null, // FIXME: check the rfc, this should be related to groups
                (addr.address || '').split('@').shift() || null,
                (addr.address || '').split('@').pop() || null
            ]);
        } else {
            // Handle group syntax
            result.push([null, null, addr.name || '', null]);
            result = result.concat(processAddress(addr.group) || []);
            result.push([null, null, null, null]);
        }
    });

    return result;
}

@@ -8,16 +8,65 @@
 rict';%0A%0A
+var addressparser = require('./addressparser');%0A%0A
 // This 
@@ -204,39 +204,14 @@
 ct%7D 
-message A parsed mime tree node
+header
 %0A * 
@@ -929,16 +929,17 @@
 , addres
+s
 :'user@e
@@ -1053,16 +1053,68 @@
 objects%0A
+ * @param %7BArray%7D def default value for empty %60arr%60%0A
  * @retu
@@ -1352,16 +1352,16 @@
 t = %5B%5D;%0A
-
     arr.
@@ -1381,24 +1381,116 @@
 ion(addr) %7B%0A
+        if (typeof addr == 'string') %7B%0A            addr = addressparser(addr)%5B0%5D;%0A        %7D%0A
         if (

fix issue #3: test fetch->"FETCH ENVELOPE"