Diffstat (limited to 'noncore/net/mail/libmailwrapper/pop3wrapper.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/net/mail/libmailwrapper/pop3wrapper.cpp | 57 |
1 files changed, 41 insertions, 16 deletions
diff --git a/noncore/net/mail/libmailwrapper/pop3wrapper.cpp b/noncore/net/mail/libmailwrapper/pop3wrapper.cpp index 46c854b..2411399 100644 --- a/noncore/net/mail/libmailwrapper/pop3wrapper.cpp +++ b/noncore/net/mail/libmailwrapper/pop3wrapper.cpp | |||
@@ -1,172 +1,197 @@ | |||
1 | 1 | #include <stdlib.h> | |
2 | #include "pop3wrapper.h" | 2 | #include "pop3wrapper.h" |
3 | #include "mailtypes.h" | 3 | #include "mailtypes.h" |
4 | #include <libetpan/mailpop3.h> | 4 | #include <libetpan/mailpop3.h> |
5 | 5 | ||
6 | POP3wrapper::POP3wrapper( POP3account *a ) | 6 | POP3wrapper::POP3wrapper( POP3account *a ) |
7 | { | 7 | { |
8 | account = a; | 8 | account = a; |
9 | m_pop3 = NULL; | 9 | m_pop3 = NULL; |
10 | } | 10 | } |
11 | 11 | ||
12 | POP3wrapper::~POP3wrapper() | 12 | POP3wrapper::~POP3wrapper() |
13 | { | 13 | { |
14 | logout(); | 14 | logout(); |
15 | } | 15 | } |
16 | 16 | ||
17 | void POP3wrapper::pop3_progress( size_t current, size_t maximum ) | 17 | void POP3wrapper::pop3_progress( size_t current, size_t maximum ) |
18 | { | 18 | { |
19 | qDebug( "POP3: %i of %i", current, maximum ); | 19 | qDebug( "POP3: %i of %i", current, maximum ); |
20 | } | 20 | } |
21 | 21 | ||
22 | RecBody POP3wrapper::fetchBody( const RecMail &mail ) | 22 | RecBody POP3wrapper::fetchBody( const RecMail &mail ) |
23 | { | 23 | { |
24 | int err = MAILPOP3_NO_ERROR; | 24 | int err = MAILPOP3_NO_ERROR; |
25 | char *message; | 25 | char *message; |
26 | size_t length; | 26 | size_t length; |
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(); | 34 | logout(); |
35 | return RecBody(); | 35 | return RecBody(); |
36 | } | 36 | } |
37 | 37 | ||
38 | logout(); | 38 | logout(); |
39 | 39 | ||
40 | return parseBody( message ); | 40 | return parseBody( message ); |
41 | } | 41 | } |
42 | 42 | ||
43 | RecBody POP3wrapper::parseBody( const char *message ) | 43 | RecBody POP3wrapper::parseBody( const char *message ) |
44 | { | 44 | { |
45 | int err = MAILIMF_NO_ERROR; | 45 | int err = MAILIMF_NO_ERROR; |
46 | size_t curTok; | 46 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ |
47 | mailimf_message *result; | 47 | size_t curTok = 0; |
48 | mailimf_message *result = 0; | ||
48 | RecBody body; | 49 | RecBody body; |
49 | 50 | ||
50 | err = mailimf_message_parse( (char *) message, strlen( message ), &curTok, &result ); | 51 | err = mailimf_message_parse( (char *) message, strlen( message ), &curTok, &result ); |
51 | if ( err != MAILIMF_NO_ERROR ) return body; | 52 | if ( err != MAILIMF_NO_ERROR ) { |
53 | if (result) mailimf_message_free(result); | ||
54 | return body; | ||
55 | } | ||
52 | 56 | ||
53 | if ( result && result->msg_body && result->msg_body->bd_text ) { | 57 | if ( result && result->msg_body && result->msg_body->bd_text ) { |
54 | qDebug( "POP3: bodytext found" ); | 58 | qDebug( "POP3: bodytext found" ); |
55 | // TODO: why does this line segfault???? gdb says segfault in strlen(), maybe a bug in libetpan. | 59 | // when curTok isn't set to 0 this line will fault! 'cause upper line faults! |
56 | body.setBodytext( QString( result->msg_body->bd_text ) ); | 60 | body.setBodytext( QString( result->msg_body->bd_text ) ); |
57 | } | 61 | } |
58 | 62 | if (result) mailimf_message_free(result); | |
59 | return body; | 63 | return body; |
60 | } | 64 | } |
61 | 65 | ||
62 | void POP3wrapper::listMessages(const QString &, QList<RecMail> &target ) | 66 | void POP3wrapper::listMessages(const QString &, QList<RecMail> &target ) |
63 | { | 67 | { |
64 | int err = MAILPOP3_NO_ERROR; | 68 | int err = MAILPOP3_NO_ERROR; |
65 | char *header; | 69 | char * header = 0; |
66 | size_t length; | 70 | /* these vars are used recurcive! set it to 0!!!!!!!!!!!!!!!!! */ |
67 | carray *messages; | 71 | size_t length = 0; |
72 | carray * messages = 0; | ||
68 | 73 | ||
69 | login(); | 74 | login(); |
70 | if (!m_pop3) return; | 75 | if (!m_pop3) return; |
71 | mailpop3_list( m_pop3, &messages ); | 76 | mailpop3_list( m_pop3, &messages ); |
72 | 77 | ||
73 | for ( int i = carray_count( messages ); i > 0; i-- ) { | 78 | //for ( int i = carray_count( messages ); i > 0; i-- ) { |
74 | mailpop3_msg_info *info = (mailpop3_msg_info *) carray_get( messages, i - 1 ); | 79 | for (unsigned int i = 0; i < carray_count(messages);++i) { |
75 | 80 | mailpop3_msg_info *info; | |
81 | int r = mailpop3_get_msg_info(m_pop3,i+1,&info); | ||
76 | err = mailpop3_header( m_pop3, info->msg_index, &header, &length ); | 82 | err = mailpop3_header( m_pop3, info->msg_index, &header, &length ); |
77 | if ( err != MAILPOP3_NO_ERROR ) { | 83 | if ( err != MAILPOP3_NO_ERROR ) { |
78 | qDebug( "POP3: error retrieving header msgid: %i", info->msg_index ); | 84 | qDebug( "POP3: error retrieving header msgid: %i", info->msg_index ); |
85 | free(header); | ||
79 | logout(); | 86 | logout(); |
80 | return; | 87 | return; |
81 | } | 88 | } |
82 | RecMail *mail = parseHeader( header ); | 89 | RecMail *mail = parseHeader( header ); |
83 | mail->setNumber( info->msg_index ); | 90 | mail->setNumber( info->msg_index ); |
84 | mail->setWrapper(this); | 91 | mail->setWrapper(this); |
85 | target.append( mail ); | 92 | target.append( mail ); |
93 | free(header); | ||
86 | } | 94 | } |
87 | |||
88 | logout(); | 95 | logout(); |
89 | } | 96 | } |
90 | 97 | ||
91 | RecMail *POP3wrapper::parseHeader( const char *header ) | 98 | RecMail *POP3wrapper::parseHeader( const char *header ) |
92 | { | 99 | { |
93 | int err = MAILIMF_NO_ERROR; | 100 | int err = MAILIMF_NO_ERROR; |
94 | size_t curTok; | 101 | size_t curTok = 0; |
95 | RecMail *mail = new RecMail(); | 102 | RecMail *mail = new RecMail(); |
96 | mailimf_fields *fields; | 103 | mailimf_fields *fields; |
97 | 104 | mailimf_references * refs; | |
105 | mailimf_keywords*keys; | ||
106 | |||
98 | err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields ); | 107 | err = mailimf_fields_parse( (char *) header, strlen( header ), &curTok, &fields ); |
99 | for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) { | 108 | for ( clistiter *current = clist_begin( fields->fld_list ); current != NULL; current = current->next ) { |
100 | mailimf_field *field = (mailimf_field *) current->data; | 109 | mailimf_field *field = (mailimf_field *) current->data; |
101 | switch ( field->fld_type ) { | 110 | switch ( field->fld_type ) { |
102 | case MAILIMF_FIELD_FROM: | 111 | case MAILIMF_FIELD_FROM: |
103 | mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) ); | 112 | mail->setFrom( parseMailboxList( field->fld_data.fld_from->frm_mb_list ) ); |
104 | break; | 113 | break; |
105 | case MAILIMF_FIELD_TO: | 114 | case MAILIMF_FIELD_TO: |
106 | mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) ); | 115 | mail->setTo( parseAddressList( field->fld_data.fld_to->to_addr_list ) ); |
107 | break; | 116 | break; |
108 | case MAILIMF_FIELD_CC: | 117 | case MAILIMF_FIELD_CC: |
109 | mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) ); | 118 | mail->setCC( parseAddressList( field->fld_data.fld_cc->cc_addr_list ) ); |
110 | break; | 119 | break; |
111 | case MAILIMF_FIELD_BCC: | 120 | case MAILIMF_FIELD_BCC: |
112 | mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) ); | 121 | mail->setBcc( parseAddressList( field->fld_data.fld_bcc->bcc_addr_list ) ); |
113 | break; | 122 | break; |
114 | case MAILIMF_FIELD_SUBJECT: | 123 | case MAILIMF_FIELD_SUBJECT: |
115 | mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) ); | 124 | mail->setSubject( QString( field->fld_data.fld_subject->sbj_value ) ); |
116 | break; | 125 | break; |
117 | case MAILIMF_FIELD_ORIG_DATE: | 126 | case MAILIMF_FIELD_ORIG_DATE: |
118 | mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) ); | 127 | mail->setDate( parseDateTime( field->fld_data.fld_orig_date->dt_date_time ) ); |
119 | break; | 128 | break; |
129 | case MAILIMF_FIELD_MESSAGE_ID: | ||
130 | mail->setMsgid(QString(field->fld_data.fld_message_id->mid_value)); | ||
131 | break; | ||
132 | case MAILIMF_FIELD_REFERENCES: | ||
133 | refs = field->fld_data.fld_references; | ||
134 | if (refs && refs->mid_list && clist_count(refs->mid_list)) { | ||
135 | char * text = (char*)refs->mid_list->first->data; | ||
136 | mail->setReplyto(QString(text)); | ||
137 | } | ||
138 | break; | ||
139 | case MAILIMF_FIELD_KEYWORDS: | ||
140 | keys = field->fld_data.fld_keywords; | ||
141 | for (clistcell*cur = clist_begin(keys->kw_list);cur!=0;cur=clist_next(cur)) { | ||
142 | qDebug("Keyword: %s",(char*)cur->data); | ||
143 | } | ||
144 | break; | ||
120 | default: | 145 | default: |
121 | break; | 146 | break; |
122 | } | 147 | } |
123 | } | 148 | } |
124 | 149 | if (fields) mailimf_fields_free(fields); | |
125 | return mail; | 150 | return mail; |
126 | } | 151 | } |
127 | 152 | ||
128 | QString POP3wrapper::parseDateTime( mailimf_date_time *date ) | 153 | QString POP3wrapper::parseDateTime( mailimf_date_time *date ) |
129 | { | 154 | { |
130 | char tmp[23]; | 155 | char tmp[23]; |
131 | 156 | ||
132 | snprintf( tmp, 23, "%02i.%02i.%04i %02i:%02i:%02i %+05i", | 157 | snprintf( tmp, 23, "%02i.%02i.%04i %02i:%02i:%02i %+05i", |
133 | date->dt_day, date->dt_month, date->dt_year, date->dt_hour, date->dt_min, date->dt_sec, date->dt_zone ); | 158 | date->dt_day, date->dt_month, date->dt_year, date->dt_hour, date->dt_min, date->dt_sec, date->dt_zone ); |
134 | 159 | ||
135 | return QString( tmp ); | 160 | return QString( tmp ); |
136 | } | 161 | } |
137 | 162 | ||
138 | QString POP3wrapper::parseAddressList( mailimf_address_list *list ) | 163 | QString POP3wrapper::parseAddressList( mailimf_address_list *list ) |
139 | { | 164 | { |
140 | QString result( "" ); | 165 | QString result( "" ); |
141 | 166 | ||
142 | bool first = true; | 167 | bool first = true; |
143 | for ( clistiter *current = clist_begin( list->ad_list ); current != NULL; current = current->next ) { | 168 | for ( clistiter *current = clist_begin( list->ad_list ); current != NULL; current = current->next ) { |
144 | mailimf_address *addr = (mailimf_address *) current->data; | 169 | mailimf_address *addr = (mailimf_address *) current->data; |
145 | 170 | ||
146 | if ( !first ) { | 171 | if ( !first ) { |
147 | result.append( "," ); | 172 | result.append( "," ); |
148 | } else { | 173 | } else { |
149 | first = false; | 174 | first = false; |
150 | } | 175 | } |
151 | 176 | ||
152 | switch ( addr->ad_type ) { | 177 | switch ( addr->ad_type ) { |
153 | case MAILIMF_ADDRESS_MAILBOX: | 178 | case MAILIMF_ADDRESS_MAILBOX: |
154 | result.append( parseMailbox( addr->ad_data.ad_mailbox ) ); | 179 | result.append( parseMailbox( addr->ad_data.ad_mailbox ) ); |
155 | break; | 180 | break; |
156 | case MAILIMF_ADDRESS_GROUP: | 181 | case MAILIMF_ADDRESS_GROUP: |
157 | result.append( parseGroup( addr->ad_data.ad_group ) ); | 182 | result.append( parseGroup( addr->ad_data.ad_group ) ); |
158 | break; | 183 | break; |
159 | default: | 184 | default: |
160 | qDebug( "POP3: unkown mailimf address type" ); | 185 | qDebug( "POP3: unkown mailimf address type" ); |
161 | break; | 186 | break; |
162 | } | 187 | } |
163 | } | 188 | } |
164 | 189 | ||
165 | return result; | 190 | return result; |
166 | } | 191 | } |
167 | 192 | ||
168 | QString POP3wrapper::parseGroup( mailimf_group *group ) | 193 | QString POP3wrapper::parseGroup( mailimf_group *group ) |
169 | { | 194 | { |
170 | QString result( "" ); | 195 | QString result( "" ); |
171 | 196 | ||
172 | result.append( group->grp_display_name ); | 197 | result.append( group->grp_display_name ); |