00001 /* 00002 Copyright 2010 Ulrich Weigelt <ulrich.weigelt@gmx.de> 00003 00004 This program is free software; you can redistribute it and/or 00005 modify it under the terms of the GNU General Public License as 00006 published by the Free Software Foundation; either version 2 of 00007 the License, or (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU General Public License for more details. 00013 00014 You should have received a copy of the GNU General Public License 00015 along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #ifndef ACCOUNT_H 00019 #define ACCOUNT_H 00020 00021 class Account; 00022 00023 //C++ headers 00024 #include <iostream> 00025 #include <unistd.h> 00026 00027 //Qt header 00028 #include <QObject> 00029 #include <QList> 00030 #include <QApplication> 00031 #include <QTimer> 00032 #include <QTextCodec> 00033 #include <QDir> 00034 #include <QSslCertificate> 00035 #include <QThread> 00036 #include <QByteArray> 00037 #include <QPointer> 00038 00039 //KDE headers 00040 #include <KConfigGroup> 00041 #include <KConfig> 00042 #include <KGlobal> 00043 #include <KPasswordDialog> 00044 #include <kcodecs.h> 00045 #include <ktcpsocket.h> 00046 #include <KApplication> 00047 #include <KDE/KLocale> 00048 #include <KMessageBox> 00049 #include <kde_file.h> 00050 00051 00052 //KShowmail headers 00053 #include "mail.h" 00054 #include "types.h" 00055 #include "constants.h" 00056 #include "encryption.h" 00057 #include "kwalletaccess.h" 00058 #include "maillist.h" 00059 #include "accountlist.h" 00060 #include "corruptdataexception.h" 00061 #include "headerfilter.h" 00062 #include "filterlog.h" 00063 #include "showmaildialog.h" 00064 #include "kshowmail.h" 00065 #include "accountviewitem.h" 00066 00070 namespace POP3Constants 00071 { 00075 static const QString RESPONSE_POSITIVE( "+OK" ); 00076 00080 static const QString RESPONSE_NEGATIVE( "-ERR" ); 00081 00085 static const QString END_MULTILINE_RESPONSE( "." ); 00086 00090 static const QString CAPA_REQUEST( "CAPA" ); 00091 00095 static const QString AUTH_REQUEST( "AUTH" ); 00096 00100 static const QString CAPA_RESPONSE_STLS( "STLS" ); 00101 00105 static const QString START_TLS( "STLS" ); 00106 00110 static const QString COMMIT( "QUIT" ); 00111 00115 static const QString LOGIN_USER( "USER" ); 00116 00120 static const QString LOGIN_PASSWD( "PASS" ); 00121 00125 static const QString LOGIN_APOP( "APOP" ); 00126 00130 static const QString UID_LIST( "UIDL" ); 00131 00135 static const QString MAIL_LIST( "LIST" ); 00136 00140 static const QString GET_HEADER( "TOP" ); 00141 00145 static const QString DELETE( "DELE" ); 00146 00150 static const QString GET_MAIL( "RETR" ); 00151 } 00152 00153 using namespace POP3Constants; 00154 using namespace std; 00155 using namespace Types; 00156 using namespace Encryption; 00157 using namespace KWalletAccess; 00158 00159 class AccountList; 00160 00164 class Account : public QObject 00165 { 00166 00167 Q_OBJECT 00168 00169 public: 00170 00177 Account( QString name, AccountList* accountList, QObject* parent ); 00178 00182 ~Account(); 00183 00187 void print() const; 00188 00193 QString getName() const; 00194 00199 bool isActive() const; 00200 00205 void setActive( bool active ); 00206 00210 void load(); 00211 00221 void refreshMailList( FilterLog* log = NULL ); 00222 00227 QString getPassword() const; 00228 00233 void setPassword( const QString& password ); 00234 00239 void setHost( const QString& host ); 00240 00245 QString getHost() const; 00246 00251 void setProtocol( const QString& protocol ); 00252 00258 QString getProtocol( bool upperCase = false ) const; 00259 00264 void setPort( unsigned short int port ); 00265 00270 unsigned short int getPort() const; 00271 00276 void setUser( const QString& user ); 00277 00282 QString getUser() const; 00283 00290 void setPasswordStorage( int storage ); 00291 00297 int getPasswordStorage() const; 00298 00303 Types::AccountState_Type getState(); 00304 00309 bool isUnsecureLoginAllowed() const; 00310 00315 int getNumberMails() const; 00316 00324 void addMailToDelete( int number ); 00325 00333 void addMailToShow( int number ); 00334 00341 virtual void deleteMails(); 00342 00347 int numberDeletedMailsLastRefresh(); 00348 00353 int numberDeletedMailsStart(); 00354 00359 int numberMovedMailsLastRefresh(); 00360 00365 int numberMovedMailsStart(); 00366 00371 int numberIgnoredMails(); 00372 00378 void reloadFilterSettings(); 00379 00388 void saveOptions( QDomDocument& doc, QDomElement& parent ); 00389 00398 void showMails(); 00399 00404 int getNumberNewMails(); 00405 00410 long getTotalSize() const; 00411 00415 QString getTotalSizeUnit() const; 00416 00421 void readStoredMails( QDomElement& parent ); 00422 00430 int compare( Account* other, AccountSort_Type property ); 00431 00436 QList<Mail> getAllMails() const; 00437 00441 void cancelTask(); 00442 00447 AccountViewItem getViewItem(); 00448 00449 00450 protected: 00451 00455 void init(); 00456 00461 bool hasPassword() const; 00462 00471 bool assertPassword( bool force = false ); 00472 00476 void doConnect(); 00477 00481 void closeConnection(); 00482 00488 void initBeforeConnect(); 00489 00494 void handleError( QString error ); 00495 00501 QStringList readfromSocket( bool singleLine ); 00502 00507 void sendCommand( const QString& command ); 00508 00514 void getCapabilities(); 00515 00520 void printServerMessage( QStringList& text ) const; 00521 00527 bool isPositiveServerMessage( QStringList& message ) const; 00528 00537 void getAuthMech(); 00538 00546 virtual void commit(); 00547 00557 void finishTask(); 00558 00565 void loginUser(); 00566 00573 void loginPasswd(); 00574 00581 void loginApop(); 00582 00588 QString removeStatusIndicator( const QString& message ); 00589 00594 void removeStatusIndicator( QStringList* response ); 00595 00600 void removeEndOfResponseMarker( QStringList* response ); 00601 00608 void getUIDList(); 00609 00615 void swapMailLists(); 00616 00623 void getMailSizes(); 00624 00628 void getHeaders(); 00629 00641 virtual void getNextHeader(); 00642 00649 bool isNegativeResponse( const QString& response ); 00650 00656 void copyHeaders(); 00657 00667 void deleteNextMail(); 00668 00679 void showNextMail(); 00680 00688 void doDownloadActions(); 00689 00700 void getNextMailForDownloadActions(); 00701 00711 void applyFiltersDeleted(); 00712 00719 void commitBeforeRefresh(); 00720 00733 void applyFilters(); 00734 00741 bool writeToMailBox( const QStringList& mail, const QString& box ); 00742 00748 bool isMailDir( const QDir& path ); 00749 00755 bool isSpam( QStringList mail ) const; 00756 00761 bool isSpamAssassinRunning() const; 00762 00766 void startTLS(); 00767 00768 00769 protected slots: 00770 00775 virtual void slotConnected(); 00776 00781 virtual void slotHostFound(); 00782 00788 void slotSocketError( KTcpSocket::Error errorCode ); 00789 00795 void slotSSLError( const QList<KSslError>& errors ); 00796 00800 void slotReadFirstServerMessage(); 00801 00806 void slotCapabilitiesResponse(); 00807 00814 void slotAuthMechResponse(); 00815 00821 void slotCommitResponse(); 00822 00827 void slotLoginUserResponse(); 00828 00833 void slotLoginPasswdResponse(); 00834 00839 void slotLoginApopResponse(); 00840 00845 void slotUIDListResponse(); 00846 00852 void slotMailSizesResponse(); 00853 00863 void slotGetHeaderResponse(); 00864 00875 void slotMailDeleted(); 00876 00888 void slotBodyDownloaded(); 00889 00904 void slotMailDownloadedForAction(); 00905 00911 void slotCommitBeforeRefreshDone(); 00912 00919 void slotTimeout(); 00920 00924 void slotStartTLSResponse(); 00925 00926 private: 00927 00931 QString name; 00932 00936 bool active; 00937 00941 TransferSecurity_Type transferSecurity; 00942 00946 KUrl url; 00947 00956 int passwordStorage; 00957 00961 MailList* mails; 00962 00968 MailList* tempMailList; 00969 00970 00974 QPointer<KTcpSocket> socket; 00975 00979 AccountList* accountList; 00980 00987 bool quitSent; 00988 00992 QTimer* timeoutTimer; 00993 00997 Types::AccountState_Type state; 00998 01002 bool apopAvail; 01003 01009 bool dontUseAPOP; 01010 01014 QString apopTimestamp; 01015 01019 bool allowUnsecureLogin; 01020 01029 bool filterApplied; 01030 01036 bool refreshPerformedByFilters; 01037 01046 MailNumberList_Type newMails; 01047 01056 MailNumberList_Type mailsToDelete; 01057 01065 MailToDownloadMap_Type mailsToDownload; 01066 01075 MailNumberList_Type mailsToShow; 01076 01082 FilterLog* fLog; 01083 01087 int nmbDeletedMailsLastRefresh; 01088 01092 int nmbMovedMailsLastRefresh; 01093 01097 int nmbMovedMailsLastStart; 01098 01102 int nmbIgnoredMails; 01103 01107 int moveCounter; 01108 01112 int nmbDeletedMailsLastStart; 01113 01119 bool downloadActionsInvoked; 01120 01124 HeaderFilter headerFilter; 01125 01131 bool deletionPerformedByFilters; 01132 01136 QString spamMailbox; 01137 01141 FilterAction_Type spamAction; 01142 01146 bool dontHandleError; 01147 01153 bool informAboutErrors; 01154 01158 int timeOutTime; 01159 01166 bool supportsStartTLS; 01167 01168 01169 signals: 01170 01175 void sigDeleteReady( QString account ); 01176 01182 void sigShowBodiesReady( QString account ); 01183 01187 void sigConfigChanged(); 01188 01192 void sigMessageWindowOpened(); 01193 01197 void sigMessageWindowClosed(); 01198 01203 void sigRefreshReady( QString account ); 01204 01205 01206 01207 }; 01208 01209 #endif // ACCOUNT_H