This repository was archived by the owner on Jun 23, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmisc.js
205 lines (193 loc) · 6.04 KB
/
misc.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
/**
* @fileoverview This file provides various utilities: some helpers to deal with
* identity management, some helpers for JS programming, some helpers for
* low-level XPCOM stuff...
* @author Jonathan Protzenko
*/
var EXPORTED_SYMBOLS = [
// Identity management helpers
"getIdentities",
"getDefaultIdentity",
"getIdentityForEmail",
// Various formatting helpers
"dateAsInMessageList",
"escapeHtml",
"parseMimeLine",
// Character set helpers
"systemCharset",
];
const { XPCOMUtils } = ChromeUtils.import(
"resource://gre/modules/XPCOMUtils.jsm"
);
XPCOMUtils.defineLazyModuleGetters(this, {
fixIterator: "resource:///modules/iteratorUtils.jsm",
MailServices: "resource:///modules/MailServices.jsm",
Services: "resource://gre/modules/Services.jsm",
});
/**
* Returns the default identity in the form { boolean isDefault; nsIMsgIdentity identity }
*/
function getDefaultIdentity() {
return getIdentities().find(x => x.isDefault);
}
/**
* Returns a list of all identities in the form [{ boolean isDefault; nsIMsgIdentity identity }].
* It is assured that there is exactly one default identity.
* If only the default identity is needed, getDefaultIdentity() can be used.
* @param aSkipNntpIdentities (default: true) Should we avoid including nntp identities in the list?
*/
function getIdentities(aSkipNntpIdentities = true) {
let identities = [];
for (let account of fixIterator(
MailServices.accounts.accounts,
Ci.nsIMsgAccount
)) {
let server = account.incomingServer;
if (
aSkipNntpIdentities &&
(!server || (server.type != "pop3" && server.type != "imap"))
) {
continue;
}
const defaultIdentity = MailServices.accounts.defaultAccount
? MailServices.accounts.defaultAccount.defaultIdentity
: null;
for (let currentIdentity of fixIterator(
account.identities,
Ci.nsIMsgIdentity
)) {
// We're only interested in identities that have a real email.
if (currentIdentity.email) {
identities.push({
isDefault: currentIdentity == defaultIdentity,
identity: currentIdentity,
});
}
}
}
if (!identities.length) {
console.warn("Didn't find any identities!");
} else if (!identities.some(x => x.isDefault)) {
console.warn(
"Didn't find any default key - mark the first identity as default!"
);
identities[0].isDefault = true;
}
return identities;
}
/*
* Searches a given email address in all identities and returns the corresponding identity.
* @param {String} anEmailAddress Email address to be searched in the identities
* @returns {{Boolean} isDefault, {{nsIMsgIdentity} identity} if found, otherwise undefined
*/
function getIdentityForEmail(anEmailAddress) {
return getIdentities(false).find(
ident => ident.identity.email.toLowerCase() == anEmailAddress.toLowerCase()
);
}
/**
* A stupid formatting function that uses Services.intl
* to format a date just like in the message list
* @param {Date} aDate a javascript Date object
* @return {String} a string containing the formatted date
*/
function dateAsInMessageList(aDate) {
const now = new Date();
// Is it today?
const isToday =
now.getFullYear() == aDate.getFullYear() &&
now.getMonth() == aDate.getMonth() &&
now.getDate() == aDate.getDate();
const format = isToday
? { timeStyle: "short" }
: { dateStyle: "short", timeStyle: "short" };
const dateTimeFormatter = new Services.intl.DateTimeFormat(undefined, format);
return dateTimeFormatter.format(aDate);
}
/**
* Helper function to escape some XML chars, so they display properly in
* innerHTML.
* @param {String} s input text
* @return {String} The string with <, >, and & replaced by the corresponding entities.
*/
function escapeHtml(s) {
s += "";
// stolen from selectionsummaries.js (thanks davida!)
return s.replace(/[<>&]/g, function(s) {
switch (s) {
case "<":
return "<";
case ">":
return ">";
case "&":
return "&";
default:
throw Error("Unexpected match");
}
});
}
/**
* Wraps the low-level header parser stuff.
* @param {String} mimeLine
* A line that looks like "John <john@cheese.com>, Jane <jane@wine.com>"
* @param {Boolean} [dontFix]
* Defaults to false. Shall we return an empty array in case aMimeLine is empty?
* @return {Array}
* A list of { email, name } objects
*/
function parseMimeLine(mimeLine, dontFix) {
if (mimeLine == null) {
console.debug("Empty aMimeLine?!!");
return [];
}
// The null here copes with pre-Thunderbird 71 compatibility.
let addresses = MailServices.headerParser.parseEncodedHeader(mimeLine, null);
if (addresses.length) {
return addresses.map(addr => {
return {
email: addr.email,
name: addr.name,
fullName: addr.toString(),
};
});
}
if (dontFix) {
return [];
}
return [{ email: "", name: "-", fullName: "-" }];
}
/**
* Returns a system character set string, which is system code page on Windows,
* LANG environment variable's encoding on Unix-like OS, otherwise UTF-8.
* @return {String} a character set string
*/
function systemCharset() {
let charset = "UTF-8";
if ("@mozilla.org/windows-registry-key;1" in Cc) {
let registry = Cc["@mozilla.org/windows-registry-key;1"].createInstance(
Ci.nsIWindowsRegKey
);
registry.open(
registry.ROOT_KEY_LOCAL_MACHINE,
"SYSTEM\\CurrentControlSet\\Control\\Nls\\CodePage",
registry.ACCESS_READ
);
let codePage = registry.readStringValue("ACP");
if (codePage) {
charset = "CP" + codePage;
}
registry.close();
} else {
let env = Cc["@mozilla.org/process/environment;1"].getService(
Ci.nsIEnvironment
);
let lang = env.get("LANG").split(".");
if (lang.length > 1) {
charset = lang[1];
}
}
return charset;
}