Skip to content

Commit

Permalink
Merge pull request #1104 from amaninyumu1/add-a-toggle-top-activate-d…
Browse files Browse the repository at this point in the history
…eactivate-a-Cypht-Sieve-rule

[ENH] Added functionality to enable or disable Sieve Filters with toggle button
  • Loading branch information
kroky authored Oct 21, 2024
2 parents e61afbb + 1b23ba6 commit 4da09be
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 0 deletions.
11 changes: 11 additions & 0 deletions modules/sievefilters/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,22 @@ function get_mailbox_filters($mailbox, $site_config, $user_config)
foreach ($scripts_sorted as $script_name => $sc) {
$exp_name = explode('-', $script_name);
$parsed_name = str_replace('_', ' ', $exp_name[0]);
$display_name = str_replace('_', ' ', implode('-', array_slice($exp_name, 0, count($exp_name) - 2)));
$checked = ' checked';
if (preg_match('/^s(en|dis)abled/', $display_name)) {
$display_name = str_replace(['senabled ', 'sdisabled '], '', $display_name);
if (strpos($display_name, 'sdisabled') === 0) {
$checked = '';
}
}
$script_list .= '
<tr>
<td>'. $exp_name[sizeof($exp_name) - 2] .'</td>
<td>' . str_replace('_', ' ', implode('-', array_slice($exp_name, 0, count($exp_name) - 2))) . '</td>
<td>
<span class="form-switch">
<input script_name_parsed="'.$parsed_name.'" priority="'.$exp_name[sizeof($exp_name) - 2].'" imap_account="'.$mailbox['name'].'" script_name="'.$script_name.'" class="toggle_filter form-check-input" type="checkbox" role="switch" id="Check" name="script_state"'.$checked.'>
</span>
<a href="#" script_name_parsed="'.$parsed_name.'" priority="'.$exp_name[sizeof($exp_name) - 2].'" imap_account="'.$mailbox['name'].'" script_name="'.$script_name.'" class="edit_'.$base_class.'">
<i class="bi bi-pencil-fill"></i>
</a>
Expand Down
43 changes: 43 additions & 0 deletions modules/sievefilters/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -1344,3 +1344,46 @@ public function process() {
}
}
}

/**
* @subpackage sievefilterstoggle/handler
*/
class Hm_Handler_sieve_toggle_script_state extends Hm_Handler_Module {
public function process() {
list($success, $form) = $this->process_form(array('imap_account', 'script_state', 'sieve_script_name'));
if (!$success) {
$this->out('success', false);
return;
}
$imap_account = Hm_IMAP_List::dump($form['imap_account']);
$factory = get_sieve_client_factory($this->config);
$success = false;
try {
$client = $factory->init($this->user_config, $imap_account);
$state = $form['script_state'] ? 'enabled': 'disabled';
$scripts = $client->listScripts();
foreach ($scripts as $key => $script) {
if ($script == 'main_script') {
$client->removeScripts('main_script');
}
if ($script == $form['sieve_script_name']) {
if (! $form['script_state']) {
unset($scripts[$key]);
}
$client->renameScript($script, "s{$state}_");
$success = true;
}
}
$scripts = $client->listScripts();
$main_script = generate_main_script($scripts);
save_main_script($client, $main_script, $scripts);
$client->activateScript('main_script');
$client->close();

Hm_Msgs::add("Script $state");
} catch (Exception $e) {
Hm_Msgs::add("ERRSieve: {$e->getMessage()}");
}
$this->out('success', $success);
}
}
10 changes: 10 additions & 0 deletions modules/sievefilters/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,14 @@
add_handler('settings', 'process_enable_sieve_filter_setting', true, 'sievefilters', 'save_user_settings', 'before');
add_output('settings', 'enable_sieve_filter_setting', true, 'sievefilters', 'start_general_settings', 'after');

/**
* toggle fliter
*/
setup_base_ajax_page('ajax_sieve_toggle_script_state', 'core');
add_handler('ajax_sieve_toggle_script_state', 'load_imap_servers_from_config', true, 'imap', 'load_user_data', 'after');
add_handler('ajax_sieve_toggle_script_state', 'settings_load_imap', true);
add_handler('ajax_sieve_toggle_script_state', 'sieve_toggle_script_state', true);

return array(
'allowed_pages' => array(
'block_list',
Expand All @@ -115,6 +123,7 @@
'ajax_sieve_get_mailboxes',
'ajax_sieve_block_domain',
'ajax_sieve_block_change_behaviour',
'ajax_sieve_toggle_script_state',
),
'allowed_output' => array(
'imap_server_ids' => array(FILTER_UNSAFE_RAW, false),
Expand All @@ -130,6 +139,7 @@
),
'allowed_get' => array(),
'allowed_post' => array(
'script_state' => FILTER_DEFAULT,
'imap_account' => FILTER_DEFAULT,
'sieve_script_name' => FILTER_DEFAULT,
'sieve_script_priority' => FILTER_VALIDATE_INT,
Expand Down
17 changes: 17 additions & 0 deletions modules/sievefilters/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -1511,6 +1511,23 @@ function sieveFiltersPageHandler() {
);
});

/**
* Toggle Filter
*/
$('.toggle_filter').on('change', function () {
const checkbox = $(this);
Hm_Ajax.request(
[ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_toggle_script_state'},
{'name': 'imap_account', 'value': checkbox.attr('imap_account')},
{'name': 'script_state', 'value': checkbox.prop('checked')},
{'name': 'sieve_script_name', 'value': checkbox.attr('script_name')}],
function(res) {
if (res.success) {
checkbox.prop('checked', !checkbox.prop('checked'));
}
}
);
});

/**
* Delete script event
Expand Down

0 comments on commit 4da09be

Please sign in to comment.