Fixed the bug for tizen 3.0
[platform/core/messaging/email-service.git] / email-daemon / email-daemon-emn.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 #include <stdio.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <time.h>
27 #include <stdarg.h>     /*      Needed for the definition of va_list */
28 #include <glib.h>
29 #include <vconf.h>
30
31 #include "email-daemon.h"
32 #include "email-daemon-emn.h"
33 #include "email-debug-log.h"
34 #include "email-utilities.h"
35 #include "email-internal-types.h"
36 #include "email-storage.h"
37
38 #ifdef __FEATURE_OMA_EMN__
39
40 #include "msg.h"
41 #include "msg_storage.h"
42 #include "msg_transport.h"
43
44 #include <wbxml/wbxml.h>
45 #include <wbxml/wbxml_handlers.h>
46 #include <wbxml/wbxml_parser.h>
47 #include <wbxml/wbxml_errors.h>
48
49 static void _cb_parser_start_document(void* ctx, WB_LONG charset, const WBXMLLangEntry* lang)
50 {
51         EM_DEBUG_FUNC_BEGIN();
52         EM_DEBUG_LOG("Root Element: %s", lang->publicID->xmlRootElt);
53         EM_DEBUG_LOG_SEC("Public ID: %s",        lang->publicID->xmlPublicID);
54         EM_DEBUG_LOG("DTD: %s",                  lang->publicID->xmlDTD);
55         EM_DEBUG_FUNC_END();
56 }
57
58 static void _cb_parser_end_document(void* ctx)
59 {
60         EM_DEBUG_FUNC_BEGIN();
61         EM_DEBUG_FUNC_END();
62 }
63
64 static void _cb_parser_start_element(void* ctx, WBXMLTag* element, WBXMLAttribute** atts, WB_BOOL empty)
65 {
66         EM_DEBUG_FUNC_BEGIN();
67         WB_UTINY* p, **pp = ctx;
68         WB_ULONG j = 0, len = 0;
69         EM_DEBUG_LOG("parse_clb_start_element");
70
71         if (strcasecmp((char *)wbxml_tag_get_xml_name(element), "emn") != 0) {
72                 EM_DEBUG_LOG("not email notification");
73                 return ;
74         }
75
76         if (atts == NULL) {
77                 EM_DEBUG_LOG("no emn attributes");
78                 return ;
79         }
80
81         len = EM_SAFE_STRLEN((char *)wbxml_tag_get_xml_name(element)) + 1;
82         while (atts[j] != NULL) {
83                 len += (EM_SAFE_STRLEN((char *)wbxml_attribute_get_xml_name(atts[j])) + EM_SAFE_STRLEN((char *)wbxml_attribute_get_xml_value(atts[j])) + 7);
84                 j++;
85         }
86         len += 3;
87
88         if (!(p = malloc(sizeof(WB_UTINY) * len + 1))) {
89                 return ;
90         }
91
92         EM_DEBUG_LOG_SEC("<%s", (char *)wbxml_tag_get_xml_name(element));
93         sprintf((char *)p, "<%s", (char *)wbxml_tag_get_xml_name(element));
94
95         j = 0;
96         while (atts[j] != NULL) {
97                 EM_DEBUG_LOG_SEC(" %s=\"%s\"", (char *)wbxml_attribute_get_xml_name(atts[j]), (char *)wbxml_attribute_get_xml_value(atts[j]));
98                 strcat((char *)p, " ");
99                 strcat((char *)p, (char *)wbxml_attribute_get_xml_name(atts[j]));
100                 strcat((char *)p, "=");
101                 strcat((char *)p, "\"");
102                 strcat((char *)p, (char *)wbxml_attribute_get_xml_value(atts[j]));
103                 strcat((char *)p, "\"");
104                 j++;
105         }
106
107         if (empty) {
108                 EM_DEBUG_LOG_SEC("/>");
109                 strcat((char *)p, "/>");
110         }
111         else {
112                 EM_DEBUG_LOG_SEC(">");
113                 strcat((char *)p, ">");
114         }
115
116         *pp = p;
117         EM_DEBUG_FUNC_END();
118 }
119
120 static void _cb_parser_end_element(void* ctx, WBXMLTag* element, WB_BOOL empty)
121 {
122         EM_DEBUG_FUNC_BEGIN();
123         EM_DEBUG_FUNC_END();
124 }
125
126 static void _cb_parser_characters(void* ctx, WB_UTINY* ch, WB_ULONG start, WB_ULONG length)
127 {
128         EM_DEBUG_FUNC_BEGIN();
129         EM_DEBUG_FUNC_END();
130 }
131
132 static int _get_addr_from_element(unsigned char* elm,int* type, unsigned char** incoming_server_user_name, unsigned char** host_addr, unsigned char** mbox_name, unsigned char** auth_type)
133 {
134         EM_DEBUG_FUNC_BEGIN();
135
136         int err = EMAIL_ERROR_NONE;
137         unsigned char *p = NULL;
138         unsigned char *s = NULL;
139         unsigned char *user = NULL;
140         unsigned char *host = NULL;
141         unsigned char *mailbox = NULL;
142         unsigned char *auth = NULL;
143
144         if (!(p = (unsigned char*)strstr((char *)elm, "mailbox"))) {
145                 EM_DEBUG_EXCEPTION("invalid notification data");
146                 err = EMAIL_ERROR_XML_PARSER_FAILURE;
147                 goto FINISH_OFF;
148         }
149
150         p += 9;
151         s = (unsigned char*)strchr((char *)p, '\"');
152
153         if (s)
154                 *s = '\0';
155
156         switch (p[0]) {
157         case 'm':/*      mailat (RFC2234) */
158                 *type = 1;
159                 p += 7;
160                 if (!(s = (unsigned char*)strchr((char *)p, '@'))) return 0;
161                 *s = '\0';
162                 user = (unsigned char*)EM_SAFE_STRDUP((char *)p);
163                 s++;
164                 host = (unsigned char*)EM_SAFE_STRDUP((char *)s);
165                 mailbox = NULL;
166                 auth = NULL;
167                 break;
168
169         case 'p':/*      pop3 (RFC2384) */
170                 *type = 2;
171                 p += 6;
172                 if ((s = (unsigned char*)strchr((char *)p, ';'))) {
173                         *s = '\0';
174                         if (EM_SAFE_STRLEN((char *)p)) user = (unsigned char*)EM_SAFE_STRDUP((char *)p);
175                         p = s + 1;
176                 }
177                 if ((s = (unsigned char*)strchr((char *)p, '@'))) {
178                         *s = '\0';
179                         if (user || *(p - 3) == '/') {
180                                 if (strncmp((char *)p, "AUTH=", 5) == 0) auth = (unsigned char*)EM_SAFE_STRDUP((char *)p + 5);
181                         }
182                         else
183                                 user = (unsigned char*)EM_SAFE_STRDUP((char *)p);
184                         p = s + 1;
185                 }
186                 if ((s = (unsigned char*)strchr((char *)p, ':'))) {
187                         *s = '\0';
188                         s++;
189                         EM_DEBUG_LOG("PORT:%s", s);
190                 }
191                 host = (unsigned char*)EM_SAFE_STRDUP((char *)p);
192                 mailbox = NULL;
193                 break;
194
195         case 'i':/*      imap (RFC2192) */
196                 *type = 3;
197                 p += 7;
198                 if ((s = (unsigned char*)strchr((char *)p, ';'))) {
199                         *s = '\0';
200                         if (EM_SAFE_STRLEN((char *)p)) user = (unsigned char*)EM_SAFE_STRDUP((char *)p);
201                         p = s + 1;
202                 }
203                 if ((s = (unsigned char*)strchr((char *)p, '@'))) {
204                         *s = '\0';
205                         if (user || *(p - 3) == '/') {
206                                 if (strncmp((char *)p, "AUTH=", 5) == 0) auth = (unsigned char*)EM_SAFE_STRDUP((char *)p + 5);
207                         }
208                         else
209                                 user = (unsigned char*)EM_SAFE_STRDUP((char *)p);
210                         p = s + 1;
211                 }
212
213                 if ((s = (unsigned char *)strchr((char *)p, '/'))) { /*prevent 47327*/
214                         *s = '\0';
215                         host = (unsigned char *)EM_SAFE_STRDUP((char *)p);
216                         p = s + 1;
217                 }
218
219                 if ((s = (unsigned char*)strchr((char *)p, '?')))
220                         *s = '\0';
221                 else if ((s = (unsigned char*)strchr((char *)p, ';')))
222                         *s = '\0';
223                 else
224                         s = p + EM_SAFE_STRLEN((char *)p);
225                 if (*(s - 1) == '/')
226                         *(s - 1) = '\0';
227
228                 if (EM_SAFE_STRLEN((char *)p))
229                         mailbox =(unsigned char*) EM_SAFE_STRDUP((char *)p);
230                 break;
231
232         case 'h': /*  not supported */
233                 EM_DEBUG_LOG("Http URI is not yet supported");
234                 err = EMAIL_ERROR_XML_PARSER_FAILURE;
235                 break;
236
237         default:
238                 err = EMAIL_ERROR_XML_PARSER_FAILURE;
239                 break;
240         }
241
242         if (err == EMAIL_ERROR_NONE) {
243                 *incoming_server_user_name = user;
244                 *host_addr = host;
245                 *mbox_name = mailbox;
246                 *auth_type = auth;
247         }
248
249  FINISH_OFF:
250
251         EM_DEBUG_FUNC_END("err [%d]", err);
252         return err;
253 }
254
255 static int _get_time_from_element(unsigned char* elm, unsigned char** time_stamp)
256 {
257         EM_DEBUG_FUNC_BEGIN("elm[%p] time_stamp[%p]", elm, time_stamp);
258         int err = EMAIL_ERROR_NONE;
259         unsigned char* p, *s, *stamp = NULL;
260
261         if (!(p = (unsigned char*)strstr((char *)elm, "timestamp"))) {
262                 EM_DEBUG_EXCEPTION("invalid notification data");
263                 err = EMAIL_ERROR_XML_PARSER_FAILURE;
264                 goto FINISH_OFF;
265         }
266
267         p += 11;
268         s = (unsigned char*)strchr((char *)p, '\"');
269
270         if (s)
271                 *s = '\0';
272
273         stamp = malloc(15);
274         if (!stamp) {
275                 EM_DEBUG_EXCEPTION("malloc failed");
276                 err = EMAIL_ERROR_OUT_OF_MEMORY;
277                 goto FINISH_OFF;
278         }
279         memcpy(stamp, p, 4);
280         memcpy(stamp + 4, p + 5, 2);
281         memcpy(stamp + 6, p + 8, 2);
282         memcpy(stamp + 8, p + 11, 2);
283         memcpy(stamp + 10, p + 14, 2);
284         memcpy(stamp + 12, p + 17, 2);
285         stamp[14] = '\0';
286
287         *time_stamp = stamp;
288
289  FINISH_OFF:
290
291         EM_DEBUG_FUNC_END("err [%d]", err);
292         return 1;
293 }
294
295 /*      <emn mailbox="mailat:user@wapforum.org" timestamp="2002-04-16T06:40:00Z"/> */
296 static int _get_data_from_element(unsigned char* elm, int* type, unsigned char** incoming_server_user_name, unsigned char** host_addr, unsigned char** mbox_name, unsigned char** auth_type, unsigned char** time_stamp)
297 {
298         EM_DEBUG_FUNC_BEGIN();
299         int err = EMAIL_ERROR_NONE;
300
301         if ((err = _get_time_from_element(elm, time_stamp)) != EMAIL_ERROR_NONE) {
302                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_XML_PARSER_FAILURE");
303                 goto FINISH_OFF;
304         }
305
306         /*      must call get_addr_from_element after calling _get_time_from_element */
307         if (!_get_addr_from_element(elm, type, incoming_server_user_name, host_addr, mbox_name, auth_type)) {
308                 EM_SAFE_FREE(*time_stamp);
309                 err = EMAIL_ERROR_XML_PARSER_FAILURE;
310                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_XML_PARSER_FAILURE");
311                 goto FINISH_OFF;
312         }
313
314  FINISH_OFF:
315         EM_DEBUG_FUNC_END("err[%d]", err);
316         return err;
317 }
318
319 /* description
320  *        get account from OMA EMN data
321  * arguments
322  *        wbxml         : wbxml data
323  *        account_id: id of account to be retrieved from EMN data
324  *        mailbox       : if mail-box information exists in EMN data, mailbox holds the name of mailbox.
325  *        err_code      : hold error code
326  * return
327  *        succeed : 1
328  *        fail : 0
329  */
330 static int _get_emn_account(unsigned char *input_wbxml, int input_wbxml_length, email_account_t *account, char **mailbox)
331 {
332         EM_DEBUG_FUNC_BEGIN("input_wbxml[%p] input_wbxml_length[%d] account[%p] mailbox[%p]", input_wbxml, input_wbxml_length, account, mailbox);
333         WBXMLContentHandler parse_handler = {
334                 (WBXMLStartDocumentHandler) _cb_parser_start_document,
335                 (WBXMLEndDocumentHandler) _cb_parser_end_document,
336                 (WBXMLStartElementHandler) _cb_parser_start_element,
337                 (WBXMLEndElementHandler) _cb_parser_end_element,
338                 (WBXMLCharactersHandler) _cb_parser_characters,
339                 NULL
340         };
341
342         WBXMLParser*    wbxml_parser = NULL;
343         WB_UTINY*               wbxml = NULL;
344         WB_UTINY*               elm = NULL;
345         WB_ULONG                wbxml_len = 0;
346         WB_ULONG                err_idx = 0;
347         WBXMLError              ret = WBXML_OK;
348         email_account_t*        accounts = NULL;
349         unsigned char*  incoming_server_user_name = NULL;
350         unsigned char*  host_addr = NULL;
351         unsigned char*  mbox_name = NULL;
352         unsigned char*  auth_type = NULL;
353         unsigned char*  time_stamp = NULL;
354         unsigned char   email_address[MAX_EMAIL_ADDRESS_LENGTH] = { 0, };
355         int                             type = 0;
356         int                             i = 0;
357         int                             count = 0;
358         int                             retr = false;
359         int                             err = EMAIL_ERROR_NONE;
360
361         if (!input_wbxml || !account) {
362                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
363                 err = EMAIL_ERROR_INVALID_PARAM;
364                 goto FINISH_OFF;
365         }
366
367         EM_DEBUG_LOG("wbxml_b64 [%s]", input_wbxml);
368
369         /* wbxml = g_base64_decode((const char*)wbxml_b64, (unsigned int*)&wbxml_len); */
370
371         wbxml = input_wbxml;
372         wbxml_len = input_wbxml_length;
373
374         EM_DEBUG_LOG("wbxml [%s]", wbxml);
375         EM_DEBUG_LOG("wbxml_len [%d]", wbxml_len);
376
377         /*      create wbxml parser */
378         if (!(wbxml_parser = wbxml_parser_create())) {
379                 err = EMAIL_ERROR_OUT_OF_MEMORY;
380                 goto FINISH_OFF;
381         }
382
383         /*      initialize wbxml parser */
384         wbxml_parser_set_user_data(wbxml_parser, (void*)&elm);
385         wbxml_parser_set_content_handler(wbxml_parser, &parse_handler);
386
387         /*      parsing wbxml */
388         if ((ret = wbxml_parser_parse(wbxml_parser, wbxml, wbxml_len)) != WBXML_OK) {
389                 err_idx = wbxml_parser_get_current_byte_index(wbxml_parser);
390                 EM_DEBUG_EXCEPTION("Parsing failed at %u - Token %x - %s", err_idx, wbxml[err_idx], wbxml_errors_string(ret));
391                 err = EMAIL_ERROR_XML_PARSER_FAILURE;
392                 goto FINISH_OFF;
393         }
394         else {
395                 EM_DEBUG_LOG("Parsing OK !");
396         }
397
398         /*      destroy parser */
399         wbxml_parser_destroy(wbxml_parser);
400         wbxml_parser = NULL;
401
402         /*      free buffer */
403         /*
404           wbxml_free(wbxml);
405           wbxml = NULL;
406         */
407
408         if (!elm) {
409                 EM_DEBUG_EXCEPTION("invalid elements");
410                 err = EMAIL_ERROR_XML_PARSER_FAILURE;
411                 goto FINISH_OFF;
412         }
413
414         EM_DEBUG_LOG("elements = [%s]", elm);
415         _get_data_from_element(elm, &type, &incoming_server_user_name, &host_addr, &mbox_name, &auth_type, &time_stamp);
416
417         EM_SAFE_FREE(elm);
418
419         EM_DEBUG_LOG("user_type = [%d]", type);
420         EM_DEBUG_LOG_SEC("incoming_server_user_name = [%s]", (char *)incoming_server_user_name ? (char*)incoming_server_user_name : "NIL");
421         EM_DEBUG_LOG("host_addr = [%s]", (char *)host_addr ? (char*)host_addr : "NIL");
422         EM_DEBUG_LOG_SEC("mbox_name = [%s]", (char *)mbox_name ? (char*)mbox_name : "NIL");
423         EM_DEBUG_LOG("auth_type = [%s]", (char *)auth_type ? (char*)auth_type : "NIL");
424         EM_DEBUG_LOG("time_stamp= [%s]", (char *)time_stamp? (char*)time_stamp: "NIL");
425
426         if(incoming_server_user_name && host_addr)
427                 SNPRINTF((char*)email_address, MAX_EMAIL_ADDRESS_LENGTH, "%s@%s", incoming_server_user_name, host_addr);
428
429         if (!emdaemon_get_account_list(NULL, &accounts, &count, &err)) {
430                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_DB_FAILURE");
431                 err = EMAIL_ERROR_DB_FAILURE;
432                 goto FINISH_OFF;
433         }
434
435         for (i = 0; i < count; i++) {
436                 char* temp_account_name = NULL;
437                 char *s = NULL;
438                 /*      EM_DEBUG_LOG(">>>> Account Information UserName [ %s ] Email Addr [ %s], Account ID [ %d] >>> ",accounts[i].incoming_server_user_name,accounts[i].user_email_address, accounts[i].account_id); */
439                 temp_account_name =(char*) EM_SAFE_STRDUP((char *)accounts[i].incoming_server_user_name);
440
441                 if ((s = (char*)strchr((char *)temp_account_name, '@')))  {
442                         *s = '\0';
443                         EM_SAFE_FREE(accounts[i].incoming_server_user_name);
444                         accounts[i].incoming_server_user_name  = (char*)EM_SAFE_STRDUP((char *)temp_account_name);
445                 }
446                 EM_SAFE_FREE(temp_account_name);
447                 if (incoming_server_user_name && host_addr)     {
448                         if (strstr(accounts[i].user_email_address, (char*)email_address)) {
449                                 EM_DEBUG_LOG(">>>> Account Match >>> ");
450                                 if ((type == 1) ||
451                                         (type == 2 && accounts[i].incoming_server_type == EMAIL_SERVER_TYPE_POP3) ||
452                                         (type == 3 && accounts[i].incoming_server_type == EMAIL_SERVER_TYPE_IMAP4)) {
453                                         /* accounts[i].flag2 = type; */
454                                         EM_DEBUG_LOG_SEC("found target account id[%d] name[%s]", accounts[i].account_id, accounts[i].incoming_server_user_name);
455                                         break;
456                                 }
457                         }
458                 }
459         }
460
461         if (i >= count) {
462                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_ACCOUNT_NOT_FOUND");
463                 err = EMAIL_ERROR_ACCOUNT_NOT_FOUND;
464                 goto FINISH_OFF;
465         }
466
467         if (account) {
468                 account->account_id = accounts[i].account_id;
469                 /* account->flag2 = accounts[i].flag2; */
470         }
471
472         if (mailbox) {
473                 *mailbox = mbox_name ? (char *)mbox_name : NULL;
474                 mbox_name = NULL;
475         }
476         emdaemon_free_account(&accounts, count, NULL);
477         accounts = NULL;
478
479         retr = true;
480
481  FINISH_OFF:
482         /*
483           if (wbxml)
484           wbxml_free(wbxml);
485         */
486
487         if (wbxml_parser)
488                 wbxml_parser_destroy(wbxml_parser);
489
490         EM_SAFE_FREE(elm);
491
492         if (accounts)
493                 emdaemon_free_account(&accounts, count, NULL);
494
495         EM_SAFE_FREE(incoming_server_user_name);
496         EM_SAFE_FREE(mbox_name);
497         EM_SAFE_FREE(auth_type);
498         EM_SAFE_FREE(time_stamp);
499
500         EM_DEBUG_FUNC_END("err [%d]", err);
501         return err;
502 }
503
504
505 static int emdaemon_handle_emn_notification(unsigned char* wbxml_b64, int input_body_lenth)
506 {
507         EM_DEBUG_FUNC_BEGIN("wbxml_b64[%p] input_body_lenth[%d]", wbxml_b64, input_body_lenth);
508
509         int err = EMAIL_ERROR_NONE;
510         int handle = 0;
511         char* mailbox_name = NULL;
512         email_account_t account = { 0 };
513         emstorage_mailbox_tbl_t *mailbox_tbl = NULL;
514
515         if (!wbxml_b64) {
516                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
517                 err = EMAIL_ERROR_INVALID_PARAM;
518                 goto FINISH_OFF;
519         }
520
521         if ((err = _get_emn_account(wbxml_b64, input_body_lenth, &account, &mailbox_name)) != EMAIL_ERROR_NONE) {
522                 EM_DEBUG_EXCEPTION("_get_emn_account failed [%d]", err);
523                 goto FINISH_OFF;
524         }
525
526         if (account.incoming_server_type == EMAIL_SERVER_TYPE_IMAP4 && mailbox_name)  {
527                 if (!emstorage_get_mailbox_by_name(multi_user_name, account.account_id, -1, mailbox_name, &mailbox_tbl, false, &err))  {
528                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_name failed [%d", err);
529                         goto FINISH_OFF;
530                 }
531         }
532         else {
533                 if (!emstorage_get_mailbox_by_mailbox_type(multi_user_name, account.account_id, EMAIL_MAILBOX_TYPE_INBOX, &mailbox_tbl, false, &err))  {
534                         EM_DEBUG_EXCEPTION("emstorage_get_mailbox_by_mailbox_type failed [%d", err);
535                         goto FINISH_OFF;
536                 }
537         }
538
539         if (!emdaemon_sync_header(multi_user_name, mailbox_tbl->account_id, mailbox_tbl->mailbox_id, &handle, &err))  {
540                 EM_DEBUG_EXCEPTION("emdaemon_sync_header failed [%d]", err);
541                 goto FINISH_OFF;
542         }
543
544
545  FINISH_OFF:
546
547         if (mailbox_tbl)
548                 emstorage_free_mailbox(&mailbox_tbl, 1, NULL);
549
550         EM_DEBUG_FUNC_END("err [%d]", err);
551         return err;
552 }
553
554 void oma_emn_push_cb(msg_handle_t input_handle, const char *input_push_header, const char *input_push_body, int input_push_body_lenth, void *input_user_param)
555 {
556         EM_DEBUG_FUNC_BEGIN("input_handle[%d] input_push_header[%p] input_push_body[%p] input_push_body_lenth[%d] input_user_param[%p]", input_handle, input_push_header, input_push_body, input_push_body_lenth, input_user_param);
557         int err = EMAIL_ERROR_NONE;
558
559         EM_DEBUG_LOG("input_push_header [%s]", input_push_header);
560         EM_DEBUG_LOG("input_push_body [%s]", input_push_body);
561         EM_DEBUG_LOG("input_push_body_lenth [%d]", input_push_body_lenth);
562
563         if((err = emdaemon_handle_emn_notification((unsigned char*)input_push_body, input_push_body_lenth)) != EMAIL_ERROR_NONE) {
564                 EM_DEBUG_EXCEPTION("emdaemon_handle_emn_notification failed [%d]", err);
565         }
566
567         EM_DEBUG_FUNC_END("err [%d]", err);
568 }
569
570 static int emdaemon_register_wap_push_callback(msg_handle_t *input_msg_handle, char *input_app_id)
571 {
572         EM_DEBUG_FUNC_BEGIN("input_msg_handle [%p] input_app_id [%p]", input_msg_handle, input_app_id);
573         int err = EMAIL_ERROR_NONE;
574         msg_error_t msg_err = MSG_SUCCESS;
575         msg_struct_t msg_struct = NULL;
576         char *content_type = "application/vnd.wap.emn+wbxml";
577         char *pkg_name = "org.tizen.email";
578         bool bLaunch = false;
579
580         if(input_msg_handle == NULL || input_app_id == NULL) {
581                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
582                 err = EMAIL_ERROR_INVALID_PARAM;
583                 goto FINISH_OFF;
584         }
585
586         msg_struct = msg_create_struct(MSG_STRUCT_PUSH_CONFIG_INFO);
587
588         if(msg_struct == NULL) {
589                 EM_DEBUG_EXCEPTION("msg_create_struct() failed [%d]", msg_err);
590                 err = EMAIL_ERROR_INPROPER_RESPONSE_FROM_MSG_SERVICE;
591                 goto FINISH_OFF;
592         }
593
594         msg_set_str_value(msg_struct, MSG_PUSH_CONFIG_CONTENT_TYPE_STR, content_type, MAX_WAPPUSH_CONTENT_TYPE_LEN);
595         msg_set_str_value(msg_struct, MSG_PUSH_CONFIG_APPLICATON_ID_STR, input_app_id, MAX_WAPPUSH_ID_LEN);
596         msg_set_str_value(msg_struct, MSG_PUSH_CONFIG_PACKAGE_NAME_STR, pkg_name, MSG_FILEPATH_LEN_MAX);
597         msg_set_bool_value(msg_struct, MSG_PUSH_CONFIG_LAUNCH_BOOL, bLaunch);
598
599         msg_add_push_event(*input_msg_handle, msg_struct);
600
601         if ((msg_err = msg_reg_push_message_callback(*input_msg_handle, &oma_emn_push_cb, input_app_id, NULL)) != MSG_SUCCESS) {
602                 EM_DEBUG_EXCEPTION_SEC("msg_reg_push_message_callback() for %s failed [%d]", msg_err, input_app_id);
603                 err = EMAIL_ERROR_INPROPER_RESPONSE_FROM_MSG_SERVICE;
604                 goto FINISH_OFF;
605         }
606
607  FINISH_OFF:
608
609         if(msg_struct)
610                 msg_release_struct(&msg_struct);
611
612         EM_DEBUG_FUNC_END("err [%d]", err);
613         return err;
614 }
615
616
617 int emdaemon_initialize_emn(void)
618 {
619         EM_DEBUG_FUNC_BEGIN();
620
621         msg_error_t msg_err = MSG_SUCCESS;
622         msg_handle_t msg_handle = NULL;
623         int err = EMAIL_ERROR_NONE;
624         char *oma_docomo_app_id = "x-oma-docomo:xmd.mail.ua";
625         char *oma_emn_app_id = "x-wap-application:emn.ua";
626
627         if ((msg_err = msg_open_msg_handle(&msg_handle)) != MSG_SUCCESS) {
628                 EM_DEBUG_EXCEPTION("msg_open_msg_handle() failed [%d]", msg_err);
629                 err = EMAIL_ERROR_INPROPER_RESPONSE_FROM_MSG_SERVICE;
630                 goto FINISH_OFF;
631         }
632
633         if ((err = emdaemon_register_wap_push_callback(&msg_handle, oma_docomo_app_id)) != EMAIL_ERROR_NONE) {
634                 EM_DEBUG_EXCEPTION("emdaemon_register_wap_push_callback() failed [%d]", err);
635                 goto FINISH_OFF;
636         }
637
638         if ((err = emdaemon_register_wap_push_callback(&msg_handle, oma_emn_app_id)) != EMAIL_ERROR_NONE) {
639                 EM_DEBUG_EXCEPTION("emdaemon_register_wap_push_callback() failed [%d]", err);
640                 goto FINISH_OFF;
641         }
642
643
644  FINISH_OFF:
645
646         EM_DEBUG_FUNC_END("err [%d]", err);
647         return err;
648 }
649
650 int emdaemon_finalize_emn(gboolean bExtDest)
651 {
652         EM_DEBUG_FUNC_BEGIN();
653         EM_DEBUG_FUNC_END();
654         return 0;
655 }
656
657 #endif /*  __FEATURE_OMA_EMN__ */
658
659 /* EOF */