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