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 | |
---|
25 | var EXPORTED_SYMBOLS = ["identityCollection", "identityData", "identityDataExtras", "DEFAULT_SMTP_TAG", "NO_SMTP_TAG"] |
---|
26 | |
---|
27 | const DEFAULT_SMTP_TAG = "vI_useDefaultSMTP" |
---|
28 | const NO_SMTP_TAG = "vI_noStoredSMTP" |
---|
29 | |
---|
30 | Components.utils.import("resource://v_identity/vI_log.js"); |
---|
31 | let Log = setupLogging("virtualIdentity.identityData"); |
---|
32 | Components.utils.import("resource://v_identity/vI_prefs.js"); |
---|
33 | Components.utils.import("resource://v_identity/vI_accountUtils.js"); |
---|
34 | |
---|
35 | Components.utils.import("resource://v_identity/vI_identityDataExtras.js"); |
---|
36 | Components.utils.import("resource://v_identity/identityDataExtras/returnReceipt.js"); |
---|
37 | Components.utils.import("resource://v_identity/identityDataExtras/fccSwitch.js"); |
---|
38 | Components.utils.import("resource://v_identity/identityDataExtras/messageFormat.js"); |
---|
39 | Components.utils.import("resource://v_identity/identityDataExtras/characterEncoding.js"); |
---|
40 | Components.utils.import("resource://v_identity/identityDataExtras/sMimeEncryption.js"); |
---|
41 | Components.utils.import("resource://v_identity/identityDataExtras/sMimeSignature.js"); |
---|
42 | Components.utils.import("resource://v_identity/identityDataExtras/PGPEncryption.js"); |
---|
43 | Components.utils.import("resource://v_identity/identityDataExtras/PGPSignature.js"); |
---|
44 | Components.utils.import("resource://v_identity/identityDataExtras/PGPMIME.js"); |
---|
45 | |
---|
46 | function 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 | } |
---|
65 | identityData.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,">").replace(/</g,"<"):"" }, |
---|
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 | equals : function(compareIdentityData) { |
---|
183 | this.comp.compareID = compareIdentityData; |
---|
184 | |
---|
185 | this.comp.equals.fullName = (((this.fullName)?this.fullName.toLowerCase():null) == ((compareIdentityData.fullName)?compareIdentityData.fullName.toLowerCase():null)); |
---|
186 | if (!this.comp.equals.fullName) { |
---|
187 | Log.debug("fullName not equal ('" + ((this.fullName)?this.fullName.toLowerCase():null) + "' != '" + ((compareIdentityData.fullName)?compareIdentityData.fullName.toLowerCase():null) + "')"); |
---|
188 | } |
---|
189 | this.comp.equals.email = (((this.email)?this.email.toLowerCase():null) == ((compareIdentityData.email)?compareIdentityData.email.toLowerCase():null)); |
---|
190 | if (!this.comp.equals.email) { |
---|
191 | Log.debug("email not equal ('" + ((this.email)?this.email.toLowerCase():null) + "' != '" + ((compareIdentityData.email)?compareIdentityData.email.toLowerCase():null) + "')"); |
---|
192 | } |
---|
193 | |
---|
194 | this.comp.equals.smtp = this.smtp.equal(compareIdentityData.smtp); |
---|
195 | this.comp.equals.id = this.id.equal(compareIdentityData.id); |
---|
196 | this.comp.equals.extras = this.extras?this.extras.equal(compareIdentityData.extras):true; |
---|
197 | |
---|
198 | return (this.comp.equals.fullName && this.comp.equals.email && this.comp.equals.smtp && this.comp.equals.id && this.comp.equals.extras); |
---|
199 | }, |
---|
200 | |
---|
201 | equalsIdentity : function(compareIdentityData, getCompareMatrix) { |
---|
202 | var equal = this.equals(compareIdentityData); |
---|
203 | var compareMatrix = null; |
---|
204 | // generate CompareMatrix only if asked and non-equal |
---|
205 | if (getCompareMatrix && !equal) compareMatrix = this.getCompareMatrix(); |
---|
206 | return { equal : equal, compareMatrix : compareMatrix }; |
---|
207 | }, |
---|
208 | |
---|
209 | getCompareMatrix : function() { |
---|
210 | const Items = Array("fullName", "email", "smtp", "id"); |
---|
211 | var string = ""; |
---|
212 | var saveBaseId = vIprefs.get("storage_store_base_id"); |
---|
213 | var saveSMTP = vIprefs.get("storage_store_SMTP"); |
---|
214 | for each (let item in Items) { |
---|
215 | var classEqual = (this.comp.equals[item])?"equal":"unequal"; |
---|
216 | var classIgnore = (((!saveBaseId) && (item == "id")) || ((!saveSMTP) && (item == "smtp")))?" ignoreValues":"" |
---|
217 | string += "<tr>" + |
---|
218 | "<td class='col1 " + classEqual + "'>" + this[item+"Label"] + "</td>" + |
---|
219 | "<td class='col2 " + classEqual + classIgnore + "'>" + this.comp.compareID[item+"Html"] + "</td>" + |
---|
220 | "<td class='col3 " + classEqual + classIgnore + "'>" + this[item+"Html"] + "</td>" + |
---|
221 | "</tr>" |
---|
222 | } |
---|
223 | string += this.extras?this.extras.getCompareMatrix():""; |
---|
224 | return string; |
---|
225 | }, |
---|
226 | |
---|
227 | getMatrix : function() { |
---|
228 | const Items = Array("smtp", "id"); |
---|
229 | var string = ""; |
---|
230 | for each (var item in Items) if (this[item+"Html"]) |
---|
231 | string += "<tr><td class='col1'>" + this[item+"Label"] + ":</td>" + |
---|
232 | "<td class='col2'>" + this[item+"Html"] + "</td></tr>" |
---|
233 | string += this.extras?this.extras.getMatrix():""; |
---|
234 | return string; |
---|
235 | } |
---|
236 | } |
---|
237 | |
---|
238 | function identityCollection() { |
---|
239 | this.number = 0; |
---|
240 | this.identityDataCollection = {}; |
---|
241 | this.menuItems = {}; |
---|
242 | } |
---|
243 | identityCollection.prototype = |
---|
244 | { |
---|
245 | number : null, |
---|
246 | identityDataCollection : null, |
---|
247 | menuItems : null, |
---|
248 | |
---|
249 | mergeWithoutDuplicates : function(addIdentityCollection) { |
---|
250 | for (var index = 0; index < addIdentityCollection.number; index++) |
---|
251 | this.addWithoutDuplicates(addIdentityCollection.identityDataCollection[index]) |
---|
252 | }, |
---|
253 | |
---|
254 | dropIdentity : function(index) { |
---|
255 | Log.debug("dropping address from inputList: " + this.identityDataCollection[index].combinedName); |
---|
256 | while (index < (this.number - 1)) { this.identityDataCollection[index] = this.identityDataCollection[++index]; }; |
---|
257 | this.identityDataCollection[--this.number] = null; |
---|
258 | }, |
---|
259 | |
---|
260 | addWithoutDuplicates : function(identityData) { |
---|
261 | if (!identityData) return; |
---|
262 | for (var index = 0; index < this.number; index++) { |
---|
263 | if (this.identityDataCollection[index].email == identityData.email && |
---|
264 | (!this.identityDataCollection[index].id.key || !identityData.id.key || |
---|
265 | (this.identityDataCollection[index].id.key == identityData.id.key && |
---|
266 | this.identityDataCollection[index].smtp.key == identityData.smtp.key))) { |
---|
267 | // found, so check if we can use the Name of the new field |
---|
268 | if (this.identityDataCollection[index].fullName == "" && identityData.fullName != "") { |
---|
269 | this.identityDataCollection[index].fullName = identityData.fullName; |
---|
270 | Log.debug("added fullName '" + identityData.fullName |
---|
271 | + "' to stored email '" + this.identityDataCollection[index].email + "'") |
---|
272 | } |
---|
273 | // check if id_key, smtp_key or extras can be used |
---|
274 | // only try this once, for the first Identity where id is set) |
---|
275 | if (!this.identityDataCollection[index].id.key && identityData.id.key) { |
---|
276 | this.identityDataCollection[index].id.key = identityData.id.key; |
---|
277 | this.identityDataCollection[index].smtp.key = identityData.smtp.key; |
---|
278 | this.identityDataCollection[index].extras = identityData.extras; |
---|
279 | Log.debug("added id '" + identityData.id.value |
---|
280 | + "' smtp '" + identityData.smtp.value + "' (+extras) to stored email '" + this.identityDataCollection[index].email + "'") |
---|
281 | } |
---|
282 | return; |
---|
283 | } |
---|
284 | } |
---|
285 | Log.debug("add new address to result: " + identityData.combinedName) |
---|
286 | this.identityDataCollection[index] = identityData; |
---|
287 | this.number = index + 1; |
---|
288 | }, |
---|
289 | |
---|
290 | // this is used to completely use the conten of another identityCollection, but without changing all pointers |
---|
291 | // see for instance vI.smartIdentity.__filterAddresses |
---|
292 | takeOver : function(newIdentityCollection) { |
---|
293 | this.number = newIdentityCollection.number |
---|
294 | this.identityDataCollection = newIdentityCollection.identityDataCollection |
---|
295 | } |
---|
296 | }; |
---|
297 | |
---|
298 | function smtpObj(key) { |
---|
299 | this._key = key; |
---|
300 | this.DEFAULT_TAG = Components.classes["@mozilla.org/intl/stringbundle;1"] |
---|
301 | .getService(Components.interfaces.nsIStringBundleService) |
---|
302 | .createBundle("chrome://messenger/locale/messenger.properties"). |
---|
303 | GetStringFromName("defaultServerTag"); |
---|
304 | } |
---|
305 | smtpObj.prototype = { |
---|
306 | DEFAULT_TAG : null, |
---|
307 | _key : null, |
---|
308 | _value : null, |
---|
309 | |
---|
310 | set key(key) { this._key = key; this._value = null; }, |
---|
311 | get key() { |
---|
312 | var dummy = this.value; // just to be sure key is adapted if SMTP is not available |
---|
313 | return this._key |
---|
314 | }, |
---|
315 | get keyNice() { // the same as key but with "" for DEFAULT_SMTP_TAG |
---|
316 | if (this.key == DEFAULT_SMTP_TAG) return ""; // this is the key used for default server |
---|
317 | return this.key |
---|
318 | }, |
---|
319 | get value() { |
---|
320 | if (this._value == null) { |
---|
321 | this._value = ""; |
---|
322 | if (this._key == null || this._key == "") this._key = DEFAULT_SMTP_TAG; |
---|
323 | if (this._key == DEFAULT_SMTP_TAG) this._value = this.DEFAULT_TAG; |
---|
324 | else if (!this._key) this._value = null; |
---|
325 | else if (this._key) { |
---|
326 | var servers, smtpService = Components.classes["@mozilla.org/messengercompose/smtp;1"] |
---|
327 | .getService(Components.interfaces.nsISmtpService); |
---|
328 | // check for new https://hg.mozilla.org/comm-central/rev/fab9e5145cd4 smtpService |
---|
329 | if (typeof(smtpService.servers) == "object") servers = smtpService.servers; |
---|
330 | else servers = smtpService.smtpServers; |
---|
331 | |
---|
332 | while (servers && servers.hasMoreElements()) { |
---|
333 | var server = servers.getNext(); |
---|
334 | if (server instanceof Components.interfaces.nsISmtpServer && |
---|
335 | !server.redirectorType && this._key == server.key) { |
---|
336 | this._value = server.description?server.description:server.hostname; |
---|
337 | break; |
---|
338 | } |
---|
339 | } |
---|
340 | } |
---|
341 | } |
---|
342 | if (!this._value) this._key = NO_SMTP_TAG; // if non-existant SMTP handle like non available |
---|
343 | return this._value; |
---|
344 | }, |
---|
345 | equal : function(compareSmtpObj) { |
---|
346 | if (this.key == NO_SMTP_TAG || compareSmtpObj.key == NO_SMTP_TAG) return true; |
---|
347 | if (this.keyNice != compareSmtpObj.keyNice) { |
---|
348 | Log.debug("smtp not equal ('" + this.keyNice + "' != '" + compareSmtpObj.keyNice + "')"); |
---|
349 | } |
---|
350 | return (this.keyNice == compareSmtpObj.keyNice); |
---|
351 | }, |
---|
352 | hasNoDefinedSMTP : function() { |
---|
353 | return (this.key == NO_SMTP_TAG); |
---|
354 | } |
---|
355 | } |
---|
356 | |
---|
357 | function idObj(key) { this._key = key; } |
---|
358 | idObj.prototype = { |
---|
359 | _key : null, |
---|
360 | _value : null, |
---|
361 | |
---|
362 | set key(key) { this._key = key; this._value = null; }, |
---|
363 | get key() { if (this._value == null) var dummy = this.value; return this._key }, |
---|
364 | get value() { |
---|
365 | if (this._value == null) { |
---|
366 | this._value = ""; |
---|
367 | // if this worked we are having at least seamonkey 1.17 |
---|
368 | let accounts = getAccountsArray(); |
---|
369 | for (let acc = 0; acc < accounts.length; acc++) { |
---|
370 | let account = accounts[acc]; |
---|
371 | let identities = getIdentitiesArray(account); |
---|
372 | if (identities.length == 0) |
---|
373 | continue; |
---|
374 | for (let i = 0; i < identities.length; i++) { |
---|
375 | let identity = identities[i]; |
---|
376 | if (this._key == identity.key) { |
---|
377 | this._value = identity.identityName; |
---|
378 | break; |
---|
379 | } |
---|
380 | } |
---|
381 | } |
---|
382 | if (!this._value) this._key = null; |
---|
383 | } |
---|
384 | return this._value; |
---|
385 | }, |
---|
386 | equal : function(compareIdObj) { |
---|
387 | if (!this.key || !compareIdObj.key) return true; |
---|
388 | if (this.key != compareIdObj.key) { |
---|
389 | Log.debug("id not equal ('" + this.key + "' != '" + compareIdObj.key + "')"); |
---|
390 | } |
---|
391 | return (this.key == compareIdObj.key); |
---|
392 | } |
---|
393 | } |
---|