~ubuntu-branches/ubuntu/quantal/enigmail/quantal-security

« back to all changes in this revision

Viewing changes to mailnews/extensions/enigmail/ui/content/enigmailMsgComposeHelper.js

  • Committer: Package Import Robot
  • Author(s): Chris Coulson
  • Date: 2013-09-13 16:02:15 UTC
  • mfrom: (0.12.16)
  • Revision ID: package-import@ubuntu.com-20130913160215-u3g8nmwa0pdwagwc
Tags: 2:1.5.2-0ubuntu0.12.10.1
* New upstream release v1.5.2 for Thunderbird 24

* Build enigmail using a stripped down Thunderbird 17 build system, as it's
  now quite difficult to build the way we were doing previously, with the
  latest Firefox build system
* Add debian/patches/no_libxpcom.patch - Don't link against libxpcom, as it
  doesn't exist anymore (but exists in the build system)
* Add debian/patches/use_sdk.patch - Use the SDK version of xpt.py and
  friends
* Drop debian/patches/ipc-pipe_rename.diff (not needed anymore)
* Drop debian/patches/makefile_depth.diff (not needed anymore)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ***** BEGIN LICENSE BLOCK *****
 
2
 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
 
3
 *
 
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/
 
8
 *
 
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.
 
13
 *
 
14
 * The Original Code is Enigmail.
 
15
 *
 
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.
 
19
 *
 
20
 * Contributor(s):
 
21
 *
 
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 ***** */
 
34
 
 
35
 
 
36
/**
 
37
 * helper functions for message composition
 
38
 */
 
39
 
 
40
Components.utils.import("resource://enigmail/enigmailCommon.jsm");
 
41
Components.utils.import("resource://enigmail/commonFuncs.jsm");
 
42
 
 
43
if (! Enigmail) var Enigmail = {};
 
44
 
 
45
Enigmail.hlp = {
 
46
 
 
47
  getRecipientsKeys: function (emailAddrs, forceSelection, interactive, matchedKeysObj, flagsObj)
 
48
  {
 
49
    EnigmailCommon.DEBUG_LOG("enigmailMsgComposeHelper.js: getRecipientsKeys: emailAddrs="+emailAddrs+"\n");
 
50
 
 
51
    const nsIEnigmail = Components.interfaces.nsIEnigmail;
 
52
 
 
53
    function getFlagVal(oldVal, node, type, conflictObj) {
 
54
      var newVal = Number(node.getAttribute(type));
 
55
 
 
56
      if ((oldVal==2 && newVal==0) || (oldVal==0 && newVal==2)) {
 
57
        conflictObj[type] = 1;
 
58
      }
 
59
 
 
60
      if (oldVal==0 || newVal==0) {
 
61
        return 0;
 
62
      }
 
63
      else {
 
64
        return (oldVal < newVal ? newVal: oldVal);
 
65
      }
 
66
    }
 
67
 
 
68
    var enigmailSvc = EnigmailCommon.getService();
 
69
    if (!enigmailSvc)
 
70
      return false;
 
71
 
 
72
    flagsObj.value = 0;
 
73
    matchedKeysObj.value = "";
 
74
    var encrypt=1;
 
75
    var sign   =1;
 
76
    var pgpMime=1;
 
77
    var conflicts = { sign: 0, encrypt: 0, pgpMime: 0};
 
78
    var addresses="{"+EnigmailFuncs.stripEmail(emailAddrs.toLowerCase()).replace(/[, ]+/g, "}{")+"}";
 
79
    var keyList=new Array;
 
80
 
 
81
    var rulesListObj= new Object;
 
82
    var foundAddresses="";
 
83
 
 
84
    if (enigmailSvc.getRulesData(rulesListObj)) {
 
85
 
 
86
      var rulesList=rulesListObj.value;
 
87
 
 
88
      if (rulesList.firstChild.nodeName=="parsererror") {
 
89
        EnigmailCommon.alert(window, "Invalid pgprules.xml file:\n"+ rulesList.firstChild.textContent);
 
90
        return true;
 
91
      }
 
92
      EnigmailCommon.DEBUG_LOG("enigmailMsgComposeHelper.js: getRecipientsKeys: keys loaded\n");
 
93
 
 
94
      // go through all rules to find match with email addresses
 
95
      var node=rulesList.firstChild.firstChild;
 
96
      while (node) {
 
97
        if (node.tagName=="pgpRule") {
 
98
          try {
 
99
            var nodeText=node.getAttribute("email");
 
100
            if (nodeText) {
 
101
              var negateRule = false;
 
102
              if (node.getAttribute("negateRule")) {
 
103
                negateRule = Number(node.getAttribute("negateRule"));
 
104
              }
 
105
              if (! negateRule) {
 
106
                // normal rule
 
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);
 
111
                  while (i>=0) {
 
112
                    sign    = getFlagVal(sign,    node, "sign", conflicts);
 
113
                    encrypt = getFlagVal(encrypt, node, "encrypt", conflicts);
 
114
                    pgpMime = getFlagVal(pgpMime, node, "pgpMime", conflicts);
 
115
 
 
116
                    // extract found address
 
117
                    var keyIds=node.getAttribute("keyId");
 
118
 
 
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);
 
122
                    if (keyIds) {
 
123
                      if (keyIds != ".") {
 
124
                        keyList.push(keyIds.replace(/[ ,;]+/g, ", "));
 
125
                      }
 
126
                      addresses=addresses.substring(0,start)+addresses.substring(end);
 
127
                      i=addresses.indexOf(email);
 
128
                    }
 
129
                    else {
 
130
                      var oldMatch=i;
 
131
                      i=addresses.substring(oldMatch+email.length).indexOf(email);
 
132
                      if (i>=0) i+=oldMatch+email.length;
 
133
                    }
 
134
                  }
 
135
                }
 
136
              }
 
137
              else {
 
138
                // "not" rule
 
139
                addrList = addresses.replace(/\}\{/g, "},{").split(/,/);
 
140
                for (i=0; i<addrList.length; i++) {
 
141
                  if (nodeText.toLowerCase().indexOf(addrList[i])>=0) {
 
142
                    i=addrList.length+2;
 
143
                    break;
 
144
                  }
 
145
                }
 
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");
 
152
                  if (keyIds) {
 
153
                    if (keyIds != ".") {
 
154
                      keyList.push(keyIds.replace(/[ ,;]+/g, ", "));
 
155
                    }
 
156
                  }
 
157
                }
 
158
              }
 
159
            }
 
160
         }
 
161
         catch (ex) {}
 
162
        }
 
163
        node = node.nextSibling;
 
164
      }
 
165
    }
 
166
 
 
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+"}";
 
177
            inputObj.options="";
 
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;
 
181
 
 
182
            // create a getAttribute() function for getFlagVal to work normally
 
183
            resultObj.getAttribute = function(attrName) {
 
184
              return this[attrName];
 
185
            };
 
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, "");
 
194
              }
 
195
              else {
 
196
                // no key -> no encryption
 
197
                encrypt=0;
 
198
              }
 
199
            }
 
200
          }
 
201
        }
 
202
      }
 
203
    }
 
204
 
 
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, "");
 
209
    }
 
210
    flagsObj.sign = sign;
 
211
    flagsObj.encrypt = encrypt;
 
212
    flagsObj.pgpMime = pgpMime;
 
213
    flagsObj.value = 1;
 
214
 
 
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);
 
222
      }
 
223
      if (!EnigmailCommon.confirmPref(window, EnigmailCommon.getString("rulesConflict", [ msg ]), "warnOnRulesConflict"))
 
224
        return false;
 
225
    }
 
226
    return true;
 
227
  },
 
228
 
 
229
  // determine invalid recipients returned from GnuPG
 
230
  getInvalidAddress: function (gpgMsg)
 
231
  {
 
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()));
 
238
      }
 
239
    }
 
240
    return invalidAddr.join(" ");
 
241
  }
 
242
};