Skip to content

Commit

Permalink
Merge branch 'add-a-toggle-top-activate-deactivate-a-Cypht-Sieve-rule…
Browse files Browse the repository at this point in the history
…' of https://github.com/amaninyumu1/cypht into add-a-toggle-top-activate-deactivate-a-Cypht-Sieve-rule
  • Loading branch information
kroky authored and amaninyumu1 committed Sep 12, 2024
1 parent 2bf174c commit 37133d4
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 52 deletions.
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"ext-openssl": "*",
"ext-session": "*",
"ezyang/htmlpurifier": "^4.17",
"henrique-borba/php-sieve-manager": "^1.0",
"henrique-borba/php-sieve-manager": "~1.0.4",
"league/commonmark": "^2.4",
"paragonie/random_compat": "^2.0.18",
"php": ">=8.1",
Expand Down Expand Up @@ -83,4 +83,4 @@
"post-package-install": "composer suggest",
"post-install-cmd": "composer suggest"
}
}
}
99 changes: 66 additions & 33 deletions modules/sievefilters/modules.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function process() {
$this->out('conditions', json_encode(base64_decode($base64_obj)));
$base64_obj = str_replace("# ", "", preg_split('#\r?\n#', $script, 0)[2]);
$this->out('actions', json_encode(base64_decode($base64_obj)));
if (mb_strstr($script, 'allof')) {
if (strstr($script, 'allof')) {
$this->out('test_type', 'ALLOF');
} else {
$this->out('test_type', 'ANYOF');
Expand Down Expand Up @@ -160,6 +160,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) {
return;
}
$imap_account = Hm_IMAP_List::dump($form['imap_account']);
$factory = get_sieve_client_factory($this->config);
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}_");
}
}
$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()}");
}
}
}

/**
* @subpackage sievefilters/handler
Expand Down Expand Up @@ -223,7 +263,7 @@ public function process() {
$blocked_wildcard = '@'.$domain;
$new_blocked_list = [];
foreach ($blocked_list as $idx => $blocked_sender) {
if (!mb_strstr($blocked_sender, $blocked_wildcard)) {
if (!strstr($blocked_sender, $blocked_wildcard)) {
$new_blocked_list[] = $blocked_sender;
}
}
Expand Down Expand Up @@ -331,7 +371,7 @@ public function process() {
}

$email_sender = $this->request->post['sender'];
if (mb_strstr($email_sender, '*')) {
if (strstr($email_sender, '*')) {
$email_sender = str_replace('*', '', $email_sender);
}

Expand Down Expand Up @@ -635,7 +675,7 @@ public function process() {
$script_name = generate_filter_name($this->request->post['sieve_filter_name'], $priority);
$conditions = json_decode($this->request->post['conditions_json']);
$actions = json_decode($this->request->post['actions_json']);
$test_type = mb_strtolower($this->request->post['filter_test_type']);
$test_type = strtolower($this->request->post['filter_test_type']);

$filter = \PhpSieveManager\Filters\FilterFactory::create($script_name);
$custom_condition = new \PhpSieveManager\Filters\Condition(
Expand Down Expand Up @@ -890,7 +930,7 @@ public function process() {
$custom_condition->addCriteria($cond);
}
}

foreach ($actions as $action) {
if ($action->action == 'discard') {
$custom_condition->addAction(
Expand All @@ -909,53 +949,51 @@ public function process() {
}
if ($action->action == 'redirect') {
$custom_condition->addAction(
new \PhpSieveManager\Filters\Actions\RedirectFilterAction(['address' => $action->value])
);
}
if ($action->action == 'forward') {
$custom_condition->addAction(
new \PhpSieveManager\Filters\Actions\RedirectFilterAction(['address' => $action->value])
);
$custom_condition->addAction(
new \PhpSieveManager\Filters\Actions\KeepFilterAction()
new \PhpSieveManager\Filters\Actions\RedirectFilterAction([$action->value])
);
}
if ($action->action == 'flag') {
$custom_condition->addAction(
new \PhpSieveManager\Filters\Actions\FlagFilterAction(['flags' => [$action->value]])
new \PhpSieveManager\Filters\Actions\FlagFilterAction([$action->value])
);
}
if ($action->action == 'addflag') {
if ($action->action == 'addflag') {
$filter->addRequirement('imap4flags');
$custom_condition->addAction(
new \PhpSieveManager\Filters\Actions\AddFlagFilterAction(['flags' => [$action->value]])
new \PhpSieveManager\Filters\Actions\AddFlagFilterAction([$action->value])
);
}
if ($action->action == 'removeflag') {
$filter->addRequirement('imap4flags');
$custom_condition->addAction(
new \PhpSieveManager\Filters\Actions\RemoveFlagFilterAction(['flags' => [$action->value]])
new \PhpSieveManager\Filters\Actions\RemoveFlagFilterAction([$action->value])
);
}
if ($action->action == 'move') {
$filter->addRequirement('fileinto');
$custom_condition->addAction(
new \PhpSieveManager\Filters\Actions\FileIntoFilterAction(['mailbox' => [$action->value]])
new \PhpSieveManager\Filters\Actions\FileIntoFilterAction([$action->value])
);
}
if ($action->action == 'reject') {
$filter->addRequirement('reject');
$custom_condition->addAction(
new \PhpSieveManager\Filters\Actions\RejectFilterAction(['reason' => $action->value])
new \PhpSieveManager\Filters\Actions\RejectFilterAction([$action->value])
);
}
if ($action->action == 'copy') {
$filter->addRequirement('fileinto');
$custom_condition->addAction(
new \PhpSieveManager\Filters\Actions\FileIntoFilterAction(['mailbox' => $action->value])
new \PhpSieveManager\Filters\Actions\FileIntoFilterAction([$action->value])
);
$custom_condition->addAction(
new \PhpSieveManager\Filters\Actions\KeepFilterAction()
);
}
if ($action->action == 'autoreply') {
$filter->addRequirement('vacation');
$custom_condition->addAction(
new \PhpSieveManager\Filters\Actions\VacationFilterAction(['reason' => $action->value, 'subject' => $action->extra_option_value])
new \PhpSieveManager\Filters\Actions\VacationFilterAction([$action->extra_option_value, $action->value])
);
}
}
Expand Down Expand Up @@ -1106,7 +1144,7 @@ protected function output() {
if (!$this->get('sieve_filters_enabled')) {
return '';
}
$res = '<li class="menu_sieve_filters"><a class="unread_link" href="?page=sieve_filters">';
$res = '<li class="menu_filters"><a class="unread_link" href="?page=sieve_filters">';
if (!$this->get('hide_folder_icons')) {
$res .= '<i class="bi bi-journal-bookmark-fill fs-5 me-2"></i>';
}
Expand Down Expand Up @@ -1179,9 +1217,6 @@ protected function output() {
$res .= get_script_modal_content();
$res .= '<div class="p-3">';
foreach($mailboxes as $idx => $mailbox) {
if (empty($mailbox['sieve_config_host'])) {
continue;
}
$behaviours = $this->get('sieve_block_default_behaviour');
$reject_messages = $this->get('sieve_block_default_reject_message');
$default_behaviour = 'Discard';
Expand All @@ -1193,20 +1228,21 @@ protected function output() {
$default_reject_message = $reject_messages[$idx];
}

$default_behaviour_html = '<div class="col-xl-9 mb-4"><div class="input-group"><span class="input-group-text">Default Behaviour:</span> <select class="select_default_behaviour form-select " imap_account="'.$idx.'">'
$default_behaviour_html = '<div class="col-xl-9 mb-4"><div class="input-group"><span class="input-group-text">Default Behaviour:</span> <select class="select_default_behaviour form-select " imap_account="'.$idx.'">'.
$default_behaviour_html = '<div class="col-sm-7 mb-4"><div class="input-group"><span class="input-group-text">Default Behaviour:</span> <select class="select_default_behaviour form-control" imap_account="'.$idx.'">'
.'<option value="Discard"'.($default_behaviour == 'Discard'? ' selected': '').'>Discard</option>'
.'<option value="Reject"'.($default_behaviour == 'Reject'? ' selected': '').'>'.$this->trans('Reject').'</option>'
.'<option value="Move" '.($default_behaviour == 'Move'? ' selected': '').'>'.$this->trans('Move To Blocked Folder').'</option></select>';
if ($default_behaviour == 'Reject') {
$default_behaviour_html .= '<input type="text" class="select_default_reject_message form-control" value="'.$default_reject_message.'" placeholder="'.$this->trans('Reject message').'" />';
}
$default_behaviour_html .= '<button class="submit_default_behavior btn btn-primary">'.$this->trans('Submit').'</button></div></div>';
$default_behaviour_html .= '<button class="submit_default_behavior btn btn-primary">Submit</button></div></div>';
$blocked_senders = get_blocked_senders_array($mailbox, $this->get('site_config'), $this->get('user_config'));
$num_blocked = $blocked_senders ? sizeof($blocked_senders) : 0;
$res .= '<div class="sievefilters_accounts_item">';
$res .= '<div class="sievefilters_accounts_title settings_subtitle py-2 border-bottom cursor-pointer d-flex justify-content-between">' . $mailbox['name'];
$res .= '<span class="filters_count"><span id="filter_num_'.$idx.'">'.$num_blocked.'</span> '.$this->trans('blocked'). '</span></div>';
$res .= '<div class="sievefilters_accounts filter_block py-3 d-none"><div class="filter_subblock">';
$res .= '<div class="sievefilters_accounts filter_block px-5 py-3 d-none"><div class="filter_subblock">';
$res .= $default_behaviour_html;
$res .= '<table class="filter_details table"><tbody>';
$res .= '<tr><th class="col-sm-6">Sender</th><th class="col-sm-3">Behavior</th><th class="col-sm-3">Actions</th></tr>';
Expand All @@ -1233,9 +1269,6 @@ protected function output() {
$res .= get_script_modal_content();
$sieve_supported = 0;
foreach($mailboxes as $mailbox) {
if (empty($mailbox['sieve_config_host'])) {
continue;
}
$sieve_supported++;
$result = get_mailbox_filters($mailbox, $this->get('site_config'), $this->get('user_config'));
$num_filters = $result['count'];
Expand Down Expand Up @@ -1344,4 +1377,4 @@ public function process() {
}
}
}
}
}
14 changes: 12 additions & 2 deletions modules/sievefilters/setup.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@
add_handler('ajax_sieve_delete_filter', 'sieve_delete_filter', true);
add_output('ajax_sieve_delete_filter', 'sieve_delete_output', true);

/**
* 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);


/* save script */
setup_base_ajax_page('ajax_sieve_save_script', 'core');
add_handler('ajax_sieve_save_script', 'settings_load_imap', true);
Expand Down Expand Up @@ -110,6 +119,7 @@
'ajax_sieve_save_filter',
'ajax_sieve_edit_filter',
'ajax_sieve_delete_filter',
'ajax_sieve_toggle_script_state',
'ajax_sieve_block_unblock',
'ajax_sieve_unblock_sender',
'ajax_sieve_get_mailboxes',
Expand All @@ -125,12 +135,12 @@
'test_type' => array(FILTER_UNSAFE_RAW, false),
'mailboxes' => array(FILTER_UNSAFE_RAW, false),
'sieve_detail_display' => array(FILTER_UNSAFE_RAW, false),
'imap_extensions_display' => array(FILTER_UNSAFE_RAW, false),
'script_details' => array(FILTER_UNSAFE_RAW, FILTER_REQUIRE_ARRAY),
),
'allowed_get' => array(),
'allowed_post' => array(
'imap_account' => FILTER_DEFAULT,
'script_state' => FILTER_DEFAULT,
'sieve_script_name' => FILTER_DEFAULT,
'sieve_script_priority' => FILTER_VALIDATE_INT,
'sieve_filter_name' => FILTER_DEFAULT,
Expand All @@ -153,4 +163,4 @@
'change_behavior' => FILTER_VALIDATE_BOOL,
'gen_script' => FILTER_VALIDATE_BOOL,
)
);
);
32 changes: 17 additions & 15 deletions modules/sievefilters/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,6 @@ var hm_sieve_possible_actions = function() {
type: 'string',
extra_field: false
},
{
name: 'forward',
description: 'Forward',
placeholder: '[email protected]',
type: 'string',
extra_field: false
},
{
name: 'reject',
description: 'Reject',
Expand Down Expand Up @@ -539,12 +532,6 @@ $(function () {
$('#stop_filtering').prop('checked', false);
current_account = $(this).attr('account');
edit_filter_modal.open();

// Reset the form fields when opening the modal
$(".modal_sieve_filter_name").val('');
$(".modal_sieve_script_priority").val('');
$(".sieve_list_conditions_modal").empty();
$(".filter_actions_modal_table").empty();
});
$('.add_script').on('click', function () {
edit_script_modal.setTitle('Add Script');
Expand Down Expand Up @@ -863,7 +850,22 @@ $(function () {
);
});


/**
* Toggle Filter
*/
$('.toggle_filter').on('change', function () {
Hm_Ajax.request(
[ {'name': 'hm_ajax_hook', 'value': 'ajax_sieve_toggle_script_state'},
{'name': 'imap_account', 'value': $(this).attr('imap_account')},
{'name': 'script_state', 'value': $(this).prop('checked')},
{'name': 'sieve_script_name', 'value': $(this).attr('script_name')}],
function(res) {
if (res.router_user_msgs[0].startsWith('ERR')) {
$(this).prop('checked', !$(this).prop('checked'));
}
}
);
});
/**
* Delete script event
*/
Expand Down Expand Up @@ -963,4 +965,4 @@ $(function () {
);
});
}
});
});

0 comments on commit 37133d4

Please sign in to comment.