forked from MailCore/MailCore
/
MailCoreUtilities.m
314 lines (293 loc) · 10.2 KB
/
MailCoreUtilities.m
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
/*
* MailCore
*
* Copyright (C) 2007 - Matt Ronge
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the MailCore project nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#import "MailCoreUtilities.h"
#import "MailCoreTypes.h"
/* direction is 1 for send, 0 for receive, -1 when it does not apply */
void mailcore_logger(int direction, const char * str, size_t size) {
char *str2 = malloc(size+1);
strncpy(str2,str,size);
str2[size] = 0;
if (direction == 1) {
printf("%s\n", str2);
}
else if (direction == 0) {
printf("%s\n", str2);
}
else {
printf("%s\n", str2);
}
free(str2);
}
void MailCoreEnableLogging() {
mailstream_debug = 1;
mailstream_logger = mailcore_logger;
}
void MailCoreDisableLogging() {
mailstream_debug = 0;
mailstream_logger = nil;
}
NSError* MailCoreCreateError(int errcode, NSString *description) {
NSMutableDictionary *errorDetail = [NSMutableDictionary dictionary];
[errorDetail setValue:description forKey:NSLocalizedDescriptionKey];
return [NSError errorWithDomain:@"mailcore" code:errcode userInfo:errorDetail];
}
NSError* MailCoreCreateErrorFromSMTPCode(int errcode) {
NSString *description = @"Unknown error";
switch (errcode) {
case MAILSMTP_ERROR_AUTH_LOGIN:
description = @"Invalid username or password";
break;
default: {
const char *errStr = mailsmtp_strerror(errcode);
if (errStr) {
description = [[[NSString alloc] initWithCString:errStr encoding:NSUTF8StringEncoding] autorelease];
}
break;
}
}
return MailCoreCreateError(errcode, description);
}
NSError* MailCoreCreateErrorFromIMAPCode(int errcode) {
NSString *description = @"";
switch (errcode) {
case MAILIMAP_ERROR_BAD_STATE:
description = @"Bad state";
break;
case MAILIMAP_ERROR_STREAM:
description = @"Stream error"; // Used to be @"Lost connection"
break;
case MAILIMAP_ERROR_PARSE:
description = @"Parse error";
break;
case MAILIMAP_ERROR_CONNECTION_REFUSED:
description = @"Connection refused";
break;
case MAILIMAP_ERROR_MEMORY:
description = @"Memory Error";
break;
case MAILIMAP_ERROR_FATAL:
description = @"IMAP connection lost"; // I renamed this to calm users
break;
case MAILIMAP_ERROR_PROTOCOL:
description = @"Protocol Error";
break;
case MAILIMAP_ERROR_DONT_ACCEPT_CONNECTION:
description = @"Connection not accepted";
break;
case MAILIMAP_ERROR_APPEND:
description = @"Append error";
break;
case MAILIMAP_ERROR_NOOP:
description = @"NOOP error";
break;
case MAILIMAP_ERROR_LOGOUT:
description = @"Logout error";
break;
case MAILIMAP_ERROR_CAPABILITY:
description = @"Capability error";
break;
case MAILIMAP_ERROR_CHECK:
description = @"Check command error";
break;
case MAILIMAP_ERROR_CLOSE:
description = @"Close command error";
break;
case MAILIMAP_ERROR_EXPUNGE:
description = @"Expunge command error";
break;
case MAILIMAP_ERROR_COPY:
description = @"Copy command error";
break;
case MAILIMAP_ERROR_UID_COPY:
description = @"UID copy command error";
break;
case MAILIMAP_ERROR_CREATE:
description = @"Create command error";
break;
case MAILIMAP_ERROR_DELETE:
description = @"Delete error";
break;
case MAILIMAP_ERROR_EXAMINE:
description = @"Examine command error";
break;
case MAILIMAP_ERROR_FETCH:
description = @"Fetch command error";
break;
case MAILIMAP_ERROR_UID_FETCH:
description = @"UID fetch command error";
break;
case MAILIMAP_ERROR_LIST:
description = @"List command error";
break;
case MAILIMAP_ERROR_LOGIN:
description = @"Login error";
break;
case MAILIMAP_ERROR_LSUB:
description = @"Lsub error";
break;
case MAILIMAP_ERROR_RENAME:
description = @"Rename error";
break;
case MAILIMAP_ERROR_SEARCH:
description = @"Search error";
break;
case MAILIMAP_ERROR_UID_SEARCH:
description = @"Uid search error";
break;
case MAILIMAP_ERROR_SELECT:
description = @"Select cmnd error";
break;
case MAILIMAP_ERROR_STATUS:
description = @"Status cmnd error";
break;
case MAILIMAP_ERROR_STORE:
description = @"Store cmnd error";
break;
case MAILIMAP_ERROR_UID_STORE:
description = @"Uid store cmd error";
break;
case MAILIMAP_ERROR_SUBSCRIBE:
description = @"Subscribe error";
break;
case MAILIMAP_ERROR_UNSUBSCRIBE:
description = @"Unsubscribe error";
break;
case MAILIMAP_ERROR_STARTTLS:
description = @"StartTLS error";
break;
case MAILIMAP_ERROR_INVAL:
description = @"Inval cmd error";
break;
case MAILIMAP_ERROR_EXTENSION:
description = @"Extension error";
break;
case MAILIMAP_ERROR_SASL:
description = @"SASL error";
break;
case MAILIMAP_ERROR_SSL:
description = @"SSL error";
break;
// the following are from maildriver_errors.h
case MAIL_ERROR_PROTOCOL:
description = @"Protocol error";
break;
case MAIL_ERROR_CAPABILITY:
description = @"Capability error";
break;
case MAIL_ERROR_CLOSE:
description = @"Close error";
break;
case MAIL_ERROR_FATAL:
description = @"Fatal error";
break;
case MAIL_ERROR_READONLY:
description = @"Readonly error";
break;
case MAIL_ERROR_NO_APOP:
description = @"No APOP error";
break;
case MAIL_ERROR_COMMAND_NOT_SUPPORTED:
description = @"Cmd not supported";
break;
case MAIL_ERROR_NO_PERMISSION:
description = @"No permission";
break;
case MAIL_ERROR_PROGRAM_ERROR:
description = @"Program error";
break;
case MAIL_ERROR_SUBJECT_NOT_FOUND:
description = @"Subject not found";
break;
case MAIL_ERROR_CHAR_ENCODING_FAILED:
description = @"Encoding failed";
break;
case MAIL_ERROR_SEND:
description = @"Send error";
break;
case MAIL_ERROR_COMMAND:
description = @"Command error";
break;
case MAIL_ERROR_SYSTEM:
description = @"System error";
break;
case MAIL_ERROR_UNABLE:
description = @"Unable error";
break;
case MAIL_ERROR_FOLDER:
description = @"Folder errror";
break;
default:
description = [NSString stringWithFormat:@"Error: %d", errcode];
break;
}
return MailCoreCreateError(errcode, description);
}
NSString *MailCoreDecodeMIMEPhrase(char *data) {
int err;
size_t currToken = 0;
char *decodedSubject;
NSString *result;
if (data && *data != '\0') {
err = mailmime_encoded_phrase_parse(DEST_CHARSET, data, strlen(data),
&currToken, DEST_CHARSET, &decodedSubject);
if (err != MAILIMF_NO_ERROR) {
if (decodedSubject == NULL)
free(decodedSubject);
return nil;
}
} else {
return @"";
}
result = [NSString stringWithCString:decodedSubject encoding:NSUTF8StringEncoding];
free(decodedSubject);
return result;
}
NSArray * MailCoreStringArrayFromClist(clist *list) {
clistiter *iter;
NSMutableArray *stringSet = [NSMutableArray array];
char *string;
if(list == NULL)
return stringSet;
for(iter = clist_begin(list); iter != NULL; iter = clist_next(iter)) {
string = clist_content(iter);
NSString *strObj = [[NSString alloc] initWithUTF8String:string];
[stringSet addObject:strObj];
[strObj release];
}
return stringSet;
}
clist *MailCoreClistFromStringArray(NSArray *strings) {
clist * str_list = clist_new();
for (NSString *str in strings) {
clist_append(str_list, strdup([str UTF8String]));
}
return str_list;
}