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.

upload.js 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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"],
  21. function ($) {
  22. "use strict";
  23. var ui = {};
  24. function cleanTextUpload(source) {
  25. $("#" + source + "TextSource").val("");
  26. }
  27. // @upload text
  28. function uploadText(rspamd, data, source, headers) {
  29. var url = null;
  30. if (source === "spam") {
  31. url = "learnspam";
  32. } else if (source === "ham") {
  33. url = "learnham";
  34. } else if (source === "fuzzy") {
  35. url = "fuzzyadd";
  36. } else if (source === "scan") {
  37. url = "checkv2";
  38. }
  39. rspamd.query(url, {
  40. data: data,
  41. params: {
  42. processData: false,
  43. },
  44. method: "POST",
  45. headers: headers,
  46. success: function (json, jqXHR) {
  47. cleanTextUpload(source);
  48. rspamd.alertMessage("alert-success", "Data successfully uploaded");
  49. if (jqXHR.status !== 200) {
  50. rspamd.alertMessage("alert-info", jqXHR.statusText);
  51. }
  52. }
  53. });
  54. }
  55. function columns_v2() {
  56. return [{
  57. name: "id",
  58. title: "ID",
  59. style: {
  60. "font-size": "11px",
  61. "minWidth": 130,
  62. "overflow": "hidden",
  63. "textOverflow": "ellipsis",
  64. "wordBreak": "break-all",
  65. "whiteSpace": "normal"
  66. }
  67. }, {
  68. name: "action",
  69. title: "Action",
  70. style: {
  71. "font-size": "11px",
  72. "minwidth": 82
  73. }
  74. }, {
  75. name: "score",
  76. title: "Score",
  77. style: {
  78. "font-size": "11px",
  79. "maxWidth": 110
  80. },
  81. sortValue: function (val) { return Number(val.options.sortValue); }
  82. }, {
  83. name: "symbols",
  84. title: "Symbols<br /><br />" +
  85. '<span style="font-weight:normal;">Sort by:</span><br />' +
  86. '<div class="btn-group btn-group-toggle btn-group-xs btn-sym-order-scan" data-toggle="buttons">' +
  87. '<label type="button" class="btn btn-outline-secondary btn-sym-scan-magnitude">' +
  88. '<input type="radio" value="magnitude">Magnitude</label>' +
  89. '<label type="button" class="btn btn-outline-secondary btn-sym-scan-score">' +
  90. '<input type="radio" value="score">Value</label>' +
  91. '<label type="button" class="btn btn-outline-secondary btn-sym-scan-name">' +
  92. '<input type="radio" value="name">Name</label>' +
  93. "</div>",
  94. breakpoints: "all",
  95. style: {
  96. "font-size": "11px",
  97. "width": 550,
  98. "maxWidth": 550
  99. }
  100. }, {
  101. name: "time_real",
  102. title: "Scan time",
  103. breakpoints: "xs sm md",
  104. style: {
  105. "font-size": "11px",
  106. "maxWidth": 72
  107. },
  108. sortValue: function (val) { return Number(val); }
  109. }, {
  110. sorted: true,
  111. direction: "DESC",
  112. name: "time",
  113. title: "Time",
  114. style: {
  115. "font-size": "11px"
  116. },
  117. sortValue: function (val) { return Number(val.options.sortValue); }
  118. }];
  119. }
  120. // @upload text
  121. function scanText(rspamd, tables, data, server, headers) {
  122. rspamd.query("checkv2", {
  123. data: data,
  124. params: {
  125. processData: false,
  126. },
  127. method: "POST",
  128. headers: headers,
  129. success: function (neighbours_status) {
  130. function scrollTop(rows_total) {
  131. // Is there a way to get an event when all rows are loaded?
  132. rspamd.waitForRowsDisplayed("scan", rows_total, function () {
  133. $("html, body").animate({
  134. scrollTop: $("#scanResult").offset().top
  135. }, 1000);
  136. });
  137. }
  138. var json = neighbours_status[0].data;
  139. if (json.action) {
  140. rspamd.alertMessage("alert-success", "Data successfully scanned");
  141. var rows_total = $("#historyTable_scan > tbody > tr:not(.footable-detail-row)").length + 1;
  142. var o = rspamd.process_history_v2(rspamd, {rows:[json]}, "scan");
  143. var items = o.items;
  144. rspamd.symbols.scan.push(o.symbols[0]);
  145. if (Object.prototype.hasOwnProperty.call(tables, "scan")) {
  146. tables.scan.rows.load(items, true);
  147. scrollTop(rows_total);
  148. } else {
  149. rspamd.destroyTable("scan");
  150. // Is there a way to get an event when the table is destroyed?
  151. setTimeout(function () {
  152. rspamd.initHistoryTable(rspamd, data, items, "scan", columns_v2(), true);
  153. scrollTop(rows_total);
  154. }, 200);
  155. }
  156. } else {
  157. rspamd.alertMessage("alert-error", "Cannot scan data");
  158. }
  159. },
  160. errorMessage: "Cannot upload data",
  161. statusCode: {
  162. 404: function () {
  163. rspamd.alertMessage("alert-error", "Cannot upload data, no server found");
  164. },
  165. 500: function () {
  166. rspamd.alertMessage("alert-error", "Cannot tokenize message: no text data");
  167. },
  168. 503: function () {
  169. rspamd.alertMessage("alert-error", "Cannot tokenize message: no text data");
  170. }
  171. },
  172. server: server
  173. });
  174. }
  175. ui.setup = function (rspamd, tables) {
  176. rspamd.set_page_size("scan", $("#scan_page_size").val());
  177. rspamd.bindHistoryTableEventHandlers("scan", 3);
  178. $("#cleanScanHistory").off("click");
  179. $("#cleanScanHistory").on("click", function (e) {
  180. e.preventDefault();
  181. if (!confirm("Are you sure you want to clean scan history?")) { // eslint-disable-line no-alert
  182. return;
  183. }
  184. rspamd.destroyTable("scan");
  185. rspamd.symbols.scan.length = 0;
  186. });
  187. function enable_disable_scan_btn() {
  188. $("#scan button:not(#scanOptionsToggle)").prop("disabled", ($.trim($("textarea").val()).length === 0));
  189. }
  190. enable_disable_scan_btn();
  191. $("textarea").on("input", function () {
  192. enable_disable_scan_btn();
  193. });
  194. $("#scanClean").on("click", function () {
  195. $("#scan button:not(#scanOptionsToggle)").attr("disabled", true);
  196. $("#scanForm")[0].reset();
  197. $("#scanResult").hide();
  198. $("#scanOutput tbody").remove();
  199. $("html, body").animate({scrollTop:0}, 1000);
  200. return false;
  201. });
  202. // @init upload
  203. $("[data-upload]").on("click", function () {
  204. var source = $(this).data("upload");
  205. var data = $("#scanMsgSource").val();
  206. var headers = {};
  207. if ($.trim(data).length > 0) {
  208. if (source === "scan") {
  209. var checked_server = rspamd.getSelector("selSrv");
  210. var server = (checked_server === "All SERVERS") ? "local" : checked_server;
  211. headers = ["IP", "User", "From", "Rcpt", "Helo", "Hostname"].reduce(function (o, header) {
  212. var value = $("#scan-opt-" + header.toLowerCase()).val();
  213. if (value !== "") o[header] = value;
  214. return o;
  215. }, {});
  216. if ($("#scan-opt-pass-all").prop("checked")) headers.Pass = "all";
  217. scanText(rspamd, tables, data, server, headers);
  218. } else {
  219. if (source === "fuzzy") {
  220. headers = {
  221. flag: $("#fuzzyFlagText").val(),
  222. weight: $("#fuzzyWeightText").val()
  223. };
  224. }
  225. uploadText(rspamd, data, source, headers);
  226. }
  227. } else {
  228. rspamd.alertMessage("alert-error", "Message source field cannot be blank");
  229. }
  230. return false;
  231. });
  232. };
  233. return ui;
  234. });