/* The MIT License (MIT) Copyright (C) 2017 Vsevolod Stakhov Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ define(['jquery', 'footable'], function($) { var interface = {} var ft = {} function saveSymbols(rspamd, action, id, is_cluster) { var inputs = $('#' + id + ' :input[data-role="numerictextbox"]'); var url = action; var values = []; $(inputs).each(function () { values.push({ name: $(this).attr('id').substring(5), value: parseFloat($(this).val()) }); }); if (is_cluster) { rspamd.queryNeighbours(url, function () { rspamd.alertMessage('alert-modal alert-success', 'Symbols successfully saved'); }, function (serv, qXHR, textStatus, errorThrown) { rspamd.alertMessage('alert-modal alert-error', 'Save symbols error on ' + serv.name + ': ' + errorThrown); }, "POST", {}, { data: JSON.stringify(values), dataType: "json", }); } else { $.ajax({ data: JSON.stringify(values), dataType: 'json', type: 'POST', url: url, jsonp: false, beforeSend: function (xhr) { xhr.setRequestHeader('Password', rspamd.getPassword()); }, success: function () { rspamd.alertMessage('alert-modal alert-success', 'Symbols successfully saved'); }, error: function (data) { rspamd.alertMessage('alert-modal alert-error', data.statusText); } }); } } function decimalStep(number) { var digits = ((+number).toFixed(20)).replace(/^-?\d*\.?|0+$/g, '').length; if (digits === 0 || digits > 4) { return 0.1; } else { return 1.0 / (Math.pow(10, digits)); } } function process_symbols_data(data) { var items = []; var lookup = {}; var freqs = []; var distinct_groups = []; $.each(data, function (i, group) { $.each(group.rules, function (i, item) { var max = 20; var min = -20; if (item.weight > max) { max = item.weight * 2; } item.group = group.group if (item.weight < min) { min = item.weight * 2; } var label_class = ''; if (item.weight < 0) { label_class = 'scorebar-ham'; } else { label_class = 'scorebar-spam'; } item.weight = '' if (!item.time) { item.time = 0; } item.time = Number(item.time).toFixed(2) + 's' if (!item.frequency) { item.frequency = 0; } freqs.push(item.frequency); item.frequency = Number(item.frequency).toFixed(2) if (!(item.group in lookup)) { lookup[item.group] = 1; distinct_groups.push(item.group); } item.save = '' + ' '; items.push(item) }); }); // For better mean calculations var avg_freq = freqs.sort(function(a, b) { return Number(a) < Number(b); }).reduce(function(f1, acc) { return f1 + acc; }) / (freqs.length != 0 ? freqs.length : 1.0); var mult = 1.0; var exp = 0.0; if (avg_freq > 0.0) { while (mult * avg_freq < 1.0) { mult *= 10; exp ++; } } $.each(items, function (i, item) { item.frequency = Number(item.frequency) * mult; if (exp > 0) { item.frequency = item.frequency.toFixed(2) + 'e-' + exp; } else { item.frequency = item.frequency.toFixed(2); } }); return [items, distinct_groups] } // @get symbols into modal form interface.getSymbols = function(rspamd, tables, checked_server) { $.ajax({ dataType: 'json', type: 'GET', url: 'symbols', jsonp: false, beforeSend: function (xhr) { xhr.setRequestHeader('Password', rspamd.getPassword()); }, success: function (data) { var items = process_symbols_data(data); FooTable.groupFilter = FooTable.Filtering.extend({ construct : function(instance) { this._super(instance); this.groups = items[1]; this.def = 'Any group'; this.$group = null; }, $create : function() { this._super(); var self = this, $form_grp = $('
', { 'class' : 'form-group' }).append($('