4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
25 * Created on: 2012. 3. 6.
26 * Author: kyuho.jo@samsung.com
42 #include "email-types.h"
43 #include "email-internal-types.h"
44 #include "email-utilities.h"
46 INTERNAL_FUNC void* em_malloc(int len)
48 /* EM_DEBUG_LOG("Memory allocation size[%d] bytes", len); */
50 EM_DEBUG_EXCEPTION("len should be positive.[%d]", len);
54 void *p = calloc(1,len);
56 EM_DEBUG_PERROR("malloc failed");
62 INTERNAL_FUNC void* em_memdup(void* src, int len)
64 /* EM_DEBUG_LOG("Memory allocation size[%d] bytes", len); */
66 EM_DEBUG_EXCEPTION("len should be positive.[%d]", len);
70 void *p = calloc(1,len);
72 EM_DEBUG_EXCEPTION("malloc failed");
82 /* remove left space, tab, CR, L */
83 INTERNAL_FUNC char *em_trim_left(char *str)
85 char *p, *temp_buffer = NULL;
87 /* EM_DEBUG_FUNC_BEGIN() */
88 if (!str) return NULL;
91 while (*p && (*p == ' ' || *p == '\t' || *p == LF || *p == CR)) p++;
95 temp_buffer = EM_SAFE_STRDUP(p);
97 strncpy(str, temp_buffer, EM_SAFE_STRLEN(str));
98 str[EM_SAFE_STRLEN(temp_buffer)] = NULL_CHAR;
100 EM_SAFE_FREE(temp_buffer);
105 /* remove right space, tab, CR, L */
106 INTERNAL_FUNC char *em_trim_right(char *str)
110 /* EM_DEBUG_FUNC_BEGIN() */
111 if (!str) return NULL;
113 p = str+EM_SAFE_STRLEN(str)-1;
114 while (((int)p >= (int)str) && (*p == ' ' || *p == '\t' || *p == LF || *p == CR))
117 if ((int) p < (int)str)
123 INTERNAL_FUNC char* em_upper_string(char *str)
133 INTERNAL_FUNC char* em_lower_string(char *str)
143 INTERNAL_FUNC int em_upper_path(char *path)
145 int i = 0, is_utf7 = 0, len = path ? (int)EM_SAFE_STRLEN(path) : -1;
146 for (; i < len; i++) {
147 if (path[i] == '&' || path[i] == 5) {
152 if (path[i] == '-') is_utf7 = 0;
155 path[i] = toupper(path[i]);
163 INTERNAL_FUNC void em_skip_whitespace(char *addr_str, char **pAddr)
165 EM_DEBUG_FUNC_BEGIN("addr_str[%p]", addr_str);
169 char *str = addr_str;
170 char ptr[EM_SAFE_STRLEN(str)+1] ;
174 for (i = 0; str[i] != NULL_CHAR ; i++) {
175 if (str[i] != SPACE && str[i] != TAB && str[i] != CR && str[i] != LF)
180 *pAddr = EM_SAFE_STRDUP(ptr);
181 EM_DEBUG_FUNC_END("ptr[%s]", ptr);
184 INTERNAL_FUNC char* em_skip_whitespace_without_strdup(char *source_string)
186 EM_DEBUG_FUNC_BEGIN("source_string[%p]", source_string);
192 for (i = 0; source_string[i] != NULL_CHAR ; i++) {
193 if (source_string[i] != SPACE) /* || source_string[i] != TAB || source_string[i] != CR || source_string[i] || LF) */
197 EM_DEBUG_FUNC_END("i[%d]", i);
198 return source_string + i;
201 /*refactoring required*/
202 INTERNAL_FUNC char* em_replace_all_string(char *source_string, char *old_string, char *new_string)
204 EM_DEBUG_FUNC_BEGIN();
205 char *result_buffer = NULL;
207 int i = 0, count = 0;
208 int old_str_length = 0;
209 int new_str_length = 0;
211 EM_IF_NULL_RETURN_VALUE(source_string, NULL);
212 EM_IF_NULL_RETURN_VALUE(old_string, NULL);
213 EM_IF_NULL_RETURN_VALUE(new_string, NULL);
215 old_str_length = EM_SAFE_STRLEN(old_string);
216 new_str_length = EM_SAFE_STRLEN(new_string);
218 if (old_str_length != new_str_length) {
219 for (i = 0; source_string[i] != '\0';) {
220 if (memcmp(&source_string[i], old_string, old_str_length) == 0) {
228 i = EM_SAFE_STRLEN(source_string);
231 result_buffer = (char *)malloc(i + 1 + count*(new_str_length-old_str_length));
232 if (result_buffer == NULL)
236 while (*source_string) {
237 if (memcmp(source_string, old_string, old_str_length) == 0) {
238 memcpy(p, new_string, new_str_length);
240 source_string += old_str_length;
242 *p++ = *source_string++;
247 EM_DEBUG_FUNC_END("result_buffer : %s", result_buffer);
248 return result_buffer;
251 INTERNAL_FUNC char* em_replace_string(char *source_string, char *old_string, char *new_string)
253 EM_DEBUG_FUNC_BEGIN();
254 char *result_buffer = NULL;
256 int buffer_length = 0;
258 EM_IF_NULL_RETURN_VALUE(source_string, NULL);
259 EM_IF_NULL_RETURN_VALUE(old_string, NULL);
260 EM_IF_NULL_RETURN_VALUE(new_string, NULL);
262 p = strstr(source_string, old_string);
265 EM_DEBUG_EXCEPTION("old_string not found in source_string");
266 EM_DEBUG_FUNC_END("return NULL");
270 buffer_length = EM_SAFE_STRLEN(source_string) + 1024;
271 result_buffer = (char *)em_malloc(buffer_length);
273 if (!result_buffer) {
274 EM_DEBUG_EXCEPTION("em_malloc failed");
278 strncpy(result_buffer, source_string, p - source_string);
279 snprintf(result_buffer + strlen(result_buffer), buffer_length - strlen(result_buffer), "%s%s", new_string, p + strlen(old_string)); /*prevent 34351*/
281 EM_DEBUG_FUNC_END("result_buffer[%p]", result_buffer);
282 return result_buffer;
285 /* Memory clean up */
286 #include <sys/mman.h>
288 /* #define GETSP() ({ unsigned int sp; asm volatile ("mov %0, sp " : "=r"(sp)); sp;}) */
290 #define PAGE_SIZE (1 << 12)
291 #define _ALIGN_UP(addr, size) (((addr)+((size)-1))&(~((size)-1)))
292 #define _ALIGN_DOWN(addr, size) ((addr)&(~((size)-1)))
293 #define PAGE_ALIGN(addr) _ALIGN_DOWN(addr, PAGE_SIZE)
300 unsigned int stacktop;
304 asm volatile ("mov %0, sp " : "=r"(sp));
306 sprintf(buf, "/proc/%d/maps", getpid());
307 file = fopen(buf, "r");
308 while (fgets(buf, BUF_SIZE, file) != NULL) {
309 if (strstr(buf, "[stack]")) {
318 sscanf(buf, "%x-", &stacktop);
319 if (madvise((void *)PAGE_ALIGN(stacktop), PAGE_ALIGN(sp)-stacktop, MADV_DONTNEED) < 0)
320 perror("stack madvise fail");
326 INTERNAL_FUNC void em_flush_memory()
328 EM_DEBUG_FUNC_BEGIN();
329 /* flush memory in heap */
332 /* flush memory in stack */
335 /* flush memory for sqlite */
336 emstorage_flush_db_cache();
340 INTERNAL_FUNC int em_get_file_name_from_file_path(char *input_source_file_path, char **output_file_name)
342 EM_DEBUG_FUNC_BEGIN("input_source_file_path[%s], output_file_name [%p]", input_source_file_path, output_file_name);
343 int err = EMAIL_ERROR_NONE;
344 int pos_on_string = 0;
345 int file_name_length = 0;
346 char *start_pos_of_file_name = NULL;
347 char *end_pos_of_file_name = NULL;
348 char *end_pos_of_file_path = NULL;
349 char file_name_string[MAX_PATH] = { 0, };
351 if (!input_source_file_path || !output_file_name) {
352 EM_DEBUG_EXCEPTION("Invalid Parameter");
353 err = EMAIL_ERROR_INVALID_PARAM;
357 pos_on_string = EM_SAFE_STRLEN(input_source_file_path) - 1;
358 end_pos_of_file_path = input_source_file_path + pos_on_string;
359 end_pos_of_file_name = end_pos_of_file_path;
361 while(pos_on_string >= 0 && input_source_file_path[pos_on_string] != '/') {
367 if(pos_on_string >= 0) {
368 start_pos_of_file_name = input_source_file_path + pos_on_string;
369 file_name_length = end_pos_of_file_name - start_pos_of_file_name + 1;
370 memcpy(file_name_string, start_pos_of_file_name, file_name_length);
373 EM_DEBUG_LOG("file_name_string [%s] pos_on_string [%d] file_name_length [%d]", file_name_string, pos_on_string, file_name_length);
375 *output_file_name = EM_SAFE_STRDUP(file_name_string);
378 EM_DEBUG_FUNC_END("err = [%d]", err);
382 INTERNAL_FUNC int em_get_file_name_and_extension_from_file_path(char *input_source_file_path, char **output_file_name, char **output_extension)
384 EM_DEBUG_FUNC_BEGIN("input_source_file_path[%s], output_file_name [%p], output_extension [%p]", input_source_file_path, output_file_name, output_extension);
385 int err = EMAIL_ERROR_NONE;
386 int pos_on_string = 0;
387 int file_name_length = 0;
388 int extention_length = 0;
389 char *start_pos_of_file_name = NULL;
390 char *end_pos_of_file_name = NULL;
391 char *dot_pos_of_file_path = NULL;
392 char *end_pos_of_file_path = NULL;
393 char file_name_string[MAX_PATH] = { 0, };
394 char extension_string[MAX_PATH] = { 0, };
396 if (!input_source_file_path || !output_file_name || !output_extension) {
397 EM_DEBUG_EXCEPTION("Invalid Parameter");
398 err = EMAIL_ERROR_INVALID_PARAM;
402 pos_on_string = EM_SAFE_STRLEN(input_source_file_path) - 1;
403 end_pos_of_file_path = input_source_file_path + pos_on_string;
404 end_pos_of_file_name = end_pos_of_file_path;
406 while(pos_on_string >= 0 && input_source_file_path[pos_on_string] != '/') {
407 if(input_source_file_path[pos_on_string] == '.') {
408 if(dot_pos_of_file_path == NULL) {
409 end_pos_of_file_name = input_source_file_path + pos_on_string;
410 dot_pos_of_file_path = end_pos_of_file_name;
418 if(pos_on_string >= 0) {
419 start_pos_of_file_name = input_source_file_path + pos_on_string;
420 file_name_length = end_pos_of_file_name - start_pos_of_file_name;
421 memcpy(file_name_string, start_pos_of_file_name, file_name_length);
424 if(dot_pos_of_file_path != NULL) {
425 extention_length = (end_pos_of_file_path + 1) - (dot_pos_of_file_path + 1);
426 memcpy(extension_string, dot_pos_of_file_path + 1, extention_length);
429 EM_DEBUG_LOG("*file_name_string [%s] pos_on_string [%d]", file_name_string, pos_on_string);
431 *output_file_name = EM_SAFE_STRDUP(file_name_string);
432 *output_extension = EM_SAFE_STRDUP(extension_string);
435 EM_DEBUG_FUNC_END("err = [%d]", err);
439 INTERNAL_FUNC char *em_get_extension_from_file_path(char *source_file_path, int *err_code)
441 EM_DEBUG_FUNC_BEGIN("source_file_path[%s]", source_file_path);
442 int err = EMAIL_ERROR_NONE, pos_on_string = 0;
443 char *extension = NULL;
445 if (!source_file_path) {
446 EM_DEBUG_EXCEPTION("Invalid Parameter");
447 err = EMAIL_ERROR_INVALID_PARAM;
451 pos_on_string = EM_SAFE_STRLEN(source_file_path) - 1;
453 while(pos_on_string > 0 && source_file_path[pos_on_string--] != '.') ;
455 if(pos_on_string > 0)
456 extension = source_file_path + pos_on_string + 2;
458 EM_DEBUG_LOG("*extension [%s] pos_on_string [%d]", extension, pos_on_string);
467 INTERNAL_FUNC int em_get_encoding_type_from_file_path(const char *input_file_path, char **output_encoding_type)
469 EM_DEBUG_FUNC_BEGIN("input_file_path[%d], output_encoding_type[%p]", input_file_path, output_encoding_type);
470 int err = EMAIL_ERROR_NONE;
471 int pos_of_filename = 0;
473 int enf_of_string = 0;
474 int result_string_length = 0;
475 char *filename = NULL;
476 char *result_encoding_type = NULL;
478 if (!input_file_path || !output_encoding_type) {
479 EM_DEBUG_EXCEPTION("Invalid Parameter");
480 err = EMAIL_ERROR_INVALID_PARAM;
484 enf_of_string = pos_of_filename = EM_SAFE_STRLEN(input_file_path);
486 while(pos_of_filename >= 0 && input_file_path[pos_of_filename--] != '/') {
487 if(input_file_path[pos_of_filename] == '.')
488 pos_of_dot = pos_of_filename;
491 if(pos_of_filename != 0)
492 pos_of_filename += 2;
494 filename = (char*)input_file_path + pos_of_filename;
496 if(pos_of_dot != 0 && pos_of_dot > pos_of_filename)
497 result_string_length = pos_of_dot - pos_of_filename;
499 result_string_length = enf_of_string - pos_of_filename;
501 EM_DEBUG_LOG("pos_of_dot [%d], pos_of_filename [%d], enf_of_string[%d],result_string_length [%d]", pos_of_dot, pos_of_filename, enf_of_string, result_string_length);
503 if( !(result_encoding_type = em_malloc(sizeof(char) * (result_string_length + 1))) ) {
504 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
505 err = EMAIL_ERROR_OUT_OF_MEMORY;
509 memcpy(result_encoding_type, input_file_path + pos_of_filename, result_string_length);
511 EM_DEBUG_LOG("*result_encoding_type [%s]", result_encoding_type);
513 *output_encoding_type = result_encoding_type;
516 EM_DEBUG_FUNC_END("err [%d]", err);
520 INTERNAL_FUNC int em_get_content_type_from_extension_string(const char *extension_string, int *err_code)
522 EM_DEBUG_FUNC_BEGIN("extension_string[%s]", extension_string);
523 int i = 0, err = EMAIL_ERROR_NONE, result_content_type = TYPEAPPLICATION;
524 char *image_extension[] = { "jpeg", "jpg", "png", "gif", "bmp", "pic", "agif", "tif", "wbmp" , "p7s", "p7m", NULL};
526 if (!extension_string) {
527 EM_DEBUG_EXCEPTION("Invalid Parameter");
528 err = EMAIL_ERROR_INVALID_PARAM;
532 while(image_extension[i]) {
533 EM_DEBUG_LOG("image_extension[%d] [%s]", i, image_extension[i]);
534 if(strcasecmp(image_extension[i], extension_string) == 0) {
550 result_content_type = TYPEIMAGE;
553 result_content_type = TYPEPKCS7_SIGN;
556 result_content_type = TYPEPKCS7_MIME;
566 return result_content_type;
569 #define EMAIL_ACCOUNT_RGEX "([a-z0-9!#$%&'*+/=?^_`{|}~-]+.)*[a-z0-9!#$%&'*+/=?^_`{|}~-]+"
570 #define EMAIL_DOMAIN_RGEX "([a-z0-9!#$%&'*+/=?^_`{|}~-]+.)+[a-z0-9!#$%&'*+/=?^_`{|}~-]+"
572 #define EMAIL_ADDR_RGEX "[[:space:]]*<"EMAIL_ACCOUNT_RGEX"@"EMAIL_DOMAIN_RGEX">[[:space:]]*"
573 #define EMAIL_ALIAS_RGEX "([[:space:]]*\"[^\"]*\")?"EMAIL_ADDR_RGEX
574 #define EMAIL_ALIAS_LIST_RGEX "^("EMAIL_ALIAS_RGEX"[;,])*"EMAIL_ALIAS_RGEX"[;,]?[[:space:]]*$"
576 #define EMAIL_ADDR_WITHOUT_BRACKET_RGEX "[[:space:]]*"EMAIL_ACCOUNT_RGEX"@"EMAIL_DOMAIN_RGEX"[[:space:]]*"
577 #define EMAIL_ALIAS_WITHOUT_BRACKET_RGEX "([[:space:]]*\"[^\"]*\")?"EMAIL_ADDR_WITHOUT_BRACKET_RGEX
578 #define EMAIL_ALIAS_LIST_WITHOUT_BRACKET_RGEX "("EMAIL_ALIAS_WITHOUT_BRACKET_RGEX"[;,])*"EMAIL_ADDR_WITHOUT_BRACKET_RGEX"[;,]?[[:space:]]*$"
580 INTERNAL_FUNC int em_verify_email_address(char *address, int without_bracket, int *err_code)
582 EM_DEBUG_FUNC_BEGIN("address[%s] without_bracket[%d]", address, without_bracket);
584 /* this following code verfies the email alias string using reg. exp. */
585 regex_t alias_list_regex = {0};
586 int ret = false, error = EMAIL_ERROR_NONE;
587 char *reg_rule = NULL;
589 if(!address || EM_SAFE_STRLEN(address) == 0) {
590 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
592 *err_code = EMAIL_ERROR_INVALID_PARAM;
597 reg_rule = EMAIL_ALIAS_LIST_WITHOUT_BRACKET_RGEX;
599 reg_rule = EMAIL_ALIAS_LIST_RGEX;
601 if (regcomp(&alias_list_regex, reg_rule, REG_ICASE | REG_EXTENDED)) {
602 EM_DEBUG_EXCEPTION("email alias regex unrecognized");
604 *err_code = EMAIL_ERROR_UNKNOWN;
608 int alias_len = EM_SAFE_STRLEN(address) + 1;
609 regmatch_t pmatch[alias_len];
611 bzero(pmatch, alias_len);
613 if (regexec(&alias_list_regex, address, alias_len, pmatch, 0) == REG_NOMATCH)
614 EM_DEBUG_LOG("failed :[%s]", address);
616 EM_DEBUG_LOG("success :[%s]", address);
620 regfree(&alias_list_regex);
625 EM_DEBUG_FUNC_END("ret [%d]", ret);
629 INTERNAL_FUNC int em_verify_email_address_of_mail_data(email_mail_data_t *mail_data, int without_bracket, int *err_code)
631 EM_DEBUG_FUNC_BEGIN("mail_data[%p] without_bracket[%d]", mail_data, without_bracket);
632 char *address_array[4] = { mail_data->full_address_from, mail_data->full_address_to, mail_data->full_address_cc, mail_data->full_address_bcc};
633 int ret = false, err = EMAIL_ERROR_NONE, i;
635 /* check for email_address validation */
636 for (i = 0; i < 4; i++) {
637 if (address_array[i] && address_array[i][0] != 0) {
638 if (!em_verify_email_address(address_array[i] , without_bracket, &err)) {
639 err = EMAIL_ERROR_INVALID_ADDRESS;
640 EM_DEBUG_EXCEPTION("Invalid Email Address [%d][%s]", i, address_array[i]);
647 EM_DEBUG_FUNC_END("ret [%d]", ret);
651 INTERNAL_FUNC int em_verify_email_address_of_mail_tbl(emstorage_mail_tbl_t *input_mail_tbl, int input_without_bracket)
653 EM_DEBUG_FUNC_BEGIN("input_mail_tbl[%p] input_without_bracket[%d]", input_mail_tbl, input_without_bracket);
654 char *address_array[4] = { input_mail_tbl->full_address_to, input_mail_tbl->full_address_cc, input_mail_tbl->full_address_bcc, input_mail_tbl->full_address_from};
655 int err = EMAIL_ERROR_NONE, i;
657 /* check for email_address validation */
658 for (i = 0; i < 4; i++) {
659 if (address_array[i] && address_array[i][0] != 0) {
660 if (!em_verify_email_address(address_array[i] , input_without_bracket, &err)) {
661 err = EMAIL_ERROR_INVALID_ADDRESS;
662 EM_DEBUG_EXCEPTION("Invalid Email Address [%d][%s]", i, address_array[i]);
669 EM_DEBUG_FUNC_END("err [%d]", err);
673 INTERNAL_FUNC int em_find_tag_for_thread_view(char *subject, int *result)
675 EM_DEBUG_FUNC_BEGIN();
676 int error_code = EMAIL_ERROR_NONE;
677 char *copy_of_subject = NULL;
679 EM_IF_NULL_RETURN_VALUE(subject, EMAIL_ERROR_INVALID_PARAM);
680 EM_IF_NULL_RETURN_VALUE(result, EMAIL_ERROR_INVALID_PARAM);
684 copy_of_subject = EM_SAFE_STRDUP(subject);
686 if (copy_of_subject == NULL) {
687 EM_DEBUG_EXCEPTION("strdup is failed.");
691 em_upper_string(copy_of_subject);
692 EM_DEBUG_LOG("em_upper_string result : %s\n", copy_of_subject);
694 if (strstr(copy_of_subject, "RE:") == NULL) {
695 if (strstr(copy_of_subject, "FWD:") == NULL) {
696 if (strstr(copy_of_subject, "FW:") != NULL)
706 EM_SAFE_FREE(copy_of_subject);
708 EM_DEBUG_FUNC_END("result : %d", *result);
713 INTERNAL_FUNC int em_find_pos_stripped_subject_for_thread_view(char *subject, char *stripped_subject, int stripped_subject_buffer_size)
715 EM_DEBUG_FUNC_BEGIN("subject [%p] stripped_subject [%p] stripped_subject_buffer_size[%d]", subject, stripped_subject, stripped_subject_buffer_size);
716 int error_code = EMAIL_ERROR_NONE;
718 char *copy_of_subject = NULL, *curpos = NULL, *result;
720 EM_IF_NULL_RETURN_VALUE(subject, EMAIL_ERROR_INVALID_PARAM);
721 EM_IF_NULL_RETURN_VALUE(stripped_subject, EMAIL_ERROR_INVALID_PARAM);
723 copy_of_subject = EM_SAFE_STRDUP(subject);
725 if (copy_of_subject == NULL) {
726 EM_DEBUG_EXCEPTION("strdup is failed");
730 em_upper_string(copy_of_subject);
731 curpos = copy_of_subject;
733 EM_DEBUG_LOG("em_upper_string result : %s", copy_of_subject);
735 while ((result = strstr(curpos, "RE:")) != NULL) {
737 EM_DEBUG_LOG("RE result : %s", curpos);
740 while ((result = strstr(curpos, "FWD:")) != NULL) {
742 EM_DEBUG_LOG("FWD result : %s", curpos);
745 while ((result = strstr(curpos, "FW:")) != NULL) {
747 EM_DEBUG_LOG("FW result : %s", curpos);
750 while (curpos != NULL && *curpos == ' ') {
754 gap = curpos - copy_of_subject;
756 EM_SAFE_STRNCPY(stripped_subject, subject + gap, stripped_subject_buffer_size);
759 EM_SAFE_FREE(copy_of_subject);
761 if (error_code == EMAIL_ERROR_NONE && stripped_subject)
762 EM_DEBUG_LOG("result[%s]", stripped_subject);
764 EM_DEBUG_FUNC_END("error_code[%d]", error_code);
772 INTERNAL_FUNC int em_encode_base64(char *src, unsigned long src_len, char **enc, unsigned long* enc_len, int *err_code)
774 EM_DEBUG_FUNC_BEGIN();
776 unsigned char *content;
777 int ret = true, err = EMAIL_ERROR_NONE;
779 if (err_code != NULL) {
780 *err_code = EMAIL_ERROR_NONE;
783 content = rfc822_binary(src, src_len, enc_len);
786 *enc = (char *)content;
788 err = EMAIL_ERROR_UNKNOWN;
802 INTERNAL_FUNC int em_decode_base64(unsigned char *enc_text, unsigned long enc_len, char **dec_text, unsigned long* dec_len, int *err_code)
804 unsigned char *text = enc_text;
805 unsigned long size = enc_len;
806 unsigned char *content;
807 int ret = true, err = EMAIL_ERROR_NONE;
809 if (err_code != NULL) {
810 *err_code = EMAIL_ERROR_NONE;
813 EM_DEBUG_FUNC_BEGIN();
815 content = rfc822_base64(text, size, dec_len);
817 *dec_text = (char *)content;
820 err = EMAIL_ERROR_UNKNOWN;
830 INTERNAL_FUNC int em_get_account_server_type_by_account_id(int account_id, email_account_server_t* account_server_type, int flag, int *error)
832 EM_DEBUG_FUNC_BEGIN();
833 emstorage_account_tbl_t *account_tbl_data = NULL;
835 int err= EMAIL_ERROR_NONE;
837 if (account_server_type == NULL ) {
838 EM_DEBUG_EXCEPTION("account_server_type is NULL");
839 err = EMAIL_ERROR_INVALID_PARAM;
844 if( !emstorage_get_account_by_id(account_id, WITHOUT_OPTION, &account_tbl_data, false, &err)) {
845 EM_DEBUG_EXCEPTION ("emstorage_get_account_by_id failed [%d] ", err);
850 if ( flag == false ) { /* sending server */
851 *account_server_type = account_tbl_data->outgoing_server_type;
852 } else if ( flag == true ) { /* receiving server */
853 *account_server_type = account_tbl_data->incoming_server_type;
859 if ( account_tbl_data != NULL ) {
860 emstorage_free_account(&account_tbl_data, 1, NULL);
862 if ( error != NULL ) {
870 #include <dbus/dbus.h>
872 #define ACTIVE_SYNC_HANDLE_INIT_VALUE (-1)
873 #define ACTIVE_SYNC_HANDLE_BOUNDARY (-100000000)
876 INTERNAL_FUNC int em_get_handle_for_activesync(int *handle, int *error)
878 EM_DEBUG_FUNC_BEGIN();
880 static int next_handle = 0;
882 int err = EMAIL_ERROR_NONE;
884 if ( handle == NULL ) {
885 EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed : handle is NULL");
886 err = EMAIL_ERROR_INVALID_PARAM;
890 if ( vconf_get_int(VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE, &next_handle) != 0 ) {
891 EM_DEBUG_EXCEPTION("vconf_get_int failed");
892 if ( next_handle != 0 ) {
893 err = EMAIL_ERROR_GCONF_FAILURE;
898 EM_DEBUG_LOG(">>>>>> VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE : get lastest handle[%d]", next_handle);
900 /* set the value of the handle for active sync */
902 if ( next_handle < ACTIVE_SYNC_HANDLE_BOUNDARY ) {
903 next_handle = ACTIVE_SYNC_HANDLE_INIT_VALUE;
905 if ( vconf_set_int(VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE, next_handle) != 0) {
906 EM_DEBUG_EXCEPTION("vconf_set_int failed");
907 err = EMAIL_ERROR_GCONF_FAILURE;
911 *handle = next_handle;
912 EM_DEBUG_LOG(">>>>>> return next handle[%d]", *handle);
915 if ( error != NULL ) {
922 INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNotiData *data)
924 EM_DEBUG_FUNC_BEGIN("subType [%d], data [%p]", subType, data);
926 DBusConnection *connection;
927 DBusMessage *signal = NULL;
931 dbus_error_init (&error);
932 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
934 if(connection == NULL)
937 signal = dbus_message_new_signal("/User/Email/ActiveSync", EMAIL_ACTIVE_SYNC_NOTI, "email");
939 dbus_message_append_args(signal, DBUS_TYPE_INT32, &subType, DBUS_TYPE_INVALID);
941 case ACTIVE_SYNC_NOTI_SEND_MAIL:
942 EM_DEBUG_LOG("handle:[%d]", data->send_mail.handle);
943 EM_DEBUG_LOG("account_id:[%d]", data->send_mail.account_id);
944 EM_DEBUG_LOG("mail_id:[%d]", data->send_mail.mail_id);
946 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.handle), DBUS_TYPE_INVALID);
947 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.account_id), DBUS_TYPE_INVALID);
948 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.mail_id), DBUS_TYPE_INVALID);
950 case ACTIVE_SYNC_NOTI_SEND_SAVED: /* publish a send notification to ASE (active sync engine) */
951 EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_SEND_SAVED]", subType);
953 case ACTIVE_SYNC_NOTI_SEND_REPORT:
954 EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_SEND_REPORT]", subType);
956 case ACTIVE_SYNC_NOTI_SYNC_HEADER:
957 EM_DEBUG_LOG("handle:[%d]", data->sync_header.handle);
958 EM_DEBUG_LOG("account_id:[%d]", data->sync_header.account_id);
959 EM_DEBUG_LOG("mailbox_id:[%d]", data->sync_header.mailbox_id);
961 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->sync_header.handle ), DBUS_TYPE_INVALID);
962 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->sync_header.account_id ), DBUS_TYPE_INVALID);
963 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->sync_header.mailbox_id ), DBUS_TYPE_INVALID);
965 case ACTIVE_SYNC_NOTI_DOWNLOAD_BODY: /* publish a download body notification to ASE */
966 EM_DEBUG_LOG("handle:[%d]", data->download_body.handle);
967 EM_DEBUG_LOG("account_id:[%d]", data->download_body.account_id);
968 EM_DEBUG_LOG("mail_id:[%d]", data->download_body.mail_id);
969 EM_DEBUG_LOG("with_attachment:[%d]", data->download_body.with_attachment);
971 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.handle ), DBUS_TYPE_INVALID);
972 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.account_id ), DBUS_TYPE_INVALID);
973 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.mail_id ), DBUS_TYPE_INVALID);
974 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.with_attachment ), DBUS_TYPE_INVALID);
976 case ACTIVE_SYNC_NOTI_DOWNLOAD_ATTACHMENT:
977 EM_DEBUG_LOG("handle:[%d]", data->download_attachment.handle);
978 EM_DEBUG_LOG("account_id:[%d]", data->download_attachment.account_id );
979 EM_DEBUG_LOG("mail_id:[%d]", data->download_attachment.mail_id);
980 EM_DEBUG_LOG("with_attachment:[%d]", data->download_attachment.attachment_order );
982 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.handle ), DBUS_TYPE_INVALID);
983 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.account_id ), DBUS_TYPE_INVALID);
984 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.mail_id ), DBUS_TYPE_INVALID);
985 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.attachment_order), DBUS_TYPE_INVALID);
987 case ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT:
988 EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT]", subType);
990 case ACTIVE_SYNC_NOTI_CANCEL_JOB:
991 EM_DEBUG_LOG("account_id:[%d]", data->cancel_job.account_id );
992 EM_DEBUG_LOG("handle to cancel:[%d]", data->cancel_job.handle);
993 EM_DEBUG_LOG("cancel_type:[%d]", data->cancel_job.cancel_type);
995 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_job.account_id ), DBUS_TYPE_INVALID);
996 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_job.handle ), DBUS_TYPE_INVALID);
997 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_job.cancel_type ), DBUS_TYPE_INVALID);
999 case ACTIVE_SYNC_NOTI_SEARCH_ON_SERVER:
1000 EM_DEBUG_LOG("account_id:[%d]", data->search_mail_on_server.account_id );
1001 EM_DEBUG_LOG("mailbox_id:[%d]", data->search_mail_on_server.mailbox_id );
1002 EM_DEBUG_LOG("search_filter_count:[%d]", data->search_mail_on_server.search_filter_count );
1003 EM_DEBUG_LOG("handle to cancel:[%d]", data->search_mail_on_server.handle);
1005 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.account_id), DBUS_TYPE_INVALID);
1006 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.mailbox_id), DBUS_TYPE_INVALID);
1007 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.search_filter_count), DBUS_TYPE_INVALID);
1008 for(i = 0; i < data->search_mail_on_server.search_filter_count; i++) {
1009 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.search_filter_list[i].search_filter_type), DBUS_TYPE_INVALID);
1010 switch(data->search_mail_on_server.search_filter_list[i].search_filter_type) {
1011 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO :
1012 case EMAIL_SEARCH_FILTER_TYPE_UID :
1013 case EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER :
1014 case EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER :
1015 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED :
1016 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED :
1017 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT :
1018 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED :
1019 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT :
1020 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN :
1021 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.search_filter_list[i].search_filter_key_value.integer_type_key_value), DBUS_TYPE_INVALID);
1024 case EMAIL_SEARCH_FILTER_TYPE_BCC :
1025 case EMAIL_SEARCH_FILTER_TYPE_CC :
1026 case EMAIL_SEARCH_FILTER_TYPE_FROM :
1027 case EMAIL_SEARCH_FILTER_TYPE_KEYWORD :
1028 case EMAIL_SEARCH_FILTER_TYPE_SUBJECT :
1029 case EMAIL_SEARCH_FILTER_TYPE_TO :
1030 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID :
1031 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->search_mail_on_server.search_filter_list[i].search_filter_key_value.string_type_key_value), DBUS_TYPE_INVALID);
1034 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE :
1035 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON :
1036 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE :
1037 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.search_filter_list[i].search_filter_key_value.time_type_key_value), DBUS_TYPE_INVALID);
1040 EM_DEBUG_EXCEPTION("Invalid filter type [%d]", data->search_mail_on_server.search_filter_list[i].search_filter_type);
1044 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.handle), DBUS_TYPE_INVALID);
1047 case ACTIVE_SYNC_NOTI_CLEAR_RESULT_OF_SEARCH_ON_SERVER :
1048 EM_DEBUG_LOG("account_id:[%d]", data->clear_result_of_search_mail_on_server.account_id );
1049 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.account_id), DBUS_TYPE_INVALID);
1052 case ACTIVE_SYNC_NOTI_EXPUNGE_MAILS_DELETED_FLAGGED :
1053 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->expunge_mails_deleted_flagged.mailbox_id ), DBUS_TYPE_INVALID);
1054 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->expunge_mails_deleted_flagged.on_server ), DBUS_TYPE_INVALID);
1055 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->expunge_mails_deleted_flagged.handle ), DBUS_TYPE_INVALID);
1058 case ACTIVE_SYNC_NOTI_RESOLVE_RECIPIENT :
1059 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->get_resolve_recipients.account_id), DBUS_TYPE_INVALID);
1060 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->get_resolve_recipients.email_address), DBUS_TYPE_INVALID);
1061 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->get_resolve_recipients.handle), DBUS_TYPE_INVALID);
1064 case ACTIVE_SYNC_NOTI_VALIDATE_CERTIFICATE :
1065 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->validate_certificate.account_id), DBUS_TYPE_INVALID);
1066 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->validate_certificate.email_address), DBUS_TYPE_INVALID);
1067 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->validate_certificate.handle), DBUS_TYPE_INVALID);
1070 case ACTIVE_SYNC_NOTI_ADD_MAILBOX :
1071 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->add_mailbox.account_id), DBUS_TYPE_INVALID);
1072 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->add_mailbox.mailbox_path), DBUS_TYPE_INVALID);
1073 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->add_mailbox.mailbox_alias), DBUS_TYPE_INVALID);
1074 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->add_mailbox.handle), DBUS_TYPE_INVALID);
1077 case ACTIVE_SYNC_NOTI_RENAME_MAILBOX :
1078 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->rename_mailbox.account_id), DBUS_TYPE_INVALID);
1079 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->rename_mailbox.mailbox_id), DBUS_TYPE_INVALID);
1080 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->rename_mailbox.mailbox_name), DBUS_TYPE_INVALID);
1081 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->rename_mailbox.mailbox_alias), DBUS_TYPE_INVALID);
1082 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->rename_mailbox.handle), DBUS_TYPE_INVALID);
1085 case ACTIVE_SYNC_NOTI_DELETE_MAILBOX :
1086 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->delete_mailbox.account_id), DBUS_TYPE_INVALID);
1087 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->delete_mailbox.mailbox_id), DBUS_TYPE_INVALID);
1088 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->delete_mailbox.handle), DBUS_TYPE_INVALID);
1091 case ACTIVE_SYNC_NOTI_DELETE_MAILBOX_EX :
1092 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->delete_mailbox_ex.account_id), DBUS_TYPE_INVALID);
1093 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->delete_mailbox_ex.mailbox_id_count), DBUS_TYPE_INVALID);
1094 for(i = 0; i <data->delete_mailbox_ex.mailbox_id_count; i++)
1095 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->delete_mailbox_ex.mailbox_id_array[i]), DBUS_TYPE_INVALID);
1096 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->delete_mailbox_ex.handle), DBUS_TYPE_INVALID);
1099 case ACTIVE_SYNC_NOTI_SEND_MAIL_WITH_DOWNLOADING_OF_ORIGINAL_MAIL:
1100 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail_with_downloading_attachment_of_original_mail.handle), DBUS_TYPE_INVALID);
1101 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail_with_downloading_attachment_of_original_mail.mail_id), DBUS_TYPE_INVALID);
1105 EM_DEBUG_EXCEPTION("Invalid Notification type of Active Sync : subType[%d]", subType);
1109 if(!dbus_connection_send (connection, signal, NULL)) {
1110 EM_DEBUG_EXCEPTION("dbus_connection_send is failed");
1113 EM_DEBUG_LOG("dbus_connection_send is successful");
1115 dbus_connection_flush(connection);
1120 dbus_message_unref(signal);
1122 EM_DEBUG_FUNC_END();