Diffstat (limited to 'noncore/net/mail/libmailwrapper/smtpwrapper.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/net/mail/libmailwrapper/smtpwrapper.cpp | 22 |
1 files changed, 22 insertions, 0 deletions
diff --git a/noncore/net/mail/libmailwrapper/smtpwrapper.cpp b/noncore/net/mail/libmailwrapper/smtpwrapper.cpp index 30c0707..7e03af9 100644 --- a/noncore/net/mail/libmailwrapper/smtpwrapper.cpp +++ b/noncore/net/mail/libmailwrapper/smtpwrapper.cpp | |||
@@ -1,73 +1,86 @@ | |||
1 | #include <stdlib.h> | 1 | #include <stdlib.h> |
2 | #include <sys/stat.h> | 2 | #include <sys/stat.h> |
3 | #include <sys/types.h> | 3 | #include <sys/types.h> |
4 | #include <unistd.h> | 4 | #include <unistd.h> |
5 | #include <fcntl.h> | 5 | #include <fcntl.h> |
6 | #include <string.h> | 6 | #include <string.h> |
7 | #include <qdir.h> | 7 | #include <qdir.h> |
8 | #include <qt.h> | 8 | #include <qt.h> |
9 | 9 | ||
10 | #include <qpe/config.h> | ||
11 | #include <qpe/qcopenvelope_qws.h> | ||
12 | |||
10 | #include <libetpan/libetpan.h> | 13 | #include <libetpan/libetpan.h> |
11 | 14 | ||
12 | #include "smtpwrapper.h" | 15 | #include "smtpwrapper.h" |
13 | #include "mailwrapper.h" | 16 | #include "mailwrapper.h" |
14 | #include "mboxwrapper.h" | 17 | #include "mboxwrapper.h" |
15 | #include "logindialog.h" | 18 | #include "logindialog.h" |
16 | #include "mailtypes.h" | 19 | #include "mailtypes.h" |
17 | #include "defines.h" | 20 | #include "defines.h" |
18 | #include "sendmailprogress.h" | 21 | #include "sendmailprogress.h" |
19 | 22 | ||
20 | progressMailSend*SMTPwrapper::sendProgress = 0; | 23 | progressMailSend*SMTPwrapper::sendProgress = 0; |
21 | 24 | ||
22 | SMTPwrapper::SMTPwrapper( Settings *s ) | 25 | SMTPwrapper::SMTPwrapper( Settings *s ) |
23 | : QObject() | 26 | : QObject() |
24 | { | 27 | { |
25 | settings = s; | 28 | settings = s; |
29 | Config cfg( "mail" ); | ||
30 | cfg.setGroup( "Status" ); | ||
31 | m_queuedMail = cfg.readNumEntry( "outgoing", 0 ); | ||
32 | emit queuedMails( m_queuedMail ); | ||
33 | connect( this, SIGNAL( queuedMails( int ) ), this, SLOT( emitQCop( int ) ) ); | ||
34 | } | ||
35 | |||
36 | void SMTPwrapper::emitQCop( int queued ) { | ||
37 | QCopEnvelope env( "QPE/Pim", "outgoingMails(int)" ); | ||
38 | env << queued; | ||
26 | } | 39 | } |
27 | 40 | ||
28 | QString SMTPwrapper::mailsmtpError( int errnum ) | 41 | QString SMTPwrapper::mailsmtpError( int errnum ) |
29 | { | 42 | { |
30 | switch ( errnum ) { | 43 | switch ( errnum ) { |
31 | case MAILSMTP_NO_ERROR: | 44 | case MAILSMTP_NO_ERROR: |
32 | return tr( "No error" ); | 45 | return tr( "No error" ); |
33 | case MAILSMTP_ERROR_UNEXPECTED_CODE: | 46 | case MAILSMTP_ERROR_UNEXPECTED_CODE: |
34 | return tr( "Unexpected error code" ); | 47 | return tr( "Unexpected error code" ); |
35 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: | 48 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: |
36 | return tr( "Service not available" ); | 49 | return tr( "Service not available" ); |
37 | case MAILSMTP_ERROR_STREAM: | 50 | case MAILSMTP_ERROR_STREAM: |
38 | return tr( "Stream error" ); | 51 | return tr( "Stream error" ); |
39 | case MAILSMTP_ERROR_HOSTNAME: | 52 | case MAILSMTP_ERROR_HOSTNAME: |
40 | return tr( "gethostname() failed" ); | 53 | return tr( "gethostname() failed" ); |
41 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: | 54 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: |
42 | return tr( "Not implemented" ); | 55 | return tr( "Not implemented" ); |
43 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: | 56 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: |
44 | return tr( "Error, action not taken" ); | 57 | return tr( "Error, action not taken" ); |
45 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: | 58 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: |
46 | return tr( "Data exceeds storage allocation" ); | 59 | return tr( "Data exceeds storage allocation" ); |
47 | case MAILSMTP_ERROR_IN_PROCESSING: | 60 | case MAILSMTP_ERROR_IN_PROCESSING: |
48 | return tr( "Error in processing" ); | 61 | return tr( "Error in processing" ); |
49 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: | 62 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: |
50 | // return tr( "Insufficient system storage" ); | 63 | // return tr( "Insufficient system storage" ); |
51 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: | 64 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: |
52 | return tr( "Mailbox unavailable" ); | 65 | return tr( "Mailbox unavailable" ); |
53 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: | 66 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: |
54 | return tr( "Mailbox name not allowed" ); | 67 | return tr( "Mailbox name not allowed" ); |
55 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: | 68 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: |
56 | return tr( "Bad command sequence" ); | 69 | return tr( "Bad command sequence" ); |
57 | case MAILSMTP_ERROR_USER_NOT_LOCAL: | 70 | case MAILSMTP_ERROR_USER_NOT_LOCAL: |
58 | return tr( "User not local" ); | 71 | return tr( "User not local" ); |
59 | case MAILSMTP_ERROR_TRANSACTION_FAILED: | 72 | case MAILSMTP_ERROR_TRANSACTION_FAILED: |
60 | return tr( "Transaction failed" ); | 73 | return tr( "Transaction failed" ); |
61 | case MAILSMTP_ERROR_MEMORY: | 74 | case MAILSMTP_ERROR_MEMORY: |
62 | return tr( "Memory error" ); | 75 | return tr( "Memory error" ); |
63 | case MAILSMTP_ERROR_CONNECTION_REFUSED: | 76 | case MAILSMTP_ERROR_CONNECTION_REFUSED: |
64 | return tr( "Connection refused" ); | 77 | return tr( "Connection refused" ); |
65 | default: | 78 | default: |
66 | return tr( "Unknown error code" ); | 79 | return tr( "Unknown error code" ); |
67 | } | 80 | } |
68 | } | 81 | } |
69 | 82 | ||
70 | mailimf_mailbox *SMTPwrapper::newMailbox(const QString&name, const QString&mail ) | 83 | mailimf_mailbox *SMTPwrapper::newMailbox(const QString&name, const QString&mail ) |
71 | { | 84 | { |
72 | return mailimf_mailbox_new( strdup( name.latin1() ), | 85 | return mailimf_mailbox_new( strdup( name.latin1() ), |
73 | strdup( mail.latin1() ) ); | 86 | strdup( mail.latin1() ) ); |
@@ -462,96 +475,100 @@ char *SMTPwrapper::getFrom( mailmime *mail ) | |||
462 | return getFrom(ffrom); | 475 | return getFrom(ffrom); |
463 | } | 476 | } |
464 | 477 | ||
465 | void SMTPwrapper::progress( size_t current, size_t maximum ) | 478 | void SMTPwrapper::progress( size_t current, size_t maximum ) |
466 | { | 479 | { |
467 | if (SMTPwrapper::sendProgress) { | 480 | if (SMTPwrapper::sendProgress) { |
468 | SMTPwrapper::sendProgress->setSingleMail(current, maximum ); | 481 | SMTPwrapper::sendProgress->setSingleMail(current, maximum ); |
469 | qApp->processEvents(); | 482 | qApp->processEvents(); |
470 | } | 483 | } |
471 | } | 484 | } |
472 | 485 | ||
473 | void SMTPwrapper::storeMail(char*mail, size_t length, const QString&box) | 486 | void SMTPwrapper::storeMail(char*mail, size_t length, const QString&box) |
474 | { | 487 | { |
475 | if (!mail) return; | 488 | if (!mail) return; |
476 | QString localfolders = (QString) getenv( "HOME" ) + QString("/Applications/opiemail/localmail/"); | 489 | QString localfolders = (QString) getenv( "HOME" ) + QString("/Applications/opiemail/localmail/"); |
477 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); | 490 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); |
478 | wrap->storeMessage(mail,length,box); | 491 | wrap->storeMessage(mail,length,box); |
479 | delete wrap; | 492 | delete wrap; |
480 | } | 493 | } |
481 | 494 | ||
482 | void SMTPwrapper::smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ) | 495 | void SMTPwrapper::smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ) |
483 | { | 496 | { |
484 | clist *rcpts = 0; | 497 | clist *rcpts = 0; |
485 | char *from, *data; | 498 | char *from, *data; |
486 | size_t size; | 499 | size_t size; |
487 | 500 | ||
488 | if ( smtp == NULL ) { | 501 | if ( smtp == NULL ) { |
489 | return; | 502 | return; |
490 | } | 503 | } |
491 | from = data = 0; | 504 | from = data = 0; |
492 | 505 | ||
493 | mailmessage * msg = 0; | 506 | mailmessage * msg = 0; |
494 | msg = mime_message_init(mail); | 507 | msg = mime_message_init(mail); |
495 | mime_message_set_tmpdir(msg,getenv( "HOME" )); | 508 | mime_message_set_tmpdir(msg,getenv( "HOME" )); |
496 | int r = mailmessage_fetch(msg,&data,&size); | 509 | int r = mailmessage_fetch(msg,&data,&size); |
497 | mime_message_detach_mime(msg); | 510 | mime_message_detach_mime(msg); |
498 | mailmessage_free(msg); | 511 | mailmessage_free(msg); |
499 | if (r != MAIL_NO_ERROR || !data) { | 512 | if (r != MAIL_NO_ERROR || !data) { |
500 | if (data) free(data); | 513 | if (data) free(data); |
501 | qDebug("Error fetching mime..."); | 514 | qDebug("Error fetching mime..."); |
502 | return; | 515 | return; |
503 | } | 516 | } |
504 | QString tmp = data; | 517 | QString tmp = data; |
505 | tmp.replace(QRegExp("\r+",true,false),""); | 518 | tmp.replace(QRegExp("\r+",true,false),""); |
506 | msg = 0; | 519 | msg = 0; |
507 | if (later) { | 520 | if (later) { |
508 | storeMail((char*)tmp.data(),tmp.length(),"Outgoing"); | 521 | storeMail((char*)tmp.data(),tmp.length(),"Outgoing"); |
509 | if (data) free( data ); | 522 | if (data) free( data ); |
523 | Config cfg( "mail" ); | ||
524 | cfg.setGroup( "Status" ); | ||
525 | cfg.writeEntry( "outgoing", ++m_queuedMail ); | ||
526 | emit queuedMails( m_queuedMail ); | ||
510 | return; | 527 | return; |
511 | } | 528 | } |
512 | from = getFrom( mail ); | 529 | from = getFrom( mail ); |
513 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); | 530 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); |
514 | smtpSend(from,rcpts,data,size,smtp); | 531 | smtpSend(from,rcpts,data,size,smtp); |
515 | if (data) {free(data);} | 532 | if (data) {free(data);} |
516 | if (from) {free(from);} | 533 | if (from) {free(from);} |
517 | if (rcpts) smtp_address_list_free( rcpts ); | 534 | if (rcpts) smtp_address_list_free( rcpts ); |
518 | } | 535 | } |
519 | 536 | ||
520 | int SMTPwrapper::smtpSend(char*from,clist*rcpts,char*data,size_t size, SMTPaccount *smtp ) | 537 | int SMTPwrapper::smtpSend(char*from,clist*rcpts,char*data,size_t size, SMTPaccount *smtp ) |
521 | { | 538 | { |
522 | char *server, *user, *pass; | 539 | char *server, *user, *pass; |
523 | bool ssl; | 540 | bool ssl; |
524 | uint16_t port; | 541 | uint16_t port; |
525 | mailsmtp *session; | 542 | mailsmtp *session; |
526 | int err,result; | 543 | int err,result; |
527 | 544 | ||
528 | result = 1; | 545 | result = 1; |
529 | server = user = pass = 0; | 546 | server = user = pass = 0; |
530 | server = strdup( smtp->getServer().latin1() ); | 547 | server = strdup( smtp->getServer().latin1() ); |
531 | ssl = smtp->getSSL(); | 548 | ssl = smtp->getSSL(); |
532 | port = smtp->getPort().toUInt(); | 549 | port = smtp->getPort().toUInt(); |
533 | 550 | ||
534 | session = mailsmtp_new( 20, &progress ); | 551 | session = mailsmtp_new( 20, &progress ); |
535 | if ( session == NULL ) goto free_mem; | 552 | if ( session == NULL ) goto free_mem; |
536 | 553 | ||
537 | qDebug( "Servername %s at port %i", server, port ); | 554 | qDebug( "Servername %s at port %i", server, port ); |
538 | if ( ssl ) { | 555 | if ( ssl ) { |
539 | qDebug( "SSL session" ); | 556 | qDebug( "SSL session" ); |
540 | err = mailsmtp_ssl_connect( session, server, port ); | 557 | err = mailsmtp_ssl_connect( session, server, port ); |
541 | } else { | 558 | } else { |
542 | qDebug( "No SSL session" ); | 559 | qDebug( "No SSL session" ); |
543 | err = mailsmtp_socket_connect( session, server, port ); | 560 | err = mailsmtp_socket_connect( session, server, port ); |
544 | } | 561 | } |
545 | if ( err != MAILSMTP_NO_ERROR ) {result = 0;goto free_mem_session;} | 562 | if ( err != MAILSMTP_NO_ERROR ) {result = 0;goto free_mem_session;} |
546 | 563 | ||
547 | err = mailsmtp_init( session ); | 564 | err = mailsmtp_init( session ); |
548 | if ( err != MAILSMTP_NO_ERROR ) {result = 0; goto free_con_session;} | 565 | if ( err != MAILSMTP_NO_ERROR ) {result = 0; goto free_con_session;} |
549 | 566 | ||
550 | qDebug( "INIT OK" ); | 567 | qDebug( "INIT OK" ); |
551 | 568 | ||
552 | if ( smtp->getLogin() ) { | 569 | if ( smtp->getLogin() ) { |
553 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { | 570 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { |
554 | // get'em | 571 | // get'em |
555 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); | 572 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); |
556 | login.show(); | 573 | login.show(); |
557 | if ( QDialog::Accepted == login.exec() ) { | 574 | if ( QDialog::Accepted == login.exec() ) { |
@@ -655,56 +672,61 @@ int SMTPwrapper::sendQueuedMail(MBOXwrapper*wrap,SMTPaccount*smtp,RecMail*which) | |||
655 | free(data); | 672 | free(data); |
656 | } | 673 | } |
657 | if (from) { | 674 | if (from) { |
658 | free(from); | 675 | free(from); |
659 | } | 676 | } |
660 | if (rcpts) { | 677 | if (rcpts) { |
661 | smtp_address_list_free( rcpts ); | 678 | smtp_address_list_free( rcpts ); |
662 | } | 679 | } |
663 | return res; | 680 | return res; |
664 | } | 681 | } |
665 | 682 | ||
666 | /* this is a special fun */ | 683 | /* this is a special fun */ |
667 | bool SMTPwrapper::flushOutbox(SMTPaccount*smtp) | 684 | bool SMTPwrapper::flushOutbox(SMTPaccount*smtp) |
668 | { | 685 | { |
669 | bool returnValue = true; | 686 | bool returnValue = true; |
670 | 687 | ||
671 | if (!smtp) return false; | 688 | if (!smtp) return false; |
672 | 689 | ||
673 | QString localfolders = (QString) getenv( "HOME" ) + QString("/Applications/opiemail/localmail/"); | 690 | QString localfolders = (QString) getenv( "HOME" ) + QString("/Applications/opiemail/localmail/"); |
674 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); | 691 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); |
675 | if (!wrap) { | 692 | if (!wrap) { |
676 | qDebug("memory error"); | 693 | qDebug("memory error"); |
677 | return false; | 694 | return false; |
678 | } | 695 | } |
679 | QList<RecMail> mailsToSend; | 696 | QList<RecMail> mailsToSend; |
680 | QList<RecMail> mailsToRemove; | 697 | QList<RecMail> mailsToRemove; |
681 | QString mbox("Outgoing"); | 698 | QString mbox("Outgoing"); |
682 | wrap->listMessages(mbox,mailsToSend); | 699 | wrap->listMessages(mbox,mailsToSend); |
683 | if (mailsToSend.count()==0) { | 700 | if (mailsToSend.count()==0) { |
684 | delete wrap; | 701 | delete wrap; |
685 | return false; | 702 | return false; |
686 | } | 703 | } |
687 | mailsToSend.setAutoDelete(false); | 704 | mailsToSend.setAutoDelete(false); |
688 | sendProgress = new progressMailSend(); | 705 | sendProgress = new progressMailSend(); |
689 | sendProgress->show(); | 706 | sendProgress->show(); |
690 | sendProgress->setMaxMails(mailsToSend.count()); | 707 | sendProgress->setMaxMails(mailsToSend.count()); |
691 | 708 | ||
692 | while (mailsToSend.count()>0) { | 709 | while (mailsToSend.count()>0) { |
693 | if (sendQueuedMail(wrap,smtp,mailsToSend.at(0))==0) { | 710 | if (sendQueuedMail(wrap,smtp,mailsToSend.at(0))==0) { |
694 | QMessageBox::critical(0,tr("Error sending mail"), | 711 | QMessageBox::critical(0,tr("Error sending mail"), |
695 | tr("Error sending queued mail - breaking")); | 712 | tr("Error sending queued mail - breaking")); |
696 | returnValue = false; | 713 | returnValue = false; |
697 | break; | 714 | break; |
698 | } | 715 | } |
699 | mailsToRemove.append(mailsToSend.at(0)); | 716 | mailsToRemove.append(mailsToSend.at(0)); |
700 | mailsToSend.removeFirst(); | 717 | mailsToSend.removeFirst(); |
701 | sendProgress->setCurrentMails(mailsToRemove.count()); | 718 | sendProgress->setCurrentMails(mailsToRemove.count()); |
702 | } | 719 | } |
720 | Config cfg( "mail" ); | ||
721 | cfg.setGroup( "Status" ); | ||
722 | m_queuedMail = 0; | ||
723 | cfg.writeEntry( "outgoing", m_queuedMail ); | ||
724 | emit queuedMails( m_queuedMail ); | ||
703 | sendProgress->hide(); | 725 | sendProgress->hide(); |
704 | delete sendProgress; | 726 | delete sendProgress; |
705 | sendProgress = 0; | 727 | sendProgress = 0; |
706 | wrap->deleteMails(mbox,mailsToRemove); | 728 | wrap->deleteMails(mbox,mailsToRemove); |
707 | mailsToSend.setAutoDelete(true); | 729 | mailsToSend.setAutoDelete(true); |
708 | delete wrap; | 730 | delete wrap; |
709 | return returnValue; | 731 | return returnValue; |
710 | } | 732 | } |