Tizen 2.0 Release
[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 /*refactoring required*/
202 INTERNAL_FUNC char* em_replace_all_string(char *source_string, char *old_string, char *new_string)
203 {
204         EM_DEBUG_FUNC_BEGIN();
205         char *result_buffer = NULL;
206         char *p = NULL;
207         int i = 0, count = 0;
208         int old_str_length = 0;
209         int new_str_length = 0;
210
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);
214
215         old_str_length = EM_SAFE_STRLEN(old_string);
216         new_str_length = EM_SAFE_STRLEN(new_string);
217
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) {
221                                 count++;
222                                 i += old_str_length;
223                         } else {
224                                 i++;
225                         }
226                 }
227         } else {
228                 i = EM_SAFE_STRLEN(source_string);
229         }
230
231         result_buffer = (char *)malloc(i + 1 + count*(new_str_length-old_str_length));
232         if (result_buffer == NULL)
233                 return NULL;
234
235         p = result_buffer;
236         while (*source_string) {
237                 if (memcmp(source_string, old_string, old_str_length) == 0) {
238                         memcpy(p, new_string, new_str_length);
239                         p += new_str_length;
240                         source_string += old_str_length;
241                 } else {
242                         *p++ = *source_string++;
243                 }
244         }
245         *p = '\0';
246
247         EM_DEBUG_FUNC_END("result_buffer : %s", result_buffer);
248         return result_buffer;
249 }
250
251 INTERNAL_FUNC char* em_replace_string(char *source_string, char *old_string, char *new_string)
252 {
253         EM_DEBUG_FUNC_BEGIN();
254         char *result_buffer = NULL;
255         char *p = NULL;
256         int   buffer_length = 0;
257
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);
261
262         p = strstr(source_string, old_string);
263
264         if (p == NULL) {
265                 EM_DEBUG_EXCEPTION("old_string not found in source_string");
266                 EM_DEBUG_FUNC_END("return NULL");
267                 return NULL;
268         }
269
270         buffer_length   = EM_SAFE_STRLEN(source_string) + 1024;
271         result_buffer  = (char *)em_malloc(buffer_length);
272
273         if (!result_buffer) {
274                 EM_DEBUG_EXCEPTION("em_malloc failed");
275                 return NULL;
276         }
277
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*/
280
281         EM_DEBUG_FUNC_END("result_buffer[%p]", result_buffer);
282         return result_buffer;
283 }
284
285 /* Memory clean up */
286 #include <sys/mman.h>
287
288 /* #define GETSP()                              ({ unsigned int sp; asm volatile ("mov %0, sp " : "=r"(sp)); sp;}) */
289 #define BUF_SIZE                                256
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)
294
295 int stack_trim(void)
296 {
297         /*
298         char buf[BUF_SIZE];
299         FILE *file;
300         unsigned int stacktop;
301         int found = 0;
302         unsigned int sp;
303
304         asm volatile ("mov %0, sp " : "=r"(sp));
305
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]")) {
310                         found = 1;
311                         break;
312                 }
313         }
314
315         fclose(file);
316
317         if (found) {
318                 sscanf(buf, "%x-", &stacktop);
319                 if (madvise((void *)PAGE_ALIGN(stacktop), PAGE_ALIGN(sp)-stacktop, MADV_DONTNEED) < 0)
320                         perror("stack madvise fail");
321         }
322         */
323         return 1;
324 }
325
326 INTERNAL_FUNC void em_flush_memory()
327 {
328         EM_DEBUG_FUNC_BEGIN();
329         /*  flush memory in heap */
330         malloc_trim(0);
331
332         /*  flush memory in stack */
333         stack_trim();
334
335         /*  flush memory for sqlite */
336         emstorage_flush_db_cache();
337         EM_DEBUG_FUNC_END();
338 }
339
340 INTERNAL_FUNC int em_get_file_name_from_file_path(char *input_source_file_path, char **output_file_name)
341 {
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, };
350
351         if (!input_source_file_path || !output_file_name) {
352                 EM_DEBUG_EXCEPTION("Invalid Parameter");
353                 err  = EMAIL_ERROR_INVALID_PARAM;
354                 goto FINISH_OFF;
355         }
356
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;
360
361         while(pos_on_string >= 0 && input_source_file_path[pos_on_string] != '/') {
362                 pos_on_string--;
363         }
364
365         pos_on_string++;
366
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);
371         }
372
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);
374
375         *output_file_name = EM_SAFE_STRDUP(file_name_string);
376
377 FINISH_OFF:
378         EM_DEBUG_FUNC_END("err = [%d]", err);
379         return err;
380 }
381
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)
383 {
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, };
395
396         if (!input_source_file_path || !output_file_name || !output_extension) {
397                 EM_DEBUG_EXCEPTION("Invalid Parameter");
398                 err  = EMAIL_ERROR_INVALID_PARAM;
399                 goto FINISH_OFF;
400         }
401
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;
405
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;
411                         }
412                 }
413                 pos_on_string--;
414         }
415
416         pos_on_string++;
417
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);
422         }
423
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);
427         }
428
429         EM_DEBUG_LOG("*file_name_string [%s] pos_on_string [%d]", file_name_string, pos_on_string);
430
431         *output_file_name = EM_SAFE_STRDUP(file_name_string);
432         *output_extension = EM_SAFE_STRDUP(extension_string);
433
434 FINISH_OFF:
435         EM_DEBUG_FUNC_END("err = [%d]", err);
436         return err;
437 }
438
439 INTERNAL_FUNC char *em_get_extension_from_file_path(char *source_file_path, int *err_code)
440 {
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;
444
445         if (!source_file_path) {
446                 EM_DEBUG_EXCEPTION("Invalid Parameter");
447                 err  = EMAIL_ERROR_INVALID_PARAM;
448                 goto FINISH_OFF;
449         }
450
451         pos_on_string = EM_SAFE_STRLEN(source_file_path) - 1;
452
453         while(pos_on_string > 0 && source_file_path[pos_on_string--] != '.') ;
454
455         if(pos_on_string > 0)
456                 extension = source_file_path + pos_on_string + 2;
457
458         EM_DEBUG_LOG("*extension [%s] pos_on_string [%d]", extension, pos_on_string);
459
460 FINISH_OFF:
461         if (err_code)
462                 *err_code = err;
463         EM_DEBUG_FUNC_END();
464         return extension;
465 }
466
467 INTERNAL_FUNC int em_get_encoding_type_from_file_path(const char *input_file_path, char **output_encoding_type)
468 {
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;
472         int   pos_of_dot = 0;
473         int   enf_of_string = 0;
474         int   result_string_length = 0;
475         char *filename = NULL;
476         char *result_encoding_type = NULL;
477
478         if (!input_file_path || !output_encoding_type) {
479                 EM_DEBUG_EXCEPTION("Invalid Parameter");
480                 err  = EMAIL_ERROR_INVALID_PARAM;
481                 goto FINISH_OFF;
482         }
483
484         enf_of_string = pos_of_filename = EM_SAFE_STRLEN(input_file_path);
485
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;
489         }
490
491         if(pos_of_filename != 0)
492                 pos_of_filename += 2;
493
494         filename = (char*)input_file_path + pos_of_filename;
495
496         if(pos_of_dot != 0 && pos_of_dot > pos_of_filename)
497                 result_string_length = pos_of_dot - pos_of_filename;
498         else
499                 result_string_length = enf_of_string - pos_of_filename;
500
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);
502
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;
506                 goto FINISH_OFF;
507         }
508
509         memcpy(result_encoding_type, input_file_path + pos_of_filename, result_string_length);
510
511         EM_DEBUG_LOG("*result_encoding_type [%s]", result_encoding_type);
512
513         *output_encoding_type = result_encoding_type;
514
515 FINISH_OFF:
516         EM_DEBUG_FUNC_END("err [%d]", err);
517         return err;
518 }
519
520 INTERNAL_FUNC int em_get_content_type_from_extension_string(const char *extension_string, int *err_code)
521 {
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};
525
526         if (!extension_string) {
527                 EM_DEBUG_EXCEPTION("Invalid Parameter");
528                 err  = EMAIL_ERROR_INVALID_PARAM;
529                 goto FINISH_OFF;
530         }
531
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) {
535                         break;
536                 }
537                 i++;
538         }
539
540         switch (i) {
541         case EXTENSION_JPEG:
542         case EXTENSION_JPG:
543         case EXTENSION_PNG:
544         case EXTENSION_GIF:
545         case EXTENSION_BMP:
546         case EXTENSION_PIC:
547         case EXTENSION_AGIF:
548         case EXTENSION_TIF:
549         case EXTENSION_WBMP:
550                 result_content_type = TYPEIMAGE;
551                 break;
552         case EXTENSION_P7S:
553                 result_content_type = TYPEPKCS7_SIGN;
554                 break;
555         case EXTENSION_P7M:
556                 result_content_type = TYPEPKCS7_MIME;
557                 break;
558         default:
559                 break;
560         }
561
562 FINISH_OFF:
563         if (err_code)
564                 *err_code = err;
565         EM_DEBUG_FUNC_END();
566         return result_content_type;
567 }
568
569 #define EMAIL_ACCOUNT_RGEX                     "([a-z0-9!#$%&'*+/=?^_`{|}~-]+.)*[a-z0-9!#$%&'*+/=?^_`{|}~-]+"
570 #define EMAIL_DOMAIN_RGEX                      "([a-z0-9!#$%&'*+/=?^_`{|}~-]+.)+[a-z0-9!#$%&'*+/=?^_`{|}~-]+"
571
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:]]*$"
575
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:]]*$"
579
580 INTERNAL_FUNC int em_verify_email_address(char *address, int without_bracket, int *err_code)
581 {
582         EM_DEBUG_FUNC_BEGIN("address[%s] without_bracket[%d]", address, without_bracket);
583
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;
588
589         if(!address || EM_SAFE_STRLEN(address) == 0) {
590                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
591                 if (err_code)
592                         *err_code = EMAIL_ERROR_INVALID_PARAM;
593                 return false;
594         }
595
596         if(without_bracket)
597                 reg_rule = EMAIL_ALIAS_LIST_WITHOUT_BRACKET_RGEX;
598         else
599                 reg_rule = EMAIL_ALIAS_LIST_RGEX;
600
601         if (regcomp(&alias_list_regex, reg_rule, REG_ICASE | REG_EXTENDED)) {
602                 EM_DEBUG_EXCEPTION("email alias regex unrecognized");
603                 if (err_code)
604                         *err_code = EMAIL_ERROR_UNKNOWN;
605                 return false;
606         }
607
608         int alias_len = EM_SAFE_STRLEN(address) + 1;
609         regmatch_t pmatch[alias_len];
610
611         bzero(pmatch, alias_len);
612
613         if (regexec(&alias_list_regex, address, alias_len, pmatch, 0) == REG_NOMATCH)
614                 EM_DEBUG_LOG("failed :[%s]", address);
615         else {
616                 EM_DEBUG_LOG("success :[%s]", address);
617                 ret = true;
618         }
619
620         regfree(&alias_list_regex);
621
622         if (err_code)
623                 *err_code = error;
624
625         EM_DEBUG_FUNC_END("ret [%d]", ret);
626         return ret;
627 }
628
629 INTERNAL_FUNC int em_verify_email_address_of_mail_data(email_mail_data_t *mail_data, int without_bracket, int *err_code)
630 {
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;
634
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]);
641                                 goto FINISH_OFF;
642                         }
643                 }
644         }
645         ret = true;
646 FINISH_OFF:
647         EM_DEBUG_FUNC_END("ret [%d]", ret);
648         return ret;
649 }
650
651 INTERNAL_FUNC int em_verify_email_address_of_mail_tbl(emstorage_mail_tbl_t *input_mail_tbl, int input_without_bracket)
652 {
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;
656
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]);
663                                 goto FINISH_OFF;
664                         }
665                 }
666         }
667
668 FINISH_OFF:
669         EM_DEBUG_FUNC_END("err [%d]", err);
670         return err;
671 }
672
673 INTERNAL_FUNC int em_find_tag_for_thread_view(char *subject, int *result)
674 {
675         EM_DEBUG_FUNC_BEGIN();
676         int error_code = EMAIL_ERROR_NONE;
677         char *copy_of_subject = NULL;
678
679         EM_IF_NULL_RETURN_VALUE(subject, EMAIL_ERROR_INVALID_PARAM);
680         EM_IF_NULL_RETURN_VALUE(result, EMAIL_ERROR_INVALID_PARAM);
681
682         *result = FALSE;
683
684         copy_of_subject = EM_SAFE_STRDUP(subject);
685
686         if (copy_of_subject == NULL) {
687                 EM_DEBUG_EXCEPTION("strdup is failed.");
688                 goto FINISH_OFF;
689         }
690
691         em_upper_string(copy_of_subject);
692         EM_DEBUG_LOG("em_upper_string result : %s\n", copy_of_subject);
693
694         if (strstr(copy_of_subject, "RE:") == NULL) {
695                 if (strstr(copy_of_subject, "FWD:") == NULL) {
696                         if (strstr(copy_of_subject, "FW:") != NULL)
697                                 *result = TRUE;
698                 }
699                 else
700                         *result = TRUE;
701         }
702         else
703                 *result = TRUE;
704
705 FINISH_OFF:
706         EM_SAFE_FREE(copy_of_subject);
707
708         EM_DEBUG_FUNC_END("result : %d", *result);
709
710         return error_code;
711 }
712
713 INTERNAL_FUNC int em_find_pos_stripped_subject_for_thread_view(char *subject, char *stripped_subject, int stripped_subject_buffer_size)
714 {
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;
717         int gap;
718         char *copy_of_subject = NULL, *curpos = NULL, *result;
719
720         EM_IF_NULL_RETURN_VALUE(subject, EMAIL_ERROR_INVALID_PARAM);
721         EM_IF_NULL_RETURN_VALUE(stripped_subject, EMAIL_ERROR_INVALID_PARAM);
722
723         copy_of_subject = EM_SAFE_STRDUP(subject);
724
725         if (copy_of_subject == NULL) {
726                 EM_DEBUG_EXCEPTION("strdup is failed");
727                 goto FINISH_OFF;
728         }
729
730         em_upper_string(copy_of_subject);
731         curpos = copy_of_subject;
732
733         EM_DEBUG_LOG("em_upper_string result : %s", copy_of_subject);
734
735         while ((result = strstr(curpos, "RE:")) != NULL) {
736                 curpos = result + 3;
737                 EM_DEBUG_LOG("RE result : %s", curpos);
738         }
739
740         while ((result = strstr(curpos, "FWD:")) != NULL) {
741                 curpos = result + 4;
742                 EM_DEBUG_LOG("FWD result : %s", curpos);
743         }
744
745         while ((result = strstr(curpos, "FW:")) != NULL) {
746                 curpos = result + 3;
747                 EM_DEBUG_LOG("FW result : %s", curpos);
748         }
749
750         while (curpos != NULL && *curpos == ' ') {
751                 curpos++;
752         }
753
754         gap = curpos - copy_of_subject;
755
756         EM_SAFE_STRNCPY(stripped_subject, subject + gap, stripped_subject_buffer_size);
757
758 FINISH_OFF:
759         EM_SAFE_FREE(copy_of_subject);
760
761         if (error_code == EMAIL_ERROR_NONE && stripped_subject)
762                 EM_DEBUG_LOG("result[%s]", stripped_subject);
763
764         EM_DEBUG_FUNC_END("error_code[%d]", error_code);
765         return error_code;
766 }
767
768
769 /*
770  * encoding base64
771  */
772 INTERNAL_FUNC int em_encode_base64(char *src, unsigned long src_len, char **enc, unsigned long* enc_len, int *err_code)
773 {
774         EM_DEBUG_FUNC_BEGIN();
775
776     unsigned char *content;
777     int ret = true, err = EMAIL_ERROR_NONE;
778
779         if (err_code != NULL) {
780                 *err_code = EMAIL_ERROR_NONE;
781         }
782
783     content = rfc822_binary(src, src_len, enc_len);
784
785     if (content)
786         *enc = (char *)content;
787     else {
788         err = EMAIL_ERROR_UNKNOWN;
789         ret = false;
790     }
791
792     if (err_code)
793         *err_code = err;
794
795         EM_DEBUG_FUNC_END();
796     return ret;
797 }
798
799 /*
800  * decoding base64
801  */
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)
803 {
804     unsigned char *text = enc_text;
805     unsigned long size = enc_len;
806     unsigned char *content;
807     int ret = true, err = EMAIL_ERROR_NONE;
808
809         if (err_code != NULL) {
810                 *err_code = EMAIL_ERROR_NONE;
811         }
812
813     EM_DEBUG_FUNC_BEGIN();
814
815     content = rfc822_base64(text, size, dec_len);
816     if (content)
817         *dec_text = (char *)content;
818     else
819     {
820         err = EMAIL_ERROR_UNKNOWN;
821         ret = false;
822     }
823
824     if (err_code)
825         *err_code = err;
826
827     return ret;
828 }
829
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)
831 {
832         EM_DEBUG_FUNC_BEGIN();
833         emstorage_account_tbl_t *account_tbl_data = NULL;
834         int ret = false;
835         int err= EMAIL_ERROR_NONE;
836
837         if (account_server_type == NULL ) {
838                 EM_DEBUG_EXCEPTION("account_server_type is NULL");
839                 err = EMAIL_ERROR_INVALID_PARAM;
840                 ret = false;
841                 goto FINISH_OFF;
842         }
843
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);
846                 ret = false;
847                 goto FINISH_OFF;
848         }
849
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;
854         }
855
856         ret = true;
857
858 FINISH_OFF:
859         if ( account_tbl_data != NULL ) {
860                 emstorage_free_account(&account_tbl_data, 1, NULL);
861         }
862         if ( error != NULL ) {
863                 *error = err;
864         }
865
866         return ret;
867 }
868
869 #include <vconf.h>
870 #include <dbus/dbus.h>
871
872 #define ACTIVE_SYNC_HANDLE_INIT_VALUE           (-1)
873 #define ACTIVE_SYNC_HANDLE_BOUNDARY                     (-100000000)
874
875
876 INTERNAL_FUNC int em_get_handle_for_activesync(int *handle, int *error)
877 {
878         EM_DEBUG_FUNC_BEGIN();
879
880         static int next_handle = 0;
881         int ret = false;
882         int err = EMAIL_ERROR_NONE;
883
884         if ( handle == NULL ) {
885                 EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed : handle is NULL");
886                 err = EMAIL_ERROR_INVALID_PARAM;
887                 goto FINISH_OFF;
888         }
889
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;
894                         goto FINISH_OFF;
895                 }
896         }
897
898         EM_DEBUG_LOG(">>>>>> VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE : get lastest handle[%d]", next_handle);
899
900         /*  set the value of the handle for active sync */
901         next_handle--;
902         if ( next_handle < ACTIVE_SYNC_HANDLE_BOUNDARY ) {
903                 next_handle = ACTIVE_SYNC_HANDLE_INIT_VALUE;
904         }
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;
908                 goto FINISH_OFF;
909         }
910         ret = true;
911         *handle = next_handle;
912         EM_DEBUG_LOG(">>>>>> return next handle[%d]", *handle);
913
914 FINISH_OFF:
915         if ( error != NULL ) {
916                 *error = err;
917         }
918
919         return ret;
920 }
921
922 INTERNAL_FUNC int em_send_notification_to_active_sync_engine(int subType, ASNotiData *data)
923 {
924         EM_DEBUG_FUNC_BEGIN("subType [%d], data [%p]", subType, data);
925
926         DBusConnection     *connection;
927         DBusMessage        *signal = NULL;
928         DBusError           error;
929         int                 i = 0;
930
931         dbus_error_init (&error);
932         connection = dbus_bus_get(DBUS_BUS_SYSTEM, &error);
933
934         if(connection == NULL)
935                 goto FINISH_OFF;
936
937         signal = dbus_message_new_signal("/User/Email/ActiveSync", EMAIL_ACTIVE_SYNC_NOTI, "email");
938
939         dbus_message_append_args(signal, DBUS_TYPE_INT32, &subType, DBUS_TYPE_INVALID);
940         switch ( subType ) {
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);
945
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);
949                         break;
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);
952                         break;
953                 case ACTIVE_SYNC_NOTI_SEND_REPORT:
954                         EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_SEND_REPORT]", subType);
955                         break;
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);
960
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);
964                         break;
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);
970
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);
975                         break;
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 );
981
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);
986                         break;
987                 case ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT:
988                         EM_DEBUG_EXCEPTION("Not support yet : subType[ACTIVE_SYNC_NOTI_VALIDATE_ACCOUNT]", subType);
989                         break;
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);
994
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);
998                         break;
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);
1004
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);
1022                                                 break;
1023
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);
1032                                                 break;
1033
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);
1038                                                 break;
1039                                         default :
1040                                                 EM_DEBUG_EXCEPTION("Invalid filter type [%d]", data->search_mail_on_server.search_filter_list[i].search_filter_type);
1041                                                 break;
1042                                 }
1043                         }
1044                         dbus_message_append_args(signal, DBUS_TYPE_INT32,  &(data->search_mail_on_server.handle), DBUS_TYPE_INVALID);
1045                         break;
1046
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);
1050                         break;
1051
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);
1056                         break;
1057
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);
1062                         break;
1063
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);
1068                         break;
1069
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);
1075                         break;
1076
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);
1083                         break;
1084
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);
1089                         break;
1090
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);
1097                         break;
1098
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);
1102                         break;
1103
1104                 default:
1105                         EM_DEBUG_EXCEPTION("Invalid Notification type of Active Sync : subType[%d]", subType);
1106                         return FAILURE;
1107         }
1108
1109         if(!dbus_connection_send (connection, signal, NULL)) {
1110                 EM_DEBUG_EXCEPTION("dbus_connection_send is failed");
1111                 return FAILURE;
1112         } else
1113                 EM_DEBUG_LOG("dbus_connection_send is successful");
1114
1115         dbus_connection_flush(connection);
1116
1117 FINISH_OFF:
1118
1119         if(signal)
1120                 dbus_message_unref(signal);
1121
1122         EM_DEBUG_FUNC_END();
1123         return true;
1124 }