Diffstat (limited to 'noncore/net/mail/libmailwrapper/smtpwrapper.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/net/mail/libmailwrapper/smtpwrapper.cpp | 460 |
1 files changed, 8 insertions, 452 deletions
diff --git a/noncore/net/mail/libmailwrapper/smtpwrapper.cpp b/noncore/net/mail/libmailwrapper/smtpwrapper.cpp index 63acfd5..a4e0beb 100644 --- a/noncore/net/mail/libmailwrapper/smtpwrapper.cpp +++ b/noncore/net/mail/libmailwrapper/smtpwrapper.cpp | |||
@@ -1,15 +1,2 @@ | |||
1 | #include <stdlib.h> | ||
2 | #include <sys/stat.h> | ||
3 | #include <sys/types.h> | ||
4 | #include <unistd.h> | ||
5 | #include <fcntl.h> | ||
6 | #include <string.h> | ||
7 | #include <qt.h> | ||
8 | |||
9 | #include <qpe/config.h> | ||
10 | #include <qpe/qcopenvelope_qws.h> | ||
11 | |||
12 | #include <libetpan/libetpan.h> | ||
13 | |||
14 | #include "smtpwrapper.h" | 1 | #include "smtpwrapper.h" |
15 | #include "mailwrapper.h" | 2 | #include "mailwrapper.h" |
@@ -19,10 +6,16 @@ | |||
19 | #include "sendmailprogress.h" | 6 | #include "sendmailprogress.h" |
20 | 7 | ||
21 | const char* SMTPwrapper::USER_AGENT="OpieMail v0.4"; | 8 | #include <qt.h> |
9 | |||
10 | #include <qpe/config.h> | ||
11 | #include <qpe/qcopenvelope_qws.h> | ||
12 | |||
13 | #include <libetpan/libetpan.h> | ||
14 | |||
22 | 15 | ||
23 | progressMailSend*SMTPwrapper::sendProgress = 0; | 16 | progressMailSend*SMTPwrapper::sendProgress = 0; |
24 | 17 | ||
25 | SMTPwrapper::SMTPwrapper(SMTPaccount * aSmtp ) | 18 | SMTPwrapper::SMTPwrapper(SMTPaccount * aSmtp ) |
26 | : QObject() | 19 | : Generatemail() |
27 | { | 20 | { |
28 | m_SmtpAccount = aSmtp; | 21 | m_SmtpAccount = aSmtp; |
@@ -88,441 +81,4 @@ QString SMTPwrapper::mailsmtpError( int errnum ) { | |||
88 | } | 81 | } |
89 | 82 | ||
90 | mailimf_mailbox *SMTPwrapper::newMailbox(const QString&name, const QString&mail ) { | ||
91 | return mailimf_mailbox_new( strdup( name.latin1() ), | ||
92 | strdup( mail.latin1() ) ); | ||
93 | } | ||
94 | |||
95 | mailimf_address_list *SMTPwrapper::parseAddresses(const QString&addr ) { | ||
96 | mailimf_address_list *addresses; | ||
97 | |||
98 | if ( addr.isEmpty() ) | ||
99 | return NULL; | ||
100 | |||
101 | addresses = mailimf_address_list_new_empty(); | ||
102 | |||
103 | bool literal_open = false; | ||
104 | unsigned int startpos = 0; | ||
105 | QStringList list; | ||
106 | QString s; | ||
107 | unsigned int i = 0; | ||
108 | for (; i < addr.length();++i) { | ||
109 | switch (addr[i]) { | ||
110 | case '\"': | ||
111 | literal_open = !literal_open; | ||
112 | break; | ||
113 | case ',': | ||
114 | if (!literal_open) { | ||
115 | s = addr.mid(startpos,i-startpos); | ||
116 | if (!s.isEmpty()) { | ||
117 | list.append(s); | ||
118 | qDebug("Appended %s",s.latin1()); | ||
119 | } | ||
120 | // !!!! this is a MUST BE! | ||
121 | startpos = ++i; | ||
122 | } | ||
123 | break; | ||
124 | default: | ||
125 | break; | ||
126 | } | ||
127 | } | ||
128 | s = addr.mid(startpos,i-startpos); | ||
129 | if (!s.isEmpty()) { | ||
130 | list.append(s); | ||
131 | qDebug("Appended %s",s.latin1()); | ||
132 | } | ||
133 | QStringList::Iterator it; | ||
134 | for ( it = list.begin(); it != list.end(); it++ ) { | ||
135 | int err = mailimf_address_list_add_parse( addresses, (char*)(*it).latin1() ); | ||
136 | if ( err != MAILIMF_NO_ERROR ) { | ||
137 | qDebug( "Error parsing" ); | ||
138 | qDebug( *it ); | ||
139 | } else { | ||
140 | qDebug( "Parse success! %s",(*it).latin1()); | ||
141 | } | ||
142 | } | ||
143 | return addresses; | ||
144 | } | ||
145 | |||
146 | mailimf_fields *SMTPwrapper::createImfFields(const Mail&mail ) { | ||
147 | mailimf_fields *fields; | ||
148 | mailimf_field *xmailer; | ||
149 | mailimf_mailbox *sender=0,*fromBox=0; | ||
150 | mailimf_mailbox_list *from=0; | ||
151 | mailimf_address_list *to=0, *cc=0, *bcc=0, *reply=0; | ||
152 | clist*in_reply_to = 0; | ||
153 | char *subject = strdup( mail.getSubject().latin1() ); | ||
154 | int err; | ||
155 | |||
156 | sender = newMailbox( mail.getName(), mail.getMail() ); | ||
157 | if ( sender == NULL ) | ||
158 | goto err_free; | ||
159 | |||
160 | fromBox = newMailbox( mail.getName(), mail.getMail() ); | ||
161 | if ( fromBox == NULL ) | ||
162 | goto err_free_sender; | ||
163 | |||
164 | from = mailimf_mailbox_list_new_empty(); | ||
165 | if ( from == NULL ) | ||
166 | goto err_free_fromBox; | ||
167 | |||
168 | err = mailimf_mailbox_list_add( from, fromBox ); | ||
169 | if ( err != MAILIMF_NO_ERROR ) | ||
170 | goto err_free_from; | ||
171 | |||
172 | to = parseAddresses( mail.getTo() ); | ||
173 | if ( to == NULL ) | ||
174 | goto err_free_from; | ||
175 | |||
176 | cc = parseAddresses( mail.getCC() ); | ||
177 | bcc = parseAddresses( mail.getBCC() ); | ||
178 | reply = parseAddresses( mail.getReply() ); | ||
179 | |||
180 | if (mail.Inreply().count()>0) { | ||
181 | in_reply_to = clist_new(); | ||
182 | char*c_reply; | ||
183 | unsigned int nsize = 0; | ||
184 | for (QStringList::ConstIterator it=mail.Inreply().begin(); | ||
185 | it != mail.Inreply().end();++it) { | ||
186 | /* yes! must be malloc! */ | ||
187 | if ((*it).isEmpty()) | ||
188 | continue; | ||
189 | QString h((*it)); | ||
190 | while (h.length()>0 && h[0]=='<') { | ||
191 | h.remove(0,1); | ||
192 | } | ||
193 | while (h.length()>0 && h[h.length()-1]=='>') { | ||
194 | h.remove(h.length()-1,1); | ||
195 | } | ||
196 | if (h.isEmpty()) continue; | ||
197 | nsize = strlen(h.latin1()); | ||
198 | c_reply = (char*)malloc( (nsize+1)*sizeof(char)); | ||
199 | memset(c_reply,0,nsize+1); | ||
200 | memcpy(c_reply,h.latin1(),nsize); | ||
201 | clist_append(in_reply_to,c_reply); | ||
202 | qDebug("In reply to: %s",c_reply); | ||
203 | } | ||
204 | } | ||
205 | |||
206 | fields = mailimf_fields_new_with_data( from, sender, reply, to, cc, bcc, | ||
207 | in_reply_to, NULL, subject ); | ||
208 | if ( fields == NULL ) | ||
209 | goto err_free_reply; | ||
210 | |||
211 | xmailer = mailimf_field_new_custom( strdup( "User-Agent" ), | ||
212 | strdup( USER_AGENT ) ); | ||
213 | if ( xmailer == NULL ) | ||
214 | goto err_free_fields; | ||
215 | |||
216 | err = mailimf_fields_add( fields, xmailer ); | ||
217 | if ( err != MAILIMF_NO_ERROR ) | ||
218 | goto err_free_xmailer; | ||
219 | |||
220 | return fields; // Success :) | ||
221 | |||
222 | err_free_xmailer: | ||
223 | if (xmailer) | ||
224 | mailimf_field_free( xmailer ); | ||
225 | err_free_fields: | ||
226 | if (fields) | ||
227 | mailimf_fields_free( fields ); | ||
228 | err_free_reply: | ||
229 | if (reply) | ||
230 | mailimf_address_list_free( reply ); | ||
231 | if (bcc) | ||
232 | mailimf_address_list_free( bcc ); | ||
233 | if (cc) | ||
234 | mailimf_address_list_free( cc ); | ||
235 | if (to) | ||
236 | mailimf_address_list_free( to ); | ||
237 | err_free_from: | ||
238 | if (from) | ||
239 | mailimf_mailbox_list_free( from ); | ||
240 | err_free_fromBox: | ||
241 | mailimf_mailbox_free( fromBox ); | ||
242 | err_free_sender: | ||
243 | if (sender) | ||
244 | mailimf_mailbox_free( sender ); | ||
245 | err_free: | ||
246 | if (subject) | ||
247 | free( subject ); | ||
248 | qDebug( "createImfFields - error" ); | ||
249 | |||
250 | return NULL; // Error :( | ||
251 | } | ||
252 | |||
253 | mailmime *SMTPwrapper::buildTxtPart(const QString&str ) { | ||
254 | mailmime *txtPart; | ||
255 | mailmime_fields *fields; | ||
256 | mailmime_content *content; | ||
257 | mailmime_parameter *param; | ||
258 | int err; | ||
259 | |||
260 | param = mailmime_parameter_new( strdup( "charset" ), | ||
261 | strdup( "iso-8859-1" ) ); | ||
262 | if ( param == NULL ) | ||
263 | goto err_free; | ||
264 | |||
265 | content = mailmime_content_new_with_str( "text/plain" ); | ||
266 | if ( content == NULL ) | ||
267 | goto err_free_param; | ||
268 | |||
269 | err = clist_append( content->ct_parameters, param ); | ||
270 | if ( err != MAILIMF_NO_ERROR ) | ||
271 | goto err_free_content; | ||
272 | |||
273 | fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_8BIT); | ||
274 | if ( fields == NULL ) | ||
275 | goto err_free_content; | ||
276 | |||
277 | txtPart = mailmime_new_empty( content, fields ); | ||
278 | if ( txtPart == NULL ) | ||
279 | goto err_free_fields; | ||
280 | |||
281 | err = mailmime_set_body_text( txtPart, (char*)str.data(), str.length() ); | ||
282 | if ( err != MAILIMF_NO_ERROR ) | ||
283 | goto err_free_txtPart; | ||
284 | |||
285 | return txtPart; // Success :) | ||
286 | |||
287 | err_free_txtPart: | ||
288 | mailmime_free( txtPart ); | ||
289 | err_free_fields: | ||
290 | mailmime_fields_free( fields ); | ||
291 | err_free_content: | ||
292 | mailmime_content_free( content ); | ||
293 | err_free_param: | ||
294 | mailmime_parameter_free( param ); | ||
295 | err_free: | ||
296 | qDebug( "buildTxtPart - error" ); | ||
297 | |||
298 | return NULL; // Error :( | ||
299 | } | ||
300 | |||
301 | mailmime *SMTPwrapper::buildFilePart(const QString&filename,const QString&mimetype,const QString&TextContent ) { | ||
302 | mailmime * filePart = 0; | ||
303 | mailmime_fields * fields = 0; | ||
304 | mailmime_content * content = 0; | ||
305 | mailmime_parameter * param = 0; | ||
306 | char*name = 0; | ||
307 | char*file = 0; | ||
308 | int err; | ||
309 | |||
310 | int pos = filename.findRev( '/' ); | ||
311 | |||
312 | if (filename.length()>0) { | ||
313 | QString tmp = filename.right( filename.length() - ( pos + 1 ) ); | ||
314 | name = strdup( tmp.latin1() ); // just filename | ||
315 | file = strdup( filename.latin1() ); // full name with path | ||
316 | } | ||
317 | |||
318 | int disptype = MAILMIME_DISPOSITION_TYPE_ATTACHMENT; | ||
319 | int mechanism = MAILMIME_MECHANISM_BASE64; | ||
320 | |||
321 | if ( mimetype.startsWith( "text/" ) ) { | ||
322 | param = mailmime_parameter_new( strdup( "charset" ), | ||
323 | strdup( "iso-8859-1" ) ); | ||
324 | mechanism = MAILMIME_MECHANISM_QUOTED_PRINTABLE; | ||
325 | } | ||
326 | |||
327 | fields = mailmime_fields_new_filename( | ||
328 | disptype, name, | ||
329 | mechanism ); | ||
330 | content = mailmime_content_new_with_str( (char*)mimetype.latin1() ); | ||
331 | if (content!=0 && fields != 0) { | ||
332 | if (param) { | ||
333 | clist_append(content->ct_parameters,param); | ||
334 | param = 0; | ||
335 | } | ||
336 | if (filename.length()>0) { | ||
337 | QFileInfo f(filename); | ||
338 | param = mailmime_parameter_new(strdup("name"),strdup(f.fileName().latin1())); | ||
339 | clist_append(content->ct_parameters,param); | ||
340 | param = 0; | ||
341 | } | ||
342 | filePart = mailmime_new_empty( content, fields ); | ||
343 | } | ||
344 | if (filePart) { | ||
345 | if (filename.length()>0) { | ||
346 | err = mailmime_set_body_file( filePart, file ); | ||
347 | } else { | ||
348 | err = mailmime_set_body_text(filePart,strdup(TextContent.data()),TextContent.length()); | ||
349 | } | ||
350 | if (err != MAILIMF_NO_ERROR) { | ||
351 | qDebug("Error setting body with file %s",file); | ||
352 | mailmime_free( filePart ); | ||
353 | filePart = 0; | ||
354 | } | ||
355 | } | ||
356 | |||
357 | if (!filePart) { | ||
358 | if ( param != NULL ) { | ||
359 | mailmime_parameter_free( param ); | ||
360 | } | ||
361 | if (content) { | ||
362 | mailmime_content_free( content ); | ||
363 | } | ||
364 | if (fields) { | ||
365 | mailmime_fields_free( fields ); | ||
366 | } else { | ||
367 | if (name) { | ||
368 | free( name ); | ||
369 | } | ||
370 | if (file) { | ||
371 | free( file ); | ||
372 | } | ||
373 | } | ||
374 | } | ||
375 | return filePart; // Success :) | ||
376 | |||
377 | } | ||
378 | |||
379 | void SMTPwrapper::addFileParts( mailmime *message,const QList<Attachment>&files ) { | ||
380 | const Attachment *it; | ||
381 | unsigned int count = files.count(); | ||
382 | qDebug("List contains %i values",count); | ||
383 | for ( unsigned int i = 0; i < count; ++i ) { | ||
384 | qDebug( "Adding file" ); | ||
385 | mailmime *filePart; | ||
386 | int err; | ||
387 | it = ((QList<Attachment>)files).at(i); | ||
388 | |||
389 | filePart = buildFilePart( it->getFileName(), it->getMimeType(),"" ); | ||
390 | if ( filePart == NULL ) { | ||
391 | qDebug( "addFileParts: error adding file:" ); | ||
392 | qDebug( it->getFileName() ); | ||
393 | continue; | ||
394 | } | ||
395 | err = mailmime_smart_add_part( message, filePart ); | ||
396 | if ( err != MAILIMF_NO_ERROR ) { | ||
397 | mailmime_free( filePart ); | ||
398 | qDebug("error smart add"); | ||
399 | } | ||
400 | } | ||
401 | } | ||
402 | |||
403 | mailmime *SMTPwrapper::createMimeMail(const Mail &mail ) { | ||
404 | mailmime *message, *txtPart; | ||
405 | mailimf_fields *fields; | ||
406 | int err; | ||
407 | |||
408 | fields = createImfFields( mail ); | ||
409 | if ( fields == NULL ) | ||
410 | goto err_free; | ||
411 | |||
412 | message = mailmime_new_message_data( NULL ); | ||
413 | if ( message == NULL ) | ||
414 | goto err_free_fields; | ||
415 | |||
416 | mailmime_set_imf_fields( message, fields ); | ||
417 | |||
418 | txtPart = buildTxtPart( mail.getMessage() ); | ||
419 | |||
420 | if ( txtPart == NULL ) | ||
421 | goto err_free_message; | ||
422 | |||
423 | err = mailmime_smart_add_part( message, txtPart ); | ||
424 | if ( err != MAILIMF_NO_ERROR ) | ||
425 | goto err_free_txtPart; | ||
426 | |||
427 | addFileParts( message, mail.getAttachments() ); | ||
428 | |||
429 | return message; // Success :) | ||
430 | |||
431 | err_free_txtPart: | ||
432 | mailmime_free( txtPart ); | ||
433 | err_free_message: | ||
434 | mailmime_free( message ); | ||
435 | err_free_fields: | ||
436 | mailimf_fields_free( fields ); | ||
437 | err_free: | ||
438 | qDebug( "createMimeMail: error" ); | ||
439 | |||
440 | return NULL; // Error :( | ||
441 | } | ||
442 | |||
443 | mailimf_field *SMTPwrapper::getField( mailimf_fields *fields, int type ) { | ||
444 | mailimf_field *field; | ||
445 | clistiter *it; | ||
446 | |||
447 | it = clist_begin( fields->fld_list ); | ||
448 | while ( it ) { | ||
449 | field = (mailimf_field *) it->data; | ||
450 | if ( field->fld_type == type ) { | ||
451 | return field; | ||
452 | } | ||
453 | it = it->next; | ||
454 | } | ||
455 | |||
456 | return NULL; | ||
457 | } | ||
458 | |||
459 | void SMTPwrapper::addRcpts( clist *list, mailimf_address_list *addr_list ) { | ||
460 | clistiter *it, *it2; | ||
461 | |||
462 | for ( it = clist_begin( addr_list->ad_list ); it; it = it->next ) { | ||
463 | mailimf_address *addr; | ||
464 | addr = (mailimf_address *) it->data; | ||
465 | |||
466 | if ( addr->ad_type == MAILIMF_ADDRESS_MAILBOX ) { | ||
467 | esmtp_address_list_add( list, addr->ad_data.ad_mailbox->mb_addr_spec, 0, NULL ); | ||
468 | } else if ( addr->ad_type == MAILIMF_ADDRESS_GROUP ) { | ||
469 | clist *l = addr->ad_data.ad_group->grp_mb_list->mb_list; | ||
470 | for ( it2 = clist_begin( l ); it2; it2 = it2->next ) { | ||
471 | mailimf_mailbox *mbox; | ||
472 | mbox = (mailimf_mailbox *) it2->data; | ||
473 | esmtp_address_list_add( list, mbox->mb_addr_spec, 0, NULL ); | ||
474 | } | ||
475 | } | ||
476 | } | ||
477 | } | ||
478 | |||
479 | clist *SMTPwrapper::createRcptList( mailimf_fields *fields ) { | ||
480 | clist *rcptList; | ||
481 | mailimf_field *field; | ||
482 | |||
483 | rcptList = esmtp_address_list_new(); | ||
484 | |||
485 | field = getField( fields, MAILIMF_FIELD_TO ); | ||
486 | if ( field && (field->fld_type == MAILIMF_FIELD_TO) | ||
487 | && field->fld_data.fld_to->to_addr_list ) { | ||
488 | addRcpts( rcptList, field->fld_data.fld_to->to_addr_list ); | ||
489 | } | ||
490 | |||
491 | field = getField( fields, MAILIMF_FIELD_CC ); | ||
492 | if ( field && (field->fld_type == MAILIMF_FIELD_CC) | ||
493 | && field->fld_data.fld_cc->cc_addr_list ) { | ||
494 | addRcpts( rcptList, field->fld_data.fld_cc->cc_addr_list ); | ||
495 | } | ||
496 | |||
497 | field = getField( fields, MAILIMF_FIELD_BCC ); | ||
498 | if ( field && (field->fld_type == MAILIMF_FIELD_BCC) | ||
499 | && field->fld_data.fld_bcc->bcc_addr_list ) { | ||
500 | addRcpts( rcptList, field->fld_data.fld_bcc->bcc_addr_list ); | ||
501 | } | ||
502 | |||
503 | return rcptList; | ||
504 | } | ||
505 | |||
506 | char *SMTPwrapper::getFrom( mailimf_field *ffrom) { | ||
507 | char *from = NULL; | ||
508 | if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM) | ||
509 | && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) { | ||
510 | clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list; | ||
511 | clistiter *it; | ||
512 | for ( it = clist_begin( cl ); it; it = it->next ) { | ||
513 | mailimf_mailbox *mb = (mailimf_mailbox *) it->data; | ||
514 | from = strdup( mb->mb_addr_spec ); | ||
515 | } | ||
516 | } | ||
517 | |||
518 | return from; | ||
519 | } | ||
520 | |||
521 | char *SMTPwrapper::getFrom( mailmime *mail ) { | ||
522 | /* no need to delete - its just a pointer to structure content */ | ||
523 | mailimf_field *ffrom = 0; | ||
524 | ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM ); | ||
525 | return getFrom(ffrom); | ||
526 | } | ||
527 | 83 | ||
528 | void SMTPwrapper::progress( size_t current, size_t maximum ) { | 84 | void SMTPwrapper::progress( size_t current, size_t maximum ) { |