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 INTERNAL_FUNC char* em_replace_all_string(char *source_string, char *old_string, char *new_string)
203 EM_DEBUG_FUNC_BEGIN();
204 char *result_buffer = NULL;
206 int old_str_length = 0;
207 int new_str_length = 0;
210 EM_IF_NULL_RETURN_VALUE(source_string, NULL);
211 EM_IF_NULL_RETURN_VALUE(old_string, NULL);
212 EM_IF_NULL_RETURN_VALUE(new_string, NULL);
214 int src_len = EM_SAFE_STRLEN(source_string);
215 old_str_length = EM_SAFE_STRLEN(old_string);
216 new_str_length = EM_SAFE_STRLEN(new_string);
219 EM_DEBUG_LOG("source_string is invalid");
223 result_buffer = calloc(src_len+1, sizeof(char));
224 if (!result_buffer) {
225 EM_DEBUG_EXCEPTION("calloc failed");
228 realloc_len = src_len + 1;
230 for (i = 0; i < src_len && source_string[i] != '\0';) {
231 if (old_str_length <= src_len - i &&
232 memcmp(&source_string[i], old_string, old_str_length) == 0) {
234 if (old_str_length != new_str_length) {
235 realloc_len = realloc_len - old_str_length + new_str_length;
236 result_buffer = realloc(result_buffer, realloc_len);
237 if (!result_buffer) {
238 EM_DEBUG_EXCEPTION("realloc failed");
242 memcpy(&result_buffer[j], new_string, new_str_length);
246 result_buffer[j] = source_string[i];
253 result_buffer[j] = '\0';
255 result_buffer[realloc_len-1] = '\0';
257 EM_DEBUG_FUNC_END("result_buffer : %s", result_buffer);
258 return result_buffer;
261 INTERNAL_FUNC char* em_replace_string(char *source_string, char *old_string, char *new_string)
263 EM_DEBUG_FUNC_BEGIN();
264 char *result_buffer = NULL;
266 int buffer_length = 0;
268 EM_IF_NULL_RETURN_VALUE(source_string, NULL);
269 EM_IF_NULL_RETURN_VALUE(old_string, NULL);
270 EM_IF_NULL_RETURN_VALUE(new_string, NULL);
272 p = strstr(source_string, old_string);
275 EM_DEBUG_EXCEPTION("old_string not found in source_string");
276 EM_DEBUG_FUNC_END("return NULL");
280 buffer_length = EM_SAFE_STRLEN(source_string) + 1024;
281 result_buffer = (char *)em_malloc(buffer_length);
283 if (!result_buffer) {
284 EM_DEBUG_EXCEPTION("em_malloc failed");
288 strncpy(result_buffer, source_string, p - source_string);
289 snprintf(result_buffer + strlen(result_buffer), buffer_length - strlen(result_buffer), "%s%s", new_string, p + strlen(old_string)); /*prevent 34351*/
291 EM_DEBUG_FUNC_END("result_buffer[%p]", result_buffer);
292 return result_buffer;
295 /* Memory clean up */
296 #include <sys/mman.h>
298 /* #define GETSP() ({ unsigned int sp; asm volatile ("mov %0, sp " : "=r"(sp)); sp;}) */
300 #define PAGE_SIZE (1 << 12)
301 #define _ALIGN_UP(addr, size) (((addr)+((size)-1))&(~((size)-1)))
302 #define _ALIGN_DOWN(addr, size) ((addr)&(~((size)-1)))
303 #define PAGE_ALIGN(addr) _ALIGN_DOWN(addr, PAGE_SIZE)
310 unsigned int stacktop;
314 asm volatile ("mov %0, sp " : "=r"(sp));
316 sprintf(buf, "/proc/%d/maps", getpid());
317 file = fopen(buf, "r");
318 while (fgets(buf, BUF_SIZE, file) != NULL) {
319 if (strstr(buf, "[stack]")) {
328 sscanf(buf, "%x-", &stacktop);
329 if (madvise((void *)PAGE_ALIGN(stacktop), PAGE_ALIGN(sp)-stacktop, MADV_DONTNEED) < 0)
330 perror("stack madvise fail");
336 INTERNAL_FUNC void em_flush_memory()
338 EM_DEBUG_FUNC_BEGIN();
339 /* flush memory in heap */
342 /* flush memory in stack */
345 /* flush memory for sqlite */
346 emstorage_flush_db_cache();
350 INTERNAL_FUNC int em_get_file_name_from_file_path(char *input_source_file_path, char **output_file_name)
352 EM_DEBUG_FUNC_BEGIN("input_source_file_path[%s], output_file_name [%p]", input_source_file_path, output_file_name);
353 int err = EMAIL_ERROR_NONE;
354 int pos_on_string = 0;
355 int file_name_length = 0;
356 char *start_pos_of_file_name = NULL;
357 char *end_pos_of_file_name = NULL;
358 char *end_pos_of_file_path = NULL;
359 char file_name_string[MAX_PATH] = { 0, };
361 if (!input_source_file_path || !output_file_name) {
362 EM_DEBUG_EXCEPTION("Invalid Parameter");
363 err = EMAIL_ERROR_INVALID_PARAM;
367 pos_on_string = EM_SAFE_STRLEN(input_source_file_path) - 1;
368 end_pos_of_file_path = input_source_file_path + pos_on_string;
369 end_pos_of_file_name = end_pos_of_file_path;
371 while(pos_on_string >= 0 && input_source_file_path[pos_on_string] != '/') {
377 if(pos_on_string >= 0) {
378 start_pos_of_file_name = input_source_file_path + pos_on_string;
379 file_name_length = end_pos_of_file_name - start_pos_of_file_name + 1;
380 memcpy(file_name_string, start_pos_of_file_name, file_name_length);
383 EM_DEBUG_LOG("file_name_string [%s] pos_on_string [%d] file_name_length [%d]", file_name_string, pos_on_string, file_name_length);
385 *output_file_name = EM_SAFE_STRDUP(file_name_string);
388 EM_DEBUG_FUNC_END("err = [%d]", err);
392 INTERNAL_FUNC int em_get_file_name_and_extension_from_file_path(char *input_source_file_path, char **output_file_name, char **output_extension)
394 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);
395 int err = EMAIL_ERROR_NONE;
396 int pos_on_string = 0;
397 int file_name_length = 0;
398 int extention_length = 0;
399 char *start_pos_of_file_name = NULL;
400 char *end_pos_of_file_name = NULL;
401 char *dot_pos_of_file_path = NULL;
402 char *end_pos_of_file_path = NULL;
403 char file_name_string[MAX_PATH] = { 0, };
404 char extension_string[MAX_PATH] = { 0, };
406 if (!input_source_file_path || !output_file_name || !output_extension) {
407 EM_DEBUG_EXCEPTION("Invalid Parameter");
408 err = EMAIL_ERROR_INVALID_PARAM;
412 pos_on_string = EM_SAFE_STRLEN(input_source_file_path) - 1;
413 end_pos_of_file_path = input_source_file_path + pos_on_string;
414 end_pos_of_file_name = end_pos_of_file_path;
416 while(pos_on_string >= 0 && input_source_file_path[pos_on_string] != '/') {
417 if(input_source_file_path[pos_on_string] == '.') {
418 if(dot_pos_of_file_path == NULL) {
419 end_pos_of_file_name = input_source_file_path + pos_on_string;
420 dot_pos_of_file_path = end_pos_of_file_name;
428 if(pos_on_string >= 0) {
429 start_pos_of_file_name = input_source_file_path + pos_on_string;
430 file_name_length = end_pos_of_file_name - start_pos_of_file_name;
431 memcpy(file_name_string, start_pos_of_file_name, file_name_length);
434 if(dot_pos_of_file_path != NULL) {
435 extention_length = (end_pos_of_file_path + 1) - (dot_pos_of_file_path + 1);
436 memcpy(extension_string, dot_pos_of_file_path + 1, extention_length);
439 EM_DEBUG_LOG("*file_name_string [%s] pos_on_string [%d]", file_name_string, pos_on_string);
441 *output_file_name = EM_SAFE_STRDUP(file_name_string);
442 *output_extension = EM_SAFE_STRDUP(extension_string);
445 EM_DEBUG_FUNC_END("err = [%d]", err);
449 INTERNAL_FUNC char *em_get_extension_from_file_path(char *source_file_path, int *err_code)
451 EM_DEBUG_FUNC_BEGIN("source_file_path[%s]", source_file_path);
452 int err = EMAIL_ERROR_NONE, pos_on_string = 0;
453 char *extension = NULL;
455 if (!source_file_path) {
456 EM_DEBUG_EXCEPTION("Invalid Parameter");
457 err = EMAIL_ERROR_INVALID_PARAM;
461 pos_on_string = EM_SAFE_STRLEN(source_file_path) - 1;
463 while(pos_on_string > 0 && source_file_path[pos_on_string--] != '.') ;
465 if(pos_on_string > 0)
466 extension = source_file_path + pos_on_string + 2;
468 EM_DEBUG_LOG("*extension [%s] pos_on_string [%d]", extension, pos_on_string);
477 INTERNAL_FUNC int em_get_encoding_type_from_file_path(const char *input_file_path, char **output_encoding_type)
479 EM_DEBUG_FUNC_BEGIN("input_file_path[%d], output_encoding_type[%p]", input_file_path, output_encoding_type);
480 int err = EMAIL_ERROR_NONE;
481 int pos_of_filename = 0;
483 int enf_of_string = 0;
484 int result_string_length = 0;
485 char *filename = NULL;
486 char *result_encoding_type = NULL;
488 if (!input_file_path || !output_encoding_type) {
489 EM_DEBUG_EXCEPTION("Invalid Parameter");
490 err = EMAIL_ERROR_INVALID_PARAM;
494 enf_of_string = pos_of_filename = EM_SAFE_STRLEN(input_file_path);
496 while(pos_of_filename >= 0 && input_file_path[pos_of_filename--] != '/') {
497 if(input_file_path[pos_of_filename] == '.')
498 pos_of_dot = pos_of_filename;
501 if(pos_of_filename != 0)
502 pos_of_filename += 2;
504 filename = (char*)input_file_path + pos_of_filename;
506 if(pos_of_dot != 0 && pos_of_dot > pos_of_filename)
507 result_string_length = pos_of_dot - pos_of_filename;
509 result_string_length = enf_of_string - pos_of_filename;
511 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);
513 if( !(result_encoding_type = em_malloc(sizeof(char) * (result_string_length + 1))) ) {
514 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
515 err = EMAIL_ERROR_OUT_OF_MEMORY;
519 memcpy(result_encoding_type, input_file_path + pos_of_filename, result_string_length);
521 EM_DEBUG_LOG("*result_encoding_type [%s]", result_encoding_type);
523 *output_encoding_type = result_encoding_type;
526 EM_DEBUG_FUNC_END("err [%d]", err);
530 INTERNAL_FUNC int em_get_content_type_from_extension_string(const char *extension_string, int *err_code)
532 EM_DEBUG_FUNC_BEGIN("extension_string[%s]", extension_string);
533 int i = 0, err = EMAIL_ERROR_NONE, result_content_type = TYPEAPPLICATION;
534 char *image_extension[] = { "jpeg", "jpg", "png", "gif", "bmp", "pic", "agif", "tif", "wbmp" , "p7s", "p7m", NULL};
536 if (!extension_string) {
537 EM_DEBUG_EXCEPTION("Invalid Parameter");
538 err = EMAIL_ERROR_INVALID_PARAM;
542 while(image_extension[i]) {
543 EM_DEBUG_LOG("image_extension[%d] [%s]", i, image_extension[i]);
544 if(strcasecmp(image_extension[i], extension_string) == 0) {
560 result_content_type = TYPEIMAGE;
563 result_content_type = TYPEPKCS7_SIGN;
566 result_content_type = TYPEPKCS7_MIME;
576 return result_content_type;
579 #define EMAIL_ACCOUNT_RGEX "([a-z0-9!#$%&'*+/=?^_`{|}~-]+.)*[a-z0-9!#$%&'*+/=?^_`{|}~-]+"
580 #define EMAIL_DOMAIN_RGEX "([a-z0-9!#$%&'*+/=?^_`{|}~-]+.)+[a-z0-9!#$%&'*+/=?^_`{|}~-]+"
582 #define EMAIL_ADDR_RGEX "[[:space:]]*<"EMAIL_ACCOUNT_RGEX"@"EMAIL_DOMAIN_RGEX">[[:space:]]*"
583 #define EMAIL_ALIAS_RGEX "([[:space:]]*\"[^\"]*\")?"EMAIL_ADDR_RGEX
584 #define EMAIL_ALIAS_LIST_RGEX "^("EMAIL_ALIAS_RGEX"[;,])*"EMAIL_ALIAS_RGEX"[;,]?[[:space:]]*$"
586 #define EMAIL_ADDR_WITHOUT_BRACKET_RGEX "[[:space:]]*"EMAIL_ACCOUNT_RGEX"@"EMAIL_DOMAIN_RGEX"[[:space:]]*"
587 #define EMAIL_ALIAS_WITHOUT_BRACKET_RGEX "([[:space:]]*\"[^\"]*\")?"EMAIL_ADDR_WITHOUT_BRACKET_RGEX
588 #define EMAIL_ALIAS_LIST_WITHOUT_BRACKET_RGEX "("EMAIL_ALIAS_WITHOUT_BRACKET_RGEX"[;,])*"EMAIL_ADDR_WITHOUT_BRACKET_RGEX"[;,]?[[:space:]]*$"
590 INTERNAL_FUNC int em_verify_email_address(char *address, int without_bracket, int *err_code)
592 EM_DEBUG_FUNC_BEGIN("address[%s] without_bracket[%d]", address, without_bracket);
594 /* this following code verfies the email alias string using reg. exp. */
595 regex_t alias_list_regex = {0};
596 int ret = false, error = EMAIL_ERROR_NONE;
597 char *reg_rule = NULL;
599 if(!address || EM_SAFE_STRLEN(address) == 0) {
600 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
602 *err_code = EMAIL_ERROR_INVALID_PARAM;
607 reg_rule = EMAIL_ALIAS_LIST_WITHOUT_BRACKET_RGEX;
609 reg_rule = EMAIL_ALIAS_LIST_RGEX;
611 if (regcomp(&alias_list_regex, reg_rule, REG_ICASE | REG_EXTENDED)) {
612 EM_DEBUG_EXCEPTION("email alias regex unrecognized");
614 *err_code = EMAIL_ERROR_UNKNOWN;
618 int alias_len = EM_SAFE_STRLEN(address) + 1;
619 regmatch_t pmatch[alias_len];
621 bzero(pmatch, alias_len);
623 if (regexec(&alias_list_regex, address, alias_len, pmatch, 0) == REG_NOMATCH)
624 EM_DEBUG_LOG("failed :[%s]", address);
626 EM_DEBUG_LOG("success :[%s]", address);
630 regfree(&alias_list_regex);
635 EM_DEBUG_FUNC_END("ret [%d]", ret);
639 INTERNAL_FUNC int em_verify_email_address_of_mail_data(email_mail_data_t *mail_data, int without_bracket, int *err_code)
641 EM_DEBUG_FUNC_BEGIN("mail_data[%p] without_bracket[%d]", mail_data, without_bracket);
642 char *address_array[4] = { mail_data->full_address_from, mail_data->full_address_to, mail_data->full_address_cc, mail_data->full_address_bcc};
643 int ret = false, err = EMAIL_ERROR_NONE, i;
645 /* check for email_address validation */
646 for (i = 0; i < 4; i++) {
647 if (address_array[i] && address_array[i][0] != 0) {
648 if (!em_verify_email_address(address_array[i] , without_bracket, &err)) {
649 err = EMAIL_ERROR_INVALID_ADDRESS;
650 EM_DEBUG_EXCEPTION("Invalid Email Address [%d][%s]", i, address_array[i]);
657 EM_DEBUG_FUNC_END("ret [%d]", ret);
661 INTERNAL_FUNC int em_verify_email_address_of_mail_tbl(emstorage_mail_tbl_t *input_mail_tbl, int input_without_bracket)
663 EM_DEBUG_FUNC_BEGIN("input_mail_tbl[%p] input_without_bracket[%d]", input_mail_tbl, input_without_bracket);
664 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};
665 int err = EMAIL_ERROR_NONE, i;
667 /* check for email_address validation */
668 for (i = 0; i < 4; i++) {
669 if (address_array[i] && address_array[i][0] != 0) {
670 if (!em_verify_email_address(address_array[i] , input_without_bracket, &err)) {
671 err = EMAIL_ERROR_INVALID_ADDRESS;
672 EM_DEBUG_EXCEPTION("Invalid Email Address [%d][%s]", i, address_array[i]);
679 EM_DEBUG_FUNC_END("err [%d]", err);
683 INTERNAL_FUNC int em_find_tag_for_thread_view(char *subject, int *result)
685 EM_DEBUG_FUNC_BEGIN();
686 int error_code = EMAIL_ERROR_NONE;
687 char *copy_of_subject = NULL;
689 EM_IF_NULL_RETURN_VALUE(subject, EMAIL_ERROR_INVALID_PARAM);
690 EM_IF_NULL_RETURN_VALUE(result, EMAIL_ERROR_INVALID_PARAM);
694 copy_of_subject = EM_SAFE_STRDUP(subject);
696 if (copy_of_subject == NULL) {
697 EM_DEBUG_EXCEPTION("strdup is failed.");
701 em_upper_string(copy_of_subject);
702 EM_DEBUG_LOG("em_upper_string result : %s\n", copy_of_subject);
704 if (strstr(copy_of_subject, "RE:") == NULL) {
705 if (strstr(copy_of_subject, "FWD:") == NULL) {
706 if (strstr(copy_of_subject, "FW:") != NULL)
716 EM_SAFE_FREE(copy_of_subject);
718 EM_DEBUG_FUNC_END("result : %d", *result);
723 INTERNAL_FUNC int em_find_pos_stripped_subject_for_thread_view(char *subject, char *stripped_subject, int stripped_subject_buffer_size)
725 EM_DEBUG_FUNC_BEGIN("subject [%p] stripped_subject [%p] stripped_subject_buffer_size[%d]", subject, stripped_subject, stripped_subject_buffer_size);
726 int error_code = EMAIL_ERROR_NONE;
728 char *copy_of_subject = NULL, *curpos = NULL, *result;
730 EM_IF_NULL_RETURN_VALUE(subject, EMAIL_ERROR_INVALID_PARAM);
731 EM_IF_NULL_RETURN_VALUE(stripped_subject, EMAIL_ERROR_INVALID_PARAM);
733 copy_of_subject = EM_SAFE_STRDUP(subject);
735 if (copy_of_subject == NULL) {
736 EM_DEBUG_EXCEPTION("strdup is failed");
740 em_upper_string(copy_of_subject);
741 curpos = copy_of_subject;
743 EM_DEBUG_LOG("em_upper_string result : %s", copy_of_subject);
745 while ((result = strstr(curpos, "RE:")) != NULL) {
747 EM_DEBUG_LOG("RE result : %s", curpos);
750 while ((result = strstr(curpos, "FWD:")) != NULL) {
752 EM_DEBUG_LOG("FWD result : %s", curpos);
755 while ((result = strstr(curpos, "FW:")) != NULL) {
757 EM_DEBUG_LOG("FW result : %s", curpos);
760 while (curpos != NULL && *curpos == ' ') {
764 gap = curpos - copy_of_subject;
766 EM_SAFE_STRNCPY(stripped_subject, subject + gap, stripped_subject_buffer_size);
769 EM_SAFE_FREE(copy_of_subject);
771 if (error_code == EMAIL_ERROR_NONE && stripped_subject)
772 EM_DEBUG_LOG("result[%s]", stripped_subject);
774 EM_DEBUG_FUNC_END("error_code[%d]", error_code);
782 INTERNAL_FUNC int em_encode_base64(char *src, unsigned long src_len, char **enc, unsigned long* enc_len, int *err_code)
784 EM_DEBUG_FUNC_BEGIN();
786 unsigned char *content;
787 int ret = true, err = EMAIL_ERROR_NONE;
789 if (err_code != NULL) {
790 *err_code = EMAIL_ERROR_NONE;
793 content = rfc822_binary(src, src_len, enc_len);
796 *enc = (char *)content;
798 err = EMAIL_ERROR_UNKNOWN;
812 INTERNAL_FUNC int em_decode_base64(unsigned char *enc_text, unsigned long enc_len, char **dec_text, unsigned long* dec_len, int *err_code)
814 unsigned char *text = enc_text;
815 unsigned long size = enc_len;
816 unsigned char *content;
817 int ret = true, err = EMAIL_ERROR_NONE;
819 if (err_code != NULL) {
820 *err_code = EMAIL_ERROR_NONE;
823 EM_DEBUG_FUNC_BEGIN();
825 content = rfc822_base64(text, size, dec_len);
827 *dec_text = (char *)content;
830 err = EMAIL_ERROR_UNKNOWN;
840 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)
842 EM_DEBUG_FUNC_BEGIN();
843 emstorage_account_tbl_t *account_tbl_data = NULL;
845 int err= EMAIL_ERROR_NONE;
847 if (account_server_type == NULL ) {
848 EM_DEBUG_EXCEPTION("account_server_type is NULL");
849 err = EMAIL_ERROR_INVALID_PARAM;
854 if( !emstorage_get_account_by_id(account_id, WITHOUT_OPTION, &account_tbl_data, false, &err)) {
855 EM_DEBUG_EXCEPTION ("emstorage_get_account_by_id failed [%d] ", err);
860 if ( flag == false ) { /* sending server */
861 *account_server_type = account_tbl_data->outgoing_server_type;
862 } else if ( flag == true ) { /* receiving server */
863 *account_server_type = account_tbl_data->incoming_server_type;
869 if ( account_tbl_data != NULL ) {
870 emstorage_free_account(&account_tbl_data, 1, NULL);
872 if ( error != NULL ) {
880 #include <dbus/dbus.h>
882 #define ACTIVE_SYNC_HANDLE_INIT_VALUE (-1)
883 #define ACTIVE_SYNC_HANDLE_BOUNDARY (-100000000)
886 INTERNAL_FUNC int em_get_handle_for_activesync(int *handle, int *error)
888 EM_DEBUG_FUNC_BEGIN();
890 static int next_handle = 0;
892 int err = EMAIL_ERROR_NONE;
894 if ( handle == NULL ) {
895 EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed : handle is NULL");
896 err = EMAIL_ERROR_INVALID_PARAM;
900 if ( vconf_get_int(VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE, &next_handle) != 0 ) {
901 EM_DEBUG_EXCEPTION("vconf_get_int failed");
902 if ( next_handle != 0 ) {
903 err = EMAIL_ERROR_GCONF_FAILURE;
908 EM_DEBUG_LOG(">>>>>> VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE : get lastest handle[%d]", next_handle);
910 /* set the value of the handle for active sync */
912 if ( next_handle < ACTIVE_SYNC_HANDLE_BOUNDARY ) {
913 next_handle = ACTIVE_SYNC_HANDLE_INIT_VALUE;
915 if ( vconf_set_int(VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE, next_handle) != 0) {
916 EM_DEBUG_EXCEPTION("vconf_set_int failed");
917 err = EMAIL_ERROR_GCONF_FAILURE;
921 *handle = next_handle;
922 EM_DEBUG_LOG(">>>>>> return next handle[%d]", *handle);
925 if ( error != NULL ) {
932 INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNotiData *data)
934 EM_DEBUG_FUNC_BEGIN("subType [%d], data [%p]", subType, data);
936 DBusConnection *connection;
937 DBusMessage *signal = NULL;
941 dbus_error_init (&error);
942 connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
944 if(connection == NULL)
947 signal = dbus_message_new_signal("/User/Email/ActiveSync", EMAIL_ACTIVE_SYNC_NOTI, "email");
949 dbus_message_append_args(signal, DBUS_TYPE_INT32, &subType, DBUS_TYPE_INVALID);
951 case ACTIVE_SYNC_NOTI_SEND_MAIL:
952 EM_DEBUG_LOG("handle:[%d]", data->send_mail.handle);
953 EM_DEBUG_LOG("account_id:[%d]", data->send_mail.account_id);
954 EM_DEBUG_LOG("mail_id:[%d]", data->send_mail.mail_id);
956 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.handle), DBUS_TYPE_INVALID);
957 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.account_id), DBUS_TYPE_INVALID);
958 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail.mail_id), DBUS_TYPE_INVALID);
960 case ACTIVE_SYNC_NOTI_SEND_SAVED: /* publish a send notification to ASE (active sync engine) */
961 EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_SEND_SAVED]", subType);
963 case ACTIVE_SYNC_NOTI_SEND_REPORT:
964 EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_SEND_REPORT]", subType);
966 case ACTIVE_SYNC_NOTI_SYNC_HEADER:
967 EM_DEBUG_LOG("handle:[%d]", data->sync_header.handle);
968 EM_DEBUG_LOG("account_id:[%d]", data->sync_header.account_id);
969 EM_DEBUG_LOG("mailbox_id:[%d]", data->sync_header.mailbox_id);
971 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->sync_header.handle ), DBUS_TYPE_INVALID);
972 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->sync_header.account_id ), DBUS_TYPE_INVALID);
973 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->sync_header.mailbox_id ), DBUS_TYPE_INVALID);
975 case ACTIVE_SYNC_NOTI_DOWNLOAD_BODY: /* publish a download body notification to ASE */
976 EM_DEBUG_LOG("handle:[%d]", data->download_body.handle);
977 EM_DEBUG_LOG("account_id:[%d]", data->download_body.account_id);
978 EM_DEBUG_LOG("mail_id:[%d]", data->download_body.mail_id);
979 EM_DEBUG_LOG("with_attachment:[%d]", data->download_body.with_attachment);
981 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.handle ), DBUS_TYPE_INVALID);
982 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.account_id ), DBUS_TYPE_INVALID);
983 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.mail_id ), DBUS_TYPE_INVALID);
984 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_body.with_attachment ), DBUS_TYPE_INVALID);
986 case ACTIVE_SYNC_NOTI_DOWNLOAD_ATTACHMENT:
987 EM_DEBUG_LOG("handle:[%d]", data->download_attachment.handle);
988 EM_DEBUG_LOG("account_id:[%d]", data->download_attachment.account_id );
989 EM_DEBUG_LOG("mail_id:[%d]", data->download_attachment.mail_id);
990 EM_DEBUG_LOG("with_attachment:[%d]", data->download_attachment.attachment_order );
992 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.handle ), DBUS_TYPE_INVALID);
993 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.account_id ), DBUS_TYPE_INVALID);
994 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.mail_id ), DBUS_TYPE_INVALID);
995 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->download_attachment.attachment_order), DBUS_TYPE_INVALID);
997 case ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT:
998 EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT]", subType);
1000 case ACTIVE_SYNC_NOTI_CANCEL_JOB:
1001 EM_DEBUG_LOG("account_id:[%d]", data->cancel_job.account_id );
1002 EM_DEBUG_LOG("handle to cancel:[%d]", data->cancel_job.handle);
1003 EM_DEBUG_LOG("cancel_type:[%d]", data->cancel_job.cancel_type);
1005 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_job.account_id ), DBUS_TYPE_INVALID);
1006 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_job.handle ), DBUS_TYPE_INVALID);
1007 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_job.cancel_type ), DBUS_TYPE_INVALID);
1009 case ACTIVE_SYNC_NOTI_SEARCH_ON_SERVER:
1010 EM_DEBUG_LOG("account_id:[%d]", data->search_mail_on_server.account_id );
1011 EM_DEBUG_LOG("mailbox_id:[%d]", data->search_mail_on_server.mailbox_id );
1012 EM_DEBUG_LOG("search_filter_count:[%d]", data->search_mail_on_server.search_filter_count );
1013 EM_DEBUG_LOG("handle to cancel:[%d]", data->search_mail_on_server.handle);
1015 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.account_id), DBUS_TYPE_INVALID);
1016 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.mailbox_id), DBUS_TYPE_INVALID);
1017 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.search_filter_count), DBUS_TYPE_INVALID);
1018 for(i = 0; i < data->search_mail_on_server.search_filter_count; i++) {
1019 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.search_filter_list[i].search_filter_type), DBUS_TYPE_INVALID);
1020 switch(data->search_mail_on_server.search_filter_list[i].search_filter_type) {
1021 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_NO :
1022 case EMAIL_SEARCH_FILTER_TYPE_UID :
1023 case EMAIL_SEARCH_FILTER_TYPE_SIZE_LARSER :
1024 case EMAIL_SEARCH_FILTER_TYPE_SIZE_SMALLER :
1025 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_ANSWERED :
1026 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DELETED :
1027 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_DRAFT :
1028 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_FLAGED :
1029 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_RECENT :
1030 case EMAIL_SEARCH_FILTER_TYPE_FLAGS_SEEN :
1031 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);
1034 case EMAIL_SEARCH_FILTER_TYPE_BCC :
1035 case EMAIL_SEARCH_FILTER_TYPE_CC :
1036 case EMAIL_SEARCH_FILTER_TYPE_FROM :
1037 case EMAIL_SEARCH_FILTER_TYPE_KEYWORD :
1038 case EMAIL_SEARCH_FILTER_TYPE_SUBJECT :
1039 case EMAIL_SEARCH_FILTER_TYPE_TO :
1040 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID :
1041 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);
1044 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_BEFORE :
1045 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_ON :
1046 case EMAIL_SEARCH_FILTER_TYPE_SENT_DATE_SINCE :
1047 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);
1050 EM_DEBUG_EXCEPTION("Invalid filter type [%d]", data->search_mail_on_server.search_filter_list[i].search_filter_type);
1054 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.handle), DBUS_TYPE_INVALID);
1057 case ACTIVE_SYNC_NOTI_CLEAR_RESULT_OF_SEARCH_ON_SERVER :
1058 EM_DEBUG_LOG("account_id:[%d]", data->clear_result_of_search_mail_on_server.account_id );
1059 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->search_mail_on_server.account_id), DBUS_TYPE_INVALID);
1062 case ACTIVE_SYNC_NOTI_EXPUNGE_MAILS_DELETED_FLAGGED :
1063 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->expunge_mails_deleted_flagged.account_id), DBUS_TYPE_INVALID);
1064 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->expunge_mails_deleted_flagged.mailbox_id), DBUS_TYPE_INVALID);
1065 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->expunge_mails_deleted_flagged.on_server), DBUS_TYPE_INVALID);
1066 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->expunge_mails_deleted_flagged.handle), DBUS_TYPE_INVALID);
1069 case ACTIVE_SYNC_NOTI_RESOLVE_RECIPIENT :
1070 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->get_resolve_recipients.account_id), DBUS_TYPE_INVALID);
1071 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->get_resolve_recipients.email_address), DBUS_TYPE_INVALID);
1072 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->get_resolve_recipients.handle), DBUS_TYPE_INVALID);
1075 case ACTIVE_SYNC_NOTI_VALIDATE_CERTIFICATE :
1076 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->validate_certificate.account_id), DBUS_TYPE_INVALID);
1077 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->validate_certificate.email_address), DBUS_TYPE_INVALID);
1078 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->validate_certificate.handle), DBUS_TYPE_INVALID);
1081 case ACTIVE_SYNC_NOTI_ADD_MAILBOX :
1082 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->add_mailbox.account_id), DBUS_TYPE_INVALID);
1083 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->add_mailbox.mailbox_path), DBUS_TYPE_INVALID);
1084 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->add_mailbox.mailbox_alias), DBUS_TYPE_INVALID);
1085 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->add_mailbox.handle), DBUS_TYPE_INVALID);
1088 case ACTIVE_SYNC_NOTI_RENAME_MAILBOX :
1089 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->rename_mailbox.account_id), DBUS_TYPE_INVALID);
1090 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->rename_mailbox.mailbox_id), DBUS_TYPE_INVALID);
1091 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->rename_mailbox.mailbox_name), DBUS_TYPE_INVALID);
1092 dbus_message_append_args(signal, DBUS_TYPE_STRING, &(data->rename_mailbox.mailbox_alias), DBUS_TYPE_INVALID);
1093 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->rename_mailbox.handle), DBUS_TYPE_INVALID);
1096 case ACTIVE_SYNC_NOTI_DELETE_MAILBOX :
1097 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->delete_mailbox.account_id), DBUS_TYPE_INVALID);
1098 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->delete_mailbox.mailbox_id), DBUS_TYPE_INVALID);
1099 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->delete_mailbox.handle), DBUS_TYPE_INVALID);
1102 case ACTIVE_SYNC_NOTI_DELETE_MAILBOX_EX :
1103 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->delete_mailbox_ex.account_id), DBUS_TYPE_INVALID);
1104 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->delete_mailbox_ex.mailbox_id_count), DBUS_TYPE_INVALID);
1105 for(i = 0; i <data->delete_mailbox_ex.mailbox_id_count; i++)
1106 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->delete_mailbox_ex.mailbox_id_array[i]), DBUS_TYPE_INVALID);
1107 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->delete_mailbox_ex.handle), DBUS_TYPE_INVALID);
1110 case ACTIVE_SYNC_NOTI_SEND_MAIL_WITH_DOWNLOADING_OF_ORIGINAL_MAIL:
1111 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail_with_downloading_attachment_of_original_mail.handle), DBUS_TYPE_INVALID);
1112 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail_with_downloading_attachment_of_original_mail.mail_id), DBUS_TYPE_INVALID);
1113 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->send_mail_with_downloading_attachment_of_original_mail.account_id), DBUS_TYPE_INVALID);
1116 case ACTIVE_SYNC_NOTI_SCHEDULE_SENDING_MAIL:
1117 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->schedule_sending_mail.handle), DBUS_TYPE_INVALID);
1118 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->schedule_sending_mail.account_id), DBUS_TYPE_INVALID);
1119 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->schedule_sending_mail.mail_id), DBUS_TYPE_INVALID);
1120 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->schedule_sending_mail.scheduled_time), DBUS_TYPE_INVALID);
1123 case ACTIVE_SYNC_NOTI_CANCEL_SENDING_MAIL:
1124 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_sending_mail.handle), DBUS_TYPE_INVALID);
1125 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_sending_mail.account_id ), DBUS_TYPE_INVALID);
1126 dbus_message_append_args(signal, DBUS_TYPE_INT32, &(data->cancel_sending_mail.mail_id), DBUS_TYPE_INVALID);
1130 EM_DEBUG_EXCEPTION("Invalid Notification type of Active Sync : subType[%d]", subType);
1134 if(!dbus_connection_send (connection, signal, NULL)) {
1135 EM_DEBUG_EXCEPTION("dbus_connection_send is failed");
1138 EM_DEBUG_LOG("dbus_connection_send is successful");
1140 dbus_connection_flush(connection);
1145 dbus_message_unref(signal);
1147 EM_DEBUG_FUNC_END();