From 72166371804aa1a38b1063b9eb635e2cfa89bedf Mon Sep 17 00:00:00 2001 From: Magnus Melin Date: Wed, 3 Apr 2024 12:56:48 +0300 Subject: [PATCH] Bug 1814893 - Clean up filters.sys.mjs. r=kaie Differential Revision: https://phabricator.services.mozilla.com/D206042 --HG-- extra : rebase_source : 7f998a3d15ac3599023180b2a9ce7f56ead466d6 --- .../openpgp/content/modules/filters.sys.mjs | 178 +++++------------- .../openpgp/content/ui/keyDetailsDlg.js | 3 - 2 files changed, 51 insertions(+), 130 deletions(-) diff --git a/mail/extensions/openpgp/content/modules/filters.sys.mjs b/mail/extensions/openpgp/content/modules/filters.sys.mjs index 365f243456..4950bde273 100644 --- a/mail/extensions/openpgp/content/modules/filters.sys.mjs +++ b/mail/extensions/openpgp/content/modules/filters.sys.mjs @@ -12,7 +12,6 @@ ChromeUtils.defineESModuleGetters(lazy, { EnigmailCore: "chrome://openpgp/content/modules/core.sys.mjs", EnigmailFuncs: "chrome://openpgp/content/modules/funcs.sys.mjs", EnigmailKeyRing: "chrome://openpgp/content/modules/keyRing.sys.mjs", - EnigmailLog: "chrome://openpgp/content/modules/log.sys.mjs", EnigmailPersistentCrypto: "chrome://openpgp/content/modules/persistentCrypto.sys.mjs", EnigmailStreams: "chrome://openpgp/content/modules/streams.sys.mjs", @@ -21,28 +20,32 @@ ChromeUtils.defineESModuleGetters(lazy, { getMimeTree: "chrome://openpgp/content/modules/MimeTree.sys.mjs", jsmime: "resource:///modules/jsmime.sys.mjs", }); +ChromeUtils.defineLazyGetter(lazy, "log", () => { + return console.createInstance({ + prefix: "openpgp", + maxLogLevel: "Warn", + maxLogLevelPref: "openpgp.loglevel", + }); +}); const l10n = new Localization(["messenger/openpgp/openpgp.ftl"], true); var gNewMailListenerInitiated = false; /** - * filter action for creating a decrypted version of the mail and - * deleting the original mail at the same time + * Filter action for creating a decrypted version of the mail and + * deleting the original mail at the same time. */ - const filterActionMoveDecrypt = { - async applyAction(aMsgHdrs, aActionValue) { - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: filterActionMoveDecrypt: Move to: " + - aActionValue + - "\n" - ); - - for (const msgHdr of aMsgHdrs) { + /** + * @param {nsIMsgDBHdr{[]} hdrs - Messages to apply to. + * @param {string} actionValue - Action value. + */ + async applyAction(hdrs, actionValue) { + for (const msgHdr of hdrs) { await lazy.EnigmailPersistentCrypto.cryptMessage( msgHdr, - aActionValue, + actionValue, true, null ); @@ -67,47 +70,33 @@ const filterActionMoveDecrypt = { }; /** - * filter action for creating a decrypted copy of the mail, leaving the original - * message untouched + * Filter action for creating a decrypted copy of the mail, leaving the original + * message untouched. */ const filterActionCopyDecrypt = { - async applyAction(aMsgHdrs, aActionValue) { - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: filterActionCopyDecrypt: Copy to: " + - aActionValue + - "\n" - ); - - for (const msgHdr of aMsgHdrs) { + /** + * @param {nsIMsgDBHdr{[]} hdrs - Messages to apply to. + * @param {string} actionValue - Action value. + */ + async applyAction(hdrs, actionValue) { + for (const msgHdr of hdrs) { await lazy.EnigmailPersistentCrypto.cryptMessage( msgHdr, - aActionValue, + actionValue, false, null ); } }, - isValidForType(type) { - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: filterActionCopyDecrypt.isValidForType(" + type + ")\n" - ); - - const r = true; - return r; + isValidForType(_type) { + return true; }, validateActionValue(value) { - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: filterActionCopyDecrypt.validateActionValue(" + - value + - ")\n" - ); - if (value === "") { return l10n.formatValueSync("filter-folder-required"); } - return null; }, }; @@ -121,42 +110,33 @@ const filterActionEncrypt = { * @param {string} aActionValue - Key ID or recipient email address. * @param {nsIMsgCopyServiceListener} aListener */ - async applyAction(aMsgHdrs, aActionValue, aListener) { + /** + * @param {nsIMsgDBHdr{[]} hdrs - Messages to apply to. + * @param {string} actionValue - Action value. + * @param {nsIMsgCopyServiceListener} listener + */ + async applyAction(hdrs, actionValue, listener) { // Ensure KeyRing is loaded. lazy.EnigmailCore.init(); lazy.EnigmailKeyRing.getAllKeys(); - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: filterActionEncrypt: Encrypt to: " + aActionValue + "\n" - ); - let keyObj = lazy.EnigmailKeyRing.getKeyById(aActionValue); + let keyObj = lazy.EnigmailKeyRing.getKeyById(actionValue); if (keyObj === null) { - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: failed to find key by id: " + aActionValue + "\n" - ); - const keyId = lazy.EnigmailKeyRing.getValidKeyForRecipient(aActionValue); + const keyId = lazy.EnigmailKeyRing.getValidKeyForRecipient(actionValue); if (keyId) { keyObj = lazy.EnigmailKeyRing.getKeyById(keyId); } } - if (keyObj === null && aListener) { - lazy.EnigmailLog.DEBUG("filters.sys.mjs: no valid key - aborting\n"); - - aListener.onStartCopy(); - aListener.onStopCopy(Cr.NS_ERROR_ABORT); - + if (keyObj === null && listener) { + // No valid key, aborting. + listener.onStartCopy(); + listener.onStopCopy(Cr.NS_ERROR_ABORT); return; } - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: key to encrypt to: " + - JSON.stringify(keyObj) + - ", userId: " + - keyObj.userId + - "\n" - ); + lazy.log.debug(`Encrypting to 0x${keyObj.id} - ${keyObj.userId}`); // Maybe skip messages here if they are already encrypted to // the target key? There might be some use case for unconditionally @@ -165,7 +145,7 @@ const filterActionEncrypt = { // Also not encrypting to already encrypted messages would make the // behavior less transparent as it's not obvious. - for (const msgHdr of aMsgHdrs) { + for (const msgHdr of hdrs) { await lazy.EnigmailPersistentCrypto.cryptMessage( msgHdr, null /* same folder */, @@ -180,14 +160,10 @@ const filterActionEncrypt = { }, validateActionValue(value) { - // Initialize KeyRing. Ugly as it blocks the GUI but - // we need it. + // Initialize KeyRing. Ugly as it blocks the GUI but we need it. lazy.EnigmailCore.init(); lazy.EnigmailKeyRing.getAllKeys(); - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: validateActionValue: Encrypt to: " + value + "\n" - ); if (value === "") { return l10n.formatValueSync("filter-key-required"); } @@ -195,9 +171,6 @@ const filterActionEncrypt = { let keyObj = lazy.EnigmailKeyRing.getKeyById(value); if (keyObj === null) { - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: failed to find key by id. Looking for uid.\n" - ); const keyId = lazy.EnigmailKeyRing.getValidKeyForRecipient(value); if (keyId) { keyObj = lazy.EnigmailKeyRing.getKeyById(keyId); @@ -223,7 +196,6 @@ const filterActionEncrypt = { Services.prompt.alert(null, null, value); }); } - return null; }, }; @@ -263,7 +235,7 @@ function isPGPEncrypted(data) { } /** - * filter term for OpenPGP Encrypted mail + * Filter term for OpenPGP encrypted mail. */ const filterTermPGPEncrypted = { id: EnigmailConstants.FILTER_TERM_PGP_ENCRYPTED, @@ -280,9 +252,7 @@ const filterTermPGPEncrypted = { try { data = lazy.NetUtil.readInputStreamToString(stream, messageSize); } catch (ex) { - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: filterTermPGPEncrypted: failed to get data.\n" - ); + lazy.log.warn("Reading message data FAILED.", ex); // If we don't know better to return false. stream.close(); return false; @@ -313,8 +283,6 @@ const filterTermPGPEncrypted = { }; function initNewMailListener() { - lazy.EnigmailLog.DEBUG("filters.sys.mjs: initNewMailListener()\n"); - if (!gNewMailListenerInitiated) { const notificationService = Cc[ "@mozilla.org/messenger/msgnotificationservice;1" @@ -328,8 +296,6 @@ function initNewMailListener() { } function shutdownNewMailListener() { - lazy.EnigmailLog.DEBUG("filters.sys.mjs: shutdownNewMailListener()\n"); - if (gNewMailListenerInitiated) { const notificationService = Cc[ "@mozilla.org/messenger/msgnotificationservice;1" @@ -348,6 +314,7 @@ function getIdentityForSender(senderEmail, msgServer) { var consumerList = []; +/** @see {MimeTreeEmitter} */ function JsmimeEmitter(requireBody) { this.requireBody = requireBody; this.mimeTree = { @@ -376,17 +343,12 @@ JsmimeEmitter.prototype = { return this.mimeTree.subParts[0]; }, - /** JSMime API */ startMessage() { this.currentPart = this.mimeTree; }, endMessage() {}, startPart(partNum, headers) { - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: JsmimeEmitter.startPart: partNum=" + partNum + "\n" - ); - //this.stack.push(partNum); const newPart = this.createPartObj(partNum, headers, this.currentPart); if (partNum.indexOf(this.currPartNum) === 0) { @@ -400,19 +362,11 @@ JsmimeEmitter.prototype = { this.currentPart = newPart; }, - endPart(partNum) { - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: JsmimeEmitter.startPart: partNum=" + partNum + "\n" - ); + endPart(_partNum) { this.currentPart = this.currentPart.parent; }, deliverPartData(partNum, data) { - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: JsmimeEmitter.deliverPartData: partNum=" + - partNum + - "\n" - ); if (this.requireBody) { if (typeof data === "string") { this.currentPart.body += data; @@ -425,8 +379,6 @@ JsmimeEmitter.prototype = { }; function processIncomingMail(url, requireBody, aMsgHdr) { - lazy.EnigmailLog.DEBUG("filters.sys.mjs: processIncomingMail()\n"); - const inputStream = lazy.EnigmailStreams.newStringStreamListener(msgData => { const opt = { strformat: "unicode", @@ -442,11 +394,7 @@ function processIncomingMail(url, requireBody, aMsgHdr) { try { c.consumeMessage(e.getMimeTree(), msgData, aMsgHdr); } catch (ex) { - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: processIncomingMail: exception: " + - ex.toString() + - "\n" - ); + lazy.log.error("Processing incoming mail FAILED.", ex); } } } catch (ex) {} @@ -455,12 +403,8 @@ function processIncomingMail(url, requireBody, aMsgHdr) { try { const channel = lazy.EnigmailStreams.createChannel(url); channel.asyncOpen(inputStream, null); - } catch (e) { - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: processIncomingMail: open stream exception " + - e.toString() + - "\n" - ); + } catch (ex) { + lazy.log.error("Opening channel FAILED.", ex); } } @@ -506,12 +450,6 @@ function getRequireMessageProcessing(aMsgHdr) { } } - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: getRequireMessageProcessing: author: " + - aMsgHdr.author + - "\n" - ); - const u = lazy.EnigmailFuncs.getUrlFromUriSpec( aMsgHdr.folder.getUriForMsg(aMsgHdr) ); @@ -529,14 +467,9 @@ function getRequireMessageProcessing(aMsgHdr) { }; } +/** @implements {nsIMsgFolderListener} */ const newMailListener = { msgAdded(aMsgHdr) { - lazy.EnigmailLog.DEBUG( - "filters.sys.mjs: newMailListener.msgAdded() - got new mail in " + - aMsgHdr.folder.prettiestName + - "\n" - ); - if (consumerList.length === 0) { return; } @@ -548,15 +481,6 @@ const newMailListener = { }, }; -/** - messageStructure - Object: - - partNum: String - MIME part number - - headers: Object(nsIStructuredHeaders) - MIME part headers - - body: String or typedarray - the body part - - parent: Object(messageStructure) - link to the parent part - - subParts: Array of Object(messageStructure) - array of the sub-parts - */ - export var EnigmailFilters = { onStartup() { const filterService = Cc[ @@ -580,7 +504,7 @@ export var EnigmailFilters = { * @param {boolean} consumer.unreadOnly - Only process unread mails * @param {boolean} consumer.selfSentOnly - Only process mails with sender * Email == Account Email - * @param {Function} consumer.consumeMessage - Callback function(messageStructure, rawMessageData, nsIMsgHdr) + * @param {Function} consumer.consumeMessage - Callback function(mimeTreePart, rawMessageData, nsIMsgHdr) */ addNewMailConsumer(consumer) { consumerList.push(consumer); diff --git a/mail/extensions/openpgp/content/ui/keyDetailsDlg.js b/mail/extensions/openpgp/content/ui/keyDetailsDlg.js index 2851f7c5cb..ac7a778104 100644 --- a/mail/extensions/openpgp/content/ui/keyDetailsDlg.js +++ b/mail/extensions/openpgp/content/ui/keyDetailsDlg.js @@ -10,9 +10,6 @@ var { CommonUtils } = ChromeUtils.importESModule( var { EnigmailFuncs } = ChromeUtils.importESModule( "chrome://openpgp/content/modules/funcs.sys.mjs" ); -var { EnigmailLog } = ChromeUtils.importESModule( - "chrome://openpgp/content/modules/log.sys.mjs" -); var { EnigmailKey } = ChromeUtils.importESModule( "chrome://openpgp/content/modules/key.sys.mjs" );