You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

config.js 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. /*
  2. The MIT License (MIT)
  3. Copyright (C) 2017 Vsevolod Stakhov <vsevolod@highsecure.ru>
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. */
  20. /* global require */
  21. define(["jquery", "app/common"],
  22. ($, common) => {
  23. "use strict";
  24. const ui = {};
  25. ui.getActions = function getActions() {
  26. common.query("actions", {
  27. success: function (data) {
  28. $("#actionsFormField").empty();
  29. const items = [];
  30. $.each(data[0].data, (i, item) => {
  31. const actionsOrder = ["greylist", "add header", "rewrite subject", "reject"];
  32. const idx = actionsOrder.indexOf(item.action);
  33. if (idx >= 0) {
  34. items.push({
  35. idx: idx,
  36. html:
  37. '<div class="form-group">' +
  38. '<label class="col-form-label col-md-2 float-start">' + item.action + "</label>" +
  39. '<div class="controls slider-controls col-md-10">' +
  40. '<input class="action-scores form-control" data-id="action" type="number" value="' +
  41. item.value + '">' +
  42. "</div>" +
  43. "</div>"
  44. });
  45. }
  46. });
  47. items.sort((a, b) => a.idx - b.idx);
  48. $("#actionsFormField").html(
  49. items.map((e) => e.html).join(""));
  50. },
  51. server: common.getServer()
  52. });
  53. };
  54. ui.saveActions = function (server) {
  55. function descending(arr) {
  56. let desc = true;
  57. const filtered = arr.filter((el) => el !== null);
  58. for (let i = 0; i < filtered.length - 1; i++) {
  59. if (filtered[i + 1] >= filtered[i]) {
  60. desc = false;
  61. break;
  62. }
  63. }
  64. return desc;
  65. }
  66. const elts = (function () {
  67. const values = [];
  68. const inputs = $("#actionsForm :input[data-id=\"action\"]");
  69. // Rspamd order: [spam, rewrite_subject, probable_spam, greylist]
  70. values[0] = parseFloat(inputs[3].value);
  71. values[1] = parseFloat(inputs[2].value);
  72. values[2] = parseFloat(inputs[1].value);
  73. values[3] = parseFloat(inputs[0].value);
  74. return JSON.stringify(values);
  75. }());
  76. // String to array for comparison
  77. const eltsArray = JSON.parse(elts);
  78. if (eltsArray[0] < 0) {
  79. common.alertMessage("alert-modal alert-error", "Spam can not be negative");
  80. } else if (eltsArray[1] < 0) {
  81. common.alertMessage("alert-modal alert-error", "Rewrite subject can not be negative");
  82. } else if (eltsArray[2] < 0) {
  83. common.alertMessage("alert-modal alert-error", "Probable spam can not be negative");
  84. } else if (eltsArray[3] < 0) {
  85. common.alertMessage("alert-modal alert-error", "Greylist can not be negative");
  86. } else if (descending(eltsArray)) {
  87. common.query("saveactions", {
  88. method: "POST",
  89. params: {
  90. data: elts,
  91. dataType: "json"
  92. },
  93. server: server
  94. });
  95. } else {
  96. common.alertMessage("alert-modal alert-error", "Incorrect order of actions thresholds");
  97. }
  98. };
  99. ui.getMaps = function () {
  100. const $listmaps = $("#listMaps");
  101. $listmaps.closest(".card").hide();
  102. common.query("maps", {
  103. success: function (json) {
  104. const [{data}] = json;
  105. $listmaps.empty();
  106. $("#modalBody").empty();
  107. const $tbody = $("<tbody>");
  108. $.each(data, (i, item) => {
  109. let $td = '<td><span class="badge text-bg-secondary">Read</span></td>';
  110. if (!(item.editable === false || common.read_only)) {
  111. $td = $($td).append('&nbsp;<span class="badge text-bg-success">Write</span>');
  112. }
  113. const $tr = $("<tr>").append($td);
  114. const $span = $('<span class="map-link" data-bs-toggle="modal" data-bs-target="#modalDialog">' +
  115. item.uri + "</span>").data("item", item);
  116. $span.wrap("<td>").parent().appendTo($tr);
  117. $("<td>" + item.description + "</td>").appendTo($tr);
  118. $tr.appendTo($tbody);
  119. });
  120. $tbody.appendTo($listmaps);
  121. $listmaps.closest(".card").show();
  122. },
  123. server: common.getServer()
  124. });
  125. };
  126. let jar = {};
  127. const editor = {
  128. advanced: {
  129. codejar: true,
  130. elt: "div",
  131. class: "editor language-clike",
  132. readonly_attr: {contenteditable: false},
  133. },
  134. basic: {
  135. elt: "textarea",
  136. class: "form-control map-textarea",
  137. readonly_attr: {readonly: true},
  138. }
  139. };
  140. let mode = "advanced";
  141. // Modal form for maps
  142. $(document).on("click", "[data-bs-toggle=\"modal\"]", function () {
  143. const item = $(this).data("item");
  144. common.query("getmap", {
  145. headers: {
  146. Map: item.map
  147. },
  148. success: function (data) {
  149. // Highlighting a large amount of text is unresponsive
  150. mode = (new Blob([data[0].data]).size > 5120) ? "basic" : $("input[name=editorMode]:checked").val();
  151. $("<" + editor[mode].elt + ' id="editor" class="' + editor[mode].class + '" data-id="' + item.map +
  152. '"></' + editor[mode].elt + ">").appendTo("#modalBody");
  153. if (editor[mode].codejar) {
  154. require(["codejar", "linenumbers", "prism"], (CodeJar, withLineNumbers, Prism) => {
  155. jar = new CodeJar(
  156. document.querySelector("#editor"),
  157. withLineNumbers((el) => Prism.highlightElement(el))
  158. );
  159. jar.updateCode(data[0].data);
  160. });
  161. } else {
  162. document.querySelector("#editor").innerHTML = common.escapeHTML(data[0].data);
  163. }
  164. let icon = "fa-edit";
  165. if (item.editable === false || common.read_only) {
  166. $("#editor").attr(editor[mode].readonly_attr);
  167. icon = "fa-eye";
  168. $("#modalSaveGroup").hide();
  169. } else {
  170. $("#modalSaveGroup").show();
  171. }
  172. $("#modalDialog .modal-header").find("[data-fa-i2svg]").addClass(icon);
  173. $("#modalTitle").html(item.uri);
  174. $("#modalDialog").modal("show");
  175. },
  176. errorMessage: "Cannot receive maps data",
  177. server: common.getServer()
  178. });
  179. return false;
  180. });
  181. $("#modalDialog").on("hidden.bs.modal", () => {
  182. if (editor[mode].codejar) {
  183. jar.destroy();
  184. $(".codejar-wrap").remove();
  185. } else {
  186. $("#editor").remove();
  187. }
  188. });
  189. $("#saveActionsBtn").on("click", () => {
  190. ui.saveActions();
  191. });
  192. $("#saveActionsClusterBtn").on("click", () => {
  193. ui.saveActions("All SERVERS");
  194. });
  195. function saveMap(server) {
  196. common.query("savemap", {
  197. success: function () {
  198. common.alertMessage("alert-success", "Map data successfully saved");
  199. $("#modalDialog").modal("hide");
  200. },
  201. errorMessage: "Save map error",
  202. method: "POST",
  203. headers: {
  204. Map: $("#editor").data("id"),
  205. },
  206. params: {
  207. data: editor[mode].codejar ? jar.toString() : $("#editor").val(),
  208. dataType: "text",
  209. },
  210. server: server
  211. });
  212. }
  213. $("#modalSave").on("click", () => {
  214. saveMap();
  215. });
  216. $("#modalSaveAll").on("click", () => {
  217. saveMap("All SERVERS");
  218. });
  219. return ui;
  220. });