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 | vI_smartIdentity = { |
---|
26 | messenger : Components.classes["@mozilla.org/messenger;1"].createInstance() |
---|
27 | .QueryInterface(Components.interfaces.nsIMessenger), |
---|
28 | |
---|
29 | smartIdentity : null, |
---|
30 | smartIdentity_BaseIdentity : null, |
---|
31 | |
---|
32 | // After Loading the MessageComposeDialog, check if smartIdentity is needed |
---|
33 | init : function() { |
---|
34 | // if there is no ID of the original Message (Why?) leave the function |
---|
35 | var uri = vI.params.originalMsgURI; |
---|
36 | if (!uri) { |
---|
37 | vI_notificationBar.dump("## vI_smartIdentity: cant get URI of former Message\n"); |
---|
38 | return; |
---|
39 | } |
---|
40 | var hdr = vI_smartIdentity.messenger.msgHdrFromURI(uri); |
---|
41 | |
---|
42 | var type = vI.params.type; |
---|
43 | var msgComposeType = Components.interfaces.nsIMsgCompType; |
---|
44 | switch (type) { |
---|
45 | case msgComposeType.Reply: |
---|
46 | case msgComposeType.ReplyAll: |
---|
47 | case msgComposeType.ReplyToGroup: |
---|
48 | case msgComposeType.ReplyToSender: |
---|
49 | case msgComposeType.ReplyToSenderAndGroup: |
---|
50 | if (vI.preferences.getBoolPref("smart_reply")) |
---|
51 | vI_smartIdentity.SmartReply(hdr); |
---|
52 | break; |
---|
53 | case msgComposeType.Draft: |
---|
54 | case msgComposeType.Template: |
---|
55 | if (vI.preferences.getBoolPref("smart_draft")) |
---|
56 | vI_smartIdentity.SmartDraft(hdr); |
---|
57 | break; |
---|
58 | } |
---|
59 | }, |
---|
60 | |
---|
61 | // this function checks if we have a draft-case and Smart-Draft should replace the Identity |
---|
62 | SmartDraft : function(hdr) { |
---|
63 | vI_notificationBar.dump("## vI_smartIdentity: SmartDraft()\n"); |
---|
64 | |
---|
65 | vI_notificationBar.dump("## vI_smartIdentity: sender " + hdr.author + "\n"); |
---|
66 | vI_notificationBar.setNote(vI.elements.strings.getString("vident.smartIdentity.vIUsage") + ".", |
---|
67 | "smart_reply_notification"); |
---|
68 | vI_msgIdentityClone.setIdentity(hdr.author) |
---|
69 | |
---|
70 | vI.helper.addSeparatorToCloneMenu(); |
---|
71 | var object = vI_msgIdentityClone.elements.Obj_MsgIdentityPopup_clone |
---|
72 | var accountname = document.getElementById("prettyName-Prefix").getAttribute("label") |
---|
73 | + " - " + vI.helper.getBaseIdentity().email |
---|
74 | vI.helper.addIdentityMenuItem(object, hdr.author, accountname, "", "vid") |
---|
75 | }, |
---|
76 | |
---|
77 | // check if recent email-address (pre-choosen identity) is found in at least one email-address |
---|
78 | matchSelectedIdentity : function(all_addresses) { |
---|
79 | for (index = 0; index < all_addresses.number; index++) { |
---|
80 | if (vI.params.identity.email.toLowerCase() == all_addresses.emails.value[index].toLowerCase() |
---|
81 | && (vI.preferences.getBoolPref("smart_reply_ignoreFullName") || |
---|
82 | vI.params.identity.fullName == all_addresses.fullNames.value[index])) { |
---|
83 | vI_notificationBar.dump("## vI_smartIdentity: found preselected Identity in address sets, aborting\n"); |
---|
84 | return true; // direct hit |
---|
85 | } |
---|
86 | } |
---|
87 | return false; |
---|
88 | }, |
---|
89 | |
---|
90 | // checks if the Identity currently described by the extension-area fields i still available as |
---|
91 | // a stored identity. If so, use the stored one. |
---|
92 | matchAnyIdentity : function(all_addresses) { |
---|
93 | var accounts = queryISupportsArray(gAccountManager.accounts, Components.interfaces.nsIMsgAccount); |
---|
94 | for (var i in accounts) { |
---|
95 | var server = accounts[i].incomingServer; |
---|
96 | // ignore (other) virtualIdentity Accounts |
---|
97 | if (!server || server.hostName == "virtualIdentity") continue; |
---|
98 | |
---|
99 | var identities = queryISupportsArray(accounts[i].identities, Components.interfaces.nsIMsgIdentity); |
---|
100 | for (var j in identities) { |
---|
101 | for (index = 0; index < all_addresses.number; index++) { |
---|
102 | if (identities[j].getUnicharAttribute("useremail").toLowerCase() == |
---|
103 | all_addresses.emails.value[index].toLowerCase() && |
---|
104 | (vI.preferences.getBoolPref("smart_reply_ignoreFullName") || |
---|
105 | identities[j].getUnicharAttribute("fullName").toLowerCase() == |
---|
106 | all_addresses.fullNames.value[index].toLowerCase())) { |
---|
107 | vI_notificationBar.dump("## vI_smartIdentity: found existing Identity in address sets, aborting\n"); |
---|
108 | return identities[j]; |
---|
109 | } |
---|
110 | } |
---|
111 | } |
---|
112 | } |
---|
113 | return null; |
---|
114 | }, |
---|
115 | |
---|
116 | |
---|
117 | filterAddresses : function(all_addresses) { |
---|
118 | var return_addresses = { number : 0, emails : { value : new Array() }, |
---|
119 | fullNames : { value : new Array() }, |
---|
120 | combinedNames : { value : new Array() } }; |
---|
121 | |
---|
122 | var filter_list = vI.preferences.getCharPref("smart_reply_filter").split(/\n/) |
---|
123 | if (filter_list.length == 0) filter_list[0] == "" |
---|
124 | |
---|
125 | for (i = 0; i < filter_list.length; i++) { |
---|
126 | const filterType = { None : 0, RegExp : 1, StrCmp : 2 } |
---|
127 | var recentfilterType; var skipRegExp = false; |
---|
128 | if (filter_list.length <= 1 && filter_list[0] == "") |
---|
129 | { vI_notificationBar.dump("## vI_smartIdentity: no filters configured\n"); recentfilterType = filterType.None; } |
---|
130 | else if (/^\/(.*)\/$/.exec(filter_list[i])) |
---|
131 | { vI_notificationBar.dump("## vI_smartIdentity: filter emails with RegExp '" |
---|
132 | + filter_list[i].replace(/\\/g,"\\\\") + "'\n"); recentfilterType = filterType.RegExp; } |
---|
133 | else { vI_notificationBar.dump("## vI_smartIdentity: filter emails, compare with '" |
---|
134 | + filter_list[i] + "'\n"); recentfilterType = filterType.StrCmp; } |
---|
135 | for (j = 0; j < all_addresses.number; j++) { // check if recent email-address (pre-choosen identity) is found in |
---|
136 | // copied and adapted from correctIdentity, thank you for the RegExp-idea! |
---|
137 | var add_addr = false; |
---|
138 | switch (recentfilterType) { |
---|
139 | case filterType.None: |
---|
140 | add_addr = true; break; |
---|
141 | case filterType.RegExp: |
---|
142 | if (skipRegExp) break; |
---|
143 | try { /^\/(.*)\/$/.exec(filter_list[i]); |
---|
144 | add_addr = (all_addresses.emails.value[j].match(new RegExp(RegExp.$1,"i"))) |
---|
145 | } |
---|
146 | catch(vErr) { |
---|
147 | vI_notificationBar.addNote( |
---|
148 | vI.elements.strings.getString("vident.smartIdentity.ignoreRegExp") + |
---|
149 | +filter_list[i].replace(/\\/g,"\\\\") + " .", |
---|
150 | "smart_reply_notification"); |
---|
151 | skipRegExp = true; } |
---|
152 | break; |
---|
153 | case filterType.StrCmp: |
---|
154 | add_addr = (filter_list[i] == all_addresses.emails.value[j]) |
---|
155 | break; |
---|
156 | } |
---|
157 | if (add_addr) return_addresses = vI_smartIdentity.addWithoutDuplicates(return_addresses, |
---|
158 | all_addresses.emails.value[j], |
---|
159 | all_addresses.fullNames.value[j], |
---|
160 | all_addresses.combinedNames.value[j]) |
---|
161 | } |
---|
162 | } |
---|
163 | return return_addresses; |
---|
164 | }, |
---|
165 | |
---|
166 | addWithoutDuplicates : function(all_addresses, email, fullName, combinedName) { |
---|
167 | for (index = 0; index < all_addresses.number; index++) { |
---|
168 | if (all_addresses.emails.value[index] == email) { |
---|
169 | // found, so check if we can use the Name of the new field |
---|
170 | if (all_addresses.fullNames.value[index] == "" && fullName != "") { |
---|
171 | all_addresses.fullNames.value[index] = fullName |
---|
172 | all_addresses.combinedNames.value[index] = combinedName |
---|
173 | vI_notificationBar.dump("## vI_smartIdentity: added fullName '" + fullName |
---|
174 | + "' to stored email '" + email +"'\n") |
---|
175 | } |
---|
176 | return all_addresses; |
---|
177 | } |
---|
178 | } |
---|
179 | vI_notificationBar.dump("## vI_smartIdentity: add new address to result:" + email + "\n") |
---|
180 | all_addresses.emails.value[index] = email; |
---|
181 | all_addresses.fullNames.value[index] = fullName; |
---|
182 | all_addresses.combinedNames.value[index] = combinedName; |
---|
183 | all_addresses.number = index + 1; |
---|
184 | return all_addresses; |
---|
185 | }, |
---|
186 | |
---|
187 | // this function checks if we have a reply-case and Smart-Reply should replace the Identity |
---|
188 | SmartReply : function(hdr) { |
---|
189 | vI_notificationBar.dump("## vI_smartIdentity: SmartReply()\n"); |
---|
190 | |
---|
191 | if (gMsgCompose.compFields.newsgroups && !vI.preferences.getBoolPref("smart_reply_for_newsgroups")) { |
---|
192 | vI_notificationBar.dump("## vI_smartIdentity: answering to a newsgroup, aborting\n"); |
---|
193 | return; |
---|
194 | } |
---|
195 | |
---|
196 | /* first step: collect addresses */ |
---|
197 | |
---|
198 | // add additional emails from selected headers (stored by vI_getHeader.xul/js) |
---|
199 | var addresses = "" |
---|
200 | var reply_headers = vI.preferences.getCharPref("smart_reply_headers").split(/\n/) |
---|
201 | for (index = 0; index < reply_headers.length; index++) { |
---|
202 | var value = hdr.getStringProperty(reply_headers[index]) |
---|
203 | if (value != "") { |
---|
204 | vI_notificationBar.dump("## vI_smartIdentity: found '" + value + "' in stored headers\n") |
---|
205 | addresses += ", " + value |
---|
206 | } |
---|
207 | } |
---|
208 | |
---|
209 | if (vI.preferences.getBoolPref("smart_reply_prefer_headers")) |
---|
210 | addresses = addresses + ", " + hdr.recipients + ", " + hdr.ccList |
---|
211 | else addresses = hdr.recipients + ", " + hdr.ccList + addresses |
---|
212 | // replace trailing commata |
---|
213 | addresses = addresses.replace(/^((, )+)?/, ""); |
---|
214 | vI_notificationBar.dump("## vI_smartIdentity: address-string: '" + addresses + "'\n") |
---|
215 | |
---|
216 | var all_addresses = { number : 0, emails : {}, fullNames : {}, combinedNames : {} }; |
---|
217 | all_addresses.number = vI.headerParser |
---|
218 | .parseHeadersWithArray(addresses, all_addresses.emails, |
---|
219 | all_addresses.fullNames, all_addresses.combinedNames); |
---|
220 | |
---|
221 | vI_notificationBar.dump("## vI_smartIdentity: " + all_addresses.number + " addresses after parsing, before filtering\n") |
---|
222 | |
---|
223 | |
---|
224 | /* second step: filter (and sort) addresses */ |
---|
225 | |
---|
226 | if (vI_smartIdentity.matchSelectedIdentity(all_addresses)) return; |
---|
227 | |
---|
228 | if (vI_smartIdentity.smartIdentity_BaseIdentity = vI_smartIdentity.matchAnyIdentity(all_addresses)) { |
---|
229 | vI_notificationBar.addNote( |
---|
230 | vI.elements.strings.getString("vident.smartIdentity.matchExisting"), |
---|
231 | "smart_reply_notification"); |
---|
232 | window.setTimeout(vI_smartIdentity.updateMsgComposeDialog, 0); |
---|
233 | return; |
---|
234 | } |
---|
235 | |
---|
236 | all_addresses = vI_smartIdentity.filterAddresses(all_addresses); |
---|
237 | |
---|
238 | vI_notificationBar.dump("## vI_smartIdentity: filtering done, " + all_addresses.number + " addresses left\n") |
---|
239 | if (all_addresses.number == 0) return; |
---|
240 | |
---|
241 | /* second step: select address */ |
---|
242 | |
---|
243 | vI_smartIdentity.addSmartIdentitiesToCloneMenu(all_addresses); |
---|
244 | |
---|
245 | if (vI.preferences.getBoolPref("smart_reply_ask") && |
---|
246 | (all_addresses.number == 1 && vI.preferences.getBoolPref("smart_reply_ask_always") |
---|
247 | || all_addresses.number > 1)) |
---|
248 | window.openDialog("chrome://v_identity/content/vI_smartReplyDialog.xul",0, // give the Dialog a unique id |
---|
249 | "chrome, dialog, modal, alwaysRaised, resizable=yes", |
---|
250 | all_addresses, |
---|
251 | /* callback: */ vI_smartIdentity.changeIdentityToSmartIdentity).focus(); |
---|
252 | else if (vI.preferences.getBoolPref("smart_reply_autocreate")) { |
---|
253 | var label=vI.elements.strings.getString("vident.smartIdentity.vIUsage"); |
---|
254 | if (all_addresses.number > 1) label += " " |
---|
255 | + vI.elements.strings.getString("vident.smartIdentity.moreThanOne"); |
---|
256 | vI_notificationBar.addNote(label + ".", "smart_reply_notification"); |
---|
257 | vI_smartIdentity.changeIdentityToSmartIdentity(all_addresses, 0); |
---|
258 | } |
---|
259 | }, |
---|
260 | |
---|
261 | changeIdentityToSmartIdentity : function(all_addresses, selectedValue) { |
---|
262 | vI_msgIdentityClone.setIdentity(all_addresses.combinedNames.value[selectedValue]); |
---|
263 | vI_smartIdentity.removeSmartIdentityFromRecipients(all_addresses, selectedValue); |
---|
264 | }, |
---|
265 | |
---|
266 | removeSmartIdentityFromRecipients : function(all_addresses, index) { |
---|
267 | for (var row = 1; row <= top.MAX_RECIPIENTS; row ++) { |
---|
268 | var input = awGetInputElement(row); |
---|
269 | if (input.value == all_addresses.emails.value[index] || |
---|
270 | input.value == all_addresses.combinedNames.value[index]) { |
---|
271 | awDeleteRow(row); |
---|
272 | vI_notificationBar.addNote(" " + |
---|
273 | vI.elements.strings.getString("vident.smartIdentity.remRecipient"), |
---|
274 | "smart_reply_notification"); |
---|
275 | break; |
---|
276 | } |
---|
277 | } |
---|
278 | }, |
---|
279 | |
---|
280 | updateMsgComposeDialog : function() { |
---|
281 | vI_msgIdentityClone.setMenuToIdentity(vI_smartIdentity.smartIdentity_BaseIdentity.key, false); |
---|
282 | // after inserting a new signature (as part of the new identity) the cursor is not at the right place. |
---|
283 | // there is no easy way to est the cursor at the end, before the signature, so set it at the beginning. |
---|
284 | if (vI_smartIdentity.smartIdentity_BaseIdentity.attachSignature) |
---|
285 | gMsgCompose.editor.beginningOfDocument(); |
---|
286 | }, |
---|
287 | |
---|
288 | // adds MenuItem for SmartIdentities to the Identity-Select Dropdown Menu |
---|
289 | // this might get conflicts with other code, so use the cloned Dropdown-Menu instead |
---|
290 | addSmartIdentitiesToCloneMenu: function(all_addresses) { |
---|
291 | vI.helper.addSeparatorToCloneMenu(); |
---|
292 | var object = vI_msgIdentityClone.elements.Obj_MsgIdentityPopup_clone |
---|
293 | var accountname = document.getElementById("prettyName-Prefix").getAttribute("label") |
---|
294 | + vI_msgIdentityClone.elements.Obj_MsgIdentity_clone |
---|
295 | .getAttribute("accountname") |
---|
296 | for (index = 0; index < all_addresses.number; index++) |
---|
297 | vI.helper.addIdentityMenuItem(object, all_addresses.combinedNames.value[index], |
---|
298 | accountname, "", "vid") |
---|
299 | }, |
---|
300 | } |
---|