aboutsummaryrefslogtreecommitdiffstats
path: root/interface/js/app/selectors.js
blob: 759e3ea16817ffa416a77a980664d22c36b1bd4c (plain)
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
define(["jquery", "app/rspamd"],
    function ($, rspamd) {
        "use strict";
        const ui = {};

        function enable_disable_check_btn() {
            $("#selectorsChkMsgBtn").prop("disabled", (
                $.trim($("#selectorsMsgArea").val()).length === 0 ||
                !$("#selectorsSelArea").hasClass("is-valid")
            ));
        }

        function get_server() {
            const checked_server = rspamd.getSelector("selSrv");
            return (checked_server === "All SERVERS") ? "local" : checked_server;
        }

        function checkMsg(data) {
            const selector = $("#selectorsSelArea").val();
            rspamd.query("plugins/selectors/check_message?selector=" + encodeURIComponent(selector), {
                data: data,
                method: "POST",
                success: function (neighbours_status) {
                    const json = neighbours_status[0].data;
                    if (json.success) {
                        rspamd.alertMessage("alert-success", "Message successfully processed");
                        $("#selectorsResArea")
                            .val(Object.prototype.hasOwnProperty.call(json, "data") ? json.data.toString() : "");
                    } else {
                        rspamd.alertMessage("alert-error", "Unexpected error processing message");
                    }
                },
                server: get_server()
            });
        }

        function checkSelectors() {
            function toggle_form_group_class(remove, add) {
                $("#selectorsSelArea").removeClass("is-" + remove).addClass("is-" + add);
                enable_disable_check_btn();
            }
            const selector = $("#selectorsSelArea").val();
            if (selector.length && !rspamd.read_only ) {
                rspamd.query("plugins/selectors/check_selector?selector=" + encodeURIComponent(selector), {
                    method: "GET",
                    success: function (json) {
                        if (json[0].data.success) {
                            toggle_form_group_class("invalid", "valid");
                        } else {
                            toggle_form_group_class("valid", "invalid");
                        }
                    },
                    server: get_server()
                });
            } else {
                $("#selectorsSelArea").removeClass("is-valid is-invalid");
                enable_disable_check_btn();
            }
        }

        function buildLists() {
            function build_table_from_json(json, table_id) {
                Object.keys(json).forEach(function (key) {
                    const td = $("<td/>");
                    const tr = $("<tr/>")
                        .append(td.clone().html("<code>" + key + "</code>"))
                        .append(td.clone().html(json[key].description));
                    $(table_id + " tbody").append(tr);
                });
            }

            function getList(list) {
                rspamd.query("plugins/selectors/list_" + list, {
                    method: "GET",
                    success: function (neighbours_status) {
                        const json = neighbours_status[0].data;
                        build_table_from_json(json, "#selectorsTable-" + list);
                    },
                    server: get_server()
                });
            }

            getList("extractors");
            getList("transforms");
        }

        ui.displayUI = function () {
            if (!rspamd.read_only &&
                !$("#selectorsTable-extractors>tbody>tr").length &&
                !$("#selectorsTable-transforms>tbody>tr").length) buildLists();
            if (!$("#selectorsSelArea").is(".is-valid, .is-invalid")) checkSelectors();
        };


        function toggleSidebar(side) {
            $("#sidebar-" + side).toggleClass("collapsed");
            let contentClass = "col-lg-6";
            const openSidebarsCount = $("#sidebar-left").hasClass("collapsed") +
                    $("#sidebar-right").hasClass("collapsed");
            switch (openSidebarsCount) {
                case 1:
                    contentClass = "col-lg-9";
                    break;
                case 2:
                    contentClass = "col-lg-12";
                    break;
                default:
            }
            $("#content").removeClass("col-lg-12 col-lg-9 col-lg-6")
                .addClass(contentClass);
        }
        $("#sidebar-tab-left>a").click(function () {
            toggleSidebar("left");
            return false;
        });
        $("#sidebar-tab-right>a").click(function () {
            toggleSidebar("right");
            return false;
        });

        $("#selectorsMsgClean").on("click", function () {
            $("#selectorsChkMsgBtn").attr("disabled", true);
            $("#selectorsMsgArea").val("");
            return false;
        });
        $("#selectorsClean").on("click", function () {
            $("#selectorsSelArea").val("");
            checkSelectors();
            return false;
        });
        $("#selectorsChkMsgBtn").on("click", function () {
            $("#selectorsResArea").val("");
            checkMsg($("#selectorsMsgArea").val());
            return false;
        });

        $("#selectorsMsgArea").on("input", function () {
            enable_disable_check_btn();
        });
        $("#selectorsSelArea").on("input", function () {
            checkSelectors();
        });

        return ui;
    });