-rw-r--r-- | noncore/net/mail/libmailwrapper/imapwrapper.cpp | 84 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/imapwrapper.h | 1 |
2 files changed, 79 insertions, 6 deletions
diff --git a/noncore/net/mail/libmailwrapper/imapwrapper.cpp b/noncore/net/mail/libmailwrapper/imapwrapper.cpp index 098dbdc..1dfcc4c 100644 --- a/noncore/net/mail/libmailwrapper/imapwrapper.cpp +++ b/noncore/net/mail/libmailwrapper/imapwrapper.cpp | |||
@@ -12,139 +12,211 @@ IMAPwrapper::IMAPwrapper( IMAPaccount *a ) | |||
12 | account = a; | 12 | account = a; |
13 | m_imap = 0; | 13 | m_imap = 0; |
14 | m_Lastmbox = ""; | 14 | m_Lastmbox = ""; |
15 | } | 15 | } |
16 | 16 | ||
17 | IMAPwrapper::~IMAPwrapper() | 17 | IMAPwrapper::~IMAPwrapper() |
18 | { | 18 | { |
19 | logout(); | 19 | logout(); |
20 | } | 20 | } |
21 | 21 | ||
22 | /* to avoid to often select statements in loops etc. | 22 | /* to avoid to often select statements in loops etc. |
23 | we trust that we are logged in and connection is established!*/ | 23 | we trust that we are logged in and connection is established!*/ |
24 | int IMAPwrapper::selectMbox(const QString&mbox) | 24 | int IMAPwrapper::selectMbox(const QString&mbox) |
25 | { | 25 | { |
26 | if (mbox == m_Lastmbox) { | 26 | if (mbox == m_Lastmbox) { |
27 | return MAILIMAP_NO_ERROR; | 27 | return MAILIMAP_NO_ERROR; |
28 | } | 28 | } |
29 | int err = mailimap_select( m_imap, (char*)mbox.latin1()); | 29 | int err = mailimap_select( m_imap, (char*)mbox.latin1()); |
30 | if ( err != MAILIMAP_NO_ERROR ) { | 30 | if ( err != MAILIMAP_NO_ERROR ) { |
31 | qDebug("error selecting mailbox: %s",m_imap->imap_response); | 31 | qDebug("error selecting mailbox: %s",m_imap->imap_response); |
32 | m_Lastmbox = ""; | 32 | m_Lastmbox = ""; |
33 | return err; | 33 | return err; |
34 | } | 34 | } |
35 | m_Lastmbox = mbox; | 35 | m_Lastmbox = mbox; |
36 | return err; | 36 | return err; |
37 | } | 37 | } |
38 | 38 | ||
39 | void IMAPwrapper::imap_progress( size_t current, size_t maximum ) | 39 | void IMAPwrapper::imap_progress( size_t current, size_t maximum ) |
40 | { | 40 | { |
41 | qDebug( "IMAP: %i of %i", current, maximum ); | 41 | qDebug( "IMAP: %i of %i", current, maximum ); |
42 | } | 42 | } |
43 | 43 | ||
44 | bool IMAPwrapper::start_tls(bool force_tls) | ||
45 | { | ||
46 | int err; | ||
47 | bool try_tls; | ||
48 | mailimap_capability_data * cap_data = 0; | ||
49 | |||
50 | err = mailimap_capability(m_imap,&cap_data); | ||
51 | if (err != MAILIMAP_NO_ERROR) { | ||
52 | Global::statusMessage("error getting capabilities!"); | ||
53 | qDebug("error getting capabilities!"); | ||
54 | return false; | ||
55 | } | ||
56 | clistiter * cur; | ||
57 | for(cur = clist_begin(cap_data->cap_list) ; cur != NULL;cur = clist_next(cur)) { | ||
58 | struct mailimap_capability * cap; | ||
59 | cap = (struct mailimap_capability *)clist_content(cur); | ||
60 | if (cap->cap_type == MAILIMAP_CAPABILITY_NAME) { | ||
61 | if (strcasecmp(cap->cap_data.cap_name, "STARTTLS") == 0) { | ||
62 | try_tls = true; | ||
63 | break; | ||
64 | } | ||
65 | } | ||
66 | } | ||
67 | if (cap_data) { | ||
68 | mailimap_capability_data_free(cap_data); | ||
69 | } | ||
70 | if (try_tls) { | ||
71 | err = mailimap_starttls(m_imap); | ||
72 | if (err != MAILIMAP_NO_ERROR && force_tls) { | ||
73 | Global::statusMessage(tr("Server has no TLS support!")); | ||
74 | qDebug("Server has no TLS support!"); | ||
75 | try_tls = false; | ||
76 | } else { | ||
77 | mailstream_low * low; | ||
78 | mailstream_low * new_low; | ||
79 | low = mailstream_get_low(m_imap->imap_stream); | ||
80 | if (!low) { | ||
81 | try_tls = false; | ||
82 | } else { | ||
83 | int fd = mailstream_low_get_fd(low); | ||
84 | if (fd > -1 && (new_low = mailstream_low_ssl_open(fd))!=0) { | ||
85 | mailstream_low_free(low); | ||
86 | mailstream_set_low(m_imap->imap_stream, new_low); | ||
87 | } else { | ||
88 | try_tls = false; | ||
89 | } | ||
90 | } | ||
91 | } | ||
92 | } | ||
93 | return try_tls; | ||
94 | } | ||
95 | |||
44 | void IMAPwrapper::login() | 96 | void IMAPwrapper::login() |
45 | { | 97 | { |
46 | const char *server, *user, *pass; | 98 | const char *server, *user, *pass; |
47 | uint16_t port; | 99 | uint16_t port; |
48 | int err = MAILIMAP_NO_ERROR; | 100 | int err = MAILIMAP_NO_ERROR; |
49 | 101 | ||
50 | if (account->getOffline()) return; | 102 | if (account->getOffline()) return; |
51 | /* we are connected this moment */ | 103 | /* we are connected this moment */ |
52 | /* TODO: setup a timer holding the line or if connection closed - delete the value */ | 104 | /* TODO: setup a timer holding the line or if connection closed - delete the value */ |
53 | if (m_imap) { | 105 | if (m_imap) { |
54 | err = mailimap_noop(m_imap); | 106 | err = mailimap_noop(m_imap); |
55 | if (err!=MAILIMAP_NO_ERROR) { | 107 | if (err!=MAILIMAP_NO_ERROR) { |
56 | logout(); | 108 | logout(); |
57 | } else { | 109 | } else { |
58 | mailstream_flush(m_imap->imap_stream); | 110 | mailstream_flush(m_imap->imap_stream); |
59 | return; | 111 | return; |
60 | } | 112 | } |
61 | } | 113 | } |
62 | server = account->getServer().latin1(); | 114 | server = account->getServer().latin1(); |
63 | port = account->getPort().toUInt(); | 115 | port = account->getPort().toUInt(); |
64 | if ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) { | 116 | if ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) { |
65 | LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true ); | 117 | LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true ); |
66 | login.show(); | 118 | login.show(); |
67 | if ( QDialog::Accepted == login.exec() ) { | 119 | if ( QDialog::Accepted == login.exec() ) { |
68 | // ok | 120 | // ok |
69 | user = login.getUser().latin1(); | 121 | user = login.getUser().latin1(); |
70 | pass = login.getPassword().latin1(); | 122 | pass = login.getPassword().latin1(); |
71 | } else { | 123 | } else { |
72 | // cancel | 124 | // cancel |
73 | qDebug( "IMAP: Login canceled" ); | 125 | qDebug( "IMAP: Login canceled" ); |
74 | return; | 126 | return; |
75 | } | 127 | } |
76 | } else { | 128 | } else { |
77 | user = account->getUser().latin1(); | 129 | user = account->getUser().latin1(); |
78 | pass = account->getPassword().latin1(); | 130 | pass = account->getPassword().latin1(); |
79 | } | 131 | } |
80 | 132 | ||
81 | m_imap = mailimap_new( 20, &imap_progress ); | 133 | m_imap = mailimap_new( 20, &imap_progress ); |
82 | 134 | ||
83 | |||
84 | |||
85 | /* connect */ | 135 | /* connect */ |
86 | |||
87 | bool ssl = false; | 136 | bool ssl = false; |
137 | bool try_tls = false; | ||
138 | bool force_tls = false; | ||
88 | 139 | ||
89 | if ( account->ConnectionType() == 2 ) { | 140 | if ( account->ConnectionType() == 2 ) { |
90 | ssl = true; | 141 | ssl = true; |
91 | } | 142 | } |
143 | if (account->ConnectionType()==1) { | ||
144 | force_tls = true; | ||
145 | } | ||
92 | 146 | ||
93 | if ( ssl ) { | 147 | if ( ssl ) { |
94 | qDebug( "using ssl" ); | 148 | qDebug( "using ssl" ); |
95 | err = mailimap_ssl_connect( m_imap, (char*)server, port ); | 149 | err = mailimap_ssl_connect( m_imap, (char*)server, port ); |
96 | } else { | 150 | } else { |
97 | err = mailimap_socket_connect( m_imap, (char*)server, port ); | 151 | err = mailimap_socket_connect( m_imap, (char*)server, port ); |
98 | } | 152 | } |
99 | 153 | ||
100 | if ( err != MAILIMAP_NO_ERROR && | 154 | if ( err != MAILIMAP_NO_ERROR && |
101 | err != MAILIMAP_NO_ERROR_AUTHENTICATED && | 155 | err != MAILIMAP_NO_ERROR_AUTHENTICATED && |
102 | err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) { | 156 | err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) { |
103 | QString failure = ""; | 157 | QString failure = ""; |
104 | if (err == MAILIMAP_ERROR_CONNECTION_REFUSED) { | 158 | if (err == MAILIMAP_ERROR_CONNECTION_REFUSED) { |
105 | failure="Connection refused"; | 159 | failure="Connection refused"; |
106 | } else { | 160 | } else { |
107 | failure="Unknown failure"; | 161 | failure="Unknown failure"; |
108 | } | 162 | } |
109 | Global::statusMessage(tr("error connecting imap server: %1").arg(failure)); | 163 | Global::statusMessage(tr("error connecting imap server: %1").arg(failure)); |
110 | mailimap_free( m_imap ); | 164 | mailimap_free( m_imap ); |
111 | m_imap = 0; | 165 | m_imap = 0; |
112 | return; | 166 | return; |
113 | } | 167 | } |
114 | 168 | ||
169 | if (!ssl) { | ||
170 | try_tls = start_tls(force_tls); | ||
171 | } | ||
172 | |||
173 | bool ok = true; | ||
174 | if (force_tls && !try_tls) { | ||
175 | Global::statusMessage(tr("Server has no TLS support!")); | ||
176 | qDebug("Server has no TLS support!"); | ||
177 | ok = false; | ||
178 | } | ||
179 | |||
180 | |||
115 | /* login */ | 181 | /* login */ |
116 | err = mailimap_login_simple( m_imap, (char*)user, (char*)pass ); | 182 | |
117 | if ( err != MAILIMAP_NO_ERROR ) { | 183 | if (ok) { |
118 | Global::statusMessage(tr("error logging in imap server: %1").arg(m_imap->imap_response)); | 184 | err = mailimap_login_simple( m_imap, (char*)user, (char*)pass ); |
185 | if ( err != MAILIMAP_NO_ERROR ) { | ||
186 | Global::statusMessage(tr("error logging in imap server: %1").arg(m_imap->imap_response)); | ||
187 | ok = false; | ||
188 | } | ||
189 | } | ||
190 | if (!ok) { | ||
119 | err = mailimap_close( m_imap ); | 191 | err = mailimap_close( m_imap ); |
120 | mailimap_free( m_imap ); | 192 | mailimap_free( m_imap ); |
121 | m_imap = 0; | 193 | m_imap = 0; |
122 | } | 194 | } |
123 | } | 195 | } |
124 | 196 | ||
125 | void IMAPwrapper::logout() | 197 | void IMAPwrapper::logout() |
126 | { | 198 | { |
127 | int err = MAILIMAP_NO_ERROR; | 199 | int err = MAILIMAP_NO_ERROR; |
128 | if (!m_imap) return; | 200 | if (!m_imap) return; |
129 | err = mailimap_logout( m_imap ); | 201 | err = mailimap_logout( m_imap ); |
130 | err = mailimap_close( m_imap ); | 202 | err = mailimap_close( m_imap ); |
131 | mailimap_free( m_imap ); | 203 | mailimap_free( m_imap ); |
132 | m_imap = 0; | 204 | m_imap = 0; |
133 | m_Lastmbox = ""; | 205 | m_Lastmbox = ""; |
134 | } | 206 | } |
135 | 207 | ||
136 | void IMAPwrapper::listMessages(const QString&mailbox,QList<RecMail> &target ) | 208 | void IMAPwrapper::listMessages(const QString&mailbox,QList<RecMail> &target ) |
137 | { | 209 | { |
138 | int err = MAILIMAP_NO_ERROR; | 210 | int err = MAILIMAP_NO_ERROR; |
139 | clist *result = 0; | 211 | clist *result = 0; |
140 | clistcell *current; | 212 | clistcell *current; |
141 | mailimap_fetch_type *fetchType = 0; | 213 | mailimap_fetch_type *fetchType = 0; |
142 | mailimap_set *set = 0; | 214 | mailimap_set *set = 0; |
143 | 215 | ||
144 | login(); | 216 | login(); |
145 | if (!m_imap) { | 217 | if (!m_imap) { |
146 | return; | 218 | return; |
147 | } | 219 | } |
148 | /* select mailbox READONLY for operations */ | 220 | /* select mailbox READONLY for operations */ |
149 | err = selectMbox(mailbox); | 221 | err = selectMbox(mailbox); |
150 | if ( err != MAILIMAP_NO_ERROR ) { | 222 | if ( err != MAILIMAP_NO_ERROR ) { |
diff --git a/noncore/net/mail/libmailwrapper/imapwrapper.h b/noncore/net/mail/libmailwrapper/imapwrapper.h index c10f86a..0a1fe2c 100644 --- a/noncore/net/mail/libmailwrapper/imapwrapper.h +++ b/noncore/net/mail/libmailwrapper/imapwrapper.h | |||
@@ -23,55 +23,56 @@ class IMAPwrapper : public AbstractMail | |||
23 | public: | 23 | public: |
24 | IMAPwrapper( IMAPaccount *a ); | 24 | IMAPwrapper( IMAPaccount *a ); |
25 | virtual ~IMAPwrapper(); | 25 | virtual ~IMAPwrapper(); |
26 | virtual QList<Folder>* listFolders(); | 26 | virtual QList<Folder>* listFolders(); |
27 | virtual void listMessages(const QString & mailbox,QList<RecMail>&target ); | 27 | virtual void listMessages(const QString & mailbox,QList<RecMail>&target ); |
28 | virtual void statusFolder(folderStat&target_stat,const QString & mailbox="INBOX"); | 28 | virtual void statusFolder(folderStat&target_stat,const QString & mailbox="INBOX"); |
29 | 29 | ||
30 | virtual void deleteMail(const RecMail&mail); | 30 | virtual void deleteMail(const RecMail&mail); |
31 | virtual void answeredMail(const RecMail&mail); | 31 | virtual void answeredMail(const RecMail&mail); |
32 | virtual int deleteAllMail(const Folder*folder); | 32 | virtual int deleteAllMail(const Folder*folder); |
33 | virtual void storeMessage(const char*msg,size_t length, const QString&folder); | 33 | virtual void storeMessage(const char*msg,size_t length, const QString&folder); |
34 | virtual void mvcpAllMails(Folder*fromFolder,const QString&targetFolder,AbstractMail*targetWrapper,bool moveit); | 34 | virtual void mvcpAllMails(Folder*fromFolder,const QString&targetFolder,AbstractMail*targetWrapper,bool moveit); |
35 | virtual void mvcpMail(const RecMail&mail,const QString&targetFolder,AbstractMail*targetWrapper,bool moveit); | 35 | virtual void mvcpMail(const RecMail&mail,const QString&targetFolder,AbstractMail*targetWrapper,bool moveit); |
36 | 36 | ||
37 | virtual RecBody fetchBody(const RecMail&mail); | 37 | virtual RecBody fetchBody(const RecMail&mail); |
38 | virtual QString fetchTextPart(const RecMail&mail,const RecPart&part); | 38 | virtual QString fetchTextPart(const RecMail&mail,const RecPart&part); |
39 | virtual encodedString* fetchDecodedPart(const RecMail&mail,const RecPart&part); | 39 | virtual encodedString* fetchDecodedPart(const RecMail&mail,const RecPart&part); |
40 | virtual encodedString* fetchRawPart(const RecMail&mail,const RecPart&part); | 40 | virtual encodedString* fetchRawPart(const RecMail&mail,const RecPart&part); |
41 | virtual encodedString* fetchRawBody(const RecMail&mail); | 41 | virtual encodedString* fetchRawBody(const RecMail&mail); |
42 | 42 | ||
43 | virtual int createMbox(const QString&,const Folder*parentfolder=0,const QString& delemiter="/",bool getsubfolder=false); | 43 | virtual int createMbox(const QString&,const Folder*parentfolder=0,const QString& delemiter="/",bool getsubfolder=false); |
44 | virtual int deleteMbox(const Folder*folder); | 44 | virtual int deleteMbox(const Folder*folder); |
45 | 45 | ||
46 | static void imap_progress( size_t current, size_t maximum ); | 46 | static void imap_progress( size_t current, size_t maximum ); |
47 | 47 | ||
48 | virtual void logout(); | 48 | virtual void logout(); |
49 | virtual const QString&getType()const; | 49 | virtual const QString&getType()const; |
50 | virtual const QString&getName()const; | 50 | virtual const QString&getName()const; |
51 | 51 | ||
52 | protected: | 52 | protected: |
53 | RecMail*parse_list_result(mailimap_msg_att*); | 53 | RecMail*parse_list_result(mailimap_msg_att*); |
54 | void login(); | 54 | void login(); |
55 | bool start_tls(bool force=true); | ||
55 | 56 | ||
56 | virtual QString fetchTextPart(const RecMail&mail,const QValueList<int>&path,bool internal_call=false,const QString&enc=""); | 57 | virtual QString fetchTextPart(const RecMail&mail,const QValueList<int>&path,bool internal_call=false,const QString&enc=""); |
57 | virtual encodedString*fetchRawPart(const RecMail&mail,const QValueList<int>&path,bool internal_call); | 58 | virtual encodedString*fetchRawPart(const RecMail&mail,const QValueList<int>&path,bool internal_call); |
58 | int selectMbox(const QString&mbox); | 59 | int selectMbox(const QString&mbox); |
59 | 60 | ||
60 | void fillSinglePart(RecPart&target_part,mailimap_body_type_1part*Description); | 61 | void fillSinglePart(RecPart&target_part,mailimap_body_type_1part*Description); |
61 | void fillSingleTextPart(RecPart&target_part,mailimap_body_type_text*which); | 62 | void fillSingleTextPart(RecPart&target_part,mailimap_body_type_text*which); |
62 | void fillSingleBasicPart(RecPart&target_part,mailimap_body_type_basic*which); | 63 | void fillSingleBasicPart(RecPart&target_part,mailimap_body_type_basic*which); |
63 | void fillSingleMsgPart(RecPart&target_part,mailimap_body_type_msg*which); | 64 | void fillSingleMsgPart(RecPart&target_part,mailimap_body_type_msg*which); |
64 | void fillMultiPart(RecPart&target_part,mailimap_body_type_mpart*which); | 65 | void fillMultiPart(RecPart&target_part,mailimap_body_type_mpart*which); |
65 | void traverseBody(const RecMail&mail,mailimap_body*body,RecBody&target_body,int current_recursion,QValueList<int>recList,int current_count=1); | 66 | void traverseBody(const RecMail&mail,mailimap_body*body,RecBody&target_body,int current_recursion,QValueList<int>recList,int current_count=1); |
66 | 67 | ||
67 | /* just helpers */ | 68 | /* just helpers */ |
68 | static void fillBodyFields(RecPart&target_part,mailimap_body_fields*which); | 69 | static void fillBodyFields(RecPart&target_part,mailimap_body_fields*which); |
69 | static QStringList address_list_to_stringlist(clist*list); | 70 | static QStringList address_list_to_stringlist(clist*list); |
70 | 71 | ||
71 | 72 | ||
72 | IMAPaccount *account; | 73 | IMAPaccount *account; |
73 | mailimap *m_imap; | 74 | mailimap *m_imap; |
74 | QString m_Lastmbox; | 75 | QString m_Lastmbox; |
75 | }; | 76 | }; |
76 | 77 | ||
77 | #endif | 78 | #endif |