author | harlekin <harlekin> | 2003-12-08 13:37:32 (UTC) |
---|---|---|
committer | harlekin <harlekin> | 2003-12-08 13:37:32 (UTC) |
commit | 3b37149813d940535b12ba3b1edd938c41804a1b (patch) (unidiff) | |
tree | 0e35a3b28fb147514f22a6a6cb5aaf246e4c5670 /noncore/net/mail/imapwrapper.cpp | |
parent | 3d49fc2f7bba9c9e8856e5f4f7ec6c15ef84c252 (diff) | |
download | opie-3b37149813d940535b12ba3b1edd938c41804a1b.zip opie-3b37149813d940535b12ba3b1edd938c41804a1b.tar.gz opie-3b37149813d940535b12ba3b1edd938c41804a1b.tar.bz2 |
beginning of the new mailer
Diffstat (limited to 'noncore/net/mail/imapwrapper.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/net/mail/imapwrapper.cpp | 411 |
1 files changed, 411 insertions, 0 deletions
diff --git a/noncore/net/mail/imapwrapper.cpp b/noncore/net/mail/imapwrapper.cpp new file mode 100644 index 0000000..16f4565 --- a/dev/null +++ b/noncore/net/mail/imapwrapper.cpp | |||
@@ -0,0 +1,411 @@ | |||
1 | |||
2 | #include <stdlib.h> | ||
3 | |||
4 | #include "mailwrapper.h" | ||
5 | |||
6 | |||
7 | IMAPwrapper::IMAPwrapper( IMAPaccount *a ) | ||
8 | { | ||
9 | account = a; | ||
10 | } | ||
11 | |||
12 | void imap_progress( size_t current, size_t maximum ) | ||
13 | { | ||
14 | qDebug( "IMAP: %i of %i", current, maximum ); | ||
15 | } | ||
16 | |||
17 | void IMAPwrapper::listMessages(const QString&mailbox,Maillist&target ) | ||
18 | { | ||
19 | const char *server, *user, *pass, *mb; | ||
20 | uint16_t port; | ||
21 | int err = MAILIMAP_NO_ERROR; | ||
22 | clist *result; | ||
23 | clistcell *current; | ||
24 | mailimap_fetch_att *fetchAtt,*fetchAttFlags; | ||
25 | mailimap_fetch_type *fetchType; | ||
26 | mailimap_set *set; | ||
27 | |||
28 | mb = mailbox.latin1(); | ||
29 | server = account->getServer().latin1(); | ||
30 | port = account->getPort().toUInt(); | ||
31 | user = account->getUser().latin1(); | ||
32 | pass = account->getPassword().latin1(); | ||
33 | |||
34 | mailimap *imap = mailimap_new( 20, &imap_progress ); | ||
35 | if ( imap == NULL ) { | ||
36 | qDebug("IMAP Memory error"); | ||
37 | return; | ||
38 | } | ||
39 | |||
40 | /* connect */ | ||
41 | err = mailimap_socket_connect( imap, (char*)server, port ); | ||
42 | if ( err != MAILIMAP_NO_ERROR && | ||
43 | err != MAILIMAP_NO_ERROR_AUTHENTICATED && | ||
44 | err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) { | ||
45 | qDebug("error connecting server: %s",imap->response); | ||
46 | mailimap_free( imap ); | ||
47 | return; | ||
48 | } | ||
49 | |||
50 | /* login */ | ||
51 | err = mailimap_login_simple( imap, (char*)user, (char*)pass ); | ||
52 | if ( err != MAILIMAP_NO_ERROR ) { | ||
53 | qDebug("error logging in imap: %s",imap->response); | ||
54 | err = mailimap_close( imap ); | ||
55 | mailimap_free( imap ); | ||
56 | return; | ||
57 | } | ||
58 | |||
59 | /* select mailbox READONLY for operations */ | ||
60 | err = mailimap_examine( imap, (char*)mb); | ||
61 | if ( err != MAILIMAP_NO_ERROR ) { | ||
62 | qDebug("error selecting mailbox: %s",imap->response); | ||
63 | err = mailimap_logout( imap ); | ||
64 | err = mailimap_close( imap ); | ||
65 | mailimap_free( imap ); | ||
66 | return; | ||
67 | } | ||
68 | |||
69 | int last = imap->selection_info->exists; | ||
70 | if (last == 0) { | ||
71 | qDebug("mailbox has no mails"); | ||
72 | err = mailimap_logout( imap ); | ||
73 | err = mailimap_close( imap ); | ||
74 | mailimap_free( imap ); | ||
75 | return; | ||
76 | } | ||
77 | |||
78 | |||
79 | result = clist_new(); | ||
80 | /* the range has to start at 1!!! not with 0!!!! */ | ||
81 | set = mailimap_set_new_interval( 1, last ); | ||
82 | fetchAtt = mailimap_fetch_att_new_envelope(); | ||
83 | fetchAttFlags = mailimap_fetch_att_new_flags(); | ||
84 | |||
85 | //fetchType = mailimap_fetch_type_new_fetch_att(fetchAtt); | ||
86 | fetchType = mailimap_fetch_type_new_fetch_att_list_empty(); | ||
87 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,fetchAtt); | ||
88 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,fetchAttFlags); | ||
89 | |||
90 | err = mailimap_fetch( imap, set, fetchType, &result ); | ||
91 | mailimap_set_free( set ); | ||
92 | mailimap_fetch_type_free( fetchType ); | ||
93 | |||
94 | QString date,subject,from; | ||
95 | |||
96 | if ( err == MAILIMAP_NO_ERROR ) { | ||
97 | current = clist_begin(result); | ||
98 | mailimap_msg_att * msg_att; | ||
99 | int i = 0; | ||
100 | while ( current != 0 ) { | ||
101 | ++i; | ||
102 | msg_att = (mailimap_msg_att*)current->data; | ||
103 | RecMail*m = parse_list_result(msg_att); | ||
104 | if (m) { | ||
105 | m->setNumber(i); | ||
106 | target.append(m); | ||
107 | } | ||
108 | current = current->next; | ||
109 | } | ||
110 | } else { | ||
111 | qDebug("Error fetching headers: %s",imap->response); | ||
112 | } | ||
113 | |||
114 | err = mailimap_logout( imap ); | ||
115 | err = mailimap_close( imap ); | ||
116 | clist_free(result); | ||
117 | mailimap_free( imap ); | ||
118 | } | ||
119 | |||
120 | QList<IMAPFolder>* IMAPwrapper::listFolders() | ||
121 | { | ||
122 | const char *server, *user, *pass, *path, *mask; | ||
123 | uint16_t port; | ||
124 | int err = MAILIMAP_NO_ERROR; | ||
125 | clist *result; | ||
126 | clistcell *current; | ||
127 | |||
128 | QList<IMAPFolder> * folders = new QList<IMAPFolder>(); | ||
129 | folders->setAutoDelete( true ); | ||
130 | |||
131 | server = account->getServer().latin1(); | ||
132 | port = account->getPort().toUInt(); | ||
133 | user = account->getUser().latin1(); | ||
134 | pass = account->getPassword().latin1(); | ||
135 | path = account->getPrefix().latin1(); | ||
136 | |||
137 | mailimap *imap = mailimap_new( 20, &imap_progress ); | ||
138 | if ( imap == NULL ) { | ||
139 | qDebug("error mailimap_new"); | ||
140 | return folders; | ||
141 | } | ||
142 | |||
143 | err = mailimap_socket_connect( imap, (char*)server, port ); | ||
144 | if ( err != MAILIMAP_NO_ERROR && | ||
145 | err != MAILIMAP_NO_ERROR_AUTHENTICATED && | ||
146 | err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) { | ||
147 | mailimap_free(imap); | ||
148 | qDebug("error imap_socket_connect: %s",imap->response); | ||
149 | return folders; | ||
150 | } | ||
151 | |||
152 | err = mailimap_login_simple( imap, (char*)user, (char*)pass ); | ||
153 | if ( err != MAILIMAP_NO_ERROR ) { | ||
154 | mailimap_free(imap); | ||
155 | qDebug("error logging in: %s",imap->response); | ||
156 | return folders; | ||
157 | } | ||
158 | /* | ||
159 | * First we have to check for INBOX 'cause it sometimes it's not inside the path. | ||
160 | * We must not forget to filter them out in next loop! | ||
161 | * it seems like ugly code. and yes - it is ugly code. but the best way. | ||
162 | */ | ||
163 | QString temp; | ||
164 | mask = "INBOX" ; | ||
165 | result = clist_new(); | ||
166 | mailimap_mailbox_list *list; | ||
167 | err = mailimap_list( imap, (char*)"", (char*)mask, &result ); | ||
168 | if ( err == MAILIMAP_NO_ERROR ) { | ||
169 | current = result->first; | ||
170 | for ( int i = result->count; i > 0; i-- ) { | ||
171 | list = (mailimap_mailbox_list *) current->data; | ||
172 | // it is better use the deep copy mechanism of qt itself | ||
173 | // instead of using strdup! | ||
174 | temp = list->mb; | ||
175 | folders->append( new IMAPFolder(temp)); | ||
176 | current = current->next; | ||
177 | } | ||
178 | } else { | ||
179 | qDebug("error fetching folders: %s",imap->response); | ||
180 | } | ||
181 | mailimap_list_result_free( result ); | ||
182 | |||
183 | /* | ||
184 | * second stage - get the other then inbox folders | ||
185 | */ | ||
186 | mask = "*" ; | ||
187 | result = clist_new(); | ||
188 | err = mailimap_list( imap, (char*)path, (char*)mask, &result ); | ||
189 | if ( err == MAILIMAP_NO_ERROR ) { | ||
190 | current = result->first; | ||
191 | for ( int i = result->count; i > 0; i-- ) { | ||
192 | list = (mailimap_mailbox_list *) current->data; | ||
193 | // it is better use the deep copy mechanism of qt itself | ||
194 | // instead of using strdup! | ||
195 | temp = list->mb; | ||
196 | current = current->next; | ||
197 | if (temp.lower()=="inbox") | ||
198 | continue; | ||
199 | folders->append(new IMAPFolder(temp)); | ||
200 | |||
201 | } | ||
202 | } else { | ||
203 | qDebug("error fetching folders"); | ||
204 | } | ||
205 | mailimap_list_result_free( result ); | ||
206 | err = mailimap_logout( imap ); | ||
207 | err = mailimap_close( imap ); | ||
208 | mailimap_free( imap ); | ||
209 | return folders; | ||
210 | } | ||
211 | |||
212 | RecMail*IMAPwrapper::parse_list_result(mailimap_msg_att* m_att) | ||
213 | { | ||
214 | RecMail * m = 0; | ||
215 | mailimap_msg_att_item *item=0; | ||
216 | bool named_from = false; | ||
217 | QString from,date,subject; | ||
218 | date = from = subject = ""; | ||
219 | clistcell *current,*c,*cf, *current_from = NULL; | ||
220 | mailimap_address * current_address = NULL; | ||
221 | mailimap_msg_att_dynamic*flist; | ||
222 | mailimap_flag_fetch*cflag; | ||
223 | QBitArray mFlags(7); | ||
224 | |||
225 | if (!m_att) { | ||
226 | return m; | ||
227 | } | ||
228 | |||
229 | #if 0 | ||
230 | MAILIMAP_FLAG_KEYWORD, /* keyword flag */ | ||
231 | MAILIMAP_FLAG_EXTENSION, /* \extension flag */ | ||
232 | #endif | ||
233 | c = clist_begin(m_att->list); | ||
234 | while ( c ) { | ||
235 | current = c; | ||
236 | c = c->next; | ||
237 | item = (mailimap_msg_att_item*)current->data; | ||
238 | if (item->type!=MAILIMAP_MSG_ATT_ITEM_STATIC) { | ||
239 | flist = (mailimap_msg_att_dynamic*)item->msg_att_dyn; | ||
240 | if (!flist->list) { | ||
241 | continue; | ||
242 | } | ||
243 | cf = flist->list->first; | ||
244 | while (cf) { | ||
245 | cflag = (mailimap_flag_fetch*)cf->data; | ||
246 | if (cflag->type==MAILIMAP_FLAG_FETCH_OTHER && cflag->flag!=0) { | ||
247 | switch (cflag->flag->type) { | ||
248 | case MAILIMAP_FLAG_ANSWERED: /* \Answered flag */ | ||
249 | mFlags.setBit(FLAG_ANSWERED); | ||
250 | break; | ||
251 | case MAILIMAP_FLAG_FLAGGED: /* \Flagged flag */ | ||
252 | mFlags.setBit(FLAG_FLAGGED); | ||
253 | break; | ||
254 | case MAILIMAP_FLAG_DELETED: /* \Deleted flag */ | ||
255 | mFlags.setBit(FLAG_DELETED); | ||
256 | break; | ||
257 | case MAILIMAP_FLAG_SEEN: /* \Seen flag */ | ||
258 | mFlags.setBit(FLAG_SEEN); | ||
259 | break; | ||
260 | case MAILIMAP_FLAG_DRAFT: /* \Draft flag */ | ||
261 | mFlags.setBit(FLAG_DRAFT); | ||
262 | break; | ||
263 | case MAILIMAP_FLAG_KEYWORD: /* keyword flag */ | ||
264 | break; | ||
265 | case MAILIMAP_FLAG_EXTENSION: /* \extension flag */ | ||
266 | break; | ||
267 | default: | ||
268 | break; | ||
269 | } | ||
270 | } else if (cflag->type==MAILIMAP_FLAG_FETCH_RECENT) { | ||
271 | mFlags.setBit(FLAG_RECENT); | ||
272 | } | ||
273 | |||
274 | cf = cf->next; | ||
275 | } | ||
276 | continue; | ||
277 | } | ||
278 | if ( item->msg_att_static->type == MAILIMAP_MSG_ATT_RFC822_HEADER ) { | ||
279 | qDebug( "header: \n%s", item->msg_att_static->rfc822_header ); | ||
280 | } else if (item->msg_att_static->type==MAILIMAP_MSG_ATT_ENVELOPE) { | ||
281 | mailimap_envelope * head = item->msg_att_static->env; | ||
282 | date = head->date; | ||
283 | subject = head->subject; | ||
284 | if (head->from!=NULL) | ||
285 | current_from = head->from->list->first; | ||
286 | while (current_from != NULL) { | ||
287 | from = ""; | ||
288 | named_from = false; | ||
289 | current_address=(mailimap_address*)current_from->data; | ||
290 | current_from = current_from->next; | ||
291 | if (current_address->personal_name){ | ||
292 | from+=QString(current_address->personal_name); | ||
293 | from+=" "; | ||
294 | named_from = true; | ||
295 | } | ||
296 | if (named_from && (current_address->mailbox_name || current_address->host_name)) { | ||
297 | from+="<"; | ||
298 | } | ||
299 | if (current_address->mailbox_name) { | ||
300 | from+=QString(current_address->mailbox_name); | ||
301 | from+="@"; | ||
302 | } | ||
303 | if (current_address->host_name) { | ||
304 | from+=QString(current_address->host_name); | ||
305 | } | ||
306 | if (named_from && (current_address->mailbox_name || current_address->host_name)) { | ||
307 | from+=">"; | ||
308 | } | ||
309 | } | ||
310 | qDebug("header: \nFrom: %s\nSubject: %s\nDate: %s", | ||
311 | from.latin1(), | ||
312 | subject.latin1(),date.latin1()); | ||
313 | m = new RecMail(); | ||
314 | m->setSubject(subject); | ||
315 | m->setFrom(from); | ||
316 | m->setDate(date); | ||
317 | } else { | ||
318 | qDebug("Another type"); | ||
319 | } | ||
320 | } | ||
321 | /* msg is already deleted */ | ||
322 | if (mFlags.testBit(FLAG_DELETED) && m) { | ||
323 | delete m; | ||
324 | m = 0; | ||
325 | } | ||
326 | if (m) { | ||
327 | m->setFlags(mFlags); | ||
328 | } | ||
329 | return m; | ||
330 | } | ||
331 | |||
332 | QString IMAPwrapper::fetchBody(const QString & mailbox,const RecMail&mail) | ||
333 | { | ||
334 | QString body = ""; | ||
335 | const char *server, *user, *pass, *mb; | ||
336 | uint16_t port; | ||
337 | int err = MAILIMAP_NO_ERROR; | ||
338 | clist *result; | ||
339 | clistcell *current; | ||
340 | mailimap_fetch_att *fetchAtt; | ||
341 | mailimap_fetch_type *fetchType; | ||
342 | mailimap_set *set; | ||
343 | |||
344 | mb = mailbox.latin1(); | ||
345 | server = account->getServer().latin1(); | ||
346 | port = account->getPort().toUInt(); | ||
347 | user = account->getUser().latin1(); | ||
348 | pass = account->getPassword().latin1(); | ||
349 | |||
350 | mailimap *imap = mailimap_new( 20, &imap_progress ); | ||
351 | if ( imap == NULL ) { | ||
352 | qDebug("IMAP Memory error"); | ||
353 | return body; | ||
354 | } | ||
355 | |||
356 | /* connect */ | ||
357 | err = mailimap_socket_connect( imap, (char*)server, port ); | ||
358 | if ( err != MAILIMAP_NO_ERROR && | ||
359 | err != MAILIMAP_NO_ERROR_AUTHENTICATED && | ||
360 | err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) { | ||
361 | qDebug("error connecting server: %s",imap->response); | ||
362 | mailimap_free( imap ); | ||
363 | return body; | ||
364 | } | ||
365 | |||
366 | /* login */ | ||
367 | err = mailimap_login_simple( imap, (char*)user, (char*)pass ); | ||
368 | if ( err != MAILIMAP_NO_ERROR ) { | ||
369 | qDebug("error logging in imap: %s",imap->response); | ||
370 | err = mailimap_close( imap ); | ||
371 | mailimap_free( imap ); | ||
372 | return body; | ||
373 | } | ||
374 | |||
375 | /* select mailbox READONLY for operations */ | ||
376 | err = mailimap_examine( imap, (char*)mb); | ||
377 | if ( err != MAILIMAP_NO_ERROR ) { | ||
378 | qDebug("error selecting mailbox: %s",imap->response); | ||
379 | err = mailimap_logout( imap ); | ||
380 | err = mailimap_close( imap ); | ||
381 | mailimap_free( imap ); | ||
382 | return body; | ||
383 | } | ||
384 | result = clist_new(); | ||
385 | /* the range has to start at 1!!! not with 0!!!! */ | ||
386 | set = mailimap_set_new_interval( mail.getNumber(),mail.getNumber() ); | ||
387 | fetchAtt = mailimap_fetch_att_new_rfc822_text(); | ||
388 | fetchType = mailimap_fetch_type_new_fetch_att(fetchAtt); | ||
389 | err = mailimap_fetch( imap, set, fetchType, &result ); | ||
390 | mailimap_set_free( set ); | ||
391 | mailimap_fetch_type_free( fetchType ); | ||
392 | |||
393 | if (err == MAILIMAP_NO_ERROR && (current=clist_begin(result)) ) { | ||
394 | mailimap_msg_att * msg_att; | ||
395 | msg_att = (mailimap_msg_att*)current->data; | ||
396 | mailimap_msg_att_item*item = (mailimap_msg_att_item*)msg_att->list->first->data; | ||
397 | |||
398 | if (item->msg_att_static && item->msg_att_static->rfc822_text) { | ||
399 | body = item->msg_att_static->rfc822_text; | ||
400 | } | ||
401 | } else { | ||
402 | qDebug("error fetching text: %s",imap->response); | ||
403 | } | ||
404 | |||
405 | err = mailimap_logout( imap ); | ||
406 | err = mailimap_close( imap ); | ||
407 | mailimap_free( imap ); | ||
408 | clist_free(result); | ||
409 | |||
410 | return body; | ||
411 | } | ||