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

source: chrome/content/v_identity/vI_getHeader.js @ 828364

lite_0.1multiEditng_0.6ng_0.6_helpng_0.8ng_0.9
Last change on this file since 828364 was 828364, checked in by rene <rene@…>, 13 years ago

added explicit variable declarations

  • Property mode set to 100644
File size: 12.6 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): Christian Weiske
23    Contributor(s): Patrick Brunschwig
24 * ***** END LICENSE BLOCK ***** */
25
26/**
27* some code copied and adapted from 'display Mail User Agent (MUA)'
28* thanks to Christian Weiske <cweiske@cweiske.de>
29*/
30/**
31* some code copied and adapted from 'enigmail'
32* thanks to Patrick Brunschwig <patrick.brunschwig@gmx.net>
33*/
34
35var vI_getHeader = {
36    messenger: null,
37    preferences : Components.classes["@mozilla.org/preferences-service;1"]
38            .getService(Components.interfaces.nsIPrefService)
39            .getBranch("extensions.virtualIdentity."),
40           
41    unicodeConverter : Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
42            .createInstance(Components.interfaces.nsIScriptableUnicodeConverter),
43
44    strings : document.getElementById("vIdentBundle"),
45   
46    headerToSearch : null,
47   
48    prepareHeaderToSearchArray : function() {
49        var headerList = vI_getHeader.unicodeConverter.ConvertToUnicode(vI_getHeader.preferences.getCharPref("smart_reply_headers")).split(/\n/)
50       
51        vI_getHeader.headerToSearch = [];
52       
53        // prepare headerToSearch for speedup.
54        for (var index = 0; index < headerList.length; index++) {
55            var headerToSearch_splitted = headerList[index].split(/:/)
56            // use first part (all before ':') as the header name
57            var headerNameToSearch = headerToSearch_splitted[0].toLowerCase()
58            // check second or third part for any number
59            var headerNumberToSearch = parseInt(headerToSearch_splitted[1])
60            if (isNaN(headerNumberToSearch)) headerNumberToSearch = parseInt(headerToSearch_splitted[2])
61           
62            // create header name to store the value
63            var headerNameToStore = "vI_" + headerNameToSearch
64            if (!isNaN(headerNumberToSearch)) headerNameToStore += ":" + headerNumberToSearch
65           
66            vI_getHeader.headerToSearch.push({ headerNameToSearch : headerNameToSearch, headerNumberToSearch : headerNumberToSearch,
67                    headerNameToStore : headerNameToStore });
68        }
69    },
70
71    getHeader: function() {
72        vI_notificationBar.clear_dump()
73        var index;
74
75        try { var srcMsgURI = gDBView.URIForFirstSelectedMessage; } catch (ex) { return; }
76        if (srcMsgURI == null) return;
77       
78        if (/type=application\/x-message-display/.test(srcMsgURI)) {
79            vI_notificationBar.dump("## vI_getHeader: opening stored Message, can't get Message Header\n");
80            return;
81        }
82       
83        try { var hdr = vI_getHeader.messenger.messageServiceFromURI(srcMsgURI).messageURIToMsgHdr(srcMsgURI); }
84        catch(vErr) {
85            vI_notificationBar.dump("## vI_getHeader: can't get Message Header.\n");
86            return;
87        };
88
89        if (!vI_getHeader.headerToSearch) vI_getHeader.prepareHeaderToSearchArray()
90
91        var found = false; var label = "";
92        var subtitle = vI_getHeader.strings.getString("vident.getHeader.noHeader");
93        // create array to count the header
94        var currentHeadersCounter = [];
95       
96        var vI_listId = false; var vI_received = false;
97        // loop through the headers
98        for (var header in currentHeaderData) {
99            var headerName = currentHeaderData[header].headerName.toLowerCase();
100
101            // remember list-id header to prevent using Mailing-List addresses as sender
102            if (!vI_listId && headerName == "list-id") {
103                hdr.setStringProperty("vI_list-id","found"); vI_listId = true;
104                vI_notificationBar.dump("## vI_getHeader: found header: list-id  ...stored to recognize mailing-list\n");
105//              continue;
106            }
107
108            // remember received header to prevent using Mailing-List addresses as sender
109            if (!vI_received && headerName == "received") {
110                hdr.setStringProperty("vI_received","found"); vI_received = true;
111                vI_notificationBar.dump("## vI_getHeader: found header: received  ...stored to recognize received mail\n");
112//              continue;
113            }
114           
115            if (currentHeadersCounter[headerName]) currentHeadersCounter[headerName]++
116            else currentHeadersCounter[headerName] = 1
117           
118            for (var index = 0; index < vI_getHeader.headerToSearch.length; index++) {
119                if (headerName == vI_getHeader.headerToSearch[index].headerNameToSearch &&
120                    (isNaN(vI_getHeader.headerToSearch[index].headerNumberToSearch) ||
121                        vI_getHeader.headerToSearch[index].headerNumberToSearch == currentHeadersCounter[headerName])) {
122                   
123                    var value = currentHeaderData[header].headerValue;
124                    if (currentHeadersCounter[headerName] != 1)
125                        value = hdr.getStringProperty(vI_getHeader.headerToSearch[index].headerNameToStore) + 
126                        ", " + value;
127                    hdr.setStringProperty(vI_getHeader.headerToSearch[index].headerNameToStore,vI_getHeader.unicodeConverter.ConvertFromUnicode(value) + vI_getHeader.unicodeConverter.Finish());
128
129                    var storedValue = hdr.getProperty(vI_getHeader.headerToSearch[index].headerNameToStore)
130                    var storedConvValue = vI_getHeader.unicodeConverter.ConvertToUnicode(storedValue)
131                    vI_notificationBar.dump("## vI_getHeader: found header: " + headerName +
132                        "[:" + currentHeadersCounter[headerName] + "] - stored as '" + 
133                        storedConvValue + "'\n");
134                    if (!found) { 
135                        subtitle = vI_getHeader.strings.getString("vident.getHeader.headerFound");
136                        found = true;
137                    }
138                    label += (label)?"\n":""
139                    label += currentHeaderData[header].headerName + 
140                    "[:" + currentHeadersCounter[headerName] + "]:\t" + currentHeaderData[header].headerValue
141                    break;
142                }
143            }
144        }
145        vI_notificationBar.setNote(label, "get_header_notification", subtitle);
146    },
147   
148    hideExtraHeader: function() {
149        var header_list = vI_prepareHeader.addedHeaders
150        for (var index = 0; index < header_list.length; index++) {
151            var header_to_search_splitted=header_list[index].split(/:/)
152            var header_to_search=header_to_search_splitted[0].toLowerCase()
153            if (typeof(gExpandedHeaderView[header_to_search]) == "object") {
154            if (! gViewAllHeaders) {
155                gExpandedHeaderView[header_to_search].enclosingBox.setAttribute("hidden", true);
156            }
157            else {
158                gExpandedHeaderView[header_to_search].enclosingBox.removeAttribute("hidden");
159            }
160            }
161        }
162    },
163
164    setupEventListener: function() {
165        var listener = {};
166        listener.onStartHeaders = vI_getHeader.hideExtraHeader;
167        listener.onEndHeaders   = vI_getHeader.getHeader;
168        gMessageListeners.push(listener);
169
170        vI_getHeader.messenger = Components.classes["@mozilla.org/messenger;1"].createInstance();
171        vI_getHeader.messenger = vI_getHeader.messenger.QueryInterface(Components.interfaces.nsIMessenger);
172        vI_getHeader.strings = document.getElementById("vIdentBundle");
173       
174        vI_getHeader.unicodeConverter.charset = "UTF-8";
175    }
176}
177
178
179var vI_prepareHeader = {
180    prefroot : Components.classes["@mozilla.org/preferences-service;1"]
181            .getService(Components.interfaces.nsIPrefService)
182            .getBranch(null),
183
184    unicodeConverter : Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
185            .createInstance(Components.interfaces.nsIScriptableUnicodeConverter),
186           
187    addedHeaders : [],
188   
189    observer_added : false,
190   
191    init : function() {
192        if (vI_prepareHeader.addExtraHeader()) vI_prepareHeader.addObserver();
193    },
194   
195    cleanup : function() {
196        vI_prepareHeader.removeObserver();
197        vI_prepareHeader.removeExtraHeader();
198    },
199   
200    addObserver : function() {
201        if (vI_prepareHeader.observer_added) return;
202        vI_prepareHeader.prefroot.QueryInterface(Components.interfaces.nsIPrefBranch2);
203        vI_prepareHeader.prefroot.addObserver("extensions.virtualIdentity.smart_reply_headers", this, false);
204        vI_prepareHeader.observer_added = true;
205    },
206   
207    removeObserver : function() {
208        if (!vI_prepareHeader.observer_added) return;
209        vI_prepareHeader.prefroot.removeObserver("extensions.virtualIdentity.smart_reply_headers", this);
210        vI_prepareHeader.observer_added = false;
211    },
212   
213    // this is a adapted copy of enigEnsureExtraHeaders() from enigmail, thanks
214    addExtraHeader : function() {
215        vI_prepareHeader.unicodeConverter.charset = "UTF-8";
216        var header_list = vI_prepareHeader.unicodeConverter.ConvertToUnicode(vI_prepareHeader.prefroot.getCharPref("extensions.virtualIdentity.smart_reply_headers")).split(/\n/)
217       
218        // add List-Id to recognizable headers to prevent using Mailing-List addresses as sender
219        header_list.push("List-Id")
220
221        // add Received to recognizable headers to detect if mail was sent or received
222        header_list.push("Received")
223
224//      try {
225            var extraHdrs = " " + 
226                vI_prepareHeader.prefroot.getCharPref("mailnews.headers.extraExpandedHeaders").toLowerCase()
227                + " ";
228
229            for (var index = 0; index < header_list.length; index++) {
230                var headerToSearch_splitted = header_list[index].split(/:/)
231                var headerToSearch = headerToSearch_splitted[0].toLowerCase()
232               
233                var j; var found = false;
234               
235                // collapsedHeaderView is removed in
236                // https://bugzilla.mozilla.org/show_bug.cgi?id=480623
237                // http://build.mozillamessaging.com/mercurial/comm-central/rev/1fbbd90413d9
238                if (typeof(gCollapsedHeaderList) != "undefined") {
239                    // check if Header is included in collapsed HeaderView
240                    for (var j = 0; j < gCollapsedHeaderList.length; j++) {
241                        if (gCollapsedHeaderList[j].name == headerToSearch) {
242    //                      vI_notificationBar.dump("## vI_prepareHeader: Header '" + headerToSearch + "' in gCollapsedHeaderList\n");
243                            found = true; break;
244                        }
245                    }
246                    if (found) continue;
247                }
248
249                // check if Header is included in expanded HeaderView
250                for (var j = 0; j < gExpandedHeaderList.length; j++) {
251                    if (gExpandedHeaderList[j].name == headerToSearch) {
252//                      vI_notificationBar.dump("## vI_prepareHeader: Header '" + headerToSearch + "' in gExpandedHeaderList\n");
253                        found = true; break;
254                    }
255                }
256                if (found) continue;
257
258                var addedHeadersString = " " + vI_prepareHeader.addedHeaders.join(" ") + " "
259                if ((extraHdrs.indexOf(" " + headerToSearch + " ") < 0) &&
260                    (addedHeadersString.indexOf(" " + headerToSearch + " ") < 0))
261                    vI_prepareHeader.addedHeaders.push(headerToSearch);
262//              else vI_notificationBar.dump("## vI_prepareHeader: Header '" + headerToSearch + "' already in extraExpandedHeaders\n");
263            }
264           
265            if (vI_prepareHeader.addedHeaders.length > 0) {
266                extraHdrs += vI_prepareHeader.addedHeaders.join(" ");
267                extraHdrs = extraHdrs.replace(/^\s+|\s+$/g,"")
268                vI_prepareHeader.prefroot.setCharPref("mailnews.headers.extraExpandedHeaders", extraHdrs)
269                vI_notificationBar.dump("## vI_prepareHeader: extraExpandedHeaders '" + vI_prepareHeader.addedHeaders.join(" ") + "' added\n");
270            }       
271
272            return true;
273//      }
274//      catch (e) {
275//          vI_notificationBar.dump("## vI_prepareHeader: your application is too old, please update. Otherwise try to install mnenhy or enigmail to use additional headers.")
276//          return false;
277//      }
278    },
279
280    removeExtraHeader: function() {
281        vI_notificationBar.dump("## vI_prepareHeader: cleanupExtraHeader\n");
282
283        if (vI_prepareHeader.addedHeaders.length > 0) {
284            var extraHdrs = vI_prepareHeader.prefroot.getCharPref("mailnews.headers.extraExpandedHeaders").toLowerCase().split(/ /);
285       
286            for (var i = 0; i < vI_prepareHeader.addedHeaders.length; i++) {
287                for (var j = 0; j < extraHdrs.length; j++) {
288                    if (extraHdrs[j] == vI_prepareHeader.addedHeaders[i]) {
289                        extraHdrs.splice(j,1);
290                        break;
291                    }
292                }
293            }
294            vI_prepareHeader.prefroot.setCharPref("mailnews.headers.extraExpandedHeaders", extraHdrs.join(" "))
295            vI_notificationBar.dump("## vI_prepareHeader: extraExpandedHeaders '" + vI_prepareHeader.addedHeaders.join(" ") + "' removed\n");
296            vI_prepareHeader.addedHeaders = [];
297        }
298    },
299   
300    observe: function(subject, topic, data) {
301        if (topic == "nsPref:changed") {
302            vI_prepareHeader.removeExtraHeader();
303            vI_prepareHeader.addExtraHeader();
304            vI_notificationBar.dump("## vI_prepareHeader: changed preference '" + subject + " " + topic + " " + data + "'\n");
305           
306            // remove (old) prepared headerArray
307            vI_getHeader.headerToSearch = null;
308           
309            vI_notificationBar.dump("## vI_prepareHeader: reload Message\n");
310            MsgReload();
311        }
312    }
313}
314
315addEventListener('messagepane-loaded', vI_getHeader.setupEventListener, true);
316window.addEventListener("load", function(e) { vI_prepareHeader.init(); }, false);
317window.addEventListener("unload", function(e) { vI_prepareHeader.cleanup(); }, false);
Note: See TracBrowser for help on using the repository browser.