-rw-r--r-- | noncore/net/mail/libmailwrapper/pop3wrapper.cpp | 5 | ||||
-rw-r--r-- | noncore/net/mail/pop3wrapper.cpp | 5 |
2 files changed, 8 insertions, 2 deletions
diff --git a/noncore/net/mail/libmailwrapper/pop3wrapper.cpp b/noncore/net/mail/libmailwrapper/pop3wrapper.cpp index b4a8f4b..a33a16b 100644 --- a/noncore/net/mail/libmailwrapper/pop3wrapper.cpp +++ b/noncore/net/mail/libmailwrapper/pop3wrapper.cpp | |||
@@ -1,173 +1,175 @@ | |||
1 | #include <stdlib.h> | 1 | #include <stdlib.h> |
2 | #include "pop3wrapper.h" | 2 | #include "pop3wrapper.h" |
3 | #include "mailtypes.h" | 3 | #include "mailtypes.h" |
4 | #include <libetpan/mailpop3.h> | 4 | #include <libetpan/mailpop3.h> |
5 | 5 | ||
6 | POP3wrapper::POP3wrapper( POP3account *a ) | 6 | POP3wrapper::POP3wrapper( POP3account *a ) |
7 | { | 7 | { |
8 | account = a; | 8 | account = a; |
9 | m_pop3 = NULL; | 9 | m_pop3 = NULL; |
10 | } | 10 | } |
11 | 11 | ||
12 | POP3wrapper::~POP3wrapper() | 12 | POP3wrapper::~POP3wrapper() |
13 | { | 13 | { |
14 | logout(); | 14 | logout(); |
15 | } | 15 | } |
16 | 16 | ||
17 | void POP3wrapper::pop3_progress( size_t current, size_t maximum ) | 17 | void POP3wrapper::pop3_progress( size_t current, size_t maximum ) |
18 | { | 18 | { |
19 | //qDebug( "POP3: %i of %i", current, maximum ); | 19 | //qDebug( "POP3: %i of %i", current, maximum ); |
20 | } | 20 | } |
21 | 21 | ||
22 | RecBody POP3wrapper::fetchBody( const RecMail &mail ) | 22 | RecBody POP3wrapper::fetchBody( const RecMail &mail ) |
23 | { | 23 | { |
24 | int err = MAILPOP3_NO_ERROR; | 24 | int err = MAILPOP3_NO_ERROR; |
25 | char *message; | 25 | char *message; |
26 | size_t length = 0; | 26 | size_t length = 0; |
27 | 27 | ||
28 | login(); | 28 | login(); |
29 | if ( !m_pop3 ) return RecBody(); | 29 | if ( !m_pop3 ) return RecBody(); |
30 | 30 | ||
31 | err = mailpop3_retr( m_pop3, mail.getNumber(), &message, &length ); | 31 | err = mailpop3_retr( m_pop3, mail.getNumber(), &message, &length ); |
32 | if ( err != MAILPOP3_NO_ERROR ) { | 32 | if ( err != MAILPOP3_NO_ERROR ) { |
33 | qDebug( "POP3: error retrieving body with index %i", mail.getNumber() ); | 33 | qDebug( "POP3: error retrieving body with index %i", mail.getNumber() ); |
34 | return RecBody(); | 34 | return RecBody(); |
35 | } | 35 | } |
36 | 36 | ||
37 | return parseBody( message ); | 37 | return parseBody( message ); |
38 | } | 38 | } |
39 | 39 | ||
40 | RecBody POP3wrapper::parseBody( const char *message ) | 40 | RecBody POP3wrapper::parseBody( const char *message ) |
41 | { | 41 | { |
42 | int err = MAILIMF_NO_ERROR; | 42 | int err = MAILIMF_NO_ERROR; |
43 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ | 43 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ |
44 | size_t curTok = 0; | 44 | size_t curTok = 0; |
45 | mailimf_message *result = 0; | 45 | mailimf_message *result = 0; |
46 | RecBody body; | 46 | RecBody body; |
47 | 47 | ||
48 | err = mailimf_message_parse( (char *) message, strlen( message ), &curTok, &result ); | 48 | err = mailimf_message_parse( (char *) message, strlen( message ), &curTok, &result ); |
49 | if ( err != MAILIMF_NO_ERROR ) { | 49 | if ( err != MAILIMF_NO_ERROR ) { |
50 | if (result) mailimf_message_free(result); | 50 | if (result) mailimf_message_free(result); |
51 | return body; | 51 | return body; |
52 | } | 52 | } |
53 | 53 | ||
54 | if ( result && result->msg_body && result->msg_body->bd_text ) { | 54 | if ( result && result->msg_body && result->msg_body->bd_text ) { |
55 | qDebug( "POP3: bodytext found" ); | 55 | qDebug( "POP3: bodytext found" ); |
56 | // when curTok isn't set to 0 this line will fault! 'cause upper line faults! | 56 | // when curTok isn't set to 0 this line will fault! 'cause upper line faults! |
57 | body.setBodytext( QString( result->msg_body->bd_text ) ); | 57 | body.setBodytext( QString( result->msg_body->bd_text ) ); |
58 | } | 58 | } |
59 | if (result) mailimf_message_free(result); | 59 | if (result) mailimf_message_free(result); |
60 | return body; | 60 | return body; |
61 | } | 61 | } |
62 | 62 | ||
63 | void POP3wrapper::listMessages(const QString &, QList<RecMail> &target ) | 63 | void POP3wrapper::listMessages(const QString &, QList<RecMail> &target ) |
64 | { | 64 | { |
65 | int err = MAILPOP3_NO_ERROR; | 65 | int err = MAILPOP3_NO_ERROR; |
66 | char * header = 0; | 66 | char * header = 0; |
67 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ | 67 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ |
68 | size_t length = 0; | 68 | size_t length = 0; |
69 | carray * messages = 0; | 69 | carray * messages = 0; |
70 | 70 | ||
71 | login(); | 71 | login(); |
72 | if (!m_pop3) return; | 72 | if (!m_pop3) return; |
73 | mailpop3_list( m_pop3, &messages ); | 73 | mailpop3_list( m_pop3, &messages ); |
74 | 74 | ||
75 | for (unsigned int i = 0; i < carray_count(messages);++i) { | 75 | for (unsigned int i = 0; i < carray_count(messages);++i) { |
76 | mailpop3_msg_info *info; | 76 | mailpop3_msg_info *info; |
77 | err = mailpop3_get_msg_info(m_pop3,i+1,&info); | 77 | err = mailpop3_get_msg_info(m_pop3,i+1,&info); |
78 | if (info->msg_deleted) | ||
79 | continue; | ||
78 | err = mailpop3_header( m_pop3, info->msg_index, &header, &length ); | 80 | err = mailpop3_header( m_pop3, info->msg_index, &header, &length ); |
79 | if ( err != MAILPOP3_NO_ERROR ) { | 81 | if ( err != MAILPOP3_NO_ERROR ) { |
80 | qDebug( "POP3: error retrieving header msgid: %i", info->msg_index ); | 82 | qDebug( "POP3: error retrieving header msgid: %i", info->msg_index ); |
81 | free(header); | 83 | free(header); |
82 | return; | 84 | return; |
83 | } | 85 | } |
84 | RecMail *mail = parseHeader( header ); | 86 | RecMail *mail = parseHeader( header ); |
85 | mail->setNumber( info->msg_index ); | 87 | mail->setNumber( info->msg_index ); |
86 | mail->setWrapper(this); | 88 | mail->setWrapper(this); |
87 | mail->setMsgsize(info->msg_size); | 89 | mail->setMsgsize(info->msg_size); |
88 | target.append( mail ); | 90 | target.append( mail ); |
89 | free(header); | 91 | free(header); |
90 | } | 92 | } |
91 | } | 93 | } |
92 | 94 | ||
93 | RecMail *POP3wrapper::parseHeader( const char *header ) | 95 | RecMail *POP3wrapper::parseHeader( const char *header ) |
94 | { | 96 | { |
95 | int err = MAILIMF_NO_ERROR; | 97 | int err = MAILIMF_NO_ERROR; |
96 | size_t curTok = 0; | 98 | size_t curTok = 0; |
97 | RecMail *mail = new RecMail(); | 99 | RecMail *mail = new RecMail(); |
98 | mailimf_fields *fields; | 100 | mailimf_fields *fields; |
99 | mailimf_references * refs; | 101 | mailimf_references * refs; |
100 | mailimf_keywords*keys; | 102 | mailimf_keywords*keys; |
101 | QString status; | 103 | QString status; |
102 | QString value; | 104 | QString value; |
103 | QBitArray mFlags(7); | 105 | QBitArray mFlags(7); |
104 | 106 | ||
105 | err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields ); | 107 | err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields ); |
106 | for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) { | 108 | for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) { |
107 | mailimf_field *field = (mailimf_field *) current->data; | 109 | mailimf_field *field = (mailimf_field *) current->data; |
108 | switch ( field->fld_type ) { | 110 | switch ( field->fld_type ) { |
109 | case MAILIMF_FIELD_FROM: | 111 | case MAILIMF_FIELD_FROM: |
110 | mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) ); | 112 | mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) ); |
111 | break; | 113 | break; |
112 | case MAILIMF_FIELD_TO: | 114 | case MAILIMF_FIELD_TO: |
113 | mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) ); | 115 | mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) ); |
114 | break; | 116 | break; |
115 | case MAILIMF_FIELD_CC: | 117 | case MAILIMF_FIELD_CC: |
116 | mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) ); | 118 | mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) ); |
117 | break; | 119 | break; |
118 | case MAILIMF_FIELD_BCC: | 120 | case MAILIMF_FIELD_BCC: |
119 | mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) ); | 121 | mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) ); |
120 | break; | 122 | break; |
121 | case MAILIMF_FIELD_SUBJECT: | 123 | case MAILIMF_FIELD_SUBJECT: |
122 | mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) ); | 124 | mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) ); |
123 | break; | 125 | break; |
124 | case MAILIMF_FIELD_ORIG_DATE: | 126 | case MAILIMF_FIELD_ORIG_DATE: |
125 | mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) ); | 127 | mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) ); |
126 | break; | 128 | break; |
127 | case MAILIMF_FIELD_MESSAGE_ID: | 129 | case MAILIMF_FIELD_MESSAGE_ID: |
128 | mail->setMsgid(QString(field->fld_data.fld_message_id->mid_value)); | 130 | mail->setMsgid(QString(field->fld_data.fld_message_id->mid_value)); |
129 | break; | 131 | break; |
130 | case MAILIMF_FIELD_REFERENCES: | 132 | case MAILIMF_FIELD_REFERENCES: |
131 | refs = field->fld_data.fld_references; | 133 | refs = field->fld_data.fld_references; |
132 | if (refs && refs->mid_list && clist_count(refs->mid_list)) { | 134 | if (refs && refs->mid_list && clist_count(refs->mid_list)) { |
133 | char * text = (char*)refs->mid_list->first->data; | 135 | char * text = (char*)refs->mid_list->first->data; |
134 | mail->setReplyto(QString(text)); | 136 | mail->setReplyto(QString(text)); |
135 | } | 137 | } |
136 | break; | 138 | break; |
137 | case MAILIMF_FIELD_KEYWORDS: | 139 | case MAILIMF_FIELD_KEYWORDS: |
138 | keys = field->fld_data.fld_keywords; | 140 | keys = field->fld_data.fld_keywords; |
139 | for (clistcell*cur = clist_begin(keys->kw_list);cur!=0;cur=clist_next(cur)) { | 141 | for (clistcell*cur = clist_begin(keys->kw_list);cur!=0;cur=clist_next(cur)) { |
140 | qDebug("Keyword: %s",(char*)cur->data); | 142 | qDebug("Keyword: %s",(char*)cur->data); |
141 | } | 143 | } |
142 | break; | 144 | break; |
143 | case MAILIMF_FIELD_OPTIONAL_FIELD: | 145 | case MAILIMF_FIELD_OPTIONAL_FIELD: |
144 | status = field->fld_data.fld_optional_field->fld_name; | 146 | status = field->fld_data.fld_optional_field->fld_name; |
145 | value = field->fld_data.fld_optional_field->fld_value; | 147 | value = field->fld_data.fld_optional_field->fld_value; |
146 | if (status.lower()=="status") { | 148 | if (status.lower()=="status") { |
147 | if (value.lower()=="ro") { | 149 | if (value.lower()=="ro") { |
148 | mFlags.setBit(FLAG_SEEN); | 150 | mFlags.setBit(FLAG_SEEN); |
149 | } | 151 | } |
150 | } else if (status.lower()=="x-status") { | 152 | } else if (status.lower()=="x-status") { |
151 | qDebug("X-Status: %s",value.latin1()); | 153 | qDebug("X-Status: %s",value.latin1()); |
152 | if (value.lower()=="a") { | 154 | if (value.lower()=="a") { |
153 | 155 | ||
154 | mFlags.setBit(FLAG_ANSWERED); | 156 | mFlags.setBit(FLAG_ANSWERED); |
155 | } | 157 | } |
156 | } else { | 158 | } else { |
157 | // qDebug("Optionales feld: %s -> %s)",field->fld_data.fld_optional_field->fld_name, | 159 | // qDebug("Optionales feld: %s -> %s)",field->fld_data.fld_optional_field->fld_name, |
158 | // field->fld_data.fld_optional_field->fld_value); | 160 | // field->fld_data.fld_optional_field->fld_value); |
159 | } | 161 | } |
160 | break; | 162 | break; |
161 | default: | 163 | default: |
162 | qDebug("Non parsed field"); | 164 | qDebug("Non parsed field"); |
163 | break; | 165 | break; |
164 | } | 166 | } |
165 | } | 167 | } |
166 | if (fields) mailimf_fields_free(fields); | 168 | if (fields) mailimf_fields_free(fields); |
167 | mail->setFlags(mFlags); | 169 | mail->setFlags(mFlags); |
168 | return mail; | 170 | return mail; |
169 | } | 171 | } |
170 | 172 | ||
171 | QString POP3wrapper::parseDateTime( mailimf_date_time *date ) | 173 | QString POP3wrapper::parseDateTime( mailimf_date_time *date ) |
172 | { | 174 | { |
173 | char tmp[23]; | 175 | char tmp[23]; |
@@ -224,123 +226,124 @@ QString POP3wrapper::parseGroup( mailimf_group *group ) | |||
224 | return result; | 226 | return result; |
225 | } | 227 | } |
226 | 228 | ||
227 | QString POP3wrapper::parseMailbox( mailimf_mailbox *box ) | 229 | QString POP3wrapper::parseMailbox( mailimf_mailbox *box ) |
228 | { | 230 | { |
229 | QString result( "" ); | 231 | QString result( "" ); |
230 | 232 | ||
231 | if ( box->mb_display_name == NULL ) { | 233 | if ( box->mb_display_name == NULL ) { |
232 | result.append( box->mb_addr_spec ); | 234 | result.append( box->mb_addr_spec ); |
233 | } else { | 235 | } else { |
234 | result.append( box->mb_display_name ); | 236 | result.append( box->mb_display_name ); |
235 | result.append( " <" ); | 237 | result.append( " <" ); |
236 | result.append( box->mb_addr_spec ); | 238 | result.append( box->mb_addr_spec ); |
237 | result.append( ">" ); | 239 | result.append( ">" ); |
238 | } | 240 | } |
239 | 241 | ||
240 | return result; | 242 | return result; |
241 | } | 243 | } |
242 | 244 | ||
243 | QString POP3wrapper::parseMailboxList( mailimf_mailbox_list *list ) | 245 | QString POP3wrapper::parseMailboxList( mailimf_mailbox_list *list ) |
244 | { | 246 | { |
245 | QString result( "" ); | 247 | QString result( "" ); |
246 | 248 | ||
247 | bool first = true; | 249 | bool first = true; |
248 | for ( clistiter *current = clist_begin( list->mb_list ); current != NULL; current = current->next ) { | 250 | for ( clistiter *current = clist_begin( list->mb_list ); current != NULL; current = current->next ) { |
249 | mailimf_mailbox *box = (mailimf_mailbox *) current->data; | 251 | mailimf_mailbox *box = (mailimf_mailbox *) current->data; |
250 | 252 | ||
251 | if ( !first ) { | 253 | if ( !first ) { |
252 | result.append( "," ); | 254 | result.append( "," ); |
253 | } else { | 255 | } else { |
254 | first = false; | 256 | first = false; |
255 | } | 257 | } |
256 | 258 | ||
257 | result.append( parseMailbox( box ) ); | 259 | result.append( parseMailbox( box ) ); |
258 | } | 260 | } |
259 | 261 | ||
260 | return result; | 262 | return result; |
261 | } | 263 | } |
262 | 264 | ||
263 | void POP3wrapper::login() | 265 | void POP3wrapper::login() |
264 | { | 266 | { |
265 | /* we'll hold the line */ | 267 | /* we'll hold the line */ |
266 | if ( m_pop3 != NULL ) return; | 268 | if ( m_pop3 != NULL ) return; |
267 | 269 | ||
268 | const char *server, *user, *pass; | 270 | const char *server, *user, *pass; |
269 | uint16_t port; | 271 | uint16_t port; |
270 | int err = MAILPOP3_NO_ERROR; | 272 | int err = MAILPOP3_NO_ERROR; |
271 | 273 | ||
272 | server = account->getServer().latin1(); | 274 | server = account->getServer().latin1(); |
273 | port = account->getPort().toUInt(); | 275 | port = account->getPort().toUInt(); |
274 | user = account->getUser().latin1(); | 276 | user = account->getUser().latin1(); |
275 | pass = account->getPassword().latin1(); | 277 | pass = account->getPassword().latin1(); |
276 | 278 | ||
277 | m_pop3 = mailpop3_new( 200, &pop3_progress ); | 279 | m_pop3 = mailpop3_new( 200, &pop3_progress ); |
278 | 280 | ||
279 | // connect | 281 | // connect |
280 | if (account->getSSL()) { | 282 | if (account->getSSL()) { |
281 | err = mailpop3_ssl_connect( m_pop3, (char*)server, port ); | 283 | err = mailpop3_ssl_connect( m_pop3, (char*)server, port ); |
282 | } else { | 284 | } else { |
283 | err = mailpop3_socket_connect( m_pop3, (char*)server, port ); | 285 | err = mailpop3_socket_connect( m_pop3, (char*)server, port ); |
284 | } | 286 | } |
285 | 287 | ||
286 | if ( err != MAILPOP3_NO_ERROR ) { | 288 | if ( err != MAILPOP3_NO_ERROR ) { |
287 | qDebug( "pop3: error connecting to %s\n reason: %s", server, | 289 | qDebug( "pop3: error connecting to %s\n reason: %s", server, |
288 | m_pop3->pop3_response ); | 290 | m_pop3->pop3_response ); |
289 | mailpop3_free( m_pop3 ); | 291 | mailpop3_free( m_pop3 ); |
290 | m_pop3 = NULL; | 292 | m_pop3 = NULL; |
291 | return; | 293 | return; |
292 | } | 294 | } |
293 | qDebug( "POP3: connected!" ); | 295 | qDebug( "POP3: connected!" ); |
294 | 296 | ||
295 | // login | 297 | // login |
296 | // TODO: decide if apop or plain login should be used | 298 | // TODO: decide if apop or plain login should be used |
297 | err = mailpop3_login( m_pop3, (char *) user, (char *) pass ); | 299 | err = mailpop3_login( m_pop3, (char *) user, (char *) pass ); |
298 | if ( err != MAILPOP3_NO_ERROR ) { | 300 | if ( err != MAILPOP3_NO_ERROR ) { |
299 | qDebug( "pop3: error logging in: %s", m_pop3->pop3_response ); | 301 | qDebug( "pop3: error logging in: %s", m_pop3->pop3_response ); |
300 | logout(); | 302 | logout(); |
301 | return; | 303 | return; |
302 | } | 304 | } |
303 | 305 | ||
304 | qDebug( "POP3: logged in!" ); | 306 | qDebug( "POP3: logged in!" ); |
305 | } | 307 | } |
306 | 308 | ||
307 | void POP3wrapper::logout() | 309 | void POP3wrapper::logout() |
308 | { | 310 | { |
309 | int err = MAILPOP3_NO_ERROR; | 311 | int err = MAILPOP3_NO_ERROR; |
310 | if ( m_pop3 == NULL ) return; | 312 | if ( m_pop3 == NULL ) return; |
311 | err = mailpop3_quit( m_pop3 ); | 313 | err = mailpop3_quit( m_pop3 ); |
312 | mailpop3_free( m_pop3 ); | 314 | mailpop3_free( m_pop3 ); |
313 | m_pop3 = NULL; | 315 | m_pop3 = NULL; |
314 | } | 316 | } |
315 | 317 | ||
316 | 318 | ||
317 | QList<Folder>* POP3wrapper::listFolders() | 319 | QList<Folder>* POP3wrapper::listFolders() |
318 | { | 320 | { |
319 | /* TODO: integrate MH directories | 321 | /* TODO: integrate MH directories |
320 | but not vor version 0.1 ;) | 322 | but not before version 0.1 ;) |
321 | */ | 323 | */ |
322 | QList<Folder> * folders = new QList<Folder>(); | 324 | QList<Folder> * folders = new QList<Folder>(); |
323 | folders->setAutoDelete( false ); | 325 | folders->setAutoDelete( false ); |
324 | Folder*inb=new Folder("INBOX"); | 326 | Folder*inb=new Folder("INBOX"); |
325 | folders->append(inb); | 327 | folders->append(inb); |
326 | return folders; | 328 | return folders; |
327 | } | 329 | } |
328 | 330 | ||
329 | QString POP3wrapper::fetchPart(const RecMail&,const RecPart&) | 331 | QString POP3wrapper::fetchPart(const RecMail&,const RecPart&) |
330 | { | 332 | { |
331 | return ""; | 333 | return ""; |
332 | } | 334 | } |
333 | 335 | ||
334 | void POP3wrapper::deleteMail(const RecMail&mail) | 336 | void POP3wrapper::deleteMail(const RecMail&mail) |
335 | { | 337 | { |
336 | login(); | 338 | login(); |
337 | if (!m_pop3) return; | 339 | if (!m_pop3) return; |
338 | int err = mailpop3_dele(m_pop3,mail.getNumber()); | 340 | int err = mailpop3_dele(m_pop3,mail.getNumber()); |
339 | if (err != MAILPOP3_NO_ERROR) { | 341 | if (err != MAILPOP3_NO_ERROR) { |
340 | qDebug("error deleting mail"); | 342 | qDebug("error deleting mail"); |
341 | } | 343 | } |
342 | } | 344 | } |
343 | 345 | ||
344 | void POP3wrapper::answeredMail(const RecMail&) | 346 | void POP3wrapper::answeredMail(const RecMail&) |
345 | { | 347 | { |
346 | } | 348 | } |
349 | |||
diff --git a/noncore/net/mail/pop3wrapper.cpp b/noncore/net/mail/pop3wrapper.cpp index b4a8f4b..a33a16b 100644 --- a/noncore/net/mail/pop3wrapper.cpp +++ b/noncore/net/mail/pop3wrapper.cpp | |||
@@ -1,173 +1,175 @@ | |||
1 | #include <stdlib.h> | 1 | #include <stdlib.h> |
2 | #include "pop3wrapper.h" | 2 | #include "pop3wrapper.h" |
3 | #include "mailtypes.h" | 3 | #include "mailtypes.h" |
4 | #include <libetpan/mailpop3.h> | 4 | #include <libetpan/mailpop3.h> |
5 | 5 | ||
6 | POP3wrapper::POP3wrapper( POP3account *a ) | 6 | POP3wrapper::POP3wrapper( POP3account *a ) |
7 | { | 7 | { |
8 | account = a; | 8 | account = a; |
9 | m_pop3 = NULL; | 9 | m_pop3 = NULL; |
10 | } | 10 | } |
11 | 11 | ||
12 | POP3wrapper::~POP3wrapper() | 12 | POP3wrapper::~POP3wrapper() |
13 | { | 13 | { |
14 | logout(); | 14 | logout(); |
15 | } | 15 | } |
16 | 16 | ||
17 | void POP3wrapper::pop3_progress( size_t current, size_t maximum ) | 17 | void POP3wrapper::pop3_progress( size_t current, size_t maximum ) |
18 | { | 18 | { |
19 | //qDebug( "POP3: %i of %i", current, maximum ); | 19 | //qDebug( "POP3: %i of %i", current, maximum ); |
20 | } | 20 | } |
21 | 21 | ||
22 | RecBody POP3wrapper::fetchBody( const RecMail &mail ) | 22 | RecBody POP3wrapper::fetchBody( const RecMail &mail ) |
23 | { | 23 | { |
24 | int err = MAILPOP3_NO_ERROR; | 24 | int err = MAILPOP3_NO_ERROR; |
25 | char *message; | 25 | char *message; |
26 | size_t length = 0; | 26 | size_t length = 0; |
27 | 27 | ||
28 | login(); | 28 | login(); |
29 | if ( !m_pop3 ) return RecBody(); | 29 | if ( !m_pop3 ) return RecBody(); |
30 | 30 | ||
31 | err = mailpop3_retr( m_pop3, mail.getNumber(), &message, &length ); | 31 | err = mailpop3_retr( m_pop3, mail.getNumber(), &message, &length ); |
32 | if ( err != MAILPOP3_NO_ERROR ) { | 32 | if ( err != MAILPOP3_NO_ERROR ) { |
33 | qDebug( "POP3: error retrieving body with index %i", mail.getNumber() ); | 33 | qDebug( "POP3: error retrieving body with index %i", mail.getNumber() ); |
34 | return RecBody(); | 34 | return RecBody(); |
35 | } | 35 | } |
36 | 36 | ||
37 | return parseBody( message ); | 37 | return parseBody( message ); |
38 | } | 38 | } |
39 | 39 | ||
40 | RecBody POP3wrapper::parseBody( const char *message ) | 40 | RecBody POP3wrapper::parseBody( const char *message ) |
41 | { | 41 | { |
42 | int err = MAILIMF_NO_ERROR; | 42 | int err = MAILIMF_NO_ERROR; |
43 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ | 43 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ |
44 | size_t curTok = 0; | 44 | size_t curTok = 0; |
45 | mailimf_message *result = 0; | 45 | mailimf_message *result = 0; |
46 | RecBody body; | 46 | RecBody body; |
47 | 47 | ||
48 | err = mailimf_message_parse( (char *) message, strlen( message ), &curTok, &result ); | 48 | err = mailimf_message_parse( (char *) message, strlen( message ), &curTok, &result ); |
49 | if ( err != MAILIMF_NO_ERROR ) { | 49 | if ( err != MAILIMF_NO_ERROR ) { |
50 | if (result) mailimf_message_free(result); | 50 | if (result) mailimf_message_free(result); |
51 | return body; | 51 | return body; |
52 | } | 52 | } |
53 | 53 | ||
54 | if ( result && result->msg_body && result->msg_body->bd_text ) { | 54 | if ( result && result->msg_body && result->msg_body->bd_text ) { |
55 | qDebug( "POP3: bodytext found" ); | 55 | qDebug( "POP3: bodytext found" ); |
56 | // when curTok isn't set to 0 this line will fault! 'cause upper line faults! | 56 | // when curTok isn't set to 0 this line will fault! 'cause upper line faults! |
57 | body.setBodytext( QString( result->msg_body->bd_text ) ); | 57 | body.setBodytext( QString( result->msg_body->bd_text ) ); |
58 | } | 58 | } |
59 | if (result) mailimf_message_free(result); | 59 | if (result) mailimf_message_free(result); |
60 | return body; | 60 | return body; |
61 | } | 61 | } |
62 | 62 | ||
63 | void POP3wrapper::listMessages(const QString &, QList<RecMail> &target ) | 63 | void POP3wrapper::listMessages(const QString &, QList<RecMail> &target ) |
64 | { | 64 | { |
65 | int err = MAILPOP3_NO_ERROR; | 65 | int err = MAILPOP3_NO_ERROR; |
66 | char * header = 0; | 66 | char * header = 0; |
67 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ | 67 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ |
68 | size_t length = 0; | 68 | size_t length = 0; |
69 | carray * messages = 0; | 69 | carray * messages = 0; |
70 | 70 | ||
71 | login(); | 71 | login(); |
72 | if (!m_pop3) return; | 72 | if (!m_pop3) return; |
73 | mailpop3_list( m_pop3, &messages ); | 73 | mailpop3_list( m_pop3, &messages ); |
74 | 74 | ||
75 | for (unsigned int i = 0; i < carray_count(messages);++i) { | 75 | for (unsigned int i = 0; i < carray_count(messages);++i) { |
76 | mailpop3_msg_info *info; | 76 | mailpop3_msg_info *info; |
77 | err = mailpop3_get_msg_info(m_pop3,i+1,&info); | 77 | err = mailpop3_get_msg_info(m_pop3,i+1,&info); |
78 | if (info->msg_deleted) | ||
79 | continue; | ||
78 | err = mailpop3_header( m_pop3, info->msg_index, &header, &length ); | 80 | err = mailpop3_header( m_pop3, info->msg_index, &header, &length ); |
79 | if ( err != MAILPOP3_NO_ERROR ) { | 81 | if ( err != MAILPOP3_NO_ERROR ) { |
80 | qDebug( "POP3: error retrieving header msgid: %i", info->msg_index ); | 82 | qDebug( "POP3: error retrieving header msgid: %i", info->msg_index ); |
81 | free(header); | 83 | free(header); |
82 | return; | 84 | return; |
83 | } | 85 | } |
84 | RecMail *mail = parseHeader( header ); | 86 | RecMail *mail = parseHeader( header ); |
85 | mail->setNumber( info->msg_index ); | 87 | mail->setNumber( info->msg_index ); |
86 | mail->setWrapper(this); | 88 | mail->setWrapper(this); |
87 | mail->setMsgsize(info->msg_size); | 89 | mail->setMsgsize(info->msg_size); |
88 | target.append( mail ); | 90 | target.append( mail ); |
89 | free(header); | 91 | free(header); |
90 | } | 92 | } |
91 | } | 93 | } |
92 | 94 | ||
93 | RecMail *POP3wrapper::parseHeader( const char *header ) | 95 | RecMail *POP3wrapper::parseHeader( const char *header ) |
94 | { | 96 | { |
95 | int err = MAILIMF_NO_ERROR; | 97 | int err = MAILIMF_NO_ERROR; |
96 | size_t curTok = 0; | 98 | size_t curTok = 0; |
97 | RecMail *mail = new RecMail(); | 99 | RecMail *mail = new RecMail(); |
98 | mailimf_fields *fields; | 100 | mailimf_fields *fields; |
99 | mailimf_references * refs; | 101 | mailimf_references * refs; |
100 | mailimf_keywords*keys; | 102 | mailimf_keywords*keys; |
101 | QString status; | 103 | QString status; |
102 | QString value; | 104 | QString value; |
103 | QBitArray mFlags(7); | 105 | QBitArray mFlags(7); |
104 | 106 | ||
105 | err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields ); | 107 | err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields ); |
106 | for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) { | 108 | for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) { |
107 | mailimf_field *field = (mailimf_field *) current->data; | 109 | mailimf_field *field = (mailimf_field *) current->data; |
108 | switch ( field->fld_type ) { | 110 | switch ( field->fld_type ) { |
109 | case MAILIMF_FIELD_FROM: | 111 | case MAILIMF_FIELD_FROM: |
110 | mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) ); | 112 | mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) ); |
111 | break; | 113 | break; |
112 | case MAILIMF_FIELD_TO: | 114 | case MAILIMF_FIELD_TO: |
113 | mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) ); | 115 | mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) ); |
114 | break; | 116 | break; |
115 | case MAILIMF_FIELD_CC: | 117 | case MAILIMF_FIELD_CC: |
116 | mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) ); | 118 | mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) ); |
117 | break; | 119 | break; |
118 | case MAILIMF_FIELD_BCC: | 120 | case MAILIMF_FIELD_BCC: |
119 | mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) ); | 121 | mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) ); |
120 | break; | 122 | break; |
121 | case MAILIMF_FIELD_SUBJECT: | 123 | case MAILIMF_FIELD_SUBJECT: |
122 | mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) ); | 124 | mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) ); |
123 | break; | 125 | break; |
124 | case MAILIMF_FIELD_ORIG_DATE: | 126 | case MAILIMF_FIELD_ORIG_DATE: |
125 | mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) ); | 127 | mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) ); |
126 | break; | 128 | break; |
127 | case MAILIMF_FIELD_MESSAGE_ID: | 129 | case MAILIMF_FIELD_MESSAGE_ID: |
128 | mail->setMsgid(QString(field->fld_data.fld_message_id->mid_value)); | 130 | mail->setMsgid(QString(field->fld_data.fld_message_id->mid_value)); |
129 | break; | 131 | break; |
130 | case MAILIMF_FIELD_REFERENCES: | 132 | case MAILIMF_FIELD_REFERENCES: |
131 | refs = field->fld_data.fld_references; | 133 | refs = field->fld_data.fld_references; |
132 | if (refs && refs->mid_list && clist_count(refs->mid_list)) { | 134 | if (refs && refs->mid_list && clist_count(refs->mid_list)) { |
133 | char * text = (char*)refs->mid_list->first->data; | 135 | char * text = (char*)refs->mid_list->first->data; |
134 | mail->setReplyto(QString(text)); | 136 | mail->setReplyto(QString(text)); |
135 | } | 137 | } |
136 | break; | 138 | break; |
137 | case MAILIMF_FIELD_KEYWORDS: | 139 | case MAILIMF_FIELD_KEYWORDS: |
138 | keys = field->fld_data.fld_keywords; | 140 | keys = field->fld_data.fld_keywords; |
139 | for (clistcell*cur = clist_begin(keys->kw_list);cur!=0;cur=clist_next(cur)) { | 141 | for (clistcell*cur = clist_begin(keys->kw_list);cur!=0;cur=clist_next(cur)) { |
140 | qDebug("Keyword: %s",(char*)cur->data); | 142 | qDebug("Keyword: %s",(char*)cur->data); |
141 | } | 143 | } |
142 | break; | 144 | break; |
143 | case MAILIMF_FIELD_OPTIONAL_FIELD: | 145 | case MAILIMF_FIELD_OPTIONAL_FIELD: |
144 | status = field->fld_data.fld_optional_field->fld_name; | 146 | status = field->fld_data.fld_optional_field->fld_name; |
145 | value = field->fld_data.fld_optional_field->fld_value; | 147 | value = field->fld_data.fld_optional_field->fld_value; |
146 | if (status.lower()=="status") { | 148 | if (status.lower()=="status") { |
147 | if (value.lower()=="ro") { | 149 | if (value.lower()=="ro") { |
148 | mFlags.setBit(FLAG_SEEN); | 150 | mFlags.setBit(FLAG_SEEN); |
149 | } | 151 | } |
150 | } else if (status.lower()=="x-status") { | 152 | } else if (status.lower()=="x-status") { |
151 | qDebug("X-Status: %s",value.latin1()); | 153 | qDebug("X-Status: %s",value.latin1()); |
152 | if (value.lower()=="a") { | 154 | if (value.lower()=="a") { |
153 | 155 | ||
154 | mFlags.setBit(FLAG_ANSWERED); | 156 | mFlags.setBit(FLAG_ANSWERED); |
155 | } | 157 | } |
156 | } else { | 158 | } else { |
157 | // qDebug("Optionales feld: %s -> %s)",field->fld_data.fld_optional_field->fld_name, | 159 | // qDebug("Optionales feld: %s -> %s)",field->fld_data.fld_optional_field->fld_name, |
158 | // field->fld_data.fld_optional_field->fld_value); | 160 | // field->fld_data.fld_optional_field->fld_value); |
159 | } | 161 | } |
160 | break; | 162 | break; |
161 | default: | 163 | default: |
162 | qDebug("Non parsed field"); | 164 | qDebug("Non parsed field"); |
163 | break; | 165 | break; |
164 | } | 166 | } |
165 | } | 167 | } |
166 | if (fields) mailimf_fields_free(fields); | 168 | if (fields) mailimf_fields_free(fields); |
167 | mail->setFlags(mFlags); | 169 | mail->setFlags(mFlags); |
168 | return mail; | 170 | return mail; |
169 | } | 171 | } |
170 | 172 | ||
171 | QString POP3wrapper::parseDateTime( mailimf_date_time *date ) | 173 | QString POP3wrapper::parseDateTime( mailimf_date_time *date ) |
172 | { | 174 | { |
173 | char tmp[23]; | 175 | char tmp[23]; |
@@ -224,123 +226,124 @@ QString POP3wrapper::parseGroup( mailimf_group *group ) | |||
224 | return result; | 226 | return result; |
225 | } | 227 | } |
226 | 228 | ||
227 | QString POP3wrapper::parseMailbox( mailimf_mailbox *box ) | 229 | QString POP3wrapper::parseMailbox( mailimf_mailbox *box ) |
228 | { | 230 | { |
229 | QString result( "" ); | 231 | QString result( "" ); |
230 | 232 | ||
231 | if ( box->mb_display_name == NULL ) { | 233 | if ( box->mb_display_name == NULL ) { |
232 | result.append( box->mb_addr_spec ); | 234 | result.append( box->mb_addr_spec ); |
233 | } else { | 235 | } else { |
234 | result.append( box->mb_display_name ); | 236 | result.append( box->mb_display_name ); |
235 | result.append( " <" ); | 237 | result.append( " <" ); |
236 | result.append( box->mb_addr_spec ); | 238 | result.append( box->mb_addr_spec ); |
237 | result.append( ">" ); | 239 | result.append( ">" ); |
238 | } | 240 | } |
239 | 241 | ||
240 | return result; | 242 | return result; |
241 | } | 243 | } |
242 | 244 | ||
243 | QString POP3wrapper::parseMailboxList( mailimf_mailbox_list *list ) | 245 | QString POP3wrapper::parseMailboxList( mailimf_mailbox_list *list ) |
244 | { | 246 | { |
245 | QString result( "" ); | 247 | QString result( "" ); |
246 | 248 | ||
247 | bool first = true; | 249 | bool first = true; |
248 | for ( clistiter *current = clist_begin( list->mb_list ); current != NULL; current = current->next ) { | 250 | for ( clistiter *current = clist_begin( list->mb_list ); current != NULL; current = current->next ) { |
249 | mailimf_mailbox *box = (mailimf_mailbox *) current->data; | 251 | mailimf_mailbox *box = (mailimf_mailbox *) current->data; |
250 | 252 | ||
251 | if ( !first ) { | 253 | if ( !first ) { |
252 | result.append( "," ); | 254 | result.append( "," ); |
253 | } else { | 255 | } else { |
254 | first = false; | 256 | first = false; |
255 | } | 257 | } |
256 | 258 | ||
257 | result.append( parseMailbox( box ) ); | 259 | result.append( parseMailbox( box ) ); |
258 | } | 260 | } |
259 | 261 | ||
260 | return result; | 262 | return result; |
261 | } | 263 | } |
262 | 264 | ||
263 | void POP3wrapper::login() | 265 | void POP3wrapper::login() |
264 | { | 266 | { |
265 | /* we'll hold the line */ | 267 | /* we'll hold the line */ |
266 | if ( m_pop3 != NULL ) return; | 268 | if ( m_pop3 != NULL ) return; |
267 | 269 | ||
268 | const char *server, *user, *pass; | 270 | const char *server, *user, *pass; |
269 | uint16_t port; | 271 | uint16_t port; |
270 | int err = MAILPOP3_NO_ERROR; | 272 | int err = MAILPOP3_NO_ERROR; |
271 | 273 | ||
272 | server = account->getServer().latin1(); | 274 | server = account->getServer().latin1(); |
273 | port = account->getPort().toUInt(); | 275 | port = account->getPort().toUInt(); |
274 | user = account->getUser().latin1(); | 276 | user = account->getUser().latin1(); |
275 | pass = account->getPassword().latin1(); | 277 | pass = account->getPassword().latin1(); |
276 | 278 | ||
277 | m_pop3 = mailpop3_new( 200, &pop3_progress ); | 279 | m_pop3 = mailpop3_new( 200, &pop3_progress ); |
278 | 280 | ||
279 | // connect | 281 | // connect |
280 | if (account->getSSL()) { | 282 | if (account->getSSL()) { |
281 | err = mailpop3_ssl_connect( m_pop3, (char*)server, port ); | 283 | err = mailpop3_ssl_connect( m_pop3, (char*)server, port ); |
282 | } else { | 284 | } else { |
283 | err = mailpop3_socket_connect( m_pop3, (char*)server, port ); | 285 | err = mailpop3_socket_connect( m_pop3, (char*)server, port ); |
284 | } | 286 | } |
285 | 287 | ||
286 | if ( err != MAILPOP3_NO_ERROR ) { | 288 | if ( err != MAILPOP3_NO_ERROR ) { |
287 | qDebug( "pop3: error connecting to %s\n reason: %s", server, | 289 | qDebug( "pop3: error connecting to %s\n reason: %s", server, |
288 | m_pop3->pop3_response ); | 290 | m_pop3->pop3_response ); |
289 | mailpop3_free( m_pop3 ); | 291 | mailpop3_free( m_pop3 ); |
290 | m_pop3 = NULL; | 292 | m_pop3 = NULL; |
291 | return; | 293 | return; |
292 | } | 294 | } |
293 | qDebug( "POP3: connected!" ); | 295 | qDebug( "POP3: connected!" ); |
294 | 296 | ||
295 | // login | 297 | // login |
296 | // TODO: decide if apop or plain login should be used | 298 | // TODO: decide if apop or plain login should be used |
297 | err = mailpop3_login( m_pop3, (char *) user, (char *) pass ); | 299 | err = mailpop3_login( m_pop3, (char *) user, (char *) pass ); |
298 | if ( err != MAILPOP3_NO_ERROR ) { | 300 | if ( err != MAILPOP3_NO_ERROR ) { |
299 | qDebug( "pop3: error logging in: %s", m_pop3->pop3_response ); | 301 | qDebug( "pop3: error logging in: %s", m_pop3->pop3_response ); |
300 | logout(); | 302 | logout(); |
301 | return; | 303 | return; |
302 | } | 304 | } |
303 | 305 | ||
304 | qDebug( "POP3: logged in!" ); | 306 | qDebug( "POP3: logged in!" ); |
305 | } | 307 | } |
306 | 308 | ||
307 | void POP3wrapper::logout() | 309 | void POP3wrapper::logout() |
308 | { | 310 | { |
309 | int err = MAILPOP3_NO_ERROR; | 311 | int err = MAILPOP3_NO_ERROR; |
310 | if ( m_pop3 == NULL ) return; | 312 | if ( m_pop3 == NULL ) return; |
311 | err = mailpop3_quit( m_pop3 ); | 313 | err = mailpop3_quit( m_pop3 ); |
312 | mailpop3_free( m_pop3 ); | 314 | mailpop3_free( m_pop3 ); |
313 | m_pop3 = NULL; | 315 | m_pop3 = NULL; |
314 | } | 316 | } |
315 | 317 | ||
316 | 318 | ||
317 | QList<Folder>* POP3wrapper::listFolders() | 319 | QList<Folder>* POP3wrapper::listFolders() |
318 | { | 320 | { |
319 | /* TODO: integrate MH directories | 321 | /* TODO: integrate MH directories |
320 | but not vor version 0.1 ;) | 322 | but not before version 0.1 ;) |
321 | */ | 323 | */ |
322 | QList<Folder> * folders = new QList<Folder>(); | 324 | QList<Folder> * folders = new QList<Folder>(); |
323 | folders->setAutoDelete( false ); | 325 | folders->setAutoDelete( false ); |
324 | Folder*inb=new Folder("INBOX"); | 326 | Folder*inb=new Folder("INBOX"); |
325 | folders->append(inb); | 327 | folders->append(inb); |
326 | return folders; | 328 | return folders; |
327 | } | 329 | } |
328 | 330 | ||
329 | QString POP3wrapper::fetchPart(const RecMail&,const RecPart&) | 331 | QString POP3wrapper::fetchPart(const RecMail&,const RecPart&) |
330 | { | 332 | { |
331 | return ""; | 333 | return ""; |
332 | } | 334 | } |
333 | 335 | ||
334 | void POP3wrapper::deleteMail(const RecMail&mail) | 336 | void POP3wrapper::deleteMail(const RecMail&mail) |
335 | { | 337 | { |
336 | login(); | 338 | login(); |
337 | if (!m_pop3) return; | 339 | if (!m_pop3) return; |
338 | int err = mailpop3_dele(m_pop3,mail.getNumber()); | 340 | int err = mailpop3_dele(m_pop3,mail.getNumber()); |
339 | if (err != MAILPOP3_NO_ERROR) { | 341 | if (err != MAILPOP3_NO_ERROR) { |
340 | qDebug("error deleting mail"); | 342 | qDebug("error deleting mail"); |
341 | } | 343 | } |
342 | } | 344 | } |
343 | 345 | ||
344 | void POP3wrapper::answeredMail(const RecMail&) | 346 | void POP3wrapper::answeredMail(const RecMail&) |
345 | { | 347 | { |
346 | } | 348 | } |
349 | |||