Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Server supports custom headers #1152

Merged
merged 1 commit into from
Aug 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 49 additions & 1 deletion modules/imap/hm-imap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ public function search($target='ALL', $uids=false, $terms=array(), $esearch=arra
if ($only_auto_bcc) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the same conditions must have been here too

$fld .= ' HEADER X-Auto-Bcc cypht';
}
if (!mb_strstr($this->server, 'yahoo') && $exclude_auto_bcc) {
if ($exclude_auto_bcc && !mb_strstr($this->server, 'yahoo') && $this->server_supports_custom_headers()) {
$fld .= ' NOT HEADER X-Auto-Bcc cypht';
}
$esearch_enabled = false;
Expand Down Expand Up @@ -2213,5 +2213,53 @@ public function get_folder_list_by_level($level='', $only_subscribed=false, $wit
}
return $result;
}

/**
* Test if the server supports searching by custom headers.
*
* This function sends a test search command to check if the server supports
* searching by custom headers (e.g., X-Auto-Bcc). If the server does not support
* this feature, it will return false.
*
* Reference: Stalwart's current limitation on searching by custom headers
* discussed in the following GitHub thread:
* https://github.com/stalwartlabs/mail-server/discussions/477
*
* Note: This function should be removed once Stalwart starts supporting custom headers.
*
* @return boolean true if the server supports searching by custom headers.
*/
protected function server_supports_custom_headers() {
$test_command = 'UID SEARCH HEADER "X-NonExistent-Header" "test"'."\r\n";
$this->send_command($test_command);
$response = $this->get_response(false, true);
$status = $this->check_response($response, true);

// Keywords that indicate the header search is not supported
$keywords = ['is', 'not', 'supported.'];

if (!$status) {
return false;
}

// Flatten the response array to a single array of strings
$flattened_response = array_reduce($response, 'array_merge', []);

// Check if all keywords are present in the flattened response
$sequence_match = true;
foreach ($keywords as $keyword) {
if (!in_array($keyword, $flattened_response)) {
$sequence_match = false;
break;
}
}

// If all keywords are found, the header search is not supported
if ($sequence_match) {
return false;
}

return true;
}
}
}