diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 17:32:43 +0000 |
commit | 6bf0a5cb5034a7e684dcc3500e841785237ce2dd (patch) | |
tree | a68f146d7fa01f0134297619fbe7e33db084e0aa /comm/mailnews/news/src/NntpUtils.jsm | |
parent | Initial commit. (diff) | |
download | thunderbird-upstream.tar.xz thunderbird-upstream.zip |
Adding upstream version 1:115.7.0.upstream/1%115.7.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'comm/mailnews/news/src/NntpUtils.jsm')
-rw-r--r-- | comm/mailnews/news/src/NntpUtils.jsm | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/comm/mailnews/news/src/NntpUtils.jsm b/comm/mailnews/news/src/NntpUtils.jsm new file mode 100644 index 0000000000..40cc51b993 --- /dev/null +++ b/comm/mailnews/news/src/NntpUtils.jsm @@ -0,0 +1,63 @@ +/* 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 http://mozilla.org/MPL/2.0/. */ + +const EXPORTED_SYMBOLS = ["NntpUtils"]; + +const { MailServices } = ChromeUtils.import( + "resource:///modules/MailServices.jsm" +); + +/** + * Collection of helper functions for NNTP. + */ +var NntpUtils = { + logger: console.createInstance({ + prefix: "mailnews.nntp", + maxLogLevel: "Warn", + maxLogLevelPref: "mailnews.nntp.loglevel", + }), + + /** + * Find a server instance by its hostname. + * + * Sometimes we create a server instance to load a news url, this server is + * written to the prefs but not associated with any account. Different from + * nsIMsgAccountManager.findServer which can only find servers associated + * with accounts, this function looks for NNTP server in the mail.server. + * branch directly. + * + * @param {string} hostname - The hostname of the server. + * @returns {nsINntpIncomingServer|null} + */ + findServer(hostname) { + let branch = Services.prefs.getBranch("mail.server."); + + // Collect all the server keys. + let keySet = new Set(); + for (let name of branch.getChildList("")) { + keySet.add(name.split(".")[0]); + } + + // Find the NNTP server that matches the hostname. + hostname = hostname.toLowerCase(); + for (let key of keySet) { + let type = branch.getCharPref(`${key}.type`, ""); + let hostnameValue = branch + .getCharPref(`${key}.hostname`, "") + .toLowerCase(); + if (type == "nntp" && hostnameValue == hostname) { + try { + return MailServices.accounts + .getIncomingServer(key) + .QueryInterface(Ci.nsINntpIncomingServer); + } catch (e) { + // In some profiles, two servers have the same hostname, but only one + // can be loaded into AccountManager. Catch the error here and the + // already loaded server will be found. + } + } + } + return null; + }, +}; |