Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-core / email-core-mailbox-sync.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 /******************************************************************************
24  * File :  email-core-mailbox-sync.c
25  * Desc :  Mail Header Sync
26  *
27  * Auth :
28  *****************************************************************************/
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <vconf.h>
34
35 #include "email-internal-types.h"
36
37 #ifdef __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__
38 #include <vconf/vconf-internal-email-keys.h>
39 #endif /* __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__ */
40
41 #include "c-client.h"
42 #include "lnx_inc.h"
43
44 #include "email-utilities.h"
45 #include "email-convert.h"
46 #include "email-core-mailbox-sync.h"
47 #include "email-core-global.h"
48 #include "email-core-imap-mailbox.h"
49 #include "email-core-event.h"
50 #include "email-core-mailbox.h"
51 #include "email-core-mail.h"
52 #include "email-core-mime.h"
53 #include "email-core-utils.h"
54 #include "email-core-smtp.h"
55 #include "email-core-account.h"
56 #include "email-storage.h"
57 #include "email-core-signal.h"
58 #include "flstring.h"
59 #include "email-debug-log.h"
60
61 #define MAX_CHARSET_VALUE 256
62
63 static char g_append_uid_rsp[129]; /* added for getting server response  */
64
65 extern void imap_parse_body_structure (MAILSTREAM *stream, BODY *body, unsigned char **txtptr, IMAPPARSEDREPLY *reply);
66
67 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
68 static void emcore_free_email_partial_buffer(email_partial_buffer **partial_buffer, int item_count);
69 static email_partial_buffer *emcore_get_response_from_server (NETSTREAM *nstream, char *tag, IMAPPARSEDREPLY **reply, int input_download_size, int item_count);
70 static int emcore_initiate_pbd(MAILSTREAM *stream, int account_id, int mail_id, char *uid, int mailbox_id, int *err_code);
71 #endif
72
73 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
74 static char g_append_uid_rsp[129]; /* added for getting server response  */
75 #endif
76
77
78 int pop3_mail_calc_rfc822_size(MAILSTREAM *stream, int msgno, int *size, int *err_code)
79 {
80         EM_DEBUG_FUNC_BEGIN();
81
82         int ret = false;
83         int err = EMAIL_ERROR_NONE;
84
85         POP3LOCAL *pop3local = NULL;
86         char command[16];
87         char *response = NULL;
88
89         if (!stream || !size) {
90                 EM_DEBUG_EXCEPTION(" stream[%p], msgno[%d], size[%p]\n", stream, msgno, size);
91
92                 err = EMAIL_ERROR_INVALID_PARAM;
93                 goto FINISH_OFF;
94         }
95
96         if (!(pop3local = stream->local) || !pop3local->netstream) {
97                 err = EMAIL_ERROR_INVALID_STREAM;
98                 goto FINISH_OFF;
99         }
100
101         memset(command, 0x00, sizeof(command));
102
103         SNPRINTF(command, sizeof(command), "LIST %d\015\012", msgno);
104
105         /* EM_DEBUG_LOG(" [POP3] >>> %s", command); */
106
107         /*  send command  :  get rfc822 size by msgno */
108         if (!net_sout(pop3local->netstream, command, (int)EM_SAFE_STRLEN(command))) {
109                 EM_DEBUG_EXCEPTION(" net_sout failed...");
110
111                 err = EMAIL_ERROR_INVALID_RESPONSE;
112                 goto FINISH_OFF;
113         }
114
115         /*  receive response */
116         if (!(response = net_getline(pop3local->netstream))) {
117                 err = EMAIL_ERROR_CONNECTION_BROKEN;            /* EMAIL_ERROR_UNKNOWN; */
118                 goto FINISH_OFF;
119         }
120
121         /* EM_DEBUG_LOG(" [POP3] <<< %s", response); */
122
123         if (*response == '+') {         /*  "+ OK" */
124                 char *p = NULL;
125
126                 if (!(p = strchr(response + strlen("+OK "), ' '))) {
127                         err = EMAIL_ERROR_INVALID_RESPONSE;
128                         goto FINISH_OFF;
129                 }
130
131                 *size = atoi(p + 1);
132         }
133         else if (*response == '-') {            /*  "- ERR" */
134                 err = EMAIL_ERROR_POP3_LIST_FAILURE;
135                 goto FINISH_OFF;
136         }
137         else {
138                 err = EMAIL_ERROR_INVALID_RESPONSE;
139                 goto FINISH_OFF;
140         }
141
142         ret = true;
143
144 FINISH_OFF:
145         EM_SAFE_FREE(response);
146
147         if (err_code  != NULL)
148                 *err_code = err;
149
150         EM_DEBUG_FUNC_END("ret [%d]", ret);
151         return ret;
152 }
153
154 int imap4_mail_calc_rfc822_size(MAILSTREAM *stream, int msgno, int *size, int *err_code)
155 {
156         EM_DEBUG_FUNC_BEGIN();
157
158         int ret = false;
159         int err = EMAIL_ERROR_NONE;
160
161         IMAPLOCAL *imaplocal = NULL;
162         char tag[32], command[128];
163         char *response = NULL;
164
165         if (!stream || !size) {
166                 EM_DEBUG_EXCEPTION("stream[%p], msgno[%d], size[%p]", stream, msgno, size);
167
168                 err = EMAIL_ERROR_INVALID_PARAM;
169                 goto FINISH_OFF;
170         }
171
172         if (!(imaplocal = stream->local) || !imaplocal->netstream) {
173                 err = EMAIL_ERROR_INVALID_STREAM;
174                 goto FINISH_OFF;
175         }
176
177         memset(tag, 0x00, sizeof(tag));
178         memset(command, 0x00, sizeof(command));
179
180         SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
181         SNPRINTF(command, sizeof(command), "%s FETCH %d RFC822.SIZE\015\012", tag, msgno);
182
183         /* EM_DEBUG_LOG(" [IMAP4] >>> %s", command); */
184
185         /*  send command  :  get rfc822 size by msgno */
186         if (!net_sout(imaplocal->netstream, command, (int)EM_SAFE_STRLEN(command))) {
187                 EM_DEBUG_EXCEPTION(" net_sout failed...");
188
189                 err = EMAIL_ERROR_INVALID_RESPONSE;
190                 goto FINISH_OFF;
191         }
192
193         while (imaplocal->netstream) {
194                 char *s = NULL;
195                 char *t = NULL;
196
197                 /*  receive response */
198                 if (!(response = net_getline(imaplocal->netstream)))
199                         break;
200
201                 /* EM_DEBUG_LOG(" [IMAP4] <<< %s", response); */
202
203                 if (!strncmp(response, tag, EM_SAFE_STRLEN(tag))) {
204                         if (!strncmp(response + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
205                                 EM_SAFE_FREE(response);
206                                 break;
207                         }
208                         else {          /*  'NO' or 'BAD' */
209                                 err = EMAIL_ERROR_IMAP4_FETCH_SIZE_FAILURE;             /* EMAIL_ERROR_INVALID_RESPONSE; */
210                                 goto FINISH_OFF;
211                         }
212                 }
213                 else {          /*  untagged response */
214                         if (*response == '*') {
215                                 if (!(t = strstr(response, "FETCH (RFC822.SIZE "))) {
216                                         EM_SAFE_FREE(response);
217                                         continue;
218                                 }
219
220                                 s = t + strlen("FETCH (RFC822.SIZE ");
221
222                                 if (!(t = strchr(s, ' '))) {
223                                         err = EMAIL_ERROR_INVALID_RESPONSE;
224                                         goto FINISH_OFF;
225                                 }
226
227                                 *t = '\0';
228
229                                 *size = atoi(s);
230                         }
231                 }
232
233                 EM_SAFE_FREE(response);
234         }
235
236         ret = true;
237
238 FINISH_OFF:
239         EM_SAFE_FREE(response);
240
241         if (err_code  != NULL)
242                 *err_code = err;
243
244         EM_DEBUG_FUNC_END("ret [%d]", ret);
245         return ret;
246 }
247
248 int pop3_mailbox_get_uids(MAILSTREAM *stream, emcore_uid_list** uid_list, int *err_code)
249 {
250         EM_PROFILE_BEGIN(pop3MailboxGetuid);
251         EM_DEBUG_FUNC_BEGIN("stream[%p], uid_list[%p], err_code[%p]", stream, uid_list, err_code);
252
253         int ret = false;
254         int err = EMAIL_ERROR_NONE;
255
256         POP3LOCAL *pop3local = NULL;
257         char command[64];
258         char *response = NULL;
259         emcore_uid_list *uid_elem = NULL;
260
261         if (!stream || !uid_list) {
262                 EM_DEBUG_EXCEPTION("stream[%p], uid_list[%p]n", stream, uid_list);
263                 err = EMAIL_ERROR_INVALID_PARAM;
264                 goto FINISH_OFF;
265         }
266
267         if (!(pop3local = stream->local) || !pop3local->netstream) {
268                 EM_DEBUG_EXCEPTION("invalid POP3 stream detected...");
269                 err = EMAIL_ERROR_INVALID_STREAM;
270                 goto FINISH_OFF;
271         }
272
273         memset(command, 0x00, sizeof(command));
274
275         SNPRINTF(command, sizeof(command), "UIDL\015\012");
276
277 #ifdef FEATURE_CORE_DEBUG
278         EM_DEBUG_LOG(" [POP3] >>> [%s]", command);
279 #endif
280
281         /*  send command  :  get msgno/uid for all message */
282         if (!net_sout(pop3local->netstream, command, (int)EM_SAFE_STRLEN(command))) {
283                 EM_DEBUG_EXCEPTION("net_sout failed...");
284                 err = EMAIL_ERROR_CONNECTION_BROKEN;            /* EMAIL_ERROR_UNKNOWN; */
285                 goto FINISH_OFF;
286         }
287
288         *uid_list = NULL;
289
290         while (pop3local->netstream) {
291                 char *p = NULL;
292
293                 /*  receive response */
294                 if (!(response = net_getline(pop3local->netstream))) {
295                         EM_DEBUG_EXCEPTION("net_getline failed...");
296                         err = EMAIL_ERROR_INVALID_RESPONSE;
297                         goto FINISH_OFF;
298                 }
299
300 #ifdef FEATURE_CORE_DEBUG
301                 EM_DEBUG_LOG(" [POP3] <<< [%s]", response);
302 #endif
303
304                 if (*response == '-') {         /*  "-ERR" */
305                         err = EMAIL_ERROR_POP3_UIDL_FAILURE;            /* EMAIL_ERROR_INVALID_RESPONSE; */
306                         goto FINISH_OFF;
307                 }
308
309                 if (*response == '+') {         /*  "+OK" */
310                         free(response); response = NULL;
311                         continue;
312                 }
313
314                 if (*response == '.') {
315                         free(response); response = NULL;
316                         break;
317                 }
318
319                 if ((p = strchr(response, ' '))) {
320                         *p = '\0';
321
322                         if (!(uid_elem = em_malloc(sizeof(emcore_uid_list)))) {
323                                 EM_DEBUG_EXCEPTION("malloc failed...");
324                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
325                                 goto FINISH_OFF;
326                         }
327
328                         uid_elem->msgno = atoi(response);
329                         uid_elem->uid = EM_SAFE_STRDUP(p + 1);
330
331                         if (*uid_list  != NULL)
332                                 uid_elem->next = *uid_list;             /*  prepend new data to table */
333
334                         *uid_list = uid_elem;
335                 }
336                 else {
337                         err = EMAIL_ERROR_INVALID_RESPONSE;
338                         goto FINISH_OFF;
339                 }
340
341                 free(response); response = NULL;
342         }
343
344         ret = true;
345
346 FINISH_OFF:
347         if (response  != NULL)
348                 free(response);
349
350         if (err_code  != NULL)
351                 *err_code = err;
352
353         EM_PROFILE_END(pop3MailboxGetuid);
354         EM_DEBUG_FUNC_END("ret [%d]", ret);
355         return ret;
356 }
357
358 int imap4_mailbox_get_uids(MAILSTREAM *stream, emcore_uid_list** uid_list, int *err_code)
359 {
360         EM_PROFILE_BEGIN(ImapMailboxGetUids);
361         EM_DEBUG_FUNC_BEGIN("stream[%p], uid_list[%p], err_code[%p]", stream, uid_list, err_code);
362
363         int ret = false;
364         int err = EMAIL_ERROR_NONE;
365
366         IMAPLOCAL *imaplocal = NULL;
367         char tag[16], command[64];
368         char *response = NULL;
369         emcore_uid_list *uid_elem = NULL;
370
371         if (!stream || !uid_list) {
372                 EM_DEBUG_EXCEPTION("stream[%p], uid_list[%p]", stream, uid_list);
373                 err = EMAIL_ERROR_INVALID_PARAM;
374                 goto FINISH_OFF;
375         }
376
377         if (!(imaplocal = stream->local) || !imaplocal->netstream) {
378                 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
379                 err = EMAIL_ERROR_INVALID_PARAM;                /* EMAIL_ERROR_UNKNOWN */
380                 goto FINISH_OFF;
381         }
382
383         if (stream->nmsgs == 0){
384                 err = EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER;
385                 goto FINISH_OFF;
386         }
387         memset(tag, 0x00, sizeof(tag));
388         memset(command, 0x00, sizeof(command));
389
390         SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
391         SNPRINTF(command, sizeof(command), "%s FETCH 1:* (FLAGS UID)\015\012", tag);
392         EM_DEBUG_LOG("COMMAND [%s] \n", command);
393 #ifdef FEATURE_CORE_DEBUG
394         EM_DEBUG_LOG(" [IMAP4] >>> [%s]", command);
395 #endif
396
397         /*  send command  :  get msgno/uid for all message */
398         if (!net_sout(imaplocal->netstream, command, (int)EM_SAFE_STRLEN(command))) {
399                 EM_DEBUG_EXCEPTION(" net_sout failed...\n");
400                 err = EMAIL_ERROR_CONNECTION_BROKEN;
401                 goto FINISH_OFF;
402         }
403
404         *uid_list = NULL;
405
406         while (imaplocal->netstream) {
407                 char *p = NULL;
408                 char *s = NULL;
409                 int seen = 0;
410                 int forwarded = 0;
411                 int draft = 0;
412                 /*  receive response */
413                 if (!(response = net_getline(imaplocal->netstream))) {
414                         EM_DEBUG_EXCEPTION("net_getline failed...");
415                         err = EMAIL_ERROR_INVALID_RESPONSE;
416                         goto FINISH_OFF;
417                 }
418
419 #ifdef FEATURE_CORE_DEBUG
420                 EM_DEBUG_LOG(" [IMAP4] <<< [%s]", response);
421 #endif
422
423                 if (!strncmp(response, tag, EM_SAFE_STRLEN(tag))) {
424                         if (!strncmp(response + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
425                                 free(response); response = NULL;
426                                 break;
427                         }
428                         else {          /*  'NO' or 'BAD' */
429                                 err = EMAIL_ERROR_IMAP4_FETCH_UID_FAILURE;              /* EMAIL_ERROR_INVALID_RESPONSE; */
430                                 goto FINISH_OFF;
431                         }
432                 }
433
434                 if ((p = strstr(response, " FETCH ("))) {
435                         if (!strstr(p, "\\Deleted")) {  /*  undeleted only */
436                                 *p = '\0'; p  += strlen(" FETCH ");
437
438                                 seen = strstr(p, "\\Seen") ? 1  :  0;
439                                 draft = strstr(p, "\\Draft") ? 1  :  0;
440                                 forwarded = strstr(p, "$Forwarded") ? 1  :  0;
441
442                                 if ((p = strstr(p, "UID "))) {
443                                         s = p + strlen("UID ");
444
445                                         while (isdigit(*s))
446                                                 s++;
447
448                                         *s = '\0';
449
450                                         if (!(uid_elem = em_malloc(sizeof(emcore_uid_list)))) {
451                                                 EM_DEBUG_EXCEPTION("em_malloc failed...");
452                                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
453                                                 goto FINISH_OFF;
454                                         }
455
456                                         uid_elem->msgno = atoi(response + strlen("* "));
457                                         uid_elem->uid = EM_SAFE_STRDUP(p + strlen("UID "));
458                                         uid_elem->flag.seen = seen;
459                                         uid_elem->flag.draft = draft;
460                                         uid_elem->flag.forwarded = forwarded;
461                                         if (*uid_list  != NULL)
462                                                 uid_elem->next = *uid_list;             /*  prepend new data to list */
463
464                                         *uid_list = uid_elem;
465                                 }
466                                 else {
467                                         err = EMAIL_ERROR_INVALID_RESPONSE;
468                                         goto FINISH_OFF;
469                                 }
470                         }
471                 }
472                 else {
473                         err = EMAIL_ERROR_INVALID_RESPONSE;
474                         goto FINISH_OFF;
475                 }
476
477                 EM_SAFE_FREE(response);;
478         }
479
480         ret = true;
481
482 FINISH_OFF:
483         EM_SAFE_FREE(response);
484
485         if (err_code  != NULL)
486                 *err_code = err;
487
488         EM_PROFILE_END(ImapMailboxGetUids);
489         EM_DEBUG_FUNC_END("ret [%d]", ret);
490         return ret;
491 }
492
493 static char *__em_get_month_in_string(int month)
494 {
495         EM_DEBUG_FUNC_BEGIN("month [%d]", month);
496
497         char *mon = NULL;
498
499         switch (month){
500             case 0:
501                         mon = strdup("jan");
502                 break;
503             case 1:
504                         mon = strdup("feb");
505                 break;
506             case 2:
507                         mon = strdup("mar");
508                 break;
509             case 3:
510                         mon = strdup("apr");
511                 break;
512             case 4:
513                         mon = strdup("may");
514                 break;
515             case 5:
516                         mon = strdup("jun");
517                 break;
518             case 6:
519                         mon = strdup("jul");
520                 break;
521             case 7:
522                         mon = strdup("aug");
523                 break;
524             case 8:
525                         mon = strdup("sep");
526                 break;
527             case 9:
528                         mon = strdup("oct");
529                 break;
530             case 10:
531                         mon = strdup("nov");
532                 break;
533             case 11:
534                         mon = strdup("dec");
535                 break;
536         }
537         return mon;
538 }
539
540 int imap4_mailbox_get_uids_by_timestamp(MAILSTREAM *stream, emcore_uid_list** uid_list,  int *err_code)
541 {
542         EM_PROFILE_BEGIN(emCoreMailboxuidsbystamp);
543         EM_DEBUG_FUNC_BEGIN("stream[%p], uid_list[%p],  err_code[%p]", stream, uid_list, err_code);
544
545         int ret = false;
546         int err = EMAIL_ERROR_NONE;
547
548         IMAPLOCAL *imaplocal = NULL;
549         char tag[16], command[64];
550         char *response = NULL;
551         emcore_uid_list *uid_elem = NULL;
552         char delims[] = " ";
553         char *result = NULL;
554
555         struct tm   *timeinfo = NULL;
556         time_t         RawTime = 0;
557         time_t         week_before_RawTime = 0;
558         char  date_string[16];
559         char *mon = NULL;
560
561         if (!stream || !uid_list) {
562                 EM_DEBUG_EXCEPTION(" stream[%p], uid_list[%p]", stream, uid_list);
563                 err = EMAIL_ERROR_INVALID_PARAM;
564                 goto FINISH_OFF;
565         }
566
567         if (!(imaplocal = stream->local) || !imaplocal->netstream) {
568                 EM_DEBUG_EXCEPTION(" invalid IMAP4 stream detected...");
569                 err = EMAIL_ERROR_INVALID_PARAM;                /* EMAIL_ERROR_UNKNOWN */
570                 goto FINISH_OFF;
571         }
572
573         /* Fetch the System time and Retrieve the a Week before time */
574         /*      tzset(); */
575         time(&RawTime);
576
577         EM_DEBUG_LOG("RawTime Info [%lu]", RawTime);
578
579         timeinfo = localtime (&RawTime);
580
581         EM_DEBUG_LOG(">>>>>Current TIme %d %d %d %d %d %d", 1900+timeinfo->tm_year, timeinfo->tm_mon+1, timeinfo->tm_mday);
582
583         week_before_RawTime = RawTime - 604800;
584
585         /* Reading the current timeinfo */
586         timeinfo = localtime (&week_before_RawTime);
587
588         EM_DEBUG_LOG(">>>>>Mobile Date a Week before %d %d %d %d %d %d", 1900 + timeinfo->tm_year, timeinfo->tm_mon+1, timeinfo->tm_mday);
589
590         memset(&date_string, 0x00, 16);
591
592         mon = __em_get_month_in_string(timeinfo->tm_mon);
593
594         if (mon){
595                 snprintf(date_string, 16, "%d-%s-%04d", timeinfo->tm_mday, mon, 1900 + timeinfo->tm_year);
596                 EM_DEBUG_LOG("DATE IS [ %s ] ", date_string);
597                 EM_SAFE_FREE(mon);
598         }
599
600         memset(tag, 0x00, sizeof(tag));
601         memset(command, 0x00, sizeof(command));
602
603         SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
604         SNPRINTF(command, sizeof(command), "%s UID SEARCH 1:* SINCE %s\015\012", tag, date_string);
605         EM_DEBUG_LOG("COMMAND [%s] ", command);
606
607 #ifdef FEATURE_CORE_DEBUG
608         EM_DEBUG_LOG(" [IMAP4] >>> [%s]", command);
609 #endif
610
611         /*  send command  :  get msgno/uid for all message */
612         if (!net_sout(imaplocal->netstream, command, (int)EM_SAFE_STRLEN(command))) {
613                 EM_DEBUG_EXCEPTION(" net_sout failed...");
614                 err = EMAIL_ERROR_CONNECTION_BROKEN;            /* EMAIL_ERROR_UNKNOWN */
615                 goto FINISH_OFF;
616         }
617
618         *uid_list = NULL;
619
620         while (imaplocal->netstream) {
621                 char *p = NULL;
622                 /*  receive response */
623                 if (!(response = net_getline(imaplocal->netstream))) {
624                         EM_DEBUG_EXCEPTION(" net_getline failed...");
625                         err = EMAIL_ERROR_INVALID_RESPONSE;             /* EMAIL_ERROR_UNKNOWN; */
626                         goto FINISH_OFF;
627                 }
628 #ifdef FEATURE_CORE_DEBUG
629                 EM_DEBUG_LOG(" [IMAP4] <<< [%s]", response);
630 #endif
631
632                 if (!strncmp(response, tag, EM_SAFE_STRLEN(tag))) {
633                         if (!strncmp(response + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
634                                 free(response); response = NULL;
635                                 break;
636                         }
637                         else {  /*  'NO' or 'BAD' */
638                                 err = EMAIL_ERROR_IMAP4_FETCH_UID_FAILURE;              /* EMAIL_ERROR_INVALID_RESPONSE; */
639                                 goto FINISH_OFF;
640                         }
641                 }
642
643                 if ((p = strstr(response, " SEARCH "))){
644                     *p = '\0'; p  += strlen(" SEARCH ");
645
646                     result = strtok(p, delims);
647
648                     while (result  != NULL)
649                     {
650                                 EM_DEBUG_LOG("UID VALUE DEEP is [%s]", result);
651
652                                 if (!(uid_elem = em_malloc(sizeof(emcore_uid_list)))) {
653                                         EM_DEBUG_EXCEPTION(" malloc failed...");
654                                         err = EMAIL_ERROR_OUT_OF_MEMORY;
655                                         goto FINISH_OFF;
656                                 }
657
658                                 uid_elem->uid = EM_SAFE_STRDUP(result);
659
660                                 if (*uid_list  != NULL)
661                                         uid_elem->next = *uid_list;
662                                 *uid_list = uid_elem;
663                                 result = strtok(NULL, delims);
664                     }
665
666                         EM_SAFE_FREE(response);
667                     return 1;
668                 }
669                 else {
670                         err = EMAIL_ERROR_INVALID_RESPONSE;
671                         goto FINISH_OFF;
672                 }
673                 /* Delete the dead code */
674         }
675
676         ret = true;
677
678 FINISH_OFF:
679         if (response  != NULL)
680                 free(response);
681
682         if (err_code  != NULL)
683                 *err_code = err;
684         EM_PROFILE_END(emCoreMailboxuidsbystamp);
685         EM_DEBUG_FUNC_END("ret [%d]", ret);
686         return ret;
687 }
688
689 #define PARSE_BUFFER_LENGTH 4096
690 static int emcore_parse_header(char *rfc822_header, int *req_read_receipt, int *priority, int *err_code)
691 {
692         EM_DEBUG_FUNC_BEGIN("rfc822_header[%p], req_read_receipt[%p], priority[%p], err_code[%p]", rfc822_header, req_read_receipt, priority,  err_code);
693
694         if (!rfc822_header || !priority)
695                 return false;
696
697         if (err_code)
698                 *err_code = EMAIL_ERROR_NONE;
699
700         EM_PROFILE_BEGIN(emCoreMailboxParseHeader);
701
702         char buf[PARSE_BUFFER_LENGTH];
703         int len, i, j;
704
705         EM_DEBUG_LOG("Buffer length [%d]", PARSE_BUFFER_LENGTH);
706
707         *priority = 3;
708
709         memset(buf, 0x00, PARSE_BUFFER_LENGTH);
710
711         for (len = EM_SAFE_STRLEN(rfc822_header), i = 0, j = 0; i < len; i++) {
712                 if (rfc822_header[i] == CR && rfc822_header[i+1] == LF){
713                         if (j + 3 < PARSE_BUFFER_LENGTH) /* '3' include CR LF NULL */
714                                 strncpy(buf + j, CRLF_STRING, PARSE_BUFFER_LENGTH - (j + 2)); /* '3' include CR LF */
715                         else
716                                 EM_DEBUG_EXCEPTION("buf is too small.");
717
718                         i++;
719                         j = 0;
720
721                         /*  parsing data */
722                         em_upper_string(buf);
723
724                         /*  disposition_notification_to */
725                         if (buf[0] == 'D' && buf[11] == '-' && buf[12] == 'N' && buf[24] == '-' && buf[25] == 'T') {
726                                 if (req_read_receipt)
727                                         *req_read_receipt = 1;
728                                 memset(buf, 0x00, PARSE_BUFFER_LENGTH);
729                                 continue;
730                         }
731
732                         /*  x-priority */
733                         if (buf[0] == 'X' && buf[2] == 'P' && buf[9] == 'Y'){
734                                 size_t len_2 = EM_SAFE_STRLEN(buf);
735                                 if (len_2 >= 12){
736                                         buf[len_2 - 2] = '\0';
737                                         *priority = atoi(buf + 11);
738                                         memset(buf, 0x00, PARSE_BUFFER_LENGTH);
739                                         continue;
740                                 }
741                         }
742
743                         /*  x-msmail-priority */
744                         if (buf[0] == 'X' && buf[2] == 'M' && buf[9] == 'P' && buf[16] == 'Y'){
745                                 if (strstr(buf, "HIGH"))
746                                         *priority = 1;
747                                 if (strstr(buf, "NORMAL"))
748                                         *priority = 3;
749                                 if (strstr(buf, "LOW"))
750                                         *priority = 5;
751                                 memset(buf, 0x00, PARSE_BUFFER_LENGTH);
752                                 continue;
753                         }
754
755                         memset(buf, 0x00, PARSE_BUFFER_LENGTH);
756                         continue;
757                 }
758
759                 if (j + 1 < PARSE_BUFFER_LENGTH)
760                         buf[j++] = rfc822_header[i];
761                 else
762                         EM_DEBUG_EXCEPTION("buf is too small.");
763         }
764
765         EM_PROFILE_END(emCoreMailboxParseHeader);
766
767         if (err_code)
768                 *err_code = EMAIL_ERROR_NONE;
769
770         return true;
771 }
772
773
774 static int emcore_get_mail_extra_info(MAILSTREAM *stream, int msgno, int *req_read_receipt, int *priority, int *err_code)
775 {
776         EM_PROFILE_BEGIN(emCoreMailGetExtraInfo);
777         EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], req_read_receipt[%p], priority[%p], err_code[%p]", stream, msgno, req_read_receipt, priority, err_code);
778
779         int ret = false;
780         int err = EMAIL_ERROR_NONE;
781         char *rfc822_header = NULL;
782         unsigned long len = 0;
783
784         EM_PROFILE_BEGIN(MaiFetchHeader);
785 #ifdef __FEATURE_HEADER_OPTIMIZATION__
786         /* Check if header already available in cache */
787         if (stream && stream->cache && stream->cache[msgno-1]->private.msg.header.text.data){
788                 EM_DEBUG_LOG("I found the header in stream->cache!!");
789                 rfc822_header = (char *) stream->cache[msgno-1]->private.msg.header.text.data;
790         }
791         else{
792                 EM_DEBUG_LOG("I couldn't find the header. I'll fetch the header from server again.");
793                 if (stream)
794                         rfc822_header = mail_fetch_header(stream, msgno, NULL, NULL, &len, FT_PEEK);
795         }
796
797 #else
798         rfc822_header = mail_fetch_header(stream, msgno, NULL, NULL, &len, FT_PEEK);
799 #endif
800         EM_PROFILE_END(MaiFetchHeader);
801
802         if (!rfc822_header || !*rfc822_header) {
803                 EM_DEBUG_EXCEPTION("mail_fetch_header failed...");
804                 err = EMAIL_ERROR_IMAP4_FETCH_UID_FAILURE;              /* EMAIL_ERROR_UNKNOWN; */
805                 goto FINISH_OFF;
806         }
807
808         if (!emcore_parse_header(rfc822_header, req_read_receipt, priority, &err)) {
809                 EM_DEBUG_EXCEPTION("emcore_parse_header falied - %d", err);
810                 goto FINISH_OFF;
811         }
812         ret = true;
813
814 FINISH_OFF:
815
816         if (err_code  != NULL)
817                 *err_code = err;
818
819         EM_PROFILE_END(emCoreMailGetExtraInfo);
820         EM_DEBUG_FUNC_END("ret [%d]", ret);
821         return ret;
822 }
823
824 static int emcore_get_uids_to_download(MAILSTREAM *stream, email_account_t *account, emstorage_mailbox_tbl_t *input_mailbox_tbl, int limit_count, emcore_uid_list** uid_list, int *uids, int retrieve_mode ,  int *err_code)
825 {
826         EM_PROFILE_BEGIN(emCoreGetUidsDownload);
827         EM_DEBUG_FUNC_BEGIN("account[%p], input_mailbox_tbl[%p], limit_count[%d], uid_list[%p], err_code[%p]", account, input_mailbox_tbl, limit_count, uid_list, err_code);
828
829         int ret = false;
830         int err = EMAIL_ERROR_NONE;
831
832         emstorage_read_mail_uid_tbl_t *downloaded_uids = NULL;
833         int i = 0, j = 0, uid_count = 0, uid_to_be_downloaded_count = 0;
834         emcore_uid_list *uid_elem = NULL;
835         emcore_uid_list *head_uid_elem = NULL, *end =  NULL;
836         emcore_uid_list *next_uid_elem = NULL;
837         emstorage_mail_tbl_t *mail = NULL;
838
839         if (!account || !input_mailbox_tbl || !uid_list) {
840                 EM_DEBUG_EXCEPTION("account[%p], input_mailbox_tbl[%p], uid_list[%p]", account, input_mailbox_tbl, uid_list);
841                 err = EMAIL_ERROR_INVALID_PARAM;
842                 goto FINISH_OFF;
843         }
844
845         *uid_list = NULL;
846
847         if (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {
848                 if (!pop3_mailbox_get_uids(stream, uid_list, &err)) {
849                         EM_DEBUG_EXCEPTION("pop3_mailbox_get_uids failed - %d", err);
850                         goto FINISH_OFF;
851                 }
852         }
853         else {  /*  EMAIL_SERVER_TYPE_IMAP4 */
854                 /*  sowmya.kr commented , since imap4_mailbox_get_uids_by_timestamp will fetch mails since last week and not all mails  */
855
856                 EM_DEBUG_LOG("calling imap4_mailbox_get_uids");
857                 if (!imap4_mailbox_get_uids(stream, uid_list, &err)) {
858                         EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed [%d]", err);
859                         if (err  != EMAIL_ERROR_MAIL_NOT_FOUND_ON_SERVER)
860                                 goto FINISH_OFF;
861             }
862         }
863
864         if (!emstorage_get_downloaded_list(input_mailbox_tbl->account_id,
865         (account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) ? 0 : input_mailbox_tbl->mailbox_id,
866         &downloaded_uids, &j, true, &err)) {
867                 EM_DEBUG_EXCEPTION("emstorage_get_downloaded_list failed [%d]", err);
868
869                 goto FINISH_OFF;
870         }
871         EM_DEBUG_LOG("Number of Mails in Downloaded Table [%d]", j);
872
873         uid_elem = *uid_list;
874         uid_count = 0;
875
876         if(!uid_elem) { /* If there is no mail in the input_mailbox_tbl, remove all mails in the input_mailbox_tbl */
877                 for (i = 0; i < j; i++) {
878                         downloaded_uids[i].reserved = 0;
879                 }
880         }
881
882         EM_PROFILE_BEGIN(emCoreGetUidsDownloadWhilwLoop);
883
884         while (uid_elem) {
885                 next_uid_elem = uid_elem->next;
886
887                 if ((account->retrieval_mode == EMAIL_IMAP4_RETRIEVAL_MODE_NEW) && (uid_elem->flag.seen != 0)){         /*  already seen */
888                         if (uid_elem->uid)
889                                 free(uid_elem->uid);
890
891                         free(uid_elem);
892                 }
893                 else {
894                         int to_be_downloaded = 1;
895
896                         if (limit_count > 0 && uid_count >= limit_count){
897                                 /*  EM_DEBUG_LOG("hit the limit[%d] for [%s]", limit_count, uid_elem->uid);              */
898                                 to_be_downloaded = 0;
899                         }
900                         else{
901                                 for (i = j; i > 0; i--) {
902                                         if (downloaded_uids[i - 1].reserved == 0 && !strcmp(uid_elem->uid, downloaded_uids[i - 1].s_uid)) {
903                                                 downloaded_uids[i - 1].reserved = uid_elem->flag.seen ? 2 : 1;
904                                                 to_be_downloaded = 0;
905                                                 break;
906                                         }
907                                 }
908                         }
909
910                         /*  EM_DEBUG_LOG("Is uid[%s] going to be downloded ? [%d]", uid_elem->uid, to_be_downloaded); */
911
912                         if (to_be_downloaded) {
913                                 if (retrieve_mode == EMAIL_SYNC_OLDEST_MAILS_FIRST){
914                                         uid_elem->next = head_uid_elem;
915                                         head_uid_elem = uid_elem;
916                                 }
917                                 else{   /* if retrieve_mode is EMAIL_SYNC_LATEST_MAILS_FIRST, add uid elem to end so that latest mails are in front of list */
918                                         if (head_uid_elem == NULL){
919                                                 uid_elem->next = head_uid_elem;
920                                                 head_uid_elem = uid_elem;
921                                                 end = head_uid_elem;
922                                         }
923                                         else{
924                                                 end->next = uid_elem;
925                                                 uid_elem->next = NULL;
926                                                 end = uid_elem;
927                                         }
928                                 }
929                                 uid_to_be_downloaded_count++;
930
931                         }
932                         else {
933                                 if (uid_elem->uid)
934                                         free(uid_elem->uid);
935                                 free(uid_elem);
936                         }
937
938                         uid_count++;
939                 }
940
941                 uid_elem = next_uid_elem;
942         }
943
944         EM_PROFILE_END(emCoreGetUidsDownloadWhilwLoop);
945         EM_PROFILE_BEGIN(emCoreGetUidsDownloadForLoop);
946
947         for (i = 0; i < j; i++) {
948                 /*  EM_DEBUG_LOG("input_mailbox_tbl[%s] reserved[%d]", input_mailbox_tbl->name, downloaded_uids[i].reserved); */
949                 if (downloaded_uids[i].reserved == 0) {         /*  deleted on server */
950                         if (!emstorage_get_maildata_by_servermailid(input_mailbox_tbl->account_id, downloaded_uids[i].s_uid, &mail, true, &err)){
951                                 EM_DEBUG_EXCEPTION("emstorage_get_maildata_by_servermailid for uid[%s] Failed [%d] \n ", downloaded_uids[i].s_uid, err);
952                                 if (err == EMAIL_ERROR_MAIL_NOT_FOUND){
953                                         continue;
954                                 }
955                         }
956
957                         if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
958                                 if (!emcore_delete_mails_from_local_storage(input_mailbox_tbl->account_id, &(mail->mail_id), 1, EMAIL_DELETED_FROM_SERVER, false, &err)) {
959                                         EM_DEBUG_EXCEPTION("emcore_delete_mails_from_local_storage falied - %d", err);
960                                         goto FINISH_OFF;
961                                 }
962                                 /* emcore_delete_notification_for_read_mail(mail->mail_id); */
963                                 emcore_display_unread_in_badge();
964                         }
965
966                         if (!emstorage_remove_downloaded_mail(input_mailbox_tbl->account_id, input_mailbox_tbl->mailbox_name, downloaded_uids[i].s_uid, true, &err)) {   /*  remove uid from uid list */
967                                 EM_DEBUG_EXCEPTION("emstorage_remove_downloaded_mail failed - %d", err);
968
969                                 /* goto FINISH_OFF; */
970                         }
971
972                 }
973                 else if (account->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && downloaded_uids[i].reserved == 1) {
974                         /*  unseen on server */
975                         if  (!emstorage_get_mail_by_id(downloaded_uids[i].local_uid, &mail, true, &err)){
976                                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed for [%d] - [%d]", downloaded_uids[i].local_uid, err);
977                                 continue;
978                         }
979
980                         if (mail) {
981                                 if (mail->body_download_status && mail->flags_seen_field){
982                                         EM_DEBUG_LOG("Set flag as seen on server");
983                                         mail_setflag_full(stream, downloaded_uids[i].s_uid, "\\Seen", ST_UID);
984                                 }
985                         }
986                 }
987                 if (mail != NULL)
988                         emstorage_free_mail(&mail, 1, NULL);
989                 mail = NULL;
990         }
991         EM_PROFILE_END(emCoreGetUidsDownloadForLoop);
992
993         *uid_list = head_uid_elem;
994         *uids = uid_to_be_downloaded_count;
995
996         ret = true;
997
998 FINISH_OFF:
999         if (ret == false){
1000                 if (head_uid_elem)
1001                         emcore_free_uids(head_uid_elem, NULL);
1002         }
1003
1004         if (downloaded_uids != NULL)
1005                 emstorage_free_read_mail_uid(&downloaded_uids, j, NULL);
1006
1007         if (mail != NULL)
1008                 emstorage_free_mail(&mail, 1, NULL);
1009
1010         if (err_code  != NULL)
1011                 *err_code = err;
1012
1013         EM_PROFILE_END(emCoreGetUidsDownload);
1014         EM_DEBUG_FUNC_END("ret [%d]", ret);
1015         return ret;
1016 }
1017
1018 /* insert received mail UID to read mail uid table */
1019 static int emcore_add_read_mail_uid(emstorage_mailbox_tbl_t *input_maibox_data, char *server_mailbox_name, int mail_id, char *uid, int rfc822_size, int rule_id, int *err_code)
1020 {
1021         EM_DEBUG_FUNC_BEGIN("input_maibox_data[%p], server_mailbox_name[%s], uid[%s], rfc822_size[%d], rule_id[%d], err_code[%p]", input_maibox_data, server_mailbox_name, uid, rfc822_size, rule_id, err_code);
1022
1023         int ret = false;
1024         int err = EMAIL_ERROR_NONE;
1025
1026         emstorage_read_mail_uid_tbl_t  read_mail_uid = { 0 };
1027         emstorage_mailbox_tbl_t       *mailbox_tbl = NULL;
1028
1029         if (!input_maibox_data || !uid) {
1030                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1031                 err = EMAIL_ERROR_INVALID_PARAM;
1032                 goto FINISH_OFF;
1033         }
1034
1035         read_mail_uid.account_id = input_maibox_data->account_id;
1036
1037         if (!(input_maibox_data->mailbox_id) || !(server_mailbox_name)){
1038                 if (!emstorage_get_mailbox_by_mailbox_type(input_maibox_data->account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_tbl, false, &err)) {
1039                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1040                         goto FINISH_OFF;
1041                 }
1042         }
1043
1044         if (input_maibox_data->mailbox_id)
1045                 read_mail_uid.mailbox_id = input_maibox_data->mailbox_id;
1046         else
1047                 read_mail_uid.mailbox_id = mailbox_tbl->mailbox_id;
1048
1049         read_mail_uid.local_uid = mail_id;
1050         EM_DEBUG_LOG("MAIL ID [%d] LOCAL_UID [%d]", mail_id, read_mail_uid.local_uid);
1051
1052         if (server_mailbox_name)
1053                 read_mail_uid.mailbox_name = server_mailbox_name;
1054         else
1055                 read_mail_uid.mailbox_name = mailbox_tbl->mailbox_name;
1056
1057         read_mail_uid.s_uid = uid;
1058         read_mail_uid.data1 = rfc822_size;
1059         read_mail_uid.flag = rule_id;
1060
1061         if (!emstorage_add_downloaded_mail(&read_mail_uid, false, &err)) {
1062                 EM_DEBUG_EXCEPTION("emstorage_add_downloaded_mail failed [%d]", err);
1063
1064                 goto FINISH_OFF;
1065         }
1066
1067         ret = true;
1068
1069 FINISH_OFF:
1070         if (mailbox_tbl)
1071                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1072
1073         if (err_code)
1074                 *err_code = err;
1075
1076         EM_DEBUG_FUNC_END("ret [%d]", ret);
1077         return ret;
1078 }
1079
1080 int emcore_add_mail_to_mailbox(emstorage_mailbox_tbl_t *input_maibox_data, emstorage_mail_tbl_t *input_new_mail_tbl_data, int *output_mail_id, int *output_thread_id)
1081 {
1082         EM_DEBUG_FUNC_BEGIN("input_maibox_data[%p], input_new_mail_tbl_data[%p], uid_elem[%p], output_mail_id[%p], output_thread_id[%p]", mail_stream, input_maibox_data, input_new_mail_tbl_data, output_mail_id, output_thread_id);
1083
1084         int                   err = EMAIL_ERROR_NONE;
1085         int                   thread_id = -1;
1086         int                   thread_item_count = 0;
1087         int                   latest_mail_id_in_thread = -1;
1088
1089         if (!input_maibox_data || !input_new_mail_tbl_data) {
1090                 EM_DEBUG_EXCEPTION("Invalid Parameter");
1091                 err = EMAIL_ERROR_INVALID_PARAM;
1092                 goto FINISH_OFF;
1093         }
1094
1095         input_new_mail_tbl_data->account_id            = input_maibox_data->account_id;
1096         input_new_mail_tbl_data->mailbox_id            = input_maibox_data->mailbox_id;
1097         input_new_mail_tbl_data->mailbox_name          = EM_SAFE_STRDUP(input_maibox_data->mailbox_name);
1098         input_new_mail_tbl_data->mailbox_type          = input_maibox_data->mailbox_type;
1099
1100         emstorage_begin_transaction(NULL, NULL, NULL);
1101
1102         /* Get the Mail_id */
1103         if (!emstorage_increase_mail_id(&(input_new_mail_tbl_data->mail_id), false, &err)) {
1104                 EM_DEBUG_EXCEPTION("emstorage_increase_mail_id failed [%d]", err);
1105                 goto FINISH_OFF;
1106         }
1107
1108         if (emstorage_get_thread_id_of_thread_mails(input_new_mail_tbl_data, &thread_id, &latest_mail_id_in_thread, &thread_item_count)  != EMAIL_ERROR_NONE)
1109                 EM_DEBUG_LOG(" emstorage_get_thread_id_of_thread_mails is failed.");
1110
1111         if (thread_id == -1){
1112                 input_new_mail_tbl_data->thread_id = input_new_mail_tbl_data->mail_id;
1113                 input_new_mail_tbl_data->thread_item_count = thread_item_count = 1;
1114         }
1115         else {
1116                 input_new_mail_tbl_data->thread_id = thread_id;
1117                 thread_item_count++;
1118         }
1119
1120         if (!emstorage_add_mail(input_new_mail_tbl_data, 0, false, &err)) {
1121                 EM_DEBUG_EXCEPTION("emstorage_add_mail failed [%d]", err);
1122                 emstorage_rollback_transaction(NULL, NULL, NULL);
1123                 goto FINISH_OFF;
1124         }
1125
1126         if (thread_item_count > 1){
1127                 if (!emstorage_update_latest_thread_mail(input_new_mail_tbl_data->account_id, input_new_mail_tbl_data->thread_id, 0, 0, false, &err)) {
1128                         EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
1129                         emstorage_rollback_transaction(NULL, NULL, NULL);
1130                         goto FINISH_OFF;
1131                 }
1132         }
1133         if (output_thread_id)
1134                 *output_thread_id = input_new_mail_tbl_data->thread_id;
1135
1136         if (output_mail_id != NULL)
1137                 *output_mail_id = input_new_mail_tbl_data->mail_id;
1138
1139         EM_DEBUG_LOG("mail_table_data.mail_id [%d]", input_new_mail_tbl_data->mail_id);
1140         EM_DEBUG_LOG("mail_table_data.thread_id [%d]", input_new_mail_tbl_data->thread_id);
1141
1142         if (!emcore_add_read_mail_uid(input_maibox_data, input_maibox_data->mailbox_name, input_new_mail_tbl_data->mail_id, input_new_mail_tbl_data->server_mail_id, input_new_mail_tbl_data->mail_size, 0, &err)) {
1143                 EM_DEBUG_EXCEPTION("emcore_add_read_mail_uid failed [%d]", err);
1144                 emstorage_rollback_transaction(NULL, NULL, NULL);
1145                 goto FINISH_OFF;
1146         }
1147
1148         emstorage_commit_transaction(NULL, NULL, NULL);
1149
1150 FINISH_OFF:
1151
1152         EM_DEBUG_FUNC_END("err [%d]", err);
1153         return err;
1154 }
1155
1156 int emcore_check_rule(const char *input_full_address_from, const char *input_subject, emstorage_rule_tbl_t *rule, int rule_len, int *matched, int *err_code)
1157 {
1158         EM_DEBUG_FUNC_BEGIN("input_full_address_from [%p], input_subject [%p], rule [%p], rule_len [%d], matched [%p], err_code [%p]", input_full_address_from, input_subject, rule, rule_len, matched, err_code);
1159
1160         int ret = false, err = EMAIL_ERROR_NONE, i;
1161         size_t len = 0;
1162         char *src = NULL;       /*  string which will be compared with rules */
1163         char *from_address = NULL;
1164         ADDRESS *addr = NULL;
1165
1166         if (!matched || !input_full_address_from || !input_subject) {
1167                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1168                 err = EMAIL_ERROR_INVALID_PARAM;
1169                 goto FINISH_OFF;
1170         }
1171
1172         *matched = -1;
1173
1174         rfc822_parse_adrlist(&addr, (char*)input_full_address_from, NULL);
1175
1176         if(addr) {
1177                 EM_DEBUG_LOG("rule : full_address_from[%s], addr->mailbox[%s], addr->host[%s]", input_full_address_from, addr->mailbox, addr->host);
1178
1179                 if (addr->mailbox)
1180                         len = EM_SAFE_STRLEN(addr->mailbox);
1181                 if (addr->host)
1182                         len  += EM_SAFE_STRLEN(addr->host);
1183                         len  += 2;
1184
1185                 if (!(from_address = em_malloc(len))) {
1186                         EM_DEBUG_EXCEPTION("em_malloc failed...");
1187                         err = EMAIL_ERROR_OUT_OF_MEMORY;
1188                         goto FINISH_OFF;
1189                 }
1190
1191                 SNPRINTF(from_address, len, "%s@%s", addr->mailbox, addr->host);
1192         }
1193         else  {
1194                 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed.");
1195                 err = EMAIL_ERROR_INVALID_ADDRESS;
1196                 goto FINISH_OFF;
1197         }
1198
1199         for (i = 0; i < rule_len; i++) {
1200                 if (!(rule + i)) {
1201                         EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1202                         err = EMAIL_ERROR_INVALID_PARAM;
1203                         goto FINISH_OFF;
1204                 }
1205
1206                 EM_DEBUG_LOG("rule[%d].flag1(rule id[%d]) is %d", i, rule[i].rule_id, rule[i].flag1);
1207
1208                 if (rule[i].flag1){
1209                         /*  'ON' */
1210                         EM_DEBUG_LOG("rule[%d].flag2(rule id[%d]) is %d", i, rule[i].rule_id, rule[i].flag2);
1211                         switch (rule[i].type) {
1212                                 case EMAIL_FILTER_FROM:
1213                                         src = from_address;
1214                                         break;
1215                                 case EMAIL_FILTER_SUBJECT:
1216                                         src = (char*)input_subject;
1217                                         break;
1218                                 case EMAIL_FILTER_BODY:
1219                                         err = EMAIL_ERROR_NOT_SUPPORTED;
1220                                         goto FINISH_OFF;
1221                                         break;
1222                         }
1223                         EM_DEBUG_LOG("rule src[%s], value[%s]\n", src, rule[i].value);
1224
1225                         if (src && rule[i].value) {
1226                             if (RULE_TYPE_INCLUDES == rule[i].flag2) {
1227                                         if (strstr(src, rule[i].value)) {
1228                                                 *matched = i;
1229                                                 break;
1230                                         }
1231                                 }
1232                                 else if (RULE_TYPE_EXACTLY == rule[i].flag2) {
1233                                         if (!strcmp(src, rule[i].value)) {
1234                                                 *matched = i;
1235                                                 break;
1236                                         }
1237                                 }
1238                         }
1239                 }
1240                 else
1241                         EM_DEBUG_LOG("Invald src or rule[i].value");
1242         }
1243         ret = true;
1244
1245         EM_DEBUG_LOG("i [%d], matched [%d]", i, *matched);
1246 FINISH_OFF:
1247
1248         EM_SAFE_FREE(from_address);
1249
1250         if (addr  != NULL)
1251                 mail_free_address(&addr);
1252
1253         if (err_code  != NULL)
1254                 *err_code = err;
1255
1256         EM_DEBUG_FUNC_END("ret [%d]", ret);
1257         return ret;
1258 }
1259
1260 static int emcore_get_utf8_address(char **dest, ADDRESS *address, int *err_code)
1261 {
1262         EM_DEBUG_FUNC_BEGIN("dest[%p], address[%p], err_code[%p]", dest, address, err_code);
1263
1264         if (!dest || !address)  {
1265                 EM_DEBUG_EXCEPTION("dest[%p], address[%p]", dest, address);
1266                 if (err_code != NULL)
1267                         *err_code = EMAIL_ERROR_INVALID_PARAM;
1268                 return false;
1269         }
1270
1271         int ret = false;
1272         int err = EMAIL_ERROR_NONE;
1273
1274         gchar *concatenated = NULL;
1275         gchar *utf8_address = NULL;
1276         gchar *temp = NULL;
1277         char *nickname = NULL;
1278
1279         while (address)  {
1280                 EM_DEBUG_LOG("address->mailbox[%s], address->host[%s]", address->mailbox, address->host);
1281                 if (!address->mailbox || !address->host) {
1282                         address = address->next;
1283                         continue;
1284                 }
1285                 EM_DEBUG_LOG("address->mailbox[%p]", address->personal);
1286                 if (address->personal)  {
1287                         if (!(nickname = emcore_decode_rfc2047_text(address->personal, &err)))  {
1288                                 EM_DEBUG_EXCEPTION("emcore_decode_rfc2047_text failed - %d", err);
1289                                 goto FINISH_OFF;
1290                         }
1291                         EM_DEBUG_LOG("nickname[%s]", nickname);
1292                         if (*nickname != '\0')
1293                                 utf8_address = g_strdup_printf("\"%s\" <%s@%s>", nickname, address->mailbox ? address->mailbox : "", address->host ? address->host : "");
1294                         else
1295                                 utf8_address = g_strdup_printf("<%s@%s>", address->mailbox ? address->mailbox : "", address->host ? address->host : "");
1296
1297                         EM_SAFE_FREE(nickname);
1298                 }
1299                 else
1300                         utf8_address = g_strdup_printf("<%s@%s>", address->mailbox ? address->mailbox : "", address->host ? address->host : "");
1301
1302                 EM_DEBUG_LOG("utf8_address[%s]", utf8_address);
1303
1304                 if (concatenated != NULL)  {
1305                         temp = concatenated;
1306                         concatenated = g_strdup_printf("%s; %s", temp, utf8_address);
1307                         g_free(temp);
1308                 }
1309                 else
1310                         concatenated = g_strdup(utf8_address);
1311
1312                 g_free(utf8_address);
1313                 utf8_address = NULL;
1314
1315                 address = address->next;
1316         }
1317
1318         *dest = concatenated;
1319
1320         ret = true;
1321
1322 FINISH_OFF:
1323         EM_SAFE_FREE(nickname);
1324         EM_DEBUG_FUNC_END("ret[%d]", ret);
1325         return ret;
1326 }
1327
1328
1329 int emcore_make_mail_tbl_data_from_envelope(MAILSTREAM *mail_stream, ENVELOPE *input_envelope, emcore_uid_list *input_uid_elem, emstorage_mail_tbl_t **output_mail_tbl_data,  int *err_code)
1330 {
1331         EM_PROFILE_BEGIN(emCoreParseEnvelope);
1332         EM_DEBUG_FUNC_BEGIN("input_envelope[%p], input_uid_elem [%p], output_mail_tbl_data[%p],  err_code[%p]", input_envelope, input_uid_elem, output_mail_tbl_data, err_code);
1333
1334         int zone_hour = 0;
1335         int ret = false;
1336         int err = EMAIL_ERROR_NONE;
1337         int priority = 3;
1338         int req_read_receipt = 0;
1339         struct tm             temp_time_info;
1340         MESSAGECACHE         *mail_cache_element = NULL;
1341         emstorage_mail_tbl_t *temp_mail_tbl_data = NULL;
1342
1343         if (!output_mail_tbl_data) {
1344                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
1345                 err = EMAIL_ERROR_INVALID_PARAM;
1346                 goto FINISH_OFF;
1347         }
1348
1349         if (!(temp_mail_tbl_data = em_malloc(sizeof(emstorage_mail_tbl_t)))) {
1350                 EM_DEBUG_EXCEPTION("em_malloc failed...");
1351                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1352                 goto FINISH_OFF;
1353         }
1354
1355         if (!(mail_cache_element = mail_elt(mail_stream, input_uid_elem->msgno))) {
1356                 EM_DEBUG_EXCEPTION("mail_elt failed...");
1357                 err = EMAIL_ERROR_UNKNOWN;
1358                 goto FINISH_OFF;
1359         }
1360
1361         if (!emcore_get_mail_extra_info(mail_stream, input_uid_elem->msgno, &req_read_receipt, &priority, &err)) {
1362                 EM_DEBUG_EXCEPTION("emcore_get_mail_extra_info failed [%d]", err);
1363                 goto FINISH_OFF;
1364         }
1365
1366         if (input_envelope->subject) {
1367                 temp_mail_tbl_data->subject = emcore_decode_rfc2047_text(input_envelope->subject, &err);
1368                 EM_DEBUG_LOG("subject[%s]", temp_mail_tbl_data->subject);
1369         }
1370
1371         if (input_envelope->from) {
1372                 if (!emcore_get_utf8_address(&temp_mail_tbl_data->full_address_from, input_envelope->from, &err)) {
1373                         EM_DEBUG_EXCEPTION("emcore_get_utf8_address failed [%d]", err);
1374                         goto FINISH_OFF;
1375                 }
1376
1377                 EM_DEBUG_LOG("full_address_from[%s]", temp_mail_tbl_data->full_address_from);
1378         }
1379
1380         if (input_envelope->to) {
1381                 EM_DEBUG_LOG("input_envelope->to");
1382                 if (!emcore_get_utf8_address(&temp_mail_tbl_data->full_address_to, input_envelope->to, &err)) {
1383                         EM_DEBUG_EXCEPTION("emcore_get_utf8_address failed [%d]", err);
1384                         goto FINISH_OFF;
1385                 }
1386
1387                 EM_DEBUG_LOG("full_address_to[%s]", temp_mail_tbl_data->full_address_to);
1388         }
1389
1390         if (input_envelope->cc) {
1391                 EM_DEBUG_LOG("input_envelope->cc");
1392                 if (!emcore_get_utf8_address(&temp_mail_tbl_data->full_address_cc, input_envelope->cc, &err)) {
1393                         EM_DEBUG_EXCEPTION("emcore_get_utf8_address failed [%d]", err);
1394                         goto FINISH_OFF;
1395                 }
1396
1397                 EM_DEBUG_LOG("full_address_cc [%s]", temp_mail_tbl_data->full_address_cc);
1398         }
1399
1400         if (input_envelope->bcc) {
1401                 if (!emcore_get_utf8_address(&temp_mail_tbl_data->full_address_bcc, input_envelope->bcc, &err)) {
1402                         EM_DEBUG_EXCEPTION("emcore_get_utf8_address failed [%d]", err);
1403                         goto FINISH_OFF;
1404                 }
1405
1406                 EM_DEBUG_LOG("full_address_bcc [%s]", temp_mail_tbl_data->full_address_bcc);
1407         }
1408
1409         if (input_envelope->reply_to) {
1410                 if (!emcore_get_utf8_address(&temp_mail_tbl_data->full_address_reply, input_envelope->reply_to, &err)) {
1411                         EM_DEBUG_EXCEPTION("emcore_get_utf8_address failed [%d]", err);
1412                         goto FINISH_OFF;
1413                 }
1414
1415                 EM_DEBUG_LOG("full_address_reply [%s]\n", temp_mail_tbl_data->full_address_reply);
1416         }
1417
1418         if (input_envelope->return_path) {
1419                 if (!emcore_get_utf8_address(&temp_mail_tbl_data->full_address_return , input_envelope->return_path, &err)) {
1420                         EM_DEBUG_EXCEPTION("emcore_get_utf8_address failed [%d]", err);
1421                         goto FINISH_OFF;
1422                 }
1423                 EM_DEBUG_LOG("full_address_return[%s]", temp_mail_tbl_data->full_address_return);
1424         }
1425
1426         temp_mail_tbl_data->message_id            = EM_SAFE_STRDUP(input_envelope->message_id);
1427
1428         memset((void*)&temp_time_info, 0,  sizeof(struct tm));
1429
1430         EM_DEBUG_LOG("tm_hour[%d] zhours [%d] tm_min[%d] zminutes[%d] zoocident[%d]", mail_cache_element->hours, mail_cache_element->zhours,  mail_cache_element->minutes, mail_cache_element->zminutes, mail_cache_element->zoccident);
1431
1432         zone_hour = mail_cache_element->zhours * (mail_cache_element->zoccident?-1:1);
1433
1434         temp_time_info.tm_sec  = mail_cache_element->seconds;
1435         temp_time_info.tm_min  = mail_cache_element->minutes + mail_cache_element->zminutes;
1436         temp_time_info.tm_hour = mail_cache_element->hours - zone_hour;
1437
1438         if(temp_time_info.tm_hour < 0) {
1439                 temp_time_info.tm_mday = mail_cache_element->day - 1;
1440                 temp_time_info.tm_hour += 24;
1441         }
1442         else
1443                 temp_time_info.tm_mday = mail_cache_element->day;
1444
1445         temp_time_info.tm_mon  = mail_cache_element->month - 1;
1446         temp_time_info.tm_year = mail_cache_element->year + 70;
1447
1448         temp_mail_tbl_data->date_time             = timegm(&temp_time_info);
1449
1450         temp_mail_tbl_data->server_mail_status    = 1;
1451         temp_mail_tbl_data->server_mail_id        = EM_SAFE_STRDUP(input_uid_elem->uid);
1452         temp_mail_tbl_data->mail_size             = mail_cache_element->rfc822_size;
1453         temp_mail_tbl_data->flags_seen_field      = input_uid_elem->flag.seen;
1454         temp_mail_tbl_data->flags_deleted_field   = mail_cache_element->deleted;
1455         temp_mail_tbl_data->flags_flagged_field   = mail_cache_element->flagged;
1456         temp_mail_tbl_data->flags_answered_field  = mail_cache_element->answered;
1457         temp_mail_tbl_data->flags_recent_field    = mail_cache_element->recent;
1458         temp_mail_tbl_data->flags_draft_field     = mail_cache_element->draft;
1459         temp_mail_tbl_data->flags_forwarded_field = input_uid_elem->flag.forwarded;
1460         temp_mail_tbl_data->priority              = priority;
1461         temp_mail_tbl_data->report_status         = (req_read_receipt ? EMAIL_MAIL_REQUEST_MDN :  0);
1462         temp_mail_tbl_data->attachment_count      = 0;
1463
1464         emcore_fill_address_information_of_mail_tbl(temp_mail_tbl_data);
1465
1466         *output_mail_tbl_data = temp_mail_tbl_data;
1467         temp_mail_tbl_data = NULL;
1468
1469         ret = true;
1470
1471 FINISH_OFF:
1472
1473         if (ret != true)
1474                 EM_SAFE_FREE(temp_mail_tbl_data);
1475
1476         if (err_code  != NULL)
1477                 *err_code = err;
1478
1479         EM_PROFILE_END(emCoreParseEnvelope);
1480         EM_DEBUG_FUNC_END("ret [%d]", ret);
1481         return ret;
1482 }
1483
1484 INTERNAL_FUNC int emcore_sync_header(emstorage_mailbox_tbl_t *input_mailbox_tbl, emstorage_mailbox_tbl_t *input_mailbox_tbl_spam, void *stream_recycle, emcore_uid_list **input_uid_list, int *unread_mail, int *err_code)
1485 {
1486         EM_PROFILE_BEGIN(emCoreSyncHeader);
1487         EM_DEBUG_FUNC_BEGIN("input_mailbox_tbl[%p], input_mailbox_tbl_spam[%p], input_uid_list [%p], err_code[%p]", input_mailbox_tbl, input_mailbox_tbl_spam, input_uid_list, err_code);
1488
1489         int ret = false;
1490         int err = EMAIL_ERROR_NONE, err_2 = EMAIL_ERROR_NONE;
1491         int err_from_vconf = 0;
1492         int status = EMAIL_LIST_FAIL;
1493         int download_limit_count;
1494         email_account_t      *account_ref = NULL;
1495         emstorage_rule_tbl_t *rule = NULL;
1496         emcore_uid_list      *uid_list = NULL;
1497         emcore_uid_list      *uid_elem = NULL;
1498         emstorage_mail_tbl_t *new_mail_tbl_data = NULL;
1499         ENVELOPE *env = NULL;
1500         int account_id = 0, mail_id = 0, rule_len = 1000, total = 0, unread = 0, i = 0, percentage  = 0, thread_id = -1;
1501         void *stream = NULL;
1502         char *uid_range = NULL;
1503         char mailbox_id_param_string[10] = {0,};
1504
1505         if (!input_mailbox_tbl) {
1506                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM:input_mailbox_tbl[%p]", input_mailbox_tbl);
1507                 err = EMAIL_ERROR_INVALID_PARAM;
1508                 goto FINISH_OFF;
1509         }
1510
1511         account_id = input_mailbox_tbl->account_id;
1512         account_ref = emcore_get_account_reference(account_id);
1513         if (!account_ref) {
1514                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", account_id);
1515                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1516                 goto FINISH_OFF;
1517         }
1518
1519         if(account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && input_mailbox_tbl->local_yn == 1) {
1520                 EM_DEBUG_EXCEPTION("local_yn flag is false for this mailbox");
1521                 err = EMAIL_ERROR_INVALID_MAILBOX;
1522                 goto FINISH_OFF;
1523         }
1524
1525         FINISH_OFF_IF_CANCELED;
1526
1527 #ifdef __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__
1528         if ((err_from_vconf = vconf_set_int(VCONFKEY_EMAIL_SYNC_STATE, 1)) != 0 ) {
1529                 EM_DEBUG_LOG("vconf_set_int failed [%d]", err_from_vconf);
1530         }
1531 #endif /* __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__ */
1532
1533 #ifndef         __FEATURE_KEEP_CONNECTION__
1534         /*  h.gahlaut :  Recycling of stream is taken care internally in emcore_connect_to_remote_mailbox so no need of this code here */
1535         if (stream_recycle)
1536                 stream = stream_recycle; /*  set stream for recycling connection. */
1537 #endif
1538
1539         if (!emcore_connect_to_remote_mailbox(account_id, input_mailbox_tbl->mailbox_id, (void **)&stream, &err) || !stream){
1540                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
1541                 status = EMAIL_LIST_CONNECTION_FAIL;
1542                 goto FINISH_OFF;
1543         }
1544
1545         FINISH_OFF_IF_CANCELED;
1546
1547         /*  save total mail count on server to DB */
1548         if (!emstorage_update_mailbox_total_count(account_id, input_mailbox_tbl->mailbox_id, ((MAILSTREAM *)stream)->nmsgs, 1, &err)){
1549                 EM_DEBUG_EXCEPTION("emstorage_update_mailbox_total_count failed [%d]", err);
1550
1551                 goto FINISH_OFF;
1552         }
1553
1554         /* if (((MAILSTREAM *)stream)->nmsgs > 0) */
1555         {
1556                 email_option_t *opt_ref = &account_ref->options;
1557                 EM_DEBUG_LOG("block_address = %d, block_subject = %d", opt_ref->block_address, opt_ref->block_subject);
1558
1559                 if (opt_ref->block_address || opt_ref->block_subject) {
1560                         int is_completed = false;
1561                         int type = 0;
1562
1563                         if (!opt_ref->block_address)
1564                                 type = EMAIL_FILTER_SUBJECT;
1565                         else if (!opt_ref->block_subject)
1566                                 type = EMAIL_FILTER_FROM;
1567
1568                         if (!emstorage_get_rule(ALL_ACCOUNT, type, 0, &rule_len, &is_completed, &rule, true, &err) || !rule)
1569                                 EM_DEBUG_EXCEPTION("emstorage_get_rule failed - %d", err);
1570                 }
1571                 download_limit_count = input_mailbox_tbl->mail_slot_size;
1572                 if (!emcore_get_uids_to_download(stream, account_ref, input_mailbox_tbl, download_limit_count,  &uid_list, &total, EMAIL_SYNC_LATEST_MAILS_FIRST, &err)){
1573                         EM_DEBUG_EXCEPTION("emcore_get_uids_to_download failed [%d]", err);
1574                         uid_list = NULL;
1575                         goto FINISH_OFF;
1576                 }
1577
1578                 FINISH_OFF_IF_CANCELED;
1579
1580                 if (input_uid_list && *input_uid_list){
1581                         emcore_free_uids(*input_uid_list, NULL);
1582                         *input_uid_list = uid_list;
1583                 }
1584                 uid_elem = uid_list;
1585                 i = 0;
1586                 EM_PROFILE_BEGIN(emCoreSyncHeaderwhileloop);
1587
1588 #ifdef  __FEATURE_HEADER_OPTIMIZATION__
1589                 /* g.shyamakshi@samsung.com : Bulk fetch of headers only if the recieving server type is IMAP */
1590
1591                 EM_DEBUG_LOG("((MAILSTREAM *)stream)->nmsgs [%d]", ((MAILSTREAM *)stream)->nmsgs);
1592                 EM_DEBUG_LOG("uid_list [%p]", uid_list);
1593                 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && uid_list  != NULL){
1594                         emcore_uid_list *uid_list_prev = NULL;
1595                         emcore_uid_list *uid_list_fast = uid_list;
1596                         int index = 0;
1597                         int msg_count = total;
1598                         int uid_range_size = msg_count * 8 + 1000;
1599
1600                         EM_DEBUG_LOG("memory allocation for uid_range [%d, %d]", msg_count, uid_range_size);
1601                         uid_range = malloc(sizeof(char) * uid_range_size);
1602
1603                         if (uid_range == NULL){
1604                                 EM_DEBUG_EXCEPTION("memory allocation for uid_range failed");
1605                                 err  = EMAIL_ERROR_OUT_OF_MEMORY;
1606                                 goto FINISH_OFF;
1607                         }
1608
1609                         uid_list_prev = uid_list_fast;
1610
1611                         if (uid_list_fast->next == NULL){
1612                                 /* Single list entry */
1613                                 snprintf(uid_range, uid_range_size, "%d", atoi(uid_list_fast->uid));
1614                         }
1615                         else{
1616                                 /* forming range of uids to be passed */
1617                                 while (uid_list_fast  != NULL){
1618                                         /* uid_list_fast = uid_list_fast->next; */
1619
1620                                         if ((uid_list_fast->next != NULL) && (((atoi(uid_list_prev->uid)) - (atoi(uid_list_fast->next->uid))) == 1)){
1621                                                 index += snprintf(uid_range+index, uid_range_size, "%d", atoi(uid_list_prev->uid));
1622
1623                                                 uid_list_fast = uid_list_fast->next;
1624                                                 uid_list_prev = uid_list_fast;
1625
1626                                                 /* to make UID range string "abc, XX : YY" */
1627                                                 while (uid_list_fast  != NULL){
1628                                                         if (uid_list_fast->next == NULL)
1629                                                                 break;
1630                                                         if (((atoi(uid_list_prev->uid)) - (atoi(uid_list_fast->next->uid))) == 1){
1631                                                                 uid_list_fast = uid_list_fast->next;
1632                                                                 uid_list_prev = uid_list_fast;
1633                                                         }
1634                                                         else
1635                                                                 break;
1636                                                 }
1637                                                 if ((uid_list_fast  != NULL) && (uid_list_fast->next  != NULL))
1638                                                         index  += snprintf(uid_range+index, uid_range_size, ":%d,", atoi(uid_list_prev->uid));
1639                                                 else
1640                                                         index  += snprintf(uid_range+index, uid_range_size, ":%d", atoi(uid_list_prev->uid));
1641
1642                                                 uid_list_fast = uid_list_fast->next;
1643                                                 uid_list_prev = uid_list_fast;
1644                                         }
1645                                         else{
1646                                                 if (uid_list_fast->next  != NULL)
1647                                                         index  += snprintf(uid_range+index, uid_range_size, "%d,", atoi(uid_list_prev->uid));
1648                                                 else
1649                                                         index  += snprintf(uid_range+index, uid_range_size, "%d", atoi(uid_list_prev->uid));
1650                                                 uid_list_fast = uid_list_fast->next;
1651                                                 uid_list_prev = uid_list_fast;
1652                                         }
1653                                 }
1654                         }
1655
1656                         /* h.gahlaut :  [Start] */
1657                         /* Adding this check here to check if application has called cancel job. */
1658                         /* This checking should be done because fetching 50 headers will take time  */
1659                         FINISH_OFF_IF_CANCELED;
1660
1661                         EM_DEBUG_LOG("index [%d]", index);
1662
1663                         /*  h.gahlaut :  [End] */
1664                         uid_elem = uid_list;
1665                         if (stream && uid_elem){
1666                                 EM_DEBUG_LOG("msgno : %d", uid_elem->msgno);
1667                                 ((MAILSTREAM *)stream)->nmsgs = uid_elem->msgno;
1668                         }
1669                         else{
1670                                 EM_DEBUG_LOG("Uid List Null");
1671                         }
1672                         EM_DEBUG_LOG("Calling ... mail_fetch_fast. uid_range [%s]", uid_range);
1673                         mail_fetch_fast(stream, uid_range, FT_UID | FT_PEEK | FT_NEEDENV);
1674                         EM_SAFE_FREE(uid_range);
1675                 }
1676 #endif
1677
1678                 /*  h.gahlaut@samsung.com :  Clear the event queue of partial body download thread before starting fetching new headers  */
1679 #ifndef __PARTIAL_BODY_FOR_POP3__
1680                 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4){
1681 #endif /*  __PARTIAL_BODY_FOR_POP3__ */
1682                         /*  Partial body download feature is only for IMAP accounts  */
1683                         if (false == emcore_clear_partial_body_thd_event_que(&err))
1684                                 EM_DEBUG_LOG("emcore_clear_partial_body_thd_event_que failed [%d]", err);
1685 #ifndef __PARTIAL_BODY_FOR_POP3__
1686                 }
1687 #endif /*  __PARTIAL_BODY_FOR_POP3__ */
1688                 while (uid_elem) {
1689                         EM_PROFILE_BEGIN(emCoreSyncHeaderEachMail);
1690                         EM_DEBUG_LOG("mail_fetchstructure_full  :  uid_elem->msgno[%d]", uid_elem->msgno);
1691
1692                         env = NULL;
1693
1694                         if (uid_elem->msgno > ((MAILSTREAM *)stream)->nmsgs)
1695                                 EM_DEBUG_EXCEPTION("Warnings! msgno[%d] can't be greater than nmsgs[%d]. It might cause crash.", uid_elem->msgno, ((MAILSTREAM *)stream)->nmsgs);
1696                         else{
1697
1698 #ifdef __FEATURE_HEADER_OPTIMIZATION__
1699                                 if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) /* Fetch env from cache in case of IMAP */
1700                                         env = mail_fetchstructure_full(stream, uid_elem->msgno, NULL, FT_PEEK, 0);
1701                                 else /* Fetch header from network in case of POP */
1702                                         env = mail_fetchstructure_full(stream, uid_elem->msgno, NULL, FT_PEEK, 1);
1703 #else
1704                                 env = mail_fetchstructure_full(stream, uid_elem->msgno, NULL, FT_PEEK);
1705 #endif
1706                         }
1707                         FINISH_OFF_IF_CANCELED;
1708
1709                         if (env != NULL){
1710                                 int matched = -1;
1711
1712                                 if (!emcore_make_mail_tbl_data_from_envelope(stream, env, uid_elem, &new_mail_tbl_data, &err) || !new_mail_tbl_data) {
1713                                         EM_DEBUG_EXCEPTION("emcore_make_mail_tbl_data_from_envelope failed [%d]", err);
1714                                         goto FINISH_OFF;
1715                                 }
1716
1717                                 if (rule && input_mailbox_tbl_spam && !emcore_check_rule(new_mail_tbl_data->full_address_from, new_mail_tbl_data->subject, rule, rule_len, &matched, &err)) {
1718                                         EM_DEBUG_EXCEPTION("emcore_check_rule failed [%d]", err);
1719                                         goto FINISH_OFF;
1720                                 }
1721
1722                                 if (matched >= 0 && input_mailbox_tbl_spam){    /*  add filtered mails to SPAMBOX */
1723                                         EM_DEBUG_LOG("mail[%d] added to spambox", mail_id);
1724
1725                                         if ( (err = emcore_add_mail_to_mailbox(input_mailbox_tbl_spam, new_mail_tbl_data, &mail_id, &thread_id)) != EMAIL_ERROR_NONE) {
1726                                                 EM_DEBUG_EXCEPTION("emcore_add_mail_to_mailbox falied [%d]", err);
1727                                                 goto FINISH_OFF;
1728                                         }
1729
1730                                         if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4){
1731                                                 if (!emcore_move_mail_on_server(account_id, input_mailbox_tbl->mailbox_id,  &mail_id, 1, input_mailbox_tbl_spam->mailbox_name, &err)){
1732                                                         EM_DEBUG_EXCEPTION("emcore_move_mail_on_server falied [%d]", err);
1733                                                         goto FINISH_OFF;
1734                                                 }
1735                                         }
1736                                 } else {
1737                                         /*  add mails to specified mail box */
1738                                         EM_DEBUG_LOG("mail[%d] moved to input_mailbox_tbl [%s]", mail_id, input_mailbox_tbl->mailbox_name);
1739                                         if ( (err = emcore_add_mail_to_mailbox(input_mailbox_tbl, new_mail_tbl_data, &mail_id, &thread_id)) != EMAIL_ERROR_NONE) {
1740                                                 EM_DEBUG_EXCEPTION("emcore_add_mail_to_mailbox falied [%d]", err);
1741                                                 goto FINISH_OFF;
1742                                         }
1743
1744                                         /*h.gahlaut :  Start partial body dowload using partial body thread only for IMAP accounts*/
1745 #ifndef __PARTIAL_BODY_FOR_POP3__
1746                                         if (account_ref->incoming_server_type == EMAIL_SERVER_TYPE_IMAP4) {
1747 #endif /*  __PARTIAL_BODY_FOR_POP3__ */
1748                                                 if (account_ref->auto_download_size != 0) {
1749                                                         if (false == emcore_initiate_pbd(stream, account_id, mail_id, uid_elem->uid, input_mailbox_tbl->mailbox_id, &err))
1750                                                                 EM_DEBUG_LOG("Partial body download initiation failed [%d]", err);
1751                                                 }
1752 #ifndef __PARTIAL_BODY_FOR_POP3__
1753                                         }
1754 #endif /*  __PARTIAL_BODY_FOR_POP3__ */
1755 /*
1756                                         if (!uid_elem->flag.seen && input_mailbox_tbl->mailbox_type != EMAIL_MAILBOX_TYPE_SPAMBOX)
1757                                                 emcore_add_notification_for_unread_mail(new_mail_tbl_data);
1758 */
1759                                         FINISH_OFF_IF_CANCELED;
1760
1761                                         if (!uid_elem->flag.seen)
1762                                                 unread++;
1763
1764                                         percentage = ((i+1) * 100) / total ;
1765                                         EM_DEBUG_LOG("Header Percentage Completed [%d] : [%d/%d]  mail_id [%d]", percentage, i+1, total, mail_id);
1766                                         SNPRINTF(mailbox_id_param_string, 10, "%d", input_mailbox_tbl->mailbox_id);
1767
1768                                         if (!emcore_notify_storage_event(NOTI_MAIL_ADD, account_id, mail_id, mailbox_id_param_string, thread_id))
1769                                                 EM_DEBUG_EXCEPTION("emcore_notify_storage_event [NOTI_MAIL_ADD] failed");
1770                                 }
1771
1772                                 /* Set contact log */
1773                                 switch (input_mailbox_tbl->mailbox_type) {
1774                                 case EMAIL_MAILBOX_TYPE_INBOX :
1775                                         if ((err = emcore_set_received_contacts_log(new_mail_tbl_data)) != EMAIL_ERROR_NONE) {
1776                                                 EM_DEBUG_EXCEPTION("emcore_set_received_contacts_log failed : [%d]", err);
1777                                         }
1778                                         break;
1779                                 case EMAIL_MAILBOX_TYPE_SENTBOX:
1780                                 case EMAIL_MAILBOX_TYPE_OUTBOX:
1781                                         if ((err = emcore_set_sent_contacts_log(new_mail_tbl_data)) != EMAIL_ERROR_NONE) {
1782                                                 EM_DEBUG_EXCEPTION("emcore_set_sent_contacts_log failed : [%d]", err);
1783                                         }
1784                                         break;
1785                                 default:
1786                                         EM_DEBUG_LOG("Mailbox type : [%d]", input_mailbox_tbl->mailbox_type);
1787                                         break;
1788                                 }
1789
1790                                 /*  Release for envelope is not required and it may cause crash. Don't free the memory for envelope here. */
1791                                 /*  Envelope data will be freed by garbage collector in mail_close_full */
1792                                 if (new_mail_tbl_data){
1793                                         emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
1794                                         new_mail_tbl_data = NULL;
1795                                 }
1796
1797                                 FINISH_OFF_IF_CANCELED;
1798                         }
1799
1800                         uid_elem = uid_elem->next;
1801                         i++;
1802                         EM_PROFILE_END(emCoreSyncHeaderEachMail);
1803                 }
1804                 EM_PROFILE_END(emCoreSyncHeaderwhileloop);
1805         }
1806
1807         ret = true;
1808 FINISH_OFF:
1809
1810         *unread_mail = unread;
1811
1812         if (account_ref) {
1813                 emcore_free_account(account_ref);
1814                 EM_SAFE_FREE(account_ref);
1815         }
1816
1817         if (!emcore_remove_overflowed_mails(input_mailbox_tbl, &err_2))
1818                 EM_DEBUG_EXCEPTION("emcore_remove_overflowed_mails failed - %d", err_2);
1819
1820         if (input_mailbox_tbl)
1821                 emstorage_stamp_last_sync_time_of_mailbox(input_mailbox_tbl->mailbox_id, 1);
1822
1823 #ifdef __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__
1824         if ((err_from_vconf = vconf_set_int(VCONFKEY_EMAIL_SYNC_STATE, 0)) != 0 ) {
1825                 EM_DEBUG_LOG("vconf_set_int failed [%d]", err_from_vconf);
1826         }
1827 #endif /* __FEATURE_SUPPORT_SYNC_STATE_ON_NOTI_BAR__ */
1828
1829         if (new_mail_tbl_data)
1830                 emstorage_free_mail(&new_mail_tbl_data, 1, NULL);
1831
1832         if (uid_list != NULL){
1833                 emcore_free_uids(uid_list, NULL);
1834                 /*  uid_list point to the same memory with input_mailbox_tbl->user_data.  */
1835                 /*  input_mailbox_tbl->user_data should be set NULL if uid_list is freed */
1836                 *input_uid_list = NULL;
1837         }
1838
1839         EM_SAFE_FREE(uid_range);
1840
1841         if (rule  != NULL)
1842                 emstorage_free_rule(&rule, rule_len, NULL);
1843
1844 #ifdef __FEATURE_KEEP_CONNECTION__
1845         if (stream  != NULL) {
1846 #else /*  __FEATURE_KEEP_CONNECTION__    */
1847         if (stream  != NULL && stream_recycle == NULL)  {
1848 #endif /*  __FEATURE_KEEP_CONNECTION__   */
1849                 emcore_close_mailbox(0, stream);
1850                 stream = NULL;
1851         }
1852
1853         if (err_code  != NULL)
1854                 *err_code = err;
1855
1856         EM_PROFILE_END(emCoreSyncHeader);
1857         EM_DEBUG_FUNC_END("ret [%d]", ret);
1858         return ret;
1859 }
1860
1861
1862 emcore_uid_list *__ReverseList(emcore_uid_list *uid_list)
1863 {
1864         emcore_uid_list *temp, *current, *result;
1865
1866         temp = NULL;
1867         result = NULL;
1868         current = uid_list;
1869
1870         while (current != NULL){
1871                 temp = current->next;
1872                 current->next = result;
1873                 result = current;
1874                 current = temp;
1875         }
1876         uid_list = result;
1877         return uid_list;
1878 }
1879
1880
1881
1882 int emcore_download_uid_all(email_internal_mailbox_t *mailbox, emcore_uid_list** uid_list, int *total, emstorage_read_mail_uid_tbl_t *downloaded_uids, int count, emcore_get_uids_for_delete_t for_delete, int *err_code)
1883 {
1884         EM_DEBUG_FUNC_BEGIN("mailbox[%p], uid_list[%p], total[%p], downloaded_uids[%p], count[%d], for_delete[%d], err_code[%p]", mailbox, uid_list, total, downloaded_uids, count, for_delete, err_code);
1885
1886         int ret = false;
1887         int err = EMAIL_ERROR_NONE;
1888
1889         MAILSTREAM *stream = NULL;
1890         email_account_t *ref_account = NULL;
1891         emcore_uid_list *uid_elem = NULL;
1892         emcore_uid_list *fetch_data_p = NULL;
1893         void *tmp_stream = NULL;
1894         char cmd[64] = {0x00, };
1895         char *p = NULL;
1896
1897         if (!mailbox || !uid_list) {
1898                 EM_DEBUG_EXCEPTION("mailbox[%p], uid_list[%p], total[%p], downloaded_uids[%p], count[%d], for_delete[%d]", mailbox, uid_list, total, downloaded_uids, count, for_delete);
1899
1900                 err = EMAIL_ERROR_INVALID_PARAM;
1901                 goto FINISH_OFF;
1902         }
1903
1904         if (!(ref_account = emcore_get_account_reference(mailbox->account_id))) {
1905                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed - %d", mailbox->account_id);
1906
1907                 err = EMAIL_ERROR_INVALID_ACCOUNT;
1908                 goto FINISH_OFF;
1909         }
1910
1911         if (!mailbox->mail_stream) {
1912                 if (!emcore_connect_to_remote_mailbox(mailbox->account_id, mailbox->mailbox_id, (void **)&tmp_stream, &err)){
1913                         EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed...");
1914
1915                         goto FINISH_OFF;
1916                 }
1917
1918                 stream = (MAILSTREAM *)tmp_stream;
1919         }
1920         else
1921                 stream = mailbox->mail_stream;
1922
1923         if (ref_account->incoming_server_type == EMAIL_SERVER_TYPE_POP3) {              /*  POP3 */
1924                 POP3LOCAL *pop3local = NULL;
1925
1926                 if (!stream || !(pop3local = stream->local) || !pop3local->netstream) {
1927                         err = EMAIL_ERROR_INVALID_PARAM;                /* EMAIL_ERROR_UNKNOWN; */
1928                         goto FINISH_OFF;
1929                 }
1930
1931                 /*  send UIDL */
1932                 memset(cmd, 0x00, sizeof(cmd));
1933
1934                 SNPRINTF(cmd, sizeof(cmd), "UIDL\015\012");
1935
1936 #ifdef FEATURE_CORE_DEBUG
1937                 EM_DEBUG_LOG("[POP3] >>> [%s]", cmd);
1938 #endif
1939
1940                 if (!net_sout(pop3local->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
1941                         EM_DEBUG_EXCEPTION("net_sout failed...");
1942
1943                         err = EMAIL_ERROR_CONNECTION_BROKEN;
1944                         goto FINISH_OFF;
1945                 }
1946
1947                 /*  get uid from replied data */
1948                 while (pop3local->netstream) {
1949                         char *s = NULL;
1950
1951                         if (!(p = net_getline(pop3local->netstream)))
1952                                 break;
1953
1954 #ifdef FEATURE_CORE_DEBUG
1955                         EM_DEBUG_LOG(" [POP3] <<< [%s]", p);
1956 #endif
1957
1958                         /*  replied error "-ERR" */
1959                         if (*p == '-') {
1960                                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
1961                                 goto FINISH_OFF;
1962                         }
1963
1964                         /*  replied success "+OK" */
1965                         if (*p == '+') {
1966                                 free(p); p = NULL;
1967                                 continue;
1968                         }
1969
1970                         /*  end of command */
1971                         if (*p == '.')
1972                                 break;
1973
1974                         /* EM_DEBUG_LOG("UID list [%s]", p); */
1975
1976                         uid_elem = (emcore_uid_list *)malloc(sizeof(emcore_uid_list));
1977                         if (!uid_elem) {
1978                                 EM_DEBUG_EXCEPTION("malloc falied...");
1979
1980                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1981                                 goto FINISH_OFF;
1982                         }
1983
1984                         memset(uid_elem, 0x00, sizeof(emcore_uid_list));
1985
1986                         /*  format  :  "1 AAA6FHEAAAQrB6c1ymXxty04yks7hcQ7" */
1987
1988                         /*  save uid                     */
1989                         s = strstr(p, " ");
1990                         if (s) {
1991                                 *s = '\0';
1992                                 uid_elem->msgno = atoi(p);
1993                                 uid_elem->uid = EM_SAFE_STRDUP(s+1);
1994                         }
1995
1996                         /*  check downloaded_uids */
1997                         if (downloaded_uids) {
1998                                 int i;
1999                                 for (i = 0; i < count; ++i) {
2000                                         if (!strcmp(uid_elem->uid, downloaded_uids[i].s_uid)) {
2001                                                 downloaded_uids[i].flag = 1;
2002                                                 break;
2003                                         }
2004                                 }
2005                         }
2006
2007                         if (*uid_list) {
2008                                 fetch_data_p = *uid_list;
2009
2010                                 while (fetch_data_p->next)
2011                                         fetch_data_p = fetch_data_p->next;
2012
2013                                 fetch_data_p->next = uid_elem;
2014                         }
2015                         else
2016                                 *uid_list = uid_elem;
2017
2018                         if (total)
2019                                 ++(*total);
2020
2021                         free(p); p = NULL;
2022                 }
2023         }
2024         else {          /*  IMAP */
2025                 IMAPLOCAL *imaplocal = NULL;
2026                 char tag[16];
2027                 char *s = NULL;
2028                 char *t = NULL;
2029
2030                 if (!stream || !(imaplocal = stream->local) || !imaplocal->netstream) {
2031                         err = EMAIL_ERROR_INVALID_PARAM;                /* EMAIL_ERROR_UNKNOWN; */
2032                         goto FINISH_OFF;
2033                 }
2034
2035                 /*  send FETCH UID */
2036                 memset(tag, 0x00, sizeof(tag));
2037                 memset(cmd, 0x00, sizeof(cmd));
2038
2039                 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
2040                 SNPRINTF(cmd, sizeof(cmd), "%s UID FETCH %d:* (FLAGS)\015\012", tag, 1);
2041 #ifdef FEATURE_CORE_DEBUG
2042                 EM_DEBUG_LOG("[IMAP] >>> %s", cmd);
2043 #endif
2044                 if (!net_sout(imaplocal->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
2045                         EM_DEBUG_EXCEPTION("net_sout failed...");
2046
2047                         err = EMAIL_ERROR_CONNECTION_BROKEN;
2048                         goto FINISH_OFF;
2049                 }
2050
2051                 /*  get uid from replied data */
2052                 while (imaplocal->netstream) {
2053                         if (!(p = net_getline(imaplocal->netstream)))
2054                                 break;
2055
2056                         /* EM_DEBUG_LOG(" [IMAP] <<< %s", p); */
2057
2058                         /*  tagged line - end of command */
2059                         if (!strncmp(p, tag, EM_SAFE_STRLEN(tag)))
2060                                 break;
2061
2062                         /*  check that reply is reply to our command */
2063                         /*  format  :  "* 9 FETCH (UID 68)" */
2064                         if (!strstr(p, "FETCH (FLAGS")) {
2065                                 free(p); p = NULL;
2066                                 continue;
2067                         }
2068
2069                         if (for_delete == EM_CORE_GET_UIDS_FOR_NO_DELETE) {
2070                                 if ((ref_account->retrieval_mode == EMAIL_IMAP4_RETRIEVAL_MODE_NEW) && (strstr(p, "\\Seen"))) {
2071                                         free(p); p = NULL;
2072                                         continue;
2073                                 }
2074                         }
2075
2076                         uid_elem = (emcore_uid_list *)malloc(sizeof(emcore_uid_list));
2077                         if (!uid_elem) {
2078                                 EM_DEBUG_EXCEPTION("malloc failed...");
2079
2080                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2081                                 goto FINISH_OFF;
2082                         }
2083
2084                         memset(uid_elem, 0x00, sizeof(emcore_uid_list));
2085                         /*  parse uid, sequence, flags from replied data */
2086
2087                         /*  parse uid from replied data */
2088                         s = p+2;
2089                         t = strchr(s, ' ');
2090
2091                         if (!t) {
2092                                 err = EMAIL_ERROR_INVALID_RESPONSE;
2093                                 goto FINISH_OFF;
2094                         }
2095
2096                         *t = '\0';
2097
2098                         /*  save sequence */
2099                         uid_elem->msgno = atoi(s);
2100
2101                         if (strstr(++t, "\\Deleted"))
2102                                 uid_elem->flag.deleted = 1;
2103
2104                         /*  parse uid */
2105                         s  = strstr(++t, "UID ");
2106                         if (s) {
2107                                 s  += strlen("UID ");
2108                                 t  = strchr(s, ')');
2109
2110                                 if (!t) {
2111                                         err = EMAIL_ERROR_INVALID_RESPONSE;
2112                                         goto FINISH_OFF;
2113                                 }
2114
2115                                 *t = '\0';
2116                         }
2117                         else {
2118                                 err = EMAIL_ERROR_INVALID_RESPONSE;
2119                                 goto FINISH_OFF;
2120                         }
2121
2122                         /*  save uid */
2123                         uid_elem->uid = EM_SAFE_STRDUP(s);
2124
2125                         /*  check downloaded_uids */
2126                         if (downloaded_uids) {
2127                                 int i;
2128                                 for (i = 0; i < count; ++i) {
2129                                         if (uid_elem->uid && !strcmp(uid_elem->uid, downloaded_uids[i].s_uid)) {
2130                                                 downloaded_uids[i].flag = 1;
2131                                                 free(uid_elem->uid);
2132                                                 free(uid_elem); uid_elem = NULL;
2133                                                 break;
2134                                         }
2135                                 }
2136                         }
2137
2138                         if (uid_elem) {
2139                                 if (*uid_list) {
2140                                         fetch_data_p = *uid_list;
2141
2142                                         while (fetch_data_p->next)
2143                                                 fetch_data_p = fetch_data_p->next;
2144
2145                                         fetch_data_p->next = uid_elem;
2146                                 }
2147                                 else {
2148                                         *uid_list = uid_elem;
2149                                 }
2150
2151                                 if (total)
2152                                         ++(*total);
2153                         }
2154
2155                         free(p); p = NULL;
2156                 }
2157         }
2158
2159         ret = true;
2160
2161 FINISH_OFF:
2162         if (uid_elem && ret == false)
2163                 free(uid_elem);
2164
2165         if (p)
2166                 free(p);
2167
2168         if (mailbox && !mailbox->mail_stream) {
2169                 emcore_close_mailbox(0, stream);
2170                 stream = NULL;
2171         }
2172
2173         if (ref_account) {
2174                 emcore_free_account(ref_account);
2175                 EM_SAFE_FREE(ref_account);
2176         }
2177
2178         if (err_code)
2179                 *err_code = err;
2180
2181         EM_DEBUG_FUNC_END("ret [%d]", ret);
2182         return ret;
2183 }
2184
2185 int emcore_download_imap_msgno(email_internal_mailbox_t *mailbox, char *uid, int *msgno, int *err_code)
2186 {
2187         EM_DEBUG_FUNC_BEGIN("mailbox[%p], uid[%p], msgno[%p], err_code[%p]", mailbox, uid, msgno, err_code);
2188
2189         int ret = false;
2190         int err = EMAIL_ERROR_NONE;
2191         MAILSTREAM *stream = NULL;
2192         IMAPLOCAL *imaplocal = NULL;
2193         email_account_t *ref_account = NULL;
2194         void *tmp_stream = NULL;
2195         char tag[32], cmd[64];
2196         char *p = NULL;
2197
2198         if (!mailbox || !uid) {
2199                 EM_DEBUG_EXCEPTION("mailbox[%p], uid[%p], msgno[%p]", mailbox, uid, msgno);
2200
2201                 err = EMAIL_ERROR_INVALID_PARAM;
2202                 goto FINISH_OFF;
2203         }
2204
2205         if (!(ref_account = emcore_get_account_reference(mailbox->account_id))) {
2206                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", mailbox->account_id);
2207
2208                 err = EMAIL_ERROR_INVALID_ACCOUNT;
2209                 goto FINISH_OFF;
2210         }
2211
2212         if (ref_account->incoming_server_type  != EMAIL_SERVER_TYPE_IMAP4) {
2213                 err = EMAIL_ERROR_INVALID_ACCOUNT;              /* EMAIL_ERROR_INVALID_PARAM; */
2214                 goto FINISH_OFF;
2215         }
2216
2217         if (!mailbox->mail_stream) {
2218                 if (!emcore_connect_to_remote_mailbox(mailbox->account_id, mailbox->mailbox_id, (void **)&tmp_stream, &err)) {
2219                         EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed - %d", err);
2220
2221                         goto FINISH_OFF;
2222                 }
2223
2224                 stream = (MAILSTREAM *)tmp_stream;
2225         }
2226         else
2227                 stream = mailbox->mail_stream;
2228
2229         if (!stream || !(imaplocal = stream->local) || !imaplocal->netstream) {
2230                 err = EMAIL_ERROR_INVALID_PARAM;                /* EMAIL_ERROR_UNKNOWN; */
2231                 goto FINISH_OFF;
2232         }
2233
2234         /*  send SEARCH UID */
2235         memset(tag, 0x00, sizeof(tag));
2236         memset(cmd, 0x00, sizeof(cmd));
2237
2238         SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
2239         SNPRINTF(cmd, sizeof(cmd), "%s SEARCH UID %s\015\012", tag, uid);
2240
2241         if (!net_sout(imaplocal->netstream, cmd, (int)EM_SAFE_STRLEN(cmd))) {
2242                 EM_DEBUG_EXCEPTION("net_sout failed...");
2243
2244                 err = EMAIL_ERROR_CONNECTION_BROKEN;
2245                 goto FINISH_OFF;
2246         }
2247
2248         /*  get message number from replied data */
2249         while (imaplocal->netstream) {
2250                 if (!(p = net_getline(imaplocal->netstream)))
2251                         break;
2252
2253                 /*  tagged line - end of command */
2254                 if (!strncmp(p, tag, EM_SAFE_STRLEN(tag)))
2255                         break;
2256
2257                 /*  check that reply is reply to our command */
2258                 /*  format :  "* SEARCH 68", if not found, "* SEARCH" */
2259                 if (!strstr(p, "SEARCH ") || (p[9] < '0' || p[9] > '9')) {
2260                         free(p); p = NULL;
2261                         continue;
2262                 }
2263
2264                 if (msgno)
2265                         *msgno = atoi(p+9);
2266
2267                 free(p); p = NULL;
2268
2269                 ret = true;
2270         }
2271
2272         if (ret  != true)
2273                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
2274
2275 FINISH_OFF:
2276         if (p)
2277                 free(p);
2278
2279         if (mailbox && !mailbox->mail_stream){
2280                 emcore_close_mailbox(0, stream);
2281                 stream = NULL;
2282         }
2283
2284         if (ref_account) {
2285                 emcore_free_account(ref_account);
2286                 EM_SAFE_FREE(ref_account);
2287         }
2288
2289         if (err_code)
2290                 *err_code = err;
2291
2292         EM_DEBUG_FUNC_END("ret [%d]", ret);
2293         return ret;
2294 }
2295
2296 int emcore_get_msgno(emcore_uid_list *uid_list, char *uid, int *msgno, int *err_code)
2297 {
2298         EM_DEBUG_FUNC_BEGIN("uid_list[%p], uid[%s], msgno[%p], err_code[%p]", uid_list, uid, msgno, err_code);
2299
2300         int ret = false;
2301         int err = EMAIL_ERROR_MAIL_NOT_FOUND;           /* EMAIL_ERROR_NONE; */
2302
2303         if (!uid || !msgno || !uid_list) {
2304                 EM_DEBUG_EXCEPTION("uid_list[%p], uid[%p], msgno[%p]", uid_list, uid, msgno);
2305
2306                 err = EMAIL_ERROR_INVALID_PARAM;
2307                 goto FINISH_OFF;
2308         }
2309
2310         EM_DEBUG_LOG(" >> uid[%s]", uid);
2311
2312         while (uid_list) {
2313                 if (!strcmp(uid_list->uid, uid)) {
2314                         *msgno = uid_list->msgno;
2315
2316                         EM_DEBUG_LOG("*msgno[%d]", *msgno);
2317
2318                         err = EMAIL_ERROR_NONE;
2319
2320                         ret = true;
2321                         break;
2322                 }
2323
2324                 uid_list = uid_list->next;
2325         }
2326
2327 FINISH_OFF:
2328         if (err_code  != NULL)
2329                 *err_code = err;
2330
2331         EM_DEBUG_FUNC_END("ret [%d]", ret);
2332         return ret;
2333 }
2334
2335 int emcore_get_uid(emcore_uid_list *uid_list, int msgno, char **uid, int *err_code)
2336 {
2337         EM_DEBUG_FUNC_BEGIN();
2338
2339         int ret = false, err = EMAIL_ERROR_NONE;
2340
2341         if (!uid || msgno <= 0 || !uid_list){
2342                 err = EMAIL_ERROR_INVALID_PARAM;
2343                 goto FINISH_OFF;
2344         }
2345
2346         while (uid_list) {
2347                 if (uid_list->msgno == msgno) {
2348                         if (uid) {
2349                                 if (!(*uid = EM_SAFE_STRDUP(uid_list->uid))) {
2350                                         EM_DEBUG_EXCEPTION("strdup failed...");
2351                                         err = EMAIL_ERROR_OUT_OF_MEMORY;
2352                                         goto FINISH_OFF;
2353                                 }
2354
2355                                 ret = true;
2356                                 break;
2357                         }
2358                 }
2359
2360                 uid_list = uid_list->next;
2361         }
2362
2363         if (ret  != true)
2364                 err = EMAIL_ERROR_MAIL_NOT_FOUND;
2365
2366 FINISH_OFF:
2367         if (err_code)
2368                 *err_code = err;
2369
2370         EM_DEBUG_FUNC_END("ret [%d]", ret);
2371         return ret;
2372 }
2373
2374 int emcore_free_uids(emcore_uid_list *uid_list, int *err_code)
2375 {
2376         EM_DEBUG_FUNC_BEGIN("uid_list[%p], err_code[%p]", uid_list, err_code);
2377
2378         int ret = false;
2379         int err = EMAIL_ERROR_NONE;
2380         emcore_uid_list *p = NULL;
2381
2382         if (!uid_list) {
2383                 EM_DEBUG_EXCEPTION(" uid_list[%p]\n", uid_list);
2384                 err = EMAIL_ERROR_INVALID_PARAM;
2385                 goto FINISH_OFF;
2386         }
2387
2388         while (uid_list) {
2389                 p = uid_list;
2390                 uid_list = uid_list->next;
2391                 EM_SAFE_FREE(p->uid);
2392                 free(p);
2393                 p = NULL;
2394         }
2395
2396         ret = true;
2397
2398 FINISH_OFF:
2399         if (err_code)
2400                 *err_code = err;
2401
2402         EM_DEBUG_FUNC_END("ret [%d]", ret);
2403         return ret;
2404 }
2405
2406
2407 #ifdef __FEATURE_SYNC_CLIENT_TO_SERVER__
2408 /* callback for GET_APPENDUID - shasikala.p */
2409 void mail_appenduid(char *mailbox, unsigned long uidvalidity, SEARCHSET *set)
2410 {
2411         EM_DEBUG_FUNC_BEGIN("mailbox - %s", mailbox);
2412     EM_DEBUG_LOG("UID - %ld", set->first);
2413
2414     memset(g_append_uid_rsp, 0x00, 129);
2415
2416     sprintf(g_append_uid_rsp, "%ld", set->first);
2417     EM_DEBUG_LOG("append uid - %s", g_append_uid_rsp);
2418 }
2419
2420 INTERNAL_FUNC int emcore_sync_mail_from_client_to_server(int mail_id)
2421 {
2422         EM_DEBUG_FUNC_BEGIN("mail_id [%d]", mail_id);
2423
2424         int                             err                  = EMAIL_ERROR_NONE;
2425         int                             len                  = 0;
2426         int                             read_size            = 0;
2427         int                             attachment_tbl_count = 0;
2428         char                           *fname                = NULL;
2429         char                           *long_enc_path        = NULL;
2430         char                           *data                 = NULL;
2431         char                            set_flags[100]       = { 0, };
2432         ENVELOPE                       *envelope             = NULL;
2433         FILE                           *fp                   = NULL;
2434         STRING                          str;
2435         MAILSTREAM                     *stream               = NULL;
2436         email_account_t                *account_ref          = NULL;
2437         emstorage_mail_tbl_t           *mail_table_data      = NULL;
2438         emstorage_attachment_tbl_t     *attachment_tbl_data  = NULL;
2439         emstorage_mailbox_tbl_t        *mailbox_tbl          = NULL;
2440
2441         /*  get a mail from mail table */
2442         if (!emstorage_get_mail_by_id(mail_id, &mail_table_data, true, &err)) {
2443                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2444                 goto FINISH_OFF;
2445         }
2446
2447         account_ref = emcore_get_account_reference(mail_table_data->account_id);
2448
2449         if(account_ref == NULL || account_ref->incoming_server_type != EMAIL_SERVER_TYPE_IMAP4) {
2450                 EM_DEBUG_EXCEPTION("This account doesn't support sync");
2451                 err = EMAIL_ERROR_INVALID_ACCOUNT;
2452                 goto FINISH_OFF;
2453         }
2454
2455         if ((err = emstorage_get_attachment_list(mail_id, false, &attachment_tbl_data, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
2456                 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2457                 goto FINISH_OFF;
2458         }
2459
2460         EM_DEBUG_LOG("mailbox_name [%s]", mail_table_data->mailbox_name);
2461         if (!emstorage_get_mailbox_by_name(mail_table_data->account_id, 0, mail_table_data->mailbox_name, &mailbox_tbl, false, &err) || !mailbox_tbl){
2462                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed [%d]", err);
2463                 goto FINISH_OFF;
2464         }
2465
2466         if (mailbox_tbl->local_yn == 0) {
2467                 EM_DEBUG_EXCEPTION("The mailbox [%s] is not on server.", mail_table_data->mailbox_name);
2468                 err = EMAIL_ERROR_INVALID_MAILBOX;
2469                 goto FINISH_OFF;
2470         }
2471
2472         if (!emcore_get_long_encoded_path(mail_table_data->account_id, mail_table_data->mailbox_name, '/', &long_enc_path, &err)) {
2473                 EM_DEBUG_EXCEPTION(">>emcore_get_long_encoded_path  :  Failed [%d] ", err);
2474                 goto FINISH_OFF;
2475         }
2476
2477         if (!long_enc_path) {
2478             EM_DEBUG_EXCEPTION(">>long_enc_path  :  NULL ");
2479             goto FINISH_OFF;
2480         }
2481
2482         if (!emcore_make_rfc822_file_from_mail(mail_table_data, attachment_tbl_data, attachment_tbl_count, &envelope, &fname, NULL, &err)){
2483             EM_DEBUG_EXCEPTION(" emcore_make_rfc822_file_from_mail failed [%d]", err);
2484             goto FINISH_OFF;
2485         }
2486
2487         if (fname){
2488             if (!(fp = fopen(fname, "a+"))) {
2489                 EM_DEBUG_EXCEPTION("fopen failed - %s", fname);
2490                         err = EMAIL_ERROR_SYSTEM_FAILURE;
2491                 goto FINISH_OFF;
2492             }
2493         }
2494
2495         if (!fp) {
2496             EM_DEBUG_EXCEPTION("fp is NULL..!");
2497                 err = EMAIL_ERROR_SYSTEM_FAILURE;
2498             goto FINISH_OFF;
2499         }
2500
2501         rewind(fp);
2502
2503         /* get file length */
2504         if(fseek(fp, 0, SEEK_END) != 0 || (len = ftell(fp)) == -1) {
2505                 EM_DEBUG_EXCEPTION("fseek or ftell failed");
2506                 err = EMAIL_ERROR_SYSTEM_FAILURE;
2507                 goto FINISH_OFF;
2508         }
2509
2510         if (fname)
2511                 EM_DEBUG_LOG("Composed file name [%s] and file size [%d]", fname, len);
2512
2513         rewind(fp);
2514
2515         stream = NULL;
2516         if (!emcore_connect_to_remote_mailbox(mail_table_data->account_id, 0, (void **)&stream, &err)){
2517             EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed :  Mailbox open[%d]", err);
2518             goto FINISH_OFF;
2519         }
2520
2521         /* added for copying server UID to DB */
2522         mail_parameters(stream, SET_APPENDUID, mail_appenduid);
2523
2524         data = (char *)malloc(len + 1);
2525         /* copy data from file to data */
2526         read_size = fread(data, sizeof (char), len, fp);
2527         if (read_size  != len){
2528             /* read faiil. */
2529             EM_DEBUG_EXCEPTION("Read from file failed");
2530         }
2531
2532         INIT(&str, mail_string, data, len);
2533
2534         sprintf(set_flags, "\\Seen");
2535
2536         if (mail_table_data->flags_seen_field){
2537                 if (!mail_append_full(stream, long_enc_path, set_flags, NULL, &str)) {
2538                     EM_DEBUG_EXCEPTION("mail_append  failed -");
2539                     err = EMAIL_ERROR_IMAP4_APPEND_FAILURE;
2540                     goto FINISH_OFF;
2541                 }
2542         }
2543         else{
2544                 if (!mail_append_full(stream, long_enc_path, NULL, NULL, &str)) {
2545                         EM_DEBUG_EXCEPTION("mail_append  failed -");
2546                         err = EMAIL_ERROR_IMAP4_APPEND_FAILURE;
2547                         goto FINISH_OFF;
2548                 }
2549         }
2550
2551         /* Update read_mail_uid tbl */
2552         if (!emcore_add_read_mail_uid(mailbox_tbl, mail_table_data->mailbox_name, mail_table_data->mail_id, g_append_uid_rsp, mail_table_data->mail_size, 0, &err)) {
2553                 EM_DEBUG_EXCEPTION(" emcore_add_read_mail_uid failed [%d]", err);
2554                 emstorage_rollback_transaction(NULL, NULL, NULL);
2555                 goto FINISH_OFF;
2556         }
2557
2558
2559 FINISH_OFF:
2560
2561 #ifdef __FEATURE_LOCAL_ACTIVITY__
2562         if (ret){
2563                 emstorage_activity_tbl_t new_activity;
2564                 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
2565                 new_activity.activity_type = ACTIVITY_SAVEMAIL;
2566                 new_activity.account_id    = account_id;
2567                 new_activity.mail_id       = mail_id;
2568                 new_activity.dest_mbox     = NULL;
2569                 new_activity.server_mailid = NULL;
2570                 new_activity.src_mbox      = NULL;
2571
2572                 if (!emcore_delete_activity(&new_activity, &err)){
2573                         EM_DEBUG_EXCEPTION(">>>>>>Local Activity [ACTIVITY_SAVEMAIL] [%d] ", err);
2574                 }
2575         }
2576 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
2577
2578         EM_SAFE_FREE(data);
2579         EM_SAFE_FREE(long_enc_path);
2580
2581         if (account_ref) {
2582                 emcore_free_account(account_ref);
2583                 EM_SAFE_FREE(account_ref);
2584         }
2585
2586         if (fp)
2587                 fclose(fp);
2588
2589         if (envelope)
2590                 mail_free_envelope(&envelope);
2591
2592         if (mail_table_data)
2593                 emstorage_free_mail(&mail_table_data, 1, NULL);
2594
2595         if (attachment_tbl_data)
2596                 emstorage_free_attachment(&attachment_tbl_data, attachment_tbl_count, NULL);
2597
2598         if (mailbox_tbl)
2599                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
2600
2601         if (fname) {
2602                 remove(fname);
2603                 EM_SAFE_FREE(fname);
2604         }
2605
2606         if (stream){
2607                 emcore_close_mailbox(0, stream);
2608                 stream = NULL;
2609         }
2610
2611         EM_DEBUG_FUNC_END("err [%d]", err);
2612         return err;
2613 }
2614
2615 #endif
2616
2617 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
2618
2619 static int emcore_initiate_pbd(MAILSTREAM *stream, int account_id, int mail_id, char *uid, int input_maibox_id, int *err_code)
2620 {
2621         EM_DEBUG_FUNC_BEGIN("account_id [%d], mail_id[%d], uid[%p], input_maibox_id[%d]", account_id, mail_id, uid, input_maibox_id);
2622
2623         int ret = false;
2624         int err = EMAIL_ERROR_NONE;
2625         email_account_t *account_ref = NULL;
2626         emstorage_mailbox_tbl_t* mailbox = NULL;
2627
2628         if (account_id < FIRST_ACCOUNT_ID || mail_id < 0 || NULL == uid || 0 == input_maibox_id){
2629                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
2630                 err = EMAIL_ERROR_INVALID_PARAM;
2631                 goto FINISH_OFF;
2632         }
2633
2634         account_ref = emcore_get_account_reference(account_id);
2635
2636         email_event_partial_body_thd pbd_event;
2637
2638         memset(&pbd_event, 0x00, sizeof(email_event_partial_body_thd));
2639
2640         pbd_event.account_id = account_id;
2641         if (account_ref && account_ref->incoming_server_type == EMAIL_SERVER_TYPE_POP3)
2642                 pbd_event.activity_type = ACTIVITY_PARTIAL_BODY_DOWNLOAD_POP3_WAIT;
2643         else
2644                 pbd_event.activity_type = ACTIVITY_PARTIAL_BODY_DOWNLOAD_IMAP4;
2645
2646         if ( (err = emstorage_get_mailbox_by_id(input_maibox_id, &mailbox)) != EMAIL_ERROR_NONE || !mailbox) {
2647                 EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_id failed [%d]", err);
2648                 goto FINISH_OFF;
2649         }
2650
2651         pbd_event.mailbox_id     = input_maibox_id;
2652         pbd_event.mail_id        = mail_id;
2653         pbd_event.server_mail_id = strtoul(uid, NULL, 0);
2654         pbd_event.mailbox_name   = EM_SAFE_STRDUP(mailbox->mailbox_name);
2655
2656         EM_DEBUG_LOG("input_mailbox_name name [%d]", pbd_event.mailbox_id);
2657         EM_DEBUG_LOG("uid [%s]", uid);
2658         EM_DEBUG_LOG("pbd_event.account_id[%d], pbd_event.mail_id[%d], pbd_event.server_mail_id [%d]", pbd_event.account_id, pbd_event.mail_id , pbd_event.server_mail_id);
2659
2660         if (!emcore_insert_pbd_activity(&pbd_event, &pbd_event.activity_id, &err)){
2661                 EM_DEBUG_EXCEPTION("Inserting Partial Body Download activity failed with error[%d]", err);
2662                 goto FINISH_OFF;
2663         }
2664         else{
2665                 if (false == emcore_is_partial_body_thd_que_full()){
2666                         /* h.gahaut :  Before inserting the event into event queue activity_type should be made 0
2667                         Because partial body thread differentiates events coming from DB and event queue
2668                         on the basis of activity_type and event_type fields */
2669
2670                         pbd_event.activity_type = 0;
2671                         pbd_event.event_type = EMAIL_EVENT_BULK_PARTIAL_BODY_DOWNLOAD;
2672
2673                         if (!emcore_insert_partial_body_thread_event(&pbd_event, &err)){
2674                                 EM_DEBUG_EXCEPTION("Inserting Partial body thread event failed with error[%d]", err);
2675                                 goto FINISH_OFF;
2676                         }
2677
2678                         /*h.gahlaut :  Partial body thread has created a copy of event for itself so this local event should be freed here*/
2679                         if (!emcore_free_partial_body_thd_event(&pbd_event, &err))
2680                                 EM_DEBUG_EXCEPTION("Freeing Partial body thread event failed with error[%d]", err);
2681                 }
2682                 else{
2683                         EM_DEBUG_LOG(" Activity inserted only in DB .. Queue is Full");
2684                 }
2685         }
2686
2687         ret = true;
2688
2689 FINISH_OFF:
2690
2691         if (mailbox) {
2692                 emstorage_free_mailbox(&mailbox, 1, &err);
2693         }
2694
2695         if (account_ref) {
2696                 emcore_free_account(account_ref);
2697                 EM_SAFE_FREE(account_ref);
2698         }
2699
2700         if (NULL  != err_code)
2701                 *err_code = err;
2702
2703         EM_DEBUG_FUNC_END("ret [%d]", ret);
2704         return ret;
2705 }
2706
2707 static int emcore_parse_bodystructure(void *stream, IMAPPARSEDREPLY *reply_from_server, char *bodystructure, BODY **body, struct _m_content_info **cnt_info, int *total_mail_size, unsigned long *uid_no)
2708 {
2709         EM_DEBUG_FUNC_BEGIN("stream:[%p], reply_from_server:[%p], bodystructure:[%p]", stream, reply_from_server, bodystructure);
2710
2711         int err = EMAIL_ERROR_NONE;
2712
2713         if (!stream || !reply_from_server || !bodystructure) {
2714                 EM_DEBUG_EXCEPTION("Invalid paramter");
2715                 err = EMAIL_ERROR_INVALID_PARAM;
2716                 return err;
2717         }
2718
2719         int p_total_mail_size = 0;
2720         unsigned long p_uid_no = 0;
2721         char *uid_string = NULL;
2722         char *bodystructure_start = NULL;
2723         char *bodystructure_string = NULL;
2724         char *bodystructure_uid_start = NULL;
2725         BODY *p_body = NULL;
2726         struct _m_content_info *p_cnt_info = NULL;
2727
2728         /* Get the body strcuture string */
2729         bodystructure_start = strstr(bodystructure, "BODYSTRUCTURE (") + strlen("BODYSTRUCTURE");
2730         if (bodystructure_start == NULL) {
2731                 EM_DEBUG_EXCEPTION("Invalid bodystructure");
2732                 err = EMAIL_ERROR_INVALID_PARAM;
2733                 goto FINISH_OFF;
2734         }
2735
2736         bodystructure_string = strdup(bodystructure_start);
2737
2738         /* Get the UID */
2739         bodystructure_uid_start = strcasestr(bodystructure, "UID ") + strlen("UID ");
2740
2741         uid_string = em_malloc((bodystructure_start - bodystructure_uid_start) + 1);
2742         if (uid_string == NULL) {
2743                 EM_DEBUG_EXCEPTION("em_malloc failed");
2744                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2745                 goto FINISH_OFF;
2746         }
2747
2748         memcpy(uid_string, bodystructure_uid_start, bodystructure_start - bodystructure_uid_start);
2749
2750         p_uid_no = strtoul(uid_string, NULL, 0);
2751         EM_DEBUG_LOG("UID number : [%d]", p_uid_no);
2752
2753         /* Parse the bodystructure string */
2754         p_body = mail_newbody();
2755         if (p_body == NULL) {
2756                 EM_DEBUG_EXCEPTION("New body creationg failed");
2757                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2758                 goto FINISH_OFF;
2759         }
2760
2761         imap_parse_body_structure(stream, p_body, (unsigned char **)&bodystructure_string, reply_from_server);
2762
2763         /* Get the total mail size */
2764         if (emcore_set_fetch_body_section(p_body, true, &p_total_mail_size, &err) < 0) {
2765                 EM_DEBUG_EXCEPTION("emcore_set_fetch_body_section failed:[%d]", err);
2766                 goto FINISH_OFF;
2767         }
2768
2769         /* Fill the content_info structure */
2770         if (!(p_cnt_info = em_malloc(sizeof(struct _m_content_info)))) {
2771                 EM_DEBUG_EXCEPTION("em_malloc failed");
2772                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2773                 goto FINISH_OFF;
2774         }
2775
2776         if (emcore_get_body(stream, 0, 0, 0, p_body, p_cnt_info, &err) < 0 || !p_cnt_info) {
2777                 EM_DEBUG_EXCEPTION("emcore_get_body failed[%d]", err);
2778                 err = EMAIL_ERROR_IMAP4_FETCH_UID_FAILURE;
2779                 goto FINISH_OFF;
2780         }
2781
2782 FINISH_OFF:
2783
2784         EM_SAFE_FREE(uid_string);
2785 //      EM_SAFE_FREE(bodystructure_string);
2786
2787         if (err != EMAIL_ERROR_NONE) {
2788                 if (p_cnt_info)
2789                         emcore_free_content_info(p_cnt_info);
2790
2791                 if (body)
2792                         mail_free_body(&p_body);
2793         } else {
2794                 if (cnt_info)
2795                         *cnt_info = p_cnt_info;
2796
2797                 if (body)
2798                         *body = p_body;
2799
2800                 if (total_mail_size)
2801                         *total_mail_size = p_total_mail_size;
2802
2803                 if (uid_no)
2804                         *uid_no = p_uid_no;
2805         }
2806
2807         EM_DEBUG_FUNC_END("Err:[%d]", err);
2808         return err;
2809 }
2810
2811 static int emcore_update_attachment_except_inline(struct _m_content_info *cnt_info, int account_id, int mail_id, int mailbox_id, int *output_total_attachment_size, int *output_attachment_count)
2812 {
2813         EM_DEBUG_FUNC_BEGIN("cnt_info : [%p], account_id : [%d], mail_id : [%d], mailbox_id : [%d]", cnt_info, account_id, mail_id, mailbox_id);
2814         int err = EMAIL_ERROR_NONE;
2815
2816         if (!cnt_info || !account_id || !mail_id || !mailbox_id) {
2817                 EM_DEBUG_EXCEPTION("Invalid parameter");
2818                 err = EMAIL_ERROR_INVALID_PARAM;
2819                 return err;
2820
2821         }
2822
2823         if (!cnt_info->file || !cnt_info->file->name) {
2824                 EM_DEBUG_LOG("Not include attachment");
2825                 return err;
2826         }
2827
2828         int attachment_count = 0;
2829         int total_attach_size = 0;
2830         int inline_attachment_count = 0;
2831         emstorage_attachment_tbl_t attachment_tbl = {0};
2832         struct attachment_info *attach_info = NULL;
2833
2834         attachment_tbl.account_id = account_id;
2835         attachment_tbl.mail_id = mail_id;
2836         attachment_tbl.mailbox_id = mailbox_id;
2837         attachment_tbl.attachment_save_status = 0;
2838
2839         for (attachment_count = 1, attach_info = cnt_info->file; attach_info; attach_info = attach_info->next, attachment_count++) {
2840                 if (attach_info->type == INLINE_ATTACHMENT) {
2841                         EM_DEBUG_LOG("INLINE ATTACHMENT");
2842                         inline_attachment_count++;
2843                         continue;
2844                 }
2845
2846                 total_attach_size                              += attach_info->size;
2847                 attachment_tbl.attachment_size                  = attach_info->size;
2848                 attachment_tbl.attachment_path                  = attach_info->save;
2849                 attachment_tbl.attachment_name                  = attach_info->name;
2850                 attachment_tbl.attachment_drm_type              = attach_info->drm;
2851                 attachment_tbl.attachment_mime_type             = attach_info->attachment_mime_type;
2852 #ifdef __ATTACHMENT_OPTI__
2853                 attachment_tbl.encoding                         = attach_info->encoding;
2854                 attachment_tbl.section                          = attach_info->section;
2855 #endif
2856
2857                 EM_DEBUG_LOG("attachment_count [%d]", attachment_count);
2858
2859                 if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err)) {
2860                         EM_DEBUG_EXCEPTION("emstorage_add_attachment failed : [%d]", err);
2861                         goto FINISH_OFF;
2862                 }
2863         }
2864
2865 FINISH_OFF:
2866
2867         if (output_attachment_count)
2868                 *output_attachment_count = attachment_count - inline_attachment_count;
2869
2870         if (output_total_attachment_size)
2871                 *output_total_attachment_size = total_attach_size;
2872
2873         EM_DEBUG_FUNC_END("err : [%d]", err);
2874         return err;
2875 }
2876 #define UID_RANGE_STRING_LENGTH 3000
2877 #define TEMP_STRING_LENGTH              50
2878
2879 static int emcore_parse_html_part_for_partial_body(char *start_header, char *boundary_string, char *bufsendforparse, char *text_html, int body_size)
2880 {
2881         EM_DEBUG_FUNC_BEGIN("start_header [%s], boundary_string [%s], bufsendforparse [%s], text_html [%s], body_size [%d]", start_header, boundary_string, bufsendforparse, text_html, body_size);
2882
2883         int   err = EMAIL_ERROR_NONE;
2884         int   html_uidno = 0;
2885         int   iEncodingHeader = 0;
2886         int   enc_type = ENCOTHER, dec_len = 0, html_length = 0;
2887         char  EncodingHeader[40] = {0};
2888         char  Encoding[30] = {0};
2889         char *pEncodingHeaderEnd = NULL;
2890         char *txt_html = NULL;
2891         char *pHeaderStart = NULL;
2892         char *start = NULL, *end = NULL;
2893         char *temp_enc1 = NULL;
2894
2895         EM_DEBUG_LOG("Content-Type : text/html or message/rfc822 or text/rfc822-headers");
2896
2897         pHeaderStart = start_header;
2898         pHeaderStart = pHeaderStart-2;
2899         do{
2900                 pHeaderStart--;
2901         } while (*pHeaderStart != LF && bufsendforparse < pHeaderStart);
2902
2903         pHeaderStart++;
2904
2905         memcpy(EncodingHeader, pHeaderStart, 25);
2906
2907         if (strcasecmp(EncodingHeader, "Content-Transfer-Encoding") == 0){
2908                 pEncodingHeaderEnd = strstr(pHeaderStart, CRLF_STRING);
2909                 memcpy(Encoding, pHeaderStart + 27, pEncodingHeaderEnd - (pHeaderStart+27));
2910                 iEncodingHeader = 1;
2911         }
2912
2913         /* HTML Content found */
2914
2915         if(!start_header) { /*prevent 27448*/
2916                 EM_DEBUG_EXCEPTION("start_header NULL");
2917                 return false;
2918         }
2919         txt_html = start_header;
2920         txt_html = strstr(txt_html, CRLF_STRING CRLF_STRING);
2921
2922         if (txt_html != NULL){
2923                 txt_html += 4; /*  txt_html points at html content */
2924                 start = txt_html;
2925                 char multipart_boundary[100] = {0};
2926                 char *multipart_related_boundry = NULL;
2927                 char *multipart_related_boundry_end = NULL;
2928                 if (iEncodingHeader == 1)
2929                         multipart_related_boundry = pHeaderStart;
2930                 else
2931                         multipart_related_boundry = start_header;
2932
2933                 multipart_related_boundry = multipart_related_boundry - 3;
2934                 multipart_related_boundry_end = multipart_related_boundry;
2935
2936                 while (bufsendforparse < multipart_related_boundry && *multipart_related_boundry != LF && *multipart_related_boundry != NULL_CHAR)
2937                         multipart_related_boundry -= 1;
2938
2939                 multipart_related_boundry  += 1;
2940                 memcpy(multipart_boundary, multipart_related_boundry, multipart_related_boundry_end - multipart_related_boundry);
2941
2942                 EM_DEBUG_LOG("multipart_boundary [%s], boundary_string [%s]", multipart_boundary, boundary_string);
2943
2944                 if (strcmp(multipart_boundary, boundary_string) == 0)
2945                         end = strstr(txt_html, boundary_string);
2946                 else
2947                         end = strstr(txt_html, multipart_boundary);
2948
2949                 memset(multipart_boundary, 0, EM_SAFE_STRLEN(multipart_boundary));
2950
2951                 EM_DEBUG_LOG("end [%p], txt_html [%p]", end, txt_html);
2952
2953                 if (end == NULL) {
2954                         EM_DEBUG_LOG("HTML body contents exceeds limited Bytes");
2955
2956                         end = txt_html + body_size - (txt_html - bufsendforparse);
2957                         html_uidno = 1;
2958                 }
2959                 else if(end == txt_html) { /* empty multipart */
2960                         EM_DEBUG_LOG("Emtpy HTML multipart");
2961                         return false;
2962                 }
2963                 else {
2964                         if ((*(end-2) == CR) && (*(end-1) == LF))
2965                                 end -= 2;
2966                         else if ((*(end-2) == CR) && (*(end-1) == LF)
2967                                     && (*(end-4) == CR) && (*(end-3) == LF))
2968                                 end -= 4;
2969                         else
2970                                 EM_DEBUG_EXCEPTION(" Content not per as grammar.");
2971                 }
2972
2973                 EM_DEBUG_LOG("end [%p], txt_html [%p]", end, txt_html);
2974
2975                 EM_DEBUG_LOG("iEncodingHeader [%d]", iEncodingHeader);
2976
2977                 if (iEncodingHeader == 1){
2978                         enc_type = ENCOTHER;
2979                         if (strncasecmp(Encoding, "base64", strlen("base64")) == 0)
2980                                 enc_type = ENCBASE64;
2981                         else if (strncasecmp(Encoding, "quoted-printable", strlen("quoted-printable")) == 0)
2982                                 enc_type = ENCQUOTEDPRINTABLE;
2983
2984                         EM_DEBUG_LOG("enc_type [%d]", enc_type);
2985
2986                         memcpy(text_html, start, end - txt_html);
2987
2988                         if (emcore_decode_body_text(text_html, end - txt_html, enc_type , &dec_len, &err) < 0)
2989                                 EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
2990                 }
2991         else if ((temp_enc1 = (char *)strcasestr(start_header, "Content-transfer-encoding:")) && (temp_enc1 < end)){ /*prevent 27448*/
2992                         if (temp_enc1)
2993                                 start_header = temp_enc1;
2994
2995                         start_header += strlen("Content-Transfer-Encoding:");
2996                         start_header = em_skip_whitespace_without_strdup(start_header);
2997
2998                         if (!start_header)
2999                                 EM_DEBUG_EXCEPTION(" Invalid parsing ");
3000                         else{
3001                                 enc_type = ENCOTHER;
3002                                 if (strncasecmp(start_header, "base64", strlen("base64")) == 0)
3003                                         enc_type = ENCBASE64;
3004                                 else if (strncasecmp(start_header, "quoted-printable", strlen("quoted-printable")) == 0)
3005                                         enc_type = ENCQUOTEDPRINTABLE;
3006
3007                                 EM_DEBUG_LOG("enc_type [%d]", enc_type);
3008
3009                                 memcpy(text_html, start, end - txt_html);
3010
3011                                 if (emcore_decode_body_text(text_html, end - txt_html, enc_type , &dec_len, &err) < 0)
3012                                         EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
3013                                 html_length = dec_len;
3014                         }
3015
3016                         EM_DEBUG_LOG("Decoded length = %d", dec_len);
3017                         EM_DEBUG_LOG("start - %s", start);
3018                 }
3019                 else{
3020                         memcpy(text_html, start, end-txt_html);
3021                         html_length = (end-txt_html);
3022                 }
3023
3024                 /* EM_DEBUG_LOG(" Content-Type:  text/html [%s]\n", text_html);                                                          */
3025         }
3026         else
3027                 EM_DEBUG_EXCEPTION(" Invalid html body content ");
3028
3029         EM_DEBUG_FUNC_END();
3030         return true;
3031 }
3032
3033
3034
3035 /*For the following scenario
3036 *------= SAMSUNG_mySingle_MIME_MULTIPART_BOUNDARY
3037 *Content-Transfer-Encoding :  base64
3038 *Content-Type :  text/plain; charset = "windows-1252"
3039 *MIME-Version :  1.0
3040 *Message-ID:  <11512468.945901271910226702.JavaMail.weblogic@epml03>
3041 */
3042
3043 #define CONTENT_TRANSFER_ENCODING "Content-Transfer-Encoding"
3044
3045 static int emcore_parse_plain_part_for_partial_body(char *header_start_string, char *start_header, char *boundary_string, char *bufsendforparse, char *text_plain, int body_size)
3046 {
3047         EM_DEBUG_FUNC_BEGIN("header_start_string[%s], start_header[%s], boundary_string [%s], bufsendforparse [%s], text_plain [%s]", header_start_string, start_header, boundary_string, bufsendforparse, text_plain);
3048         int   err = EMAIL_ERROR_NONE, iEncodingHeader = 0, enc_type = ENCOTHER;
3049         int  dec_len = 0, strcmpret = -1;
3050         char *pHeaderStart = NULL, *pEncodingHeaderEnd = NULL;
3051         char  EncodingHeader[40] = {0, };
3052         char  Encoding[30] = {0, };
3053         char *temp_text_boundary = NULL;
3054         char *start = NULL, *end = NULL, *txt_plain = NULL, *temp_enc1 = NULL;
3055
3056         EM_DEBUG_LOG("Content-Type : text/plain");
3057
3058         pHeaderStart = header_start_string;
3059         temp_text_boundary = start_header;
3060
3061         memcpy(EncodingHeader, pHeaderStart, 25);
3062
3063         if (strcasecmp(EncodingHeader, "Content-Transfer-Encoding") == 0){
3064                 pEncodingHeaderEnd = strstr(pHeaderStart, CRLF_STRING);
3065                 memcpy(Encoding, pHeaderStart + 27, pEncodingHeaderEnd - (pHeaderStart + 27));
3066                 iEncodingHeader = 1;
3067         }
3068
3069         /*  Plain text content found  */
3070         if(!start_header) { /*prevent 27450*/
3071                 EM_DEBUG_EXCEPTION("start_header NULL");
3072                 return false;
3073         }
3074         txt_plain = start_header;
3075         txt_plain = strstr(txt_plain, CRLF_STRING CRLF_STRING);
3076
3077         if (txt_plain != NULL){
3078                 txt_plain += EM_SAFE_STRLEN(CRLF_STRING CRLF_STRING); /*  txt_plain points at plain text content  */
3079
3080                 /* Fix is done for mail having "Content-Type: text/plain" but there is no content but having only attachment. */
3081
3082                 strcmpret = strncmp(txt_plain, boundary_string, EM_SAFE_STRLEN(boundary_string));
3083                 if (strcmpret == 0){
3084                 }
3085                 else{
3086                         start = txt_plain;
3087                         end = strstr(txt_plain, boundary_string);
3088
3089                         if (end == NULL){
3090                                 EM_DEBUG_LOG("Text body contents exceeds limited Bytes");
3091                                 end = txt_plain + body_size - (txt_plain - bufsendforparse);
3092                         }
3093                         else{
3094                                 /* EM_DEBUG_LOG("pbd_event[temp_count].partial_body_complete - %d", partial_body_complete); */
3095
3096                                 if ((*(end-2) == CR) && (*(end-1) == LF))
3097                                         end -= 2;
3098                                 else if ((*(end-2) == CR) && (*(end-1) == LF)
3099                                             && (*(end-4) == CR) && (*(end-3) == LF))
3100                                         end -= 4;
3101                                 else
3102                                         EM_DEBUG_EXCEPTION(" Content not per as grammar.");
3103                         }
3104
3105                         if (iEncodingHeader == 1){
3106                                 enc_type = ENCOTHER;
3107                                 if (strncasecmp(Encoding, "base64", strlen("base64")) == 0)
3108                                         enc_type = ENCBASE64;
3109                                 else if (strncasecmp(Encoding, "quoted-printable", strlen("quoted-printable")) == 0)
3110                                         enc_type = ENCQUOTEDPRINTABLE;
3111
3112                                 EM_DEBUG_LOG("enc_type [%d]", enc_type);
3113
3114                                 memcpy(text_plain, start, end - txt_plain);
3115
3116                                 if (emcore_decode_body_text(text_plain, end - txt_plain, enc_type , &dec_len, &err) < 0)
3117                                         EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
3118                         }
3119                         else if (start_header && ((temp_enc1 = (char *)strcasestr(start_header, "Content-transfer-encoding:"))  != NULL) && !(temp_enc1 && temp_enc1 >= end)){
3120                                 if (temp_enc1)
3121                                         start_header = temp_enc1;
3122
3123                                 start_header += strlen("Content-Transfer-Encoding:");
3124                                 start_header = em_skip_whitespace_without_strdup(start_header);
3125
3126                                 if (!start_header)
3127                                         EM_DEBUG_EXCEPTION(" Invalid parsing ");
3128                                 else{
3129                                         enc_type = ENCOTHER;
3130                                         if (strncasecmp(start_header, "base64", strlen("base64")) == 0)
3131                                                 enc_type = ENCBASE64;
3132                                         else if (strncasecmp(start_header, "quoted-printable", strlen("quoted-printable")) == 0)
3133                                                 enc_type = ENCQUOTEDPRINTABLE;
3134
3135                                         EM_DEBUG_LOG("enc_type [%d]", enc_type);
3136                                         memcpy(text_plain, start, end - txt_plain);
3137                                         if (emcore_decode_body_text(text_plain, end - txt_plain, enc_type , &dec_len, &err) < 0)
3138                                                 EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
3139                                 }
3140
3141                                 EM_DEBUG_LOG("Decoded length = %d", dec_len);
3142                                 /*  EM_DEBUG_LOG("start - %s", start); */ /* print raw MIME content. */
3143                         }
3144                         else
3145                                 memcpy(text_plain, start, end-txt_plain);
3146
3147                         /* EM_DEBUG_LOG(" Content-type: text/plain [%s]\n", text_plain); */
3148                 }
3149         }
3150         else
3151                 EM_DEBUG_EXCEPTION(" Invalid text body content ");
3152
3153         EM_DEBUG_FUNC_END();
3154         return 1;
3155 }
3156
3157
3158
3159 /*  Content-Type:  IMAGE/octet-stream; name = Default.png */
3160 /*  Content-Transfer-Encoding:  BASE64 */
3161 /*  Content-ID:  <4b0d6810b17291f9438783a8eb9d5228@com.samsung.slp.email> */
3162 /*  Content-Disposition:  inline; filename = Default.png */
3163
3164 static void emcore_free_email_image_data(email_image_data **image_data, int count)
3165 {
3166         EM_DEBUG_FUNC_BEGIN("image_data : [%p], count : [%d]", image_data, count);
3167
3168         if ((image_data == NULL) || (*image_data == NULL) || (count <= 0)) {
3169                 EM_DEBUG_EXCEPTION("Invalid paramter");
3170                 return;
3171         }
3172
3173         email_image_data *p = *image_data;
3174         int i = 0;
3175
3176         for (i = 0; i < count; i++) {
3177                 EM_SAFE_FREE(p[i].text_image);
3178                 EM_SAFE_FREE(p[i].content_id);
3179                 EM_SAFE_FREE(p[i].mime_type);
3180         }
3181
3182         EM_SAFE_FREE(p);
3183         *image_data = NULL;
3184
3185         EM_DEBUG_FUNC_END();
3186 }
3187
3188 static int emcore_parse_image_part_for_partial_body(char *header_start_string, char *start_header, char *boundary_string, char *bufsendforparse, email_image_data *image_data, int body_size)
3189 {
3190         EM_DEBUG_FUNC_BEGIN("boundary_string : [%s]", boundary_string);
3191
3192         int   err = EMAIL_ERROR_NONE;
3193         char *multiple_image = NULL;
3194         int   donot_parse_next_image = 0;
3195         char *image_boundary = NULL;
3196         char *image_boundary_end = NULL;
3197         char *temp_image_boundary = NULL;
3198         int   i = 0, ch_image = 0, cidno = 0;
3199         int   enc_type = ENCOTHER, dec_len = 0, image_length = 0;
3200         char *p = header_start_string;
3201         char *start = NULL, *end = NULL, *txt_image = NULL;
3202         char *temp_image = NULL;
3203         char *temp_cid1 = NULL;
3204         char *cid_end = NULL;
3205         char *temp_enc1 = NULL;
3206         char *p_boundary_string = NULL;
3207
3208         if(image_data == NULL) {
3209                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3210                 err = EMAIL_ERROR_INVALID_PARAM;
3211                 return false;
3212         }
3213
3214         image_boundary     = start_header;
3215         image_boundary_end = image_boundary - 2;
3216         image_boundary     = image_boundary - 2;
3217
3218         EM_DEBUG_LOG("Content-type: image");
3219
3220         while (*image_boundary != LF)
3221                 image_boundary--;
3222
3223         image_boundary++;
3224
3225         temp_image_boundary = em_malloc((image_boundary_end - image_boundary) + 1);
3226         if (temp_image_boundary == NULL) {
3227                 EM_DEBUG_EXCEPTION("em_malloc failed");
3228                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3229                 return false;
3230         }
3231
3232         if (image_boundary  != NULL && image_boundary_end != NULL)
3233                 memcpy(temp_image_boundary, image_boundary, image_boundary_end-image_boundary);
3234
3235         if ((char *)strcasestr((const char *)temp_image_boundary, "Content-type:") == NULL)
3236                 p_boundary_string = strdup(temp_image_boundary);
3237         else
3238                 p_boundary_string = EM_SAFE_STRDUP(boundary_string);
3239
3240         EM_SAFE_FREE(temp_image_boundary);
3241
3242         do {
3243                 if (multiple_image != NULL){
3244                         p = multiple_image;
3245                         start_header = multiple_image;
3246                 }
3247
3248                 emcore_get_content_type_from_mime_string(start_header, &(image_data[i].mime_type));
3249
3250                 if ((strcasestr(p, "Content-Disposition: attachment")) || (!strcasestr(p, "Content-ID: <"))){
3251                         EM_DEBUG_EXCEPTION(" Body has attachment no need to parse ");
3252                         end = NULL;
3253                         multiple_image = NULL;
3254                 }
3255                 else {  /*  HTML Content found */
3256                         ch_image = 0;
3257                         cidno = 0;
3258                         int   boundarylen = -1;
3259                         char *cid = NULL;
3260                         char *temp_name = NULL;
3261                         char *decoded_filename = NULL;
3262
3263                         memset(image_data[i].image_file_name, 0, 100);
3264
3265                         if(!start_header) { /*prevent 27449*/
3266                                 EM_DEBUG_EXCEPTION("start_header NULL");
3267                                 EM_SAFE_FREE(p_boundary_string);
3268                                 return false;
3269                         }
3270
3271                         txt_image  = start_header;
3272                         temp_image = start_header;
3273
3274                         temp_name = strstr(txt_image, "name=");
3275                         if (temp_name  != NULL){
3276                                 temp_image = temp_name;
3277                                 if (*(temp_image+5) == '"')
3278                                         temp_image = temp_image+5;
3279
3280                                 while (*temp_image  != CR){
3281                                         temp_image++;
3282                                         memcpy(image_data[i].image_file_name+ch_image, temp_image, 1);
3283                                         ch_image++;
3284                                 }
3285
3286                                 if ((*(temp_name+4) == '=') && (*(temp_name+5) == '\"'))
3287                                         image_data[i].image_file_name[ch_image-2] = '\0';
3288
3289                                 if ((*(temp_name+4) == '=') && (*(temp_name+5) != '\"'))
3290                                         image_data[i].image_file_name[ch_image-1] = '\0';
3291
3292                                 decoded_filename = emcore_decode_rfc2047_text(image_data[i].image_file_name, &err);
3293                                 if(decoded_filename) {
3294                                         memset(image_data[i].image_file_name, 0, 100);
3295                                         memcpy(image_data[i].image_file_name, decoded_filename, EM_SAFE_STRLEN(decoded_filename));
3296                                         EM_SAFE_FREE(decoded_filename);
3297                                 }
3298                         }
3299
3300                         if (((temp_cid1 = (char *)strcasestr((const char *)start_header, "Content-ID: <")) != NULL)){
3301                                 if (temp_cid1){
3302                                         cid = temp_cid1;
3303                                         temp_image = temp_cid1;
3304                                 }
3305
3306                                 cid += 13;
3307                                 cid_end = strstr(temp_image, "\076");           /*  076 == '>' */
3308
3309                                 image_data[i].content_id = (char *)em_malloc(cid_end-cid+5);
3310                                 if (image_data[i].content_id != NULL){
3311                                         strcpy(image_data[i].content_id, "cid:");
3312                                         memcpy(image_data[i].content_id+4, cid, cid_end-cid);
3313                                 }
3314                                 else
3315                                         EM_DEBUG_EXCEPTION("em_malloc() failed");
3316
3317                                 if (image_data[i].image_file_name[0] == '\0')
3318                                         memcpy(image_data[i].image_file_name, cid, cid_end - cid);
3319                         }
3320
3321                         txt_image = strstr(txt_image, CRLF_STRING CRLF_STRING);
3322
3323                         if (txt_image != NULL){
3324                                 txt_image += 4; /*  txt_image points at image content */
3325                                 start = txt_image;
3326                                 end = strstr(txt_image, p_boundary_string);
3327
3328
3329                                 if (end == NULL){
3330                                         EM_DEBUG_LOG("HTML body contents exceeds limited Bytes");
3331                                         /*  end points to end of partial body data */
3332                                         end = txt_image + body_size - (txt_image-bufsendforparse);
3333                                 }
3334                                 else{
3335                                         boundarylen = EM_SAFE_STRLEN(p_boundary_string);
3336                                         end -= 2;
3337                                 }
3338
3339                                 if ((temp_enc1 = (char *)strcasestr((const char *)start_header, "Content-transfer-encoding:"))  != NULL){ /*prevent 27449*/
3340                                         if (temp_enc1)
3341                                                 start_header = temp_enc1;
3342
3343                                         start_header  += strlen("Content-Transfer-Encoding:");
3344                                         start_header = em_skip_whitespace_without_strdup(start_header);
3345
3346                                         if (!start_header)
3347                                                 EM_DEBUG_EXCEPTION(" Invalid parsing ");
3348                                         else{
3349                                                 enc_type = ENCOTHER;
3350                                                 if (strncasecmp(start_header, "base64", strlen("base64")) == 0)
3351                                                         enc_type = ENCBASE64;
3352                                                 else if (strncasecmp(start_header, "quoted-printable", strlen("quoted-printable")) == 0)
3353                                                         enc_type = ENCQUOTEDPRINTABLE;
3354
3355                                                 EM_DEBUG_LOG("enc_type [%d]", enc_type);
3356
3357                                                 image_data[i].text_image = (char *)em_malloc((end-txt_image)+1);
3358                                                 if (image_data[i].text_image){
3359                                                         memcpy(image_data[i].text_image, start, end-txt_image);
3360                                                         if (emcore_decode_body_text(image_data[i].text_image, end-txt_image, enc_type , &(image_data[i].dec_len), &err) < 0)
3361                                                                 EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
3362                                                         else
3363                                                                 image_length = image_data[i].dec_len;
3364                                                 }
3365                                                 else
3366                                                         EM_DEBUG_EXCEPTION("em_malloc() failed");
3367                                         }
3368
3369                                         EM_DEBUG_LOG("Decoded length [%d]", dec_len);
3370                                 }
3371                                 else{
3372                                         image_data[i].text_image = (char *)em_malloc(end-txt_image);
3373                                         if (image_data[i].text_image)
3374                                                 memcpy(image_data[i].text_image, start, end - txt_image);
3375                                         else
3376                                                 EM_DEBUG_EXCEPTION("em_malloc() failed");
3377                                 }
3378                         }
3379                         else{
3380                                 donot_parse_next_image = 1;
3381                                 EM_DEBUG_EXCEPTION(" Invalid html body content ");
3382                         }
3383                 }
3384
3385                 if (end != NULL) {
3386                         multiple_image = (char *)strcasestr((const char *)end, "Content-type: image");
3387                         i++;
3388                 }
3389         } while (multiple_image != NULL && donot_parse_next_image != 1 && (i < IMAGE_DISPLAY_PARTIAL_BODY_COUNT));
3390
3391         EM_SAFE_FREE(p_boundary_string);
3392         
3393         EM_DEBUG_FUNC_END();
3394         return 1;
3395 }
3396
3397 static int emcore_find_boundary_string_of_the_part(const char *whole_string, const char *first_line_of_part, char **result_boundary_string, int *error)
3398 {
3399         EM_DEBUG_FUNC_BEGIN("whole_string[%p], first_line_of_part[%p], result_boundary_string[%p]", whole_string, first_line_of_part, result_boundary_string);
3400         int ret = false, err = EMAIL_ERROR_NONE;
3401         char *boundary_cur = NULL, *boundary_end = NULL, *boundary_string = NULL;
3402
3403         if(!whole_string || !first_line_of_part || !result_boundary_string) {
3404                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3405                 err = EMAIL_ERROR_INVALID_PARAM;
3406                 goto FINISH_OFF;
3407         }
3408
3409         if(first_line_of_part - 2 > whole_string) {
3410                 boundary_cur = (char*)first_line_of_part - 2; /* 2 means CRLF. */
3411                 boundary_end = boundary_cur;
3412
3413                 do{
3414                         boundary_cur--;
3415                 } while (whole_string < boundary_cur && *boundary_cur != LF && *boundary_cur != NULL_CHAR);
3416
3417                 boundary_cur++;
3418
3419                 if(boundary_end > boundary_cur && boundary_cur > whole_string) {
3420                         EM_DEBUG_LOG("boundary_end - boundary_cur + 15 [%d]", boundary_end - boundary_cur + 15);
3421                         boundary_string = em_malloc(boundary_end - boundary_cur + 15);
3422                         if(!boundary_string) {
3423                                 EM_DEBUG_EXCEPTION("em_malloc failed");
3424                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3425                                 goto FINISH_OFF;
3426                         }
3427                         memcpy(boundary_string, boundary_cur, boundary_end - boundary_cur);
3428                         EM_DEBUG_LOG("boundary_string [%s]", boundary_string);
3429                         *result_boundary_string = boundary_string;
3430                 }
3431                 else {
3432                         EM_DEBUG_EXCEPTION("There is no string before the part");
3433                         err = EMAIL_ERROR_ON_PARSING;
3434                         goto FINISH_OFF;
3435                 }
3436         }
3437         else {
3438                 EM_DEBUG_EXCEPTION("There is no string before the part");
3439                 err = EMAIL_ERROR_ON_PARSING;
3440                 goto FINISH_OFF;
3441         }
3442         ret = true;
3443 FINISH_OFF:
3444
3445         if(error)
3446                 *error = err;
3447
3448         EM_DEBUG_FUNC_END("ret[%d], err[%d]", ret, err);
3449         return ret;
3450 }
3451
3452 static int emcore_parse_body_for_imap(char *body_str, int body_size, struct _m_content_info *cnt_info, int encoding_type, char **output_text_plain, char **output_text_html, email_image_data **output_image_data)
3453 {
3454         EM_DEBUG_FUNC_BEGIN("body_str:[%p], cnt_info:[%p], encoding_type:[%d]", body_str, cnt_info, encoding_type);
3455         int err = EMAIL_ERROR_NONE;
3456
3457         if (!body_str || strlen(body_str) == 0 || !cnt_info) {
3458                 EM_DEBUG_EXCEPTION("Invalid paramter");
3459                 err = EMAIL_ERROR_INVALID_PARAM;
3460                 return err;
3461         }
3462
3463         if (!output_text_plain || !output_text_html || !output_image_data) {
3464                 EM_DEBUG_EXCEPTION("Invalid parameter");
3465                 err = EMAIL_ERROR_INVALID_PARAM;
3466                 return err;
3467         }
3468
3469         int dec_len = 0;
3470         int no_alternative_part_flag = 0;
3471         int no_html = 0;
3472         char *boundary_start = NULL;
3473         char *boundary_end = NULL;
3474         char *boundary_string = NULL;
3475         char *p_body_str = NULL;
3476         char *start_header = NULL;
3477         char *text_plain = NULL;
3478         char *text_html = NULL;
3479         email_image_data *image_data = NULL;
3480
3481         char *temp_alternative_plain_header = NULL;
3482         char *temp_content_type = NULL;
3483
3484         p_body_str = EM_SAFE_STRDUP(body_str);
3485
3486         text_plain = em_malloc(body_size + 1);
3487         text_html  = em_malloc(body_size + 1);
3488
3489         if (!text_plain || !text_html) {
3490                 EM_DEBUG_EXCEPTION("em_malloc failed");
3491                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3492                 goto FINISH_OFF;
3493         }
3494
3495         if (!strcasestr(p_body_str, "Content-Type: ") && (cnt_info->text.plain || cnt_info->text.html)) {
3496                 /*  Encoded Content-Type: text/html or Content-Type: text/plain  */
3497                 /*  No Partial body has No headers with Content-Type: text/html or Content-Type: text/plain  */
3498
3499                 EM_DEBUG_LOG("mbody->encoding [%d] ", encoding_type);
3500
3501                 if (emcore_decode_body_text(p_body_str, EM_SAFE_STRLEN(p_body_str), encoding_type, &dec_len, &err) < 0) {
3502                         EM_DEBUG_EXCEPTION("emcore_decode_body_text failed [%d]", err);
3503                         goto FINISH_OFF;
3504                 }
3505
3506                 EM_DEBUG_LOG("Decoded length [%d]", dec_len);
3507
3508                 if (dec_len > 0) {
3509                         if (cnt_info->text.plain) {
3510                                 memcpy(text_plain, p_body_str, dec_len);
3511                         }
3512
3513                         if (cnt_info->text.html) {
3514                                 memcpy(text_html, p_body_str, dec_len);
3515                         }
3516                 }
3517         } else {   /*  Partial body has headers with Content-Type: text/html or Content-Type: text/plain */
3518                 no_alternative_part_flag = 0;
3519                 if (((temp_alternative_plain_header = (char *)strcasestr(p_body_str, "Content-type: multipart/alternative")) != NULL)) {        /*  Found 'alternative' */
3520                         if (((temp_content_type = (char *)strcasestr(p_body_str, "Content-type: text/plain")) != NULL)) {
3521                                 if (temp_content_type < temp_alternative_plain_header) {
3522                                         /*  This part is text/plain not alternative. */
3523                                         no_html = 1;
3524                                         no_alternative_part_flag = 1;
3525                                 } else {
3526                                         EM_DEBUG_LOG(" Content-type: multipart/alternative ");
3527                                         boundary_start = strstr(temp_alternative_plain_header, "--");
3528                                         if(!boundary_start) goto FINISH_OFF; /*prevent 37946 */
3529                                         boundary_end = strcasestr(boundary_start, "Content-type:");
3530                                         if(!boundary_end) goto FINISH_OFF; /*prevent 37946 */
3531
3532                                         boundary_string = em_malloc(boundary_end - (boundary_start + strlen("--")));
3533                                         if (boundary_string == NULL) {
3534                                                 EM_DEBUG_EXCEPTION("em_malloc failed");
3535                                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3536                                                 goto FINISH_OFF;
3537                                         }
3538
3539                                         memcpy(boundary_string, boundary_start, boundary_end - (boundary_start + strlen("--")));
3540                                 }
3541                         }
3542                 } else
3543                         no_alternative_part_flag = 1;
3544
3545                 if (no_alternative_part_flag) {
3546                         boundary_start = strstr(p_body_str, "--");
3547                         if(!boundary_start) goto FINISH_OFF; /*prevent 37946 */
3548                         boundary_end = strcasestr(boundary_start, "Content-type:");
3549                         if(!boundary_end) goto FINISH_OFF; /*prevent 37946 */
3550
3551                         boundary_string = em_malloc(boundary_end - (boundary_start + strlen("--")));
3552                         if (boundary_string == NULL) {
3553                                 EM_DEBUG_EXCEPTION("em_malloc failed");
3554                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3555                                 goto FINISH_OFF;
3556                         }
3557
3558                         memcpy(boundary_string, boundary_start, boundary_end - (boundary_start + strlen("--")));
3559                 }
3560
3561                 if (boundary_string && boundary_end) { /*prevent 37946 */
3562                         EM_DEBUG_LOG("boundary_string : [%s]", boundary_string);
3563
3564                         if (((start_header = (char *)strcasestr(boundary_end, "Content-Type: text/html"))  != NULL) && (no_html  != 1) &&(((char *)strcasestr(boundary_end, "Content-Type: message/rfc822")) == NULL) &&
3565                                 (((char *)strcasestr(boundary_end, "Content-Type: text/rfc822-headers")) == NULL))
3566                                 emcore_parse_html_part_for_partial_body(start_header, boundary_string, p_body_str, text_html, body_size);
3567
3568                         if (((start_header = (char *)strcasestr(boundary_end, "Content-Type: text/plain"))  != NULL)) {
3569                                 char *internal_boundary_string = NULL;
3570
3571                                 if(!emcore_find_boundary_string_of_the_part(p_body_str, start_header, &internal_boundary_string, &err)) {
3572                                         EM_DEBUG_EXCEPTION("internal_boundary_string failed [%d]", err);
3573                                 }
3574
3575                                 if(!internal_boundary_string)
3576                                         internal_boundary_string = EM_SAFE_STRDUP(boundary_string);
3577
3578                                 emcore_parse_plain_part_for_partial_body(boundary_end, start_header, internal_boundary_string, p_body_str, text_plain, body_size);
3579                                 EM_SAFE_FREE(internal_boundary_string);
3580                         }
3581
3582                         if (((start_header = (char *)strcasestr((const char *)boundary_end, "Content-type: image/jpeg")) != (char *)NULL) ||
3583                                 ((start_header = (char *)strcasestr((const char *)boundary_end, "Content-Type: image/jpg"))  != (char *)NULL) ||
3584                                 ((start_header = (char *)strcasestr((const char *)boundary_end, "Content-Type: image/gif"))  != (char *)NULL) ||
3585                                 ((start_header = (char *)strcasestr((const char *)boundary_end, "Content-Type: image/bmp"))  != (char *)NULL) ||
3586                                 ((start_header = (char *)strcasestr((const char *)boundary_end, "Content-Type: image/png"))  != (char *)NULL)) {
3587                                 image_data = em_malloc(sizeof(email_image_data) * IMAGE_DISPLAY_PARTIAL_BODY_COUNT);
3588                                 if (image_data == NULL) {
3589                                         EM_DEBUG_EXCEPTION("em_malloc failed");
3590                                         err = EMAIL_ERROR_OUT_OF_MEMORY;
3591                                         goto FINISH_OFF;
3592                                 }
3593
3594                                 emcore_parse_image_part_for_partial_body(boundary_end, start_header, boundary_string, p_body_str, image_data, body_size);
3595                         }
3596                 }
3597         }
3598
3599 FINISH_OFF:
3600
3601         EM_SAFE_FREE(p_body_str);
3602         EM_SAFE_FREE(boundary_string);
3603
3604         if (err != EMAIL_ERROR_NONE) {
3605                 EM_SAFE_FREE(text_plain);
3606                 EM_SAFE_FREE(text_html);
3607
3608                 if (image_data)
3609                         emcore_free_email_image_data(&image_data, IMAGE_DISPLAY_PARTIAL_BODY_COUNT);
3610         } else {
3611                 *output_text_plain = text_plain;
3612                 *output_text_html = text_html;
3613                 *output_image_data = image_data;
3614         }
3615
3616         EM_DEBUG_FUNC_END("err : [%d]", err);
3617         return err;
3618 }
3619
3620 #define TAG_LENGTH 16
3621 #define COMMAND_LENGTH 2000
3622 static int emcore_download_bulk_partial_mail_body_for_imap(MAILSTREAM *stream, int input_download_size, email_event_partial_body_thd *pbd_event, int item_count, int *error)
3623 {
3624         EM_DEBUG_FUNC_BEGIN("stream [%p], input_download_size[%d], pbd_event [%p], item_count [%d], error [%p]", stream, input_download_size, pbd_event, item_count, error);
3625
3626         int ret = false, err = EMAIL_ERROR_NONE;
3627         int j = 0, i = 0;
3628         int i32_index = 0;
3629         int total_mail_size = 0, total_attachment_size = 0;
3630         int temp_count = 0, attachment_num = 0;
3631         unsigned long uidno = 0;
3632         char buf[512] = {0, };
3633         char *text_plain    = NULL;
3634         char *text_html     = NULL;
3635         char uid_range_string_to_be_downloaded[UID_RANGE_STRING_LENGTH] = {0, };
3636         char imap_tag[TAG_LENGTH] = {0, };
3637         char command[COMMAND_LENGTH] = {0, };
3638         char *temp_data_html = NULL;
3639         char uid_string[TEMP_STRING_LENGTH] = {0, };
3640         IMAPLOCAL *imaplocal = NULL;
3641         IMAPPARSEDREPLY *reply_from_server = NULL;
3642         emstorage_mail_tbl_t *mail = NULL;
3643         email_partial_buffer *imap_response = NULL;
3644         BODY *body = NULL;
3645         struct _m_content_info *cnt_info = NULL;
3646         emstorage_attachment_tbl_t attachment_tbl;
3647         email_event_partial_body_thd *stSectionNo = NULL;
3648         email_image_data  *image_data = NULL;
3649
3650         if (!(stream) || !(imaplocal = stream->local) || !imaplocal->netstream || !pbd_event) {
3651                 EM_DEBUG_EXCEPTION("invalid parameter");
3652                 err = EMAIL_ERROR_INVALID_PARAM;
3653                 EM_DEBUG_FUNC_END("ret [%d]", ret);
3654                 return ret;
3655         }
3656
3657         EM_DEBUG_LOG("Start of emcore_get_section_for_partial_download, item_count = %d ", item_count);
3658
3659         /* For constructing UID list which is having 10 UID or less at a time */
3660         for (j = 0, stSectionNo = pbd_event; (stSectionNo  != NULL && j < item_count); j++)
3661         {
3662                 EM_DEBUG_LOG("pbd_event[%d].account_id [%d], mail_id [%d], server_mail_id [%d], activity_id [%d]", \
3663                         j, stSectionNo[j].account_id, stSectionNo[j].mail_id, stSectionNo[j].server_mail_id, stSectionNo[j].activity_id);
3664
3665                 if (i32_index >= UID_RANGE_STRING_LENGTH){
3666                         EM_DEBUG_EXCEPTION("String length exceeded its limitation!");
3667                         goto FINISH_OFF;
3668                 }
3669
3670                 if (j == item_count - 1)
3671                         i32_index += SNPRINTF(uid_range_string_to_be_downloaded + i32_index, UID_RANGE_STRING_LENGTH, "%lu", stSectionNo[j].server_mail_id);
3672                 else
3673                         i32_index += SNPRINTF(uid_range_string_to_be_downloaded + i32_index, UID_RANGE_STRING_LENGTH, "%lu,", stSectionNo[j].server_mail_id);
3674         }
3675
3676         SNPRINTF(imap_tag, TAG_LENGTH, "%08lx", 0xffffffff & (stream->gensym++));
3677         SNPRINTF(command, COMMAND_LENGTH, "%s UID FETCH %s (BODYSTRUCTURE BODY.PEEK[TEXT]<0.%d>)\015\012", imap_tag, uid_range_string_to_be_downloaded, input_download_size);
3678
3679         EM_DEBUG_LOG("command : %s", command);
3680
3681         /*  Sending out the IMAP request */
3682         if (!net_sout(imaplocal->netstream, command, (int)EM_SAFE_STRLEN(command))) {
3683                 EM_DEBUG_EXCEPTION("net_sout failed...");
3684                 err = EMAIL_ERROR_CONNECTION_BROKEN;
3685                 goto FINISH_OFF;
3686         }
3687
3688         /*  responce from the server */
3689         imap_response = emcore_get_response_from_server(imaplocal->netstream, imap_tag, &reply_from_server, input_download_size, item_count);
3690
3691         if (!imap_response || !reply_from_server ){
3692                 EM_DEBUG_EXCEPTION(" Invalid response from emcore_get_response_from_server");
3693                 goto FINISH_OFF;
3694         }
3695
3696         for (i = 0; i < item_count ; i++) {
3697                 total_mail_size = 0;
3698                 total_attachment_size = 0;
3699                 attachment_num = 0;
3700                 uidno = 0;
3701
3702                 if ((err = emcore_parse_bodystructure(stream, reply_from_server, imap_response[i].header, &body, &cnt_info, &total_mail_size, &uidno)) != EMAIL_ERROR_NONE) {
3703                         EM_DEBUG_EXCEPTION("emcore_parse_bodystructure failed : [%d]", err);
3704                         goto FINISH_OFF;
3705                 }
3706
3707                 /* Search the account id of pbd_event */
3708                 for (temp_count = 0; temp_count <= item_count && pbd_event[temp_count].server_mail_id != uidno; temp_count++)
3709                         continue;
3710
3711                 if (temp_count > item_count) {
3712                         EM_DEBUG_EXCEPTION("Can't find proper server_mail_id");
3713                         goto FINISH_OFF;
3714                 }
3715
3716                 /* Start to parse the body */
3717                 EM_DEBUG_LOG("Start partial body");
3718
3719                 /* Check the body download status and body size */
3720                 SNPRINTF(uid_string, sizeof(uid_string), "%ld", uidno);
3721                 if (!emstorage_get_maildata_by_servermailid(pbd_event[temp_count].account_id, uid_string, &mail, true, &err) || !mail) {
3722                         EM_DEBUG_EXCEPTION("emstorage_get_mail_data_by_servermailid failed : [%d]", err);
3723                         if (err == EMAIL_ERROR_MAIL_NOT_FOUND || !mail)
3724                                 goto FINISH_OFF;
3725                 }
3726
3727                 if (mail->body_download_status == EMAIL_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED) {
3728                         EM_DEBUG_LOG("Downloaded mail");
3729                         continue;
3730                 }
3731
3732                 /* Update the attachment info except inline attachment */
3733                 if ((err = emcore_update_attachment_except_inline(cnt_info, pbd_event[temp_count].account_id, mail->mail_id, pbd_event[temp_count].mailbox_id, &total_attachment_size, &attachment_num)) != EMAIL_ERROR_NONE) {
3734                         EM_DEBUG_EXCEPTION("emcore_update_attachment_except_inline failed : [%d]", err);
3735                         goto FINISH_OFF;
3736                 }
3737
3738                 EM_DEBUG_LOG("total_mail_size:[%d], total_attachment_size:[%d], attachment_num:[%d]", total_mail_size, total_attachment_size, attachment_num);
3739                 mail->mail_size               = total_mail_size;
3740                 mail->attachment_count = attachment_num;
3741
3742                 if (imap_response[i].body_len == 0) {
3743                         EM_DEBUG_LOG("BODY size is zero");
3744                         continue;
3745                 }
3746
3747                 if ((err = emcore_parse_body_for_imap(imap_response[i].body, imap_response[i].body_len, cnt_info, body->encoding, &text_plain, &text_html, &image_data)) != EMAIL_ERROR_NONE) {
3748                         EM_DEBUG_EXCEPTION("emcore_parse_body_for_imap failed");
3749                         goto FINISH_OFF;
3750                 }
3751
3752                 memset(buf, 0x00, sizeof(buf));
3753
3754                 if (!emstorage_create_dir(pbd_event[temp_count].account_id, mail->mail_id, 0, &err))
3755                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3756
3757                 if (!emstorage_get_save_name(pbd_event[temp_count].account_id, mail->mail_id, 0, cnt_info->text.plain_charset ? cnt_info->text.plain_charset : "UTF-8", buf, &err))
3758                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3759
3760                 if (!emstorage_create_file(text_plain, EM_SAFE_STRLEN(text_plain), buf, &err))
3761                         EM_DEBUG_EXCEPTION("emstorage_create_file failed [%d]", err);
3762
3763                 mail->file_path_plain = EM_SAFE_STRDUP(buf);
3764                 EM_DEBUG_LOG("mail->file_path_plain [%s]", mail->file_path_plain);
3765
3766                 if (image_data != NULL && image_data[0].text_image != NULL && image_data[0].text_image[0] != NULL_CHAR) {
3767                         char *result_string_of_replcaing = NULL;
3768                         int store_file = 0;
3769                         int content_index = 0;
3770
3771                         temp_data_html = em_malloc(imap_response[i].body_len + 1);
3772                         if(!temp_data_html) {
3773                                 EM_DEBUG_EXCEPTION("em_malloc failed");
3774                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
3775                                 goto FINISH_OFF;
3776                         }
3777
3778                         memset(buf, 0x00, sizeof(buf));
3779                         if (text_html != NULL)
3780                                 memcpy(temp_data_html, text_html, EM_SAFE_STRLEN(text_html));
3781                                 /* EM_SAFE_STRNCPY(temp_data_html, text_html, text_html); */
3782
3783                         do {
3784                                 if (!emstorage_create_dir(pbd_event[temp_count].account_id, mail->mail_id, 0, &err))
3785                                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3786
3787                                 if (!emstorage_get_save_name(pbd_event[temp_count].account_id, mail->mail_id, 0, image_data[store_file].image_file_name, buf, &err))
3788                                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3789
3790                                 if (!emstorage_create_file(image_data[store_file].text_image, image_data[store_file].dec_len, buf, &err))
3791                                         EM_DEBUG_EXCEPTION("emstorage_create_file failed [%d]", err);
3792
3793                                 if (mail->body_download_status  != EMAIL_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED){
3794                                         memset(&attachment_tbl, 0x00, sizeof(emstorage_attachment_tbl_t));
3795                                         attachment_tbl.mail_id                          = mail->mail_id;
3796                                         attachment_tbl.account_id                       = pbd_event[temp_count].account_id;
3797                                         attachment_tbl.mailbox_id                       = pbd_event[temp_count].mailbox_id;
3798                                         attachment_tbl.attachment_name                  = image_data[store_file].image_file_name;
3799                                         attachment_tbl.attachment_size                  = image_data[store_file].dec_len;
3800                                         attachment_tbl.attachment_path                  = buf;
3801                                         attachment_tbl.attachment_save_status           = 1;
3802                                         attachment_tbl.attachment_inline_content_status = 1; /*  set to 1 for inline image */
3803                                         attachment_tbl.attachment_mime_type             = image_data[store_file].mime_type;
3804                                         mail->attachment_count++;
3805                                         mail->inline_content_count++;
3806                                         if (!emstorage_add_attachment (&attachment_tbl, false, false, &err))
3807                                                 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed - %d", err);
3808                                 }
3809
3810                                 store_file++;
3811                         } while (image_data[store_file].text_image  != NULL && image_data[store_file].text_image[0]  != NULL_CHAR && (store_file < IMAGE_DISPLAY_PARTIAL_BODY_COUNT));
3812
3813                         while (image_data[content_index].text_image  != NULL && image_data[content_index].text_image[0]  != NULL_CHAR &&
3814                                   image_data[content_index].content_id && image_data[content_index].content_id[0]  != NULL_CHAR && (content_index < IMAGE_DISPLAY_PARTIAL_BODY_COUNT)){   /*  Finding CID in HTML and replacing with image name. */
3815                                 result_string_of_replcaing = em_replace_string((char *)temp_data_html, (char *)image_data[content_index].content_id, (char *)image_data[content_index].image_file_name);
3816
3817                                 EM_SAFE_STRNCPY(temp_data_html, result_string_of_replcaing, EM_SAFE_STRLEN(result_string_of_replcaing));
3818                                 EM_SAFE_FREE(result_string_of_replcaing);
3819
3820                                 if (strstr(temp_data_html, image_data[content_index].content_id)  != NULL)
3821                                         continue; /*  Replace content id on HTML with same file name one more time. */
3822
3823                                 content_index++;
3824                         }
3825
3826                         memset(text_html, 0, imap_response[i].body_len + 1);
3827
3828                         if (temp_data_html != NULL)
3829                                 memcpy(text_html, temp_data_html, EM_SAFE_STRLEN(temp_data_html));
3830
3831                         EM_SAFE_FREE(temp_data_html);
3832                 }
3833
3834                 if (EM_SAFE_STRLEN(text_html) > 0) {
3835                         memset(buf, 0x00, sizeof(buf));
3836                         char html_body[MAX_CHARSET_VALUE] = {0x00, };
3837                         if (cnt_info->text.plain_charset != NULL){
3838                                 if (EM_SAFE_STRLEN(cnt_info->text.plain_charset) < MAX_CHARSET_VALUE)
3839                                         memcpy(html_body, cnt_info->text.plain_charset, EM_SAFE_STRLEN(cnt_info->text.plain_charset));
3840                                 else
3841                                         memcpy(html_body, "UTF-8", strlen("UTF-8"));
3842                         }
3843                         if (html_body[0]  != NULL_CHAR)
3844                                 strcat(html_body, HTML_EXTENSION_STRING);
3845                         else
3846                                 memcpy(html_body, "UTF-8.htm", strlen("UTF-8.htm"));
3847
3848                         if (!emstorage_create_dir(pbd_event[temp_count].account_id, mail->mail_id, 0, &err))
3849                                 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3850
3851                         if (!emstorage_get_save_name(pbd_event[temp_count].account_id, mail->mail_id, 0, html_body, buf, &err))
3852                                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3853
3854                         if (!emstorage_create_file(text_html, EM_SAFE_STRLEN(text_html), buf, &err))
3855                                 EM_DEBUG_EXCEPTION("emstorage_create_file failed [%d]", err);
3856
3857                         mail->file_path_html = EM_SAFE_STRDUP(buf);
3858
3859                         /* free the plain, html, image data */
3860                         EM_SAFE_FREE(text_plain);
3861                         EM_SAFE_FREE(text_html);
3862                         if (image_data)
3863                                 emcore_free_email_image_data(&image_data, IMAGE_DISPLAY_PARTIAL_BODY_COUNT);
3864
3865                 }
3866
3867                 mail->body_download_status = (total_mail_size - total_attachment_size < input_download_size) ? 1 : 2;
3868                 EM_DEBUG_LOG("mail->body_download_status [%d]", mail->body_download_status);
3869
3870                 /* Get preview text */
3871                 if ( (err = emcore_get_preview_text_from_file(mail->file_path_plain, mail->file_path_html, MAX_PREVIEW_TEXT_LENGTH, &(mail->preview_text))) != EMAIL_ERROR_NONE)
3872                         EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file() failed[%d]", err);
3873
3874                 /* Update body contents */
3875                 if (!emstorage_change_mail_field(mail->mail_id, UPDATE_PARTIAL_BODY_DOWNLOAD, mail, true, &err))
3876                         EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed - %d", err);
3877
3878                 if (mail)
3879                         emstorage_free_mail(&mail, 1, NULL);
3880
3881                 if (false == emcore_delete_pbd_activity(pbd_event[temp_count].account_id, pbd_event[temp_count].mail_id, pbd_event[temp_count].activity_id, &err)){
3882                         EM_DEBUG_EXCEPTION("emcore_delete_pbd_activity failed [%d]", err);
3883                         goto FINISH_OFF;
3884                 }
3885         }
3886
3887         ret = true;
3888
3889 FINISH_OFF:
3890
3891         if (error)
3892                 *error = err;
3893
3894         if (true != ret)
3895                 EM_DEBUG_EXCEPTION("Failed download for the uid list %s", command);
3896
3897         if(reply_from_server) {
3898                 EM_SAFE_FREE(reply_from_server->key);
3899                 EM_SAFE_FREE(reply_from_server->line);
3900                 EM_SAFE_FREE(reply_from_server->tag);
3901                 EM_SAFE_FREE(reply_from_server->text);
3902                 EM_SAFE_FREE(reply_from_server);
3903         }
3904
3905         EM_SAFE_FREE(text_plain);
3906         EM_SAFE_FREE(text_html);
3907
3908         if (image_data)
3909                 emcore_free_email_image_data(&image_data, IMAGE_DISPLAY_PARTIAL_BODY_COUNT);
3910
3911         if (cnt_info)
3912                 emcore_free_content_info(cnt_info);
3913
3914         if (body)
3915                 mail_free_body(&body);
3916
3917         if (mail)
3918                 emstorage_free_mail(&mail, 1, NULL);
3919
3920         if (imap_response)
3921                 emcore_free_email_partial_buffer(&imap_response, item_count);
3922
3923         EM_DEBUG_FUNC_END("ret [%d]", ret);
3924         return ret;
3925 }
3926
3927 INTERNAL_FUNC int emcore_download_bulk_partial_mail_body_for_pop3(MAILSTREAM *stream, int input_download_size, email_event_partial_body_thd *pbd_event, int item_count, int *error)
3928 {
3929         EM_DEBUG_FUNC_BEGIN("stream [%p], pbd_event [%p], item_count [%d], error [%p]", stream, pbd_event, item_count, error);
3930         int ret = false, err = EMAIL_ERROR_NONE;
3931         int i;
3932
3933         if (!stream || !pbd_event) {
3934                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3935                 err = EMAIL_ERROR_INVALID_PARAM;
3936                 goto FINISH_OFF;
3937         }
3938
3939         for (i = 0; i < item_count; i++) {
3940                 EM_DEBUG_LOG("pbd_event[%d].account_id [%d], mail_id [%d], server_mail_id [%d], activity_id [%d]", \
3941                         i, pbd_event[i].account_id, pbd_event[i].mail_id, pbd_event[i].server_mail_id, pbd_event[i].activity_id);
3942
3943                 if (!emcore_download_body_multi_sections_bulk(stream, pbd_event[i].account_id, pbd_event[i].mail_id, false, false, input_download_size, 0 , &err)){
3944                         EM_DEBUG_EXCEPTION("emcore_download_body_multi_sections_bulk failed");
3945                         goto FINISH_OFF;
3946                 }
3947
3948                 if (false == emcore_delete_pbd_activity(pbd_event[i].account_id, pbd_event[i].mail_id, pbd_event[i].activity_id, &err)){
3949                         EM_DEBUG_EXCEPTION("emcore_delete_pbd_activity failed [%d]", err);
3950                         goto FINISH_OFF;
3951                 }
3952         }
3953
3954         ret = true;
3955
3956 FINISH_OFF:
3957         if (error)
3958                 *error = err;
3959
3960         EM_DEBUG_FUNC_END("ret [%d] err [%d]", ret, err);
3961         return ret;
3962 }
3963
3964
3965
3966 INTERNAL_FUNC int emcore_download_bulk_partial_mail_body(MAILSTREAM *stream, email_event_partial_body_thd *pbd_event, int item_count, int *error)
3967 {
3968         EM_DEBUG_FUNC_BEGIN("stream [%p], pbd_event [%p], item_count [%d], error [%p]", stream, pbd_event, item_count, error);
3969         int ret = false, err = EMAIL_ERROR_NONE;
3970         emstorage_account_tbl_t *pbd_account_tbl = NULL;
3971         int auto_download_size = 0;
3972
3973         if (!stream || !pbd_event) {
3974                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
3975                 err = EMAIL_ERROR_INVALID_PARAM;
3976                 goto FINISH_OFF;
3977         }
3978
3979         if (!emstorage_get_account_by_id(pbd_event[0].account_id, EMAIL_ACC_GET_OPT_DEFAULT, &pbd_account_tbl, true, &err)) {
3980                 EM_DEBUG_EXCEPTION("emstorage_get_account_by_id failed [%d]", err);
3981                 goto FINISH_OFF;
3982         }
3983
3984         auto_download_size = (pbd_account_tbl->auto_download_size<4096)?4096:pbd_account_tbl->auto_download_size;
3985
3986         switch (pbd_account_tbl->incoming_server_type){
3987                 case EMAIL_SERVER_TYPE_IMAP4:
3988                         ret = emcore_download_bulk_partial_mail_body_for_imap(stream, auto_download_size, pbd_event, item_count, &err);
3989                         break;
3990                 case EMAIL_SERVER_TYPE_POP3:
3991                         ret = emcore_download_bulk_partial_mail_body_for_pop3(stream, auto_download_size, pbd_event, item_count, &err);
3992                         break;
3993                 default:
3994                         err = EMAIL_ERROR_NOT_SUPPORTED;
3995                         ret = false;
3996                         break;
3997         }
3998
3999         ret = true;
4000 FINISH_OFF:
4001         if (error)
4002                 *error = err;
4003
4004         emstorage_free_account(&pbd_account_tbl, 1, NULL);
4005
4006         EM_DEBUG_FUNC_END("ret [%d] err [%d]", ret, err);
4007         return ret;
4008 }
4009
4010 static void emcore_free_email_partial_buffer(email_partial_buffer **partial_buffer, int item_count)
4011 {
4012         EM_DEBUG_FUNC_BEGIN("count : [%d]", item_count);
4013
4014         if (item_count <= 0 || !partial_buffer || !*partial_buffer) {
4015                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4016                 return;
4017         }
4018
4019         email_partial_buffer *p = *partial_buffer;
4020         int i;
4021
4022         for (i = 0; i < item_count ; i++, p++) {
4023                 EM_SAFE_FREE(p->header);
4024                 EM_SAFE_FREE(p->body);
4025         }
4026
4027         EM_SAFE_FREE(*partial_buffer);
4028         EM_DEBUG_FUNC_END();
4029 }
4030
4031 static email_partial_buffer *emcore_get_response_from_server (NETSTREAM *nstream, char *tag, IMAPPARSEDREPLY **reply, int input_download_size, int item_count)
4032 {
4033         EM_DEBUG_FUNC_BEGIN();
4034
4035         if (!nstream || !tag || !reply){
4036                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
4037                 return NIL;
4038         }
4039
4040         email_partial_buffer *retPartialBuffer = NULL;
4041         IMAPPARSEDREPLY *ret_reply = NULL;
4042         char *pre_content = NULL;
4043         char *pline = NULL;
4044         char *p = NULL;
4045         int body_size = 0;
4046         int header_len = 0;
4047         int count = 0;
4048         int ret = false;
4049         int header_start = false;
4050
4051         retPartialBuffer = (email_partial_buffer *)em_malloc(sizeof(email_partial_buffer) * item_count);
4052         if (NULL == retPartialBuffer){
4053                 EM_DEBUG_EXCEPTION("em_malloc failed");
4054                 return NIL;
4055         }
4056
4057         while (nstream){
4058                 if (!(pline = net_getline(nstream))) {
4059                         EM_DEBUG_EXCEPTION("net_getline failed...");
4060                         goto FINISH_OFF;
4061                 }
4062
4063                 /* Search the start of header */
4064                 if (strcasestr(pline, "BODYSTRUCTURE") != NULL) {
4065                         header_len = EM_SAFE_STRLEN(pline);
4066                         header_start = true;
4067                 }
4068
4069                 /* Search the end of header */
4070                 if ((p = strcasestr(pline, "BODY[TEXT]")) != NULL && header_len > 0) {
4071                         /* Get the header content */
4072                         retPartialBuffer[count].header_len = (p - pline) + EM_SAFE_STRLEN(pre_content);
4073                         retPartialBuffer[count].header = em_malloc(retPartialBuffer[count].header_len + 1);
4074                         if (retPartialBuffer[count].header == NULL) {
4075                                 EM_DEBUG_EXCEPTION("em_malloc failed");
4076                                 goto FINISH_OFF;
4077                         }
4078
4079                         if (pre_content != NULL)
4080                                 SNPRINTF(retPartialBuffer[count].header, retPartialBuffer[count].header_len, "%s%s", pre_content, pline);
4081                         else
4082                                 SNPRINTF(retPartialBuffer[count].header, retPartialBuffer[count].header_len, "%s", pline);
4083
4084                         /* Get the body content */
4085                         p += strlen("BODY[TEXT]<0> {");
4086                         body_size = atoi(p);
4087                         retPartialBuffer[count].body_len = (body_size > input_download_size) ? input_download_size : body_size ;
4088                         retPartialBuffer[count].body = em_malloc(retPartialBuffer[count].body_len + 1);
4089                         if (retPartialBuffer[count].body == NULL) {
4090                                 EM_DEBUG_EXCEPTION("em_malloc failed");
4091                                 goto FINISH_OFF;
4092                         }
4093
4094                         if (net_getbuffer(nstream, retPartialBuffer[count].body_len, retPartialBuffer[count].body) <= 0) {
4095                                 EM_DEBUG_EXCEPTION("net_getbuffer failed");
4096                                 goto FINISH_OFF;
4097                         }
4098
4099                         count++;
4100                         header_start = false;
4101                 }
4102
4103                 if (header_start)
4104                         pre_content = EM_SAFE_STRDUP(pline);
4105
4106                 if (0 == strncmp(pline, tag, EM_SAFE_STRLEN(tag))) {
4107
4108                         ret_reply = em_malloc(sizeof(IMAPPARSEDREPLY));
4109                         if (!ret_reply){
4110                                 EM_DEBUG_EXCEPTION("em_malloc failed");
4111                                 goto FINISH_OFF;
4112                         }
4113
4114                         if(reply)
4115                                 *reply = ret_reply;
4116
4117                         if (0 == strncmp(pline + EM_SAFE_STRLEN(tag) + 1, "OK", 2)) {
4118                                 ret_reply->line = (unsigned char*)EM_SAFE_STRDUP(tag);
4119                                 ret_reply->tag  = (unsigned char*)EM_SAFE_STRDUP(tag);
4120                                 ret_reply->key  = (unsigned char*)strdup("OK");
4121                                 ret_reply->text = (unsigned char*)strdup("Success");
4122                                 EM_SAFE_FREE(pline);
4123                                 break;
4124                         }
4125                         else {
4126                                 EM_DEBUG_EXCEPTION("Tagged Response not OK. IMAP4 Response -> [%s]", pline);
4127                                 ret_reply->line = (unsigned char*)EM_SAFE_STRDUP(tag);
4128                                 ret_reply->tag  = (unsigned char*)EM_SAFE_STRDUP(tag);
4129                                 ret_reply->key  = (unsigned char*)strdup("NO");
4130                                 ret_reply->text = (unsigned char*)strdup("Fail");
4131
4132                                 goto FINISH_OFF;
4133
4134                         }
4135                 }
4136                 EM_SAFE_FREE(pline);
4137         }
4138
4139         ret = true;
4140
4141 FINISH_OFF:
4142
4143         EM_SAFE_FREE(pline);
4144         EM_SAFE_FREE(pre_content);
4145
4146         if (!ret) {
4147                 emcore_free_email_partial_buffer(&retPartialBuffer, item_count);
4148         }
4149
4150         EM_DEBUG_FUNC_END("retPartialBuffer [%p]", retPartialBuffer);
4151         return retPartialBuffer;
4152 }
4153
4154 #endif /*  __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
4155 /* EOF */