2 * Copyright (c) 2014 Samsung Electronics Co., Ltd. All rights reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
22 #include <unicode/ucnv.h>
23 #include <unicode/ustring.h>
29 #include "MsgMmsTypes.h"
30 #include "MsgContact.h"
32 #include "MsgUtilFile.h"
33 #include "MsgCppTypes.h"
34 #include "MsgVMessage.h"
35 #include "MsgMmsMessage.h"
36 #include "MsgSerialize.h"
38 #define VMSG_INIT_LENGTH 1024
39 #define VMSG_ITEM_LENGTH 1024
40 #define MSGSVC_VMSG_FOLDING_LIMIT 75
42 #define SMART_STRDUP(src) (src && *src)?strdup(src):NULL
43 #define SAFE_STR(src) (src)?src:""
45 #define MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, str) do { \
46 if ((len = __msgsvc_vmsg_append_str(buf, buf_size, len, str, false)) < 0) { \
47 MSG_ERR("__msgsvc_vmsg_append_str() Failed"); \
48 return MSG_ERR_MEMORY_ERROR; \
52 #define MSGSVC_VMSG_APPEND_STR_FREE(buf, buf_size, len, str) do { \
53 if ((len = __msgsvc_vmsg_append_str(buf, buf_size, len, str, false)) < 0) { \
54 MSG_ERR("__msgsvc_vmsg_append_str() Failed"); \
57 return MSG_ERR_MEMORY_ERROR; \
61 #define MSGSVC_VMSG_APPEND_CONTENT_STR(buf, buf_size, len, content) do { \
62 if ((len = __msgsvc_vmsg_append_str(buf, buf_size, len, content, true)) < 0) { \
63 MSG_ERR("__msgsvc_vmsg_append_str() Failed"); \
64 return MSG_ERR_MEMORY_ERROR; \
69 #define MSGSVC_VMSG_APPEND_CONTENT(buf, buf_size, len, content) do { \
70 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, ";CHARSET=UTF-8"); \
71 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, ":"); \
72 MSGSVC_VMSG_APPEND_CONTENT_STR(buf, buf_size, len, content); \
73 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF); \
112 FNAME_UTF8_VCARD = 17,
113 NAME_UTF8_VCARD = 18,
116 VMSG_ATTR_ENCODING_QP = 19,
117 VMSG_ATTR_ENCODING_BASE64_V21 = 20,
118 VMSG_ATTR_ENCODING_BASE64_V30 = 21,
120 /* Message type indication */
121 VMSG_INDICATION_MESSAGE_TYPE = 22,
122 VMSG_INDICATION_MESSAGE_TYPE_INET = 23,
123 VMSG_INDICATION_MESSAGE_TYPE_MSG = 24,
125 /* Read status indication */
126 VMSG_INDICATION_READ_STATUS = 25,
127 VMSG_INDICATION_READ_STATUS_READ = 26,
128 VMSG_INDICATION_READ_STATUS_UNREAD = 27,
130 /* Mailbox status indication */
131 VMSG_INDICATION_MSG_BOX = 28,
132 VMSG_INDICATION_MSG_BOX_SENT = 29,
133 VMSG_INDICATION_MSG_BOX_INBOX = 30,
134 VMSG_INDICATION_MSG_BOX_DRAFT = 31,
137 VMSG_ATTR_CHARSET_PROPERTY = 32,
140 VMSG_ATTR_LANGUAGE_PROPERTY = 33,
141 VMSG_DATA_SEPARATOR = 34,
143 VMSG_BODY_PROPERTY_DATE = 35,
144 VMSG_BODY_PROPERTY_SUBJECT = 36,
145 VMSG_BODY_PROPERTY_FROM = 37,
146 VMSG_BODY_PROPERTY_TO = 38,
155 VMSG_MAXIMUM_VALUE = 44,
158 static const char *content_name[VMSG_MAXIMUM_VALUE+1] = {0};
159 const char *MSGSVC_CRLF = "\r\n";
160 bool needCharset = false;
162 static void __msgsvc_vmsg_initial(void)
164 if (NULL == *content_name) {
166 content_name[BEGIN_VMSG] = "BEGIN:VMSG";
167 content_name[VERSION_VMSG] = "VERSION:1.1";
168 content_name[BEGIN_VCARD] = "BEGIN:VCARD";
169 content_name[VERSION_VCARD] = "VERSION:2.1";
170 content_name[END_VCARD] = "END:VCARD";
171 content_name[BEGIN_VENV] = "BEGIN:VENV";
172 content_name[BEGIN_VBODY] = "BEGIN:VBODY";
173 content_name[END_VBODY] = "END:VBODY";
174 content_name[END_VENV] = "END:VENV";
175 content_name[END_VMSG] = "END:VMSG";
177 content_name[FNAME_VCARD] = "FN:";
178 content_name[NAME_VCARD] = "N:";
179 content_name[TEL_VCARD] = "TEL;";
180 content_name[TEL_VCARD_CELL] = "CELL:";
182 content_name[DATE_VMSG] = "Date:";
183 content_name[SUBJECT_VMSG] = "Subject:";
185 content_name[FNAME_UTF8_VCARD] = "FN;CHARSET=UTF-8:";
186 content_name[NAME_UTF8_VCARD] = "N;CHARSET=UTF-8:";
189 content_name[VMSG_ATTR_ENCODING_QP] = "ENCODING=QUOTED-PRINTABLE";
190 content_name[VMSG_ATTR_ENCODING_BASE64_V21] = "ENCODING=BASE64";
191 content_name[VMSG_ATTR_ENCODING_BASE64_V30] = "ENCODING=b";
193 /* Message type indication */
194 content_name[VMSG_INDICATION_MESSAGE_TYPE] = "X-IRMC-TYPE";
195 content_name[VMSG_INDICATION_MESSAGE_TYPE_INET] = "INET";
196 content_name[VMSG_INDICATION_MESSAGE_TYPE_MSG] = "MSG";
198 /* Read status indication */
199 content_name[VMSG_INDICATION_READ_STATUS] = "X-IRMC-STATUS";
200 content_name[VMSG_INDICATION_READ_STATUS_READ] = "READ";
201 content_name[VMSG_INDICATION_READ_STATUS_UNREAD] = "UNREAD";
203 /* Mailbox status indication */
204 content_name[VMSG_INDICATION_MSG_BOX] = "X-IRMC-BOX";
205 content_name[VMSG_INDICATION_MSG_BOX_SENT] = "SENT";
206 content_name[VMSG_INDICATION_MSG_BOX_INBOX] = "INBOX";
207 content_name[VMSG_INDICATION_MSG_BOX_DRAFT] = "DRAFT";
210 content_name[VMSG_ATTR_CHARSET_PROPERTY] = "CHARSET";
213 content_name[VMSG_ATTR_LANGUAGE_PROPERTY] = "LANGUAGE";
214 content_name[VMSG_DATA_SEPARATOR] = ":";
216 content_name[VMSG_BODY_PROPERTY_DATE] = "Date";
217 content_name[VMSG_BODY_PROPERTY_SUBJECT] = "Subject";
218 content_name[VMSG_BODY_PROPERTY_FROM] = "From";
219 content_name[VMSG_BODY_PROPERTY_TO] = "To";
221 content_name[KEY_BODY] = "body";
222 content_name[KEY_DATE] = "date";
224 content_name[VMSG_VCARD_TEL] = "TEL";
225 content_name[VMSG_MSG_BEGIN] = "BEGIN";
226 content_name[VMSG_MSG_END] = "END";
227 content_name[VMSG_MAXIMUM_VALUE] = "MAX";
231 static int __msgsvc_vmsg_append_str(char **buf, int *buf_size, int len, const char *str, bool need_conversion)
235 const char *safe_str = SAFE_STR(str);
237 bool need_realloc = false;
239 str_len = strlen(safe_str);
240 while ((*buf_size-len) < (str_len+1)) {
241 *buf_size = *buf_size * 2;
246 if (NULL == (tmp = (char *)realloc(*buf, *buf_size)))
252 if (need_conversion) {
253 const char *s = safe_str;
254 char *r = (char *)(*buf+len);
257 switch ((unsigned int)*s) {
259 if (*(s+1) && '\n' == *(s+1)) {
273 if (*buf_size < str_len+len+1) {
274 *buf_size = *buf_size * 2;
275 if (NULL == (tmp = (char *)realloc(*buf, *buf_size)))
278 int pos_temp = r-(*buf+len);
280 r = (char *)(*buf+len+pos_temp);
294 if (*buf_size < str_len+len+1) {
295 *buf_size = *buf_size * 2;
296 if (NULL == (tmp = (char *)realloc(*buf, *buf_size)))
299 int pos_temp = r-(*buf+len);
301 r = (char *)(*buf+len+pos_temp);
307 if (*(s+1) && 0xAC == *(s+1)) { /* en/em backslash */
311 if (*buf_size < str_len+len+1) {
312 *buf_size = *buf_size * 2;
313 if (NULL == (tmp = (char *)realloc(*buf, *buf_size)))
316 int pos_temp = r-(*buf+len);
318 r = (char *)(*buf+len+pos_temp);
325 if (*buf_size < str_len+len+1) {
326 *buf_size = *buf_size * 2;
327 if (NULL == (tmp = (char *)realloc(*buf, *buf_size)))
330 int pos_temp = r-(*buf+len);
332 r = (char *)(*buf+len+pos_temp);
342 if (*(s+1) && 0x5F == *(s+1)) { /* en/em backslash */
346 if (*buf_size < str_len+len+1) {
347 *buf_size = *buf_size * 2;
348 if (NULL == (tmp = (char *)realloc(*buf, *buf_size)))
351 int pos_temp = r-(*buf+len);
353 r = (char *)(*buf+len+pos_temp);
360 if (*buf_size < str_len+len+1) {
361 *buf_size = *buf_size * 2;
362 if (NULL == (tmp = (char *)realloc(*buf, *buf_size)))
365 int pos_temp = r-(*buf+len);
367 r = (char *)(*buf+len+pos_temp);
386 len_temp = snprintf(*buf+len, *buf_size-len+1, "%s", safe_str);
393 /*==================================================================================================
395 ==================================================================================================*/
396 #define INSERT_VMSG_OBJ pObject = (VObject*)calloc(1, sizeof(VObject)); \
399 vmsg_free_vtree_memory(pMessage);\
402 if (pMessage->pTop == NULL)\
404 pMessage->pTop = pObject;\
408 pMessage->pCur->pSibling = pObject;\
410 pMessage->pCur = pObject;
413 #define INSERT_VBODY_OBJ pObject = (VObject*)calloc(1, sizeof(VObject)); \
416 vmsg_free_vtree_memory(pMessage);\
419 if (pBody->pTop == NULL)\
421 pBody->pTop = pObject;\
425 pBody->pCur->pSibling = pObject;\
427 pBody->pCur = pObject;
430 #define INSERT_VCARD_OBJ pObject = (VObject*)calloc(1, sizeof(VObject)); \
433 vmsg_free_vtree_memory(pMessage);\
436 if (pCard->pTop == NULL)\
438 pCard->pTop = pObject;\
442 pCard->pCur->pSibling = pObject;\
444 pCard->pCur = pObject;
447 #define INSERT_PARAM param = (VParam*)calloc(1, sizeof(VParam));\
450 vmsg_free_vtree_memory(pMessage);\
451 if (pObject != NULL)\
460 /*==================================================================================================
461 FUNCTION IMPLEMENTATION
462 ==================================================================================================*/
464 char* _convert_tm_to_vdata_str(const struct tm * tm)
466 char str[17] = {0, };
468 snprintf(str, 17, "%04d%02d%02dT%02d%02d%02dZ",
479 bool _convert_vdata_str_to_tm(const char* szText, struct tm * tm)
481 if (szText == NULL) return false;
482 if (strlen(szText) < 15) return false;
483 if (szText[8] != 'T') return false;
486 memset(tm, 0, sizeof(struct tm));
488 /* year, month, day */
489 memcpy(szBuff, &(szText[0]), 4);
491 tm->tm_year = atol(szBuff) - 1900;
492 if ((tm->tm_year > 137) || (tm->tm_year < 0))
495 memcpy(szBuff, &(szText[4]), 2);
497 tm->tm_mon = atol(szBuff)-1;
498 if ((tm->tm_mon > 11) || (tm->tm_mon < 0))
501 memcpy(szBuff, &(szText[6]), 2);
503 tm->tm_mday = atol(szBuff);
504 if ((tm->tm_mday > 31) || (tm->tm_mday < 1))
507 /* hour, minute, second */
508 memcpy(szBuff, &(szText[9]), 2);
510 tm->tm_hour = atol(szBuff);
511 if ((tm->tm_hour > 23) || (tm->tm_hour < 0))
514 memcpy(szBuff, &(szText[11]), 2);
516 tm->tm_min = atol(szBuff);
517 if ((tm->tm_min > 59) || (tm->tm_min < 0))
520 memcpy(szBuff, &(szText[13]), 2);
522 tm->tm_sec = atol(szBuff);
523 if ((tm->tm_sec > 59) || (tm->tm_sec < 0))
529 int msgsvc_check_utf8(char c)
531 if ((c & 0xff) < (128 & 0xff))
533 else if ((c & (char)0xe0) == (char)0xc0)
535 else if ((c & (char)0xf0) == (char)0xe0)
537 else if ((c & (char)0xf8) == (char)0xf0)
539 else if ((c & (char)0xfc) == (char)0xf8)
541 else if ((c & (char)0xfe) == (char)0xfc)
544 return MSG_ERR_INVALID_PARAMETER;
547 char* __msgsvc_vmsg_convert_tm_to_vdata_str(struct tm * tm)
549 char str[22] = {0, };
551 char month[15] = {0, };
554 mon = tm->tm_mon + 1;
556 if (tm->tm_hour >= 12)
557 strncpy(APM, "PM", 2);
559 strncpy(APM, "AM", 2);
561 if (tm->tm_hour > 12)
562 hour = tm->tm_hour - 12;
568 strncpy(month, "Jan", 3);
571 strncpy(month, "Feb", 3);
574 strncpy(month, "Mar", 3);
577 strncpy(month, "Apr", 3);
580 strncpy(month, "May", 3);
583 strncpy(month, "Jun", 3);
586 strncpy(month, "Jul", 3);
589 strncpy(month, "Aug", 3);
592 strncpy(month, "Sep", 3);
595 strncpy(month, "Oct", 3);
598 strncpy(month, "Nov", 3);
601 strncpy(month, "Dec", 3);
604 MSG_DEBUG("invalid month number");
608 snprintf(str, 22, "%d:%02d%s, %04d %s %d",
618 static inline int __msgsvc_vmsg_add_folding(char **buf, int *buf_size, int buf_len)
621 char *buf_copy = NULL;
625 bool content_start = false;
626 bool encode_64 = false;
628 buf_copy = (char *)calloc(1, *buf_size);
632 len = result_len = 0;
635 if (*buf_size < result_len + 5) {
637 *buf_size = *buf_size + 1000;
638 if (NULL == (tmp = (char *)realloc(buf_copy, *buf_size))) {
644 r = (buf_copy + result_len);
648 if (false == content_start) {
650 content_start = true;
651 else if (0 == strncmp(s, "ENCODING=BASE64", strlen("ENCODING=BASE64")))
657 else if ('\n' == *s) {
660 content_start = false;
665 if (false == encode_64)
666 char_len = msgsvc_check_utf8(*s);
668 if (MSGSVC_VMSG_FOLDING_LIMIT <= len + char_len) {
695 static inline int __msgsvc_vmsg_append_start_vmsg_1_1(char **buf, int *buf_size, int len)
697 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[BEGIN_VMSG]);
698 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
699 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VERSION_VMSG]);
700 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
704 static inline int __msgsvc_vmsg_append_end_vmsg(char **buf, int *buf_size, int len)
706 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[END_VMSG]);
707 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
711 static inline int __msgsvc_vmsg_append_read_status(MSG_MESSAGE_INFO_S *pMsg, char **buf, int *buf_size, int len)
713 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_INDICATION_READ_STATUS]);
714 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_DATA_SEPARATOR]);
716 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_INDICATION_READ_STATUS_READ]);
717 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
719 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_INDICATION_READ_STATUS_UNREAD]);
720 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
724 static inline int __msgsvc_vmsg_append_box_type(MSG_MESSAGE_INFO_S *pMsg, char **buf, int *buf_size, int len)
726 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_INDICATION_MSG_BOX]);
727 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_DATA_SEPARATOR]);
728 switch(pMsg->folderId) {
730 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_INDICATION_MSG_BOX_INBOX]);
734 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_INDICATION_MSG_BOX_SENT]);
738 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_INDICATION_MSG_BOX_DRAFT]);
741 /* Discard User Defined, outbox or Spam folder's messages. */
742 MSG_DEBUG("Invalid or unhandled msg box");
744 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
748 static inline int __msgsvc_vmsg_append_msg_type(MSG_MESSAGE_INFO_S *pMsg, char **buf, int *buf_size, int len)
750 /* TO DO check with msg text contains Only PrintableAscii if true then else handle INET_TYPE */
751 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_INDICATION_MESSAGE_TYPE]);
752 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_DATA_SEPARATOR]);
753 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_INDICATION_MESSAGE_TYPE_MSG]);
754 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
758 static inline int __msgsvc_vmsg_append_origin_address_vcard(MSG_MESSAGE_INFO_S *pMsg, char **buf, int *buf_size, int len)
760 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[BEGIN_VCARD]);
761 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
762 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VERSION_VCARD]);
763 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
764 char originAddress[MAX_ADDRESS_VAL_LEN + 1] = {0, };
765 bool isDisplayName = false;
767 signed char folderId = (signed char)pMsg->folderId;
768 if (folderId == (signed char)MSG_INBOX_ID) {
769 snprintf(originAddress, sizeof(originAddress), "%s", pMsg->addressList[0].addressVal);
772 needCharset = true; /* as per android */
774 if (strlen(originAddress) > 0) {
775 MSG_CONTACT_INFO_S contactInfo;
776 memset(&contactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
778 if (MsgGetContactInfo(&(pMsg->addressList[0]), &contactInfo) != MSG_SUCCESS) {
779 MSG_WARN("MsgGetContactInfo() fail.");
781 snprintf(pMsg->addressList[0].displayName, sizeof(pMsg->addressList[0].displayName), "%s", contactInfo.firstName);
782 if (pMsg->addressList[0].displayName[0] != '\0')
783 isDisplayName = true;
784 if (needCharset && isDisplayName) {
785 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[FNAME_UTF8_VCARD]);
786 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, pMsg->addressList->displayName);
787 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
788 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[NAME_UTF8_VCARD]);
789 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, pMsg->addressList->displayName);
790 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
792 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[FNAME_VCARD]);
793 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, pMsg->addressList->displayName);
794 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
795 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[NAME_VCARD]);
796 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, pMsg->addressList->displayName);
797 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
799 if (MsgIsNumber(originAddress)) {
800 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[TEL_VCARD]);
801 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[TEL_VCARD_CELL]);
802 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, originAddress);
803 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
806 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[FNAME_VCARD]);
807 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
808 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[NAME_VCARD]);
809 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
813 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[END_VCARD]);
814 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
818 static inline int __msgsvc_vmsg_append_recipient_address_vcard(MSG_MESSAGE_INFO_S *pMsg, char **buf, int *buf_size, int len)
820 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[BEGIN_VCARD]);
821 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
822 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VERSION_VCARD]);
823 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
825 needCharset = true; /* as per android */
826 signed char folderId;
828 for (int i = 0; i < pMsg->nAddressCnt; ++i) {
829 char originAddress[MAX_ADDRESS_VAL_LEN + 1] = {0, };
830 bool isDisplayName = false;
832 folderId = (signed char)pMsg->folderId;
833 if (folderId == MSG_SENTBOX_ID) {
834 snprintf(originAddress, sizeof(originAddress), "%s", pMsg->addressList[0].addressVal);
837 if (strlen(originAddress) > 0) {
838 MSG_CONTACT_INFO_S contactInfo;
839 memset(&contactInfo, 0x00, sizeof(MSG_CONTACT_INFO_S));
841 if (MsgGetContactInfo(&(pMsg->addressList[i]), &contactInfo) != MSG_SUCCESS) {
842 MSG_WARN("MsgGetContactInfo() fail.");
844 snprintf(pMsg->addressList[i].displayName, sizeof(pMsg->addressList[i].displayName), "%s", contactInfo.firstName);
845 if (pMsg->addressList[i].displayName[0] != '\0')
846 isDisplayName = true;
847 if (needCharset && isDisplayName) {
848 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[FNAME_UTF8_VCARD]);
849 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, pMsg->addressList[i].displayName);
850 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
851 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[NAME_UTF8_VCARD]);
852 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, pMsg->addressList[i].displayName);
853 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
855 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[FNAME_VCARD]);
856 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, pMsg->addressList[i].displayName);
857 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
858 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[NAME_VCARD]);
859 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, pMsg->addressList[i].displayName);
860 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
862 if (MsgIsNumber(originAddress)) {
863 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[TEL_VCARD]);
864 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[TEL_VCARD_CELL]);
865 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, originAddress);
866 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
869 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[FNAME_VCARD]);
870 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
871 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[NAME_VCARD]);
872 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
876 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[END_VCARD]);
877 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
882 static inline int __msgsvc_vmsg_append_msg_body(MSG_MESSAGE_INFO_S *pMsg, char **buf, int *buf_size, int len)
884 struct tm display_time;
886 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[BEGIN_VENV]);
887 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
889 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[BEGIN_VBODY]);
890 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
893 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_BODY_PROPERTY_DATE]);
894 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_DATA_SEPARATOR]);
896 localtime_r(&(pMsg->displayTime), &display_time);
897 char *msgDate = __msgsvc_vmsg_convert_tm_to_vdata_str(&display_time);
898 if (msgDate !=NULL) {
899 MSGSVC_VMSG_APPEND_STR_FREE(buf, buf_size, len, msgDate);
901 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
905 if (pMsg->subject[0] != '\0') {
906 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_BODY_PROPERTY_SUBJECT]);
907 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[VMSG_DATA_SEPARATOR]);
908 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, pMsg->subject);
909 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
913 if(pMsg->msgType.mainType == MSG_SMS_TYPE) {
914 if(pMsg->msgType.subType == MSG_NORMAL_SMS) {
915 if (pMsg->bTextSms == false) {
916 char* pFileData = NULL;
917 unique_ptr<char*, void(*)(char**)> buff(&pFileData, unique_ptr_deleter);
920 char* msgText = NULL;
922 if (MsgOpenAndReadFile(pMsg->msgData, &pFileData, &fileSize) == false)
925 msgText = (char *)calloc(1, fileSize);
926 memcpy(msgText, pFileData, fileSize);
927 MSGSVC_VMSG_APPEND_STR_FREE(buf, buf_size, len, msgText);
930 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, pMsg->msgText);
935 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
937 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[END_VBODY]);
938 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
940 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[END_VENV]);
941 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
945 static inline int __msgsvc_vmsg_append_msg_envelope(MSG_MESSAGE_INFO_S *pMsg, char **buf, int *buf_size, int len)
947 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[BEGIN_VENV]);
948 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
950 len = __msgsvc_vmsg_append_recipient_address_vcard(pMsg, buf, buf_size, len);
951 MSG_ERR_RET_VM(len < 0, len, "Invalid length : vcard");
953 len = __msgsvc_vmsg_append_msg_body(pMsg, buf, buf_size, len);
954 MSG_ERR_RET_VM(len < 0, len, "Invalid length : body");
956 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, content_name[END_VENV]);
957 MSGSVC_VMSG_APPEND_STR(buf, buf_size, len, MSGSVC_CRLF);
961 static inline int __msgsvc_vmsg_append_msg(MSG_MESSAGE_INFO_S *pMsg, char **buf, int *buf_size, int len)
963 len = __msgsvc_vmsg_append_read_status(pMsg, buf, buf_size, len);
964 MSG_ERR_RET_VM(len < 0, len, "Invalid length : read status");
966 len = __msgsvc_vmsg_append_box_type(pMsg, buf, buf_size, len);
967 MSG_ERR_RET_VM(len < 0, len, "Invalid length : box type");
969 len = __msgsvc_vmsg_append_msg_type(pMsg, buf, buf_size, len);
970 MSG_ERR_RET_VM(len < 0, len, "Invalid length : msg type");
972 len = __msgsvc_vmsg_append_origin_address_vcard(pMsg, buf, buf_size, len);
973 MSG_ERR_RET_VM(len < 0, len, "Invalid length : origin address");
975 len = __msgsvc_vmsg_append_msg_envelope(pMsg, buf, buf_size, len);
976 MSG_ERR_RET_VM(len < 0, len, "Invalid length : envelop");
981 char *MsgVMessageEncode(MSG_MESSAGE_INFO_S *pMsg)
985 VObject* pObject = NULL;
986 VParam* param = NULL;
987 VTree * pBody = NULL;
988 VTree * pCard = NULL;
989 VTree * pCurrent = NULL;
990 struct tm display_time;
991 char* encoded_data = NULL;
993 VTree* pMessage = NULL;
996 if (pMsg->msgType.mainType == MSG_SMS_TYPE && pMsg->msgType.subType == MSG_NORMAL_SMS) {
997 return MsgVMessageEncodeSMS(pMsg);
1001 if (pMessage == NULL) {
1002 pMessage = (VTree *)malloc(sizeof(VTree));
1006 pMessage->treeType = VMESSAGE;
1007 pMessage->pTop = NULL;
1008 pMessage->pCur = NULL;
1009 pMessage->pNext = NULL;
1011 pCurrent = pMessage;
1013 /* Insert VObject (X-MESSAGE-TYPE) to VMessage tree */
1016 pObject->property = VMSG_TYPE_MSGTYPE;
1018 if (pMsg->msgType.mainType == MSG_MMS_TYPE && (pMsg->msgType.subType == MSG_SENDREQ_MMS || pMsg->msgType.subType == MSG_SENDCONF_MMS))
1019 pObject->pszValue[0] = strdup("MMS SEND");
1021 else if (pMsg->msgType.mainType == MSG_MMS_TYPE && pMsg->msgType.subType == MSG_NOTIFICATIONIND_MMS)
1022 pObject->pszValue[0] = strdup("MMS NOTIFICATION");
1025 else if (pMsg->msgType.mainType == MSG_MMS_TYPE && (pMsg->msgType.subType == MSG_RETRIEVE_AUTOCONF_MMS || pMsg->msgType.subType == MSG_RETRIEVE_MANUALCONF_MMS))
1026 pObject->pszValue[0] = strdup("MMS RETRIEVED");
1029 goto __CATCH_FAIL__;
1031 pObject->valueCount = 1;
1034 /* Insert VObject (X-IRMC-BOX) to VMessate tree */
1037 pObject->property = VMSG_TYPE_MSGBOX;
1039 switch(pMsg->folderId) {
1041 pObject->pszValue[0] = strdup("INBOX");
1044 pObject->pszValue[0] = strdup("OUTBOX");
1046 case MSG_SENTBOX_ID:
1047 pObject->pszValue[0] = strdup("SENTBOX");
1050 pObject->pszValue[0] = strdup("DRAFTBOX");
1053 /* Discard User Defined or Spam folder's messages. */
1054 goto __CATCH_FAIL__;
1056 pObject->valueCount = 1;
1059 /* Insert VObject (X-SS-DT) to VMessage tree */
1062 pObject->property = VMSG_TYPE_DATE;
1064 localtime_r(&(pMsg->displayTime), &display_time);
1065 pObject->pszValue[0] = _convert_tm_to_vdata_str(&display_time);
1066 pObject->valueCount = 1;
1069 /* Insert Vobject read status to VMessage tree */
1072 pObject->property = VMSG_TYPE_STATUS;
1075 pObject->pszValue[0] = strdup("READ");
1077 pObject->pszValue[0] = strdup("UNREAD");
1079 pObject->valueCount = 1;
1082 /* Insert VBody tree for message body; */
1083 pBody = (VTree*)calloc(1, sizeof(VTree));
1085 goto __CATCH_FAIL__;
1086 pBody->treeType = VBODY;
1089 pBody->pNext = NULL;
1090 pCurrent->pNext = pBody;
1093 if (strlen(pMsg->subject) > 0) {
1094 /* Insert Subject object */
1096 pObject->property = VMSG_TYPE_SUBJECT;
1097 pObject->pszValue[0] = strdup(pMsg->subject);
1098 pObject->valueCount = 1;
1100 /* Insert VBody object */
1102 pObject->property = VMSG_TYPE_BODY;
1104 if (pMsg->msgType.mainType == MSG_MMS_TYPE) {
1105 /* Insert VBody for mms raw data; */
1106 char* pFileData = NULL;
1107 unique_ptr<char*, void(*)(char**)> buf(&pFileData, unique_ptr_deleter);
1108 MMS_DATA_S *pMmsData = NULL;
1110 char* msgText = NULL;
1112 char filePath[MSG_FILEPATH_LEN_MAX] = {0, };
1113 if(pMsg->msgType.subType == MSG_NOTIFICATIONIND_MMS)
1114 pFileData = MsgOpenAndReadMmsFile(pMsg->msgData, 0, -1, &fileSize);
1116 err = MsgStoGetMmsRawFilePath(pDbHandle, pMsg->msgId, filePath);
1118 if (err != MSG_SUCCESS)
1119 goto __CATCH_FAIL__;
1121 pFileData = MsgOpenAndReadMmsFile(filePath, 0, -1, &fileSize);
1125 if (pMsg->bTextSms == false) {
1126 if (MsgOpenAndReadFile(pMsg->msgData, &pFileData, &fileSize) == false) {
1127 goto __CATCH_FAIL__;
1130 fileSize = strlen(pMsg->msgData);
1131 pFileData = (char *)calloc(1, fileSize+1);
1133 goto __CATCH_FAIL__;
1134 snprintf(pFileData, fileSize, "%s", pMsg->msgData);
1137 if (MsgDeserializeMmsData(pFileData, fileSize, &pMmsData) != 0) {
1138 MSG_DEBUG("Fail to Deserialize Message Data");
1139 MsgMmsRelease(&pMmsData);
1140 goto __CATCH_FAIL__;
1143 int serializedDataSize = 0;
1146 MsgMmsSetMultipartListData(pMmsData); /* app file -> data */
1147 serializedDataSize = MsgSerializeMms(pMmsData, &pFileData);
1151 fileSize = serializedDataSize;
1154 MsgMmsRelease(&pMmsData);
1157 MSG_DEBUG("FILE SIZE IS %d, %s", fileSize, pFileData);
1159 msgText = (char *)calloc(1, fileSize);
1160 if(pFileData && msgText)
1161 memcpy(msgText, pFileData, fileSize);
1163 pObject->numOfBiData = fileSize;
1164 pObject->pszValue[0] = msgText;
1165 pObject->valueCount = 1;
1173 /* Insert parameter for base64 encoding */
1174 MSG_DEBUG("before to start INSERT_PARAM");
1176 pObject->pParam = param;
1177 param->parameter = VMSG_PARAM_ENCODING;
1178 param->paramValue = VMSG_ENC_PARAM_BASE64;
1180 /* Add VCard tree for recipient address information. */
1181 for (int i = 0; i < pMsg->nAddressCnt; ++i) {
1182 pCard = (VTree*)calloc(1, sizeof(VTree));
1184 goto __CATCH_FAIL__;
1186 pCard->treeType = VCARD;
1189 pCard->pNext = NULL;
1190 pCurrent->pNext = pCard;
1194 pObject->property = VCARD_TYPE_TEL;
1195 pObject->pszValue[0] = strdup(pMsg->addressList[i].addressVal);
1196 pObject->valueCount = 1;
1198 MSG_DEBUG("before to start vmsg_encode");
1199 encoded_data = vmsg_encode(pMessage);
1201 vmsg_free_vtree_memory(pMessage);
1203 return encoded_data;
1206 vmsg_free_vtree_memory(pMessage);
1211 char *MsgVMessageEncodeSMS(MSG_MESSAGE_INFO_S *pMsg)
1216 int buf_size = VMSG_INIT_LENGTH;
1219 __msgsvc_vmsg_initial();
1221 buf = (char *)calloc(1, buf_size);
1223 len = __msgsvc_vmsg_append_start_vmsg_1_1(&buf, &buf_size, len);
1229 len = __msgsvc_vmsg_append_msg(pMsg, &buf, &buf_size, len);
1235 len = __msgsvc_vmsg_append_end_vmsg(&buf, &buf_size, len);
1241 len = __msgsvc_vmsg_add_folding(&buf, &buf_size, len);
1250 bool __msgsvc_vmsg_convert_vdata_to_tm_str(const char* szText, struct tm * tm)
1255 char delims[] = ",";
1256 gchar **token = NULL;
1259 token = g_strsplit_set(szText, delims, -1);
1260 if (token && token[0]) {
1261 g_strstrip(token[0]);
1262 strptime(token[0], "%I:%M%p", tm);
1268 if (token && token[1]) {
1269 g_strstrip(token[1]);
1270 strptime(token[1], "%Y %b %e", &tmTemp);
1276 tm->tm_year = tmTemp.tm_year;
1277 tm->tm_mon = tmTemp.tm_mon;
1278 tm->tm_mday = tmTemp.tm_mday;
1286 static inline char* __msgsvc_vmsg_remove_empty_line(char *src)
1289 if ('\n' != *src && '\r' != *src)
1296 static char* __msgsvc_vmsg_check_word(char *src, const char *word)
1300 MSG_ERR_RET_VM(NULL == src, NULL, "The src is NULL.");
1302 src = __msgsvc_vmsg_remove_empty_line(src);
1318 while (*src == *word) {
1322 if ('\0' == *src || '\0' == *word)
1332 static inline int __msgsvc_vmsg_check_quoted(char *src, int max, int *quoted)
1335 if (TRUE == *quoted)
1338 while (*src && max) {
1340 ret = strncmp(src, "QUOTED-PRINTABLE", sizeof("QUOTED-PRINTABLE") - 1);
1345 } else if (':' == *src) {
1354 static inline int __msgsvc_vmsg_remove_folding(char *folded_src)
1356 char *result = folded_src;
1358 MSG_ERR_RET_VM(NULL == folded_src, MSG_ERR_INVALID_PARAMETER, " Invalid Parameter : __msgsvc_vmsg_remove_folding");
1360 while (*folded_src) {
1361 if ('\r' == *folded_src && '\n' == *(folded_src+1) && ' ' == *(folded_src+2))
1363 else if ('\n' == *folded_src && ' ' == *(folded_src+1))
1366 if ('\0' == *folded_src)
1369 *result = *folded_src;
1377 static inline int __msgsvc_vmsg_hex_to_dec(char hex)
1383 return hex - 'a' + 10;
1385 return hex - 'A' + 10;
1390 static inline int __msgsvc_vmsg_decode_quoted_val(char *val)
1395 src = strchr(val, ':');
1402 pre = __msgsvc_vmsg_hex_to_dec(*(src+1));
1404 *dest = (char)((pre << 4) + __msgsvc_vmsg_hex_to_dec(*(src+2)));
1408 if ('\r' == *(src+1) && '\n' == *(src+2))
1422 static inline char* __msgsvc_vmsg_translate_charset(char *src, int len)
1429 ret = strncmp(val, "CHARSET", sizeof("CHARSET") - 1);
1431 val += sizeof("CHARSET");
1435 else if (':' == *val) {
1444 UErrorCode err = U_ZERO_ERROR;
1448 char enc[32] = {0}, *dest;
1450 while (';' != *val && ':' != *val && ',' != *val) {
1454 if (0 == strcasecmp("UTF-8", enc))
1460 src_len = len - (val - src);
1462 temp_size = (src_len+1) * sizeof(UChar);
1463 temp = (UChar *)malloc(temp_size);
1464 conv = ucnv_open(enc, &err);
1465 MSG_WARN_M(U_FAILURE(err), "ucnv_open() Failed(%d), enc=%s", err, enc);
1466 ucnv_toUChars(conv, temp, temp_size, val, src_len, &err);
1467 MSG_WARN_M(U_FAILURE(err), "ucnv_toUChars() Failed(%d), enc=%s", err, enc);
1470 dest_size = temp_size*2;
1471 dest = (char *)malloc(dest_size);
1472 conv = ucnv_open("UTF-8", &err);
1473 MSG_WARN_M(U_FAILURE(err), "ucnv_open() Failed(%d), enc=%s", err, enc);
1474 ucnv_fromUChars(conv, dest, dest_size, temp, u_strlen(temp), &err);
1475 MSG_WARN_M(U_FAILURE(err), "ucnv_fromUChars() Failed(%d), enc=%s", err, enc);
1484 static void __msgsvc_vmsg_get_prefix(char **prefix, char *src)
1486 char *temp = strchr(src, ':');
1488 long len = (long)temp - (long)src;
1489 *prefix = (char *)calloc(len+1, sizeof(char));
1490 snprintf(*prefix, len+1, "%s", src);
1497 static void __msgsvc_vmsg_remove_spec_out(char **prefix)
1511 static char* __msgsvc_vmsg_get_val(int ver, char *src, char **prefix, char **dest)
1517 MSG_ERR_RET_VM(NULL == src, NULL, "Invalid parameter : The src is NULL.");
1518 MSG_ERR_RET_VM(NULL == dest, NULL, "sInvalid parameter : The dest is NULL.");
1537 if (MSGSVC_VMSG_VER_1_1 == ver) {
1539 if ('=' == *cursor && __msgsvc_vmsg_check_quoted(src, cursor - src, "ed)) {
1540 if ('\r' == *(cursor+1) && '\n' == *(cursor+2))
1543 if ('\r' == *cursor && '\n' == *(cursor+1) && ' ' != *(cursor+2))
1545 if ('\n' == *cursor && ' ' != *(cursor+1))
1554 if ('\r' == *cursor && '\n' == *(cursor+1) && ' ' != *(cursor+2))
1557 if ('\n' == *cursor && ' ' != *(cursor+1))
1564 if (src == cursor) {
1570 char temp = *cursor;
1574 __msgsvc_vmsg_get_prefix(prefix, src);
1576 __msgsvc_vmsg_remove_spec_out(prefix);
1579 *dest = strdup(src);
1580 if (MSGSVC_VMSG_VER_1_1 != ver)
1581 __msgsvc_vmsg_remove_folding(*dest);
1583 if (__msgsvc_vmsg_check_quoted(*dest, -1, "ed))
1584 len = __msgsvc_vmsg_decode_quoted_val(*dest);
1586 len = strlen(*dest);
1587 new_dest = __msgsvc_vmsg_translate_charset(*dest, len);
1593 return (cursor + 1);
1597 static int __msgsvc_vmsg_check_content_type(char **vcard)
1601 for (i = VMSG_VALUE_NONE+1; i < VMSG_MAXIMUM_VALUE; i++) {
1602 new_start = __msgsvc_vmsg_check_word(*vcard, content_name[i]);
1603 if (new_start && (':' == *new_start || ';' == *new_start))
1607 if (VMSG_MAXIMUM_VALUE == i)
1608 return VMSG_VALUE_NONE;
1615 static inline bool __msgsvc_vmsg_check_base64_encoded(char *src)
1622 ret = strncmp(tmp, "BASE64", sizeof("BASE64") - 1);
1625 } else if (':' == *tmp || '\r' == *tmp) {
1633 static char* __msgsvc_vmsg_decode_base64_val(char *val)
1636 guchar *decoded_str;
1640 src = strchr(val, ':');
1646 decoded_str = g_base64_decode(src, &size);
1648 dest = (char *)calloc((src-val)+size+1, sizeof(char));
1649 snprintf(dest, (src-val)+1, "%s", val);
1650 snprintf(dest+(src-val), size+1, "%s", decoded_str);
1651 g_free(decoded_str);
1656 static inline char* __msgsvc_vmsg_pass_unsupported(char *vmsg)
1667 static inline char* __msgsvc_vmsg_get_content_value(char *val)
1671 temp = strchr(val, ':');
1677 MSG_ERR_RET_VM('\0' == *(temp) || '\r' == *(temp) || '\n' == *(temp),
1678 NULL, "Invalid vcard content");
1683 static char* __msgsvc_vmsg_remove_escape_char(char *str)
1685 const char *s = SAFE_STR(str);
1686 char *r = (char *)s;
1688 if (*s == '\\' && *(s+1)) {
1689 char *n = (char*)(s+1);
1690 switch ((unsigned int)*n) {
1705 case 0xA1: /* en/em backslash */
1706 if (*(n+1) && 0xAC == *(n+1)) {
1713 case 0x81: /* en/em backslash */
1714 if (*(n+1) && 0x5F == *(n+1)) {
1737 static inline msg_error_t __msgsvc_vmsg_get_read_status(MSG_MESSAGE_INFO_S *pMsg, char *val)
1741 temp = __msgsvc_vmsg_get_content_value(val);
1742 MSG_ERR_RET_VM(NULL == temp, MSG_ERR_INVALID_PARAMETER, "Invalid parameter : read status");
1744 temp = __msgsvc_vmsg_remove_escape_char(temp);
1746 if (strcmp(temp, content_name[VMSG_INDICATION_READ_STATUS_READ]) == 0)
1748 else if (strcmp(temp, content_name[VMSG_INDICATION_READ_STATUS_UNREAD]) == 0)
1749 pMsg->bRead = false;
1751 return MSG_ERR_INVALID_PARAMETER;
1753 MSG_DEBUG("pMsg->bRead = %d", pMsg->bRead);
1757 static inline msg_error_t __msgsvc_vmsg_get_msg_box(MSG_MESSAGE_INFO_S *pMsg, char *val)
1761 temp = __msgsvc_vmsg_get_content_value(val);
1762 MSG_ERR_RET_VM(NULL == temp, MSG_ERR_INVALID_PARAMETER, "Invalid parameter : msg box");
1764 temp = __msgsvc_vmsg_remove_escape_char(temp);
1766 if (strcmp(temp, content_name[VMSG_INDICATION_MSG_BOX_DRAFT]) == 0) {
1767 pMsg->folderId = MSG_DRAFT_ID;
1768 pMsg->direction = MSG_DIRECTION_TYPE_MO;
1769 pMsg->networkStatus = MSG_NETWORK_NOT_SEND;
1771 else if (strcmp(temp, content_name[VMSG_INDICATION_MSG_BOX_INBOX]) == 0) {
1772 pMsg->folderId = MSG_INBOX_ID;
1773 pMsg->direction = MSG_DIRECTION_TYPE_MT;
1774 pMsg->networkStatus = MSG_NETWORK_RECEIVED;
1775 } else if (strcmp(temp, content_name[VMSG_INDICATION_MSG_BOX_SENT]) == 0) {
1776 pMsg->folderId = MSG_SENTBOX_ID;
1777 pMsg->direction = MSG_DIRECTION_TYPE_MO;
1778 pMsg->networkStatus = MSG_NETWORK_SEND_SUCCESS;
1780 return MSG_ERR_INVALID_PARAMETER;
1782 MSG_DEBUG("pMsg->folderId = %d", pMsg->folderId);
1786 static inline msg_error_t __msgsvc_vmsg_get_msg_type(MSG_MESSAGE_INFO_S *pMsg, char *val)
1790 temp = __msgsvc_vmsg_get_content_value(val);
1791 MSG_ERR_RET_VM(NULL == temp, MSG_ERR_INVALID_PARAMETER, "Invalid parameter : msg type");
1793 temp = __msgsvc_vmsg_remove_escape_char(temp);
1795 if (strcmp(temp, content_name[VMSG_INDICATION_MESSAGE_TYPE_MSG]) == 0) {
1796 pMsg->msgType.mainType = MSG_SMS_TYPE;
1797 pMsg->msgType.subType = MSG_NORMAL_SMS;
1798 pMsg->msgType.classType = MSG_CLASS_NONE;
1800 else if (strcmp(temp, content_name[VMSG_INDICATION_MESSAGE_TYPE_INET]) == 0) {
1804 return MSG_ERR_INVALID_PARAMETER;
1806 MSG_DEBUG("pMsg->msgType.subType = %d", pMsg->msgType.subType);
1810 static inline msg_error_t __msgsvc_vmsg_get_address(MSG_MESSAGE_INFO_S *pMsg, char *prefix, char *val, int* vCardCnt)
1813 MSG_DEBUG("vCardCnt is : %d", *vCardCnt);
1814 if ((pMsg->folderId == MSG_SENTBOX_ID || pMsg->folderId == MSG_DRAFT_ID) && *vCardCnt == 1)
1817 if (pMsg->folderId == MSG_INBOX_ID && *vCardCnt > 1)
1820 temp = __msgsvc_vmsg_get_content_value(val);
1821 MSG_ERR_RET_VM(NULL == temp, MSG_ERR_INVALID_PARAMETER, "Invalid parameter : address");
1823 temp = __msgsvc_vmsg_remove_escape_char(temp);
1824 MSG_ADDRESS_INFO_S * addrInfo = NULL;
1826 pMsg->nAddressCnt++;
1827 MSG_DEBUG("Address is : %s", temp);
1828 MSG_DEBUG("Address Cnt : %d", pMsg->nAddressCnt);
1831 if (pMsg->addressList == NULL) {
1832 addrInfo = (MSG_ADDRESS_INFO_S *)calloc(1, sizeof(MSG_ADDRESS_INFO_S));
1834 addrInfo = (MSG_ADDRESS_INFO_S *)realloc(pMsg->addressList, pMsg->nAddressCnt * sizeof(MSG_ADDRESS_INFO_S));
1837 if (addrInfo == NULL) {
1838 return MSG_ERR_INVALID_PARAMETER;
1840 pMsg->addressList = addrInfo;
1842 pMsg->addressList[pMsg->nAddressCnt-1].addressType = MSG_ADDRESS_TYPE_PLMN;
1843 pMsg->addressList[pMsg->nAddressCnt-1].recipientType = MSG_RECIPIENTS_TYPE_TO;
1844 strncpy(pMsg->addressList[pMsg->nAddressCnt-1].addressVal, temp, MAX_ADDRESS_VAL_LEN);
1845 MSG_DEBUG("pMsg->addressList[pMsg->nAddressCnt-1].addressVal = %s", pMsg->addressList[pMsg->nAddressCnt-1].addressVal);
1849 static inline msg_error_t __msgsvc_vmsg_get_msg_date(MSG_MESSAGE_INFO_S *pMsg, char *val)
1853 temp = __msgsvc_vmsg_get_content_value(val);
1854 MSG_ERR_RET_VM(NULL == temp, MSG_ERR_INVALID_PARAMETER, "Invalid parameter : date");
1856 temp = __msgsvc_vmsg_remove_escape_char(temp);
1857 MSG_DEBUG("pMsg->displayTime = %s", temp);
1858 struct tm displayTime;
1859 if ( __msgsvc_vmsg_convert_vdata_to_tm_str(temp, &displayTime))
1860 pMsg->displayTime = mktime(&displayTime);
1862 pMsg->displayTime = time(NULL);
1866 static inline msg_error_t __msgsvc_vmsg_get_msg_subject(MSG_MESSAGE_INFO_S *pMsg, char *val)
1870 temp = __msgsvc_vmsg_get_content_value(val);
1871 MSG_ERR_RET_VM(NULL == temp, MSG_ERR_INVALID_PARAMETER, "Invalid parameter : subject");
1873 temp = __msgsvc_vmsg_remove_escape_char(temp);
1875 if (temp && temp[0] != '\0')
1876 strncpy(pMsg->subject, temp, MAX_SUBJECT_LEN);
1877 MSG_DEBUG("pMsg->subject = %s", pMsg->subject);
1881 static inline bool __msgsvc_vmsg_get_msg_begin(MSG_MESSAGE_INFO_S *pMsg, char *val, int *vCardCnt)
1883 pMsg->encodeType = MSG_ENCODE_AUTO;
1887 temp = __msgsvc_vmsg_get_content_value(val);
1888 MSG_ERR_RET_VM(NULL == temp, MSG_ERR_INVALID_PARAMETER, "Invalid parameter : body");
1890 temp = __msgsvc_vmsg_remove_escape_char(temp);
1892 if (temp && temp[0] != '\0' && strcmp(temp, "VCARD") == 0) {
1899 static inline bool __msgsvc_vmsg_get_msg_end(MSG_MESSAGE_INFO_S *pMsg, char *val)
1901 pMsg->encodeType = MSG_ENCODE_AUTO;
1905 temp = __msgsvc_vmsg_get_content_value(val);
1906 MSG_ERR_RET_VM(NULL == temp, MSG_ERR_INVALID_PARAMETER, "Invalid parameter : body");
1908 temp = __msgsvc_vmsg_remove_escape_char(temp);
1910 if (temp && temp[0] != '\0' && strcmp(temp, "VMSG") == 0) {
1911 MSG_DEBUG("VMessage decoding completed");
1913 } else if (temp && temp[0] != '\0' && strcmp(temp, "VCARD") == 0) {
1914 MSG_DEBUG("VMessage decoding completed");
1920 static inline msg_error_t __msgsvc_vmsg_get_msg(int ver, char *vmsg, MSG_MESSAGE_INFO_S *record)
1923 char *cursor, *new_start, *val, *prefix;
1925 MSG_MESSAGE_INFO_S *pMsg = record;
1927 bool isDateAvailable = false;
1936 bool base64_encoded = false;
1937 char *bodyStart = cursor;
1938 type = __msgsvc_vmsg_check_content_type(&cursor);
1940 if (VMSG_VALUE_NONE == type) {
1941 if (isDateAvailable == true) {
1942 isDateAvailable = false;
1944 MSG_DEBUG("Decoding body :");
1945 bodyStart = __msgsvc_vmsg_pass_unsupported(bodyStart);
1947 char tempMsgText[MAX_MSG_TEXT_LEN + 1] = {0, };
1949 if (i >= MAX_MSG_TEXT_LEN)
1951 if (*bodyStart == 'E' && *(bodyStart+1) == 'N' && *(bodyStart+2) == 'D' && *(bodyStart+3) == ':') {
1955 tempMsgText[i] = *bodyStart;
1959 tempMsgText[i] = '\0';
1960 char * temp = __msgsvc_vmsg_remove_escape_char(tempMsgText);
1961 snprintf(pMsg->msgText, sizeof(pMsg->msgText), "%s", temp);
1962 MSG_DEBUG("pMsg->msgText : %s", pMsg->msgText);
1963 pMsg->dataSize = strlen(pMsg->msgText);
1964 pMsg->bTextSms = true;
1965 base64_encoded = __msgsvc_vmsg_check_base64_encoded(pMsg->msgText);
1967 if (base64_encoded) {
1968 char * decodedText = NULL;
1969 decodedText = __msgsvc_vmsg_decode_base64_val(pMsg->msgText);
1971 strncpy(pMsg->msgText, decodedText, MAX_MSG_TEXT_LEN);
1972 pMsg->dataSize = strlen(pMsg->msgText);
1975 pMsg->msgText[0] = '\0';
1983 new_start = __msgsvc_vmsg_pass_unsupported(cursor);
1993 base64_encoded = __msgsvc_vmsg_check_base64_encoded(cursor);
1995 new_start = __msgsvc_vmsg_get_val(ver, cursor, &prefix, &val);
1997 if (NULL == new_start) {
2010 if (base64_encoded) {
2011 char *temp = __msgsvc_vmsg_decode_base64_val(val);
2016 case VMSG_INDICATION_READ_STATUS:
2017 __msgsvc_vmsg_get_read_status(pMsg, val);
2019 case VMSG_INDICATION_MSG_BOX:
2020 __msgsvc_vmsg_get_msg_box(pMsg, val);
2022 case VMSG_INDICATION_MESSAGE_TYPE:
2023 __msgsvc_vmsg_get_msg_type(pMsg, val);
2025 case VMSG_VCARD_TEL:
2026 __msgsvc_vmsg_get_address(pMsg, prefix, val, &vCardCnt);
2028 case VMSG_BODY_PROPERTY_DATE:
2029 isDateAvailable = true;
2030 __msgsvc_vmsg_get_msg_date(pMsg, val);
2032 case VMSG_BODY_PROPERTY_SUBJECT:
2033 __msgsvc_vmsg_get_msg_subject(pMsg, val);
2035 case VMSG_MSG_BEGIN:
2036 end = __msgsvc_vmsg_get_msg_begin(pMsg, val, &vCardCnt);
2039 end = __msgsvc_vmsg_get_msg_end(pMsg, val);
2047 MSG_ERR("Invalid parameter : __msgsvc_vmsg_check_content_type() Failed(%d)", type);
2050 return MSG_ERR_INVALID_PARAMETER;
2057 MSG_ERR("Invalid vmsg");
2058 return MSG_ERR_INVALID_PARAMETER;
2061 msg_error_t MsgVMessageDecodeSMS(const char *vmsg_stream, MSG_MESSAGE_INFO_S *pMsg)
2067 char *vmsg = (char *)vmsg_stream;
2069 char *MMSsend, *MMSretrieve, *MMSnoti;
2071 MSG_ERR_RET_VM(NULL == vmsg_stream, MSG_ERR_INVALID_PARAMETER, "Invalid Parameter : vmsg");
2073 vmsg = __msgsvc_vmsg_check_word(vmsg, "BEGIN:VMSG");
2074 MSG_ERR_RET_VM(NULL == vmsg, MSG_ERR_INVALID_PARAMETER, "Invalid parameter : The vmsg is invalid.");
2076 /* check for mms() */
2081 MMSsend = __msgsvc_vmsg_check_word(MMSsend, "X-MESSAGE-TYPE:MMS SEND");
2082 MSG_ERR_RET_VM(NULL != MMSsend, MSG_ERR_INVALID_MESSAGE, "Invalid parameter : The vmsg format is invalid.");
2084 MMSretrieve = __msgsvc_vmsg_check_word(MMSretrieve, "X-MESSAGE-TYPE:MMS RETRIEVE");
2085 MSG_ERR_RET_VM(NULL != MMSretrieve, MSG_ERR_INVALID_MESSAGE, "Invalid parameter : The vmsg format is invalid.");
2087 MMSnoti = __msgsvc_vmsg_check_word(MMSnoti, "X-MESSAGE-TYPE:MMS NOTIFICATION");
2088 MSG_ERR_RET_VM(NULL != MMSnoti, MSG_ERR_INVALID_MESSAGE, "Invalid parameter : The vmsg format is invalid.");
2091 __msgsvc_vmsg_initial();
2093 vmsg = __msgsvc_vmsg_check_word(vmsg, "VERSION:1.1");
2094 MSG_ERR_RET_VM(NULL == vmsg, MSG_ERR_INVALID_PARAMETER, "Invalid parameter : The vmsg format is invalid.");
2096 ver = MSGSVC_VMSG_VER_1_1;
2098 ret = __msgsvc_vmsg_get_msg(ver, vmsg, pMsg);
2099 if (MSG_SUCCESS!= ret) {