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 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. define(["jquery", "codejar", "linenumbers", "prism"],
  21. function ($, CodeJar, withLineNumbers, Prism) {
  22. "use strict";
  23. var ui = {};
  24. ui.getActions = function getActions(rspamd, checked_server) {
  25. rspamd.query("actions", {
  26. success: function (data) {
  27. $("#actionsFormField").empty();
  28. var items = [];
  29. $.each(data[0].data, function (i, item) {
  30. var idx = -1;
  31. var label = null;
  32. if (item.action === "greylist") {
  33. label = "Greylist";
  34. idx = 0;
  35. } else if (item.action === "add header") {
  36. label = "Probably Spam";
  37. idx = 1;
  38. } else if (item.action === "rewrite subject") {
  39. label = "Rewrite subject";
  40. idx = 2;
  41. } else if (item.action === "reject") {
  42. label = "Spam";
  43. idx = 3;
  44. }
  45. if (idx >= 0) {
  46. items.push({
  47. idx: idx,
  48. html:
  49. '<div class="form-group">' +
  50. '<label class="col-form-label col-md-2 float-left">' + label + "</label>" +
  51. '<div class="controls slider-controls col-md-10">' +
  52. '<input class="action-scores form-control" data-id="action" type="number" value="' +
  53. item.value + '">' +
  54. "</div>" +
  55. "</div>"
  56. });
  57. }
  58. });
  59. items.sort(function (a, b) {
  60. return a.idx - b.idx;
  61. });
  62. $("#actionsFormField").html(
  63. items.map(function (e) {
  64. return e.html;
  65. }).join(""));
  66. },
  67. server: (checked_server === "All SERVERS") ? "local" : checked_server
  68. });
  69. };
  70. ui.saveActions = function (rspamd, server) {
  71. function descending(arr) {
  72. var desc = true;
  73. var filtered = arr.filter(function (el) {
  74. return el !== null;
  75. });
  76. for (var i = 0; i < filtered.length - 1; i++) {
  77. if (filtered[i + 1] >= filtered[i]) {
  78. desc = false;
  79. break;
  80. }
  81. }
  82. return desc;
  83. }
  84. var elts = (function () {
  85. var values = [];
  86. var inputs = $("#actionsForm :input[data-id=\"action\"]");
  87. // Rspamd order: [spam, rewrite_subject, probable_spam, greylist]
  88. values[0] = parseFloat(inputs[3].value);
  89. values[1] = parseFloat(inputs[2].value);
  90. values[2] = parseFloat(inputs[1].value);
  91. values[3] = parseFloat(inputs[0].value);
  92. return JSON.stringify(values);
  93. }());
  94. // String to array for comparison
  95. var eltsArray = JSON.parse(elts);
  96. if (eltsArray[0] < 0) {
  97. rspamd.alertMessage("alert-modal alert-error", "Spam can not be negative");
  98. } else if (eltsArray[1] < 0) {
  99. rspamd.alertMessage("alert-modal alert-error", "Rewrite subject can not be negative");
  100. } else if (eltsArray[2] < 0) {
  101. rspamd.alertMessage("alert-modal alert-error", "Probable spam can not be negative");
  102. } else if (eltsArray[3] < 0) {
  103. rspamd.alertMessage("alert-modal alert-error", "Greylist can not be negative");
  104. } else if (descending(eltsArray)) {
  105. rspamd.query("saveactions", {
  106. method: "POST",
  107. params: {
  108. data: elts,
  109. dataType: "json"
  110. },
  111. server: server
  112. });
  113. } else {
  114. rspamd.alertMessage("alert-modal alert-error", "Incorrect order of actions thresholds");
  115. }
  116. };
  117. ui.getMaps = function (rspamd, checked_server) {
  118. var $listmaps = $("#listMaps");
  119. $listmaps.closest(".card").hide();
  120. rspamd.query("maps", {
  121. success: function (json) {
  122. var data = json[0].data;
  123. $listmaps.empty();
  124. $("#modalBody").empty();
  125. var $tbody = $("<tbody>");
  126. $.each(data, function (i, item) {
  127. var label = (item.editable === false || rspamd.read_only)
  128. ? "<span class=\"badge badge-secondary\">Read</span>"
  129. : "<span class=\"badge badge-secondary\">Read</span>&nbsp;<span class=\"badge badge-success\">Write</span>";
  130. var $tr = $("<tr>");
  131. $("<td class=\"col-lg-2 maps-cell\">" + label + "</td>").appendTo($tr);
  132. var $span = $("<span class=\"map-link\" data-toggle=\"modal\" data-target=\"#modalDialog\">" + item.uri + "</span>").data("item", item);
  133. $span.wrap("<td>").parent().appendTo($tr);
  134. $("<td>" + item.description + "</td>").appendTo($tr);
  135. $tr.appendTo($tbody);
  136. });
  137. $tbody.appendTo($listmaps);
  138. $listmaps.closest(".card").show();
  139. },
  140. server: (checked_server === "All SERVERS") ? "local" : checked_server
  141. });
  142. };
  143. ui.setup = function (rspamd) {
  144. var jar = {};
  145. // CodeJar requires ES6
  146. var editor = window.CodeJar &&
  147. // Required to restore cursor position
  148. (typeof window.getSelection().setBaseAndExtent === "function")
  149. ? {
  150. codejar: true,
  151. elt: "div",
  152. class: "editor language-clike",
  153. }
  154. // Fallback to textarea if the browser does not support ES6
  155. : {
  156. elt: "textarea",
  157. class: "form-control map-textarea",
  158. };
  159. // Modal form for maps
  160. $(document).on("click", "[data-toggle=\"modal\"]", function () {
  161. var checked_server = rspamd.getSelector("selSrv");
  162. var item = $(this).data("item");
  163. rspamd.query("getmap", {
  164. headers: {
  165. Map: item.map
  166. },
  167. success: function (data) {
  168. var readonly = "";
  169. var icon = "fa-edit";
  170. var text = data[0].data;
  171. if (item.editable === false || rspamd.read_only) {
  172. readonly = " readonly";
  173. icon = "fa-eye";
  174. $("#modalSaveGroup").hide();
  175. } else {
  176. $("#modalSaveGroup").show();
  177. }
  178. $("#modalDialog .modal-header").find("[data-fa-i2svg]").addClass(icon);
  179. $("#modalTitle").html(item.uri);
  180. $("<" + editor.elt + ' id="editor" class="' + editor.class + '"' + readonly +
  181. ' data-id="' + item.map + '">' +
  182. text +
  183. "</" + editor.elt + ">").appendTo("#modalBody");
  184. if (editor.codejar) {
  185. jar = new CodeJar(
  186. document.querySelector("#editor"),
  187. withLineNumbers(Prism.highlightElement)
  188. );
  189. }
  190. $("#modalDialog").modal("show");
  191. },
  192. errorMessage: "Cannot receive maps data",
  193. server: (checked_server === "All SERVERS") ? "local" : checked_server
  194. });
  195. return false;
  196. });
  197. $("#modalDialog").on("hidden.bs.modal", function () {
  198. if (editor.codejar) {
  199. jar.destroy();
  200. $(".codejar-wrap").remove();
  201. } else {
  202. $("#editor").remove();
  203. }
  204. });
  205. $("#saveActionsBtn").on("click", function () {
  206. ui.saveActions(rspamd);
  207. });
  208. $("#saveActionsClusterBtn").on("click", function () {
  209. ui.saveActions(rspamd, "All SERVERS");
  210. });
  211. function saveMap(server) {
  212. rspamd.query("savemap", {
  213. success: function () {
  214. rspamd.alertMessage("alert-success", "Map data successfully saved");
  215. $("#modalDialog").modal("hide");
  216. },
  217. errorMessage: "Save map error",
  218. method: "POST",
  219. headers: {
  220. Map: $("#editor").data("id"),
  221. },
  222. params: {
  223. data: editor.codejar ? jar.toString() : $("#editor").val(),
  224. dataType: "text",
  225. },
  226. server: server
  227. });
  228. }
  229. $("#modalSave").on("click", function () {
  230. saveMap();
  231. });
  232. $("#modalSaveAll").on("click", function () {
  233. saveMap("All SERVERS");
  234. });
  235. };
  236. return ui;
  237. });