Skip to content

Commit e4f54e0

Browse files
committed
feat(spam): add 'Report Spam' functionality to message controls and implement AJAX handler
1 parent c06602d commit e4f54e0

File tree

5 files changed

+162
-5
lines changed

5 files changed

+162
-5
lines changed

modules/core/message_list_functions.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,6 +419,7 @@ function message_controls($output_mod) {
419419
'<li><a class="dropdown-item msg_delete core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="delete">'.$output_mod->trans('Delete').'</a></li>'.
420420
'<li><a class="dropdown-item msg_archive core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="archive">'.$output_mod->trans('Archive').'</a></li>'.
421421
'<li><a class="dropdown-item msg_junk core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="junk">'.$output_mod->trans('Junk').'</a></li>'.
422+
'<li><a class="dropdown-item msg_report_spam core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="report_spam">'.$output_mod->trans('Report Spam').'</a></li>'.
422423
'</ul>'.
423424
'</div>'.
424425
'<a class="msg_read core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="read">'.$output_mod->trans('Read').'</a>'.
@@ -427,7 +428,8 @@ function message_controls($output_mod) {
427428
'<a class="msg_unflag core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="unflag">'.$output_mod->trans('Unflag').'</a>'.
428429
'<a class="msg_delete core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="delete">'.$output_mod->trans('Delete').'</a>'.
429430
'<a class="msg_archive core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="archive">'.$output_mod->trans('Archive').'</a>'.
430-
'<a class="msg_junk core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="junk">'.$output_mod->trans('Junk').'</a>';
431+
'<a class="msg_junk core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="junk">'.$output_mod->trans('Junk').'</a>'.
432+
'<a class="msg_report_spam core_msg_control btn btn-sm btn-light no_mobile border text-black-50" href="#" data-action="report_spam">'.$output_mod->trans('Report Spam').'</a>';
431433

432434
if ($output_mod->get('msg_controls_extra')) {
433435
$res .= $output_mod->get('msg_controls_extra');

modules/imap/handler_modules.php

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2079,3 +2079,88 @@ function process_ceo_amount_limit_callback($val) { return $val; }
20792079
process_site_setting('ceo_rate_limit', $this, 'process_ceo_amount_limit_callback');
20802080
}
20812081
}
2082+
2083+
/**
2084+
* Report a message as spam
2085+
* @subpackage imap/handler
2086+
*/
2087+
class Hm_Handler_imap_report_spam extends Hm_Handler_Module {
2088+
/**
2089+
* Use IMAP to move the selected message to the junk folder
2090+
*/
2091+
public function process() {
2092+
Hm_Debug::add('Report Spam handler starting');
2093+
list($success, $form) = $this->process_form(array('imap_msg_uid', 'imap_server_id', 'folder'));
2094+
Hm_Debug::add('Form processing result:', array('success' => $success, 'form' => $form));
2095+
2096+
if (!$success) {
2097+
Hm_Debug::add('Form processing failed');
2098+
return;
2099+
}
2100+
2101+
$junk_folder = false;
2102+
$form_folder = hex2bin($form['folder']);
2103+
$errors = 0;
2104+
$status = null;
2105+
2106+
$specials = get_special_folders($this, $form['imap_server_id']);
2107+
Hm_Debug::add('Special folders:', $specials);
2108+
2109+
if (array_key_exists('junk', $specials) && $specials['junk']) {
2110+
$junk_folder = $specials['junk'];
2111+
Hm_Debug::add('Found junk folder:', $junk_folder);
2112+
}
2113+
2114+
$mailbox = Hm_IMAP_List::get_connected_mailbox($form['imap_server_id'], $this->cache);
2115+
Hm_Debug::add('Mailbox connection:', array(
2116+
'connected' => ($mailbox !== false),
2117+
'authed' => ($mailbox && $mailbox->authed()),
2118+
'is_imap' => ($mailbox && $mailbox->is_imap())
2119+
));
2120+
2121+
if ($mailbox && !$mailbox->is_imap() && empty($junk_folder)) {
2122+
Hm_Debug::add('Using EWS JUNK action');
2123+
// EWS supports moving to junk folder directly
2124+
$status = $mailbox->message_action($form_folder, 'JUNK', array($form['imap_msg_uid']))['status'];
2125+
Hm_Debug::add('EWS JUNK action result:', $status);
2126+
} else {
2127+
if (!$junk_folder) {
2128+
Hm_Debug::add('No junk folder configured');
2129+
Hm_Msgs::add('No junk folder configured for this IMAP server', 'warning');
2130+
$errors++;
2131+
}
2132+
2133+
if (!$errors && $mailbox && $mailbox->authed()) {
2134+
$junk_exists = count($mailbox->get_folder_status($junk_folder));
2135+
Hm_Debug::add('Junk folder status:', array(
2136+
'folder' => $junk_folder,
2137+
'exists' => $junk_exists
2138+
));
2139+
2140+
if (!$junk_exists) {
2141+
Hm_Debug::add('Junk folder does not exist');
2142+
Hm_Msgs::add('Configured junk folder for this IMAP server does not exist', 'warning');
2143+
$errors++;
2144+
}
2145+
2146+
/* try to move the message */
2147+
if (!$errors) {
2148+
Hm_Debug::add('Attempting to move message to junk folder');
2149+
$status = $mailbox->message_action($form_folder, 'MOVE', array($form['imap_msg_uid']), $junk_folder)['status'];
2150+
Hm_Debug::add('Move action result:', $status);
2151+
}
2152+
}
2153+
}
2154+
2155+
if ($status) {
2156+
Hm_Debug::add('Message successfully reported as spam');
2157+
Hm_Msgs::add("Message reported as spam");
2158+
} else {
2159+
Hm_Debug::add('Failed to report message as spam');
2160+
Hm_Msgs::add('An error occurred reporting the message as spam', 'danger');
2161+
}
2162+
2163+
$this->out('imap_report_spam_error', !$status);
2164+
$this->save_hm_msgs();
2165+
}
2166+
}

modules/imap/output_modules.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -388,9 +388,7 @@ protected function output() {
388388
$txt .= ' | <a class="hlink" id="copy_message" href="#">'.$this->trans('Copy').'</a>';
389389
$txt .= ' | <a class="hlink" id="move_message" href="#">'.$this->trans('Move').'</a>';
390390
$txt .= ' | <a class="archive_link hlink" id="archive_message" href="#">'.$this->trans('Archive').'</a>';
391-
if($this->get('tags')){
392-
$txt .= ' | '. tags_dropdown($this, $headers);
393-
}
391+
$txt .= ' | <a class="msg_report_spam core_msg_control btn btn-sm btn-light text-black-50" href="#" data-action="report_spam">'.$this->trans('Report Spam').'</a>';
394392
if (isset($headers['X-Schedule'])) {
395393
$txt .= ' | ' . schedule_dropdown($this, true);
396394
}

modules/imap/setup.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,14 @@
217217
add_handler('ajax_imap_archive_message', 'close_session_early', true, 'core');
218218
add_handler('ajax_imap_archive_message', 'imap_archive_message', true);
219219

220+
/* report spam message callback */
221+
setup_base_ajax_page('ajax_imap_report_spam', 'core');
222+
add_handler('ajax_imap_report_spam', 'message_list_type', true, 'core');
223+
add_handler('ajax_imap_report_spam', 'imap_message_list_type', true);
224+
add_handler('ajax_imap_report_spam', 'load_imap_servers_from_config', true);
225+
add_handler('ajax_imap_report_spam', 'imap_oauth2_token_check', true);
226+
add_handler('ajax_imap_report_spam', 'close_session_early', true, 'core');
227+
add_handler('ajax_imap_report_spam', 'imap_report_spam', true);
220228

221229
/* ajax message action callback */
222230
add_handler('ajax_message_action', 'load_imap_servers_from_config', true, 'imap', 'load_user_data', 'after');
@@ -300,6 +308,7 @@
300308
'ajax_imap_message_action',
301309
'ajax_imap_delete_message',
302310
'ajax_imap_archive_message',
311+
'ajax_imap_report_spam',
303312
'ajax_imap_flag_message',
304313
'ajax_imap_update_combined_source',
305314
'ajax_imap_mark_as_read',

modules/imap/site.js

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1264,8 +1264,15 @@ $(function() {
12641264
}
12651265
}
12661266
setTimeout(prefetch_imap_folders, 2);
1267-
});
12681267

1268+
$('#report_spam_message').on("click", function(e) { return imap_report_spam_message(); });
1269+
$(document).on('click', '.msg_report_spam', function(e) {
1270+
e.preventDefault();
1271+
var uid = getMessageUidParam();
1272+
var detail = Hm_Utils.parse_folder_path(getListPathParam(), 'imap');
1273+
return imap_report_spam_message(false, uid, detail);
1274+
});
1275+
});
12691276

12701277
var imap_archive_message = function(state, supplied_uid, supplied_detail) {
12711278
var uid = getMessageUidParam();
@@ -1306,6 +1313,62 @@ var imap_archive_message = function(state, supplied_uid, supplied_detail) {
13061313
return false;
13071314
};
13081315

1316+
var imap_report_spam_message = function(state, supplied_uid, supplied_detail) {
1317+
console.log('Report Spam clicked - Starting process');
1318+
var uid = getMessageUidParam();
1319+
var detail = Hm_Utils.parse_folder_path(getListPathParam(), 'imap');
1320+
console.log('Message details:', {uid: uid, detail: detail});
1321+
1322+
if (supplied_uid) {
1323+
uid = supplied_uid;
1324+
}
1325+
if (supplied_detail) {
1326+
detail = supplied_detail;
1327+
}
1328+
if (detail && uid) {
1329+
console.log('Making AJAX request to report spam with params:', {
1330+
hook: 'ajax_imap_report_spam',
1331+
uid: uid,
1332+
server_id: detail.server_id,
1333+
folder: detail.folder
1334+
});
1335+
Hm_Ajax.request(
1336+
[{'name': 'hm_ajax_hook', 'value': 'ajax_imap_report_spam'},
1337+
{'name': 'imap_msg_uid', 'value': uid},
1338+
{'name': 'imap_server_id', 'value': detail.server_id},
1339+
{'name': 'folder', 'value': detail.folder}],
1340+
function(res) {
1341+
console.log('Report spam AJAX response:', res);
1342+
if (!res.imap_report_spam_error) {
1343+
if (Hm_Utils.get_from_global('msg_uid', false)) {
1344+
console.log('Message UID in global, returning');
1345+
return;
1346+
}
1347+
var nlink = $('.nlink');
1348+
if (nlink.length && Hm_Utils.get_from_global('auto_advance_email_enabled')) {
1349+
console.log('Auto-advance enabled, redirecting to next message');
1350+
Hm_Utils.redirect(nlink.attr('href'));
1351+
}
1352+
else {
1353+
console.log('Redirecting to message list');
1354+
if (!hm_list_parent()) {
1355+
Hm_Utils.redirect("?page=message_list&list_path="+getListPathParam());
1356+
}
1357+
else {
1358+
Hm_Utils.redirect("?page=message_list&list_path="+hm_list_parent());
1359+
}
1360+
}
1361+
} else {
1362+
console.error('Error in report spam response:', res.imap_report_spam_error);
1363+
}
1364+
}
1365+
);
1366+
} else {
1367+
console.error('Missing required details:', {detail: detail, uid: uid});
1368+
}
1369+
return false;
1370+
};
1371+
13091372
var imap_show_add_contact_popup = function() {
13101373
var popup = document.getElementById("contact_popup");
13111374
popup.classList.toggle("show");

0 commit comments

Comments
 (0)