From 30cf92eb68eb00f89577a9d555f79bb3c78c65a9 Mon Sep 17 00:00:00 2001
From: root <root@example.com>
Date: Thu, 20 Mar 2014 14:33:38 -0400
Subject: [PATCH] htc rfc822 mails

---
 ActiveSync/SOGoActiveSyncDispatcher.m |   26 +++++++++++++++++++++++++-
 1 file changed, 25 insertions(+), 1 deletion(-)

diff --git a/ActiveSync/SOGoActiveSyncDispatcher.m b/ActiveSync/SOGoActiveSyncDispatcher.m
index 1143219..9ade60a 100644
--- a/ActiveSync/SOGoActiveSyncDispatcher.m
+++ b/ActiveSync/SOGoActiveSyncDispatcher.m
@@ -1546,7 +1546,31 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
   [context setObject: deviceId  forKey: @"DeviceId"];
   [context setObject: [[theRequest uri] deviceType]  forKey: @"DeviceType"];
 
-  d = [[theRequest content] wbxml2xml];
+   // htc desire z send mail with content-type message/rfc822 base64 encoded
+   if ([[theRequest headerForKey: @"content-type"] isEqualToString: @"message/rfc822"]) {
+        NSMutableString *myRFC822 = [NSMutableString string];
+        NSMutableString *myRFC822_x = [NSMutableString string];
+
+        NSLog(@"tfu  content %@ ",  [theRequest  contentAsString ]);
+        // tfu htc doesn't include date-header causing sogo to show null for the sent-date in sent-folder. Adding Date-Header here if not there
+        if ([ [theRequest  contentAsString ] rangeOfString:@"Date:" options:NSCaseInsensitiveSearch].location == NSNotFound) {
+              NSLog(@"tfu  no-date-header");
+              [ myRFC822_x  appendFormat: @"Date: %@\n%@", [[ NSDate date ]  descriptionWithCalendarFormat: @"%a, %d %b %Y %H:%M:%S %z"  timeZone: [NSTimeZone timeZoneWithName: @"GMT"]  locale: nil ], [theRequest  contentAsString ]];
+              [ myRFC822  appendFormat: @"<?xml version=\"1.0\"?><!DOCTYPE ActiveSync PUBLIC \"-//MICROSOFT//DTD ActiveSync//EN\" \"http://www.microsoft.com/\"><SendMail xmlns=\"ComposeMail:\"><SaveInSentItems/><MIME>%@</MIME></SendMail>", [myRFC822_x stringByEncodingBase64]];
+        } else {
+              NSLog(@"tfu  date-header");
+              [ myRFC822  appendFormat: @"<?xml version=\"1.0\"?><!DOCTYPE ActiveSync PUBLIC \"-//MICROSOFT//DTD ActiveSync//EN\" \"http://www.microsoft.com/\"><SendMail xmlns=\"ComposeMail:\"><SaveInSentItems/><MIME>%@</MIME></SendMail>", [[theRequest  contentAsString ] stringByEncodingBase64]];
+        }
+
+        // tfu convert to wbxml and back to xml to ensure proper formating for further processing
+        d = [[[myRFC822 dataUsingEncoding: NSUTF8StringEncoding] xml2wbxml] wbxml2xml];
+
+   }
+        else
+   {
+       d = [[theRequest content] wbxml2xml];
+   }
+
   documentElement = nil;
 
   if (!d)
-- 
1.7.9.5

