| 1 | /* ***** BEGIN LICENSE BLOCK ***** | 
|---|
| 2 | This program is free software; you can redistribute it and/or modify | 
|---|
| 3 | it under the terms of the GNU General Public License as published by | 
|---|
| 4 | the Free Software Foundation; either version 2 of the License, or | 
|---|
| 5 | (at your option) any later version. | 
|---|
| 6 |  | 
|---|
| 7 | This program is distributed in the hope that it will be useful, | 
|---|
| 8 | but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 9 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 10 | GNU General Public License for more details. | 
|---|
| 11 |  | 
|---|
| 12 | You should have received a copy of the GNU General Public License | 
|---|
| 13 | along with this program; if not, write to the Free Software | 
|---|
| 14 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA | 
|---|
| 15 |  | 
|---|
| 16 | The Original Code is the Virtual Identity Extension. | 
|---|
| 17 |  | 
|---|
| 18 | The Initial Developer of the Original Code is Rene Ejury. | 
|---|
| 19 | Portions created by the Initial Developer are Copyright (C) 2007 | 
|---|
| 20 | the Initial Developer. All Rights Reserved. | 
|---|
| 21 |  | 
|---|
| 22 | Contributor(s): | 
|---|
| 23 | * ***** END LICENSE BLOCK ***** */ | 
|---|
| 24 |  | 
|---|
| 25 | var EXPORTED_SYMBOLS = []; | 
|---|
| 26 |  | 
|---|
| 27 | const { | 
|---|
| 28 | classes: Cc, | 
|---|
| 29 | interfaces: Ci, | 
|---|
| 30 | utils: Cu, | 
|---|
| 31 | results: Cr | 
|---|
| 32 | } = Components; | 
|---|
| 33 |  | 
|---|
| 34 | Cu.import("resource://v_identity/vI_log.js"); | 
|---|
| 35 | Cu.import("resource://v_identity/vI_rdfDatasource.js"); | 
|---|
| 36 | Cu.import("resource://v_identity/vI_account.js"); | 
|---|
| 37 | Cu.import("resource://v_identity/vI_smartIdentityCollection.js"); | 
|---|
| 38 | Cu.import("resource://v_identity/vI_identityData.js"); | 
|---|
| 39 | Cu.import("resource://v_identity/vI_prefs.js"); | 
|---|
| 40 |  | 
|---|
| 41 | let Log = setupLogging("virtualIdentity.plugins.conversations"); | 
|---|
| 42 |  | 
|---|
| 43 | const AccountManager = Cc["@mozilla.org/messenger/account-manager;1"] | 
|---|
| 44 | .getService(Components.interfaces.nsIMsgAccountManager); | 
|---|
| 45 |  | 
|---|
| 46 | const HeaderParser = Cc["@mozilla.org/messenger/headerparser;1"] | 
|---|
| 47 | .getService(Ci.nsIMsgHeaderParser); | 
|---|
| 48 |  | 
|---|
| 49 | let currentIdentityData; | 
|---|
| 50 | let currentIdSenderName; | 
|---|
| 51 | let virtualIdInUse; | 
|---|
| 52 | let virtualSenderNameElem; | 
|---|
| 53 |  | 
|---|
| 54 | let _rdfDatasourceAccess; | 
|---|
| 55 |  | 
|---|
| 56 | let changeIdentityToSmartIdentity = function (allIdentities, index) { | 
|---|
| 57 | _changeIdentityToSmartIdentity(allIdentities.identityDataCollection[index]); | 
|---|
| 58 | }; | 
|---|
| 59 |  | 
|---|
| 60 | let _changeIdentityToSmartIdentity = function (identityData) { | 
|---|
| 61 | Log.debug("changeIdentityToSmartIdentity"); | 
|---|
| 62 |  | 
|---|
| 63 | if (identityData.id.key != null) { | 
|---|
| 64 | currentParams.identity = AccountManager.getIdentity(identityData.id.key); | 
|---|
| 65 | Log.debug("changed base identity to ", identityData.id.key); | 
|---|
| 66 | virtualSenderNameElem.text(currentIdSenderName); | 
|---|
| 67 | } | 
|---|
| 68 | virtualIdInUse = !(identityData.isExistingIdentity(false)); | 
|---|
| 69 | Log.debug("changeIdentityToSmartIdentity virtualIdInUse=" + virtualIdInUse); | 
|---|
| 70 | if (virtualIdInUse) { | 
|---|
| 71 | currentIdentityData = identityData; | 
|---|
| 72 | currentIdSenderName = currentIdentityData.combinedName; | 
|---|
| 73 | } | 
|---|
| 74 | virtualSenderNameElem.text(identityData.combinedName); // change this also to reflect changes of base id | 
|---|
| 75 | }; | 
|---|
| 76 |  | 
|---|
| 77 | let virtualIdentityHook = { | 
|---|
| 78 | onComposeSessionChanged: function _virtualIdentityHook_onComposeSessionChanged(aComposeSession, aMessage, aAddress) { | 
|---|
| 79 | let toAddrList = aAddress.to.concat(aAddress.cc); | 
|---|
| 80 |  | 
|---|
| 81 | currentParams = aComposeSession.params; | 
|---|
| 82 | virtualSenderNameElem = aComposeSession.senderNameElem; // to enable access from out of this class. | 
|---|
| 83 | let identity = aComposeSession.params.identity; | 
|---|
| 84 |  | 
|---|
| 85 | if (typeof (this._AccountManager.getServersForIdentity) == 'function') { // new style | 
|---|
| 86 | let server = this._AccountManager.getServersForIdentity(identity).queryElementAt(0, Components.interfaces.nsIMsgIncomingServer); | 
|---|
| 87 | } else { | 
|---|
| 88 | let server = this._AccountManager.GetServersForIdentity(identity).QueryElementAt(0, Components.interfaces.nsIMsgIncomingServer); | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | currentIdentityData = new identityData(identity.email, identity.fullName, identity.key, | 
|---|
| 92 | identity.smtpServerKey, null, server.prettyName, true) | 
|---|
| 93 | currentIdSenderName = currentIdentityData.combinedName; | 
|---|
| 94 | virtualIdInUse = false; | 
|---|
| 95 |  | 
|---|
| 96 | let recipients = []; | 
|---|
| 97 | var combinedNames = {}; | 
|---|
| 98 | var number; | 
|---|
| 99 | number = HeaderParser.parseHeadersWithArray(toAddrList.join(", "), {}, {}, combinedNames); | 
|---|
| 100 | for (var index = 0; index < number; index++) | 
|---|
| 101 | recipients.push({ | 
|---|
| 102 | recipient: combinedNames.value[index], | 
|---|
| 103 | recipientType: "addr_to" | 
|---|
| 104 | }) | 
|---|
| 105 |  | 
|---|
| 106 | var localSmartIdentityCollection = new smartIdentityCollection(aComposeSession.params.msgHdr, identity, | 
|---|
| 107 | false, false, recipients); | 
|---|
| 108 | localSmartIdentityCollection.Reply(); // we can always use the reply-case, msgHdr is set the right way | 
|---|
| 109 |  | 
|---|
| 110 | if (localSmartIdentityCollection._allIdentities.number == 0) | 
|---|
| 111 | return; | 
|---|
| 112 |  | 
|---|
| 113 | if (vIprefs.get("idSelection_preferExisting")) { | 
|---|
| 114 | var existingIDIndex = localSmartIdentityCollection._foundExistingIdentity(); | 
|---|
| 115 | if (existingIDIndex) { | 
|---|
| 116 | Log.debug("smartIdentity: found existing Identity, use without interaction.", existingIDIndex.key); | 
|---|
| 117 | changeIdentityToSmartIdentity(localSmartIdentityCollection._allIdentities, existingIDIndex.key); | 
|---|
| 118 | return; | 
|---|
| 119 | } | 
|---|
| 120 | } | 
|---|
| 121 |  | 
|---|
| 122 | if (vIprefs.get("idSelection_ask") && | 
|---|
| 123 | ((localSmartIdentityCollection._allIdentities.number == 1 && vIprefs.get("idSelection_ask_always")) || localSmartIdentityCollection._allIdentities.number > 1)) { | 
|---|
| 124 | recentWindow = Cc["@mozilla.org/appshell/window-mediator;1"] | 
|---|
| 125 | .getService(Ci.nsIWindowMediator) | 
|---|
| 126 | .getMostRecentWindow("mail:3pane"); | 
|---|
| 127 |  | 
|---|
| 128 | recentWindow.openDialog("chrome://v_identity/content/vI_smartReplyDialog.xul", 0, | 
|---|
| 129 | "chrome, dialog, modal, alwaysRaised, resizable=yes", | 
|---|
| 130 | localSmartIdentityCollection._allIdentities, | 
|---|
| 131 | /* callback: */ | 
|---|
| 132 | changeIdentityToSmartIdentity).focus(); | 
|---|
| 133 | } else if (vIprefs.get("idSelection_autocreate")) | 
|---|
| 134 | changeIdentityToSmartIdentity(localSmartIdentityCollection._allIdentities, 0); | 
|---|
| 135 | }, | 
|---|
| 136 |  | 
|---|
| 137 | onMessageBeforeSendOrPopout_canceled: function _enigmailHook_onMessageBeforeSendOrPopout_canceledy(aAddress, aEditor, aStatus, aPopout) { | 
|---|
| 138 | Log.debug("onMessageBeforeSendOrPopout_canceled"); | 
|---|
| 139 | return aStatus; | 
|---|
| 140 |  | 
|---|
| 141 | }, | 
|---|
| 142 | onMessageBeforeSendOrPopout: function _enigmailHook_onMessageBeforeSendOrPopout(aAddress, aEditor, aStatus, aPopout) { | 
|---|
| 143 | Log.debug("onMessageBeforeSendOrPopout"); | 
|---|
| 144 | return aStatus; | 
|---|
| 145 |  | 
|---|
| 146 | }, | 
|---|
| 147 | onMessageBeforeSendOrPopout_early: function _enigmailHook_onMessageBeforeSendOrPopout_early(aAddress, aEditor, aStatus, aPopout) { | 
|---|
| 148 | if (aStatus.canceled) | 
|---|
| 149 | return aStatus; | 
|---|
| 150 |  | 
|---|
| 151 | let toAddrList = aAddress.to.concat(aAddress.cc); | 
|---|
| 152 | Log.debug("onMessageBeforeSendOrPopup_early"); | 
|---|
| 153 |  | 
|---|
| 154 | if (virtualIdInUse) { | 
|---|
| 155 | if (!aPopout) { | 
|---|
| 156 | let recipients = []; | 
|---|
| 157 | var combinedNames = {}; | 
|---|
| 158 | var number; | 
|---|
| 159 | number = HeaderParser.parseHeadersWithArray(toAddrList.join(", "), {}, {}, combinedNames); | 
|---|
| 160 | for (var index = 0; index < number; index++) | 
|---|
| 161 | recipients.push({ | 
|---|
| 162 | recipient: combinedNames.value[index], | 
|---|
| 163 | recipientType: "addr_to" | 
|---|
| 164 | }) | 
|---|
| 165 |  | 
|---|
| 166 | let recentWindow = Cc["@mozilla.org/appshell/window-mediator;1"] | 
|---|
| 167 | .getService(Ci.nsIWindowMediator) | 
|---|
| 168 | .getMostRecentWindow("mail:3pane"); | 
|---|
| 169 |  | 
|---|
| 170 | returnValue = vIaccount_prepareSendMsg(virtualIdInUse, Ci.nsIMsgCompDeliverMode.Now, | 
|---|
| 171 | currentIdentityData, aAddress.params.identity, recipients, recentWindow); | 
|---|
| 172 | Log.debug("returnValue.update:", returnValue.update); | 
|---|
| 173 |  | 
|---|
| 174 | if (returnValue.update == "abort") { | 
|---|
| 175 | aStatus.canceled = true; | 
|---|
| 176 | return aStatus; | 
|---|
| 177 | } else if (returnValue.update == "takeover") { | 
|---|
| 178 | _changeIdentityToSmartIdentity(returnValue.storedIdentity); | 
|---|
| 179 | aStatus.canceled = true; | 
|---|
| 180 | return aStatus; | 
|---|
| 181 | } | 
|---|
| 182 |  | 
|---|
| 183 | aAddress.params.identity = get_vIaccount().defaultIdentity; | 
|---|
| 184 | if (!vIaccount_finalCheck(currentIdentityData, aAddress.params.identity)) { | 
|---|
| 185 | vIaccount_removeUsedVIAccount(); | 
|---|
| 186 | aStatus.canceled = true; | 
|---|
| 187 | return aStatus; | 
|---|
| 188 | } | 
|---|
| 189 | } else { | 
|---|
| 190 | // code virtual Identity into subject - this will be decoded by smartIdentity - newMail | 
|---|
| 191 | aAddress.params.subject = aAddress.params.subject + "\nvirtualIdentityExtension\n" + currentIdSenderName; | 
|---|
| 192 | Log.debug("coding virtualIdentity into subject:", aAddress.params.subject); | 
|---|
| 193 | } | 
|---|
| 194 | } | 
|---|
| 195 | Log.debug("onSendMessage done"); | 
|---|
| 196 | return aStatus; | 
|---|
| 197 | }, | 
|---|
| 198 |  | 
|---|
| 199 | onStopSending: function _virtualIdentityHook_onStopSending(aMsgID, aStatus, aMsg, aReturnFile) { | 
|---|
| 200 | vIaccount_removeUsedVIAccount(); | 
|---|
| 201 | Log.debug("onStopSending done"); | 
|---|
| 202 | }, | 
|---|
| 203 |  | 
|---|
| 204 | onRecipientAdded: function _virtualIdentityHook_onRecipientAdded(aData, aType, aCount) { | 
|---|
| 205 | Log.debug("onRecipientAdded", aData.data, aType, aCount); | 
|---|
| 206 | if (!vIprefs.get("storage")) return; | 
|---|
| 207 | if (aType == "bcc") return; | 
|---|
| 208 | if (aData.data == "") return; | 
|---|
| 209 |  | 
|---|
| 210 | // if we are editing the "cc" or not the first recipient, recognize this. | 
|---|
| 211 | var isNotFirstInputElement = !(aType == "to" && aCount == 0); | 
|---|
| 212 | Log.debug("onRecipientAdded isNotFirstInputElement", isNotFirstInputElement); | 
|---|
| 213 |  | 
|---|
| 214 | if (!_rdfDatasourceAccess) _rdfDatasourceAccess = new rdfDatasourceAccess(); | 
|---|
| 215 | else _rdfDatasourceAccess.clean(); | 
|---|
| 216 |  | 
|---|
| 217 | let recentWindow = Cc["@mozilla.org/appshell/window-mediator;1"] | 
|---|
| 218 | .getService(Ci.nsIWindowMediator) | 
|---|
| 219 | .getMostRecentWindow("mail:3pane"); | 
|---|
| 220 |  | 
|---|
| 221 | var storageResult = _rdfDatasourceAccess.updateVIdentityFromStorage(aData.data, "addr_to", | 
|---|
| 222 | currentIdentityData, virtualIdInUse, isNotFirstInputElement, recentWindow); | 
|---|
| 223 |  | 
|---|
| 224 | if (storageResult.identityCollection.number == 0) return; // return if there was no match | 
|---|
| 225 | if (storageResult.result != "accept") return; // return if we don't like the resulting id | 
|---|
| 226 |  | 
|---|
| 227 | changeIdentityToSmartIdentity(storageResult.identityCollection, 0); | 
|---|
| 228 | } | 
|---|
| 229 | } | 
|---|
| 230 |  | 
|---|
| 231 | try { | 
|---|
| 232 | Cu.import("resource://conversations/modules/hook.js"); | 
|---|
| 233 | mainWindow = Cc["@mozilla.org/appshell/window-mediator;1"] | 
|---|
| 234 | .getService(Ci.nsIWindowMediator) | 
|---|
| 235 | .getMostRecentWindow("mail:3pane"); | 
|---|
| 236 | mainWindow.addEventListener("load", function () { | 
|---|
| 237 | Log.debug("Virtual Identity plugin for Thunderbird Conversations loaded!"); | 
|---|
| 238 | registerHook(virtualIdentityHook); | 
|---|
| 239 | }, false); | 
|---|
| 240 | } catch (e) { | 
|---|
| 241 | Log.debug("virtualIdentity is ready for conversations, but you don't use it"); | 
|---|
| 242 | } | 
|---|