4 * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
6 * Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
27 #include <stdarg.h> /* Needed for the definition of va_list */
32 /* #include "emf-emn-storage.h" */
35 /* --------------------------------------------------------------------------------*/
36 /* ----- WBXML Parsing (2007.06.09) --------------------------------------------------*/
37 /* --------------------------------------------------------------------------------*/
41 #include <wbxml_errors.h>
42 #include "emf-dbglog.h"
44 #include "emf-global.h"
49 emf_emn_noti_cb callback;
56 __emn_get_username(char *account_username);
58 _cb_parser_start_document(void* ctx, WB_LONG charset, const WBXMLLangEntry* lang)
60 EM_DEBUG_LOG("_cb_parser_start_document\n");
61 EM_DEBUG_LOG("Parsing Document:\n\tRoot Element: %s\n\tPublic ID: %s\n\tDTD: %s\n",
62 lang->publicID->xmlRootElt,
63 lang->publicID->xmlPublicID,
64 lang->publicID->xmlDTD);
68 _cb_parser_end_document(void* ctx)
70 EM_DEBUG_LOG("_cb_parser_end_document\n");
74 _cb_parser_start_element(void* ctx, WBXMLTag* element, WBXMLAttribute** atts, WB_BOOL empty)
76 WB_UTINY* p, **pp = ctx;
77 WB_ULONG j = 0, len = 0;
78 EM_DEBUG_LOG("parse_clb_start_element\n");
80 if (strcasecmp((char *)wbxml_tag_get_xml_name(element), "emn") != 0)
82 EM_DEBUG_LOG("not email notification\n");
88 EM_DEBUG_LOG("no emn attributes\n");
92 len = strlen((char *)wbxml_tag_get_xml_name(element)) + 1;
93 while (atts[j] != NULL)
95 len += (strlen((char *)wbxml_attribute_get_xml_name(atts[j])) + strlen((char *)wbxml_attribute_get_xml_value(atts[j])) + 7);
100 if (!(p = malloc(sizeof(WB_UTINY) * len + 1)))
105 EM_DEBUG_LOG("<%s", (char *)wbxml_tag_get_xml_name(element));
106 sprintf((char *)p, "<%s", (char *)wbxml_tag_get_xml_name(element));
109 while (atts[j] != NULL)
111 EM_DEBUG_LOG(" %s=\"%s\"", (char *)wbxml_attribute_get_xml_name(atts[j]), (char *)wbxml_attribute_get_xml_value(atts[j]));
112 strcat((char *)p, " ");
113 strcat((char *)p, (char *)wbxml_attribute_get_xml_name(atts[j]));
114 strcat((char *)p, "=");
115 strcat((char *)p, "\"");
116 strcat((char *)p, (char *)wbxml_attribute_get_xml_value(atts[j]));
117 strcat((char *)p, "\"");
123 EM_DEBUG_LOG("/>\n");
124 strcat((char *)p, "/>");
129 strcat((char *)p, ">");
136 _cb_parser_end_element(void* ctx, WBXMLTag* element, WB_BOOL empty)
138 EM_DEBUG_LOG("parse_clb_end_element\n");
142 _cb_parser_characters(void* ctx, WB_UTINY* ch, WB_ULONG start, WB_ULONG length)
144 EM_DEBUG_LOG("_cb_parser_characters\n");
147 static int _get_addr_from_element(unsigned char* elm,
149 unsigned char** user_name,
150 unsigned char** host_addr,
151 unsigned char** mbox_name,
152 unsigned char** auth_type)
156 unsigned char* user = NULL;
157 unsigned char* host = NULL;
158 unsigned char* mbox = NULL;
159 unsigned char* auth = NULL;
161 EM_DEBUG_FUNC_BEGIN();
163 if (!(p = (unsigned char*)strstr((char *)elm, "mailbox"))) /* add acetrack.20080331.K8.4043 */
165 EM_DEBUG_EXCEPTION("invalid notification data\n");
170 s = (unsigned char*)strchr((char *)p, '\"');
171 /* 2007-05-08 by acetrack */
177 case 'm':/* mailat (RFC2234) */
180 if (!(s = (unsigned char*)strchr((char *)p, '@'))) return 0;
182 user = (unsigned char*)EM_SAFE_STRDUP((char *)p);
184 host = (unsigned char*)EM_SAFE_STRDUP((char *)s);
189 case 'p':/* pop3 (RFC2384) */
192 if ((s = (unsigned char*)strchr((char *)p, ';')))
195 if (strlen((char *)p)) user = (unsigned char*)EM_SAFE_STRDUP((char *)p);
198 if ((s = (unsigned char*)strchr((char *)p, '@')))
201 if (user || *(p - 3) == '/')
203 if (strncmp((char *)p, "AUTH=", 5) == 0) auth = (unsigned char*)EM_SAFE_STRDUP((char *)p + 5);
206 user = (unsigned char*)EM_SAFE_STRDUP((char *)p);
209 if ((s = (unsigned char*)strchr((char *)p, ':')))
213 EM_DEBUG_LOG("PORT:%s\n", s);
215 host = (unsigned char*)EM_SAFE_STRDUP((char *)p);
219 case 'i':/* imap (RFC2192) */
222 if ((s = (unsigned char*)strchr((char *)p, ';')))
225 if (strlen((char *)p)) user = (unsigned char*)EM_SAFE_STRDUP((char *)p);
228 if ((s = (unsigned char*)strchr((char *)p, '@')))
231 if (user || *(p - 3) == '/')
233 if (strncmp((char *)p, "AUTH=", 5) == 0) auth = (unsigned char*)EM_SAFE_STRDUP((char *)p + 5);
236 user = (unsigned char*)EM_SAFE_STRDUP((char *)p);
240 if ((s = (unsigned char *)strchr((char *)p, '/'))) * s = '\0';
241 host = (unsigned char *)EM_SAFE_STRDUP((char *)p);
244 if ((s = (unsigned char*)strchr((char *)p, '?'))) * s = '\0';
245 else if ((s = (unsigned char*)strchr((char *)p, ';'))) * s = '\0';
246 else s = p + strlen((char *)p);
247 if (*(s - 1) == '/') *(s - 1) = '\0';
249 if (strlen((char *)p)) mbox =(unsigned char*) EM_SAFE_STRDUP((char *)p);
252 case 'h': /* not supported */
253 EM_DEBUG_LOG("Http URI is not yet supported\n");
269 _get_time_from_element(unsigned char* elm,
270 unsigned char** time_stamp)
272 EM_DEBUG_FUNC_BEGIN();
274 unsigned char* p, *s, *stamp = NULL;
275 if (!(p = (unsigned char*)strstr((char *)elm, "timestamp")))
277 EM_DEBUG_EXCEPTION("invalid notification data\n");
282 s = (unsigned char*)strchr((char *)p, '\"');
283 /* 2007-05-08 by acetrack */
288 if (!stamp) /* added acetrack.20080331.K8.4045 */
290 EM_DEBUG_EXCEPTION("malloc failed");
294 memcpy(stamp + 4, p + 5, 2);
295 memcpy(stamp + 6, p + 8, 2);
296 memcpy(stamp + 8, p + 11, 2);
297 memcpy(stamp + 10, p + 14, 2);
298 memcpy(stamp + 12, p + 17, 2);
306 /* <emn mailbox="mailat:user@wapforum.org" timestamp="2002-04-16T06:40:00Z"/> */
307 static int _get_data_from_element(unsigned char* elm,
309 unsigned char** user_name,
310 unsigned char** host_addr,
311 unsigned char** mbox_name,
312 unsigned char** auth_type,
313 unsigned char** time_stamp)
316 EM_DEBUG_FUNC_BEGIN();
317 if (!_get_time_from_element(elm, time_stamp))
322 /* must call get_addr_from_element after calling _get_time_from_element */
323 if (!_get_addr_from_element(elm, type, user_name, host_addr, mbox_name, auth_type))
325 EM_SAFE_FREE*time_stamp) /* added acetrack.20080331.K8.4046 */
332 /* Parse the Email address to Get the user Name for the account [deepam.p@samsung.com] */
334 __emn_get_username(char *account_username)
337 EM_IF_NULL_RETURN_VALUE(account_username, NULL);
340 char **token_list = NULL;
341 char *username = NULL, *name = NULL;
343 username = g_strdup(account_username);
344 token_list = g_strsplit_set(username, "@", -1);
350 if (token_list[index] != NULL)
352 name = g_strdup(token_list[index]);
353 g_strfreev(token_list); /* MUST BE. */
354 EM_DEBUG_LOG("ACCOUNT USER NAME [%s] \n", name);
363 * get account from OMA EMN data
365 * wbxml_b64 : BASE64 encoded data
366 * account_id: id of account to be retrieved from EMN data
367 * mailbox : if mail-box information exists in EMN data, mailbox holds the name of mailbox.
368 * err_code : hold error code
374 _get_emn_account(unsigned char* wbxml_b64, emf_account_t* account, char** mailbox, int* err_code)
376 EM_DEBUG_LOG("_get_emn_account Enter");
377 WBXMLContentHandler parse_handler = {
378 (WBXMLStartDocumentHandler) _cb_parser_start_document,
379 (WBXMLEndDocumentHandler) _cb_parser_end_document,
380 (WBXMLStartElementHandler) _cb_parser_start_element,
381 (WBXMLEndElementHandler) _cb_parser_end_element,
382 (WBXMLCharactersHandler) _cb_parser_characters,
386 WBXMLParser* wbxml_parser = NULL;
387 WB_UTINY* wbxml = NULL;
388 WB_UTINY* elm = NULL;
389 WB_ULONG wbxml_len = 0;
390 WB_ULONG err_idx = 0;
391 WBXMLError ret = WBXML_OK;
392 emf_account_t* accounts = NULL;
393 unsigned char* user_name = NULL;
394 unsigned char* host_addr = NULL;
395 unsigned char* mbox_name = NULL;
396 unsigned char* auth_type = NULL;
397 unsigned char* time_stamp = NULL;
402 int err = EMF_ERROR_NONE;
406 if (!wbxml_b64 || !account)
408 EM_DEBUG_EXCEPTION(">>>> Invalid Parameter >>>> \n");
409 err = EMF_ERROR_INVALID_PARAM;
413 wbxml = g_base64_decode((const char*)wbxml_b64, (unsigned int*)&wbxml_len);
414 EM_DEBUG_LOG("wbxml = %p\n", wbxml);
415 EM_DEBUG_LOG("wbxml_len = %d\n", wbxml_len);
417 /* create wbxml parser */
418 if (!(wbxml_parser = wbxml_parser_create()))
420 err = EMF_ERROR_OUT_OF_MEMORY;
424 /* initialize wbxml parser */
425 wbxml_parser_set_user_data(wbxml_parser, (void*)&elm);
426 wbxml_parser_set_content_handler(wbxml_parser, &parse_handler);
429 if ((ret = wbxml_parser_parse(wbxml_parser, wbxml, wbxml_len)) != WBXML_OK)
431 err_idx = wbxml_parser_get_current_byte_index(wbxml_parser);
432 EM_DEBUG_LOG("Parsing failed at %u - Token %x - %s", err_idx, wbxml[err_idx], wbxml_errors_string(ret));
434 err = EMF_ERROR_XML_PARSER_FAILURE;
439 EM_DEBUG_LOG("Parsing OK !\n");
443 wbxml_parser_destroy(wbxml_parser);
452 EM_DEBUG_EXCEPTION("invalid elements\n");
454 err = EMF_ERROR_XML_PARSER_FAILURE;
458 EM_DEBUG_LOG("elements = [%s]\n", elm);
459 _get_data_from_element(elm, &type, &user_name, &host_addr, &mbox_name, &auth_type, &time_stamp);
463 EM_DEBUG_LOG("user_type = [%d]\n", type);
464 EM_DEBUG_LOG("user_name = [%s]\n", (char *)user_name ? (char*)user_name : "NIL");
465 EM_DEBUG_LOG("host_addr = [%s]\n", (char *)host_addr ? (char*)host_addr : "NIL");
466 EM_DEBUG_LOG("mbox_name = [%s]\n", (char *)mbox_name ? (char*)mbox_name : "NIL");
467 EM_DEBUG_LOG("auth_type = [%s]\n", (char *)auth_type ? (char*)auth_type : "NIL");
468 EM_DEBUG_LOG("time_stamp= [%s]\n", (char *)time_stamp? (char*)time_stamp: "NIL");
470 if (!emf_account_get_list(&accounts, &count, &err))
472 EM_DEBUG_EXCEPTION(" emf_account_get_list error");
473 err = EMF_ERROR_DB_FAILURE;
477 for (i = 0; i < count; i++)
479 /* sowmya.kr, 201009, Fix for EMN */
480 char* temp_account_name = NULL;
482 /* EM_DEBUG_LOG(">>>> Account Information UserName [ %s ] Email Addr [ %s], Account ID [ %d] >>> \n",accounts[i].user_name,accounts[i].email_addr, accounts[i].account_id); */
483 temp_account_name =(char*) EM_SAFE_STRDUP((char *)accounts[i].user_name);
485 if ((s = (char*)strchr((char *)temp_account_name, '@'))) {
487 EM_SAFE_FREE(accounts[i].user_name);
488 accounts[i].user_name = (char*)EM_SAFE_STRDUP((char *)temp_account_name);
490 EM_SAFE_FREE(temp_account_name);
492 if (strcmp(accounts[i].user_name, (char *)user_name) == 0 &&
493 strstr(accounts[i].email_addr, (char *)host_addr)) {
494 EM_DEBUG_LOG(">>>> Account Match >>> \n");
496 (type == 2 && accounts[i].receiving_server_type == EMF_SERVER_TYPE_POP3) ||
497 (type == 3 && accounts[i].receiving_server_type == EMF_SERVER_TYPE_IMAP4)) {
498 accounts[i].flag2 = type;
499 EM_DEBUG_LOG("found target account id[%d] name[%s]", accounts[i].account_id, accounts[i].user_name);
507 EM_DEBUG_EXCEPTION("no account was found");
508 err = EMF_ERROR_ACCOUNT_NOT_FOUND;
512 account->account_bind_type = accounts[i].account_bind_type;
513 account->account_id = accounts[i].account_id;
514 account->flag2 = accounts[i].flag2;
519 *mailbox = mbox_name ? (char *)mbox_name : NULL;
522 emf_account_free(&accounts, count, NULL);
528 if (wbxml) wbxml_free(wbxml);
529 if (wbxml_parser) wbxml_parser_destroy(wbxml_parser);
531 if (accounts) emf_account_free(&accounts, count, NULL);
532 EM_SAFE_FREE(user_name);
533 EM_SAFE_FREE(mbox_name);
534 EM_SAFE_FREE(auth_type);
535 EM_SAFE_FREE(time_stamp);
536 if (err_code) *err_code = err;
540 EXPORT_API int emf_emn_handler(unsigned char* wbxml_b64, emf_emn_noti_cb callback, int* err_code)
542 EM_DEBUG_FUNC_BEGIN("wbxml_b64[%p], callback[%p], err_code[%p]", wbxml_b64, callback, err_code);
545 int err = EMF_ERROR_NONE;;
546 char* mailbox_name = NULL;
547 emf_mailbox_t mailbox = { 0 };
548 emf_account_t account = { 0 };
549 emf_emn_noti_pack_t* pack = NULL;
550 char* pmailbox = NULL;
553 EM_DEBUG_EXCEPTION("EMF_ERROR_INVALID_PARAM");
554 err = EMF_ERROR_INVALID_PARAM;
558 pack = (emf_emn_noti_pack_t*)em_core_malloc(sizeof(emf_emn_noti_pack_t));
560 EM_DEBUG_EXCEPTION("em_core_malloc failed");
561 err = EMF_ERROR_OUT_OF_MEMORY;
565 if (!_get_emn_account(wbxml_b64, &account, &mailbox_name, &err)) {
566 EM_DEBUG_EXCEPTION("_get_emn_account failed [%d]", err);
570 mailbox.account_id = account.account_id;
572 if (!em_storage_get_mailboxname_by_mailbox_type(mailbox.account_id,EMF_MAILBOX_TYPE_INBOX,&pmailbox, false, &err)) {
573 EM_DEBUG_EXCEPTION("em_storage_get_mailboxname_by_mailbox_type failed [%d", err);
574 err = em_storage_get_emf_error_from_em_storage_error(err);
578 if ((account.receiving_server_type == EMF_SERVER_TYPE_IMAP4) && (account.flag2 == 3)) {
580 if (!mailbox_name || strncmp(pmailbox, mailbox_name, strlen(pmailbox)) != 0) {
581 EM_DEBUG_EXCEPTION("invalid inbox name [%p]", mailbox_name);
582 err = EMF_ERROR_INVALID_MAILBOX;
587 /* sync header with mail server */
588 mailbox.name = mailbox_tbl->mailbox_name;
590 if (!emf_mailbox_sync_header(&mailbox, NULL, &err)) {
591 EM_DEBUG_EXCEPTION("emf_mailbox_sync_header falied [%d]", err);
601 EM_SAFE_FREE(pmailbox);
603 if (err_code != NULL)
610 #endif /* USE_OMA_EMN */