Fixed the invalid casting and remove the flash noti
[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 #include <locale.h>
40
41 #include "c-client.h"
42
43 #include "email-types.h"
44 #include "email-internal-types.h"
45 #include "email-utilities.h"
46
47 INTERNAL_FUNC void* em_malloc(int len)
48 {
49         /* EM_DEBUG_LOG("Memory allocation size[%d] bytes", len); */
50         if (len <= 0) {
51                 EM_DEBUG_LOG ("len should be positive.[%d]", len);
52                 return NULL;
53         }
54
55         void *p = calloc(1,len);
56         if (!p)
57                 EM_DEBUG_PERROR("malloc failed");
58
59         return p;
60 }
61
62
63 INTERNAL_FUNC void* em_memdup(void* src, int len)
64 {
65         /* EM_DEBUG_LOG("Memory allocation size[%d] bytes", len); */
66         if (len <= 0) {
67                 EM_DEBUG_LOG ("len should be positive.[%d]", len);
68                 return NULL;
69         }
70
71         void *p = calloc(1,len);
72         if (!p) {
73                 EM_DEBUG_EXCEPTION("malloc failed");
74                 return NULL;
75         }
76
77         memcpy(p, src, len);
78
79         return p;
80 }
81
82
83 /*  remove left space, tab, CR, L */
84 INTERNAL_FUNC char *em_trim_left(char *str)
85 {
86         char *p, *temp_buffer = NULL;
87
88         /* EM_DEBUG_FUNC_BEGIN() */
89         if (!str) return NULL;
90
91         p = str;
92         while (*p && (*p == ' ' || *p == '\t' || *p == LF || *p == CR)) p++;
93
94         if (!*p) return NULL;
95
96         temp_buffer = EM_SAFE_STRDUP(p);
97
98         strncpy(str, temp_buffer, EM_SAFE_STRLEN(str));
99         str[EM_SAFE_STRLEN(temp_buffer)] = NULL_CHAR;
100
101         EM_SAFE_FREE(temp_buffer);
102
103         return str;
104 }
105
106 /*  remove right space, tab, CR, L */
107 INTERNAL_FUNC char *em_trim_right(char *str)
108 {
109         char *p;
110
111         /* EM_DEBUG_FUNC_BEGIN() */
112         if (!str) return NULL;
113
114         p = str+EM_SAFE_STRLEN(str)-1;
115         while ((p >= str) && (*p == ' ' || *p == '\t' || *p == LF || *p == CR))
116                 *p --= '\0';
117
118         if (p < str)
119                 return NULL;
120
121         return str;
122 }
123
124 INTERNAL_FUNC char* em_upper_string(char *str)
125 {
126         char *p = str;
127         while (*p)  {
128                 *p = toupper(*p);
129                 p++;
130         }
131         return str;
132 }
133
134 INTERNAL_FUNC char*  em_lower_string(char *str)
135 {
136         char *p = str;
137         while (*p)  {
138                 *p = tolower(*p);
139                 p++;
140         }
141         return str;
142 }
143
144 INTERNAL_FUNC int em_upper_path(char *path)
145 {
146         int i = 0, is_utf7 = 0, len = path ? (int)EM_SAFE_STRLEN(path) : -1;
147         for (; i < len; i++) {
148                 if (path[i] == '&' || path[i] == 5) {
149                         is_utf7 = 1;
150                 }
151                 else {
152                         if (is_utf7) {
153                                 if (path[i] == '-') is_utf7 = 0;
154                         }
155                         else {
156                                 path[i] = toupper(path[i]);
157                         }
158                 }
159         }
160
161         return 1;
162 }
163
164 INTERNAL_FUNC void em_skip_whitespace(char *addr_str, char **pAddr)
165 {
166         EM_DEBUG_FUNC_BEGIN("addr_str[%p]", addr_str);
167
168         if (!addr_str)
169                 return ;
170         char *str = addr_str;
171         char ptr[EM_SAFE_STRLEN(str)+1]  ;
172         int i, j = 0;
173
174         str = addr_str ;
175         for (i = 0; str[i] != NULL_CHAR ; i++) {
176                 if (str[i] != SPACE && str[i] != TAB && str[i] != CR && str[i] != LF)
177                         ptr[j++] = str[i];
178         }
179         ptr[j] = NULL_CHAR;
180
181         *pAddr = EM_SAFE_STRDUP(ptr);
182         EM_DEBUG_FUNC_END("ptr[%s]", ptr);
183 }
184
185 INTERNAL_FUNC void em_skip_whitespace_without_alias(char *addr_str, char **pAddr)
186 {
187         EM_DEBUG_FUNC_BEGIN("addr_str[%p]", addr_str);
188
189         if (!addr_str)
190                 return ;
191         char *str = addr_str;
192         char ptr[EM_SAFE_STRLEN(addr_str) + 1];
193         int i = 0, j = 0;
194         char *first_qu = NULL;
195         char *last_qu = NULL;
196         char *first_c = NULL;
197         char *last_c = NULL;
198
199         // find first and last quatation
200         for (i = 0; str[i] != '\0'; i++) {
201                 if (!first_qu && str[i] == '\"')
202                         first_qu = str + i;
203                 if (str[i] == '\"')
204                         last_qu = str + i;
205         }
206
207         if (!first_qu || !last_qu) {
208                 // if there is no qutation
209                 for (i = 0; str[i] != NULL_CHAR ; i++) {
210                         if (str[i] != SPACE && str[i] != TAB && str[i] != CR && str[i] != LF)
211                                 ptr[j++] = str[i];
212                 }
213         } else {
214                 // find first and last character except for space
215                 for (first_c = first_qu + 1; *first_c == ' '; first_c++);
216                 for (last_c = last_qu - 1; *last_c == ' '; last_c--);
217                 for (i = 0; str[i] != '\0'; i++) {
218                         if (str + i <= first_qu || str + i >= last_qu) {
219                                 if (str[i] != SPACE && str[i] != TAB && str[i] != CR && str[i] != LF)
220                                         ptr[j++] = str[i];
221                         } else if (first_qu < str + i && str + i < last_qu) {
222                                 if (str + i < first_c || str + i > last_c)
223                                         continue;
224                                 else
225                                         ptr[j++] = str[i];
226                         }
227                 }
228         }
229         ptr[j++] = NULL_CHAR;
230
231         *pAddr = EM_SAFE_STRDUP(ptr);
232         EM_DEBUG_FUNC_END("ptr[%s]", ptr);
233 }
234
235 INTERNAL_FUNC char* em_skip_whitespace_without_strdup(char *source_string)
236 {
237         EM_DEBUG_FUNC_BEGIN("source_string[%p]", source_string);
238
239         if (!source_string)
240                 return NULL;
241         int i;
242
243         for (i = 0; source_string[i] != NULL_CHAR ; i++) {
244                 if (source_string[i] != SPACE) /*  || source_string[i] != TAB || source_string[i] != CR || source_string[i] || LF) */
245                         break;
246         }
247
248         EM_DEBUG_FUNC_END("i[%d]", i);
249         return source_string + i;
250 }
251
252 INTERNAL_FUNC char* em_replace_all_string(char *source_string, char *old_string, char *new_string)
253 {
254         EM_DEBUG_FUNC_BEGIN();
255         char *result_buffer = NULL;
256         int i = 0, j = 0;
257         int old_str_length = 0;
258         int new_str_length = 0;
259         int realloc_len = 0;
260
261         EM_IF_NULL_RETURN_VALUE(source_string, NULL);
262         EM_IF_NULL_RETURN_VALUE(old_string, NULL);
263         EM_IF_NULL_RETURN_VALUE(new_string, NULL);
264
265         int src_len = EM_SAFE_STRLEN(source_string);
266         old_str_length = EM_SAFE_STRLEN(old_string);
267         new_str_length = EM_SAFE_STRLEN(new_string);
268
269         if (src_len <= 0) {
270                 EM_DEBUG_LOG("source_string is invalid");
271                 return NULL;
272         }
273
274         result_buffer = calloc(src_len+1, sizeof(char));
275         if (!result_buffer) {
276                 EM_DEBUG_EXCEPTION("calloc failed");
277                 return NULL;
278         }
279
280         if (!strstr(source_string, old_string)) {
281                 memcpy(result_buffer, source_string, src_len);
282                 return result_buffer;
283         }
284
285         realloc_len = src_len + 1;
286
287         for (i = 0; i < src_len && source_string[i] != '\0';) {
288                 if (old_str_length <= src_len - i &&
289                                 memcmp(&source_string[i], old_string, old_str_length) == 0) {
290
291                         if (old_str_length != new_str_length) {
292                                 realloc_len = realloc_len - old_str_length + new_str_length;
293                                 result_buffer = realloc(result_buffer, realloc_len);
294                                 if (!result_buffer) {
295                                         EM_DEBUG_EXCEPTION("realloc failed");
296                                         return NULL;
297                                 }
298                         }
299                         memcpy(&result_buffer[j], new_string, new_str_length);
300                         i += old_str_length;
301                         j += new_str_length;
302                 } else {
303                         result_buffer[j] = source_string[i];
304                         i++;
305                         j++;
306                 }
307         }
308
309         if (j < realloc_len)
310                 result_buffer[j] = '\0';
311         else
312                 result_buffer[realloc_len-1] = '\0';
313
314         EM_DEBUG_FUNC_END();
315         return result_buffer;
316 }
317
318 INTERNAL_FUNC char* em_replace_string(char *source_string, char *old_string, char *new_string)
319 {
320         EM_DEBUG_FUNC_BEGIN();
321         char *result_buffer = NULL;
322         char *p = NULL;
323         int   buffer_length = 0;
324
325         EM_IF_NULL_RETURN_VALUE(source_string, NULL);
326         EM_IF_NULL_RETURN_VALUE(old_string, NULL);
327         EM_IF_NULL_RETURN_VALUE(new_string, NULL);
328
329         p = strstr(source_string, old_string);
330
331         if (p == NULL) {
332                 EM_DEBUG_EXCEPTION("old_string not found in source_string");
333                 EM_DEBUG_FUNC_END("return NULL");
334                 return NULL;
335         }
336
337         buffer_length   = EM_SAFE_STRLEN(source_string) + 1024;
338         result_buffer  = (char *)em_malloc(buffer_length);
339
340         if (!result_buffer) {
341                 EM_DEBUG_EXCEPTION("em_malloc failed");
342                 return NULL;
343         }
344
345         strncpy(result_buffer, source_string, p - source_string);
346         snprintf(result_buffer + strlen(result_buffer), buffer_length - strlen(result_buffer), "%s%s", new_string, p + strlen(old_string)); /*prevent 34351*/
347
348         EM_DEBUG_FUNC_END("result_buffer[%p]", result_buffer);
349         return result_buffer;
350 }
351
352 INTERNAL_FUNC int em_replace_string_ex(char **input_source_string, char *input_old_string, char *input_new_string)
353 {
354         EM_DEBUG_FUNC_BEGIN();
355         int   err = 0;
356         int   buffer_length = 0;
357         int   old_string_length = 0;
358         int   new_string_length = 0;
359         int   match_count = 0;
360
361         char *cursor_of_source_string = NULL;
362         char *cursor_of_result_buffer = NULL;
363         char *result_buffer = NULL;
364         char *source_string = NULL;
365         char *found_pos = NULL;
366
367         EM_IF_NULL_RETURN_VALUE(input_source_string, EMAIL_ERROR_INVALID_PARAM);
368         EM_IF_NULL_RETURN_VALUE(input_old_string, EMAIL_ERROR_INVALID_PARAM);
369         EM_IF_NULL_RETURN_VALUE(input_new_string, EMAIL_ERROR_INVALID_PARAM);
370
371         source_string = *input_source_string;
372
373         found_pos = strstr(source_string, input_old_string);
374
375         if (found_pos == NULL) {
376                 err = EMAIL_ERROR_DATA_NOT_FOUND;
377                 goto FINISH_OFF;
378         }
379
380         old_string_length = EM_SAFE_STRLEN(input_old_string);
381         new_string_length = EM_SAFE_STRLEN(input_new_string);
382
383         while (found_pos) {
384                 match_count++;
385                 found_pos++;
386                 found_pos = strstr(found_pos, input_old_string);
387         }
388
389         buffer_length  = EM_SAFE_STRLEN(source_string) + ((new_string_length - old_string_length) * match_count) + 50;
390
391         result_buffer  = (char*)malloc(buffer_length);
392
393         if (!result_buffer) {
394                 err = EMAIL_ERROR_OUT_OF_MEMORY;
395                 goto FINISH_OFF;
396         }
397         memset(result_buffer, 0 , buffer_length);
398
399         cursor_of_source_string = source_string;
400         cursor_of_result_buffer = result_buffer;
401         found_pos = strstr(source_string, input_old_string);
402
403         while (found_pos) {
404                 memcpy(cursor_of_result_buffer, cursor_of_source_string, found_pos - cursor_of_source_string);
405
406                 cursor_of_result_buffer = result_buffer + EM_SAFE_STRLEN(result_buffer);
407                 cursor_of_source_string = found_pos + old_string_length;
408
409                 memcpy(cursor_of_result_buffer, input_new_string, new_string_length);
410
411                 cursor_of_result_buffer = result_buffer + EM_SAFE_STRLEN(result_buffer);
412
413                 found_pos++;
414                 found_pos = strstr(found_pos, input_old_string);
415         }
416
417         EM_SAFE_STRCAT(result_buffer, cursor_of_source_string);
418
419         EM_SAFE_FREE(*input_source_string);
420         *input_source_string = result_buffer;
421
422 FINISH_OFF:
423
424         EM_DEBUG_FUNC_END("err[%d]", err);
425         return err;
426 }
427
428 INTERNAL_FUNC int em_get_file_name_from_file_path(char *input_source_file_path, char **output_file_name)
429 {
430         EM_DEBUG_FUNC_BEGIN_SEC("input_source_file_path[%s], output_file_name [%p]", input_source_file_path, output_file_name);
431         int   err = EMAIL_ERROR_NONE;
432         int   pos_on_string = 0;
433         int   file_name_length = 0;
434         char *start_pos_of_file_name = NULL;
435         char *end_pos_of_file_name = NULL;
436         char *end_pos_of_file_path = NULL;
437         char  file_name_string[MAX_PATH] = { 0, };
438
439         if (!input_source_file_path || !output_file_name) {
440                 EM_DEBUG_EXCEPTION("Invalid Parameter");
441                 err  = EMAIL_ERROR_INVALID_PARAM;
442                 goto FINISH_OFF;
443         }
444
445         pos_on_string        = EM_SAFE_STRLEN(input_source_file_path) - 1;
446         end_pos_of_file_path = input_source_file_path + pos_on_string;
447         end_pos_of_file_name = end_pos_of_file_path;
448
449         while(pos_on_string >= 0 && input_source_file_path[pos_on_string] != '/') {
450                 pos_on_string--;
451         }
452
453         pos_on_string++;
454
455         if(pos_on_string >= 0) {
456                 start_pos_of_file_name = input_source_file_path + pos_on_string;
457                 file_name_length       = end_pos_of_file_name - start_pos_of_file_name + 1;
458                 memcpy(file_name_string, start_pos_of_file_name, file_name_length);
459         }
460
461         EM_DEBUG_LOG_SEC("file_name_string [%s] pos_on_string [%d] file_name_length [%d]", file_name_string, pos_on_string, file_name_length);
462
463         *output_file_name = EM_SAFE_STRDUP(file_name_string);
464
465 FINISH_OFF:
466         EM_DEBUG_FUNC_END("err = [%d]", err);
467         return err;
468 }
469
470 INTERNAL_FUNC int em_get_file_name_and_extension_from_file_path(char *input_source_file_path, char **output_file_name, char **output_extension)
471 {
472         EM_DEBUG_FUNC_BEGIN_SEC("input_source_file_path[%s], output_file_name [%p], output_extension [%p]", input_source_file_path, output_file_name, output_extension);
473         int   err = EMAIL_ERROR_NONE;
474         int   pos_on_string = 0;
475         int   file_name_length = 0;
476         int   extention_length = 0;
477         char *start_pos_of_file_name = NULL;
478         char *end_pos_of_file_name = NULL;
479         char *dot_pos_of_file_path = NULL;
480         char *end_pos_of_file_path = NULL;
481         char  file_name_string[MAX_PATH] = { 0, };
482         char  extension_string[MAX_PATH] = { 0, };
483
484         if (!input_source_file_path || !output_file_name || !output_extension || EM_SAFE_STRLEN(input_source_file_path) <= 0) {
485                 EM_DEBUG_EXCEPTION("Invalid Parameter");
486                 err  = EMAIL_ERROR_INVALID_PARAM;
487                 goto FINISH_OFF;
488         }
489
490         pos_on_string        = EM_SAFE_STRLEN(input_source_file_path) - 1;
491         end_pos_of_file_path = input_source_file_path + pos_on_string;
492         end_pos_of_file_name = end_pos_of_file_path;
493
494         while(pos_on_string >= 0 && input_source_file_path[pos_on_string] != '/') {
495                 if(input_source_file_path[pos_on_string] == '.') {
496                         if(dot_pos_of_file_path == NULL) {
497                                 end_pos_of_file_name = input_source_file_path + pos_on_string;
498                                 dot_pos_of_file_path = end_pos_of_file_name;
499                         }
500                 }
501                 pos_on_string--;
502         }
503
504         pos_on_string++;
505
506         if(pos_on_string >= 0) {
507                 start_pos_of_file_name = input_source_file_path + pos_on_string;
508                 file_name_length       = end_pos_of_file_name - start_pos_of_file_name;
509                 memcpy(file_name_string, start_pos_of_file_name, file_name_length);
510         }
511
512         if(dot_pos_of_file_path != NULL) {
513                 extention_length       = (end_pos_of_file_path + 1) - (dot_pos_of_file_path + 1);
514                 memcpy(extension_string, dot_pos_of_file_path + 1, extention_length);
515         }
516
517         EM_DEBUG_LOG_SEC("*file_name_string [%s] pos_on_string [%d]", file_name_string, pos_on_string);
518
519         *output_file_name = EM_SAFE_STRDUP(file_name_string);
520         *output_extension = EM_SAFE_STRDUP(extension_string);
521
522 FINISH_OFF:
523         EM_DEBUG_FUNC_END("err = [%d]", err);
524         return err;
525 }
526
527 INTERNAL_FUNC char *em_get_extension_from_file_path(char *source_file_path, int *err_code)
528 {
529         EM_DEBUG_FUNC_BEGIN_SEC("source_file_path[%s]", source_file_path);
530         int err = EMAIL_ERROR_NONE, pos_on_string = 0;
531         char *extension = NULL;
532
533         if (!source_file_path) {
534                 EM_DEBUG_EXCEPTION("Invalid Parameter");
535                 err  = EMAIL_ERROR_INVALID_PARAM;
536                 goto FINISH_OFF;
537         }
538
539         pos_on_string = EM_SAFE_STRLEN(source_file_path) - 1;
540
541         while(pos_on_string >= 0 && source_file_path[pos_on_string] != '.') {
542                 pos_on_string--;
543         }
544
545         if (pos_on_string >= 0 && pos_on_string < EM_SAFE_STRLEN(source_file_path) - 1)
546                 extension = source_file_path + pos_on_string + 1;
547
548         EM_DEBUG_LOG("*extension [%s] pos_on_string [%d]", extension, pos_on_string);
549
550 FINISH_OFF:
551         if (err_code)
552                 *err_code = err;
553         EM_DEBUG_FUNC_END();
554         return extension;
555 }
556
557 INTERNAL_FUNC char *em_shrink_filename(char *fname, int size_limit)
558 {
559         EM_DEBUG_FUNC_BEGIN("fname[%s], size_limit[%d]", fname, size_limit);
560
561         char *modified_name = NULL;
562         char *extension = NULL;
563
564         modified_name = em_malloc(sizeof(char)*size_limit);
565         if (!modified_name) {
566                 return NULL;
567         }
568
569         char *tmp_ext = NULL;
570         char *tmp_name = NULL;
571         char *tmp_name_strip = NULL;
572
573         extension = em_get_extension_from_file_path(fname, NULL);
574
575         if (extension && EM_SAFE_STRLEN(extension) > 0) {
576                 int ext_len = EM_SAFE_STRLEN(extension);
577                 int name_len = EM_SAFE_STRLEN(fname) - EM_SAFE_STRLEN(extension) - 1;
578                 int name_strip_len = size_limit - EM_SAFE_STRLEN(extension) - 2;
579
580                 tmp_ext = em_malloc(sizeof(char)*(ext_len+1));
581                 if (tmp_ext == NULL) {
582                         EM_DEBUG_EXCEPTION("em_malloc failed");
583                         goto FINISH_OFF;
584                 }
585
586                 tmp_name = em_malloc(sizeof(char)*(name_len+1));
587                 if (tmp_name == NULL) {
588                         EM_DEBUG_EXCEPTION("em_malloc failed");
589                         goto FINISH_OFF;
590                 }
591
592                 tmp_name_strip = em_malloc(sizeof(char)*name_strip_len);
593                 if (tmp_name_strip == NULL) {
594                         EM_DEBUG_EXCEPTION("em_malloc failed");
595                         goto FINISH_OFF;
596                 }
597
598                 snprintf(tmp_ext, sizeof(char)*(ext_len+1), "%s", extension);
599                 snprintf(tmp_name, sizeof(char)*(name_len+1), "%s", fname);
600
601                 EM_DEBUG_LOG(">>>>> extention [%s]", tmp_ext);
602                 EM_DEBUG_LOG(">>>>> name [%s]", tmp_name);
603
604                 if (EM_SAFE_STRLEN(extension) > EM_SAFE_STRLEN(fname) - EM_SAFE_STRLEN(extension)) {
605                         snprintf(modified_name, sizeof(char)*size_limit, "%s", fname);
606                 } else {
607                         if (tmp_name_strip && name_strip_len > 1) {
608                                 snprintf(tmp_name_strip, sizeof(char)*name_strip_len, "%s", tmp_name);
609                                 snprintf(modified_name, sizeof(char)*size_limit, "%s.%s", tmp_name_strip, tmp_ext);
610                         } else {
611                                 snprintf(modified_name, sizeof(char)*size_limit, "%s", fname);
612                         }
613                 }
614         } else {
615                 snprintf(modified_name, sizeof(char)*size_limit, "%s", fname);
616         }
617
618 FINISH_OFF:
619
620         EM_SAFE_FREE(tmp_ext);
621         EM_SAFE_FREE(tmp_name);
622         EM_SAFE_FREE(tmp_name_strip);
623
624         EM_DEBUG_FUNC_END();
625
626         return modified_name;
627 }
628
629 INTERNAL_FUNC int em_get_encoding_type_from_file_path(const char *input_file_path, char **output_encoding_type)
630 {
631         EM_DEBUG_FUNC_BEGIN("input_file_path[%d], output_encoding_type[%p]", input_file_path, output_encoding_type);
632         int   err = EMAIL_ERROR_NONE;
633         int   pos_of_filename = 0;
634         int   pos_of_dot = 0;
635         int   enf_of_string = 0;
636         int   result_string_length = 0;
637         char *result_encoding_type = NULL;
638
639         if (!input_file_path || !output_encoding_type) {
640                 EM_DEBUG_EXCEPTION("Invalid Parameter");
641                 err  = EMAIL_ERROR_INVALID_PARAM;
642                 goto FINISH_OFF;
643         }
644
645         enf_of_string = pos_of_filename = EM_SAFE_STRLEN(input_file_path);
646
647         while(pos_of_filename > 0 && input_file_path[pos_of_filename--] != '/') {
648                 if(input_file_path[pos_of_filename] == '.')
649                         pos_of_dot = pos_of_filename;
650         }
651
652         if(pos_of_filename != 0)
653                 pos_of_filename += 2;
654
655         if(pos_of_dot != 0 && pos_of_dot > pos_of_filename)
656                 result_string_length = pos_of_dot - pos_of_filename;
657         else
658                 result_string_length = enf_of_string - pos_of_filename;
659
660         if( !(result_encoding_type = em_malloc(sizeof(char) * (result_string_length + 1))) ) {
661                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
662                 err  = EMAIL_ERROR_OUT_OF_MEMORY;
663                 goto FINISH_OFF;
664         }
665
666         memcpy(result_encoding_type, input_file_path + pos_of_filename, result_string_length);
667
668         EM_DEBUG_LOG("result_encoding_type [%s]", result_encoding_type);
669
670         *output_encoding_type = result_encoding_type;
671
672 FINISH_OFF:
673         EM_DEBUG_FUNC_END("err [%d]", err);
674         return err;
675 }
676
677 INTERNAL_FUNC int em_get_content_type_from_extension_string(const char *extension_string, int *err_code)
678 {
679         EM_DEBUG_FUNC_BEGIN("extension_string[%s]", extension_string);
680         int i = 0, err = EMAIL_ERROR_NONE, result_content_type = TYPEAPPLICATION;
681         char *image_extension[] = { "jpeg", "jpg", "png", "gif", "bmp", "pic", "agif", "tif", "wbmp" , "p7s", "p7m", "asc", NULL};
682
683         if (!extension_string) {
684                 EM_DEBUG_EXCEPTION("Invalid Parameter");
685                 err  = EMAIL_ERROR_INVALID_PARAM;
686                 goto FINISH_OFF;
687         }
688
689         while(image_extension[i]) {
690                 EM_DEBUG_LOG("image_extension[%d] [%s]", i, image_extension[i]);
691                 if(strcasecmp(image_extension[i], extension_string) == 0) {
692                         break;
693                 }
694                 i++;
695         }
696
697         switch (i) {
698         case EXTENSION_JPEG:
699         case EXTENSION_JPG:
700         case EXTENSION_PNG:
701         case EXTENSION_GIF:
702         case EXTENSION_BMP:
703         case EXTENSION_PIC:
704         case EXTENSION_AGIF:
705         case EXTENSION_TIF:
706         case EXTENSION_WBMP:
707                 result_content_type = TYPEIMAGE;
708                 break;
709         case EXTENSION_P7S:
710                 result_content_type = TYPEPKCS7_SIGN;
711                 break;
712         case EXTENSION_P7M:
713                 result_content_type = TYPEPKCS7_MIME;
714                 break;
715         case EXTENSION_ASC:
716                 result_content_type = TYPEPGP;
717                 break;
718         default:
719                 break;
720         }
721
722 FINISH_OFF:
723         if (err_code)
724                 *err_code = err;
725         EM_DEBUG_FUNC_END();
726         return result_content_type;
727 }
728
729 #define EMAIL_ATOM                             "([^]()<>@,;:\\\".[\x20\x01-\x1f\x7f])+"  // x20: space,
730 #define EMAIL_QTEXT                            "[^\"\\\x0d]" /* " \ CR */
731 #define EMAIL_DTEXT                            "[^][\\\x0d]" /* [ ] \ CR */
732 #define EMAIL_QUOTED_PAIR                      "([\\].)" // first char :\ second char : anything (.)
733 #define EMAIL_QUOTED_STRING                    "[\"](" EMAIL_QTEXT "|" EMAIL_QUOTED_PAIR ")*[\"]"
734 #define EMAIL_WORD                             "(" EMAIL_ATOM "|" EMAIL_QUOTED_STRING ")"
735 #define EMAIL_PHRASE                           "(" EMAIL_ATOM "|" EMAIL_QUOTED_STRING ")"
736
737 #define EMAIL_DOMAIN_LITERAL                   "\\[(" EMAIL_DTEXT "|" EMAIL_QUOTED_PAIR ")*\\]" /* literal match for "[" and "]"*/
738 #define EMAIL_SUB_DOMAIN                       "(" EMAIL_ATOM "|" EMAIL_DOMAIN_LITERAL ")"
739
740 #define EMAIL_LOCAL_PART                       "(" EMAIL_WORD "(\\." EMAIL_WORD ")*)"
741 #define EMAIL_DOMAIN                           "(" EMAIL_SUB_DOMAIN "(\\." EMAIL_SUB_DOMAIN ")*)"
742 #define EMAIL_ADDR_SPEC                        "(" EMAIL_LOCAL_PART "@" EMAIL_DOMAIN ")"
743
744 #define EMAIL_MAILBOX                          "("EMAIL_ADDR_SPEC "|" EMAIL_PHRASE "[[:space:]]*" "<" EMAIL_ADDR_SPEC ">|<" EMAIL_ADDR_SPEC ">)"
745 #define EMAIL_ADDRESS                          "^([:blank:]*" EMAIL_MAILBOX "[,;[:blank:]]*([,;][,;[:blank:]]*" EMAIL_MAILBOX "[,;[:blank:]]*)*)$"
746
747
748 static int em_verify_email_address_by_using_regex(char *address)
749 {
750         EM_DEBUG_FUNC_BEGIN_SEC("address[%s]", address);
751
752         /*  this following code verfies the email alias string using reg. exp. */
753         regex_t alias_list_regex = {0};
754         int error = EMAIL_ERROR_NONE;
755         char *reg_rule = NULL;
756         int alias_len = 0;
757         regmatch_t *pmatch = NULL;
758
759         if(!address || EM_SAFE_STRLEN(address) == 0) {
760                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
761                 return EMAIL_ERROR_INVALID_PARAM;
762         }
763
764         reg_rule = EMAIL_ADDRESS;
765
766         if (regcomp (&alias_list_regex, reg_rule, REG_ICASE | REG_EXTENDED) != 0) {
767                 EM_DEBUG_EXCEPTION("email alias regex unrecognized");
768                 error = EMAIL_ERROR_SYSTEM_FAILURE;
769                 goto FINISH_OFF;
770         }
771
772         alias_len = EM_SAFE_STRLEN(address) + 1;
773         pmatch = (regmatch_t *) em_malloc (alias_len * sizeof (regmatch_t));
774         if (!pmatch) {
775                 EM_DEBUG_EXCEPTION("em_malloc error");
776                 goto FINISH_OFF;
777         }
778
779         if (regexec (&alias_list_regex, address, alias_len, pmatch, 0) == REG_NOMATCH) {
780                 EM_DEBUG_LOG_SEC("failed :[%s]", address);
781                 error = EMAIL_ERROR_INVALID_ADDRESS;
782                 goto FINISH_OFF;
783         }
784
785 FINISH_OFF:
786         regfree(&alias_list_regex);
787         EM_SAFE_FREE (pmatch);
788
789         EM_DEBUG_FUNC_END("err [%d]", error);
790         return error;
791 }
792
793 static int em_verify_email_address_without_regex(char *address)
794 {
795         EM_DEBUG_FUNC_BEGIN_SEC("address[%s]", address);
796         char *local_address = NULL;
797         char *address_start = NULL;
798         char *cur = NULL;
799         char *local_part = NULL;
800         char *domain = NULL;
801         char *saveptr = NULL;
802         char currunt_char;
803         int address_length = 0;
804         int i = 0;
805         int error = EMAIL_ERROR_NONE;
806         int occur = 0;
807
808         EM_DEBUG_LOG_SEC("address [%s]", address);
809
810         if (address == NULL) {
811                 EM_DEBUG_EXCEPTION("Invalid parameter");
812                 error = EMAIL_ERROR_INVALID_PARAM;
813                 return error;
814         }
815
816         local_address = strdup(address);
817
818         address_start = local_address;
819
820         while ((cur = strchr(address_start, '\"'))) {
821                 address_start = cur + 1;
822                 if (local_address >= cur - 1 || *(cur - 1) != '\\')
823                         occur++;
824         }
825
826         if (occur % 2) {
827                 error = EMAIL_ERROR_INVALID_ADDRESS;
828                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
829                 goto FINISH_OFF;
830         }
831
832         if ((cur = strchr(address_start, '<'))) {
833                 char *close_pos = NULL;
834
835                 address_start = cur + 1;
836                 close_pos = address_start;
837
838                 while ((cur = strchr(close_pos, '>'))) {
839                         close_pos = cur + 1;
840                 }
841
842                 if (address_start == close_pos) {
843                         error = EMAIL_ERROR_INVALID_ADDRESS;
844                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
845                         goto FINISH_OFF;
846                 }
847
848                 address_start[close_pos - 1 - address_start] = '\0';
849         }
850
851         EM_DEBUG_LOG_SEC("address_start [%s]", address_start);
852
853         address_length = EM_SAFE_STRLEN(address_start);
854
855         for (i = 0; i < address_length; i++) {
856                 currunt_char = address_start[i];
857                 if (!isalpha(currunt_char) && !isdigit(currunt_char) && currunt_char != '_' && currunt_char != '.' && currunt_char != '@') {
858                         error = EMAIL_ERROR_INVALID_ADDRESS;
859                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
860                         goto FINISH_OFF;
861                 }
862         }
863
864         if (strstr(address_start, "..") || strstr(address_start, ".@") || strstr(address_start, "@.") || strstr(address_start, "._.")) {
865                 error = EMAIL_ERROR_INVALID_ADDRESS;
866                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
867                 goto FINISH_OFF;
868         }
869
870         local_part = strtok_r(address_start, "@", &saveptr);
871
872         EM_DEBUG_LOG("local_part [%s]", local_part);
873
874         if (local_part == NULL || EM_SAFE_STRLEN(local_part) == 0) {
875                 error = EMAIL_ERROR_INVALID_ADDRESS;
876                 goto FINISH_OFF;
877         }
878
879         domain = strtok_r(NULL, "@", &saveptr);
880
881         EM_DEBUG_LOG("domain [%s]", domain);
882
883         if (domain == NULL || EM_SAFE_STRLEN(domain) < 3) {
884                 error = EMAIL_ERROR_INVALID_ADDRESS;
885                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
886                 goto FINISH_OFF;
887         }
888
889         if (strchr(domain, '.') == NULL) {
890                 error = EMAIL_ERROR_INVALID_ADDRESS;
891                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
892                 goto FINISH_OFF;
893         }
894
895         if (!isalpha(local_part[0])) {
896                 error = EMAIL_ERROR_INVALID_ADDRESS;
897                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
898                 goto FINISH_OFF;
899         }
900
901 FINISH_OFF:
902         EM_SAFE_FREE(local_address);
903
904         EM_DEBUG_FUNC_END("error [%d]", error);
905         return error;
906 }
907
908 INTERNAL_FUNC int em_verify_email_address(char *address)
909 {
910         EM_DEBUG_FUNC_BEGIN("address[%p]", address);
911         int error = EMAIL_ERROR_NONE;
912         char *result_locale = NULL;
913
914         setlocale(LC_ALL, "");
915
916         result_locale = setlocale(LC_ALL, NULL);
917
918         EM_DEBUG_LOG("LC_ALL[%s]" , result_locale);
919
920         if ( EM_SAFE_STRCMP(result_locale, "or_IN.UTF-8") == 0)
921                 error = em_verify_email_address_without_regex(address);
922         else
923                 error = em_verify_email_address_by_using_regex(address);
924
925         EM_DEBUG_FUNC_END("error [%d]", error);
926         return error;
927 }
928
929 INTERNAL_FUNC int em_verify_email_address_of_mail_data (email_mail_data_t *mail_data)
930 {
931         EM_DEBUG_FUNC_BEGIN("mail_data[%p]", mail_data);
932         char *address_array[4] = { mail_data->full_address_from, mail_data->full_address_to, mail_data->full_address_cc, mail_data->full_address_bcc};
933         int  err = EMAIL_ERROR_NONE, i;
934
935         /* check for email_address validation */
936         for (i = 0; i < 4; i++) {
937                 if (address_array[i] && address_array[i][0] != 0) {
938                         err = em_verify_email_address (address_array[i]);
939                         if (err != EMAIL_ERROR_NONE) {
940                                 EM_DEBUG_EXCEPTION_SEC("em_verify_email_address error[%d] idx[%d] addr[%s]", err, i, address_array[i]);
941                                 goto FINISH_OFF;
942                         }
943                 }
944         }
945 FINISH_OFF:
946         EM_DEBUG_FUNC_END("err [%d]", err);
947         return err;
948 }
949
950 INTERNAL_FUNC int em_verify_email_address_of_mail_tbl(emstorage_mail_tbl_t *input_mail_tbl)
951 {
952         EM_DEBUG_FUNC_BEGIN("input_mail_tbl[%p]", input_mail_tbl);
953         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};
954         int  err = EMAIL_ERROR_NONE, i;
955
956         /* check for email_address validation */
957         for (i = 0; i < 4; i++) {
958                 if (address_array[i] && address_array[i][0] != 0) {
959                         if ((err = em_verify_email_address (address_array[i])) != EMAIL_ERROR_NONE) {
960                                 EM_DEBUG_EXCEPTION_SEC("em_verify_email_address error[%d] idx[%d] addr[%s]", err, i, address_array[i]);
961                                 goto FINISH_OFF;
962                         }
963                 }
964         }
965
966 FINISH_OFF:
967         EM_DEBUG_FUNC_END("err [%d]", err);
968         return err;
969 }
970
971 INTERNAL_FUNC int em_find_tag_for_thread_view(char *subject, int *result)
972 {
973         EM_DEBUG_FUNC_BEGIN();
974         int error_code = EMAIL_ERROR_NONE;
975         char *copy_of_subject = NULL;
976
977         EM_IF_NULL_RETURN_VALUE(subject, EMAIL_ERROR_INVALID_PARAM);
978         EM_IF_NULL_RETURN_VALUE(result, EMAIL_ERROR_INVALID_PARAM);
979
980         *result = FALSE;
981
982         copy_of_subject = EM_SAFE_STRDUP(subject);
983
984         if (copy_of_subject == NULL) {
985                 EM_DEBUG_EXCEPTION("strdup is failed.");
986                 goto FINISH_OFF;
987         }
988
989         em_upper_string(copy_of_subject);
990         EM_DEBUG_LOG_SEC("em_upper_string result : %s\n", copy_of_subject);
991
992         if (strstr(copy_of_subject, "RE:") == NULL) {
993                 if (strstr(copy_of_subject, "FWD:") == NULL) {
994                         if (strstr(copy_of_subject, "FW:") != NULL)
995                                 *result = TRUE;
996                 }
997                 else
998                         *result = TRUE;
999         }
1000         else
1001                 *result = TRUE;
1002
1003 FINISH_OFF:
1004         EM_SAFE_FREE(copy_of_subject);
1005
1006         EM_DEBUG_FUNC_END("result : %d", *result);
1007
1008         return error_code;
1009 }
1010
1011 INTERNAL_FUNC int em_find_pos_stripped_subject_for_thread_view(char *subject, char *stripped_subject, int stripped_subject_buffer_size)
1012 {
1013         EM_DEBUG_FUNC_BEGIN("subject [%p] stripped_subject [%p] stripped_subject_buffer_size[%d]", subject, stripped_subject, stripped_subject_buffer_size);
1014         int error_code = EMAIL_ERROR_NONE;
1015         int gap;
1016         char *copy_of_subject = NULL, *curpos = NULL, *result;
1017
1018         EM_IF_NULL_RETURN_VALUE(subject, EMAIL_ERROR_INVALID_PARAM);
1019         EM_IF_NULL_RETURN_VALUE(stripped_subject, EMAIL_ERROR_INVALID_PARAM);
1020
1021         copy_of_subject = EM_SAFE_STRDUP(subject);
1022
1023         if (copy_of_subject == NULL) {
1024                 EM_DEBUG_EXCEPTION("strdup is failed");
1025                 goto FINISH_OFF;
1026         }
1027
1028         em_upper_string(copy_of_subject);
1029         curpos = copy_of_subject;
1030
1031
1032
1033         while ((result = g_strrstr(curpos, "RE:")) != NULL) {
1034                 curpos = result + 3;
1035                 EM_DEBUG_LOG_SEC("RE result : %s", curpos);
1036         }
1037
1038         while ((result = g_strrstr(curpos, "FWD:")) != NULL) {
1039                 curpos = result + 4;
1040                 EM_DEBUG_LOG_SEC("FWD result : %s", curpos);
1041         }
1042
1043         while ((result = g_strrstr(curpos, "FW:")) != NULL) {
1044                 curpos = result + 3;
1045                 EM_DEBUG_LOG_SEC("FW result : %s", curpos);
1046         }
1047
1048         while (curpos != NULL && *curpos == ' ') {
1049                 curpos++;
1050         }
1051
1052         gap = curpos - copy_of_subject;
1053
1054         EM_SAFE_STRNCPY(stripped_subject, subject + gap, stripped_subject_buffer_size);
1055
1056 FINISH_OFF:
1057         EM_SAFE_FREE(copy_of_subject);
1058
1059         EM_DEBUG_FUNC_END("error_code[%d]", error_code);
1060         return error_code;
1061 }
1062
1063
1064 /*
1065  * encoding base64
1066  */
1067 INTERNAL_FUNC int em_encode_base64(char *src, unsigned long src_len, char **enc, unsigned long* enc_len, int *err_code)
1068 {
1069         EM_DEBUG_FUNC_BEGIN();
1070
1071     unsigned char *content;
1072     int ret = true, err = EMAIL_ERROR_NONE;
1073
1074         if (err_code != NULL) {
1075                 *err_code = EMAIL_ERROR_NONE;
1076         }
1077
1078     content = rfc822_binary(src, src_len, enc_len);
1079
1080     if (content)
1081         *enc = (char *)content;
1082     else {
1083         err = EMAIL_ERROR_UNKNOWN;
1084         ret = false;
1085     }
1086
1087     if (err_code)
1088         *err_code = err;
1089
1090         EM_DEBUG_FUNC_END();
1091     return ret;
1092 }
1093
1094 /*
1095  * decoding base64
1096  */
1097 INTERNAL_FUNC int em_decode_base64(unsigned char *enc_text, unsigned long enc_len, char **dec_text, unsigned long* dec_len, int *err_code)
1098 {
1099     unsigned char *text = enc_text;
1100     unsigned long size = enc_len;
1101     unsigned char *content;
1102     int ret = true, err = EMAIL_ERROR_NONE;
1103
1104         if (err_code != NULL) {
1105                 *err_code = EMAIL_ERROR_NONE;
1106         }
1107
1108     EM_DEBUG_FUNC_BEGIN();
1109
1110     content = rfc822_base64(text, size, dec_len);
1111     if (content)
1112         *dec_text = (char *)content;
1113     else
1114     {
1115         err = EMAIL_ERROR_UNKNOWN;
1116         ret = false;
1117     }
1118
1119     if (err_code)
1120         *err_code = err;
1121
1122     return ret;
1123 }
1124
1125 INTERNAL_FUNC int em_get_account_server_type_by_account_id(char *multi_user_name, int account_id, email_account_server_t* account_server_type, int flag, int *error)
1126 {
1127         EM_DEBUG_FUNC_BEGIN();
1128         emstorage_account_tbl_t *account_tbl_data = NULL;
1129         int ret = false;
1130         int err= EMAIL_ERROR_NONE;
1131
1132         if (account_server_type == NULL ) {
1133                 EM_DEBUG_EXCEPTION("account_server_type is NULL");
1134                 err = EMAIL_ERROR_INVALID_PARAM;
1135                 ret = false;
1136                 goto FINISH_OFF;
1137         }
1138
1139         if( !emstorage_get_account_by_id(multi_user_name, account_id, WITHOUT_OPTION, &account_tbl_data, false, &err)) {
1140                 EM_DEBUG_EXCEPTION ("emstorage_get_account_by_id failed [%d] ", err);
1141                 ret = false;
1142                 goto FINISH_OFF;
1143         }
1144
1145         if ( flag == false )  { /*  sending server */
1146                 *account_server_type = account_tbl_data->outgoing_server_type;
1147         } else if ( flag == true ) {    /*  receiving server */
1148                 *account_server_type = account_tbl_data->incoming_server_type;
1149         }
1150
1151         ret = true;
1152
1153 FINISH_OFF:
1154         if ( account_tbl_data != NULL ) {
1155                 emstorage_free_account(&account_tbl_data, 1, NULL);
1156         }
1157         if ( error != NULL ) {
1158                 *error = err;
1159         }
1160
1161         return ret;
1162 }
1163
1164 #include <vconf.h>
1165 #include <dbus/dbus.h>
1166
1167 #define ACTIVE_SYNC_HANDLE_INIT_VALUE           (-1)
1168 #define ACTIVE_SYNC_HANDLE_BOUNDARY                     (-100000000)
1169
1170
1171 INTERNAL_FUNC int em_get_handle_for_activesync(int *handle, int *error)
1172 {
1173         EM_DEBUG_FUNC_BEGIN();
1174
1175         static int next_handle = 0;
1176         int ret = false;
1177         int err = EMAIL_ERROR_NONE;
1178
1179         if ( handle == NULL ) {
1180                 EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed : handle is NULL");
1181                 err = EMAIL_ERROR_INVALID_PARAM;
1182                 goto FINISH_OFF;
1183         }
1184
1185         if ( vconf_get_int(VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE, &next_handle)  != 0 ) {
1186                 EM_DEBUG_EXCEPTION("vconf_get_int failed");
1187                 if ( next_handle != 0 ) {
1188                         err = EMAIL_ERROR_GCONF_FAILURE;
1189                         goto FINISH_OFF;
1190                 }
1191         }
1192
1193         EM_DEBUG_LOG(">>>>>> VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE : get lastest handle[%d]", next_handle);
1194
1195         /*  set the value of the handle for active sync */
1196         next_handle--;
1197         if ( next_handle < ACTIVE_SYNC_HANDLE_BOUNDARY ) {
1198                 next_handle = ACTIVE_SYNC_HANDLE_INIT_VALUE;
1199         }
1200         if ( vconf_set_int(VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE, next_handle) != 0) {
1201                 EM_DEBUG_EXCEPTION("vconf_set_int failed");
1202                 err = EMAIL_ERROR_GCONF_FAILURE;
1203                 goto FINISH_OFF;
1204         }
1205         ret = true;
1206         *handle = next_handle;
1207         EM_DEBUG_LOG(">>>>>> return next handle[%d]", *handle);
1208
1209 FINISH_OFF:
1210         if ( error != NULL ) {
1211                 *error = err;
1212         }
1213
1214         return ret;
1215 }
1216
1217 /* thread with task queue generic functions */
1218
1219 pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER;
1220
1221 email_thread_handle_t* em_thread_create(void *(*thread_exit)(void*), void *arg)
1222 {
1223         pthread_mutex_lock(&g_mu);
1224
1225         email_thread_handle_t* thd_handle = (email_thread_handle_t*) calloc (1,sizeof (email_thread_handle_t));
1226         if (!thd_handle) {
1227                 EM_DEBUG_EXCEPTION ("out of memory");
1228                 goto FINISH_OFF;
1229         }
1230
1231         thd_handle->q = g_queue_new ();
1232         if (!thd_handle->q) {
1233                 EM_DEBUG_EXCEPTION ("g_queue_new failed");
1234                 goto FINISH_OFF;
1235         }
1236         g_queue_init (thd_handle->q);
1237
1238         int ret = pthread_mutex_init (&thd_handle->mu, NULL);
1239         if (ret == -1) {
1240                 EM_DEBUG_EXCEPTION ("pthread_mutex_init failed [%d]", errno);
1241                 goto FINISH_OFF;
1242         }
1243
1244         ret = pthread_cond_init (&thd_handle->cond, NULL);
1245         if (ret == -1) {
1246                 EM_DEBUG_EXCEPTION ("pthread_cond_init failed [%d]", errno);
1247                 goto FINISH_OFF;
1248         }
1249
1250         thd_handle->thread_exit     = thread_exit;
1251         thd_handle->thread_exit_arg = arg;
1252
1253         pthread_mutex_unlock (&g_mu);
1254         return thd_handle;
1255
1256 FINISH_OFF:
1257         pthread_mutex_unlock (&g_mu);
1258         em_thread_destroy (thd_handle);
1259         return NULL;
1260 }
1261
1262 void em_thread_destroy (email_thread_handle_t* thd_handle)
1263 {
1264         if (!thd_handle) return;
1265
1266         if (!g_queue_is_empty (thd_handle->q)) {
1267                 EM_DEBUG_EXCEPTION ("delete q item routine here");
1268         }
1269
1270         pthread_mutex_destroy (&thd_handle->mu);
1271         pthread_cond_destroy (&thd_handle->cond);
1272
1273         EM_SAFE_FREE (thd_handle);
1274 }
1275
1276 typedef struct {
1277         void *(*thread_func)(void*);    /* thread main function */
1278         void *(*destroy)(void*);                /* destroyer function */
1279         void *arg;
1280         email_thread_handle_t *thd_handle;
1281 } worker_handle_t;
1282
1283 static void* worker_func (void* arg)
1284 {
1285         if (!arg) {
1286                 EM_DEBUG_EXCEPTION ("PARAMETER NULL");
1287                 return NULL;
1288         }
1289         /* first task is passed by arg */
1290         worker_handle_t* warg = (worker_handle_t*) arg;
1291         email_thread_handle_t *thd_handle = warg->thd_handle;
1292
1293         if (!thd_handle) {
1294                 EM_DEBUG_EXCEPTION ("PARAMETER NULL");
1295                 return NULL;
1296         }
1297
1298         /* consume task in queue and exit */
1299         do {
1300                 /* running thread main function */
1301                 if (warg->thread_func)
1302                         (warg->thread_func) (warg->arg);
1303                 if (warg->destroy)
1304                         (warg->destroy) (warg->arg);
1305                 EM_SAFE_FREE (warg);
1306
1307                 /* if there is a pending job */
1308                 pthread_mutex_lock (&thd_handle->mu);
1309                 warg = g_queue_pop_head (thd_handle->q);
1310                 if (!warg) {
1311                         thd_handle->running = 0;
1312                         pthread_mutex_unlock (&thd_handle->mu);
1313                         (thd_handle->thread_exit) (thd_handle->thread_exit_arg);
1314                         return NULL;
1315                 }
1316                 pthread_mutex_unlock (&thd_handle->mu);
1317         } while (1);
1318
1319         return NULL;
1320 }
1321
1322 void em_thread_run (email_thread_handle_t *thd_handle, void *(*thread_func)(void*), void *(*destroy)(void*), void* arg)
1323 {
1324         if(!thd_handle || !thread_func) {
1325                 EM_DEBUG_EXCEPTION ("invalid param");
1326                 return;
1327         }
1328
1329         worker_handle_t *worker_handle = (worker_handle_t*) calloc (1, sizeof (worker_handle_t));
1330         if (!worker_handle) {
1331                 EM_DEBUG_EXCEPTION ("out of memory");
1332                 return;
1333         }
1334         worker_handle->thread_func = thread_func;
1335         worker_handle->destroy = destroy;
1336         worker_handle->arg  = arg;
1337         worker_handle->thd_handle = thd_handle;
1338
1339         pthread_mutex_lock (&thd_handle->mu);
1340
1341         /* adding task to queue */
1342         if (thd_handle->running) {
1343                 g_queue_push_tail (thd_handle->q, worker_handle);
1344         }
1345         else {
1346                 thd_handle->running = 1;
1347                 pthread_t tid = 0;
1348                 int err = pthread_create (&tid, NULL, worker_func, worker_handle);
1349                 if (err < 0 )
1350                         EM_DEBUG_EXCEPTION ("pthread_create failed [%d]", errno);
1351                 thd_handle->tid = tid;
1352         }
1353         pthread_mutex_unlock (&thd_handle->mu);
1354 }
1355
1356 void em_thread_join (email_thread_handle_t *thd_handle)
1357 {
1358         int err = pthread_join (thd_handle->tid, NULL);
1359         if (err < 0) {
1360                 EM_DEBUG_EXCEPTION ("pthread_join failed [%d]", errno);
1361         }
1362 }
1363
1364 INTERNAL_FUNC int em_fopen(const char *filename, const char *mode, FILE **fp)
1365 {
1366         EM_DEBUG_FUNC_BEGIN("filename : [%s]", filename);
1367
1368         int err = EMAIL_ERROR_NONE;
1369
1370         if (!filename) {
1371                 EM_DEBUG_EXCEPTION("Invalid param");
1372                 err = EMAIL_ERROR_INVALID_PARAM;
1373                 return err;
1374         }
1375
1376         FILE *temp_fp = NULL;
1377         char errno_buf[ERRNO_BUF_SIZE] = {0};
1378
1379         temp_fp = fopen(filename, mode);
1380         if (temp_fp == NULL) {
1381                 EM_DEBUG_EXCEPTION("fopen failed : [%s][%d]", EM_STRERROR(errno_buf), errno);
1382                 if (errno == EACCES || errno == EPERM)
1383                         err = EMAIL_ERROR_PERMISSION_DENIED;
1384                 else if (errno == ENOSPC)
1385                         err = EMAIL_ERROR_MAIL_MEMORY_FULL;
1386                 else
1387                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1388         }
1389
1390         if (fp)
1391                 *fp = temp_fp;
1392         else
1393                 if (temp_fp) fclose(temp_fp);
1394
1395         EM_DEBUG_FUNC_END();
1396         return err;
1397 }
1398
1399 INTERNAL_FUNC int em_open(const char *filename, int oflags, mode_t mode, int *handle)
1400 {
1401         EM_DEBUG_FUNC_BEGIN("filename : [%s]", filename);
1402         int err = EMAIL_ERROR_NONE;
1403
1404         if (!filename) {
1405                 EM_DEBUG_EXCEPTION("Invalid param");
1406                 err = EMAIL_ERROR_INVALID_PARAM;
1407                 return err;
1408         }
1409
1410         int temp_handle = -1;
1411         char errno_buf[ERRNO_BUF_SIZE] = {0};
1412
1413         if (mode)
1414                 temp_handle = open(filename, oflags, mode);
1415         else
1416                 temp_handle = open(filename, oflags);
1417
1418         if (temp_handle < 0) {
1419                 EM_DEBUG_EXCEPTION("open failed : [%s][%d]", EM_STRERROR(errno_buf), errno);
1420                 if (errno == EACCES || errno == EPERM)
1421                         err = EMAIL_ERROR_PERMISSION_DENIED;
1422                 else if (errno == ENOSPC)
1423                         err = EMAIL_ERROR_MAIL_MEMORY_FULL;
1424                 else
1425                         err = EMAIL_ERROR_SYSTEM_FAILURE;
1426         }
1427
1428         if (handle)
1429                 *handle = temp_handle;
1430         else
1431                 if (temp_handle >= 0) close(temp_handle);
1432
1433         EM_DEBUG_FUNC_END();
1434         return err;
1435 }