From 1156e84933841504ca6c069dcd9225add73b8521 Mon Sep 17 00:00:00 2001
From: Peter Rotich <peter@osticket.com>
Date: Fri, 7 Feb 2014 22:38:30 +0000
Subject: [PATCH] Make sure Delivered-To addresses are NOT added as
 collaborators

---
 include/class.mailparse.php | 29 +++++++++++++++++++++++++----
 include/class.ticket.php    |  4 ++++
 2 files changed, 29 insertions(+), 4 deletions(-)

diff --git a/include/class.mailparse.php b/include/class.mailparse.php
index 5cc22c97c..7f90a748e 100644
--- a/include/class.mailparse.php
+++ b/include/class.mailparse.php
@@ -171,7 +171,7 @@ class Mail_Parse {
         return Mail_Parse::parseAddressList($header);
     }
 
-    function getDeliveredTOAddressList() {
+    function getDeliveredToAddressList() {
         if (!($header = $this->struct->headers['delivered-to']))
             return null;
 
@@ -463,7 +463,12 @@ class EmailDataParser {
                 $data['name'] = $data['email'];
         }
 
-        //TO Address:Try to figure out the email address... associated with the incoming email.
+        /* Scan through the list of addressees (via To, Cc, and Delivered-To headers), and identify
+         * how the mail arrived at the system. One of the mails should be in the system email list.
+         * The recipient list (without the Delivered-To addressees) will be made available to the
+         * ticket filtering system. However, addresses in the Delivered-To header should never be
+         * considered for the collaborator list.
+         */
         $data['emailId'] = 0;
         $data['recipients'] = array();
         $tolist = array();
@@ -474,13 +479,13 @@ class EmailDataParser {
             $tolist['cc'] = $cc;
 
         if (($dt = $parser->getDeliveredToAddressList()))
-            $tolist['dt'] = $dt;
+            $tolist['delivered-to'] = $dt;
 
         foreach ($tolist as $source => $list) {
             foreach($list as $addr) {
                 if (!($emailId=Email::getIdByEmail(strtolower($addr->mailbox).'@'.$addr->host))) {
                     //Skip virtual Delivered-To addresses
-                    if ($source == 'dt') continue;
+                    if ($source == 'delivered-to') continue;
 
                     $data['recipients'][] = array(
                         'source' => "Email ($source)",
@@ -492,6 +497,22 @@ class EmailDataParser {
             }
         }
 
+        /*
+         * In the event that the mail was delivered to the system although none of the system
+         * mail addresses are in the addressee lists, be careful not to include the addressee
+         * in the collaborator list. Therefore, the delivered-to addressees should be flagged so they
+         * are not added to the collaborator list in the ticket creation process.
+         */
+        if ($tolist['delivered-to']) {
+            foreach ($tolist['delivered-to'] as $addr) {
+                foreach ($data['recipients'] as $i=>$r) {
+                    if (strcasecmp($r['email'], $addr->mailbox.'@'.$addr->host) === 0)
+                        $data['recipients'][$i]['source'] = 'delivered-to';
+                }
+            }
+        }
+
+
         //maybe we got BCC'ed??
         if(!$data['emailId']) {
             $emailId =  0;
diff --git a/include/class.ticket.php b/include/class.ticket.php
index 827f6d944..fd407d922 100644
--- a/include/class.ticket.php
+++ b/include/class.ticket.php
@@ -1531,6 +1531,10 @@ class Ticket {
                     'isactive' => ($message->getUserId() == $this->getUserId())? 1: 0);
             $collabs = array();
             foreach ($vars['recipients'] as $recipient) {
+                // Skip virtual delivered-to addresses
+                if (strcasecmp($recipient['source'], 'delivered-to') === 0)
+                    continue;
+
                 if (($user=User::fromVars($recipient)))
                     if ($c=$this->addCollaborator($user, $info, $errors))
                         $collabs[] = sprintf('%s%s',
-- 
GitLab