From 783c099108e5da2e2eea728b7765253b7a6ed030 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Thu, 7 Jan 2016 15:59:41 +0000
Subject: [PATCH] Decouple autoresponse from alerts

This is commit reverts prior commit to prevent email loops by dead-banning
alerts and auto-responses based on recent communication with the end user.
---
 include/class.thread.php |  2 ++
 include/class.ticket.php | 35 ++++++++++++++---------------------
 2 files changed, 16 insertions(+), 21 deletions(-)

diff --git a/include/class.thread.php b/include/class.thread.php
index 06ad342f5..0a1162487 100644
--- a/include/class.thread.php
+++ b/include/class.thread.php
@@ -604,6 +604,8 @@ implements TemplateVariable {
     var $_headers;
     var $_thread;
     var $_actions;
+    var $is_autoreply;
+    var $is_bounce;
 
     static protected $perms = array(
         self::PERM_EDIT => array(
diff --git a/include/class.ticket.php b/include/class.ticket.php
index 733529c4d..9cc8b4492 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -1542,6 +1542,9 @@ implements RestrictedAccess, Threadable {
                 $this->setStaffId(0); // Clear assignment
         }
 
+        if (!$autorespond)
+            return;
+
         // Figure out the user
         if ($this->getOwnerId() == $message->getUserId())
             $user = new TicketOwner(
@@ -1553,11 +1556,11 @@ implements RestrictedAccess, Threadable {
 
         /**********   double check auto-response  ************/
         if (!$user)
-            $autorespond=false;
-        elseif ($autorespond && (Email::getIdByEmail($user->getEmail())))
-            $autorespond=false;
-        elseif ($autorespond && ($dept=$this->getDept()))
-            $autorespond=$dept->autoRespONNewMessage();
+            $autorespond = false;
+        elseif ((Email::getIdByEmail($user->getEmail())))
+            $autorespond = false;
+        elseif (($dept=$this->getDept()))
+            $autorespond = $dept->autoRespONNewMessage();
 
         if (!$autorespond
             || !$cfg->autoRespONNewMessage()
@@ -2293,28 +2296,18 @@ implements RestrictedAccess, Threadable {
         }
 
         // Do not auto-respond to bounces and other auto-replies
-        if ($alerts)
-            $alerts = isset($vars['mailflags'])
+        $autorespond = isset($vars['mailflags'])
                 ? !$vars['mailflags']['bounce'] && !$vars['mailflags']['auto-reply']
                 : true;
-        if ($alerts && $message->isBounceOrAutoReply())
-            $alerts = false;
-
-        if ($alerts && $this->getThread()->getLastEmailMessage(array(
-            'user_id' => $message->user_id,
-            'id__lt' => $message->id,
-            'created__gt' => SqlFunction::NOW()->minus(SqlInterval::MINUTE(5)),
-        ))) {
-            // One message already from this user in the last five minutes
-            $alerts = false;
-        }
+        if ($autorespond && $message->isBounceOrAutoReply())
+            $autorespond = false;
 
-        $this->onMessage($message, $alerts); //must be called b4 sending alerts to staff.
+        $this->onMessage($message, $autorespond); //must be called b4 sending alerts to staff.
 
-        if ($alerts && $cfg && $cfg->notifyCollabsONNewMessage())
+        if ($autorespond && $cfg && $cfg->notifyCollabsONNewMessage())
             $this->notifyCollaborators($message, array('signature' => ''));
 
-        if (!$alerts)
+        if (!($alerts && $autorespond))
             return $message; //Our work is done...
 
         $dept = $this->getDept();
-- 
GitLab