This is just some static backup of the original site, don't expect every link to work!

source: modules/vI_identityData.js @ 85fa10

ng_0.9
Last change on this file since 85fa10 was 85fa10, checked in by rene <rene@…>, 4 years ago

removed everything related to smtp-storage

  • Property mode set to 100644
File size: 18.1 KB
Line 
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
25var EXPORTED_SYMBOLS = ["identityCollection", "identityData", "identityDataExtras", "DEFAULT_SMTP_TAG", "NO_SMTP_TAG"]
26
27const DEFAULT_SMTP_TAG = "vI_useDefaultSMTP"
28const NO_SMTP_TAG = "vI_noStoredSMTP"
29
30Components.utils.import("resource://v_identity/vI_log.js");
31let Log = setupLogging("virtualIdentity.identityData");
32Components.utils.import("resource://v_identity/vI_prefs.js");
33Components.utils.import("resource://v_identity/vI_accountUtils.js");
34
35Components.utils.import("resource://v_identity/vI_identityDataExtras.js");
36Components.utils.import("resource://v_identity/identityDataExtras/returnReceipt.js");
37Components.utils.import("resource://v_identity/identityDataExtras/fccSwitch.js");
38Components.utils.import("resource://v_identity/identityDataExtras/messageFormat.js");
39Components.utils.import("resource://v_identity/identityDataExtras/characterEncoding.js");
40Components.utils.import("resource://v_identity/identityDataExtras/sMimeEncryption.js");
41Components.utils.import("resource://v_identity/identityDataExtras/sMimeSignature.js");
42Components.utils.import("resource://v_identity/identityDataExtras/PGPEncryption.js");
43Components.utils.import("resource://v_identity/identityDataExtras/PGPSignature.js");
44Components.utils.import("resource://v_identity/identityDataExtras/PGPMIME.js");
45
46ChromeUtils.import("resource:///modules/mailServices.js");
47
48function identityData(currentWindow, email, fullName, id, extras, sideDescription, existingID) {
49  this._currentWindow = currentWindow;
50  this._email = email ? email : "";
51  this._emailParsed = false;
52  this._fullName = fullName ? fullName : "";
53  this.id = new idObj(id);
54  if (extras) this.extras = extras;
55  else this.extras = new identityDataExtras(currentWindow);
56  this.comp = { // holds the results of the last comparison for later creation of a compareMatrix
57    compareID: null,
58    equals: {
59      fullName: {},
60      email: {},
61      id: {},
62      extras: {}
63    }
64  }
65  if (sideDescription) this.sideDescription = sideDescription;
66  if (existingID) this.existingID = existingID;
67  else if (this.id.value) this.sideDescription = " - " + this.id.value;
68  this.stringBundle = Components.classes["@mozilla.org/intl/stringbundle;1"]
69    .getService(Components.interfaces.nsIStringBundleService)
70    .createBundle("chrome://v_identity/locale/v_identity.properties");
71}
72identityData.prototype = {
73  _email: null, // internal email-field might contain combinedName (until first queried via email)
74  _fullName: null,
75  _emailParsed: null,
76  _currentWindow: null,
77  id: null,
78  extras: null,
79  sideDescription: null,
80  existingID: null, // indicates that this is a pre-defined Identity, which might handled slightly differently
81
82  stringBundle: null,
83  comp: null,
84
85  parseEmail: function () {
86    if (this._emailParsed) return;
87    // parse email and move any additional parts to fullName
88    if (this._email.match(/<\s*[^>\s]*@[^>\s]*\s*>/) || this._email.match(/<?\s*[^>\s]*@[^>\s]*\s*>?/) || this._email.match(/$/)) {
89      this._fullName += RegExp.leftContext + RegExp.rightContext;
90      this._email = RegExp.lastMatch;
91      //            Log.debug("parseEmail _fullName = '" + this._fullName + "'");
92      //            Log.debug("parseEmail _email =    '" + this._email + "'");
93    }
94    this._emailParsed = true;
95  },
96  get email() {
97    this.parseEmail();
98    return (this._email ? this._email.replace(/\s+|<|>/g, "") : "");
99  },
100  set email(email) {
101    this._email = email;
102    this._emailParsed = false;
103  },
104
105  cleanName: function (fullName) {
106    //      Log.debug("cleanName init '" + fullName + "'");
107    var _fullName = fullName.replace(/^\s+|\s+$/g, "");
108    if (_fullName.search(/^\".+\"$|^'.+'$/g) != -1) {
109      _fullName = this.cleanName(_fullName.replace(/^\"(.+)\"$|^'(.+)'$/g, "$1$2"));
110    }
111    //      Log.debug("cleanName done '" + _fullName + "'");
112    return _fullName;
113  },
114
115  get fullName() {
116    this.parseEmail();
117    return (this._fullName ? this.cleanName(this._fullName) : "")
118  },
119  set fullName(fullName) {
120    this._fullName = fullName;
121  },
122
123  get combinedName() {
124    var fullName = this.fullName;
125    var email = this.email;
126    return fullName ? fullName + (email ? " <" + email + ">" : "") : email
127  },
128  set combinedName(combinedName) {
129    this._email = combinedName;
130    this._fullName = "";
131    this._emailParsed = false;
132  },
133
134  __makeHtml: function (string) {
135    return string ? string.replace(/>/g, "&gt;").replace(/</g, "&lt;") : ""
136  },
137  get idHtml() {
138    return this.__makeHtml(this.id.value);
139  },
140  get fullNameHtml() {
141    return this.__makeHtml(this.fullName);
142  },
143  get emailHtml() {
144    return this.__makeHtml(this.email);
145  },
146  get combinedNameHtml() {
147    return this.__makeHtml(this.combinedName);
148  },
149
150  get idLabel() {
151    return this.stringBundle.GetStringFromName("vident.identityData.baseID")
152  },
153  get fullNameLabel() {
154    return this.stringBundle.GetStringFromName("vident.identityData.Name")
155  },
156  get emailLabel() {
157    return this.stringBundle.GetStringFromName("vident.identityData.Address")
158  },
159
160  // creates an Duplicate of the current IdentityData, cause usually we are working with a pointer
161  getDuplicate: function () {
162    return new identityData(this._currentWindow, this.email, this.fullName, this.id.key, this.extras ? this.extras.getDuplicate() : null,
163      this.sideDescription, this.existingID);
164  },
165 
166  takeOverAvailableData: function(identityData) {
167    if (identityData.email)
168      this.email = identityData.email;
169    if (identityData.fullName)
170      this.fullName = identityData.fullName;
171    if (identityData.id.key)
172      this.id.key = identityData.id.key;
173    if (identityData.sideDescription)
174      this.sideDescription = identityData.sideDescription;
175    if (identityData.extras)
176      this.extras.copy(identityData.extras);
177  },
178
179  // copys all values of an identity. This way we can create a new object with a different document-context
180  copy: function (identityData) {
181    this.email = identityData.email;
182    this.fullName = identityData.fullName;
183    this.id.key = identityData.id.key;
184    this.sideDescription = identityData.sideDescription;
185    if (this.extras) this.extras.copy(identityData.extras);
186    // don't copy the currentWindow value
187  },
188
189  // dependent on MsgComposeCommands, should/will only be called in ComposeDialog
190  isExistingIdentity: function (ignoreFullNameWhileComparing) {
191    Log.debug("isExistingIdentity: ignoreFullNameWhileComparing='" + ignoreFullNameWhileComparing + "'");
192    //      Log.debug("base: fullName.toLowerCase()='" + this.fullName + "' email.toLowerCase()='" + this.email + "'");
193
194    var ignoreFullNameMatchKey = null;
195    var accounts = getAccountsArray();
196    for (let acc = 0; acc < accounts.length; acc++) {
197      let account = accounts[acc];
198      try {
199        prefroot.getBoolPref("mail.account." + account.key + ".vIdentity");
200        continue;
201      } catch (e) {};
202      let identities = getIdentitiesArray(account);
203      for (let i = 0; i < identities.length; i++) {
204        let identity = identities[i];
205        //              Log.debug("comp: fullName.toLowerCase()='" + identity.fullName.toLowerCase() + "' email.toLowerCase()='" + identity.email.toLowerCase() + "'");
206        var email = this.email ? this.email : ""; // might be null if no identity is set
207        var idEmail = identity.email ? identity.email : ""; // might be null if no identity is set
208        if (email.toLowerCase() == idEmail.toLowerCase()) {
209          // if fullName matches, than this is a final match
210          if (this.fullName.toLowerCase() == identity.fullName.toLowerCase()) {
211            Log.debug("isExistingIdentity: " + this.combinedName + " found, id='" + identity.key + "'");
212            return identity.key; // return key and stop searching
213          }
214          // if fullNames don't match, remember the key but continue to search for full match
215          else if (!ignoreFullNameMatchKey) ignoreFullNameMatchKey = identity.key;
216        }
217      }
218    }
219
220    if (ignoreFullNameWhileComparing && ignoreFullNameMatchKey) {
221      Log.debug("isExistingIdentity: " + this.combinedName + " found, id='" + ignoreFullNameMatchKey + "'");
222      return ignoreFullNameMatchKey;
223    }
224
225    Log.debug("isExistingIdentity: " + this.combinedName + " not found");
226    return null;
227  },
228
229  // dependent on MsgComposeCommands, should/will only be called in ComposeDialog
230  hasMatchingDomainIdentity: function () {
231    Log.debug("hasMatchingDomainIdentity");
232
233    var domainArray = this.email.match(/@[^@]+$/);
234    if (!domainArray) {
235      Log.debug("hasMatchingDomainIdentity found no domain for email " + this.email);
236      return;
237    }
238    Log.debug("hasMatchingDomainIdentity searching for domain " + domainArray[0]);
239
240    var accounts = getAccountsArray();
241    for (let acc = 0; acc < accounts.length; acc++) {
242      let account = accounts[acc];
243      try {
244        prefroot.getBoolPref("mail.account." + account.key + ".vIdentity");
245        continue;
246      } catch (e) {};
247      let identities = getIdentitiesArray(account);
248      for (let i = 0; i < identities.length; i++) {
249        let identity = identities[i];
250        var idDomainArray = identity.email.match(/@[^@]+$/);
251        if (!idDomainArray) continue;
252        //                 Log.debug("comp: domain.toLowerCase()='" + domainArray[0].toLowerCase() + "' idDomain.toLowerCase()='" + idDomainArray[0].toLowerCase() + "'");
253        if (domainArray[0].toLowerCase() == idDomainArray[0].toLowerCase()) {
254          // if domain matches, everything is perfect!
255          Log.debug("hasMatchingDomainIdentity: found matching id for domain '" + domainArray[0] + "'");
256          return identity.key; // return key and stop searching
257        }
258      }
259    }
260    Log.debug("hasMatchingDomainIdentity: '" + domainArray[0] + "' not found");
261    return null;
262  },
263
264  equals: function (compareIdentityData) {
265    if (!compareIdentityData)
266      return false;
267   
268    this.comp.compareID = compareIdentityData;
269
270    this.comp.equals.fullName = (((this.fullName) ? this.fullName.toLowerCase() : null) == ((compareIdentityData.fullName) ? compareIdentityData.fullName.toLowerCase() : null));
271    if (!this.comp.equals.fullName) {
272      //       Log.debug("fullName not equal ('" + ((this.fullName)?this.fullName.toLowerCase():null) + "' != '" + ((compareIdentityData.fullName)?compareIdentityData.fullName.toLowerCase():null) + "')");
273    }
274    this.comp.equals.email = (((this.email) ? this.email.toLowerCase() : null) == ((compareIdentityData.email) ? compareIdentityData.email.toLowerCase() : null));
275    if (!this.comp.equals.email) {
276      //       Log.debug("email not equal ('" + ((this.email)?this.email.toLowerCase():null) + "' != '" + ((compareIdentityData.email)?compareIdentityData.email.toLowerCase():null) + "')");
277    }
278
279    this.comp.equals.id = this.id.equal(compareIdentityData.id);
280    this.comp.equals.extras = this.extras ? this.extras.equal(compareIdentityData.extras) : true;
281
282    return (this.comp.equals.fullName && this.comp.equals.email && this.comp.equals.id && this.comp.equals.extras);
283  },
284
285  equalsIdentity: function (compareIdentityData, getCompareMatrix) {
286    var equal = this.equals(compareIdentityData);
287    var compareMatrix = null;
288    // generate CompareMatrix only if asked and non-equal
289    if (getCompareMatrix && !equal) compareMatrix = this.getCompareMatrix();
290    return {
291      equal: equal,
292      compareMatrix: compareMatrix
293    };
294  },
295
296  getCompareMatrix: function () {
297    const Items = Array("fullName", "email", "id");
298    var string = "";
299    var saveBaseId = vIprefs.get("storage_store_base_id");
300    for (let item of Items) {
301      var classEqual = (this.comp.equals[item]) ? "equal" : "unequal";
302      var classIgnore = ((!saveBaseId) && (item == "id")) ? " ignoreValues" : ""
303      string += "<tr>" +
304        "<td class='col1 " + classEqual + "'>" + this[item + "Label"] + "</td>" +
305        "<td class='col2 " + classEqual + classIgnore + "'>" + this.comp.compareID[item + "Html"] + "</td>" +
306        "<td class='col3 " + classEqual + classIgnore + "'>" + this[item + "Html"] + "</td>" +
307        "</tr>"
308    }
309    string += this.extras ? this.extras.getCompareMatrix() : "";
310    return string;
311  },
312
313  getMatrix: function () {
314    var string = "";
315    if (this["idHtml"])
316        string = "<tr><td class='col1'>" + this["idLabel"] + ":</td>" +
317        "<td class='col2'>" + this["idHtml"] + "</td></tr>"
318    string += this.extras ? this.extras.getMatrix() : "";
319    return string;
320  }
321}
322
323function identityCollection() {
324  this.number = 0;
325  this.identityDataCollection = {};
326  this.menuItems = {};
327}
328identityCollection.prototype = {
329  number: null,
330  identityDataCollection: null,
331  menuItems: null,
332
333  mergeWithoutDuplicates: function (addIdentityCollection) {
334    for (var index = 0; index < addIdentityCollection.number; index++)
335      this.addWithoutDuplicates(addIdentityCollection.identityDataCollection[index])
336  },
337
338  dropIdentity: function (index) {
339    Log.debug("dropping address from inputList: " + this.identityDataCollection[index].combinedName);
340    while (index < (this.number - 1)) {
341      this.identityDataCollection[index] = this.identityDataCollection[++index];
342    };
343    this.identityDataCollection[--this.number] = null;
344  },
345
346  addWithoutDuplicates: function (identityData) {
347    if (!identityData) return;
348    for (var index = 0; index < this.number; index++) {
349      if (this.identityDataCollection[index].email == identityData.email &&
350        (!this.identityDataCollection[index].id.key || !identityData.id.key ||
351          this.identityDataCollection[index].id.key == identityData.id.key)) {
352        // found, so check if we can use the Name of the new field
353        if (this.identityDataCollection[index].fullName == "" && identityData.fullName != "") {
354          this.identityDataCollection[index].fullName = identityData.fullName;
355          Log.debug("added fullName '" + identityData.fullName + "' to stored email '" + this.identityDataCollection[index].email + "'")
356        }
357        // check if id_key or extras can be used
358        // only try this once, for the first Identity where id is set)
359        if (!this.identityDataCollection[index].id.key && identityData.id.key) {
360          this.identityDataCollection[index].id.key = identityData.id.key;
361          this.identityDataCollection[index].extras = identityData.extras;
362          Log.debug("added id '" + identityData.id.value + "' (+extras) to stored email '" + this.identityDataCollection[index].email + "'")
363        }
364        return;
365      }
366    }
367    Log.debug("add new address to result: " + identityData.combinedName)
368    this.identityDataCollection[index] = identityData;
369    this.number = index + 1;
370  },
371
372  // this is used to completely use the conten of another identityCollection, but without changing all pointers
373  // see for instance vI.smartIdentity.__filterAddresses
374  takeOver: function (newIdentityCollection) {
375    this.number = newIdentityCollection.number
376    this.identityDataCollection = newIdentityCollection.identityDataCollection
377  }
378};
379
380function idObj(key) {
381  this._key = key;
382}
383idObj.prototype = {
384  _key: null,
385  _value: null,
386  _accountkey: null,
387
388  set key(key) {
389    this._key = key;
390    this._value = null;
391  },
392  get key() {
393    if (this._value == null) var dummy = this.value;
394    return this._key
395  },
396  get accountkey() {
397    if (this._value == null) var dummy = this.value;
398    return this._accountkey
399  },
400  get value() {
401    if (this._value == null) {
402      this._value = "";
403      // if this worked we are having at least seamonkey 1.17
404      let accounts = getAccountsArray();
405      for (let acc = 0; acc < accounts.length; acc++) {
406        let account = accounts[acc];
407        let identities = getIdentitiesArray(account);
408        if (identities.length == 0)
409          continue;
410        for (let i = 0; i < identities.length; i++) {
411          let identity = identities[i];
412          if (this._key == identity.key) {
413            this._value = identity.identityName;
414            this._accountkey = account.key;
415            break;
416          }
417        }
418      }
419      if (!this._value) {
420        this._key = null;
421        this._accountkey = null;
422      }
423    }
424    return this._value;
425  },
426
427  get smtpServerKey() {
428    if (!this.key)
429      return null;
430    var AccountManager = Components.classes["@mozilla.org/messenger/account-manager;1"]
431          .getService(Components.interfaces.nsIMsgAccountManager);
432
433    var identity = AccountManager.getIdentity(this.key);
434    if (identity) {
435      if (identity.smtpServerKey)
436        return identity.smtpServerKey;
437      else
438        return MailServices.smtp.defaultServer.key
439    }
440    return null;
441  },
442 
443  get smtpServerName() {
444    if (!this.smtpServerKey)
445      return null;
446    var servers = MailServices.smtp.servers;
447
448    var smtpName;
449    while (servers && servers.hasMoreElements()) {
450      var server = servers.getNext();
451      if (server instanceof Components.interfaces.nsISmtpServer &&
452        !server.redirectorType && this.smtpServerKey == server.key) {
453        smtpName = server.description ? server.description : server.hostname;
454        break;
455      }
456    }
457    return smtpName;
458  },
459 
460  equal: function (compareIdObj) {
461    if (!this.key || !compareIdObj.key) return true;
462    if (this.key != compareIdObj.key) {
463      //       Log.debug("id not equal ('" + this.key + "' != '" + compareIdObj.key + "')");
464    }
465    return (this.key == compareIdObj.key);
466  }
467}
Note: See TracBrowser for help on using the repository browser.