1
/* ***** BEGIN LICENSE BLOCK *****
2
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
4
* The contents of this file are subject to the Mozilla Public
5
* License Version 1.1 (the "MPL"); you may not use this file
6
* except in compliance with the MPL. You may obtain a copy of
7
* the MPL at http://www.mozilla.org/MPL/
9
* Software distributed under the MPL is distributed on an "AS
10
* IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11
* implied. See the MPL for the specific language governing
12
* rights and limitations under the MPL.
14
* The Original Code is Enigmail.
16
* The Initial Developer of the Original Code is Patrick Brunschwig.
17
* Portions created by Patrick Brunschwig <patrick@mozilla-enigmail.org> are
18
* Copyright (C) 2004 Patrick Brunschwig. All Rights Reserved.
22
* Alternatively, the contents of this file may be used under the terms of
23
* either the GNU General Public License Version 2 or later (the "GPL"), or
24
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25
* in which case the provisions of the GPL or the LGPL are applicable instead
26
* of those above. If you wish to allow use of your version of this file only
27
* under the terms of either the GPL or the LGPL, and not to allow others to
28
* use your version of this file under the terms of the MPL, indicate your
29
* decision by deleting the provisions above and replace them with the notice
30
* and other provisions required by the GPL or the LGPL. If you do not delete
31
* the provisions above, a recipient may use your version of this file under
32
* the terms of any one of the MPL, the GPL or the LGPL.
33
* ***** END LICENSE BLOCK ***** */
37
* helper functions for message composition
40
Components.utils.import("resource://enigmail/enigmailCommon.jsm");
41
Components.utils.import("resource://enigmail/commonFuncs.jsm");
43
if (! Enigmail) var Enigmail = {};
47
getRecipientsKeys: function (emailAddrs, forceSelection, interactive, matchedKeysObj, flagsObj)
49
EnigmailCommon.DEBUG_LOG("enigmailMsgComposeHelper.js: getRecipientsKeys: emailAddrs="+emailAddrs+"\n");
51
const nsIEnigmail = Components.interfaces.nsIEnigmail;
53
function getFlagVal(oldVal, node, type, conflictObj) {
54
var newVal = Number(node.getAttribute(type));
56
if ((oldVal==2 && newVal==0) || (oldVal==0 && newVal==2)) {
57
conflictObj[type] = 1;
60
if (oldVal==0 || newVal==0) {
64
return (oldVal < newVal ? newVal: oldVal);
68
var enigmailSvc = EnigmailCommon.getService();
73
matchedKeysObj.value = "";
77
var conflicts = { sign: 0, encrypt: 0, pgpMime: 0};
78
var addresses="{"+EnigmailFuncs.stripEmail(emailAddrs.toLowerCase()).replace(/[, ]+/g, "}{")+"}";
79
var keyList=new Array;
81
var rulesListObj= new Object;
82
var foundAddresses="";
84
if (enigmailSvc.getRulesData(rulesListObj)) {
86
var rulesList=rulesListObj.value;
88
if (rulesList.firstChild.nodeName=="parsererror") {
89
EnigmailCommon.alert(window, "Invalid pgprules.xml file:\n"+ rulesList.firstChild.textContent);
92
EnigmailCommon.DEBUG_LOG("enigmailMsgComposeHelper.js: getRecipientsKeys: keys loaded\n");
94
// go through all rules to find match with email addresses
95
var node=rulesList.firstChild.firstChild;
97
if (node.tagName=="pgpRule") {
99
var nodeText=node.getAttribute("email");
101
var negateRule = false;
102
if (node.getAttribute("negateRule")) {
103
negateRule = Number(node.getAttribute("negateRule"));
107
addrList=nodeText.toLowerCase().split(/[ ,;]+/);
108
for(var addrIndex=0; addrIndex < addrList.length; addrIndex++) {
109
var email=addrList[addrIndex];
110
var i=addresses.indexOf(email);
112
sign = getFlagVal(sign, node, "sign", conflicts);
113
encrypt = getFlagVal(encrypt, node, "encrypt", conflicts);
114
pgpMime = getFlagVal(pgpMime, node, "pgpMime", conflicts);
116
// extract found address
117
var keyIds=node.getAttribute("keyId");
119
var start=addresses.substring(0,i+email.length).lastIndexOf("{");
120
var end=start+addresses.substring(start).indexOf("}")+1;
121
foundAddresses+=addresses.substring(start,end);
124
keyList.push(keyIds.replace(/[ ,;]+/g, ", "));
126
addresses=addresses.substring(0,start)+addresses.substring(end);
127
i=addresses.indexOf(email);
131
i=addresses.substring(oldMatch+email.length).indexOf(email);
132
if (i>=0) i+=oldMatch+email.length;
139
addrList = addresses.replace(/\}\{/g, "},{").split(/,/);
140
for (i=0; i<addrList.length; i++) {
141
if (nodeText.toLowerCase().indexOf(addrList[i])>=0) {
146
if (i==addrList.length) {
147
// no matching address; apply rule
148
sign = getFlagVal(sign, node, "sign", conflicts);
149
encrypt = getFlagVal(encrypt, node, "encrypt", conflicts);
150
pgpMime = getFlagVal(pgpMime, node, "pgpMime", conflicts);
151
keyIds=node.getAttribute("keyId");
154
keyList.push(keyIds.replace(/[ ,;]+/g, ", "));
163
node = node.nextSibling;
167
if (interactive && (EnigmailCommon.getPref("recipientsSelection")==1 || forceSelection)) {
168
var addrList=emailAddrs.split(/,/);
169
var inputObj=new Object;
170
var resultObj=new Object;
171
for (i=0; i<addrList.length; i++) {
172
if (addrList[i].length>0) {
173
var theAddr=EnigmailFuncs.stripEmail(addrList[i]).toLowerCase();
174
if ((foundAddresses.indexOf("{"+theAddr+"}")==-1) &&
175
(! (theAddr.indexOf("0x")==0 && theAddr.indexOf("@")==-1))) {
176
inputObj.toAddress="{"+theAddr+"}";
178
inputObj.command = "add";
179
window.openDialog("chrome://enigmail/content/enigmailSingleRcptSettings.xul","", "dialog,modal,centerscreen,resizable", inputObj, resultObj);
180
if (resultObj.cancelled==true) return false;
182
// create a getAttribute() function for getFlagVal to work normally
183
resultObj.getAttribute = function(attrName) {
184
return this[attrName];
186
if (!resultObj.negate) {
187
sign =getFlagVal(sign, resultObj, "sign", conflicts);
188
encrypt=getFlagVal(encrypt, resultObj, "encrypt", conflicts);
189
pgpMime=getFlagVal(pgpMime, resultObj, "pgpMime", conflicts);
190
if (resultObj.keyId.length>0) {
191
keyList.push(resultObj.keyId);
192
var replaceAddr=new RegExp("{"+addrList[i]+"}", "g");
193
addresses=addresses.replace(replaceAddr, "");
196
// no key -> no encryption
205
if (keyList.length>0) {
206
// sort key list and make it unique?
207
matchedKeysObj.value = keyList.join(", ");
208
matchedKeysObj.value += addresses.replace(/\{/g, ", ").replace(/\}/g, "");
210
flagsObj.sign = sign;
211
flagsObj.encrypt = encrypt;
212
flagsObj.pgpMime = pgpMime;
215
if (interactive && (!EnigmailCommon.getPref("confirmBeforeSend")) && (conflicts.encrypt ||conflicts.sign)) {
216
if (sign<2) sign = (sign & (Enigmail.msg.sendMode & nsIEnigmail.SEND_SIGNED));
217
if (encrypt<2) encrypt = (encrypt & (Enigmail.msg.sendMode & nsIEnigmail.SEND_ENCRYPTED ? 1 : 0));
218
var msg = "\n"+"- " + EnigmailCommon.getString(sign>0 ? "signYes" : "signNo");
219
msg += "\n"+"- " + EnigmailCommon.getString(encrypt>0 ? "encryptYes" : "encryptNo");
220
if (EnigmailCommon.getPref("warnOnRulesConflict")==2) {
221
EnigmailCommon.setPref("warnOnRulesConflict", 0);
223
if (!EnigmailCommon.confirmPref(window, EnigmailCommon.getString("rulesConflict", [ msg ]), "warnOnRulesConflict"))
229
// determine invalid recipients returned from GnuPG
230
getInvalidAddress: function (gpgMsg)
232
var invalidAddr = [];
233
var lines = gpgMsg.split(/[\n\r]+/);
234
for (var i=0; i < lines.length; i++) {
235
var m = lines[i].match(/^(INV_RECP \d+ )(.*)$/);
236
if (m && m.length == 3) {
237
invalidAddr.push(EnigmailFuncs.stripEmail(m[2].toLowerCase()));
240
return invalidAddr.join(" ");