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

source: modules/vI_identityData.js @ 1fb53b

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

removed fcc settings, only available via identity now

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