summaryrefslogtreecommitdiff
authoralwin <alwin>2003-12-15 15:40:51 (UTC)
committer alwin <alwin>2003-12-15 15:40:51 (UTC)
commit2a4d7a2fcdf76c032a17135bff461bbf4089ad93 (patch) (unidiff)
treef8208fc563fb0b1af674b095d1dc547265f38c98
parent4acec249d48e57c911fc2544d92785b86d5f08d0 (diff)
downloadopie-2a4d7a2fcdf76c032a17135bff461bbf4089ad93.zip
opie-2a4d7a2fcdf76c032a17135bff461bbf4089ad93.tar.gz
opie-2a4d7a2fcdf76c032a17135bff461bbf4089ad93.tar.bz2
delete mail implemented
test for read flags
Diffstat (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/mail/libmailwrapper/pop3wrapper.cpp50
-rw-r--r--noncore/net/mail/pop3wrapper.cpp50
2 files changed, 78 insertions, 22 deletions
diff --git a/noncore/net/mail/libmailwrapper/pop3wrapper.cpp b/noncore/net/mail/libmailwrapper/pop3wrapper.cpp
index 2411399..b4a8f4b 100644
--- a/noncore/net/mail/libmailwrapper/pop3wrapper.cpp
+++ b/noncore/net/mail/libmailwrapper/pop3wrapper.cpp
@@ -1,318 +1,346 @@
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
6POP3wrapper::POP3wrapper( POP3account *a ) 6POP3wrapper::POP3wrapper( POP3account *a )
7{ 7{
8 account = a; 8 account = a;
9 m_pop3 = NULL; 9 m_pop3 = NULL;
10} 10}
11 11
12POP3wrapper::~POP3wrapper() 12POP3wrapper::~POP3wrapper()
13{ 13{
14 logout(); 14 logout();
15} 15}
16 16
17void POP3wrapper::pop3_progress( size_t current, size_t maximum ) 17void 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
22RecBody POP3wrapper::fetchBody( const RecMail &mail ) 22RecBody 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; 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 logout();
35 return RecBody(); 34 return RecBody();
36 } 35 }
37 36
38 logout();
39
40 return parseBody( message ); 37 return parseBody( message );
41} 38}
42 39
43RecBody POP3wrapper::parseBody( const char *message ) 40RecBody POP3wrapper::parseBody( const char *message )
44{ 41{
45 int err = MAILIMF_NO_ERROR; 42 int err = MAILIMF_NO_ERROR;
46 /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ 43 /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */
47 size_t curTok = 0; 44 size_t curTok = 0;
48 mailimf_message *result = 0; 45 mailimf_message *result = 0;
49 RecBody body; 46 RecBody body;
50 47
51 err = mailimf_message_parse( (char *) message, strlen( message ), &curTok, &result ); 48 err = mailimf_message_parse( (char *) message, strlen( message ), &curTok, &result );
52 if ( err != MAILIMF_NO_ERROR ) { 49 if ( err != MAILIMF_NO_ERROR ) {
53 if (result) mailimf_message_free(result); 50 if (result) mailimf_message_free(result);
54 return body; 51 return body;
55 } 52 }
56 53
57 if ( result && result->msg_body && result->msg_body->bd_text ) { 54 if ( result && result->msg_body && result->msg_body->bd_text ) {
58 qDebug( "POP3: bodytext found" ); 55 qDebug( "POP3: bodytext found" );
59 // 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!
60 body.setBodytext( QString( result->msg_body->bd_text ) ); 57 body.setBodytext( QString( result->msg_body->bd_text ) );
61 } 58 }
62 if (result) mailimf_message_free(result); 59 if (result) mailimf_message_free(result);
63 return body; 60 return body;
64} 61}
65 62
66void POP3wrapper::listMessages(const QString &, QList<RecMail> &target ) 63void POP3wrapper::listMessages(const QString &, QList<RecMail> &target )
67{ 64{
68 int err = MAILPOP3_NO_ERROR; 65 int err = MAILPOP3_NO_ERROR;
69 char * header = 0; 66 char * header = 0;
70 /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ 67 /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */
71 size_t length = 0; 68 size_t length = 0;
72 carray * messages = 0; 69 carray * messages = 0;
73 70
74 login(); 71 login();
75 if (!m_pop3) return; 72 if (!m_pop3) return;
76 mailpop3_list( m_pop3, &messages ); 73 mailpop3_list( m_pop3, &messages );
77 74
78 //for ( int i = carray_count( messages ); i > 0; i-- ) {
79 for (unsigned int i = 0; i < carray_count(messages);++i) { 75 for (unsigned int i = 0; i < carray_count(messages);++i) {
80 mailpop3_msg_info *info; 76 mailpop3_msg_info *info;
81 int r = mailpop3_get_msg_info(m_pop3,i+1,&info); 77 err = mailpop3_get_msg_info(m_pop3,i+1,&info);
82 err = mailpop3_header( m_pop3, info->msg_index, &header, &length ); 78 err = mailpop3_header( m_pop3, info->msg_index, &header, &length );
83 if ( err != MAILPOP3_NO_ERROR ) { 79 if ( err != MAILPOP3_NO_ERROR ) {
84 qDebug( "POP3: error retrieving header msgid: %i", info->msg_index ); 80 qDebug( "POP3: error retrieving header msgid: %i", info->msg_index );
85 free(header); 81 free(header);
86 logout();
87 return; 82 return;
88 } 83 }
89 RecMail *mail = parseHeader( header ); 84 RecMail *mail = parseHeader( header );
90 mail->setNumber( info->msg_index ); 85 mail->setNumber( info->msg_index );
91 mail->setWrapper(this); 86 mail->setWrapper(this);
87 mail->setMsgsize(info->msg_size);
92 target.append( mail ); 88 target.append( mail );
93 free(header); 89 free(header);
94 } 90 }
95 logout();
96} 91}
97 92
98RecMail *POP3wrapper::parseHeader( const char *header ) 93RecMail *POP3wrapper::parseHeader( const char *header )
99{ 94{
100 int err = MAILIMF_NO_ERROR; 95 int err = MAILIMF_NO_ERROR;
101 size_t curTok = 0; 96 size_t curTok = 0;
102 RecMail *mail = new RecMail(); 97 RecMail *mail = new RecMail();
103 mailimf_fields *fields; 98 mailimf_fields *fields;
104 mailimf_references * refs; 99 mailimf_references * refs;
105 mailimf_keywords*keys; 100 mailimf_keywords*keys;
101 QString status;
102 QString value;
103 QBitArray mFlags(7);
106 104
107 err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields ); 105 err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields );
108 for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) { 106 for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) {
109 mailimf_field *field = (mailimf_field *) current->data; 107 mailimf_field *field = (mailimf_field *) current->data;
110 switch ( field->fld_type ) { 108 switch ( field->fld_type ) {
111 case MAILIMF_FIELD_FROM: 109 case MAILIMF_FIELD_FROM:
112 mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) ); 110 mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) );
113 break; 111 break;
114 case MAILIMF_FIELD_TO: 112 case MAILIMF_FIELD_TO:
115 mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) ); 113 mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) );
116 break; 114 break;
117 case MAILIMF_FIELD_CC: 115 case MAILIMF_FIELD_CC:
118 mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) ); 116 mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) );
119 break; 117 break;
120 case MAILIMF_FIELD_BCC: 118 case MAILIMF_FIELD_BCC:
121 mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) ); 119 mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) );
122 break; 120 break;
123 case MAILIMF_FIELD_SUBJECT: 121 case MAILIMF_FIELD_SUBJECT:
124 mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) ); 122 mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) );
125 break; 123 break;
126 case MAILIMF_FIELD_ORIG_DATE: 124 case MAILIMF_FIELD_ORIG_DATE:
127 mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) ); 125 mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) );
128 break; 126 break;
129 case MAILIMF_FIELD_MESSAGE_ID: 127 case MAILIMF_FIELD_MESSAGE_ID:
130 mail->setMsgid(QString(field->fld_data.fld_message_id->mid_value)); 128 mail->setMsgid(QString(field->fld_data.fld_message_id->mid_value));
131 break; 129 break;
132 case MAILIMF_FIELD_REFERENCES: 130 case MAILIMF_FIELD_REFERENCES:
133 refs = field->fld_data.fld_references; 131 refs = field->fld_data.fld_references;
134 if (refs && refs->mid_list && clist_count(refs->mid_list)) { 132 if (refs && refs->mid_list && clist_count(refs->mid_list)) {
135 char * text = (char*)refs->mid_list->first->data; 133 char * text = (char*)refs->mid_list->first->data;
136 mail->setReplyto(QString(text)); 134 mail->setReplyto(QString(text));
137 } 135 }
138 break; 136 break;
139 case MAILIMF_FIELD_KEYWORDS: 137 case MAILIMF_FIELD_KEYWORDS:
140 keys = field->fld_data.fld_keywords; 138 keys = field->fld_data.fld_keywords;
141 for (clistcell*cur = clist_begin(keys->kw_list);cur!=0;cur=clist_next(cur)) { 139 for (clistcell*cur = clist_begin(keys->kw_list);cur!=0;cur=clist_next(cur)) {
142 qDebug("Keyword: %s",(char*)cur->data); 140 qDebug("Keyword: %s",(char*)cur->data);
143 } 141 }
144 break; 142 break;
143 case MAILIMF_FIELD_OPTIONAL_FIELD:
144 status = field->fld_data.fld_optional_field->fld_name;
145 value = field->fld_data.fld_optional_field->fld_value;
146 if (status.lower()=="status") {
147 if (value.lower()=="ro") {
148 mFlags.setBit(FLAG_SEEN);
149 }
150 } else if (status.lower()=="x-status") {
151 qDebug("X-Status: %s",value.latin1());
152 if (value.lower()=="a") {
153
154 mFlags.setBit(FLAG_ANSWERED);
155 }
156 } else {
157// qDebug("Optionales feld: %s -> %s)",field->fld_data.fld_optional_field->fld_name,
158// field->fld_data.fld_optional_field->fld_value);
159 }
160 break;
145 default: 161 default:
162 qDebug("Non parsed field");
146 break; 163 break;
147 } 164 }
148 } 165 }
149 if (fields) mailimf_fields_free(fields); 166 if (fields) mailimf_fields_free(fields);
167 mail->setFlags(mFlags);
150 return mail; 168 return mail;
151} 169}
152 170
153QString POP3wrapper::parseDateTime( mailimf_date_time *date ) 171QString POP3wrapper::parseDateTime( mailimf_date_time *date )
154{ 172{
155 char tmp[23]; 173 char tmp[23];
156 174
157 snprintf( tmp, 23, "%02i.%02i.%04i %02i:%02i:%02i %+05i", 175 snprintf( tmp, 23, "%02i.%02i.%04i %02i:%02i:%02i %+05i",
158 date->dt_day, date->dt_month, date->dt_year, date->dt_hour, date->dt_min, date->dt_sec, date->dt_zone ); 176 date->dt_day, date->dt_month, date->dt_year, date->dt_hour, date->dt_min, date->dt_sec, date->dt_zone );
159 177
160 return QString( tmp ); 178 return QString( tmp );
161} 179}
162 180
163QString POP3wrapper::parseAddressList( mailimf_address_list *list ) 181QString POP3wrapper::parseAddressList( mailimf_address_list *list )
164{ 182{
165 QString result( "" ); 183 QString result( "" );
166 184
167 bool first = true; 185 bool first = true;
168 for ( clistiter *current = clist_begin( list->ad_list ); current != NULL; current = current->next ) { 186 for ( clistiter *current = clist_begin( list->ad_list ); current != NULL; current = current->next ) {
169 mailimf_address *addr = (mailimf_address *) current->data; 187 mailimf_address *addr = (mailimf_address *) current->data;
170 188
171 if ( !first ) { 189 if ( !first ) {
172 result.append( "," ); 190 result.append( "," );
173 } else { 191 } else {
174 first = false; 192 first = false;
175 } 193 }
176 194
177 switch ( addr->ad_type ) { 195 switch ( addr->ad_type ) {
178 case MAILIMF_ADDRESS_MAILBOX: 196 case MAILIMF_ADDRESS_MAILBOX:
179 result.append( parseMailbox( addr->ad_data.ad_mailbox ) ); 197 result.append( parseMailbox( addr->ad_data.ad_mailbox ) );
180 break; 198 break;
181 case MAILIMF_ADDRESS_GROUP: 199 case MAILIMF_ADDRESS_GROUP:
182 result.append( parseGroup( addr->ad_data.ad_group ) ); 200 result.append( parseGroup( addr->ad_data.ad_group ) );
183 break; 201 break;
184 default: 202 default:
185 qDebug( "POP3: unkown mailimf address type" ); 203 qDebug( "POP3: unkown mailimf address type" );
186 break; 204 break;
187 } 205 }
188 } 206 }
189 207
190 return result; 208 return result;
191} 209}
192 210
193QString POP3wrapper::parseGroup( mailimf_group *group ) 211QString POP3wrapper::parseGroup( mailimf_group *group )
194{ 212{
195 QString result( "" ); 213 QString result( "" );
196 214
197 result.append( group->grp_display_name ); 215 result.append( group->grp_display_name );
198 result.append( ": " ); 216 result.append( ": " );
199 217
200 if ( group->grp_mb_list != NULL ) { 218 if ( group->grp_mb_list != NULL ) {
201 result.append( parseMailboxList( group->grp_mb_list ) ); 219 result.append( parseMailboxList( group->grp_mb_list ) );
202 } 220 }
203 221
204 result.append( ";" ); 222 result.append( ";" );
205 223
206 return result; 224 return result;
207} 225}
208 226
209QString POP3wrapper::parseMailbox( mailimf_mailbox *box ) 227QString POP3wrapper::parseMailbox( mailimf_mailbox *box )
210{ 228{
211 QString result( "" ); 229 QString result( "" );
212 230
213 if ( box->mb_display_name == NULL ) { 231 if ( box->mb_display_name == NULL ) {
214 result.append( box->mb_addr_spec ); 232 result.append( box->mb_addr_spec );
215 } else { 233 } else {
216 result.append( box->mb_display_name ); 234 result.append( box->mb_display_name );
217 result.append( " <" ); 235 result.append( " <" );
218 result.append( box->mb_addr_spec ); 236 result.append( box->mb_addr_spec );
219 result.append( ">" ); 237 result.append( ">" );
220 } 238 }
221 239
222 return result; 240 return result;
223} 241}
224 242
225QString POP3wrapper::parseMailboxList( mailimf_mailbox_list *list ) 243QString POP3wrapper::parseMailboxList( mailimf_mailbox_list *list )
226{ 244{
227 QString result( "" ); 245 QString result( "" );
228 246
229 bool first = true; 247 bool first = true;
230 for ( clistiter *current = clist_begin( list->mb_list ); current != NULL; current = current->next ) { 248 for ( clistiter *current = clist_begin( list->mb_list ); current != NULL; current = current->next ) {
231 mailimf_mailbox *box = (mailimf_mailbox *) current->data; 249 mailimf_mailbox *box = (mailimf_mailbox *) current->data;
232 250
233 if ( !first ) { 251 if ( !first ) {
234 result.append( "," ); 252 result.append( "," );
235 } else { 253 } else {
236 first = false; 254 first = false;
237 } 255 }
238 256
239 result.append( parseMailbox( box ) ); 257 result.append( parseMailbox( box ) );
240 } 258 }
241 259
242 return result; 260 return result;
243} 261}
244 262
245void POP3wrapper::login() 263void POP3wrapper::login()
246{ 264{
247 if ( m_pop3 != NULL ) logout(); 265 /* we'll hold the line */
266 if ( m_pop3 != NULL ) return;
248 267
249 const char *server, *user, *pass; 268 const char *server, *user, *pass;
250 uint16_t port; 269 uint16_t port;
251 int err = MAILPOP3_NO_ERROR; 270 int err = MAILPOP3_NO_ERROR;
252 271
253 server = account->getServer().latin1(); 272 server = account->getServer().latin1();
254 port = account->getPort().toUInt(); 273 port = account->getPort().toUInt();
255 user = account->getUser().latin1(); 274 user = account->getUser().latin1();
256 pass = account->getPassword().latin1(); 275 pass = account->getPassword().latin1();
257 276
258 m_pop3 = mailpop3_new( 200, &pop3_progress ); 277 m_pop3 = mailpop3_new( 200, &pop3_progress );
259 278
260 // connect 279 // connect
261 if (account->getSSL()) { 280 if (account->getSSL()) {
262 err = mailpop3_ssl_connect( m_pop3, (char*)server, port ); 281 err = mailpop3_ssl_connect( m_pop3, (char*)server, port );
263 } else { 282 } else {
264 err = mailpop3_socket_connect( m_pop3, (char*)server, port ); 283 err = mailpop3_socket_connect( m_pop3, (char*)server, port );
265 } 284 }
266 285
267 if ( err != MAILPOP3_NO_ERROR ) { 286 if ( err != MAILPOP3_NO_ERROR ) {
268 qDebug( "pop3: error connecting to %s\n reason: %s", server, 287 qDebug( "pop3: error connecting to %s\n reason: %s", server,
269 m_pop3->pop3_response ); 288 m_pop3->pop3_response );
270 mailpop3_free( m_pop3 ); 289 mailpop3_free( m_pop3 );
271 m_pop3 = NULL; 290 m_pop3 = NULL;
272 return; 291 return;
273 } 292 }
274 qDebug( "POP3: connected!" ); 293 qDebug( "POP3: connected!" );
275 294
276 // login 295 // login
277 // TODO: decide if apop or plain login should be used 296 // TODO: decide if apop or plain login should be used
278 err = mailpop3_login( m_pop3, (char *) user, (char *) pass ); 297 err = mailpop3_login( m_pop3, (char *) user, (char *) pass );
279 if ( err != MAILPOP3_NO_ERROR ) { 298 if ( err != MAILPOP3_NO_ERROR ) {
280 qDebug( "pop3: error logging in: %s", m_pop3->pop3_response ); 299 qDebug( "pop3: error logging in: %s", m_pop3->pop3_response );
281 logout(); 300 logout();
282 return; 301 return;
283 } 302 }
284 303
285 qDebug( "POP3: logged in!" ); 304 qDebug( "POP3: logged in!" );
286} 305}
287 306
288void POP3wrapper::logout() 307void POP3wrapper::logout()
289{ 308{
290 int err = MAILPOP3_NO_ERROR; 309 int err = MAILPOP3_NO_ERROR;
291 if ( m_pop3 == NULL ) return; 310 if ( m_pop3 == NULL ) return;
292 err = mailpop3_quit( m_pop3 ); 311 err = mailpop3_quit( m_pop3 );
293 mailpop3_free( m_pop3 ); 312 mailpop3_free( m_pop3 );
294 m_pop3 = NULL; 313 m_pop3 = NULL;
295} 314}
296 315
297 316
298QList<Folder>* POP3wrapper::listFolders() 317QList<Folder>* POP3wrapper::listFolders()
299{ 318{
319 /* TODO: integrate MH directories
320 but not vor version 0.1 ;)
321 */
300 QList<Folder> * folders = new QList<Folder>(); 322 QList<Folder> * folders = new QList<Folder>();
301 folders->setAutoDelete( false ); 323 folders->setAutoDelete( false );
302 Folder*inb=new Folder("INBOX"); 324 Folder*inb=new Folder("INBOX");
303 folders->append(inb); 325 folders->append(inb);
304 return folders; 326 return folders;
305} 327}
306 328
307QString POP3wrapper::fetchPart(const RecMail&,const RecPart&) 329QString POP3wrapper::fetchPart(const RecMail&,const RecPart&)
308{ 330{
309 return ""; 331 return "";
310} 332}
311 333
312void POP3wrapper::deleteMail(const RecMail&) 334void POP3wrapper::deleteMail(const RecMail&mail)
313{ 335{
336 login();
337 if (!m_pop3) return;
338 int err = mailpop3_dele(m_pop3,mail.getNumber());
339 if (err != MAILPOP3_NO_ERROR) {
340 qDebug("error deleting mail");
341 }
314} 342}
315 343
316void POP3wrapper::answeredMail(const RecMail&) 344void POP3wrapper::answeredMail(const RecMail&)
317{ 345{
318} 346}
diff --git a/noncore/net/mail/pop3wrapper.cpp b/noncore/net/mail/pop3wrapper.cpp
index 2411399..b4a8f4b 100644
--- a/noncore/net/mail/pop3wrapper.cpp
+++ b/noncore/net/mail/pop3wrapper.cpp
@@ -1,318 +1,346 @@
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
6POP3wrapper::POP3wrapper( POP3account *a ) 6POP3wrapper::POP3wrapper( POP3account *a )
7{ 7{
8 account = a; 8 account = a;
9 m_pop3 = NULL; 9 m_pop3 = NULL;
10} 10}
11 11
12POP3wrapper::~POP3wrapper() 12POP3wrapper::~POP3wrapper()
13{ 13{
14 logout(); 14 logout();
15} 15}
16 16
17void POP3wrapper::pop3_progress( size_t current, size_t maximum ) 17void 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
22RecBody POP3wrapper::fetchBody( const RecMail &mail ) 22RecBody 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; 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 logout();
35 return RecBody(); 34 return RecBody();
36 } 35 }
37 36
38 logout();
39
40 return parseBody( message ); 37 return parseBody( message );
41} 38}
42 39
43RecBody POP3wrapper::parseBody( const char *message ) 40RecBody POP3wrapper::parseBody( const char *message )
44{ 41{
45 int err = MAILIMF_NO_ERROR; 42 int err = MAILIMF_NO_ERROR;
46 /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ 43 /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */
47 size_t curTok = 0; 44 size_t curTok = 0;
48 mailimf_message *result = 0; 45 mailimf_message *result = 0;
49 RecBody body; 46 RecBody body;
50 47
51 err = mailimf_message_parse( (char *) message, strlen( message ), &curTok, &result ); 48 err = mailimf_message_parse( (char *) message, strlen( message ), &curTok, &result );
52 if ( err != MAILIMF_NO_ERROR ) { 49 if ( err != MAILIMF_NO_ERROR ) {
53 if (result) mailimf_message_free(result); 50 if (result) mailimf_message_free(result);
54 return body; 51 return body;
55 } 52 }
56 53
57 if ( result && result->msg_body && result->msg_body->bd_text ) { 54 if ( result && result->msg_body && result->msg_body->bd_text ) {
58 qDebug( "POP3: bodytext found" ); 55 qDebug( "POP3: bodytext found" );
59 // 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!
60 body.setBodytext( QString( result->msg_body->bd_text ) ); 57 body.setBodytext( QString( result->msg_body->bd_text ) );
61 } 58 }
62 if (result) mailimf_message_free(result); 59 if (result) mailimf_message_free(result);
63 return body; 60 return body;
64} 61}
65 62
66void POP3wrapper::listMessages(const QString &, QList<RecMail> &target ) 63void POP3wrapper::listMessages(const QString &, QList<RecMail> &target )
67{ 64{
68 int err = MAILPOP3_NO_ERROR; 65 int err = MAILPOP3_NO_ERROR;
69 char * header = 0; 66 char * header = 0;
70 /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ 67 /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */
71 size_t length = 0; 68 size_t length = 0;
72 carray * messages = 0; 69 carray * messages = 0;
73 70
74 login(); 71 login();
75 if (!m_pop3) return; 72 if (!m_pop3) return;
76 mailpop3_list( m_pop3, &messages ); 73 mailpop3_list( m_pop3, &messages );
77 74
78 //for ( int i = carray_count( messages ); i > 0; i-- ) {
79 for (unsigned int i = 0; i < carray_count(messages);++i) { 75 for (unsigned int i = 0; i < carray_count(messages);++i) {
80 mailpop3_msg_info *info; 76 mailpop3_msg_info *info;
81 int r = mailpop3_get_msg_info(m_pop3,i+1,&info); 77 err = mailpop3_get_msg_info(m_pop3,i+1,&info);
82 err = mailpop3_header( m_pop3, info->msg_index, &header, &length ); 78 err = mailpop3_header( m_pop3, info->msg_index, &header, &length );
83 if ( err != MAILPOP3_NO_ERROR ) { 79 if ( err != MAILPOP3_NO_ERROR ) {
84 qDebug( "POP3: error retrieving header msgid: %i", info->msg_index ); 80 qDebug( "POP3: error retrieving header msgid: %i", info->msg_index );
85 free(header); 81 free(header);
86 logout();
87 return; 82 return;
88 } 83 }
89 RecMail *mail = parseHeader( header ); 84 RecMail *mail = parseHeader( header );
90 mail->setNumber( info->msg_index ); 85 mail->setNumber( info->msg_index );
91 mail->setWrapper(this); 86 mail->setWrapper(this);
87 mail->setMsgsize(info->msg_size);
92 target.append( mail ); 88 target.append( mail );
93 free(header); 89 free(header);
94 } 90 }
95 logout();
96} 91}
97 92
98RecMail *POP3wrapper::parseHeader( const char *header ) 93RecMail *POP3wrapper::parseHeader( const char *header )
99{ 94{
100 int err = MAILIMF_NO_ERROR; 95 int err = MAILIMF_NO_ERROR;
101 size_t curTok = 0; 96 size_t curTok = 0;
102 RecMail *mail = new RecMail(); 97 RecMail *mail = new RecMail();
103 mailimf_fields *fields; 98 mailimf_fields *fields;
104 mailimf_references * refs; 99 mailimf_references * refs;
105 mailimf_keywords*keys; 100 mailimf_keywords*keys;
101 QString status;
102 QString value;
103 QBitArray mFlags(7);
106 104
107 err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields ); 105 err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields );
108 for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) { 106 for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) {
109 mailimf_field *field = (mailimf_field *) current->data; 107 mailimf_field *field = (mailimf_field *) current->data;
110 switch ( field->fld_type ) { 108 switch ( field->fld_type ) {
111 case MAILIMF_FIELD_FROM: 109 case MAILIMF_FIELD_FROM:
112 mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) ); 110 mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) );
113 break; 111 break;
114 case MAILIMF_FIELD_TO: 112 case MAILIMF_FIELD_TO:
115 mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) ); 113 mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) );
116 break; 114 break;
117 case MAILIMF_FIELD_CC: 115 case MAILIMF_FIELD_CC:
118 mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) ); 116 mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) );
119 break; 117 break;
120 case MAILIMF_FIELD_BCC: 118 case MAILIMF_FIELD_BCC:
121 mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) ); 119 mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) );
122 break; 120 break;
123 case MAILIMF_FIELD_SUBJECT: 121 case MAILIMF_FIELD_SUBJECT:
124 mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) ); 122 mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) );
125 break; 123 break;
126 case MAILIMF_FIELD_ORIG_DATE: 124 case MAILIMF_FIELD_ORIG_DATE:
127 mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) ); 125 mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) );
128 break; 126 break;
129 case MAILIMF_FIELD_MESSAGE_ID: 127 case MAILIMF_FIELD_MESSAGE_ID:
130 mail->setMsgid(QString(field->fld_data.fld_message_id->mid_value)); 128 mail->setMsgid(QString(field->fld_data.fld_message_id->mid_value));
131 break; 129 break;
132 case MAILIMF_FIELD_REFERENCES: 130 case MAILIMF_FIELD_REFERENCES:
133 refs = field->fld_data.fld_references; 131 refs = field->fld_data.fld_references;
134 if (refs && refs->mid_list && clist_count(refs->mid_list)) { 132 if (refs && refs->mid_list && clist_count(refs->mid_list)) {
135 char * text = (char*)refs->mid_list->first->data; 133 char * text = (char*)refs->mid_list->first->data;
136 mail->setReplyto(QString(text)); 134 mail->setReplyto(QString(text));
137 } 135 }
138 break; 136 break;
139 case MAILIMF_FIELD_KEYWORDS: 137 case MAILIMF_FIELD_KEYWORDS:
140 keys = field->fld_data.fld_keywords; 138 keys = field->fld_data.fld_keywords;
141 for (clistcell*cur = clist_begin(keys->kw_list);cur!=0;cur=clist_next(cur)) { 139 for (clistcell*cur = clist_begin(keys->kw_list);cur!=0;cur=clist_next(cur)) {
142 qDebug("Keyword: %s",(char*)cur->data); 140 qDebug("Keyword: %s",(char*)cur->data);
143 } 141 }
144 break; 142 break;
143 case MAILIMF_FIELD_OPTIONAL_FIELD:
144 status = field->fld_data.fld_optional_field->fld_name;
145 value = field->fld_data.fld_optional_field->fld_value;
146 if (status.lower()=="status") {
147 if (value.lower()=="ro") {
148 mFlags.setBit(FLAG_SEEN);
149 }
150 } else if (status.lower()=="x-status") {
151 qDebug("X-Status: %s",value.latin1());
152 if (value.lower()=="a") {
153
154 mFlags.setBit(FLAG_ANSWERED);
155 }
156 } else {
157// qDebug("Optionales feld: %s -> %s)",field->fld_data.fld_optional_field->fld_name,
158// field->fld_data.fld_optional_field->fld_value);
159 }
160 break;
145 default: 161 default:
162 qDebug("Non parsed field");
146 break; 163 break;
147 } 164 }
148 } 165 }
149 if (fields) mailimf_fields_free(fields); 166 if (fields) mailimf_fields_free(fields);
167 mail->setFlags(mFlags);
150 return mail; 168 return mail;
151} 169}
152 170
153QString POP3wrapper::parseDateTime( mailimf_date_time *date ) 171QString POP3wrapper::parseDateTime( mailimf_date_time *date )
154{ 172{
155 char tmp[23]; 173 char tmp[23];
156 174
157 snprintf( tmp, 23, "%02i.%02i.%04i %02i:%02i:%02i %+05i", 175 snprintf( tmp, 23, "%02i.%02i.%04i %02i:%02i:%02i %+05i",
158 date->dt_day, date->dt_month, date->dt_year, date->dt_hour, date->dt_min, date->dt_sec, date->dt_zone ); 176 date->dt_day, date->dt_month, date->dt_year, date->dt_hour, date->dt_min, date->dt_sec, date->dt_zone );
159 177
160 return QString( tmp ); 178 return QString( tmp );
161} 179}
162 180
163QString POP3wrapper::parseAddressList( mailimf_address_list *list ) 181QString POP3wrapper::parseAddressList( mailimf_address_list *list )
164{ 182{
165 QString result( "" ); 183 QString result( "" );
166 184
167 bool first = true; 185 bool first = true;
168 for ( clistiter *current = clist_begin( list->ad_list ); current != NULL; current = current->next ) { 186 for ( clistiter *current = clist_begin( list->ad_list ); current != NULL; current = current->next ) {
169 mailimf_address *addr = (mailimf_address *) current->data; 187 mailimf_address *addr = (mailimf_address *) current->data;
170 188
171 if ( !first ) { 189 if ( !first ) {
172 result.append( "," ); 190 result.append( "," );
173 } else { 191 } else {
174 first = false; 192 first = false;
175 } 193 }
176 194
177 switch ( addr->ad_type ) { 195 switch ( addr->ad_type ) {
178 case MAILIMF_ADDRESS_MAILBOX: 196 case MAILIMF_ADDRESS_MAILBOX:
179 result.append( parseMailbox( addr->ad_data.ad_mailbox ) ); 197 result.append( parseMailbox( addr->ad_data.ad_mailbox ) );
180 break; 198 break;
181 case MAILIMF_ADDRESS_GROUP: 199 case MAILIMF_ADDRESS_GROUP:
182 result.append( parseGroup( addr->ad_data.ad_group ) ); 200 result.append( parseGroup( addr->ad_data.ad_group ) );
183 break; 201 break;
184 default: 202 default:
185 qDebug( "POP3: unkown mailimf address type" ); 203 qDebug( "POP3: unkown mailimf address type" );
186 break; 204 break;
187 } 205 }
188 } 206 }
189 207
190 return result; 208 return result;
191} 209}
192 210
193QString POP3wrapper::parseGroup( mailimf_group *group ) 211QString POP3wrapper::parseGroup( mailimf_group *group )
194{ 212{
195 QString result( "" ); 213 QString result( "" );
196 214
197 result.append( group->grp_display_name ); 215 result.append( group->grp_display_name );
198 result.append( ": " ); 216 result.append( ": " );
199 217
200 if ( group->grp_mb_list != NULL ) { 218 if ( group->grp_mb_list != NULL ) {
201 result.append( parseMailboxList( group->grp_mb_list ) ); 219 result.append( parseMailboxList( group->grp_mb_list ) );
202 } 220 }
203 221
204 result.append( ";" ); 222 result.append( ";" );
205 223
206 return result; 224 return result;
207} 225}
208 226
209QString POP3wrapper::parseMailbox( mailimf_mailbox *box ) 227QString POP3wrapper::parseMailbox( mailimf_mailbox *box )
210{ 228{
211 QString result( "" ); 229 QString result( "" );
212 230
213 if ( box->mb_display_name == NULL ) { 231 if ( box->mb_display_name == NULL ) {
214 result.append( box->mb_addr_spec ); 232 result.append( box->mb_addr_spec );
215 } else { 233 } else {
216 result.append( box->mb_display_name ); 234 result.append( box->mb_display_name );
217 result.append( " <" ); 235 result.append( " <" );
218 result.append( box->mb_addr_spec ); 236 result.append( box->mb_addr_spec );
219 result.append( ">" ); 237 result.append( ">" );
220 } 238 }
221 239
222 return result; 240 return result;
223} 241}
224 242
225QString POP3wrapper::parseMailboxList( mailimf_mailbox_list *list ) 243QString POP3wrapper::parseMailboxList( mailimf_mailbox_list *list )
226{ 244{
227 QString result( "" ); 245 QString result( "" );
228 246
229 bool first = true; 247 bool first = true;
230 for ( clistiter *current = clist_begin( list->mb_list ); current != NULL; current = current->next ) { 248 for ( clistiter *current = clist_begin( list->mb_list ); current != NULL; current = current->next ) {
231 mailimf_mailbox *box = (mailimf_mailbox *) current->data; 249 mailimf_mailbox *box = (mailimf_mailbox *) current->data;
232 250
233 if ( !first ) { 251 if ( !first ) {
234 result.append( "," ); 252 result.append( "," );
235 } else { 253 } else {
236 first = false; 254 first = false;
237 } 255 }
238 256
239 result.append( parseMailbox( box ) ); 257 result.append( parseMailbox( box ) );
240 } 258 }
241 259
242 return result; 260 return result;
243} 261}
244 262
245void POP3wrapper::login() 263void POP3wrapper::login()
246{ 264{
247 if ( m_pop3 != NULL ) logout(); 265 /* we'll hold the line */
266 if ( m_pop3 != NULL ) return;
248 267
249 const char *server, *user, *pass; 268 const char *server, *user, *pass;
250 uint16_t port; 269 uint16_t port;
251 int err = MAILPOP3_NO_ERROR; 270 int err = MAILPOP3_NO_ERROR;
252 271
253 server = account->getServer().latin1(); 272 server = account->getServer().latin1();
254 port = account->getPort().toUInt(); 273 port = account->getPort().toUInt();
255 user = account->getUser().latin1(); 274 user = account->getUser().latin1();
256 pass = account->getPassword().latin1(); 275 pass = account->getPassword().latin1();
257 276
258 m_pop3 = mailpop3_new( 200, &pop3_progress ); 277 m_pop3 = mailpop3_new( 200, &pop3_progress );
259 278
260 // connect 279 // connect
261 if (account->getSSL()) { 280 if (account->getSSL()) {
262 err = mailpop3_ssl_connect( m_pop3, (char*)server, port ); 281 err = mailpop3_ssl_connect( m_pop3, (char*)server, port );
263 } else { 282 } else {
264 err = mailpop3_socket_connect( m_pop3, (char*)server, port ); 283 err = mailpop3_socket_connect( m_pop3, (char*)server, port );
265 } 284 }
266 285
267 if ( err != MAILPOP3_NO_ERROR ) { 286 if ( err != MAILPOP3_NO_ERROR ) {
268 qDebug( "pop3: error connecting to %s\n reason: %s", server, 287 qDebug( "pop3: error connecting to %s\n reason: %s", server,
269 m_pop3->pop3_response ); 288 m_pop3->pop3_response );
270 mailpop3_free( m_pop3 ); 289 mailpop3_free( m_pop3 );
271 m_pop3 = NULL; 290 m_pop3 = NULL;
272 return; 291 return;
273 } 292 }
274 qDebug( "POP3: connected!" ); 293 qDebug( "POP3: connected!" );
275 294
276 // login 295 // login
277 // TODO: decide if apop or plain login should be used 296 // TODO: decide if apop or plain login should be used
278 err = mailpop3_login( m_pop3, (char *) user, (char *) pass ); 297 err = mailpop3_login( m_pop3, (char *) user, (char *) pass );
279 if ( err != MAILPOP3_NO_ERROR ) { 298 if ( err != MAILPOP3_NO_ERROR ) {
280 qDebug( "pop3: error logging in: %s", m_pop3->pop3_response ); 299 qDebug( "pop3: error logging in: %s", m_pop3->pop3_response );
281 logout(); 300 logout();
282 return; 301 return;
283 } 302 }
284 303
285 qDebug( "POP3: logged in!" ); 304 qDebug( "POP3: logged in!" );
286} 305}
287 306
288void POP3wrapper::logout() 307void POP3wrapper::logout()
289{ 308{
290 int err = MAILPOP3_NO_ERROR; 309 int err = MAILPOP3_NO_ERROR;
291 if ( m_pop3 == NULL ) return; 310 if ( m_pop3 == NULL ) return;
292 err = mailpop3_quit( m_pop3 ); 311 err = mailpop3_quit( m_pop3 );
293 mailpop3_free( m_pop3 ); 312 mailpop3_free( m_pop3 );
294 m_pop3 = NULL; 313 m_pop3 = NULL;
295} 314}
296 315
297 316
298QList<Folder>* POP3wrapper::listFolders() 317QList<Folder>* POP3wrapper::listFolders()
299{ 318{
319 /* TODO: integrate MH directories
320 but not vor version 0.1 ;)
321 */
300 QList<Folder> * folders = new QList<Folder>(); 322 QList<Folder> * folders = new QList<Folder>();
301 folders->setAutoDelete( false ); 323 folders->setAutoDelete( false );
302 Folder*inb=new Folder("INBOX"); 324 Folder*inb=new Folder("INBOX");
303 folders->append(inb); 325 folders->append(inb);
304 return folders; 326 return folders;
305} 327}
306 328
307QString POP3wrapper::fetchPart(const RecMail&,const RecPart&) 329QString POP3wrapper::fetchPart(const RecMail&,const RecPart&)
308{ 330{
309 return ""; 331 return "";
310} 332}
311 333
312void POP3wrapper::deleteMail(const RecMail&) 334void POP3wrapper::deleteMail(const RecMail&mail)
313{ 335{
336 login();
337 if (!m_pop3) return;
338 int err = mailpop3_dele(m_pop3,mail.getNumber());
339 if (err != MAILPOP3_NO_ERROR) {
340 qDebug("error deleting mail");
341 }
314} 342}
315 343
316void POP3wrapper::answeredMail(const RecMail&) 344void POP3wrapper::answeredMail(const RecMail&)
317{ 345{
318} 346}