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 vI_account = { |
---|
26 | account : null, |
---|
27 | |
---|
28 | AccountManager : Components.classes["@mozilla.org/messenger/account-manager;1"] |
---|
29 | .getService(Components.interfaces.nsIMsgAccountManager), |
---|
30 | |
---|
31 | |
---|
32 | prefroot : Components.classes["@mozilla.org/preferences-service;1"] |
---|
33 | .getService(Components.interfaces.nsIPrefService) |
---|
34 | .getBranch(null), |
---|
35 | |
---|
36 | _getBaseIdentity : function () { |
---|
37 | return gAccountManager.getIdentity(vI.elements.Obj_MsgIdentity.value); |
---|
38 | }, |
---|
39 | |
---|
40 | _copyBoolAttribute : function(name) { |
---|
41 | vI_account.account.defaultIdentity.setBoolAttribute(name, |
---|
42 | vI_account._getBaseIdentity().getBoolAttribute(name)); |
---|
43 | }, |
---|
44 | |
---|
45 | _copyIntAttribute : function(name) { |
---|
46 | vI_account.account.defaultIdentity.setIntAttribute(name, |
---|
47 | vI_account._getBaseIdentity().getIntAttribute(name)); |
---|
48 | }, |
---|
49 | |
---|
50 | _copyCharAttribute : function(name) { |
---|
51 | vI_account.account.defaultIdentity.setCharAttribute(name, |
---|
52 | vI_account._getBaseIdentity().getCharAttribute(name)); |
---|
53 | }, |
---|
54 | |
---|
55 | _copyUnicharAttribute : function(name) { |
---|
56 | vI_account.account.defaultIdentity.setUnicharAttribute(name, |
---|
57 | vI_account._getBaseIdentity().getUnicharAttribute(name)); |
---|
58 | }, |
---|
59 | |
---|
60 | copyPreferences : function() { |
---|
61 | if (vI.preferences.getBoolPref("copySMIMESettings")) { |
---|
62 | // SMIME settings |
---|
63 | vI_notificationBar.dump("## vI_account: copy S/MIME settings\n") |
---|
64 | vI_account._copyUnicharAttribute("signing_cert_name"); |
---|
65 | vI_account._copyUnicharAttribute("encryption_cert_name"); |
---|
66 | vI_account._copyIntAttribute("encryptionpolicy"); |
---|
67 | } |
---|
68 | /* seems not required, encryption happens before Virtual Identity account is created |
---|
69 | if (vI.preferences.getBoolPref("copyEnigmailSettings")) { |
---|
70 | // pgp/enigmail settings |
---|
71 | vI_notificationBar.dump("## vI_account: copy PGP settings\n") |
---|
72 | vI_account._copyBoolAttribute("pgpSignEncrypted"); |
---|
73 | vI_account._copyBoolAttribute("pgpSignPlain"); |
---|
74 | vI_account._copyBoolAttribute("enablePgp"); |
---|
75 | vI_account._copyIntAttribute("pgpKeyMode"); |
---|
76 | vI_account._copyCharAttribute("pgpkeyId"); |
---|
77 | vI_account._copyIntAttribute("openPgpHeaderMode"); |
---|
78 | vI_account._copyCharAttribute("openPgpUrlName"); |
---|
79 | |
---|
80 | vI_account._copyIntAttribute("defaultEncryptionPolicy"); |
---|
81 | } */ |
---|
82 | if (vI.preferences.getBoolPref("copyAttachVCardSettings")) { |
---|
83 | // attach vcard |
---|
84 | vI_notificationBar.dump("## vI_account: copy VCard settings\n") |
---|
85 | vI_account._copyBoolAttribute("attachVCard"); |
---|
86 | vI_account._copyCharAttribute("escapedVCard"); |
---|
87 | } |
---|
88 | }, |
---|
89 | |
---|
90 | // checks if directory is empty, not really used |
---|
91 | // ignores files ending with *.msf, else reports if a non-zero file is found. |
---|
92 | __dirEmpty : function(directory) { |
---|
93 | var dirEnumerator = directory.directoryEntries; |
---|
94 | while (dirEnumerator.hasMoreElements()) { |
---|
95 | var maildir = dirEnumerator.getNext(); |
---|
96 | maildir.QueryInterface(Components.interfaces.nsIFile); |
---|
97 | // recurse into all subdirectories |
---|
98 | if (maildir.isDirectory() && |
---|
99 | !vI_account.__dirEmpty(maildir)) return false; |
---|
100 | // ignore files with ending "*.msf" |
---|
101 | if (!maildir.path.match(new RegExp(".*\.msf$","i")) && |
---|
102 | maildir.fileSize != 0) return false; |
---|
103 | } |
---|
104 | return true; |
---|
105 | }, |
---|
106 | |
---|
107 | __cleanupDirectories : function() { |
---|
108 | var file = Components.classes["@mozilla.org/file/directory_service;1"] |
---|
109 | .getService(Components.interfaces.nsIProperties) |
---|
110 | .get("ProfD", Components.interfaces.nsIFile); |
---|
111 | |
---|
112 | var fileEnumerator = file.directoryEntries |
---|
113 | while (fileEnumerator.hasMoreElements()) { |
---|
114 | var dir = fileEnumerator.getNext() |
---|
115 | dir.QueryInterface(Components.interfaces.nsIFile); |
---|
116 | if (dir.path.match(new RegExp("[/\\\\]Mail$","i"))) { // match Windows and Linux/Mac separators |
---|
117 | var dirEnumerator = dir.directoryEntries |
---|
118 | while (dirEnumerator.hasMoreElements()) { |
---|
119 | var maildir = dirEnumerator.getNext() |
---|
120 | maildir.QueryInterface(Components.interfaces.nsIFile); |
---|
121 | // match Windows and Linux/Mac separators |
---|
122 | if (maildir.path.match(new RegExp("[/\\\\]virtualIdentity.*$","i"))) { |
---|
123 | // should be empty, VirtualIdentity never uses those directories |
---|
124 | if (vI_account.__dirEmpty(maildir)) { |
---|
125 | try {maildir.remove(true)} catch(e) { } |
---|
126 | vI_notificationBar.dump("x"); |
---|
127 | } |
---|
128 | else vI_notificationBar.dump("."); |
---|
129 | |
---|
130 | } |
---|
131 | } |
---|
132 | } |
---|
133 | } |
---|
134 | vI_notificationBar.dump("\n") |
---|
135 | }, |
---|
136 | |
---|
137 | cleanupSystem : function() { |
---|
138 | vI_notificationBar.dump("## vI_account: cleanupSystem:\n") |
---|
139 | vI_notificationBar.dump("## vI_account: checking for leftover VirtualIdentity accounts ") |
---|
140 | for (var i=0; i < vI_account.AccountManager.accounts.Count(); i++) { |
---|
141 | var account = vI_account.AccountManager.accounts.QueryElementAt(i, Components.interfaces.nsIMsgAccount); |
---|
142 | if (vI_account.__isVIdentityAccount(account)) { |
---|
143 | vI_notificationBar.dump(".") |
---|
144 | vI_account.__removeAccount(account); |
---|
145 | } |
---|
146 | } |
---|
147 | vI_notificationBar.dump("\n## vI_account: checking for leftover VirtualIdentity directories ") |
---|
148 | vI_account.__cleanupDirectories(); |
---|
149 | |
---|
150 | vI_notificationBar.dump("## vI_account: cleanupSystem done.\n") |
---|
151 | }, |
---|
152 | |
---|
153 | __isVIdentityAccount : function(account) { |
---|
154 | // check for new (post0.5.0) accounts, |
---|
155 | try { vI_account.prefroot.getBoolPref("mail.account." + account.key + ".vIdentity"); |
---|
156 | return true; |
---|
157 | } catch (e) { }; |
---|
158 | // check for old (pre 0.5.0) accounts |
---|
159 | if (account.incomingServer && account.incomingServer.hostName == "virtualIdentity") return true; |
---|
160 | return false; |
---|
161 | }, |
---|
162 | |
---|
163 | __removeAccount : function(account) { |
---|
164 | // in new (post 0.5.0) Virtual Identity accounts the incomingServer of the account |
---|
165 | // points to an incoming server of a different account. Cause the internal |
---|
166 | // removeAccount function tries to removes the incomingServer ether, create |
---|
167 | // a real one before calling this function. |
---|
168 | if (!account.incomingServer || account.incomingServer.hostName != "virtualIdentity") { |
---|
169 | // if not some of the 'old' accounts |
---|
170 | account.incomingServer = vI_account.AccountManager. |
---|
171 | createIncomingServer("toRemove","virtualIdentity","pop3"); |
---|
172 | } |
---|
173 | |
---|
174 | // remove the rootFolder of the account |
---|
175 | try { account.incomingServer.rootFolder.Delete(); } |
---|
176 | catch (e) { }; |
---|
177 | |
---|
178 | var key = account.key; |
---|
179 | vI_notificationBar.dump("## vI_account: removing account " + key + ".\n") |
---|
180 | // remove the account |
---|
181 | vI_account.AccountManager.removeAccount(account); |
---|
182 | // remove the additional tagging-pref |
---|
183 | try { vI_account.prefroot.clearUserPref("mail.account." + key + ".vIdentity"); } |
---|
184 | catch (e) { }; |
---|
185 | }, |
---|
186 | |
---|
187 | removeUsedVIAccount : function() { |
---|
188 | if (vI_account.account) { |
---|
189 | vI_account.__removeAccount(vI_account.account); |
---|
190 | vI_account.account = null; |
---|
191 | } |
---|
192 | }, |
---|
193 | |
---|
194 | createAccount : function() |
---|
195 | { |
---|
196 | if (vI_account.account) { // if the Account is still created, then leave all like it is |
---|
197 | alert("account still created, shouldn't happen"); |
---|
198 | return; |
---|
199 | } |
---|
200 | /* |
---|
201 | // the easiest way would be to get all requiered Attributes might be to duplicate the default account like this |
---|
202 | var recentAccount = vI_account.AccountManager.getAccount(vI.elements.Obj_MsgIdentity.selectedItem.getAttribute("accountkey")); |
---|
203 | vI.VIdent_Account = vI_account.AccountManager.duplicateAccount(recentAccount); |
---|
204 | // but this ends up in the following exception: |
---|
205 | // "Component returned failure code: 0x80004001 (NS_ERROR_NOT_IMPLEMENTED) [nsIMsgAccountManager.duplicateAccount]" |
---|
206 | // so I have to do this by hand ;( |
---|
207 | */ |
---|
208 | |
---|
209 | vI_account.account = vI_account.AccountManager.createAccount(); |
---|
210 | vI_account.prefroot.setBoolPref("mail.account." + vI_account.account.key + ".vIdentity", true) |
---|
211 | |
---|
212 | vI_account.account.addIdentity(vI_account.AccountManager.createIdentity()); |
---|
213 | |
---|
214 | // the new account uses the same incomingServer than the base one, |
---|
215 | // it's especially required for NNTP cause incomingServer is used for sending newsposts. |
---|
216 | // by pointing to the same incomingServer stored passwords can be reused |
---|
217 | // the incomingServer has to be replaced before the account is removed, else it get removed ether |
---|
218 | var servers = vI_account.AccountManager.GetServersForIdentity(vI_account._getBaseIdentity()); |
---|
219 | vI_account.account.incomingServer = servers.QueryElementAt(0, Components.interfaces.nsIMsgIncomingServer); |
---|
220 | |
---|
221 | vI_account.copyMsgIdentityClone(); |
---|
222 | vI_account.copyPreferences(); |
---|
223 | vI_account.setupFcc(); |
---|
224 | vI_account.setupDraft(); |
---|
225 | vI_account.setupTemplates(); |
---|
226 | }, |
---|
227 | |
---|
228 | copyMsgIdentityClone : function() { |
---|
229 | var identityData = document.getElementById("msgIdentity_clone").identityData; |
---|
230 | vI_account.account.defaultIdentity.setCharAttribute("useremail", identityData.email); |
---|
231 | vI_account.account.defaultIdentity.setUnicharAttribute("fullName", identityData.fullName); |
---|
232 | |
---|
233 | vI_account.account.defaultIdentity.smtpServerKey = identityData.smtp |
---|
234 | |
---|
235 | vI_notificationBar.dump("## vI_account: Stored virtualIdentity (name " |
---|
236 | + vI_account.account.defaultIdentity.fullName + " email " |
---|
237 | + vI_account.account.defaultIdentity.email + " smtp " |
---|
238 | + vI_account.account.defaultIdentity.smtpServerKey +")\n"); |
---|
239 | }, |
---|
240 | |
---|
241 | setupFcc : function() |
---|
242 | { |
---|
243 | if (vI.preferences.getBoolPref("doFcc")) { |
---|
244 | switch (vI.preferences.getCharPref("fccFolderPickerMode")) |
---|
245 | { |
---|
246 | case "2" : |
---|
247 | vI_notificationBar.dump ("## vI_account: preparing Fcc --- use Settings of Default Account\n"); |
---|
248 | vI_account.account.defaultIdentity.doFcc = vI_account.AccountManager.defaultAccount.defaultIdentity.doFcc; |
---|
249 | vI_account.account.defaultIdentity.fccFolder = vI_account.AccountManager.defaultAccount.defaultIdentity.fccFolder; |
---|
250 | vI_account.account.defaultIdentity.fccFolderPickerMode = vI_account.AccountManager.defaultAccount.defaultIdentity.fccFolderPickerMode; |
---|
251 | if (!vI_helper.olderVersion("TB", "2.0")) |
---|
252 | vI_account.account.defaultIdentity.fccReplyFollowsParent = vI_account.AccountManager.defaultAccount.defaultIdentity.fccReplyFollowsParent; |
---|
253 | break; |
---|
254 | case "3" : |
---|
255 | vI_notificationBar.dump ("## vI_account: preparing Fcc --- use Settings of Modified Account\n"); |
---|
256 | vI_account.account.defaultIdentity.doFcc = vI_account._getBaseIdentity().doFcc; |
---|
257 | vI_account.account.defaultIdentity.fccFolder = vI_account._getBaseIdentity().fccFolder; |
---|
258 | vI_account.account.defaultIdentity.fccFolderPickerMode = vI_account._getBaseIdentity().fccFolderPickerMode; |
---|
259 | if (!vI_helper.olderVersion("TB", "2.0")) |
---|
260 | vI_account.account.defaultIdentity.fccReplyFollowsParent = vI_account._getBaseIdentity().fccReplyFollowsParent; |
---|
261 | break; |
---|
262 | default : |
---|
263 | vI_notificationBar.dump ("## vI_account: preparing Fcc --- use Virtual Identity Settings\n"); |
---|
264 | vI_account.account.defaultIdentity.doFcc |
---|
265 | = vI.preferences.getBoolPref("doFcc"); |
---|
266 | vI_account.account.defaultIdentity.fccFolder |
---|
267 | = vI.unicodeConverter.ConvertToUnicode(vI.preferences.getCharPref("fccFolder")); |
---|
268 | vI_account.account.defaultIdentity.fccFolderPickerMode |
---|
269 | = vI.preferences.getCharPref("fccFolderPickerMode"); |
---|
270 | if (!vI_helper.olderVersion("TB", "2.0")) |
---|
271 | vI_account.account.defaultIdentity.fccReplyFollowsParent = vI.preferences.getBoolPref("fccReplyFollowsParent"); |
---|
272 | |
---|
273 | break; |
---|
274 | } |
---|
275 | } |
---|
276 | else { |
---|
277 | dump ("## vI_account: dont performing Fcc\n"); |
---|
278 | vI_account.account.defaultIdentity.doFcc = false; |
---|
279 | } |
---|
280 | vI_notificationBar.dump("## vI_account: Stored (doFcc " + vI_account.account.defaultIdentity.doFcc + " fccFolder " + |
---|
281 | vI_account.account.defaultIdentity.fccFolder + " fccFolderPickerMode " + |
---|
282 | vI_account.account.defaultIdentity.fccFolderPickerMode + "(" + |
---|
283 | vI.preferences.getCharPref("fccFolderPickerMode") + "))\n"); |
---|
284 | }, |
---|
285 | |
---|
286 | setupDraft : function() { |
---|
287 | switch (vI.preferences.getCharPref("draftFolderPickerMode")) |
---|
288 | { |
---|
289 | case "2" : |
---|
290 | vI_notificationBar.dump ("## vI_account: preparing Draft --- use Settings of Default Account\n"); |
---|
291 | vI_account.account.defaultIdentity.draftFolder = vI_account.AccountManager.defaultAccount.defaultIdentity.draftFolder; |
---|
292 | vI_account.account.defaultIdentity.draftsFolderPickerMode = vI_account.AccountManager.defaultAccount.defaultIdentity.draftsFolderPickerMode; |
---|
293 | break; |
---|
294 | case "3" : |
---|
295 | vI_notificationBar.dump ("## vI_account: preparing Draft --- use Settings of Modified Account\n"); |
---|
296 | vI_account.account.defaultIdentity.draftFolder = vI_account._getBaseIdentity().draftFolder; |
---|
297 | vI_account.account.defaultIdentity.draftsFolderPickerMode = vI_account._getBaseIdentity().draftsFolderPickerMode; |
---|
298 | break; |
---|
299 | default : |
---|
300 | vI_notificationBar.dump ("## vI_account: preparing Draft --- use Virtual Identity Settings\n"); |
---|
301 | vI_account.account.defaultIdentity.draftFolder |
---|
302 | = vI.unicodeConverter.ConvertToUnicode(vI.preferences.getCharPref("draftFolder")); |
---|
303 | vI_account.account.defaultIdentity.draftsFolderPickerMode |
---|
304 | = vI.preferences.getCharPref("draftFolderPickerMode"); |
---|
305 | break; |
---|
306 | } |
---|
307 | vI_notificationBar.dump("## vI_account: Stored (draftFolder " + |
---|
308 | vI_account.account.defaultIdentity.draftFolder + " draftsFolderPickerMode " + |
---|
309 | vI_account.account.defaultIdentity.draftsFolderPickerMode + "(" + |
---|
310 | vI.preferences.getCharPref("draftFolderPickerMode") + "))\n"); |
---|
311 | }, |
---|
312 | |
---|
313 | setupTemplates : function() { |
---|
314 | switch (vI.preferences.getCharPref("stationeryFolderPickerMode")) |
---|
315 | { |
---|
316 | case "2" : |
---|
317 | vI_notificationBar.dump ("## vI_account: preparing Templates --- use Settings of Default Account\n"); |
---|
318 | vI_account.account.defaultIdentity.stationeryFolder = vI_account.AccountManager.defaultAccount.defaultIdentity.stationeryFolder; |
---|
319 | vI_account.account.defaultIdentity.tmplFolderPickerMode = vI_account.AccountManager.defaultAccount.defaultIdentity.tmplFolderPickerMode; |
---|
320 | break; |
---|
321 | case "3" : |
---|
322 | vI_notificationBar.dump ("## vI_account: preparing Templates --- use Settings of Modified Account\n"); |
---|
323 | vI_account.account.defaultIdentity.stationeryFolder = vI_account._getBaseIdentity().stationeryFolder; |
---|
324 | vI_account.account.defaultIdentity.tmplFolderPickerMode = vI_account._getBaseIdentity().tmplFolderPickerMode; |
---|
325 | break; |
---|
326 | default : |
---|
327 | vI_notificationBar.dump ("## vI_account: preparing Templates --- use Virtual Identity Settings\n"); |
---|
328 | vI_account.account.defaultIdentity.stationeryFolder |
---|
329 | = vI.unicodeConverter.ConvertToUnicode(vI.preferences.getCharPref("stationeryFolder")); |
---|
330 | vI_account.account.defaultIdentity.tmplFolderPickerMode |
---|
331 | = vI.preferences.getCharPref("stationeryFolderPickerMode"); |
---|
332 | break; |
---|
333 | } |
---|
334 | vI_notificationBar.dump("## vI_account: Stored (stationeryFolder " + |
---|
335 | vI_account.account.defaultIdentity.stationeryFolder + " tmplFolderPickerMode " + |
---|
336 | vI_account.account.defaultIdentity.tmplFolderPickerMode + "(" + |
---|
337 | vI.preferences.getCharPref("stationeryFolderPickerMode") + "))\n"); |
---|
338 | } |
---|
339 | } |
---|