-rw-r--r-- | noncore/net/mail/libmailwrapper/pop3wrapper.cpp | 69 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/pop3wrapper.h | 7 | ||||
-rw-r--r-- | noncore/net/mail/pop3wrapper.cpp | 69 | ||||
-rw-r--r-- | noncore/net/mail/pop3wrapper.h | 7 |
4 files changed, 128 insertions, 24 deletions
diff --git a/noncore/net/mail/libmailwrapper/pop3wrapper.cpp b/noncore/net/mail/libmailwrapper/pop3wrapper.cpp index 4508a95..30f80ff 100644 --- a/noncore/net/mail/libmailwrapper/pop3wrapper.cpp +++ b/noncore/net/mail/libmailwrapper/pop3wrapper.cpp | |||
@@ -1,315 +1,366 @@ | |||
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 | #include <libetpan/mailmime.h> | ||
6 | #include <qfile.h> | ||
5 | 7 | ||
6 | POP3wrapper::POP3wrapper( POP3account *a ) | 8 | POP3wrapper::POP3wrapper( POP3account *a ) |
7 | { | 9 | { |
8 | account = a; | 10 | account = a; |
9 | m_pop3 = NULL; | 11 | m_pop3 = NULL; |
12 | msgTempName = a->getFileName()+"_msg_cache"; | ||
13 | last_msg_id = 0; | ||
10 | } | 14 | } |
11 | 15 | ||
12 | POP3wrapper::~POP3wrapper() | 16 | POP3wrapper::~POP3wrapper() |
13 | { | 17 | { |
14 | logout(); | 18 | logout(); |
19 | QFile msg_cache(msgTempName); | ||
20 | if (msg_cache.exists()) { | ||
21 | msg_cache.remove(); | ||
22 | } | ||
15 | } | 23 | } |
16 | 24 | ||
17 | void POP3wrapper::pop3_progress( size_t current, size_t maximum ) | 25 | void POP3wrapper::pop3_progress( size_t current, size_t maximum ) |
18 | { | 26 | { |
19 | //qDebug( "POP3: %i of %i", current, maximum ); | 27 | //qDebug( "POP3: %i of %i", current, maximum ); |
20 | } | 28 | } |
21 | 29 | ||
22 | RecBody POP3wrapper::fetchBody( const RecMail &mail ) | 30 | RecBody POP3wrapper::fetchBody( const RecMail &mail ) |
23 | { | 31 | { |
24 | int err = MAILPOP3_NO_ERROR; | 32 | int err = MAILPOP3_NO_ERROR; |
25 | char *message; | 33 | char *message; |
26 | size_t length = 0; | 34 | size_t length = 0; |
27 | 35 | ||
28 | login(); | 36 | login(); |
29 | if ( !m_pop3 ) return RecBody(); | 37 | if ( !m_pop3 ) { |
30 | |||
31 | err = mailpop3_retr( m_pop3, mail.getNumber(), &message, &length ); | ||
32 | if ( err != MAILPOP3_NO_ERROR ) { | ||
33 | qDebug( "POP3: error retrieving body with index %i", mail.getNumber() ); | ||
34 | return RecBody(); | 38 | return RecBody(); |
35 | } | 39 | } |
40 | RecBody body; | ||
36 | 41 | ||
37 | return parseBody( message ); | 42 | QFile msg_cache(msgTempName); |
43 | |||
44 | if (mail.getNumber()!=last_msg_id) { | ||
45 | if (msg_cache.exists()) { | ||
46 | msg_cache.remove(); | ||
47 | } | ||
48 | msg_cache.open(IO_ReadWrite|IO_Truncate); | ||
49 | last_msg_id = mail.getNumber(); | ||
50 | err = mailpop3_retr( m_pop3, mail.getNumber(), &message, &length ); | ||
51 | if ( err != MAILPOP3_NO_ERROR ) { | ||
52 | qDebug( "POP3: error retrieving body with index %i", mail.getNumber() ); | ||
53 | last_msg_id = 0; | ||
54 | return RecBody(); | ||
55 | } | ||
56 | msg_cache.writeBlock(message,length); | ||
57 | } else { | ||
58 | QString msg=""; | ||
59 | msg_cache.open(IO_ReadOnly); | ||
60 | message = new char[4096]; | ||
61 | memset(message,0,4096); | ||
62 | while (msg_cache.readBlock(message,4095)>0) { | ||
63 | msg+=message; | ||
64 | memset(message,0,4096); | ||
65 | } | ||
66 | delete message; | ||
67 | message = (char*)malloc(msg.length()+1*sizeof(char)); | ||
68 | memset(message,0,msg.length()+1); | ||
69 | memcpy(message,msg.latin1(),msg.length()); | ||
70 | } | ||
71 | body = parseMail(message); | ||
72 | free(message); | ||
73 | return body; | ||
38 | } | 74 | } |
39 | 75 | ||
40 | RecBody POP3wrapper::parseBody( const char *message ) | 76 | RecBody POP3wrapper::parseMail( char *message ) |
41 | { | 77 | { |
42 | int err = MAILIMF_NO_ERROR; | 78 | int err = MAILIMF_NO_ERROR; |
43 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ | 79 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ |
44 | size_t curTok = 0; | 80 | size_t curTok = 0; |
45 | mailimf_message *result = 0; | 81 | mailimf_message *result = 0; |
46 | RecBody body; | 82 | RecBody body; |
47 | 83 | ||
84 | |||
48 | err = mailimf_message_parse( (char *) message, strlen( message ), &curTok, &result ); | 85 | err = mailimf_message_parse( (char *) message, strlen( message ), &curTok, &result ); |
49 | if ( err != MAILIMF_NO_ERROR ) { | 86 | if ( err != MAILIMF_NO_ERROR ) { |
50 | if (result) mailimf_message_free(result); | 87 | if (result) mailimf_message_free(result); |
51 | return body; | 88 | return body; |
52 | } | 89 | } |
53 | 90 | ||
91 | struct mailimf_body * b = 0; | ||
92 | struct mailimf_fields * f = 0; | ||
93 | |||
94 | |||
54 | if ( result && result->msg_body && result->msg_body->bd_text ) { | 95 | if ( result && result->msg_body && result->msg_body->bd_text ) { |
55 | qDebug( "POP3: bodytext found" ); | 96 | qDebug( "POP3: bodytext found" ); |
56 | // when curTok isn't set to 0 this line will fault! 'cause upper line faults! | 97 | // when curTok isn't set to 0 this line will fault! 'cause upper line faults! |
57 | body.setBodytext( QString( result->msg_body->bd_text ) ); | 98 | body.setBodytext( QString( result->msg_body->bd_text ) ); |
99 | #if 0 | ||
100 | curTok = 0; | ||
101 | mailmime_content*mresult = 0; | ||
102 | size_t index = 0; | ||
103 | mailmime_content_parse(result->msg_body->bd_text, | ||
104 | strlen(result->msg_body->bd_text),&index,&mresult); | ||
105 | if (mresult) { | ||
106 | mailmime_content_free(mresult); | ||
107 | } | ||
108 | #endif | ||
109 | mailimf_message_free(result); | ||
58 | } | 110 | } |
59 | if (result) mailimf_message_free(result); | ||
60 | return body; | 111 | return body; |
61 | } | 112 | } |
62 | 113 | ||
63 | void POP3wrapper::listMessages(const QString &, QList<RecMail> &target ) | 114 | void POP3wrapper::listMessages(const QString &, QList<RecMail> &target ) |
64 | { | 115 | { |
65 | int err = MAILPOP3_NO_ERROR; | 116 | int err = MAILPOP3_NO_ERROR; |
66 | char * header = 0; | 117 | char * header = 0; |
67 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ | 118 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ |
68 | size_t length = 0; | 119 | size_t length = 0; |
69 | carray * messages = 0; | 120 | carray * messages = 0; |
70 | 121 | ||
71 | login(); | 122 | login(); |
72 | if (!m_pop3) return; | 123 | if (!m_pop3) return; |
73 | mailpop3_list( m_pop3, &messages ); | 124 | mailpop3_list( m_pop3, &messages ); |
74 | 125 | ||
75 | for (unsigned int i = 0; i < carray_count(messages);++i) { | 126 | for (unsigned int i = 0; i < carray_count(messages);++i) { |
76 | mailpop3_msg_info *info; | 127 | mailpop3_msg_info *info; |
77 | err = mailpop3_get_msg_info(m_pop3,i+1,&info); | 128 | err = mailpop3_get_msg_info(m_pop3,i+1,&info); |
78 | if (info->msg_deleted) | 129 | if (info->msg_deleted) |
79 | continue; | 130 | continue; |
80 | err = mailpop3_header( m_pop3, info->msg_index, &header, &length ); | 131 | err = mailpop3_header( m_pop3, info->msg_index, &header, &length ); |
81 | if ( err != MAILPOP3_NO_ERROR ) { | 132 | if ( err != MAILPOP3_NO_ERROR ) { |
82 | qDebug( "POP3: error retrieving header msgid: %i", info->msg_index ); | 133 | qDebug( "POP3: error retrieving header msgid: %i", info->msg_index ); |
83 | free(header); | 134 | free(header); |
84 | return; | 135 | return; |
85 | } | 136 | } |
86 | RecMail *mail = parseHeader( header ); | 137 | RecMail *mail = parseHeader( header ); |
87 | mail->setNumber( info->msg_index ); | 138 | mail->setNumber( info->msg_index ); |
88 | mail->setWrapper(this); | 139 | mail->setWrapper(this); |
89 | mail->setMsgsize(info->msg_size); | 140 | mail->setMsgsize(info->msg_size); |
90 | target.append( mail ); | 141 | target.append( mail ); |
91 | free(header); | 142 | free(header); |
92 | } | 143 | } |
93 | } | 144 | } |
94 | 145 | ||
95 | RecMail *POP3wrapper::parseHeader( const char *header ) | 146 | RecMail *POP3wrapper::parseHeader( const char *header ) |
96 | { | 147 | { |
97 | int err = MAILIMF_NO_ERROR; | 148 | int err = MAILIMF_NO_ERROR; |
98 | size_t curTok = 0; | 149 | size_t curTok = 0; |
99 | RecMail *mail = new RecMail(); | 150 | RecMail *mail = new RecMail(); |
100 | mailimf_fields *fields; | 151 | mailimf_fields *fields; |
101 | mailimf_references * refs; | 152 | mailimf_references * refs; |
102 | mailimf_keywords*keys; | 153 | mailimf_keywords*keys; |
103 | QString status; | 154 | QString status; |
104 | QString value; | 155 | QString value; |
105 | QBitArray mFlags(7); | 156 | QBitArray mFlags(7); |
106 | 157 | ||
107 | err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields ); | 158 | err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields ); |
108 | for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) { | 159 | for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) { |
109 | mailimf_field *field = (mailimf_field *) current->data; | 160 | mailimf_field *field = (mailimf_field *) current->data; |
110 | switch ( field->fld_type ) { | 161 | switch ( field->fld_type ) { |
111 | case MAILIMF_FIELD_FROM: | 162 | case MAILIMF_FIELD_FROM: |
112 | mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) ); | 163 | mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) ); |
113 | break; | 164 | break; |
114 | case MAILIMF_FIELD_TO: | 165 | case MAILIMF_FIELD_TO: |
115 | mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) ); | 166 | mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) ); |
116 | break; | 167 | break; |
117 | case MAILIMF_FIELD_CC: | 168 | case MAILIMF_FIELD_CC: |
118 | mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) ); | 169 | mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) ); |
119 | break; | 170 | break; |
120 | case MAILIMF_FIELD_BCC: | 171 | case MAILIMF_FIELD_BCC: |
121 | mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) ); | 172 | mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) ); |
122 | break; | 173 | break; |
123 | case MAILIMF_FIELD_SUBJECT: | 174 | case MAILIMF_FIELD_SUBJECT: |
124 | mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) ); | 175 | mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) ); |
125 | break; | 176 | break; |
126 | case MAILIMF_FIELD_ORIG_DATE: | 177 | case MAILIMF_FIELD_ORIG_DATE: |
127 | mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) ); | 178 | mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) ); |
128 | break; | 179 | break; |
129 | case MAILIMF_FIELD_MESSAGE_ID: | 180 | case MAILIMF_FIELD_MESSAGE_ID: |
130 | mail->setMsgid(QString(field->fld_data.fld_message_id->mid_value)); | 181 | mail->setMsgid(QString(field->fld_data.fld_message_id->mid_value)); |
131 | break; | 182 | break; |
132 | case MAILIMF_FIELD_REFERENCES: | 183 | case MAILIMF_FIELD_REFERENCES: |
133 | refs = field->fld_data.fld_references; | 184 | refs = field->fld_data.fld_references; |
134 | if (refs && refs->mid_list && clist_count(refs->mid_list)) { | 185 | if (refs && refs->mid_list && clist_count(refs->mid_list)) { |
135 | char * text = (char*)refs->mid_list->first->data; | 186 | char * text = (char*)refs->mid_list->first->data; |
136 | mail->setReplyto(QString(text)); | 187 | mail->setReplyto(QString(text)); |
137 | } | 188 | } |
138 | break; | 189 | break; |
139 | case MAILIMF_FIELD_KEYWORDS: | 190 | case MAILIMF_FIELD_KEYWORDS: |
140 | keys = field->fld_data.fld_keywords; | 191 | keys = field->fld_data.fld_keywords; |
141 | for (clistcell*cur = clist_begin(keys->kw_list);cur!=0;cur=clist_next(cur)) { | 192 | for (clistcell*cur = clist_begin(keys->kw_list);cur!=0;cur=clist_next(cur)) { |
142 | qDebug("Keyword: %s",(char*)cur->data); | 193 | qDebug("Keyword: %s",(char*)cur->data); |
143 | } | 194 | } |
144 | break; | 195 | break; |
145 | case MAILIMF_FIELD_OPTIONAL_FIELD: | 196 | case MAILIMF_FIELD_OPTIONAL_FIELD: |
146 | status = field->fld_data.fld_optional_field->fld_name; | 197 | status = field->fld_data.fld_optional_field->fld_name; |
147 | value = field->fld_data.fld_optional_field->fld_value; | 198 | value = field->fld_data.fld_optional_field->fld_value; |
148 | if (status.lower()=="status") { | 199 | if (status.lower()=="status") { |
149 | if (value.lower()=="ro") { | 200 | if (value.lower()=="ro") { |
150 | mFlags.setBit(FLAG_SEEN); | 201 | mFlags.setBit(FLAG_SEEN); |
151 | } | 202 | } |
152 | } else if (status.lower()=="x-status") { | 203 | } else if (status.lower()=="x-status") { |
153 | qDebug("X-Status: %s",value.latin1()); | 204 | qDebug("X-Status: %s",value.latin1()); |
154 | if (value.lower()=="a") { | 205 | if (value.lower()=="a") { |
155 | 206 | ||
156 | mFlags.setBit(FLAG_ANSWERED); | 207 | mFlags.setBit(FLAG_ANSWERED); |
157 | } | 208 | } |
158 | } else { | 209 | } else { |
159 | // qDebug("Optionales feld: %s -> %s)",field->fld_data.fld_optional_field->fld_name, | 210 | // qDebug("Optionales feld: %s -> %s)",field->fld_data.fld_optional_field->fld_name, |
160 | // field->fld_data.fld_optional_field->fld_value); | 211 | // field->fld_data.fld_optional_field->fld_value); |
161 | } | 212 | } |
162 | break; | 213 | break; |
163 | default: | 214 | default: |
164 | qDebug("Non parsed field"); | 215 | qDebug("Non parsed field"); |
165 | break; | 216 | break; |
166 | } | 217 | } |
167 | } | 218 | } |
168 | if (fields) mailimf_fields_free(fields); | 219 | if (fields) mailimf_fields_free(fields); |
169 | mail->setFlags(mFlags); | 220 | mail->setFlags(mFlags); |
170 | return mail; | 221 | return mail; |
171 | } | 222 | } |
172 | 223 | ||
173 | QString POP3wrapper::parseDateTime( mailimf_date_time *date ) | 224 | QString POP3wrapper::parseDateTime( mailimf_date_time *date ) |
174 | { | 225 | { |
175 | char tmp[23]; | 226 | char tmp[23]; |
176 | 227 | ||
177 | snprintf( tmp, 23, "%02i.%02i.%04i %02i:%02i:%02i %+05i", | 228 | snprintf( tmp, 23, "%02i.%02i.%04i %02i:%02i:%02i %+05i", |
178 | date->dt_day, date->dt_month, date->dt_year, date->dt_hour, date->dt_min, date->dt_sec, date->dt_zone ); | 229 | date->dt_day, date->dt_month, date->dt_year, date->dt_hour, date->dt_min, date->dt_sec, date->dt_zone ); |
179 | 230 | ||
180 | return QString( tmp ); | 231 | return QString( tmp ); |
181 | } | 232 | } |
182 | 233 | ||
183 | QString POP3wrapper::parseAddressList( mailimf_address_list *list ) | 234 | QString POP3wrapper::parseAddressList( mailimf_address_list *list ) |
184 | { | 235 | { |
185 | QString result( "" ); | 236 | QString result( "" ); |
186 | 237 | ||
187 | bool first = true; | 238 | bool first = true; |
188 | for ( clistiter *current = clist_begin( list->ad_list ); current != NULL; current = current->next ) { | 239 | for ( clistiter *current = clist_begin( list->ad_list ); current != NULL; current = current->next ) { |
189 | mailimf_address *addr = (mailimf_address *) current->data; | 240 | mailimf_address *addr = (mailimf_address *) current->data; |
190 | 241 | ||
191 | if ( !first ) { | 242 | if ( !first ) { |
192 | result.append( "," ); | 243 | result.append( "," ); |
193 | } else { | 244 | } else { |
194 | first = false; | 245 | first = false; |
195 | } | 246 | } |
196 | 247 | ||
197 | switch ( addr->ad_type ) { | 248 | switch ( addr->ad_type ) { |
198 | case MAILIMF_ADDRESS_MAILBOX: | 249 | case MAILIMF_ADDRESS_MAILBOX: |
199 | result.append( parseMailbox( addr->ad_data.ad_mailbox ) ); | 250 | result.append( parseMailbox( addr->ad_data.ad_mailbox ) ); |
200 | break; | 251 | break; |
201 | case MAILIMF_ADDRESS_GROUP: | 252 | case MAILIMF_ADDRESS_GROUP: |
202 | result.append( parseGroup( addr->ad_data.ad_group ) ); | 253 | result.append( parseGroup( addr->ad_data.ad_group ) ); |
203 | break; | 254 | break; |
204 | default: | 255 | default: |
205 | qDebug( "POP3: unkown mailimf address type" ); | 256 | qDebug( "POP3: unkown mailimf address type" ); |
206 | break; | 257 | break; |
207 | } | 258 | } |
208 | } | 259 | } |
209 | 260 | ||
210 | return result; | 261 | return result; |
211 | } | 262 | } |
212 | 263 | ||
213 | QString POP3wrapper::parseGroup( mailimf_group *group ) | 264 | QString POP3wrapper::parseGroup( mailimf_group *group ) |
214 | { | 265 | { |
215 | QString result( "" ); | 266 | QString result( "" ); |
216 | 267 | ||
217 | result.append( group->grp_display_name ); | 268 | result.append( group->grp_display_name ); |
218 | result.append( ": " ); | 269 | result.append( ": " ); |
219 | 270 | ||
220 | if ( group->grp_mb_list != NULL ) { | 271 | if ( group->grp_mb_list != NULL ) { |
221 | result.append( parseMailboxList( group->grp_mb_list ) ); | 272 | result.append( parseMailboxList( group->grp_mb_list ) ); |
222 | } | 273 | } |
223 | 274 | ||
224 | result.append( ";" ); | 275 | result.append( ";" ); |
225 | 276 | ||
226 | return result; | 277 | return result; |
227 | } | 278 | } |
228 | 279 | ||
229 | QString POP3wrapper::parseMailbox( mailimf_mailbox *box ) | 280 | QString POP3wrapper::parseMailbox( mailimf_mailbox *box ) |
230 | { | 281 | { |
231 | QString result( "" ); | 282 | QString result( "" ); |
232 | 283 | ||
233 | if ( box->mb_display_name == NULL ) { | 284 | if ( box->mb_display_name == NULL ) { |
234 | result.append( box->mb_addr_spec ); | 285 | result.append( box->mb_addr_spec ); |
235 | } else { | 286 | } else { |
236 | result.append( box->mb_display_name ); | 287 | result.append( box->mb_display_name ); |
237 | result.append( " <" ); | 288 | result.append( " <" ); |
238 | result.append( box->mb_addr_spec ); | 289 | result.append( box->mb_addr_spec ); |
239 | result.append( ">" ); | 290 | result.append( ">" ); |
240 | } | 291 | } |
241 | 292 | ||
242 | return result; | 293 | return result; |
243 | } | 294 | } |
244 | 295 | ||
245 | QString POP3wrapper::parseMailboxList( mailimf_mailbox_list *list ) | 296 | QString POP3wrapper::parseMailboxList( mailimf_mailbox_list *list ) |
246 | { | 297 | { |
247 | QString result( "" ); | 298 | QString result( "" ); |
248 | 299 | ||
249 | bool first = true; | 300 | bool first = true; |
250 | for ( clistiter *current = clist_begin( list->mb_list ); current != NULL; current = current->next ) { | 301 | for ( clistiter *current = clist_begin( list->mb_list ); current != NULL; current = current->next ) { |
251 | mailimf_mailbox *box = (mailimf_mailbox *) current->data; | 302 | mailimf_mailbox *box = (mailimf_mailbox *) current->data; |
252 | 303 | ||
253 | if ( !first ) { | 304 | if ( !first ) { |
254 | result.append( "," ); | 305 | result.append( "," ); |
255 | } else { | 306 | } else { |
256 | first = false; | 307 | first = false; |
257 | } | 308 | } |
258 | 309 | ||
259 | result.append( parseMailbox( box ) ); | 310 | result.append( parseMailbox( box ) ); |
260 | } | 311 | } |
261 | 312 | ||
262 | return result; | 313 | return result; |
263 | } | 314 | } |
264 | 315 | ||
265 | void POP3wrapper::login() | 316 | void POP3wrapper::login() |
266 | { | 317 | { |
267 | /* we'll hold the line */ | 318 | /* we'll hold the line */ |
268 | if ( m_pop3 != NULL ) return; | 319 | if ( m_pop3 != NULL ) return; |
269 | 320 | ||
270 | const char *server, *user, *pass; | 321 | const char *server, *user, *pass; |
271 | uint16_t port; | 322 | uint16_t port; |
272 | int err = MAILPOP3_NO_ERROR; | 323 | int err = MAILPOP3_NO_ERROR; |
273 | 324 | ||
274 | server = account->getServer().latin1(); | 325 | server = account->getServer().latin1(); |
275 | port = account->getPort().toUInt(); | 326 | port = account->getPort().toUInt(); |
276 | user = account->getUser().latin1(); | 327 | user = account->getUser().latin1(); |
277 | pass = account->getPassword().latin1(); | 328 | pass = account->getPassword().latin1(); |
278 | 329 | ||
279 | m_pop3 = mailpop3_new( 200, &pop3_progress ); | 330 | m_pop3 = mailpop3_new( 200, &pop3_progress ); |
280 | 331 | ||
281 | // connect | 332 | // connect |
282 | if (account->getSSL()) { | 333 | if (account->getSSL()) { |
283 | err = mailpop3_ssl_connect( m_pop3, (char*)server, port ); | 334 | err = mailpop3_ssl_connect( m_pop3, (char*)server, port ); |
284 | } else { | 335 | } else { |
285 | err = mailpop3_socket_connect( m_pop3, (char*)server, port ); | 336 | err = mailpop3_socket_connect( m_pop3, (char*)server, port ); |
286 | } | 337 | } |
287 | 338 | ||
288 | if ( err != MAILPOP3_NO_ERROR ) { | 339 | if ( err != MAILPOP3_NO_ERROR ) { |
289 | qDebug( "pop3: error connecting to %s\n reason: %s", server, | 340 | qDebug( "pop3: error connecting to %s\n reason: %s", server, |
290 | m_pop3->pop3_response ); | 341 | m_pop3->pop3_response ); |
291 | mailpop3_free( m_pop3 ); | 342 | mailpop3_free( m_pop3 ); |
292 | m_pop3 = NULL; | 343 | m_pop3 = NULL; |
293 | return; | 344 | return; |
294 | } | 345 | } |
295 | qDebug( "POP3: connected!" ); | 346 | qDebug( "POP3: connected!" ); |
296 | 347 | ||
297 | // login | 348 | // login |
298 | // TODO: decide if apop or plain login should be used | 349 | // TODO: decide if apop or plain login should be used |
299 | err = mailpop3_login( m_pop3, (char *) user, (char *) pass ); | 350 | err = mailpop3_login( m_pop3, (char *) user, (char *) pass ); |
300 | if ( err != MAILPOP3_NO_ERROR ) { | 351 | if ( err != MAILPOP3_NO_ERROR ) { |
301 | qDebug( "pop3: error logging in: %s", m_pop3->pop3_response ); | 352 | qDebug( "pop3: error logging in: %s", m_pop3->pop3_response ); |
302 | logout(); | 353 | logout(); |
303 | return; | 354 | return; |
304 | } | 355 | } |
305 | 356 | ||
306 | qDebug( "POP3: logged in!" ); | 357 | qDebug( "POP3: logged in!" ); |
307 | } | 358 | } |
308 | 359 | ||
309 | void POP3wrapper::logout() | 360 | void POP3wrapper::logout() |
310 | { | 361 | { |
311 | int err = MAILPOP3_NO_ERROR; | 362 | int err = MAILPOP3_NO_ERROR; |
312 | if ( m_pop3 == NULL ) return; | 363 | if ( m_pop3 == NULL ) return; |
313 | err = mailpop3_quit( m_pop3 ); | 364 | err = mailpop3_quit( m_pop3 ); |
314 | mailpop3_free( m_pop3 ); | 365 | mailpop3_free( m_pop3 ); |
315 | m_pop3 = NULL; | 366 | m_pop3 = NULL; |
diff --git a/noncore/net/mail/libmailwrapper/pop3wrapper.h b/noncore/net/mail/libmailwrapper/pop3wrapper.h index 8d3adda..a05021c 100644 --- a/noncore/net/mail/libmailwrapper/pop3wrapper.h +++ b/noncore/net/mail/libmailwrapper/pop3wrapper.h | |||
@@ -1,48 +1,49 @@ | |||
1 | #ifndef __POP3WRAPPER | 1 | #ifndef __POP3WRAPPER |
2 | #define __POP3WRAPPER | 2 | #define __POP3WRAPPER |
3 | 3 | ||
4 | #include "mailwrapper.h" | 4 | #include "mailwrapper.h" |
5 | #include "abstractmail.h" | 5 | #include "abstractmail.h" |
6 | 6 | ||
7 | class RecMail; | 7 | class RecMail; |
8 | class RecBody; | 8 | class RecBody; |
9 | class encodedString; | 9 | class encodedString; |
10 | struct mailpop3; | 10 | struct mailpop3; |
11 | 11 | ||
12 | class POP3wrapper : public AbstractMail | 12 | class POP3wrapper : public AbstractMail |
13 | { | 13 | { |
14 | Q_OBJECT | 14 | Q_OBJECT |
15 | 15 | ||
16 | public: | 16 | public: |
17 | POP3wrapper( POP3account *a ); | 17 | POP3wrapper( POP3account *a ); |
18 | virtual ~POP3wrapper(); | 18 | virtual ~POP3wrapper(); |
19 | /* mailbox will be ignored */ | 19 | /* mailbox will be ignored */ |
20 | virtual void listMessages(const QString & mailbox, QList<RecMail> &target ); | 20 | virtual void listMessages(const QString & mailbox, QList<RecMail> &target ); |
21 | virtual QList<Folder>* listFolders(); | 21 | virtual QList<Folder>* listFolders(); |
22 | virtual QString fetchTextPart(const RecMail&mail,const RecPart&part); | 22 | virtual QString fetchTextPart(const RecMail&mail,const RecPart&part); |
23 | virtual encodedString* fetchDecodedPart(const RecMail&mail,const RecPart&part); | 23 | virtual encodedString* fetchDecodedPart(const RecMail&mail,const RecPart&part); |
24 | virtual encodedString* fetchRawPart(const RecMail&mail,const RecPart&part); | 24 | virtual encodedString* fetchRawPart(const RecMail&mail,const RecPart&part); |
25 | 25 | ||
26 | virtual void deleteMail(const RecMail&mail); | 26 | virtual void deleteMail(const RecMail&mail); |
27 | virtual void answeredMail(const RecMail&mail); | 27 | virtual void answeredMail(const RecMail&mail); |
28 | 28 | ||
29 | RecBody fetchBody( const RecMail &mail ); | 29 | RecBody fetchBody( const RecMail &mail ); |
30 | static void pop3_progress( size_t current, size_t maximum ); | 30 | static void pop3_progress( size_t current, size_t maximum ); |
31 | 31 | ||
32 | protected: | 32 | protected: |
33 | void login(); | 33 | void login(); |
34 | void logout(); | 34 | void logout(); |
35 | 35 | ||
36 | private: | ||
37 | RecMail *parseHeader( const char *header ); | 36 | RecMail *parseHeader( const char *header ); |
38 | RecBody parseBody( const char *message ); | 37 | RecBody parseMail( char *message ); |
39 | QString parseMailboxList( mailimf_mailbox_list *list ); | 38 | QString parseMailboxList( mailimf_mailbox_list *list ); |
40 | QString parseMailbox( mailimf_mailbox *box ); | 39 | QString parseMailbox( mailimf_mailbox *box ); |
41 | QString parseGroup( mailimf_group *group ); | 40 | QString parseGroup( mailimf_group *group ); |
42 | QString parseAddressList( mailimf_address_list *list ); | 41 | QString parseAddressList( mailimf_address_list *list ); |
43 | QString parseDateTime( mailimf_date_time *date ); | 42 | QString parseDateTime( mailimf_date_time *date ); |
44 | POP3account *account; | 43 | POP3account *account; |
45 | mailpop3 *m_pop3; | 44 | mailpop3 *m_pop3; |
45 | QString msgTempName; | ||
46 | unsigned int last_msg_id; | ||
46 | }; | 47 | }; |
47 | 48 | ||
48 | #endif | 49 | #endif |
diff --git a/noncore/net/mail/pop3wrapper.cpp b/noncore/net/mail/pop3wrapper.cpp index 4508a95..30f80ff 100644 --- a/noncore/net/mail/pop3wrapper.cpp +++ b/noncore/net/mail/pop3wrapper.cpp | |||
@@ -1,315 +1,366 @@ | |||
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 | #include <libetpan/mailmime.h> | ||
6 | #include <qfile.h> | ||
5 | 7 | ||
6 | POP3wrapper::POP3wrapper( POP3account *a ) | 8 | POP3wrapper::POP3wrapper( POP3account *a ) |
7 | { | 9 | { |
8 | account = a; | 10 | account = a; |
9 | m_pop3 = NULL; | 11 | m_pop3 = NULL; |
12 | msgTempName = a->getFileName()+"_msg_cache"; | ||
13 | last_msg_id = 0; | ||
10 | } | 14 | } |
11 | 15 | ||
12 | POP3wrapper::~POP3wrapper() | 16 | POP3wrapper::~POP3wrapper() |
13 | { | 17 | { |
14 | logout(); | 18 | logout(); |
19 | QFile msg_cache(msgTempName); | ||
20 | if (msg_cache.exists()) { | ||
21 | msg_cache.remove(); | ||
22 | } | ||
15 | } | 23 | } |
16 | 24 | ||
17 | void POP3wrapper::pop3_progress( size_t current, size_t maximum ) | 25 | void POP3wrapper::pop3_progress( size_t current, size_t maximum ) |
18 | { | 26 | { |
19 | //qDebug( "POP3: %i of %i", current, maximum ); | 27 | //qDebug( "POP3: %i of %i", current, maximum ); |
20 | } | 28 | } |
21 | 29 | ||
22 | RecBody POP3wrapper::fetchBody( const RecMail &mail ) | 30 | RecBody POP3wrapper::fetchBody( const RecMail &mail ) |
23 | { | 31 | { |
24 | int err = MAILPOP3_NO_ERROR; | 32 | int err = MAILPOP3_NO_ERROR; |
25 | char *message; | 33 | char *message; |
26 | size_t length = 0; | 34 | size_t length = 0; |
27 | 35 | ||
28 | login(); | 36 | login(); |
29 | if ( !m_pop3 ) return RecBody(); | 37 | if ( !m_pop3 ) { |
30 | |||
31 | err = mailpop3_retr( m_pop3, mail.getNumber(), &message, &length ); | ||
32 | if ( err != MAILPOP3_NO_ERROR ) { | ||
33 | qDebug( "POP3: error retrieving body with index %i", mail.getNumber() ); | ||
34 | return RecBody(); | 38 | return RecBody(); |
35 | } | 39 | } |
40 | RecBody body; | ||
36 | 41 | ||
37 | return parseBody( message ); | 42 | QFile msg_cache(msgTempName); |
43 | |||
44 | if (mail.getNumber()!=last_msg_id) { | ||
45 | if (msg_cache.exists()) { | ||
46 | msg_cache.remove(); | ||
47 | } | ||
48 | msg_cache.open(IO_ReadWrite|IO_Truncate); | ||
49 | last_msg_id = mail.getNumber(); | ||
50 | err = mailpop3_retr( m_pop3, mail.getNumber(), &message, &length ); | ||
51 | if ( err != MAILPOP3_NO_ERROR ) { | ||
52 | qDebug( "POP3: error retrieving body with index %i", mail.getNumber() ); | ||
53 | last_msg_id = 0; | ||
54 | return RecBody(); | ||
55 | } | ||
56 | msg_cache.writeBlock(message,length); | ||
57 | } else { | ||
58 | QString msg=""; | ||
59 | msg_cache.open(IO_ReadOnly); | ||
60 | message = new char[4096]; | ||
61 | memset(message,0,4096); | ||
62 | while (msg_cache.readBlock(message,4095)>0) { | ||
63 | msg+=message; | ||
64 | memset(message,0,4096); | ||
65 | } | ||
66 | delete message; | ||
67 | message = (char*)malloc(msg.length()+1*sizeof(char)); | ||
68 | memset(message,0,msg.length()+1); | ||
69 | memcpy(message,msg.latin1(),msg.length()); | ||
70 | } | ||
71 | body = parseMail(message); | ||
72 | free(message); | ||
73 | return body; | ||
38 | } | 74 | } |
39 | 75 | ||
40 | RecBody POP3wrapper::parseBody( const char *message ) | 76 | RecBody POP3wrapper::parseMail( char *message ) |
41 | { | 77 | { |
42 | int err = MAILIMF_NO_ERROR; | 78 | int err = MAILIMF_NO_ERROR; |
43 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ | 79 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ |
44 | size_t curTok = 0; | 80 | size_t curTok = 0; |
45 | mailimf_message *result = 0; | 81 | mailimf_message *result = 0; |
46 | RecBody body; | 82 | RecBody body; |
47 | 83 | ||
84 | |||
48 | err = mailimf_message_parse( (char *) message, strlen( message ), &curTok, &result ); | 85 | err = mailimf_message_parse( (char *) message, strlen( message ), &curTok, &result ); |
49 | if ( err != MAILIMF_NO_ERROR ) { | 86 | if ( err != MAILIMF_NO_ERROR ) { |
50 | if (result) mailimf_message_free(result); | 87 | if (result) mailimf_message_free(result); |
51 | return body; | 88 | return body; |
52 | } | 89 | } |
53 | 90 | ||
91 | struct mailimf_body * b = 0; | ||
92 | struct mailimf_fields * f = 0; | ||
93 | |||
94 | |||
54 | if ( result && result->msg_body && result->msg_body->bd_text ) { | 95 | if ( result && result->msg_body && result->msg_body->bd_text ) { |
55 | qDebug( "POP3: bodytext found" ); | 96 | qDebug( "POP3: bodytext found" ); |
56 | // when curTok isn't set to 0 this line will fault! 'cause upper line faults! | 97 | // when curTok isn't set to 0 this line will fault! 'cause upper line faults! |
57 | body.setBodytext( QString( result->msg_body->bd_text ) ); | 98 | body.setBodytext( QString( result->msg_body->bd_text ) ); |
99 | #if 0 | ||
100 | curTok = 0; | ||
101 | mailmime_content*mresult = 0; | ||
102 | size_t index = 0; | ||
103 | mailmime_content_parse(result->msg_body->bd_text, | ||
104 | strlen(result->msg_body->bd_text),&index,&mresult); | ||
105 | if (mresult) { | ||
106 | mailmime_content_free(mresult); | ||
107 | } | ||
108 | #endif | ||
109 | mailimf_message_free(result); | ||
58 | } | 110 | } |
59 | if (result) mailimf_message_free(result); | ||
60 | return body; | 111 | return body; |
61 | } | 112 | } |
62 | 113 | ||
63 | void POP3wrapper::listMessages(const QString &, QList<RecMail> &target ) | 114 | void POP3wrapper::listMessages(const QString &, QList<RecMail> &target ) |
64 | { | 115 | { |
65 | int err = MAILPOP3_NO_ERROR; | 116 | int err = MAILPOP3_NO_ERROR; |
66 | char * header = 0; | 117 | char * header = 0; |
67 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ | 118 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ |
68 | size_t length = 0; | 119 | size_t length = 0; |
69 | carray * messages = 0; | 120 | carray * messages = 0; |
70 | 121 | ||
71 | login(); | 122 | login(); |
72 | if (!m_pop3) return; | 123 | if (!m_pop3) return; |
73 | mailpop3_list( m_pop3, &messages ); | 124 | mailpop3_list( m_pop3, &messages ); |
74 | 125 | ||
75 | for (unsigned int i = 0; i < carray_count(messages);++i) { | 126 | for (unsigned int i = 0; i < carray_count(messages);++i) { |
76 | mailpop3_msg_info *info; | 127 | mailpop3_msg_info *info; |
77 | err = mailpop3_get_msg_info(m_pop3,i+1,&info); | 128 | err = mailpop3_get_msg_info(m_pop3,i+1,&info); |
78 | if (info->msg_deleted) | 129 | if (info->msg_deleted) |
79 | continue; | 130 | continue; |
80 | err = mailpop3_header( m_pop3, info->msg_index, &header, &length ); | 131 | err = mailpop3_header( m_pop3, info->msg_index, &header, &length ); |
81 | if ( err != MAILPOP3_NO_ERROR ) { | 132 | if ( err != MAILPOP3_NO_ERROR ) { |
82 | qDebug( "POP3: error retrieving header msgid: %i", info->msg_index ); | 133 | qDebug( "POP3: error retrieving header msgid: %i", info->msg_index ); |
83 | free(header); | 134 | free(header); |
84 | return; | 135 | return; |
85 | } | 136 | } |
86 | RecMail *mail = parseHeader( header ); | 137 | RecMail *mail = parseHeader( header ); |
87 | mail->setNumber( info->msg_index ); | 138 | mail->setNumber( info->msg_index ); |
88 | mail->setWrapper(this); | 139 | mail->setWrapper(this); |
89 | mail->setMsgsize(info->msg_size); | 140 | mail->setMsgsize(info->msg_size); |
90 | target.append( mail ); | 141 | target.append( mail ); |
91 | free(header); | 142 | free(header); |
92 | } | 143 | } |
93 | } | 144 | } |
94 | 145 | ||
95 | RecMail *POP3wrapper::parseHeader( const char *header ) | 146 | RecMail *POP3wrapper::parseHeader( const char *header ) |
96 | { | 147 | { |
97 | int err = MAILIMF_NO_ERROR; | 148 | int err = MAILIMF_NO_ERROR; |
98 | size_t curTok = 0; | 149 | size_t curTok = 0; |
99 | RecMail *mail = new RecMail(); | 150 | RecMail *mail = new RecMail(); |
100 | mailimf_fields *fields; | 151 | mailimf_fields *fields; |
101 | mailimf_references * refs; | 152 | mailimf_references * refs; |
102 | mailimf_keywords*keys; | 153 | mailimf_keywords*keys; |
103 | QString status; | 154 | QString status; |
104 | QString value; | 155 | QString value; |
105 | QBitArray mFlags(7); | 156 | QBitArray mFlags(7); |
106 | 157 | ||
107 | err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields ); | 158 | err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields ); |
108 | for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) { | 159 | for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) { |
109 | mailimf_field *field = (mailimf_field *) current->data; | 160 | mailimf_field *field = (mailimf_field *) current->data; |
110 | switch ( field->fld_type ) { | 161 | switch ( field->fld_type ) { |
111 | case MAILIMF_FIELD_FROM: | 162 | case MAILIMF_FIELD_FROM: |
112 | mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) ); | 163 | mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) ); |
113 | break; | 164 | break; |
114 | case MAILIMF_FIELD_TO: | 165 | case MAILIMF_FIELD_TO: |
115 | mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) ); | 166 | mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) ); |
116 | break; | 167 | break; |
117 | case MAILIMF_FIELD_CC: | 168 | case MAILIMF_FIELD_CC: |
118 | mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) ); | 169 | mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) ); |
119 | break; | 170 | break; |
120 | case MAILIMF_FIELD_BCC: | 171 | case MAILIMF_FIELD_BCC: |
121 | mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) ); | 172 | mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) ); |
122 | break; | 173 | break; |
123 | case MAILIMF_FIELD_SUBJECT: | 174 | case MAILIMF_FIELD_SUBJECT: |
124 | mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) ); | 175 | mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) ); |
125 | break; | 176 | break; |
126 | case MAILIMF_FIELD_ORIG_DATE: | 177 | case MAILIMF_FIELD_ORIG_DATE: |
127 | mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) ); | 178 | mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) ); |
128 | break; | 179 | break; |
129 | case MAILIMF_FIELD_MESSAGE_ID: | 180 | case MAILIMF_FIELD_MESSAGE_ID: |
130 | mail->setMsgid(QString(field->fld_data.fld_message_id->mid_value)); | 181 | mail->setMsgid(QString(field->fld_data.fld_message_id->mid_value)); |
131 | break; | 182 | break; |
132 | case MAILIMF_FIELD_REFERENCES: | 183 | case MAILIMF_FIELD_REFERENCES: |
133 | refs = field->fld_data.fld_references; | 184 | refs = field->fld_data.fld_references; |
134 | if (refs && refs->mid_list && clist_count(refs->mid_list)) { | 185 | if (refs && refs->mid_list && clist_count(refs->mid_list)) { |
135 | char * text = (char*)refs->mid_list->first->data; | 186 | char * text = (char*)refs->mid_list->first->data; |
136 | mail->setReplyto(QString(text)); | 187 | mail->setReplyto(QString(text)); |
137 | } | 188 | } |
138 | break; | 189 | break; |
139 | case MAILIMF_FIELD_KEYWORDS: | 190 | case MAILIMF_FIELD_KEYWORDS: |
140 | keys = field->fld_data.fld_keywords; | 191 | keys = field->fld_data.fld_keywords; |
141 | for (clistcell*cur = clist_begin(keys->kw_list);cur!=0;cur=clist_next(cur)) { | 192 | for (clistcell*cur = clist_begin(keys->kw_list);cur!=0;cur=clist_next(cur)) { |
142 | qDebug("Keyword: %s",(char*)cur->data); | 193 | qDebug("Keyword: %s",(char*)cur->data); |
143 | } | 194 | } |
144 | break; | 195 | break; |
145 | case MAILIMF_FIELD_OPTIONAL_FIELD: | 196 | case MAILIMF_FIELD_OPTIONAL_FIELD: |
146 | status = field->fld_data.fld_optional_field->fld_name; | 197 | status = field->fld_data.fld_optional_field->fld_name; |
147 | value = field->fld_data.fld_optional_field->fld_value; | 198 | value = field->fld_data.fld_optional_field->fld_value; |
148 | if (status.lower()=="status") { | 199 | if (status.lower()=="status") { |
149 | if (value.lower()=="ro") { | 200 | if (value.lower()=="ro") { |
150 | mFlags.setBit(FLAG_SEEN); | 201 | mFlags.setBit(FLAG_SEEN); |
151 | } | 202 | } |
152 | } else if (status.lower()=="x-status") { | 203 | } else if (status.lower()=="x-status") { |
153 | qDebug("X-Status: %s",value.latin1()); | 204 | qDebug("X-Status: %s",value.latin1()); |
154 | if (value.lower()=="a") { | 205 | if (value.lower()=="a") { |
155 | 206 | ||
156 | mFlags.setBit(FLAG_ANSWERED); | 207 | mFlags.setBit(FLAG_ANSWERED); |
157 | } | 208 | } |
158 | } else { | 209 | } else { |
159 | // qDebug("Optionales feld: %s -> %s)",field->fld_data.fld_optional_field->fld_name, | 210 | // qDebug("Optionales feld: %s -> %s)",field->fld_data.fld_optional_field->fld_name, |
160 | // field->fld_data.fld_optional_field->fld_value); | 211 | // field->fld_data.fld_optional_field->fld_value); |
161 | } | 212 | } |
162 | break; | 213 | break; |
163 | default: | 214 | default: |
164 | qDebug("Non parsed field"); | 215 | qDebug("Non parsed field"); |
165 | break; | 216 | break; |
166 | } | 217 | } |
167 | } | 218 | } |
168 | if (fields) mailimf_fields_free(fields); | 219 | if (fields) mailimf_fields_free(fields); |
169 | mail->setFlags(mFlags); | 220 | mail->setFlags(mFlags); |
170 | return mail; | 221 | return mail; |
171 | } | 222 | } |
172 | 223 | ||
173 | QString POP3wrapper::parseDateTime( mailimf_date_time *date ) | 224 | QString POP3wrapper::parseDateTime( mailimf_date_time *date ) |
174 | { | 225 | { |
175 | char tmp[23]; | 226 | char tmp[23]; |
176 | 227 | ||
177 | snprintf( tmp, 23, "%02i.%02i.%04i %02i:%02i:%02i %+05i", | 228 | snprintf( tmp, 23, "%02i.%02i.%04i %02i:%02i:%02i %+05i", |
178 | date->dt_day, date->dt_month, date->dt_year, date->dt_hour, date->dt_min, date->dt_sec, date->dt_zone ); | 229 | date->dt_day, date->dt_month, date->dt_year, date->dt_hour, date->dt_min, date->dt_sec, date->dt_zone ); |
179 | 230 | ||
180 | return QString( tmp ); | 231 | return QString( tmp ); |
181 | } | 232 | } |
182 | 233 | ||
183 | QString POP3wrapper::parseAddressList( mailimf_address_list *list ) | 234 | QString POP3wrapper::parseAddressList( mailimf_address_list *list ) |
184 | { | 235 | { |
185 | QString result( "" ); | 236 | QString result( "" ); |
186 | 237 | ||
187 | bool first = true; | 238 | bool first = true; |
188 | for ( clistiter *current = clist_begin( list->ad_list ); current != NULL; current = current->next ) { | 239 | for ( clistiter *current = clist_begin( list->ad_list ); current != NULL; current = current->next ) { |
189 | mailimf_address *addr = (mailimf_address *) current->data; | 240 | mailimf_address *addr = (mailimf_address *) current->data; |
190 | 241 | ||
191 | if ( !first ) { | 242 | if ( !first ) { |
192 | result.append( "," ); | 243 | result.append( "," ); |
193 | } else { | 244 | } else { |
194 | first = false; | 245 | first = false; |
195 | } | 246 | } |
196 | 247 | ||
197 | switch ( addr->ad_type ) { | 248 | switch ( addr->ad_type ) { |
198 | case MAILIMF_ADDRESS_MAILBOX: | 249 | case MAILIMF_ADDRESS_MAILBOX: |
199 | result.append( parseMailbox( addr->ad_data.ad_mailbox ) ); | 250 | result.append( parseMailbox( addr->ad_data.ad_mailbox ) ); |
200 | break; | 251 | break; |
201 | case MAILIMF_ADDRESS_GROUP: | 252 | case MAILIMF_ADDRESS_GROUP: |
202 | result.append( parseGroup( addr->ad_data.ad_group ) ); | 253 | result.append( parseGroup( addr->ad_data.ad_group ) ); |
203 | break; | 254 | break; |
204 | default: | 255 | default: |
205 | qDebug( "POP3: unkown mailimf address type" ); | 256 | qDebug( "POP3: unkown mailimf address type" ); |
206 | break; | 257 | break; |
207 | } | 258 | } |
208 | } | 259 | } |
209 | 260 | ||
210 | return result; | 261 | return result; |
211 | } | 262 | } |
212 | 263 | ||
213 | QString POP3wrapper::parseGroup( mailimf_group *group ) | 264 | QString POP3wrapper::parseGroup( mailimf_group *group ) |
214 | { | 265 | { |
215 | QString result( "" ); | 266 | QString result( "" ); |
216 | 267 | ||
217 | result.append( group->grp_display_name ); | 268 | result.append( group->grp_display_name ); |
218 | result.append( ": " ); | 269 | result.append( ": " ); |
219 | 270 | ||
220 | if ( group->grp_mb_list != NULL ) { | 271 | if ( group->grp_mb_list != NULL ) { |
221 | result.append( parseMailboxList( group->grp_mb_list ) ); | 272 | result.append( parseMailboxList( group->grp_mb_list ) ); |
222 | } | 273 | } |
223 | 274 | ||
224 | result.append( ";" ); | 275 | result.append( ";" ); |
225 | 276 | ||
226 | return result; | 277 | return result; |
227 | } | 278 | } |
228 | 279 | ||
229 | QString POP3wrapper::parseMailbox( mailimf_mailbox *box ) | 280 | QString POP3wrapper::parseMailbox( mailimf_mailbox *box ) |
230 | { | 281 | { |
231 | QString result( "" ); | 282 | QString result( "" ); |
232 | 283 | ||
233 | if ( box->mb_display_name == NULL ) { | 284 | if ( box->mb_display_name == NULL ) { |
234 | result.append( box->mb_addr_spec ); | 285 | result.append( box->mb_addr_spec ); |
235 | } else { | 286 | } else { |
236 | result.append( box->mb_display_name ); | 287 | result.append( box->mb_display_name ); |
237 | result.append( " <" ); | 288 | result.append( " <" ); |
238 | result.append( box->mb_addr_spec ); | 289 | result.append( box->mb_addr_spec ); |
239 | result.append( ">" ); | 290 | result.append( ">" ); |
240 | } | 291 | } |
241 | 292 | ||
242 | return result; | 293 | return result; |
243 | } | 294 | } |
244 | 295 | ||
245 | QString POP3wrapper::parseMailboxList( mailimf_mailbox_list *list ) | 296 | QString POP3wrapper::parseMailboxList( mailimf_mailbox_list *list ) |
246 | { | 297 | { |
247 | QString result( "" ); | 298 | QString result( "" ); |
248 | 299 | ||
249 | bool first = true; | 300 | bool first = true; |
250 | for ( clistiter *current = clist_begin( list->mb_list ); current != NULL; current = current->next ) { | 301 | for ( clistiter *current = clist_begin( list->mb_list ); current != NULL; current = current->next ) { |
251 | mailimf_mailbox *box = (mailimf_mailbox *) current->data; | 302 | mailimf_mailbox *box = (mailimf_mailbox *) current->data; |
252 | 303 | ||
253 | if ( !first ) { | 304 | if ( !first ) { |
254 | result.append( "," ); | 305 | result.append( "," ); |
255 | } else { | 306 | } else { |
256 | first = false; | 307 | first = false; |
257 | } | 308 | } |
258 | 309 | ||
259 | result.append( parseMailbox( box ) ); | 310 | result.append( parseMailbox( box ) ); |
260 | } | 311 | } |
261 | 312 | ||
262 | return result; | 313 | return result; |
263 | } | 314 | } |
264 | 315 | ||
265 | void POP3wrapper::login() | 316 | void POP3wrapper::login() |
266 | { | 317 | { |
267 | /* we'll hold the line */ | 318 | /* we'll hold the line */ |
268 | if ( m_pop3 != NULL ) return; | 319 | if ( m_pop3 != NULL ) return; |
269 | 320 | ||
270 | const char *server, *user, *pass; | 321 | const char *server, *user, *pass; |
271 | uint16_t port; | 322 | uint16_t port; |
272 | int err = MAILPOP3_NO_ERROR; | 323 | int err = MAILPOP3_NO_ERROR; |
273 | 324 | ||
274 | server = account->getServer().latin1(); | 325 | server = account->getServer().latin1(); |
275 | port = account->getPort().toUInt(); | 326 | port = account->getPort().toUInt(); |
276 | user = account->getUser().latin1(); | 327 | user = account->getUser().latin1(); |
277 | pass = account->getPassword().latin1(); | 328 | pass = account->getPassword().latin1(); |
278 | 329 | ||
279 | m_pop3 = mailpop3_new( 200, &pop3_progress ); | 330 | m_pop3 = mailpop3_new( 200, &pop3_progress ); |
280 | 331 | ||
281 | // connect | 332 | // connect |
282 | if (account->getSSL()) { | 333 | if (account->getSSL()) { |
283 | err = mailpop3_ssl_connect( m_pop3, (char*)server, port ); | 334 | err = mailpop3_ssl_connect( m_pop3, (char*)server, port ); |
284 | } else { | 335 | } else { |
285 | err = mailpop3_socket_connect( m_pop3, (char*)server, port ); | 336 | err = mailpop3_socket_connect( m_pop3, (char*)server, port ); |
286 | } | 337 | } |
287 | 338 | ||
288 | if ( err != MAILPOP3_NO_ERROR ) { | 339 | if ( err != MAILPOP3_NO_ERROR ) { |
289 | qDebug( "pop3: error connecting to %s\n reason: %s", server, | 340 | qDebug( "pop3: error connecting to %s\n reason: %s", server, |
290 | m_pop3->pop3_response ); | 341 | m_pop3->pop3_response ); |
291 | mailpop3_free( m_pop3 ); | 342 | mailpop3_free( m_pop3 ); |
292 | m_pop3 = NULL; | 343 | m_pop3 = NULL; |
293 | return; | 344 | return; |
294 | } | 345 | } |
295 | qDebug( "POP3: connected!" ); | 346 | qDebug( "POP3: connected!" ); |
296 | 347 | ||
297 | // login | 348 | // login |
298 | // TODO: decide if apop or plain login should be used | 349 | // TODO: decide if apop or plain login should be used |
299 | err = mailpop3_login( m_pop3, (char *) user, (char *) pass ); | 350 | err = mailpop3_login( m_pop3, (char *) user, (char *) pass ); |
300 | if ( err != MAILPOP3_NO_ERROR ) { | 351 | if ( err != MAILPOP3_NO_ERROR ) { |
301 | qDebug( "pop3: error logging in: %s", m_pop3->pop3_response ); | 352 | qDebug( "pop3: error logging in: %s", m_pop3->pop3_response ); |
302 | logout(); | 353 | logout(); |
303 | return; | 354 | return; |
304 | } | 355 | } |
305 | 356 | ||
306 | qDebug( "POP3: logged in!" ); | 357 | qDebug( "POP3: logged in!" ); |
307 | } | 358 | } |
308 | 359 | ||
309 | void POP3wrapper::logout() | 360 | void POP3wrapper::logout() |
310 | { | 361 | { |
311 | int err = MAILPOP3_NO_ERROR; | 362 | int err = MAILPOP3_NO_ERROR; |
312 | if ( m_pop3 == NULL ) return; | 363 | if ( m_pop3 == NULL ) return; |
313 | err = mailpop3_quit( m_pop3 ); | 364 | err = mailpop3_quit( m_pop3 ); |
314 | mailpop3_free( m_pop3 ); | 365 | mailpop3_free( m_pop3 ); |
315 | m_pop3 = NULL; | 366 | m_pop3 = NULL; |
diff --git a/noncore/net/mail/pop3wrapper.h b/noncore/net/mail/pop3wrapper.h index 8d3adda..a05021c 100644 --- a/noncore/net/mail/pop3wrapper.h +++ b/noncore/net/mail/pop3wrapper.h | |||
@@ -1,48 +1,49 @@ | |||
1 | #ifndef __POP3WRAPPER | 1 | #ifndef __POP3WRAPPER |
2 | #define __POP3WRAPPER | 2 | #define __POP3WRAPPER |
3 | 3 | ||
4 | #include "mailwrapper.h" | 4 | #include "mailwrapper.h" |
5 | #include "abstractmail.h" | 5 | #include "abstractmail.h" |
6 | 6 | ||
7 | class RecMail; | 7 | class RecMail; |
8 | class RecBody; | 8 | class RecBody; |
9 | class encodedString; | 9 | class encodedString; |
10 | struct mailpop3; | 10 | struct mailpop3; |
11 | 11 | ||
12 | class POP3wrapper : public AbstractMail | 12 | class POP3wrapper : public AbstractMail |
13 | { | 13 | { |
14 | Q_OBJECT | 14 | Q_OBJECT |
15 | 15 | ||
16 | public: | 16 | public: |
17 | POP3wrapper( POP3account *a ); | 17 | POP3wrapper( POP3account *a ); |
18 | virtual ~POP3wrapper(); | 18 | virtual ~POP3wrapper(); |
19 | /* mailbox will be ignored */ | 19 | /* mailbox will be ignored */ |
20 | virtual void listMessages(const QString & mailbox, QList<RecMail> &target ); | 20 | virtual void listMessages(const QString & mailbox, QList<RecMail> &target ); |
21 | virtual QList<Folder>* listFolders(); | 21 | virtual QList<Folder>* listFolders(); |
22 | virtual QString fetchTextPart(const RecMail&mail,const RecPart&part); | 22 | virtual QString fetchTextPart(const RecMail&mail,const RecPart&part); |
23 | virtual encodedString* fetchDecodedPart(const RecMail&mail,const RecPart&part); | 23 | virtual encodedString* fetchDecodedPart(const RecMail&mail,const RecPart&part); |
24 | virtual encodedString* fetchRawPart(const RecMail&mail,const RecPart&part); | 24 | virtual encodedString* fetchRawPart(const RecMail&mail,const RecPart&part); |
25 | 25 | ||
26 | virtual void deleteMail(const RecMail&mail); | 26 | virtual void deleteMail(const RecMail&mail); |
27 | virtual void answeredMail(const RecMail&mail); | 27 | virtual void answeredMail(const RecMail&mail); |
28 | 28 | ||
29 | RecBody fetchBody( const RecMail &mail ); | 29 | RecBody fetchBody( const RecMail &mail ); |
30 | static void pop3_progress( size_t current, size_t maximum ); | 30 | static void pop3_progress( size_t current, size_t maximum ); |
31 | 31 | ||
32 | protected: | 32 | protected: |
33 | void login(); | 33 | void login(); |
34 | void logout(); | 34 | void logout(); |
35 | 35 | ||
36 | private: | ||
37 | RecMail *parseHeader( const char *header ); | 36 | RecMail *parseHeader( const char *header ); |
38 | RecBody parseBody( const char *message ); | 37 | RecBody parseMail( char *message ); |
39 | QString parseMailboxList( mailimf_mailbox_list *list ); | 38 | QString parseMailboxList( mailimf_mailbox_list *list ); |
40 | QString parseMailbox( mailimf_mailbox *box ); | 39 | QString parseMailbox( mailimf_mailbox *box ); |
41 | QString parseGroup( mailimf_group *group ); | 40 | QString parseGroup( mailimf_group *group ); |
42 | QString parseAddressList( mailimf_address_list *list ); | 41 | QString parseAddressList( mailimf_address_list *list ); |
43 | QString parseDateTime( mailimf_date_time *date ); | 42 | QString parseDateTime( mailimf_date_time *date ); |
44 | POP3account *account; | 43 | POP3account *account; |
45 | mailpop3 *m_pop3; | 44 | mailpop3 *m_pop3; |
45 | QString msgTempName; | ||
46 | unsigned int last_msg_id; | ||
46 | }; | 47 | }; |
47 | 48 | ||
48 | #endif | 49 | #endif |