|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * CODE SNIPPET: Replace synchronous auto-block with queue |
| 4 | + * |
| 5 | + * Location: modules/imap/handler_modules.php |
| 6 | + * Class: Hm_Handler_imap_report_spam |
| 7 | + * Method: process() |
| 8 | + * |
| 9 | + * STEP 1: Add this after line 2278 (after loading spam_report_services.php) |
| 10 | + */ |
| 11 | + |
| 12 | +// Include queue integration functions |
| 13 | +if (!function_exists('queue_spam_sender_for_blocking')) { |
| 14 | + require_once APP_PATH.'modules/imap/spam_queue_integration.php'; |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * STEP 2: Replace lines 2363-2437 with this code |
| 19 | + * |
| 20 | + * Find this section (starts around line 2363): |
| 21 | + * $move_result = $mailbox->message_action($form_folder, 'MOVE', array($uid), $junk_folder); |
| 22 | + * |
| 23 | + * Replace everything from there until: |
| 24 | + * $bulk_results[] = $result; |
| 25 | + * |
| 26 | + * With the code below: |
| 27 | + */ |
| 28 | + |
| 29 | +// Move message to junk folder |
| 30 | +$move_result = $mailbox->message_action($form_folder, 'MOVE', array($uid), $junk_folder); |
| 31 | +if ($move_result['status']) { |
| 32 | + $result['success'] = true; |
| 33 | + |
| 34 | + // Queue sender for async Sieve synchronization (non-blocking) |
| 35 | + $queue_result = queue_spam_sender_for_blocking( |
| 36 | + $this->user_config, |
| 37 | + $form['imap_server_id'], |
| 38 | + $message_data, |
| 39 | + $spam_reason, |
| 40 | + $junk_folder |
| 41 | + ); |
| 42 | + |
| 43 | + if ($queue_result['success'] && $queue_result['queued']) { |
| 44 | + // Successfully queued |
| 45 | + delayed_debug_log('Spam sender queued for Sieve sync', array( |
| 46 | + 'entry_id' => $queue_result['entry_id'], |
| 47 | + 'sender' => $queue_result['sender'], |
| 48 | + 'server_id' => $form['imap_server_id'] |
| 49 | + )); |
| 50 | + |
| 51 | + // Store queue info for UI feedback (optional) |
| 52 | + $result['queue_info'] = $queue_result; |
| 53 | + |
| 54 | + } else if (!$queue_result['success']) { |
| 55 | + // Queueing failed - log warning but don't fail the whole operation |
| 56 | + delayed_debug_log('Failed to queue sender for blocking', array( |
| 57 | + 'error' => isset($queue_result['error']) ? $queue_result['error'] : 'Unknown error', |
| 58 | + 'sender' => isset($message_data['from']) ? $message_data['from'] : 'unknown' |
| 59 | + ), 'warning'); |
| 60 | + |
| 61 | + // Message is still in junk, just won't be auto-blocked |
| 62 | + $result['error'] .= 'Auto-block queuing failed (sender will not be blocked automatically); '; |
| 63 | + } |
| 64 | + // If success=true but queued=false: auto-blocking is disabled by user |
| 65 | + |
| 66 | +} else { |
| 67 | + $result['error'] .= 'Move to junk failed; '; |
| 68 | +} |
| 69 | + |
| 70 | +$bulk_results[] = $result; |
| 71 | + |
| 72 | +/** |
| 73 | + * OPTIONAL STEP 3: Enhance success message |
| 74 | + * |
| 75 | + * Find this section (around line 2447-2451): |
| 76 | + * if ($status) { |
| 77 | + * Hm_Msgs::add('Message reported as spam and moved to junk folder', 'success'); |
| 78 | + * } else { |
| 79 | + * Hm_Msgs::add('Failed to report message as spam', 'danger'); |
| 80 | + * } |
| 81 | + * |
| 82 | + * Replace with: |
| 83 | + */ |
| 84 | + |
| 85 | +if ($status) { |
| 86 | + // Check if sender was queued for blocking |
| 87 | + if (isset($bulk_results[0]['queue_info']['queued']) && $bulk_results[0]['queue_info']['queued']) { |
| 88 | + Hm_Msgs::add( |
| 89 | + 'Message reported as spam and moved to junk folder. Sender will be blocked automatically.', |
| 90 | + 'success' |
| 91 | + ); |
| 92 | + } else { |
| 93 | + Hm_Msgs::add('Message reported as spam and moved to junk folder', 'success'); |
| 94 | + } |
| 95 | +} else { |
| 96 | + Hm_Msgs::add('Failed to report message as spam', 'danger'); |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * BULK OPERATION VERSION (around line 2461-2467) |
| 101 | + * |
| 102 | + * Find: |
| 103 | + * if ($success_count === count($bulk_results)) { |
| 104 | + * Hm_Msgs::add('All messages reported as spam and moved to junk folder', 'success'); |
| 105 | + * } elseif ($success_count > 0) { |
| 106 | + * Hm_Msgs::add("$success_count of " . count($bulk_results) . " messages reported as spam", 'warning'); |
| 107 | + * } else { |
| 108 | + * Hm_Msgs::add('Failed to report messages as spam', 'danger'); |
| 109 | + * } |
| 110 | + * |
| 111 | + * Replace with: |
| 112 | + */ |
| 113 | + |
| 114 | +// Count queued senders |
| 115 | +$queued_count = 0; |
| 116 | +foreach ($bulk_results as $result) { |
| 117 | + if (isset($result['queue_info']['queued']) && $result['queue_info']['queued']) { |
| 118 | + $queued_count++; |
| 119 | + } |
| 120 | +} |
| 121 | + |
| 122 | +if ($success_count === count($bulk_results)) { |
| 123 | + if ($queued_count > 0) { |
| 124 | + $msg = sprintf( |
| 125 | + 'All %d messages reported as spam and moved to junk folder. %d sender(s) queued for automatic blocking.', |
| 126 | + count($bulk_results), |
| 127 | + $queued_count |
| 128 | + ); |
| 129 | + } else { |
| 130 | + $msg = 'All messages reported as spam and moved to junk folder'; |
| 131 | + } |
| 132 | + Hm_Msgs::add($msg, 'success'); |
| 133 | +} elseif ($success_count > 0) { |
| 134 | + if ($queued_count > 0) { |
| 135 | + $msg = sprintf( |
| 136 | + '%d of %d messages reported as spam. %d sender(s) queued for blocking.', |
| 137 | + $success_count, |
| 138 | + count($bulk_results), |
| 139 | + $queued_count |
| 140 | + ); |
| 141 | + } else { |
| 142 | + $msg = "$success_count of " . count($bulk_results) . " messages reported as spam"; |
| 143 | + } |
| 144 | + Hm_Msgs::add($msg, 'warning'); |
| 145 | +} else { |
| 146 | + Hm_Msgs::add('Failed to report messages as spam', 'danger'); |
| 147 | +} |
| 148 | + |
| 149 | +/** |
| 150 | + * THAT'S IT! |
| 151 | + * |
| 152 | + * Summary of changes: |
| 153 | + * 1. Added require_once for spam_queue_integration.php |
| 154 | + * 2. Replaced ~70 lines of synchronous Sieve code with ~30 lines of queue code |
| 155 | + * 3. Enhanced user messages to mention queuing |
| 156 | + * |
| 157 | + * Result: |
| 158 | + * - Users get instant response (no waiting for Sieve) |
| 159 | + * - Senders are queued for blocking |
| 160 | + * - Cron job will process queue in background |
| 161 | + * - Automatic retries on failure |
| 162 | + * - No data loss |
| 163 | + */ |
| 164 | + |
0 commit comments