Skip to content
This repository has been archived by the owner on Apr 8, 2022. It is now read-only.

Commit

Permalink
MDL-48164 messageinbound: Attempt to detect autoreplies
Browse files Browse the repository at this point in the history
Any message which includes:
* Precedence: bulk
* X-Autoreply: !no
* X-Autoresponse: !no
* Auto-Submitted: !no

Will not be detected as a bulk message and therefore ignored.
  • Loading branch information
andrewnicols committed Dec 16, 2014
1 parent c9fa2de commit dd6c9ee
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions admin/tool/messageinbound/classes/manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ public function process_message(
// First flag this message to prevent another running hitting this message while we look at the headers.
$this->add_flag_to_message($messageid, self::MESSAGE_FLAGGED);

if ($this->is_bulk_message($message, $messageid)) {
mtrace("- The message has a bulk header set. This is likely an auto-generated reply - discarding.");
return;
}

// Record the user that this script is currently being run as. This is important when re-processing existing
// messages, as cron_setup_user is called multiple times.
$originaluser = $USER;
Expand Down Expand Up @@ -741,6 +746,43 @@ private function message_has_flag($messageid, $flag) {
return in_array($flag, $flags);
}

/**
* Attempt to determine whether this message is a bulk message (e.g. automated reply).
*
* @param \Horde_Imap_Client_Data_Fetch $message The message to process
* @param string|\Horde_Imap_Client_Ids $messageid The Hore message Uid
* @return boolean
*/
private function is_bulk_message(
\Horde_Imap_Client_Data_Fetch $message,
$messageid) {
$query = new \Horde_Imap_Client_Fetch_Query();
$query->headerText(array('peek' => true));

$messagedata = $this->client->fetch($this->get_mailbox(), $query, array('ids' => $messageid))->first();

// Assume that this message is not bulk to begin with.
$isbulk = false;

// An auto-reply may itself include the Bulk Precedence.
$precedence = $messagedata->getHeaderText(0, \Horde_Imap_Client_Data_Fetch::HEADER_PARSE)->getValue('Precedence');
$isbulk = $isbulk || strtolower($precedence) == 'bulk';

// If the X-Autoreply header is set, and not 'no', then this is an automatic reply.
$autoreply = $messagedata->getHeaderText(0, \Horde_Imap_Client_Data_Fetch::HEADER_PARSE)->getValue('X-Autoreply');
$isbulk = $isbulk || ($autoreply && $autoreply != 'no');

// If the X-Autorespond header is set, and not 'no', then this is an automatic response.
$autorespond = $messagedata->getHeaderText(0, \Horde_Imap_Client_Data_Fetch::HEADER_PARSE)->getValue('X-Autorespond');
$isbulk = $isbulk || ($autorespond && $autorespond != 'no');

// If the Auto-Submitted header is set, and not 'no', then this is a non-human response.
$autosubmitted = $messagedata->getHeaderText(0, \Horde_Imap_Client_Data_Fetch::HEADER_PARSE)->getValue('Auto-Submitted');
$isbulk = $isbulk || ($autosubmitted && $autosubmitted != 'no');

return $isbulk;
}

/**
* Send the message to the appropriate handler.
*
Expand Down

0 comments on commit dd6c9ee

Please sign in to comment.