summaryrefslogtreecommitdiff
path: root/email-core/email-core-mm-callbacks.c
blob: 5165572c3dfd9d63076766a0c443d41083e36ee4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
*  email-service
*
* Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
*
* Contact: Kyuho Jo <kyuho.jo@samsung.com>, Sunghyun Kwon <sh0701.kwon@samsung.com>
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/


/******************************************************************************
 * File :  email-core-mm_callbacks.c
 * Desc :  mm_callbacks for IMAP-2004g
 *
 * Auth :  
 *
 * History : 
 * 2006.08.22  :  created
 *****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "email-internal-types.h"
#include "c-client.h"
#include "email-core-global.h"
#include "email-core-utils.h"
#include "email-debug-log.h"
#include "email-core-mailbox.h"
#include "email-core-account.h"

static void mm_get_error(char *string, int *err_code);

/*
 * callback mm_lsub
 *		get subscribed mailbox list
 */
INTERNAL_FUNC void mm_lsub(MAILSTREAM *stream, int delimiter, char *mailbox, long attributes)
{
	EM_DEBUG_FUNC_BEGIN();
	email_callback_holder_t *p_holder = (email_callback_holder_t *)stream->sparep;
	email_mailbox_t *p, *p_old = p_holder->data;
	int count = p_holder->num;
	char *s, *enc_path;

	/* memory allocation */
	p = realloc(p_old, sizeof(email_mailbox_t) * (count + 1));
	if (!p) return ;

	/* uw-imap mailbox name format (ex : "{mail.test.com...}inbox" or "{mail.test.com...}anybox/anysubbox") */
	enc_path = strchr(mailbox, '}');
	if (enc_path)	
		enc_path += 1;
	else {
		emcore_free_mailbox_list(&p, count+1);
		return ;
	}

	/* Convert UTF7 mailbox name to UTF8 mailbox name */

	/* convert directory delimiter to '/' */
	for (s = enc_path; *s; s++) {
		if (*s == (char)delimiter) {
			*s = '/';
		}
	}

	/* coyp string */
	p[count].mailbox_name = cpystr(enc_path);
	p[count].alias        = cpystr(enc_path);
	p[count].local        = 0;
	p[count].account_id   = stream->spare8;


	p_holder->data = p;
	p_holder->num++;

	/* ignore attributes */
/*  if (attributes & latt_noinferiors) fputs (", no inferiors", fp_log); */
/*  if (attributes & latt_noselect) fputs (", no select", fp_log); */
/*  if (attributes & latt_marked) fputs (", marked", fp_log); */
/*  if (attributes & latt_unmarked) fputs (", unmarked", fp_log); */
	EM_DEBUG_FUNC_END();
}


/*
 * callback mm_lsub
 * get mailbox list
 */
INTERNAL_FUNC void mm_list(MAILSTREAM *stream, int delimiter, char *mailbox, long attributes)
{
	EM_DEBUG_FUNC_BEGIN();
	email_callback_holder_t *p_holder = (email_callback_holder_t *)stream->sparep;
	email_internal_mailbox_t *p, *p_old = p_holder->data;
	int count = p_holder->num;
	char *s, *enc_path;

	/* memory allocation */
	p = realloc(p_old, sizeof(email_internal_mailbox_t) * (count + 1));
	if (!p) return ;

	/* uw-imap mailbox name format (ex : "{mail.test.com...}inbox" or "{mail.test.com...}anybox/anysubbox") */
	enc_path = strchr(mailbox, '}');
	if (enc_path)	
		enc_path += 1;
	else {
		emcore_free_internal_mailbox(&p, count+1, NULL);
		return ;
	}

	/* convert directory delimiter to '/' */
	for (s = enc_path;*s;s++) 
		if (*s == (char)delimiter) 
			*s = '/';

	/* copy string */
	memset(p + count, 0x00, sizeof(email_internal_mailbox_t));

#ifdef __FEATURE_XLIST_SUPPORT__
	if(attributes & LATT_XLIST_INBOX)
		p[count].mailbox_type = EMAIL_MAILBOX_TYPE_INBOX;
	else if(attributes & LATT_XLIST_ALL)
		p[count].mailbox_type = EMAIL_MAILBOX_TYPE_ALL_EMAILS;
	else if(attributes & LATT_XLIST_DRAFTS)
		p[count].mailbox_type = EMAIL_MAILBOX_TYPE_DRAFT;
	else if(attributes & LATT_XLIST_SENT)
		p[count].mailbox_type = EMAIL_MAILBOX_TYPE_SENTBOX;
	else if(attributes & LATT_XLIST_JUNK)
		p[count].mailbox_type = EMAIL_MAILBOX_TYPE_SPAMBOX;
	else if(attributes & LATT_XLIST_FLAGGED)
		p[count].mailbox_type = EMAIL_MAILBOX_TYPE_FLAGGED;
	else if(attributes & LATT_XLIST_TRASH)
		p[count].mailbox_type = EMAIL_MAILBOX_TYPE_TRASH;
#endif /* __FEATURE_XLIST_SUPPORT__ */

	if(p[count].mailbox_type == EMAIL_MAILBOX_TYPE_INBOX) /* For exception handling of Gmail inbox*/
		p[count].mailbox_name  = cpystr("INBOX");
	else
		p[count].mailbox_name  = cpystr(enc_path);

	EM_DEBUG_LOG("mm_list mailbox name is %s ", p[count].mailbox_name);

	p[count].alias = emcore_get_alias_of_mailbox((const char *)enc_path);
	p[count].local = 0;

	EM_DEBUG_LOG("mm_list account_id %d", stream->spare8);

	char *tmp = NULL;
	/* in mailbox name parse n get user = %d - which is account_id */
	tmp = strstr(mailbox, "user=");
	if (tmp) {
		tmp = tmp+5;
		for (s = tmp; *s != '/'; s++);
		*s = '\0';
		p[count].account_id = atoi(tmp);
	}
	EM_DEBUG_LOG("mm_list account_id %d ", p[count].account_id);

	p_holder->data = p;
	p_holder->num++;

	/* ignore attributes */
	EM_DEBUG_FUNC_END();
}


/*
 * callback mm_status
 * get mailbox status
 */
INTERNAL_FUNC void mm_status(MAILSTREAM *stream, char *mailbox, MAILSTATUS* status)
{
	EM_DEBUG_FUNC_BEGIN();
	email_callback_holder_t *p = stream->sparep;

	EM_DEBUG_FUNC_BEGIN();
	if (status->flags & SA_MESSAGES) 
		p->num = status->messages;
	if (status->flags & SA_UNSEEN) 
		p->data = (void *)status->unseen;
	EM_DEBUG_FUNC_END();
}

/* callback mm_login
 * get user_name and password
 */

INTERNAL_FUNC void mm_login(NETMBX *mb, char *user, char *pwd, long trial)
{
	EM_DEBUG_FUNC_BEGIN();
	int account_id;
	email_account_t *ref_account;
	char *username = NULL;
	char *password = NULL;

	if (!mb->user[0])  {
		EM_DEBUG_EXCEPTION("invalid account_id...");
		return;
	}
	
	account_id = atoi(mb->user);

	ref_account = emcore_get_account_reference(account_id);

	if (!ref_account)  {
		EM_DEBUG_EXCEPTION("emcore_get_account_reference failed");
		return;
	}

	if (ref_account->incoming_server_user_name == NULL) {
		EM_DEBUG_EXCEPTION("invalid incoming_server_user_name...");
		return;
	}
	username = EM_SAFE_STRDUP(ref_account->incoming_server_user_name);

	if (ref_account->incoming_server_password == NULL) {
		EM_SAFE_FREE(username);
		EM_DEBUG_EXCEPTION("invalid password...");
		return;
	}

	password = EM_SAFE_STRDUP(ref_account->incoming_server_password);

	if (username && password && strlen(username) > 0 && strlen(password) > 0) {
		strcpy(user, username);
		strcpy(pwd, password);
	}
	else
		EM_DEBUG_EXCEPTION("User Information is NULL || strlen is 0 ");
		
	EM_SAFE_FREE(username);
	EM_SAFE_FREE(password);

	EM_DEBUG_FUNC_END();
}


INTERNAL_FUNC void mm_dlog(char *string)
{
#ifdef FEATURE_CORE_DEBUG
	EM_DEBUG_LOG("IMAP_TOOLKIT_DLOG [%s]", string);
	/* Write into debug file
	FILE *fp_dlog = NULL; 
	fp_dlog = fopen("/opt/data/email/.emfdata/core_debug", "a");
    if(fp_dlog) {
		fprintf(fp_dlog, "%s\n", string); 
		fclose(fp_dlog);
	}
	*/
#endif
}

INTERNAL_FUNC void mm_log(char *string, long errflg)
{
	/* EM_DEBUG_FUNC_BEGIN(); */
	
	switch ((short)errflg)  {
		case NIL:
			EM_DEBUG_LOG("IMAP_TOOLKIT_LOG NIL [%s]", string);
			break;
			
		case WARN:
			EM_DEBUG_LOG("IMAP_TOOLKIT_LOG WARN [%s]", string);
			break;
			
		case PARSE:
			EM_DEBUG_LOG("IMAP_TOOLKIT_LOG PARSE [%s]", string);
			break;
			
		case BYE:
			EM_DEBUG_LOG("IMAP_TOOLKIT_LOG BYE [%s]", string);
			break;
			
		case TCPDEBUG:
			EM_DEBUG_LOG("IMAP_TOOLKIT_LOG TCPDEBUG [%s]", string);
			break;
			
		case ERROR: {
			email_session_t *session = NULL;
			
			EM_DEBUG_EXCEPTION("IMAP_TOOLKIT_LOG ERROR [%s]", string);

			emcore_get_current_session(&session);
			
			if (session) {
				mm_get_error(string, &session->error);
				EM_DEBUG_EXCEPTION("IMAP_TOOLKIT_LOG ERROR [%d]", session->error);
			}
			
			/*  Handling exceptional case of connection failures.  */
			/*
			if (string) {
				if (strstr(string, "15 minute") != 0) {
					if (session)
						session->error = EMAIL_ERROR_LOGIN_ALLOWED_EVERY_15_MINS;
				}
				else if (strstr(string, "Too many login failures") == 0) {
					if (session)
						session->error = EMAIL_ERROR_TOO_MANY_LOGIN_FAILURE;
				}
			}
			*/
			
			break;
		}
	}
}

INTERNAL_FUNC void mm_searched(MAILSTREAM *stream, unsigned long number)
{
	EM_DEBUG_FUNC_BEGIN("stream [%p], number [%d]", stream, number);
	EM_DEBUG_FUNC_END();
}

INTERNAL_FUNC void mm_exists(MAILSTREAM *stream, unsigned long number)
{
	EM_DEBUG_FUNC_BEGIN("stream [%p], number [%d]", stream, number);
	EM_DEBUG_FUNC_END();
}

INTERNAL_FUNC void mm_expunged(MAILSTREAM *stream, unsigned long number)
{
	EM_DEBUG_FUNC_BEGIN("stream [%p], number [%d]", stream, number);
	EM_DEBUG_FUNC_END();
}

INTERNAL_FUNC void mm_flags(MAILSTREAM *stream, unsigned long number)
{
	EM_DEBUG_FUNC_BEGIN("stream [%p], number [%d]", stream, number);
	EM_DEBUG_FUNC_END();
}

INTERNAL_FUNC void mm_notify(MAILSTREAM *stream, char *string, long errflg)
{
	EM_DEBUG_FUNC_BEGIN();
	mm_log(string, errflg);
	EM_DEBUG_FUNC_END();
}

INTERNAL_FUNC void mm_critical(MAILSTREAM *stream)
{
	EM_DEBUG_FUNC_BEGIN("stream [%p]", stream);
	EM_DEBUG_FUNC_END();
}

INTERNAL_FUNC void mm_nocritical(MAILSTREAM *stream)
{
	EM_DEBUG_FUNC_BEGIN("stream [%p]", stream);
	EM_DEBUG_FUNC_END();
}

INTERNAL_FUNC long mm_diskerror(MAILSTREAM *stream, long errcode, long serious)
{
	EM_DEBUG_FUNC_BEGIN("stream [%p] errcode[%d] serious[%d]", stream, errcode, serious);
#if UNIXLIKE
	kill(getpid(), SIGSTOP);
#else
	abort();
#endif
	EM_DEBUG_FUNC_END();
	return NIL;
}

INTERNAL_FUNC void mm_fatal(char *string)
{
	EM_DEBUG_EXCEPTION("%s", string);
}

INTERNAL_FUNC void mm_get_error(char *string, int *err_code)
{
	if (!string || !err_code)
		return ;

	EM_DEBUG_LOG("string [%s]", string);

	if (strstr(string, "login failure") || strstr(string, "Login aborted") || strstr(string, "Can't login"))
		*err_code = EMAIL_ERROR_LOGIN_FAILURE;
	else if (strstr(string, "Scan not valid"))
		*err_code = EMAIL_ERROR_MAILBOX_FAILURE;
	else if (strstr(string, "Authentication cancelled"))
		*err_code = EMAIL_ERROR_AUTHENTICATE;
	else if (strstr(string, "authuser"))
		*err_code = EMAIL_ERROR_AUTH_NOT_SUPPORTED;
	else if (strstr(string, "negotiate TLS"))
		*err_code = EMAIL_ERROR_CANNOT_NEGOTIATE_TLS;
	else if (strstr(string, "TLS/SSL failure"))
		*err_code = EMAIL_ERROR_TLS_SSL_FAILURE;
	else if (strstr(string, "STARTLS"))
		*err_code = EMAIL_ERROR_STARTLS;
	else if (strstr(string, "TLS unavailable"))
		*err_code = EMAIL_ERROR_TLS_NOT_SUPPORTED;
	else if (strstr(string, "Can't access"))
		*err_code = EMAIL_ERROR_APPEND_FAILURE;
	else if (strstr(string, "Can not authenticate"))
		*err_code = EMAIL_ERROR_AUTHENTICATE;
	else if (strstr(string, "Unexpected IMAP response") || strstr(string, "hello"))
		*err_code = EMAIL_ERROR_INVALID_RESPONSE;
	else if (strstr(string, "NOTIMAP4REV1"))
		*err_code = EMAIL_ERROR_COMMAND_NOT_SUPPORTED;
	else if (strstr(string, "Anonymous"))
		*err_code = EMAIL_ERROR_ANNONYM_NOT_SUPPORTED;
	else if (strstr(string, "connection broken"))
		*err_code = EMAIL_ERROR_CONNECTION_BROKEN;
	else if (strstr(string, "SMTP greeting"))
		*err_code = EMAIL_ERROR_NO_RESPONSE;
	else if (strstr(string, "ESMTP failure"))
		*err_code = EMAIL_ERROR_SMTP_SEND_FAILURE;
	else if (strstr(string, "socket") || strstr(string, "Socket"))
		*err_code = EMAIL_ERROR_SOCKET_FAILURE;
	else if (strstr(string, "connect to") || strstr(string, "Connection failed"))
		*err_code = EMAIL_ERROR_CONNECTION_FAILURE;
	else if (strstr(string, "Certificate failure"))
		*err_code = EMAIL_ERROR_CERTIFICATE_FAILURE;
	else if (strstr(string, "ESMTP failure"))
		*err_code = EMAIL_ERROR_INVALID_PARAM;
	else if (strstr(string, "No such host"))
		*err_code = EMAIL_ERROR_NO_SUCH_HOST;
	else if (strstr(string, "host") || strstr(string, "Host"))
		*err_code = EMAIL_ERROR_INVALID_SERVER;
	else if (strstr(string, "SELECT failed"))
		*err_code = EMAIL_ERROR_MAILBOX_NOT_FOUND;
	else
		*err_code = EMAIL_ERROR_UNKNOWN;
}
/* EOF */