-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 | |||
@@ -1,374 +1,446 @@ | |||
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 | 4 | ||
5 | #include "imapwrapper.h" | 5 | #include "imapwrapper.h" |
6 | #include "mailtypes.h" | 6 | #include "mailtypes.h" |
7 | #include "logindialog.h" | 7 | #include "logindialog.h" |
8 | 8 | ||
9 | IMAPwrapper::IMAPwrapper( IMAPaccount *a ) | 9 | IMAPwrapper::IMAPwrapper( IMAPaccount *a ) |
10 | : AbstractMail() | 10 | : AbstractMail() |
11 | { | 11 | { |
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 ) { |
151 | return; | 223 | return; |
152 | } | 224 | } |
153 | 225 | ||
154 | int last = m_imap->imap_selection_info->sel_exists; | 226 | int last = m_imap->imap_selection_info->sel_exists; |
155 | 227 | ||
156 | if (last == 0) { | 228 | if (last == 0) { |
157 | Global::statusMessage(tr("Mailbox has no mails")); | 229 | Global::statusMessage(tr("Mailbox has no mails")); |
158 | return; | 230 | return; |
159 | } else { | 231 | } else { |
160 | } | 232 | } |
161 | 233 | ||
162 | /* the range has to start at 1!!! not with 0!!!! */ | 234 | /* the range has to start at 1!!! not with 0!!!! */ |
163 | set = mailimap_set_new_interval( 1, last ); | 235 | set = mailimap_set_new_interval( 1, last ); |
164 | fetchType = mailimap_fetch_type_new_fetch_att_list_empty(); | 236 | fetchType = mailimap_fetch_type_new_fetch_att_list_empty(); |
165 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_envelope()); | 237 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_envelope()); |
166 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_flags()); | 238 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_flags()); |
167 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_internaldate()); | 239 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_internaldate()); |
168 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_rfc822_size()); | 240 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_rfc822_size()); |
169 | 241 | ||
170 | err = mailimap_fetch( m_imap, set, fetchType, &result ); | 242 | err = mailimap_fetch( m_imap, set, fetchType, &result ); |
171 | mailimap_set_free( set ); | 243 | mailimap_set_free( set ); |
172 | mailimap_fetch_type_free( fetchType ); | 244 | mailimap_fetch_type_free( fetchType ); |
173 | 245 | ||
174 | QString date,subject,from; | 246 | QString date,subject,from; |
175 | 247 | ||
176 | if ( err == MAILIMAP_NO_ERROR ) { | 248 | if ( err == MAILIMAP_NO_ERROR ) { |
177 | mailimap_msg_att * msg_att; | 249 | mailimap_msg_att * msg_att; |
178 | int i = 0; | 250 | int i = 0; |
179 | for (current = clist_begin(result); current != 0; current=clist_next(current)) { | 251 | for (current = clist_begin(result); current != 0; current=clist_next(current)) { |
180 | ++i; | 252 | ++i; |
181 | msg_att = (mailimap_msg_att*)current->data; | 253 | msg_att = (mailimap_msg_att*)current->data; |
182 | RecMail*m = parse_list_result(msg_att); | 254 | RecMail*m = parse_list_result(msg_att); |
183 | if (m) { | 255 | if (m) { |
184 | m->setNumber(i); | 256 | m->setNumber(i); |
185 | m->setMbox(mailbox); | 257 | m->setMbox(mailbox); |
186 | m->setWrapper(this); | 258 | m->setWrapper(this); |
187 | target.append(m); | 259 | target.append(m); |
188 | } | 260 | } |
189 | } | 261 | } |
190 | Global::statusMessage(tr("Mailbox has %1 mails").arg(target.count())); | 262 | Global::statusMessage(tr("Mailbox has %1 mails").arg(target.count())); |
191 | } else { | 263 | } else { |
192 | Global::statusMessage(tr("Error fetching headers: %1").arg(m_imap->imap_response)); | 264 | Global::statusMessage(tr("Error fetching headers: %1").arg(m_imap->imap_response)); |
193 | } | 265 | } |
194 | if (result) mailimap_fetch_list_free(result); | 266 | if (result) mailimap_fetch_list_free(result); |
195 | } | 267 | } |
196 | 268 | ||
197 | QList<Folder>* IMAPwrapper::listFolders() | 269 | QList<Folder>* IMAPwrapper::listFolders() |
198 | { | 270 | { |
199 | const char *path, *mask; | 271 | const char *path, *mask; |
200 | int err = MAILIMAP_NO_ERROR; | 272 | int err = MAILIMAP_NO_ERROR; |
201 | clist *result = 0; | 273 | clist *result = 0; |
202 | clistcell *current = 0; | 274 | clistcell *current = 0; |
203 | clistcell*cur_flag = 0; | 275 | clistcell*cur_flag = 0; |
204 | mailimap_mbx_list_flags*bflags = 0; | 276 | mailimap_mbx_list_flags*bflags = 0; |
205 | 277 | ||
206 | QList<Folder> * folders = new QList<Folder>(); | 278 | QList<Folder> * folders = new QList<Folder>(); |
207 | folders->setAutoDelete( false ); | 279 | folders->setAutoDelete( false ); |
208 | login(); | 280 | login(); |
209 | if (!m_imap) { | 281 | if (!m_imap) { |
210 | return folders; | 282 | return folders; |
211 | } | 283 | } |
212 | 284 | ||
213 | /* | 285 | /* |
214 | * First we have to check for INBOX 'cause it sometimes it's not inside the path. | 286 | * First we have to check for INBOX 'cause it sometimes it's not inside the path. |
215 | * We must not forget to filter them out in next loop! | 287 | * We must not forget to filter them out in next loop! |
216 | * it seems like ugly code. and yes - it is ugly code. but the best way. | 288 | * it seems like ugly code. and yes - it is ugly code. but the best way. |
217 | */ | 289 | */ |
218 | QString temp; | 290 | QString temp; |
219 | mask = "INBOX" ; | 291 | mask = "INBOX" ; |
220 | mailimap_mailbox_list *list; | 292 | mailimap_mailbox_list *list; |
221 | err = mailimap_list( m_imap, (char*)"", (char*)mask, &result ); | 293 | err = mailimap_list( m_imap, (char*)"", (char*)mask, &result ); |
222 | QString del; | 294 | QString del; |
223 | bool selectable = true; | 295 | bool selectable = true; |
224 | bool no_inferiors = false; | 296 | bool no_inferiors = false; |
225 | if ( err == MAILIMAP_NO_ERROR ) { | 297 | if ( err == MAILIMAP_NO_ERROR ) { |
226 | current = result->first; | 298 | current = result->first; |
227 | for ( int i = result->count; i > 0; i-- ) { | 299 | for ( int i = result->count; i > 0; i-- ) { |
228 | list = (mailimap_mailbox_list *) current->data; | 300 | list = (mailimap_mailbox_list *) current->data; |
229 | // it is better use the deep copy mechanism of qt itself | 301 | // it is better use the deep copy mechanism of qt itself |
230 | // instead of using strdup! | 302 | // instead of using strdup! |
231 | temp = list->mb_name; | 303 | temp = list->mb_name; |
232 | del = list->mb_delimiter; | 304 | del = list->mb_delimiter; |
233 | current = current->next; | 305 | current = current->next; |
234 | if ( (bflags = list->mb_flag) ) { | 306 | if ( (bflags = list->mb_flag) ) { |
235 | selectable = !(bflags->mbf_type==MAILIMAP_MBX_LIST_FLAGS_SFLAG&& | 307 | selectable = !(bflags->mbf_type==MAILIMAP_MBX_LIST_FLAGS_SFLAG&& |
236 | bflags->mbf_sflag==MAILIMAP_MBX_LIST_SFLAG_NOSELECT); | 308 | bflags->mbf_sflag==MAILIMAP_MBX_LIST_SFLAG_NOSELECT); |
237 | for(cur_flag=clist_begin(bflags->mbf_oflags);cur_flag;cur_flag=clist_next(cur_flag)) { | 309 | for(cur_flag=clist_begin(bflags->mbf_oflags);cur_flag;cur_flag=clist_next(cur_flag)) { |
238 | if ( ((mailimap_mbx_list_oflag*)cur_flag->data)->of_type==MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS) { | 310 | if ( ((mailimap_mbx_list_oflag*)cur_flag->data)->of_type==MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS) { |
239 | no_inferiors = true; | 311 | no_inferiors = true; |
240 | } | 312 | } |
241 | } | 313 | } |
242 | } | 314 | } |
243 | folders->append( new IMAPFolder(temp,del,selectable,no_inferiors,account->getPrefix())); | 315 | folders->append( new IMAPFolder(temp,del,selectable,no_inferiors,account->getPrefix())); |
244 | } | 316 | } |
245 | } else { | 317 | } else { |
246 | qDebug("error fetching folders: %s",m_imap->imap_response); | 318 | qDebug("error fetching folders: %s",m_imap->imap_response); |
247 | } | 319 | } |
248 | mailimap_list_result_free( result ); | 320 | mailimap_list_result_free( result ); |
249 | 321 | ||
250 | /* | 322 | /* |
251 | * second stage - get the other then inbox folders | 323 | * second stage - get the other then inbox folders |
252 | */ | 324 | */ |
253 | mask = "*" ; | 325 | mask = "*" ; |
254 | path = account->getPrefix().latin1(); | 326 | path = account->getPrefix().latin1(); |
255 | if (!path) path = ""; | 327 | if (!path) path = ""; |
256 | qDebug(path); | 328 | qDebug(path); |
257 | err = mailimap_list( m_imap, (char*)path, (char*)mask, &result ); | 329 | err = mailimap_list( m_imap, (char*)path, (char*)mask, &result ); |
258 | if ( err == MAILIMAP_NO_ERROR ) { | 330 | if ( err == MAILIMAP_NO_ERROR ) { |
259 | current = result->first; | 331 | current = result->first; |
260 | for ( current=clist_begin(result);current!=NULL;current=clist_next(current)) { | 332 | for ( current=clist_begin(result);current!=NULL;current=clist_next(current)) { |
261 | no_inferiors = false; | 333 | no_inferiors = false; |
262 | list = (mailimap_mailbox_list *) current->data; | 334 | list = (mailimap_mailbox_list *) current->data; |
263 | // it is better use the deep copy mechanism of qt itself | 335 | // it is better use the deep copy mechanism of qt itself |
264 | // instead of using strdup! | 336 | // instead of using strdup! |
265 | temp = list->mb_name; | 337 | temp = list->mb_name; |
266 | if (temp.lower()=="inbox") | 338 | if (temp.lower()=="inbox") |
267 | continue; | 339 | continue; |
268 | if (temp.lower()==account->getPrefix().lower()) | 340 | if (temp.lower()==account->getPrefix().lower()) |
269 | continue; | 341 | continue; |
270 | if ( (bflags = list->mb_flag) ) { | 342 | if ( (bflags = list->mb_flag) ) { |
271 | selectable = !(bflags->mbf_type==MAILIMAP_MBX_LIST_FLAGS_SFLAG&& | 343 | selectable = !(bflags->mbf_type==MAILIMAP_MBX_LIST_FLAGS_SFLAG&& |
272 | bflags->mbf_sflag==MAILIMAP_MBX_LIST_SFLAG_NOSELECT); | 344 | bflags->mbf_sflag==MAILIMAP_MBX_LIST_SFLAG_NOSELECT); |
273 | for(cur_flag=clist_begin(bflags->mbf_oflags);cur_flag;cur_flag=clist_next(cur_flag)) { | 345 | for(cur_flag=clist_begin(bflags->mbf_oflags);cur_flag;cur_flag=clist_next(cur_flag)) { |
274 | if ( ((mailimap_mbx_list_oflag*)cur_flag->data)->of_type==MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS) { | 346 | if ( ((mailimap_mbx_list_oflag*)cur_flag->data)->of_type==MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS) { |
275 | no_inferiors = true; | 347 | no_inferiors = true; |
276 | } | 348 | } |
277 | } | 349 | } |
278 | } | 350 | } |
279 | del = list->mb_delimiter; | 351 | del = list->mb_delimiter; |
280 | folders->append(new IMAPFolder(temp,del,selectable,no_inferiors,account->getPrefix())); | 352 | folders->append(new IMAPFolder(temp,del,selectable,no_inferiors,account->getPrefix())); |
281 | } | 353 | } |
282 | } else { | 354 | } else { |
283 | qDebug("error fetching folders %s",m_imap->imap_response); | 355 | qDebug("error fetching folders %s",m_imap->imap_response); |
284 | } | 356 | } |
285 | if (result) mailimap_list_result_free( result ); | 357 | if (result) mailimap_list_result_free( result ); |
286 | return folders; | 358 | return folders; |
287 | } | 359 | } |
288 | 360 | ||
289 | RecMail*IMAPwrapper::parse_list_result(mailimap_msg_att* m_att) | 361 | RecMail*IMAPwrapper::parse_list_result(mailimap_msg_att* m_att) |
290 | { | 362 | { |
291 | RecMail * m = 0; | 363 | RecMail * m = 0; |
292 | mailimap_msg_att_item *item=0; | 364 | mailimap_msg_att_item *item=0; |
293 | clistcell *current,*c,*cf; | 365 | clistcell *current,*c,*cf; |
294 | mailimap_msg_att_dynamic*flist; | 366 | mailimap_msg_att_dynamic*flist; |
295 | mailimap_flag_fetch*cflag; | 367 | mailimap_flag_fetch*cflag; |
296 | int size; | 368 | int size; |
297 | QBitArray mFlags(7); | 369 | QBitArray mFlags(7); |
298 | QStringList addresslist; | 370 | QStringList addresslist; |
299 | 371 | ||
300 | if (!m_att) { | 372 | if (!m_att) { |
301 | return m; | 373 | return m; |
302 | } | 374 | } |
303 | m = new RecMail(); | 375 | m = new RecMail(); |
304 | for (c = clist_begin(m_att->att_list); c!=NULL;c=clist_next(c) ) { | 376 | for (c = clist_begin(m_att->att_list); c!=NULL;c=clist_next(c) ) { |
305 | current = c; | 377 | current = c; |
306 | size = 0; | 378 | size = 0; |
307 | item = (mailimap_msg_att_item*)current->data; | 379 | item = (mailimap_msg_att_item*)current->data; |
308 | if (item->att_type!=MAILIMAP_MSG_ATT_ITEM_STATIC) { | 380 | if (item->att_type!=MAILIMAP_MSG_ATT_ITEM_STATIC) { |
309 | flist = (mailimap_msg_att_dynamic*)item->att_data.att_dyn; | 381 | flist = (mailimap_msg_att_dynamic*)item->att_data.att_dyn; |
310 | if (!flist->att_list) { | 382 | if (!flist->att_list) { |
311 | continue; | 383 | continue; |
312 | } | 384 | } |
313 | cf = flist->att_list->first; | 385 | cf = flist->att_list->first; |
314 | for (cf = clist_begin(flist->att_list); cf!=NULL; cf = clist_next(cf)) { | 386 | for (cf = clist_begin(flist->att_list); cf!=NULL; cf = clist_next(cf)) { |
315 | cflag = (mailimap_flag_fetch*)cf->data; | 387 | cflag = (mailimap_flag_fetch*)cf->data; |
316 | if (cflag->fl_type==MAILIMAP_FLAG_FETCH_OTHER && cflag->fl_flag!=0) { | 388 | if (cflag->fl_type==MAILIMAP_FLAG_FETCH_OTHER && cflag->fl_flag!=0) { |
317 | switch (cflag->fl_flag->fl_type) { | 389 | switch (cflag->fl_flag->fl_type) { |
318 | case MAILIMAP_FLAG_ANSWERED: /* \Answered flag */ | 390 | case MAILIMAP_FLAG_ANSWERED: /* \Answered flag */ |
319 | mFlags.setBit(FLAG_ANSWERED); | 391 | mFlags.setBit(FLAG_ANSWERED); |
320 | break; | 392 | break; |
321 | case MAILIMAP_FLAG_FLAGGED: /* \Flagged flag */ | 393 | case MAILIMAP_FLAG_FLAGGED: /* \Flagged flag */ |
322 | mFlags.setBit(FLAG_FLAGGED); | 394 | mFlags.setBit(FLAG_FLAGGED); |
323 | break; | 395 | break; |
324 | case MAILIMAP_FLAG_DELETED: /* \Deleted flag */ | 396 | case MAILIMAP_FLAG_DELETED: /* \Deleted flag */ |
325 | mFlags.setBit(FLAG_DELETED); | 397 | mFlags.setBit(FLAG_DELETED); |
326 | break; | 398 | break; |
327 | case MAILIMAP_FLAG_SEEN: /* \Seen flag */ | 399 | case MAILIMAP_FLAG_SEEN: /* \Seen flag */ |
328 | mFlags.setBit(FLAG_SEEN); | 400 | mFlags.setBit(FLAG_SEEN); |
329 | break; | 401 | break; |
330 | case MAILIMAP_FLAG_DRAFT: /* \Draft flag */ | 402 | case MAILIMAP_FLAG_DRAFT: /* \Draft flag */ |
331 | mFlags.setBit(FLAG_DRAFT); | 403 | mFlags.setBit(FLAG_DRAFT); |
332 | break; | 404 | break; |
333 | case MAILIMAP_FLAG_KEYWORD: /* keyword flag */ | 405 | case MAILIMAP_FLAG_KEYWORD: /* keyword flag */ |
334 | break; | 406 | break; |
335 | case MAILIMAP_FLAG_EXTENSION: /* \extension flag */ | 407 | case MAILIMAP_FLAG_EXTENSION: /* \extension flag */ |
336 | break; | 408 | break; |
337 | default: | 409 | default: |
338 | break; | 410 | break; |
339 | } | 411 | } |
340 | } else if (cflag->fl_type==MAILIMAP_FLAG_FETCH_RECENT) { | 412 | } else if (cflag->fl_type==MAILIMAP_FLAG_FETCH_RECENT) { |
341 | mFlags.setBit(FLAG_RECENT); | 413 | mFlags.setBit(FLAG_RECENT); |
342 | } | 414 | } |
343 | } | 415 | } |
344 | continue; | 416 | continue; |
345 | } | 417 | } |
346 | if (item->att_data.att_static->att_type==MAILIMAP_MSG_ATT_ENVELOPE) { | 418 | if (item->att_data.att_static->att_type==MAILIMAP_MSG_ATT_ENVELOPE) { |
347 | mailimap_envelope * head = item->att_data.att_static->att_data.att_env; | 419 | mailimap_envelope * head = item->att_data.att_static->att_data.att_env; |
348 | m->setDate(head->env_date); | 420 | m->setDate(head->env_date); |
349 | m->setSubject(convert_String((const char*)head->env_subject)); | 421 | m->setSubject(convert_String((const char*)head->env_subject)); |
350 | //m->setSubject(head->env_subject); | 422 | //m->setSubject(head->env_subject); |
351 | if (head->env_from!=NULL) { | 423 | if (head->env_from!=NULL) { |
352 | addresslist = address_list_to_stringlist(head->env_from->frm_list); | 424 | addresslist = address_list_to_stringlist(head->env_from->frm_list); |
353 | if (addresslist.count()) { | 425 | if (addresslist.count()) { |
354 | m->setFrom(addresslist.first()); | 426 | m->setFrom(addresslist.first()); |
355 | } | 427 | } |
356 | } | 428 | } |
357 | if (head->env_to!=NULL) { | 429 | if (head->env_to!=NULL) { |
358 | addresslist = address_list_to_stringlist(head->env_to->to_list); | 430 | addresslist = address_list_to_stringlist(head->env_to->to_list); |
359 | m->setTo(addresslist); | 431 | m->setTo(addresslist); |
360 | } | 432 | } |
361 | if (head->env_cc!=NULL) { | 433 | if (head->env_cc!=NULL) { |
362 | addresslist = address_list_to_stringlist(head->env_cc->cc_list); | 434 | addresslist = address_list_to_stringlist(head->env_cc->cc_list); |
363 | m->setCC(addresslist); | 435 | m->setCC(addresslist); |
364 | } | 436 | } |
365 | if (head->env_bcc!=NULL) { | 437 | if (head->env_bcc!=NULL) { |
366 | addresslist = address_list_to_stringlist(head->env_bcc->bcc_list); | 438 | addresslist = address_list_to_stringlist(head->env_bcc->bcc_list); |
367 | m->setBcc(addresslist); | 439 | m->setBcc(addresslist); |
368 | } | 440 | } |
369 | if (head->env_reply_to!=NULL) { | 441 | if (head->env_reply_to!=NULL) { |
370 | addresslist = address_list_to_stringlist(head->env_reply_to->rt_list); | 442 | addresslist = address_list_to_stringlist(head->env_reply_to->rt_list); |
371 | if (addresslist.count()) { | 443 | if (addresslist.count()) { |
372 | m->setReplyto(addresslist.first()); | 444 | m->setReplyto(addresslist.first()); |
373 | } | 445 | } |
374 | } | 446 | } |
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 | |||
@@ -1,77 +1,78 @@ | |||
1 | #ifndef __IMAPWRAPPER | 1 | #ifndef __IMAPWRAPPER |
2 | #define __IMAPWRAPPER | 2 | #define __IMAPWRAPPER |
3 | 3 | ||
4 | #include <qlist.h> | 4 | #include <qlist.h> |
5 | #include "mailwrapper.h" | 5 | #include "mailwrapper.h" |
6 | #include "abstractmail.h" | 6 | #include "abstractmail.h" |
7 | #include <libetpan/clist.h> | 7 | #include <libetpan/clist.h> |
8 | 8 | ||
9 | struct mailimap; | 9 | struct mailimap; |
10 | struct mailimap_body; | 10 | struct mailimap_body; |
11 | struct mailimap_body_type_1part; | 11 | struct mailimap_body_type_1part; |
12 | struct mailimap_body_type_text; | 12 | struct mailimap_body_type_text; |
13 | struct mailimap_body_type_basic; | 13 | struct mailimap_body_type_basic; |
14 | struct mailimap_body_type_msg; | 14 | struct mailimap_body_type_msg; |
15 | struct mailimap_body_type_mpart; | 15 | struct mailimap_body_type_mpart; |
16 | struct mailimap_body_fields; | 16 | struct mailimap_body_fields; |
17 | struct mailimap_msg_att; | 17 | struct mailimap_msg_att; |
18 | class encodedString; | 18 | class encodedString; |
19 | 19 | ||
20 | class IMAPwrapper : public AbstractMail | 20 | class IMAPwrapper : public AbstractMail |
21 | { | 21 | { |
22 | Q_OBJECT | 22 | Q_OBJECT |
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 |