-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,572 +1,574 @@ | |||
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; |
285 | 287 | ||
286 | QValueList<FolderP>* folders = new QValueList<FolderP>(); | 288 | QValueList<FolderP>* folders = new QValueList<FolderP>(); |
287 | login(); | 289 | login(); |
288 | if (!m_imap) { | 290 | if (!m_imap) { |
289 | return folders; | 291 | return folders; |
290 | } | 292 | } |
291 | 293 | ||
292 | /* | 294 | /* |
293 | * First we have to check for INBOX 'cause it sometimes it's not inside the path. | 295 | * First we have to check for INBOX 'cause it sometimes it's not inside the path. |
294 | * We must not forget to filter them out in next loop! | 296 | * We must not forget to filter them out in next loop! |
295 | * it seems like ugly code. and yes - it is ugly code. but the best way. | 297 | * it seems like ugly code. and yes - it is ugly code. but the best way. |
296 | */ | 298 | */ |
297 | QString temp; | 299 | QString temp; |
298 | mask = "INBOX" ; | 300 | mask = "INBOX" ; |
299 | mailimap_mailbox_list *list; | 301 | mailimap_mailbox_list *list; |
300 | err = mailimap_list( m_imap, (char*)"", (char*)mask, &result ); | 302 | err = mailimap_list( m_imap, (char*)"", (char*)mask, &result ); |
301 | QString del; | 303 | QString del; |
302 | bool selectable = true; | 304 | bool selectable = true; |
303 | bool no_inferiors = false; | 305 | bool no_inferiors = false; |
304 | if ( err == MAILIMAP_NO_ERROR ) { | 306 | if ( err == MAILIMAP_NO_ERROR ) { |
305 | current = result->first; | 307 | current = result->first; |
306 | for ( int i = result->count; i > 0; i-- ) { | 308 | for ( int i = result->count; i > 0; i-- ) { |
307 | list = (mailimap_mailbox_list *) current->data; | 309 | list = (mailimap_mailbox_list *) current->data; |
308 | // it is better use the deep copy mechanism of qt itself | 310 | // it is better use the deep copy mechanism of qt itself |
309 | // instead of using strdup! | 311 | // instead of using strdup! |
310 | temp = list->mb_name; | 312 | temp = list->mb_name; |
311 | del = list->mb_delimiter; | 313 | del = list->mb_delimiter; |
312 | current = current->next; | 314 | current = current->next; |
313 | if ( (bflags = list->mb_flag) ) { | 315 | if ( (bflags = list->mb_flag) ) { |
314 | selectable = !(bflags->mbf_type==MAILIMAP_MBX_LIST_FLAGS_SFLAG&& | 316 | selectable = !(bflags->mbf_type==MAILIMAP_MBX_LIST_FLAGS_SFLAG&& |
315 | bflags->mbf_sflag==MAILIMAP_MBX_LIST_SFLAG_NOSELECT); | 317 | bflags->mbf_sflag==MAILIMAP_MBX_LIST_SFLAG_NOSELECT); |
316 | for(cur_flag=clist_begin(bflags->mbf_oflags);cur_flag;cur_flag=clist_next(cur_flag)) { | 318 | for(cur_flag=clist_begin(bflags->mbf_oflags);cur_flag;cur_flag=clist_next(cur_flag)) { |
317 | if ( ((mailimap_mbx_list_oflag*)cur_flag->data)->of_type==MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS) { | 319 | if ( ((mailimap_mbx_list_oflag*)cur_flag->data)->of_type==MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS) { |
318 | no_inferiors = true; | 320 | no_inferiors = true; |
319 | } | 321 | } |
320 | } | 322 | } |
321 | } | 323 | } |
322 | folders->append( new IMAPFolder(temp,del,selectable,no_inferiors,account->getPrefix())); | 324 | folders->append( new IMAPFolder(temp,del,selectable,no_inferiors,account->getPrefix())); |
323 | } | 325 | } |
324 | } else { | 326 | } else { |
325 | odebug << "error fetching folders: " << m_imap->imap_response << "" << oendl; | 327 | odebug << "error fetching folders: " << m_imap->imap_response << "" << oendl; |
326 | } | 328 | } |
327 | mailimap_list_result_free( result ); | 329 | mailimap_list_result_free( result ); |
328 | 330 | ||
329 | /* | 331 | /* |
330 | * second stage - get the other then inbox folders | 332 | * second stage - get the other then inbox folders |
331 | */ | 333 | */ |
332 | mask = "*" ; | 334 | mask = "*" ; |
333 | path = account->getPrefix().latin1(); | 335 | path = account->getPrefix().latin1(); |
334 | if (!path) path = ""; | 336 | if (!path) path = ""; |
335 | odebug << path << oendl; | 337 | odebug << path << oendl; |
336 | err = mailimap_list( m_imap, (char*)path, (char*)mask, &result ); | 338 | err = mailimap_list( m_imap, (char*)path, (char*)mask, &result ); |
337 | if ( err == MAILIMAP_NO_ERROR ) { | 339 | if ( err == MAILIMAP_NO_ERROR ) { |
338 | current = result->first; | 340 | current = result->first; |
339 | for ( current=clist_begin(result);current!=NULL;current=clist_next(current)) { | 341 | for ( current=clist_begin(result);current!=NULL;current=clist_next(current)) { |
340 | no_inferiors = false; | 342 | no_inferiors = false; |
341 | list = (mailimap_mailbox_list *) current->data; | 343 | list = (mailimap_mailbox_list *) current->data; |
342 | // it is better use the deep copy mechanism of qt itself | 344 | // it is better use the deep copy mechanism of qt itself |
343 | // instead of using strdup! | 345 | // instead of using strdup! |
344 | temp = list->mb_name; | 346 | temp = list->mb_name; |
345 | if (temp.lower()=="inbox") | 347 | if (temp.lower()=="inbox") |
346 | continue; | 348 | continue; |
347 | if (temp.lower()==account->getPrefix().lower()) | 349 | if (temp.lower()==account->getPrefix().lower()) |
348 | continue; | 350 | continue; |
349 | if ( (bflags = list->mb_flag) ) { | 351 | if ( (bflags = list->mb_flag) ) { |
350 | selectable = !(bflags->mbf_type==MAILIMAP_MBX_LIST_FLAGS_SFLAG&& | 352 | selectable = !(bflags->mbf_type==MAILIMAP_MBX_LIST_FLAGS_SFLAG&& |
351 | bflags->mbf_sflag==MAILIMAP_MBX_LIST_SFLAG_NOSELECT); | 353 | bflags->mbf_sflag==MAILIMAP_MBX_LIST_SFLAG_NOSELECT); |
352 | for(cur_flag=clist_begin(bflags->mbf_oflags);cur_flag;cur_flag=clist_next(cur_flag)) { | 354 | for(cur_flag=clist_begin(bflags->mbf_oflags);cur_flag;cur_flag=clist_next(cur_flag)) { |
353 | if ( ((mailimap_mbx_list_oflag*)cur_flag->data)->of_type==MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS) { | 355 | if ( ((mailimap_mbx_list_oflag*)cur_flag->data)->of_type==MAILIMAP_MBX_LIST_OFLAG_NOINFERIORS) { |
354 | no_inferiors = true; | 356 | no_inferiors = true; |
355 | } | 357 | } |
356 | } | 358 | } |
357 | } | 359 | } |
358 | del = list->mb_delimiter; | 360 | del = list->mb_delimiter; |
359 | folders->append(new IMAPFolder(temp,del,selectable,no_inferiors,account->getPrefix())); | 361 | folders->append(new IMAPFolder(temp,del,selectable,no_inferiors,account->getPrefix())); |
360 | } | 362 | } |
361 | } else { | 363 | } else { |
362 | odebug << "error fetching folders " << m_imap->imap_response << "" << oendl; | 364 | odebug << "error fetching folders " << m_imap->imap_response << "" << oendl; |
363 | } | 365 | } |
364 | if (result) mailimap_list_result_free( result ); | 366 | if (result) mailimap_list_result_free( result ); |
365 | return folders; | 367 | return folders; |
366 | } | 368 | } |
367 | 369 | ||
368 | RecMail*IMAPwrapper::parse_list_result(mailimap_msg_att* m_att) | 370 | RecMail*IMAPwrapper::parse_list_result(mailimap_msg_att* m_att) |
369 | { | 371 | { |
370 | RecMail * m = 0; | 372 | RecMail * m = 0; |
371 | mailimap_msg_att_item *item=0; | 373 | mailimap_msg_att_item *item=0; |
372 | clistcell *current,*c,*cf; | 374 | clistcell *current,*c,*cf; |
373 | mailimap_msg_att_dynamic*flist; | 375 | mailimap_msg_att_dynamic*flist; |
374 | mailimap_flag_fetch*cflag; | 376 | mailimap_flag_fetch*cflag; |
375 | int size,toffset; | 377 | int size,toffset; |
376 | QBitArray mFlags(7); | 378 | QBitArray mFlags(7); |
377 | QStringList addresslist; | 379 | QStringList addresslist; |
378 | 380 | ||
379 | if (!m_att) { | 381 | if (!m_att) { |
380 | return m; | 382 | return m; |
381 | } | 383 | } |
382 | size = 0; | 384 | size = 0; |
383 | m = new RecMail(); | 385 | m = new RecMail(); |
384 | for (c = clist_begin(m_att->att_list); c!=NULL;c=clist_next(c) ) { | 386 | for (c = clist_begin(m_att->att_list); c!=NULL;c=clist_next(c) ) { |
385 | current = c; | 387 | current = c; |
386 | item = (mailimap_msg_att_item*)current->data; | 388 | item = (mailimap_msg_att_item*)current->data; |
387 | if (item->att_type!=MAILIMAP_MSG_ATT_ITEM_STATIC) { | 389 | if (item->att_type!=MAILIMAP_MSG_ATT_ITEM_STATIC) { |
388 | flist = (mailimap_msg_att_dynamic*)item->att_data.att_dyn; | 390 | flist = (mailimap_msg_att_dynamic*)item->att_data.att_dyn; |
389 | if (!flist->att_list) { | 391 | if (!flist->att_list) { |
390 | continue; | 392 | continue; |
391 | } | 393 | } |
392 | cf = flist->att_list->first; | 394 | cf = flist->att_list->first; |
393 | for (cf = clist_begin(flist->att_list); cf!=NULL; cf = clist_next(cf)) { | 395 | for (cf = clist_begin(flist->att_list); cf!=NULL; cf = clist_next(cf)) { |
394 | cflag = (mailimap_flag_fetch*)cf->data; | 396 | cflag = (mailimap_flag_fetch*)cf->data; |
395 | if (cflag->fl_type==MAILIMAP_FLAG_FETCH_OTHER && cflag->fl_flag!=0) { | 397 | if (cflag->fl_type==MAILIMAP_FLAG_FETCH_OTHER && cflag->fl_flag!=0) { |
396 | switch (cflag->fl_flag->fl_type) { | 398 | switch (cflag->fl_flag->fl_type) { |
397 | case MAILIMAP_FLAG_ANSWERED: /* \Answered flag */ | 399 | case MAILIMAP_FLAG_ANSWERED: /* \Answered flag */ |
398 | mFlags.setBit(FLAG_ANSWERED); | 400 | mFlags.setBit(FLAG_ANSWERED); |
399 | break; | 401 | break; |
400 | case MAILIMAP_FLAG_FLAGGED: /* \Flagged flag */ | 402 | case MAILIMAP_FLAG_FLAGGED: /* \Flagged flag */ |
401 | mFlags.setBit(FLAG_FLAGGED); | 403 | mFlags.setBit(FLAG_FLAGGED); |
402 | break; | 404 | break; |
403 | case MAILIMAP_FLAG_DELETED: /* \Deleted flag */ | 405 | case MAILIMAP_FLAG_DELETED: /* \Deleted flag */ |
404 | mFlags.setBit(FLAG_DELETED); | 406 | mFlags.setBit(FLAG_DELETED); |
405 | break; | 407 | break; |
406 | case MAILIMAP_FLAG_SEEN: /* \Seen flag */ | 408 | case MAILIMAP_FLAG_SEEN: /* \Seen flag */ |
407 | mFlags.setBit(FLAG_SEEN); | 409 | mFlags.setBit(FLAG_SEEN); |
408 | break; | 410 | break; |
409 | case MAILIMAP_FLAG_DRAFT: /* \Draft flag */ | 411 | case MAILIMAP_FLAG_DRAFT: /* \Draft flag */ |
410 | mFlags.setBit(FLAG_DRAFT); | 412 | mFlags.setBit(FLAG_DRAFT); |
411 | break; | 413 | break; |
412 | case MAILIMAP_FLAG_KEYWORD: /* keyword flag */ | 414 | case MAILIMAP_FLAG_KEYWORD: /* keyword flag */ |
413 | break; | 415 | break; |
414 | case MAILIMAP_FLAG_EXTENSION: /* \extension flag */ | 416 | case MAILIMAP_FLAG_EXTENSION: /* \extension flag */ |
415 | break; | 417 | break; |
416 | default: | 418 | default: |
417 | break; | 419 | break; |
418 | } | 420 | } |
419 | } else if (cflag->fl_type==MAILIMAP_FLAG_FETCH_RECENT) { | 421 | } else if (cflag->fl_type==MAILIMAP_FLAG_FETCH_RECENT) { |
420 | mFlags.setBit(FLAG_RECENT); | 422 | mFlags.setBit(FLAG_RECENT); |
421 | } | 423 | } |
422 | } | 424 | } |
423 | continue; | 425 | continue; |
424 | } | 426 | } |
425 | if (item->att_data.att_static->att_type==MAILIMAP_MSG_ATT_ENVELOPE) { | 427 | if (item->att_data.att_static->att_type==MAILIMAP_MSG_ATT_ENVELOPE) { |
426 | mailimap_envelope * head = item->att_data.att_static->att_data.att_env; | 428 | mailimap_envelope * head = item->att_data.att_static->att_data.att_env; |
427 | QDateTime d = parseDateTime(head->env_date,toffset); | 429 | QDateTime d = parseDateTime(head->env_date,toffset); |
428 | m->setDate(d,toffset); | 430 | m->setDate(d,toffset); |
429 | m->setSubject(convert_String((const char*)head->env_subject)); | 431 | m->setSubject(convert_String((const char*)head->env_subject)); |
430 | if (head->env_from!=NULL) { | 432 | if (head->env_from!=NULL) { |
431 | addresslist = address_list_to_stringlist(head->env_from->frm_list); | 433 | addresslist = address_list_to_stringlist(head->env_from->frm_list); |
432 | if (addresslist.count()) { | 434 | if (addresslist.count()) { |
433 | m->setFrom(addresslist.first()); | 435 | m->setFrom(addresslist.first()); |
434 | } | 436 | } |
435 | } | 437 | } |
436 | if (head->env_to!=NULL) { | 438 | if (head->env_to!=NULL) { |
437 | addresslist = address_list_to_stringlist(head->env_to->to_list); | 439 | addresslist = address_list_to_stringlist(head->env_to->to_list); |
438 | m->setTo(addresslist); | 440 | m->setTo(addresslist); |
439 | } | 441 | } |
440 | if (head->env_cc!=NULL) { | 442 | if (head->env_cc!=NULL) { |
441 | addresslist = address_list_to_stringlist(head->env_cc->cc_list); | 443 | addresslist = address_list_to_stringlist(head->env_cc->cc_list); |
442 | m->setCC(addresslist); | 444 | m->setCC(addresslist); |
443 | } | 445 | } |
444 | if (head->env_bcc!=NULL) { | 446 | if (head->env_bcc!=NULL) { |
445 | addresslist = address_list_to_stringlist(head->env_bcc->bcc_list); | 447 | addresslist = address_list_to_stringlist(head->env_bcc->bcc_list); |
446 | m->setBcc(addresslist); | 448 | m->setBcc(addresslist); |
447 | } | 449 | } |
448 | /* reply to address, eg. email. */ | 450 | /* reply to address, eg. email. */ |
449 | if (head->env_reply_to!=NULL) { | 451 | if (head->env_reply_to!=NULL) { |
450 | addresslist = address_list_to_stringlist(head->env_reply_to->rt_list); | 452 | addresslist = address_list_to_stringlist(head->env_reply_to->rt_list); |
451 | if (addresslist.count()) { | 453 | if (addresslist.count()) { |
452 | m->setReplyto(addresslist.first()); | 454 | m->setReplyto(addresslist.first()); |
453 | } | 455 | } |
454 | } | 456 | } |
455 | if (head->env_in_reply_to!=NULL) { | 457 | if (head->env_in_reply_to!=NULL) { |
456 | QString h(head->env_in_reply_to); | 458 | QString h(head->env_in_reply_to); |
457 | while (h.length()>0 && h[0]=='<') { | 459 | while (h.length()>0 && h[0]=='<') { |
458 | h.remove(0,1); | 460 | h.remove(0,1); |
459 | } | 461 | } |
460 | while (h.length()>0 && h[h.length()-1]=='>') { | 462 | while (h.length()>0 && h[h.length()-1]=='>') { |
461 | h.remove(h.length()-1,1); | 463 | h.remove(h.length()-1,1); |
462 | } | 464 | } |
463 | if (h.length()>0) { | 465 | if (h.length()>0) { |
464 | m->setInreply(QStringList(h)); | 466 | m->setInreply(QStringList(h)); |
465 | } | 467 | } |
466 | } | 468 | } |
467 | if (head->env_message_id) { | 469 | if (head->env_message_id) { |
468 | m->setMsgid(QString(head->env_message_id)); | 470 | m->setMsgid(QString(head->env_message_id)); |
469 | } | 471 | } |
470 | } else if (item->att_data.att_static->att_type==MAILIMAP_MSG_ATT_INTERNALDATE) { | 472 | } else if (item->att_data.att_static->att_type==MAILIMAP_MSG_ATT_INTERNALDATE) { |
471 | // not used this moment | 473 | // not used this moment |
472 | } else if (item->att_data.att_static->att_type==MAILIMAP_MSG_ATT_RFC822_SIZE) { | 474 | } else if (item->att_data.att_static->att_type==MAILIMAP_MSG_ATT_RFC822_SIZE) { |
473 | //size = item->att_data.att_static->att_data.att_rfc822_size; | 475 | //size = item->att_data.att_static->att_data.att_rfc822_size; |
474 | m->setMsgsize(item->att_data.att_static->att_data.att_rfc822_size); | 476 | m->setMsgsize(item->att_data.att_static->att_data.att_rfc822_size); |
475 | } | 477 | } |
476 | } | 478 | } |
477 | /* msg is already deleted */ | 479 | /* msg is already deleted */ |
478 | if (mFlags.testBit(FLAG_DELETED) && m) { | 480 | if (mFlags.testBit(FLAG_DELETED) && m) { |
479 | delete m; | 481 | delete m; |
480 | m = 0; | 482 | m = 0; |
481 | } | 483 | } |
482 | if (m) { | 484 | if (m) { |
483 | m->setFlags(mFlags); | 485 | m->setFlags(mFlags); |
484 | } | 486 | } |
485 | return m; | 487 | return m; |
486 | } | 488 | } |
487 | 489 | ||
488 | RecBodyP IMAPwrapper::fetchBody(const RecMailP&mail) | 490 | RecBodyP IMAPwrapper::fetchBody(const RecMailP&mail) |
489 | { | 491 | { |
490 | RecBodyP body = new RecBody(); | 492 | RecBodyP body = new RecBody(); |
491 | const char *mb; | 493 | const char *mb; |
492 | int err = MAILIMAP_NO_ERROR; | 494 | int err = MAILIMAP_NO_ERROR; |
493 | clist *result = 0; | 495 | clist *result = 0; |
494 | clistcell *current; | 496 | clistcell *current; |
495 | mailimap_fetch_att *fetchAtt = 0; | 497 | mailimap_fetch_att *fetchAtt = 0; |
496 | mailimap_fetch_type *fetchType = 0; | 498 | mailimap_fetch_type *fetchType = 0; |
497 | mailimap_set *set = 0; | 499 | mailimap_set *set = 0; |
498 | mailimap_body*body_desc = 0; | 500 | mailimap_body*body_desc = 0; |
499 | 501 | ||
500 | mb = mail->getMbox().latin1(); | 502 | mb = mail->getMbox().latin1(); |
501 | 503 | ||
502 | login(); | 504 | login(); |
503 | if (!m_imap) { | 505 | if (!m_imap) { |
504 | return body; | 506 | return body; |
505 | } | 507 | } |
506 | err = selectMbox(mail->getMbox()); | 508 | err = selectMbox(mail->getMbox()); |
507 | if ( err != MAILIMAP_NO_ERROR ) { | 509 | if ( err != MAILIMAP_NO_ERROR ) { |
508 | return body; | 510 | return body; |
509 | } | 511 | } |
510 | 512 | ||
511 | /* the range has to start at 1!!! not with 0!!!! */ | 513 | /* the range has to start at 1!!! not with 0!!!! */ |
512 | set = mailimap_set_new_interval( mail->getNumber(),mail->getNumber() ); | 514 | set = mailimap_set_new_interval( mail->getNumber(),mail->getNumber() ); |
513 | fetchAtt = mailimap_fetch_att_new_bodystructure(); | 515 | fetchAtt = mailimap_fetch_att_new_bodystructure(); |
514 | fetchType = mailimap_fetch_type_new_fetch_att(fetchAtt); | 516 | fetchType = mailimap_fetch_type_new_fetch_att(fetchAtt); |
515 | err = mailimap_fetch( m_imap, set, fetchType, &result ); | 517 | err = mailimap_fetch( m_imap, set, fetchType, &result ); |
516 | mailimap_set_free( set ); | 518 | mailimap_set_free( set ); |
517 | mailimap_fetch_type_free( fetchType ); | 519 | mailimap_fetch_type_free( fetchType ); |
518 | 520 | ||
519 | if (err == MAILIMAP_NO_ERROR && (current=clist_begin(result)) ) { | 521 | if (err == MAILIMAP_NO_ERROR && (current=clist_begin(result)) ) { |
520 | mailimap_msg_att * msg_att; | 522 | mailimap_msg_att * msg_att; |
521 | msg_att = (mailimap_msg_att*)current->data; | 523 | msg_att = (mailimap_msg_att*)current->data; |
522 | mailimap_msg_att_item*item = (mailimap_msg_att_item*)msg_att->att_list->first->data; | 524 | mailimap_msg_att_item*item = (mailimap_msg_att_item*)msg_att->att_list->first->data; |
523 | QValueList<int> path; | 525 | QValueList<int> path; |
524 | body_desc = item->att_data.att_static->att_data.att_body; | 526 | body_desc = item->att_data.att_static->att_data.att_body; |
525 | traverseBody(mail,body_desc,body,0,path); | 527 | traverseBody(mail,body_desc,body,0,path); |
526 | } else { | 528 | } else { |
527 | odebug << "error fetching body: " << m_imap->imap_response << "" << oendl; | 529 | odebug << "error fetching body: " << m_imap->imap_response << "" << oendl; |
528 | } | 530 | } |
529 | if (result) mailimap_fetch_list_free(result); | 531 | if (result) mailimap_fetch_list_free(result); |
530 | return body; | 532 | return body; |
531 | } | 533 | } |
532 | 534 | ||
533 | QStringList IMAPwrapper::address_list_to_stringlist(clist*list) | 535 | QStringList IMAPwrapper::address_list_to_stringlist(clist*list) |
534 | { | 536 | { |
535 | QStringList l; | 537 | QStringList l; |
536 | QString from; | 538 | QString from; |
537 | bool named_from; | 539 | bool named_from; |
538 | clistcell *current = NULL; | 540 | clistcell *current = NULL; |
539 | mailimap_address * current_address=NULL; | 541 | mailimap_address * current_address=NULL; |
540 | if (!list) { | 542 | if (!list) { |
541 | return l; | 543 | return l; |
542 | } | 544 | } |
543 | unsigned int count = 0; | 545 | unsigned int count = 0; |
544 | for (current=clist_begin(list);current!= NULL;current=clist_next(current)) { | 546 | for (current=clist_begin(list);current!= NULL;current=clist_next(current)) { |
545 | from = ""; | 547 | from = ""; |
546 | named_from = false; | 548 | named_from = false; |
547 | current_address=(mailimap_address*)current->data; | 549 | current_address=(mailimap_address*)current->data; |
548 | if (current_address->ad_personal_name){ | 550 | if (current_address->ad_personal_name){ |
549 | from+=convert_String((const char*)current_address->ad_personal_name); | 551 | from+=convert_String((const char*)current_address->ad_personal_name); |
550 | from+=" "; | 552 | from+=" "; |
551 | named_from = true; | 553 | named_from = true; |
552 | } | 554 | } |
553 | if (named_from && (current_address->ad_mailbox_name || current_address->ad_host_name)) { | 555 | if (named_from && (current_address->ad_mailbox_name || current_address->ad_host_name)) { |
554 | from+="<"; | 556 | from+="<"; |
555 | } | 557 | } |
556 | if (current_address->ad_mailbox_name) { | 558 | if (current_address->ad_mailbox_name) { |
557 | from+=QString(current_address->ad_mailbox_name); | 559 | from+=QString(current_address->ad_mailbox_name); |
558 | from+="@"; | 560 | from+="@"; |
559 | } | 561 | } |
560 | if (current_address->ad_host_name) { | 562 | if (current_address->ad_host_name) { |
561 | from+=QString(current_address->ad_host_name); | 563 | from+=QString(current_address->ad_host_name); |
562 | } | 564 | } |
563 | if (named_from && (current_address->ad_mailbox_name || current_address->ad_host_name)) { | 565 | if (named_from && (current_address->ad_mailbox_name || current_address->ad_host_name)) { |
564 | from+=">"; | 566 | from+=">"; |
565 | } | 567 | } |
566 | l.append(QString(from)); | 568 | l.append(QString(from)); |
567 | if (++count > 99) { | 569 | if (++count > 99) { |
568 | break; | 570 | break; |
569 | } | 571 | } |
570 | } | 572 | } |
571 | return l; | 573 | return l; |
572 | } | 574 | } |