David Graf has proposed merging lp:~davidagraf/zorba/bug-867248 into 
lp:zorba/email-module.

Requested reviews:
  David Graf (davidagraf)

For more details, see:
https://code.launchpad.net/~davidagraf/zorba/bug-867248/+merge/113059

Fix for bug #867248.
-- 
https://code.launchpad.net/~davidagraf/zorba/bug-867248/+merge/113059
Your team Zorba Coders is subscribed to branch lp:zorba/email-module.
=== modified file 'src/com/zorba-xquery/www/modules/email/smtp.xq.src/mime_handler.cpp'
--- src/com/zorba-xquery/www/modules/email/smtp.xq.src/mime_handler.cpp	2011-09-30 14:44:37 +0000
+++ src/com/zorba-xquery/www/modules/email/smtp.xq.src/mime_handler.cpp	2012-07-02 14:54:22 +0000
@@ -18,12 +18,13 @@
 #include <cstdio>
 #include <sstream>
 
-#include <zorba/zorba_string.h>
+#include <zorba/base64.h>
 #include <zorba/iterator.h>
 #include <zorba/item_factory.h>
 #include <zorba/store_consts.h>
 #include <zorba/user_exception.h>
 #include <zorba/xquery_functions.h>
+#include <zorba/zorba_string.h>
 
 #include "mime_handler.h"
 
@@ -59,6 +60,41 @@
   lChildrenIt->close();
 }
 
+/**
+ * Assigns a Zorba String to CClient value and does base64 encoding
+ * if necessary.
+ */
+void encodeStringForCClient(const zorba::String& aString, char*& aCClientVal)
+{
+  // check if string contains non ascii chars
+  bool lContainsNonAscii = false;
+  for (
+    zorba::String::const_iterator lIter = aString.begin();
+    lIter != aString.end();
+    ++lIter)
+  {
+    unsigned int u = static_cast<unsigned int>(*lIter);
+    if (!(u == '\t' || u == '\n' || u == '\t' || (u >= 32 && u <= 127)))
+    {
+      lContainsNonAscii = true;
+      break;
+    }
+  }
+
+  if (lContainsNonAscii)
+  {
+    zorba::String lEncodedValue = zorba::encoding::Base64::encode(aString);
+    zorba::String lFullValue = zorba::String("=?UTF-8?B?") 
+                             + lEncodedValue 
+                             + zorba::String("?=");
+    aCClientVal = cpystr(const_cast<char*>(lFullValue.c_str()));
+  }
+  else 
+  {
+    aCClientVal = cpystr(const_cast<char*>(aString.c_str()));
+  }
+}
+
 // helper function for retrieving the name and email address from an item
 static void
 getNameAndEmailAddress(
@@ -70,7 +106,7 @@
   Iterator_t lChildren = aEmailItem.getChildren();
   lChildren->open();
   Item lChild;
-  bool lReadName = true;
+  aName = "";
   while (lChildren->next(lChild)) {
     if (lChild.getNodeKind() != store::StoreConsts::elementNode) {
       continue;
@@ -81,9 +117,7 @@
     getNodeName(lChild, lNodeName);
     if (lNodeName == "name") {
       aName = lChild.getStringValue();
-      lReadName = false;
     } else {
-      aName = "";
       String lEmail = lChild.getStringValue();
       int lIndexOfAt = lEmail.find('@'); 
       aMailbox = lEmail.substr(0, lIndexOfAt).c_str();
@@ -96,9 +130,9 @@
 create_mail_address(String& aName, String& aMailbox, String& aHost)
 {
   mail_address* address = mail_newaddr();
-  address->personal = cpystr(const_cast<char*>(aName.c_str()));
-  address->mailbox = cpystr(const_cast<char*>(aMailbox.c_str()));
-  address->host = cpystr(const_cast<char*>(aHost.c_str()));
+  encodeStringForCClient(aName, address->personal);
+  encodeStringForCClient(aMailbox, address->mailbox);
+  encodeStringForCClient(aHost, address->host);
   return address;
 }
 
@@ -188,7 +222,7 @@
       getNameAndEmailAddress(lChild, lName, lMailbox, lHost);
       theEnvelope->reply_to = create_mail_address(lName, lMailbox, lHost);
     } else if (lNodeName == "subject") {
-      theEnvelope->subject = cpystr(const_cast<char*>(lNodeValue.c_str()));
+      encodeStringForCClient(lNodeValue, theEnvelope->subject);
     } else if (lNodeName == "recipient") {
       Iterator_t lRecipentChildren = lChild.getChildren(); 
       lRecipentChildren->open();
@@ -383,7 +417,7 @@
       aBody->parameter = create_param("charset", fn::upper_case(lNodeValue), NIL);
     } else if (lNodeName == "contentTransferEncoding") {
       set_encoding(aBody, lNodeValue);  
-    } else if (lNodeName ,"contentDisposition") {
+    } else if (lNodeName == "contentDisposition") {
       aBody->disposition.type = cpystr(fn::upper_case(lNodeValue).c_str()); 
     } else if (lNodeName == "contentDisposition-filename") {
       if (!aBody->disposition.parameter) {  

-- 
Mailing list: https://launchpad.net/~zorba-coders
Post to     : zorba-coders@lists.launchpad.net
Unsubscribe : https://launchpad.net/~zorba-coders
More help   : https://help.launchpad.net/ListHelp

Reply via email to