Skip to content

Commit 10360b4

Browse files
committed
Implement auto-block sender after spam report
1 parent 791b582 commit 10360b4

File tree

5 files changed

+388
-2
lines changed

5 files changed

+388
-2
lines changed

modules/imap/handler_modules.php

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1836,7 +1836,7 @@ public function process() {
18361836
$imap_details = Hm_IMAP_List::dump($form['imap_server_id'], true);
18371837
if ($success && $imap_details) {
18381838
if ($this->module_is_supported('sievefilters') && $this->user_config->get('enable_sieve_filter_setting', DEFAULT_ENABLE_SIEVE_FILTER)) {
1839-
$factory = get_sieve_client_factory($this->site_config);
1839+
$factory = get_sieve_client_factory($this->config);
18401840
try {
18411841
$client = $factory->init($this->user_config, $imap_details, $this->module_is_supported('nux'));
18421842
} catch (Exception $e) {
@@ -2200,6 +2200,50 @@ public function process() {
22002200
$result = $mailbox->message_action($form_folder, 'MOVE', array($form['imap_msg_uid']), $junk_folder);
22012201
$status = $result['status'];
22022202
delayed_debug_log('Move action result:', array('status' => $status, 'result' => $result));
2203+
2204+
// Auto-block sender after successful spam report
2205+
if ($status) {
2206+
delayed_debug_log('Message moved successfully, attempting auto-block sender');
2207+
2208+
// Check if Sieve filters are enabled for this server
2209+
$imap_servers = $this->user_config->get('imap_servers', array());
2210+
$imap_account = null;
2211+
foreach ($imap_servers as $idx => $mailbox_config) {
2212+
if ($idx == $form['imap_server_id']) {
2213+
$imap_account = $mailbox_config;
2214+
break;
2215+
}
2216+
}
2217+
2218+
if ($imap_account && isset($imap_account['sieve_config_host'])) {
2219+
delayed_debug_log('Sieve filters enabled, proceeding with auto-block');
2220+
2221+
// Perform auto-block
2222+
$auto_block_result = auto_block_spam_sender(
2223+
$this->user_config,
2224+
$this->config,
2225+
$form['imap_server_id'],
2226+
$message_data,
2227+
$spam_reason
2228+
);
2229+
2230+
delayed_debug_log('Auto-block result:', $auto_block_result);
2231+
2232+
if ($auto_block_result['success']) {
2233+
Hm_Msgs::add($auto_block_result['message'], 'success');
2234+
delayed_debug_log('Auto-block successful', array(
2235+
'sender' => $auto_block_result['sender'],
2236+
'action' => $auto_block_result['action'],
2237+
'scope' => $auto_block_result['scope']
2238+
));
2239+
} else {
2240+
Hm_Msgs::add('Auto-block failed: ' . $auto_block_result['error'], 'warning');
2241+
delayed_debug_log('Auto-block failed', array('error' => $auto_block_result['error']));
2242+
}
2243+
} else {
2244+
delayed_debug_log('Sieve filters not enabled for this server, skipping auto-block');
2245+
}
2246+
}
22032247
}
22042248

22052249
if ($status) {
@@ -2220,3 +2264,19 @@ public function process() {
22202264
}
22212265
}
22222266
}
2267+
2268+
/**
2269+
* Process auto-block spam settings
2270+
* @subpackage imap/handler
2271+
*/
2272+
class Hm_Handler_process_auto_block_spam_setting extends Hm_Handler_Module {
2273+
public function process() {
2274+
function auto_block_spam_enabled_callback($val) { return $val; }
2275+
function auto_block_spam_action_callback($val) { return $val; }
2276+
function auto_block_spam_scope_callback($val) { return $val; }
2277+
2278+
process_site_setting('auto_block_spam_sender', $this, 'auto_block_spam_enabled_callback', true, true);
2279+
process_site_setting('auto_block_spam_action', $this, 'auto_block_spam_action_callback', 'move_to_junk', true);
2280+
process_site_setting('auto_block_spam_scope', $this, 'auto_block_spam_scope_callback', 'sender', true);
2281+
}
2282+
}

modules/imap/output_modules.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1659,3 +1659,58 @@ protected function output() {
16591659
return $res;
16601660
}
16611661
}
1662+
1663+
/**
1664+
* Auto-block spam settings UI
1665+
* @subpackage imap/output
1666+
*/
1667+
class Hm_Output_auto_block_spam_setting extends Hm_Output_Module {
1668+
protected function output() {
1669+
$settings = $this->get('user_settings', array());
1670+
$enabled = array_key_exists('auto_block_spam_sender', $settings) ? $settings['auto_block_spam_sender'] : true;
1671+
$action = array_key_exists('auto_block_spam_action', $settings) ? $settings['auto_block_spam_action'] : 'move_to_junk';
1672+
$scope = array_key_exists('auto_block_spam_scope', $settings) ? $settings['auto_block_spam_scope'] : 'sender';
1673+
1674+
$res = '<tr class="general_setting"><td colspan="2">';
1675+
$res .= '<div class="auto_block_spam_setting mb-3">';
1676+
$res .= '<div class="subtitle">' . $this->trans('Auto-Block Spam Sender Settings') . '</div>';
1677+
1678+
// Enable/Disable toggle
1679+
$res .= '<div class="form-check mb-3">';
1680+
$res .= '<input type="checkbox" id="auto_block_spam_sender" name="auto_block_spam_sender" class="form-check-input" value="1"' . ($enabled ? ' checked' : '') . '>';
1681+
$res .= '<label for="auto_block_spam_sender" class="form-check-label">' . $this->trans('Automatically block sender when reporting spam') . '</label>';
1682+
$res .= '</div>';
1683+
1684+
// Action selection
1685+
$res .= '<div class="mb-3">';
1686+
$res .= '<label for="auto_block_spam_action" class="form-label">' . $this->trans('Action for blocked senders') . '</label>';
1687+
$res .= '<select class="form-select" id="auto_block_spam_action" name="auto_block_spam_action">';
1688+
$res .= '<option value="move_to_junk"' . ($action == 'move_to_junk' ? ' selected' : '') . '>' . $this->trans('Move to Junk Folder') . '</option>';
1689+
$res .= '<option value="discard"' . ($action == 'discard' ? ' selected' : '') . '>' . $this->trans('Discard Messages') . '</option>';
1690+
$res .= '<option value="reject"' . ($action == 'reject' ? ' selected' : '') . '>' . $this->trans('Reject with Bounce') . '</option>';
1691+
$res .= '</select>';
1692+
$res .= '<div class="form-text">' . $this->trans('What happens to future messages from blocked senders') . '</div>';
1693+
$res .= '</div>';
1694+
1695+
// Scope selection
1696+
$res .= '<div class="mb-3">';
1697+
$res .= '<label for="auto_block_spam_scope" class="form-label">' . $this->trans('Blocking scope') . '</label>';
1698+
$res .= '<select class="form-select" id="auto_block_spam_scope" name="auto_block_spam_scope">';
1699+
$res .= '<option value="sender"' . ($scope == 'sender' ? ' selected' : '') . '>' . $this->trans('Block specific sender only') . '</option>';
1700+
$res .= '<option value="domain"' . ($scope == 'domain' ? ' selected' : '') . '>' . $this->trans('Block entire domain') . '</option>';
1701+
$res .= '</select>';
1702+
$res .= '<div class="form-text">' . $this->trans('Whether to block just the sender or the entire domain') . '</div>';
1703+
$res .= '</div>';
1704+
1705+
// Information note
1706+
$res .= '<div class="alert alert-info">';
1707+
$res .= '<i class="bi bi-info-circle"></i> ';
1708+
$res .= $this->trans('Auto-blocking requires Sieve filters to be enabled on your IMAP server. Future messages from blocked senders will be automatically filtered based on your chosen action.');
1709+
$res .= '</div>';
1710+
1711+
$res .= '</div>';
1712+
$res .= '</td></tr>';
1713+
1714+
return $res;
1715+
}
1716+
}

modules/imap/setup.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
add_handler('settings', 'process_setting_move_messages_in_screen_email', true, 'imap', 'process_first_time_screen_emails_per_page_setting', 'after');
5252
add_handler('settings', 'process_setting_active_preview_message', true, 'imap', 'process_setting_move_messages_in_screen_email', 'after');
5353
add_handler('settings', 'process_setting_ceo_detection_fraud', true, 'imap', 'process_setting_move_messages_in_screen_email', 'after');
54+
add_handler('settings', 'process_auto_block_spam_setting', true, 'imap', 'process_setting_ceo_detection_fraud', 'after');
5455
add_output('settings', 'imap_server_ids', true, 'imap', 'page_js', 'before');
5556
add_output('settings', 'start_sent_settings', true, 'imap', 'end_settings_form', 'before');
5657
add_output('settings', 'sent_since_setting', true, 'imap', 'start_sent_settings', 'after');
@@ -70,6 +71,7 @@
7071
add_output('settings', 'setting_move_messages_in_screen_email', true, 'imap', 'first_time_screen_emails_per_page_setting', 'after');
7172
add_output('settings', 'setting_active_preview_message', true, 'imap', 'setting_move_messages_in_screen_email', 'after');
7273
add_output('settings', 'setting_ceo_detection_fraud', true, 'imap', 'default_sort_order_setting', 'after');
74+
add_output('settings', 'auto_block_spam_setting', true, 'imap', 'setting_ceo_detection_fraud', 'after');
7375

7476
/* compose page data */
7577
add_output('compose', 'imap_server_ids', true, 'imap', 'page_js', 'before');

modules/imap/spam_report_config.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,19 @@ function get_spam_report_services() {
6565
);
6666
}
6767

68+
/**
69+
* Get auto-block configuration for spam reporting
70+
* @return array Auto-block configuration
71+
*/
72+
function get_auto_block_spam_config() {
73+
return array(
74+
'enabled' => true, // Whether auto-blocking is enabled
75+
'action' => 'move_to_junk', // Default action: move_to_junk, discard, reject
76+
'scope' => 'sender', // Default scope: sender, domain
77+
'junk_folder' => 'Junk' // Default junk folder name
78+
);
79+
}
80+
6881
/**
6982
* Helper function to get enabled spam reporting services
7083
* @return array List of enabled services
@@ -101,4 +114,31 @@ function get_spam_service_config($service_name) {
101114
function is_spam_service_enabled($service_name) {
102115
$config = get_spam_service_config($service_name);
103116
return $config && $config['enabled'];
117+
}
118+
119+
/**
120+
* Helper function to check if auto-blocking is enabled
121+
* @param object $user_config User configuration object
122+
* @return boolean True if auto-blocking is enabled
123+
*/
124+
function is_auto_block_spam_enabled($user_config) {
125+
return $user_config->get('auto_block_spam_sender', true);
126+
}
127+
128+
/**
129+
* Helper function to get auto-block action
130+
* @param object $user_config User configuration object
131+
* @return string Auto-block action
132+
*/
133+
function get_auto_block_spam_action($user_config) {
134+
return $user_config->get('auto_block_spam_action', 'move_to_junk');
135+
}
136+
137+
/**
138+
* Helper function to get auto-block scope
139+
* @param object $user_config User configuration object
140+
* @return string Auto-block scope
141+
*/
142+
function get_auto_block_spam_scope($user_config) {
143+
return $user_config->get('auto_block_spam_scope', 'sender');
104144
}

0 commit comments

Comments
 (0)