/** * (Manage)Sieve Filters plugin * * @licstart The following is the entire license notice for the * JavaScript code in this file. * * Copyright (c) The Roundcube Dev Team * * 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. */ if (window.rcmail) { rcmail.addEventListener('init', function(evt) { // add managesieve-create command to message_commands array, // so it's state will be updated on message selection/unselection if (rcmail.env.task == 'mail') { if (rcmail.env.action != 'show') rcmail.env.message_commands.push('managesieve-create'); else rcmail.enable_command('managesieve-create', true); } if (rcmail.env.task == 'mail' || rcmail.env.action.startsWith('plugin.managesieve')) { // Create layer for form tips if (!rcmail.env.framed) { rcmail.env.ms_tip_layer = $('
'); rcmail.env.ms_tip_layer.appendTo(document.body); } } // register commands rcmail.register_command('plugin.managesieve-save', function() { rcmail.managesieve_save() }); rcmail.register_command('plugin.managesieve-act', function() { rcmail.managesieve_act() }); rcmail.register_command('plugin.managesieve-add', function() { rcmail.managesieve_add() }); rcmail.register_command('plugin.managesieve-del', function() { rcmail.managesieve_del() }); rcmail.register_command('plugin.managesieve-move', function() { rcmail.managesieve_move() }); rcmail.register_command('plugin.managesieve-setadd', function() { rcmail.managesieve_setadd() }); rcmail.register_command('plugin.managesieve-setdel', function() { rcmail.managesieve_setdel() }); rcmail.register_command('plugin.managesieve-setact', function() { rcmail.managesieve_setact() }); rcmail.register_command('plugin.managesieve-setget', function() { rcmail.managesieve_setget() }); rcmail.register_command('plugin.managesieve-seteditraw', function() { rcmail.managesieve_seteditraw() }); if (rcmail.env.action.startsWith('plugin.managesieve')) { if (rcmail.gui_objects.sieveform) { rcmail.enable_command('plugin.managesieve-save', true); sieve_form_init(); } else if (rcmail.gui_objects.sievesetrawform) { rcmail.enable_command('plugin.managesieve-save', true); sieve_raw_editor_init(); } else { rcmail.enable_command('plugin.managesieve-add', !rcmail.env.sieveconnerror && $.inArray('new_rule', rcmail.env.managesieve_disabled_actions) == -1); rcmail.enable_command('plugin.managesieve-setadd', !rcmail.env.sieveconnerror && $.inArray('new_set', rcmail.env.managesieve_disabled_actions) == -1); } var setcnt, set = rcmail.env.currentset; if (rcmail.gui_objects.filterslist) { rcmail.filters_list = new rcube_list_widget(rcmail.gui_objects.filterslist, {multiselect:false, draggable:true, keyboard:true}); rcmail.filters_list .addEventListener('select', function(e) { rcmail.managesieve_select(e); }) .addEventListener('dragstart', function(e) { rcmail.managesieve_dragstart(e); }) .addEventListener('dragend', function(e) { rcmail.managesieve_dragend(e); }) .addEventListener('initrow', function(row) { row.obj.onmouseover = function() { rcmail.managesieve_focus_filter(row); }; row.obj.onmouseout = function() { rcmail.managesieve_unfocus_filter(row); }; }) .init(); } if (rcmail.gui_objects.filtersetslist) { rcmail.filtersets_list = new rcube_list_widget(rcmail.gui_objects.filtersetslist, {multiselect:false, draggable:false, keyboard:true}); rcmail.filtersets_list.init().focus(); if (set != null) { $('#filterset-name').text(set); set = rcmail.managesieve_setid(set); rcmail.filtersets_list.select(set); } // attach select event after initial record was selected rcmail.filtersets_list.addEventListener('select', function(e) { rcmail.managesieve_setselect(e); }); setcnt = rcmail.filtersets_list.rowcount; rcmail.enable_command('plugin.managesieve-set', true); rcmail.enable_command('plugin.managesieve-setact', setcnt > 0 && $.inArray('enable_disable_set', rcmail.env.managesieve_disabled_actions) == -1); rcmail.enable_command('plugin.managesieve-setget', setcnt > 0 && $.inArray('download_set', rcmail.env.managesieve_disabled_actions) == -1); rcmail.enable_command('plugin.managesieve-setdel', setcnt > 1 && $.inArray('delete_set', rcmail.env.managesieve_disabled_actions) == -1); rcmail.enable_command('plugin.managesieve-seteditraw', setcnt > 0 && rcmail.env.raw_sieve_editor); // Fix dragging filters over sets list $('tr', rcmail.gui_objects.filtersetslist).each(function (i, e) { rcmail.managesieve_fixdragend(e); }); } } if (rcmail.gui_objects.sieveform && rcmail.env.rule_disabled) $('#disabled').attr('checked', true); }); }; /*********************************************************/ /********* Managesieve UI methods *********/ /*********************************************************/ rcube_webmail.prototype.managesieve_add = function() { this.load_managesieveframe('_nav=hide', true); }; rcube_webmail.prototype.managesieve_del = function() { var id = this.filters_list.get_single_selection(); this.confirm_dialog(this.get_label('managesieve.filterdeleteconfirm'), 'delete', function(e, ref) { var post = '_act=delete&_fid=' + ref.filters_list.rows[id].uid, lock = ref.set_busy(true, 'loading'); ref.http_post('plugin.managesieve-action', post, lock); }); }; rcube_webmail.prototype.managesieve_act = function() { var id = this.filters_list.get_single_selection(), lock = this.set_busy(true, 'loading'); this.http_post('plugin.managesieve-action', '_act=act&_fid='+this.filters_list.rows[id].uid, lock); }; // Filter selection rcube_webmail.prototype.managesieve_select = function(list) { var id = list.get_single_selection(); if (id != null) { id = list.rows[id].uid; this.load_managesieveframe('_fid=' + id); } var has_id = typeof(id) != 'undefined' && id != null; this.enable_command('plugin.managesieve-act', has_id); this.enable_command('plugin.managesieve-del', has_id && $.inArray('delete_rule', rcmail.env.managesieve_disabled_actions) == -1); }; // Set selection rcube_webmail.prototype.managesieve_setselect = function(list) { this.enable_command('plugin.managesieve-setdel', list.rowcount > 1 && $.inArray('delete_set', rcmail.env.managesieve_disabled_actions) == -1); this.enable_command('plugin.managesieve-setact', list.rowcount > 0 && $.inArray('enable_disable_set', rcmail.env.managesieve_disabled_actions) == -1); this.enable_command('plugin.managesieve-setget', list.rowcount > 0 && $.inArray('delete_set', rcmail.env.managesieve_disabled_actions) == -1); this.enable_command('plugin.managesieve-seteditraw', list.rowcount > 0 && this.env.raw_sieve_editor); if (rcmail.env.contextmenu_opening) return; this.show_contentframe(false); this.filters_list.clear(true); var id = list.get_single_selection(); if (id != null) { this.managesieve_list(this.env.filtersets[id]); $('#filterset-name').text(this.env.filtersets[id]); } }; rcube_webmail.prototype.managesieve_rowid = function(id) { var i, rows = this.filters_list.rows; for (i in rows) if (rows[i] != null && rows[i].uid == id) return i; }; // Returns set's identifier rcube_webmail.prototype.managesieve_setid = function(name) { for (var i in this.env.filtersets) if (this.env.filtersets[i] == name) return i; }; // Filters listing request rcube_webmail.prototype.managesieve_list = function(script) { var lock = this.set_busy(true, 'loading'); this.http_post('plugin.managesieve-action', '_act=list&_set='+urlencode(script), lock); }; // Script download request rcube_webmail.prototype.managesieve_setget = function() { var id = this.filtersets_list.get_single_selection(), script = this.env.filtersets[id]; this.goto_url('plugin.managesieve-action', {_act: 'setget', _set: script}, false, true); }; // Set activate/deactivate request rcube_webmail.prototype.managesieve_setact = function() { var id = this.filtersets_list.get_single_selection(), lock = this.set_busy(true, 'loading'), script = this.env.filtersets[id], action = $('#rcmrow'+id).hasClass('disabled') ? 'setact' : 'deact'; this.http_post('plugin.managesieve-action', '_act='+action+'&_set='+urlencode(script), lock); }; // Set delete request rcube_webmail.prototype.managesieve_setdel = function() { var id = this.filtersets_list.get_single_selection(); this.confirm_dialog(this.get_label('managesieve.setdeleteconfirm'), 'delete', function(e, ref) { var script = ref.env.filtersets[id], lock = ref.set_busy(true, 'loading'); ref.http_post('plugin.managesieve-action', '_act=setdel&_set=' + urlencode(script), lock); }); }; // Set edit raw request rcube_webmail.prototype.managesieve_seteditraw = function() { var id = this.filtersets_list.get_single_selection(), script = this.env.filtersets[id]; this.load_managesieveframe('_nav=hide&_seteditraw=1&_set=' + urlencode(script), true); }; // Set add request rcube_webmail.prototype.managesieve_setadd = function() { this.load_managesieveframe('_nav=hide&_newset=1', true); }; rcube_webmail.prototype.managesieve_updatelist = function(action, o) { this.set_busy(true); switch (action) { // Delete filter row case 'del': var id = o.id, list = this.filters_list; list.remove_row(this.managesieve_rowid(o.id)); this.show_contentframe(false); this.reset_filters_list(); // filter identifiers changed, fix the list $('tr', this.filters_list.list).each(function() { // remove hidden (deleted) rows if (this.style.display == 'none') { $(this).detach(); return; } var rowid = this.id.substr(6); // remove all attached events $(this).off(); // update row id if (rowid > id) { this.uid = rowid - 1; $(this).attr('id', 'rcmrow' + this.uid); } }); list.init(); break; // Update filter row case 'update': var i, row = $('#rcmrow'+this.managesieve_rowid(o.id)); if (o.name) $('td', row).text(o.name); if (o.disabled) row.addClass('disabled'); else row.removeClass('disabled'); $('#disabled', $('iframe').contents()).prop('checked', o.disabled); break; // Add filter row to the list case 'add': var list = this.filters_list, row = $('