Tizen 2.0 Release
[platform/core/messaging/email-service.git] / email-core / email-core-mm-callbacks.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 /******************************************************************************
24  * File :  email-core-mm_callbacks.c
25  * Desc :  mm_callbacks for IMAP-2004g
26  *
27  * Auth :  
28  *
29  * History : 
30  * 2006.08.22  :  created
31  *****************************************************************************/
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include "email-internal-types.h"
36 #include "c-client.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"
42
43 static void mm_get_error(char *string, int *err_code);
44
45 /*
46  * callback mm_lsub
47  *              get subscribed mailbox list
48  */
49 INTERNAL_FUNC void mm_lsub(MAILSTREAM *stream, int delimiter, char *mailbox, long attributes)
50 {
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;
55         char *s, *enc_path;
56
57         /* memory allocation */
58         p = realloc(p_old, sizeof(email_mailbox_t) * (count + 1));
59         if (!p) return ;
60
61         /* uw-imap mailbox name format (ex : "{mail.test.com...}inbox" or "{mail.test.com...}anybox/anysubbox") */
62         enc_path = strchr(mailbox, '}');
63         if (enc_path)   
64                 enc_path += 1;
65         else {
66                 emcore_free_mailbox_list(&p, count+1);
67                 return ;
68         }
69
70         /* Convert UTF7 mailbox name to UTF8 mailbox name */
71
72         /* convert directory delimiter to '/' */
73         for (s = enc_path; *s; s++) {
74                 if (*s == (char)delimiter) {
75                         *s = '/';
76                 }
77         }
78
79         /* coyp string */
80         p[count].mailbox_name = cpystr(enc_path);
81         p[count].alias        = cpystr(enc_path);
82         p[count].local        = 0;
83         p[count].account_id   = stream->spare8;
84
85
86         p_holder->data = p;
87         p_holder->num++;
88
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); */
94         EM_DEBUG_FUNC_END();
95 }
96
97
98 /*
99  * callback mm_lsub
100  * get mailbox list
101  */
102 INTERNAL_FUNC void mm_list(MAILSTREAM *stream, int delimiter, char *mailbox, long attributes)
103 {
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;
108         char *s, *enc_path;
109
110         /* memory allocation */
111         p = realloc(p_old, sizeof(email_internal_mailbox_t) * (count + 1));
112         if (!p) return ;
113
114         /* uw-imap mailbox name format (ex : "{mail.test.com...}inbox" or "{mail.test.com...}anybox/anysubbox") */
115         enc_path = strchr(mailbox, '}');
116         if (enc_path)   
117                 enc_path += 1;
118         else {
119                 emcore_free_internal_mailbox(&p, count+1, NULL);
120                 return ;
121         }
122
123         /* convert directory delimiter to '/' */
124         for (s = enc_path;*s;s++) 
125                 if (*s == (char)delimiter) 
126                         *s = '/';
127
128         /* copy string */
129         memset(p + count, 0x00, sizeof(email_internal_mailbox_t));
130
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__ */
147
148         if(attributes & LATT_NOSELECT)
149                 p[count].no_select    = true;
150
151         if(p[count].mailbox_type == EMAIL_MAILBOX_TYPE_INBOX) /* For exception handling of Gmail inbox*/
152                 p[count].mailbox_name  = cpystr("INBOX");
153         else
154                 p[count].mailbox_name  = cpystr(enc_path);
155
156         EM_DEBUG_LOG("mailbox name [%s] mailbox_type [%d] no_select [%d]", p[count].mailbox_name, p[count].mailbox_type, p[count].no_select);
157
158         p[count].alias = emcore_get_alias_of_mailbox((const char *)enc_path);
159         p[count].local = 0;
160
161         EM_DEBUG_LOG("mm_list account_id %d", stream->spare8);
162
163         char *tmp = NULL;
164         /* in mailbox name parse n get user = %d - which is account_id */
165         tmp = strstr(mailbox, "user=");
166         if (tmp) {
167                 tmp = tmp+5;
168                 for (s = tmp; *s != '/'; s++);
169                 *s = '\0';
170                 p[count].account_id = atoi(tmp);
171         }
172         EM_DEBUG_LOG("mm_list account_id %d ", p[count].account_id);
173
174         p_holder->data = p;
175         p_holder->num++;
176
177         /* ignore attributes */
178         EM_DEBUG_FUNC_END();
179 }
180
181
182 /*
183  * callback mm_status
184  * get mailbox status
185  */
186 INTERNAL_FUNC void mm_status(MAILSTREAM *stream, char *mailbox, MAILSTATUS* status)
187 {
188         EM_DEBUG_FUNC_BEGIN();
189         email_callback_holder_t *p = stream->sparep;
190
191         EM_DEBUG_FUNC_BEGIN();
192         if (status->flags & SA_MESSAGES) 
193                 p->num = status->messages;
194         if (status->flags & SA_UNSEEN) 
195                 p->data = (void *)status->unseen;
196         EM_DEBUG_FUNC_END();
197 }
198
199 /* callback mm_login
200  * get user_name and password
201  */
202
203 INTERNAL_FUNC void mm_login(NETMBX *mb, char *user, char *pwd, long trial)
204 {
205         EM_DEBUG_FUNC_BEGIN();
206         int account_id;
207         email_account_t *ref_account = NULL;
208         char *username = NULL;
209         char *password = NULL;
210
211         if (!mb->user[0])  {
212                 EM_DEBUG_EXCEPTION("invalid account_id...");
213                 goto FINISH_OFF;
214         }
215         
216         account_id = atoi(mb->user);
217
218         ref_account = emcore_get_account_reference(account_id);
219
220         if (!ref_account)  {
221                 EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
222                 goto FINISH_OFF;
223         }
224
225         if (ref_account->incoming_server_user_name == NULL) {
226                 EM_DEBUG_EXCEPTION("invalid incoming_server_user_name...");
227                 goto FINISH_OFF;
228         }
229         username = EM_SAFE_STRDUP(ref_account->incoming_server_user_name);
230
231         if (ref_account->incoming_server_password == NULL) {
232                 EM_SAFE_FREE(username);
233                 EM_DEBUG_EXCEPTION("invalid password...");
234                 goto FINISH_OFF;
235         }
236
237         password = EM_SAFE_STRDUP(ref_account->incoming_server_password);
238
239         if(EM_SAFE_STRLEN(username) > 0 && EM_SAFE_STRLEN(password) > 0) { /*prevent 34355*/
240                 strcpy(user, username);
241                 strcpy(pwd, password);
242         }
243         else
244                 EM_DEBUG_EXCEPTION("User Information is NULL || EM_SAFE_STRLEN is 0 ");
245
246 FINISH_OFF:
247
248         if (ref_account) {
249                 emcore_free_account(ref_account);
250                 EM_SAFE_FREE(ref_account);
251         }
252
253         EM_SAFE_FREE(username);
254         EM_SAFE_FREE(password);
255
256         EM_DEBUG_FUNC_END();
257 }
258
259
260 INTERNAL_FUNC void mm_dlog(char *string)
261 {
262 #ifdef FEATURE_CORE_DEBUG
263         EM_DEBUG_LOG("IMAP_TOOLKIT_DLOG [%s]", string);
264 #endif
265 }
266
267 INTERNAL_FUNC void mm_log(char *string, long errflg)
268 {
269         
270         switch ((short)errflg)  {
271                 case NIL:
272                         EM_DEBUG_LOG("IMAP_TOOLKIT_LOG NIL [%s]", string);
273                         break;
274                         
275                 case WARN:
276                         EM_DEBUG_LOG("IMAP_TOOLKIT_LOG WARN [%s]", string);
277                         break;
278                         
279                 case PARSE:
280                         EM_DEBUG_LOG("IMAP_TOOLKIT_LOG PARSE [%s]", string);
281                         break;
282                         
283                 case BYE:
284                         EM_DEBUG_LOG("IMAP_TOOLKIT_LOG BYE [%s]", string);
285                         break;
286                         
287                 case TCPDEBUG:
288                         EM_DEBUG_LOG("IMAP_TOOLKIT_LOG TCPDEBUG [%s]", string);
289                         break;
290                         
291                 case ERROR: {
292                         email_session_t *session = NULL;
293                         
294                         EM_DEBUG_EXCEPTION("IMAP_TOOLKIT_LOG ERROR [%s]", string);
295
296                         emcore_get_current_session(&session);
297                         
298                         if (session) {
299                                 mm_get_error(string, &session->error);
300                                 EM_DEBUG_EXCEPTION("IMAP_TOOLKIT_LOG ERROR [%d]", session->error);
301                         }
302                         
303                         break;
304                 }
305         }
306 }
307
308 INTERNAL_FUNC void mm_searched(MAILSTREAM *stream, unsigned long number)
309 {
310         EM_DEBUG_FUNC_BEGIN("stream [%p], number [%d]", stream, number);
311         EM_DEBUG_FUNC_END();
312 }
313
314 INTERNAL_FUNC void mm_exists(MAILSTREAM *stream, unsigned long number)
315 {
316         EM_DEBUG_FUNC_BEGIN("stream [%p], number [%d]", stream, number);
317         EM_DEBUG_FUNC_END();
318 }
319
320 INTERNAL_FUNC void mm_expunged(MAILSTREAM *stream, unsigned long number)
321 {
322         EM_DEBUG_FUNC_BEGIN("stream [%p], number [%d]", stream, number);
323         EM_DEBUG_FUNC_END();
324 }
325
326 INTERNAL_FUNC void mm_flags(MAILSTREAM *stream, unsigned long number)
327 {
328         EM_DEBUG_FUNC_BEGIN("stream [%p], number [%d]", stream, number);
329         EM_DEBUG_FUNC_END();
330 }
331
332 INTERNAL_FUNC void mm_notify(MAILSTREAM *stream, char *string, long errflg)
333 {
334         EM_DEBUG_FUNC_BEGIN();
335         mm_log(string, errflg);
336         EM_DEBUG_FUNC_END();
337 }
338
339 INTERNAL_FUNC void mm_critical(MAILSTREAM *stream)
340 {
341         EM_DEBUG_FUNC_BEGIN("stream [%p]", stream);
342         EM_DEBUG_FUNC_END();
343 }
344
345 INTERNAL_FUNC void mm_nocritical(MAILSTREAM *stream)
346 {
347         EM_DEBUG_FUNC_BEGIN("stream [%p]", stream);
348         EM_DEBUG_FUNC_END();
349 }
350
351 INTERNAL_FUNC long mm_diskerror(MAILSTREAM *stream, long errcode, long serious)
352 {
353         EM_DEBUG_FUNC_BEGIN("stream [%p] errcode[%d] serious[%d]", stream, errcode, serious);
354 #if UNIXLIKE
355         kill(getpid(), SIGSTOP);
356 #else
357         abort();
358 #endif
359         EM_DEBUG_FUNC_END();
360         return NIL;
361 }
362
363 INTERNAL_FUNC void mm_fatal(char *string)
364 {
365         EM_DEBUG_EXCEPTION("%s", string);
366 }
367
368 INTERNAL_FUNC void mm_get_error(char *string, int *err_code)
369 {
370         if (!string || !err_code)
371                 return ;
372
373         EM_DEBUG_LOG("string [%s]", string);
374
375         if (strstr(string, "login failure") || strstr(string, "Login aborted") || strstr(string, "Can't login"))
376                 *err_code = EMAIL_ERROR_LOGIN_FAILURE;
377         else if (strstr(string, "Scan not valid"))
378                 *err_code = EMAIL_ERROR_SCAN_NOT_SUPPORTED;
379         else if (strstr(string, "Authentication cancelled"))
380                 *err_code = EMAIL_ERROR_AUTHENTICATE;
381         else if (strstr(string, "authuser"))
382                 *err_code = EMAIL_ERROR_AUTH_NOT_SUPPORTED;
383         else if (strstr(string, "negotiate TLS"))
384                 *err_code = EMAIL_ERROR_CANNOT_NEGOTIATE_TLS;
385         else if (strstr(string, "TLS/SSL failure"))
386                 *err_code = EMAIL_ERROR_TLS_SSL_FAILURE;
387         else if (strstr(string, "STARTLS"))
388                 *err_code = EMAIL_ERROR_STARTLS;
389         else if (strstr(string, "TLS unavailable"))
390                 *err_code = EMAIL_ERROR_TLS_NOT_SUPPORTED;
391         else if (strstr(string, "Can't access"))
392                 *err_code = EMAIL_ERROR_IMAP4_APPEND_FAILURE;
393         else if (strstr(string, "Can not authenticate"))
394                 *err_code = EMAIL_ERROR_AUTHENTICATE;
395         else if (strstr(string, "Unexpected IMAP response") || strstr(string, "hello"))
396                 *err_code = EMAIL_ERROR_INVALID_RESPONSE;
397         else if (strstr(string, "NOTIMAP4REV1"))
398                 *err_code = EMAIL_ERROR_COMMAND_NOT_SUPPORTED;
399         else if (strstr(string, "Anonymous"))
400                 *err_code = EMAIL_ERROR_ANNONYM_NOT_SUPPORTED;
401         else if (strstr(string, "connection broken"))
402                 *err_code = EMAIL_ERROR_CONNECTION_BROKEN;
403         else if (strstr(string, "SMTP greeting"))
404                 *err_code = EMAIL_ERROR_NO_RESPONSE;
405         else if (strstr(string, "ESMTP failure"))
406                 *err_code = EMAIL_ERROR_SMTP_SEND_FAILURE;
407         else if (strstr(string, "socket") || strstr(string, "Socket"))
408                 *err_code = EMAIL_ERROR_SOCKET_FAILURE;
409         else if (strstr(string, "connect to") || strstr(string, "Connection failed"))
410                 *err_code = EMAIL_ERROR_CONNECTION_FAILURE;
411         else if (strstr(string, "Certificate failure"))
412                 *err_code = EMAIL_ERROR_CERTIFICATE_FAILURE;
413         else if (strstr(string, "ESMTP failure"))
414                 *err_code = EMAIL_ERROR_INVALID_PARAM;
415         else if (strstr(string, "No such host"))
416                 *err_code = EMAIL_ERROR_NO_SUCH_HOST;
417         else if (strstr(string, "host") || strstr(string, "Host"))
418                 *err_code = EMAIL_ERROR_INVALID_SERVER;
419         else if (strstr(string, "SELECT failed"))
420                 *err_code = EMAIL_ERROR_MAILBOX_NOT_FOUND;
421         else if (strstr(string, "15 minute"))
422                 *err_code = EMAIL_ERROR_LOGIN_ALLOWED_EVERY_15_MINS;
423         else
424                 *err_code = EMAIL_ERROR_UNKNOWN;
425 }
426
427 #ifdef __FEATURE_SUPPORT_IMAP_ID__
428 INTERNAL_FUNC void mm_imap_id (char **id_string)
429 {
430         EM_DEBUG_FUNC_BEGIN("id_string [%p]", id_string);
431
432         int   err = EMAIL_ERROR_NONE;
433         /*
434         char *result_string = NULL;
435         char *tag_string = "ID (\"os\" \"" IMAP_ID_OS "\" \"os-version\" \"" IMAP_ID_OS_VERSION "\" \"vendor\" \"" IMAP_ID_VENDOR "\" \"device\" \"" IMAP_ID_DEVICE_NAME "\" \"AGUID\" \"" IMAP_ID_AGUID "\" \"ACLID\" \"" IMAP_ID_ACLID "\"";
436         int   tag_length = 0;
437         */
438
439         if (id_string == NULL) {
440                 EM_DEBUG_EXCEPTION("EMAIL_ERROR_INVALID_PARAM");
441                 err = EMAIL_ERROR_INVALID_PARAM;
442                 goto FINISH_OFF;
443         }
444
445         *id_string = NULL;
446
447         /*
448         tag_length = EM_SAFE_STRLEN(tag_string);
449         result_string = EM_SAFE_STRDUP(tag_string);
450
451         if(result_string == NULL) {
452                 EM_DEBUG_EXCEPTION("malloc failed");
453                 err = EMAIL_ERROR_OUT_OF_MEMORY;
454                 goto FINISH_OFF;
455         }
456
457         *id_string = result_string;
458         */
459
460 FINISH_OFF:
461         EM_DEBUG_FUNC_END("err [%d]", err);
462 }
463 #endif /* __FEATURE_SUPPORT_IMAP_ID__ */
464 /* EOF */