<commit_before>/* global RspamdSpamness:false, RspamdSpamnessColumn:false */

"use strict";

RspamdSpamness.Message = {};

/**
 * Display headers in msgHeaderView box.
 * @param {boolean} [update_rules=false] - Just redraw "Rules" header, leave other headers as they are.
 */
RspamdSpamness.Message.displayHeaders = function (update_rules) {
    const {prefs} = Services;

    var id = {
        score: {
            hdr: {
                bayes: {
                    icon:  "rspamdSpamnessBayesIcon",
                    score: "rspamdSpamnessBayesHeader"
                },
                fuzzy: {
                    icon:  "rspamdSpamnessFuzzyIcon",
                    score: "rspamdSpamnessFuzzyHeader"
                },
                score: {
                    icon:  "rspamdSpamnessScoreIcon",
                    score: "rspamdSpamnessScoreHeader"
                }
            }
        }
    };

    var el = {
        greyl: {
            hdr: getEl("expandedRspamdSpamnessGreylistHeader"),
            row: getEl("expandedRspamdSpamnessGreylistRow")
        },
        rules: {
            box: getEl("expandedRspamdSpamnessRulesBox"),
            row: getEl("expandedRspamdSpamnessRulesRow")
        },
        scanTime: getEl("rspamdSpamnessScanTimeHeader"),
        score: {
            row: getEl("expandedRspamdSpamnessRow")
        }
    };

    var show = {
        greyl: update_rules ? false : getPref("extensions.rspamd-spamness.display.messageGreylist"),
        rules: getPref("extensions.rspamd-spamness.display.messageRules"),
        score: update_rules ? false : getPref("extensions.rspamd-spamness.display.messageScore")
    };

    function getEl(id) {
        return document.getElementById(id);
    }

    function getPref(prefName) {
        try {
            return prefs.getBoolPref(prefName);
        } catch (e) {
            RspamdSpamness.err(e);
        }
    }

    function getHeaderBody(msgHeaders, name) {
        let headerBody = [];
        if (name in msgHeaders) {
            msgHeaders[name].forEach(function (body) {
                if (body !== null)
                    headerBody.push(body);
            });
        }
        return headerBody;
    }

    function b64DecodeUnicode(str) {
        return decodeURIComponent(atob(str).split("").map(function (c) {
            return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
        }).join(""));
    }

    if (!update_rules) {
        el.greyl.row.collapsed = true;
        el.score.row.collapsed = true;
        el.rules.row.collapsed = true;
    }

    // Height could be set on more indicator click. Remove it.
    getEl("expandedHeaderView").removeAttribute("height");

    const msg = gMessageDisplay.displayedMessage;

    if (show.greyl && msg.folder) {
        MsgHdrToMimeMessage(msg, null, function (aMsgHdr, aMimeMsg) {
            const greylistHeaders = getHeaderBody(aMimeMsg.headers, "x-rmilter-greylist");
            el.greyl.row.collapsed = (greylistHeaders.length === 0);
            el.greyl.hdr.headerValue = greylistHeaders;
            el.greyl.hdr.valid = true;
        }, true, {
            partsOnDemand: true
        });
    }

    if (!show.score && !show.rules)
        return;

    if (gDBView.msgFolder === null)
        return;

    var hdr = gDBView.msgFolder.GetMessageHeader(gDBView.getKeyAt(gDBView.currentlyDisplayedMessage));

    if (!hdr)
        return;

    // Get symbols from Rmilter header
    RspamdSpamness.Message.headerStr = RspamdSpamness.getHeaderStr(hdr);
    if (RspamdSpamness.Message.headerStr) {

        const converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
            .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
        converter.charset = "UTF-8";
        RspamdSpamness.Message.headerStr = converter.ConvertToUnicode(RspamdSpamness.Message.headerStr);

        const s = RspamdSpamness.Message.headerStr.match(/: \S+ \[[-\d.]+ \/ [-\d.]+\] *(.*)$/);
        if (s) {
            displayScoreRulesHeaders(s[1]);
            return;
        }
    }

    // Get symbols from Exim header
    if (msg.folder) {
        MsgHdrToMimeMessage(msg, null, function (aMsgHdr, aMimeMsg) {
            [RspamdSpamness.Message.headerStr] = getHeaderBody(aMimeMsg.headers, "x-spam-report");
            if (RspamdSpamness.Message.headerStr) {
                const s = RspamdSpamness.Message.headerStr.match(/^Action: [ a-z]+?(Symbol: .*)Message-ID:/);
                if (s) {
                    displayScoreRulesHeaders(s[1]);
                    return;
                }
            }
        }, true, {
            partsOnDemand: true
        });
    }

    // Get symbols from LDA mode header
    if (msg.folder) {
        MsgHdrToMimeMessage(msg, null, function (aMsgHdr, aMimeMsg) {
            const hdrBody = getHeaderBody(aMimeMsg.headers, "x-spam-result");
            if (hdrBody) {
                RspamdSpamness.Message.headerStr = b64DecodeUnicode(hdrBody);
                if (RspamdSpamness.Message.headerStr) {
                    const metric = JSON.parse(RspamdSpamness.Message.headerStr).default;
                    var s = null;
                    for (var item in metric) {
                        let symbol = metric[item];
                        if (symbol.name) {
                            s += " " + symbol.name +
                                "(" + symbol.score.toFixed(2) + ")" +
                                "[" + (symbol.options ? symbol.options.join(", ") : "") + "]";
                        }
                    }
                    if (s) {
                        displayScoreRulesHeaders(s);
                    }
                }
            }
        }, true, {
            partsOnDemand: true
        });
    }

    return;

    function displayScoreRulesHeaders(symbols) {
        if (show.score) {
            var parsed = [];
            parsed.score = RspamdSpamnessColumn.getScoreByHdr(hdr);<commit_msg>[Refactor] Remove redundant "return" statements<commit_after>/* global RspamdSpamness:false, RspamdSpamnessColumn:false */

"use strict";

RspamdSpamness.Message = {};

/**
 * Display headers in msgHeaderView box.
 * @param {boolean} [update_rules=false] - Just redraw "Rules" header, leave other headers as they are.
 */
RspamdSpamness.Message.displayHeaders = function (update_rules) {
    const {prefs} = Services;

    var id = {
        score: {
            hdr: {
                bayes: {
                    icon:  "rspamdSpamnessBayesIcon",
                    score: "rspamdSpamnessBayesHeader"
                },
                fuzzy: {
                    icon:  "rspamdSpamnessFuzzyIcon",
                    score: "rspamdSpamnessFuzzyHeader"
                },
                score: {
                    icon:  "rspamdSpamnessScoreIcon",
                    score: "rspamdSpamnessScoreHeader"
                }
            }
        }
    };

    var el = {
        greyl: {
            hdr: getEl("expandedRspamdSpamnessGreylistHeader"),
            row: getEl("expandedRspamdSpamnessGreylistRow")
        },
        rules: {
            box: getEl("expandedRspamdSpamnessRulesBox"),
            row: getEl("expandedRspamdSpamnessRulesRow")
        },
        scanTime: getEl("rspamdSpamnessScanTimeHeader"),
        score: {
            row: getEl("expandedRspamdSpamnessRow")
        }
    };

    var show = {
        greyl: update_rules ? false : getPref("extensions.rspamd-spamness.display.messageGreylist"),
        rules: getPref("extensions.rspamd-spamness.display.messageRules"),
        score: update_rules ? false : getPref("extensions.rspamd-spamness.display.messageScore")
    };

    function getEl(id) {
        return document.getElementById(id);
    }

    function getPref(prefName) {
        try {
            return prefs.getBoolPref(prefName);
        } catch (e) {
            RspamdSpamness.err(e);
        }
    }

    function getHeaderBody(msgHeaders, name) {
        let headerBody = [];
        if (name in msgHeaders) {
            msgHeaders[name].forEach(function (body) {
                if (body !== null)
                    headerBody.push(body);
            });
        }
        return headerBody;
    }

    function b64DecodeUnicode(str) {
        return decodeURIComponent(atob(str).split("").map(function (c) {
            return "%" + ("00" + c.charCodeAt(0).toString(16)).slice(-2);
        }).join(""));
    }

    if (!update_rules) {
        el.greyl.row.collapsed = true;
        el.score.row.collapsed = true;
        el.rules.row.collapsed = true;
    }

    // Height could be set on more indicator click. Remove it.
    getEl("expandedHeaderView").removeAttribute("height");

    const msg = gMessageDisplay.displayedMessage;

    if (show.greyl && msg.folder) {
        MsgHdrToMimeMessage(msg, null, function (aMsgHdr, aMimeMsg) {
            const greylistHeaders = getHeaderBody(aMimeMsg.headers, "x-rmilter-greylist");
            el.greyl.row.collapsed = (greylistHeaders.length === 0);
            el.greyl.hdr.headerValue = greylistHeaders;
            el.greyl.hdr.valid = true;
        }, true, {
            partsOnDemand: true
        });
    }

    if (!show.score && !show.rules)
        return;

    if (gDBView.msgFolder === null)
        return;

    var hdr = gDBView.msgFolder.GetMessageHeader(gDBView.getKeyAt(gDBView.currentlyDisplayedMessage));

    if (!hdr)
        return;

    // Get symbols from Rmilter header
    RspamdSpamness.Message.headerStr = RspamdSpamness.getHeaderStr(hdr);
    if (RspamdSpamness.Message.headerStr) {

        const converter = Components.classes["@mozilla.org/intl/scriptableunicodeconverter"]
            .createInstance(Components.interfaces.nsIScriptableUnicodeConverter);
        converter.charset = "UTF-8";
        RspamdSpamness.Message.headerStr = converter.ConvertToUnicode(RspamdSpamness.Message.headerStr);

        const s = RspamdSpamness.Message.headerStr.match(/: \S+ \[[-\d.]+ \/ [-\d.]+\] *(.*)$/);
        if (s) {
            displayScoreRulesHeaders(s[1]);
            return;
        }
    }

    // Get symbols from Exim header
    if (msg.folder) {
        MsgHdrToMimeMessage(msg, null, function (aMsgHdr, aMimeMsg) {
            [RspamdSpamness.Message.headerStr] = getHeaderBody(aMimeMsg.headers, "x-spam-report");
            if (RspamdSpamness.Message.headerStr) {
                const s = RspamdSpamness.Message.headerStr.match(/^Action: [ a-z]+?(Symbol: .*)Message-ID:/);
                if (s) {
                    displayScoreRulesHeaders(s[1]);
                }
            }
        }, true, {
            partsOnDemand: true
        });
    }

    // Get symbols from LDA mode header
    if (msg.folder) {
        MsgHdrToMimeMessage(msg, null, function (aMsgHdr, aMimeMsg) {
            const hdrBody = getHeaderBody(aMimeMsg.headers, "x-spam-result");
            if (hdrBody) {
                RspamdSpamness.Message.headerStr = b64DecodeUnicode(hdrBody);
                if (RspamdSpamness.Message.headerStr) {
                    const metric = JSON.parse(RspamdSpamness.Message.headerStr).default;
                    var s = null;
                    for (var item in metric) {
                        let symbol = metric[item];
                        if (symbol.name) {
                            s += " " + symbol.name +
                                "(" + symbol.score.toFixed(2) + ")" +
                                "[" + (symbol.options ? symbol.options.join(", ") : "") + "]";
                        }
                    }
                    if (s) {
                        displayScoreRulesHeaders(s);
                    }
                }
            }
        }, true, {
            partsOnDemand: true
        });
    }

    function displayScoreRulesHeaders(symbols) {
        if (show.score) {
            var parsed = [];
            parsed.score = RspamdSpamnessColumn.getScoreByHdr(hdr);
{'fraction_non_alphanumeric': 0.11807933194154488, 'fraction_numerical': 0.0025052192066805845, 'mean_word_length': 1.7518382352941178, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2543001', 'n_tokens_mistral': 3654, 'n_tokens_neox': 3540, 'n_words': 581}