apply removal of mailbox_name field
[platform/core/messaging/email-service.git] / email-common-use / email-utilities.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
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
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
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.
19 *
20 */
21
22 /*
23  * email-utilities.c
24  *
25  *  Created on: 2012. 3. 6.
26  *      Author: kyuho.jo@samsung.com
27  */
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <ctype.h>
32 #include <errno.h>
33 #include <sys/time.h>
34 #include <sys/vfs.h>
35 #include <unistd.h>
36 #include <malloc.h>
37 #include <pthread.h>
38 #include <regex.h>
39
40 #include "c-client.h"
41
42 #include "email-types.h"
43 #include "email-internal-types.h"
44 #include "email-utilities.h"
45
46 INTERNAL_FUNC void* em_malloc(int len)
47 {
48         /* EM_DEBUG_LOG("Memory allocation size[%d] bytes", len); */
49         if (len <= 0) {
50                 EM_DEBUG_EXCEPTION("len should be positive.[%d]", len);
51                 return NULL;
52         }
53
54         void *p = calloc(1,len);
55         if (!p)
56                 EM_DEBUG_PERROR("malloc failed");
57
58         return p;
59 }
60
61
62 INTERNAL_FUNC void* em_memdup(void* src, int len)
63 {
64         /* EM_DEBUG_LOG("Memory allocation size[%d] bytes", len); */
65         if (len <= 0) {
66                 EM_DEBUG_EXCEPTION("len should be positive.[%d]", len);
67                 return NULL;
68         }
69
70         void *p = calloc(1,len);
71         if (!p) {
72                 EM_DEBUG_EXCEPTION("malloc failed");
73                 return NULL;
74         }
75
76         memcpy(p, src, len);
77
78         return p;
79 }
80
81
82 /*  remove left space, tab, CR, L */
83 INTERNAL_FUNC char *em_trim_left(char *str)
84 {
85         char *p, *temp_buffer = NULL;
86
87         /* EM_DEBUG_FUNC_BEGIN() */
88         if (!str) return NULL;
89
90         p = str;
91         while (*p && (*p == ' ' || *p == '\t' || *p == LF || *p == CR)) p++;
92
93         if (!*p) return NULL;
94
95         temp_buffer = EM_SAFE_STRDUP(p);
96
97         strncpy(str, temp_buffer, EM_SAFE_STRLEN(str));
98         str[EM_SAFE_STRLEN(temp_buffer)] = NULL_CHAR;
99
100         EM_SAFE_FREE(temp_buffer);
101
102         return str;
103 }
104
105 /*  remove right space, tab, CR, L */
106 INTERNAL_FUNC char *em_trim_right(char *str)
107 {
108         char *p;
109
110         /* EM_DEBUG_FUNC_BEGIN() */
111         if (!str) return NULL;
112
113         p = str+EM_SAFE_STRLEN(str)-1;
114         while (((int)p >= (int)str) && (*p == ' ' || *p == '\t' || *p == LF || *p == CR))
115                 *p --= '\0';
116
117         if ((int) p < (int)str)
118                 return NULL;
119
120         return str;
121 }
122
123 INTERNAL_FUNC char* em_upper_string(char *str)
124 {
125         char *p = str;
126         while (*p)  {
127                 *p = toupper(*p);
128                 p++;
129         }
130         return str;
131 }
132
133 INTERNAL_FUNC char*  em_lower_string(char *str)
134 {
135         char *p = str;
136         while (*p)  {
137                 *p = tolower(*p);
138                 p++;
139         }
140         return str;
141 }
142
143 INTERNAL_FUNC int em_upper_path(char *path)
144 {
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) {
148                         is_utf7 = 1;
149                 }
150                 else {
151                         if (is_utf7) {
152                                 if (path[i] == '-') is_utf7 = 0;
153                         }
154                         else {
155                                 path[i] = toupper(path[i]);
156                         }
157                 }
158         }
159
160         return 1;
161 }
162
163 INTERNAL_FUNC void em_skip_whitespace(char *addr_str, char **pAddr)
164 {
165         EM_DEBUG_FUNC_BEGIN("addr_str[%p]", addr_str);
166
167         if (!addr_str)
168                 return ;
169         char *str = addr_str;
170         char ptr[EM_SAFE_STRLEN(str)+1]  ;
171         int i, j = 0;
172
173         str = addr_str ;
174         for (i = 0; str[i] != NULL_CHAR ; i++) {
175                 if (str[i] != SPACE && str[i] != TAB && str[i] != CR && str[i] != LF)
176                         ptr[j++] = str[i];
177         }
178         ptr[j] = NULL_CHAR;
179
180         *pAddr = EM_SAFE_STRDUP(ptr);
181         EM_DEBUG_FUNC_END("ptr[%s]", ptr);
182 }
183
184 INTERNAL_FUNC char* em_skip_whitespace_without_strdup(char *source_string)
185 {
186         EM_DEBUG_FUNC_BEGIN("source_string[%p]", source_string);
187
188         if (!source_string)
189                 return NULL;
190         int i;
191
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) */
194                         break;
195         }
196
197         EM_DEBUG_FUNC_END("i[%d]", i);
198         return source_string + i;
199 }
200
201 INTERNAL_FUNC char* em_replace_all_string(char *source_string, char *old_string, char *new_string)
202 {
203         EM_DEBUG_FUNC_BEGIN();
204         char *result_buffer = NULL;
205         int i = 0, j = 0;
206         int old_str_length = 0;
207         int new_str_length = 0;
208         int realloc_len = 0;
209
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);
213
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);
217
218         if (src_len <= 0) {
219                 EM_DEBUG_LOG("source_string is invalid");
220                 return NULL;
221         }
222
223         result_buffer = calloc(src_len+1, sizeof(char));
224         if (!result_buffer) {
225                 EM_DEBUG_EXCEPTION("calloc failed");
226                 return NULL;
227         }
228         realloc_len = src_len + 1;
229
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) {
233
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");
239                                         return NULL;
240                                 }
241                         }
242                         memcpy(&result_buffer[j], new_string, new_str_length);
243                         i += old_str_length;
244                         j += new_str_length;
245                 } else {
246                         result_buffer[j] = source_string[i];
247                         i++;
248                         j++;
249                 }
250         }
251
252         if (j < realloc_len)
253                 result_buffer[j] = '\0';
254         else
255                 result_buffer[realloc_len-1] = '\0';
256
257         EM_DEBUG_FUNC_END("result_buffer : %s", result_buffer);
258         return result_buffer;
259 }
260
261 INTERNAL_FUNC char* em_replace_string(char *source_string, char *old_string, char *new_string)
262 {
263         EM_DEBUG_FUNC_BEGIN();
264         char *result_buffer = NULL;
265         char *p = NULL;
266         int   buffer_length = 0;
267
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);
271
272         p = strstr(source_string, old_string);
273
274         if (p == NULL) {
275                 EM_DEBUG_EXCEPTION("old_string not found in source_string");
276                 EM_DEBUG_FUNC_END("return NULL");
277                 return NULL;
278         }
279
280         buffer_length   = EM_SAFE_STRLEN(source_string) + 1024;
281         result_buffer  = (char *)em_malloc(buffer_length);
282
283         if (!result_buffer) {
284                 EM_DEBUG_EXCEPTION("em_malloc failed");
285                 return NULL;
286         }
287
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*/
290
291         EM_DEBUG_FUNC_END("result_buffer[%p]", result_buffer);
292         return result_buffer;
293 }
294
295 /* Memory clean up */
296 #include <sys/mman.h>
297
298 /* #define GETSP()                              ({ unsigned int sp; asm volatile ("mov %0, sp " : "=r"(sp)); sp;}) */
299 #define BUF_SIZE                                256
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)
304
305 int stack_trim(void)
306 {
307         /*
308         char buf[BUF_SIZE];
309         FILE *file;
310         unsigned int stacktop;
311         int found = 0;
312         unsigned int sp;
313
314         asm volatile ("mov %0, sp " : "=r"(sp));
315
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]")) {
320                         found = 1;
321                         break;
322                 }
323         }
324
325         fclose(file);
326
327         if (found) {
328                 sscanf(buf, "%x-", &stacktop);
329                 if (madvise((void *)PAGE_ALIGN(stacktop), PAGE_ALIGN(sp)-stacktop, MADV_DONTNEED) < 0)
330                         perror("stack madvise fail");
331         }
332         */
333         return 1;
334 }
335
336 INTERNAL_FUNC void em_flush_memory()
337 {
338         EM_DEBUG_FUNC_BEGIN();
339         /*  flush memory in heap */
340         malloc_trim(0);
341
342         /*  flush memory in stack */
343         stack_trim();
344
345         /*  flush memory for sqlite */
346         emstorage_flush_db_cache();
347         EM_DEBUG_FUNC_END();
348 }
349
350 INTERNAL_FUNC int em_get_file_name_from_file_path(char *input_source_file_path, char **output_file_name)
351 {
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, };
360
361         if (!input_source_file_path || !output_file_name) {
362                 EM_DEBUG_EXCEPTION("Invalid Parameter");
363                 err  = EMAIL_ERROR_INVALID_PARAM;
364                 goto FINISH_OFF;
365         }
366
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;
370
371         while(pos_on_string >= 0 && input_source_file_path[pos_on_string] != '/') {
372                 pos_on_string--;
373         }
374
375         pos_on_string++;
376
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);
381         }
382
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);
384
385         *output_file_name = EM_SAFE_STRDUP(file_name_string);
386
387 FINISH_OFF:
388         EM_DEBUG_FUNC_END("err = [%d]", err);
389         return err;
390 }
391
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)
393 {
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, };
405
406         if (!input_source_file_path || !output_file_name || !output_extension) {
407                 EM_DEBUG_EXCEPTION("Invalid Parameter");
408                 err  = EMAIL_ERROR_INVALID_PARAM;
409                 goto FINISH_OFF;
410         }
411
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;
415
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;
421                         }
422                 }
423                 pos_on_string--;
424         }
425
426         pos_on_string++;
427
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);
432         }
433
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);
437         }
438
439         EM_DEBUG_LOG("*file_name_string [%s] pos_on_string [%d]", file_name_string, pos_on_string);
440
441         *output_file_name = EM_SAFE_STRDUP(file_name_string);
442         *output_extension = EM_SAFE_STRDUP(extension_string);
443
444 FINISH_OFF:
445         EM_DEBUG_FUNC_END("err = [%d]", err);
446         return err;
447 }
448
449 INTERNAL_FUNC char *em_get_extension_from_file_path(char *source_file_path, int *err_code)
450 {
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;
454
455         if (!source_file_path) {
456                 EM_DEBUG_EXCEPTION("Invalid Parameter");
457                 err  = EMAIL_ERROR_INVALID_PARAM;
458                 goto FINISH_OFF;
459         }
460
461         pos_on_string = EM_SAFE_STRLEN(source_file_path) - 1;
462
463         while(pos_on_string > 0 && source_file_path[pos_on_string--] != '.') ;
464
465         if(pos_on_string > 0)
466                 extension = source_file_path + pos_on_string + 2;
467
468         EM_DEBUG_LOG("*extension [%s] pos_on_string [%d]", extension, pos_on_string);
469
470 FINISH_OFF:
471         if (err_code)
472                 *err_code = err;
473         EM_DEBUG_FUNC_END();
474         return extension;
475 }
476
477 INTERNAL_FUNC int em_get_encoding_type_from_file_path(const char *input_file_path, char **output_encoding_type)
478 {
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;
482         int   pos_of_dot = 0;
483         int   enf_of_string = 0;
484         int   result_string_length = 0;
485         char *filename = NULL;
486         char *result_encoding_type = NULL;
487
488         if (!input_file_path || !output_encoding_type) {
489                 EM_DEBUG_EXCEPTION("Invalid Parameter");
490                 err  = EMAIL_ERROR_INVALID_PARAM;
491                 goto FINISH_OFF;
492         }
493
494         enf_of_string = pos_of_filename = EM_SAFE_STRLEN(input_file_path);
495
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;
499         }
500
501         if(pos_of_filename != 0)
502                 pos_of_filename += 2;
503
504         filename = (char*)input_file_path + pos_of_filename;
505
506         if(pos_of_dot != 0 && pos_of_dot > pos_of_filename)
507                 result_string_length = pos_of_dot - pos_of_filename;
508         else
509                 result_string_length = enf_of_string - pos_of_filename;
510
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);
512
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;
516                 goto FINISH_OFF;
517         }
518
519         memcpy(result_encoding_type, input_file_path + pos_of_filename, result_string_length);
520
521         EM_DEBUG_LOG("*result_encoding_type [%s]", result_encoding_type);
522
523         *output_encoding_type = result_encoding_type;
524
525 FINISH_OFF:
526         EM_DEBUG_FUNC_END("err [%d]", err);
527         return err;
528 }
529
530 INTERNAL_FUNC int em_get_content_type_from_extension_string(const char *extension_string, int *err_code)
531 {
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};
535
536         if (!extension_string) {
537                 EM_DEBUG_EXCEPTION("Invalid Parameter");
538                 err  = EMAIL_ERROR_INVALID_PARAM;
539                 goto FINISH_OFF;
540         }
541
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) {
545                         break;
546                 }
547                 i++;
548         }
549
550         switch (i) {
551         case EXTENSION_JPEG:
552         case EXTENSION_JPG:
553         case EXTENSION_PNG:
554         case EXTENSION_GIF:
555         case EXTENSION_BMP:
556         case EXTENSION_PIC:
557         case EXTENSION_AGIF:
558         case EXTENSION_TIF:
559         case EXTENSION_WBMP:
560                 result_content_type = TYPEIMAGE;
561                 break;
562         case EXTENSION_P7S:
563                 result_content_type = TYPEPKCS7_SIGN;
564                 break;
565         case EXTENSION_P7M:
566                 result_content_type = TYPEPKCS7_MIME;
567                 break;
568         default:
569                 break;
570         }
571
572 FINISH_OFF:
573         if (err_code)
574                 *err_code = err;
575         EM_DEBUG_FUNC_END();
576         return result_content_type;
577 }
578
579 #define EMAIL_ACCOUNT_RGEX                     "([a-z0-9!#$%&'*+/=?^_`{|}~-]+.)*[a-z0-9!#$%&'*+/=?^_`{|}~-]+"
580 #define EMAIL_DOMAIN_RGEX                      "([a-z0-9!#$%&'*+/=?^_`{|}~-]+.)+[a-z0-9!#$%&'*+/=?^_`{|}~-]+"
581
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:]]*$"
585
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:]]*$"
589
590 INTERNAL_FUNC int em_verify_email_address(char *address, int without_bracket, int *err_code)
591 {
592         EM_DEBUG_FUNC_BEGIN("address[%s] without_bracket[%d]", address, without_bracket);
593
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;
598
599         if(!address || EM_SAFE_STRLEN(address) == 0) {
600                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
601                 if (err_code)
602                         *err_code = EMAIL_ERROR_INVALID_PARAM;
603                 return false;
604         }
605
606         if(without_bracket)
607                 reg_rule = EMAIL_ALIAS_LIST_WITHOUT_BRACKET_RGEX;
608         else
609                 reg_rule = EMAIL_ALIAS_LIST_RGEX;
610
611         if (regcomp(&alias_list_regex, reg_rule, REG_ICASE | REG_EXTENDED)) {
612                 EM_DEBUG_EXCEPTION("email alias regex unrecognized");
613                 if (err_code)
614                         *err_code = EMAIL_ERROR_UNKNOWN;
615                 return false;
616         }
617
618         int alias_len = EM_SAFE_STRLEN(address) + 1;
619         regmatch_t pmatch[alias_len];
620
621         bzero(pmatch, alias_len);
622
623         if (regexec(&alias_list_regex, address, alias_len, pmatch, 0) == REG_NOMATCH)
624                 EM_DEBUG_LOG("failed :[%s]", address);
625         else {
626                 EM_DEBUG_LOG("success :[%s]", address);
627                 ret = true;
628         }
629
630         regfree(&alias_list_regex);
631
632         if (err_code)
633                 *err_code = error;
634
635         EM_DEBUG_FUNC_END("ret [%d]", ret);
636         return ret;
637 }
638
639 INTERNAL_FUNC int em_verify_email_address_of_mail_data(email_mail_data_t *mail_data, int without_bracket, int *err_code)
640 {
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;
644
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]);
651                                 goto FINISH_OFF;
652                         }
653                 }
654         }
655         ret = true;
656 FINISH_OFF:
657         EM_DEBUG_FUNC_END("ret [%d]", ret);
658         return ret;
659 }
660
661 INTERNAL_FUNC int em_verify_email_address_of_mail_tbl(emstorage_mail_tbl_t *input_mail_tbl, int input_without_bracket)
662 {
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;
666
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]);
673                                 goto FINISH_OFF;
674                         }
675                 }
676         }
677
678 FINISH_OFF:
679         EM_DEBUG_FUNC_END("err [%d]", err);
680         return err;
681 }
682
683 INTERNAL_FUNC int em_find_tag_for_thread_view(char *subject, int *result)
684 {
685         EM_DEBUG_FUNC_BEGIN();
686         int error_code = EMAIL_ERROR_NONE;
687         char *copy_of_subject = NULL;
688
689         EM_IF_NULL_RETURN_VALUE(subject, EMAIL_ERROR_INVALID_PARAM);
690         EM_IF_NULL_RETURN_VALUE(result, EMAIL_ERROR_INVALID_PARAM);
691
692         *result = FALSE;
693
694         copy_of_subject = EM_SAFE_STRDUP(subject);
695
696         if (copy_of_subject == NULL) {
697                 EM_DEBUG_EXCEPTION("strdup is failed.");
698                 goto FINISH_OFF;
699         }
700
701         em_upper_string(copy_of_subject);
702         EM_DEBUG_LOG("em_upper_string result : %s\n", copy_of_subject);
703
704         if (strstr(copy_of_subject, "RE:") == NULL) {
705                 if (strstr(copy_of_subject, "FWD:") == NULL) {
706                         if (strstr(copy_of_subject, "FW:") != NULL)
707                                 *result = TRUE;
708                 }
709                 else
710                         *result = TRUE;
711         }
712         else
713                 *result = TRUE;
714
715 FINISH_OFF:
716         EM_SAFE_FREE(copy_of_subject);
717
718         EM_DEBUG_FUNC_END("result : %d", *result);
719
720         return error_code;
721 }
722
723 INTERNAL_FUNC int em_find_pos_stripped_subject_for_thread_view(char *subject, char *stripped_subject, int stripped_subject_buffer_size)
724 {
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;
727         int gap;
728         char *copy_of_subject = NULL, *curpos = NULL, *result;
729
730         EM_IF_NULL_RETURN_VALUE(subject, EMAIL_ERROR_INVALID_PARAM);
731         EM_IF_NULL_RETURN_VALUE(stripped_subject, EMAIL_ERROR_INVALID_PARAM);
732
733         copy_of_subject = EM_SAFE_STRDUP(subject);
734
735         if (copy_of_subject == NULL) {
736                 EM_DEBUG_EXCEPTION("strdup is failed");
737                 goto FINISH_OFF;
738         }
739
740         em_upper_string(copy_of_subject);
741         curpos = copy_of_subject;
742
743         EM_DEBUG_LOG("em_upper_string result : %s", copy_of_subject);
744
745         while ((result = strstr(curpos, "RE:")) != NULL) {
746                 curpos = result + 3;
747                 EM_DEBUG_LOG("RE result : %s", curpos);
748         }
749
750         while ((result = strstr(curpos, "FWD:")) != NULL) {
751                 curpos = result + 4;
752                 EM_DEBUG_LOG("FWD result : %s", curpos);
753         }
754
755         while ((result = strstr(curpos, "FW:")) != NULL) {
756                 curpos = result + 3;
757                 EM_DEBUG_LOG("FW result : %s", curpos);
758         }
759
760         while (curpos != NULL && *curpos == ' ') {
761                 curpos++;
762         }
763
764         gap = curpos - copy_of_subject;
765
766         EM_SAFE_STRNCPY(stripped_subject, subject + gap, stripped_subject_buffer_size);
767
768 FINISH_OFF:
769         EM_SAFE_FREE(copy_of_subject);
770
771         if (error_code == EMAIL_ERROR_NONE && stripped_subject)
772                 EM_DEBUG_LOG("result[%s]", stripped_subject);
773
774         EM_DEBUG_FUNC_END("error_code[%d]", error_code);
775         return error_code;
776 }
777
778
779 /*
780  * encoding base64
781  */
782 INTERNAL_FUNC int em_encode_base64(char *src, unsigned long src_len, char **enc, unsigned long* enc_len, int *err_code)
783 {
784         EM_DEBUG_FUNC_BEGIN();
785
786     unsigned char *content;
787     int ret = true, err = EMAIL_ERROR_NONE;
788
789         if (err_code != NULL) {
790                 *err_code = EMAIL_ERROR_NONE;
791         }
792
793     content = rfc822_binary(src, src_len, enc_len);
794
795     if (content)
796         *enc = (char *)content;
797     else {
798         err = EMAIL_ERROR_UNKNOWN;
799         ret = false;
800     }
801
802     if (err_code)
803         *err_code = err;
804
805         EM_DEBUG_FUNC_END();
806     return ret;
807 }
808
809 /*
810  * decoding base64
811  */
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)
813 {
814     unsigned char *text = enc_text;
815     unsigned long size = enc_len;
816     unsigned char *content;
817     int ret = true, err = EMAIL_ERROR_NONE;
818
819         if (err_code != NULL) {
820                 *err_code = EMAIL_ERROR_NONE;
821         }
822
823     EM_DEBUG_FUNC_BEGIN();
824
825     content = rfc822_base64(text, size, dec_len);
826     if (content)
827         *dec_text = (char *)content;
828     else
829     {
830         err = EMAIL_ERROR_UNKNOWN;
831         ret = false;
832     }
833
834     if (err_code)
835         *err_code = err;
836
837     return ret;
838 }
839
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)
841 {
842         EM_DEBUG_FUNC_BEGIN();
843         emstorage_account_tbl_t *account_tbl_data = NULL;
844         int ret = false;
845         int err= EMAIL_ERROR_NONE;
846
847         if (account_server_type == NULL ) {
848                 EM_DEBUG_EXCEPTION("account_server_type is NULL");
849                 err = EMAIL_ERROR_INVALID_PARAM;
850                 ret = false;
851                 goto FINISH_OFF;
852         }
853
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);
856                 ret = false;
857                 goto FINISH_OFF;
858         }
859
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;
864         }
865
866         ret = true;
867
868 FINISH_OFF:
869         if ( account_tbl_data != NULL ) {
870                 emstorage_free_account(&account_tbl_data, 1, NULL);
871         }
872         if ( error != NULL ) {
873                 *error = err;
874         }
875
876         return ret;
877 }
878
879 #include <vconf.h>
880 #include <dbus/dbus.h>
881
882 #define ACTIVE_SYNC_HANDLE_INIT_VALUE           (-1)
883 #define ACTIVE_SYNC_HANDLE_BOUNDARY                     (-100000000)
884
885
886 INTERNAL_FUNC int em_get_handle_for_activesync(int *handle, int *error)
887 {
888         EM_DEBUG_FUNC_BEGIN();
889
890         static int next_handle = 0;
891         int ret = false;
892         int err = EMAIL_ERROR_NONE;
893
894         if ( handle == NULL ) {
895                 EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed : handle is NULL");
896                 err = EMAIL_ERROR_INVALID_PARAM;
897                 goto FINISH_OFF;
898         }
899
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;
904                         goto FINISH_OFF;
905                 }
906         }
907
908         EM_DEBUG_LOG(">>>>>> VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE : get lastest handle[%d]", next_handle);
909
910         /*  set the value of the handle for active sync */
911         next_handle--;
912         if ( next_handle < ACTIVE_SYNC_HANDLE_BOUNDARY ) {
913                 next_handle = ACTIVE_SYNC_HANDLE_INIT_VALUE;
914         }
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;
918                 goto FINISH_OFF;
919         }
920         ret = true;
921         *handle = next_handle;
922         EM_DEBUG_LOG(">>>>>> return next handle[%d]", *handle);
923
924 FINISH_OFF:
925         if ( error != NULL ) {
926                 *error = err;
927         }
928
929         return ret;
930 }
931
932 INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNotiData *data)
933 {
934         EM_DEBUG_FUNC_BEGIN("subType [%d], data [%p]", subType, data);
935
936         DBusConnection     *connection;
937         DBusMessage        *signal = NULL;
938         DBusError           error;
939         int                 i = 0;
940
941         dbus_error_init (&error);
942         connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
943
944         if(connection == NULL)
945                 goto FINISH_OFF;
946
947         signal = dbus_message_new_signal("/User/Email/ActiveSync", EMAIL_ACTIVE_SYNC_NOTI, "email");
948
949         dbus_message_append_args(signal, DBUS_TYPE_INT32, &subType, DBUS_TYPE_INVALID);
950         switch ( subType ) {
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);
955
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);
959                         break;
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);
962                         break;
963                 case ACTIVE_SYNC_NOTI_SEND_REPORT:
964                         EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_SEND_REPORT]", subType);
965                         break;
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);
970
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);
974                         break;
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);
980
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);
985                         break;
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 );
991
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);
996                         break;
997                 case ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT:
998                         EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT]", subType);
999                         break;
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);
1004
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);
1008                         break;
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);
1014
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);
1032                                                 break;
1033
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);
1042                                                 break;
1043
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);
1048                                                 break;
1049                                         default :
1050                                                 EM_DEBUG_EXCEPTION("Invalid filter type [%d]", data->search_mail_on_server.search_filter_list[i].search_filter_type);
1051                                                 break;
1052                                 }
1053                         }
1054                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->search_mail_on_server.handle), DBUS_TYPE_INVALID);
1055                         break;
1056
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);
1060                         break;
1061
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);
1067                         break;
1068
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);
1073                         break;
1074
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);
1079                         break;
1080
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);
1086                         break;
1087
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);
1094                         break;
1095
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);
1100                         break;
1101
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);
1108                         break;
1109
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);
1114                         break;
1115
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);
1121                         break;
1122
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);
1127                         break;
1128
1129                 default:
1130                         EM_DEBUG_EXCEPTION("Invalid Notification type of Active Sync : subType[%d]", subType);
1131                         return FAILURE;
1132         }
1133
1134         if(!dbus_connection_send (connection, signal, NULL)) {
1135                 EM_DEBUG_EXCEPTION("dbus_connection_send is failed");
1136                 return FAILURE;
1137         } else
1138                 EM_DEBUG_LOG("dbus_connection_send is successful");
1139
1140         dbus_connection_flush(connection);
1141
1142 FINISH_OFF:
1143
1144         if(signal)
1145                 dbus_message_unref(signal);
1146
1147         EM_DEBUG_FUNC_END();
1148         return true;
1149 }