Diffstat (limited to 'noncore/net/mail/libmailwrapper/pop3wrapper.cpp') (more/less context) (show whitespace changes)
-rw-r--r-- | noncore/net/mail/libmailwrapper/pop3wrapper.cpp | 111 |
1 files changed, 69 insertions, 42 deletions
diff --git a/noncore/net/mail/libmailwrapper/pop3wrapper.cpp b/noncore/net/mail/libmailwrapper/pop3wrapper.cpp index 633faf6..abb5a42 100644 --- a/noncore/net/mail/libmailwrapper/pop3wrapper.cpp +++ b/noncore/net/mail/libmailwrapper/pop3wrapper.cpp | |||
@@ -10,24 +10,64 @@ POP3wrapper::POP3wrapper( POP3account *a ) | |||
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 ) | ||
23 | { | ||
24 | int err = MAILPOP3_NO_ERROR; | ||
25 | char *message; | ||
26 | size_t length; | ||
27 | |||
28 | login(); | ||
29 | if ( !m_pop3 ) return RecBody(); | ||
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 | logout(); | ||
35 | return RecBody(); | ||
36 | } | ||
37 | |||
38 | logout(); | ||
39 | |||
40 | return parseBody( message ); | ||
41 | } | ||
42 | |||
43 | RecBody POP3wrapper::parseBody( const char *message ) | ||
44 | { | ||
45 | int err = MAILIMF_NO_ERROR; | ||
46 | size_t curTok; | ||
47 | mailimf_message *result; | ||
48 | RecBody body; | ||
49 | |||
50 | err = mailimf_message_parse( (char *) message, strlen( message ), &curTok, &result ); | ||
51 | if ( err != MAILIMF_NO_ERROR ) return body; | ||
52 | |||
53 | if ( result && result->msg_body && result->msg_body->bd_text ) { | ||
54 | qDebug( "POP3: bodytext found" ); | ||
55 | // TODO: why does this line segfault???? gdb says segfault in strlen(), maybe a bug in libetpan. | ||
56 | body.setBodytext( QString( result->msg_body->bd_text ) ); | ||
57 | } | ||
58 | |||
59 | return body; | ||
60 | } | ||
61 | |||
22 | void POP3wrapper::listMessages( QList<RecMail> &target ) | 62 | void POP3wrapper::listMessages( QList<RecMail> &target ) |
23 | { | 63 | { |
24 | int err = MAILPOP3_NO_ERROR; | 64 | int err = MAILPOP3_NO_ERROR; |
25 | char *header; | 65 | char *header; |
26 | size_t length; | 66 | size_t length; |
27 | carray *messages; | 67 | carray *messages; |
28 | 68 | ||
29 | login(); | 69 | login(); |
30 | if (!m_pop3) return; | 70 | if (!m_pop3) return; |
31 | mailpop3_list( m_pop3, &messages ); | 71 | mailpop3_list( m_pop3, &messages ); |
32 | 72 | ||
33 | for ( int i = carray_count( messages ); i > 0; i-- ) { | 73 | for ( int i = carray_count( messages ); i > 0; i-- ) { |
@@ -50,148 +90,136 @@ void POP3wrapper::listMessages( QList<RecMail> &target ) | |||
50 | RecMail *POP3wrapper::parseHeader( const char *header ) | 90 | RecMail *POP3wrapper::parseHeader( const char *header ) |
51 | { | 91 | { |
52 | int err = MAILIMF_NO_ERROR; | 92 | int err = MAILIMF_NO_ERROR; |
53 | size_t curTok; | 93 | size_t curTok; |
54 | RecMail *mail = new RecMail(); | 94 | RecMail *mail = new RecMail(); |
55 | mailimf_fields *fields; | 95 | mailimf_fields *fields; |
56 | 96 | ||
57 | err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields ); | 97 | err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields ); |
58 | for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) { | 98 | for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) { |
59 | mailimf_field *field = (mailimf_field *) current->data; | 99 | mailimf_field *field = (mailimf_field *) current->data; |
60 | switch ( field->fld_type ) { | 100 | switch ( field->fld_type ) { |
61 | case MAILIMF_FIELD_FROM: | 101 | case MAILIMF_FIELD_FROM: |
62 | mail->setFrom( *parseMailboxList( field->fld_data.fld_from->frm_mb_list ) ); | 102 | mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) ); |
63 | break; | 103 | break; |
64 | case MAILIMF_FIELD_TO: | 104 | case MAILIMF_FIELD_TO: |
65 | mail->setTo( *parseAddressList( field->fld_data.fld_to->to_addr_list ) ); | 105 | mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) ); |
66 | break; | 106 | break; |
67 | case MAILIMF_FIELD_CC: | 107 | case MAILIMF_FIELD_CC: |
68 | mail->setCC( *parseAddressList( field->fld_data.fld_cc->cc_addr_list ) ); | 108 | mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) ); |
69 | break; | 109 | break; |
70 | case MAILIMF_FIELD_BCC: | 110 | case MAILIMF_FIELD_BCC: |
71 | mail->setBcc( *parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) ); | 111 | mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) ); |
72 | break; | 112 | break; |
73 | case MAILIMF_FIELD_SUBJECT: | 113 | case MAILIMF_FIELD_SUBJECT: |
74 | mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) ); | 114 | mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) ); |
75 | break; | 115 | break; |
76 | case MAILIMF_FIELD_ORIG_DATE: | 116 | case MAILIMF_FIELD_ORIG_DATE: |
77 | mail->setDate( *parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) ); | 117 | mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) ); |
78 | break; | 118 | break; |
79 | default: | 119 | default: |
80 | break; | 120 | break; |
81 | } | 121 | } |
82 | } | 122 | } |
83 | 123 | ||
84 | return mail; | 124 | return mail; |
85 | } | 125 | } |
86 | 126 | ||
87 | QString *POP3wrapper::parseDateTime( mailimf_date_time *date ) | 127 | QString POP3wrapper::parseDateTime( mailimf_date_time *date ) |
88 | { | 128 | { |
89 | char tmp[23]; | 129 | char tmp[23]; |
90 | 130 | ||
91 | snprintf( tmp, 23, "%02i.%02i.%04i %02i:%02i:%02i %+05i", | 131 | snprintf( tmp, 23, "%02i.%02i.%04i %02i:%02i:%02i %+05i", |
92 | date->dt_day, date->dt_month, date->dt_year, date->dt_hour, date->dt_min, date->dt_sec, date->dt_zone ); | 132 | date->dt_day, date->dt_month, date->dt_year, date->dt_hour, date->dt_min, date->dt_sec, date->dt_zone ); |
93 | 133 | ||
94 | QString *result = new QString( tmp ); | 134 | return QString( tmp ); |
95 | |||
96 | return result; | ||
97 | } | 135 | } |
98 | 136 | ||
99 | QString *POP3wrapper::parseAddressList( mailimf_address_list *list ) | 137 | QString POP3wrapper::parseAddressList( mailimf_address_list *list ) |
100 | { | 138 | { |
101 | QString *result = new QString( "" ); | 139 | QString result( "" ); |
102 | 140 | ||
103 | bool first = true; | 141 | bool first = true; |
104 | for ( clistiter *current = clist_begin( list->ad_list ); current != NULL; current = current->next ) { | 142 | for ( clistiter *current = clist_begin( list->ad_list ); current != NULL; current = current->next ) { |
105 | mailimf_address *addr = (mailimf_address *) current->data; | 143 | mailimf_address *addr = (mailimf_address *) current->data; |
106 | 144 | ||
107 | if ( !first ) { | 145 | if ( !first ) { |
108 | result->append( "," ); | 146 | result.append( "," ); |
109 | } else { | 147 | } else { |
110 | first = false; | 148 | first = false; |
111 | } | 149 | } |
112 | 150 | ||
113 | QString *tmp; | ||
114 | |||
115 | switch ( addr->ad_type ) { | 151 | switch ( addr->ad_type ) { |
116 | case MAILIMF_ADDRESS_MAILBOX: | 152 | case MAILIMF_ADDRESS_MAILBOX: |
117 | tmp = parseMailbox( addr->ad_data.ad_mailbox ); | 153 | result.append( parseMailbox( addr->ad_data.ad_mailbox ) ); |
118 | result->append( *tmp ); | ||
119 | delete tmp; | ||
120 | break; | 154 | break; |
121 | case MAILIMF_ADDRESS_GROUP: | 155 | case MAILIMF_ADDRESS_GROUP: |
122 | tmp = parseGroup( addr->ad_data.ad_group ); | 156 | result.append( parseGroup( addr->ad_data.ad_group ) ); |
123 | result->append( *tmp ); | ||
124 | delete tmp; | ||
125 | break; | 157 | break; |
126 | default: | 158 | default: |
127 | qDebug( "POP3: unkown mailimf address type" ); | 159 | qDebug( "POP3: unkown mailimf address type" ); |
128 | break; | 160 | break; |
129 | } | 161 | } |
130 | } | 162 | } |
131 | 163 | ||
132 | return result; | 164 | return result; |
133 | } | 165 | } |
134 | 166 | ||
135 | QString *POP3wrapper::parseGroup( mailimf_group *group ) | 167 | QString POP3wrapper::parseGroup( mailimf_group *group ) |
136 | { | 168 | { |
137 | QString *result = new QString( "" ); | 169 | QString result( "" ); |
138 | 170 | ||
139 | result->append( group->grp_display_name ); | 171 | result.append( group->grp_display_name ); |
140 | result->append( ": " ); | 172 | result.append( ": " ); |
141 | 173 | ||
142 | if ( group->grp_mb_list != NULL ) { | 174 | if ( group->grp_mb_list != NULL ) { |
143 | QString *tmp = parseMailboxList( group->grp_mb_list ); | 175 | result.append( parseMailboxList( group->grp_mb_list ) ); |
144 | result->append( *tmp ); | ||
145 | delete tmp; | ||
146 | } | 176 | } |
147 | 177 | ||
148 | result->append( ";" ); | 178 | result.append( ";" ); |
149 | 179 | ||
150 | return result; | 180 | return result; |
151 | } | 181 | } |
152 | 182 | ||
153 | QString *POP3wrapper::parseMailbox( mailimf_mailbox *box ) | 183 | QString POP3wrapper::parseMailbox( mailimf_mailbox *box ) |
154 | { | 184 | { |
155 | QString *result = new QString( "" ); | 185 | QString result( "" ); |
156 | 186 | ||
157 | if ( box->mb_display_name == NULL ) { | 187 | if ( box->mb_display_name == NULL ) { |
158 | result->append( box->mb_addr_spec ); | 188 | result.append( box->mb_addr_spec ); |
159 | } else { | 189 | } else { |
160 | result->append( box->mb_display_name ); | 190 | result.append( box->mb_display_name ); |
161 | result->append( " <" ); | 191 | result.append( " <" ); |
162 | result->append( box->mb_addr_spec ); | 192 | result.append( box->mb_addr_spec ); |
163 | result->append( ">" ); | 193 | result.append( ">" ); |
164 | } | 194 | } |
165 | 195 | ||
166 | return result; | 196 | return result; |
167 | } | 197 | } |
168 | 198 | ||
169 | QString *POP3wrapper::parseMailboxList( mailimf_mailbox_list *list ) | 199 | QString POP3wrapper::parseMailboxList( mailimf_mailbox_list *list ) |
170 | { | 200 | { |
171 | QString *result = new QString( "" ); | 201 | QString result( "" ); |
172 | 202 | ||
173 | bool first = true; | 203 | bool first = true; |
174 | for ( clistiter *current = clist_begin( list->mb_list ); current != NULL; current = current->next ) { | 204 | for ( clistiter *current = clist_begin( list->mb_list ); current != NULL; current = current->next ) { |
175 | mailimf_mailbox *box = (mailimf_mailbox *) current->data; | 205 | mailimf_mailbox *box = (mailimf_mailbox *) current->data; |
176 | 206 | ||
177 | if ( !first ) { | 207 | if ( !first ) { |
178 | result->append( "," ); | 208 | result.append( "," ); |
179 | } else { | 209 | } else { |
180 | first = false; | 210 | first = false; |
181 | } | 211 | } |
182 | 212 | ||
183 | QString *tmp = parseMailbox( box ); | 213 | result.append( parseMailbox( box ) ); |
184 | result->append( *tmp ); | ||
185 | delete tmp; | ||
186 | } | 214 | } |
187 | 215 | ||
188 | return result; | 216 | return result; |
189 | } | 217 | } |
190 | 218 | ||
191 | void POP3wrapper::login() | 219 | void POP3wrapper::login() |
192 | { | 220 | { |
193 | if ( m_pop3 != NULL ) logout(); | 221 | if ( m_pop3 != NULL ) logout(); |
194 | 222 | ||
195 | const char *server, *user, *pass; | 223 | const char *server, *user, *pass; |
196 | uint16_t port; | 224 | uint16_t port; |
197 | int err = MAILPOP3_NO_ERROR; | 225 | int err = MAILPOP3_NO_ERROR; |
@@ -231,13 +259,12 @@ void POP3wrapper::login() | |||
231 | qDebug( "POP3: logged in!" ); | 259 | qDebug( "POP3: logged in!" ); |
232 | } | 260 | } |
233 | 261 | ||
234 | void POP3wrapper::logout() | 262 | void POP3wrapper::logout() |
235 | { | 263 | { |
236 | int err = MAILPOP3_NO_ERROR; | 264 | int err = MAILPOP3_NO_ERROR; |
237 | if ( m_pop3 == NULL ) return; | 265 | if ( m_pop3 == NULL ) return; |
238 | err = mailpop3_quit( m_pop3 ); | 266 | err = mailpop3_quit( m_pop3 ); |
239 | mailpop3_free( m_pop3 ); | 267 | mailpop3_free( m_pop3 ); |
240 | m_pop3 = NULL; | 268 | m_pop3 = NULL; |
241 | } | 269 | } |
242 | 270 | ||
243 | |||