summaryrefslogtreecommitdiff
Unidiff
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/mail/libmailwrapper/smtpwrapper.cpp108
1 files changed, 67 insertions, 41 deletions
diff --git a/noncore/net/mail/libmailwrapper/smtpwrapper.cpp b/noncore/net/mail/libmailwrapper/smtpwrapper.cpp
index 7671133..e8db9ca 100644
--- a/noncore/net/mail/libmailwrapper/smtpwrapper.cpp
+++ b/noncore/net/mail/libmailwrapper/smtpwrapper.cpp
@@ -1,48 +1,47 @@
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#include <qmessagebox.h> 9#include <qmessagebox.h>
10 10
11#include <qpe/config.h> 11#include <qpe/config.h>
12#include <qpe/qcopenvelope_qws.h> 12#include <qpe/qcopenvelope_qws.h>
13 13
14#include <libetpan/libetpan.h> 14#include <libetpan/libetpan.h>
15 15
16#include "smtpwrapper.h" 16#include "smtpwrapper.h"
17#include "mailwrapper.h" 17#include "mailwrapper.h"
18#include "abstractmail.h" 18#include "abstractmail.h"
19#include "logindialog.h" 19#include "logindialog.h"
20#include "mailtypes.h" 20#include "mailtypes.h"
21//#include "defines.h"
22#include "sendmailprogress.h" 21#include "sendmailprogress.h"
23 22
24const char* SMTPwrapper::USER_AGENT="OpieMail v0.3"; 23const char* SMTPwrapper::USER_AGENT="OpieMail v0.4";
25 24
26progressMailSend*SMTPwrapper::sendProgress = 0; 25progressMailSend*SMTPwrapper::sendProgress = 0;
27 26
28SMTPwrapper::SMTPwrapper( Settings *s ) 27SMTPwrapper::SMTPwrapper( Settings *s )
29: QObject() { 28: QObject() {
30 settings = s; 29 settings = s;
31 Config cfg( "mail" ); 30 Config cfg( "mail" );
32 cfg.setGroup( "Status" ); 31 cfg.setGroup( "Status" );
33 m_queuedMail = cfg.readNumEntry( "outgoing", 0 ); 32 m_queuedMail = cfg.readNumEntry( "outgoing", 0 );
34 emit queuedMails( m_queuedMail ); 33 emit queuedMails( m_queuedMail );
35 connect( this, SIGNAL( queuedMails( int ) ), this, SLOT( emitQCop( int ) ) ); 34 connect( this, SIGNAL( queuedMails( int ) ), this, SLOT( emitQCop( int ) ) );
36} 35}
37 36
38void SMTPwrapper::emitQCop( int queued ) { 37void SMTPwrapper::emitQCop( int queued ) {
39 QCopEnvelope env( "QPE/Pim", "outgoingMails(int)" ); 38 QCopEnvelope env( "QPE/Pim", "outgoingMails(int)" );
40 env << queued; 39 env << queued;
41} 40}
42 41
43QString SMTPwrapper::mailsmtpError( int errnum ) { 42QString SMTPwrapper::mailsmtpError( int errnum ) {
44 switch ( errnum ) { 43 switch ( errnum ) {
45 case MAILSMTP_NO_ERROR: 44 case MAILSMTP_NO_ERROR:
46 return tr( "No error" ); 45 return tr( "No error" );
47 case MAILSMTP_ERROR_UNEXPECTED_CODE: 46 case MAILSMTP_ERROR_UNEXPECTED_CODE:
48 return tr( "Unexpected error code" ); 47 return tr( "Unexpected error code" );
@@ -551,141 +550,141 @@ void SMTPwrapper::smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ) {
551 if (from) { 550 if (from) {
552 free(from); 551 free(from);
553 } 552 }
554 if (rcpts) 553 if (rcpts)
555 smtp_address_list_free( rcpts ); 554 smtp_address_list_free( rcpts );
556} 555}
557 556
558void SMTPwrapper::storeFailedMail(const char*data,unsigned int size, const char*failuremessage) 557void SMTPwrapper::storeFailedMail(const char*data,unsigned int size, const char*failuremessage)
559{ 558{
560 if (data) { 559 if (data) {
561 storeMail(data,size,"Sendfailed"); 560 storeMail(data,size,"Sendfailed");
562 } 561 }
563 if (failuremessage) { 562 if (failuremessage) {
564 QMessageBox::critical(0,tr("Error sending mail"), 563 QMessageBox::critical(0,tr("Error sending mail"),
565 tr("<center>%1</center>").arg(failuremessage)); 564 tr("<center>%1</center>").arg(failuremessage));
566 } 565 }
567} 566}
568 567
569int SMTPwrapper::smtpSend(char*from,clist*rcpts,const char*data,size_t size, SMTPaccount *smtp ) { 568int SMTPwrapper::smtpSend(char*from,clist*rcpts,const char*data,size_t size, SMTPaccount *smtp ) {
570 const char *server, *user, *pass; 569 const char *server, *user, *pass;
571 bool ssl; 570 bool ssl;
572 uint16_t port; 571 uint16_t port;
573 mailsmtp *session; 572 mailsmtp *session;
574 int err,result; 573 int err,result;
574 QString failuretext = "";
575 575
576 result = 1; 576 result = 1;
577 server = user = pass = 0; 577 server = user = pass = 0;
578 server = smtp->getServer().latin1(); 578 server = smtp->getServer().latin1();
579 579
580 // FIXME: currently only TLS and Plain work. 580 // FIXME: currently only TLS and Plain work.
581 581
582 ssl = false; 582 ssl = false;
583 583
584 if ( smtp->ConnectionType() == 2 ) { 584 if ( smtp->ConnectionType() == 2 ) {
585 ssl = true; 585 ssl = true;
586 } 586 }
587 587
588 port = smtp->getPort().toUInt(); 588 port = smtp->getPort().toUInt();
589 589
590 session = mailsmtp_new( 20, &progress ); 590 session = mailsmtp_new( 20, &progress );
591 if ( session == NULL ) 591 if ( session == NULL ) {
592 goto free_mem; 592 /* no failure message cause this happens when problems with memory - than we
593 we can not display any messagebox */
594 return 0;
595 }
593 596
594 qDebug( "Servername %s at port %i", server, port ); 597 qDebug( "Servername %s at port %i", server, port );
595 if ( ssl ) { 598 if ( ssl ) {
596 qDebug( "SSL session" ); 599 qDebug( "SSL session" );
597 err = mailsmtp_ssl_connect( session, server, port ); 600 err = mailsmtp_ssl_connect( session, server, port );
598 } else { 601 } else {
599 qDebug( "No SSL session" ); 602 qDebug( "No SSL session" );
600 err = mailsmtp_socket_connect( session, server, port ); 603 err = mailsmtp_socket_connect( session, server, port );
601 } 604 }
602 if ( err != MAILSMTP_NO_ERROR ) { 605 if ( err != MAILSMTP_NO_ERROR ) {
603 qDebug("Error init connection"); 606 qDebug("Error init connection");
604 storeFailedMail(data,size,mailsmtpError(err)); 607 failuretext = tr("Error init SMTP connection: %1").arg(mailsmtpError(err));
605 result = 0; 608 result = 0;
606 goto free_mem_session;
607 } 609 }
608 610
609 err = mailsmtp_init( session ); 611 if (result) {
610 if ( err != MAILSMTP_NO_ERROR ) { 612 err = mailsmtp_init( session );
611 storeFailedMail(data,size,mailsmtpError(err)); 613 if (err != MAILSMTP_NO_ERROR) {
612 result = 0; 614 result = 0;
613 goto free_con_session; 615 failuretext = tr("Error init SMTP connection: %1").arg(mailsmtpError(err));
616 }
614 } 617 }
615 618
616 qDebug( "INIT OK" ); 619 if (result==1 && smtp->getLogin() ) {
617
618 if ( smtp->getLogin() ) {
619 qDebug("smtp with auth"); 620 qDebug("smtp with auth");
620 if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { 621 if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) {
621 // get'em 622 // get'em
622 LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); 623 LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true );
623 login.show(); 624 login.show();
624 if ( QDialog::Accepted == login.exec() ) { 625 if ( QDialog::Accepted == login.exec() ) {
625 // ok 626 // ok
626 user = login.getUser().latin1(); 627 user = login.getUser().latin1();
627 pass = login.getPassword().latin1(); 628 pass = login.getPassword().latin1();
628 } else { 629 } else {
629 result = 0; 630 result = 0;
630 goto free_con_session; 631 failuretext=tr("Login aborted - storing mail to localfolder");
631 } 632 }
632 } else { 633 } else {
633 user = smtp->getUser().latin1(); 634 user = smtp->getUser().latin1();
634 pass = smtp->getPassword().latin1(); 635 pass = smtp->getPassword().latin1();
635 } 636 }
636 qDebug( "session->auth: %i", session->auth); 637 qDebug( "session->auth: %i", session->auth);
637 err = mailsmtp_auth( session, (char*)user, (char*)pass ); 638 if (result) {
638 if ( err == MAILSMTP_NO_ERROR ) { 639 err = mailsmtp_auth( session, (char*)user, (char*)pass );
639 qDebug("auth ok"); 640 if ( err == MAILSMTP_NO_ERROR ) {
640 } else { 641 qDebug("auth ok");
641 storeFailedMail(data,size,tr("Authentification failed")); 642 } else {
642 result = 0; 643 failuretext = tr("Authentification failed");
643 goto free_con_session; 644 result = 0;
645 }
644 } 646 }
645 qDebug( "Done auth!" );
646 } else {
647 qDebug("SMTP without auth");
648 result = 0;
649 goto free_con_session;
650 } 647 }
651 648
652 err = mailsmtp_send( session, from, rcpts, data, size ); 649 if (result) {
653 if ( err != MAILSMTP_NO_ERROR ) { 650 err = mailsmtp_send( session, from, rcpts, data, size );
654 storeFailedMail(data,size,mailsmtpError(err)); 651 if ( err != MAILSMTP_NO_ERROR ) {
655 qDebug("Error sending mail: %s",mailsmtpError(err).latin1()); 652 failuretext=tr("Error sending mail: %1").arg(mailsmtpError(err));
656 result = 0; 653 result = 0;
657 goto free_con_session; 654 }
658 } 655 }
659 656
660 qDebug( "Mail sent." ); 657 if (!result) {
661 storeMail(data,size,"Sent"); 658 storeFailedMail(data,size,failuretext);
662 659 } else {
663free_con_session: 660 qDebug( "Mail sent." );
664 mailsmtp_quit( session ); 661 storeMail(data,size,"Sent");
665free_mem_session: 662 }
666 mailsmtp_free( session ); 663 if (session) {
667free_mem: 664 mailsmtp_quit( session );
665 mailsmtp_free( session );
666 }
668 return result; 667 return result;
669} 668}
670 669
671void SMTPwrapper::sendMail(const Mail&mail,SMTPaccount*aSmtp,bool later ) { 670void SMTPwrapper::sendMail(const Mail&mail,SMTPaccount*aSmtp,bool later ) {
672 mailmime * mimeMail; 671 mailmime * mimeMail;
673 672
674 SMTPaccount *smtp = aSmtp; 673 SMTPaccount *smtp = aSmtp;
675 674
676 if (!later && !smtp) { 675 if (!later && !smtp) {
677 qDebug("Didn't get any send method - giving up"); 676 qDebug("Didn't get any send method - giving up");
678 return; 677 return;
679 } 678 }
680 mimeMail = createMimeMail(mail ); 679 mimeMail = createMimeMail(mail );
681 if ( mimeMail == NULL ) { 680 if ( mimeMail == NULL ) {
682 qDebug( "sendMail: error creating mime mail" ); 681 qDebug( "sendMail: error creating mime mail" );
683 } else { 682 } else {
684 sendProgress = new progressMailSend(); 683 sendProgress = new progressMailSend();
685 sendProgress->show(); 684 sendProgress->show();
686 sendProgress->setMaxMails(1); 685 sendProgress->setMaxMails(1);
687 smtpSend( mimeMail,later,smtp); 686 smtpSend( mimeMail,later,smtp);
688 qDebug("Clean up done"); 687 qDebug("Clean up done");
689 sendProgress->hide(); 688 sendProgress->hide();
690 delete sendProgress; 689 delete sendProgress;
691 sendProgress = 0; 690 sendProgress = 0;
@@ -720,67 +719,94 @@ int SMTPwrapper::sendQueuedMail(AbstractMail*wrap,SMTPaccount*smtp,RecMail*which
720 } 719 }
721 if (fields) { 720 if (fields) {
722 mailimf_fields_free(fields); 721 mailimf_fields_free(fields);
723 fields = 0; 722 fields = 0;
724 } 723 }
725 if (data) { 724 if (data) {
726 delete data; 725 delete data;
727 } 726 }
728 if (from) { 727 if (from) {
729 free(from); 728 free(from);
730 } 729 }
731 if (rcpts) { 730 if (rcpts) {
732 smtp_address_list_free( rcpts ); 731 smtp_address_list_free( rcpts );
733 } 732 }
734 return res; 733 return res;
735} 734}
736 735
737/* this is a special fun */ 736/* this is a special fun */
738bool SMTPwrapper::flushOutbox(SMTPaccount*smtp) { 737bool SMTPwrapper::flushOutbox(SMTPaccount*smtp) {
739 bool returnValue = true; 738 bool returnValue = true;
740 739
741 if (!smtp) 740 if (!smtp)
742 return false; 741 return false;
743 742
743 bool reset_user_value = false;
744 QString localfolders = AbstractMail::defaultLocalfolder(); 744 QString localfolders = AbstractMail::defaultLocalfolder();
745 AbstractMail*wrap = AbstractMail::getWrapper(localfolders); 745 AbstractMail*wrap = AbstractMail::getWrapper(localfolders);
746 if (!wrap) { 746 if (!wrap) {
747 qDebug("memory error"); 747 qDebug("memory error");
748 return false; 748 return false;
749 } 749 }
750 QString oldPw, oldUser;
750 QList<RecMail> mailsToSend; 751 QList<RecMail> mailsToSend;
751 QList<RecMail> mailsToRemove; 752 QList<RecMail> mailsToRemove;
752 QString mbox("Outgoing"); 753 QString mbox("Outgoing");
753 wrap->listMessages(mbox,mailsToSend); 754 wrap->listMessages(mbox,mailsToSend);
754 if (mailsToSend.count()==0) { 755 if (mailsToSend.count()==0) {
755 delete wrap; 756 delete wrap;
756 return false; 757 return false;
757 } 758 }
759
760 oldPw = smtp->getPassword();
761 oldUser = smtp->getUser();
762 if (smtp->getLogin() && (smtp->getUser().isEmpty() || smtp->getPassword().isEmpty()) ) {
763 // get'em
764 QString user,pass;
765 LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true );
766 login.show();
767 if ( QDialog::Accepted == login.exec() ) {
768 // ok
769 user = login.getUser().latin1();
770 pass = login.getPassword().latin1();
771 reset_user_value = true;
772 smtp->setUser(user);
773 smtp->setPassword(pass);
774 } else {
775 return true;
776 }
777 }
778
779
758 mailsToSend.setAutoDelete(false); 780 mailsToSend.setAutoDelete(false);
759 sendProgress = new progressMailSend(); 781 sendProgress = new progressMailSend();
760 sendProgress->show(); 782 sendProgress->show();
761 sendProgress->setMaxMails(mailsToSend.count()); 783 sendProgress->setMaxMails(mailsToSend.count());
762 784
763 while (mailsToSend.count()>0) { 785 while (mailsToSend.count()>0) {
764 if (sendQueuedMail(wrap,smtp,mailsToSend.at(0))==0) { 786 if (sendQueuedMail(wrap,smtp,mailsToSend.at(0))==0) {
765 QMessageBox::critical(0,tr("Error sending mail"), 787 QMessageBox::critical(0,tr("Error sending mail"),
766 tr("Error sending queued mail - breaking")); 788 tr("Error sending queued mail - breaking"));
767 returnValue = false; 789 returnValue = false;
768 break; 790 break;
769 } 791 }
770 mailsToRemove.append(mailsToSend.at(0)); 792 mailsToRemove.append(mailsToSend.at(0));
771 mailsToSend.removeFirst(); 793 mailsToSend.removeFirst();
772 sendProgress->setCurrentMails(mailsToRemove.count()); 794 sendProgress->setCurrentMails(mailsToRemove.count());
773 } 795 }
796 if (reset_user_value) {
797 smtp->setUser(oldUser);
798 smtp->setPassword(oldPw);
799 }
774 Config cfg( "mail" ); 800 Config cfg( "mail" );
775 cfg.setGroup( "Status" ); 801 cfg.setGroup( "Status" );
776 m_queuedMail = 0; 802 m_queuedMail = 0;
777 cfg.writeEntry( "outgoing", m_queuedMail ); 803 cfg.writeEntry( "outgoing", m_queuedMail );
778 emit queuedMails( m_queuedMail ); 804 emit queuedMails( m_queuedMail );
779 sendProgress->hide(); 805 sendProgress->hide();
780 delete sendProgress; 806 delete sendProgress;
781 sendProgress = 0; 807 sendProgress = 0;
782 wrap->deleteMails(mbox,mailsToRemove); 808 wrap->deleteMails(mbox,mailsToRemove);
783 mailsToSend.setAutoDelete(true); 809 mailsToSend.setAutoDelete(true);
784 delete wrap; 810 delete wrap;
785 return returnValue; 811 return returnValue;
786} 812}