/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var ReplyToAllAsCc = {
  isReplyAll: function() {
    return gMsgCompose.type == Components.interfaces.nsIMsgCompType.ReplyAll;
  },

  getOriginalSender: function() {
    var originalHdr = this.getMsgHdrFromURI(gMsgCompose.originalMsgURI);
    var sender = this.extractAddresses(originalHdr.mime2DecodedAuthor);
    return sender.length > 0 ? sender[0] : null ;
  },

  getMsgHdrFromURI: function(aURI) {
    return Components.classes['@mozilla.org/messenger;1']
             .getService(Components.interfaces.nsIMessenger)
             .msgHdrFromURI(aURI);
  },

  extractAddresses: function(aAddressesWithComments) {
    var parser = Components.classes['@mozilla.org/messenger/headerparser;1']
                   .getService(Components.interfaces.nsIMsgHeaderParser);
    var addresses = {};
    var names = {};
    var fullNames = {};
    var count = {};
    parser.parseHeadersWithArray(aAddressesWithComments, addresses, names, fullNames, count);
    return addresses.value;
  },

  get addressingWidget() {
    return document.getElementById('addressingWidget');
  },

  get awRecipientItems() {
    var menulists = this.addressingWidget.querySelectorAll('listitem.addressingWidgetItem');
    return Array.slice(menulists, 0);
  },

  getRecipientTypeChooser: function(aItem) {
    return aItem.querySelector('menulist');
  },
  getRecipientValue: function(aItem) {
    return aItem.querySelector('textbox').value;
  },

  init: function() {
    if (!this.isReplyAll())
      return;

    var sender = this.getOriginalSender();

    this.awRecipientItems.forEach(function(aItem) {
      var chooser = this.getRecipientTypeChooser(aItem);
      var recipient = this.getRecipientValue(aItem);
      var addresses = this.extractAddresses(recipient);
      if (chooser.value == 'addr_to' && addresses[0] != sender)
        chooser.value = 'addr_cc';
    }, this);
  },

  handleEvent: function(aEvent) {
    switch (aEvent.type) {
      case 'compose-window-init':
        document.documentElement.addEventListener('compose-window-close', this, false);
        window.addEventListener('unload', this, false);
        gMsgCompose.RegisterStateListener(this);
        return;

      case 'compose-window-close':
        gMsgCompose.UnregisterStateListener(this);
        return;

      case 'unload':
        document.documentElement.removeEventListener('compose-window-init', this, false);
        document.documentElement.removeEventListener('compose-window-close', this, false);
        window.removeEventListener('unload', this, false);
        return;
    }
  },

  // nsIMsgComposeStateListener
  NotifyComposeFieldsReady: function() {
    // do it after all fields are constructed completely.
    setTimeout(this.init.bind(this), 0);
  },
  NotifyComposeBodyReady: function() {},
  ComposeProcessDone: function() {},
  SaveInFolderDone: function() {}
};

document.documentElement.addEventListener('compose-window-init', ReplyToAllAsCc, false);

@@ -329,24 +329,227 @@
 yAll;%0A  %7D,%0A%0A
+  isSentMail: function() %7B%0A    var originalMailFolder = gMsgCompose.originalMsgURI.split('#')%5B0%5D.replace(/%5E(%5Cw+)-message:/, '$1:');%0A    return originalMailFolder == gMsgCompose.identity.fccFolder;%0A  %7D,%0A%0A
   getOrigina
@@ -1878,16 +1878,151 @@
 plyAll()
+ %7C%7C%0A        (Services.prefs.getBoolPref('extensions.reply-to-all-as-cc@clear-code.com.exceptSentFolder') &&%0A         this.isSentMail())
 )%0A      

Add ability to prevent convert To to Cc, when it is a reply for a sent mail