summaryrefslogtreecommitdiffabout
path: root/kmicromail/libmailwrapper
Unidiff
Diffstat (limited to 'kmicromail/libmailwrapper') (more/less context) (ignore whitespace changes)
-rw-r--r--kmicromail/libmailwrapper/imapwrapper.cpp6
1 files changed, 2 insertions, 4 deletions
diff --git a/kmicromail/libmailwrapper/imapwrapper.cpp b/kmicromail/libmailwrapper/imapwrapper.cpp
index 09e52b8..4f0c849 100644
--- a/kmicromail/libmailwrapper/imapwrapper.cpp
+++ b/kmicromail/libmailwrapper/imapwrapper.cpp
@@ -1,275 +1,273 @@
1// CHANGED 2004-09-31 Lutz Rogowski 1// CHANGED 2004-09-31 Lutz Rogowski
2#include <stdlib.h> 2#include <stdlib.h>
3#include <libetpan/libetpan.h> 3#include <libetpan/libetpan.h>
4#include <qpe/global.h> 4#include <qpe/global.h>
5#include <qapplication.h> 5#include <qapplication.h>
6#include "imapwrapper.h" 6#include "imapwrapper.h"
7#include "mailtypes.h" 7#include "mailtypes.h"
8#include "logindialog.h" 8#include "logindialog.h"
9#include <qprogressbar.h> 9#include <qprogressbar.h>
10#include "genericwrapper.h" 10#include "genericwrapper.h"
11#include <kglobal.h> 11#include <kglobal.h>
12 12
13using namespace Opie::Core; 13using namespace Opie::Core;
14int IMAPwrapper::mMax = 0; 14int IMAPwrapper::mMax = 0;
15int IMAPwrapper::mCurrent = 0; 15int IMAPwrapper::mCurrent = 0;
16 16
17IMAPwrapper::IMAPwrapper( IMAPaccount *a ) 17IMAPwrapper::IMAPwrapper( IMAPaccount *a )
18 : AbstractMail() 18 : AbstractMail()
19{ 19{
20 account = a; 20 account = a;
21 m_imap = 0; 21 m_imap = 0;
22 m_Lastmbox = ""; 22 m_Lastmbox = "";
23 mCurrent = 0; 23 mCurrent = 0;
24 mMax = 0; 24 mMax = 0;
25} 25}
26 26
27IMAPwrapper::~IMAPwrapper() 27IMAPwrapper::~IMAPwrapper()
28{ 28{
29 logout(); 29 logout();
30} 30}
31 31
32/* to avoid to often select statements in loops etc. 32/* to avoid to often select statements in loops etc.
33 we trust that we are logged in and connection is established!*/ 33 we trust that we are logged in and connection is established!*/
34int IMAPwrapper::selectMbox(const QString&mbox) 34int IMAPwrapper::selectMbox(const QString&mbox)
35{ 35{
36 if (mbox == m_Lastmbox) { 36 if (mbox == m_Lastmbox) {
37 return MAILIMAP_NO_ERROR; 37 return MAILIMAP_NO_ERROR;
38 } 38 }
39 int err = mailimap_select( m_imap, (char*)mbox.latin1()); 39 int err = mailimap_select( m_imap, (char*)mbox.latin1());
40 if ( err != MAILIMAP_NO_ERROR ) { 40 if ( err != MAILIMAP_NO_ERROR ) {
41 m_Lastmbox = ""; 41 m_Lastmbox = "";
42 return err; 42 return err;
43 } 43 }
44 m_Lastmbox = mbox; 44 m_Lastmbox = mbox;
45 return err; 45 return err;
46} 46}
47 47
48void IMAPwrapper::imap_progress( size_t current, size_t maximum ) 48void IMAPwrapper::imap_progress( size_t current, size_t maximum )
49{ 49{
50 qApp->processEvents();
51 return; 50 return;
52 //qDebug("imap progress %d of %d ",current,maximum ); 51 //qDebug("imap progress %d of %d ",current,maximum );
53 //Global::statusMessage(i18n("Downloading message %1 of %2").arg( current).arg(maximum)); 52 //Global::statusMessage(i18n("Downloading message %1 of %2").arg( current).arg(maximum));
54 //qApp->processEvents() 53 //qApp->processEvents()
55 static unsigned int last = 0; 54 static unsigned int last = 0;
56 if ( last != current ) 55 if ( last != current )
57 IMAPwrapper::progress(); 56 IMAPwrapper::progress();
58 last = current; 57 last = current;
59} 58}
60void IMAPwrapper::progress( QString m ) 59void IMAPwrapper::progress( QString m )
61{ 60{
62 static QString mProgrMess; 61 static QString mProgrMess;
63 if ( m != QString::null ) { 62 if ( m != QString::null ) {
64 mProgrMess = m; 63 mProgrMess = m;
65 mCurrent = 1; 64 mCurrent = 1;
66 return; 65 return;
67 } 66 }
68 QString mess; 67 QString mess;
69 //qDebug("progress "); 68 //qDebug("progress ");
70 if ( mMax ) mess = mProgrMess +i18n(" message %1 of %2").arg( mCurrent++).arg(mMax); 69 if ( mMax ) mess = mProgrMess +i18n(" message %1 of %2").arg( mCurrent++).arg(mMax);
71 else mess = mProgrMess +i18n(" message %1").arg( mCurrent++); 70 else mess = mProgrMess +i18n(" message %1").arg( mCurrent++);
72 Global::statusMessage(mess); 71 Global::statusMessage(mess);
73 //qDebug("Progress %s %s", mess.latin1(), m.latin1()); 72 //qDebug("Progress %s %s", mess.latin1(), m.latin1());
74 qApp->processEvents(); 73 qApp->processEvents();
75} 74}
76bool IMAPwrapper::start_tls(bool force_tls) 75bool IMAPwrapper::start_tls(bool force_tls)
77{ 76{
78 int err; 77 int err;
79 bool try_tls = force_tls; 78 bool try_tls = force_tls;
80 mailimap_capability_data * cap_data = 0; 79 mailimap_capability_data * cap_data = 0;
81 80
82 err = mailimap_capability(m_imap,&cap_data); 81 err = mailimap_capability(m_imap,&cap_data);
83 if (err != MAILIMAP_NO_ERROR) { 82 if (err != MAILIMAP_NO_ERROR) {
84 Global::statusMessage("error getting capabilities!"); 83 Global::statusMessage("error getting capabilities!");
85 return false; 84 return false;
86 } 85 }
87 clistiter * cur; 86 clistiter * cur;
88 for(cur = clist_begin(cap_data->cap_list) ; cur != NULL;cur = clist_next(cur)) { 87 for(cur = clist_begin(cap_data->cap_list) ; cur != NULL;cur = clist_next(cur)) {
89 struct mailimap_capability * cap; 88 struct mailimap_capability * cap;
90 cap = (struct mailimap_capability *)clist_content(cur); 89 cap = (struct mailimap_capability *)clist_content(cur);
91 if (cap->cap_type == MAILIMAP_CAPABILITY_NAME) { 90 if (cap->cap_type == MAILIMAP_CAPABILITY_NAME) {
92 if (strcasecmp(cap->cap_data.cap_name, "STARTTLS") == 0) { 91 if (strcasecmp(cap->cap_data.cap_name, "STARTTLS") == 0) {
93 try_tls = true; 92 try_tls = true;
94 break; 93 break;
95 } 94 }
96 } 95 }
97 } 96 }
98 if (cap_data) { 97 if (cap_data) {
99 mailimap_capability_data_free(cap_data); 98 mailimap_capability_data_free(cap_data);
100 } 99 }
101 if (try_tls) { 100 if (try_tls) {
102 err = mailimap_starttls(m_imap); 101 err = mailimap_starttls(m_imap);
103 if (err != MAILIMAP_NO_ERROR && force_tls) { 102 if (err != MAILIMAP_NO_ERROR && force_tls) {
104 Global::statusMessage(i18n("Server has no TLS support!")); 103 Global::statusMessage(i18n("Server has no TLS support!"));
105 try_tls = false; 104 try_tls = false;
106 } else { 105 } else {
107 mailstream_low * low; 106 mailstream_low * low;
108 mailstream_low * new_low; 107 mailstream_low * new_low;
109 low = mailstream_get_low(m_imap->imap_stream); 108 low = mailstream_get_low(m_imap->imap_stream);
110 if (!low) { 109 if (!low) {
111 try_tls = false; 110 try_tls = false;
112 } else { 111 } else {
113 int fd = mailstream_low_get_fd(low); 112 int fd = mailstream_low_get_fd(low);
114 if (fd > -1 && (new_low = mailstream_low_ssl_open(fd))!=0) { 113 if (fd > -1 && (new_low = mailstream_low_ssl_open(fd))!=0) {
115 mailstream_low_free(low); 114 mailstream_low_free(low);
116 mailstream_set_low(m_imap->imap_stream, new_low); 115 mailstream_set_low(m_imap->imap_stream, new_low);
117 } else { 116 } else {
118 try_tls = false; 117 try_tls = false;
119 } 118 }
120 } 119 }
121 } 120 }
122 } 121 }
123 return try_tls; 122 return try_tls;
124} 123}
125 124
126void IMAPwrapper::login(bool tryTLS) // = true) 125void IMAPwrapper::login(bool tryTLS) // = true)
127{ 126{
128 QString server, user, pass; 127 QString server, user, pass;
129 uint16_t port; 128 uint16_t port;
130 int err = MAILIMAP_NO_ERROR; 129 int err = MAILIMAP_NO_ERROR;
131 130
132 if (account->getOffline()) return; 131 if (account->getOffline()) return;
133 /* we are connected this moment */ 132 /* we are connected this moment */
134 /* TODO: setup a timer holding the line or if connection closed - delete the value */ 133 /* TODO: setup a timer holding the line or if connection closed - delete the value */
135 if (m_imap) { 134 if (m_imap) {
136 err = mailimap_noop(m_imap); 135 err = mailimap_noop(m_imap);
137 if (err!=MAILIMAP_NO_ERROR) { 136 if (err!=MAILIMAP_NO_ERROR) {
138 logout(); 137 logout();
139 } else { 138 } else {
140 mailstream_flush(m_imap->imap_stream); 139 mailstream_flush(m_imap->imap_stream);
141 return; 140 return;
142 } 141 }
143 } 142 }
144 server = account->getServer(); 143 server = account->getServer();
145 port = account->getPort().toUInt(); 144 port = account->getPort().toUInt();
146 if ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) { 145 if ( account->getUser().isEmpty() || account->getPassword().isEmpty() ) {
147 LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true ); 146 LoginDialog login( account->getUser(), account->getPassword(), NULL, 0, true );
148 login.show(); 147 login.show();
149 if ( QDialog::Accepted == login.exec() ) { 148 if ( QDialog::Accepted == login.exec() ) {
150 // ok 149 // ok
151 user = login.getUser(); 150 user = login.getUser();
152 pass = login.getPassword(); 151 pass = login.getPassword();
153 } else { 152 } else {
154 // cancel 153 // cancel
155 return; 154 return;
156 } 155 }
157 } else { 156 } else {
158 user = account->getUser(); 157 user = account->getUser();
159 pass = account->getPassword(); 158 pass = account->getPassword();
160 } 159 }
161
162 m_imap = mailimap_new( 20, &imap_progress ); 160 m_imap = mailimap_new( 20, &imap_progress );
163 161
164 /* connect */ 162 /* connect */
165 bool ssl = false; 163 bool ssl = false;
166 bool try_tls = false; 164 bool try_tls = false;
167 bool force_tls = false; 165 bool force_tls = false;
168 166
169 if ( account->ConnectionType() == 2 ) { 167 if ( account->ConnectionType() == 2 ) {
170 ssl = true; 168 ssl = true;
171 } 169 }
172 if (account->ConnectionType()==1) { 170 if (account->ConnectionType()==1) {
173 force_tls = true; 171 force_tls = true;
174 } 172 }
175 173
176 if ( ssl ) { 174 if ( ssl ) {
177 qDebug("using ssl "); 175 //qDebug("using ssl ");
178 err = mailimap_ssl_connect( m_imap, (char*)server.latin1(), port ); 176 err = mailimap_ssl_connect( m_imap, (char*)server.latin1(), port );
179 qDebug("back "); 177 //qDebug("back ");
180 } else { 178 } else {
181 err = mailimap_socket_connect( m_imap, (char*)server.latin1(), port ); 179 err = mailimap_socket_connect( m_imap, (char*)server.latin1(), port );
182 } 180 }
183 181
184 if ( err != MAILIMAP_NO_ERROR && 182 if ( err != MAILIMAP_NO_ERROR &&
185 err != MAILIMAP_NO_ERROR_AUTHENTICATED && 183 err != MAILIMAP_NO_ERROR_AUTHENTICATED &&
186 err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) { 184 err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) {
187 QString failure = ""; 185 QString failure = "";
188 if (err == MAILIMAP_ERROR_CONNECTION_REFUSED) { 186 if (err == MAILIMAP_ERROR_CONNECTION_REFUSED) {
189 failure="Connection refused"; 187 failure="Connection refused";
190 } else { 188 } else {
191 failure="Unknown failure"; 189 failure="Unknown failure";
192 } 190 }
193 Global::statusMessage(i18n("error connecting imap server: %1").arg(failure)); 191 Global::statusMessage(i18n("error connecting imap server: %1").arg(failure));
194 mailimap_free( m_imap ); 192 mailimap_free( m_imap );
195 m_imap = 0; 193 m_imap = 0;
196 return; 194 return;
197 } 195 }
198 if ( tryTLS ) { 196 if ( tryTLS ) {
199 if (!ssl) { 197 if (!ssl) {
200 try_tls = start_tls(force_tls); 198 try_tls = start_tls(force_tls);
201 } 199 }
202 } 200 }
203 bool ok = true; 201 bool ok = true;
204 if (force_tls && !try_tls) { 202 if (force_tls && !try_tls) {
205 Global::statusMessage(i18n("Server has no TLS support!")); 203 Global::statusMessage(i18n("Server has no TLS support!"));
206 ok = false; 204 ok = false;
207 } 205 }
208 206
209 207
210 /* login */ 208 /* login */
211 209
212 if (ok) { 210 if (ok) {
213 err = mailimap_login_simple( m_imap, (char*)user.latin1(), (char*)pass.latin1() ); 211 err = mailimap_login_simple( m_imap, (char*)user.latin1(), (char*)pass.latin1() );
214 if ( err != MAILIMAP_NO_ERROR ) { 212 if ( err != MAILIMAP_NO_ERROR ) {
215 if ( tryTLS && !force_tls && !try_tls ) { 213 if ( tryTLS && !force_tls && !try_tls ) {
216 err = mailimap_close( m_imap ); 214 err = mailimap_close( m_imap );
217 mailimap_free( m_imap ); 215 mailimap_free( m_imap );
218 m_imap = 0; 216 m_imap = 0;
219 login( false ); 217 login( false );
220 return; 218 return;
221 } 219 }
222 Global::statusMessage(i18n("error logging in imap server: %1").arg(m_imap->imap_response)); 220 Global::statusMessage(i18n("error logging in imap server: %1").arg(m_imap->imap_response));
223 ok = false; 221 ok = false;
224 } 222 }
225 } 223 }
226 if (!ok) { 224 if (!ok) {
227 err = mailimap_close( m_imap ); 225 err = mailimap_close( m_imap );
228 mailimap_free( m_imap ); 226 mailimap_free( m_imap );
229 m_imap = 0; 227 m_imap = 0;
230 } 228 }
231} 229}
232 230
233void IMAPwrapper::logout() 231void IMAPwrapper::logout()
234{ 232{
235 int err = MAILIMAP_NO_ERROR; 233 int err = MAILIMAP_NO_ERROR;
236 if (!m_imap) return; 234 if (!m_imap) return;
237 err = mailimap_logout( m_imap ); 235 err = mailimap_logout( m_imap );
238 err = mailimap_close( m_imap ); 236 err = mailimap_close( m_imap );
239 mailimap_free( m_imap ); 237 mailimap_free( m_imap );
240 m_imap = 0; 238 m_imap = 0;
241 m_Lastmbox = ""; 239 m_Lastmbox = "";
242} 240}
243 241
244void IMAPwrapper::listMessages(const QString&mailbox,QValueList<Opie::Core::OSmartPointer<RecMail> > &target , int maxSizeInKb) 242void IMAPwrapper::listMessages(const QString&mailbox,QValueList<Opie::Core::OSmartPointer<RecMail> > &target , int maxSizeInKb)
245{ 243{
246 244
247 int tryAgain = 1; 245 int tryAgain = 1;
248 while ( tryAgain >= 0 ) { 246 while ( tryAgain >= 0 ) {
249 int err = MAILIMAP_NO_ERROR; 247 int err = MAILIMAP_NO_ERROR;
250 clist *result = 0; 248 clist *result = 0;
251 clistcell *current; 249 clistcell *current;
252 mailimap_fetch_type *fetchType = 0; 250 mailimap_fetch_type *fetchType = 0;
253 mailimap_set *set = 0; 251 mailimap_set *set = 0;
254 252
255 login(); 253 login();
256 if (!m_imap) { 254 if (!m_imap) {
257 return; 255 return;
258 } 256 }
259 /* select mailbox READONLY for operations */ 257 /* select mailbox READONLY for operations */
260 err = selectMbox(mailbox); 258 err = selectMbox(mailbox);
261 if ( err != MAILIMAP_NO_ERROR ) { 259 if ( err != MAILIMAP_NO_ERROR ) {
262 return; 260 return;
263 } 261 }
264 262
265 int last = m_imap->imap_selection_info->sel_exists; 263 int last = m_imap->imap_selection_info->sel_exists;
266 264
267 if (last == 0) { 265 if (last == 0) {
268 Global::statusMessage(i18n("Mailbox has no mails")); 266 Global::statusMessage(i18n("Mailbox has no mails"));
269 return; 267 return;
270 } else { 268 } else {
271 } 269 }
272 progress( i18n("Fetch ")); 270 progress( i18n("Fetch "));
273 mMax = last; 271 mMax = last;
274 //qDebug("last %d ", last); 272 //qDebug("last %d ", last);
275 Global::statusMessage(i18n("Fetching header list")); 273 Global::statusMessage(i18n("Fetching header list"));