-rw-r--r-- | noncore/net/mail/imapwrapper.cpp | 3 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/imapwrapper.cpp | 3 |
2 files changed, 4 insertions, 2 deletions
diff --git a/noncore/net/mail/imapwrapper.cpp b/noncore/net/mail/imapwrapper.cpp index 11bf3b8..48e476b 100644 --- a/noncore/net/mail/imapwrapper.cpp +++ b/noncore/net/mail/imapwrapper.cpp | |||
@@ -1,288 +1,289 @@ | |||
1 | 1 | ||
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | 3 | ||
4 | #include "imapwrapper.h" | 4 | #include "imapwrapper.h" |
5 | #include "mailtypes.h" | 5 | #include "mailtypes.h" |
6 | #include <libetpan/mailimap.h> | 6 | #include <libetpan/mailimap.h> |
7 | 7 | ||
8 | IMAPwrapper::IMAPwrapper( IMAPaccount *a ) | 8 | IMAPwrapper::IMAPwrapper( IMAPaccount *a ) |
9 | { | 9 | { |
10 | account = a; | 10 | account = a; |
11 | m_imap = 0; | 11 | m_imap = 0; |
12 | } | 12 | } |
13 | 13 | ||
14 | IMAPwrapper::~IMAPwrapper() | 14 | IMAPwrapper::~IMAPwrapper() |
15 | { | 15 | { |
16 | logout(); | 16 | logout(); |
17 | } | 17 | } |
18 | 18 | ||
19 | void IMAPwrapper::imap_progress( size_t current, size_t maximum ) | 19 | void IMAPwrapper::imap_progress( size_t current, size_t maximum ) |
20 | { | 20 | { |
21 | qDebug( "IMAP: %i of %i", current, maximum ); | 21 | qDebug( "IMAP: %i of %i", current, maximum ); |
22 | } | 22 | } |
23 | 23 | ||
24 | void IMAPwrapper::login() | 24 | void IMAPwrapper::login() |
25 | { | 25 | { |
26 | const char *server, *user, *pass; | 26 | const char *server, *user, *pass; |
27 | uint16_t port; | 27 | uint16_t port; |
28 | int err = MAILIMAP_NO_ERROR; | 28 | int err = MAILIMAP_NO_ERROR; |
29 | 29 | ||
30 | /* we are connected this moment */ | 30 | /* we are connected this moment */ |
31 | /* TODO: setup a timer holding the line or if connection closed - delete the value */ | 31 | /* TODO: setup a timer holding the line or if connection closed - delete the value */ |
32 | if (m_imap) { | 32 | if (m_imap) { |
33 | mailstream_flush(m_imap->imap_stream); | ||
33 | return; | 34 | return; |
34 | } | 35 | } |
35 | server = account->getServer().latin1(); | 36 | server = account->getServer().latin1(); |
36 | port = account->getPort().toUInt(); | 37 | port = account->getPort().toUInt(); |
37 | user = account->getUser().latin1(); | 38 | user = account->getUser().latin1(); |
38 | pass = account->getPassword().latin1(); | 39 | pass = account->getPassword().latin1(); |
39 | 40 | ||
40 | m_imap = mailimap_new( 20, &imap_progress ); | 41 | m_imap = mailimap_new( 20, &imap_progress ); |
41 | /* connect */ | 42 | /* connect */ |
42 | if (account->getSSL()) { | 43 | if (account->getSSL()) { |
43 | err = mailimap_ssl_connect( m_imap, (char*)server, port ); | 44 | err = mailimap_ssl_connect( m_imap, (char*)server, port ); |
44 | } else { | 45 | } else { |
45 | err = mailimap_socket_connect( m_imap, (char*)server, port ); | 46 | err = mailimap_socket_connect( m_imap, (char*)server, port ); |
46 | } | 47 | } |
47 | 48 | ||
48 | if ( err != MAILIMAP_NO_ERROR && | 49 | if ( err != MAILIMAP_NO_ERROR && |
49 | err != MAILIMAP_NO_ERROR_AUTHENTICATED && | 50 | err != MAILIMAP_NO_ERROR_AUTHENTICATED && |
50 | err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) { | 51 | err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) { |
51 | qDebug("error connecting server: %s",m_imap->imap_response); | 52 | qDebug("error connecting server: %s",m_imap->imap_response); |
52 | mailimap_free( m_imap ); | 53 | mailimap_free( m_imap ); |
53 | m_imap = 0; | 54 | m_imap = 0; |
54 | return; | 55 | return; |
55 | } | 56 | } |
56 | 57 | ||
57 | /* login */ | 58 | /* login */ |
58 | err = mailimap_login_simple( m_imap, (char*)user, (char*)pass ); | 59 | err = mailimap_login_simple( m_imap, (char*)user, (char*)pass ); |
59 | if ( err != MAILIMAP_NO_ERROR ) { | 60 | if ( err != MAILIMAP_NO_ERROR ) { |
60 | qDebug("error logging in imap: %s",m_imap->imap_response); | 61 | qDebug("error logging in imap: %s",m_imap->imap_response); |
61 | err = mailimap_close( m_imap ); | 62 | err = mailimap_close( m_imap ); |
62 | mailimap_free( m_imap ); | 63 | mailimap_free( m_imap ); |
63 | m_imap = 0; | 64 | m_imap = 0; |
64 | } | 65 | } |
65 | } | 66 | } |
66 | 67 | ||
67 | void IMAPwrapper::logout() | 68 | void IMAPwrapper::logout() |
68 | { | 69 | { |
69 | int err = MAILIMAP_NO_ERROR; | 70 | int err = MAILIMAP_NO_ERROR; |
70 | if (!m_imap) return; | 71 | if (!m_imap) return; |
71 | err = mailimap_logout( m_imap ); | 72 | err = mailimap_logout( m_imap ); |
72 | err = mailimap_close( m_imap ); | 73 | err = mailimap_close( m_imap ); |
73 | mailimap_free( m_imap ); | 74 | mailimap_free( m_imap ); |
74 | m_imap = 0; | 75 | m_imap = 0; |
75 | } | 76 | } |
76 | 77 | ||
77 | void IMAPwrapper::listMessages(const QString&mailbox,QList<RecMail> &target ) | 78 | void IMAPwrapper::listMessages(const QString&mailbox,QList<RecMail> &target ) |
78 | { | 79 | { |
79 | const char *mb; | 80 | const char *mb; |
80 | int err = MAILIMAP_NO_ERROR; | 81 | int err = MAILIMAP_NO_ERROR; |
81 | clist *result; | 82 | clist *result; |
82 | clistcell *current; | 83 | clistcell *current; |
83 | // mailimap_fetch_att *fetchAtt,*fetchAttFlags,*fetchAttDate,*fetchAttSize; | 84 | // mailimap_fetch_att *fetchAtt,*fetchAttFlags,*fetchAttDate,*fetchAttSize; |
84 | mailimap_fetch_type *fetchType; | 85 | mailimap_fetch_type *fetchType; |
85 | mailimap_set *set; | 86 | mailimap_set *set; |
86 | 87 | ||
87 | mb = mailbox.latin1(); | 88 | mb = mailbox.latin1(); |
88 | login(); | 89 | login(); |
89 | if (!m_imap) { | 90 | if (!m_imap) { |
90 | return; | 91 | return; |
91 | } | 92 | } |
92 | /* select mailbox READONLY for operations */ | 93 | /* select mailbox READONLY for operations */ |
93 | err = mailimap_examine( m_imap, (char*)mb); | 94 | err = mailimap_examine( m_imap, (char*)mb); |
94 | if ( err != MAILIMAP_NO_ERROR ) { | 95 | if ( err != MAILIMAP_NO_ERROR ) { |
95 | qDebug("error selecting mailbox: %s",m_imap->imap_response); | 96 | qDebug("error selecting mailbox: %s",m_imap->imap_response); |
96 | return; | 97 | return; |
97 | } | 98 | } |
98 | 99 | ||
99 | int last = m_imap->imap_selection_info->sel_exists; | 100 | int last = m_imap->imap_selection_info->sel_exists; |
100 | 101 | ||
101 | if (last == 0) { | 102 | if (last == 0) { |
102 | qDebug("mailbox has no mails"); | 103 | qDebug("mailbox has no mails"); |
103 | return; | 104 | return; |
104 | } | 105 | } |
105 | 106 | ||
106 | result = clist_new(); | 107 | result = clist_new(); |
107 | /* the range has to start at 1!!! not with 0!!!! */ | 108 | /* the range has to start at 1!!! not with 0!!!! */ |
108 | set = mailimap_set_new_interval( 1, last ); | 109 | set = mailimap_set_new_interval( 1, last ); |
109 | fetchType = mailimap_fetch_type_new_fetch_att_list_empty(); | 110 | fetchType = mailimap_fetch_type_new_fetch_att_list_empty(); |
110 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_envelope()); | 111 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_envelope()); |
111 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_flags()); | 112 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_flags()); |
112 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_internaldate()); | 113 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_internaldate()); |
113 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_rfc822_size()); | 114 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_rfc822_size()); |
114 | 115 | ||
115 | err = mailimap_fetch( m_imap, set, fetchType, &result ); | 116 | err = mailimap_fetch( m_imap, set, fetchType, &result ); |
116 | mailimap_set_free( set ); | 117 | mailimap_set_free( set ); |
117 | mailimap_fetch_type_free( fetchType ); | 118 | mailimap_fetch_type_free( fetchType ); |
118 | 119 | ||
119 | QString date,subject,from; | 120 | QString date,subject,from; |
120 | 121 | ||
121 | if ( err == MAILIMAP_NO_ERROR ) { | 122 | if ( err == MAILIMAP_NO_ERROR ) { |
122 | 123 | ||
123 | mailimap_msg_att * msg_att; | 124 | mailimap_msg_att * msg_att; |
124 | int i = 0; | 125 | int i = 0; |
125 | for (current = clist_begin(result); current != 0; current=clist_next(current)) { | 126 | for (current = clist_begin(result); current != 0; current=clist_next(current)) { |
126 | ++i; | 127 | ++i; |
127 | msg_att = (mailimap_msg_att*)current->data; | 128 | msg_att = (mailimap_msg_att*)current->data; |
128 | RecMail*m = parse_list_result(msg_att); | 129 | RecMail*m = parse_list_result(msg_att); |
129 | if (m) { | 130 | if (m) { |
130 | m->setNumber(i); | 131 | m->setNumber(i); |
131 | m->setMbox(mailbox); | 132 | m->setMbox(mailbox); |
132 | target.append(m); | 133 | target.append(m); |
133 | } | 134 | } |
134 | } | 135 | } |
135 | } else { | 136 | } else { |
136 | qDebug("Error fetching headers: %s",m_imap->imap_response); | 137 | qDebug("Error fetching headers: %s",m_imap->imap_response); |
137 | } | 138 | } |
138 | mailimap_fetch_list_free(result); | 139 | mailimap_fetch_list_free(result); |
139 | } | 140 | } |
140 | 141 | ||
141 | QList<IMAPFolder>* IMAPwrapper::listFolders() | 142 | QList<IMAPFolder>* IMAPwrapper::listFolders() |
142 | { | 143 | { |
143 | const char *path, *mask; | 144 | const char *path, *mask; |
144 | int err = MAILIMAP_NO_ERROR; | 145 | int err = MAILIMAP_NO_ERROR; |
145 | clist *result; | 146 | clist *result; |
146 | clistcell *current; | 147 | clistcell *current; |
147 | 148 | ||
148 | QList<IMAPFolder> * folders = new QList<IMAPFolder>(); | 149 | QList<IMAPFolder> * folders = new QList<IMAPFolder>(); |
149 | folders->setAutoDelete( true ); | 150 | folders->setAutoDelete( true ); |
150 | login(); | 151 | login(); |
151 | if (!m_imap) { | 152 | if (!m_imap) { |
152 | return folders; | 153 | return folders; |
153 | } | 154 | } |
154 | 155 | ||
155 | /* | 156 | /* |
156 | * First we have to check for INBOX 'cause it sometimes it's not inside the path. | 157 | * First we have to check for INBOX 'cause it sometimes it's not inside the path. |
157 | * We must not forget to filter them out in next loop! | 158 | * We must not forget to filter them out in next loop! |
158 | * it seems like ugly code. and yes - it is ugly code. but the best way. | 159 | * it seems like ugly code. and yes - it is ugly code. but the best way. |
159 | */ | 160 | */ |
160 | QString temp; | 161 | QString temp; |
161 | mask = "INBOX" ; | 162 | mask = "INBOX" ; |
162 | result = clist_new(); | 163 | result = clist_new(); |
163 | mailimap_mailbox_list *list; | 164 | mailimap_mailbox_list *list; |
164 | err = mailimap_list( m_imap, (char*)"", (char*)mask, &result ); | 165 | err = mailimap_list( m_imap, (char*)"", (char*)mask, &result ); |
165 | if ( err == MAILIMAP_NO_ERROR ) { | 166 | if ( err == MAILIMAP_NO_ERROR ) { |
166 | current = result->first; | 167 | current = result->first; |
167 | for ( int i = result->count; i > 0; i-- ) { | 168 | for ( int i = result->count; i > 0; i-- ) { |
168 | list = (mailimap_mailbox_list *) current->data; | 169 | list = (mailimap_mailbox_list *) current->data; |
169 | // it is better use the deep copy mechanism of qt itself | 170 | // it is better use the deep copy mechanism of qt itself |
170 | // instead of using strdup! | 171 | // instead of using strdup! |
171 | temp = list->mb_name; | 172 | temp = list->mb_name; |
172 | folders->append( new IMAPFolder(temp)); | 173 | folders->append( new IMAPFolder(temp)); |
173 | current = current->next; | 174 | current = current->next; |
174 | } | 175 | } |
175 | } else { | 176 | } else { |
176 | qDebug("error fetching folders: %s",m_imap->imap_response); | 177 | qDebug("error fetching folders: %s",m_imap->imap_response); |
177 | } | 178 | } |
178 | mailimap_list_result_free( result ); | 179 | mailimap_list_result_free( result ); |
179 | 180 | ||
180 | /* | 181 | /* |
181 | * second stage - get the other then inbox folders | 182 | * second stage - get the other then inbox folders |
182 | */ | 183 | */ |
183 | mask = "*" ; | 184 | mask = "*" ; |
184 | path = account->getPrefix().latin1(); | 185 | path = account->getPrefix().latin1(); |
185 | if (!path) path = ""; | 186 | if (!path) path = ""; |
186 | result = clist_new(); | 187 | result = clist_new(); |
187 | qDebug(path); | 188 | qDebug(path); |
188 | err = mailimap_list( m_imap, (char*)path, (char*)mask, &result ); | 189 | err = mailimap_list( m_imap, (char*)path, (char*)mask, &result ); |
189 | if ( err == MAILIMAP_NO_ERROR ) { | 190 | if ( err == MAILIMAP_NO_ERROR ) { |
190 | current = result->first; | 191 | current = result->first; |
191 | for ( int i = result->count; i > 0; i-- ) { | 192 | for ( int i = result->count; i > 0; i-- ) { |
192 | list = (mailimap_mailbox_list *) current->data; | 193 | list = (mailimap_mailbox_list *) current->data; |
193 | // it is better use the deep copy mechanism of qt itself | 194 | // it is better use the deep copy mechanism of qt itself |
194 | // instead of using strdup! | 195 | // instead of using strdup! |
195 | temp = list->mb_name; | 196 | temp = list->mb_name; |
196 | current = current->next; | 197 | current = current->next; |
197 | if (temp.lower()=="inbox") | 198 | if (temp.lower()=="inbox") |
198 | continue; | 199 | continue; |
199 | folders->append(new IMAPFolder(temp)); | 200 | folders->append(new IMAPFolder(temp)); |
200 | 201 | ||
201 | } | 202 | } |
202 | } else { | 203 | } else { |
203 | qDebug("error fetching folders %s",m_imap->imap_response); | 204 | qDebug("error fetching folders %s",m_imap->imap_response); |
204 | } | 205 | } |
205 | mailimap_list_result_free( result ); | 206 | mailimap_list_result_free( result ); |
206 | return folders; | 207 | return folders; |
207 | } | 208 | } |
208 | 209 | ||
209 | RecMail*IMAPwrapper::parse_list_result(mailimap_msg_att* m_att) | 210 | RecMail*IMAPwrapper::parse_list_result(mailimap_msg_att* m_att) |
210 | { | 211 | { |
211 | RecMail * m = 0; | 212 | RecMail * m = 0; |
212 | mailimap_msg_att_item *item=0; | 213 | mailimap_msg_att_item *item=0; |
213 | clistcell *current,*c,*cf; | 214 | clistcell *current,*c,*cf; |
214 | mailimap_msg_att_dynamic*flist; | 215 | mailimap_msg_att_dynamic*flist; |
215 | mailimap_flag_fetch*cflag; | 216 | mailimap_flag_fetch*cflag; |
216 | int size; | 217 | int size; |
217 | QBitArray mFlags(7); | 218 | QBitArray mFlags(7); |
218 | QStringList addresslist; | 219 | QStringList addresslist; |
219 | 220 | ||
220 | if (!m_att) { | 221 | if (!m_att) { |
221 | return m; | 222 | return m; |
222 | } | 223 | } |
223 | m = new RecMail(); | 224 | m = new RecMail(); |
224 | for (c = clist_begin(m_att->att_list); c!=NULL;c=clist_next(c) ) { | 225 | for (c = clist_begin(m_att->att_list); c!=NULL;c=clist_next(c) ) { |
225 | current = c; | 226 | current = c; |
226 | size = 0; | 227 | size = 0; |
227 | item = (mailimap_msg_att_item*)current->data; | 228 | item = (mailimap_msg_att_item*)current->data; |
228 | if (item->att_type!=MAILIMAP_MSG_ATT_ITEM_STATIC) { | 229 | if (item->att_type!=MAILIMAP_MSG_ATT_ITEM_STATIC) { |
229 | flist = (mailimap_msg_att_dynamic*)item->att_data.att_dyn; | 230 | flist = (mailimap_msg_att_dynamic*)item->att_data.att_dyn; |
230 | if (!flist->att_list) { | 231 | if (!flist->att_list) { |
231 | continue; | 232 | continue; |
232 | } | 233 | } |
233 | cf = flist->att_list->first; | 234 | cf = flist->att_list->first; |
234 | for (cf = clist_begin(flist->att_list); cf!=NULL; cf = clist_next(cf)) { | 235 | for (cf = clist_begin(flist->att_list); cf!=NULL; cf = clist_next(cf)) { |
235 | cflag = (mailimap_flag_fetch*)cf->data; | 236 | cflag = (mailimap_flag_fetch*)cf->data; |
236 | if (cflag->fl_type==MAILIMAP_FLAG_FETCH_OTHER && cflag->fl_flag!=0) { | 237 | if (cflag->fl_type==MAILIMAP_FLAG_FETCH_OTHER && cflag->fl_flag!=0) { |
237 | switch (cflag->fl_flag->fl_type) { | 238 | switch (cflag->fl_flag->fl_type) { |
238 | case MAILIMAP_FLAG_ANSWERED: /* \Answered flag */ | 239 | case MAILIMAP_FLAG_ANSWERED: /* \Answered flag */ |
239 | mFlags.setBit(FLAG_ANSWERED); | 240 | mFlags.setBit(FLAG_ANSWERED); |
240 | break; | 241 | break; |
241 | case MAILIMAP_FLAG_FLAGGED: /* \Flagged flag */ | 242 | case MAILIMAP_FLAG_FLAGGED: /* \Flagged flag */ |
242 | mFlags.setBit(FLAG_FLAGGED); | 243 | mFlags.setBit(FLAG_FLAGGED); |
243 | break; | 244 | break; |
244 | case MAILIMAP_FLAG_DELETED: /* \Deleted flag */ | 245 | case MAILIMAP_FLAG_DELETED: /* \Deleted flag */ |
245 | mFlags.setBit(FLAG_DELETED); | 246 | mFlags.setBit(FLAG_DELETED); |
246 | break; | 247 | break; |
247 | case MAILIMAP_FLAG_SEEN: /* \Seen flag */ | 248 | case MAILIMAP_FLAG_SEEN: /* \Seen flag */ |
248 | mFlags.setBit(FLAG_SEEN); | 249 | mFlags.setBit(FLAG_SEEN); |
249 | break; | 250 | break; |
250 | case MAILIMAP_FLAG_DRAFT: /* \Draft flag */ | 251 | case MAILIMAP_FLAG_DRAFT: /* \Draft flag */ |
251 | mFlags.setBit(FLAG_DRAFT); | 252 | mFlags.setBit(FLAG_DRAFT); |
252 | break; | 253 | break; |
253 | case MAILIMAP_FLAG_KEYWORD: /* keyword flag */ | 254 | case MAILIMAP_FLAG_KEYWORD: /* keyword flag */ |
254 | break; | 255 | break; |
255 | case MAILIMAP_FLAG_EXTENSION: /* \extension flag */ | 256 | case MAILIMAP_FLAG_EXTENSION: /* \extension flag */ |
256 | break; | 257 | break; |
257 | default: | 258 | default: |
258 | break; | 259 | break; |
259 | } | 260 | } |
260 | } else if (cflag->fl_type==MAILIMAP_FLAG_FETCH_RECENT) { | 261 | } else if (cflag->fl_type==MAILIMAP_FLAG_FETCH_RECENT) { |
261 | mFlags.setBit(FLAG_RECENT); | 262 | mFlags.setBit(FLAG_RECENT); |
262 | } | 263 | } |
263 | } | 264 | } |
264 | continue; | 265 | continue; |
265 | } | 266 | } |
266 | if (item->att_data.att_static->att_type==MAILIMAP_MSG_ATT_ENVELOPE) { | 267 | if (item->att_data.att_static->att_type==MAILIMAP_MSG_ATT_ENVELOPE) { |
267 | mailimap_envelope * head = item->att_data.att_static->att_data.att_env; | 268 | mailimap_envelope * head = item->att_data.att_static->att_data.att_env; |
268 | m->setDate(head->env_date); | 269 | m->setDate(head->env_date); |
269 | m->setSubject(head->env_subject); | 270 | m->setSubject(head->env_subject); |
270 | if (head->env_from!=NULL) { | 271 | if (head->env_from!=NULL) { |
271 | addresslist = address_list_to_stringlist(head->env_from->frm_list); | 272 | addresslist = address_list_to_stringlist(head->env_from->frm_list); |
272 | if (addresslist.count()) { | 273 | if (addresslist.count()) { |
273 | m->setFrom(addresslist.first()); | 274 | m->setFrom(addresslist.first()); |
274 | } | 275 | } |
275 | } | 276 | } |
276 | if (head->env_to!=NULL) { | 277 | if (head->env_to!=NULL) { |
277 | addresslist = address_list_to_stringlist(head->env_to->to_list); | 278 | addresslist = address_list_to_stringlist(head->env_to->to_list); |
278 | m->setTo(addresslist); | 279 | m->setTo(addresslist); |
279 | } | 280 | } |
280 | if (head->env_cc!=NULL) { | 281 | if (head->env_cc!=NULL) { |
281 | addresslist = address_list_to_stringlist(head->env_cc->cc_list); | 282 | addresslist = address_list_to_stringlist(head->env_cc->cc_list); |
282 | m->setCC(addresslist); | 283 | m->setCC(addresslist); |
283 | } | 284 | } |
284 | if (head->env_bcc!=NULL) { | 285 | if (head->env_bcc!=NULL) { |
285 | addresslist = address_list_to_stringlist(head->env_bcc->bcc_list); | 286 | addresslist = address_list_to_stringlist(head->env_bcc->bcc_list); |
286 | m->setBcc(addresslist); | 287 | m->setBcc(addresslist); |
287 | } | 288 | } |
288 | if (head->env_reply_to!=NULL) { | 289 | if (head->env_reply_to!=NULL) { |
diff --git a/noncore/net/mail/libmailwrapper/imapwrapper.cpp b/noncore/net/mail/libmailwrapper/imapwrapper.cpp index 11bf3b8..48e476b 100644 --- a/noncore/net/mail/libmailwrapper/imapwrapper.cpp +++ b/noncore/net/mail/libmailwrapper/imapwrapper.cpp | |||
@@ -1,288 +1,289 @@ | |||
1 | 1 | ||
2 | #include <stdlib.h> | 2 | #include <stdlib.h> |
3 | 3 | ||
4 | #include "imapwrapper.h" | 4 | #include "imapwrapper.h" |
5 | #include "mailtypes.h" | 5 | #include "mailtypes.h" |
6 | #include <libetpan/mailimap.h> | 6 | #include <libetpan/mailimap.h> |
7 | 7 | ||
8 | IMAPwrapper::IMAPwrapper( IMAPaccount *a ) | 8 | IMAPwrapper::IMAPwrapper( IMAPaccount *a ) |
9 | { | 9 | { |
10 | account = a; | 10 | account = a; |
11 | m_imap = 0; | 11 | m_imap = 0; |
12 | } | 12 | } |
13 | 13 | ||
14 | IMAPwrapper::~IMAPwrapper() | 14 | IMAPwrapper::~IMAPwrapper() |
15 | { | 15 | { |
16 | logout(); | 16 | logout(); |
17 | } | 17 | } |
18 | 18 | ||
19 | void IMAPwrapper::imap_progress( size_t current, size_t maximum ) | 19 | void IMAPwrapper::imap_progress( size_t current, size_t maximum ) |
20 | { | 20 | { |
21 | qDebug( "IMAP: %i of %i", current, maximum ); | 21 | qDebug( "IMAP: %i of %i", current, maximum ); |
22 | } | 22 | } |
23 | 23 | ||
24 | void IMAPwrapper::login() | 24 | void IMAPwrapper::login() |
25 | { | 25 | { |
26 | const char *server, *user, *pass; | 26 | const char *server, *user, *pass; |
27 | uint16_t port; | 27 | uint16_t port; |
28 | int err = MAILIMAP_NO_ERROR; | 28 | int err = MAILIMAP_NO_ERROR; |
29 | 29 | ||
30 | /* we are connected this moment */ | 30 | /* we are connected this moment */ |
31 | /* TODO: setup a timer holding the line or if connection closed - delete the value */ | 31 | /* TODO: setup a timer holding the line or if connection closed - delete the value */ |
32 | if (m_imap) { | 32 | if (m_imap) { |
33 | mailstream_flush(m_imap->imap_stream); | ||
33 | return; | 34 | return; |
34 | } | 35 | } |
35 | server = account->getServer().latin1(); | 36 | server = account->getServer().latin1(); |
36 | port = account->getPort().toUInt(); | 37 | port = account->getPort().toUInt(); |
37 | user = account->getUser().latin1(); | 38 | user = account->getUser().latin1(); |
38 | pass = account->getPassword().latin1(); | 39 | pass = account->getPassword().latin1(); |
39 | 40 | ||
40 | m_imap = mailimap_new( 20, &imap_progress ); | 41 | m_imap = mailimap_new( 20, &imap_progress ); |
41 | /* connect */ | 42 | /* connect */ |
42 | if (account->getSSL()) { | 43 | if (account->getSSL()) { |
43 | err = mailimap_ssl_connect( m_imap, (char*)server, port ); | 44 | err = mailimap_ssl_connect( m_imap, (char*)server, port ); |
44 | } else { | 45 | } else { |
45 | err = mailimap_socket_connect( m_imap, (char*)server, port ); | 46 | err = mailimap_socket_connect( m_imap, (char*)server, port ); |
46 | } | 47 | } |
47 | 48 | ||
48 | if ( err != MAILIMAP_NO_ERROR && | 49 | if ( err != MAILIMAP_NO_ERROR && |
49 | err != MAILIMAP_NO_ERROR_AUTHENTICATED && | 50 | err != MAILIMAP_NO_ERROR_AUTHENTICATED && |
50 | err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) { | 51 | err != MAILIMAP_NO_ERROR_NON_AUTHENTICATED ) { |
51 | qDebug("error connecting server: %s",m_imap->imap_response); | 52 | qDebug("error connecting server: %s",m_imap->imap_response); |
52 | mailimap_free( m_imap ); | 53 | mailimap_free( m_imap ); |
53 | m_imap = 0; | 54 | m_imap = 0; |
54 | return; | 55 | return; |
55 | } | 56 | } |
56 | 57 | ||
57 | /* login */ | 58 | /* login */ |
58 | err = mailimap_login_simple( m_imap, (char*)user, (char*)pass ); | 59 | err = mailimap_login_simple( m_imap, (char*)user, (char*)pass ); |
59 | if ( err != MAILIMAP_NO_ERROR ) { | 60 | if ( err != MAILIMAP_NO_ERROR ) { |
60 | qDebug("error logging in imap: %s",m_imap->imap_response); | 61 | qDebug("error logging in imap: %s",m_imap->imap_response); |
61 | err = mailimap_close( m_imap ); | 62 | err = mailimap_close( m_imap ); |
62 | mailimap_free( m_imap ); | 63 | mailimap_free( m_imap ); |
63 | m_imap = 0; | 64 | m_imap = 0; |
64 | } | 65 | } |
65 | } | 66 | } |
66 | 67 | ||
67 | void IMAPwrapper::logout() | 68 | void IMAPwrapper::logout() |
68 | { | 69 | { |
69 | int err = MAILIMAP_NO_ERROR; | 70 | int err = MAILIMAP_NO_ERROR; |
70 | if (!m_imap) return; | 71 | if (!m_imap) return; |
71 | err = mailimap_logout( m_imap ); | 72 | err = mailimap_logout( m_imap ); |
72 | err = mailimap_close( m_imap ); | 73 | err = mailimap_close( m_imap ); |
73 | mailimap_free( m_imap ); | 74 | mailimap_free( m_imap ); |
74 | m_imap = 0; | 75 | m_imap = 0; |
75 | } | 76 | } |
76 | 77 | ||
77 | void IMAPwrapper::listMessages(const QString&mailbox,QList<RecMail> &target ) | 78 | void IMAPwrapper::listMessages(const QString&mailbox,QList<RecMail> &target ) |
78 | { | 79 | { |
79 | const char *mb; | 80 | const char *mb; |
80 | int err = MAILIMAP_NO_ERROR; | 81 | int err = MAILIMAP_NO_ERROR; |
81 | clist *result; | 82 | clist *result; |
82 | clistcell *current; | 83 | clistcell *current; |
83 | // mailimap_fetch_att *fetchAtt,*fetchAttFlags,*fetchAttDate,*fetchAttSize; | 84 | // mailimap_fetch_att *fetchAtt,*fetchAttFlags,*fetchAttDate,*fetchAttSize; |
84 | mailimap_fetch_type *fetchType; | 85 | mailimap_fetch_type *fetchType; |
85 | mailimap_set *set; | 86 | mailimap_set *set; |
86 | 87 | ||
87 | mb = mailbox.latin1(); | 88 | mb = mailbox.latin1(); |
88 | login(); | 89 | login(); |
89 | if (!m_imap) { | 90 | if (!m_imap) { |
90 | return; | 91 | return; |
91 | } | 92 | } |
92 | /* select mailbox READONLY for operations */ | 93 | /* select mailbox READONLY for operations */ |
93 | err = mailimap_examine( m_imap, (char*)mb); | 94 | err = mailimap_examine( m_imap, (char*)mb); |
94 | if ( err != MAILIMAP_NO_ERROR ) { | 95 | if ( err != MAILIMAP_NO_ERROR ) { |
95 | qDebug("error selecting mailbox: %s",m_imap->imap_response); | 96 | qDebug("error selecting mailbox: %s",m_imap->imap_response); |
96 | return; | 97 | return; |
97 | } | 98 | } |
98 | 99 | ||
99 | int last = m_imap->imap_selection_info->sel_exists; | 100 | int last = m_imap->imap_selection_info->sel_exists; |
100 | 101 | ||
101 | if (last == 0) { | 102 | if (last == 0) { |
102 | qDebug("mailbox has no mails"); | 103 | qDebug("mailbox has no mails"); |
103 | return; | 104 | return; |
104 | } | 105 | } |
105 | 106 | ||
106 | result = clist_new(); | 107 | result = clist_new(); |
107 | /* the range has to start at 1!!! not with 0!!!! */ | 108 | /* the range has to start at 1!!! not with 0!!!! */ |
108 | set = mailimap_set_new_interval( 1, last ); | 109 | set = mailimap_set_new_interval( 1, last ); |
109 | fetchType = mailimap_fetch_type_new_fetch_att_list_empty(); | 110 | fetchType = mailimap_fetch_type_new_fetch_att_list_empty(); |
110 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_envelope()); | 111 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_envelope()); |
111 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_flags()); | 112 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_flags()); |
112 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_internaldate()); | 113 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_internaldate()); |
113 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_rfc822_size()); | 114 | mailimap_fetch_type_new_fetch_att_list_add(fetchType,mailimap_fetch_att_new_rfc822_size()); |
114 | 115 | ||
115 | err = mailimap_fetch( m_imap, set, fetchType, &result ); | 116 | err = mailimap_fetch( m_imap, set, fetchType, &result ); |
116 | mailimap_set_free( set ); | 117 | mailimap_set_free( set ); |
117 | mailimap_fetch_type_free( fetchType ); | 118 | mailimap_fetch_type_free( fetchType ); |
118 | 119 | ||
119 | QString date,subject,from; | 120 | QString date,subject,from; |
120 | 121 | ||
121 | if ( err == MAILIMAP_NO_ERROR ) { | 122 | if ( err == MAILIMAP_NO_ERROR ) { |
122 | 123 | ||
123 | mailimap_msg_att * msg_att; | 124 | mailimap_msg_att * msg_att; |
124 | int i = 0; | 125 | int i = 0; |
125 | for (current = clist_begin(result); current != 0; current=clist_next(current)) { | 126 | for (current = clist_begin(result); current != 0; current=clist_next(current)) { |
126 | ++i; | 127 | ++i; |
127 | msg_att = (mailimap_msg_att*)current->data; | 128 | msg_att = (mailimap_msg_att*)current->data; |
128 | RecMail*m = parse_list_result(msg_att); | 129 | RecMail*m = parse_list_result(msg_att); |
129 | if (m) { | 130 | if (m) { |
130 | m->setNumber(i); | 131 | m->setNumber(i); |
131 | m->setMbox(mailbox); | 132 | m->setMbox(mailbox); |
132 | target.append(m); | 133 | target.append(m); |
133 | } | 134 | } |
134 | } | 135 | } |
135 | } else { | 136 | } else { |
136 | qDebug("Error fetching headers: %s",m_imap->imap_response); | 137 | qDebug("Error fetching headers: %s",m_imap->imap_response); |
137 | } | 138 | } |
138 | mailimap_fetch_list_free(result); | 139 | mailimap_fetch_list_free(result); |
139 | } | 140 | } |
140 | 141 | ||
141 | QList<IMAPFolder>* IMAPwrapper::listFolders() | 142 | QList<IMAPFolder>* IMAPwrapper::listFolders() |
142 | { | 143 | { |
143 | const char *path, *mask; | 144 | const char *path, *mask; |
144 | int err = MAILIMAP_NO_ERROR; | 145 | int err = MAILIMAP_NO_ERROR; |
145 | clist *result; | 146 | clist *result; |
146 | clistcell *current; | 147 | clistcell *current; |
147 | 148 | ||
148 | QList<IMAPFolder> * folders = new QList<IMAPFolder>(); | 149 | QList<IMAPFolder> * folders = new QList<IMAPFolder>(); |
149 | folders->setAutoDelete( true ); | 150 | folders->setAutoDelete( true ); |
150 | login(); | 151 | login(); |
151 | if (!m_imap) { | 152 | if (!m_imap) { |
152 | return folders; | 153 | return folders; |
153 | } | 154 | } |
154 | 155 | ||
155 | /* | 156 | /* |
156 | * First we have to check for INBOX 'cause it sometimes it's not inside the path. | 157 | * First we have to check for INBOX 'cause it sometimes it's not inside the path. |
157 | * We must not forget to filter them out in next loop! | 158 | * We must not forget to filter them out in next loop! |
158 | * it seems like ugly code. and yes - it is ugly code. but the best way. | 159 | * it seems like ugly code. and yes - it is ugly code. but the best way. |
159 | */ | 160 | */ |
160 | QString temp; | 161 | QString temp; |
161 | mask = "INBOX" ; | 162 | mask = "INBOX" ; |
162 | result = clist_new(); | 163 | result = clist_new(); |
163 | mailimap_mailbox_list *list; | 164 | mailimap_mailbox_list *list; |
164 | err = mailimap_list( m_imap, (char*)"", (char*)mask, &result ); | 165 | err = mailimap_list( m_imap, (char*)"", (char*)mask, &result ); |
165 | if ( err == MAILIMAP_NO_ERROR ) { | 166 | if ( err == MAILIMAP_NO_ERROR ) { |
166 | current = result->first; | 167 | current = result->first; |
167 | for ( int i = result->count; i > 0; i-- ) { | 168 | for ( int i = result->count; i > 0; i-- ) { |
168 | list = (mailimap_mailbox_list *) current->data; | 169 | list = (mailimap_mailbox_list *) current->data; |
169 | // it is better use the deep copy mechanism of qt itself | 170 | // it is better use the deep copy mechanism of qt itself |
170 | // instead of using strdup! | 171 | // instead of using strdup! |
171 | temp = list->mb_name; | 172 | temp = list->mb_name; |
172 | folders->append( new IMAPFolder(temp)); | 173 | folders->append( new IMAPFolder(temp)); |
173 | current = current->next; | 174 | current = current->next; |
174 | } | 175 | } |
175 | } else { | 176 | } else { |
176 | qDebug("error fetching folders: %s",m_imap->imap_response); | 177 | qDebug("error fetching folders: %s",m_imap->imap_response); |
177 | } | 178 | } |
178 | mailimap_list_result_free( result ); | 179 | mailimap_list_result_free( result ); |
179 | 180 | ||
180 | /* | 181 | /* |
181 | * second stage - get the other then inbox folders | 182 | * second stage - get the other then inbox folders |
182 | */ | 183 | */ |
183 | mask = "*" ; | 184 | mask = "*" ; |
184 | path = account->getPrefix().latin1(); | 185 | path = account->getPrefix().latin1(); |
185 | if (!path) path = ""; | 186 | if (!path) path = ""; |
186 | result = clist_new(); | 187 | result = clist_new(); |
187 | qDebug(path); | 188 | qDebug(path); |
188 | err = mailimap_list( m_imap, (char*)path, (char*)mask, &result ); | 189 | err = mailimap_list( m_imap, (char*)path, (char*)mask, &result ); |
189 | if ( err == MAILIMAP_NO_ERROR ) { | 190 | if ( err == MAILIMAP_NO_ERROR ) { |
190 | current = result->first; | 191 | current = result->first; |
191 | for ( int i = result->count; i > 0; i-- ) { | 192 | for ( int i = result->count; i > 0; i-- ) { |
192 | list = (mailimap_mailbox_list *) current->data; | 193 | list = (mailimap_mailbox_list *) current->data; |
193 | // it is better use the deep copy mechanism of qt itself | 194 | // it is better use the deep copy mechanism of qt itself |
194 | // instead of using strdup! | 195 | // instead of using strdup! |
195 | temp = list->mb_name; | 196 | temp = list->mb_name; |
196 | current = current->next; | 197 | current = current->next; |
197 | if (temp.lower()=="inbox") | 198 | if (temp.lower()=="inbox") |
198 | continue; | 199 | continue; |
199 | folders->append(new IMAPFolder(temp)); | 200 | folders->append(new IMAPFolder(temp)); |
200 | 201 | ||
201 | } | 202 | } |
202 | } else { | 203 | } else { |
203 | qDebug("error fetching folders %s",m_imap->imap_response); | 204 | qDebug("error fetching folders %s",m_imap->imap_response); |
204 | } | 205 | } |
205 | mailimap_list_result_free( result ); | 206 | mailimap_list_result_free( result ); |
206 | return folders; | 207 | return folders; |
207 | } | 208 | } |
208 | 209 | ||
209 | RecMail*IMAPwrapper::parse_list_result(mailimap_msg_att* m_att) | 210 | RecMail*IMAPwrapper::parse_list_result(mailimap_msg_att* m_att) |
210 | { | 211 | { |
211 | RecMail * m = 0; | 212 | RecMail * m = 0; |
212 | mailimap_msg_att_item *item=0; | 213 | mailimap_msg_att_item *item=0; |
213 | clistcell *current,*c,*cf; | 214 | clistcell *current,*c,*cf; |
214 | mailimap_msg_att_dynamic*flist; | 215 | mailimap_msg_att_dynamic*flist; |
215 | mailimap_flag_fetch*cflag; | 216 | mailimap_flag_fetch*cflag; |
216 | int size; | 217 | int size; |
217 | QBitArray mFlags(7); | 218 | QBitArray mFlags(7); |
218 | QStringList addresslist; | 219 | QStringList addresslist; |
219 | 220 | ||
220 | if (!m_att) { | 221 | if (!m_att) { |
221 | return m; | 222 | return m; |
222 | } | 223 | } |
223 | m = new RecMail(); | 224 | m = new RecMail(); |
224 | for (c = clist_begin(m_att->att_list); c!=NULL;c=clist_next(c) ) { | 225 | for (c = clist_begin(m_att->att_list); c!=NULL;c=clist_next(c) ) { |
225 | current = c; | 226 | current = c; |
226 | size = 0; | 227 | size = 0; |
227 | item = (mailimap_msg_att_item*)current->data; | 228 | item = (mailimap_msg_att_item*)current->data; |
228 | if (item->att_type!=MAILIMAP_MSG_ATT_ITEM_STATIC) { | 229 | if (item->att_type!=MAILIMAP_MSG_ATT_ITEM_STATIC) { |
229 | flist = (mailimap_msg_att_dynamic*)item->att_data.att_dyn; | 230 | flist = (mailimap_msg_att_dynamic*)item->att_data.att_dyn; |
230 | if (!flist->att_list) { | 231 | if (!flist->att_list) { |
231 | continue; | 232 | continue; |
232 | } | 233 | } |
233 | cf = flist->att_list->first; | 234 | cf = flist->att_list->first; |
234 | for (cf = clist_begin(flist->att_list); cf!=NULL; cf = clist_next(cf)) { | 235 | for (cf = clist_begin(flist->att_list); cf!=NULL; cf = clist_next(cf)) { |
235 | cflag = (mailimap_flag_fetch*)cf->data; | 236 | cflag = (mailimap_flag_fetch*)cf->data; |
236 | if (cflag->fl_type==MAILIMAP_FLAG_FETCH_OTHER && cflag->fl_flag!=0) { | 237 | if (cflag->fl_type==MAILIMAP_FLAG_FETCH_OTHER && cflag->fl_flag!=0) { |
237 | switch (cflag->fl_flag->fl_type) { | 238 | switch (cflag->fl_flag->fl_type) { |
238 | case MAILIMAP_FLAG_ANSWERED: /* \Answered flag */ | 239 | case MAILIMAP_FLAG_ANSWERED: /* \Answered flag */ |
239 | mFlags.setBit(FLAG_ANSWERED); | 240 | mFlags.setBit(FLAG_ANSWERED); |
240 | break; | 241 | break; |
241 | case MAILIMAP_FLAG_FLAGGED: /* \Flagged flag */ | 242 | case MAILIMAP_FLAG_FLAGGED: /* \Flagged flag */ |
242 | mFlags.setBit(FLAG_FLAGGED); | 243 | mFlags.setBit(FLAG_FLAGGED); |
243 | break; | 244 | break; |
244 | case MAILIMAP_FLAG_DELETED: /* \Deleted flag */ | 245 | case MAILIMAP_FLAG_DELETED: /* \Deleted flag */ |
245 | mFlags.setBit(FLAG_DELETED); | 246 | mFlags.setBit(FLAG_DELETED); |
246 | break; | 247 | break; |
247 | case MAILIMAP_FLAG_SEEN: /* \Seen flag */ | 248 | case MAILIMAP_FLAG_SEEN: /* \Seen flag */ |
248 | mFlags.setBit(FLAG_SEEN); | 249 | mFlags.setBit(FLAG_SEEN); |
249 | break; | 250 | break; |
250 | case MAILIMAP_FLAG_DRAFT: /* \Draft flag */ | 251 | case MAILIMAP_FLAG_DRAFT: /* \Draft flag */ |
251 | mFlags.setBit(FLAG_DRAFT); | 252 | mFlags.setBit(FLAG_DRAFT); |
252 | break; | 253 | break; |
253 | case MAILIMAP_FLAG_KEYWORD: /* keyword flag */ | 254 | case MAILIMAP_FLAG_KEYWORD: /* keyword flag */ |
254 | break; | 255 | break; |
255 | case MAILIMAP_FLAG_EXTENSION: /* \extension flag */ | 256 | case MAILIMAP_FLAG_EXTENSION: /* \extension flag */ |
256 | break; | 257 | break; |
257 | default: | 258 | default: |
258 | break; | 259 | break; |
259 | } | 260 | } |
260 | } else if (cflag->fl_type==MAILIMAP_FLAG_FETCH_RECENT) { | 261 | } else if (cflag->fl_type==MAILIMAP_FLAG_FETCH_RECENT) { |
261 | mFlags.setBit(FLAG_RECENT); | 262 | mFlags.setBit(FLAG_RECENT); |
262 | } | 263 | } |
263 | } | 264 | } |
264 | continue; | 265 | continue; |
265 | } | 266 | } |
266 | if (item->att_data.att_static->att_type==MAILIMAP_MSG_ATT_ENVELOPE) { | 267 | if (item->att_data.att_static->att_type==MAILIMAP_MSG_ATT_ENVELOPE) { |
267 | mailimap_envelope * head = item->att_data.att_static->att_data.att_env; | 268 | mailimap_envelope * head = item->att_data.att_static->att_data.att_env; |
268 | m->setDate(head->env_date); | 269 | m->setDate(head->env_date); |
269 | m->setSubject(head->env_subject); | 270 | m->setSubject(head->env_subject); |
270 | if (head->env_from!=NULL) { | 271 | if (head->env_from!=NULL) { |
271 | addresslist = address_list_to_stringlist(head->env_from->frm_list); | 272 | addresslist = address_list_to_stringlist(head->env_from->frm_list); |
272 | if (addresslist.count()) { | 273 | if (addresslist.count()) { |
273 | m->setFrom(addresslist.first()); | 274 | m->setFrom(addresslist.first()); |
274 | } | 275 | } |
275 | } | 276 | } |
276 | if (head->env_to!=NULL) { | 277 | if (head->env_to!=NULL) { |
277 | addresslist = address_list_to_stringlist(head->env_to->to_list); | 278 | addresslist = address_list_to_stringlist(head->env_to->to_list); |
278 | m->setTo(addresslist); | 279 | m->setTo(addresslist); |
279 | } | 280 | } |
280 | if (head->env_cc!=NULL) { | 281 | if (head->env_cc!=NULL) { |
281 | addresslist = address_list_to_stringlist(head->env_cc->cc_list); | 282 | addresslist = address_list_to_stringlist(head->env_cc->cc_list); |
282 | m->setCC(addresslist); | 283 | m->setCC(addresslist); |
283 | } | 284 | } |
284 | if (head->env_bcc!=NULL) { | 285 | if (head->env_bcc!=NULL) { |
285 | addresslist = address_list_to_stringlist(head->env_bcc->bcc_list); | 286 | addresslist = address_list_to_stringlist(head->env_bcc->bcc_list); |
286 | m->setBcc(addresslist); | 287 | m->setBcc(addresslist); |
287 | } | 288 | } |
288 | if (head->env_reply_to!=NULL) { | 289 | if (head->env_reply_to!=NULL) { |