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

source: modules/vI_identityData.js @ 25edf9

ng_0.9
Last change on this file since 25edf9 was 25edf9, checked in by rene <rene@…>, 8 years ago

decreased debugging

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