959a349aabc7de6fc3169e3c1cd635517ad60fe4
[platform/core/messaging/email-service.git] / email-daemon / main.c
1 /*
2 *  email-service
3 *
4 * Copyright (c) 2012 - 2013 Samsung Electronics Co., Ltd. All rights reserved.
5 *
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 *
20 */
21
22
23 /* Email service Main .c */
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27 #include <malloc.h>
28 #include <glib.h>
29 #include <glib-object.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <signal.h>
33 #include <time.h>
34
35 #include "email-daemon.h"
36 #include "email-ipc.h"
37 #include "email-utilities.h"
38 #include "email-debug-log.h"
39 #include "email-daemon-auto-poll.h"
40 #include "email-daemon-account.h"
41 #include "email-convert.h"
42 #include "email-internal-types.h"
43 #include "email-types.h"
44 #include "email-core-account.h"
45 #include "email-core-mail.h"
46 #include "email-core-smtp.h"
47 #include "email-core-event.h"
48 #include "email-core-global.h"
49 #include "email-core-mailbox.h"
50 #include "email-core-utils.h"
51 #include "email-core-smime.h"
52 #include "email-core-cert.h"
53 #include "email-core-task-manager.h"
54 #include "email-core-signal.h"
55 #include "email-storage.h"
56
57 void stb_create_account(HIPC_API a_hAPI)
58 {
59         EM_DEBUG_FUNC_BEGIN();
60         int buffer_size = 0;
61         int local_result = 0;
62         char* local_account_stream = NULL;
63         email_account_t account;
64         int err = EMAIL_ERROR_NONE;
65
66         /* Initialize the email_account_t */
67         memset(&account, 0x00, sizeof(email_account_t));
68
69         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
70         EM_DEBUG_LOG("size [%d]", buffer_size);
71         if(buffer_size <= 0)    {
72                 err = EMAIL_ERROR_INVALID_PARAM;
73                 goto FINISH_OFF;
74         }
75         local_account_stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
76         /* Convert account stream to structure */
77         em_convert_byte_stream_to_account(local_account_stream, buffer_size, &account);
78
79
80         EM_DEBUG_LOG("Account name - %s", account.account_name);
81         EM_DEBUG_LOG("Email Address - %s", account.user_email_address);
82
83         if(!emdaemon_create_account(&account, &err)) {
84                 EM_DEBUG_EXCEPTION("emdaemon_create_account fail ");
85                 goto FINISH_OFF;
86         }
87
88
89 #ifdef __FEATURE_AUTO_POLLING__
90         /* start auto polling, if check_interval not zero */
91         if(account.check_interval > 0) {
92                 if(!emdaemon_add_polling_alarm( account.account_id,account.check_interval))
93                         EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm[NOTI_ACCOUNT_ADD] : start auto poll failed >>> ");
94         }
95 #endif
96
97         /* add account details to contact DB */
98         emdaemon_insert_accountinfo_to_contact(&account);
99
100         local_result = 1;
101         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
102                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
103
104         EM_DEBUG_LOG("[3] APPID[%d], APIID [%d]", emipc_get_app_id(a_hAPI), emipc_get_api_id(a_hAPI));
105         EM_DEBUG_LOG("account id[%d]", account.account_id);
106
107         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &(account.account_id), sizeof(int)))
108                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
109
110         /* pass result to app */
111         if (!emipc_execute_stub_api(a_hAPI))
112                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
113
114 FINISH_OFF:
115         if ( local_result == 0 ) {
116                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
117                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : local_result ");
118                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
119                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
120                 if (!emipc_execute_stub_api(a_hAPI))
121                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
122         }
123         EM_DEBUG_FUNC_END();
124 }
125
126 void stb_delete_account(HIPC_API a_hAPI)
127 {
128         EM_DEBUG_FUNC_BEGIN();
129         int account_id = 0;
130         int local_result = 0;
131         int err = EMAIL_ERROR_NONE;
132
133         /* account_id */
134         account_id = *((int*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0));
135         if(!emdaemon_delete_account(account_id, &err)) {
136                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
137                         EM_DEBUG_LOG("emipc_add_parameter failed ");
138                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
139                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
140
141                 if (!emipc_execute_stub_api(a_hAPI))
142                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
143                 goto FINISH_OFF;
144         }
145
146 #ifdef __FEATURE_AUTO_POLLING__
147         /* stop auto polling for this acount */
148         if(emdaemon_check_auto_polling_started(account_id)) {
149                 if(!emdaemon_remove_polling_alarm(account_id))
150                         EM_DEBUG_EXCEPTION("emdaemon_remove_polling_alarm[ NOTI_ACCOUNT_DELETE] : remove auto poll failed >>> ");
151         }
152 #endif
153         local_result = 1;
154
155         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
156                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
157
158         if (!emipc_execute_stub_api(a_hAPI))
159                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
160
161 FINISH_OFF:
162
163         EM_DEBUG_FUNC_END();
164 }
165
166 void stb_update_account(HIPC_API a_hAPI)
167 {
168         EM_DEBUG_FUNC_BEGIN();
169         int account_id = 0, buffer_size = 0, local_result = 0, with_validation = 0;
170         char* local_account_stream = NULL;
171         email_account_t new_account_info = {0};
172         email_account_t old_account_info = {0};
173         int err = EMAIL_ERROR_NONE;
174         int handle = 0;
175
176         account_id = *((int*)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0));
177
178         /* get account structure from stream */
179         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 1);
180         EM_DEBUG_LOG("size [%d]", buffer_size);
181         local_account_stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 1);
182         em_convert_byte_stream_to_account(local_account_stream, buffer_size, &new_account_info);
183
184         /*get validation flag */
185         with_validation = *((int*)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 2));
186
187
188         if(!emdaemon_get_account(account_id, EMAIL_ACC_GET_OPT_FULL_DATA, &old_account_info, &err)) {
189                 EM_DEBUG_EXCEPTION("emdaemon_get_account failed ");
190                 goto FINISH_OFF;
191         }
192
193
194         if( EM_SAFE_STRLEN (new_account_info.incoming_server_password) == 0 ) {
195                 EM_SAFE_FREE(new_account_info.incoming_server_password); /* be allocated but has zero length */
196                 EM_DEBUG_LOG("old_account_info->incoming_server_password [%s]", old_account_info.incoming_server_password);
197                 new_account_info.incoming_server_password = EM_SAFE_STRDUP(old_account_info.incoming_server_password);
198                 if(new_account_info.incoming_server_password == NULL) {
199                                 EM_DEBUG_EXCEPTION("allocation for new_account_info->password failed");
200                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
201                                 goto FINISH_OFF;
202                         }
203                 }
204
205
206         if( EM_SAFE_STRLEN (new_account_info.outgoing_server_password) == 0 ) {
207                 EM_SAFE_FREE(new_account_info.outgoing_server_password);
208                 if(old_account_info.outgoing_server_password) {
209                         new_account_info.outgoing_server_password = strdup (old_account_info.outgoing_server_password);
210                         if(new_account_info.outgoing_server_password == NULL) {
211                                 EM_DEBUG_EXCEPTION("allocation for new_account_info->outgoing_server_password failed");
212                                 err = EMAIL_ERROR_OUT_OF_MEMORY;
213                                 goto FINISH_OFF;
214                         }
215                 }
216         }
217
218         if(with_validation) {
219                 emdaemon_validate_account_and_update(account_id, &new_account_info, &handle, &err);
220                 local_result = 1;
221         }
222         else {
223                 if(emdaemon_update_account(account_id, &new_account_info, &err)) {
224                         emdaemon_update_accountinfo_to_contact(&old_account_info, &new_account_info);
225                         local_result = 1;
226                 }
227                 else {
228                         EM_DEBUG_EXCEPTION("emdaemon_update_account failed [%d]", err);
229                         goto FINISH_OFF;
230                 }
231
232         }
233
234         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
235                 EM_DEBUG_EXCEPTION("emipc_add_parameter for result failed");
236
237         if(with_validation) {
238                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
239                         EM_DEBUG_EXCEPTION("emipc_add_parameter for handle failed");
240         }
241
242         if (!emipc_execute_stub_api(a_hAPI))
243                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
244
245 FINISH_OFF:
246         if ( local_result == 0 ) {
247                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
248                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
249
250                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
251                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
252
253                 if (!emipc_execute_stub_api(a_hAPI))
254                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
255         }
256
257         emcore_free_account(&old_account_info);
258
259         emcore_free_account(&new_account_info);
260
261         EM_DEBUG_FUNC_END();
262 }
263
264 void stb_validate_account(HIPC_API a_hAPI)
265 {
266         EM_DEBUG_FUNC_BEGIN();
267
268         int account_id = 0;
269         int local_result = 0;
270         int err_code = 0;
271         int handle = 0;
272
273         /* account_id */
274         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
275         local_result = emdaemon_validate_account(account_id, &handle,&err_code);
276
277         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
278                 EM_DEBUG_LOG("emipc_add_parameter failed ");
279
280         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
281                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
282
283         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
284                 EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
285
286         if (!emipc_execute_stub_api(a_hAPI))
287                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
288
289         EM_DEBUG_FUNC_END();
290 }
291
292
293 void stb_get_account_list(HIPC_API a_hAPI)
294 {
295         EM_DEBUG_FUNC_BEGIN();
296
297         int local_result = 0;
298         int i= 0;
299         char* local_stream = NULL;
300         email_account_t* account_list;
301         int count;
302         int size = 0;
303         int err = EMAIL_ERROR_NONE;
304
305         if(emdaemon_get_account_list(&account_list, &count, &err)) {
306                 EM_DEBUG_LOG("emdaemon_get_account_list success");
307                 local_result = 1;
308                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
309                         EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
310
311                 EM_DEBUG_LOG("Count [%d]", count);
312                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &count, sizeof(int)))
313                         EM_DEBUG_EXCEPTION("emipc_add_parameter count failed ");
314
315                 for(i=0; i<count; i++) {
316                         EM_DEBUG_LOG("Name - %s", account_list[i].account_name);
317
318                         local_stream = em_convert_account_to_byte_stream(account_list+i, &size);
319
320                         if (!local_stream) {
321                                 EM_DEBUG_EXCEPTION ("INVALID PARAM: local_stream NULL ");
322                                 emcore_free_account_list(&account_list, count, NULL);
323                                 return;
324                         }
325
326                         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, local_stream, size))
327                                 EM_DEBUG_EXCEPTION("Add  Param mailbox failed  ");
328
329                         size = 0;
330                         EM_SAFE_FREE(local_stream);
331                 }
332
333                 emcore_free_account_list(&account_list, count, NULL);
334                 if (!emipc_execute_stub_api(a_hAPI))
335                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
336         }
337
338         else {
339                 EM_DEBUG_LOG("emdaemon_get_account_list failed");
340                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
341                         EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
342                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
343                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
344                 if (!emipc_execute_stub_api(a_hAPI))
345                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
346         }
347
348         EM_SAFE_FREE(local_stream);
349         EM_DEBUG_FUNC_END();
350 }
351
352 /* sowmya.kr, 10-May-2010, changes for API improvement */
353 void stb_sync_header(HIPC_API a_hAPI)
354 {
355         EM_DEBUG_FUNC_BEGIN();
356         int err = EMAIL_ERROR_NONE;
357         int handle = -1;
358         int account_id = 0, maibox_id = 0;
359
360         /* account_id */
361         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
362         EM_DEBUG_LOG("account_id [%d]", account_id);
363
364         /* maibox_id */
365         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &maibox_id);
366         EM_DEBUG_LOG("maibox_id [%d]", maibox_id);
367
368         if(emdaemon_sync_header(account_id, maibox_id, &handle, &err)) {
369                 EM_DEBUG_LOG("emdaemon_sync_header success ");
370         }
371
372         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
373                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
374         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
375                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
376         if (!emipc_execute_stub_api(a_hAPI))
377                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
378         EM_DEBUG_FUNC_END();
379 }
380
381 void stb_download_body(HIPC_API a_hAPI)
382 {
383         EM_DEBUG_FUNC_BEGIN();
384         int err = EMAIL_ERROR_NONE;
385         int mail_id = 0;
386         int attachment_count = 0;
387         int handle = 0;
388         int account_id = 0;
389
390         /* Account Id */
391         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
392
393         /* Mail Id */
394         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mail_id);
395
396         /* Has Attachment */
397         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &attachment_count);
398
399         /*Download Body */
400         if (!emdaemon_download_body(account_id, mail_id, 1, attachment_count, &handle, &err)) {
401                 EM_DEBUG_EXCEPTION("emdaemon_download_body - failed");
402                 goto FINISH_OFF;
403         }
404
405         err = EMAIL_ERROR_NONE;
406
407 FINISH_OFF:
408
409         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
410         EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
411         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
412                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
413         if (!emipc_execute_stub_api(a_hAPI))
414                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
415
416         EM_DEBUG_FUNC_END();
417 }
418
419
420 void stb_create_mailbox(HIPC_API a_hAPI)
421 {
422         EM_DEBUG_FUNC_BEGIN();
423         int                 buffer_size                         = 0;
424         int                 err = EMAIL_ERROR_NONE;
425         char               *local_stream  = NULL;
426         int                 on_server           = 0;
427         email_mailbox_t mailbox = {0};
428         int             handle = 0; /* Added for cancelling mailbox creating  */
429
430         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
431         EM_DEBUG_LOG("size [%d]", buffer_size);
432
433         if(buffer_size <= 0) {
434                 EM_DEBUG_EXCEPTION("buffer_size(%d) should be greater than 0", buffer_size);
435                 err = EMAIL_ERROR_INVALID_PARAM;
436                 goto FINISH_OFF;
437         }
438
439         local_stream = emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
440         em_convert_byte_stream_to_mailbox(local_stream, buffer_size, &mailbox);
441         EM_DEBUG_LOG("Mailbox name - %s", mailbox.mailbox_name);
442
443         on_server = *((int*)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 1));
444
445         emdaemon_add_mailbox(&mailbox, on_server, &handle, &err);
446
447 FINISH_OFF:
448         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
449                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
450         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
451                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 2");
452         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &mailbox.mailbox_id, sizeof(int)))
453                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 3");
454
455         if (!emipc_execute_stub_api(a_hAPI)) {
456                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
457                 return;
458         }
459
460         EM_DEBUG_FUNC_END();
461 }
462
463
464 void stb_delete_mailbox(HIPC_API a_hAPI)
465 {
466         EM_DEBUG_FUNC_BEGIN();
467         int              err = EMAIL_ERROR_NONE;
468         int      on_server              = 0;
469         int      handle = 0; /* Added for cancelling mailbox deleting */
470         int      input_mailbox_id = 0;
471
472         /* src_mailbox_id */
473         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &input_mailbox_id);
474         EM_DEBUG_LOG("input_mailbox_id [%d]", input_mailbox_id);
475
476         if (input_mailbox_id > 0)
477                 EM_DEBUG_LOG("input_mailbox_id [%d]", input_mailbox_id);
478         else
479                 EM_DEBUG_LOG("input_mailbox_id == 0");
480
481         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &on_server);
482
483         if(emdaemon_delete_mailbox(input_mailbox_id, on_server, &handle, &err))
484                 err = EMAIL_ERROR_NONE;
485
486         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
487                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
488         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
489                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
490
491         if (!emipc_execute_stub_api(a_hAPI))
492                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
493
494         EM_DEBUG_FUNC_END();
495 }
496
497 void stb_set_mailbox_type(HIPC_API a_hAPI)
498 {
499         EM_DEBUG_FUNC_BEGIN();
500         int     err = EMAIL_ERROR_NONE;
501         int mailbox_id = 0;
502         int mailbox_type = 0;
503
504         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
505         EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
506
507         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_type);
508         EM_DEBUG_LOG("mailbox_type[%d]", mailbox_type);
509
510         if( (err = emdaemon_set_mailbox_type(mailbox_id, mailbox_type)) != EMAIL_ERROR_NONE)
511                 err = EMAIL_ERROR_NONE;
512
513         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
514                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
515
516         if (!emipc_execute_stub_api(a_hAPI))
517                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
518
519         EM_DEBUG_FUNC_END();
520 }
521
522 void stb_set_local_mailbox(HIPC_API a_hAPI)
523 {
524         EM_DEBUG_FUNC_BEGIN();
525         int     err = EMAIL_ERROR_NONE;
526         int mailbox_id = 0;
527         int is_local_mailbox = 0;
528
529         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
530         EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
531
532         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &is_local_mailbox);
533         EM_DEBUG_LOG("is_local_mailbox[%d]", is_local_mailbox);
534
535         if( (err = emdaemon_set_local_mailbox(mailbox_id, is_local_mailbox)) != EMAIL_ERROR_NONE)
536                 err = EMAIL_ERROR_NONE;
537
538         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
539                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
540
541         if (!emipc_execute_stub_api(a_hAPI))
542                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
543
544         EM_DEBUG_FUNC_END();
545 }
546
547 void stb_set_mail_slot_size_of_mailbox(HIPC_API a_hAPI)
548 {
549         EM_DEBUG_FUNC_BEGIN();
550         int     err    = EMAIL_ERROR_NONE;
551         int handle = 0;
552         int account_id = 0;
553         int mailbox_id = 0;
554         int mail_slot_size = 0;
555
556         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
557         EM_DEBUG_LOG("account_id[%d]", account_id);
558
559         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_id);
560         EM_DEBUG_LOG("mail_slot_size[%d]", mail_slot_size);
561
562         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &mail_slot_size);
563         EM_DEBUG_LOG("mail_slot_size[%d]", mail_slot_size);
564
565         if(emdaemon_set_mail_slot_size_of_mailbox(account_id, mailbox_id, mail_slot_size, &handle, &err))
566                 err = EMAIL_ERROR_NONE;
567         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
568                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
569
570         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
571                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 2");
572
573         if (!emipc_execute_stub_api(a_hAPI))
574                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
575
576         EM_DEBUG_FUNC_END();
577 }
578
579 void stb_rename_mailbox(HIPC_API a_hAPI)
580 {
581         EM_DEBUG_FUNC_BEGIN();
582         int buffer_size  = 0;
583         int     err = EMAIL_ERROR_NONE;
584         int handle = 0;
585         int mailbox_id = 0;
586         int on_server = 0;
587         char *mailbox_path = NULL;
588         char *mailbox_alias = NULL;
589
590         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
591         EM_DEBUG_LOG("mailbox_id[%d]", mailbox_id);
592
593         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 1);
594         EM_DEBUG_LOG("mailbox_path string size[%d]", buffer_size);
595         if(buffer_size > 0)       {
596                 mailbox_path = (char*)em_malloc(buffer_size);
597                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, buffer_size, mailbox_path);
598                 EM_DEBUG_LOG("mailbox_path [%s]", mailbox_path);
599         }
600
601         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 2);
602         EM_DEBUG_LOG("mailbox_alias string size[%d]", buffer_size);
603         if(buffer_size > 0)       {
604                 mailbox_alias = (char*)em_malloc(buffer_size);
605                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, buffer_size, mailbox_alias);
606                 EM_DEBUG_LOG("mailbox_alias [%s]", mailbox_alias);
607         }
608
609         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &on_server);
610         EM_DEBUG_LOG("on_server[%d]", on_server);
611
612         if ((err = emdaemon_rename_mailbox(mailbox_id, mailbox_path, mailbox_alias, on_server, &handle)) != EMAIL_ERROR_NONE) {
613                 EM_DEBUG_EXCEPTION("emdaemon_rename_mailbox failed [%d]", err);
614         }
615
616         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
617                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 1");
618
619         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
620                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed 2");
621
622         if (!emipc_execute_stub_api(a_hAPI))
623                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
624
625         EM_SAFE_FREE(mailbox_alias);
626         EM_SAFE_FREE(mailbox_path);
627
628         EM_DEBUG_FUNC_END();
629 }
630
631 void stb_send_mail(HIPC_API a_hAPI)
632 {
633         EM_DEBUG_FUNC_BEGIN();
634         char* local_stream = NULL;
635         int mail_id;
636         int handle;
637         int err = EMAIL_ERROR_NONE;
638
639         /* Mail_id */
640         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
641         EM_DEBUG_LOG("mail_id [%d]", mail_id);
642
643         if(emdaemon_send_mail(mail_id, &handle, &err)) {
644                 err = EMAIL_ERROR_NONE;
645                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
646                         EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
647                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
648                         EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
649                 if (!emipc_execute_stub_api(a_hAPI))
650                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
651         }
652         else {
653                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
654                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
655                 if (!emipc_execute_stub_api(a_hAPI))
656                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
657         }
658
659         EM_SAFE_FREE(local_stream);
660
661         EM_DEBUG_FUNC_END();
662 }
663
664
665 /* obsolete - there is no api calling this function */
666 void stb_get_mailbox_list(HIPC_API a_hAPI)
667 {
668         EM_DEBUG_FUNC_BEGIN();
669         int err = EMAIL_ERROR_NONE;
670         int counter = 0;
671         char* local_stream = NULL;
672         int account_id;
673         email_mailbox_t* mailbox_list = NULL;
674         int count = 0;
675         int size = 0;
676
677         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
678
679         if(emdaemon_get_mailbox_list(account_id, &mailbox_list, &count, &err))
680                 EM_DEBUG_LOG("emdaemon_get_mailbox_list - success");
681
682         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
683                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
684
685         if(EMAIL_ERROR_NONE == err) {
686                 EM_DEBUG_LOG("Count [%d]", count);
687                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &count, sizeof(int)))
688                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
689
690                 for(counter=0; counter<count; counter++) {
691                         EM_DEBUG_LOG("Name - %s", mailbox_list[counter].mailbox_name);
692
693                         local_stream = em_convert_mailbox_to_byte_stream(mailbox_list+counter, &size);
694
695                         if (!local_stream) {
696                                 EM_DEBUG_EXCEPTION ("INVALID PARAM: local_stream NULL ");
697                                 emcore_free_mailbox_list(&mailbox_list, count);
698                                 return;
699                         }
700
701                         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, local_stream, size))
702                                 EM_DEBUG_EXCEPTION("Add  Param mailbox failed  ");
703
704                         EM_SAFE_FREE(local_stream);
705                         size = 0;
706                 }
707         }
708
709         if (!emipc_execute_stub_api(a_hAPI))
710                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
711
712         emcore_free_mailbox_list(&mailbox_list, count);
713
714         EM_DEBUG_FUNC_END();
715 }
716
717 void stb_delete_all_mails(HIPC_API a_hAPI)
718 {
719         EM_DEBUG_FUNC_BEGIN();
720         int mailbox_id = 0;
721         int from_server = 0;
722         int err = EMAIL_ERROR_NONE;
723
724         /* mailbox_id */
725         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
726
727         /* from_server */
728         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &from_server);
729
730         emdaemon_delete_mail_all(mailbox_id, from_server, NULL, &err);
731
732         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
733                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
734         if (!emipc_execute_stub_api(a_hAPI))
735                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
736
737         EM_DEBUG_FUNC_END();
738 }
739
740 void stb_delete_mail(HIPC_API a_hAPI)
741 {
742         EM_DEBUG_FUNC_BEGIN();
743         int err = EMAIL_ERROR_NONE;
744         int mailbox_id = 0;
745         int from_server = 0;
746         int counter = 0;
747         int num;
748         int *mail_ids = NULL;
749
750         /* Mailbox */
751         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mailbox_id);
752
753         EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
754
755         /* Number of mail_ids */
756         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &num);
757         EM_DEBUG_LOG("number of mails [%d]", num);
758
759         if (num > 0) {
760                 mail_ids = em_malloc(sizeof(int) * num);
761                 if (!mail_ids) {
762                         EM_DEBUG_EXCEPTION("em_malloc failed");
763                         err = EMAIL_ERROR_OUT_OF_MEMORY;
764                         goto FINISH_OFF;
765                 }
766         }
767
768         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, num * sizeof(int), mail_ids);
769
770         for(counter = 0; counter < num; counter++)
771                 EM_DEBUG_LOG("mail_ids [%d]", mail_ids[counter]);
772
773         /* from_server */
774         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &from_server);
775         EM_DEBUG_LOG("from_server [%d]", from_server);
776
777         emdaemon_delete_mail(mailbox_id, mail_ids, num, from_server, NULL, &err);
778
779 FINISH_OFF:
780         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
781                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
782
783         if (!emipc_execute_stub_api(a_hAPI))
784                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
785
786         EM_SAFE_FREE(mail_ids);
787
788         EM_DEBUG_FUNC_END();
789 }
790 void stb_clear_mail_data (HIPC_API a_hAPI)
791 {
792         EM_DEBUG_FUNC_BEGIN();
793         int err = EMAIL_ERROR_NONE;
794
795         if(emdaemon_clear_all_mail_data(&err)) {
796                 EM_DEBUG_LOG(">>> stb_clear_mail_data Success [ %d]  >> ", err);
797         }
798
799         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
800                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
801         if (!emipc_execute_stub_api(a_hAPI))
802                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
803         EM_DEBUG_FUNC_END();
804 }
805
806 void stb_add_rule(HIPC_API a_hAPI)
807 {
808         EM_DEBUG_FUNC_BEGIN();
809         int buffer_size = 0;
810         int err = EMAIL_ERROR_NONE;
811         char* local_rule_stream = NULL;
812         email_rule_t rule = { 0 };
813
814         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
815         if(buffer_size <= 0) {
816                 err = EMAIL_ERROR_INVALID_PARAM;
817                 goto FINISH_OFF;
818         }
819
820         local_rule_stream = (char*)     emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
821         if(!local_rule_stream) {
822                 err = EMAIL_ERROR_INVALID_PARAM;
823                 goto FINISH_OFF;
824         }
825         em_convert_byte_stream_to_rule(local_rule_stream, buffer_size, &rule);
826         EM_DEBUG_LOG("account ID  [%d]", rule.account_id);
827
828         /* call add_filter handler */
829         err = emdaemon_add_filter(&rule);
830
831 FINISH_OFF:
832
833         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
834                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
835         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &rule.filter_id, sizeof(int)))
836                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
837         if (!emipc_execute_stub_api(a_hAPI))
838                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed ");
839
840         emcore_free_rule(&rule);
841
842         EM_DEBUG_FUNC_END();
843 }
844
845
846 /* obsolete - there is no api calling this function */
847 void stb_get_rule(HIPC_API a_hAPI)
848 {
849         EM_DEBUG_FUNC_BEGIN();
850         int err = EMAIL_ERROR_NONE;
851         int filter_id = 0;
852         email_rule_t* rule = NULL;
853         int size =0;
854         char* local_rule_stream = NULL;
855
856         filter_id = *((int*)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0));
857
858         emdaemon_get_filter(filter_id, &rule, &err);
859
860         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
861                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
862
863         /* insert a rule if there exists a rule */
864         if ( rule ) {
865                 local_rule_stream = em_convert_rule_to_byte_stream(rule, &size);
866                 if(!local_rule_stream) { /*prevent 26265*/
867                         EM_DEBUG_EXCEPTION("em_convert_rule_to_byte_stream failed");
868                         emcore_free_rule(rule);
869                         EM_SAFE_FREE(rule);
870                         return;
871                 }
872
873                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, local_rule_stream, size))
874                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
875
876                 EM_SAFE_FREE(local_rule_stream);
877                 emcore_free_rule(rule);
878                 EM_SAFE_FREE(rule);
879         }
880
881         if (!emipc_execute_stub_api(a_hAPI)) {
882                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
883                 return;
884         }
885
886         EM_DEBUG_FUNC_END();
887 }
888
889 /* obsolete - there is no api calling this function */
890 void stb_get_rule_list(HIPC_API a_hAPI)
891 {
892         EM_DEBUG_FUNC_BEGIN();
893         int err = EMAIL_ERROR_NONE;
894         int i = 0;
895         char* local_stream = NULL;
896         int count = 0;
897         int size = 0;
898         email_rule_t* filtering_list = NULL;
899
900         emdaemon_get_filter_list(&filtering_list, &count, &err);
901
902         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
903                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
904
905         /* insert rules if there exist rules*/
906         if( count > 0 ) {
907                 EM_DEBUG_LOG("num of rules [%d]", count);
908                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &count, sizeof(int)))
909                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
910
911                 for(i=0; i<count; i++) {
912                         EM_DEBUG_LOG("Value - %s", filtering_list[i].value);
913
914                         local_stream = em_convert_rule_to_byte_stream(filtering_list+i, &size);
915
916                         if(!local_stream) break;
917
918                         if(!emipc_add_dynamic_parameter(a_hAPI, ePARAMETER_OUT, local_stream, size))
919                                 EM_DEBUG_EXCEPTION("emipc_add_dynamic_parameter failed  ");
920
921                         size = 0;
922                 }
923         }
924
925         if (!emipc_execute_stub_api(a_hAPI)) {
926                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
927         }
928
929         EM_DEBUG_FUNC_END();
930 }
931
932 /* obsolete - there is no api calling this function */
933 void stb_find_rule(HIPC_API a_hAPI)
934 {
935         EM_DEBUG_FUNC_BEGIN();
936         int buffer_size = 0;
937         int err = EMAIL_ERROR_NONE;
938         char* local_rule_stream = NULL;
939         email_rule_t rule = { 0 };
940
941
942         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
943
944         if(buffer_size > 0)      {
945                 local_rule_stream = (char*)em_malloc(buffer_size);
946                 EM_NULL_CHECK_FOR_VOID(local_rule_stream);
947                 if(local_rule_stream) {
948                         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, buffer_size, local_rule_stream);
949                         em_convert_byte_stream_to_rule(local_rule_stream, buffer_size, &rule);
950                         EM_SAFE_FREE(local_rule_stream);
951                         EM_DEBUG_LOG("account ID  [%d]", rule.account_id);
952
953                         if(emdaemon_find_filter(&rule, &err))
954                                 err = EMAIL_ERROR_NONE;
955
956                         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
957                                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
958                         if (!emipc_execute_stub_api(a_hAPI))
959                                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
960
961                 }
962         }
963         EM_DEBUG_FUNC_END();
964 }
965
966 void stb_update_rule(HIPC_API a_hAPI)
967 {
968         EM_DEBUG_FUNC_BEGIN();
969
970         int filter_id = 0;
971         int buffer_size = 0;
972         int err = EMAIL_ERROR_NONE;
973         char* rule_stream = NULL;
974         email_rule_t rule = {0};
975
976
977         /* get filter_id */
978         filter_id = *((int*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0));
979
980         /* get rule */
981         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 1);
982         EM_DEBUG_LOG("size [%d]", buffer_size);
983         if(buffer_size <= 0)  {
984                 err = EMAIL_ERROR_INVALID_PARAM;
985                 goto FINISH_OFF;
986         }
987         rule_stream     = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 1);
988         if(!rule_stream) {
989                 err = EMAIL_ERROR_INVALID_PARAM;
990                 goto FINISH_OFF;
991         }
992         em_convert_byte_stream_to_rule(rule_stream, buffer_size, &rule);
993
994         /* call update handler */
995         emdaemon_update_filter(filter_id, &rule, &err);
996
997 FINISH_OFF:
998         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
999                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1000
1001         if (!emipc_execute_stub_api(a_hAPI))
1002                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1003
1004         emcore_free_rule(&rule);
1005
1006         EM_DEBUG_FUNC_END();
1007 }
1008
1009 void stb_move_all_mails(HIPC_API a_hAPI)
1010 {
1011         EM_DEBUG_FUNC_BEGIN();
1012         int err = EMAIL_ERROR_NONE;
1013         int src_mailbox_id = 0, dst_mailbox_id = 0;
1014
1015         /* src_mailbox_id */
1016         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &src_mailbox_id);
1017         EM_DEBUG_LOG("src_mailbox_id [%d]", src_mailbox_id);
1018
1019         if (src_mailbox_id > 0)
1020                 EM_DEBUG_LOG("src_mailbox_id [%d]", src_mailbox_id);
1021         else
1022                 EM_DEBUG_LOG("src_mailbox_id == 0");
1023
1024         /* dst_mailbox_id */
1025         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &dst_mailbox_id);
1026         EM_DEBUG_LOG("dst_mailbox_id [%d]", dst_mailbox_id);
1027
1028         if (dst_mailbox_id > 0)
1029                 EM_DEBUG_LOG("dst_mailbox_id [%d]", dst_mailbox_id);
1030         else
1031                 EM_DEBUG_LOG("dst_mailbox_id == 0");
1032
1033         if(emdaemon_move_mail_all_mails(src_mailbox_id, dst_mailbox_id, &err))
1034                 EM_DEBUG_LOG("emdaemon_move_mail_all_mails:Success");
1035         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1036                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1037                 return;
1038         }
1039
1040         if (!emipc_execute_stub_api(a_hAPI)) {
1041                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1042                 return;
1043         }
1044         EM_DEBUG_FUNC_END();
1045 }
1046
1047 void stb_set_flags_field(HIPC_API a_hAPI)
1048 {
1049         EM_DEBUG_FUNC_BEGIN();
1050         int err = EMAIL_ERROR_NONE;
1051         email_flags_field_type field_type = 0;
1052         int account_id;
1053         int value = 0;
1054         int onserver = 0;
1055         int num = 0;
1056         int counter = 0;
1057         int *mail_ids = NULL;
1058
1059         /* account_id */
1060         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1061         EM_DEBUG_LOG("account_id [%d]", account_id);
1062
1063         /* Number of mail_ids */
1064         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &num);
1065         EM_DEBUG_LOG("number of mails [%d]", num);
1066
1067         /* mail_id */
1068         mail_ids = em_malloc(sizeof(int) * num);
1069
1070         if(!mail_ids) {
1071                 EM_DEBUG_EXCEPTION("em_malloc failed ");
1072                 err = EMAIL_ERROR_OUT_OF_MEMORY;
1073                 goto FINISH_OFF;
1074         }
1075
1076         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, num * sizeof(int), mail_ids);
1077
1078         for(counter=0; counter < num; counter++)
1079                 EM_DEBUG_LOG("mailids [%d]", mail_ids[counter]);
1080
1081         /* field type */
1082         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 3, sizeof(int), &field_type);
1083         EM_DEBUG_LOG("field_type [%d]", field_type);
1084
1085         /* value */
1086         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 4, sizeof(int), &value);
1087         EM_DEBUG_LOG("value [%d]", value);
1088
1089         /*  on server */
1090         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 5, sizeof(int), &onserver);
1091         EM_DEBUG_LOG("onserver [%d]", onserver);
1092
1093         if(emdaemon_set_flags_field(account_id, mail_ids, num, field_type, value, onserver, &err))
1094                 EM_DEBUG_LOG("emdaemon_set_flags_field - success");
1095
1096 FINISH_OFF:
1097
1098         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1099                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
1100
1101         if (!emipc_execute_stub_api(a_hAPI))
1102                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1103
1104         if (mail_ids)
1105                 EM_SAFE_FREE(mail_ids);
1106
1107         EM_DEBUG_FUNC_END();
1108 }
1109
1110 void stb_add_mail(HIPC_API a_hAPI)
1111 {
1112         EM_DEBUG_FUNC_BEGIN();
1113         int  buffer_size = 0;
1114         int  local_result = 0;
1115         int  result_attachment_data_count = 0;
1116         int  param_index = 0;
1117         int  sync_server = 0;
1118         int  err = EMAIL_ERROR_NONE;
1119         email_mail_data_t result_mail_data = {0};
1120         email_attachment_data_t *result_attachment_data = NULL;
1121         email_meeting_request_t result_meeting_request = {0};
1122
1123
1124         /* email_mail_data_t */;
1125         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1126         EM_DEBUG_LOG("email_attachment_data_t buffer_size[%d]", buffer_size);
1127
1128         /* mail_data */
1129         if(buffer_size > 0)      {
1130                 char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1131                 em_convert_byte_stream_to_mail_data(stream, buffer_size, &result_mail_data);
1132         }
1133
1134         /* attachment */
1135         if (result_mail_data.attachment_count > 0) {
1136                 buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1137                 EM_DEBUG_LOG("email_attachment_data_t buffer_size[%d]", buffer_size);
1138
1139                 if(buffer_size > 0)      {
1140                         char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index);
1141                         em_convert_byte_stream_to_attachment_data(stream, buffer_size, &result_attachment_data, &result_attachment_data_count);
1142
1143                         EM_DEBUG_LOG("result_attachment_data_count[%d]", result_attachment_data_count);
1144
1145                         if(result_attachment_data_count && !result_attachment_data) {
1146                                 EM_DEBUG_EXCEPTION("em_convert_byte_stream_to_attachment_data failed");
1147                                 err = EMAIL_ERROR_ON_PARSING;
1148                                 goto FINISH_OFF;
1149                         }
1150                 }
1151
1152                 param_index++;
1153         }
1154
1155
1156         /* meeting request */
1157         EM_DEBUG_LOG("email_meeting_request_t");
1158         if ( result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
1159                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
1160                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
1161                 buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1162
1163                 if(buffer_size > 0) {
1164                         char* stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1165                         em_convert_byte_stream_to_meeting_req(stream, buffer_size, &result_meeting_request);
1166                 }
1167         }
1168
1169         EM_DEBUG_LOG("sync_server");
1170         emipc_get_parameter(a_hAPI, ePARAMETER_IN, param_index++, sizeof(int), &sync_server);
1171
1172         if( (err = emdaemon_add_mail(&result_mail_data, result_attachment_data, result_attachment_data_count, &result_meeting_request, sync_server)) != EMAIL_ERROR_NONE) {
1173                 EM_DEBUG_EXCEPTION("emdaemon_add_mail failed [%d]", err);
1174                 goto FINISH_OFF;
1175         }
1176
1177         local_result = 1;
1178         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1179                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1180         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.mail_id, sizeof(int)))
1181                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1182         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.thread_id, sizeof(int)))
1183                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1184         if (!emipc_execute_stub_api(a_hAPI))
1185                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1186
1187 FINISH_OFF:
1188         if ( local_result == 0 ) {
1189                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1190                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1191                 if (!emipc_execute_stub_api(a_hAPI))
1192                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1193         }
1194
1195         emcore_free_mail_data(&result_mail_data);
1196
1197         if(result_attachment_data)
1198                 emcore_free_attachment_data(&result_attachment_data, result_attachment_data_count, NULL);
1199
1200         emstorage_free_meeting_request(&result_meeting_request);
1201
1202         em_flush_memory();
1203
1204         EM_DEBUG_FUNC_END();
1205 }
1206
1207
1208 void stb_update_mail(HIPC_API a_hAPI)
1209 {
1210         EM_DEBUG_FUNC_BEGIN();
1211         int  buffer_size = 0;
1212         int  local_result = 0;
1213         int  result_attachment_data_count = 0;
1214         int  param_index = 0;
1215         int  sync_server = 0;
1216         int  *temp_buffer = NULL;
1217         int  err = EMAIL_ERROR_NONE;
1218         email_mail_data_t result_mail_data = {0};
1219         email_attachment_data_t *result_attachment_data = NULL;
1220         email_meeting_request_t result_meeting_request = {0};
1221
1222         EM_DEBUG_LOG("email_mail_data_t");
1223         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1224
1225         if(buffer_size > 0)      {
1226                 char* stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1227                 em_convert_byte_stream_to_mail_data(stream, buffer_size, &result_mail_data);
1228         }
1229
1230         if (result_mail_data.attachment_count > 0) {
1231                 buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1232                 EM_DEBUG_LOG("email_attachment_data_t buffer_size[%d]", buffer_size);
1233
1234                 if(buffer_size > 0) {
1235                         char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index);
1236                         em_convert_byte_stream_to_attachment_data(stream, buffer_size, &result_attachment_data, &result_attachment_data_count);
1237
1238                         EM_DEBUG_LOG("result_attachment_data_count[%d]", result_attachment_data_count);
1239
1240                         if(result_attachment_data_count && !result_attachment_data) {
1241                                 EM_DEBUG_EXCEPTION("em_convert_byte_stream_to_attachment_data failed");
1242                                 err = EMAIL_ERROR_ON_PARSING;
1243                                 goto FINISH_OFF;
1244                         }
1245                 }
1246                 param_index++;
1247         }
1248
1249         EM_DEBUG_LOG("email_meeting_request_t");
1250
1251         if ( result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_REQUEST
1252                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_RESPONSE
1253                 || result_mail_data.meeting_request_status == EMAIL_MAIL_TYPE_MEETING_ORIGINATINGREQUEST) {
1254                 buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
1255
1256                 if(buffer_size > 0) {
1257                         char* stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1258                         em_convert_byte_stream_to_meeting_req(stream, buffer_size, &result_meeting_request);
1259                 }
1260         }
1261
1262         EM_DEBUG_LOG("sync_server");
1263
1264         temp_buffer = emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
1265
1266         if(!temp_buffer) {
1267                 EM_DEBUG_EXCEPTION("emipc_get_nth_parameter_data[%d] failed", param_index - 1);
1268                 err = EMAIL_ERROR_IPC_PROTOCOL_FAILURE;
1269                 goto FINISH_OFF;
1270         }
1271
1272         sync_server = *temp_buffer;
1273
1274         if( (err = emdaemon_update_mail(&result_mail_data, result_attachment_data,
1275                         result_attachment_data_count, &result_meeting_request, sync_server)) != EMAIL_ERROR_NONE) {
1276                 EM_DEBUG_EXCEPTION("emdaemon_update_mail failed [%d]", err);
1277                 goto FINISH_OFF;
1278         }
1279
1280         local_result = 1;
1281         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1282                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1283         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.mail_id, sizeof(int)))
1284                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1285         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &result_mail_data.thread_id, sizeof(int)))
1286                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1287         if (!emipc_execute_stub_api(a_hAPI))
1288                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1289
1290 FINISH_OFF:
1291         if ( local_result == 0 ) {
1292                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1293                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1294                 if (!emipc_execute_stub_api(a_hAPI))
1295                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1296         }
1297
1298         emcore_free_mail_data(&result_mail_data);
1299
1300         if(result_attachment_data)
1301                 emcore_free_attachment_data(&result_attachment_data, result_attachment_data_count, NULL);
1302
1303         emstorage_free_meeting_request(&result_meeting_request);
1304
1305         em_flush_memory();
1306         EM_DEBUG_FUNC_END();
1307 }
1308
1309
1310 void stb_move_thread_to_mailbox(HIPC_API a_hAPI)
1311 {
1312         EM_DEBUG_FUNC_BEGIN();
1313         int mailbox_id = 0, thread_id = 0, move_always_flag = 0;
1314         int err = EMAIL_ERROR_NONE;
1315         char *target_mailbox_name = NULL;
1316
1317         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &thread_id);
1318         EM_DEBUG_LOG("thread_id [%d]", thread_id);
1319
1320         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_id);
1321         EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
1322
1323         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &move_always_flag);
1324         EM_DEBUG_LOG("move_always_flag [%d]", move_always_flag);
1325
1326         if(emdaemon_move_mail_thread_to_mailbox(thread_id, mailbox_id, move_always_flag, &err))
1327                 EM_DEBUG_LOG("emdaemon_move_mail_thread_to_mailbox success");
1328
1329         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1330                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1331                 EM_SAFE_FREE(target_mailbox_name);
1332                 return;
1333         }
1334
1335         if (!emipc_execute_stub_api(a_hAPI)) {
1336                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1337                 EM_SAFE_FREE(target_mailbox_name);
1338                 return;
1339         }
1340
1341         EM_SAFE_FREE(target_mailbox_name);
1342         EM_DEBUG_FUNC_END();
1343 }
1344
1345 void stb_delete_thread(HIPC_API a_hAPI)
1346 {
1347         EM_DEBUG_FUNC_BEGIN();
1348
1349         int thread_id = 0, delete_always_flag = 0;
1350         int handle = 0;
1351         int err = EMAIL_ERROR_NONE;
1352
1353         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &thread_id);
1354         EM_DEBUG_LOG("thread_id [%d]", thread_id);
1355
1356         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &delete_always_flag);
1357         EM_DEBUG_LOG("delete_always_flag [%d]", delete_always_flag);
1358
1359         if(emdaemon_delete_mail_thread(thread_id, delete_always_flag, &handle, &err))
1360                 EM_DEBUG_LOG("emdaemon_delete_mail_thread success");
1361
1362         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1363                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1364                 return;
1365         }
1366
1367         if (!emipc_execute_stub_api(a_hAPI)) {
1368                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1369                 return;
1370         }
1371         EM_DEBUG_FUNC_END();
1372 }
1373
1374 void stb_modify_seen_flag_of_thread(HIPC_API a_hAPI)
1375 {
1376         EM_DEBUG_FUNC_BEGIN();
1377
1378         int thread_id = 0, seen_flag = 0, on_server = 0;
1379         int handle = 0;
1380         int err = EMAIL_ERROR_NONE;
1381
1382         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &thread_id);
1383         EM_DEBUG_LOG("thread_id [%d]", thread_id);
1384
1385         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &seen_flag);
1386         EM_DEBUG_LOG("seen_flag [%d]", seen_flag);
1387
1388         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &on_server);
1389         EM_DEBUG_LOG("on_server [%d]", on_server);
1390
1391         if(emdaemon_modify_seen_flag_of_thread(thread_id, seen_flag, on_server, &handle, &err))
1392                 EM_DEBUG_LOG("emdaemon_modify_seen_flag_of_thread success");
1393
1394         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1395                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1396                 return;
1397         }
1398
1399         if (!emipc_execute_stub_api(a_hAPI)) {
1400                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1401                 return;
1402         }
1403         EM_DEBUG_FUNC_END();
1404 }
1405
1406 void stb_expunge_mails_deleted_flagged(HIPC_API a_hAPI)
1407 {
1408         EM_DEBUG_FUNC_BEGIN();
1409
1410         int mailbox_id = 0, on_server = 0;
1411         int handle = 0;
1412         int err = EMAIL_ERROR_NONE;
1413
1414         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), (void*)&mailbox_id);
1415         EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
1416
1417         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), (void*)&on_server);
1418         EM_DEBUG_LOG("on_server [%d]", on_server);
1419
1420         if( (err = emdaemon_expunge_mails_deleted_flagged(mailbox_id, on_server, &handle)) != EMAIL_ERROR_NONE)
1421                 EM_DEBUG_LOG("emdaemon_expunge_mails_deleted_flagged success");
1422
1423         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int))) {
1424                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1425                 return;
1426         }
1427
1428         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int))) {
1429                 EM_DEBUG_LOG("ipcAPI_AddParameter local_result failed ");
1430                 return;
1431         }
1432
1433         if (!emipc_execute_stub_api(a_hAPI)) {
1434                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1435                 return;
1436         }
1437         EM_DEBUG_FUNC_END();
1438 }
1439
1440 void stb_move_mail(HIPC_API a_hAPI)
1441 {
1442         int err = EMAIL_ERROR_NONE;
1443         int num = 0, counter = 0, mailbox_id = 0;
1444
1445         /* Number of mail_ids */
1446         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &num);
1447         EM_DEBUG_LOG("number of mails [%d]", num);
1448
1449         /* mail_id */
1450         int mail_ids[num];
1451         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, num * sizeof(int), mail_ids);
1452
1453         for(counter = 0; counter < num; counter++)
1454                 EM_DEBUG_LOG("mailids [%d]", mail_ids[counter]);
1455
1456         /* target_mailbox_id */
1457         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &mailbox_id);
1458         EM_DEBUG_LOG("target_mailbox_id [%d]", mailbox_id);
1459
1460         if (mailbox_id > 0)
1461                 EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
1462         else
1463                 EM_DEBUG_LOG("mailbox_id == 0");
1464
1465         if(emdaemon_move_mail(mail_ids, num, mailbox_id, EMAIL_MOVED_BY_COMMAND, 0, &err))
1466                 EM_DEBUG_LOG("emdaemon_move_mail success");
1467
1468         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1469                 EM_DEBUG_EXCEPTION("emipc_add_parameter fail");
1470
1471         if (!emipc_execute_stub_api(a_hAPI))
1472                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api fail");
1473         EM_DEBUG_FUNC_END();
1474 }
1475
1476 void stb_delete_rule(HIPC_API a_hAPI)
1477 {
1478         EM_DEBUG_FUNC_BEGIN();
1479
1480         int filter_id = 0;
1481         int err = EMAIL_ERROR_NONE;
1482
1483         /* filter_id */
1484         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &filter_id);
1485
1486         if(emdaemon_delete_filter(filter_id, &err))
1487                 err = EMAIL_ERROR_NONE;
1488
1489         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1490                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1491
1492         if (!emipc_execute_stub_api(a_hAPI))
1493                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1494         EM_DEBUG_FUNC_END();
1495 }
1496
1497 void stb_apply_rule(HIPC_API a_hAPI)
1498 {
1499         EM_DEBUG_FUNC_BEGIN();
1500
1501         int filter_id = 0;
1502         int err = EMAIL_ERROR_NONE;
1503
1504         /* filter_id */
1505         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &filter_id);
1506
1507         if(emdaemon_apply_filter(filter_id, &err))
1508                 err = EMAIL_ERROR_NONE;
1509
1510         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1511                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1512
1513         if (!emipc_execute_stub_api(a_hAPI))
1514                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1515         EM_DEBUG_FUNC_END();
1516 }
1517
1518 void stb_add_attachment(HIPC_API a_hAPI)
1519 {
1520         EM_DEBUG_FUNC_BEGIN();
1521         int buffer_size = 0;
1522         int err = EMAIL_ERROR_NONE;
1523         int mail_id = -1;
1524         int attachment_count = 0;
1525         char* attachment_stream = NULL;
1526         email_attachment_data_t* attachment = NULL;
1527
1528         /* mail_id */
1529         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
1530
1531         /* attachment */
1532         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 1);
1533
1534         if(buffer_size > 0)      {
1535                 attachment_stream = (char*)em_malloc(buffer_size);
1536                 EM_NULL_CHECK_FOR_VOID(attachment_stream);
1537                 if(attachment_stream) {
1538                         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, buffer_size, attachment_stream);
1539                         em_convert_byte_stream_to_attachment_data(attachment_stream, buffer_size, &attachment, &attachment_count);
1540                         EM_SAFE_FREE(attachment_stream);
1541                 }
1542         }
1543
1544         if (!attachment) {
1545                 EM_DEBUG_EXCEPTION("em_convert_byte_stream_to_attachment_data failed  ");
1546                 return;
1547         }
1548
1549         emdaemon_add_attachment(mail_id, attachment, &err);
1550
1551         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1552                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
1553         if(EMAIL_ERROR_NONE == err) {
1554                 EM_DEBUG_LOG("emdaemon_add_attachment -Success");
1555
1556                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &(attachment->attachment_id), sizeof(int)))
1557                         EM_DEBUG_EXCEPTION("emipc_add_parameter attachment_id failed ");
1558         }
1559                 if (!emipc_execute_stub_api(a_hAPI))
1560                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1561
1562         EM_SAFE_FREE(attachment);
1563         EM_DEBUG_FUNC_END();
1564 }
1565
1566 void stb_get_attachment(HIPC_API a_hAPI)
1567 {
1568         EM_DEBUG_FUNC_BEGIN();
1569         int err = EMAIL_ERROR_NONE;
1570         int attachment_id;
1571         char* attachment_stream = NULL;
1572         email_attachment_data_t* attachment = NULL;
1573         int size = 0;
1574
1575         /* attachment_id */
1576         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &attachment_id);
1577
1578         emdaemon_get_attachment(attachment_id, &attachment, &err);
1579
1580         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1581                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
1582
1583         if(EMAIL_ERROR_NONE == err) {
1584                 EM_DEBUG_LOG("emdaemon_get_attachment - Success");
1585                 /* attachment */
1586                 attachment_stream = em_convert_attachment_data_to_byte_stream(attachment, 1, &size);
1587                 if(!attachment_stream) { /*prevent 26263*/
1588                         emcore_free_attachment_data(&attachment, 1, &err);
1589                         return;
1590                 }
1591                 EM_NULL_CHECK_FOR_VOID(attachment_stream);
1592
1593                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, attachment_stream, size))
1594                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed  ");
1595         }
1596
1597         if (!emipc_execute_stub_api(a_hAPI)) {
1598                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1599         }
1600
1601         EM_SAFE_FREE(attachment_stream);
1602         emcore_free_attachment_data(&attachment, 1, &err);
1603
1604         EM_DEBUG_FUNC_END();
1605 }
1606
1607 void stb_get_imap_mailbox_list(HIPC_API a_hAPI)
1608 {
1609         EM_DEBUG_FUNC_BEGIN();
1610         int err = EMAIL_ERROR_NONE;
1611         int account_id = 0;
1612         int handle = 0;
1613
1614         /* account_id */
1615         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1616
1617         if(emdaemon_get_imap_mailbox_list(account_id, "", &handle, &err))
1618                 err = EMAIL_ERROR_NONE;
1619
1620         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1621                 EM_DEBUG_LOG("ipcAPI_AddParameter local_result failed ");
1622         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
1623                 EM_DEBUG_LOG("ipcAPI_AddParameter local_result failed ");
1624         if (!emipc_execute_stub_api(a_hAPI))
1625                 EM_DEBUG_LOG("emipc_execute_stub_api failed  ");
1626         EM_DEBUG_FUNC_END();
1627 }
1628
1629 void stb_delete_attachment(HIPC_API a_hAPI)
1630 {
1631         EM_DEBUG_FUNC_BEGIN();
1632         int err = EMAIL_ERROR_NONE;
1633         int attachment_id = 0;
1634
1635         /* attachment_index */
1636         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &attachment_id);
1637
1638         emdaemon_delete_mail_attachment(attachment_id, &err);
1639
1640         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1641                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
1642         if (!emipc_execute_stub_api(a_hAPI))
1643                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1644
1645         EM_DEBUG_FUNC_END();
1646 }
1647
1648 void stb_download_attachment(HIPC_API a_hAPI)
1649 {
1650         EM_DEBUG_FUNC_BEGIN();
1651
1652         int err = EMAIL_ERROR_NONE;
1653         int mail_id = 0;
1654         int nth = 0;
1655         int handle = 0;
1656         int account_id = 0;
1657
1658         EM_DEBUG_LOG("account_id");
1659         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1660
1661         EM_DEBUG_LOG("mail_id");
1662         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mail_id);
1663
1664         EM_DEBUG_LOG("nth");
1665         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &nth);
1666
1667         if(emdaemon_download_attachment(account_id, mail_id, nth, &handle, &err)) {
1668                 err = EMAIL_ERROR_NONE;
1669
1670                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1671                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1672                 EM_DEBUG_LOG(">>>>>>>>>> HANDLE = %d", handle);
1673                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
1674                         EM_DEBUG_EXCEPTION("emipc_add_parameter handle failed ");
1675                 if (!emipc_execute_stub_api(a_hAPI))
1676                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1677         }
1678         else {
1679                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1680                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1681                 /* Download handle - 17-Apr-09 */
1682                 EM_DEBUG_LOG(">>>>>>>>>> HANDLE = %d", handle);
1683                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
1684                         EM_DEBUG_EXCEPTION("emipc_add_parameter handle failed ");
1685                 if (!emipc_execute_stub_api(a_hAPI))
1686                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1687         }
1688
1689         EM_DEBUG_FUNC_END();
1690 }
1691
1692 void stb_mail_send_saved(HIPC_API a_hAPI)
1693 {
1694         EM_DEBUG_FUNC_BEGIN();
1695         int err = EMAIL_ERROR_NONE;
1696         int account_id = 0;
1697
1698         /* account_id */
1699         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1700
1701         EM_DEBUG_LOG("calling emdaemon_send_mail_saved");
1702         if(emdaemon_send_mail_saved(account_id, NULL, &err))
1703                 err = EMAIL_ERROR_NONE;
1704
1705         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1706                 EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
1707
1708         if (!emipc_execute_stub_api(a_hAPI))
1709                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1710
1711         EM_DEBUG_FUNC_END();
1712 }
1713
1714 void stb_add_read_receipt(HIPC_API a_hAPI){
1715         EM_DEBUG_FUNC_BEGIN();
1716         int read_mail_id = 0;
1717         int receipt_mail_id = 0;
1718         int err = EMAIL_ERROR_NONE;
1719
1720         /* read_mail_id */
1721         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &read_mail_id);
1722         EM_DEBUG_LOG("read_mail_id [%d]", read_mail_id);
1723
1724         if( (err = emcore_add_read_receipt(read_mail_id, &receipt_mail_id)) != EMAIL_ERROR_NONE )
1725                 EM_DEBUG_EXCEPTION("emcore_add_read_receipt failed [%d]", err);
1726
1727         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1728                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1729
1730         if (err == EMAIL_ERROR_NONE) {
1731                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &receipt_mail_id, sizeof(int)))
1732                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1733         }
1734
1735         if (!emipc_execute_stub_api(a_hAPI))
1736                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1737
1738         EM_DEBUG_FUNC_END("err [%d]", err);
1739 }
1740
1741 void stb_retry_sending_mail(HIPC_API a_hAPI)
1742 {
1743
1744         EM_DEBUG_FUNC_BEGIN();
1745         int mail_id = 0;
1746         int timeout_in_sec = 0;
1747         int err = EMAIL_ERROR_NONE;
1748
1749         /* Mail_id */
1750         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
1751         EM_DEBUG_LOG("mail_id [%d]", mail_id);
1752
1753         /* timeout_in_sec */
1754         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &timeout_in_sec);
1755         EM_DEBUG_LOG("timeout_in_sec [%d]", timeout_in_sec);
1756
1757         if(emdaemon_send_mail_retry(mail_id, timeout_in_sec,&err))
1758                 EM_DEBUG_LOG("emdaemon_get_mailbox_list - success");
1759
1760         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1761                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1762
1763                 if (!emipc_execute_stub_api(a_hAPI))
1764                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1765         EM_DEBUG_FUNC_END();
1766 }
1767
1768 void stb_get_event_queue_status(HIPC_API a_hAPI)
1769 {
1770         EM_DEBUG_FUNC_BEGIN();
1771         int on_sending = 0;
1772         int on_receiving = 0;
1773
1774         /*get the network status */
1775         emdaemon_get_event_queue_status(&on_sending, &on_receiving);
1776
1777         /* on_sending */
1778         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &on_sending, sizeof(int)))
1779                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1780
1781         /* on_receving */
1782         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &on_receiving, sizeof(int)))
1783                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1784
1785         EM_DEBUG_LOG("stb_get_event_queue_status - Before Execute API");
1786         if (!emipc_execute_stub_api(a_hAPI))
1787                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1788
1789         EM_DEBUG_FUNC_END();
1790 }
1791
1792 void stb_cancel_job(HIPC_API a_hAPI)
1793 {
1794         EM_DEBUG_FUNC_BEGIN();
1795         int account_id = 0;
1796         int handle = 0;
1797         int err = EMAIL_ERROR_NONE;
1798
1799         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1800         EM_DEBUG_LOG("account_id [%d]", account_id);
1801
1802         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &handle);
1803         EM_DEBUG_LOG("handle [%d]", handle);
1804
1805         if(emdaemon_cancel_job(account_id, handle, &err))
1806                 err = EMAIL_ERROR_NONE;
1807
1808         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1809                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
1810         if (!emipc_execute_stub_api(a_hAPI))
1811                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1812         EM_DEBUG_FUNC_END();
1813 }
1814
1815 void stb_get_pending_job(HIPC_API a_hAPI)
1816 {
1817         EM_DEBUG_FUNC_BEGIN();
1818         email_action_t action = -1;
1819         int account_id = 0;
1820         int mail_id = -1;
1821         int err = EMAIL_ERROR_NONE;
1822         email_event_status_type_t status = 0;
1823
1824         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &action);
1825         EM_DEBUG_LOG("action [%d]", action);
1826
1827         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &account_id);
1828         EM_DEBUG_LOG("account_id [%d]", account_id);
1829
1830         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &mail_id);
1831         EM_DEBUG_LOG("mail_id [%d]", mail_id);
1832
1833         if(emdaemon_get_pending_job(action, account_id, mail_id, &status))
1834                 err = EMAIL_ERROR_NONE;
1835         else
1836                 err = EMAIL_ERROR_UNKNOWN;
1837
1838         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1839                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
1840         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &status, sizeof(int)))
1841                 EM_DEBUG_EXCEPTION("emipc_add_parameter status failed ");
1842         if (!emipc_execute_stub_api(a_hAPI))
1843                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
1844         EM_DEBUG_FUNC_END();
1845 }
1846
1847 void stb_print_receiving_event_queue_via_debug_msg(HIPC_API a_hAPI)
1848 {
1849         email_event_t *event_queue = NULL;
1850         int event_active_queue = 0, err, i;
1851
1852         emcore_get_receiving_event_queue(&event_queue, &event_active_queue, &err);
1853
1854         EM_DEBUG_LOG("======================================================================");
1855         EM_DEBUG_LOG("Event active index [%d]", event_active_queue);
1856         EM_DEBUG_LOG("======================================================================");
1857         for(i = 1; i < 32; i++)
1858                 EM_DEBUG_LOG("event[%d] : type[%d], account_id[%d], arg[%d], status[%d]", i, event_queue[i].type, event_queue[i].account_id, event_queue[i].event_param_data_4, event_queue[i].status);
1859         EM_DEBUG_LOG("======================================================================");
1860         EM_DEBUG_LOG("======================================================================");
1861
1862         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1863                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1864
1865         if (!emipc_execute_stub_api(a_hAPI))
1866                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1867         EM_DEBUG_FUNC_END();
1868 }
1869
1870 void stb_cancel_send_mail_job(HIPC_API a_hAPI)
1871 {
1872         EM_DEBUG_FUNC_BEGIN();
1873         int mail_id = 0;
1874         int err = EMAIL_ERROR_NONE;
1875         int account_id = 0;
1876
1877         /* account_id */
1878         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1879         EM_DEBUG_LOG("account_id [%d]", account_id);
1880
1881         /* Mail_id */
1882         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mail_id);
1883         EM_DEBUG_LOG("mail_id [%d]", mail_id);
1884
1885         if(emdaemon_cancel_sending_mail_job(account_id,mail_id,&err))
1886                 EM_DEBUG_LOG("success");
1887
1888         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1889                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1890
1891         if (!emipc_execute_stub_api(a_hAPI))
1892                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1893         EM_DEBUG_FUNC_END();
1894 }
1895
1896 void stb_search_mail_on_server(HIPC_API a_hAPI)
1897 {
1898         EM_DEBUG_FUNC_BEGIN();
1899         int                    i = 0;
1900         int                    err = EMAIL_ERROR_NONE;
1901         int                    account_id = 0;
1902         int                                        mailbox_id = 0;
1903         int                    buffer_size = 0;
1904         int                    search_filter_count = 0;
1905         char                  *stream_for_search_filter_list = NULL;
1906         unsigned int           job_handle = 0;
1907         email_search_filter_t *search_filter_list = NULL;
1908
1909         /* account_id */
1910         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1911         EM_DEBUG_LOG("account_id [%d]", account_id);
1912
1913         /* mailbox_id */
1914         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &mailbox_id);
1915         EM_DEBUG_LOG("mailbox_id [%d]", mailbox_id);
1916
1917         /* search_filter_list */
1918         buffer_size = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 2);
1919
1920         if(buffer_size > 0)      {
1921                 stream_for_search_filter_list = (char*)em_malloc(buffer_size);
1922                 EM_NULL_CHECK_FOR_VOID(stream_for_search_filter_list);
1923                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, buffer_size, stream_for_search_filter_list);
1924                 em_convert_byte_stream_to_search_filter(stream_for_search_filter_list, &search_filter_list, &search_filter_count);
1925                 EM_SAFE_FREE(stream_for_search_filter_list);
1926         }
1927
1928         if(!emdaemon_search_mail_on_server(account_id ,mailbox_id, search_filter_list, search_filter_count, &job_handle, &err))
1929                 EM_DEBUG_LOG("success");
1930
1931         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1932                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1933
1934         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &job_handle, sizeof(int)))
1935                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
1936
1937         if (!emipc_execute_stub_api(a_hAPI))
1938                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1939
1940         if(search_filter_list) {
1941                 for(i = 0; i < search_filter_count; i++) {
1942                         switch(search_filter_list[i].search_filter_type) {
1943                                 case EMAIL_SEARCH_FILTER_TYPE_BCC              :
1944                                 case EMAIL_SEARCH_FILTER_TYPE_CC               :
1945                                 case EMAIL_SEARCH_FILTER_TYPE_FROM             :
1946                                 case EMAIL_SEARCH_FILTER_TYPE_KEYWORD          :
1947                                 case EMAIL_SEARCH_FILTER_TYPE_SUBJECT          :
1948                                 case EMAIL_SEARCH_FILTER_TYPE_TO               :
1949                                 case EMAIL_SEARCH_FILTER_TYPE_MESSAGE_ID       :
1950                                         EM_SAFE_FREE(search_filter_list[i].search_filter_key_value.string_type_key_value);
1951                                         break;
1952                                 default :
1953                                         break;
1954                         }
1955                 }
1956         }
1957
1958         EM_SAFE_FREE(search_filter_list);
1959
1960         EM_DEBUG_FUNC_END();
1961 }
1962
1963 void stb_clear_result_of_search_mail_on_server(HIPC_API a_hAPI)
1964 {
1965         EM_DEBUG_FUNC_BEGIN();
1966         int                      err = EMAIL_ERROR_NONE;
1967         int                      account_id = 0;
1968         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
1969
1970         /* account_id */
1971         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
1972
1973         EM_DEBUG_LOG("account_id [%d]", account_id);
1974
1975         if (!emstorage_get_mailbox_by_mailbox_type(account_id, EMAIL_MAILBOX_TYPE_SEARCH_RESULT, &mailbox_tbl, true, &err)) {
1976                 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1977                 goto FINISH_OFF;
1978         }
1979
1980         if (!emstorage_delete_mail_by_mailbox(account_id, mailbox_tbl->mailbox_id, true, &err))
1981                 EM_DEBUG_EXCEPTION(" emstorage_get_mailbox_by_mailbox_type failed [%d]", err);
1982
1983 FINISH_OFF:
1984
1985         if (mailbox_tbl) {
1986                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
1987         }
1988
1989         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
1990                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
1991
1992         if (!emipc_execute_stub_api(a_hAPI))
1993                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
1994
1995         EM_DEBUG_FUNC_END();
1996 }
1997
1998
1999
2000 void stb_add_account_with_validation(HIPC_API a_hAPI)
2001 {
2002         EM_DEBUG_FUNC_BEGIN();
2003         int buffer_size = 0;
2004         int local_result = 0;
2005         int handle = 0;
2006         char* stream = NULL;
2007         email_account_t *account = NULL;
2008         email_account_t *ref_account = NULL;
2009         int ref_check_interval = 0;
2010         int ref_account_id = 0;
2011         int err = EMAIL_ERROR_NONE;
2012
2013         /* get account info */
2014         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2015         EM_DEBUG_LOG("size [%d]", buffer_size);
2016         if(buffer_size <= 0) {
2017                 err = EMAIL_ERROR_INVALID_PARAM;
2018                 goto FINISH_OFF;
2019         }
2020
2021         stream =(char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
2022         if(!stream) {
2023                 err = EMAIL_ERROR_INVALID_PARAM;
2024                 goto FINISH_OFF;
2025         }
2026
2027         account = em_malloc(sizeof(email_account_t));
2028
2029         if(account == NULL) {
2030                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
2031                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2032                 goto FINISH_OFF;
2033         }
2034
2035         em_convert_byte_stream_to_account(stream, buffer_size, account);
2036
2037         if((err = emcore_add_account_to_unvalidated_account_list(account)) != EMAIL_ERROR_NONE) {
2038                 EM_DEBUG_EXCEPTION("emcore_add_account_to_unvalidated_account_list failed [%d]", err);
2039                 goto FINISH_OFF;
2040         }
2041
2042         ref_account = emcore_get_account_reference(account->account_id);
2043
2044         if (!ref_account) {
2045                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", err);
2046                 goto FINISH_OFF;
2047         }
2048
2049         ref_check_interval = ref_account->check_interval;
2050         ref_account_id = ref_account->account_id;
2051
2052         if(!emdaemon_validate_account_and_create(ref_account, &handle, &err)) {
2053                 EM_DEBUG_EXCEPTION("emdaemon_validate_account_and_create fail [%d]", err);
2054                 goto FINISH_OFF;
2055         }
2056 #ifdef __FEATURE_AUTO_POLLING__
2057         /*  start auto polling, if check_interval not zero */
2058         if(ref_check_interval > 0) {
2059                 if(!emdaemon_add_polling_alarm(ref_account_id, ref_check_interval))
2060                         EM_DEBUG_EXCEPTION("emdaemon_add_polling_alarm[NOTI_ACCOUNT_ADD] : start auto poll failed >>> ");
2061         }
2062 #endif /*  __FEATURE_AUTO_POLLING__ */
2063         /*  add account details to contact DB  */
2064         /*  emdaemon_insert_accountinfo_to_contact(account); */
2065
2066         local_result = 1;
2067         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2068                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2069         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
2070                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2071         if (!emipc_execute_stub_api(a_hAPI))
2072                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2073
2074 FINISH_OFF:
2075         if ( local_result == 0 ) { /* there is an error */
2076                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2077                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : local_result ");
2078                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2079                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
2080                 if (!emipc_execute_stub_api(a_hAPI))
2081                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2082         }
2083
2084 /*      note: account is freed in thread_func_branch_command, which is run by other thread */
2085 /*      emcore_free_account(account); */
2086
2087         EM_DEBUG_FUNC_END();
2088 }
2089
2090 void stb_backup_account(HIPC_API a_hAPI)
2091 {
2092         EM_DEBUG_FUNC_BEGIN();
2093
2094 #ifdef __FEATURE_BACKUP_ACCOUNT__
2095         char *file_path = NULL;
2096         int local_result = 0, err_code = 0, handle = 0, file_path_length = 0;
2097
2098         /* file_path_length */
2099         file_path_length = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2100         if(file_path_length > 0) {
2101                 EM_DEBUG_LOG("file_path_length [%d]", file_path_length);
2102                 file_path = em_malloc(file_path_length);
2103                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, file_path_length, file_path);
2104                 EM_DEBUG_LOG("file_path [%s]", file_path);
2105                 local_result = emcore_backup_accounts((const char*)file_path, &err_code);
2106         }
2107
2108         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2109                 EM_DEBUG_LOG("emipc_add_parameter failed ");
2110
2111         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
2112                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2113
2114         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
2115                 EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
2116
2117         if (!emipc_execute_stub_api(a_hAPI))
2118                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2119
2120         EM_SAFE_FREE(file_path);
2121 #endif /*  __FEATURE_BACKUP_ACCOUNT__ */
2122         EM_DEBUG_FUNC_END();
2123 }
2124
2125 void stb_restore_account(HIPC_API a_hAPI)
2126 {
2127         EM_DEBUG_FUNC_BEGIN();
2128 #ifdef __FEATURE_BACKUP_ACCOUNT__
2129         char *file_path = NULL;
2130         int local_result = 0, err_code = 0, handle = 0, file_path_length = 0;
2131
2132         /* file_path_length */
2133         file_path_length = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2134         if(file_path_length > 0)  {
2135                 EM_DEBUG_LOG("file_path_length [%d]", file_path_length);
2136                 file_path = em_malloc(file_path_length);
2137                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, file_path_length, file_path);
2138                 EM_DEBUG_LOG("file_path [%s]", file_path);
2139                 local_result = emcore_restore_accounts((const char*)file_path, &err_code);
2140         }
2141
2142         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2143                 EM_DEBUG_LOG("emipc_add_parameter failed ");
2144
2145         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
2146                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2147
2148         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
2149                 EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
2150
2151         if (!emipc_execute_stub_api(a_hAPI))
2152                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2153
2154         EM_SAFE_FREE(file_path);
2155 #endif /*  __FEATURE_BACKUP_ACCOUNT__ */
2156         EM_DEBUG_FUNC_END();
2157 }
2158
2159
2160 void stb_get_password_length(HIPC_API a_hAPI)
2161 {
2162         EM_DEBUG_FUNC_BEGIN();
2163         int account_id = 0;
2164         int local_result = 0;
2165         int err_code = 0;
2166         int password_length = 0;
2167
2168         /* account_id */
2169         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2170         local_result = emstorage_get_password_length_of_account(account_id, &password_length,&err_code);
2171
2172         EM_DEBUG_LOG("password_length [%d]", password_length);
2173
2174         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2175                 EM_DEBUG_LOG("emipc_add_parameter failed ");
2176
2177         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err_code, sizeof(int)))
2178                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2179
2180         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &password_length, sizeof(int)))
2181                 EM_DEBUG_EXCEPTION("emipc_add_parameter result failed ");
2182
2183         if (!emipc_execute_stub_api(a_hAPI))
2184                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2185         EM_DEBUG_FUNC_END();
2186 }
2187
2188 void stb_add_certificate(HIPC_API a_hAPI)
2189 {
2190         int err = EMAIL_ERROR_NONE;
2191         int cert_file_len = 0;
2192         int email_address_len = 0;
2193         char *cert_file_path = NULL;
2194         char *email_address = NULL;
2195
2196         cert_file_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2197         if (cert_file_len > 0) {
2198                 cert_file_path = em_malloc(cert_file_len + 1);
2199                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, cert_file_len, cert_file_path);
2200         }
2201
2202         email_address_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 1);
2203         if (email_address_len > 0) {
2204                 email_address = em_malloc(email_address_len + 1);
2205                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, email_address_len, email_address);
2206         }
2207
2208         if (!emcore_add_public_certificate(cert_file_path, email_address, &err)) {
2209                 EM_DEBUG_EXCEPTION("em_core_smime_add_certificate failed");
2210         }
2211
2212         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2213                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2214
2215         if (EMAIL_ERROR_NONE == err) {
2216                 EM_DEBUG_LOG("email_mail_add_attachment -Success");
2217         }
2218
2219         if (!emipc_execute_stub_api(a_hAPI))
2220                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2221
2222         EM_SAFE_FREE(cert_file_path);          
2223         EM_SAFE_FREE(email_address); 
2224         EM_DEBUG_FUNC_END();    
2225 }
2226
2227 void stb_delete_certificate(HIPC_API a_hAPI)
2228 {
2229         int err = EMAIL_ERROR_NONE;
2230         int email_address_len = 0;
2231         char *email_address = NULL;
2232         char temp_email_address[130] = {0, };
2233
2234         email_address_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2235         if (email_address_len > 0) {
2236                 EM_DEBUG_LOG("email address string length [%d]", email_address_len);
2237                 email_address = em_malloc(email_address_len + 1);
2238                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, email_address_len, email_address);
2239                 EM_DEBUG_LOG("email address [%s]", email_address);
2240         }
2241
2242         SNPRINTF(temp_email_address, sizeof(temp_email_address), "<%s>", email_address);
2243         if (!emcore_delete_public_certificate(temp_email_address, &err)) {
2244                 EM_DEBUG_EXCEPTION("em_core_smime_add_certificate failed");
2245         }
2246
2247         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2248                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2249
2250         if (EMAIL_ERROR_NONE == err) {
2251                 EM_DEBUG_LOG("email_mail_add_attachment -Success");
2252         }
2253
2254         if (!emipc_execute_stub_api(a_hAPI))
2255                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2256
2257         
2258         EM_SAFE_FREE(email_address);
2259         EM_DEBUG_FUNC_END();    
2260 }
2261
2262 void stb_verify_signature(HIPC_API a_hAPI)
2263 {
2264         int err = EMAIL_ERROR_NONE;
2265         int verify = 0;
2266         int mail_id = 0;
2267         int count = 0;
2268         int attachment_tbl_count = 0;
2269         email_mail_data_t *mail_data = NULL;
2270         emstorage_attachment_tbl_t *attachment_tbl_list = NULL;
2271
2272         err = emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &mail_id);
2273         if (err != EMAIL_ERROR_NONE) {
2274                 EM_DEBUG_EXCEPTION("emipc_get_parameter failed");
2275                 goto FINISH_OFF;
2276         }
2277
2278         if (!emcore_get_mail_data(mail_id, &mail_data)) {
2279                 EM_DEBUG_EXCEPTION("emcore_get_mail_data failed");
2280                 goto FINISH_OFF;
2281         }
2282
2283         if ((err = emstorage_get_attachment_list(mail_id, true, &attachment_tbl_list, &attachment_tbl_count)) != EMAIL_ERROR_NONE) {
2284                 EM_DEBUG_EXCEPTION("emstorage_get_attachment_list failed : [%d]", err);
2285                 goto FINISH_OFF;
2286         }
2287
2288         if (attachment_tbl_count <= 0) {
2289                 EM_DEBUG_EXCEPTION("Invalid signed mail");
2290                 err = EMAIL_ERROR_INVALID_MAIL;
2291                 goto FINISH_OFF;
2292         }
2293
2294         for (count = 0; count < attachment_tbl_count; count++) {
2295                 if (strcasestr(attachment_tbl_list[count].attachment_name, "p7s") == NULL)
2296                         continue;
2297
2298                 break;  
2299         }
2300
2301         if (!emcore_verify_signature(attachment_tbl_list[count].attachment_path, mail_data->file_path_mime_entity, &verify, &err)) {
2302                 EM_DEBUG_EXCEPTION("emcore_verify_signature failed");
2303                 goto FINISH_OFF;
2304         }
2305
2306 FINISH_OFF:
2307
2308         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &verify, sizeof(int)))
2309                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2310
2311         if (verify) {
2312                 EM_DEBUG_LOG("Verify S/MIME signed mail-Success");
2313         }
2314
2315         if (!emipc_execute_stub_api(a_hAPI))
2316                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2317
2318
2319         if (attachment_tbl_list)
2320                 emstorage_free_attachment(&attachment_tbl_list, attachment_tbl_count, NULL);
2321
2322         if (mail_data) {
2323                 emcore_free_mail_data(mail_data);
2324                 EM_SAFE_FREE(mail_data);
2325         }
2326
2327         EM_DEBUG_FUNC_END();
2328 }
2329
2330 void stb_verify_certificate(HIPC_API a_hAPI)
2331 {
2332         int err = EMAIL_ERROR_NONE;
2333         int verify = 0;
2334         int cert_file_len = 0;
2335         char *cert_file_path = 0;
2336
2337         cert_file_len = emipc_get_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2338         if (cert_file_len > 0) {
2339                 cert_file_path = em_malloc(cert_file_len + 1);
2340                 emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, cert_file_len, cert_file_path);
2341         }
2342
2343         if (!emcore_verify_certificate(cert_file_path, &verify, &err)) {
2344                 EM_DEBUG_EXCEPTION("em_core_smime_add_certificate failed");
2345         }
2346
2347         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &verify, sizeof(int)))
2348                 EM_DEBUG_EXCEPTION("emipc_add_parameter local_result failed ");
2349
2350         if (verify) {
2351                 EM_DEBUG_LOG("Verify S/MIME signed mail-Success");
2352         }
2353
2354         if (!emipc_execute_stub_api(a_hAPI))
2355                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2356
2357         EM_SAFE_FREE(cert_file_path);           
2358         EM_DEBUG_FUNC_END();    
2359 }
2360
2361 void stb_ping_service(HIPC_API a_hAPI)
2362 {
2363         EM_DEBUG_FUNC_BEGIN();
2364         int err = EMAIL_ERROR_NONE;
2365         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2366                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2367
2368         if (!emipc_execute_stub_api(a_hAPI))
2369                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");  
2370         EM_DEBUG_FUNC_END();
2371 }
2372
2373 void stb_update_notification_bar_for_unread_mail(HIPC_API a_hAPI)
2374 {
2375         EM_DEBUG_FUNC_BEGIN();
2376         int err = EMAIL_ERROR_NONE, account_id;
2377
2378         /* account_id */
2379         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &account_id);
2380         EM_DEBUG_LOG("account_id [%d]", account_id);
2381
2382         if(!emcore_finalize_sync(account_id, &err)) {
2383                 EM_DEBUG_EXCEPTION("emcore_finalize_sync failed [%d]", err);
2384         }
2385
2386         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2387                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2388
2389         if (!emipc_execute_stub_api(a_hAPI))
2390                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2391         EM_DEBUG_FUNC_END();
2392 }
2393
2394 void stb_show_user_message(HIPC_API a_hAPI)
2395 {
2396         EM_DEBUG_FUNC_BEGIN();
2397         int err = EMAIL_ERROR_NONE;
2398         int param_id = 0;
2399         int param_error = 0;
2400         email_action_t param_action = 0;
2401
2402         /* param_id */
2403         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 0, sizeof(int), &param_id);
2404         EM_DEBUG_LOG("param_id [%d]", param_id);
2405
2406         /* param_action */
2407         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 1, sizeof(int), &param_action);
2408         EM_DEBUG_LOG("param_action [%d]", param_action);
2409
2410         /* param_error */
2411         emipc_get_parameter(a_hAPI, ePARAMETER_IN, 2, sizeof(int), &param_error);
2412         EM_DEBUG_LOG("param_error [%d]", param_error);
2413
2414         if( (err = emcore_show_user_message(param_id, param_action, param_error)) != EMAIL_ERROR_NONE) {
2415                 EM_DEBUG_EXCEPTION("emcore_show_user_message failed [%d]", err);
2416         }
2417
2418         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2419                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2420
2421         if (!emipc_execute_stub_api(a_hAPI))
2422                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2423
2424         EM_DEBUG_FUNC_END("err [%d]", err);
2425 }
2426
2427 void stb_write_mime_file(HIPC_API a_hAPI)
2428 {
2429         EM_DEBUG_FUNC_BEGIN();
2430         int i = 0;
2431         int exist_file_path = 0;
2432         int buffer_size = 0;
2433         int local_result = 0;
2434         int result_attachment_data_count = 0;
2435         int param_index = 0;
2436         int err = EMAIL_ERROR_NONE;
2437         int ret = EMAIL_ERROR_NONE;
2438         char *file_path = NULL;
2439         char *output_file_path = NULL;
2440         email_mail_data_t result_mail_data = {0};
2441         email_attachment_data_t *result_attachment_data = NULL;
2442         emstorage_mail_tbl_t *input_mail_tbl_data = NULL;
2443         emstorage_attachment_tbl_t *input_attachment_tbl_data = NULL;
2444         
2445         /* email_mail_data_t */;
2446         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
2447         EM_DEBUG_LOG("email_mail_data_t buffer_size[%d]", buffer_size);
2448
2449         /* mail_data */
2450         if(buffer_size > 0)      {
2451                 char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
2452                 em_convert_byte_stream_to_mail_data(stream, buffer_size, &result_mail_data);
2453         }
2454
2455         /* attachment */
2456         if (result_mail_data.attachment_count > 0) {
2457                 buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
2458                 EM_DEBUG_LOG("email_attachment_data_t buffer_size[%d]", buffer_size);
2459
2460                 if(buffer_size > 0) {
2461                         char *stream = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index);
2462                         em_convert_byte_stream_to_attachment_data(stream, buffer_size, &result_attachment_data, &result_attachment_data_count);
2463
2464                         EM_DEBUG_LOG("result_attachment_data_count[%d]", result_attachment_data_count);
2465
2466                         if(result_attachment_data_count && !result_attachment_data) {
2467                                 EM_DEBUG_EXCEPTION("em_convert_byte_stream_to_attachment_data failed");
2468                                 err = EMAIL_ERROR_ON_PARSING;
2469                                 goto FINISH_OFF;
2470                         }
2471                 }
2472
2473                 param_index++;
2474         }
2475
2476         
2477         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
2478         if (buffer_size > 0) {
2479                 output_file_path = (char *)emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index);
2480                 EM_DEBUG_LOG("output_file_path : [%s]", output_file_path);
2481         }
2482
2483         /* Convert mail_data to mail_tbl */
2484         if (!em_convert_mail_data_to_mail_tbl(&result_mail_data, 1, &input_mail_tbl_data, &err)) {
2485                 EM_DEBUG_EXCEPTION("em_convert_mail_data_to_mail_tbl failed");
2486                 goto FINISH_OFF;
2487         }
2488
2489         /* Convert attachment_data to attachment_tbl */
2490         if (result_attachment_data_count > 0) {
2491                 input_attachment_tbl_data = (emstorage_attachment_tbl_t *)em_malloc(sizeof(emstorage_attachment_tbl_t) * result_attachment_data_count);
2492                 if (input_attachment_tbl_data == NULL) {
2493                         EM_DEBUG_EXCEPTION("em_malloc failed");
2494                         err = EMAIL_ERROR_OUT_OF_MEMORY;
2495                         goto FINISH_OFF;
2496                 }
2497         }
2498
2499         for (i = 0; i < result_attachment_data_count ; i++) {
2500                 input_attachment_tbl_data[i].attachment_id = result_attachment_data[i].attachment_id;
2501                 input_attachment_tbl_data[i].attachment_name = EM_SAFE_STRDUP(result_attachment_data[i].attachment_name);
2502                 input_attachment_tbl_data[i].attachment_path = EM_SAFE_STRDUP(result_attachment_data[i].attachment_path);
2503                 input_attachment_tbl_data[i].attachment_size = result_attachment_data[i].attachment_size;
2504                 input_attachment_tbl_data[i].mail_id = result_attachment_data[i].mail_id;
2505                 input_attachment_tbl_data[i].account_id = result_attachment_data[i].account_id;
2506                 input_attachment_tbl_data[i].mailbox_id = result_attachment_data[i].mailbox_id;
2507                 input_attachment_tbl_data[i].attachment_save_status = result_attachment_data[i].save_status;
2508                 input_attachment_tbl_data[i].attachment_drm_type = result_attachment_data[i].drm_status;
2509                 input_attachment_tbl_data[i].attachment_inline_content_status = result_attachment_data[i].inline_content_status;
2510                 input_attachment_tbl_data[i].attachment_mime_type = EM_SAFE_STRDUP(result_attachment_data[i].attachment_mime_type);
2511         }
2512
2513         if ((err = em_verify_email_address_of_mail_tbl(input_mail_tbl_data, false)) != EMAIL_ERROR_NONE) {
2514                 EM_DEBUG_EXCEPTION("em_verify_email_address_of_mail_tbl failed : [%d]", err);
2515                 goto FINISH_OFF;
2516         }
2517
2518         if (!emcore_make_rfc822_file_from_mail(input_mail_tbl_data, input_attachment_tbl_data, result_attachment_data_count, NULL, &file_path, NULL, &err)) {
2519                 EM_DEBUG_EXCEPTION("emcore_make_rfc822_file_from_mail failed");
2520                 goto FINISH_OFF;
2521         }
2522
2523         if (output_file_path) {
2524                 if (!emstorage_move_file(file_path, output_file_path, false, &err)) {
2525                         EM_DEBUG_EXCEPTION("emstorage_move_file failed : [%d]", err);
2526                         goto FINISH_OFF;
2527                 }
2528         } else {
2529                 exist_file_path = 1;
2530                 output_file_path = EM_SAFE_STRDUP(file_path);
2531         }
2532
2533
2534         if (!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, (char *)output_file_path, EM_SAFE_STRLEN(output_file_path) + 1)) {
2535                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2536                 err = EMAIL_ERROR_NULL_VALUE;
2537                 goto FINISH_OFF;
2538         }
2539
2540         local_result = 1;
2541
2542 FINISH_OFF:
2543         if ( local_result == 0 ) {
2544                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2545                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2546         }
2547
2548         if (!emipc_execute_stub_api(a_hAPI))
2549                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2550
2551         emstorage_free_attachment(&input_attachment_tbl_data, result_attachment_data_count, &ret);
2552
2553         emstorage_free_mail(&input_mail_tbl_data, 1, &ret);
2554
2555         emcore_free_mail_data(&result_mail_data);
2556
2557         if(result_attachment_data)
2558                 emcore_free_attachment_data(&result_attachment_data, result_attachment_data_count, NULL);
2559
2560         EM_SAFE_FREE(file_path);
2561         if (exist_file_path)
2562                 EM_SAFE_FREE(output_file_path);
2563
2564         em_flush_memory();
2565         EM_DEBUG_FUNC_END();
2566 }
2567
2568
2569 void stb_validate_account_ex(HIPC_API a_hAPI)
2570 {
2571         EM_DEBUG_FUNC_BEGIN();
2572         int buffer_size = 0;
2573         int local_result = 0;
2574         int handle = 0;
2575         char* stream = NULL;
2576         email_account_t *account = NULL;
2577         email_account_t *ref_account = NULL;
2578         int err = EMAIL_ERROR_NONE;
2579
2580         /* get account info */
2581         buffer_size = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, 0);
2582         EM_DEBUG_LOG("size [%d]", buffer_size);
2583         if(buffer_size <= 0) {
2584                 err = EMAIL_ERROR_INVALID_PARAM;
2585                 goto FINISH_OFF;
2586         }
2587
2588         stream =(char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, 0);
2589         if(!stream) {
2590                 err = EMAIL_ERROR_INVALID_PARAM;
2591                 goto FINISH_OFF;
2592         }
2593
2594         account = em_malloc(sizeof(email_account_t));
2595
2596         if(account == NULL) {
2597                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_OUT_OF_MEMORY");
2598                 err = EMAIL_ERROR_OUT_OF_MEMORY;
2599                 goto FINISH_OFF;
2600         }
2601
2602         em_convert_byte_stream_to_account(stream, buffer_size, account);
2603
2604         if((err = emcore_add_account_to_unvalidated_account_list(account)) != EMAIL_ERROR_NONE) {
2605                 EM_DEBUG_EXCEPTION("emcore_add_account_to_unvalidated_account_list failed [%d]", err);
2606                 goto FINISH_OFF;
2607         }
2608
2609         ref_account = emcore_get_account_reference(account->account_id);
2610
2611         if (!ref_account) {
2612                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", err);
2613                 goto FINISH_OFF;
2614         }
2615
2616         if((err = emdaemon_validate_account_ex(ref_account, &handle)) != EMAIL_ERROR_NONE) {
2617                 EM_DEBUG_EXCEPTION("emdaemon_validate_account_ex fail [%d]", err);
2618                 goto FINISH_OFF;
2619         }
2620
2621         local_result = 1;
2622         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2623                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2624         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &handle, sizeof(int)))
2625                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed ");
2626         if (!emipc_execute_stub_api(a_hAPI))
2627                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2628
2629 FINISH_OFF:
2630         if ( local_result == 0 ) { /* there is an error */
2631                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &local_result, sizeof(int)))
2632                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : local_result ");
2633                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2634                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed : err");
2635                 if (!emipc_execute_stub_api(a_hAPI))
2636                         EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed  ");
2637         }
2638
2639 /*      note: account is freed in thread_func_branch_command, which is run by other thread */
2640 /*      emcore_free_account(account); */
2641
2642         EM_DEBUG_FUNC_END();
2643 }
2644 void stb_handle_task(int task_type, HIPC_API a_hAPI)
2645 {
2646         EM_DEBUG_FUNC_BEGIN();
2647         int param_index = 0;
2648         int is_async_task = (task_type > EMAIL_ASYNC_TASK_BOUNDARY_START)?1:0;
2649         int err = EMAIL_ERROR_NONE;
2650         int task_id = 0;
2651         int task_parameter_length = 0;
2652         char *task_parameter = NULL;
2653
2654         /* task_parameter_length */;
2655         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
2656         EM_DEBUG_LOG("task_parameter_length [%d]", task_parameter_length);
2657
2658         /* task_parameter */
2659         if(task_parameter_length > 0)    {
2660                 task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
2661         }
2662
2663         if(is_async_task) {
2664                 /* add async task */
2665                 if((err = emcore_add_task_to_task_table(task_type, EMAIL_TASK_PRIORITY_MID, task_parameter, task_parameter_length, &task_id)) != EMAIL_ERROR_NONE) {
2666                         EM_DEBUG_EXCEPTION("emcore_add_task_to_task_pool failed [%d]", err);
2667                         goto FINISH_OFF;
2668                 }
2669         }
2670         else {
2671                 /* do sync task */
2672                 email_task_t sync_task;
2673
2674                 memset(&sync_task, 0, sizeof(email_task_t));
2675                 sync_task.task_type             = task_type;
2676                 sync_task.task_priority         = EMAIL_TASK_PRIORITY_MID;
2677                 sync_task.task_parameter        = task_parameter;
2678                 sync_task.task_parameter_length = task_parameter_length;
2679
2680                 if((err = (int)emcore_default_sync_task_handler(&sync_task)) != EMAIL_ERROR_NONE) {
2681                         EM_DEBUG_EXCEPTION("emcore_default_sync_task_handler failed [%d]", err);
2682                         goto FINISH_OFF;
2683                 }
2684         }
2685
2686 FINISH_OFF:
2687         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2688                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2689
2690         if(is_async_task) {
2691                 if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
2692                         EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2693         }
2694
2695         if (!emipc_execute_stub_api(a_hAPI))
2696                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2697
2698         em_flush_memory();
2699         EM_DEBUG_FUNC_END("err [%d]", err);
2700 }
2701
2702 void* thread_func_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT(void *input_param)
2703 {
2704         EM_DEBUG_FUNC_BEGIN("input_param [%p]", input_param);
2705         int err = EMAIL_ERROR_NONE;
2706         int err_for_signal = EMAIL_ERROR_NONE;
2707         int i = 0;
2708         int task_id = THREAD_SELF();
2709         task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT *task_param = input_param;
2710
2711         /* Send start signal */
2712         if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT, task_id, EMAIL_TASK_STATUS_STARTED, EMAIL_ERROR_NONE, 0)) != EMAIL_ERROR_NONE)
2713                 EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2714
2715         for(i = 0; i < task_param->mail_id_count; i++) {
2716                 if((err = emcore_move_mail_to_another_account(task_param->mail_id_array[i], task_param->source_mailbox_id, task_param->target_mailbox_id, task_id)) != EMAIL_ERROR_NONE) {
2717                         EM_DEBUG_EXCEPTION("emcore_move_mail_to_another_account failed [%d]", err);
2718                         goto FINISH_OFF;
2719                 }
2720
2721                 /* Send progress signal */
2722                 if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT, task_id, EMAIL_TASK_STATUS_IN_PROGRESS, i, task_param->mail_id_count)) != EMAIL_ERROR_NONE)
2723                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2724         }
2725
2726 FINISH_OFF:
2727         /* Send finish signal */
2728         if(err == EMAIL_ERROR_NONE) {
2729                 if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT, task_id, EMAIL_TASK_STATUS_FINISHED, EMAIL_ERROR_NONE, 0)) != EMAIL_ERROR_NONE)
2730                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2731         }
2732         else {
2733                 if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT, task_id, EMAIL_TASK_STATUS_FAILED, err, 0)) != EMAIL_ERROR_NONE)
2734                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2735         }
2736
2737         /* Free task parameter */
2738         EM_SAFE_FREE(task_param->mail_id_array);
2739         EM_SAFE_FREE(task_param);
2740
2741         EM_DEBUG_FUNC_END("err [%d]", err);
2742         return SUCCESS;
2743 }
2744
2745 void stb_move_mails_to_mailbox_of_another_account(HIPC_API a_hAPI)
2746 {
2747         EM_DEBUG_FUNC_BEGIN();
2748         int param_index = 0;
2749         int err = EMAIL_ERROR_NONE;
2750         int task_parameter_length = 0;
2751         int thread_error;
2752         thread_t task_id;
2753         char *task_parameter = NULL;
2754         task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT *decoded_parameter = NULL;
2755
2756         /* task_parameter_length */;
2757         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
2758
2759         if(task_parameter_length <= 0) {
2760                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : task_parameter_length [%d]", task_parameter_length);
2761                 err = EMAIL_ERROR_INVALID_PARAM;
2762                 goto FINISH_OFF;
2763         }
2764
2765         /* task_parameter */
2766         task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
2767
2768         if((err = email_decode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT(task_parameter, task_parameter_length, (void**)&decoded_parameter)) != EMAIL_ERROR_NONE) {
2769                 EM_DEBUG_EXCEPTION("email_decode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT failed[%d]", err);
2770                 goto FINISH_OFF;
2771         }
2772
2773         THREAD_CREATE(task_id, thread_func_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT, (void*)decoded_parameter, thread_error);
2774
2775         if(thread_error != 0) {
2776                 EM_DEBUG_EXCEPTION("THREAD_CREATE failed [%d]", thread_error);
2777                 err = EMAIL_ERROR_SYSTEM_FAILURE;
2778                 goto FINISH_OFF;
2779         }
2780
2781         THREAD_DETACH(task_id);
2782
2783 FINISH_OFF:
2784         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2785                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2786
2787         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
2788                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2789
2790         if (!emipc_execute_stub_api(a_hAPI))
2791                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2792
2793         EM_DEBUG_FUNC_END("err [%d]", err);
2794 }
2795
2796 void* thread_func_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX(void *input_param)
2797 {
2798         EM_DEBUG_FUNC_BEGIN("input_param [%p]", input_param);
2799         int err = EMAIL_ERROR_NONE;
2800         int err_for_signal = EMAIL_ERROR_NONE;
2801         int recursive = 1;
2802         int task_id = THREAD_SELF();
2803         email_account_t* ref_account = NULL;
2804         task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX *task_param = input_param;
2805
2806         ref_account = emcore_get_account_reference(task_param->account_id);
2807
2808         if (!ref_account)  {
2809                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed [%d]", task_param->account_id);
2810                 err = EMAIL_ERROR_INVALID_ACCOUNT;
2811                 goto FINISH_OFF;
2812         }
2813
2814         /*  on_server is allowed to be only 0 when server_type is EMAIL_SERVER_TYPE_ACTIVE_SYNC */
2815         if ( ref_account->incoming_server_type == EMAIL_SERVER_TYPE_ACTIVE_SYNC ) {
2816                 recursive = 0;
2817         }
2818
2819         /* Send start signal */
2820         if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX, task_id, EMAIL_TASK_STATUS_STARTED, EMAIL_ERROR_NONE, 0)) != EMAIL_ERROR_NONE)
2821                 EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2822
2823         if((err = emcore_delete_mailbox_ex(task_param->account_id, task_param->mailbox_id_array, task_param->mailbox_id_count, task_param->on_server, recursive)) != EMAIL_ERROR_NONE) {
2824                 EM_DEBUG_EXCEPTION("emcore_delete_mailbox_ex failed[%d]", err);
2825                 goto FINISH_OFF;
2826         }
2827
2828 FINISH_OFF:
2829         /* Send finish signal */
2830         if(err == EMAIL_ERROR_NONE) {
2831                 if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX, task_id, EMAIL_TASK_STATUS_FINISHED, EMAIL_ERROR_NONE, 0)) != EMAIL_ERROR_NONE)
2832                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2833         }
2834         else {
2835                 if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX, task_id, EMAIL_TASK_STATUS_FAILED, err, 0)) != EMAIL_ERROR_NONE)
2836                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2837         }
2838
2839         /* Free task parameter */
2840         EM_SAFE_FREE(task_param->mailbox_id_array);
2841         EM_SAFE_FREE(task_param);
2842
2843         EM_DEBUG_FUNC_END("err [%d]", err);
2844         return SUCCESS;
2845 }
2846
2847 void stb_delete_mailbox_ex(HIPC_API a_hAPI)
2848 {
2849         EM_DEBUG_FUNC_BEGIN();
2850         int param_index = 0;
2851         int err = EMAIL_ERROR_NONE;
2852         int task_parameter_length = 0;
2853         int thread_error = 0;
2854         thread_t task_id;
2855         char *task_parameter = NULL;
2856         task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX *decoded_parameter = NULL;
2857
2858         /* task_parameter_length */;
2859         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
2860
2861         if(task_parameter_length <= 0) {
2862                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : task_parameter_length [%d]", task_parameter_length);
2863                 err = EMAIL_ERROR_INVALID_PARAM;
2864                 goto FINISH_OFF;
2865         }
2866
2867         /* task_parameter */
2868         task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
2869
2870         if((err = email_decode_task_parameter_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX(task_parameter, task_parameter_length, (void**)&decoded_parameter)) != EMAIL_ERROR_NONE) {
2871                 EM_DEBUG_EXCEPTION("email_decode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT failed[%d]", err);
2872                 goto FINISH_OFF;
2873         }
2874
2875         THREAD_CREATE(task_id, thread_func_EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX, (void*)decoded_parameter, thread_error);
2876
2877         if(thread_error != 0) {
2878                 EM_DEBUG_EXCEPTION("THREAD_CREATE failed [%d]", thread_error);
2879                 err = EMAIL_ERROR_SYSTEM_FAILURE;
2880                 goto FINISH_OFF;
2881         }
2882
2883         THREAD_DETACH(task_id);
2884
2885 FINISH_OFF:
2886         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2887                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2888
2889         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
2890                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2891
2892         if (!emipc_execute_stub_api(a_hAPI))
2893                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2894
2895         EM_DEBUG_FUNC_END("err [%d]", err);
2896 }
2897
2898 void* thread_func_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL(void *input_param)
2899 {
2900         EM_DEBUG_FUNC_BEGIN("input_param [%p]", input_param);
2901         int err = EMAIL_ERROR_NONE;
2902         int err_for_signal = EMAIL_ERROR_NONE;
2903         int task_id = THREAD_SELF();
2904         task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL *task_param = input_param;
2905
2906         /* Send start signal */
2907         if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL, task_id, EMAIL_TASK_STATUS_STARTED, EMAIL_ERROR_NONE, 0)) != EMAIL_ERROR_NONE)
2908                 EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2909
2910         if((err = emcore_send_mail_with_downloading_attachment_of_original_mail(task_param->mail_id)) != EMAIL_ERROR_NONE) {
2911                 EM_DEBUG_EXCEPTION("emcore_send_mail_with_downloading_attachment_of_original_mail failed [%d]", err);
2912                 goto FINISH_OFF;
2913         }
2914
2915 FINISH_OFF:
2916         /* Send finish signal */
2917         if(err == EMAIL_ERROR_NONE) {
2918                 if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL, task_id, EMAIL_TASK_STATUS_FINISHED, EMAIL_ERROR_NONE, 0)) != EMAIL_ERROR_NONE)
2919                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2920         }
2921         else {
2922                 if((err_for_signal = emcore_send_task_status_signal(EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL, task_id, EMAIL_TASK_STATUS_FAILED, err, 0)) != EMAIL_ERROR_NONE)
2923                         EM_DEBUG_LOG("emcore_send_task_status_signal failed [%d]", err_for_signal);
2924         }
2925
2926         /* Free task parameter */
2927         EM_SAFE_FREE(task_param);
2928
2929         EM_DEBUG_FUNC_END("err [%d]", err);
2930         return SUCCESS;
2931 }
2932
2933 void stb_send_mail_with_downloading_attachment_of_original_mail(HIPC_API a_hAPI)
2934 {
2935         EM_DEBUG_FUNC_BEGIN();
2936         int param_index = 0;
2937         int err = EMAIL_ERROR_NONE;
2938         int task_parameter_length = 0;
2939         int thread_error = 0;
2940         thread_t task_id;
2941         char *task_parameter = NULL;
2942         task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL *decoded_parameter = NULL;
2943
2944         /* task_parameter_length */;
2945         task_parameter_length = emipc_get_nth_parameter_length(a_hAPI, ePARAMETER_IN, param_index);
2946
2947         if(task_parameter_length <= 0) {
2948                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM : task_parameter_length [%d]", task_parameter_length);
2949                 err = EMAIL_ERROR_INVALID_PARAM;
2950                 goto FINISH_OFF;
2951         }
2952
2953         /* task_parameter */
2954         task_parameter = (char*) emipc_get_nth_parameter_data(a_hAPI, ePARAMETER_IN, param_index++);
2955
2956         if((err = email_decode_task_parameter_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL(task_parameter, task_parameter_length, (void**)&decoded_parameter)) != EMAIL_ERROR_NONE) {
2957                 EM_DEBUG_EXCEPTION("email_decode_task_parameter_EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT failed[%d]", err);
2958                 goto FINISH_OFF;
2959         }
2960
2961         THREAD_CREATE(task_id, thread_func_EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL, (void*)decoded_parameter, thread_error);
2962
2963         if(thread_error != 0) {
2964                 EM_DEBUG_EXCEPTION("THREAD_CREATE failed [%d]", thread_error);
2965                 err = EMAIL_ERROR_SYSTEM_FAILURE;
2966                 goto FINISH_OFF;
2967         }
2968
2969         THREAD_DETACH(task_id);
2970
2971 FINISH_OFF:
2972         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &err, sizeof(int)))
2973                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2974
2975         if(!emipc_add_parameter(a_hAPI, ePARAMETER_OUT, &task_id, sizeof(int)))
2976                 EM_DEBUG_EXCEPTION("emipc_add_parameter failed");
2977
2978         if (!emipc_execute_stub_api(a_hAPI))
2979                 EM_DEBUG_EXCEPTION("emipc_execute_stub_api failed");
2980
2981         EM_DEBUG_FUNC_END("err [%d]", err);
2982 }
2983
2984 void stb_API_mapper(HIPC_API a_hAPI)
2985 {
2986         EM_DEBUG_FUNC_BEGIN();
2987         int nAPIID = emipc_get_api_id(a_hAPI);
2988
2989         switch(nAPIID) {
2990                 case _EMAIL_API_ADD_ACCOUNT:
2991                         stb_create_account(a_hAPI);
2992                         break;
2993
2994                 case _EMAIL_API_ADD_MAILBOX:
2995                         stb_create_mailbox(a_hAPI);
2996                         break;
2997
2998                 case _EMAIL_API_DELETE_MAILBOX:
2999                         stb_delete_mailbox(a_hAPI);
3000                         break;
3001
3002                 case _EMAIL_API_SET_MAILBOX_TYPE:
3003                         stb_set_mailbox_type(a_hAPI);
3004                         break;
3005
3006                 case _EMAIL_API_SET_LOCAL_MAILBOX:
3007                         stb_set_local_mailbox(a_hAPI);
3008                         break;
3009
3010                 case _EMAIL_API_SET_MAIL_SLOT_SIZE:
3011                         stb_set_mail_slot_size_of_mailbox(a_hAPI);
3012                         break;
3013
3014                 case _EMAIL_API_RENAME_MAILBOX:
3015                         stb_rename_mailbox(a_hAPI);
3016                         break;
3017
3018                 case _EMAIL_API_SEND_MAIL:
3019                         stb_send_mail(a_hAPI);
3020                         break;
3021
3022                 case _EMAIL_API_GET_MAILBOX_LIST:
3023                         stb_get_mailbox_list(a_hAPI);
3024                         break;
3025
3026                 case _EMAIL_API_SYNC_HEADER:
3027                         stb_sync_header(a_hAPI);
3028                         break;
3029
3030                 case _EMAIL_API_DOWNLOAD_BODY:
3031                         stb_download_body(a_hAPI);
3032                         break;
3033
3034                 case _EMAIL_API_CLEAR_DATA:
3035                         stb_clear_mail_data (a_hAPI);
3036                         break;
3037
3038                 case _EMAIL_API_DELETE_ALL_MAIL:
3039                         stb_delete_all_mails(a_hAPI);
3040                         break;
3041
3042                 case _EMAIL_API_DELETE_MAIL:
3043                         stb_delete_mail(a_hAPI);
3044                         break;
3045
3046                 case _EMAIL_API_ADD_RULE:
3047                         stb_add_rule(a_hAPI);
3048                         break;
3049
3050                 case _EMAIL_API_GET_RULE:
3051                         stb_get_rule(a_hAPI);
3052                         break;
3053
3054                 case _EMAIL_API_GET_RULE_LIST:
3055                         stb_get_rule_list(a_hAPI);
3056                         break;
3057
3058                 case _EMAIL_API_FIND_RULE:
3059                         stb_find_rule(a_hAPI);
3060                         break;
3061
3062                 case _EMAIL_API_UPDATE_RULE:
3063                         stb_update_rule(a_hAPI);
3064                         break;
3065
3066                 case _EMAIL_API_DELETE_RULE:
3067                         stb_delete_rule(a_hAPI);
3068                         break;
3069
3070                 case _EMAIL_API_APPLY_RULE:
3071                         stb_apply_rule(a_hAPI);
3072                         break;
3073
3074                 case _EMAIL_API_MOVE_MAIL:
3075                         stb_move_mail(a_hAPI);
3076                         break;
3077
3078                 case _EMAIL_API_MOVE_ALL_MAIL:
3079                         stb_move_all_mails(a_hAPI);
3080                         break;
3081
3082                 case _EMAIL_API_SET_FLAGS_FIELD:
3083                         stb_set_flags_field(a_hAPI);
3084                         break;
3085
3086                 case _EMAIL_API_ADD_MAIL:
3087                         stb_add_mail(a_hAPI);
3088                         break;
3089
3090                 case _EMAIL_API_UPDATE_MAIL:
3091                         stb_update_mail(a_hAPI);
3092                         break;
3093
3094                 case _EMAIL_API_MOVE_THREAD_TO_MAILBOX:
3095                         stb_move_thread_to_mailbox(a_hAPI);
3096                         break;
3097
3098                 case _EMAIL_API_DELETE_THREAD:
3099                         stb_delete_thread(a_hAPI);
3100                         break;
3101
3102                 case _EMAIL_API_MODIFY_SEEN_FLAG_OF_THREAD:
3103                         stb_modify_seen_flag_of_thread(a_hAPI);
3104                         break;
3105
3106                 case _EMAIL_API_EXPUNGE_MAILS_DELETED_FLAGGED:
3107                         stb_expunge_mails_deleted_flagged(a_hAPI);
3108                         break;
3109
3110                 case _EMAIL_API_DELETE_ACCOUNT:
3111                         stb_delete_account(a_hAPI);
3112                         break;
3113
3114                 case _EMAIL_API_UPDATE_ACCOUNT:
3115                         stb_update_account(a_hAPI);
3116                         break;
3117
3118                 case _EMAIL_API_ADD_ATTACHMENT:
3119                         stb_add_attachment(a_hAPI);
3120                         break;
3121
3122                 case _EMAIL_API_GET_IMAP_MAILBOX_LIST:
3123                         stb_get_imap_mailbox_list(a_hAPI);
3124                         break;
3125
3126                 case _EMAIL_API_GET_ATTACHMENT:
3127                         stb_get_attachment(a_hAPI);
3128                         break;
3129
3130                 case _EMAIL_API_DELETE_ATTACHMENT:
3131                         stb_delete_attachment(a_hAPI);
3132                         break;
3133
3134                 case _EMAIL_API_DOWNLOAD_ATTACHMENT:
3135                         stb_download_attachment(a_hAPI);
3136                         break;
3137
3138                 case _EMAIL_API_GET_ACCOUNT_LIST:
3139                         stb_get_account_list(a_hAPI);
3140                         break;
3141
3142                 case _EMAIL_API_SEND_SAVED:
3143                         stb_mail_send_saved(a_hAPI);
3144                         break;
3145
3146                 case _EMAIL_API_ADD_READ_RECEIPT:
3147                         stb_add_read_receipt(a_hAPI);
3148                         break;
3149
3150                 case _EMAIL_API_CANCEL_JOB:
3151                         stb_cancel_job(a_hAPI);
3152                         break;
3153
3154                 case _EMAIL_API_GET_PENDING_JOB:
3155                         stb_get_pending_job(a_hAPI);
3156                         break;
3157
3158                 case _EMAIL_API_NETWORK_GET_STATUS:
3159                         stb_get_event_queue_status(a_hAPI);
3160                         break;
3161
3162                 case _EMAIL_API_SEND_RETRY:
3163                         stb_retry_sending_mail(a_hAPI);
3164                         break;
3165
3166                 case _EMAIL_API_VALIDATE_ACCOUNT :
3167                         stb_validate_account(a_hAPI);
3168                         break;
3169
3170                 case _EMAIL_API_SEND_MAIL_CANCEL_JOB :
3171                         stb_cancel_send_mail_job(a_hAPI);
3172                         break;
3173
3174                 case _EMAIL_API_SEARCH_MAIL_ON_SERVER :
3175                         stb_search_mail_on_server(a_hAPI);
3176                         break;
3177
3178                 case _EMAIL_API_CLEAR_RESULT_OF_SEARCH_MAIL_ON_SERVER :
3179                         stb_clear_result_of_search_mail_on_server(a_hAPI);
3180                         break;
3181
3182                 case _EMAIL_API_ADD_ACCOUNT_WITH_VALIDATION :
3183                         stb_add_account_with_validation(a_hAPI);
3184                         break;
3185
3186                 case _EMAIL_API_BACKUP_ACCOUNTS:
3187                         stb_backup_account(a_hAPI);
3188                         break;
3189
3190                 case _EMAIL_API_RESTORE_ACCOUNTS:
3191                         stb_restore_account(a_hAPI);
3192                         break;
3193
3194                 case _EMAIL_API_GET_PASSWORD_LENGTH_OF_ACCOUNT:
3195                         stb_get_password_length(a_hAPI);
3196                         break;
3197
3198                 case _EMAIL_API_PRINT_RECEIVING_EVENT_QUEUE :
3199                         stb_print_receiving_event_queue_via_debug_msg(a_hAPI);
3200                         break;
3201
3202                 case _EMAIL_API_ADD_CERTIFICATE:
3203                         stb_add_certificate(a_hAPI);
3204                         break;
3205
3206                 case _EMAIL_API_DELETE_CERTIFICATE:
3207                         stb_delete_certificate(a_hAPI);
3208                         break;
3209
3210                 case _EMAIL_API_VERIFY_SIGNATURE:
3211                         stb_verify_signature(a_hAPI);
3212                         break;
3213
3214                 case _EMAIL_API_VERIFY_CERTIFICATE:
3215                         stb_verify_certificate(a_hAPI);
3216                         break;
3217
3218                 case _EMAIL_API_PING_SERVICE :
3219                         stb_ping_service(a_hAPI);
3220                         break;
3221
3222                 case _EMAIL_API_UPDATE_NOTIFICATION_BAR_FOR_UNREAD_MAIL :
3223                         stb_update_notification_bar_for_unread_mail(a_hAPI);
3224                         break;
3225
3226                 case _EMAIL_API_SHOW_USER_MESSAGE :
3227                         stb_show_user_message(a_hAPI);
3228                         break;
3229
3230                 case _EMAIL_API_WRITE_MIME_FILE :
3231                         stb_write_mime_file(a_hAPI);
3232                         break;
3233
3234                 case _EMAIL_API_VALIDATE_ACCOUNT_EX :
3235                         stb_validate_account_ex(a_hAPI);
3236                         break;
3237
3238                 case EMAIL_ASYNC_TASK_MOVE_MAILS_TO_MAILBOX_OF_ANOTHER_ACCOUNT :
3239                         stb_move_mails_to_mailbox_of_another_account(a_hAPI);
3240                         break;
3241
3242                 case EMAIL_ASYNC_TASK_DELETE_MAILBOX_EX :
3243                         stb_delete_mailbox_ex(a_hAPI);
3244                         break;
3245
3246                 case EMAIL_ASYNC_TASK_SEND_MAIL_WITH_DOWNLOADING_ATTACHMENT_OF_ORIGINAL_MAIL :
3247                         stb_send_mail_with_downloading_attachment_of_original_mail(a_hAPI);
3248                         break;
3249
3250                 default :
3251                         if(EMAIL_SYNC_TASK_BOUNDARY_START < nAPIID && nAPIID < EMAIL_ASYNC_TASK_BOUNDARY_END)
3252                                 stb_handle_task(nAPIID, a_hAPI);
3253                         break;
3254         }
3255         EM_DEBUG_FUNC_END();
3256 }
3257
3258 GMainLoop *g_mainloop = NULL;
3259
3260 static void callback_for_sigterm(int signum)
3261 {
3262         EM_DEBUG_FUNC_BEGIN("signum [%d]", signum);
3263
3264         if (g_mainloop)
3265                 g_main_loop_quit(g_mainloop);
3266
3267         EM_DEBUG_FUNC_END();
3268 }
3269
3270 INTERNAL_FUNC int main(int argc, char *argv[])
3271 {
3272         /* Do the email-service Initialization 
3273        1. Create all DB tables and load the email engine */
3274         EM_DEBUG_LOG("Email service begin");
3275         int err = 0, ret;
3276         GMainLoop *mainloop;
3277
3278         EM_DEBUG_LOG("ipcEmailStub_Initialize Start");
3279         
3280         ret = emipc_initialize_stub(stb_API_mapper);
3281
3282         if(ret == true)
3283                 EM_DEBUG_LOG("ipcEmailStub_Initialize Success");
3284         else
3285                 EM_DEBUG_EXCEPTION("ipcEmailStub_Initialize failed");
3286
3287         signal(SIGPIPE, SIG_IGN);              /* to ignore signal 13(SIGPIPE) */
3288         signal(SIGTERM, callback_for_sigterm); /* to handle signal 15(SIGTERM) - power off case*/
3289
3290         emdaemon_initialize(&err);
3291
3292         /* Start auto polling */
3293 #ifdef __FEATURE_AUTO_POLLING__
3294         emdaemon_start_auto_polling(&err);
3295 #endif
3296
3297         mainloop = g_main_loop_new(NULL, 0);
3298         g_mainloop = mainloop;
3299
3300         g_type_init();
3301
3302         g_main_loop_run(mainloop);
3303         
3304         /* Clean up resources */
3305         g_main_loop_unref(mainloop);
3306
3307         g_mainloop = NULL;
3308
3309         emipc_finalize_stub();
3310
3311         emdaemon_finalize(NULL);
3312
3313         EM_DEBUG_FUNC_END();
3314         return 0;
3315 }
3316