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.
23 /******************************************************************************
24 * File : email-core-mm_callbacks.c
25 * Desc : mm_callbacks for IMAP-2004g
30 * 2006.08.22 : created
31 *****************************************************************************/
35 #include "email-internal-types.h"
37 #include "email-core-global.h"
38 #include "email-core-utils.h"
39 #include "email-debug-log.h"
40 #include "email-core-mailbox.h"
41 #include "email-core-account.h"
43 static void mm_get_error(char *string, int *err_code);
47 * get subscribed mailbox list
49 INTERNAL_FUNC void mm_lsub(MAILSTREAM *stream, int delimiter, char *mailbox, long attributes)
51 EM_DEBUG_FUNC_BEGIN();
52 email_callback_holder_t *p_holder = (email_callback_holder_t *)stream->sparep;
53 email_mailbox_t *p, *p_old = p_holder->data;
54 int count = p_holder->num;
57 /* memory allocation */
58 p = realloc(p_old, sizeof(email_mailbox_t) * (count + 1));
61 /* uw-imap mailbox name format (ex : "{mail.test.com...}inbox" or "{mail.test.com...}anybox/anysubbox") */
62 enc_path = strchr(mailbox, '}');
66 emcore_free_mailbox_list(&p, count+1);
70 /* Convert UTF7 mailbox name to UTF8 mailbox name */
72 /* convert directory delimiter to '/' */
73 for (s = enc_path; *s; s++) {
74 if (*s == (char)delimiter) {
80 p[count].mailbox_name = cpystr(enc_path);
81 p[count].alias = cpystr(enc_path);
83 p[count].account_id = stream->spare8;
89 /* ignore attributes */
90 /* if (attributes & latt_noinferiors) fputs (", no inferiors", fp_log); */
91 /* if (attributes & latt_noselect) fputs (", no select", fp_log); */
92 /* if (attributes & latt_marked) fputs (", marked", fp_log); */
93 /* if (attributes & latt_unmarked) fputs (", unmarked", fp_log); */
102 INTERNAL_FUNC void mm_list(MAILSTREAM *stream, int delimiter, char *mailbox, long attributes)
104 EM_DEBUG_FUNC_BEGIN();
105 email_callback_holder_t *p_holder = (email_callback_holder_t *)stream->sparep;
106 email_internal_mailbox_t *p, *p_old = p_holder->data;
107 int count = p_holder->num;
110 /* memory allocation */
111 p = realloc(p_old, sizeof(email_internal_mailbox_t) * (count + 1));
114 /* uw-imap mailbox name format (ex : "{mail.test.com...}inbox" or "{mail.test.com...}anybox/anysubbox") */
115 enc_path = strchr(mailbox, '}');
119 emcore_free_internal_mailbox(&p, count+1, NULL);
123 /* convert directory delimiter to '/' */
124 for (s = enc_path;*s;s++)
125 if (*s == (char)delimiter)
129 memset(p + count, 0x00, sizeof(email_internal_mailbox_t));
131 #ifdef __FEATURE_XLIST_SUPPORT__
132 if(attributes & LATT_XLIST_INBOX)
133 p[count].mailbox_type = EMAIL_MAILBOX_TYPE_INBOX;
134 else if(attributes & LATT_XLIST_ALL)
135 p[count].mailbox_type = EMAIL_MAILBOX_TYPE_ALL_EMAILS;
136 else if(attributes & LATT_XLIST_DRAFTS)
137 p[count].mailbox_type = EMAIL_MAILBOX_TYPE_DRAFT;
138 else if(attributes & LATT_XLIST_SENT)
139 p[count].mailbox_type = EMAIL_MAILBOX_TYPE_SENTBOX;
140 else if(attributes & LATT_XLIST_JUNK)
141 p[count].mailbox_type = EMAIL_MAILBOX_TYPE_SPAMBOX;
142 else if(attributes & LATT_XLIST_FLAGGED)
143 p[count].mailbox_type = EMAIL_MAILBOX_TYPE_FLAGGED;
144 else if(attributes & LATT_XLIST_TRASH)
145 p[count].mailbox_type = EMAIL_MAILBOX_TYPE_TRASH;
146 #endif /* __FEATURE_XLIST_SUPPORT__ */
148 if(p[count].mailbox_type == EMAIL_MAILBOX_TYPE_INBOX) /* For exception handling of Gmail inbox*/
149 p[count].mailbox_name = cpystr("INBOX");
151 p[count].mailbox_name = cpystr(enc_path);
153 EM_DEBUG_LOG("mm_list mailbox name is %s ", p[count].mailbox_name);
155 p[count].alias = emcore_get_alias_of_mailbox((const char *)enc_path);
158 EM_DEBUG_LOG("mm_list account_id %d", stream->spare8);
161 /* in mailbox name parse n get user = %d - which is account_id */
162 tmp = strstr(mailbox, "user=");
165 for (s = tmp; *s != '/'; s++);
167 p[count].account_id = atoi(tmp);
169 EM_DEBUG_LOG("mm_list account_id %d ", p[count].account_id);
174 /* ignore attributes */
183 INTERNAL_FUNC void mm_status(MAILSTREAM *stream, char *mailbox, MAILSTATUS* status)
185 EM_DEBUG_FUNC_BEGIN();
186 email_callback_holder_t *p = stream->sparep;
188 EM_DEBUG_FUNC_BEGIN();
189 if (status->flags & SA_MESSAGES)
190 p->num = status->messages;
191 if (status->flags & SA_UNSEEN)
192 p->data = (void *)status->unseen;
197 * get user_name and password
200 INTERNAL_FUNC void mm_login(NETMBX *mb, char *user, char *pwd, long trial)
202 EM_DEBUG_FUNC_BEGIN();
204 email_account_t *ref_account;
205 char *username = NULL;
206 char *password = NULL;
209 EM_DEBUG_EXCEPTION("invalid account_id...");
213 account_id = atoi(mb->user);
215 ref_account = emcore_get_account_reference(account_id);
218 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
222 if (ref_account->incoming_server_user_name == NULL) {
223 EM_DEBUG_EXCEPTION("invalid incoming_server_user_name...");
226 username = EM_SAFE_STRDUP(ref_account->incoming_server_user_name);
228 if (ref_account->incoming_server_password == NULL) {
229 EM_SAFE_FREE(username);
230 EM_DEBUG_EXCEPTION("invalid password...");
234 password = EM_SAFE_STRDUP(ref_account->incoming_server_password);
236 if (username && password && strlen(username) > 0 && strlen(password) > 0) {
237 strcpy(user, username);
238 strcpy(pwd, password);
241 EM_DEBUG_EXCEPTION("User Information is NULL || strlen is 0 ");
243 EM_SAFE_FREE(username);
244 EM_SAFE_FREE(password);
250 INTERNAL_FUNC void mm_dlog(char *string)
252 #ifdef FEATURE_CORE_DEBUG
253 EM_DEBUG_LOG("IMAP_TOOLKIT_DLOG [%s]", string);
254 /* Write into debug file
255 FILE *fp_dlog = NULL;
256 fp_dlog = fopen("/opt/data/email/.emfdata/core_debug", "a");
258 fprintf(fp_dlog, "%s\n", string);
265 INTERNAL_FUNC void mm_log(char *string, long errflg)
267 /* EM_DEBUG_FUNC_BEGIN(); */
269 switch ((short)errflg) {
271 EM_DEBUG_LOG("IMAP_TOOLKIT_LOG NIL [%s]", string);
275 EM_DEBUG_LOG("IMAP_TOOLKIT_LOG WARN [%s]", string);
279 EM_DEBUG_LOG("IMAP_TOOLKIT_LOG PARSE [%s]", string);
283 EM_DEBUG_LOG("IMAP_TOOLKIT_LOG BYE [%s]", string);
287 EM_DEBUG_LOG("IMAP_TOOLKIT_LOG TCPDEBUG [%s]", string);
291 email_session_t *session = NULL;
293 EM_DEBUG_EXCEPTION("IMAP_TOOLKIT_LOG ERROR [%s]", string);
295 emcore_get_current_session(&session);
298 mm_get_error(string, &session->error);
299 EM_DEBUG_EXCEPTION("IMAP_TOOLKIT_LOG ERROR [%d]", session->error);
302 /* Handling exceptional case of connection failures. */
305 if (strstr(string, "15 minute") != 0) {
307 session->error = EMAIL_ERROR_LOGIN_ALLOWED_EVERY_15_MINS;
309 else if (strstr(string, "Too many login failures") == 0) {
311 session->error = EMAIL_ERROR_TOO_MANY_LOGIN_FAILURE;
321 INTERNAL_FUNC void mm_searched(MAILSTREAM *stream, unsigned long number)
323 EM_DEBUG_FUNC_BEGIN("stream [%p], number [%d]", stream, number);
327 INTERNAL_FUNC void mm_exists(MAILSTREAM *stream, unsigned long number)
329 EM_DEBUG_FUNC_BEGIN("stream [%p], number [%d]", stream, number);
333 INTERNAL_FUNC void mm_expunged(MAILSTREAM *stream, unsigned long number)
335 EM_DEBUG_FUNC_BEGIN("stream [%p], number [%d]", stream, number);
339 INTERNAL_FUNC void mm_flags(MAILSTREAM *stream, unsigned long number)
341 EM_DEBUG_FUNC_BEGIN("stream [%p], number [%d]", stream, number);
345 INTERNAL_FUNC void mm_notify(MAILSTREAM *stream, char *string, long errflg)
347 EM_DEBUG_FUNC_BEGIN();
348 mm_log(string, errflg);
352 INTERNAL_FUNC void mm_critical(MAILSTREAM *stream)
354 EM_DEBUG_FUNC_BEGIN("stream [%p]", stream);
358 INTERNAL_FUNC void mm_nocritical(MAILSTREAM *stream)
360 EM_DEBUG_FUNC_BEGIN("stream [%p]", stream);
364 INTERNAL_FUNC long mm_diskerror(MAILSTREAM *stream, long errcode, long serious)
366 EM_DEBUG_FUNC_BEGIN("stream [%p] errcode[%d] serious[%d]", stream, errcode, serious);
368 kill(getpid(), SIGSTOP);
376 INTERNAL_FUNC void mm_fatal(char *string)
378 EM_DEBUG_EXCEPTION("%s", string);
381 INTERNAL_FUNC void mm_get_error(char *string, int *err_code)
383 if (!string || !err_code)
386 EM_DEBUG_LOG("string [%s]", string);
388 if (strstr(string, "login failure") || strstr(string, "Login aborted") || strstr(string, "Can't login"))
389 *err_code = EMAIL_ERROR_LOGIN_FAILURE;
390 else if (strstr(string, "Scan not valid"))
391 *err_code = EMAIL_ERROR_MAILBOX_FAILURE;
392 else if (strstr(string, "Authentication cancelled"))
393 *err_code = EMAIL_ERROR_AUTHENTICATE;
394 else if (strstr(string, "authuser"))
395 *err_code = EMAIL_ERROR_AUTH_NOT_SUPPORTED;
396 else if (strstr(string, "negotiate TLS"))
397 *err_code = EMAIL_ERROR_CANNOT_NEGOTIATE_TLS;
398 else if (strstr(string, "TLS/SSL failure"))
399 *err_code = EMAIL_ERROR_TLS_SSL_FAILURE;
400 else if (strstr(string, "STARTLS"))
401 *err_code = EMAIL_ERROR_STARTLS;
402 else if (strstr(string, "TLS unavailable"))
403 *err_code = EMAIL_ERROR_TLS_NOT_SUPPORTED;
404 else if (strstr(string, "Can't access"))
405 *err_code = EMAIL_ERROR_APPEND_FAILURE;
406 else if (strstr(string, "Can not authenticate"))
407 *err_code = EMAIL_ERROR_AUTHENTICATE;
408 else if (strstr(string, "Unexpected IMAP response") || strstr(string, "hello"))
409 *err_code = EMAIL_ERROR_INVALID_RESPONSE;
410 else if (strstr(string, "NOTIMAP4REV1"))
411 *err_code = EMAIL_ERROR_COMMAND_NOT_SUPPORTED;
412 else if (strstr(string, "Anonymous"))
413 *err_code = EMAIL_ERROR_ANNONYM_NOT_SUPPORTED;
414 else if (strstr(string, "connection broken"))
415 *err_code = EMAIL_ERROR_CONNECTION_BROKEN;
416 else if (strstr(string, "SMTP greeting"))
417 *err_code = EMAIL_ERROR_NO_RESPONSE;
418 else if (strstr(string, "ESMTP failure"))
419 *err_code = EMAIL_ERROR_SMTP_SEND_FAILURE;
420 else if (strstr(string, "socket") || strstr(string, "Socket"))
421 *err_code = EMAIL_ERROR_SOCKET_FAILURE;
422 else if (strstr(string, "connect to") || strstr(string, "Connection failed"))
423 *err_code = EMAIL_ERROR_CONNECTION_FAILURE;
424 else if (strstr(string, "Certificate failure"))
425 *err_code = EMAIL_ERROR_CERTIFICATE_FAILURE;
426 else if (strstr(string, "ESMTP failure"))
427 *err_code = EMAIL_ERROR_INVALID_PARAM;
428 else if (strstr(string, "No such host"))
429 *err_code = EMAIL_ERROR_NO_SUCH_HOST;
430 else if (strstr(string, "host") || strstr(string, "Host"))
431 *err_code = EMAIL_ERROR_INVALID_SERVER;
432 else if (strstr(string, "SELECT failed"))
433 *err_code = EMAIL_ERROR_MAILBOX_NOT_FOUND;
435 *err_code = EMAIL_ERROR_UNKNOWN;