2bbac96b0f4015cef70b3d8dbb995bc8bb19d1eb
[framework/messaging/email-service.git] / email-core / email-core-mail.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2000 - 2011 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 /******************************************************************************
25  * File :  email-core-mail.c
26  * Desc :  Mail Operation
27  *
28  * Auth : 
29  *
30  * History : 
31  *    2006.08.16  :  created
32  *****************************************************************************/
33 #undef close
34 #include <time.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <fcntl.h>
40
41 #include <sys/time.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <unistd.h>
45
46 #include <vconf.h> 
47 #include <contacts-svc.h>
48
49 #include "email-internal-types.h"
50 #include "c-client.h"
51 #include "lnx_inc.h"
52 #include "email-utilities.h"
53 #include "email-core-global.h"
54 #include "email-core-utils.h"
55 #include "email-core-mail.h"
56 #include "email-core-mime.h"
57 #include "email-core-mailbox.h"
58 #include "email-storage.h"
59 #include "email-core-mailbox-sync.h"
60 #include "email-core-event.h"
61 #include "email-core-account.h" 
62
63 #include "email-convert.h"
64 #include "email-debug-log.h"
65
66 #ifdef __FEATURE_DRM__
67 #include <drm-service.h>
68 #endif /* __FEATURE_DRM__ */
69
70 #define ST_SILENT   (long) 0x2  /* don't return results */
71 #define ST_SET      (long) 0x4  /* set vs. clear */
72
73 static char g_new_server_uid[129];
74
75 bool only_body_download = false;
76
77 int multi_part_body_size = 0;
78 int is_multi_part_body_download_all = 0;
79 int _pop3_receiving_mail_id = 0;
80 int _pop3_received_body_size = 0;
81 int _pop3_last_notified_body_size = 0;
82 int _pop3_total_body_size = 0;
83
84 int _imap4_received_body_size = 0;
85 int _imap4_last_notified_body_size = 0;
86 int _imap4_total_body_size = 0;
87 int _imap4_download_noti_interval_value = 0;
88
89 BODY **g_inline_list = NULL ;
90 int g_inline_count = 0;
91
92 static int  emcore_delete_mail_from_server(emf_account_t *account, emf_mailbox_t *mailbox, int msgno, int *err_code);
93 static int  emcore_mail_cmd_read_mail_pop3(void *stream, int msgno, int limited_size, int *downloded_size, int *total_body_size, int *err_code);
94
95 extern long pop3_send (MAILSTREAM *stream, char *command, char *args);
96
97
98 #ifdef FEATURE_CORE_DEBUG
99 static char *_getType(int type)
100 {
101         switch (type) {
102                 case 0:  return "TYPETEXT";
103                 case 1:  return "TYPEMULTIPART";
104                 case 2:  return "TYPEMESSAGE";
105                 case 3:  return "TYPEAPPLICATION";
106                 case 4:  return "TYPEAUDIO";
107                 case 5:  return "TYPEVIDEO";
108                 case 6:  return "TYPEMODEL";
109                 case 7:  return "TYPEOTHER";
110         }
111         return g_strdup_printf("%d", type);
112 }
113
114 static char *_getEncoding(int encoding)
115 {
116         switch (encoding) {
117                 case 0:  return "ENC7BIT";
118                 case 1:  return "ENC8BIT";
119                 case 2:  return "ENCBINARY";
120                 case 3:  return "ENCBASE64";
121                 case 4:  return "ENCQUOTEDPRINTABLE";
122                 case 5:  return "ENCOTHER";
123         }
124         return g_strdup_printf("%d", encoding);
125 }
126
127 static void _print_parameter(PARAMETER *param)
128 {
129         while (param != NULL) {
130                 EM_DEBUG_EXCEPTION("param->attribute[%s]", param->attribute);
131                 EM_DEBUG_EXCEPTION("param->value[%s]", param->value);
132                 
133                 param = param->next;
134         }
135 }
136
137 static void _print_stringlist(STRINGLIST *stringlist)
138 {
139         while (stringlist != NULL) {
140                 EM_DEBUG_LOG("stringlist->text.data[%s]", stringlist->text.data);
141                 EM_DEBUG_LOG("stringlist->text.size[%ld]", stringlist->text.size);
142                 
143                 stringlist = stringlist->next;
144         }
145 }
146
147 static void _print_body(BODY *body, int recursive)
148 {
149         EM_DEBUG_LOG(" ========================================================== ");
150         
151         if (body != NULL) {
152                 EM_DEBUG_LOG("body->type[%s]", _getType(body->type));
153                 EM_DEBUG_LOG("body->encoding[%s]", _getEncoding(body->encoding));
154                 EM_DEBUG_LOG("body->subtype[%s]", body->subtype);
155                 
156                 EM_DEBUG_LOG("body->parameter[%p]", body->parameter);
157                 
158                 _print_parameter(body->parameter);
159                 
160                 EM_DEBUG_LOG("body->id[%s]", body->id);
161                 EM_DEBUG_LOG("body->description[%s]", body->description);
162                 
163                 EM_DEBUG_LOG("body->disposition.type[%s]", body->disposition.type);
164                 EM_DEBUG_LOG("body->disposition.parameter[%p]", body->disposition.parameter);
165                 
166                 _print_parameter(body->disposition.parameter);
167                 
168                 EM_DEBUG_LOG("body->language[%p]", body->language);
169                 
170                 _print_stringlist(body->language);
171                 
172                 EM_DEBUG_LOG("body->location[%s]", body->location);
173         
174                 EM_DEBUG_LOG("body->mime.offset[%ld]", body->mime.offset);
175                 EM_DEBUG_LOG("body->mime.text.data[%s]", body->mime.text.data);
176                 EM_DEBUG_LOG("body->mime.text.size[%ld]", body->mime.text.size);
177                 
178                 EM_DEBUG_LOG("body->contents.offset[%ld]", body->contents.offset);
179                 EM_DEBUG_LOG("body->contents.text.data[%p]", body->contents.text.data);
180                 EM_DEBUG_LOG("body->contents.text.size[%ld]", body->contents.text.size);
181         
182                 EM_DEBUG_LOG("body->nested.part[%p]", body->nested.part);
183                 
184                 EM_DEBUG_LOG("body->size.lines[%ld]", body->size.lines);
185                 EM_DEBUG_LOG("body->size.bytes[%ld]", body->size.bytes);
186                 
187                 EM_DEBUG_LOG("body->md5[%s]", body->md5);
188                 EM_DEBUG_LOG("body->sparep[%p]", body->sparep);
189                 
190                 if (recursive)  {
191                         PART *part = body->nested.part;
192                         
193                         while (part != NULL)  {
194                                 _print_body(&(part->body), recursive);
195                                 part = part->next;
196                         }
197                 }
198         }
199         
200         EM_DEBUG_LOG(" ========================================================== ");
201 }
202 #endif /*  FEATURE_CORE_DEBUG */
203
204
205 static int pop3_mail_delete(MAILSTREAM *stream, int msgno, int *err_code)
206 {
207         EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], err_code[%p]", stream, msgno, err_code);
208         
209         int ret = false;
210         int err = EMF_ERROR_NONE;
211         
212         POP3LOCAL *pop3local = NULL;
213         char cmd[64];
214         char *p = NULL;
215         
216         if (!stream)  {
217                 EM_DEBUG_EXCEPTION("stream[%p]", stream);
218                 err = EMF_ERROR_INVALID_PARAM;
219                 goto FINISH_OFF;
220         }
221         
222         if (!(pop3local = stream->local) || !pop3local->netstream) {
223                 EM_DEBUG_EXCEPTION("invalid POP3 stream detected...");
224                 err = EMF_ERROR_UNKNOWN;
225                 goto FINISH_OFF;
226         }
227         
228         memset(cmd, 0x00, sizeof(cmd));
229         
230         SNPRINTF(cmd, sizeof(cmd), "DELE %d\015\012", msgno);
231         
232 #ifdef FEATURE_CORE_DEBUG
233         EM_DEBUG_LOG("[POP3] >>> %s", cmd);
234 #endif
235         
236         /*  send command  :  delete specified mail */
237         if (!net_sout(pop3local->netstream, cmd, (int)strlen(cmd))) {
238                 EM_DEBUG_EXCEPTION("net_sout failed...");
239                 err = EMF_ERROR_CONNECTION_BROKEN;              /* EMF_ERROR_UNKNOWN; */
240                 goto FINISH_OFF;
241         }
242         
243         /*  receive response */
244         if (!(p = net_getline(pop3local->netstream))) {
245                 EM_DEBUG_EXCEPTION("net_getline failed...");
246                 err = EMF_ERROR_INVALID_RESPONSE;
247                 goto FINISH_OFF;
248         }
249         
250 #ifdef FEATURE_CORE_DEBUG
251         EM_DEBUG_LOG("[POP3] <<< %s", p);
252 #endif
253
254         if (*p == '-') {                /*  '-ERR' */
255                 err = EMF_ERROR_POP3_DELE_FAILURE;              /* EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER; */
256                 goto FINISH_OFF;
257         }
258         
259         if (*p != '+') {                /*  '+OK' ... */
260                 err = EMF_ERROR_INVALID_RESPONSE;
261                 goto FINISH_OFF;
262         }
263         
264         ret = true;
265
266 FINISH_OFF: 
267         if (p)
268                 free(p);
269         
270         if (err_code)
271                 *err_code = err;
272
273         return ret;
274 }
275
276 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
277
278 static void emcore_mail_copyuid_ex(MAILSTREAM *stream, char *mailbox, unsigned long uidvalidity, SEARCHSET *sourceset, SEARCHSET *destset)
279 {
280         
281         EM_DEBUG_FUNC_BEGIN();
282
283         int index = -1;
284         int i = 0;
285         int err = EMF_ERROR_NONE;
286         
287         unsigned long  first_uid = 0;
288         unsigned long last_uid = 0;
289         
290         unsigned long *old_server_uid = NULL;
291         unsigned long *new_server_uid = NULL;
292         int count = 0;
293         SEARCHSET *temp = sourceset;
294
295         char old_server_uid_char[129];
296         char new_server_uid_char[129];
297
298         if (NULL == sourceset || NULL == destset) {
299                 /* 
300                 sourceset will be NULL when the sequence of UIDs sent to server for mail move operation has all invalid old server uids  
301                 if sourceset is NULL then corresponding dest set will be NULL
302                 */
303                 
304                 EM_DEBUG_LOG("emcore_mail_copyuid_ex failed :  Invalid Parameters--> sourceset[%p] , destset[%p]", sourceset, destset);
305                 return;
306         }
307
308         /* To get count of mails actually moved */
309
310         while (temp) {
311                 if (temp->first > 0) {
312                         first_uid = temp->first;
313
314                         count++;
315
316                         if (temp->last > 0) {
317                                 last_uid = temp->last;
318
319                                 while (first_uid < last_uid) {
320                                         first_uid++;
321                                         count++;
322                                 }
323                         }
324                 }
325                 
326                 temp = temp->next;
327         }
328
329
330         
331
332         EM_DEBUG_LOG("Count of mails copied [%d]", count);
333         old_server_uid = em_malloc(count * sizeof(unsigned long));
334         new_server_uid = em_malloc(count * sizeof(unsigned long));
335
336         /* While loop below will collect all old server uid from sourceset into old_server_uid array */
337         
338         while (sourceset) {
339                 if (sourceset->first > 0) {
340                         first_uid = sourceset->first;
341
342                         index++;
343                         old_server_uid[index] = first_uid;
344
345                         if (sourceset->last > 0) {
346                                 last_uid = sourceset->last;
347
348                                 while (first_uid < last_uid) {
349                                         first_uid++;
350                                         index++;
351
352                                         old_server_uid[index] = first_uid;
353                                 }
354                         }
355                 }
356                 
357                 sourceset = sourceset->next;
358         }
359
360         /* While loop below will collect all new server uid from destset into new_server_uid array */
361
362         index = -1;
363         first_uid = last_uid = 0;
364         
365         while (destset) {
366                 if (destset->first > 0) {
367                         first_uid = destset->first;
368
369                         index++;
370                         new_server_uid[index] = first_uid;
371
372                         if (destset->last > 0) {
373                                 last_uid = destset->last;
374
375                                 while (first_uid < last_uid) {
376                                         first_uid++;
377                                         index++;
378
379                                         new_server_uid[index] = first_uid;
380                                 }
381                         }
382                 }
383                 
384                 destset = destset->next;
385         }
386
387         /* For loop below updates mail_tbl and mail_read_mail_uid_tbl with new server uids*/
388         
389         for (i = 0; i <= index; ++i) {
390
391                 memset(old_server_uid_char, 0x00, sizeof(old_server_uid_char));
392                 sprintf(old_server_uid_char, "%ld", old_server_uid[i]);
393
394                 EM_DEBUG_LOG("Old Server Uid Char[%s]", old_server_uid_char);
395                 
396                 memset(new_server_uid_char, 0x00, sizeof(new_server_uid_char));
397                 sprintf(new_server_uid_char, "%ld", new_server_uid[i]);
398
399                 EM_DEBUG_LOG("New Server Uid Char[%s]", new_server_uid_char);
400         
401                 if (!emstorage_update_server_uid(old_server_uid_char, new_server_uid_char, &err)) {
402                         EM_DEBUG_EXCEPTION("emstorage_update_server_uid failed...[%d]", err);
403                 }
404                 
405                 if (!emstorage_update_read_mail_uid_by_server_uid(old_server_uid_char, new_server_uid_char, mailbox, &err)) {
406                         EM_DEBUG_EXCEPTION("emstorage_update_read_mail_uid_by_server_uid failed... [%d]", err);
407                 }
408                 
409 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
410
411                 if (false == emstorage_update_pbd_activity(old_server_uid_char, new_server_uid_char, mailbox, &err)) {
412                         EM_DEBUG_EXCEPTION("emstorage_update_pbd_activity failed... [%d]", err);
413                 }
414                 
415 #endif
416         }
417
418         EM_SAFE_FREE(old_server_uid);
419         EM_SAFE_FREE(new_server_uid);
420         
421 }
422
423 INTERNAL_FUNC int emcore_move_mail_on_server_ex(int account_id, char *src_mailbox,  int mail_ids[], int num, char *dest_mailbox, int *error_code)
424 {
425         EM_DEBUG_FUNC_BEGIN();
426         MAILSTREAM *stream = NULL;
427         int err_code = EMF_ERROR_NONE;
428         emf_account_t *ref_account = NULL;
429         int ret = false;
430         int i = 0;
431         emf_id_set_t *id_set = NULL;
432         int id_set_count = 0;
433
434         emf_uid_range_set *uid_range_set = NULL;
435         int len_of_each_range = 0;
436
437         emf_uid_range_set *uid_range_node = NULL;
438
439         char **string_list = NULL;      
440         int string_count = 0;
441         
442         if (num <= 0  || account_id <= 0 || NULL == src_mailbox || NULL == dest_mailbox || NULL == mail_ids)  {
443                 if (error_code != NULL) {
444                         *error_code = EMF_ERROR_INVALID_PARAM;
445                 }
446                 EM_DEBUG_LOG("Invalid Parameters- num[%d], account_id[%d], src_mailbox[%p], dest_mailbox[%p], mail_ids[%p]", num, account_id, src_mailbox, dest_mailbox, mail_ids);
447                 return false;
448         }
449
450         ref_account = emcore_get_account_reference(account_id);
451         
452         if (NULL == ref_account) {
453                 EM_DEBUG_EXCEPTION(" emcore_get_account_reference failed[%d]", account_id);
454                 
455                 *error_code = EMF_ERROR_INVALID_ACCOUNT;
456                 goto FINISH_OFF;
457         }
458
459  
460         if (ref_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4) {
461                 *error_code = EMF_ERROR_INVALID_PARAM;
462                 goto FINISH_OFF;
463         }
464
465
466         if (!emcore_connect_to_remote_mailbox(account_id, src_mailbox, (void **)&stream, &err_code))             {
467                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed[%d]", err_code);
468                 
469                 goto FINISH_OFF;
470         }
471
472         if (NULL != stream) {
473                 mail_parameters(stream, SET_COPYUID, emcore_mail_copyuid_ex);
474                 EM_DEBUG_LOG("calling mail_copy_full FODLER MAIL COPY ");
475                 /*  [h.gahlaut] Break the set of mail_ids into comma separated strings of given length  */
476                 /*  Length is decided on the basis of remaining keywords in the Query to be formed later on in emstorage_get_id_set_from_mail_ids  */
477                 /*  Here about 90 bytes are required for fixed keywords in the query-> SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (....) ORDER by s_uid  */
478                 /*  So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90  */
479                 
480                 if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err_code))   {
481                         EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err_code);
482                         goto FINISH_OFF;
483                 }
484                 
485                 /*  Now execute one by one each comma separated string of mail_ids */
486
487                 for (i = 0; i < string_count; ++i) {
488                         /*  Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order */
489                 
490                         if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err_code)) {
491                                 EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err_code);
492                                 goto FINISH_OFF;
493                         }
494                         
495                         /*  Convert the sorted sequence of server mail ids to range sequences of given length. A range sequence will be like A : B, C, D: E, H */
496                         
497                         len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40; /*  1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag. */
498                         
499                         if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err_code)) {
500                                 EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err_code);
501                                 goto FINISH_OFF;
502                         }
503                 
504                         uid_range_node = uid_range_set;
505                 
506                         while (uid_range_node != NULL) {
507                                 /* Remove comma from end of uid_range */
508                                 uid_range_node->uid_range[strlen(uid_range_node->uid_range) - 1] = '\0';
509                                 EM_DEBUG_LOG("uid_range_node->uid_range - %s", uid_range_node->uid_range);
510                                 if (!mail_copy_full(stream, uid_range_node->uid_range, dest_mailbox, CP_UID | CP_MOVE)) {
511                                         EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex :   Mail cannot be moved failed");
512                                         EM_DEBUG_EXCEPTION("Mail MOVE failed ");
513                                         goto FINISH_OFF;
514                                 }
515                                 else if (!mail_expunge_full(stream, uid_range_node->uid_range, EX_UID)) {
516                                         EM_DEBUG_EXCEPTION("emcore_move_mail_on_server_ex :   Mail cannot be expunged after move. failed!");
517                                                 EM_DEBUG_EXCEPTION("Mail Expunge after move failed ");
518                                                   goto FINISH_OFF;
519                                 }
520                                 else {
521                                         EM_DEBUG_LOG("Mail MOVE SUCCESS ");
522                                 }
523                                 
524                                 uid_range_node = uid_range_node->next;
525                         }       
526
527                         emcore_free_uid_range_set(&uid_range_set);
528
529                         EM_SAFE_FREE(id_set);
530                         
531                         id_set_count = 0;
532                 }
533         
534         }
535         else {
536                 EM_DEBUG_EXCEPTION(">>>> STREAM DATA IS NULL >>> ");
537                 goto FINISH_OFF;
538         }
539         
540
541         ret = true;
542
543 FINISH_OFF: 
544         emcore_close_mailbox(0, stream);
545         stream = NULL;
546
547 #ifdef __FEATURE_LOCAL_ACTIVITY__
548         if (ret || ref_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4) /* Delete local activity for POP3 mails and successful move operation in IMAP */ {
549                 emstorage_activity_tbl_t new_activity;
550                 for (i = 0; i<num ; i++) {              
551                         memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
552                         new_activity.activity_type = ACTIVITY_MOVEMAIL;
553                         new_activity.account_id    = account_id;
554                         new_activity.mail_id       = mail_ids[i];
555                         new_activity.src_mbox      = src_mailbox;
556                         new_activity.dest_mbox     = dest_mailbox;
557                 
558                         if (!emcore_delete_activity(&new_activity, &err_code)) {
559                                 EM_DEBUG_EXCEPTION(">>>>>>Local Activity ACTIVITY_MOVEMAIL [%d] ", err_code);
560                         }
561                 }
562
563         }
564 #endif
565
566         if (error_code != NULL) {
567                 *error_code = err_code;
568         }
569
570         return ret;
571 }
572
573 #ifdef __FEATURE_LOCAL_ACTIVITY__
574 int imap4_mail_delete_ex(emf_mailbox_t *mailbox,  int mail_ids[], int num, int from_server, int *err_code)
575 #else
576 int imap4_mail_delete_ex(emf_mailbox_t *mailbox,  int mail_ids[], int num, int *err_code)
577 #endif
578 {
579         EM_DEBUG_FUNC_BEGIN();
580         
581         int ret = false;
582         int err = EMF_ERROR_NONE;
583         
584         IMAPLOCAL *imaplocal = NULL;
585         char *p = NULL;
586         MAILSTREAM *stream = NULL;
587         char tag[MAX_TAG_SIZE];
588         char cmd[MAX_IMAP_COMMAND_LENGTH];
589         emf_id_set_t *id_set = NULL;
590         int id_set_count = 0;
591         int i = 0;
592         emf_uid_range_set *uid_range_set = NULL;
593         int len_of_each_range = 0;
594         emf_uid_range_set *uid_range_node = NULL;
595         char **string_list = NULL;      
596         int string_count = 0;
597         int delete_success = false;
598                 
599         if (num <= 0 || !mail_ids || !mailbox) {
600                 EM_DEBUG_EXCEPTION(" Invalid parameter ");
601                 err = EMF_ERROR_INVALID_PARAM;
602                 goto FINISH_OFF;
603         }
604
605         stream = mailbox->mail_stream;
606         if (!stream) {
607                 EM_DEBUG_EXCEPTION(" Stream is NULL ");
608                 err = EMF_ERROR_INVALID_PARAM;
609                 goto FINISH_OFF;
610                 
611         }
612
613         /*  [h.gahlaut] Break the set of mail_ids into comma separated strings of given length  */
614         /*  Length is decided on the basis of remaining keywords in the Query to be formed later on in emstorage_get_id_set_from_mail_ids  */
615         /*  Here about 90 bytes are required for fixed keywords in the query-> SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (....) ORDER by s_uid  */
616         /*  So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90  */
617
618         if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err))   {
619                 EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err);
620                 goto FINISH_OFF;
621         }
622
623         /*  Now execute one by one each comma separated string of mail_ids  */
624         
625         for (i = 0; i < string_count; ++i) {
626                 /*  Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order  */
627
628                 if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err)) {
629                         EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err);
630                         goto FINISH_OFF;
631                 }
632                 
633                 /*  Convert the sorted sequence of server mail ids to range sequences of given length. A range sequence will be like A : B, C, D: E, H  */
634                 
635                 len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40;               /*   1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag.  */
636                 
637                 if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err)) {
638                         EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err);
639                         goto FINISH_OFF;
640                 }
641
642                 uid_range_node = uid_range_set;
643
644                 while (uid_range_node != NULL) {
645                         /*  Remove comma from end of uid_range  */
646
647                         uid_range_node->uid_range[strlen(uid_range_node->uid_range) - 1] = '\0';
648
649                         if (!(imaplocal = stream->local) || !imaplocal->netstream)  {
650                                 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
651                 
652                                 err = EMF_ERROR_CONNECTION_BROKEN;
653                                 goto FINISH_OFF;
654                         }
655
656                         memset(tag, 0x00, sizeof(tag));
657                         memset(cmd, 0x00, sizeof(cmd));
658                         
659                         SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
660                         SNPRINTF(cmd, sizeof(cmd), "%s UID STORE %s +FLAGS (\\Deleted)\015\012", tag, uid_range_node->uid_range);
661                         
662                         EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
663
664                         
665                         /* send command  :  set deleted flag */
666                         if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd)))  {
667                                 EM_DEBUG_EXCEPTION("net_sout failed...");
668                                 
669                                 err = EMF_ERROR_CONNECTION_BROKEN;              /* EMF_ERROR_UNKNOWN */
670                                 goto FINISH_OFF;
671                         }
672
673                         
674                         while (imaplocal->netstream)  {
675                                 /* receive response */
676                                 if (!(p = net_getline(imaplocal->netstream))) {
677                                         EM_DEBUG_EXCEPTION("net_getline failed...");
678                                         
679                                         err = EMF_ERROR_INVALID_RESPONSE;               /* EMF_ERROR_UNKNOWN; */
680                                         goto FINISH_OFF;
681                                 }
682                                 
683
684                                 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
685
686                                 /* To confirm - Commented out as FETCH response does not contain the tag - may be a problem for common stream in email-service*/
687                                 /* Success case - delete all local activity and entry from mail_read_mail_uid_tbl 
688                                 if (strstr(p, "FETCH") != NULL)  {
689                                         EM_DEBUG_LOG(" FETCH Response recieved ");
690                                         delete_success = true;
691                                         EM_SAFE_FREE(p);
692                                         break;
693                                 }
694                                 */
695                 
696                         
697                                 if (!strncmp(p, tag, strlen(tag)))  {
698                                         if (!strncmp(p + strlen(tag) + 1, "OK", 2))  {
699                                                 /*Error scenario delete all local activity and entry from mail_read_mail_uid_tbl */
700                                                 EM_DEBUG_LOG(" OK Response recieved ");
701                                                 delete_success = true;
702                                                 EM_SAFE_FREE(p);
703                                                 break;
704                                         }                                       
705                                         else  {
706                                                 /*  'NO' or 'BAD' */
707                                                 delete_success = false;
708                                                 err = EMF_ERROR_IMAP4_STORE_FAILURE;            /* EMF_ERROR_INVALID_RESPONSE; */
709                                                 goto FINISH_OFF;
710                                         }
711                                 }
712                         
713                                 EM_SAFE_FREE(p);
714                                 }
715                                 
716                                 memset(tag, 0x00, sizeof(tag));
717                                 memset(cmd, 0x00, sizeof(cmd));
718
719                                 EM_DEBUG_LOG("Calling Expunge");
720                                 
721                                 SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
722                                 SNPRINTF(cmd, sizeof(cmd), "%s EXPUNGE\015\012", tag);
723                                 
724                                 EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
725                                 
726                                 /* send command  :   EXPUNGE */
727                                 if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd)))  {
728                                         EM_DEBUG_EXCEPTION("net_sout failed...");
729                                         
730                                         err = EMF_ERROR_CONNECTION_BROKEN;
731                                         goto FINISH_OFF;
732                                 }
733
734                                 while (imaplocal->netstream)  {
735                                         /* receive response */
736                                         if (!(p = net_getline(imaplocal->netstream))) {
737                                                 EM_DEBUG_EXCEPTION("net_getline failed...");
738                                                 
739                                                 err = EMF_ERROR_INVALID_RESPONSE;               /* EMF_ERROR_UNKNOWN; */
740                                                 goto FINISH_OFF;
741                                         }
742                                         
743                                         EM_DEBUG_LOG("[IMAP4] <<< %s", p);
744                                         
745                                         if (!strncmp(p, tag, strlen(tag)))  {
746                                                 if (!strncmp(p + strlen(tag) + 1, "OK", 2))  {
747 #ifdef __FEATURE_LOCAL_ACTIVITY__
748                                                         int index = 0;
749                                                         emstorage_mail_tbl_t **mail = NULL;
750                                                         
751                                                         mail = (emstorage_mail_tbl_t **) em_malloc(num * sizeof(emstorage_mail_tbl_t *));
752                                                         if (!mail) {
753                                                                 EM_DEBUG_EXCEPTION("em_malloc failed");
754                                                                 err = EMF_ERROR_UNKNOWN;
755                                                                 goto FINISH_OFF;
756                                                         }
757
758                                                         if (delete_success) {
759                                                                 for (index = 0 ; index < num; index++) {
760                                                                         if (!emstorage_get_downloaded_mail(mail_ids[index], &mail[index], false, &err))  {
761                                                                                 EM_DEBUG_LOG("emstorage_get_uid_by_mail_id failed [%d]", err);
762                                                                                 
763                                                                                 if (err == EMF_ERROR_MAIL_NOT_FOUND)  {         
764                                                                                         EM_DEBUG_LOG("EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER  : ");
765                                                                                         continue;
766                                                                                 }
767                                                                         }               
768                                                                 
769                                                                         if (mail[index]) {                              
770                                                                                 /* Clear entry from mail_read_mail_uid_tbl */
771                                                                                 if (mail[index]->server_mail_id != NULL) {
772                                                                                         if (!emstorage_remove_downloaded_mail(mailbox->account_id, mailbox->name, mail[index]->server_mail_id, true, &err))  {
773                                                                                                 EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
774                                                                                         }
775                                                                                 }
776                                                                                 
777                                                                                 /* Delete local activity */
778                                                                                 emstorage_activity_tbl_t new_activity;
779                                                                                 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
780                                                                                 if (from_server == EMF_DELETE_FOR_SEND_THREAD) {
781                                                                                         new_activity.activity_type = ACTIVITY_DELETEMAIL_SEND;
782                                                                                         EM_DEBUG_LOG("from_server == EMF_DELETE_FOR_SEND_THREAD ");
783                                                                                 }
784                                                                                 else {
785                                                                                         new_activity.activity_type      = ACTIVITY_DELETEMAIL;
786                                                                                 }
787
788                                                                                 new_activity.mail_id            = mail[index]->mail_id;
789                                                                                 new_activity.server_mailid      = NULL;
790                                                                                 new_activity.src_mbox           = NULL;
791                                                                                 new_activity.dest_mbox          = NULL;
792                                                                                                                 
793                                                                                 if (!emcore_delete_activity(&new_activity, &err)) {
794                                                                                         EM_DEBUG_EXCEPTION(" emcore_delete_activity  failed  - %d ", err);
795                                                                                 }
796                                                                         }
797                                                                         else {
798                                                                                 /* Fix for crash seen while deleting Outbox mails which are moved to Trash. Outbox mails do not have server mail id and are not updated in mail_read_mail_uid_tbl.
799                                                                                   * So there is no need of deleting entry in mail_read_mail_uid_tbl. However local activity has to be deleted. 
800                                                                                 */
801                                                                                 /* Delete local activity */
802                                                                                 emstorage_activity_tbl_t new_activity;
803                                                                                 memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
804                                                                                 new_activity.activity_type      = ACTIVITY_DELETEMAIL;
805                                                                                 new_activity.mail_id            = mail_ids[index]; /* valid mail id passed for outbox mails*/
806                                                                                 new_activity.server_mailid      = NULL;
807                                                                                 new_activity.src_mbox           = NULL;
808                                                                                 new_activity.dest_mbox          = NULL;
809                                                                                                                 
810                                                                                 if (!emcore_delete_activity(&new_activity, &err)) {
811                                                                                         EM_DEBUG_EXCEPTION(" emcore_delete_activity  failed  - %d ", err);
812                                                                                 }
813                                                                         }
814                                                                 }
815
816                                                                 for (index = 0; index < num; index++) {
817                                                                         if (!emstorage_free_mail(&mail[index], 1, &err)) {
818                                                                                 EM_DEBUG_EXCEPTION(" emstorage_free_mail [%d]", err);
819                                                                         }
820                                                                 }
821
822                                                                 EM_SAFE_FREE(mail);
823                                                         }
824
825 #endif
826         EM_SAFE_FREE(p);
827                                                         break;
828                                                 }
829                                                 else  {         /*  'NO' or 'BAD' */
830                                                         err = EMF_ERROR_IMAP4_EXPUNGE_FAILURE;          /* EMF_ERROR_INVALID_RESPONSE; */
831                                                         goto FINISH_OFF;
832                                                 }
833                                         }
834                                         
835                                         EM_SAFE_FREE(p);
836                                 }
837                                 uid_range_node = uid_range_node->next;
838                 }       
839
840                 emcore_free_uid_range_set(&uid_range_set);
841
842                 EM_SAFE_FREE(id_set);
843                 
844                 id_set_count = 0;
845         }
846         
847         ret = true;
848
849 FINISH_OFF: 
850         EM_SAFE_FREE(p);
851         
852         emcore_free_comma_separated_strings(&string_list, &string_count);
853
854         if (false == ret) {
855                 emcore_free_uid_range_set(&uid_range_set);
856         }
857         
858         if (err_code) {
859                 *err_code = err;
860         }
861
862         return ret;
863
864 }
865
866 #endif
867
868 static int imap4_mail_delete(MAILSTREAM *stream, int msgno, int *err_code)
869 {
870         EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], err_code[%p]", stream, msgno, err_code);
871         
872         int ret = false;
873         int err = EMF_ERROR_NONE;
874         
875         IMAPLOCAL *imaplocal = NULL;
876         char tag[16], cmd[64];
877         char *p = NULL;
878         
879         if (!(imaplocal = stream->local) || !imaplocal->netstream) {
880                 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
881                 
882                 err = EMF_ERROR_INVALID_PARAM;          /* EMF_ERROR_UNKNOWN */
883                 goto FINISH_OFF;
884         }
885         
886         memset(tag, 0x00, sizeof(tag));
887         memset(cmd, 0x00, sizeof(cmd));
888         
889         SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
890         SNPRINTF(cmd, sizeof(cmd), "%s STORE %d +FLAGS.SILENT (\\Deleted)\015\012", tag, msgno);
891
892 #ifdef FEATURE_CORE_DEBUG
893         EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
894 #endif
895         
896         /*  send command  :  set deleted flag */
897         if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd))) {
898                 EM_DEBUG_EXCEPTION("net_sout failed...");
899                 
900                 err = EMF_ERROR_CONNECTION_BROKEN;              /* EMF_ERROR_UNKNOWN */
901                 goto FINISH_OFF;
902         }
903         
904         while (imaplocal->netstream) {
905                 /*  receive response */
906                 if (!(p = net_getline(imaplocal->netstream))) {
907                         EM_DEBUG_EXCEPTION("net_getline failed...");
908                         
909                         err = EMF_ERROR_INVALID_RESPONSE;               /* EMF_ERROR_UNKNOWN; */
910                         goto FINISH_OFF;
911                 }
912                 
913 #ifdef FEATURE_CORE_DEBUG
914                 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
915 #endif          
916                 if (!strncmp(p, tag, strlen(tag))) {
917                         if (!strncmp(p + strlen(tag) + 1, "OK", 2))  {
918                                 EM_SAFE_FREE(p);
919                                 break;
920                         }
921                         else  {         /*  'NO' or 'BAD' */
922                                 err = EMF_ERROR_IMAP4_STORE_FAILURE;            /* EMF_ERROR_INVALID_RESPONSE; */
923                                 goto FINISH_OFF;
924                         }
925                 }
926                 
927                 EM_SAFE_FREE(p);
928         }
929         
930         memset(tag, 0x00, sizeof(tag));
931         memset(cmd, 0x00, sizeof(cmd));
932         
933         SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
934         SNPRINTF(cmd, sizeof(cmd), "%s EXPUNGE\015\012", tag);
935
936 #ifdef FEATURE_CORE_DEBUG
937         EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
938 #endif
939         
940         /*  send command  :  delete flaged mail */
941         if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd)))  {
942                 EM_DEBUG_EXCEPTION("net_sout failed...");
943                 
944                 err = EMF_ERROR_CONNECTION_BROKEN;
945                 goto FINISH_OFF;
946         }
947         
948         while (imaplocal->netstream)  {
949                 /*  receive response */
950                 if (!(p = net_getline(imaplocal->netstream)))  {
951                         EM_DEBUG_EXCEPTION("net_getline failed...");
952                         
953                         err = EMF_ERROR_INVALID_RESPONSE;               /* EMF_ERROR_UNKNOWN; */
954                         goto FINISH_OFF;
955                 }
956                 
957 #ifdef FEATURE_CORE_DEBUG
958                 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
959 #endif
960                 
961                 if (!strncmp(p, tag, strlen(tag)))  {
962                         if (!strncmp(p + strlen(tag) + 1, "OK", 2))  {
963                                 EM_SAFE_FREE(p);
964                                 break;
965                         }
966                         else  {         /*  'NO' or 'BAD' */
967                                 err = EMF_ERROR_IMAP4_EXPUNGE_FAILURE;          /* EMF_ERROR_INVALID_RESPONSE; */
968                                 goto FINISH_OFF;
969                         }
970                 }
971                 
972                 EM_SAFE_FREE(p);
973         }
974         
975         ret = true;
976
977 FINISH_OFF: 
978         EM_SAFE_FREE(p);
979         
980         if (err_code)
981                 *err_code = err;
982
983         return ret;
984 }
985
986 typedef enum {
987         IMAP4_CMD_EXPUNGE
988 } imap4_cmd_t;
989
990 static int imap4_send_command(MAILSTREAM *stream, imap4_cmd_t cmd_type, int *err_code)
991 {
992         EM_DEBUG_FUNC_BEGIN("stream[%p], cmd_type[%d], err_code[%p]", stream, cmd_type, err_code);
993         
994         int ret = false;
995         int err = EMF_ERROR_NONE;
996         
997         IMAPLOCAL *imaplocal = NULL;
998         char tag[16], cmd[64];
999         char *p = NULL;
1000         
1001         if (!(imaplocal = stream->local) || !imaplocal->netstream) {
1002                 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
1003                 
1004                 err = EMF_ERROR_INVALID_PARAM;          /* EMF_ERROR_UNKNOWN */
1005                 goto FINISH_OFF;
1006         }
1007         
1008         memset(tag, 0x00, sizeof(tag));
1009         memset(cmd, 0x00, sizeof(cmd));
1010         
1011         SNPRINTF(tag, sizeof(tag), "%08lx", 0xffffffff & (stream->gensym++));
1012         SNPRINTF(cmd, sizeof(cmd), "%s EXPUNGE\015\012", tag);
1013
1014 #ifdef FEATURE_CORE_DEBUG
1015         EM_DEBUG_LOG("[IMAP4] >>> %s", cmd);
1016 #endif
1017         
1018         /*  send command  :  delete flaged mail */
1019         if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd))) {
1020                 EM_DEBUG_EXCEPTION("net_sout failed...");
1021                 
1022                 err = EMF_ERROR_CONNECTION_BROKEN;
1023                 goto FINISH_OFF;
1024         }
1025         
1026         while (imaplocal->netstream) {
1027                 /*  receive response */
1028                 if (!(p = net_getline(imaplocal->netstream))) {
1029                         EM_DEBUG_EXCEPTION("net_getline failed...");
1030                         
1031                         err = EMF_ERROR_INVALID_RESPONSE;               /* EMF_ERROR_UNKNOWN; */
1032                         goto FINISH_OFF;
1033                 }
1034                 
1035 #ifdef FEATURE_CORE_DEBUG
1036                 EM_DEBUG_LOG("[IMAP4] <<< %s", p);
1037 #endif
1038                 
1039                 if (!strncmp(p, tag, strlen(tag))) {
1040                         if (!strncmp(p + strlen(tag) + 1, "OK", 2)) {
1041                                 EM_SAFE_FREE(p);
1042                                 break;
1043                         }
1044                         else {          /*  'NO' or 'BAD' */
1045                                 err = EMF_ERROR_IMAP4_EXPUNGE_FAILURE;          /* EMF_ERROR_INVALID_RESPONSE; */
1046                                 goto FINISH_OFF;
1047                         }
1048                 }
1049                 
1050                 EM_SAFE_FREE(p);
1051         }
1052         
1053         ret = true;
1054
1055 FINISH_OFF: 
1056         EM_SAFE_FREE(p);
1057         
1058         if (err_code)
1059                 *err_code = err;
1060
1061         return ret;
1062 }
1063
1064 int emcore_get_mail_contact_info(emf_mail_contact_info_t *contact_info, char *full_address, int *err_code)
1065 {
1066         EM_DEBUG_FUNC_BEGIN("contact_info[%p], full_address[%s], err_code[%p]", contact_info, full_address, err_code);
1067         
1068         int ret = false;
1069         int err = EMF_ERROR_NONE;
1070         
1071         if (!emcore_get_mail_contact_info_with_update(contact_info, full_address, 0, &err))
1072                 EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info_with_update failed [%d]", err);
1073         else
1074                 ret = true;
1075         
1076         if (err_code != NULL)
1077                 *err_code = err;
1078         
1079         return ret;
1080 }
1081
1082 INTERNAL_FUNC int emcore_get_mail_display_name(CTSvalue *contact_name_value, char **contact_display_name)
1083 {
1084         EM_DEBUG_FUNC_BEGIN("contact_name_value[%p], contact_display_name[%p]", contact_name_value, contact_display_name);
1085         char *display = NULL;
1086         const char *first = contacts_svc_value_get_str(contact_name_value, CTS_NAME_VAL_FIRST_STR);
1087         const char *last = contacts_svc_value_get_str(contact_name_value, CTS_NAME_VAL_LAST_STR);
1088
1089         EM_DEBUG_LOG(">>>>>> first[%s] last[%s]", first, last);
1090         if (first != NULL && last != NULL) {
1091                 /* if (CTS_ORDER_NAME_FIRSTLAST  == contacts_svc_get_name_order()) */
1092                 if (CTS_ORDER_NAME_FIRSTLAST == contacts_svc_get_order(CTS_ORDER_OF_DISPLAY))
1093                         display = g_strconcat(first, " ", last, NULL);
1094                 else
1095                         display = g_strconcat(last, " ", first, NULL);
1096
1097         }
1098         else if (first != NULL || last != NULL) {
1099                 if (first != NULL)
1100                         display = (char *)EM_SAFE_STRDUP(first);
1101                 else
1102                         display = (char *)EM_SAFE_STRDUP(last);
1103         }
1104         else
1105                 display = g_strdup(contacts_svc_value_get_str(contact_name_value, CTS_NAME_VAL_DISPLAY_STR));
1106
1107         if (contact_display_name != NULL)
1108                 *contact_display_name = display;
1109
1110         return true;
1111 }
1112
1113 int emcore_get_mail_contact_info_with_update(emf_mail_contact_info_t *contact_info, char *full_address, int mail_id, int *err_code)
1114 {
1115         EM_DEBUG_FUNC_BEGIN("contact_info[%p], full_address[%s], mail_id[%d], err_code[%p]", contact_info, full_address, mail_id, err_code);
1116         
1117         int ret = false;
1118         int err = EMF_ERROR_NONE;
1119         ADDRESS *addr = NULL;
1120         char *address = NULL;
1121         char *temp_emailaddr = NULL;
1122         int start_text_ascii = 2;
1123         int end_text_ascii = 3; 
1124         char *alias = NULL;
1125         int is_searched = false;
1126         int address_length = 0;
1127         char *email_address = NULL;
1128         int contact_name_len = 0;
1129         char temp_string[1024] = { 0 , };
1130         int is_saved = 0;
1131         CTSstruct *contact =  NULL;
1132         CTSvalue *contact_name_value = NULL;
1133         int contact_index = -1;
1134         char *contact_display_name = NULL;
1135         char *contact_display_name_from_contact_info = NULL;
1136         int contact_display_name_len = 0;
1137         int i = 0;
1138         int contact_name_buffer_size = 0;
1139         char *contact_name = NULL;
1140
1141         if (!contact_info)  {
1142                 EM_DEBUG_EXCEPTION("contact_info[%p]", contact_info);
1143                 err = EMF_ERROR_INVALID_PARAM;
1144                 goto FINISH_OFF;
1145         }
1146
1147         if (!full_address) {
1148                 full_address = "";
1149                 address_length = 0;
1150                 temp_emailaddr = NULL;
1151         }
1152         else {
1153                 address_length = 2 * strlen(full_address);
1154                 temp_emailaddr = (char  *)calloc(1, address_length); 
1155         }
1156
1157     em_skip_whitespace(full_address , &address);
1158         EM_DEBUG_LOG("address[address][%s]", address);  
1159
1160
1161         /*  ',' -> "%2C" */
1162         gchar **tokens = g_strsplit(address, ", ", -1);
1163         char *p = g_strjoinv("%2C", tokens);
1164         
1165         g_strfreev(tokens);
1166         
1167         /*  ';' -> ',' */
1168         while (p && p[i] != '\0') 
1169     {
1170                 if (p[i] == ';') 
1171                         p[i] = ',';
1172                 i++;
1173         }
1174         EM_DEBUG_LOG("  2  converted address %s ", p);
1175
1176         rfc822_parse_adrlist(&addr, p, NULL);
1177
1178         EM_SAFE_FREE(p);
1179         EM_DEBUG_LOG("  3  full_address  %s ", full_address);
1180
1181         if (!addr) {
1182                 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
1183                 err = EMF_ERROR_INVALID_ADDRESS;
1184                 goto FINISH_OFF;
1185         }
1186
1187         contact_name_buffer_size = address_length;
1188         contact_name = (char*)em_malloc(contact_name_buffer_size);
1189
1190         if (!contact_name) {
1191                 EM_DEBUG_EXCEPTION("Memory allocation error!");
1192                 err = EMF_ERROR_OUT_OF_MEMORY;
1193                 goto FINISH_OFF;
1194         }
1195         
1196         while (addr != NULL)  {
1197                 if (addr->mailbox && addr->host) {      
1198                         if (!strncmp(addr->mailbox , "UNEXPECTED_DATA_AFTER_ADDRESS", strlen("UNEXPECTED_DATA_AFTER_ADDRESS")) || !strncmp(addr->mailbox , "INVALID_ADDRESS", strlen("INVALID_ADDRESS")) || !strncmp(addr->host , ".SYNTAX-ERROR.", strlen(".SYNTAX-ERROR.")))
1199                 {
1200                                 EM_DEBUG_LOG("Invalid address ");
1201                                 addr = addr->next;
1202                                 continue;
1203                         }
1204                 }
1205                 else  {
1206                         EM_DEBUG_LOG("Error in parsing..! ");
1207                         addr = addr->next;
1208                         continue;
1209                 }
1210
1211                 EM_SAFE_FREE(email_address);
1212                 email_address = g_strdup_printf("%s@%s", addr->mailbox ? addr->mailbox  :  "", addr->host ? addr->host  :  "");
1213                 
1214                 EM_DEBUG_LOG(" addr->personal[%s]", addr->personal);
1215                 EM_DEBUG_LOG(" email_address[%s]", email_address);
1216         
1217                 is_searched = false;
1218                 EM_DEBUG_LOG(" >>>>> emcore_get_mail_contact_info - 10");
1219                         
1220                 err = contacts_svc_find_contact_by(CTS_FIND_BY_EMAIL, email_address);
1221                 if (err > CTS_SUCCESS) {
1222                         contact_index = err;
1223                         if ((err = contacts_svc_get_contact(contact_index, &contact)) == CTS_SUCCESS) {
1224                                 /*  get  contact name */
1225                                 if (contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &contact_name_value) == CTS_SUCCESS) {    /*  set contact display name name */
1226                                         contact_info->contact_id = contact_index;               /*  NOTE :  This is valid only if there is only one address. */
1227                                         emcore_get_mail_display_name(contact_name_value, &contact_display_name_from_contact_info);
1228
1229                                         contact_display_name = contact_display_name_from_contact_info;
1230
1231                                         
1232                                         EM_DEBUG_LOG(">>> contact_index[%d]", contact_index);
1233                                         EM_DEBUG_LOG(">>> contact_name[%s]", contact_display_name);
1234
1235                                         /*  Make display name string */
1236                                         if (contact_display_name != NULL) {
1237                                                 is_searched = true;
1238
1239                                                 if (mail_id == 0 || (contact_name_len == 0))             {      /*  save only the first address information - 09-SEP-2010 */
1240                                                         contact_display_name_len = strlen(contact_display_name);
1241                                                         if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) {  /*  re-alloc memory */
1242                                                                 char *temp = contact_name;
1243                                                                 contact_name_buffer_size += contact_name_buffer_size;
1244                                                                 contact_name = (char  *)calloc(1, contact_name_buffer_size); 
1245                                                                 if (contact_name == NULL) {
1246                                                                         EM_DEBUG_EXCEPTION("Memory allocation failed.");
1247                                                                         EM_SAFE_FREE(temp);
1248                                                                         goto FINISH_OFF;
1249                                                                 }
1250                                                                 snprintf(contact_name, contact_name_buffer_size, "%s", temp);
1251                                                                 EM_SAFE_FREE(temp);
1252                                                         }
1253
1254                                                         /* snprintf(temp_string, sizeof(temp_string), "%c%d%c%s <%s>%c", start_text_ascii, contact_index, start_text_ascii, contact_display_name, email_address, end_text_ascii); */
1255                                                         if (addr->next == NULL) {
1256                                                                 snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>", contact_display_name, email_address);
1257                                                         }
1258                                                         else {
1259                                                                 snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>, ", contact_display_name, email_address);
1260                                                         }                                       
1261
1262                                                         contact_display_name_len = strlen(temp_string);
1263                                                         if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) {  /*  re-alloc memory */
1264                                                                 char *temp = contact_name;
1265                                                                 contact_name_buffer_size += contact_name_buffer_size;
1266                                                                 contact_name = (char  *)calloc(1, contact_name_buffer_size); 
1267                                                                 if (contact_name == NULL) {
1268                                                                         EM_DEBUG_EXCEPTION("Memory allocation failed.");
1269                                                                         EM_SAFE_FREE(temp);
1270                                                                         err = EMF_ERROR_OUT_OF_MEMORY;
1271                                                                         goto FINISH_OFF;
1272                                                                 }
1273                                                                 snprintf(contact_name, contact_name_buffer_size, "%s", temp);
1274                                                                 EM_SAFE_FREE(temp);
1275                                                         }
1276                                                         snprintf(contact_name + contact_name_len, contact_name_buffer_size - contact_name_len, "%s", temp_string);
1277                                                         contact_name_len += contact_display_name_len;
1278                                                         EM_DEBUG_LOG("new contact_name >>>>> %s ", contact_name);
1279                                                 }
1280                                         }
1281                                 }
1282                                 else  {
1283                                                 EM_DEBUG_LOG("contacts_svc_struct_get_value error[%d]", err);
1284                                 }
1285                         }
1286                         else {
1287                                 EM_DEBUG_LOG("contacts_svc_get_contact error [%d]", err);
1288                         }
1289                 }
1290                 else {
1291                         EM_DEBUG_LOG("contacts_svc_find_contact_by - Not found contact record(if err is 203) or error [%d]", err);
1292                 }
1293
1294                 /*  if contact doesn't exist, use alias or email address as display name */
1295                 if (addr->personal != NULL) {
1296                         /*  "%2C" -> ',' */
1297                         tokens = g_strsplit(addr->personal, "%2C", -1);
1298                         
1299                         EM_SAFE_FREE(addr->personal);
1300                         
1301                         addr->personal = g_strjoinv(", ", tokens);
1302                         
1303                         g_strfreev(tokens);
1304                         /* contact_info->contact_name = EM_SAFE_STRDUP(addr->personal); */
1305                         alias = addr->personal;
1306                 }
1307                 else {
1308                         /* alias = addr->mailbox ? addr->mailbox  :  ""; */
1309                         alias = email_address;
1310                 }
1311                 contact_info->alias = EM_SAFE_STRDUP(alias);
1312
1313                 if (!is_searched) {
1314                         contact_display_name = alias;
1315                         contact_info->contact_id = -1;          /*  NOTE :  This is valid only if there is only one address. */
1316                         contact_info->storage_type = -1;
1317
1318                         /*  Make display name string */
1319                         EM_DEBUG_LOG("contact_display_name : [%s]", contact_display_name);
1320                         EM_DEBUG_LOG("email_address : [%s]", email_address);
1321
1322                         /*  if mail_id is 0, return only contact info without saving contact info to DB */
1323                         if (mail_id == 0 || (contact_name_len == 0))             {      /*  save only the first address information - 09-SEP-2010 */
1324                                 /* snprintf(temp_string, sizeof(temp_string), "%c%d%c%s <%s>%c", start_text_ascii, contact_index, start_text_ascii, contact_display_name, email_address, end_text_ascii); */
1325                                 if (addr->next == NULL) {
1326                                         snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>", contact_display_name, email_address);
1327                                 }
1328                                 else {
1329                                         snprintf(temp_string, sizeof(temp_string), "\"%s\" <%s>, ", contact_display_name, email_address);
1330                                 }
1331                                 EM_DEBUG_LOG("temp_string[%s]", temp_string);
1332
1333                                 contact_display_name_len = strlen(temp_string);
1334                                 if (contact_name_len + contact_display_name_len >= contact_name_buffer_size) {  /*  re-alloc memory */
1335                                         char *temp = contact_name;
1336                                         contact_name_buffer_size += contact_name_buffer_size;
1337                                         contact_name = (char  *)calloc(1, contact_name_buffer_size); 
1338                                         if (contact_name == NULL) {
1339                                                 EM_DEBUG_EXCEPTION("Memory allocation failed.");
1340                                                 EM_SAFE_FREE(temp);
1341                                                 err = EMF_ERROR_OUT_OF_MEMORY;
1342                                                 goto FINISH_OFF;
1343                                         }
1344                                         snprintf(contact_name, contact_name_buffer_size, "%s", temp);
1345                                         EM_SAFE_FREE(temp);
1346                                 }
1347                                 
1348                                 snprintf(contact_name + contact_name_len, contact_name_buffer_size - contact_name_len, "%s", temp_string);
1349                                 contact_name_len += contact_display_name_len;
1350                                 EM_DEBUG_LOG("new contact_name >>>>> %s ", contact_name);
1351                         }
1352                 }
1353
1354                 if (temp_emailaddr && email_address) {  
1355                         if (mail_id == 0) {     /*  if mail_id is 0, return only contact info without saving contact info to DB */
1356                                 /* snprintf(temp_emailaddr, 400, "%s", contact_info->email_address); */
1357                                 EM_SAFE_STRCAT(temp_emailaddr, email_address);
1358                                 if (addr->next != NULL)
1359                                         EM_SAFE_STRCAT(temp_emailaddr, ", ");
1360                                 EM_DEBUG_LOG(">>>> TEMP EMail Address [ %s ] ", temp_emailaddr);
1361                         }
1362                         else {  /*  save only the first address information - 09-SEP-2010 */
1363                                 if (is_saved == 0) {
1364                                         is_saved = 1;
1365                                         /* snprintf(temp_emailaddr, 400, "%s", contact_info->email_address); */
1366                                         EM_SAFE_STRCAT(temp_emailaddr, email_address);
1367                                         /*
1368                                         if (addr->next != NULL)
1369                                                 EM_SAFE_STRCAT(temp_emailaddr, ", ");
1370                                         */
1371                                         EM_DEBUG_LOG(">>>> TEMP EMail Address [ %s ] ", temp_emailaddr);
1372                                 }
1373                         }
1374                 }
1375
1376                 if (contact != NULL) {
1377                         contacts_svc_struct_free(contact);
1378                         contact = NULL;
1379                 }
1380                 EM_SAFE_FREE(contact_display_name_from_contact_info);
1381                 /*  next address */
1382                 addr = addr->next;
1383         } /*  while (addr != NULL) */
1384
1385         if (temp_emailaddr) {
1386                 EM_DEBUG_LOG(">>>> TEMPEMAIL ADDR [ %s ] ", temp_emailaddr);
1387                 contact_info->email_address = temp_emailaddr;
1388                 temp_emailaddr = NULL;
1389         }
1390
1391         if (contact_name != NULL) {
1392                 contact_info->contact_name = g_strdup(contact_name);
1393         }
1394         else {
1395                 contact_info->contact_name = g_strdup_printf("%c%d%c%s%c", start_text_ascii, 0, start_text_ascii, full_address, end_text_ascii);
1396                 contact_info->contact_id = -1;                  
1397         }
1398
1399         ret = true;
1400         
1401 FINISH_OFF: 
1402
1403         if (contact != NULL)
1404                 contacts_svc_struct_free(contact);
1405         EM_SAFE_FREE(email_address);
1406         EM_SAFE_FREE(address);
1407         EM_SAFE_FREE(temp_emailaddr);
1408         EM_SAFE_FREE(contact_name);             
1409         EM_SAFE_FREE(contact_display_name_from_contact_info);
1410         
1411         if (err_code != NULL)
1412                 *err_code = err;
1413         
1414         return ret;
1415 }
1416
1417 int emcore_free_contact_info(emf_mail_contact_info_t *contact_info, int *err_code)
1418 {
1419         EM_DEBUG_FUNC_BEGIN("contact_info[%p], err_code[%p]", contact_info, err_code);
1420         
1421         int ret = false;
1422         int err = EMF_ERROR_NONE;
1423         
1424         if (!contact_info)  {
1425                 EM_DEBUG_EXCEPTION("contact_info[%p]", contact_info);
1426                 err = EMF_ERROR_INVALID_PARAM;
1427                 goto FINISH_OFF;
1428         }
1429         
1430         EM_SAFE_FREE(contact_info->contact_name);
1431         EM_SAFE_FREE(contact_info->email_address);
1432         EM_SAFE_FREE(contact_info->alias);
1433         
1434         contact_info->storage_type = -1;
1435         contact_info->contact_id = -1;
1436         
1437         ret = true;
1438         
1439 FINISH_OFF: 
1440         if (err_code != NULL)
1441                 *err_code = err;
1442         EM_DEBUG_FUNC_END();
1443         return ret;
1444 }
1445
1446 int emcore_sync_contact_info(int mail_id, int *err_code)
1447 {
1448         EM_PROFILE_BEGIN(emCoreMailContactSync);
1449         EM_DEBUG_FUNC_BEGIN();
1450
1451         int ret = false;
1452         int err = EMF_ERROR_NONE;
1453
1454         emstorage_mail_tbl_t *mail = NULL;
1455
1456         emf_mail_contact_info_t contact_info_from;
1457         emf_mail_contact_info_t contact_info_to;
1458         emf_mail_contact_info_t contact_info_cc;
1459         emf_mail_contact_info_t contact_info_bcc;
1460         
1461         EM_DEBUG_LOG("mail_id[%d], err_code[%p]", mail_id, err_code);
1462         
1463         memset(&contact_info_from, 0x00, sizeof(emf_mail_contact_info_t));      
1464         memset(&contact_info_to, 0x00, sizeof(emf_mail_contact_info_t));
1465         memset(&contact_info_cc, 0x00, sizeof(emf_mail_contact_info_t));
1466         memset(&contact_info_bcc, 0x00, sizeof(emf_mail_contact_info_t));       
1467
1468         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
1469                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1470
1471                 goto FINISH_OFF;
1472         }
1473
1474         if (mail->full_address_from != NULL) {
1475                 if (!emcore_get_mail_contact_info_with_update(&contact_info_from, mail->full_address_from, mail_id, &err))  {
1476                         EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1477                 }
1478         }
1479
1480         if (mail->full_address_to != NULL)  {
1481                 if (!emcore_get_mail_contact_info_with_update(&contact_info_to, mail->full_address_to, mail_id, &err))  {
1482                         EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1483                 }
1484         }
1485
1486         if (mail->full_address_cc != NULL)  {
1487                 if (!emcore_get_mail_contact_info_with_update(&contact_info_cc, mail->full_address_cc, mail_id, &err))  {
1488                         EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1489                 }
1490         }
1491
1492         if (mail->full_address_bcc != NULL)  {
1493                 if (!emcore_get_mail_contact_info_with_update(&contact_info_bcc, mail->full_address_bcc, mail_id, &err))  {
1494                         EM_DEBUG_EXCEPTION("emcore_get_mail_contact_info failed [%d]", err);
1495                 }
1496         }       
1497
1498         EM_SAFE_FREE(mail->email_address_sender);
1499         mail->email_address_sender = contact_info_from.email_address;
1500         contact_info_from.contact_name = NULL;
1501         contact_info_from.email_address = NULL;
1502         EM_SAFE_FREE(mail->email_address_recipient);
1503         if (mail->full_address_to != NULL) {
1504                 mail->email_address_recipient = contact_info_to.email_address;
1505                 contact_info_to.contact_name = NULL;
1506                 contact_info_to.email_address = NULL;
1507         }
1508         else if (mail->full_address_cc != NULL) {
1509                 mail->email_address_recipient = contact_info_cc.email_address;
1510                 contact_info_cc.contact_name = NULL;
1511                 contact_info_cc.email_address = NULL;
1512         }
1513         else if (mail->full_address_bcc != NULL) {
1514                 mail->email_address_recipient = contact_info_bcc.email_address;
1515                 contact_info_bcc.contact_name  = NULL;
1516                 contact_info_bcc.email_address = NULL;
1517         }
1518         
1519         /*  Update DB */
1520         if (!emstorage_change_mail_field(mail_id, UPDATE_ALL_CONTACT_INFO, mail, false, &err))  {                                       
1521                 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
1522
1523                 goto FINISH_OFF;
1524         }       
1525
1526         ret = true;
1527
1528 FINISH_OFF: 
1529         if (mail != NULL)
1530                 emstorage_free_mail(&mail, 1, NULL);
1531
1532         emcore_free_contact_info(&contact_info_from, NULL);
1533         emcore_free_contact_info(&contact_info_to, NULL);
1534         emcore_free_contact_info(&contact_info_cc, NULL);
1535         emcore_free_contact_info(&contact_info_bcc, NULL);      
1536
1537         if (err_code != NULL)
1538                 *err_code = err;
1539
1540         EM_PROFILE_END(emCoreMailContactSync);
1541         return ret;
1542 }
1543
1544 /*  1. parsing  :  alias and address */
1545 /*  2. sync with contact */
1546 /*  3. make glist of address info */
1547 static int emcore_sync_address_info(emf_address_type_t address_type, char *full_address, GList **address_info_list, int *err_code)
1548 {
1549         EM_DEBUG_FUNC_BEGIN("address type[%d], address_info_list[%p], full_address[%p]", address_type, address_info_list, full_address);
1550
1551         int ret = false;
1552         int error = EMF_ERROR_NONE;
1553         int contact_index = -1;
1554         int is_search = false;
1555         char *alias = NULL;
1556         char *address = NULL;
1557         char *contact_display_name_from_contact_info = NULL;
1558         char email_address[MAX_EMAIL_ADDRESS_LENGTH];
1559         emf_address_info_t *p_address_info = NULL;
1560         ADDRESS *addr = NULL;
1561         CTSstruct *contact =  NULL;
1562         CTSvalue *contact_name_value = NULL;
1563         
1564         if (full_address == NULL || address_info_list == NULL) {
1565                 EM_DEBUG_EXCEPTION("Invalid param :  full_address or address_info_list is NULL");
1566                 error = EMF_ERROR_INVALID_PARAM;
1567                 goto FINISH_OFF;
1568         }
1569
1570         /*  Parsing */
1571         address = EM_SAFE_STRDUP(full_address);
1572
1573         /*  ',' -> "%2C" */
1574         gchar **tokens = g_strsplit(address, ", ", -1);
1575         char *p = g_strjoinv("%2C", tokens);
1576         int i = 0;
1577         
1578         g_strfreev(tokens);
1579         
1580         /*  ';' -> ',' */
1581         while (p && p[i] != '\0')  {
1582                 if (p[i] == ';') 
1583                         p[i] = ',';
1584                 i++;
1585         }
1586
1587         rfc822_parse_adrlist(&addr, p, NULL);
1588
1589         EM_SAFE_FREE(p);        
1590
1591         if (!addr)  {
1592                 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
1593                 error = EMF_ERROR_INVALID_PARAM;                
1594                 goto FINISH_OFF;
1595         }
1596
1597         /*  Get a contact name */
1598         while (addr != NULL)  {
1599                 if (addr->mailbox && addr->host) {      
1600                         if (!strcmp(addr->mailbox , "UNEXPECTED_DATA_AFTER_ADDRESS") || !strcmp(addr->mailbox , "INVALID_ADDRESS") || !strcmp(addr->host , ".SYNTAX-ERROR."))
1601                 {
1602                                 EM_DEBUG_LOG("Invalid address ");
1603                                 addr = addr->next;
1604                                 continue;
1605                         }
1606                 }
1607                 else  {
1608                         EM_DEBUG_LOG("Error in parsing..! ");
1609                         addr = addr->next;
1610                         continue;
1611                 }
1612
1613                 /*   set display name  */
1614                 /*    1) contact name */
1615                 /*    2) alias (if a alias in an original mail doesn't exist, this field is set with email address */
1616                 /*    3) email address   */
1617
1618                 if (!(p_address_info = (emf_address_info_t *)malloc(sizeof(emf_address_info_t)))) {
1619                         EM_DEBUG_EXCEPTION("malloc failed...");
1620                         error = EMF_ERROR_OUT_OF_MEMORY;
1621                         goto FINISH_OFF;
1622                 }       
1623                 memset(p_address_info, 0x00, sizeof(emf_address_info_t)); 
1624
1625                 SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox ? addr->mailbox  :  "", addr->host ? addr->host  :  "");
1626  
1627                 EM_DEBUG_LOG("Search a contact  :  address[%s]", email_address);
1628
1629                 is_search = false;
1630
1631                 error = contacts_svc_find_contact_by(CTS_FIND_BY_EMAIL, email_address);
1632                 if (error > CTS_SUCCESS) {
1633                         contact_index = error;
1634                         if ((error = contacts_svc_get_contact(contact_index, &contact)) == CTS_SUCCESS) {
1635                                 /*  get  contact name */
1636                                 if (contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &contact_name_value) == CTS_SUCCESS) {    /*  set contact display name name */
1637                                         emcore_get_mail_display_name(contact_name_value, &contact_display_name_from_contact_info);
1638                                         EM_DEBUG_LOG(">>> contact index[%d]", contact_index);
1639                                         EM_DEBUG_LOG(">>> contact display name[%s]", contact_display_name_from_contact_info);
1640
1641                                         is_search = true;
1642                                 }
1643                                 else
1644                                         EM_DEBUG_EXCEPTION("contacts_svc_struct_get_value error[%d]", error);
1645                         }
1646                         else
1647                                 EM_DEBUG_EXCEPTION("contacts_svc_get_contact error [%d]", error);
1648                 }
1649                 else
1650                         EM_DEBUG_EXCEPTION("contacts_svc_find_contact_by - Not found contact record(if err is -203) or error [%d]", error);
1651
1652                 if (is_search == true) {
1653                         p_address_info->contact_id = contact_index;             
1654                         p_address_info->storage_type = -1;                              
1655                         p_address_info->display_name = contact_display_name_from_contact_info;
1656                         EM_DEBUG_LOG("display_name from contact[%s]", p_address_info->display_name);
1657                 }
1658                 else {
1659                         /*  if contact doesn't exist, use alias or email address as display name */
1660                         if (addr->personal != NULL) {
1661                                 /*  "%2C" -> ',' */
1662                                 tokens = g_strsplit(addr->personal, "%2C", -1);
1663                                 
1664                                 EM_SAFE_FREE(addr->personal);
1665                                 
1666                                 addr->personal = g_strjoinv(", ", tokens);
1667                                 
1668                                 g_strfreev(tokens);
1669                                 alias = addr->personal;
1670                         }
1671                         else {
1672                                 alias = NULL;
1673                         }               
1674                         p_address_info->contact_id = -1;
1675                         p_address_info->storage_type = -1;      
1676                         /*  Use an alias or an email address as a display name */
1677                         if (alias == NULL)
1678                                 p_address_info->display_name = EM_SAFE_STRDUP(email_address);
1679                         else
1680                                 p_address_info->display_name = EM_SAFE_STRDUP(alias);
1681
1682                         EM_DEBUG_LOG("display_name from email [%s]", p_address_info->display_name);
1683                 }
1684                 
1685                 p_address_info->address = EM_SAFE_STRDUP(email_address);
1686                 p_address_info->address_type = address_type;
1687
1688                 EM_DEBUG_LOG("email address[%s]", p_address_info->address);
1689
1690                 *address_info_list = g_list_append(*address_info_list, p_address_info);
1691                 p_address_info = NULL;
1692
1693                 EM_DEBUG_LOG("after append");
1694
1695                 alias = NULL;
1696                 if (contact != NULL) {
1697                         contacts_svc_struct_free(contact);
1698                         contact = NULL;
1699                 }
1700
1701                 EM_DEBUG_LOG("next address[%p]", addr->next);
1702
1703                 /*  next address */
1704                 addr = addr->next;
1705         }
1706
1707         ret = true;
1708         
1709 FINISH_OFF: 
1710         if (contact != NULL)
1711                 contacts_svc_struct_free(contact);
1712
1713         EM_SAFE_FREE(address);
1714         
1715         if (err_code != NULL)
1716                 *err_code = error;      
1717         EM_DEBUG_FUNC_END();
1718         return ret;
1719 }
1720
1721 static gint address_compare(gconstpointer a, gconstpointer b)
1722 {
1723         EM_DEBUG_FUNC_BEGIN();
1724         emf_sender_list_t *recipients_list1 = (emf_sender_list_t *)a;
1725         emf_sender_list_t *recipients_list2 = (emf_sender_list_t *)b;   
1726
1727         EM_DEBUG_FUNC_END();    
1728         return strcmp(recipients_list1->address, recipients_list2->address);
1729 }
1730
1731 INTERNAL_FUNC GList *emcore_get_recipients_list(GList *old_recipients_list, char *full_address, int *err_code)
1732 {
1733         EM_DEBUG_FUNC_BEGIN();
1734
1735         int i = 0, err = EMF_ERROR_NONE;
1736         int contact_index = -1;
1737         int is_search = false;
1738         char *address = NULL;
1739         char email_address[MAX_EMAIL_ADDRESS_LENGTH];
1740         char *display_name = NULL;
1741         char *alias = NULL;
1742         ADDRESS *addr = NULL;
1743         CTSstruct *contact = NULL;
1744         CTSvalue *contact_name_value = NULL;
1745         GList *new_recipients_list = old_recipients_list;
1746         GList *recipients_list;
1747
1748         emf_sender_list_t *temp_recipients_list = NULL;
1749         emf_sender_list_t *old_recipients_list_t = NULL;
1750         
1751         if (full_address == NULL || strlen(full_address) == 0) {
1752                 EM_DEBUG_EXCEPTION("Invalid param : full_address NULL or empty");
1753                 err = EMF_ERROR_INVALID_PARAM;
1754                 goto FINISH_OFF;
1755         }
1756
1757         address = EM_SAFE_STRDUP(full_address);
1758
1759         gchar **tokens = g_strsplit(address, ", ", -1);
1760         char *p = g_strjoinv("%2C", tokens);
1761
1762         g_strfreev(tokens);
1763
1764         while (p && p[i] != '\0') {
1765                 if (p[i] == ';') 
1766                         p[i] = ',';
1767                 i++;
1768         }
1769
1770         rfc822_parse_adrlist(&addr, p, NULL);
1771
1772         EM_SAFE_FREE(p);
1773
1774         if (!addr) {
1775                 EM_DEBUG_EXCEPTION("rfc822_parse_adrlist failed...");
1776                 err = EMF_ERROR_INVALID_PARAM;
1777                 goto FINISH_OFF;
1778         }
1779
1780         while (addr != NULL) {
1781                 if (addr->mailbox && addr->host) {
1782                         if (!strcmp(addr->mailbox , "UNEXPECTED_DATA_AFTER_ADDRESS") || !strcmp(addr->mailbox , "INVALID_ADDRESS") || !strcmp(addr->host , ".SYNTAX-ERROR.")) {
1783                                 EM_DEBUG_LOG("Invalid address ");
1784                                 addr = addr->next;
1785                                 continue;
1786                         } 
1787                 } else {
1788                         EM_DEBUG_LOG("Error in parsing..! ");
1789                         addr = addr->next;
1790                         continue;
1791                 }                       
1792         
1793                 temp_recipients_list = g_new0(emf_sender_list_t, 1);
1794                 
1795                 SNPRINTF(email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", addr->mailbox ? addr->mailbox : "", addr->host ? addr->host : "");
1796
1797                 EM_DEBUG_LOG("Search a contact : address[%s]", email_address);
1798
1799                 err = contacts_svc_find_contact_by(CTS_FIND_BY_EMAIL, email_address);
1800                 if (err > CTS_SUCCESS) {
1801                         contact_index = err;
1802                         if ((err = contacts_svc_get_contact(contact_index, &contact)) == CTS_SUCCESS) {
1803                                 if (contacts_svc_struct_get_value(contact, CTS_CF_NAME_VALUE, &contact_name_value) == CTS_SUCCESS) {
1804                                         emcore_get_mail_display_name(contact_name_value, &display_name);
1805                                         EM_DEBUG_LOG(">>> contact index[%d]", contact_index);
1806                                         EM_DEBUG_LOG(">>> contact display name[%s]", display_name);
1807
1808                                         is_search = true;
1809                                 } else {
1810                                                 EM_DEBUG_LOG("contacts_svc_struct_get_value error[%d]", err);
1811                                 }
1812                         } else {
1813                                 EM_DEBUG_LOG("contacts_svc_get_contact error [%d]", err);
1814                         }
1815                 } else {
1816                         EM_DEBUG_LOG("contacts_svc_find_contact_by - Not found contact record(if err is -203) or error [%d]", err);
1817                 }
1818
1819                 if (is_search) {
1820                         temp_recipients_list->display_name = display_name;
1821                         EM_DEBUG_LOG("display_name from contact[%s]", temp_recipients_list->display_name);
1822                 } else {
1823                         if (addr->personal != NULL) {
1824                                 tokens = g_strsplit(addr->personal, "%2C", -1);
1825                                 EM_SAFE_FREE(addr->personal);
1826                                 addr->personal = g_strjoinv(", ", tokens);
1827                                 g_strfreev(tokens);
1828                                 alias = addr->personal;
1829                         } else {
1830                                 alias = NULL;
1831                         }
1832
1833                         if (alias == NULL)
1834                                 temp_recipients_list->display_name = EM_SAFE_STRDUP(email_address);
1835                         else
1836                                 temp_recipients_list->display_name = EM_SAFE_STRDUP(alias);
1837
1838                         EM_DEBUG_LOG("display_name from contact[%s]", temp_recipients_list->display_name);
1839                 }
1840
1841                 temp_recipients_list->address = EM_SAFE_STRDUP(email_address);
1842                 EM_DEBUG_LOG("email address[%s]", temp_recipients_list->address);
1843
1844                 EM_SAFE_FREE(display_name);
1845                 if (contact != NULL) {
1846                         contacts_svc_struct_free(contact);
1847                         contact = NULL;
1848                 }
1849                 EM_DEBUG_LOG("next address[%p]", addr->next);
1850
1851                 recipients_list = g_list_first(new_recipients_list);
1852                 while (recipients_list != NULL) {
1853                         old_recipients_list_t = (emf_sender_list_t *)recipients_list->data;
1854                         if (!strcmp(old_recipients_list_t->address, temp_recipients_list->address)) {
1855                                 old_recipients_list_t->total_count = old_recipients_list_t->total_count + 1;
1856                                 if (temp_recipients_list != NULL)
1857                                         g_free(temp_recipients_list);
1858                                 
1859                                 goto FINISH_OFF;
1860                         }
1861                         recipients_list = g_list_next(recipients_list);
1862                 }
1863
1864                 new_recipients_list = g_list_insert_sorted(new_recipients_list, temp_recipients_list, address_compare);
1865
1866                 temp_recipients_list = NULL;
1867
1868                 alias = NULL;
1869                 if (contact != NULL) {
1870                         contacts_svc_struct_free(contact);
1871                         contact = NULL;
1872                 }
1873                 addr = addr->next;
1874         }
1875
1876 FINISH_OFF:
1877
1878         if (contact != NULL)
1879                 contacts_svc_struct_free(contact);
1880
1881         EM_SAFE_FREE(address);
1882
1883         if (err_code != NULL)
1884                 *err_code = err;
1885
1886         EM_DEBUG_FUNC_END();
1887         return new_recipients_list;                     
1888 }
1889
1890 INTERNAL_FUNC int emcore_get_mail_address_info_list(int mail_id, emf_address_info_list_t **address_info_list, int *err_code)
1891 {
1892         EM_DEBUG_FUNC_BEGIN("mail_id[%d], address_info_list[%p]", mail_id, address_info_list);
1893
1894         int ret = false, err = EMF_ERROR_NONE;
1895         int failed = true;
1896         int contact_error;
1897
1898         emstorage_mail_tbl_t *mail = NULL;
1899         emf_address_info_list_t *p_address_info_list = NULL;
1900
1901         if (mail_id <= 0 || !address_info_list) {
1902                 EM_DEBUG_EXCEPTION("mail_id[%d], address_info_list[%p]", mail_id, address_info_list);
1903                 err = EMF_ERROR_INVALID_PARAM;
1904                 goto FINISH_OFF;
1905         }
1906         
1907         /* get mail from mail table */
1908         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
1909                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
1910                 
1911
1912                 goto FINISH_OFF;
1913         }
1914
1915         if (!(p_address_info_list = (emf_address_info_list_t *)malloc(sizeof(emf_address_info_list_t)))) {
1916                 EM_DEBUG_EXCEPTION("malloc failed...");
1917                 err = EMF_ERROR_OUT_OF_MEMORY;
1918                 goto FINISH_OFF;
1919         }       
1920         memset(p_address_info_list, 0x00, sizeof(emf_address_info_list_t));     
1921
1922         if ((contact_error = contacts_svc_connect()) == CTS_SUCCESS)     {              
1923                 EM_DEBUG_LOG("Open Contact Service Success");   
1924         }       
1925         else     {              
1926                 EM_DEBUG_EXCEPTION("contact_db_service_connect failed [%d]", contact_error);            
1927                 err = EMF_ERROR_DB_FAILURE;
1928                 goto FINISH_OFF;
1929         }
1930
1931         if (emcore_sync_address_info(EMF_ADDRESS_TYPE_FROM, mail->full_address_from, &p_address_info_list->from, &err))
1932                 failed = false;
1933         if (emcore_sync_address_info(EMF_ADDRESS_TYPE_TO, mail->full_address_to, &p_address_info_list->to, &err))
1934                 failed = false;
1935         if (emcore_sync_address_info(EMF_ADDRESS_TYPE_CC, mail->full_address_cc, &p_address_info_list->cc, &err))
1936                 failed = false;
1937         if (emcore_sync_address_info(EMF_ADDRESS_TYPE_BCC, mail->full_address_bcc, &p_address_info_list->bcc, &err))
1938                 failed = false;
1939
1940         if ((contact_error = contacts_svc_disconnect()) == CTS_SUCCESS)
1941                 EM_DEBUG_LOG("Close Contact Service Success");
1942         else
1943                 EM_DEBUG_EXCEPTION("Close Contact Service Fail [%d]", contact_error);
1944
1945         if (failed == false)
1946                 ret = true;
1947
1948 FINISH_OFF: 
1949         if (ret == true) 
1950                 *address_info_list = p_address_info_list;
1951         else if (p_address_info_list != NULL)
1952                 emstorage_free_address_info_list(&p_address_info_list);
1953
1954         if (!mail)
1955                 emstorage_free_mail(&mail, 1, NULL);
1956
1957         if (err_code != NULL)
1958                 *err_code = err;
1959
1960         EM_DEBUG_FUNC_END();
1961         return ret;
1962 }
1963
1964 /* description
1965  *    get a mail data
1966  * arguments  
1967  *    input_mail_id     : [in]  mail id
1968  *    output_mail_data  : [out] double pointer to hold mail data.
1969  * return  
1970  *    succeed  :  EMF_ERROR_NONE
1971  *    fail     :  error code
1972  */
1973 INTERNAL_FUNC int emcore_get_mail_data(int input_mail_id, emf_mail_data_t **output_mail_data)
1974 {
1975         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], output_mail_data[%p]", input_mail_id, output_mail_data);
1976         
1977         int             error = EMF_ERROR_NONE;
1978         int             result_mail_count = 0;
1979         char            conditional_clause_string[QUERY_SIZE] = { 0, };
1980         emstorage_mail_tbl_t *result_mail_tbl = NULL;
1981         
1982         if (input_mail_id == 0 || !output_mail_data)  {
1983                 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
1984                 error = EMF_ERROR_INVALID_PARAM;
1985                 goto FINISH_OFF;
1986         }
1987
1988         SNPRINTF(conditional_clause_string, QUERY_SIZE, "WHERE mail_id = %d", input_mail_id);
1989         
1990         if(!emstorage_query_mail_tbl(conditional_clause_string, true, &result_mail_tbl, &result_mail_count, &error)) {
1991                 EM_DEBUG_EXCEPTION("emstorage_query_mail_tbl falied [%d]", error);
1992                 goto FINISH_OFF;
1993         }
1994
1995         if(!em_convert_mail_tbl_to_mail_data(result_mail_tbl, 1, output_mail_data, &error)) {
1996                 EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_data falied [%d]", error);
1997                 goto FINISH_OFF;
1998         }
1999         
2000 FINISH_OFF: 
2001         if (result_mail_tbl)
2002                 emstorage_free_mail(&result_mail_tbl, result_mail_count, NULL);
2003
2004         EM_DEBUG_FUNC_END("error [%d]", error);
2005         return error;
2006 }
2007
2008
2009 /* internal function */
2010 void emcore_free_body_sharep(void **p)
2011 {
2012         EM_DEBUG_FUNC_BEGIN();
2013         EM_SAFE_FREE(*p);
2014         EM_DEBUG_FUNC_END();
2015 }
2016
2017 int emcore_check_drm(emstorage_attachment_tbl_t *input_attachment_tb_data)
2018 {
2019         EM_DEBUG_FUNC_BEGIN();
2020         int ret = 0;
2021 #ifdef __FEATURE_DRM__
2022         drm_dcf_info_t  fileInfo;
2023
2024         if (input_attachment_tb_data == NULL)
2025                 return ret;
2026
2027         if (drm_svc_is_drm_file (input_attachment_tb_data->attachment_path)) {
2028                 if (drm_svc_get_dcf_file_info (input_attachment_tb_data->attachment_path, &fileInfo) == DRM_RESULT_SUCCESS) {
2029                         input_attachment_tb_data->attachment_drm_type = 0;
2030                         EM_DEBUG_LOG ("fileInfo is [%d]", fileInfo.method);
2031                         if (fileInfo.method != DRM_METHOD_UNDEFINED) {
2032                                 input_attachment_tb_data->attachment_drm_type = fileInfo.method;
2033                                 ret = 1;
2034                         }
2035                 }
2036         }
2037         else {
2038                 EM_DEBUG_LOG("not DRM file %s", input_attachment_tb_data->attachment_path);
2039                 input_attachment_tb_data->attachment_drm_type = 0;
2040                 ret = 0;
2041         }
2042 #endif
2043         EM_DEBUG_FUNC_END();
2044         return ret;
2045 }
2046
2047
2048 /* description
2049  *    get mail attachment from local mailbox
2050  * arguments  
2051  *    mailbox  :  server mailbox
2052  *    mail_id  :  mai id to own attachment
2053  *    attachment  :  the number string to be downloaded
2054  *    callback  :  function callback. if NULL, ignored.
2055  * return  
2056  *     succeed  :  1
2057  *     fail  :  0
2058  */
2059 INTERNAL_FUNC int emcore_get_attachment_info(int mail_id, char *attachment_id_string, emf_attachment_info_t **attachment, int *err_code)
2060 {
2061         EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment_id_string[%p], attachment[%p], err_code[%p]", mail_id, attachment_id_string, attachment, err_code);
2062
2063         if (attachment == NULL || attachment_id_string == NULL)  {
2064                 EM_DEBUG_EXCEPTION("mail_id[%d], attachment_id_string[%p], attachment[%p]", mail_id, attachment_id_string, attachment);
2065                 if (err_code != NULL)
2066                         *err_code = EMF_ERROR_INVALID_PARAM;
2067                 return false;
2068         }
2069         
2070         int ret = false;
2071         int err = EMF_ERROR_NONE;
2072         emstorage_attachment_tbl_t *attachment_tbl = NULL;
2073         int attachment_id = atoi(attachment_id_string);
2074         
2075         /* get attachment from attachment tbl */
2076         if (!emstorage_get_attachment(mail_id, attachment_id, &attachment_tbl, true, &err) || !attachment_tbl)  {
2077                 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
2078
2079                 goto FINISH_OFF;
2080         }
2081         
2082         *attachment = em_malloc(sizeof(emf_attachment_info_t));
2083         if (!*attachment)  {
2084                 EM_DEBUG_EXCEPTION("malloc failed...");
2085                 err = EMF_ERROR_OUT_OF_MEMORY;
2086                 goto FINISH_OFF;
2087         }
2088         
2089         (*attachment)->attachment_id = attachment_id;
2090         (*attachment)->name = attachment_tbl->attachment_name; attachment_tbl->attachment_name = NULL;
2091         (*attachment)->size = attachment_tbl->attachment_size;
2092         (*attachment)->downloaded = attachment_tbl->attachment_save_status;
2093         (*attachment)->savename = attachment_tbl->attachment_path; attachment_tbl->attachment_path = NULL;
2094         (*attachment)->drm = attachment_tbl->attachment_drm_type;
2095         (*attachment)->inline_content = attachment_tbl->attachment_inline_content_status;
2096         (*attachment)->next = NULL;
2097
2098         ret = true;
2099
2100 FINISH_OFF: 
2101         if (attachment_tbl)
2102                 emstorage_free_attachment(&attachment_tbl, 1, NULL);
2103
2104         if (err_code)
2105                 *err_code = err;
2106         
2107         return ret;
2108 }
2109
2110 /* description
2111  *    get mail attachment
2112  * arguments  
2113  *    input_mail_id           :  mail id to own attachment
2114  *    output_attachment_data  :  result attahchment data
2115  *    output_attachment_count :  result attahchment count
2116  * return  
2117  *     succeed : EMF_ERROR_NONE
2118  *     fail    : error code
2119  */
2120 INTERNAL_FUNC int emcore_get_attachment_data_list(int input_mail_id, emf_attachment_data_t **output_attachment_data, int *output_attachment_count)
2121 {
2122         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], output_attachment_data[%p], output_attachment_count[%p]", input_mail_id, output_attachment_data, output_attachment_count);
2123         
2124         if (input_mail_id == 0|| output_attachment_data == NULL || output_attachment_count == NULL)  {
2125                 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
2126                 return EMF_ERROR_INVALID_PARAM;
2127         }
2128
2129         int                        i = 0;
2130         int                        err = EMF_ERROR_NONE;
2131         int                        attachment_tbl_count = 0;
2132         emstorage_attachment_tbl_t *attachment_tbl_list = NULL;
2133         emf_attachment_data_t     *temp_attachment_data = NULL;
2134         
2135         /* get attachment from attachment tbl */
2136         if ( (err = emstorage_get_attachment_list(input_mail_id, true, &attachment_tbl_list, &attachment_tbl_count)) != EMF_ERROR_NONE ){
2137                 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2138
2139                 goto FINISH_OFF;
2140         } 
2141
2142         if (attachment_tbl_count)  {
2143                 EM_DEBUG_LOG("attchment count %d", attachment_tbl_count);
2144
2145                 *output_attachment_data = em_malloc(sizeof(emf_attachment_data_t) * attachment_tbl_count);
2146
2147                 if(*output_attachment_data == NULL) {
2148                         EM_DEBUG_EXCEPTION("em_malloc failed");
2149                         err = EMF_ERROR_OUT_OF_MEMORY;
2150                         goto FINISH_OFF;
2151                 }
2152
2153                 for (i = 0; i < attachment_tbl_count; i++)  {
2154                         temp_attachment_data = (*output_attachment_data) + i;
2155
2156                         temp_attachment_data->attachment_id         = attachment_tbl_list[i].attachment_id;
2157                         temp_attachment_data->attachment_name       = attachment_tbl_list[i].attachment_name; attachment_tbl_list[i].attachment_name = NULL;
2158                         temp_attachment_data->attachment_path       = attachment_tbl_list[i].attachment_path; attachment_tbl_list[i].attachment_path = NULL;
2159                         temp_attachment_data->attachment_size       = attachment_tbl_list[i].attachment_size;
2160                         temp_attachment_data->mail_id               = attachment_tbl_list[i].mail_id;
2161                         temp_attachment_data->account_id            = attachment_tbl_list[i].account_id;
2162                         temp_attachment_data->mailbox_name          = attachment_tbl_list[i].mailbox_name; attachment_tbl_list[i].mailbox_name = NULL;
2163                         temp_attachment_data->save_status           = attachment_tbl_list[i].attachment_save_status;
2164                         temp_attachment_data->drm_status            = attachment_tbl_list[i].attachment_drm_type;
2165                         temp_attachment_data->inline_content_status = attachment_tbl_list[i].attachment_inline_content_status;
2166                 }
2167         }
2168         
2169 FINISH_OFF: 
2170         
2171         *output_attachment_count = attachment_tbl_count;
2172
2173         if (attachment_tbl_list)
2174                 emstorage_free_attachment(&attachment_tbl_list, attachment_tbl_count, NULL);
2175         
2176         return err;
2177 }
2178
2179
2180 INTERNAL_FUNC int emcore_download_attachment(int account_id, int mail_id, char *nth, int *err_code)
2181 {
2182         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%p], err_code[%p]", account_id, mail_id, nth, err_code);
2183
2184         int attachment_no = 0;
2185         int err = EMF_ERROR_NONE;
2186
2187         if (mail_id < 1 || !nth)  {
2188                 EM_DEBUG_EXCEPTION("mail_id[%d], nth[%p]",  mail_id, nth);
2189                 err = EMF_ERROR_INVALID_PARAM;
2190
2191                 if (err_code != NULL)
2192                         *err_code = err;
2193
2194                 if (nth)
2195                         attachment_no = atoi(nth);
2196
2197                 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, 0, attachment_no, err);
2198                 return false;
2199         }
2200
2201         int ret = false;
2202         int status = EMF_DOWNLOAD_FAIL;
2203         MAILSTREAM *stream = NULL;
2204         BODY *mbody = NULL;
2205         emstorage_mail_tbl_t *mail = NULL;
2206         emstorage_attachment_tbl_t *attachment = NULL;
2207         struct attachment_info *ai = NULL;
2208         struct _m_content_info *cnt_info = NULL;
2209         void *tmp_stream = NULL;
2210         char *s_uid = NULL, *server_mbox = NULL, buf[1024];
2211         int msg_no = 0;
2212         emstorage_attachment_tbl_t *attachment_list = NULL;
2213         int current_attachment_no = 0;
2214         int attachment_count_to_be_downloaded = 0;              /*  how many attachments should be downloaded */
2215         int i = 0;
2216
2217         if (!emcore_check_thread_status())  {
2218                 err = EMF_ERROR_CANCELLED;
2219                 goto FINISH_OFF;
2220         }
2221         
2222         only_body_download = false;
2223         
2224         /*  get mail from mail table. */
2225         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
2226                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2227
2228                 goto FINISH_OFF;
2229         }
2230
2231         if (!mail->server_mail_status)  {
2232                 EM_DEBUG_EXCEPTION("not synchronous mail...");
2233                 err = EMF_ERROR_INVALID_MAIL;
2234                 goto FINISH_OFF;
2235         }
2236         
2237         attachment_no = atoi(nth);
2238         if (attachment_no == 0) {       /*  download all attachments, nth starts from 1, not zero */
2239                 /*  get attachment list from db */
2240                 attachment_count_to_be_downloaded = EMF_ATTACHMENT_MAX_COUNT;
2241                 if ( (err = emstorage_get_attachment_list(mail_id, true, &attachment_list, &attachment_count_to_be_downloaded)) != EMF_ERROR_NONE ){
2242                         EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2243
2244                         goto FINISH_OFF;
2245                 }
2246         }
2247         else {  /*  download only nth attachment */
2248                 attachment_count_to_be_downloaded = 1;
2249                 if (!emstorage_get_attachment_nth(mail_id, attachment_no, &attachment_list, true, &err) || !attachment_list)  {
2250                         EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
2251
2252                         goto FINISH_OFF;
2253                 }
2254         }
2255         
2256         if (!emcore_check_thread_status())  {
2257                 err = EMF_ERROR_CANCELLED;
2258                 goto FINISH_OFF;
2259         }
2260         
2261         account_id              = mail->account_id;
2262         s_uid                   = EM_SAFE_STRDUP(mail->server_mail_id);
2263         server_mbox     = EM_SAFE_STRDUP(mail->mailbox_name);
2264
2265         /*  open mail server. */
2266         if (!emcore_connect_to_remote_mailbox(account_id, server_mbox, (void **)&tmp_stream, &err) || !tmp_stream)  {
2267                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2268                 status = EMF_DOWNLOAD_CONNECTION_FAIL;
2269                 goto FINISH_OFF;
2270         }
2271         
2272         stream = (MAILSTREAM *)tmp_stream;
2273         
2274         for (i = 0; i < attachment_count_to_be_downloaded; i++) {
2275                 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] start", i + 1, attachment_count_to_be_downloaded);
2276
2277                 attachment = attachment_list + i;
2278                 if (attachment_no == 0)                                         /*  download all attachments, nth starts from 1, not zero */
2279                         current_attachment_no = i + 1;                  /*  attachment no */
2280                 else                                                                            /*  download only nth attachment */
2281                         current_attachment_no = attachment_no;  /*  attachment no */
2282
2283                 if (!emcore_check_thread_status())  {
2284                         err = EMF_ERROR_CANCELLED;
2285                         goto FINISH_OFF;
2286                 }
2287                 
2288                 if (!(cnt_info = em_malloc(sizeof(struct _m_content_info))))  {
2289                         EM_DEBUG_EXCEPTION("malloc failed...");
2290                         err = EMF_ERROR_OUT_OF_MEMORY;
2291                         goto FINISH_OFF;
2292                 }
2293                 
2294                 cnt_info->grab_type = GRAB_TYPE_ATTACHMENT;     /*  attachment */
2295                 cnt_info->file_no   = current_attachment_no;    /*  attachment no */
2296
2297 #ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
2298                 /*  text/html be changed to attachment, this isn't real attachment in RFC822. */
2299                 if (html_changed)
2300                         cnt_info->file_no--;
2301 #endif
2302
2303                 /*  set sparep(member of BODY) memory free function. */
2304                 mail_parameters(stream, SET_FREEBODYSPAREP, emcore_free_body_sharep);
2305                 
2306                 if (!emcore_check_thread_status()) {
2307                         err = EMF_ERROR_CANCELLED;
2308                         goto FINISH_OFF;
2309                 }
2310
2311                 msg_no = atoi(s_uid);
2312
2313                 /*  get body structure. */
2314                 /*  don't free mbody because mbody is freed in closing mail_stream. */
2315                 if ((!stream) || emcore_get_body_structure(stream, msg_no, &mbody, &err) < 0)  {
2316                         EM_DEBUG_EXCEPTION("emcore_get_body_structure failed [%d]", err);
2317                         goto FINISH_OFF;
2318                 }
2319
2320                 if (!emcore_check_thread_status())  {
2321                         err = EMF_ERROR_CANCELLED;
2322                         goto FINISH_OFF;
2323                 }
2324
2325                 /*  set body fetch section. */
2326                 if (emcore_set_fetch_body_section(mbody, false, NULL,  &err) < 0) {
2327                         EM_DEBUG_EXCEPTION("emcore_set_fetch_body_section failed [%d]", err);
2328                         goto FINISH_OFF;
2329                 }
2330         
2331                 /*  download attachment. */
2332                 _imap4_received_body_size = 0;
2333                 _imap4_last_notified_body_size = 0;
2334                 _imap4_total_body_size = 0;                                     /*  This will be assigned in imap_mail_write_body_to_file() */
2335                 _imap4_download_noti_interval_value = 0;        /*  This will be assigned in imap_mail_write_body_to_file() */
2336
2337                 EM_DEBUG_LOG("cnt_info->file_no[%d], current_attachment_no[%d]", cnt_info->file_no, current_attachment_no);
2338                 if (emcore_get_body(stream, account_id, mail_id, msg_no, mbody, cnt_info, &err) < 0)  {
2339                         EM_DEBUG_EXCEPTION("emcore_get_body failed [%d]", err);
2340                         goto FINISH_OFF;
2341                 }
2342
2343                 if (!emcore_check_thread_status())  {
2344                         err = EMF_ERROR_CANCELLED;
2345                         goto FINISH_OFF;
2346                 }
2347
2348                 /*  select target attachment information. */
2349                 for (ai = cnt_info->file ; ai; ai = ai->next) {
2350                         if (ai->name)
2351                                 EM_DEBUG_LOG("[in loop] %s, %d",  ai->name, cnt_info->file_no);
2352
2353                         if (--cnt_info->file_no == 0)
2354                                 break;
2355                 }
2356
2357                 EM_DEBUG_LOG("cnt_info->file_no = %d, ai = %p", cnt_info->file_no, ai);
2358                 
2359                 if (cnt_info->file_no == 0 && ai) {
2360                         /*  rename temporary file to real file. */
2361                         if (!emstorage_create_dir(account_id, mail_id, current_attachment_no, &err))  {
2362                                 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2363                                 goto FINISH_OFF;
2364                         }
2365
2366                         if (!emstorage_get_save_name(account_id, mail_id, current_attachment_no, ai->name, buf, &err))  {
2367                                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2368                                 goto FINISH_OFF;
2369                         }
2370
2371                         if (!emstorage_move_file(ai->save, buf, false, &err))  {
2372                                 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
2373
2374                                 goto FINISH_OFF;
2375                         }
2376
2377                         EM_SAFE_FREE(ai->save);
2378
2379                         EM_DEBUG_LOG("ai->size [%d]", ai->size);
2380                         attachment->attachment_size = ai->size;
2381                         attachment->attachment_path = EM_SAFE_STRDUP(buf);
2382
2383                         /*  update attachment information. */
2384                         if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, attachment, true, &err))  {
2385                                 EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
2386                                 /*  delete created file. */
2387                                 remove(buf);
2388
2389                                 goto FINISH_OFF;
2390                         }
2391
2392 #ifdef __FEATURE_DRM__
2393                         if (emcore_check_drm(attachment))  {    /*  has drm attachment ? */
2394                                 if (drm_svc_register_file(attachment->attachment_path) == DRM_RESULT_SUCCESS)
2395                                         EM_DEBUG_LOG("drm_svc_register_file success");
2396                                 else
2397                                         EM_DEBUG_EXCEPTION("drm_svc_register_file fail");
2398                                 mail->DRM_status = attachment->attachment_drm_type;
2399                         }
2400 #endif /* __FEATURE_DRM__ */
2401                 }
2402                 else  {
2403                         EM_DEBUG_EXCEPTION("invalid attachment sequence...");
2404                         err = EMF_ERROR_INVALID_ATTACHMENT;
2405                         goto FINISH_OFF;
2406                 }
2407         
2408                 if (cnt_info)  {
2409                         emcore_free_content_info(cnt_info);
2410                         cnt_info = NULL;
2411                 }
2412                 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] completed", i+1, attachment_count_to_be_downloaded);
2413         }
2414
2415         if (stream)  {
2416                 emcore_close_mailbox(0, stream);
2417                 stream = NULL;
2418         }
2419         
2420         ret = true;
2421         
2422 FINISH_OFF:
2423         if (stream)
2424                 emcore_close_mailbox(account_id, stream);
2425         if (attachment_list)
2426                 emstorage_free_attachment(&attachment_list, attachment_count_to_be_downloaded, NULL);
2427         if (cnt_info)
2428                 emcore_free_content_info(cnt_info);
2429         if (mail)
2430                 emstorage_free_mail(&mail, 1, NULL);
2431
2432         EM_SAFE_FREE(s_uid);
2433         EM_SAFE_FREE(server_mbox);
2434
2435         if (ret == true)
2436                 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FINISH, mail_id, NULL, attachment_no, 0);
2437         else
2438                 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, attachment_no, err);
2439
2440         if (err_code != NULL)
2441                 *err_code = err;
2442         
2443         EM_DEBUG_FUNC_END();
2444         return ret;
2445 }
2446
2447 #ifdef __ATTACHMENT_OPTI__
2448 INTERNAL_FUNC int emcore_download_attachment_bulk(int account_id, int mail_id, char *nth, int *err_code)
2449 {
2450         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], nth[%p], err_code[%p]", account_id, mail_id, nth, err_code);
2451
2452         int attachment_no = 0;
2453         int err = EMF_ERROR_NONE; /* Prevent Defect - 25093 */
2454         int ret = false;
2455         int status = EMF_DOWNLOAD_FAIL;
2456         MAILSTREAM *stream = NULL;
2457         emstorage_mail_tbl_t *mail = NULL;
2458         emstorage_attachment_tbl_t *attachment = NULL;
2459         void *tmp_stream = NULL;
2460         char *s_uid = NULL, *server_mbox = NULL, buf[512];
2461         emstorage_attachment_tbl_t *attachment_list = NULL;
2462         int current_attachment_no = 0;
2463         int attachment_count_to_be_downloaded = 0;              /*  how many attachments should be downloaded */
2464         int i = 0;
2465         char *savefile = NULL;
2466         int dec_len = 0;
2467         int uid = 0;
2468 #ifdef SUPPORT_EXTERNAL_MEMORY
2469         int iActualSize = 0;
2470         int is_add = 0;
2471         char dirName[512];
2472         int bIs_empty = 0;
2473         int bIs_full = 0;
2474         int bIsAdd_to_mmc = false;
2475         int is_on_mmc = false;
2476         emf_file_list *pFileListMMc = NULL;
2477         emf_file_list *pFileList = NULL;
2478 #endif /*  SUPPORT_EXTERNAL_MEMORY */
2479         
2480         
2481         memset(buf, 0x00, 512);
2482         /* CID FIX 31230 */
2483         if (mail_id < 1 || !nth) {
2484                 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], nth[%p]", account_id, mail_id, nth);
2485
2486                 err = EMF_ERROR_INVALID_PARAM;
2487
2488                 if (err_code != NULL)
2489                         *err_code = err;
2490
2491                 if (nth)
2492                         attachment_no = atoi(nth);
2493
2494                 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, 0, attachment_no, err);      /*  090525, kwangryul.baek */
2495
2496                 return false;
2497         }
2498         
2499         if (!emcore_check_thread_status()) {
2500                 err = EMF_ERROR_CANCELLED;
2501                 goto FINISH_OFF;
2502         }
2503         only_body_download = false;
2504         
2505         attachment_no = atoi(nth);
2506
2507         if (attachment_no == 0) {
2508                 /*  download all attachments, nth starts from 1, not zero */
2509                 /*  get attachment list from db */
2510                 attachment_count_to_be_downloaded = EMF_ATTACHMENT_MAX_COUNT;
2511                 if ( (err = emstorage_get_attachment_list(mail_id, true, &attachment_list, &attachment_count_to_be_downloaded)) != EMF_ERROR_NONE || !attachment_list){
2512                         EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
2513         
2514                         goto FINISH_OFF;
2515                 }
2516         }
2517         else {  /*  download only nth attachment */
2518                 attachment_count_to_be_downloaded = 1;
2519                 if (!emstorage_get_attachment_nth(mail_id, attachment_no, &attachment_list, true, &err) || !attachment_list) {
2520                         EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
2521
2522
2523                         goto FINISH_OFF;
2524                 }
2525         }
2526
2527
2528         if (!emcore_check_thread_status()) {
2529                 err = EMF_ERROR_CANCELLED;
2530                 goto FINISH_OFF;
2531         }
2532
2533         
2534         /*  get mail from mail table. */
2535         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail) {
2536                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2537                 
2538
2539                 goto FINISH_OFF;
2540         }
2541
2542         /* if (!mail->server_mail_yn || !mail->text_download_yn) {*/ /*  faizan.h@samsung.com */
2543
2544         if (!emcore_check_thread_status()) {
2545                 err = EMF_ERROR_CANCELLED;
2546                 goto FINISH_OFF;
2547         }
2548
2549         account_id = mail->account_id;
2550         s_uid = EM_SAFE_STRDUP(mail->server_mail_id); mail->server_mail_id = NULL;
2551         server_mbox = EM_SAFE_STRDUP(mail->mailbox); mail->server_mailbox_name = NULL;
2552
2553
2554
2555         /*  open mail server. */
2556         if (!emcore_connect_to_remote_mailbox(account_id, server_mbox, (void **)&tmp_stream, &err) || !tmp_stream) {
2557                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2558
2559                 status = EMF_DOWNLOAD_CONNECTION_FAIL;
2560                 goto FINISH_OFF;
2561         }
2562
2563         stream = (MAILSTREAM *)tmp_stream;
2564
2565
2566         if (!emcore_check_thread_status()) {
2567                 err = EMF_ERROR_CANCELLED;
2568                 goto FINISH_OFF;
2569         }
2570
2571
2572         for (i = 0; i < attachment_count_to_be_downloaded; i++) {
2573                 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] start", i+1, attachment_count_to_be_downloaded);
2574
2575                 attachment = attachment_list + i;
2576                 if (attachment_no == 0) {
2577                         /*  download all attachments, nth starts from 1, not zero */
2578                         current_attachment_no = i + 1;          /*  attachment no */
2579                 }
2580                 else {
2581                         /*  download only nth attachment */
2582                         current_attachment_no = attachment_no;          /*  attachment no */
2583                 }
2584
2585                 if (!emcore_check_thread_status()) {
2586                         err = EMF_ERROR_CANCELLED;
2587                         goto FINISH_OFF;
2588                 }
2589
2590                 _imap4_received_body_size = 0;
2591                 _imap4_last_notified_body_size = 0;
2592                 _imap4_total_body_size = 0;                             /*  This will be assigned in imap_mail_write_body_to_file() */
2593                 _imap4_download_noti_interval_value = 0;        /*  This will be assigned in imap_mail_write_body_to_file() */
2594
2595                 
2596                 EM_SAFE_FREE(savefile);
2597                 if (!emcore_get_temp_file_name(&savefile, &err)) {
2598                         EM_DEBUG_EXCEPTION("emcore_get_temp_file_name failed [%d]", err);
2599
2600                         if (err_code != NULL)
2601                                 *err_code = err;
2602                         goto FINISH_OFF;
2603                 }
2604
2605                 if (s_uid)
2606                         uid = atoi(s_uid);
2607
2608                 EM_DEBUG_LOG("uid [%d]", uid);
2609
2610                 if (!imap_mail_write_body_to_file(stream, account_id, mail_id, attachment_no, savefile, uid , attachment->section, attachment->encoding, &dec_len, NULL, &err)) {
2611                         EM_DEBUG_EXCEPTION("imap_mail_write_body_to_file failed [%d]", err);
2612                         if (err_code != NULL)
2613                                 *err_code = err;
2614                         goto FINISH_OFF;
2615                 }
2616
2617 #ifdef SUPPORT_EXTERNAL_MEMORY
2618                 iActualSize = emcore_get_actual_mail_size (cnt_info->text.plain , cnt_info->text.html, cnt_info->file , &err);
2619                 if (!emstorage_mail_check_free_space(iActualSize, &bIs_full, &err)) {
2620                         EM_DEBUG_EXCEPTION("emstorage_mail_check_free_space failed [%d]", err);
2621                         goto FINISH_OFF;
2622                 }
2623                 
2624                 if (bIs_full) {
2625                         /* If external memory not present, return error */
2626                         if (PS_MMC_REMOVED == emstorage_get_mmc_status()) {
2627                                 err = EMF_ERROR_MAIL_MEMORY_FULL;
2628                                 goto FINISH_OFF;
2629                         }
2630                         bIsAdd_to_mmc = true;
2631                 }
2632 #endif /*  SUPPORT_EXTERNAL_MEMORY */
2633
2634                 if (!emstorage_create_dir(account_id, mail_id, attachment_no, &err)) {
2635                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2636                         goto FINISH_OFF;
2637                 }
2638
2639                 if (!emstorage_get_save_name(account_id, mail_id, attachment_no, attachment->name, buf, &err)) {
2640                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2641                         goto FINISH_OFF;
2642                 }
2643
2644                 if (!emstorage_move_file(savefile, buf, false, &err)) {
2645                         EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
2646
2647                         goto FINISH_OFF;
2648                 }
2649
2650                 attachment->attachment = EM_SAFE_STRDUP(buf);
2651                 /*  update attachment information. */
2652                 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, attachment, true, &err)) {
2653                         EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
2654
2655                         goto FINISH_OFF;
2656                 }
2657
2658 #ifdef __FEATURE_DRM__
2659                 if (emcore_check_drm(attachment)) {
2660                         /*  is drm */
2661                         if (drm_svc_register_file(attachment->attachment) == DRM_RESULT_SUCCESS)
2662                                 EM_DEBUG_LOG("drm_svc_register_file success");
2663                         else
2664                                 EM_DEBUG_EXCEPTION("drm_svc_register_file fail");
2665                         mail->flag3 = attachment->flag2;
2666                 }
2667 #endif /* __FEATURE_DRM__ */
2668
2669                 if (!emcore_check_thread_status()) {
2670                         err = EMF_ERROR_CANCELLED;
2671                         goto FINISH_OFF;
2672                 }
2673
2674                 EM_DEBUG_LOG(" >>>>>> Attachment Downloading [%d / %d] completed", i+1, attachment_count_to_be_downloaded);
2675         }
2676
2677         ret = true;
2678
2679         FINISH_OFF:
2680
2681         EM_SAFE_FREE(savefile);
2682
2683         emcore_close_mailbox(0, stream);
2684         stream = NULL;
2685
2686         if (attachment_list) 
2687                 emstorage_free_attachment(&attachment_list, attachment_count_to_be_downloaded, NULL);
2688
2689         if (mail) 
2690                 emstorage_free_mail(&mail, 1, NULL);
2691
2692         if (s_uid)
2693                 free(s_uid);
2694
2695         if (server_mbox)
2696                 free(server_mbox);server_mbox = NULL;
2697
2698         if (ret == true)
2699                 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FINISH, mail_id, NULL, attachment_no, 0);
2700         else if (err != EMF_ERROR_CANCELLED)
2701                 emstorage_notify_network_event(NOTI_DOWNLOAD_ATTACH_FAIL, mail_id, NULL, attachment_no, err);
2702
2703         if (err_code != NULL)
2704                 *err_code = err;
2705
2706         return ret;
2707 }
2708 #endif
2709
2710
2711 INTERNAL_FUNC int emcore_download_body_multi_sections_bulk(void *mail_stream, int account_id, int mail_id, int verbose, int with_attach, int limited_size, int event_handle , int *err_code)
2712 {
2713         EM_DEBUG_FUNC_BEGIN("mail_stream[%p], account_id[%d], mail_id[%d], verbose[%d], with_attach[%d], event_handle [ %d ] ", mail_stream, account_id, mail_id, verbose, with_attach, event_handle);
2714
2715         int ret = false;
2716         int err = EMF_ERROR_NONE;
2717         int status = EMF_DOWNLOAD_FAIL;
2718         int pop3_body_size = 0;
2719         int pop3_downloaded_size = 0;
2720         MAILSTREAM *stream = NULL;
2721         BODY *mbody = NULL;
2722         PARTLIST *section_list = NULL;
2723         emf_mailbox_t mbox = { 0 };
2724         emstorage_mail_tbl_t *mail = NULL;
2725         emstorage_attachment_tbl_t attachment = {0, 0, NULL, };
2726         emf_account_t *ref_account = NULL;
2727         struct attachment_info *ai = NULL;
2728         struct _m_content_info *cnt_info = NULL;
2729         void *tmp_stream = NULL;
2730         char *s_uid = NULL, *server_mbox = NULL, buf[512];
2731         int msgno = 0, attachment_num = 1, local_attachment_count = 0, local_inline_content_count = 0;
2732         int iActualSize = 0;
2733         char html_body[MAX_PATH] = {0, };
2734         emcore_uid_list *uid_list = NULL;
2735         char *mailbox_name = NULL;
2736 #ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
2737         int html_changed = 0;
2738 #endif
2739
2740         if (mail_id < 1)  {
2741                 EM_DEBUG_EXCEPTION("mail_stream[%p], account_id[%d], mail_id[%d], verbose[%d], with_attach[%d]", mail_stream, account_id, mail_id, verbose, with_attach);
2742                 err = EMF_ERROR_INVALID_PARAM;
2743                 
2744                 if (err_code != NULL)
2745                         *err_code = err;
2746
2747                 emstorage_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, event_handle, err);
2748                 return false;
2749         }
2750         
2751         FINISH_OFF_IF_CANCELED;
2752
2753         only_body_download = true;
2754
2755         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
2756                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
2757
2758                 goto FINISH_OFF;
2759         }
2760         
2761         if (mail->mailbox_name)
2762                 mailbox_name = EM_SAFE_STRDUP(mail->mailbox_name);
2763         
2764         if (1 == mail->body_download_status)  {
2765                 EM_DEBUG_EXCEPTION("not synchronous mail...");
2766                 err = EMF_ERROR_INVALID_MAIL;
2767                 goto FINISH_OFF;
2768         }
2769         
2770         account_id = mail->account_id;
2771         s_uid                     = mail->server_mail_id;
2772         server_mbox               = mail->server_mailbox_name;
2773         mail->server_mail_id      = NULL;
2774     mail->server_mailbox_name = NULL;
2775
2776         attachment.account_id = mail->account_id;
2777         attachment.mail_id = mail->mail_id;
2778         attachment.mailbox_name = mail->mailbox_name; mail->mailbox_name = NULL;
2779         attachment.attachment_save_status = 0;
2780         
2781         emstorage_free_mail(&mail, 1, NULL);
2782         mail = NULL;
2783         
2784         if (!(ref_account = emcore_get_account_reference(account_id)))   {
2785                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
2786                 err = EMF_ERROR_INVALID_ACCOUNT;
2787                 goto FINISH_OFF;
2788         }
2789
2790         FINISH_OFF_IF_CANCELED;
2791
2792         /*  open mail server. */
2793         if (!mail_stream)  {
2794                 if (!emcore_connect_to_remote_mailbox(account_id, mailbox_name, (void **)&tmp_stream, &err) || !tmp_stream)  {
2795                         EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
2796                         status = EMF_DOWNLOAD_CONNECTION_FAIL;
2797                         goto FINISH_OFF;
2798                 }
2799                 stream = (MAILSTREAM *)tmp_stream;
2800         }
2801         else
2802                 stream = (MAILSTREAM *)mail_stream;
2803         
2804         free(server_mbox); server_mbox = NULL;
2805         
2806         
2807         FINISH_OFF_IF_CANCELED;
2808         
2809         if (!(cnt_info = em_malloc(sizeof(struct _m_content_info))))  {
2810                 EM_DEBUG_EXCEPTION("em_malloc failed...");
2811                 err = EMF_ERROR_OUT_OF_MEMORY;
2812                 goto FINISH_OFF;
2813         }
2814
2815         if (ref_account->receiving_server_type == EMF_SERVER_TYPE_POP3)  {      /*  POP3 */
2816                 /*  in POP3 case, both text and attachment are downloaded in this call. */
2817                 cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
2818                 attachment.attachment_save_status = 1;          /*  all attachments should be downloaded in the case of POP3 */
2819
2820                 mbox.account_id = account_id;
2821                 mbox.mail_stream = stream;
2822
2823                 /*  download all uids from server. */
2824                 if (!emcore_download_uid_all(&mbox, &uid_list, NULL, NULL, 0, EM_CORE_GET_UIDS_FOR_NO_DELETE, &err))  {
2825                         EM_DEBUG_EXCEPTION("emcore_download_uid_all failed [%d]", err);
2826                         goto FINISH_OFF;
2827                 }
2828
2829                 /*  get mesg number to be related to last download mail from uid list file */
2830                 if (!emcore_get_msgno(uid_list, s_uid, &msgno, &err))  {
2831                         EM_DEBUG_EXCEPTION("emcore_get_msgno failed [%d]", err);
2832                         err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER;
2833                         goto FINISH_OFF;
2834                 }
2835
2836                 free(s_uid); s_uid = NULL;
2837
2838                 if (!emcore_check_thread_status())  {
2839                         err = EMF_ERROR_CANCELLED;
2840                         goto FINISH_OFF;
2841                 }
2842
2843                 _pop3_received_body_size = 0;
2844                 _pop3_total_body_size = 0;
2845                 _pop3_last_notified_body_size = 0;
2846                 _pop3_receiving_mail_id = mail_id;
2847                 
2848                 /*  send read mail commnad. */
2849                 if (!emcore_mail_cmd_read_mail_pop3(stream, msgno, limited_size, &pop3_downloaded_size, &pop3_body_size, &err))
2850                 /* if (!emcore_mail_cmd_read_mail_pop3(stream, msgno, PARTIAL_BODY_SIZE_IN_BYTES, &pop3_downloaded_size, &pop3_body_size, &err))  */ {
2851                         EM_DEBUG_EXCEPTION("emcore_mail_cmd_read_mail_pop3 failed [%d]", err);
2852                         goto FINISH_OFF;
2853                 }
2854
2855                 _pop3_total_body_size = pop3_body_size;
2856
2857                 if (!emstorage_notify_network_event(NOTI_DOWNLOAD_BODY_START, mail_id, "dummy-file", _pop3_total_body_size, 0))
2858                         EM_DEBUG_EXCEPTION(" emstorage_notify_network_event [ NOTI_DOWNLOAD_BODY_START] failed >>>> ");
2859                 else
2860                         EM_DEBUG_LOG("NOTI_DOWNLOAD_BODY_START notified (%d / %d)", 0, _pop3_total_body_size);
2861
2862                 FINISH_OFF_IF_CANCELED;
2863                 
2864                 /*  save message into tempfile */
2865                 /*  parsing mime from stream. */
2866
2867                 if (!emcore_parse_mime(stream, 0, cnt_info,  &err))  {
2868                         EM_DEBUG_EXCEPTION("emcore_parse_mime failed [%d]", err);
2869                         goto FINISH_OFF;
2870                 }
2871                 
2872                 FINISH_OFF_IF_CANCELED;
2873         }
2874         else  { /*  in IMAP case, both text and attachment list are downloaded in this call. */
2875                 /*  This flag is just for downloading mailbox.(sync header), don't be used when retrieve body. */
2876                 if (with_attach > 0)
2877                         cnt_info->grab_type = GRAB_TYPE_TEXT | GRAB_TYPE_ATTACHMENT;
2878                 else
2879                         cnt_info->grab_type = GRAB_TYPE_TEXT;
2880
2881                 int uid = atoi(s_uid);
2882
2883                 free(s_uid); s_uid = NULL;
2884
2885                 /*  set sparep(member of BODY) memory free function  */
2886                 mail_parameters(stream, SET_FREEBODYSPAREP, emcore_free_body_sharep);
2887
2888                 /*  get body strucutre. */
2889                 /*  don't free mbody because mbody is freed in closing mail_stream. */
2890                 if (emcore_get_body_structure(stream, uid, &mbody, &err) < 0 || (mbody == NULL))  {
2891                         EM_DEBUG_EXCEPTION("emcore_get_body_structure failed [%d]", err);
2892                         err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER;
2893                         goto FINISH_OFF;
2894                 }
2895
2896                 FINISH_OFF_IF_CANCELED;
2897
2898                 if (mbody->type == TYPEMULTIPART) {
2899                         EM_DEBUG_LOG(">>> check multipart body size to download : only_body_download[%d]", only_body_download);
2900                         PART *part_child = mbody->nested.part;
2901                         int counter = 0;
2902
2903                         char filename[MAX_PATH+1] = {0, };
2904                         int is_attachment = 0;
2905                         while (part_child)  {
2906                                 BODY *body = &(part_child->body);
2907                                 if (only_body_download == true) {
2908                                         if (((body->id) && strlen(body->id) > 1) || (body->location))
2909                                                 is_attachment = 0;
2910                                         else if (body->disposition.type)  {     /*  "attachment" or "inline" or etc... */
2911                                         PARAMETER *param = body->disposition.parameter;
2912
2913                                         while (param)  {
2914                                                         EM_DEBUG_LOG("param->attribute [%s], param->value [%s]", param->attribute, param->value);
2915
2916                                                 if (!strcasecmp(param->attribute, "filename"))  {       /* attribute is "filename" */
2917                                                         strncpy(filename, param->value, MAX_PATH);
2918                                                         EM_DEBUG_LOG(">>>>> FILENAME [%s] ", filename);
2919                                                         break;
2920                                                 }
2921                                                 param = param->next;
2922                                         }
2923
2924                                                 is_attachment = 1;
2925
2926                                                 if (!*filename)  {              /*  it may be report msg */
2927                                                         if (body->disposition.type[0] == 'i' || body->disposition.type[0] == 'I')
2928                                                                 is_attachment = 0;
2929                                                 }
2930                                         }
2931
2932                                         if (is_attachment == 0) {
2933                                                 EM_DEBUG_LOG("%d :  body->size.bytes[%ld]", counter+1, body->size.bytes);
2934                                                 multi_part_body_size = multi_part_body_size + body->size.bytes;
2935                                         }
2936                                 }
2937                                 else {  /*  download all */
2938                                 EM_DEBUG_LOG("%d :  body->size.bytes[%ld]", counter+1, body->size.bytes);
2939                                 multi_part_body_size = multi_part_body_size + body->size.bytes;
2940                                 }
2941                                 part_child = part_child->next;
2942                                 counter++;
2943                         }
2944                 }
2945
2946                 /*  set body fetch section. */
2947                 if (emcore_set_fetch_body_section(mbody, true, &iActualSize, &err) < 0) {
2948                         EM_DEBUG_EXCEPTION("emcore_set_fetch_body_section failed [%d]", err);
2949                         goto FINISH_OFF;
2950                 }
2951
2952                 EM_DEBUG_LOG("iActualSize [%d]", iActualSize);
2953                 multi_part_body_size = iActualSize;
2954
2955                 _imap4_received_body_size = 0;
2956                 _imap4_last_notified_body_size = 0;
2957                 if (multi_part_body_size > 0) { /*  download multiparts */
2958                         _imap4_total_body_size = multi_part_body_size;
2959                         _imap4_download_noti_interval_value = DOWNLOAD_NOTI_INTERVAL_PERCENT * multi_part_body_size / 100;
2960                 }
2961                 else {  /*  download only one body part */
2962                         _imap4_total_body_size = 0;                                     /*  This will be assigned in imap_mail_write_body_to_file() */
2963                         _imap4_download_noti_interval_value = 0;        /*  This will be assigned in imap_mail_write_body_to_file() */
2964                 }
2965
2966                 /*  save message into tempfile */
2967                 /*  download body text and get attachment list. */
2968                 if (emcore_get_body_part_list_full(stream, uid, account_id, mail_id, mbody, cnt_info, &err, section_list, event_handle) < 0)  {
2969                         EM_DEBUG_EXCEPTION("emcore_get_body falied [%d]", err);
2970                         goto FINISH_OFF;
2971                 }
2972                 FINISH_OFF_IF_CANCELED;
2973         }
2974
2975
2976         if (false == emstorage_get_mail_by_id(mail_id, &mail, true, &err)) {
2977                 EM_DEBUG_EXCEPTION(" emstorage_get_mail_by_id failed [%d]", err);
2978                 goto FINISH_OFF;
2979         }
2980
2981         if (cnt_info->text.plain)  {
2982                 EM_DEBUG_LOG("cnt_info->text.plain [%s]", cnt_info->text.plain);
2983
2984                 if (!emstorage_create_dir(account_id, mail_id, 0, &err))  {
2985                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
2986                         goto FINISH_OFF;
2987                 }
2988
2989
2990                 if (!emstorage_get_save_name(account_id, mail_id, 0, cnt_info->text.plain_charset ? cnt_info->text.plain_charset  :  "UTF-8", buf, &err))  {
2991                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
2992                         goto FINISH_OFF;
2993                 }
2994
2995                 if (!emstorage_move_file(cnt_info->text.plain, buf, false, &err))  {
2996                         EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
2997                         goto FINISH_OFF;
2998                 }
2999
3000                 mail->file_path_plain = EM_SAFE_STRDUP(buf);
3001                 EM_DEBUG_LOG("mail->file_path_plain [%s]", mail->file_path_plain);
3002         }
3003         
3004         if (cnt_info->text.html)  {
3005                 if (!emstorage_create_dir(account_id, mail_id, 0, &err))  {
3006                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3007                         goto FINISH_OFF;
3008                 }
3009                 
3010                 if (cnt_info->text.plain_charset != NULL) {
3011                         memcpy(html_body, cnt_info->text.plain_charset, strlen(cnt_info->text.plain_charset));
3012                         strcat(html_body, HTML_EXTENSION_STRING);
3013                 }
3014                 else {
3015                         memcpy(html_body, "UTF-8.htm", strlen("UTF-8.htm"));
3016                 }
3017                 if (!emstorage_get_save_name(account_id, mail_id, 0, html_body, buf, &err))  {
3018                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3019                         goto FINISH_OFF;
3020                 }
3021
3022                 if (!emstorage_move_file(cnt_info->text.html, buf, false, &err))  {
3023                         EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
3024                         goto FINISH_OFF;
3025                 }
3026                 mail->file_path_html = EM_SAFE_STRDUP(buf);
3027         }
3028         
3029         if (ref_account->receiving_server_type == EMF_SERVER_TYPE_POP3 && limited_size != NO_LIMITATION && limited_size < pop3_body_size)
3030                 mail->body_download_status = EMF_BODY_DOWNLOAD_STATUS_PARTIALLY_DOWNLOADED;
3031         else
3032                 mail->body_download_status = EMF_BODY_DOWNLOAD_STATUS_FULLY_DOWNLOADED;
3033         
3034 #ifdef CHANGE_HTML_BODY_TO_ATTACHMENT
3035         if (html_changed) mail->flag2 = 1;
3036 #endif
3037         
3038         FINISH_OFF_IF_CANCELED;
3039         
3040         for (ai = cnt_info->file; ai; ai = ai->next, attachment_num++)  {
3041                 attachment.attachment_id                    = attachment_num;
3042                 attachment.attachment_size                  = ai->size;
3043                 attachment.attachment_path                  = ai->save;
3044                 attachment.attachment_name                  = ai->name;
3045                 attachment.attachment_drm_type              = ai->drm;
3046                 attachment.attachment_inline_content_status = ai->type == 1;
3047                 attachment.attachment_save_status           = 0;
3048                 attachment.attachment_mime_type             = ai->attachment_mime_type;
3049 #ifdef __ATTACHMENT_OPTI__
3050                 attachment.encoding                         = ai->encoding;
3051                 attachment.section                          = ai->section;
3052 #endif
3053                 EM_DEBUG_LOG("attachment.attachment_id[%d]", attachment.attachment_id);
3054                 EM_DEBUG_LOG("attachment.attachment_size[%d]", attachment.attachment_size);
3055                 EM_DEBUG_LOG("attachment.attachment_path[%s]", attachment.attachment_path);
3056                 EM_DEBUG_LOG("attachment.attachment_name[%s]", attachment.attachment_name);
3057                 EM_DEBUG_LOG("attachment.attachment_drm_type[%d]", attachment.attachment_drm_type);
3058                 EM_DEBUG_LOG("attachment.attachment_inline_content_status[%d]", attachment.attachment_inline_content_status);
3059
3060                 if (ai->type == 1)
3061                         local_inline_content_count++;
3062                 local_attachment_count++;
3063
3064                 if (ai->save)  {
3065                         /*  in POP3 case, rename temporary file to real file. */
3066                         attachment.attachment_save_status = 1;
3067                         if (ai->type == 1)  {           /*  it is inline content */
3068                                 if (!emstorage_create_dir(account_id, mail_id, 0, &err))  {
3069                                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3070                                         goto FINISH_OFF;
3071                                 }
3072                                 if (!emstorage_get_save_name(account_id, mail_id, 0, attachment.attachment_name, buf, &err))  {
3073                                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3074                                         goto FINISH_OFF;
3075                                 }
3076                         }
3077                         else  {
3078                                 if (!emstorage_create_dir(account_id, mail_id, attachment_num, &err)) {
3079                                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3080                                         goto FINISH_OFF;
3081                                 }
3082
3083                                 if (!emstorage_get_save_name(account_id, mail_id, attachment_num, attachment.attachment_name, buf, &err))  {
3084                                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3085                                         goto FINISH_OFF;
3086                                 }
3087                         }
3088
3089                         if (!emstorage_move_file(ai->save, buf, false, &err))  {
3090                                 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
3091
3092                                 /*  delete all created files. */
3093                                 if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, NULL)) {
3094                                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed...");
3095                                         /* goto FINISH_OFF; */
3096                                 }
3097
3098                                 if (!emstorage_delete_dir(buf, NULL)) {
3099                                         EM_DEBUG_EXCEPTION("emstorage_delete_dir failed...");
3100                                         /* goto FINISH_OFF; */
3101                                 }
3102
3103
3104                                 goto FINISH_OFF;
3105                         }
3106
3107                         free(ai->save); ai->save = EM_SAFE_STRDUP(buf);
3108
3109                         attachment.attachment_path = ai->save;
3110
3111 #ifdef __FEATURE_DRM__
3112                         if (emcore_check_drm(&attachment)) /*  is drm content ?*/ {
3113                                 if (!drm_svc_register_file(attachment.attachment_path))
3114                                         EM_DEBUG_EXCEPTION("drm_svc_register_file fail");
3115                                 mail->DRM_status = attachment.attachment_drm_type;
3116                         }
3117 #endif/*  __FEATURE_DRM__ */
3118                 }
3119
3120 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
3121                 /*  Information :  Attachment info already saved if partial body is dowloaded. */
3122                 if (ai->type)  {
3123                         emstorage_attachment_tbl_t *attch_info = NULL;
3124                         /*  Get attachment details  */
3125                         if (!emstorage_get_attachment_nth(mail_id, attachment.attachment_id, &attch_info, true, &err) || !attch_info)  {
3126                                 EM_DEBUG_EXCEPTION("emstorage_get_attachment_nth failed [%d]", err);
3127                                 if (err == EMF_ERROR_ATTACHMENT_NOT_FOUND) {   /*  save only attachment file. */
3128                                         if (!emstorage_add_attachment(&attachment, 0, false, &err)) {
3129                                                 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
3130                                                 if (attch_info)
3131                                                         emstorage_free_attachment(&attch_info, 1, NULL);
3132                                                 /*  delete all created files. */
3133                                                 if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, &err))  {
3134                                                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3135                                                         goto FINISH_OFF;
3136                                                 }
3137
3138                                                 if (!emstorage_delete_dir(buf, &err))  {
3139                                                         EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3140                                                         goto FINISH_OFF;
3141                                                 }
3142
3143                                                 /*  ROLLBACK TRANSACTION; */
3144                                                 emstorage_rollback_transaction(NULL, NULL, NULL);
3145
3146
3147                                                 goto FINISH_OFF;
3148                                         }
3149                                 }
3150                         }
3151                         else {
3152                                 EM_DEBUG_LOG("Attachment info already exists...!");
3153                                 /* Update attachment size */
3154                                 EM_DEBUG_LOG("attachment_size [%d], ai->size [%d]", attch_info->attachment_size, ai->size);
3155                                 attch_info->attachment_size = ai->size;
3156                                 if (!emstorage_update_attachment(attch_info, true, &err)) {
3157                                         EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
3158
3159                                                 goto FINISH_OFF;
3160                                 }
3161                         }
3162
3163                         if (attch_info)
3164                                 emstorage_free_attachment(&attch_info, 1, NULL);
3165                 }
3166
3167 #else
3168                 
3169                 if (ai->type)  {
3170                         mail->attachment_yn = 1;
3171                         /*  save only attachment file. */
3172                         if (!emstorage_add_attachment(&attachment, 0, false, &err))  {
3173                                 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
3174                                 if (bIsAdd_to_mmc)  {
3175                                         if (attachment.attachment) {
3176                                         }
3177                                 }
3178                                 else  {
3179                                         /*  delete all created files. */
3180                                         if (!emstorage_get_save_name(account_id, mail_id, 0, NULL, buf, &err))  {
3181                                                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3182                                                 goto FINISH_OFF;
3183                                         }
3184
3185                                         if (!emstorage_delete_dir(buf, &err))  {
3186                                                 EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3187                                                 goto FINISH_OFF;
3188                                         }
3189
3190                                         /*  ROLLBACK TRANSACTION; */
3191                                         emstorage_rollback_transaction(NULL, NULL, NULL);
3192
3193
3194                                         goto FINISH_OFF;
3195                                 }
3196                         }
3197             }
3198 #endif  /*  End of #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__ */
3199
3200         }
3201
3202         EM_DEBUG_LOG("Check #1");
3203
3204         mail->attachment_count = local_attachment_count;
3205         mail->inline_content_count = local_inline_content_count;
3206
3207         EM_DEBUG_LOG("Check #2");
3208
3209         EM_DEBUG_LOG("Mailbox Name [%s]", mailbox_name);
3210         mail->mailbox_name = EM_SAFE_STRDUP(mailbox_name);       /*  fix for mailboox sync fail */
3211
3212         EM_DEBUG_LOG("Check #3");
3213
3214         /*  change mail's information. */
3215         if (!emstorage_change_mail_field(mail_id, APPEND_BODY, mail, false, &err))  {
3216                 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
3217                 emstorage_rollback_transaction(NULL, NULL, NULL); /*  ROLLBACK TRANSACTION; */
3218
3219                 goto FINISH_OFF;
3220         }
3221
3222         EM_DEBUG_LOG("cnt_info->text.plain [%s], cnt_info->text.html [%s]", cnt_info->text.plain, cnt_info->text.html);
3223         
3224         /*  in pop3 mail case, the mail is deleted from server after being downloaded. */
3225         if (ref_account->receiving_server_type == EMF_SERVER_TYPE_POP3)  {
3226 #ifdef DELETE_AFTER_DOWNLOADING
3227                 char delmsg[24];
3228
3229                 SNPRINTF(delmsg, sizeof(delmsg), "%d", msg_no);
3230
3231                 if (!ref_account->keep_on_server)  {
3232                         if (!emcore_delete_mail_from_server(&mbox, delmsg, &err))
3233                                 EM_DEBUG_EXCEPTION("emcore_delete_mail_from_server failed [%d]", err);
3234                 }
3235 #endif
3236                 
3237                 if (!mail_stream)  {
3238                         if (stream != NULL)  {
3239                                 emcore_close_mailbox(0, stream);
3240                                 stream = NULL;
3241                         }
3242                 }
3243         }
3244
3245         FINISH_OFF_IF_CANCELED;
3246         
3247         ret = true;
3248
3249 FINISH_OFF:
3250
3251         if (g_inline_count) {
3252                 g_inline_count = 0;
3253                 EM_SAFE_FREE(g_inline_list);
3254         }
3255
3256         multi_part_body_size = 0;
3257         _pop3_received_body_size = 0;
3258         _pop3_last_notified_body_size = 0;
3259         _pop3_total_body_size = 0;
3260         _pop3_receiving_mail_id = 0;
3261         
3262         _imap4_received_body_size = 0;
3263         _imap4_last_notified_body_size = 0;
3264         _imap4_total_body_size = 0;
3265         _imap4_download_noti_interval_value = 0;
3266         
3267         if (cnt_info)
3268                 emcore_free_content_info(cnt_info);
3269         if (mail)
3270                 emstorage_free_mail(&mail, 1, NULL);
3271         if (attachment.mailbox_name)
3272                 free(attachment.mailbox_name);
3273         if (server_mbox)
3274                 free(server_mbox);
3275         if (s_uid)
3276                 free(s_uid);
3277
3278         multi_part_body_size = 0;
3279         
3280         if (ret == true)
3281                 emstorage_notify_network_event(NOTI_DOWNLOAD_BODY_FINISH, mail_id, NULL, event_handle, 0);
3282         else
3283                 emstorage_notify_network_event(NOTI_DOWNLOAD_BODY_FAIL, mail_id, NULL, event_handle, err);
3284
3285         if (mailbox_name)
3286                 free (mailbox_name);
3287
3288         if (err_code != NULL)
3289                 *err_code = err;
3290         
3291         return ret;
3292 }
3293
3294
3295
3296 void emcore_mail_copyuid(MAILSTREAM *stream, char *mailbox,
3297                            unsigned long uidvalidity, SEARCHSET *sourceset,
3298                            SEARCHSET *destset)
3299 {
3300         EM_DEBUG_FUNC_BEGIN();
3301         char  old_server_uid[129];
3302
3303         EM_DEBUG_LOG("mailbox name - %s", mailbox);
3304         EM_DEBUG_LOG("first sequence number source- %ld", sourceset->first);
3305         EM_DEBUG_LOG("last sequence number last- %ld", sourceset->last);
3306         EM_DEBUG_LOG("first sequence number dest - %ld", destset->first);
3307         EM_DEBUG_LOG("last sequence number dest- %ld", sourceset->last);
3308
3309         /* search for server _mail_id with value sourceset->first and update it with destset->first */
3310         /* faizan.h@samsung.com */
3311         memset(old_server_uid, 0x00, 129);
3312         sprintf(old_server_uid, "%ld", sourceset->first);
3313         EM_DEBUG_LOG(">>>>> old_server_uid = %s", old_server_uid);
3314
3315         memset(g_new_server_uid, 0x00, 129);
3316         sprintf(g_new_server_uid, "%ld", destset->first);
3317         EM_DEBUG_LOG(">>>>> new_server_uid =%s", g_new_server_uid);
3318
3319         if (!emstorage_update_server_uid(old_server_uid, g_new_server_uid, NULL)) {
3320                 EM_DEBUG_EXCEPTION("emstorage_update_server_uid falied...");
3321         }
3322 }
3323
3324 int emcore_delete_mail(int account_id, int mail_ids[], int num, int from_server, int noti_param_1, int noti_param_2, int *err_code)
3325 {
3326         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_ids[%p], num[%d], from_server[%d], noti_param_1 [%d], noti_param_2 [%d], err_code[%p]", account_id, mail_ids, num, from_server, noti_param_1, noti_param_2, err_code);
3327
3328         int ret = false;
3329         int err = EMF_ERROR_NONE;
3330         int status = EMF_DELETE_FAIL;
3331
3332         emf_account_t *account = NULL;
3333         emf_mailbox_t mailbox;
3334         emstorage_mail_tbl_t *mail = NULL;
3335         void *stream = NULL;
3336         int mail_id = 0;
3337         int i = 0;
3338         int msgno = 0, parameter_string_length = 0;
3339         char *parameter_string = NULL, mail_id_string[10];
3340         #ifdef  __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
3341         int bulk_flag = false;
3342         #endif
3343
3344         memset(&mailbox, 0x00, sizeof(emf_mailbox_t));
3345
3346         if (!account_id || !mail_ids || !num)  {
3347                 EM_DEBUG_EXCEPTION("account_id[%d], mail_ids[%p], num[%d], from_server[%d]", account_id, mail_ids, num, from_server);
3348                 err = EMF_ERROR_INVALID_PARAM;
3349                 goto FINISH_OFF;
3350         }
3351
3352         mail_id = mail_ids[0];
3353
3354         if (!(account = emcore_get_account_reference(account_id)))  {
3355                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
3356                 err = EMF_ERROR_INVALID_ACCOUNT;
3357                 goto FINISH_OFF;
3358         }
3359
3360         if (account->receiving_server_type == EMF_SERVER_TYPE_ACTIVE_SYNC)
3361                 from_server = EMF_DELETE_LOCALLY;
3362
3363         FINISH_OFF_IF_CANCELED;
3364
3365         parameter_string_length = sizeof(char) * (num * 8 + 128 /* MAILBOX_LEN_IN_MAIL_TBL */ * 2);
3366         parameter_string = malloc(parameter_string_length);
3367
3368         if (parameter_string == NULL) {
3369                 EM_DEBUG_EXCEPTION("Memory allocation for mail_id_list_string failed");
3370                 err = EMF_ERROR_OUT_OF_MEMORY;
3371                 goto FINISH_OFF;
3372         }
3373
3374         memset(parameter_string, 0, parameter_string_length);
3375
3376         if (from_server == EMF_DELETE_LOCAL_AND_SERVER || from_server == EMF_DELETE_FOR_SEND_THREAD)  {   /* server delete */
3377                 for (i = 0; i < num; i++)  {
3378                         mail_id = mail_ids[i];
3379
3380                         if (!emstorage_get_downloaded_mail(mail_id, &mail, false, &err) || !mail)  {
3381                                 EM_DEBUG_EXCEPTION("emstorage_get_uid_by_mail_id failed [%d]", err);
3382
3383                                 if (err == EMF_ERROR_MAIL_NOT_FOUND)  { /*  not server mail */
3384                                         /* err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER; */
3385                                         continue;
3386                                 }
3387                                 else
3388
3389                                 
3390                                 goto FINISH_OFF;
3391                         }
3392
3393                         EM_SAFE_FREE(mailbox.user_data);
3394                         
3395                         if (stream == NULL)  {
3396                                 if (!emcore_connect_to_remote_mailbox(account_id, mail->server_mailbox_name , (void **)&stream, &err))  {
3397                                         EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
3398                                         status = EMF_DELETE_CONNECTION_FAIL;
3399                                         goto FINISH_OFF;
3400                                 }
3401
3402                                 mailbox.account_id = account_id;
3403                                 mailbox.name = mail->server_mailbox_name;
3404                                 mailbox.mail_stream = stream;
3405
3406                                 FINISH_OFF_IF_CANCELED;
3407                         }
3408                                 
3409 #ifdef  __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
3410                         if (account->receiving_server_type == EMF_SERVER_TYPE_IMAP4) {
3411 #ifdef __FEATURE_LOCAL_ACTIVITY__
3412                                 if (!bulk_flag && !imap4_mail_delete_ex(&mailbox, mail_ids, num, from_server, &err)) {
3413 #else /* __FEATURE_LOCAL_ACTIVITY__ */
3414                                 if (!bulk_flag && !imap4_mail_delete_ex(&mailbox, mail_ids, num, &err)) {
3415 #endif /* __FEATURE_LOCAL_ACTIVITY__ */
3416                                         EM_DEBUG_EXCEPTION("imap4_mail_delete_ex failed [%d]", err);
3417                                         if (err == EMF_ERROR_IMAP4_STORE_FAILURE)
3418                                                 err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3419                                         goto FINISH_OFF;
3420                                 }
3421                                 else
3422                                         bulk_flag = true;
3423
3424                         }
3425                         else if (account->receiving_server_type == EMF_SERVER_TYPE_POP3) {
3426                                 if (!emcore_get_mail_msgno_by_uid(account, &mailbox, mail->server_mail_id, &msgno, &err))  {
3427                                         EM_DEBUG_EXCEPTION("emcore_get_mail_msgno_by_uid faild [%d]", err);
3428                                         if (err == EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER)
3429                                                 goto NOT_FOUND_ON_SERVER;
3430                                         else
3431                                                 goto FINISH_OFF;
3432                                 }
3433
3434                                 FINISH_OFF_IF_CANCELED;
3435
3436                                 if (!emcore_delete_mail_from_server(account, &mailbox, msgno, &err))  {
3437                                         EM_DEBUG_EXCEPTION("emcore_delete_mail_from_server falied [%d]", err);
3438                                         goto FINISH_OFF;
3439                                 }
3440 #ifdef __FEATURE_LOCAL_ACTIVITY__
3441                                 else {
3442                                         /* Remove local activity */
3443                                         emstorage_activity_tbl_t new_activity;
3444                                         memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
3445                                         if (from_server == EMF_DELETE_FOR_SEND_THREAD) {
3446                                                 new_activity.activity_type = ACTIVITY_DELETEMAIL_SEND;
3447                                                 EM_DEBUG_LOG("from_server == EMF_DELETE_FOR_SEND_THREAD ");
3448                                         }
3449                                         else {
3450                                                 new_activity.activity_type      = ACTIVITY_DELETEMAIL;
3451                                         }
3452                                 
3453                                         new_activity.mail_id            = mail->mail_id;
3454                                         new_activity.server_mailid      = mail->server_mail_id;
3455                                         new_activity.src_mbox           = NULL;
3456                                         new_activity.dest_mbox          = NULL;
3457
3458                                         if (!emcore_delete_activity(&new_activity, &err)) {
3459                                                 EM_DEBUG_EXCEPTION(" emcore_delete_activity  failed  - %d ", err);
3460                                         }
3461                                 
3462                                         /* Fix for issue - Sometimes mail move and immediately followed by mail delete is not reflected on server */
3463                                         if (!emstorage_remove_downloaded_mail(account_id, mail->server_mailbox_name, mail->server_mail_id, true, &err))  {
3464                                                 EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
3465                                         }
3466                                 }
3467
3468 #endif  /*  __FEATURE_LOCAL_ACTIVITY__ */
3469                         }
3470 #else   /*  __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__ */
3471                         if (!emcore_get_mail_msgno_by_uid(account, &mailbox, mail->server_mail_id, &msgno, &err)) {
3472                                 EM_DEBUG_LOG("emcore_get_mail_msgno_by_uid faild [%d]", err);
3473
3474                                 if (err == EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER) /* Prevent Defect - 9561 */
3475                                         goto NOT_FOUND_ON_SERVER;
3476                                 else
3477                                         goto FINISH_OFF;
3478                         }
3479
3480                         FINISH_OFF_IF_CANCELED;
3481
3482                         if (!emcore_delete_mail_from_server(account, &mailbox, msgno, &err)) {
3483                                 EM_DEBUG_LOG("emcore_delete_mail_from_server falied [%d]", err);
3484
3485                                 goto FINISH_OFF;
3486                         }
3487 #endif  /*  __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__ */
3488
3489                         if (!emstorage_remove_downloaded_mail(account_id, mail->server_mailbox_name, mail->server_mail_id, true, &err))
3490                                 EM_DEBUG_LOG("emstorage_remove_downloaded_mail falied [%d]", err);
3491
3492                         /* emcore_delete_notification_for_read_mail(mail_id); */
3493
3494 NOT_FOUND_ON_SERVER:
3495                         memset(mail_id_string, 0, 10);
3496                         SNPRINTF(mail_id_string, 10, "%d,", mail_id);
3497                         strcat(parameter_string, mail_id_string);
3498
3499                         FINISH_OFF_IF_CANCELED;
3500                 
3501                         emstorage_free_mail(&mail, 1, NULL);
3502                 }
3503         }
3504         else if (from_server == EMF_DELETE_LOCALLY) /* Local Delete */ {
3505                 emcore_delete_mail_from_local_storage(account_id, mail_ids, num, noti_param_1, noti_param_2, err_code);
3506                 for (i = 0; i < num; i++) {
3507                         /* emcore_delete_notification_for_read_mail(mail_id); */
3508                         SNPRINTF(mail_id_string, 10, "%d,", mail_id);
3509                         strcat(parameter_string, mail_id_string);
3510                 }
3511
3512                 emcore_check_unread_mail();
3513         }
3514         
3515         ret = true;
3516
3517 FINISH_OFF: 
3518
3519         if (stream)              {
3520                 emcore_close_mailbox(0, stream);
3521                 stream = NULL;
3522         }
3523
3524         if (mailbox.user_data != NULL) {
3525                 emcore_free_uids(mailbox.user_data, NULL);
3526                 mailbox.user_data = NULL;
3527         }
3528
3529         EM_SAFE_FREE(parameter_string);
3530
3531         if (mail != NULL)
3532                 emstorage_free_mail(&mail, 1, NULL);
3533
3534
3535         if (from_server)
3536                 emcore_show_popup(account_id, EMF_ACTION_DELETE_MAIL, ret == true ? 0  :  err);
3537
3538         if (err_code != NULL)
3539                 *err_code = err;
3540
3541         return ret;
3542 }
3543
3544
3545 int emcore_delete_mail_all(emf_mailbox_t *mailbox, int with_server, int *err_code)
3546 {
3547         EM_DEBUG_FUNC_BEGIN("mailbox [%p], with_server [%d], err_code [%p]", mailbox, with_server, err_code);
3548
3549         int   ret = false;
3550         int   err = EMF_ERROR_NONE;
3551         int   search_handle = 0;
3552         int  *mail_ids = NULL;
3553         int   i = 0;
3554         int   total = 0;
3555         char  buf[512] = { 0, };
3556
3557         if (!mailbox || mailbox->account_id < FIRST_ACCOUNT_ID) {
3558                 err = EMF_ERROR_INVALID_PARAM;
3559                 goto FINISH_OFF;
3560         }
3561
3562         if (mailbox->name != NULL) {
3563                 /* Delete all mails in specific mailbox */
3564                 if (!emstorage_mail_search_start(NULL, mailbox->account_id, mailbox->name, 0, &search_handle, &total, true, &err)) {
3565                         EM_DEBUG_EXCEPTION("emstorage_mail_search_start failed [%d]", err);
3566         
3567                         goto FINISH_OFF;
3568                 }
3569
3570                 EM_DEBUG_LOG("emstorage_mail_search_start returns [%d]", total);
3571
3572                 if (total > 0) {
3573                         mail_ids = em_malloc(sizeof(int) * total);
3574                         if (mail_ids == NULL)  {
3575                                 EM_DEBUG_EXCEPTION("em_malloc failed...");
3576                                 err = EMF_ERROR_OUT_OF_MEMORY;
3577                                 goto FINISH_OFF;
3578                         }
3579
3580                         for (i = 0; i < total; i++)  {
3581                                 if (!emstorage_mail_search_result(search_handle, RETRIEVE_ID, (void**)&mail_ids[i], true, &err)) {
3582                                         EM_DEBUG_EXCEPTION("emstorage_mail_search_result failed [%d]", err);
3583
3584                                         goto FINISH_OFF;
3585                                 }
3586                         }
3587
3588                         if (!emcore_delete_mail(mailbox->account_id, mail_ids, total, with_server, EMF_DELETED_BY_COMMAND, false, &err)) {
3589                                 EM_DEBUG_EXCEPTION("emcore_delete_mail failed [%d]", err);
3590                                 goto FINISH_OFF;
3591                         }
3592                 }
3593         }
3594         else if (with_server == EMF_DELETE_LOCALLY){
3595                 /* emstorage_delete_mail_by_account is available only locally */
3596                 if (!emstorage_delete_mail_by_account(mailbox->account_id, false, &err))  {
3597                         EM_DEBUG_EXCEPTION("emstorage_delete_mail_by_account failed [%d]", err);
3598
3599                         goto FINISH_OFF;
3600                 }
3601
3602                 if (!emstorage_delete_attachment_all_on_db(mailbox->account_id, NULL, false, &err))  {
3603                         EM_DEBUG_EXCEPTION("emstorage_delete_attachment_all_on_db failed [%d]", err);
3604
3605                         goto FINISH_OFF;
3606                 }
3607
3608                 /*  delete mail contents from filesystem */
3609                 if (!emstorage_get_save_name(mailbox->account_id, 0, 0, NULL, buf, &err))  {
3610                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3611
3612                         goto FINISH_OFF;
3613                 }
3614
3615                 if (!emstorage_delete_dir(buf, &err))  {
3616                         EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3617
3618                 }
3619
3620                 /*  delete meeting request */
3621                 if (!emstorage_delete_meeting_request(mailbox->account_id, 0, NULL, false, &err))  {
3622                         EM_DEBUG_EXCEPTION("emstorage_delete_attachment_all_on_db failed [%d]", err);
3623         
3624                         goto FINISH_OFF;
3625                 }
3626
3627                 emcore_check_unread_mail();
3628         }
3629         
3630         ret = true;
3631
3632 FINISH_OFF:
3633         if (search_handle >= 0)  {
3634                 if (!emstorage_mail_search_end(search_handle, true, &err))
3635                         EM_DEBUG_EXCEPTION("emstorage_mail_search_end failed [%d]", err);
3636         }
3637         
3638         EM_SAFE_FREE(mail_ids);
3639         if (err_code != NULL)
3640                 *err_code = err;
3641         EM_DEBUG_FUNC_END("ret [%d], err [%d]", ret, err);
3642         return ret;
3643 }
3644
3645 INTERNAL_FUNC int emcore_delete_mail_from_local_storage(int account_id, int *mail_ids, int num, int noti_param_1, int noti_param_2, int *err_code)
3646 {
3647         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_ids[%p], num [%d], noti_param_1 [%d], noti_param_2 [%d], err_code[%p]", account_id, mail_ids, num, noti_param_1, noti_param_2, num, err_code);
3648         int ret = false, err = EMF_ERROR_NONE, i;
3649         emstorage_mail_tbl_t *result_mail_list;
3650         char mail_id_string[10], *noti_param_string = NULL, buf[512] = {0, };
3651
3652         /* Getting mail list by using select mail_id [in] */
3653
3654         if(!emstorage_get_mail_field_by_multiple_mail_id(mail_ids, num, RETRIEVE_SUMMARY, &result_mail_list, true, &err) || !result_mail_list) {
3655                 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_multiple_mail_id failed [%d]", err);
3656                 goto FINISH_OFF;
3657         }
3658
3659         /* Deleting mails by using select mail_id [in] */
3660         if(!emstorage_delete_multiple_mails(mail_ids, num, true, &err)) {
3661                 EM_DEBUG_EXCEPTION("emstorage_delete_multiple_mails failed [%d]", err);
3662                 goto FINISH_OFF;
3663         }
3664
3665         /* Sending Notification */
3666         noti_param_string = em_malloc(sizeof(char) * 10 * num);
3667
3668         if(!noti_param_string) {
3669                 EM_DEBUG_EXCEPTION("em_malloc failed");
3670                 err = EMF_ERROR_OUT_OF_MEMORY;
3671                 goto FINISH_OFF;
3672         }
3673
3674         for(i = 0; i < num; i++) {
3675                 memset(mail_id_string, 0, 10);
3676                 SNPRINTF(mail_id_string, 10, "%d,", mail_ids[i]);
3677                 strcat(noti_param_string, mail_id_string);
3678                 /* can be optimized by appending sub string with directly pointing on string array kyuho.jo 2011-10-07 */
3679         }
3680
3681         if (!emstorage_notify_storage_event(NOTI_MAIL_DELETE, account_id, noti_param_1, noti_param_string, noti_param_2))
3682                 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event failed [ NOTI_MAIL_DELETE_FINISH ] >>>> ");
3683
3684         /* Updating Thread informations */
3685         /* Thread information should be updated as soon as possible. */
3686         for(i = 0; i < num; i++) {
3687                 if (result_mail_list[i].thread_item_count > 1)  {
3688                         if (!emstorage_update_latest_thread_mail(account_id, result_mail_list[i].thread_id, 0, 0, false,  &err)) {
3689                                 EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
3690                 
3691                                 goto FINISH_OFF;
3692                         }
3693                 }
3694         }
3695         if (!emstorage_notify_storage_event(NOTI_MAIL_DELETE_FINISH, account_id, noti_param_1, noti_param_string, noti_param_2))
3696                 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event failed [ NOTI_MAIL_DELETE_FINISH ] >>>> ");
3697
3698         for(i = 0; i < num; i++) {
3699                 /* Deleting attachments */
3700                 if (!emstorage_delete_attachment_on_db(result_mail_list[i].mail_id, 0, false, &err)) {
3701                         EM_DEBUG_EXCEPTION("emstorage_delete_attachment_on_db failed [%d]", err);
3702                         if (err == EMF_ERROR_ATTACHMENT_NOT_FOUND)
3703                                 err = EMF_ERROR_NONE;
3704                 }
3705
3706                 /* Deleting Directories */
3707                 /*  delete mail contents from filesystem */
3708                 if (!emstorage_get_save_name(account_id, result_mail_list[i].mail_id, 0, NULL, buf, &err)) {
3709                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3710
3711                         goto FINISH_OFF;
3712                 }
3713
3714                 if (!emstorage_delete_dir(buf, &err)) {
3715                         EM_DEBUG_EXCEPTION("emstorage_delete_dir failed [%d]", err);
3716
3717                 }
3718                 
3719                 /* Deleting Meeting Request */
3720                 if (!emstorage_delete_meeting_request(account_id, result_mail_list[i].mail_id, NULL, false, &err)) {
3721                         EM_DEBUG_EXCEPTION("emstorage_delete_meeting_request failed [%d]", err);
3722                         if (err != EMF_ERROR_CONTACT_NOT_FOUND) {
3723                                 goto FINISH_OFF;
3724                         }
3725                 }
3726         }
3727
3728         ret = true;
3729
3730 FINISH_OFF:
3731         EM_SAFE_FREE(noti_param_string);
3732
3733         if (err_code != NULL)
3734                 *err_code = err;
3735
3736         EM_DEBUG_FUNC_END();
3737         return ret;
3738 }
3739
3740 int emcore_delete_mail_from_server(emf_account_t *account, emf_mailbox_t *mailbox, int msgno, int *err_code)
3741 {
3742         EM_DEBUG_FUNC_BEGIN("account[%p], mailbox[%p], msgno[%d], err_code[%p]", account, mailbox, msgno, err_code);
3743
3744         int ret = false;
3745         int err = EMF_ERROR_NONE;
3746         
3747         if (!account || !mailbox) {
3748                 EM_DEBUG_EXCEPTION("account[%p], mailbox[%p], msgno[%d]", account, mailbox, msgno);
3749
3750                 err = EMF_ERROR_INVALID_PARAM;
3751                 goto FINISH_OFF;
3752         }
3753
3754         if (account->receiving_server_type == EMF_SERVER_TYPE_POP3) {
3755                 if (!pop3_mail_delete(mailbox->mail_stream, msgno, &err)) {
3756                         EM_DEBUG_EXCEPTION("pop3_mail_delete failed [%d]", err);
3757
3758                         if (err == EMF_ERROR_POP3_DELE_FAILURE)
3759                                 err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3760                         goto FINISH_OFF;
3761                 }
3762         }
3763         else {          /*  EMF_SERVER_TYPE_IMAP4 */
3764                 if (!imap4_mail_delete(mailbox->mail_stream, msgno, &err)) {
3765                         EM_DEBUG_EXCEPTION("imap4_mail_delete failed [%d]", err);
3766
3767                         if (err == EMF_ERROR_IMAP4_STORE_FAILURE)
3768                                 err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3769                         goto FINISH_OFF;
3770                 }
3771         }
3772         
3773         ret = true;
3774
3775 FINISH_OFF: 
3776         if (err_code)
3777                 *err_code = err;
3778         EM_DEBUG_FUNC_END();
3779         return ret;
3780 }
3781
3782 INTERNAL_FUNC int emcore_get_mail_msgno_by_uid(emf_account_t *account, emf_mailbox_t *mailbox, char *uid, int *msgno, int *err_code)
3783 {
3784         EM_DEBUG_FUNC_BEGIN("account[%p], mailbox[%p], uid[%s], msgno[%p], err_code[%p]", account, mailbox, uid, msgno, err_code);
3785
3786         int ret = false;
3787         int err = EMF_ERROR_NONE;
3788         
3789         emcore_uid_list *uid_list = NULL;
3790         emcore_uid_list *pTemp_uid_list = NULL;
3791         
3792         if (!account || !mailbox || !uid || !msgno)  {
3793                 EM_DEBUG_EXCEPTION("account[%p], mailbox[%p], uid[%s], msgno[%p]", account, mailbox, uid, msgno);
3794                 err = EMF_ERROR_INVALID_PARAM;
3795                 goto FINISH_OFF;
3796         }
3797         
3798         uid_list = mailbox->user_data;
3799         
3800         if (uid_list == NULL)  {
3801                 if (account->receiving_server_type == EMF_SERVER_TYPE_POP3)  {
3802                         if (!pop3_mailbox_get_uids(mailbox->mail_stream, &uid_list, &err))  {
3803                                 EM_DEBUG_EXCEPTION("pop3_mailbox_get_uids failed [%d]", err);
3804                                 goto FINISH_OFF;
3805                         }
3806                 }
3807                 else  {         /*  EMF_SERVER_TYPE_IMAP4 */
3808                         if (!imap4_mailbox_get_uids(mailbox->mail_stream, &uid_list, &err))  {
3809                                 EM_DEBUG_EXCEPTION("imap4_mailbox_get_uids failed [%d]", err);
3810                                 goto FINISH_OFF;
3811                         }
3812                 }
3813                 mailbox->user_data = uid_list;
3814         }
3815         pTemp_uid_list = uid_list;
3816         while (uid_list)  {
3817                 if (!strcmp(uid_list->uid, uid))  {
3818                         *msgno = uid_list->msgno;
3819                         EM_DEBUG_LOG("uid_list->msgno[%d]", uid_list->msgno);
3820                         ret = true;
3821                         goto FINISH_OFF;
3822                 }
3823                 EM_DEBUG_LOG("other uid_list->msgno[%d]", uid_list->msgno);
3824                 uid_list = uid_list->next;
3825         }
3826         
3827         err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER;
3828
3829 FINISH_OFF: 
3830         if (err_code != NULL)
3831                 *err_code = err;
3832         uid_list = pTemp_uid_list ;
3833         if (uid_list != NULL)
3834                 emcore_free_uids(uid_list, NULL);
3835         /*  mailbox->user_data and uid_list both point to same memory address, So when uid_list  is freed then just set  */
3836         /*  mailbox->user_data to NULL and dont use EM_SAFE_FREE, it will crash  : ) */
3837         if (mailbox)
3838         mailbox->user_data = NULL;
3839         EM_DEBUG_FUNC_END();
3840         return ret;
3841 }
3842
3843 /* description 
3844  *    add a attachment to mail.
3845  * arguments  
3846  *    mailbox  :  mail box
3847  *    mail_id  :  mail id
3848  *    attachment  :  attachment to be added
3849  * return  
3850  *    succeed  :  1
3851  *    fail  :  0
3852  */
3853 INTERNAL_FUNC int emcore_mail_add_attachment(int mail_id, emf_attachment_info_t *attachment, int *err_code)
3854 {
3855         EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment[%p], err_code[%p]", mail_id, attachment, err_code);
3856         
3857         if (attachment == NULL)  {
3858                 EM_DEBUG_EXCEPTION("mail_id[%d], attachment[%p]", mail_id, attachment);
3859                 if (err_code)
3860                         *err_code = EMF_ERROR_INVALID_PARAM;
3861                 return false;
3862         }
3863         
3864         int ret = false, err = EMF_ERROR_NONE;
3865         emstorage_mail_tbl_t *mail_table_data = NULL;
3866         int attachment_id = 0;
3867         
3868
3869         
3870         if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data)  {
3871                 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
3872
3873                 goto FINISH_OFF2;
3874         }
3875         
3876         int account_id = mail_table_data->account_id;
3877         emstorage_attachment_tbl_t attachment_tbl;
3878         
3879         memset(&attachment_tbl, 0x00, sizeof(emstorage_attachment_tbl_t));
3880
3881         mail_table_data->attachment_count               = mail_table_data->attachment_count + 1;
3882         attachment_tbl.account_id                       = mail_table_data->account_id;
3883         attachment_tbl.mailbox_name                     = mail_table_data->mailbox_name;
3884         attachment_tbl.mail_id                          = mail_id;
3885         attachment_tbl.attachment_name                  = attachment->name;
3886         attachment_tbl.attachment_size                  = attachment->size;
3887         attachment_tbl.attachment_save_status           = attachment->downloaded;
3888         attachment_tbl.attachment_drm_type          = attachment->drm;
3889         attachment_tbl.attachment_inline_content_status = attachment->inline_content;
3890
3891         /*  BEGIN TRANSACTION; */
3892         emstorage_begin_transaction(NULL, NULL, NULL);
3893
3894         if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err))  {
3895                 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
3896
3897                 goto FINISH_OFF;
3898         }
3899         
3900         attachment->attachment_id = attachment_tbl.attachment_id;
3901
3902         if (attachment->savename)  {
3903                 char buf[512];
3904
3905                 if (!attachment->inline_content) {
3906                         if (!emstorage_create_dir(account_id, mail_id, attachment_tbl.attachment_id, &err))  {
3907                                 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
3908                                 goto FINISH_OFF;
3909                         }
3910                         attachment_id = attachment_tbl.attachment_id;
3911                 }
3912
3913                 if (!emstorage_get_save_name(account_id, mail_id, attachment_id, attachment->name, buf, &err))  {
3914                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
3915                         goto FINISH_OFF;
3916                 }
3917         attachment_tbl.attachment_path = buf;
3918
3919                 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
3920                         EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
3921
3922                         goto FINISH_OFF;
3923                 }
3924
3925                 if (!emstorage_change_mail_field(mail_id, APPEND_BODY, mail_table_data, false, &err)) {
3926                         EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
3927         
3928                         goto FINISH_OFF;
3929                 }
3930
3931                 if (attachment->downloaded) {
3932                         if (!emstorage_move_file(attachment->savename, buf, false, &err))  {
3933                                 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
3934
3935                                 goto FINISH_OFF;
3936                         }
3937                 }
3938
3939                 /* Here only filename is being updated. Since first add is being done there will not be any old files.
3940                     So no need to check for old files in this update case */
3941                 if (!emstorage_change_attachment_field(mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
3942                         EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
3943         
3944                         goto FINISH_OFF;
3945                 }
3946
3947                 EM_SAFE_FREE(attachment->savename);
3948                 attachment->savename = EM_SAFE_STRDUP(buf);
3949         }
3950         
3951         ret = true;
3952         
3953 FINISH_OFF: 
3954         if (ret == true) {      /*  COMMIT TRANSACTION; */
3955                 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
3956                         err = EMF_ERROR_DB_FAILURE;
3957                         ret = false;
3958                 }
3959         }
3960         else {  /*  ROLLBACK TRANSACTION; */
3961                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
3962                         err = EMF_ERROR_DB_FAILURE;
3963         }       
3964
3965 FINISH_OFF2: 
3966         if (mail_table_data != NULL)
3967                 emstorage_free_mail(&mail_table_data, 1, NULL);
3968
3969         if (err_code)
3970                 *err_code = err;
3971         EM_DEBUG_FUNC_END("err [%d]", err);
3972         return ret;
3973 }
3974
3975
3976 INTERNAL_FUNC int emcore_mail_add_attachment_data(int input_mail_id, emf_attachment_data_t *input_attachment_data)
3977 {
3978         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_data[%p]", input_mail_id, input_attachment_data);
3979
3980         int                        err             = EMF_ERROR_NONE;
3981         int                        attachment_id   = 0;
3982         char                       buf[512] = { 0, };
3983         emstorage_mail_tbl_t            *mail_table_data = NULL;
3984         emstorage_attachment_tbl_t  attachment_tbl  = { 0 };
3985         
3986         if (input_attachment_data == NULL)  {
3987                 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
3988                 return EMF_ERROR_INVALID_PARAM;
3989         }
3990
3991         if (!emstorage_get_mail_field_by_id(input_mail_id, RETRIEVE_SUMMARY, &mail_table_data, true, &err) || !mail_table_data)  {
3992                 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
3993
3994                 goto FINISH_OFF2;
3995         }
3996
3997         mail_table_data->attachment_count               = mail_table_data->attachment_count + 1;
3998
3999         attachment_tbl.account_id                       = mail_table_data->account_id;
4000         attachment_tbl.mailbox_name                     = mail_table_data->mailbox_name;
4001         attachment_tbl.mail_id                          = input_mail_id;
4002         attachment_tbl.attachment_name                  = input_attachment_data->attachment_name;
4003         attachment_tbl.attachment_size                  = input_attachment_data->attachment_size;
4004         attachment_tbl.attachment_save_status           = input_attachment_data->save_status;
4005         attachment_tbl.attachment_drm_type          = input_attachment_data->drm_status;
4006         attachment_tbl.attachment_inline_content_status = input_attachment_data->inline_content_status;
4007         
4008         /*  BEGIN TRANSACTION; */
4009         emstorage_begin_transaction(NULL, NULL, NULL);
4010
4011         if (!emstorage_add_attachment(&attachment_tbl, 0, false, &err))  {
4012                 EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
4013
4014                 goto FINISH_OFF;
4015         }
4016         
4017         input_attachment_data->attachment_id = attachment_tbl.attachment_id;
4018
4019         if (input_attachment_data->attachment_path)  {
4020                 if (!input_attachment_data->inline_content_status) {
4021                         if (!emstorage_create_dir(mail_table_data->account_id, input_mail_id, attachment_tbl.attachment_id, &err))  {
4022                                 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4023                                 goto FINISH_OFF;
4024                         }
4025                         attachment_id = attachment_tbl.attachment_id;
4026                 }
4027
4028                 if (!emstorage_get_save_name(mail_table_data->account_id, input_mail_id, attachment_id, input_attachment_data->attachment_name, buf, &err))  {
4029                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4030                         goto FINISH_OFF;
4031                 }
4032         attachment_tbl.attachment_path = buf;
4033
4034                 if (!emstorage_change_attachment_field(input_mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
4035                         EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
4036
4037                         goto FINISH_OFF;
4038                 }
4039
4040                 if (!emstorage_change_mail_field(input_mail_id, APPEND_BODY, mail_table_data, false, &err)) {
4041                         EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
4042         
4043                         goto FINISH_OFF;
4044                 }
4045
4046                 if (input_attachment_data->save_status) {
4047                         if (!emstorage_move_file(input_attachment_data->attachment_path, buf, false, &err))  {
4048                                 EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
4049
4050                                 goto FINISH_OFF;
4051                         }
4052                 }
4053                 
4054                 /* Here only filename is being updated. Since first add is being done there will not be any old files.
4055                     So no need to check for old files in this update case */
4056                 if (!emstorage_change_attachment_field(input_mail_id, UPDATE_SAVENAME, &attachment_tbl, false, &err))  {
4057                         EM_DEBUG_EXCEPTION("emstorage_change_attachment_field failed [%d]", err);
4058         
4059                         goto FINISH_OFF;
4060                 }
4061                 
4062                 EM_SAFE_FREE(input_attachment_data->attachment_path);
4063                 input_attachment_data->attachment_path = EM_SAFE_STRDUP(buf);
4064         }
4065         
4066 FINISH_OFF:
4067         if (err == EMF_ERROR_NONE) {    /*  COMMIT TRANSACTION; */
4068                 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4069                         err = EMF_ERROR_DB_FAILURE;
4070                 }
4071         }
4072         else {  /*  ROLLBACK TRANSACTION; */
4073                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4074                         err = EMF_ERROR_DB_FAILURE;
4075         }       
4076         
4077 FINISH_OFF2:
4078         if (mail_table_data != NULL)
4079                 emstorage_free_mail(&mail_table_data, 1, NULL);
4080         
4081         EM_DEBUG_FUNC_END("err [%d]", err);
4082         return err;
4083 }
4084
4085
4086 /* description
4087  *    delete a attachment from mail.
4088  * arguments
4089  *    mailbox  :  mail box
4090  *    mail_id  :  mail id
4091  *    attachment_id  :  number string of attachment-id to be deleted
4092  *                 (ex :  if attachment id is 2, number stirng will be "2")
4093  * return
4094  *    succeed  :  1
4095  *    fail  :  0
4096  */
4097 int emcore_delete_mail_attachment(int mail_id,    char *attachment_id_string, int *err_code)
4098 {
4099         EM_DEBUG_FUNC_BEGIN("mail_id[%d], attachment_id_string[%s], err_code[%p]", mail_id, attachment_id_string, err_code);
4100         
4101         if (attachment_id_string == NULL)  {
4102                 EM_DEBUG_EXCEPTION("mail_id[%d], attachment_id[%p]", mail_id, attachment_id_string);
4103                 if (err_code != NULL)
4104                         *err_code = EMF_ERROR_INVALID_PARAM;
4105                 return false;
4106         }
4107
4108         int ret = false;
4109         int error = EMF_ERROR_NONE;
4110         int attachment_id = atoi(attachment_id_string);
4111         
4112         /*  BEGIN TRANSACTION; */
4113         emstorage_begin_transaction(NULL, NULL, NULL);
4114         
4115         if (!emstorage_delete_attachment_on_db(mail_id, attachment_id, false, &error))  {
4116                 EM_DEBUG_EXCEPTION("emstorage_delete_attachment_on_db failed [%d]", error);
4117                 goto FINISH_OFF;
4118         }
4119         
4120         ret = true;
4121         
4122 FINISH_OFF:
4123         if (ret == true) {      /*  COMMIT TRANSACTION; */
4124                 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4125                         error = EMF_ERROR_DB_FAILURE;
4126                         ret = false;
4127                 }
4128         }
4129         else {  /*  ROLLBACK TRANSACTION; */
4130                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4131                         error = EMF_ERROR_DB_FAILURE;
4132         }
4133
4134         if (err_code != NULL)
4135                 *err_code = error;
4136         EM_DEBUG_FUNC_END("ret [%d]", ret);
4137         return ret;
4138 }
4139
4140 static int emcore_mail_update_attachment_data(int input_mail_id, emf_attachment_data_t *input_attachment_data)
4141 {
4142         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_data[%p]", input_mail_id, input_attachment_data);
4143
4144         int                         err = EMF_ERROR_NONE;
4145         int                         attachment_id = 0;
4146         char                        buf[512] = { 0 , };
4147         emstorage_attachment_tbl_t *existing_attachment_info = NULL;
4148         emstorage_attachment_tbl_t  attachment_tbl = { 0 };
4149
4150         if (input_attachment_data == NULL)  {
4151                 EM_DEBUG_EXCEPTION("Invalid Parameter");
4152                 err = EMF_ERROR_INVALID_PARAM;
4153                 goto FINISH_OFF2;
4154         }
4155
4156         if (!emstorage_get_attachment(input_mail_id, input_attachment_data->attachment_id, &existing_attachment_info, 1, &err)) {
4157                 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
4158
4159                 goto FINISH_OFF2;
4160         }
4161         
4162         attachment_tbl.mail_id                          = input_mail_id;
4163         attachment_tbl.account_id                       = existing_attachment_info->account_id;
4164         attachment_tbl.mailbox_name                     = existing_attachment_info->mailbox_name;
4165         attachment_tbl.attachment_name                  = input_attachment_data->attachment_name;
4166         attachment_tbl.attachment_size                  = input_attachment_data->attachment_size;
4167         attachment_tbl.attachment_path                  = input_attachment_data->attachment_path;
4168         attachment_tbl.attachment_save_status           = input_attachment_data->save_status;
4169         attachment_tbl.attachment_drm_type          = input_attachment_data->drm_status;
4170         attachment_tbl.attachment_inline_content_status = input_attachment_data->inline_content_status;
4171         attachment_tbl.attachment_id                    = input_attachment_data->attachment_id;
4172
4173         if (!input_attachment_data->inline_content_status) {
4174                 if (!emstorage_create_dir(attachment_tbl.account_id, input_mail_id, attachment_tbl.attachment_id, &err))  {
4175                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4176                         goto FINISH_OFF;
4177                 }
4178                 attachment_id = attachment_tbl.attachment_id;
4179         }
4180         
4181         if (!emstorage_get_save_name(attachment_tbl.account_id, input_mail_id, attachment_id, input_attachment_data->attachment_name, buf, &err))  {
4182                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4183                 goto FINISH_OFF2;
4184         }
4185     attachment_tbl.attachment_path = buf;
4186
4187         EM_DEBUG_LOG("downloaded [%d], savename [%s], attachment_path [%s]", input_attachment_data->save_status, input_attachment_data->attachment_path, existing_attachment_info->attachment_path);
4188         if (input_attachment_data->save_status && EM_SAFE_STRCMP(input_attachment_data->attachment_path, existing_attachment_info->attachment_path) != 0) {
4189                 if (!emstorage_move_file(input_attachment_data->attachment_path, buf, false ,&err))  {
4190                         EM_DEBUG_EXCEPTION("emstorage_move_file failed [%d]", err);
4191
4192                         goto FINISH_OFF2;
4193                 }
4194         }
4195         else
4196                 EM_DEBUG_LOG("no need to move");
4197
4198         EM_SAFE_FREE(input_attachment_data->attachment_path);
4199         input_attachment_data->attachment_path = EM_SAFE_STRDUP(buf);
4200
4201         emstorage_begin_transaction(NULL, NULL, NULL);
4202
4203         if (!emstorage_update_attachment(&attachment_tbl, false, &err))  {
4204                 EM_DEBUG_EXCEPTION("emstorage_update_attachment failed [%d]", err);
4205
4206                 goto FINISH_OFF;
4207         }
4208         
4209 FINISH_OFF:
4210         if (err == EMF_ERROR_NONE) {    /*  COMMIT TRANSACTION; */
4211                 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4212                         err = EMF_ERROR_DB_FAILURE;
4213                 }
4214         }
4215         else {  /*  ROLLBACK TRANSACTION; */
4216                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4217                         err = EMF_ERROR_DB_FAILURE;
4218         }
4219
4220 FINISH_OFF2:
4221         if (existing_attachment_info)
4222                 emstorage_free_attachment(&existing_attachment_info, 1, NULL);
4223
4224         EM_DEBUG_FUNC_END("err [%d]", err);
4225         return err;
4226 }
4227
4228
4229 static int emcore_mail_compare_filename_of_attachment_data(int input_mail_id, int input_attachment_a_id, emf_attachment_data_t *input_attachment_b_data, int *result)
4230 {
4231         EM_DEBUG_FUNC_BEGIN("input_mail_id[%d], input_attachment_a_id[%d], input_attachment_b_data[%p], result[%p]", input_mail_id, input_attachment_a_id, input_attachment_b_data, result);
4232
4233         EM_IF_NULL_RETURN_VALUE(input_attachment_b_data, false);
4234         EM_IF_NULL_RETURN_VALUE(result, false);
4235
4236         int err, err_2, ret = EMF_ERROR_NONE;
4237         emstorage_attachment_tbl_t *attachment_a_tbl = NULL;
4238
4239         if (!emstorage_get_attachment(input_mail_id, input_attachment_a_id, &attachment_a_tbl, 1, &err)) {
4240                 EM_DEBUG_EXCEPTION("emstorage_get_attachment failed [%d]", err);
4241
4242                 goto FINISH_OFF;
4243         }
4244
4245         if (attachment_a_tbl->attachment_name && input_attachment_b_data->attachment_name) {
4246                 EM_DEBUG_LOG("attachment_a_tbl->attachment_name [%s], input_attachment_b_data->name [%s]", attachment_a_tbl->attachment_name, input_attachment_b_data->attachment_name);
4247                 *result = strcmp(attachment_a_tbl->attachment_name, input_attachment_b_data->attachment_name);
4248         }
4249
4250         ret = true;
4251
4252 FINISH_OFF: 
4253
4254         if (attachment_a_tbl)
4255                 emstorage_free_attachment(&attachment_a_tbl, 1, &err_2);
4256         EM_DEBUG_FUNC_END("*result [%d]", *result);
4257         return ret;
4258 }
4259
4260
4261 /* description 
4262  *    copy a mail to mail box
4263  * arguments  
4264  *    src_mailbox  :  source mail box
4265  *    msgno  :  mail sequence
4266  *    dst_mailbox  :  target mail box
4267  * return  
4268  *    succeed  :  1
4269  *    fail  :  0
4270  */
4271 INTERNAL_FUNC int emcore_mail_copy(int mail_id, emf_mailbox_t *dst_mailbox, int *err_code)
4272 {
4273         EM_DEBUG_FUNC_BEGIN("mail_id[%d], dst_mailbox[%p], err_code[%p]", mail_id, dst_mailbox, err_code);
4274         
4275         int ret = false;
4276         int err = EMF_ERROR_NONE;
4277         int i;
4278         emstorage_mail_tbl_t *mail = NULL;
4279         emstorage_attachment_tbl_t *atch_list = NULL;
4280         char buf[512];
4281         int count = EMF_ATTACHMENT_MAX_COUNT;
4282         char *mailbox_name = NULL;
4283
4284         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
4285                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
4286
4287                 goto FINISH_OFF;
4288         }
4289
4290         if ( (err = emstorage_get_attachment_list(mail_id, true, &atch_list, &count)) != EMF_ERROR_NONE ){
4291                 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed [%d]", err);
4292
4293                 goto FINISH_OFF;
4294         }
4295
4296         /*  get increased uid. */
4297         if (!emstorage_increase_mail_id(&mail->mail_id, true, &err))  {
4298                 EM_DEBUG_EXCEPTION("emstorage_increase_mail_id failed [%d]", err);
4299
4300                 goto FINISH_OFF;
4301         }
4302
4303         /*  copy mail body(text) file */
4304         if (mail->file_path_plain)  {
4305                 if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, 0, &err))  {
4306                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4307
4308                         goto FINISH_OFF;
4309                 }
4310
4311                 gchar *filename = g_path_get_basename(mail->file_path_plain);
4312
4313                 if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, 0, filename, buf, &err))  {
4314                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4315                         g_free(filename);
4316
4317                         goto FINISH_OFF;
4318                 }
4319
4320                 g_free(filename);
4321
4322                 if (!emstorage_copy_file(mail->file_path_plain, buf, false, &err))  {
4323                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4324
4325                         goto FINISH_OFF;
4326                 }
4327
4328                 mail->file_path_plain = EM_SAFE_STRDUP(buf);
4329         }
4330
4331         /*  copy mail body(html) file */
4332         if (mail->file_path_html)  {
4333                 if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, 0, &err))  {
4334                         EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4335
4336                         goto FINISH_OFF;
4337                 }
4338
4339                 gchar *filename = g_path_get_basename(mail->file_path_html);
4340
4341                 if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, 0, filename, buf, &err))  {
4342                         EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4343                         g_free(filename);
4344
4345                         goto FINISH_OFF;
4346                 }
4347
4348                 g_free(filename);
4349
4350                 if (!emstorage_copy_file(mail->file_path_html, buf, false, &err))  {
4351                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4352         
4353                         goto FINISH_OFF;
4354                 }
4355
4356                 mail->file_path_html = EM_SAFE_STRDUP(buf);
4357         }
4358
4359         /*  BEGIN TRANSACTION; */
4360         emstorage_begin_transaction(NULL, NULL, NULL);
4361
4362         /*  insert mail data */
4363         EM_SAFE_FREE(mail->mailbox_name);
4364         
4365         mail->account_id   = dst_mailbox->account_id;
4366         mail->mailbox_id   = dst_mailbox->mailbox_id;
4367         mail->mailbox_name = EM_SAFE_STRDUP(dst_mailbox->name);
4368         mail->mailbox_type = dst_mailbox->mailbox_type;
4369         
4370         if (!emstorage_add_mail(mail, 0, false, &err))  {
4371                 EM_DEBUG_EXCEPTION("emstorage_add_mail failed [%d]", err);
4372
4373                 if (mail->file_path_plain)  {
4374                         if (!emstorage_delete_file(mail->file_path_plain, &err))  {
4375                                 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4376                                 emstorage_rollback_transaction(NULL, NULL, NULL);
4377                                 goto FINISH_OFF;
4378                         }
4379                 }
4380                 if (mail->file_path_html) {
4381                         if (!emstorage_delete_file(mail->file_path_html, &err))  {
4382                                 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4383                                 emstorage_rollback_transaction(NULL, NULL, NULL);
4384                                 goto FINISH_OFF;
4385                         }
4386                 }
4387
4388                 /*  ROLLBACK TRANSACTION; */
4389                 emstorage_rollback_transaction(NULL, NULL, NULL);
4390
4391
4392                 goto FINISH_OFF;
4393         }
4394
4395         /*  copy attachment file */
4396         for (i = 0; i<count; i++)  {
4397                 if (atch_list[i].attachment_path)  {
4398                         if (!emstorage_create_dir(dst_mailbox->account_id, mail->mail_id, i+1, &err))  {
4399                                 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4400                                 break;
4401                         }
4402                         
4403                         if (!emstorage_get_save_name(dst_mailbox->account_id, mail->mail_id, i+1, atch_list[i].attachment_name, buf, &err))  {
4404                                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4405                                 break;
4406                         }
4407
4408                         if (!emstorage_copy_file(atch_list[i].attachment_path, buf, false, &err))  {
4409                                 EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4410                                 break;
4411                         }
4412
4413                         EM_SAFE_FREE(atch_list[i].attachment_path);
4414
4415                         atch_list[i].attachment_path = EM_SAFE_STRDUP(buf);
4416                 }
4417
4418                 atch_list[i].account_id = dst_mailbox->account_id;
4419                 atch_list[i].mail_id = mail->mail_id;
4420
4421                 EM_SAFE_FREE(atch_list[i].mailbox_name);
4422
4423                 atch_list[i].mailbox_name = EM_SAFE_STRDUP(mail->mailbox_name);
4424
4425                 if (!emstorage_add_attachment(&atch_list[i], 0, false, &err))  {
4426                         EM_DEBUG_EXCEPTION("emstorage_add_attachment failed [%d]", err);
4427
4428                         break;
4429                 }
4430         }
4431
4432         /*  in case error happened, delete copied file. */
4433         if (i && i != count)  {
4434                 for (;i >= 0; i--)  {
4435                         if (atch_list[i].attachment_path)  {
4436                         
4437                                 if (!emstorage_delete_file(atch_list[i].attachment_path, &err))  {
4438                                         EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4439                                         emstorage_rollback_transaction(NULL, NULL, NULL);
4440                                         goto FINISH_OFF;
4441                                 }
4442                         }
4443                 }
4444
4445                 if (mail->file_path_plain)  {
4446                         if (!emstorage_delete_file(mail->file_path_plain, &err))  {
4447                                 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4448                                 emstorage_rollback_transaction(NULL, NULL, NULL);
4449                                 goto FINISH_OFF;
4450                         }
4451                 }
4452                 if (mail->file_path_html)  {
4453                         if (!emstorage_delete_file(mail->file_path_html, &err))  {
4454                                 EM_DEBUG_EXCEPTION("emstorage_delete_file failed [%d]", err);
4455                                 emstorage_rollback_transaction(NULL, NULL, NULL);
4456                                 goto FINISH_OFF;
4457                         }
4458                 }
4459
4460                 /*  ROLLBACK TRANSACTION; */
4461                 emstorage_rollback_transaction(NULL, NULL, NULL);
4462                 goto FINISH_OFF;
4463         }
4464
4465         if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4466                 err = EMF_ERROR_DB_FAILURE;
4467                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4468                         err = EMF_ERROR_DB_FAILURE;
4469                 goto FINISH_OFF;
4470         }
4471
4472         if (!emstorage_get_mailboxname_by_mailbox_type(dst_mailbox->account_id, EMF_MAILBOX_TYPE_INBOX, &mailbox_name, false, &err))  {
4473                 EM_DEBUG_EXCEPTION("emstorage_get_mailboxname_by_mailbox_type failed [%d]", err);
4474                 goto FINISH_OFF;
4475         }
4476
4477         if (!strcmp(dst_mailbox->name, mailbox_name) && !(mail->flags_seen_field))
4478                 emcore_check_unread_mail();
4479
4480         ret = true;
4481
4482 FINISH_OFF: 
4483         if (atch_list != NULL)
4484                 emstorage_free_attachment(&atch_list, count, NULL);
4485
4486         if (mail != NULL)
4487                 emstorage_free_mail(&mail, 1, NULL);
4488
4489         EM_SAFE_FREE(mailbox_name);
4490
4491         if (err_code != NULL)
4492                 *err_code = err;
4493         EM_DEBUG_FUNC_END("err [%d]", err);
4494         return ret;
4495 }
4496
4497 /* description
4498  *    move a mail to mail box
4499  * arguments
4500  *    old_mailbox  :  previous mail box
4501  *    msgno  :  msgno
4502  *    new_mailbox  :  target mail box
4503  * return
4504  *    succeed  :  1
4505  *    fail  :  0
4506  */
4507 INTERNAL_FUNC int emcore_mail_move(int mail_ids[], int mail_ids_count, char *dst_mailbox_name, int noti_param_1, int noti_param_2 ,int *err_code)
4508 {
4509         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], mail_ids_count[%d], dst_mailbox_name[%s], noti_param [%d], err_code[%p]", mail_ids, mail_ids_count, dst_mailbox_name, noti_param_1, err_code);
4510
4511         int ret = false;
4512         int err = EMF_ERROR_NONE;
4513         emstorage_mail_tbl_t *mail_list = NULL;
4514         int account_id = 0;
4515         int i = 0, parameter_string_length = 0;
4516         char *parameter_string = NULL, mail_id_string[10];
4517         emstorage_attachment_tbl_t attachment;
4518
4519         if (!dst_mailbox_name && mail_ids_count < 1) {
4520                 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
4521                 err = EMF_ERROR_INVALID_PARAM;
4522                 goto FINISH_OFF;
4523         }
4524
4525         if (!emstorage_get_mail_field_by_multiple_mail_id(mail_ids, mail_ids_count, RETRIEVE_FLAG, &mail_list, true, &err) || !mail_list) {
4526                 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_multiple_mail_id failed [%d]", err);
4527
4528                 goto FINISH_OFF;
4529         }
4530
4531         account_id = mail_list[0].account_id;
4532
4533         if(!emstorage_move_multiple_mails(account_id, dst_mailbox_name, mail_ids, mail_ids_count, true, &err)) {
4534                 EM_DEBUG_EXCEPTION("emstorage_move_multiple_mails failed [%d]", err);
4535                 goto FINISH_OFF;
4536         }
4537         
4538         /* Sending a notification */
4539         parameter_string_length = sizeof(char) * (mail_ids_count * 10 + 128/*MAILBOX_LEN_IN_MAIL_TBL*/ * 2);
4540         parameter_string = em_malloc(parameter_string_length);
4541
4542         if (mail_list[0].mailbox_name)
4543                 SNPRINTF(parameter_string, parameter_string_length, "%s%c%s%c", mail_list[0].mailbox_name, 0x01, dst_mailbox_name , 0x01);
4544
4545         if (parameter_string == NULL) {
4546                 EM_DEBUG_EXCEPTION("Memory allocation for mail_id_list_string failed");
4547                 err = EMF_ERROR_OUT_OF_MEMORY;
4548                 goto FINISH_OFF;
4549         }
4550
4551         for (i = 0; i < mail_ids_count; i++) {
4552                 memset(mail_id_string, 0, 10);
4553                 SNPRINTF(mail_id_string, 10, "%d,", mail_ids[i]);
4554                 strcat(parameter_string, mail_id_string);
4555         }
4556         
4557         EM_DEBUG_LOG("num : [%d], param string : [%s]", mail_ids_count , parameter_string);
4558
4559         if (!emstorage_notify_storage_event(NOTI_MAIL_MOVE, account_id, noti_param_1, parameter_string, noti_param_2))
4560                 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event failed [NOTI_MAIL_MOVE] >>>> ");
4561
4562         /* Upating thread mail info should be done as soon as possible */
4563         attachment.mailbox_name = dst_mailbox_name;
4564
4565         for (i = 0; i < mail_ids_count; i++) {
4566                 if (!emstorage_update_latest_thread_mail(account_id, mail_list[i].thread_id, 0, 0, true, &err))
4567                         EM_DEBUG_EXCEPTION("emstorage_update_latest_thread_mail failed [%d]", err);
4568         }
4569
4570         if (!emstorage_notify_storage_event(NOTI_MAIL_MOVE_FINISH, account_id, noti_param_1, parameter_string, noti_param_2))
4571                 EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event failed [NOTI_MAIL_MOVE_FINISH] >>>> ");
4572
4573         emcore_check_unread_mail();
4574
4575         ret = true;
4576
4577 FINISH_OFF:
4578         emstorage_free_mail(&mail_list, mail_ids_count, NULL);
4579         EM_SAFE_FREE(parameter_string);
4580         if (err_code != NULL)
4581                 *err_code = err;
4582         EM_DEBUG_FUNC_END("err [%d]", err);
4583         return ret;
4584 }
4585
4586
4587 INTERNAL_FUNC int emcore_move_mail_on_server(int account_id, char *src_mailbox,  int mail_ids[], int num, char *dest_mailbox, int *error_code)
4588 {
4589         EM_DEBUG_FUNC_BEGIN();
4590         MAILSTREAM *stream = NULL;
4591         int err_code = 0;
4592         emf_account_t *ref_account = NULL;
4593         emstorage_mail_tbl_t *mail = NULL;
4594         int ret = 1;
4595         int mail_id = 0;
4596         int i = 0;
4597         
4598         mail_id = mail_ids[0];
4599         
4600         ref_account = emcore_get_account_reference(account_id);
4601         if (!ref_account)  {
4602                 EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed  :  get account reference[%d]", account_id);
4603                 *error_code = EMF_ERROR_INVALID_ACCOUNT;
4604                 ret = 0;
4605                 goto FINISH_OFF;
4606         }
4607
4608                     /* if not imap4 mail, return */
4609         if (ref_account->account_bind_type != EMF_BIND_TYPE_EM_CORE ||
4610                 ref_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4) {
4611                 *error_code = EMF_ERROR_INVALID_PARAM;
4612                 ret = 0;
4613                 goto FINISH_OFF;
4614         }
4615
4616         for (i = 0; i < num; i++)  {
4617                 mail_id = mail_ids[i];
4618
4619                 if (!emstorage_get_mail_by_id(mail_id, &mail, false, &err_code) || !mail)  {
4620                         EM_DEBUG_EXCEPTION("emstorage_get_uid_by_mail_id  :  emstorage_get_downloaded_mail failed [%d]", err_code);
4621                         mail = NULL;
4622                         if (err_code == EMF_ERROR_MAIL_NOT_FOUND)  {    /*  not server mail */
4623                                 /* err = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER; */
4624                                 /* continue; */
4625                         }
4626                         else {
4627                                 *error_code = err_code;
4628                         }
4629
4630                         ret = 0;
4631                         goto FINISH_OFF;
4632                 }
4633
4634                 if (!emcore_connect_to_remote_mailbox(account_id, src_mailbox, (void **)&stream, &err_code))            /* faizan.h@samsung.com mail_move_fix_07042009 */ {
4635                         EM_DEBUG_EXCEPTION("emcore_move_mail_on_server failed :  Mailbox open[%d]", err_code);
4636
4637                         ret = 0;
4638                         goto FINISH_OFF;
4639                 }
4640
4641                 if (stream) {
4642                         /* set callback for COPY_UID */
4643                         mail_parameters(stream, SET_COPYUID, emcore_mail_copyuid);
4644
4645                         EM_DEBUG_LOG("calling mail_copy_full FODLER MAIL COPY ");
4646                         
4647                         if (mail->server_mail_id) {
4648                                 if (!mail_copy_full(stream, mail->server_mail_id, dest_mailbox, CP_UID | CP_MOVE)) {
4649                                         EM_DEBUG_EXCEPTION("emcore_move_mail_on_server :   Mail cannot be moved failed");
4650                                         ret = 0;
4651                                 }
4652                                 else {
4653                                         /*  send EXPUNGE command */
4654                                         if (!imap4_send_command(stream, IMAP4_CMD_EXPUNGE, &err_code)) {
4655                                                 EM_DEBUG_EXCEPTION("imap4_send_command failed [%d]", err_code);
4656
4657                                                 if (err_code == EMF_ERROR_IMAP4_STORE_FAILURE)
4658                                                         err_code = EMF_ERROR_MAIL_NOT_FOUND_ON_SERVER;
4659                                                 goto FINISH_OFF;
4660                                         }
4661
4662                                         /* faizan.h@samsung.com   duplicate copy while sync mailbox issue fixed */
4663                                         EM_DEBUG_LOG(">>>mailbox_name[%s]>>>>>>", mail->mailbox_name);
4664                                         EM_DEBUG_LOG(">>>g_new_server_uid[%s]>>>>>>", g_new_server_uid);
4665                                         EM_DEBUG_LOG(">>>mail_id[%d]>>>>>>", mail_id);
4666
4667                                         if (!emstorage_update_read_mail_uid(mail_id, g_new_server_uid, mail->mailbox_name, &err_code)) {
4668                                                 EM_DEBUG_EXCEPTION("emstorage_update_read_mail_uid failed [%d]", err_code);
4669                                                 goto FINISH_OFF;
4670                                         }
4671
4672                                         EM_DEBUG_LOG("Mail MOVE SUCCESS ");
4673                                 }
4674                         }
4675                         else
4676                                 EM_DEBUG_EXCEPTION(">>>> Server MAIL ID IS NULL >>>> ");
4677                 }
4678                 else {
4679                         EM_DEBUG_EXCEPTION(">>>> STREAM DATA IS NULL >>> ");
4680                         ret = 0;
4681                         goto FINISH_OFF;
4682                 }
4683         }
4684
4685         /* ret = true; */
4686
4687 FINISH_OFF:
4688         if (stream) emcore_close_mailbox(account_id, stream);
4689
4690         if (mail != NULL)
4691                 emstorage_free_mail(&mail, 1, NULL);
4692         EM_DEBUG_FUNC_END("ret [%d]", ret);
4693         return ret;
4694 }
4695
4696 static int emcore_save_mail_file(int account_id, int mail_id, int attachment_id, char *src_file_path, char *file_name, char *full_path, int *err_code)
4697 {
4698         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], attachment_id[%d] , file_name[%p] , full_path[%p] , err_code[%p]", account_id, mail_id, attachment_id, file_name, full_path, err_code);
4699
4700         int ret = false, err = EMF_ERROR_NONE;
4701
4702         if (!file_name || !full_path || !src_file_path) {
4703                 EM_DEBUG_EXCEPTION("Invalid paramter");
4704                 err = EMF_ERROR_INVALID_PARAM;
4705                 goto FINISH_OFF;
4706         }
4707
4708         if (!emstorage_create_dir(account_id, mail_id, attachment_id, &err))  {
4709                 EM_DEBUG_EXCEPTION("emstorage_create_dir failed [%d]", err);
4710                 goto FINISH_OFF;
4711         }
4712         
4713         if (!emstorage_get_save_name(account_id, mail_id, attachment_id, file_name, full_path, &err))  {
4714                 EM_DEBUG_EXCEPTION("emstorage_get_save_name failed [%d]", err);
4715                 goto FINISH_OFF;
4716         }
4717
4718         if (strcmp(src_file_path, full_path) != 0)  {
4719                 if (!emstorage_copy_file(src_file_path, full_path, false, &err))  {
4720                         EM_DEBUG_EXCEPTION("emstorage_copy_file failed [%d]", err);
4721
4722                         goto FINISH_OFF;
4723                 }
4724         }
4725
4726         ret = true;
4727
4728 FINISH_OFF:
4729         if (err_code)
4730                 *err_code = err;
4731         return 1;
4732 }
4733
4734 /* description
4735  *    update mail information
4736  */
4737 INTERNAL_FUNC int emcore_update_mail(emf_mail_data_t *input_mail_data, emf_attachment_data_t *input_attachment_data_list, int input_attachment_count, emf_meeting_request_t* input_meeting_request, int input_from_eas)
4738 {
4739         EM_DEBUG_FUNC_BEGIN("input_mail_data[%p], input_attachment_data_list[%p], input_attachment_count[%d], input_meeting_request[%p], input_from_eas[%d]", input_mail_data, input_attachment_data_list, input_attachment_count, input_meeting_request, input_from_eas);
4740
4741         char                   filename_buf[1024]         = {0, };
4742         int                    i                          = 0;
4743         int                    err                        = EMF_ERROR_NONE;
4744         int                    local_inline_content_count = 0;
4745         emstorage_mail_tbl_t  *converted_mail_tbl_data    = NULL;
4746         emf_meeting_request_t *meeting_req = NULL;
4747         struct stat            st_buf;
4748
4749         if (!input_mail_data || (input_attachment_count && !input_attachment_data_list) || (!input_attachment_count &&input_attachment_data_list))  {
4750                 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
4751                 err = EMF_ERROR_INVALID_PARAM;
4752                 goto FINISH_OFF2;
4753         }
4754
4755         if(input_from_eas == 0) {
4756                 if (input_mail_data->file_path_plain)  {
4757                         if (stat(input_mail_data->file_path_plain, &st_buf) < 0)  {
4758                                 EM_DEBUG_EXCEPTION("input_mail_data->file_path_plain, stat(\"%s\") failed...", input_mail_data->file_path_plain);
4759                                 err = EMF_ERROR_INVALID_MAIL;
4760                                 goto FINISH_OFF;
4761                         }
4762                 }
4763                 
4764                 if (input_mail_data->file_path_html)  {
4765                         if (stat(input_mail_data->file_path_html, &st_buf) < 0)  {
4766                                 EM_DEBUG_EXCEPTION("input_mail_data->file_path_html, stat(\"%s\") failed...", input_mail_data->file_path_html);
4767                                 err = EMF_ERROR_INVALID_MAIL;
4768                                 goto FINISH_OFF;
4769                         }
4770                 }
4771                 
4772                 if (input_attachment_count && input_attachment_data_list)  {
4773                         for (i = 0; i < input_attachment_count; i++)  {
4774                                 if (input_attachment_data_list[i].save_status) {
4775                                         if (!input_attachment_data_list[i].attachment_path || stat(input_attachment_data_list[i].attachment_path, &st_buf) < 0)  {
4776                                                 EM_DEBUG_EXCEPTION("stat(\"%s\") failed...", input_attachment_data_list[i].attachment_path);
4777                                                 err = EMF_ERROR_INVALID_ATTACHMENT;
4778                                                 goto FINISH_OFF;
4779                                         }
4780                                 }
4781                         }
4782                 }
4783         }
4784         
4785         if(input_mail_data->mail_size == 0) {
4786                  emcore_calc_mail_size(input_mail_data, input_attachment_data_list, input_attachment_count, &(input_mail_data->mail_size));
4787         }
4788
4789         if (!input_mail_data->full_address_from)  {
4790                 emf_account_t *ref_account = NULL;
4791                 if (!(ref_account = emcore_get_account_reference(input_mail_data->account_id)))  {
4792                         EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", input_mail_data->account_id);
4793                         err = EMF_ERROR_INVALID_ACCOUNT;
4794                         goto FINISH_OFF2;
4795                 }
4796                 input_mail_data->full_address_from = ref_account->email_addr;
4797         }
4798         
4799         if (input_mail_data->file_path_plain)  {   /*  Save plain text body. */
4800                 if (!emcore_save_mail_file(input_mail_data->account_id, input_mail_data->mail_id, 0, input_mail_data->file_path_plain, "UTF-8", filename_buf, &err)) {
4801                         EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
4802                         goto FINISH_OFF2;
4803                 }
4804                 EM_SAFE_FREE(input_mail_data->file_path_plain);
4805                 input_mail_data->file_path_plain = EM_SAFE_STRDUP(filename_buf);
4806         }
4807         
4808         if (input_mail_data->file_path_html)  {   /*  Save HTML text body. */
4809                 if (!emcore_save_mail_file(input_mail_data->account_id, input_mail_data->mail_id, 0, input_mail_data->file_path_html, "UTF-8.htm", filename_buf, &err)) {
4810                         EM_DEBUG_EXCEPTION("emcore_save_mail_file failed [%d]", err);
4811                         goto FINISH_OFF2;
4812                 }
4813                 EM_SAFE_FREE(input_mail_data->file_path_html);
4814                 input_mail_data->file_path_html = EM_SAFE_STRDUP(filename_buf);
4815         }
4816         
4817         if (input_attachment_data_list && input_attachment_count)  {
4818                 int i = 0;
4819                 int compare_result = 1;
4820                 char attachment_id[20];
4821                 emf_attachment_data_t *temp_attachment_data = NULL;
4822
4823                 for(i = 0; i < input_attachment_count; i++) {
4824                         temp_attachment_data = input_attachment_data_list + i;
4825                         if ( (err = emcore_mail_compare_filename_of_attachment_data(input_mail_data->mail_id, temp_attachment_data->attachment_id, temp_attachment_data, &compare_result)) != EMF_ERROR_NONE) {
4826                                 EM_DEBUG_EXCEPTION("emcore_mail_compare_filename_of_attachment_data failed [%d]", err);
4827                                 compare_result = 1;
4828                         }
4829         
4830                         if (compare_result == 0) {
4831                                 EM_DEBUG_LOG("file name and attachment id are same, update exising attachment");
4832                                 if (!emcore_mail_update_attachment_data(input_mail_data->mail_id, temp_attachment_data))  {
4833                                         EM_DEBUG_EXCEPTION("emcore_mail_update_attachment_data failed [%d]", err);
4834                                         goto FINISH_OFF2;
4835                                 }
4836                         }
4837                         else {
4838                                 EM_DEBUG_LOG("save names are different");
4839                                 SNPRINTF(attachment_id, sizeof(attachment_id), "%d", temp_attachment_data->attachment_id);
4840
4841                                 if(temp_attachment_data->attachment_id > 0) {
4842                                         if (!emcore_delete_mail_attachment(input_mail_data->mail_id, attachment_id, &err)) {
4843                                                 EM_DEBUG_EXCEPTION("emcore_delete_mail_attachment failed [%d]", err);
4844                                                 goto FINISH_OFF2;
4845                                         }
4846                                 }
4847
4848                                 if ( (err = emcore_mail_add_attachment_data(input_mail_data->mail_id, temp_attachment_data)) != EMF_ERROR_NONE)  {
4849                                         EM_DEBUG_EXCEPTION("emcore_mail_add_attachment failed [%d]", err);
4850                                         goto FINISH_OFF2;
4851                                 }
4852                         }
4853
4854                         if (temp_attachment_data->inline_content_status)
4855                                 local_inline_content_count++;
4856                 }
4857         }
4858         
4859         input_mail_data->attachment_count     = input_attachment_count;
4860         input_mail_data->inline_content_count = local_inline_content_count;
4861
4862         if (!input_mail_data->date_time) {
4863                 /* time isn't set */
4864                 input_mail_data->date_time = time(NULL);
4865         }
4866
4867         EM_DEBUG_LOG("preview_text[%p]", input_mail_data->preview_text);
4868         if (input_mail_data->preview_text == NULL) {
4869                 if ( (err =emcore_get_preview_text_from_file(input_mail_data->file_path_plain, input_mail_data->file_path_html, MAX_PREVIEW_TEXT_LENGTH, &(input_mail_data->preview_text))) != EMF_ERROR_NONE) {
4870                         EM_DEBUG_EXCEPTION("emcore_get_preview_text_from_file failed[%d]", err);
4871                         goto FINISH_OFF2;
4872                 }
4873         }
4874
4875         if(!em_convert_mail_data_to_mail_tbl(input_mail_data, 1, &converted_mail_tbl_data,  &err)) {
4876                 EM_DEBUG_EXCEPTION("em_convert_mail_data_to_mail_tbl failed[%d]", err);
4877                 goto FINISH_OFF2;
4878         }
4879         
4880         /*  BEGIN TRANSACTION; */
4881         emstorage_begin_transaction(NULL, NULL, NULL);
4882         
4883         if (!emstorage_change_mail_field(input_mail_data->mail_id, UPDATE_MAIL, converted_mail_tbl_data, false, &err))  {
4884                 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
4885
4886                 goto FINISH_OFF;
4887         }
4888         
4889         if (input_mail_data->meeting_request_status == EMF_MAIL_TYPE_MEETING_REQUEST
4890                 || input_mail_data->meeting_request_status == EMF_MAIL_TYPE_MEETING_RESPONSE
4891                 || input_mail_data->meeting_request_status == EMF_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
4892                 /*  check where there is a meeting request in DB */
4893                 if (!emstorage_get_meeting_request(input_mail_data->mail_id, &meeting_req, false, &err) && err != EMF_ERROR_DATA_NOT_FOUND) {
4894                         EM_DEBUG_EXCEPTION("emstorage_get_meeting_request failed [%d]", err);
4895                         goto FINISH_OFF;
4896                 }
4897                 EM_SAFE_FREE(meeting_req);
4898                 if (err == EMF_ERROR_DATA_NOT_FOUND) {  /*  insert */
4899                         emstorage_mail_tbl_t *original_mail = NULL;
4900
4901                         if (!emstorage_get_mail_by_id(input_mail_data->mail_id, &original_mail, false, &err)) {
4902                                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
4903                                 goto FINISH_OFF;
4904                         }
4905
4906                         if (original_mail)       {
4907                         if (!emstorage_add_meeting_request(input_mail_data->account_id, original_mail->mailbox_name, input_meeting_request, false, &err))  {
4908                                 EM_DEBUG_EXCEPTION("emstorage_add_meeting_request failed [%d]", err);
4909
4910                                 goto FINISH_OFF;
4911                         }
4912                                 emstorage_free_mail(&original_mail, 1, NULL);
4913                 }
4914                 }
4915                 else {  /*  update */
4916                         if (!emstorage_update_meeting_request(input_meeting_request, false, &err))  {
4917                                 EM_DEBUG_EXCEPTION("emstorage_update_meeting_request failed [%d]", err);
4918
4919                                 goto FINISH_OFF;
4920                         }
4921                 }
4922         }
4923         
4924 FINISH_OFF: 
4925         if (err == EMF_ERROR_NONE) {
4926                 /*  COMMIT TRANSACTION; */
4927                 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
4928                         err = EMF_ERROR_DB_FAILURE;
4929                 }
4930
4931                 if (input_mail_data->meeting_request_status && !emstorage_notify_storage_event(NOTI_MAIL_UPDATE, input_mail_data->account_id, input_mail_data->mail_id, NULL, UPDATE_MEETING))
4932                         EM_DEBUG_EXCEPTION(" emstorage_notify_storage_event failed [ NOTI_MAIL_UPDATE : UPDATE_MEETING_RESPONSE ] >>>> ");
4933         }
4934         else {
4935                 /*  ROLLBACK TRANSACTION; */
4936                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
4937                         err = EMF_ERROR_DB_FAILURE;
4938         }
4939         
4940 FINISH_OFF2:
4941
4942         if(meeting_req)
4943                 emstorage_free_meeting_request(&meeting_req, 1, NULL);
4944
4945         if(converted_mail_tbl_data)
4946                 emstorage_free_mail(&converted_mail_tbl_data, 1, NULL);
4947
4948         EM_DEBUG_FUNC_END("err [%d]", err);
4949         return err;
4950 }
4951
4952 INTERNAL_FUNC int emcore_fetch_flags(int account_id, int mail_id, emf_mail_flag_t *mail_flag, int *err_code)
4953 {
4954         EM_DEBUG_FUNC_BEGIN();
4955
4956         int ret = false;
4957         int err = 0;
4958         emstorage_mail_tbl_t *mail = NULL;
4959         
4960         EM_IF_NULL_RETURN_VALUE(mail_flag, false);
4961         if (account_id < FIRST_ACCOUNT_ID || mail_id < 1) {
4962                 EM_DEBUG_EXCEPTION("emcore_fetch_flags :  Invalid Param ");
4963                 goto FINISH_OFF;
4964         }
4965
4966         if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_FLAG, &mail, true, &err) || !mail)  {
4967                 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed - %d ", err);
4968
4969                 goto FINISH_OFF;
4970         }
4971
4972         if (!em_convert_mail_tbl_to_mail_flag(mail, mail_flag, &err))  {
4973                 EM_DEBUG_EXCEPTION("em_convert_mail_flag_to_int failed - %d ", err);
4974                 goto FINISH_OFF;
4975         }
4976
4977         ret = true;
4978
4979 FINISH_OFF:
4980         if(mail)
4981                 emstorage_free_mail(&mail, 1, &err);
4982
4983         if (err_code != NULL)
4984                 *err_code = err;
4985
4986         EM_DEBUG_FUNC_END("err [%d]", err);
4987         return ret;
4988 }
4989 /* description :  modify flag of msgno.
4990    return 1(success), 0(failure)
4991    */
4992 INTERNAL_FUNC int emcore_modify_flag(int mail_id, emf_mail_flag_t new_flag, int sticky_flag, int *err_code)
4993 {
4994         EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
4995
4996         int ret = false;
4997         int err = EMF_ERROR_NONE;
4998         int old_seen, new_seen;
4999
5000         emstorage_mail_tbl_t *mail = NULL;
5001         emf_mail_flag_t old_flag;
5002
5003         if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_FLAG, &mail, true, &err) || !mail)  {
5004                 EM_DEBUG_EXCEPTION("emstorage_get_mail_field_by_id failed [%d]", err);
5005
5006                 goto FINISH_OFF;
5007         }
5008
5009         if (!em_convert_mail_tbl_to_mail_flag(mail, &old_flag, &err))  {
5010                 EM_DEBUG_EXCEPTION("em_convert_mail_tbl_to_mail_flag failed [%d]", err);
5011                 goto  FINISH_OFF;
5012         }
5013
5014         old_seen = mail->flags_seen_field;
5015         new_seen = new_flag.seen;
5016
5017         if (!em_convert_mail_flag_to_mail_tbl(&new_flag, mail, &err))  {
5018                 EM_DEBUG_EXCEPTION("em_convert_mail_flag_to_mail_tbl failed [%d]", err);
5019                 goto FINISH_OFF;
5020         }
5021
5022                 mail->lock_status = sticky_flag;
5023                 
5024         if (!emstorage_change_mail_field(mail_id, UPDATE_FLAG, mail, true, &err)) {
5025                         EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
5026                 
5027
5028                         goto FINISH_OFF;
5029                 }
5030
5031         if (old_seen != new_seen && new_seen == 0)
5032                 emcore_delete_notification_for_read_mail(mail_id);
5033
5034         emcore_check_unread_mail();
5035
5036         ret = true;
5037
5038 FINISH_OFF:
5039         if (mail)
5040                 emstorage_free_mail(&mail, 1, NULL);
5041
5042         if (err_code)
5043                 *err_code = err;
5044         EM_DEBUG_FUNC_END("err [%d]", err);
5045         return ret;
5046 }
5047
5048 INTERNAL_FUNC int emcore_set_flags_field(int account_id, int mail_ids[], int num, emf_flags_field_type field_type, int value, int *err_code)
5049 {
5050         EM_DEBUG_FUNC_BEGIN("account_id [%d], mail_ids[%p], num [%d], field_type [%d], value[%d], err_code[%p]", account_id, mail_ids, num, field_type, value, err_code);
5051
5052         int ret = false;
5053         int err = EMF_ERROR_NONE;
5054         char *field_type_name[EMF_FLAGS_FIELD_COUNT] = { "flags_seen_field"
5055                 , "flags_deleted_field", "flags_flagged_field", "flags_answered_field"
5056                 , "flags_recent_field", "flags_draft_field", "flags_forwarded_field" };
5057
5058         if(field_type < 0 || field_type >= EMF_FLAGS_FIELD_COUNT || mail_ids == NULL || num <= 0 || account_id == 0) {
5059                 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
5060                 err = EMF_ERROR_INVALID_PARAM;
5061                         goto  FINISH_OFF;
5062                 }
5063
5064         if (!emstorage_set_field_of_mails_with_integer_value(account_id, mail_ids, num, field_type_name[field_type], value, true, &err)) {
5065                 EM_DEBUG_EXCEPTION("emstorage_set_field_of_mails_with_integer_value failed [%d]", err);
5066
5067                         goto FINISH_OFF;
5068                 }
5069                 
5070                 emcore_check_unread_mail();
5071
5072         ret = true;
5073         
5074 FINISH_OFF: 
5075         
5076         if (err_code)
5077                 *err_code = err;
5078         EM_DEBUG_FUNC_END("err [%d]", err);     
5079         return ret;
5080 }
5081
5082
5083 /* description :  modify extra flag of msgno.
5084    return 0(success), -1(failure)
5085    */
5086 INTERNAL_FUNC int emcore_modify_extra_flag(int mail_id, emf_extra_flag_t new_flag, int *err_code)
5087 {
5088         EM_DEBUG_FUNC_BEGIN("mail_id[%d], err_code[%p]", mail_id, err_code);
5089         
5090         int ret = false;
5091         int err = EMF_ERROR_NONE;
5092         emstorage_mail_tbl_t mail;
5093         
5094         memset(&mail, 0x00, sizeof(emstorage_mail_tbl_t));
5095
5096         mail.mail_id    = mail_id;
5097         mail.save_status = new_flag.status;
5098         mail.lock_status = new_flag.report;
5099         mail.lock_status        = new_flag.lock;
5100         mail.priority   = new_flag.priority;
5101         mail.DRM_status  = new_flag.drm;
5102
5103         if (!emstorage_change_mail_field(mail_id, UPDATE_EXTRA_FLAG, &mail, false, &err))  {
5104                 EM_DEBUG_EXCEPTION("emstorage_change_mail_field failed [%d]", err);
5105
5106                 goto FINISH_OFF;
5107         }
5108         
5109         ret = true;
5110
5111 FINISH_OFF:
5112         if (err_code != NULL)
5113                 *err_code = err;
5114         EM_DEBUG_FUNC_END("err [%d]", err);
5115         return ret;
5116 }
5117
5118
5119 int emcore_mail_cmd_read_mail_pop3(void *stream, int msgno, int limited_size, int *downloded_size, int *result_total_body_size, int *err_code)
5120 {
5121         EM_DEBUG_FUNC_BEGIN("stream[%p], msgno[%d], limited_size[%d], err_code[%p]", stream, msgno, limited_size, err_code);
5122         
5123         int ret = false;
5124         int err = EMF_ERROR_NONE;
5125         int total_body_size = 0;
5126         char command[32];
5127         char *response = NULL;
5128         POP3LOCAL *pop3local;
5129         
5130         if (!stream || !result_total_body_size)  {
5131                 EM_DEBUG_EXCEPTION("stream[%p], total_body_size[%p]", stream, msgno, result_total_body_size);
5132                 err = EMF_ERROR_INVALID_PARAM;
5133                 goto FINISH_OFF;
5134         }
5135         
5136         pop3local = (POP3LOCAL *)(((MAILSTREAM *)stream)->local);
5137
5138         if (!pop3local  || !pop3local->netstream)  {
5139                 err = EMF_ERROR_INVALID_STREAM;
5140                 goto FINISH_OFF;
5141         }
5142         memset(command, 0x00, sizeof(command));
5143
5144         SNPRINTF(command, sizeof(command), "LIST %d\015\012", msgno);
5145
5146 #ifdef FEATURE_CORE_DEBUG
5147         EM_DEBUG_LOG("[POP3] >>> [%s]", command);
5148 #endif
5149
5150         
5151         /*  send command  :  LIST [msgno] - to get the size of the mail */
5152         if (!net_sout(pop3local->netstream, command, (int)strlen(command)))  {
5153                 EM_DEBUG_EXCEPTION("net_sout failed...");
5154
5155                 err = EMF_ERROR_INVALID_RESPONSE;
5156                 goto FINISH_OFF;
5157         }
5158
5159         EM_DEBUG_LOG("Sending command success");
5160
5161         while (pop3local->netstream) {
5162                 /*  receive response */
5163                 if (!(response = net_getline(pop3local->netstream)))
5164                         break;
5165
5166                 if (response)
5167                         EM_DEBUG_LOG("[POP3] <<< %s", response);
5168
5169                 if (*response == '.')  {
5170                         EM_SAFE_FREE(response);
5171                         break;
5172                 }
5173
5174                 if (*response == '+')  {                /*  "+ OK" */
5175                         char *p = NULL;
5176
5177                         if (!(p = strchr(response + strlen("+OK "), ' ')))  {
5178                                 err = EMF_ERROR_INVALID_RESPONSE;
5179                                 goto FINISH_OFF;
5180                         }
5181
5182                         total_body_size = atoi(p + 1);
5183                         EM_DEBUG_LOG("Body size [%d]", total_body_size);
5184
5185                         if (result_total_body_size) {
5186                                 *result_total_body_size = total_body_size;
5187                                 break;
5188                         }
5189                 }
5190                 else if (*response == '-')  {   /*  "- ERR" */
5191                         err = EMF_ERROR_INVALID_RESPONSE;
5192                         goto FINISH_OFF;
5193                 }
5194                 else  {
5195                         err = EMF_ERROR_INVALID_RESPONSE;
5196                         goto FINISH_OFF;
5197                 }
5198
5199                 EM_SAFE_FREE(response);
5200         }
5201
5202         memset(command, 0x00, sizeof(command));
5203
5204         if (limited_size && total_body_size > limited_size) {
5205                 int count_of_line = limited_size / 80;
5206                 SNPRINTF(command, sizeof(command), "TOP %d %d", msgno, count_of_line);
5207         }
5208         else
5209                 SNPRINTF(command, sizeof(command), "RETR %d", msgno);
5210
5211         EM_DEBUG_LOG("[POP3] >>> %s", command);
5212
5213         emcore_set_network_error(EMF_ERROR_NONE);               /*  set current network error as EMF_ERROR_NONE before network operation */
5214         /*  get mail from mail server */
5215         if (!pop3_send((MAILSTREAM *)stream, command, NULL))  {
5216                 EM_DEBUG_EXCEPTION("pop3_send failed...");
5217
5218                 emf_session_t *session = NULL;
5219                 
5220                 if (!emcore_get_current_session(&session))  {
5221                         EM_DEBUG_EXCEPTION("emcore_get_current_session failed...");
5222                         err = EMF_ERROR_SESSION_NOT_FOUND;
5223                         goto FINISH_OFF;
5224                 }
5225
5226                 if (session->network == EMF_ERROR_NONE)
5227                         err = EMF_ERROR_UNKNOWN;
5228                 else
5229                         err = session->network;
5230
5231                 goto FINISH_OFF;
5232         }
5233
5234         ret = true;
5235         
5236 FINISH_OFF: 
5237         EM_SAFE_FREE(response);
5238         
5239         if (err_code != NULL)
5240                 *err_code = err;
5241         EM_DEBUG_FUNC_END("err [%d]", err);     
5242         return ret;
5243 }
5244
5245
5246 INTERNAL_FUNC int emcore_sync_flag_with_server(int mail_id, int *err_code)
5247 {
5248         EM_DEBUG_FUNC_BEGIN("mail_id[%p], err_code[%p]", mail_id, err_code);
5249                 
5250         if (mail_id < 1)  {
5251                 if (err_code != NULL)
5252                         *err_code = EMF_ERROR_INVALID_PARAM;
5253                 return false;
5254         }
5255         
5256         int ret = false;
5257         int err = EMF_ERROR_NONE;
5258         int status = EMF_DOWNLOAD_FAIL;
5259         MAILSTREAM *stream = NULL;
5260         emf_mailbox_t mbox = {0};
5261         emstorage_mail_tbl_t *mail = NULL;
5262         emf_account_t *ref_account = NULL;
5263         int account_id = 0;
5264         int msgno = 0;
5265         emf_mail_flag_t new_flag = {0};
5266         char set_flags[100] = { 0, };
5267         char clear_flags[100] = { 0, };
5268         char tmp[100] = { 0, };
5269         
5270         if (!emcore_check_thread_status())  {
5271                 err = EMF_ERROR_CANCELLED;
5272                 goto FINISH_OFF;
5273         }
5274         
5275         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
5276                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5277
5278                 goto FINISH_OFF;
5279         }
5280
5281         account_id = mail->account_id;
5282         em_convert_mail_tbl_to_mail_flag(mail, &new_flag, NULL);
5283
5284         if (!(ref_account = emcore_get_account_reference(account_id)))   {
5285                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
5286                 err = EMF_ERROR_INVALID_ACCOUNT;
5287                 goto FINISH_OFF;
5288         }
5289         
5290         if (!emcore_check_thread_status())  {
5291                 err = EMF_ERROR_CANCELLED;
5292                 goto FINISH_OFF;
5293         }
5294
5295         /*  open mail server. */
5296         if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_name, (void **)&stream, &err) || !stream)  {
5297                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
5298                 status = EMF_LIST_CONNECTION_FAIL;
5299                 goto FINISH_OFF;
5300         }
5301
5302
5303         if (!emcore_check_thread_status())  {
5304                 err = EMF_ERROR_CANCELLED;
5305                 goto FINISH_OFF;
5306         }
5307
5308         mbox.name = mail->mailbox_name; 
5309         mbox.account_id = account_id;
5310         mbox.mail_stream = stream;
5311
5312         /*  download all uids from server. */
5313         if (!emcore_get_mail_msgno_by_uid(ref_account, &mbox, mail->server_mail_id, &msgno, &err))  {
5314                 EM_DEBUG_EXCEPTION("emcore_get_mail_msgno_by_uid failed message_no  :  %d ", err);
5315                 goto FINISH_OFF;
5316         }
5317                 
5318         sprintf (tmp, "%d", msgno);
5319
5320         if (new_flag.seen)
5321                 sprintf(set_flags, "\\Seen");
5322         else
5323                 sprintf(clear_flags, "\\Seen");
5324
5325         if (new_flag.answered)
5326                 sprintf(set_flags, "%s \\Answered", set_flags);
5327         else
5328                 sprintf(clear_flags, "%s \\Answered", clear_flags);
5329                 
5330         if (new_flag.flagged)
5331                 sprintf(set_flags, "%s \\Flagged", set_flags);
5332         else
5333                 sprintf(clear_flags, "%s \\Flagged", clear_flags);
5334
5335         if (new_flag.forwarded)
5336                 sprintf(set_flags, "%s $Forwarded", set_flags);
5337         else
5338                 sprintf(clear_flags, "%s $Forwarded", clear_flags);
5339
5340         if (strlen(set_flags) > 0)  {
5341                 EM_DEBUG_LOG(">>>> Calling mail_setflag [%s] ", set_flags);
5342                 mail_flag(stream, tmp, set_flags, ST_SET | ST_SILENT);
5343                 EM_DEBUG_LOG(">>>> End mail_setflag ");
5344         }
5345
5346         if (strlen(clear_flags) > 0)  {
5347                 EM_DEBUG_LOG(">>>> Calling mail_clearflag [%s]", clear_flags);
5348                 mail_clearflag(stream, tmp, clear_flags);
5349                 EM_DEBUG_LOG(">>>> End mail_clearflag ");
5350         }
5351                 
5352         if (mail->lock_status) {
5353                 memset(set_flags, 0x00, 100);
5354                 sprintf(set_flags, "Sticky");
5355                 if (strlen(set_flags) > 0)  {
5356                         EM_DEBUG_LOG(">>>> Calling mail_setflag [%s]", set_flags);
5357                         mail_flag(stream, tmp, set_flags, ST_SET | ST_SILENT);
5358                         EM_DEBUG_LOG(">>>> End mail_setflag ");
5359                 }
5360         }
5361
5362         EM_DEBUG_LOG(">>>> Returning from emcore_sync_flag_with_server ");
5363
5364         if (!emcore_check_thread_status())  {
5365                 err = EMF_ERROR_CANCELLED;
5366                 goto FINISH_OFF;
5367         }
5368
5369         ret = true;
5370
5371 FINISH_OFF: 
5372         
5373         if (stream) emcore_close_mailbox(account_id, stream);
5374         if (mail) emstorage_free_mail(&mail, 1, NULL);
5375
5376         if (err_code != NULL)
5377                 *err_code = err;
5378         EM_DEBUG_FUNC_END("err [%d]", err);     
5379         return ret;
5380 }
5381
5382 INTERNAL_FUNC int emcore_sync_seen_flag_with_server(int mail_ids[], int num, int *err_code)
5383 {
5384         EM_DEBUG_FUNC_BEGIN("mail_ids[%p], err_code[%p]", mail_ids[0], err_code);
5385
5386         if (mail_ids[0] < 1)  {
5387                 if (err_code != NULL)
5388                         *err_code = EMF_ERROR_INVALID_PARAM;
5389                 return false;
5390         }
5391
5392         int ret = false;
5393         int err = EMF_ERROR_NONE;
5394         int status = EMF_DOWNLOAD_FAIL;
5395         MAILSTREAM *stream = NULL;
5396         emf_mailbox_t mbox;
5397         emstorage_mail_tbl_t *mail = NULL;
5398         emf_account_t *ref_account = NULL;
5399         int account_id = 0;
5400         int msgno = 0;
5401         emf_mail_flag_t new_flag;
5402         char set_flags[100];
5403         char clear_flags[100];
5404         char tmp[100];
5405         int mail_id = 0;
5406         int i = 0;
5407
5408         memset(&mbox, 0x00, sizeof(emf_mailbox_t));
5409
5410         mail_id = mail_ids[0];
5411
5412         if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
5413                 EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5414
5415                 goto FINISH_OFF;
5416         }
5417
5418         account_id = mail->account_id;
5419
5420         if (!(ref_account = emcore_get_account_reference(account_id)))   {
5421                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
5422                 err = EMF_ERROR_INVALID_ACCOUNT;
5423                 goto FINISH_OFF;
5424         }
5425         
5426         /*  open mail server. */
5427         if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_name, (void **)&stream, &err) || !stream)  {
5428                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
5429                 status = EMF_LIST_CONNECTION_FAIL;
5430                 goto FINISH_OFF;
5431         }
5432
5433         mbox.name = mail->mailbox_name;
5434         mbox.account_id = account_id;
5435         mbox.mail_stream = stream;
5436         
5437         for (i = 0; i < num; i++)  {
5438                 mail_id = mail_ids[i];
5439         
5440                 if (!emcore_check_thread_status())  {
5441                         err = EMF_ERROR_CANCELLED;
5442                         goto FINISH_OFF;
5443                 }
5444
5445                 if (!emstorage_get_mail_by_id(mail_id, &mail, true, &err) || !mail)  {
5446                         EM_DEBUG_EXCEPTION("emstorage_get_mail_by_id failed [%d]", err);
5447         
5448                         goto FINISH_OFF;
5449                 }
5450                 
5451                 /* account_id = mail->account_id; */
5452                 em_convert_mail_tbl_to_mail_flag(mail, &new_flag, NULL);
5453                 
5454                 if (!emcore_check_thread_status())  {
5455                         err = EMF_ERROR_CANCELLED;
5456                         goto FINISH_OFF;
5457                 }
5458
5459                 /*  download message number from server. */
5460                 if (!emcore_get_mail_msgno_by_uid(ref_account, &mbox, mail->server_mail_id, &msgno, &err))  {
5461                         EM_DEBUG_LOG("emcore_get_mail_msgno_by_uid failed message_no  :  %d ", err);
5462                         goto FINISH_OFF;
5463                 }
5464                 
5465                 memset(tmp, 0x00, 100);
5466                 sprintf (tmp, "%d", msgno);
5467
5468                 memset(set_flags, 0x00, 100);
5469                 memset(clear_flags, 0x00, 100);
5470
5471                 if (new_flag.seen)
5472                         sprintf(set_flags, "\\Seen");
5473                 else
5474                         sprintf(clear_flags, "\\Seen");
5475                 EM_DEBUG_LOG("new_flag.seen :  %s ", set_flags);
5476
5477                 if (strlen(set_flags) > 0)  {
5478                         EM_DEBUG_LOG(">>>> Calling mail_setflag ");
5479                         mail_flag(stream, tmp, set_flags, ST_SET | ST_SILENT);
5480                         EM_DEBUG_LOG(">>>> End mail_setflag ");
5481                 }
5482                 else {
5483                         EM_DEBUG_LOG(">>>> Calling mail_clearflag ");
5484                         mail_clearflag(stream, tmp, clear_flags);
5485                         EM_DEBUG_LOG(">>>> End mail_clearflag ");
5486                 }
5487
5488                 EM_DEBUG_LOG(">>>> Returning from emcore_sync_flag_with_server ");
5489
5490                 if (!emcore_check_thread_status())  {
5491                         err = EMF_ERROR_CANCELLED;
5492                         goto FINISH_OFF;
5493                 }
5494         }
5495         ret = true;
5496
5497 FINISH_OFF:
5498         
5499         if (stream) emcore_close_mailbox(account_id, stream);
5500         if (mail) emstorage_free_mail(&mail, 1, NULL);
5501
5502         if (err_code != NULL)
5503                 *err_code = err;
5504         EM_DEBUG_FUNC_END("err [%d]", err);
5505         return ret;
5506 }
5507
5508 INTERNAL_FUNC int emcore_free_mail_data(emf_mail_data_t **mail_list, int count, int *err_code)
5509 {
5510         EM_DEBUG_FUNC_BEGIN("mail_list[%p], count[%d]", mail_list, count);
5511         
5512         if (count <= 0 || !mail_list || !*mail_list)  {
5513                 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");                  
5514                 if(err_code)
5515                         *err_code = EMF_ERROR_INVALID_PARAM;
5516                 return false;
5517         }
5518                 
5519         emf_mail_data_t* p = *mail_list;
5520         int i;
5521         
5522         for (i = 0; i < count; i++) {
5523                 EM_SAFE_FREE(p[i].mailbox_name);
5524                 EM_SAFE_FREE(p[i].subject);
5525                 EM_SAFE_FREE(p[i].server_mailbox_name);
5526                 EM_SAFE_FREE(p[i].server_mail_id);
5527                 EM_SAFE_FREE(p[i].message_id);
5528                 EM_SAFE_FREE(p[i].full_address_from);
5529                 EM_SAFE_FREE(p[i].full_address_reply);
5530                 EM_SAFE_FREE(p[i].full_address_to);
5531                 EM_SAFE_FREE(p[i].full_address_cc);
5532                 EM_SAFE_FREE(p[i].full_address_bcc);
5533                 EM_SAFE_FREE(p[i].full_address_return);
5534                 EM_SAFE_FREE(p[i].email_address_sender);
5535                 EM_SAFE_FREE(p[i].email_address_recipient);
5536                 EM_SAFE_FREE(p[i].alias_sender);
5537                 EM_SAFE_FREE(p[i].alias_recipient);
5538                 EM_SAFE_FREE(p[i].file_path_plain);
5539                 EM_SAFE_FREE(p[i].file_path_html);
5540                 EM_SAFE_FREE(p[i].preview_text);
5541         }
5542         
5543         EM_SAFE_FREE(p); *mail_list = NULL;
5544
5545         if(err_code)
5546                 *err_code = EMF_ERROR_NONE;
5547         
5548         EM_DEBUG_FUNC_END();    
5549         return true;
5550 }
5551
5552
5553 INTERNAL_FUNC int emcore_free_attachment_info(emf_attachment_info_t **atch_info, int *err_code)
5554 {
5555         EM_DEBUG_FUNC_BEGIN("atch_info[%p], err_code[%p]", atch_info, err_code);        
5556         
5557         if (!atch_info || !*atch_info)  {
5558                 if (err_code != NULL)
5559                         *err_code = EMF_ERROR_INVALID_PARAM;
5560                 return false;
5561         }
5562         
5563         emf_attachment_info_t *p = *atch_info;
5564         emf_attachment_info_t *t;
5565         
5566         while (p)  {
5567                 EM_SAFE_FREE(p->name);
5568                 EM_SAFE_FREE(p->savename);
5569                 t = p->next;
5570                 free(p); p = NULL;
5571                 p = t;
5572         }
5573         
5574         if (err_code != NULL)
5575                 *err_code = EMF_ERROR_NONE;
5576         EM_DEBUG_FUNC_END();
5577         return true;
5578 }
5579
5580
5581 INTERNAL_FUNC int emcore_free_attachment_data(emf_attachment_data_t **attachment_data_list, int attachment_data_count, int *err_code)
5582 {
5583         EM_DEBUG_FUNC_BEGIN("attachment_data_list[%p], attachment_data_count [%d], err_code[%p]", attachment_data_list, attachment_data_count, err_code);       
5584         
5585         if (!attachment_data_list || !*attachment_data_list)  {
5586                 if (err_code != NULL)
5587                         *err_code = EMF_ERROR_INVALID_PARAM;
5588                 return false;
5589         }
5590         
5591         emf_attachment_data_t* p = *attachment_data_list;
5592         int i = 0;
5593         
5594         for (i = 0; i < attachment_data_count; i++) {
5595                 EM_SAFE_FREE(p[i].mailbox_name);
5596                 EM_SAFE_FREE(p[i].attachment_name);
5597                 EM_SAFE_FREE(p[i].attachment_path);
5598         }
5599
5600         EM_SAFE_FREE(p); *attachment_data_list = NULL;
5601
5602         if(err_code)
5603                 *err_code = EMF_ERROR_NONE;
5604         
5605         EM_DEBUG_FUNC_END();    
5606         return true;
5607 }
5608
5609
5610
5611 #ifdef __FEATURE_PARTIAL_BODY_DOWNLOAD__
5612
5613 INTERNAL_FUNC int emcore_delete_pbd_activity(int account_id, int mail_id, int activity_id, int *err_code) 
5614 {
5615         EM_DEBUG_FUNC_BEGIN("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
5616
5617         if (account_id < FIRST_ACCOUNT_ID || mail_id < 0) {
5618                 EM_DEBUG_EXCEPTION("account_id[%d], mail_id[%d], err_code[%p]", account_id, mail_id, err_code);
5619                 if (err_code != NULL)
5620                         *err_code = EMF_ERROR_INVALID_PARAM;
5621                 return false;
5622         }
5623
5624         int ret = false;
5625         int err = EMF_ERROR_NONE;
5626         
5627         emstorage_begin_transaction(NULL, NULL, NULL);
5628         
5629         if (!emstorage_delete_pbd_activity(account_id, mail_id, activity_id, false, &err))  {
5630                 EM_DEBUG_EXCEPTION("emstorage_delete_pbd_activity failed [%d]", err);
5631                 
5632
5633                 goto FINISH_OFF;
5634         }
5635
5636         ret = true;
5637
5638 FINISH_OFF: 
5639         if (ret == true) {      /*  COMMIT TRANSACTION; */
5640                 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
5641                         err = EMF_ERROR_DB_FAILURE;
5642                         ret = false;
5643                 }
5644         }
5645         else {  /*  ROLLBACK TRANSACTION; */
5646                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
5647                         err = EMF_ERROR_DB_FAILURE;
5648         }       
5649         
5650         if (err_code != NULL)
5651                 *err_code = err;
5652         EM_DEBUG_FUNC_END();
5653         return ret;
5654 }
5655
5656 INTERNAL_FUNC int emcore_insert_pbd_activity(emf_event_partial_body_thd *local_activity, int *activity_id, int *err_code) 
5657 {
5658         EM_DEBUG_FUNC_BEGIN("local_activity[%p], activity_id[%p], err_code[%p]", local_activity, activity_id, err_code);
5659
5660         if (!local_activity || !activity_id) {
5661                 EM_DEBUG_EXCEPTION("local_activity[%p], activity_id[%p] err_code[%p]", local_activity, activity_id, err_code);
5662                 if (err_code != NULL)
5663                         *err_code = EMF_ERROR_INVALID_PARAM;
5664                 return false;
5665         }
5666         
5667         int ret = false;
5668         int err = EMF_ERROR_NONE;
5669         
5670         emstorage_begin_transaction(NULL, NULL, NULL);
5671         
5672         if (!emstorage_add_pbd_activity(local_activity, activity_id, false, &err))  {
5673                 EM_DEBUG_EXCEPTION("emstorage_add_pbd_activity failed [%d]", err);
5674                 
5675
5676                 goto FINISH_OFF;
5677         }
5678
5679         ret = true;
5680
5681 FINISH_OFF: 
5682         if (ret == true) {      /*  COMMIT TRANSACTION; */
5683                 if (emstorage_commit_transaction(NULL, NULL, NULL) == false) {
5684                         err = EMF_ERROR_DB_FAILURE;
5685                         ret = false;
5686                 }
5687         }
5688         else {  /*  ROLLBACK TRANSACTION; */
5689                 if (emstorage_rollback_transaction(NULL, NULL, NULL) == false)
5690                         err = EMF_ERROR_DB_FAILURE;
5691         }       
5692         
5693         if (err_code != NULL)
5694                 *err_code = err;
5695         EM_DEBUG_FUNC_END();
5696         return ret;
5697 }
5698
5699 #endif
5700
5701 #ifdef __FEATURE_BULK_DELETE_MOVE_UPDATE_REQUEST_OPTI__
5702
5703 /* API to set or unset a field of flags on server in single IMAP request to server */
5704
5705 INTERNAL_FUNC int emcore_sync_flags_field_with_server(int mail_ids[], int num, emf_flags_field_type field_type, int value, int *err_code)
5706 {
5707         EM_DEBUG_FUNC_BEGIN("mail_ids [%p], num [%d], field_type [%d], value [%d], err_code [%p]", mail_ids, num, field_type, value, err_code);
5708                 
5709         if (NULL == mail_ids || num <= 0 || field_type < 0 || field_type >= EMF_FLAGS_FIELD_COUNT)  {
5710                         EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
5711                 if (err_code != NULL) {
5712                         *err_code = EMF_ERROR_INVALID_PARAM;
5713                 }
5714                 return false;
5715         }
5716         
5717         MAILSTREAM *stream = NULL;
5718         IMAPLOCAL *imaplocal = NULL;
5719         char tag[MAX_TAG_SIZE] = {0, };
5720         char cmd[MAX_IMAP_COMMAND_LENGTH] = {0, };
5721         char *p = NULL;
5722         char **string_list = NULL;      
5723         int ret = false;
5724         int err = EMF_ERROR_NONE;
5725         int command_success = false;
5726         int account_id = 0;
5727         int mail_id = 0;
5728         int i = 0;
5729         int id_set_count = 0;
5730         int len_of_each_range = 0;
5731         int string_count = 0;
5732         emf_account_t *temp_account;
5733         emf_id_set_t *id_set = NULL;
5734         emstorage_mail_tbl_t *mail = NULL;
5735         emf_uid_range_set *uid_range_set = NULL;
5736         emf_uid_range_set *uid_range_node = NULL;
5737         char *field_type_name[EMF_FLAGS_FIELD_COUNT] = { "\\Seen"
5738                 , "\\Deleted", "\\Flagged", "\\Answered"
5739                 , "\\Recent", "\\Draft", "$Forwarded" }; 
5740         
5741         mail_id = mail_ids[0];
5742         
5743         if (!emstorage_get_mail_field_by_id(mail_id, RETRIEVE_FLAG, &mail, true, &err) || !mail)                /*To DO :  This is a existing bug. on mail deletion before this call it will fail always */ {
5744                 EM_DEBUG_LOG("emstorage_get_mail_by_id failed [%d]", err);
5745
5746                 goto FINISH_OFF;
5747         }
5748
5749         account_id = mail[0].account_id;
5750
5751         temp_account = emcore_get_account_reference(account_id);
5752
5753         if (!temp_account)   {
5754                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", account_id);
5755                 err = EMF_ERROR_INVALID_ACCOUNT;
5756                 goto FINISH_OFF;
5757         }
5758         
5759         if (temp_account->receiving_server_type != EMF_SERVER_TYPE_IMAP4) {
5760                 EM_DEBUG_EXCEPTION("Syncing seen flag is available only for IMAP4 server. The server type [%d]", temp_account->receiving_server_type);
5761                 err = EMF_ERROR_NOT_SUPPORTED;
5762                 goto FINISH_OFF;
5763         }
5764         
5765         if (!emcore_connect_to_remote_mailbox(account_id, mail->mailbox_name, (void **)&stream, &err) || !stream)  {
5766                 EM_DEBUG_EXCEPTION("emcore_connect_to_remote_mailbox failed [%d]", err);
5767                 goto FINISH_OFF;
5768         }
5769
5770         if (false == emstorage_free_mail(&mail, 1, &err)) {
5771                 EM_DEBUG_EXCEPTION("emstorage_free_mail failed - %d ", err);
5772                 goto FINISH_OFF;
5773         }
5774
5775         /* [h.gahlaut] Break the set of mail_ids into comma separated strings of given length */
5776         /* Length is decided on the basis of remaining keywords in the Query to be formed later on in emstorage_get_id_set_from_mail_ids */
5777         /* Here about 90 bytes are required for fixed keywords in the query-> SELECT local_uid, s_uid from mail_read_mail_uid_tbl where local_uid in (....) ORDER by s_uid */
5778         /* So length of comma separated strings which will be filled in (.....) in above query can be maximum QUERY_SIZE - 90 */
5779
5780         if (false == emcore_form_comma_separated_strings(mail_ids, num, QUERY_SIZE - 90, &string_list, &string_count, &err))   {
5781                 EM_DEBUG_EXCEPTION("emcore_form_comma_separated_strings failed [%d]", err);
5782                 goto FINISH_OFF;
5783         }
5784
5785         /* Now execute one by one each comma separated string of mail_ids */
5786
5787         for (i = 0; i < string_count; ++i) {
5788                 /* Get the set of mail_ds and corresponding server_mail_ids sorted by server mail ids in ascending order */
5789
5790                 if (false == emstorage_get_id_set_from_mail_ids(string_list[i], &id_set, &id_set_count, &err)) {
5791                         EM_DEBUG_EXCEPTION("emstorage_get_id_set_from_mail_ids failed [%d]", err);
5792                         goto FINISH_OFF;
5793                 }
5794                 
5795                 /* Convert the sorted sequence of server mail ids to range sequences of given length. A range sequence will be like A : B, C, D: E, H */
5796                 
5797                 len_of_each_range = MAX_IMAP_COMMAND_LENGTH - 40;               /*  1000 is the maximum length allowed RFC 2683. 40 is left for keywords and tag. */
5798                 
5799                 if (false == emcore_convert_to_uid_range_set(id_set, id_set_count, &uid_range_set, len_of_each_range, &err)) {
5800                         EM_DEBUG_EXCEPTION("emcore_convert_to_uid_range_set failed [%d]", err);
5801                         goto FINISH_OFF;
5802                 }
5803
5804                 uid_range_node = uid_range_set;
5805
5806                 while (uid_range_node != NULL) {
5807                         /* Remove comma from end of uid_range */
5808
5809                         uid_range_node->uid_range[strlen(uid_range_node->uid_range) - 1] = '\0';
5810                         
5811                         /* Form the IMAP command */
5812
5813                         SNPRINTF(tag, MAX_TAG_SIZE, "%08lx", 0xffffffff & (stream->gensym++));
5814
5815                         if (value)
5816                                 SNPRINTF(cmd, MAX_IMAP_COMMAND_LENGTH, "%s UID STORE %s +FLAGS (%s)\015\012", tag, uid_range_node->uid_range, field_type_name[field_type]);
5817                         else
5818                                 SNPRINTF(cmd, MAX_IMAP_COMMAND_LENGTH, "%s UID STORE %s -FLAGS (%s)\015\012", tag, uid_range_node->uid_range, field_type_name[field_type]);
5819
5820                         EM_DEBUG_LOG("[IMAP4] command %s", cmd);
5821
5822                         /* send command */
5823
5824                         if (!(imaplocal = stream->local) || !imaplocal->netstream)  {
5825                                 EM_DEBUG_EXCEPTION("invalid IMAP4 stream detected...");
5826                                 
5827                                 err = EMF_ERROR_UNKNOWN;                
5828                                 goto FINISH_OFF;
5829                         }
5830                 
5831                         if (!net_sout(imaplocal->netstream, cmd, (int)strlen(cmd))) {
5832                                 EM_DEBUG_EXCEPTION("net_sout failed...");
5833                                 err = EMF_ERROR_CONNECTION_BROKEN;              
5834                                 goto FINISH_OFF;
5835                         }
5836
5837                         /* Receive Response */
5838
5839                         command_success = false;
5840                         
5841                         while (imaplocal->netstream) {  
5842                                 if (!(p = net_getline(imaplocal->netstream)))  {
5843                                         EM_DEBUG_EXCEPTION("net_getline failed...");
5844                                 
5845                                         err = EMF_ERROR_INVALID_RESPONSE;       
5846                                         goto FINISH_OFF;
5847                                 }
5848                         
5849                                 EM_DEBUG_LOG("[IMAP4 Response ] %s", p);
5850                                 
5851                                 if (!strncmp(p, tag, strlen(tag)))  {
5852                                         if (!strncmp(p + strlen(tag) + 1, "OK", 2))  {
5853                                                 /*Delete all local activities */
5854                                                 command_success = true;
5855
5856                                                 EM_SAFE_FREE(p);        
5857                                                 break;
5858                                         }
5859                                         else  {
5860                                                 /* 'NO' or 'BAD' */
5861                                                 command_success = false;
5862                                                 err = EMF_ERROR_IMAP4_STORE_FAILURE;            
5863                                                 EM_SAFE_FREE(p);
5864                                                 goto FINISH_OFF;
5865                                         }               
5866                                 }
5867                                 
5868                                 EM_SAFE_FREE(p);                
5869                         }
5870
5871                         uid_range_node = uid_range_node->next;
5872                 }       
5873
5874                 emcore_free_uid_range_set(&uid_range_set);
5875
5876                 EM_SAFE_FREE(id_set);
5877                 
5878                 id_set_count = 0;
5879         }
5880
5881         ret = true;
5882
5883 FINISH_OFF: 
5884
5885 #ifdef __FEATURE_LOCAL_ACTIVITY__
5886         if (ret) {
5887                 emstorage_activity_tbl_t new_activity;
5888                 for (i = 0; i<num ; i++) {              
5889                         memset(&new_activity, 0x00, sizeof(emstorage_activity_tbl_t));
5890                         new_activity.activity_type = ACTIVITY_MODIFYSEENFLAG;
5891                         new_activity.account_id    = account_id;
5892                         new_activity.mail_id       = mail_ids[i];
5893                 
5894                         if (!emcore_delete_activity(&new_activity, &err))
5895                                 EM_DEBUG_EXCEPTION("Local Activity ACTIVITY_MOVEMAIL [%d] ", err);
5896                 }
5897
5898         }
5899 #endif
5900
5901         if (NULL != mail) {
5902                 if (false == emstorage_free_mail(&mail, 1, &err))                       
5903                         EM_DEBUG_EXCEPTION("emstorage_free_mail failed - %d ", err);
5904         }
5905         
5906         emcore_free_comma_separated_strings(&string_list, &string_count);
5907
5908         if (false == ret)
5909                 emcore_free_uid_range_set(&uid_range_set);
5910
5911         emcore_close_mailbox(0, stream);
5912         stream = NULL;
5913
5914         if (err_code != NULL)
5915                 *err_code = err;
5916         EM_DEBUG_FUNC_END();
5917         return ret;
5918
5919 }
5920 #endif
5921
5922 INTERNAL_FUNC int emcore_mail_filter_by_rule(emf_rule_t *filter_info, int *err_code)
5923 {
5924         EM_DEBUG_FUNC_BEGIN("filter_info[%p], err_code[%p]", filter_info, err_code);
5925
5926         int ret = false, err = EMF_ERROR_NONE;
5927         int account_index, account_count, mail_id_index = 0;
5928         emf_account_t *account_ref = NULL, *account_list_ref = NULL;
5929         int filtered_mail_id_count = 0, *filtered_mail_id_list = NULL, parameter_string_length = 0;
5930         char *parameter_string = NULL, mail_id_string[10] = { 0x00, };
5931         emstorage_mailbox_tbl_t *spam_mailbox = NULL;
5932
5933         if (!filter_info)  {
5934                 EM_DEBUG_EXCEPTION("filter_info[%p]", filter_info);
5935                 err = EMF_ERROR_INVALID_PARAM;
5936                 goto FINISH_OFF;
5937         }
5938
5939         if (!emcore_get_account_reference_list(&account_list_ref, &account_count, &err)) {
5940                 EM_DEBUG_EXCEPTION("emcore_get_account_reference_list failed [%d]", err);
5941                 goto FINISH_OFF;
5942         }
5943
5944         for (account_index = 0; account_index < account_count; account_index++) {
5945                 account_ref = account_list_ref + account_index;
5946
5947                 if (!emstorage_get_mailbox_by_mailbox_type(account_ref->account_id, EMF_MAILBOX_TYPE_SPAMBOX, &spam_mailbox, false, &err))
5948                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type for account_id[%d] failed [%d]", account_ref->account_id, err);
5949                 else if (spam_mailbox && spam_mailbox->mailbox_name) {
5950                         if (!emstorage_filter_mails_by_rule(account_ref->account_id, spam_mailbox->mailbox_name, (emstorage_rule_tbl_t *)filter_info, &filtered_mail_id_list, &filtered_mail_id_count, &err))
5951                                 EM_DEBUG_EXCEPTION("emstorage_filter_mails_by_rule failed [%d]", err);
5952                         else {
5953                                 if (filtered_mail_id_count) {
5954                                         parameter_string_length = strlen(spam_mailbox->mailbox_name) + 7 + (10 * filtered_mail_id_count);
5955                                         parameter_string = em_malloc(sizeof(char) * parameter_string_length);
5956
5957                                         if (parameter_string == NULL) {
5958                                                 err = EMF_ERROR_OUT_OF_MEMORY;
5959                                                 EM_DEBUG_EXCEPTION("em_malloc failed for parameter_string");
5960                                                 goto FINISH_OFF;
5961                                         }
5962
5963                                         SNPRINTF(parameter_string, parameter_string_length, "[NA]%c%s%c", 0x01, spam_mailbox->mailbox_name, 0x01);
5964                                         
5965                                         for (mail_id_index = 0; mail_id_index < filtered_mail_id_count; mail_id_index++) {
5966                                                 memset(mail_id_string, 0, 10);
5967                                                 SNPRINTF(mail_id_string, 10, "%d", filtered_mail_id_list[mail_id_index]);
5968                                                 strcat(parameter_string, mail_id_string);
5969                                                 strcat(parameter_string, ",");
5970                                         }
5971
5972                                         EM_DEBUG_LOG("filtered_mail_id_count [%d]", filtered_mail_id_count);
5973                                         EM_DEBUG_LOG("param string [%s]", parameter_string);
5974
5975                                         if (!emstorage_notify_storage_event(NOTI_MAIL_MOVE, account_ref->account_id, 0, parameter_string, 0)) 
5976                                                 EM_DEBUG_EXCEPTION("emstorage_notify_storage_event failed [ NOTI_MAIL_MOVE ] >>>> ");
5977
5978                                         EM_SAFE_FREE(filtered_mail_id_list);
5979                                         EM_SAFE_FREE(parameter_string);
5980                                 }
5981                         }
5982                         emstorage_free_mailbox(&spam_mailbox, 1, &err);
5983                         spam_mailbox = NULL;
5984                 }
5985         }
5986
5987         emcore_check_unread_mail();
5988
5989         ret = true;
5990
5991 FINISH_OFF: 
5992         EM_SAFE_FREE(account_list_ref);
5993         EM_SAFE_FREE(filtered_mail_id_list);
5994         EM_SAFE_FREE(parameter_string);
5995
5996
5997         if (spam_mailbox)
5998                 emstorage_free_mailbox(&spam_mailbox, 1, &err);
5999
6000         if (err_code)
6001                 *err_code = err;
6002         EM_DEBUG_FUNC_END();
6003         return ret;
6004 }