Diffstat (limited to 'noncore/net/mail/genericwrapper.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/mail/genericwrapper.cpp | 503 |
1 files changed, 0 insertions, 503 deletions
diff --git a/noncore/net/mail/genericwrapper.cpp b/noncore/net/mail/genericwrapper.cpp deleted file mode 100644 index 714396b..0000000 --- a/noncore/net/mail/genericwrapper.cpp +++ b/dev/null | |||
@@ -1,503 +0,0 @@ | |||
1 | #include "genericwrapper.h" | ||
2 | #include <libetpan/libetpan.h> | ||
3 | #include "mailtypes.h" | ||
4 | |||
5 | Genericwrapper::Genericwrapper() | ||
6 | : AbstractMail() | ||
7 | { | ||
8 | bodyCache.clear(); | ||
9 | } | ||
10 | |||
11 | Genericwrapper::~Genericwrapper() | ||
12 | { | ||
13 | cleanMimeCache(); | ||
14 | } | ||
15 | |||
16 | void Genericwrapper::fillSingleBody(RecPart&target,mailmessage*,mailmime*mime) | ||
17 | { | ||
18 | if (!mime) { | ||
19 | return; | ||
20 | } | ||
21 | mailmime_field*field = 0; | ||
22 | mailmime_single_fields fields; | ||
23 | memset(&fields, 0, sizeof(struct mailmime_single_fields)); | ||
24 | if (mime->mm_mime_fields != NULL) { | ||
25 | mailmime_single_fields_init(&fields, mime->mm_mime_fields, | ||
26 | mime->mm_content_type); | ||
27 | } | ||
28 | |||
29 | mailmime_content*type = fields.fld_content; | ||
30 | clistcell*current; | ||
31 | if (!type) { | ||
32 | target.setType("text"); | ||
33 | target.setSubtype("plain"); | ||
34 | } else { | ||
35 | target.setSubtype(type->ct_subtype); | ||
36 | switch(type->ct_type->tp_data.tp_discrete_type->dt_type) { | ||
37 | case MAILMIME_DISCRETE_TYPE_TEXT: | ||
38 | target.setType("text"); | ||
39 | break; | ||
40 | case MAILMIME_DISCRETE_TYPE_IMAGE: | ||
41 | target.setType("image"); | ||
42 | break; | ||
43 | case MAILMIME_DISCRETE_TYPE_AUDIO: | ||
44 | target.setType("audio"); | ||
45 | break; | ||
46 | case MAILMIME_DISCRETE_TYPE_VIDEO: | ||
47 | target.setType("video"); | ||
48 | break; | ||
49 | case MAILMIME_DISCRETE_TYPE_APPLICATION: | ||
50 | target.setType("application"); | ||
51 | break; | ||
52 | case MAILMIME_DISCRETE_TYPE_EXTENSION: | ||
53 | default: | ||
54 | if (type->ct_type->tp_data.tp_discrete_type->dt_extension) { | ||
55 | target.setType(type->ct_type->tp_data.tp_discrete_type->dt_extension); | ||
56 | } | ||
57 | break; | ||
58 | } | ||
59 | if (type->ct_parameters) { | ||
60 | fillParameters(target,type->ct_parameters); | ||
61 | } | ||
62 | } | ||
63 | if (mime->mm_mime_fields && mime->mm_mime_fields->fld_list) { | ||
64 | for (current=clist_begin(mime->mm_mime_fields->fld_list);current!=0;current=clist_next(current)) { | ||
65 | field = (mailmime_field*)current->data; | ||
66 | switch(field->fld_type) { | ||
67 | case MAILMIME_FIELD_TRANSFER_ENCODING: | ||
68 | target.setEncoding(getencoding(field->fld_data.fld_encoding)); | ||
69 | break; | ||
70 | case MAILMIME_FIELD_ID: | ||
71 | target.setIdentifier(field->fld_data.fld_id); | ||
72 | break; | ||
73 | case MAILMIME_FIELD_DESCRIPTION: | ||
74 | target.setDescription(field->fld_data.fld_description); | ||
75 | break; | ||
76 | default: | ||
77 | break; | ||
78 | } | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | |||
83 | void Genericwrapper::fillParameters(RecPart&target,clist*parameters) | ||
84 | { | ||
85 | if (!parameters) {return;} | ||
86 | clistcell*current=0; | ||
87 | mailmime_parameter*param; | ||
88 | for (current=clist_begin(parameters);current!=0;current=clist_next(current)) { | ||
89 | param = (mailmime_parameter*)current->data; | ||
90 | if (param) { | ||
91 | target.addParameter(QString(param->pa_name).lower(),QString(param->pa_value)); | ||
92 | } | ||
93 | } | ||
94 | } | ||
95 | |||
96 | QString Genericwrapper::getencoding(mailmime_mechanism*aEnc) | ||
97 | { | ||
98 | QString enc="7bit"; | ||
99 | if (!aEnc) return enc; | ||
100 | switch(aEnc->enc_type) { | ||
101 | case MAILMIME_MECHANISM_7BIT: | ||
102 | enc = "7bit"; | ||
103 | break; | ||
104 | case MAILMIME_MECHANISM_8BIT: | ||
105 | enc = "8bit"; | ||
106 | break; | ||
107 | case MAILMIME_MECHANISM_BINARY: | ||
108 | enc = "binary"; | ||
109 | break; | ||
110 | case MAILMIME_MECHANISM_QUOTED_PRINTABLE: | ||
111 | enc = "quoted-printable"; | ||
112 | break; | ||
113 | case MAILMIME_MECHANISM_BASE64: | ||
114 | enc = "base64"; | ||
115 | break; | ||
116 | case MAILMIME_MECHANISM_TOKEN: | ||
117 | default: | ||
118 | if (aEnc->enc_token) { | ||
119 | enc = QString(aEnc->enc_token); | ||
120 | } | ||
121 | break; | ||
122 | } | ||
123 | return enc; | ||
124 | } | ||
125 | |||
126 | void Genericwrapper::traverseBody(RecBody&target,mailmessage*message,mailmime*mime,QValueList<int>recList,unsigned int current_rec,int current_count) | ||
127 | { | ||
128 | if (current_rec >= 10) { | ||
129 | qDebug("too deep recursion!"); | ||
130 | } | ||
131 | if (!message || !mime) { | ||
132 | return; | ||
133 | } | ||
134 | int r; | ||
135 | char*data = 0; | ||
136 | size_t len; | ||
137 | clistiter * cur = 0; | ||
138 | QString b; | ||
139 | RecPart part; | ||
140 | |||
141 | switch (mime->mm_type) { | ||
142 | case MAILMIME_SINGLE: | ||
143 | { | ||
144 | QValueList<int>countlist = recList; | ||
145 | countlist.append(current_count); | ||
146 | r = mailmessage_fetch_section(message,mime,&data,&len); | ||
147 | part.setSize(len); | ||
148 | part.setPositionlist(countlist); | ||
149 | b = gen_attachment_id(); | ||
150 | part.setIdentifier(b); | ||
151 | fillSingleBody(part,message,mime); | ||
152 | if (part.Type()=="text" && target.Bodytext().isNull()) { | ||
153 | encodedString*r = new encodedString(); | ||
154 | r->setContent(data,len); | ||
155 | encodedString*res = decode_String(r,part.Encoding()); | ||
156 | if (countlist.count()>2) { | ||
157 | bodyCache[b]=r; | ||
158 | target.addPart(part); | ||
159 | } else { | ||
160 | delete r; | ||
161 | } | ||
162 | b = QString(res->Content()); | ||
163 | delete res; | ||
164 | target.setBodytext(b); | ||
165 | target.setDescription(part); | ||
166 | } else { | ||
167 | bodyCache[b]=new encodedString(data,len); | ||
168 | target.addPart(part); | ||
169 | } | ||
170 | } | ||
171 | break; | ||
172 | case MAILMIME_MULTIPLE: | ||
173 | { | ||
174 | unsigned int ccount = 1; | ||
175 | mailmime*cbody=0; | ||
176 | QValueList<int>countlist = recList; | ||
177 | for (cur = clist_begin(mime->mm_data.mm_multipart.mm_mp_list) ; cur != NULL ; cur = clist_next(cur)) { | ||
178 | cbody = (mailmime*)clist_content(cur); | ||
179 | if (cbody->mm_type==MAILMIME_MULTIPLE) { | ||
180 | RecPart targetPart; | ||
181 | targetPart.setType("multipart"); | ||
182 | countlist.append(current_count); | ||
183 | targetPart.setPositionlist(countlist); | ||
184 | target.addPart(targetPart); | ||
185 | } | ||
186 | traverseBody(target,message, cbody,countlist,current_rec+1,ccount); | ||
187 | if (cbody->mm_type==MAILMIME_MULTIPLE) { | ||
188 | countlist = recList; | ||
189 | } | ||
190 | ++ccount; | ||
191 | } | ||
192 | } | ||
193 | break; | ||
194 | case MAILMIME_MESSAGE: | ||
195 | { | ||
196 | QValueList<int>countlist = recList; | ||
197 | countlist.append(current_count); | ||
198 | /* the own header is always at recursion 0 - we don't need that */ | ||
199 | if (current_rec > 0) { | ||
200 | part.setPositionlist(countlist); | ||
201 | r = mailmessage_fetch_section(message,mime,&data,&len); | ||
202 | part.setSize(len); | ||
203 | part.setPositionlist(countlist); | ||
204 | b = gen_attachment_id(); | ||
205 | part.setIdentifier(b); | ||
206 | part.setType("message"); | ||
207 | part.setSubtype("rfc822"); | ||
208 | bodyCache[b]=new encodedString(data,len); | ||
209 | target.addPart(part); | ||
210 | } | ||
211 | if (mime->mm_data.mm_message.mm_msg_mime != NULL) { | ||
212 | traverseBody(target,message,mime->mm_data.mm_message.mm_msg_mime,countlist,current_rec+1); | ||
213 | } | ||
214 | } | ||
215 | break; | ||
216 | } | ||
217 | } | ||
218 | |||
219 | RecBody Genericwrapper::parseMail( mailmessage * msg ) | ||
220 | { | ||
221 | int err = MAILIMF_NO_ERROR; | ||
222 | mailmime_single_fields fields; | ||
223 | /* is bound to msg and will be freed there */ | ||
224 | mailmime * mime=0; | ||
225 | RecBody body; | ||
226 | memset(&fields, 0, sizeof(struct mailmime_single_fields)); | ||
227 | err = mailmessage_get_bodystructure(msg,&mime); | ||
228 | QValueList<int>recList; | ||
229 | traverseBody(body,msg,mime,recList); | ||
230 | return body; | ||
231 | } | ||
232 | |||
233 | RecMail *Genericwrapper::parseHeader( const char *header ) | ||
234 | { | ||
235 | int err = MAILIMF_NO_ERROR; | ||
236 | size_t curTok = 0; | ||
237 | RecMail *mail = new RecMail(); | ||
238 | mailimf_fields *fields = 0; | ||
239 | mailimf_references * refs = 0; | ||
240 | mailimf_keywords*keys = 0; | ||
241 | QString status; | ||
242 | QString value; | ||
243 | QBitArray mFlags(7); | ||
244 | |||
245 | err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields ); | ||
246 | for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) { | ||
247 | mailimf_field *field = (mailimf_field *) current->data; | ||
248 | switch ( field->fld_type ) { | ||
249 | case MAILIMF_FIELD_FROM: | ||
250 | mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) ); | ||
251 | break; | ||
252 | case MAILIMF_FIELD_TO: | ||
253 | mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) ); | ||
254 | break; | ||
255 | case MAILIMF_FIELD_CC: | ||
256 | mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) ); | ||
257 | break; | ||
258 | case MAILIMF_FIELD_BCC: | ||
259 | mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) ); | ||
260 | break; | ||
261 | case MAILIMF_FIELD_SUBJECT: | ||
262 | mail->setSubject(convert_String( field->fld_data.fld_subject->sbj_value ) ); | ||
263 | break; | ||
264 | case MAILIMF_FIELD_ORIG_DATE: | ||
265 | mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) ); | ||
266 | break; | ||
267 | case MAILIMF_FIELD_MESSAGE_ID: | ||
268 | mail->setMsgid(QString(field->fld_data.fld_message_id->mid_value)); | ||
269 | break; | ||
270 | case MAILIMF_FIELD_REFERENCES: | ||
271 | refs = field->fld_data.fld_references; | ||
272 | if (refs && refs->mid_list && clist_count(refs->mid_list)) { | ||
273 | char * text = (char*)refs->mid_list->first->data; | ||
274 | mail->setReplyto(QString(text)); | ||
275 | } | ||
276 | break; | ||
277 | case MAILIMF_FIELD_KEYWORDS: | ||
278 | keys = field->fld_data.fld_keywords; | ||
279 | for (clistcell*cur = clist_begin(keys->kw_list);cur!=0;cur=clist_next(cur)) { | ||
280 | qDebug("Keyword: %s",(char*)cur->data); | ||
281 | } | ||
282 | break; | ||
283 | case MAILIMF_FIELD_OPTIONAL_FIELD: | ||
284 | status = field->fld_data.fld_optional_field->fld_name; | ||
285 | value = field->fld_data.fld_optional_field->fld_value; | ||
286 | if (status.lower()=="status") { | ||
287 | if (value.lower()=="ro") { | ||
288 | mFlags.setBit(FLAG_SEEN); | ||
289 | } | ||
290 | } else if (status.lower()=="x-status") { | ||
291 | qDebug("X-Status: %s",value.latin1()); | ||
292 | if (value.lower()=="a") { | ||
293 | mFlags.setBit(FLAG_ANSWERED); | ||
294 | } | ||
295 | } else { | ||
296 | // qDebug("Optionales feld: %s -> %s)",field->fld_data.fld_optional_field->fld_name, | ||
297 | // field->fld_data.fld_optional_field->fld_value); | ||
298 | } | ||
299 | break; | ||
300 | default: | ||
301 | qDebug("Non parsed field"); | ||
302 | break; | ||
303 | } | ||
304 | } | ||
305 | if (fields) mailimf_fields_free(fields); | ||
306 | mail->setFlags(mFlags); | ||
307 | return mail; | ||
308 | } | ||
309 | |||
310 | QString Genericwrapper::parseDateTime( mailimf_date_time *date ) | ||
311 | { | ||
312 | char tmp[23]; | ||
313 | |||
314 | snprintf( tmp, 23, "%02i.%02i.%04i %02i:%02i:%02i %+05i", | ||
315 | date->dt_day, date->dt_month, date->dt_year, date->dt_hour, date->dt_min, date->dt_sec, date->dt_zone ); | ||
316 | |||
317 | return QString( tmp ); | ||
318 | } | ||
319 | |||
320 | QString Genericwrapper::parseAddressList( mailimf_address_list *list ) | ||
321 | { | ||
322 | QString result( "" ); | ||
323 | |||
324 | bool first = true; | ||
325 | if (list == 0) return result; | ||
326 | for ( clistiter *current = clist_begin( list->ad_list ); current != NULL; current = current->next ) { | ||
327 | mailimf_address *addr = (mailimf_address *) current->data; | ||
328 | |||
329 | if ( !first ) { | ||
330 | result.append( "," ); | ||
331 | } else { | ||
332 | first = false; | ||
333 | } | ||
334 | |||
335 | switch ( addr->ad_type ) { | ||
336 | case MAILIMF_ADDRESS_MAILBOX: | ||
337 | result.append( parseMailbox( addr->ad_data.ad_mailbox ) ); | ||
338 | break; | ||
339 | case MAILIMF_ADDRESS_GROUP: | ||
340 | result.append( parseGroup( addr->ad_data.ad_group ) ); | ||
341 | break; | ||
342 | default: | ||
343 | qDebug( "Generic: unkown mailimf address type" ); | ||
344 | break; | ||
345 | } | ||
346 | } | ||
347 | |||
348 | return result; | ||
349 | } | ||
350 | |||
351 | QString Genericwrapper::parseGroup( mailimf_group *group ) | ||
352 | { | ||
353 | QString result( "" ); | ||
354 | |||
355 | result.append( group->grp_display_name ); | ||
356 | result.append( ": " ); | ||
357 | |||
358 | if ( group->grp_mb_list != NULL ) { | ||
359 | result.append( parseMailboxList( group->grp_mb_list ) ); | ||
360 | } | ||
361 | |||
362 | result.append( ";" ); | ||
363 | |||
364 | return result; | ||
365 | } | ||
366 | |||
367 | QString Genericwrapper::parseMailbox( mailimf_mailbox *box ) | ||
368 | { | ||
369 | QString result( "" ); | ||
370 | |||
371 | if ( box->mb_display_name == NULL ) { | ||
372 | result.append( box->mb_addr_spec ); | ||
373 | } else { | ||
374 | result.append( convert_String(box->mb_display_name).latin1() ); | ||
375 | result.append( " <" ); | ||
376 | result.append( box->mb_addr_spec ); | ||
377 | result.append( ">" ); | ||
378 | } | ||
379 | |||
380 | return result; | ||
381 | } | ||
382 | |||
383 | QString Genericwrapper::parseMailboxList( mailimf_mailbox_list *list ) | ||
384 | { | ||
385 | QString result( "" ); | ||
386 | |||
387 | bool first = true; | ||
388 | for ( clistiter *current = clist_begin( list->mb_list ); current != NULL; current = current->next ) { | ||
389 | mailimf_mailbox *box = (mailimf_mailbox *) current->data; | ||
390 | |||
391 | if ( !first ) { | ||
392 | result.append( "," ); | ||
393 | } else { | ||
394 | first = false; | ||
395 | } | ||
396 | |||
397 | result.append( parseMailbox( box ) ); | ||
398 | } | ||
399 | |||
400 | return result; | ||
401 | } | ||
402 | |||
403 | encodedString* Genericwrapper::fetchDecodedPart(const RecMail&,const RecPart&part) | ||
404 | { | ||
405 | QMap<QString,encodedString*>::ConstIterator it = bodyCache.find(part.Identifier()); | ||
406 | if (it==bodyCache.end()) return new encodedString(); | ||
407 | encodedString*t = decode_String(it.data(),part.Encoding()); | ||
408 | return t; | ||
409 | } | ||
410 | |||
411 | encodedString* Genericwrapper::fetchRawPart(const RecMail&mail,const RecPart&part) | ||
412 | { | ||
413 | QMap<QString,encodedString*>::ConstIterator it = bodyCache.find(part.Identifier()); | ||
414 | if (it==bodyCache.end()) return new encodedString(); | ||
415 | encodedString*t = it.data(); | ||
416 | return t; | ||
417 | } | ||
418 | |||
419 | QString Genericwrapper::fetchTextPart(const RecMail&mail,const RecPart&part) | ||
420 | { | ||
421 | encodedString*t = fetchDecodedPart(mail,part); | ||
422 | QString text=t->Content(); | ||
423 | delete t; | ||
424 | return text; | ||
425 | } | ||
426 | |||
427 | void Genericwrapper::cleanMimeCache() | ||
428 | { | ||
429 | QMap<QString,encodedString*>::Iterator it = bodyCache.begin(); | ||
430 | for (;it!=bodyCache.end();++it) { | ||
431 | encodedString*t = it.data(); | ||
432 | //it.setValue(0); | ||
433 | if (t) delete t; | ||
434 | } | ||
435 | bodyCache.clear(); | ||
436 | qDebug("Genericwrapper: cache cleaned"); | ||
437 | } | ||
438 | |||
439 | void Genericwrapper::parseList(QList<RecMail> &target,mailsession*session,const QString&mailbox) | ||
440 | { | ||
441 | int r; | ||
442 | mailmessage_list * env_list = 0; | ||
443 | r = mailsession_get_messages_list(session,&env_list); | ||
444 | if (r != MAIL_NO_ERROR) { | ||
445 | qDebug("Error message list"); | ||
446 | return; | ||
447 | } | ||
448 | r = mailsession_get_envelopes_list(session, env_list); | ||
449 | if (r != MAIL_NO_ERROR) { | ||
450 | qDebug("Error filling message list"); | ||
451 | if (env_list) { | ||
452 | mailmessage_list_free(env_list); | ||
453 | } | ||
454 | return; | ||
455 | } | ||
456 | mailimf_references * refs; | ||
457 | uint32_t i = 0; | ||
458 | for(; i < carray_count(env_list->msg_tab) ; ++i) { | ||
459 | mailmessage * msg; | ||
460 | QBitArray mFlags(7); | ||
461 | msg = (mailmessage*)carray_get(env_list->msg_tab, i); | ||
462 | if (msg->msg_fields == NULL) { | ||
463 | qDebug("could not fetch envelope of message %i", i); | ||
464 | continue; | ||
465 | } | ||
466 | RecMail * mail = new RecMail(); | ||
467 | mail->setWrapper(this); | ||
468 | mail_flags * flag_result = 0; | ||
469 | r = mailmessage_get_flags(msg,&flag_result); | ||
470 | if (r == MAIL_ERROR_NOT_IMPLEMENTED) { | ||
471 | mFlags.setBit(FLAG_SEEN); | ||
472 | } | ||
473 | mailimf_single_fields single_fields; | ||
474 | mailimf_single_fields_init(&single_fields, msg->msg_fields); | ||
475 | mail->setMsgsize(msg->msg_size); | ||
476 | mail->setFlags(mFlags); | ||
477 | mail->setMbox(mailbox); | ||
478 | mail->setNumber(msg->msg_index); | ||
479 | if (single_fields.fld_subject) | ||
480 | mail->setSubject( convert_String(single_fields.fld_subject->sbj_value)); | ||
481 | if (single_fields.fld_from) | ||
482 | mail->setFrom(parseMailboxList(single_fields.fld_from->frm_mb_list)); | ||
483 | if (single_fields.fld_to) | ||
484 | mail->setTo( parseAddressList( single_fields.fld_to->to_addr_list ) ); | ||
485 | if (single_fields.fld_cc) | ||
486 | mail->setCC( parseAddressList( single_fields.fld_cc->cc_addr_list ) ); | ||
487 | if (single_fields.fld_bcc) | ||
488 | mail->setBcc( parseAddressList( single_fields.fld_bcc->bcc_addr_list ) ); | ||
489 | if (single_fields.fld_orig_date) | ||
490 | mail->setDate( parseDateTime( single_fields.fld_orig_date->dt_date_time ) ); | ||
491 | if (single_fields.fld_message_id->mid_value) | ||
492 | mail->setMsgid(QString(single_fields.fld_message_id->mid_value)); | ||
493 | refs = single_fields.fld_references; | ||
494 | if (refs && refs->mid_list && clist_count(refs->mid_list)) { | ||
495 | char * text = (char*)refs->mid_list->first->data; | ||
496 | mail->setReplyto(QString(text)); | ||
497 | } | ||
498 | target.append(mail); | ||
499 | } | ||
500 | if (env_list) { | ||
501 | mailmessage_list_free(env_list); | ||
502 | } | ||
503 | } | ||