-
Notifications
You must be signed in to change notification settings - Fork 18
/
sauserprefs.js
382 lines (319 loc) · 17.5 KB
/
sauserprefs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/**
* SAUserPrefs plugin script
*
* @licstart The following is the entire license notice for the
* JavaScript code in this file.
*
* Copyright (C) Philip Weir
*
* The JavaScript code in this page is free software: you can redistribute it
* and/or modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
*
* @licend The above is the entire license notice
* for the JavaScript code in this file.
*/
rcube_webmail.prototype.sauserprefs_toggle_level_char = function (checkbox) {
if ($(checkbox).is(':checked')) {
$('#rcmfd_spamlevelchar').removeAttr('disabled');
}
else {
$('#rcmfd_spamlevelchar').attr('disabled', 'disabled');
}
};
rcube_webmail.prototype.sauserprefs_toggle_bayes = function (checkbox) {
if ($(checkbox).is(':checked')) {
$('#rcmfd_spambayesrules,#rcmfd_spambayesautolearn').removeAttr('disabled');
if ($('#rcmfd_spambayesautolearn').is(':checked')) {
$('#rcmfd_bayesnonspam,#rcmfd_bayesspam').removeAttr('disabled');
}
}
else {
$('#rcmfd_spambayesrules,#rcmfd_spambayesautolearn,#rcmfd_bayesnonspam,#rcmfd_bayesspam').attr('disabled', 'disabled');
}
};
rcube_webmail.prototype.sauserprefs_toggle_bayes_auto = function (checkbox) {
if ($(checkbox).is(':checked')) {
$('#rcmfd_bayesnonspam,#rcmfd_bayesspam').removeAttr('disabled');
}
else {
$('#rcmfd_bayesnonspam,#rcmfd_bayesspam').attr('disabled', 'disabled');
}
};
rcube_webmail.prototype.sauserprefs_update_lang = function (chkbox, tickobj, enable) {
if (enable) {
chkbox.prop('checked', true);
tickobj.attr('title', this.get_label('enabled', 'sauserprefs')).removeClass('lang-disabled').addClass('lang-enabled');
}
else {
chkbox.prop('checked', false);
tickobj.attr('title', this.get_label('disabled', 'sauserprefs')).removeClass('lang-enabled').addClass('lang-disabled');
}
};
rcube_webmail.prototype.sauserprefs_addressrule_insert_row = function (p) {
var error = false;
$.each($('input[name="_address_rule_value[]"]'), function (idx) {
if ($(this).val() == p.address && $('input[name="_address_rule_act[]"]').eq(idx).val() != 'DELETE') {
error = true;
return false;
}
});
if (error) {
return false;
}
var adrTable = $('#address-rules-table tbody');
var new_row = $(adrTable).children('tr.newaddressrule').clone();
new_row.removeClass('newaddressrule').addClass(p.type);
new_row.children('td').eq(0).text(p.desc);
new_row.children('td').eq(1).text(p.address);
new_row.find('input[name="_address_rule_act[]"]').val('INSERT');
new_row.find('input[name="_address_rule_field[]"]').val(p.type);
new_row.find('input[name="_address_rule_value[]"]').val(p.address);
$(new_row).show().appendTo('#address-rules-table tbody');
$(adrTable).children('tr.noaddressrules').hide();
this.env.address_rule_count++;
this.sauserprefs_table_sort('#address-rules-table');
return true;
};
rcube_webmail.prototype.sauserprefs_addressrule_delete_row = function (obj) {
var actField = $(obj).find('input[name="_address_rule_act[]"]');
// skip empty rows
if (!actField.parent().is(':visible')) {
return;
}
if (actField.val() == 'INSERT') {
$(obj).closest('tr').remove();
}
else {
actField.val('DELETE');
$(obj).closest('tr').hide().appendTo('#address-rules-table tbody');
}
this.env.address_rule_count--;
if ($('#address-rules-table tbody').children('tr').filter(':visible').length == 0) {
$('#address-rules-table tbody').children('tr.noaddressrules').show();
}
};
rcube_webmail.prototype.sauserprefs_addressrule_import = function (address) {
parent.rcmail.set_busy(false, null, this.env.sauserprefs_welcomelist);
this.sauserprefs_addressrule_insert_row({ type: 'welcomelist_from', desc: this.get_label('welcomelist_from', 'sauserprefs'), address: address });
};
rcube_webmail.prototype.sauserprefs_help = function (sel) {
$('#' + sel).toggle();
return false;
};
rcube_webmail.prototype.sauserprefs_table_sort = function (id, idx, asc) {
if (idx == null) {
idx = this.env.sauserprefs_sort[id][0];
asc = this.env.sauserprefs_sort[id][1] == 'true';
}
var table = $(id);
var rows = table.find('tbody tr').filter(':visible').toArray().sort(
function (a, b) {
var result;
if (id == '#spam-langs-table' && $(a).children('td').eq(idx).hasClass('tick') && $(b).children('td').eq(idx).hasClass('tick')) {
if ($(a).children('td').eq(idx).find('a').length > 0) {
a = $(a).children('td').eq(idx).find('a').first().hasClass('lang-enabled');
b = $(b).children('td').eq(idx).find('a').first().hasClass('lang-enabled');
}
else {
a = $(a).children('td').eq(idx).find('input').first().is(':checked');
b = $(b).children('td').eq(idx).find('input').first().is(':checked');
}
result = asc ? b - a : a - b;
}
else {
a = $(a).children('td').eq(idx).html();
b = $(b).children('td').eq(idx).html();
result = asc ? a.localeCompare(b) : b.localeCompare(a);
}
return result;
}
);
table.children('tbody').children('tr').filter(':visible').remove();
for (var i = 0; i < rows.length; i++) {
table.children('tbody').append(rows[i]);
}
// move hidden rows to the bottom of the table
table.children('tbody').children('tr').filter(':hidden').appendTo(table.children('tbody'));
};
rcube_webmail.prototype.sauserprefs_address_import_dialog = function () {
var dialog = $('#saup_addressimport').clone();
this.simple_dialog(dialog.show(), this.gettext('sauserprefs.importaddresses'), function () {
if (dialog.find('input:checked').length > 0) {
var sources = dialog.find('input:checked').map(function () { return $(this).val(); }).get();
rcmail.command('plugin.sauserprefs.import_welcomelist', sources);
return true;
}
rcmail.display_message(rcmail.get_label('selectimportsource', 'sauserprefs'), 'warning');
return false;
}, { button: 'import' });
};
function sauserprefs_check_email(input) {
// special handeling for *.example.com (avoid *.text@email.com)
if (!input.match(/@/)) {
input = input.replace(/^\*\./, '*@');
}
return rcube_check_email(input, false);
}
$(document).ready(function () {
if (window.rcmail) {
// set table sorting classes
rcmail.env.sauserprefs_table_sort_asc = 'sortedASC'; // sortedASC class used in core
rcmail.env.sauserprefs_table_sort_desc = 'sortedDESC'; // sortedDESC class used in core
$.each(['#spam-langs-table', '#address-rules-table'], function (idx, id) {
if ($(id).length == 1) {
// add classes for sorting
var sorting_defaults = rcmail.env.sauserprefs_sort[id];
$(id).find('thead th').eq(sorting_defaults[0]).addClass(sorting_defaults[1] == 'true' ? rcmail.env.sauserprefs_table_sort_asc : rcmail.env.sauserprefs_table_sort_desc);
var temp_table = new rcube_list_widget($(id)[0], {});
temp_table.init();
// sort table according to user prefs
rcmail.sauserprefs_table_sort(id);
}
});
rcmail.addEventListener('init', function () {
if (rcmail.env.action == 'plugin.sauserprefs.edit') {
rcmail.register_command('plugin.sauserprefs.select_all_langs', function () {
$.each($('input[name="_spamlang[]"]'), function (idx) {
rcmail.sauserprefs_update_lang($(this), $('[id^=spam_lang_]').eq(idx), true);
});
return false;
}, true);
rcmail.register_command('plugin.sauserprefs.select_invert_langs', function () {
$.each($('input[name="_spamlang[]"]'), function (idx) {
rcmail.sauserprefs_update_lang($(this), $('[id^=spam_lang_]').eq(idx), !$(this).is(':checked'));
});
return false;
}, true);
rcmail.register_command('plugin.sauserprefs.select_no_langs', function () {
$.each($('input[name="_spamlang[]"]'), function (idx) {
rcmail.sauserprefs_update_lang($(this), $('[id^=spam_lang_]').eq(idx), false);
});
return false;
}, true);
rcmail.register_command('plugin.sauserprefs.message_lang', function (lang_code, obj) {
var langtick = $(obj).closest('tr').find('input');
rcmail.sauserprefs_update_lang($(langtick), $(obj), !$(langtick).is(':checked'));
return false;
}, true);
rcmail.register_command('plugin.sauserprefs.addressrule_del', function (props, obj) {
rcmail.confirm_dialog(rcmail.get_label('spamaddressdelete', 'sauserprefs'), 'delete', function (e, ref) {
ref.sauserprefs_addressrule_delete_row($(obj).parent());
});
return false;
}, true);
rcmail.register_command('plugin.sauserprefs.addressrule_add', function () {
var address_input = $('#rcmfd_spamaddress').val().trim();
// remove soem invalid characters from the end of the input
address_input = address_input.replace(/[\s\t.,;]+$/, '');
if (address_input == '') {
rcmail.display_message(rcmail.get_label('spamenteraddress', 'sauserprefs'), 'warning');
$('#rcmfd_spamaddress').addClass(rcmail.env.sauserprefs_input_error_class);
$('#rcmfd_spamaddress').focus();
return false;
}
if (!sauserprefs_check_email(address_input)) {
rcmail.display_message(rcmail.get_label('spamaddresserror', 'sauserprefs'), 'warning');
$('#rcmfd_spamaddress').addClass(rcmail.env.sauserprefs_input_error_class);
$('#rcmfd_spamaddress').focus();
return false;
}
if (!rcmail.sauserprefs_addressrule_insert_row({ type: $('#rcmfd_spamaddressrule').val(), desc: $('#rcmfd_spamaddressrule option:selected').text(), address: address_input })) {
rcmail.display_message(rcmail.get_label('spamaddressexists', 'sauserprefs'), 'warning');
$('#rcmfd_spamaddress').addClass(rcmail.env.sauserprefs_input_error_class);
$('#rcmfd_spamaddress').focus();
return false;
}
$('#rcmfd_spamaddress').removeClass(rcmail.env.sauserprefs_input_error_class);
$('#rcmfd_spamaddress').val('');
}, true);
rcmail.register_command('plugin.sauserprefs.welcomelist_delete_all', function () {
rcmail.confirm_dialog(rcmail.get_label('spamaddressdeleteall', 'sauserprefs'), 'delete', function (e, ref) {
$.each($('#address-rules-table tbody tr'), function () { ref.sauserprefs_addressrule_delete_row(this); });
});
return false;
}, true);
rcmail.register_command('plugin.sauserprefs.import_welcomelist', function (props) {
rcmail.env.sauserprefs_welcomelist = rcmail.set_busy(true, 'sauserprefs.importingaddresses');
rcmail.http_request('plugin.sauserprefs.welcomelist_import', { _sources: props }, rcmail.env.sauserprefs_welcomelist, 'POST');
return false;
}, true);
rcmail.register_command('plugin.sauserprefs.purge_bayes', function () {
rcmail.confirm_dialog(rcmail.get_label('purgebayesconfirm', 'sauserprefs'), 'delete', function (e, ref) {
var lock = ref.set_busy(true, 'sauserprefs.purgingbayes');
ref.http_request('plugin.sauserprefs.purge_bayes', '', lock);
});
return false;
}, true);
rcmail.register_command('plugin.sauserprefs.save', function () { rcmail.gui_objects.editform.submit(); }, true);
rcmail.register_command('plugin.sauserprefs.default', function () {
var reset_func = function (e, ref) {
$('#rcmfd_spamthres').val(''); // Score
$('#rcmfd_spamsubject').val(ref.env.rewrite_header_Subject); // Subject tag
// Languages
var dlangs = ' ' + ref.env.ok_languages + ' ';
$.each($('input[name="_spamlang[]"]'), function (idx) {
$(this).prop('checked', false);
$('[id^=spam_lang_]').eq(idx).attr('title', ref.get_label('disabled', 'sauserprefs')).removeClass('lang-enabled').addClass('lang-disabled');
if (dlangs.indexOf(' ' + $(this).val() + ' ') > -1 || ref.env.ok_languages == 'all') {
$(this).prop('checked', true);
$('[id^=spam_lang_]').eq(idx).attr('title', ref.get_label('enabled', 'sauserprefs')).removeClass('lang-disabled').addClass('lang-enabled');
}
});
// Defaults for checkboxes
var checkboxes = {
// Tests
rcmfd_spamuserazor1: ref.env.use_razor1 == '1',
rcmfd_spamuserazor2: ref.env.use_razor2 == '1',
rcmfd_spamusepyzor: ref.env.use_pyzor == '1',
rcmfd_spamusedcc: ref.env.use_dcc == '1',
rcmfd_spamskiprblchecks: ref.env.skip_rbl_checks == '0',
// Bayes
rcmfd_spamusebayes: ref.env.use_bayes == '1',
rcmfd_spambayesautolearn: ref.env.bayes_auto_learn == '1',
rcmfd_spambayesrules: ref.env.use_bayes_rules == '1',
// Headers
rcmfd_spamfoldheaders: ref.env.fold_headers == '1',
rcmfd_spamlevelstars: ref.env.add_header_all_Level != '',
// Report
rcmfd_spamreport_0: ref.env.report_safe == '0',
rcmfd_spamreport_1: ref.env.report_safe == '1',
rcmfd_spamreport_2: ref.env.report_safe == '2',
};
$.each(checkboxes, function (id, checked) { $('#' + id).prop('checked', checked); });
$('#rcmfd_bayesnonspam,#rcmfd_bayesspam').val(''); // Bayes non spam/spam score
$('#rcmfd_spamlevelchar').val(ref.env.add_header_all_Level.substr(7, 1)); // Spam level char
// Delete welcomelist
$.each($('#address-rules-table tbody tr'), function () { ref.sauserprefs_addressrule_delete_row(this); });
// Toggle dependant fields
ref.sauserprefs_toggle_level_char($('#rcmfd_spamlevelstars'));
ref.sauserprefs_toggle_bayes($('#rcmfd_spamusebayes'));
ref.sauserprefs_toggle_bayes_auto($('#rcmfd_spambayesautolearn'));
};
rcmail.confirm_dialog(rcmail.get_label('usedefaultconfirm', 'sauserprefs'), 'sauserprefs.saupusedefault', reset_func, { button_class: 'delete saupusedefault' });
}, true);
rcmail.register_command('plugin.sauserprefs.table_sort', function (props, obj) {
var id = props;
var idx = $(obj).parent('th').index();
var asc = !$(obj).parent('th').hasClass(rcmail.env.sauserprefs_table_sort_asc);
rcmail.sauserprefs_table_sort(id, idx, asc);
$(obj).parents('thead').first().find('th').removeClass(rcmail.env.sauserprefs_table_sort_asc).removeClass(rcmail.env.sauserprefs_table_sort_desc);
$(obj).parent('th').addClass(asc ? rcmail.env.sauserprefs_table_sort_asc : rcmail.env.sauserprefs_table_sort_desc);
rcmail.env.sauserprefs_sort[id] = [idx, asc.toString()];
rcmail.save_pref({ name: 'sauserprefs_sort', value: JSON.stringify(rcmail.env.sauserprefs_sort), env: true });
return false;
}, true);
rcmail.enable_command('plugin.sauserprefs.save', 'plugin.sauserprefs.default', true);
}
});
if (rcmail.env.action == 'plugin.sauserprefs') {
rcmail.section_select = function (list) {
var win, id = list.get_single_selection();
if (id && (win = this.get_frame_window(this.env.contentframe))) {
this.location_href({ _action: 'plugin.sauserprefs.edit', _section: id, _framed: 1 }, win, true);
}
};
}
}
});