4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
25 * Created on: 2012. 3. 6.
26 * Author: kyuho.jo@samsung.com
43 #include "email-types.h"
44 #include "email-internal-types.h"
45 #include "email-utilities.h"
47 INTERNAL_FUNC void* em_malloc(int len)
49 /* EM_DEBUG_LOG("Memory allocation size[%d] bytes", len); */
51 EM_DEBUG_LOG ("len should be positive.[%d]", len);
55 void *p = calloc(1,len);
57 EM_DEBUG_PERROR("malloc failed");
63 INTERNAL_FUNC void* em_memdup(void* src, int len)
65 /* EM_DEBUG_LOG("Memory allocation size[%d] bytes", len); */
67 EM_DEBUG_LOG ("len should be positive.[%d]", len);
71 void *p = calloc(1,len);
73 EM_DEBUG_EXCEPTION("malloc failed");
83 /* remove left space, tab, CR, L */
84 INTERNAL_FUNC char *em_trim_left(char *str)
86 char *p, *temp_buffer = NULL;
88 /* EM_DEBUG_FUNC_BEGIN() */
89 if (!str) return NULL;
92 while (*p && (*p == ' ' || *p == '\t' || *p == LF || *p == CR)) p++;
96 temp_buffer = EM_SAFE_STRDUP(p);
98 strncpy(str, temp_buffer, EM_SAFE_STRLEN(str));
99 str[EM_SAFE_STRLEN(temp_buffer)] = NULL_CHAR;
101 EM_SAFE_FREE(temp_buffer);
106 /* remove right space, tab, CR, L */
107 INTERNAL_FUNC char *em_trim_right(char *str)
111 /* EM_DEBUG_FUNC_BEGIN() */
112 if (!str) return NULL;
114 p = str+EM_SAFE_STRLEN(str)-1;
115 while ((p >= str) && (*p == ' ' || *p == '\t' || *p == LF || *p == CR))
124 INTERNAL_FUNC char* em_upper_string(char *str)
134 INTERNAL_FUNC char* em_lower_string(char *str)
144 INTERNAL_FUNC int em_upper_path(char *path)
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) {
153 if (path[i] == '-') is_utf7 = 0;
156 path[i] = toupper(path[i]);
164 INTERNAL_FUNC void em_skip_whitespace(char *addr_str, char **pAddr)
166 EM_DEBUG_FUNC_BEGIN("addr_str[%p]", addr_str);
170 char *str = addr_str;
171 char ptr[EM_SAFE_STRLEN(str)+1] ;
175 for (i = 0; str[i] != NULL_CHAR ; i++) {
176 if (str[i] != SPACE && str[i] != TAB && str[i] != CR && str[i] != LF)
181 *pAddr = EM_SAFE_STRDUP(ptr);
182 EM_DEBUG_FUNC_END("ptr[%s]", ptr);
185 INTERNAL_FUNC void em_skip_whitespace_without_alias(char *addr_str, char **pAddr)
187 EM_DEBUG_FUNC_BEGIN("addr_str[%p]", addr_str);
191 char *str = addr_str;
192 char ptr[EM_SAFE_STRLEN(addr_str) + 1];
194 char *first_qu = NULL;
195 char *last_qu = NULL;
196 char *first_c = NULL;
199 // find first and last quatation
200 for (i = 0; str[i] != '\0'; i++) {
201 if (!first_qu && str[i] == '\"')
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)
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)
221 } else if (first_qu < str + i && str + i < last_qu) {
222 if (str + i < first_c || str + i > last_c)
229 ptr[j++] = NULL_CHAR;
231 *pAddr = EM_SAFE_STRDUP(ptr);
232 EM_DEBUG_FUNC_END("ptr[%s]", ptr);
235 INTERNAL_FUNC char* em_skip_whitespace_without_strdup(char *source_string)
237 EM_DEBUG_FUNC_BEGIN("source_string[%p]", source_string);
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) */
248 EM_DEBUG_FUNC_END("i[%d]", i);
249 return source_string + i;
252 INTERNAL_FUNC char* em_replace_all_string(char *source_string, char *old_string, char *new_string)
254 EM_DEBUG_FUNC_BEGIN();
255 char *result_buffer = NULL;
257 int old_str_length = 0;
258 int new_str_length = 0;
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);
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);
270 EM_DEBUG_LOG("source_string is invalid");
274 result_buffer = calloc(src_len+1, sizeof(char));
275 if (!result_buffer) {
276 EM_DEBUG_EXCEPTION("calloc failed");
280 if (!strstr(source_string, old_string)) {
281 memcpy(result_buffer, source_string, src_len);
282 return result_buffer;
285 realloc_len = src_len + 1;
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) {
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");
299 memcpy(&result_buffer[j], new_string, new_str_length);
303 result_buffer[j] = source_string[i];
310 result_buffer[j] = '\0';
312 result_buffer[realloc_len-1] = '\0';
315 return result_buffer;
318 INTERNAL_FUNC char* em_replace_string(char *source_string, char *old_string, char *new_string)
320 EM_DEBUG_FUNC_BEGIN();
321 char *result_buffer = NULL;
323 int buffer_length = 0;
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);
329 p = strstr(source_string, old_string);
332 EM_DEBUG_EXCEPTION("old_string not found in source_string");
333 EM_DEBUG_FUNC_END("return NULL");
337 buffer_length = EM_SAFE_STRLEN(source_string) + 1024;
338 result_buffer = (char *)em_malloc(buffer_length);
340 if (!result_buffer) {
341 EM_DEBUG_EXCEPTION("em_malloc failed");
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*/
348 EM_DEBUG_FUNC_END("result_buffer[%p]", result_buffer);
349 return result_buffer;
352 INTERNAL_FUNC int em_replace_string_ex(char **input_source_string, char *input_old_string, char *input_new_string)
354 EM_DEBUG_FUNC_BEGIN();
356 int buffer_length = 0;
357 int old_string_length = 0;
358 int new_string_length = 0;
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;
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);
371 source_string = *input_source_string;
373 found_pos = strstr(source_string, input_old_string);
375 if (found_pos == NULL) {
376 err = EMAIL_ERROR_DATA_NOT_FOUND;
380 old_string_length = EM_SAFE_STRLEN(input_old_string);
381 new_string_length = EM_SAFE_STRLEN(input_new_string);
386 found_pos = strstr(found_pos, input_old_string);
389 buffer_length = EM_SAFE_STRLEN(source_string) + ((new_string_length - old_string_length) * match_count) + 50;
391 result_buffer = (char*)malloc(buffer_length);
393 if (!result_buffer) {
394 err = EMAIL_ERROR_OUT_OF_MEMORY;
397 memset(result_buffer, 0 , buffer_length);
399 cursor_of_source_string = source_string;
400 cursor_of_result_buffer = result_buffer;
401 found_pos = strstr(source_string, input_old_string);
404 memcpy(cursor_of_result_buffer, cursor_of_source_string, found_pos - cursor_of_source_string);
406 cursor_of_result_buffer = result_buffer + EM_SAFE_STRLEN(result_buffer);
407 cursor_of_source_string = found_pos + old_string_length;
409 memcpy(cursor_of_result_buffer, input_new_string, new_string_length);
411 cursor_of_result_buffer = result_buffer + EM_SAFE_STRLEN(result_buffer);
414 found_pos = strstr(found_pos, input_old_string);
417 EM_SAFE_STRCAT(result_buffer, cursor_of_source_string);
419 EM_SAFE_FREE(*input_source_string);
420 *input_source_string = result_buffer;
424 EM_DEBUG_FUNC_END("err[%d]", err);
428 INTERNAL_FUNC int em_get_file_name_from_file_path(char *input_source_file_path, char **output_file_name)
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, };
439 if (!input_source_file_path || !output_file_name) {
440 EM_DEBUG_EXCEPTION("Invalid Parameter");
441 err = EMAIL_ERROR_INVALID_PARAM;
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;
449 while(pos_on_string >= 0 && input_source_file_path[pos_on_string] != '/') {
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);
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);
463 *output_file_name = EM_SAFE_STRDUP(file_name_string);
466 EM_DEBUG_FUNC_END("err = [%d]", err);
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)
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, };
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;
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;
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;
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);
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);
517 EM_DEBUG_LOG_SEC("*file_name_string [%s] pos_on_string [%d]", file_name_string, pos_on_string);
519 *output_file_name = EM_SAFE_STRDUP(file_name_string);
520 *output_extension = EM_SAFE_STRDUP(extension_string);
523 EM_DEBUG_FUNC_END("err = [%d]", err);
527 INTERNAL_FUNC char *em_get_extension_from_file_path(char *source_file_path, int *err_code)
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;
533 if (!source_file_path) {
534 EM_DEBUG_EXCEPTION("Invalid Parameter");
535 err = EMAIL_ERROR_INVALID_PARAM;
539 pos_on_string = EM_SAFE_STRLEN(source_file_path) - 1;
541 while(pos_on_string >= 0 && source_file_path[pos_on_string] != '.') {
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;
548 EM_DEBUG_LOG("*extension [%s] pos_on_string [%d]", extension, pos_on_string);
557 INTERNAL_FUNC char *em_shrink_filename(char *fname, int size_limit)
559 EM_DEBUG_FUNC_BEGIN("fname[%s], size_limit[%d]", fname, size_limit);
561 char *modified_name = NULL;
562 char *extension = NULL;
564 modified_name = em_malloc(sizeof(char)*size_limit);
565 if (!modified_name) {
569 char *tmp_ext = NULL;
570 char *tmp_name = NULL;
571 char *tmp_name_strip = NULL;
573 extension = em_get_extension_from_file_path(fname, NULL);
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;
580 tmp_ext = em_malloc(sizeof(char)*(ext_len+1));
581 if (tmp_ext == NULL) {
582 EM_DEBUG_EXCEPTION("em_malloc failed");
586 tmp_name = em_malloc(sizeof(char)*(name_len+1));
587 if (tmp_name == NULL) {
588 EM_DEBUG_EXCEPTION("em_malloc failed");
592 tmp_name_strip = em_malloc(sizeof(char)*name_strip_len);
593 if (tmp_name_strip == NULL) {
594 EM_DEBUG_EXCEPTION("em_malloc failed");
598 snprintf(tmp_ext, sizeof(char)*(ext_len+1), "%s", extension);
599 snprintf(tmp_name, sizeof(char)*(name_len+1), "%s", fname);
601 EM_DEBUG_LOG(">>>>> extention [%s]", tmp_ext);
602 EM_DEBUG_LOG(">>>>> name [%s]", tmp_name);
604 if (EM_SAFE_STRLEN(extension) > EM_SAFE_STRLEN(fname) - EM_SAFE_STRLEN(extension)) {
605 snprintf(modified_name, sizeof(char)*size_limit, "%s", fname);
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);
611 snprintf(modified_name, sizeof(char)*size_limit, "%s", fname);
615 snprintf(modified_name, sizeof(char)*size_limit, "%s", fname);
620 EM_SAFE_FREE(tmp_ext);
621 EM_SAFE_FREE(tmp_name);
622 EM_SAFE_FREE(tmp_name_strip);
626 return modified_name;
629 INTERNAL_FUNC int em_get_encoding_type_from_file_path(const char *input_file_path, char **output_encoding_type)
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;
635 int enf_of_string = 0;
636 int result_string_length = 0;
637 char *result_encoding_type = NULL;
639 if (!input_file_path || !output_encoding_type) {
640 EM_DEBUG_EXCEPTION("Invalid Parameter");
641 err = EMAIL_ERROR_INVALID_PARAM;
645 enf_of_string = pos_of_filename = EM_SAFE_STRLEN(input_file_path);
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;
652 if(pos_of_filename != 0)
653 pos_of_filename += 2;
655 if(pos_of_dot != 0 && pos_of_dot > pos_of_filename)
656 result_string_length = pos_of_dot - pos_of_filename;
658 result_string_length = enf_of_string - pos_of_filename;
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;
666 memcpy(result_encoding_type, input_file_path + pos_of_filename, result_string_length);
668 EM_DEBUG_LOG("result_encoding_type [%s]", result_encoding_type);
670 *output_encoding_type = result_encoding_type;
673 EM_DEBUG_FUNC_END("err [%d]", err);
677 INTERNAL_FUNC int em_get_content_type_from_extension_string(const char *extension_string, int *err_code)
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};
683 if (!extension_string) {
684 EM_DEBUG_EXCEPTION("Invalid Parameter");
685 err = EMAIL_ERROR_INVALID_PARAM;
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) {
707 result_content_type = TYPEIMAGE;
710 result_content_type = TYPEPKCS7_SIGN;
713 result_content_type = TYPEPKCS7_MIME;
716 result_content_type = TYPEPGP;
726 return result_content_type;
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 ")"
737 #define EMAIL_DOMAIN_LITERAL "\\[(" EMAIL_DTEXT "|" EMAIL_QUOTED_PAIR ")*\\]" /* literal match for "[" and "]"*/
738 #define EMAIL_SUB_DOMAIN "(" EMAIL_ATOM "|" EMAIL_DOMAIN_LITERAL ")"
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 ")"
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:]]*)*)$"
748 static int em_verify_email_address_by_using_regex(char *address)
750 EM_DEBUG_FUNC_BEGIN_SEC("address[%s]", address);
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;
757 regmatch_t *pmatch = NULL;
759 if(!address || EM_SAFE_STRLEN(address) == 0) {
760 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
761 return EMAIL_ERROR_INVALID_PARAM;
764 reg_rule = EMAIL_ADDRESS;
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;
772 alias_len = EM_SAFE_STRLEN(address) + 1;
773 pmatch = (regmatch_t *) em_malloc (alias_len * sizeof (regmatch_t));
775 EM_DEBUG_EXCEPTION("em_malloc error");
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;
786 regfree(&alias_list_regex);
787 EM_SAFE_FREE (pmatch);
789 EM_DEBUG_FUNC_END("err [%d]", error);
793 static int em_verify_email_address_without_regex(char *address)
795 EM_DEBUG_FUNC_BEGIN_SEC("address[%s]", address);
796 char *local_address = NULL;
797 char *address_start = NULL;
799 char *local_part = NULL;
801 char *saveptr = NULL;
803 int address_length = 0;
805 int error = EMAIL_ERROR_NONE;
808 EM_DEBUG_LOG_SEC("address [%s]", address);
810 if (address == NULL) {
811 EM_DEBUG_EXCEPTION("Invalid parameter");
812 error = EMAIL_ERROR_INVALID_PARAM;
816 local_address = strdup(address);
818 address_start = local_address;
820 while ((cur = strchr(address_start, '\"'))) {
821 address_start = cur + 1;
822 if (local_address >= cur - 1 || *(cur - 1) != '\\')
827 error = EMAIL_ERROR_INVALID_ADDRESS;
828 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
832 if ((cur = strchr(address_start, '<'))) {
833 char *close_pos = NULL;
835 address_start = cur + 1;
836 close_pos = address_start;
838 while ((cur = strchr(close_pos, '>'))) {
842 if (address_start == close_pos) {
843 error = EMAIL_ERROR_INVALID_ADDRESS;
844 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
848 address_start[close_pos - 1 - address_start] = '\0';
851 EM_DEBUG_LOG_SEC("address_start [%s]", address_start);
853 address_length = EM_SAFE_STRLEN(address_start);
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");
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");
870 local_part = strtok_r(address_start, "@", &saveptr);
872 EM_DEBUG_LOG("local_part [%s]", local_part);
874 if (local_part == NULL || EM_SAFE_STRLEN(local_part) == 0) {
875 error = EMAIL_ERROR_INVALID_ADDRESS;
879 domain = strtok_r(NULL, "@", &saveptr);
881 EM_DEBUG_LOG("domain [%s]", domain);
883 if (domain == NULL || EM_SAFE_STRLEN(domain) < 3) {
884 error = EMAIL_ERROR_INVALID_ADDRESS;
885 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
889 if (strchr(domain, '.') == NULL) {
890 error = EMAIL_ERROR_INVALID_ADDRESS;
891 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
895 if (!isalpha(local_part[0])) {
896 error = EMAIL_ERROR_INVALID_ADDRESS;
897 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_ADDRESS");
902 EM_SAFE_FREE(local_address);
904 EM_DEBUG_FUNC_END("error [%d]", error);
908 INTERNAL_FUNC int em_verify_email_address(char *address)
910 EM_DEBUG_FUNC_BEGIN("address[%p]", address);
911 int error = EMAIL_ERROR_NONE;
912 char *result_locale = NULL;
914 setlocale(LC_ALL, "");
916 result_locale = setlocale(LC_ALL, NULL);
918 EM_DEBUG_LOG("LC_ALL[%s]" , result_locale);
920 if ( EM_SAFE_STRCMP(result_locale, "or_IN.UTF-8") == 0)
921 error = em_verify_email_address_without_regex(address);
923 error = em_verify_email_address_by_using_regex(address);
925 EM_DEBUG_FUNC_END("error [%d]", error);
929 INTERNAL_FUNC int em_verify_email_address_of_mail_data (email_mail_data_t *mail_data)
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;
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]);
946 EM_DEBUG_FUNC_END("err [%d]", err);
950 INTERNAL_FUNC int em_verify_email_address_of_mail_tbl(emstorage_mail_tbl_t *input_mail_tbl)
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;
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]);
967 EM_DEBUG_FUNC_END("err [%d]", err);
971 INTERNAL_FUNC int em_find_tag_for_thread_view(char *subject, int *result)
973 EM_DEBUG_FUNC_BEGIN();
974 int error_code = EMAIL_ERROR_NONE;
975 char *copy_of_subject = NULL;
977 EM_IF_NULL_RETURN_VALUE(subject, EMAIL_ERROR_INVALID_PARAM);
978 EM_IF_NULL_RETURN_VALUE(result, EMAIL_ERROR_INVALID_PARAM);
982 copy_of_subject = EM_SAFE_STRDUP(subject);
984 if (copy_of_subject == NULL) {
985 EM_DEBUG_EXCEPTION("strdup is failed.");
989 em_upper_string(copy_of_subject);
990 EM_DEBUG_LOG_SEC("em_upper_string result : %s\n", copy_of_subject);
992 if (strstr(copy_of_subject, "RE:") == NULL) {
993 if (strstr(copy_of_subject, "FWD:") == NULL) {
994 if (strstr(copy_of_subject, "FW:") != NULL)
1004 EM_SAFE_FREE(copy_of_subject);
1006 EM_DEBUG_FUNC_END("result : %d", *result);
1011 INTERNAL_FUNC int em_find_pos_stripped_subject_for_thread_view(char *subject, char *stripped_subject, int stripped_subject_buffer_size)
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;
1016 char *copy_of_subject = NULL, *curpos = NULL, *result;
1018 EM_IF_NULL_RETURN_VALUE(subject, EMAIL_ERROR_INVALID_PARAM);
1019 EM_IF_NULL_RETURN_VALUE(stripped_subject, EMAIL_ERROR_INVALID_PARAM);
1021 copy_of_subject = EM_SAFE_STRDUP(subject);
1023 if (copy_of_subject == NULL) {
1024 EM_DEBUG_EXCEPTION("strdup is failed");
1028 em_upper_string(copy_of_subject);
1029 curpos = copy_of_subject;
1033 while ((result = g_strrstr(curpos, "RE:")) != NULL) {
1034 curpos = result + 3;
1035 EM_DEBUG_LOG_SEC("RE result : %s", curpos);
1038 while ((result = g_strrstr(curpos, "FWD:")) != NULL) {
1039 curpos = result + 4;
1040 EM_DEBUG_LOG_SEC("FWD result : %s", curpos);
1043 while ((result = g_strrstr(curpos, "FW:")) != NULL) {
1044 curpos = result + 3;
1045 EM_DEBUG_LOG_SEC("FW result : %s", curpos);
1048 while (curpos != NULL && *curpos == ' ') {
1052 gap = curpos - copy_of_subject;
1054 EM_SAFE_STRNCPY(stripped_subject, subject + gap, stripped_subject_buffer_size);
1057 EM_SAFE_FREE(copy_of_subject);
1059 EM_DEBUG_FUNC_END("error_code[%d]", error_code);
1067 INTERNAL_FUNC int em_encode_base64(char *src, unsigned long src_len, char **enc, unsigned long* enc_len, int *err_code)
1069 EM_DEBUG_FUNC_BEGIN();
1071 unsigned char *content;
1072 int ret = true, err = EMAIL_ERROR_NONE;
1074 if (err_code != NULL) {
1075 *err_code = EMAIL_ERROR_NONE;
1078 content = rfc822_binary(src, src_len, enc_len);
1081 *enc = (char *)content;
1083 err = EMAIL_ERROR_UNKNOWN;
1090 EM_DEBUG_FUNC_END();
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)
1099 unsigned char *text = enc_text;
1100 unsigned long size = enc_len;
1101 unsigned char *content;
1102 int ret = true, err = EMAIL_ERROR_NONE;
1104 if (err_code != NULL) {
1105 *err_code = EMAIL_ERROR_NONE;
1108 EM_DEBUG_FUNC_BEGIN();
1110 content = rfc822_base64(text, size, dec_len);
1112 *dec_text = (char *)content;
1115 err = EMAIL_ERROR_UNKNOWN;
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)
1127 EM_DEBUG_FUNC_BEGIN();
1128 emstorage_account_tbl_t *account_tbl_data = NULL;
1130 int err= EMAIL_ERROR_NONE;
1132 if (account_server_type == NULL ) {
1133 EM_DEBUG_EXCEPTION("account_server_type is NULL");
1134 err = EMAIL_ERROR_INVALID_PARAM;
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);
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;
1154 if ( account_tbl_data != NULL ) {
1155 emstorage_free_account(&account_tbl_data, 1, NULL);
1157 if ( error != NULL ) {
1165 #include <dbus/dbus.h>
1167 #define ACTIVE_SYNC_HANDLE_INIT_VALUE (-1)
1168 #define ACTIVE_SYNC_HANDLE_BOUNDARY (-100000000)
1171 INTERNAL_FUNC int em_get_handle_for_activesync(int *handle, int *error)
1173 EM_DEBUG_FUNC_BEGIN();
1175 static int next_handle = 0;
1177 int err = EMAIL_ERROR_NONE;
1179 if ( handle == NULL ) {
1180 EM_DEBUG_EXCEPTION("em_get_handle_for_activesync failed : handle is NULL");
1181 err = EMAIL_ERROR_INVALID_PARAM;
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;
1193 EM_DEBUG_LOG(">>>>>> VCONFKEY_EMAIL_SERVICE_ACTIVE_SYNC_HANDLE : get lastest handle[%d]", next_handle);
1195 /* set the value of the handle for active sync */
1197 if ( next_handle < ACTIVE_SYNC_HANDLE_BOUNDARY ) {
1198 next_handle = ACTIVE_SYNC_HANDLE_INIT_VALUE;
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;
1206 *handle = next_handle;
1207 EM_DEBUG_LOG(">>>>>> return next handle[%d]", *handle);
1210 if ( error != NULL ) {
1217 /* thread with task queue generic functions */
1219 pthread_mutex_t g_mu = PTHREAD_MUTEX_INITIALIZER;
1221 email_thread_handle_t* em_thread_create(void *(*thread_exit)(void*), void *arg)
1223 pthread_mutex_lock(&g_mu);
1225 email_thread_handle_t* thd_handle = (email_thread_handle_t*) calloc (1,sizeof (email_thread_handle_t));
1227 EM_DEBUG_EXCEPTION ("out of memory");
1231 thd_handle->q = g_queue_new ();
1232 if (!thd_handle->q) {
1233 EM_DEBUG_EXCEPTION ("g_queue_new failed");
1236 g_queue_init (thd_handle->q);
1238 int ret = pthread_mutex_init (&thd_handle->mu, NULL);
1240 EM_DEBUG_EXCEPTION ("pthread_mutex_init failed [%d]", errno);
1244 ret = pthread_cond_init (&thd_handle->cond, NULL);
1246 EM_DEBUG_EXCEPTION ("pthread_cond_init failed [%d]", errno);
1250 thd_handle->thread_exit = thread_exit;
1251 thd_handle->thread_exit_arg = arg;
1253 pthread_mutex_unlock (&g_mu);
1257 pthread_mutex_unlock (&g_mu);
1258 em_thread_destroy (thd_handle);
1262 void em_thread_destroy (email_thread_handle_t* thd_handle)
1264 if (!thd_handle) return;
1266 if (!g_queue_is_empty (thd_handle->q)) {
1267 EM_DEBUG_EXCEPTION ("delete q item routine here");
1270 pthread_mutex_destroy (&thd_handle->mu);
1271 pthread_cond_destroy (&thd_handle->cond);
1273 EM_SAFE_FREE (thd_handle);
1277 void *(*thread_func)(void*); /* thread main function */
1278 void *(*destroy)(void*); /* destroyer function */
1280 email_thread_handle_t *thd_handle;
1283 static void* worker_func (void* arg)
1286 EM_DEBUG_EXCEPTION ("PARAMETER NULL");
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;
1294 EM_DEBUG_EXCEPTION ("PARAMETER NULL");
1298 /* consume task in queue and exit */
1300 /* running thread main function */
1301 if (warg->thread_func)
1302 (warg->thread_func) (warg->arg);
1304 (warg->destroy) (warg->arg);
1305 EM_SAFE_FREE (warg);
1307 /* if there is a pending job */
1308 pthread_mutex_lock (&thd_handle->mu);
1309 warg = g_queue_pop_head (thd_handle->q);
1311 thd_handle->running = 0;
1312 pthread_mutex_unlock (&thd_handle->mu);
1313 (thd_handle->thread_exit) (thd_handle->thread_exit_arg);
1316 pthread_mutex_unlock (&thd_handle->mu);
1322 void em_thread_run (email_thread_handle_t *thd_handle, void *(*thread_func)(void*), void *(*destroy)(void*), void* arg)
1324 if(!thd_handle || !thread_func) {
1325 EM_DEBUG_EXCEPTION ("invalid param");
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");
1334 worker_handle->thread_func = thread_func;
1335 worker_handle->destroy = destroy;
1336 worker_handle->arg = arg;
1337 worker_handle->thd_handle = thd_handle;
1339 pthread_mutex_lock (&thd_handle->mu);
1341 /* adding task to queue */
1342 if (thd_handle->running) {
1343 g_queue_push_tail (thd_handle->q, worker_handle);
1346 thd_handle->running = 1;
1348 int err = pthread_create (&tid, NULL, worker_func, worker_handle);
1350 EM_DEBUG_EXCEPTION ("pthread_create failed [%d]", errno);
1351 thd_handle->tid = tid;
1353 pthread_mutex_unlock (&thd_handle->mu);
1356 void em_thread_join (email_thread_handle_t *thd_handle)
1358 int err = pthread_join (thd_handle->tid, NULL);
1360 EM_DEBUG_EXCEPTION ("pthread_join failed [%d]", errno);
1364 INTERNAL_FUNC int em_fopen(const char *filename, const char *mode, FILE **fp)
1366 EM_DEBUG_FUNC_BEGIN("filename : [%s]", filename);
1368 int err = EMAIL_ERROR_NONE;
1371 EM_DEBUG_EXCEPTION("Invalid param");
1372 err = EMAIL_ERROR_INVALID_PARAM;
1376 FILE *temp_fp = NULL;
1377 char errno_buf[ERRNO_BUF_SIZE] = {0};
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;
1387 err = EMAIL_ERROR_SYSTEM_FAILURE;
1393 if (temp_fp) fclose(temp_fp);
1395 EM_DEBUG_FUNC_END();
1399 INTERNAL_FUNC int em_open(const char *filename, int oflags, mode_t mode, int *handle)
1401 EM_DEBUG_FUNC_BEGIN("filename : [%s]", filename);
1402 int err = EMAIL_ERROR_NONE;
1405 EM_DEBUG_EXCEPTION("Invalid param");
1406 err = EMAIL_ERROR_INVALID_PARAM;
1410 int temp_handle = -1;
1411 char errno_buf[ERRNO_BUF_SIZE] = {0};
1414 temp_handle = open(filename, oflags, mode);
1416 temp_handle = open(filename, oflags);
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;
1425 err = EMAIL_ERROR_SYSTEM_FAILURE;
1429 *handle = temp_handle;
1431 if (temp_handle >= 0) close(temp_handle);
1433 EM_DEBUG_FUNC_END();