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.

rspamd.js 41KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050
  1. /*
  2. The MIT License (MIT)
  3. Copyright (C) 2012-2013 Anton Simonov <untone@gmail.com>
  4. Copyright (C) 2014-2017 Vsevolod Stakhov <vsevolod@highsecure.ru>
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. */
  21. /* global jQuery:false, FooTable:false, Visibility:false */
  22. define(["jquery", "visibility", "nprogress", "stickytabs", "app/stats", "app/graph", "app/config",
  23. "app/symbols", "app/history", "app/upload", "app/selectors"],
  24. // eslint-disable-next-line max-params
  25. function ($, visibility, NProgress, stickyTabs, tab_stat, tab_graph, tab_config,
  26. tab_symbols, tab_history, tab_upload, tab_selectors) {
  27. "use strict";
  28. var ui = {
  29. chartLegend: [
  30. {label: "reject", color: "#FF0000"},
  31. {label: "soft reject", color: "#BF8040"},
  32. {label: "rewrite subject", color: "#FF6600"},
  33. {label: "add header", color: "#FFAD00"},
  34. {label: "greylist", color: "#436EEE"},
  35. {label: "no action", color: "#66CC00"}
  36. ],
  37. page_size: {
  38. scan: 25,
  39. errors: 25,
  40. history: 25
  41. },
  42. symbols: {
  43. scan: [],
  44. history: []
  45. }
  46. };
  47. const defaultAjaxTimeout = 20000;
  48. const ajaxTimeoutBox = ".popover #settings-popover #ajax-timeout";
  49. var graphs = {};
  50. var tables = {};
  51. var neighbours = []; // list of clusters
  52. var checked_server = "All SERVERS";
  53. var timer_id = [];
  54. var locale = (localStorage.getItem("selected_locale") === "custom") ? localStorage.getItem("custom_locale") : null;
  55. var selData = null; // Graph's dataset selector state
  56. NProgress.configure({
  57. minimum: 0.01,
  58. showSpinner: false,
  59. });
  60. function ajaxSetup(ajax_timeout, setFieldValue, saveToLocalStorage) {
  61. const timeout = (ajax_timeout && ajax_timeout >= 0) ? ajax_timeout : defaultAjaxTimeout;
  62. if (saveToLocalStorage) localStorage.setItem("ajax_timeout", timeout);
  63. if (setFieldValue) $(ajaxTimeoutBox).val(timeout);
  64. $.ajaxSetup({
  65. timeout: timeout,
  66. jsonp: false
  67. });
  68. }
  69. function cleanCredentials() {
  70. sessionStorage.clear();
  71. $("#statWidgets").empty();
  72. $("#listMaps").empty();
  73. $("#modalBody").empty();
  74. }
  75. function stopTimers() {
  76. for (var key in timer_id) {
  77. if (!{}.hasOwnProperty.call(timer_id, key)) continue;
  78. Visibility.stop(timer_id[key]);
  79. }
  80. }
  81. function disconnect() {
  82. [graphs, tables].forEach(function (o) {
  83. Object.keys(o).forEach(function (key) {
  84. o[key].destroy();
  85. delete o[key];
  86. });
  87. });
  88. // Remove jquery-stickytabs listeners
  89. $(window).off("hashchange");
  90. $(".nav-tabs-sticky > .nav-item > .nav-link").off("click").removeClass("active");
  91. stopTimers();
  92. cleanCredentials();
  93. ui.connect();
  94. }
  95. function tabClick(id) {
  96. var tab_id = id;
  97. if ($(id).attr("disabled")) return;
  98. var navBarControls = $("#selSrv, #navBar li, #navBar a, #navBar button");
  99. if (id !== "#autoRefresh") navBarControls.attr("disabled", true).addClass("disabled", true);
  100. stopTimers();
  101. if (id === "#refresh" || id === "#autoRefresh") {
  102. tab_id = "#" + $(".nav-link.active").attr("id");
  103. }
  104. $("#autoRefresh").hide();
  105. $("#refresh").addClass("radius-right");
  106. function setAutoRefresh(refreshInterval, timer, callback) {
  107. function countdown(interval) {
  108. Visibility.stop(timer_id.countdown);
  109. if (!interval) {
  110. $("#countdown").text("--:--");
  111. return;
  112. }
  113. var timeLeft = interval;
  114. $("#countdown").text("00:00");
  115. timer_id.countdown = Visibility.every(1000, 1000, function () {
  116. timeLeft -= 1000;
  117. $("#countdown").text(new Date(timeLeft).toISOString().substr(14, 5));
  118. if (timeLeft <= 0) Visibility.stop(timer_id.countdown);
  119. });
  120. }
  121. $("#refresh").removeClass("radius-right");
  122. $("#autoRefresh").show();
  123. countdown(refreshInterval);
  124. if (!refreshInterval) return;
  125. timer_id[timer] = Visibility.every(refreshInterval, function () {
  126. countdown(refreshInterval);
  127. if ($("#refresh").attr("disabled")) return;
  128. $("#refresh").attr("disabled", true).addClass("disabled", true);
  129. callback();
  130. });
  131. }
  132. if (["#scan_nav", "#selectors_nav", "#disconnect"].indexOf(tab_id) !== -1) {
  133. $("#refresh").hide();
  134. } else {
  135. $("#refresh").show();
  136. }
  137. switch (tab_id) {
  138. case "#status_nav":
  139. (function () {
  140. var refreshInterval = $(".dropdown-menu a.active.preset").data("value");
  141. setAutoRefresh(refreshInterval, "status",
  142. function () { return tab_stat.statWidgets(ui, graphs, checked_server); });
  143. if (id !== "#autoRefresh") tab_stat.statWidgets(ui, graphs, checked_server);
  144. $(".preset").show();
  145. $(".history").hide();
  146. $(".dynamic").hide();
  147. }());
  148. break;
  149. case "#throughput_nav":
  150. (function () {
  151. var step = {
  152. day: 60000,
  153. week: 300000
  154. };
  155. var refreshInterval = step[selData] || 3600000;
  156. $("#dynamic-item").text((refreshInterval / 60000) + " min");
  157. if (!$(".dropdown-menu a.active.dynamic").data("value")) {
  158. refreshInterval = null;
  159. }
  160. setAutoRefresh(refreshInterval, "throughput",
  161. function () { return tab_graph.draw(ui, graphs, tables, neighbours, checked_server, selData); });
  162. if (id !== "#autoRefresh") tab_graph.draw(ui, graphs, tables, neighbours, checked_server, selData);
  163. $(".preset").hide();
  164. $(".history").hide();
  165. $(".dynamic").show();
  166. }());
  167. break;
  168. case "#configuration_nav":
  169. tab_config.getActions(ui, checked_server);
  170. tab_config.getMaps(ui, checked_server);
  171. break;
  172. case "#symbols_nav":
  173. tab_symbols.getSymbols(ui, tables, checked_server);
  174. break;
  175. case "#history_nav":
  176. (function () {
  177. function getHistoryAndErrors() {
  178. tab_history.getHistory(ui, tables);
  179. tab_history.getErrors(ui, tables);
  180. }
  181. var refreshInterval = $(".dropdown-menu a.active.history").data("value");
  182. setAutoRefresh(refreshInterval, "history",
  183. function () { return getHistoryAndErrors(); });
  184. if (id !== "#autoRefresh") getHistoryAndErrors();
  185. $(".preset").hide();
  186. $(".history").show();
  187. $(".dynamic").hide();
  188. }());
  189. break;
  190. case "#disconnect":
  191. disconnect();
  192. break;
  193. default:
  194. }
  195. setTimeout(function () {
  196. // Do not enable Refresh button until AJAX requests to all neighbours are finished
  197. if (tab_id === "#history_nav") navBarControls = $(navBarControls).not("#refresh");
  198. navBarControls.removeAttr("disabled").removeClass("disabled");
  199. }, (id === "#autoRefresh") ? 0 : 1000);
  200. }
  201. function getPassword() {
  202. return sessionStorage.getItem("Password");
  203. }
  204. // Get selectors' current state
  205. function getSelector(id) {
  206. var e = document.getElementById(id);
  207. return e.options[e.selectedIndex].value;
  208. }
  209. function get_compare_function(table) {
  210. var compare_functions = {
  211. magnitude: function (e1, e2) {
  212. return Math.abs(e2.score) - Math.abs(e1.score);
  213. },
  214. name: function (e1, e2) {
  215. return e1.name.localeCompare(e2.name);
  216. },
  217. score: function (e1, e2) {
  218. return e2.score - e1.score;
  219. }
  220. };
  221. return compare_functions[getSelector("selSymOrder_" + table)];
  222. }
  223. function saveCredentials(password) {
  224. sessionStorage.setItem("Password", password);
  225. }
  226. function set_page_size(table, page_size, callback) {
  227. var n = parseInt(page_size, 10); // HTML Input elements return string representing a number
  228. if (n !== ui.page_size[table] && n > 0) {
  229. ui.page_size[table] = n;
  230. if (callback) {
  231. return callback(n);
  232. }
  233. }
  234. return null;
  235. }
  236. function sort_symbols(o, compare_function) {
  237. return Object.keys(o)
  238. .map(function (key) {
  239. return o[key];
  240. })
  241. .sort(compare_function)
  242. .map(function (e) { return e.str; })
  243. .join("<br>\n");
  244. }
  245. function unix_time_format(tm) {
  246. var date = new Date(tm ? tm * 1000 : 0);
  247. return (locale)
  248. ? date.toLocaleString(locale)
  249. : date.toLocaleString();
  250. }
  251. function displayUI() {
  252. // In many browsers local storage can only store string.
  253. // So when we store the boolean true or false, it actually stores the strings "true" or "false".
  254. ui.read_only = sessionStorage.getItem("read_only") === "true";
  255. ui.query("auth", {
  256. success: function (neighbours_status) {
  257. $("#selSrv").empty();
  258. $("#selSrv").append($('<option value="All SERVERS">All SERVERS</option>'));
  259. neighbours_status.forEach(function (e) {
  260. $("#selSrv").append($('<option value="' + e.name + '">' + e.name + "</option>"));
  261. if (checked_server === e.name) {
  262. $('#selSrv [value="' + e.name + '"]').prop("selected", true);
  263. } else if (!e.status) {
  264. $('#selSrv [value="' + e.name + '"]').prop("disabled", true);
  265. }
  266. });
  267. if (!ui.read_only) tab_selectors.displayUI(ui);
  268. },
  269. complete: function () {
  270. ajaxSetup(localStorage.getItem("ajax_timeout"));
  271. if (ui.read_only) {
  272. $(".ro-disable").attr("disabled", true);
  273. $(".ro-hide").hide();
  274. } else {
  275. $(".ro-disable").removeAttr("disabled", true);
  276. $(".ro-hide").show();
  277. }
  278. $("#preloader").addClass("d-none");
  279. $("#navBar, #mainUI").removeClass("d-none");
  280. $(".nav-tabs-sticky").stickyTabs({initialTab:"#status_nav"});
  281. },
  282. errorMessage: "Cannot get server status",
  283. server: "All SERVERS"
  284. });
  285. }
  286. function alertMessage(alertClass, alertText) {
  287. var a = $("<div class=\"alert " + alertClass + " alert-dismissible fade in show\">" +
  288. "<button type=\"button\" class=\"btn-close\" data-bs-dismiss=\"alert\" title=\"Dismiss\"></button>" +
  289. "<strong>" + alertText + "</strong>");
  290. $(".notification-area").append(a);
  291. setTimeout(function () {
  292. $(a).fadeTo(500, 0).slideUp(500, function () {
  293. $(this).alert("close");
  294. });
  295. }, 5000);
  296. }
  297. function queryServer(neighbours_status, ind, req_url, o) {
  298. neighbours_status[ind].checked = false;
  299. neighbours_status[ind].data = {};
  300. neighbours_status[ind].status = false;
  301. var req_params = {
  302. jsonp: false,
  303. data: o.data,
  304. headers: $.extend({Password:getPassword()}, o.headers),
  305. url: neighbours_status[ind].url + req_url,
  306. xhr: function () {
  307. var xhr = $.ajaxSettings.xhr();
  308. // Download progress
  309. if (req_url !== "neighbours") {
  310. xhr.addEventListener("progress", function (e) {
  311. if (e.lengthComputable) {
  312. neighbours_status[ind].percentComplete = e.loaded / e.total;
  313. var percentComplete = neighbours_status.reduce(function (prev, curr) {
  314. return curr.percentComplete ? curr.percentComplete + prev : prev;
  315. }, 0);
  316. NProgress.set(percentComplete / neighbours_status.length);
  317. }
  318. }, false);
  319. }
  320. return xhr;
  321. },
  322. success: function (json) {
  323. neighbours_status[ind].checked = true;
  324. neighbours_status[ind].status = true;
  325. neighbours_status[ind].data = json;
  326. },
  327. error: function (jqXHR, textStatus, errorThrown) {
  328. neighbours_status[ind].checked = true;
  329. function errorMessage() {
  330. alertMessage("alert-error", neighbours_status[ind].name + " > " +
  331. (o.errorMessage ? o.errorMessage : "Request failed") +
  332. (errorThrown ? ": " + errorThrown : ""));
  333. }
  334. if (o.error) {
  335. o.error(neighbours_status[ind],
  336. jqXHR, textStatus, errorThrown);
  337. } else if (o.errorOnceId) {
  338. var alert_status = o.errorOnceId + neighbours_status[ind].name;
  339. if (!(alert_status in sessionStorage)) {
  340. sessionStorage.setItem(alert_status, true);
  341. errorMessage();
  342. }
  343. } else {
  344. errorMessage();
  345. }
  346. },
  347. complete: function (jqXHR) {
  348. if (neighbours_status.every(function (elt) { return elt.checked; })) {
  349. if (neighbours_status.some(function (elt) { return elt.status; })) {
  350. if (o.success) {
  351. o.success(neighbours_status, jqXHR);
  352. } else {
  353. alertMessage("alert-success", "Request completed");
  354. }
  355. } else {
  356. alertMessage("alert-error", "Request failed");
  357. }
  358. if (o.complete) o.complete();
  359. NProgress.done();
  360. }
  361. },
  362. statusCode: o.statusCode
  363. };
  364. if (o.method) {
  365. req_params.method = o.method;
  366. }
  367. if (o.params) {
  368. $.each(o.params, function (k, v) {
  369. req_params[k] = v;
  370. });
  371. }
  372. $.ajax(req_params);
  373. }
  374. // Public functions
  375. ui.alertMessage = alertMessage;
  376. ui.setup = function () {
  377. (function initSettings() {
  378. var selected_locale = null;
  379. var custom_locale = null;
  380. const localeTextbox = ".popover #settings-popover #locale";
  381. function validateLocale(saveToLocalStorage) {
  382. function toggle_form_group_class(remove, add) {
  383. $(localeTextbox).removeClass("is-" + remove).addClass("is-" + add);
  384. }
  385. var now = new Date();
  386. if (custom_locale.length) {
  387. try {
  388. now.toLocaleString(custom_locale);
  389. if (saveToLocalStorage) localStorage.setItem("custom_locale", custom_locale);
  390. locale = (selected_locale === "custom") ? custom_locale : null;
  391. toggle_form_group_class("invalid", "valid");
  392. } catch (err) {
  393. locale = null;
  394. toggle_form_group_class("valid", "invalid");
  395. }
  396. } else {
  397. if (saveToLocalStorage) localStorage.setItem("custom_locale", null);
  398. locale = null;
  399. $(localeTextbox).removeClass("is-valid is-invalid");
  400. }
  401. // Display date example
  402. $(".popover #settings-popover #date-example").text(
  403. (locale)
  404. ? now.toLocaleString(locale)
  405. : now.toLocaleString()
  406. );
  407. }
  408. $("#settings").popover({
  409. container: "body",
  410. placement: "bottom",
  411. html: true,
  412. sanitize: false,
  413. content: function () {
  414. // Using .clone() has the side-effect of producing elements with duplicate id attributes.
  415. return $("#settings-popover").clone();
  416. }
  417. // Restore the tooltip of the element that the popover is attached to.
  418. }).attr("title", function () {
  419. return $(this).attr("data-original-title");
  420. });
  421. $("#settings").on("click", function (e) {
  422. e.preventDefault();
  423. });
  424. $("#settings").on("inserted.bs.popover", function () {
  425. selected_locale = localStorage.getItem("selected_locale") || "browser";
  426. custom_locale = localStorage.getItem("custom_locale") || "";
  427. validateLocale();
  428. $('.popover #settings-popover input:radio[name="locale"]').val([selected_locale]);
  429. $(localeTextbox).val(custom_locale);
  430. ajaxSetup(localStorage.getItem("ajax_timeout"), true);
  431. });
  432. $(document).on("change", '.popover #settings-popover input:radio[name="locale"]', function () {
  433. selected_locale = this.value;
  434. localStorage.setItem("selected_locale", selected_locale);
  435. validateLocale();
  436. });
  437. $(document).on("input", localeTextbox, function () {
  438. custom_locale = $(localeTextbox).val();
  439. validateLocale(true);
  440. });
  441. $(document).on("input", ajaxTimeoutBox, function () {
  442. ajaxSetup($(ajaxTimeoutBox).val(), false, true);
  443. });
  444. $(document).on("click", ".popover #settings-popover #ajax-timeout-restore", function () {
  445. ajaxSetup(null, true, true);
  446. });
  447. // Dismiss Bootstrap popover by clicking outside
  448. $("body").on("click", function (e) {
  449. $(".popover").each(function () {
  450. if (
  451. // Popover's descendant
  452. $(this).has(e.target).length ||
  453. // Button (or icon within a button) that triggers the popover.
  454. $(e.target).closest("button").attr("aria-describedby") === this.id
  455. ) return;
  456. $("#settings").popover("hide");
  457. });
  458. });
  459. }());
  460. $("#selData").change(function () {
  461. selData = this.value;
  462. tabClick("#throughput_nav");
  463. });
  464. $(document).ajaxStart(function () {
  465. $("#refresh > svg").addClass("fa-spin");
  466. });
  467. $(document).ajaxComplete(function () {
  468. setTimeout(function () {
  469. $("#refresh > svg").removeClass("fa-spin");
  470. }, 1000);
  471. });
  472. $('a[data-bs-toggle="tab"]').on("shown.bs.tab", function () {
  473. tabClick("#" + $(this).attr("id"));
  474. });
  475. $("#refresh, #disconnect").on("click", function (e) {
  476. e.preventDefault();
  477. tabClick("#" + $(this).attr("id"));
  478. });
  479. $(".dropdown-menu a").click(function (e) {
  480. e.preventDefault();
  481. var classList = $(this).attr("class");
  482. var menuClass = (/\b(?:dynamic|history|preset)\b/).exec(classList)[0];
  483. $(".dropdown-menu a.active." + menuClass).removeClass("active");
  484. $(this).addClass("active");
  485. tabClick("#autoRefresh");
  486. });
  487. $("#selSrv").change(function () {
  488. checked_server = this.value;
  489. $("#selSrv [value=\"" + checked_server + "\"]").prop("checked", true);
  490. if (checked_server === "All SERVERS") {
  491. $("#learnServers").show();
  492. } else {
  493. $("#learnServers").hide();
  494. }
  495. tabClick("#" + $("#tablist > .nav-item > .nav-link.active").attr("id"));
  496. });
  497. // Radio buttons
  498. $(document).on("click", "input:radio[name=\"clusterName\"]", function () {
  499. if (!this.disabled) {
  500. checked_server = this.value;
  501. tabClick("#status_nav");
  502. }
  503. });
  504. tab_config.setup(ui);
  505. tab_history.setup(ui, tables);
  506. tab_selectors.setup(ui);
  507. tab_symbols.setup(ui, tables);
  508. tab_upload.setup(ui, tables);
  509. selData = tab_graph.setup(ui);
  510. $("#loading").addClass("d-none");
  511. };
  512. ui.connect = function () {
  513. // Prevent locking out of the WebUI if timeout is too low.
  514. let timeout = localStorage.getItem("ajax_timeout");
  515. if (timeout < defaultAjaxTimeout) timeout = defaultAjaxTimeout;
  516. ajaxSetup(timeout);
  517. // Query "/stat" to check if user is already logged in or client ip matches "secure_ip"
  518. $.ajax({
  519. type: "GET",
  520. url: "stat",
  521. success: function (data) {
  522. sessionStorage.setItem("read_only", data.read_only);
  523. displayUI();
  524. },
  525. error: function () {
  526. function clearFeedback() {
  527. $("#connectPassword").off("input").removeClass("is-invalid");
  528. $("#authInvalidCharFeedback,#authUnauthorizedFeedback").hide();
  529. }
  530. $("#connectDialog")
  531. .on("show.bs.modal", () => {
  532. $("#connectDialog").off("show.bs.modal");
  533. clearFeedback();
  534. })
  535. .on("shown.bs.modal", () => {
  536. $("#connectDialog").off("shown.bs.modal");
  537. $("#connectPassword").focus();
  538. })
  539. .modal("show");
  540. $("#connectForm").off("submit").on("submit", function (e) {
  541. e.preventDefault();
  542. var password = $("#connectPassword").val();
  543. function invalidFeedback(tooltip) {
  544. $("#connectPassword")
  545. .addClass("is-invalid")
  546. .off("input").on("input", () => clearFeedback());
  547. $(tooltip).show();
  548. }
  549. if (!(/^[\u0020-\u007e]*$/).test(password)) {
  550. invalidFeedback("#authInvalidCharFeedback");
  551. $("#connectPassword").focus();
  552. return;
  553. }
  554. ui.query("auth", {
  555. headers: {
  556. Password: password
  557. },
  558. success: function (json) {
  559. var data = json[0].data;
  560. $("#connectPassword").val("");
  561. if (data.auth === "ok") {
  562. sessionStorage.setItem("read_only", data.read_only);
  563. saveCredentials(password);
  564. $("#connectForm").off("submit");
  565. $("#connectDialog").modal("hide");
  566. displayUI();
  567. }
  568. },
  569. error: function (jqXHR, textStatus) {
  570. if (textStatus.statusText === "Unauthorized") {
  571. invalidFeedback("#authUnauthorizedFeedback");
  572. } else {
  573. ui.alertMessage("alert-modal alert-error", textStatus.statusText);
  574. }
  575. $("#connectPassword").val("");
  576. $("#connectPassword").focus();
  577. },
  578. params: {
  579. global: false,
  580. },
  581. server: "local"
  582. });
  583. });
  584. }
  585. });
  586. };
  587. ui.getPassword = getPassword;
  588. ui.getSelector = getSelector;
  589. /**
  590. * @param {string} url - A string containing the URL to which the request is sent
  591. * @param {Object} [options] - A set of key/value pairs that configure the Ajax request. All settings are optional.
  592. *
  593. * @param {Function} [options.complete] - A function to be called when the requests to all neighbours complete.
  594. * @param {Object|string|Array} [options.data] - Data to be sent to the server.
  595. * @param {Function} [options.error] - A function to be called if the request fails.
  596. * @param {string} [options.errorMessage] - Text to display in the alert message if the request fails.
  597. * @param {string} [options.errorOnceId] - A prefix of the alert ID to be added to the session storage. If the
  598. * parameter is set, the error for each server will be displayed only once per session.
  599. * @param {Object} [options.headers] - An object of additional header key/value pairs to send along with requests
  600. * using the XMLHttpRequest transport.
  601. * @param {string} [options.method] - The HTTP method to use for the request.
  602. * @param {Object} [options.params] - An object of additional jQuery.ajax() settings key/value pairs.
  603. * @param {string} [options.server] - A server to which send the request.
  604. * @param {Function} [options.success] - A function to be called if the request succeeds.
  605. *
  606. * @returns {undefined}
  607. */
  608. ui.query = function (url, options) {
  609. // Force options to be an object
  610. var o = options || {};
  611. Object.keys(o).forEach(function (option) {
  612. if (["complete", "data", "error", "errorMessage", "errorOnceId", "headers", "method", "params", "server", "statusCode",
  613. "success"]
  614. .indexOf(option) < 0) {
  615. throw new Error("Unknown option: " + option);
  616. }
  617. });
  618. var neighbours_status = [{
  619. name: "local",
  620. host: "local",
  621. url: "",
  622. }];
  623. o.server = o.server || checked_server;
  624. if (o.server === "All SERVERS") {
  625. queryServer(neighbours_status, 0, "neighbours", {
  626. success: function (json) {
  627. var data = json[0].data;
  628. if (jQuery.isEmptyObject(data)) {
  629. neighbours = {
  630. local: {
  631. host: window.location.host,
  632. url: window.location.origin + window.location.pathname
  633. }
  634. };
  635. } else {
  636. neighbours = data;
  637. }
  638. neighbours_status = [];
  639. $.each(neighbours, function (ind) {
  640. neighbours_status.push({
  641. name: ind,
  642. host: neighbours[ind].host,
  643. url: neighbours[ind].url,
  644. });
  645. });
  646. $.each(neighbours_status, function (ind) {
  647. queryServer(neighbours_status, ind, url, o);
  648. });
  649. },
  650. errorMessage: "Cannot receive neighbours data"
  651. });
  652. } else {
  653. if (o.server !== "local") {
  654. neighbours_status = [{
  655. name: o.server,
  656. host: neighbours[o.server].host,
  657. url: neighbours[o.server].url,
  658. }];
  659. }
  660. queryServer(neighbours_status, 0, url, o);
  661. }
  662. };
  663. // Scan and History shared functions
  664. ui.unix_time_format = unix_time_format;
  665. ui.set_page_size = set_page_size;
  666. ui.bindHistoryTableEventHandlers = function (table, symbolsCol) {
  667. function change_symbols_order(order) {
  668. $(".btn-sym-" + table + "-" + order).addClass("active").siblings().removeClass("active");
  669. var compare_function = get_compare_function(table);
  670. $.each(tables[table].rows.all, function (i, row) {
  671. var cell_val = sort_symbols(ui.symbols[table][i], compare_function);
  672. row.cells[symbolsCol].val(cell_val, false, true);
  673. });
  674. }
  675. $("#selSymOrder_" + table).unbind().change(function () {
  676. var order = this.value;
  677. change_symbols_order(order);
  678. });
  679. $("#" + table + "_page_size").change(function () {
  680. set_page_size(table, this.value, function (n) { tables[table].pageSize(n); });
  681. });
  682. $(document).on("click", ".btn-sym-order-" + table + " input", function () {
  683. var order = this.value;
  684. $("#selSymOrder_" + table).val(order);
  685. change_symbols_order(order);
  686. });
  687. };
  688. ui.destroyTable = function (table) {
  689. if (tables[table]) {
  690. tables[table].destroy();
  691. delete tables[table];
  692. }
  693. };
  694. ui.initHistoryTable = function (rspamd, data, items, table, columns, expandFirst) {
  695. /* eslint-disable no-underscore-dangle */
  696. FooTable.Cell.extend("collapse", function () {
  697. // call the original method
  698. this._super();
  699. // Copy cell classes to detail row tr element
  700. this._setClasses(this.$detail);
  701. });
  702. /* eslint-enable no-underscore-dangle */
  703. /* eslint-disable consistent-this, no-underscore-dangle, one-var-declaration-per-line */
  704. FooTable.actionFilter = FooTable.Filtering.extend({
  705. construct: function (instance) {
  706. this._super(instance);
  707. this.actions = ["reject", "add header", "greylist",
  708. "no action", "soft reject", "rewrite subject"];
  709. this.def = "Any action";
  710. this.$action = null;
  711. },
  712. $create: function () {
  713. this._super();
  714. var self = this, $form_grp = $("<div/>", {
  715. class: "form-group"
  716. }).append($("<label/>", {
  717. class: "sr-only",
  718. text: "Action"
  719. })).prependTo(self.$form);
  720. self.$action = $("<select/>", {
  721. class: "form-select"
  722. }).on("change", {
  723. self: self
  724. }, self._onStatusDropdownChanged).append(
  725. $("<option/>", {
  726. text: self.def
  727. })).appendTo($form_grp);
  728. $.each(self.actions, function (i, action) {
  729. self.$action.append($("<option/>").text(action));
  730. });
  731. },
  732. _onStatusDropdownChanged: function (e) {
  733. var self = e.data.self, selected = $(this).val();
  734. if (selected !== self.def) {
  735. if (selected === "reject") {
  736. self.addFilter("action", "reject -soft", ["action"]);
  737. } else {
  738. self.addFilter("action", selected, ["action"]);
  739. }
  740. } else {
  741. self.removeFilter("action");
  742. }
  743. self.filter();
  744. }
  745. });
  746. /* eslint-enable consistent-this, no-underscore-dangle, one-var-declaration-per-line */
  747. tables[table] = FooTable.init("#historyTable_" + table, {
  748. columns: columns,
  749. rows: items,
  750. expandFirst: expandFirst,
  751. paging: {
  752. enabled: true,
  753. limit: 5,
  754. size: ui.page_size[table]
  755. },
  756. filtering: {
  757. enabled: true,
  758. position: "left",
  759. connectors: false
  760. },
  761. sorting: {
  762. enabled: true
  763. },
  764. components: {
  765. filtering: FooTable.actionFilter
  766. },
  767. on: {
  768. "expand.ft.row": function (e, ft, row) {
  769. setTimeout(function () {
  770. var detail_row = row.$el.next();
  771. var order = getSelector("selSymOrder_" + table);
  772. detail_row.find(".btn-sym-" + table + "-" + order)
  773. .addClass("active").siblings().removeClass("active");
  774. }, 5);
  775. }
  776. }
  777. });
  778. };
  779. ui.escapeHTML = function (string) {
  780. var htmlEscaper = /[&<>"'/`=]/g;
  781. var htmlEscapes = {
  782. "&": "&amp;",
  783. "<": "&lt;",
  784. ">": "&gt;",
  785. "\"": "&quot;",
  786. "'": "&#39;",
  787. "/": "&#x2F;",
  788. "`": "&#x60;",
  789. "=": "&#x3D;"
  790. };
  791. return String(string).replace(htmlEscaper, function (match) {
  792. return htmlEscapes[match];
  793. });
  794. };
  795. ui.preprocess_item = function (rspamd, item) {
  796. function escape_HTML_array(arr) {
  797. arr.forEach(function (d, i) { arr[i] = ui.escapeHTML(d); });
  798. }
  799. for (var prop in item) {
  800. if (!{}.hasOwnProperty.call(item, prop)) continue;
  801. switch (prop) {
  802. case "rcpt_mime":
  803. case "rcpt_smtp":
  804. escape_HTML_array(item[prop]);
  805. break;
  806. case "symbols":
  807. Object.keys(item.symbols).forEach(function (key) {
  808. var sym = item.symbols[key];
  809. if (!sym.name) {
  810. sym.name = key;
  811. }
  812. sym.name = ui.escapeHTML(sym.name);
  813. if (sym.description) {
  814. sym.description = ui.escapeHTML(sym.description);
  815. }
  816. if (sym.options) {
  817. escape_HTML_array(sym.options);
  818. }
  819. });
  820. break;
  821. default:
  822. if (typeof item[prop] === "string") {
  823. item[prop] = ui.escapeHTML(item[prop]);
  824. }
  825. }
  826. }
  827. if (item.action === "clean" || item.action === "no action") {
  828. item.action = "<div style='font-size:11px' class='badge text-bg-success'>" + item.action + "</div>";
  829. } else if (item.action === "rewrite subject" || item.action === "add header" || item.action === "probable spam") {
  830. item.action = "<div style='font-size:11px' class='badge text-bg-warning'>" + item.action + "</div>";
  831. } else if (item.action === "spam" || item.action === "reject") {
  832. item.action = "<div style='font-size:11px' class='badge text-bg-danger'>" + item.action + "</div>";
  833. } else {
  834. item.action = "<div style='font-size:11px' class='badge text-bg-info'>" + item.action + "</div>";
  835. }
  836. var score_content = (item.score < item.required_score)
  837. ? "<span class='text-success'>" + item.score.toFixed(2) + " / " + item.required_score + "</span>"
  838. : "<span class='text-danger'>" + item.score.toFixed(2) + " / " + item.required_score + "</span>";
  839. item.score = {
  840. options: {
  841. sortValue: item.score
  842. },
  843. value: score_content
  844. };
  845. };
  846. ui.process_history_v2 = function (rspamd, data, table) {
  847. // Display no more than rcpt_lim recipients
  848. var rcpt_lim = 3;
  849. var items = [];
  850. var unsorted_symbols = [];
  851. var compare_function = get_compare_function(table);
  852. $("#selSymOrder_" + table + ", label[for='selSymOrder_" + table + "']").show();
  853. $.each(data.rows,
  854. function (i, item) {
  855. function more(p) {
  856. var l = item[p].length;
  857. return (l > rcpt_lim) ? " … (" + l + ")" : "";
  858. }
  859. function format_rcpt(smtp, mime) {
  860. var full = "";
  861. var shrt = "";
  862. if (smtp) {
  863. full = "[" + item.rcpt_smtp.join(", ") + "] ";
  864. shrt = "[" + item.rcpt_smtp.slice(0, rcpt_lim).join(",&#8203;") + more("rcpt_smtp") + "]";
  865. if (mime) {
  866. full += " ";
  867. shrt += " ";
  868. }
  869. }
  870. if (mime) {
  871. full += item.rcpt_mime.join(", ");
  872. shrt += item.rcpt_mime.slice(0, rcpt_lim).join(",&#8203;") + more("rcpt_mime");
  873. }
  874. return {full:full, shrt:shrt};
  875. }
  876. function get_symbol_class(name, score) {
  877. if (name.match(/^GREYLIST$/)) {
  878. return "symbol-special";
  879. }
  880. if (score < 0) {
  881. return "symbol-negative";
  882. } else if (score > 0) {
  883. return "symbol-positive";
  884. }
  885. return null;
  886. }
  887. rspamd.preprocess_item(rspamd, item);
  888. Object.values(item.symbols).forEach(function (sym) {
  889. sym.str = '<span class="symbol-default ' + get_symbol_class(sym.name, sym.score) + '"><strong>';
  890. if (sym.description) {
  891. sym.str += '<abbr title="' + sym.description + '">' + sym.name + "</abbr>";
  892. } else {
  893. sym.str += sym.name;
  894. }
  895. sym.str += "</strong> (" + sym.score + ")</span>";
  896. if (sym.options) {
  897. sym.str += " [" + sym.options.join(",") + "]";
  898. }
  899. });
  900. unsorted_symbols.push(item.symbols);
  901. item.symbols = sort_symbols(item.symbols, compare_function);
  902. if (table === "scan") {
  903. item.unix_time = (new Date()).getTime() / 1000;
  904. }
  905. item.time = {
  906. value: unix_time_format(item.unix_time),
  907. options: {
  908. sortValue: item.unix_time
  909. }
  910. };
  911. item.time_real = item.time_real.toFixed(3);
  912. item.id = item["message-id"];
  913. if (table === "history") {
  914. var rcpt = {};
  915. if (!item.rcpt_mime.length) {
  916. rcpt = format_rcpt(true, false);
  917. } else if ($(item.rcpt_mime).not(item.rcpt_smtp).length !== 0 || $(item.rcpt_smtp).not(item.rcpt_mime).length !== 0) {
  918. rcpt = format_rcpt(true, true);
  919. } else {
  920. rcpt = format_rcpt(false, true);
  921. }
  922. item.rcpt_mime_short = rcpt.shrt;
  923. item.rcpt_mime = rcpt.full;
  924. if (item.sender_mime !== item.sender_smtp) {
  925. item.sender_mime = "[" + item.sender_smtp + "] " + item.sender_mime;
  926. }
  927. }
  928. items.push(item);
  929. });
  930. return {items:items, symbols:unsorted_symbols};
  931. };
  932. ui.waitForRowsDisplayed = function (table, rows_total, callback, iteration) {
  933. var i = (typeof iteration === "undefined") ? 10 : iteration;
  934. var num_rows = $("#historyTable_" + table + " > tbody > tr:not(.footable-detail-row)").length;
  935. if (num_rows === ui.page_size[table] ||
  936. num_rows === rows_total) {
  937. return callback();
  938. } else if (--i) {
  939. setTimeout(function () {
  940. ui.waitForRowsDisplayed(table, rows_total, callback, i);
  941. }, 500);
  942. }
  943. return null;
  944. };
  945. return ui;
  946. });