author | alwin <alwin> | 2003-12-25 14:23:31 (UTC) |
---|---|---|
committer | alwin <alwin> | 2003-12-25 14:23:31 (UTC) |
commit | 25ee009ceeda1a175c4604bf5afe5434e52ee838 (patch) (unidiff) | |
tree | ccc6b8cfbfac74880e9bebc5be595384ccf731e9 | |
parent | e0b1bc403c2792dff3aa04a00eaf58b9defc6dac (diff) | |
download | opie-25ee009ceeda1a175c4604bf5afe5434e52ee838.zip opie-25ee009ceeda1a175c4604bf5afe5434e52ee838.tar.gz opie-25ee009ceeda1a175c4604bf5afe5434e52ee838.tar.bz2 |
- some memleaks
- content generation will use the libetpan code und not own dumping to a
tmpfile.
- bugfix while creating mails with attachments (message itself could
be corrupted)
- textpart will always encoded quoted-printable
-rw-r--r-- | noncore/net/mail/libmailwrapper/smtpwrapper.cpp | 114 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/smtpwrapper.h | 3 | ||||
-rw-r--r-- | noncore/net/mail/smtpwrapper.cpp | 114 | ||||
-rw-r--r-- | noncore/net/mail/smtpwrapper.h | 3 |
4 files changed, 52 insertions, 182 deletions
diff --git a/noncore/net/mail/libmailwrapper/smtpwrapper.cpp b/noncore/net/mail/libmailwrapper/smtpwrapper.cpp index 285561c..521cd0a 100644 --- a/noncore/net/mail/libmailwrapper/smtpwrapper.cpp +++ b/noncore/net/mail/libmailwrapper/smtpwrapper.cpp | |||
@@ -1,788 +1,726 @@ | |||
1 | #include <stdlib.h> | 1 | #include <stdlib.h> |
2 | #include <sys/stat.h> | 2 | #include <sys/stat.h> |
3 | #include <sys/types.h> | 3 | #include <sys/types.h> |
4 | #include <unistd.h> | 4 | #include <unistd.h> |
5 | #include <fcntl.h> | 5 | #include <fcntl.h> |
6 | #include <string.h> | 6 | #include <string.h> |
7 | #include <qdir.h> | 7 | #include <qdir.h> |
8 | #include <qt.h> | 8 | #include <qt.h> |
9 | 9 | ||
10 | #include <libetpan/libetpan.h> | 10 | #include <libetpan/libetpan.h> |
11 | 11 | ||
12 | #include "smtpwrapper.h" | 12 | #include "smtpwrapper.h" |
13 | #include "mailwrapper.h" | 13 | #include "mailwrapper.h" |
14 | #include "mboxwrapper.h" | 14 | #include "mboxwrapper.h" |
15 | #include "logindialog.h" | 15 | #include "logindialog.h" |
16 | #include "mailtypes.h" | 16 | #include "mailtypes.h" |
17 | #include "defines.h" | 17 | #include "defines.h" |
18 | #include "sendmailprogress.h" | 18 | #include "sendmailprogress.h" |
19 | 19 | ||
20 | progressMailSend*SMTPwrapper::sendProgress = 0; | 20 | progressMailSend*SMTPwrapper::sendProgress = 0; |
21 | 21 | ||
22 | SMTPwrapper::SMTPwrapper( Settings *s ) | 22 | SMTPwrapper::SMTPwrapper( Settings *s ) |
23 | : QObject() | 23 | : QObject() |
24 | { | 24 | { |
25 | settings = s; | 25 | settings = s; |
26 | } | 26 | } |
27 | 27 | ||
28 | QString SMTPwrapper::mailsmtpError( int errnum ) | 28 | QString SMTPwrapper::mailsmtpError( int errnum ) |
29 | { | 29 | { |
30 | switch ( errnum ) { | 30 | switch ( errnum ) { |
31 | case MAILSMTP_NO_ERROR: | 31 | case MAILSMTP_NO_ERROR: |
32 | return tr( "No error" ); | 32 | return tr( "No error" ); |
33 | case MAILSMTP_ERROR_UNEXPECTED_CODE: | 33 | case MAILSMTP_ERROR_UNEXPECTED_CODE: |
34 | return tr( "Unexpected error code" ); | 34 | return tr( "Unexpected error code" ); |
35 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: | 35 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: |
36 | return tr( "Service not available" ); | 36 | return tr( "Service not available" ); |
37 | case MAILSMTP_ERROR_STREAM: | 37 | case MAILSMTP_ERROR_STREAM: |
38 | return tr( "Stream error" ); | 38 | return tr( "Stream error" ); |
39 | case MAILSMTP_ERROR_HOSTNAME: | 39 | case MAILSMTP_ERROR_HOSTNAME: |
40 | return tr( "gethostname() failed" ); | 40 | return tr( "gethostname() failed" ); |
41 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: | 41 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: |
42 | return tr( "Not implemented" ); | 42 | return tr( "Not implemented" ); |
43 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: | 43 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: |
44 | return tr( "Error, action not taken" ); | 44 | return tr( "Error, action not taken" ); |
45 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: | 45 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: |
46 | return tr( "Data exceeds storage allocation" ); | 46 | return tr( "Data exceeds storage allocation" ); |
47 | case MAILSMTP_ERROR_IN_PROCESSING: | 47 | case MAILSMTP_ERROR_IN_PROCESSING: |
48 | return tr( "Error in processing" ); | 48 | return tr( "Error in processing" ); |
49 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: | 49 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: |
50 | // return tr( "Insufficient system storage" ); | 50 | // return tr( "Insufficient system storage" ); |
51 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: | 51 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: |
52 | return tr( "Mailbox unavailable" ); | 52 | return tr( "Mailbox unavailable" ); |
53 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: | 53 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: |
54 | return tr( "Mailbox name not allowed" ); | 54 | return tr( "Mailbox name not allowed" ); |
55 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: | 55 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: |
56 | return tr( "Bad command sequence" ); | 56 | return tr( "Bad command sequence" ); |
57 | case MAILSMTP_ERROR_USER_NOT_LOCAL: | 57 | case MAILSMTP_ERROR_USER_NOT_LOCAL: |
58 | return tr( "User not local" ); | 58 | return tr( "User not local" ); |
59 | case MAILSMTP_ERROR_TRANSACTION_FAILED: | 59 | case MAILSMTP_ERROR_TRANSACTION_FAILED: |
60 | return tr( "Transaction failed" ); | 60 | return tr( "Transaction failed" ); |
61 | case MAILSMTP_ERROR_MEMORY: | 61 | case MAILSMTP_ERROR_MEMORY: |
62 | return tr( "Memory error" ); | 62 | return tr( "Memory error" ); |
63 | case MAILSMTP_ERROR_CONNECTION_REFUSED: | 63 | case MAILSMTP_ERROR_CONNECTION_REFUSED: |
64 | return tr( "Connection refused" ); | 64 | return tr( "Connection refused" ); |
65 | default: | 65 | default: |
66 | return tr( "Unknown error code" ); | 66 | return tr( "Unknown error code" ); |
67 | } | 67 | } |
68 | } | 68 | } |
69 | 69 | ||
70 | mailimf_mailbox *SMTPwrapper::newMailbox(const QString&name, const QString&mail ) | 70 | mailimf_mailbox *SMTPwrapper::newMailbox(const QString&name, const QString&mail ) |
71 | { | 71 | { |
72 | return mailimf_mailbox_new( strdup( name.latin1() ), | 72 | return mailimf_mailbox_new( strdup( name.latin1() ), |
73 | strdup( mail.latin1() ) ); | 73 | strdup( mail.latin1() ) ); |
74 | } | 74 | } |
75 | 75 | ||
76 | mailimf_address_list *SMTPwrapper::parseAddresses(const QString&addr ) | 76 | mailimf_address_list *SMTPwrapper::parseAddresses(const QString&addr ) |
77 | { | 77 | { |
78 | mailimf_address_list *addresses; | 78 | mailimf_address_list *addresses; |
79 | 79 | ||
80 | if ( addr.isEmpty() ) return NULL; | 80 | if ( addr.isEmpty() ) return NULL; |
81 | 81 | ||
82 | addresses = mailimf_address_list_new_empty(); | 82 | addresses = mailimf_address_list_new_empty(); |
83 | 83 | ||
84 | bool literal_open = false; | 84 | bool literal_open = false; |
85 | unsigned int startpos = 0; | 85 | unsigned int startpos = 0; |
86 | QStringList list; | 86 | QStringList list; |
87 | QString s; | 87 | QString s; |
88 | unsigned int i = 0; | 88 | unsigned int i = 0; |
89 | for (; i < addr.length();++i) { | 89 | for (; i < addr.length();++i) { |
90 | switch (addr[i]) { | 90 | switch (addr[i]) { |
91 | case '\"': | 91 | case '\"': |
92 | literal_open = !literal_open; | 92 | literal_open = !literal_open; |
93 | break; | 93 | break; |
94 | case ',': | 94 | case ',': |
95 | if (!literal_open) { | 95 | if (!literal_open) { |
96 | s = addr.mid(startpos,i-startpos); | 96 | s = addr.mid(startpos,i-startpos); |
97 | if (!s.isEmpty()) { | 97 | if (!s.isEmpty()) { |
98 | list.append(s); | 98 | list.append(s); |
99 | qDebug("Appended %s",s.latin1()); | 99 | qDebug("Appended %s",s.latin1()); |
100 | } | 100 | } |
101 | // !!!! this is a MUST BE! | 101 | // !!!! this is a MUST BE! |
102 | startpos = ++i; | 102 | startpos = ++i; |
103 | } | 103 | } |
104 | break; | 104 | break; |
105 | default: | 105 | default: |
106 | break; | 106 | break; |
107 | } | 107 | } |
108 | } | 108 | } |
109 | s = addr.mid(startpos,i-startpos); | 109 | s = addr.mid(startpos,i-startpos); |
110 | if (!s.isEmpty()) { | 110 | if (!s.isEmpty()) { |
111 | list.append(s); | 111 | list.append(s); |
112 | qDebug("Appended %s",s.latin1()); | 112 | qDebug("Appended %s",s.latin1()); |
113 | } | 113 | } |
114 | QStringList::Iterator it; | 114 | QStringList::Iterator it; |
115 | for ( it = list.begin(); it != list.end(); it++ ) { | 115 | for ( it = list.begin(); it != list.end(); it++ ) { |
116 | char *str = strdup( (*it).latin1() ); | 116 | int err = mailimf_address_list_add_parse( addresses, (char*)(*it).latin1() ); |
117 | int err = mailimf_address_list_add_parse( addresses, str ); | ||
118 | if ( err != MAILIMF_NO_ERROR ) { | 117 | if ( err != MAILIMF_NO_ERROR ) { |
119 | qDebug( "Error parsing" ); | 118 | qDebug( "Error parsing" ); |
120 | qDebug( *it ); | 119 | qDebug( *it ); |
121 | free( str ); | ||
122 | } else { | 120 | } else { |
123 | qDebug( "Parse success! %s",(*it).latin1()); | 121 | qDebug( "Parse success! %s",(*it).latin1()); |
124 | } | 122 | } |
125 | } | 123 | } |
126 | return addresses; | 124 | return addresses; |
127 | } | 125 | } |
128 | 126 | ||
129 | mailimf_fields *SMTPwrapper::createImfFields(const Mail&mail ) | 127 | mailimf_fields *SMTPwrapper::createImfFields(const Mail&mail ) |
130 | { | 128 | { |
131 | mailimf_fields *fields; | 129 | mailimf_fields *fields; |
132 | mailimf_field *xmailer; | 130 | mailimf_field *xmailer; |
133 | mailimf_mailbox *sender, *fromBox; | 131 | mailimf_mailbox *sender, *fromBox; |
134 | mailimf_mailbox_list *from; | 132 | mailimf_mailbox_list *from; |
135 | mailimf_address_list *to, *cc, *bcc, *reply; | 133 | mailimf_address_list *to, *cc, *bcc, *reply; |
136 | char *subject = strdup( mail.getSubject().latin1() ); | 134 | char *subject = strdup( mail.getSubject().latin1() ); |
137 | int err; | 135 | int err; |
138 | 136 | ||
139 | sender = newMailbox( mail.getName(), mail.getMail() ); | 137 | sender = newMailbox( mail.getName(), mail.getMail() ); |
140 | if ( sender == NULL ) goto err_free; | 138 | if ( sender == NULL ) goto err_free; |
141 | 139 | ||
142 | fromBox = newMailbox( mail.getName(), mail.getMail() ); | 140 | fromBox = newMailbox( mail.getName(), mail.getMail() ); |
143 | if ( fromBox == NULL ) goto err_free_sender; | 141 | if ( fromBox == NULL ) goto err_free_sender; |
144 | 142 | ||
145 | from = mailimf_mailbox_list_new_empty(); | 143 | from = mailimf_mailbox_list_new_empty(); |
146 | if ( from == NULL ) goto err_free_fromBox; | 144 | if ( from == NULL ) goto err_free_fromBox; |
147 | 145 | ||
148 | err = mailimf_mailbox_list_add( from, fromBox ); | 146 | err = mailimf_mailbox_list_add( from, fromBox ); |
149 | if ( err != MAILIMF_NO_ERROR ) goto err_free_from; | 147 | if ( err != MAILIMF_NO_ERROR ) goto err_free_from; |
150 | 148 | ||
151 | to = parseAddresses( mail.getTo() ); | 149 | to = parseAddresses( mail.getTo() ); |
152 | if ( to == NULL ) goto err_free_from; | 150 | if ( to == NULL ) goto err_free_from; |
153 | 151 | ||
154 | cc = parseAddresses( mail.getCC() ); | 152 | cc = parseAddresses( mail.getCC() ); |
155 | bcc = parseAddresses( mail.getBCC() ); | 153 | bcc = parseAddresses( mail.getBCC() ); |
156 | reply = parseAddresses( mail.getReply() ); | 154 | reply = parseAddresses( mail.getReply() ); |
157 | 155 | ||
158 | fields = mailimf_fields_new_with_data( from, sender, reply, to, cc, bcc, | 156 | fields = mailimf_fields_new_with_data( from, sender, reply, to, cc, bcc, |
159 | NULL, NULL, subject ); | 157 | NULL, NULL, subject ); |
160 | if ( fields == NULL ) goto err_free_reply; | 158 | if ( fields == NULL ) goto err_free_reply; |
161 | 159 | ||
162 | xmailer = mailimf_field_new_custom( strdup( "User-Agent" ), | 160 | xmailer = mailimf_field_new_custom( strdup( "User-Agent" ), |
163 | strdup( USER_AGENT ) ); | 161 | strdup( USER_AGENT ) ); |
164 | if ( xmailer == NULL ) goto err_free_fields; | 162 | if ( xmailer == NULL ) goto err_free_fields; |
165 | 163 | ||
166 | err = mailimf_fields_add( fields, xmailer ); | 164 | err = mailimf_fields_add( fields, xmailer ); |
167 | if ( err != MAILIMF_NO_ERROR ) goto err_free_xmailer; | 165 | if ( err != MAILIMF_NO_ERROR ) goto err_free_xmailer; |
168 | 166 | ||
169 | return fields; // Success :) | 167 | return fields; // Success :) |
170 | 168 | ||
171 | err_free_xmailer: | 169 | err_free_xmailer: |
172 | mailimf_field_free( xmailer ); | 170 | mailimf_field_free( xmailer ); |
173 | err_free_fields: | 171 | err_free_fields: |
174 | mailimf_fields_free( fields ); | 172 | mailimf_fields_free( fields ); |
175 | err_free_reply: | 173 | err_free_reply: |
176 | mailimf_address_list_free( reply ); | 174 | mailimf_address_list_free( reply ); |
177 | mailimf_address_list_free( bcc ); | 175 | mailimf_address_list_free( bcc ); |
178 | mailimf_address_list_free( cc ); | 176 | mailimf_address_list_free( cc ); |
179 | mailimf_address_list_free( to ); | 177 | mailimf_address_list_free( to ); |
180 | err_free_from: | 178 | err_free_from: |
181 | mailimf_mailbox_list_free( from ); | 179 | mailimf_mailbox_list_free( from ); |
182 | err_free_fromBox: | 180 | err_free_fromBox: |
183 | mailimf_mailbox_free( fromBox ); | 181 | mailimf_mailbox_free( fromBox ); |
184 | err_free_sender: | 182 | err_free_sender: |
185 | mailimf_mailbox_free( sender ); | 183 | mailimf_mailbox_free( sender ); |
186 | err_free: | 184 | err_free: |
187 | free( subject ); | 185 | free( subject ); |
188 | qDebug( "createImfFields - error" ); | 186 | qDebug( "createImfFields - error" ); |
189 | 187 | ||
190 | return NULL; // Error :( | 188 | return NULL; // Error :( |
191 | } | 189 | } |
192 | 190 | ||
193 | mailmime *SMTPwrapper::buildTxtPart(const QString&str ) | 191 | mailmime *SMTPwrapper::buildTxtPart(const QString&str ) |
194 | { | 192 | { |
195 | mailmime *txtPart; | 193 | mailmime *txtPart; |
196 | mailmime_fields *fields; | 194 | mailmime_fields *fields; |
197 | mailmime_content *content; | 195 | mailmime_content *content; |
198 | mailmime_parameter *param; | 196 | mailmime_parameter *param; |
199 | char *txt = strdup( str.latin1() ); | ||
200 | int err; | 197 | int err; |
201 | 198 | ||
202 | param = mailmime_parameter_new( strdup( "charset" ), | 199 | param = mailmime_parameter_new( strdup( "charset" ), |
203 | strdup( "iso-8859-1" ) ); | 200 | strdup( "iso-8859-1" ) ); |
204 | if ( param == NULL ) goto err_free; | 201 | if ( param == NULL ) goto err_free; |
205 | 202 | ||
206 | content = mailmime_content_new_with_str( "text/plain" ); | 203 | content = mailmime_content_new_with_str( "text/plain" ); |
207 | if ( content == NULL ) goto err_free_param; | 204 | if ( content == NULL ) goto err_free_param; |
208 | 205 | ||
209 | err = clist_append( content->ct_parameters, param ); | 206 | err = clist_append( content->ct_parameters, param ); |
210 | if ( err != MAILIMF_NO_ERROR ) goto err_free_content; | 207 | if ( err != MAILIMF_NO_ERROR ) goto err_free_content; |
211 | 208 | ||
212 | fields = mailmime_fields_new_encoding( MAILMIME_MECHANISM_8BIT ); | 209 | fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_QUOTED_PRINTABLE); |
213 | if ( fields == NULL ) goto err_free_content; | 210 | if ( fields == NULL ) goto err_free_content; |
214 | 211 | ||
215 | txtPart = mailmime_new_empty( content, fields ); | 212 | txtPart = mailmime_new_empty( content, fields ); |
216 | if ( txtPart == NULL ) goto err_free_fields; | 213 | if ( txtPart == NULL ) goto err_free_fields; |
217 | 214 | ||
218 | err = mailmime_set_body_text( txtPart, txt, strlen( txt ) ); | 215 | err = mailmime_set_body_text( txtPart, (char*)str.data(), str.length() ); |
219 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; | 216 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; |
220 | 217 | ||
221 | return txtPart; // Success :) | 218 | return txtPart; // Success :) |
222 | 219 | ||
223 | err_free_txtPart: | 220 | err_free_txtPart: |
224 | mailmime_free( txtPart ); | 221 | mailmime_free( txtPart ); |
225 | err_free_fields: | 222 | err_free_fields: |
226 | mailmime_fields_free( fields ); | 223 | mailmime_fields_free( fields ); |
227 | err_free_content: | 224 | err_free_content: |
228 | mailmime_content_free( content ); | 225 | mailmime_content_free( content ); |
229 | err_free_param: | 226 | err_free_param: |
230 | mailmime_parameter_free( param ); | 227 | mailmime_parameter_free( param ); |
231 | err_free: | 228 | err_free: |
232 | free( txt ); | ||
233 | qDebug( "buildTxtPart - error" ); | 229 | qDebug( "buildTxtPart - error" ); |
234 | 230 | ||
235 | return NULL; // Error :( | 231 | return NULL; // Error :( |
236 | } | 232 | } |
237 | 233 | ||
238 | mailmime *SMTPwrapper::buildFilePart(const QString&filename,const QString&mimetype,const QString&TextContent ) | 234 | mailmime *SMTPwrapper::buildFilePart(const QString&filename,const QString&mimetype,const QString&TextContent ) |
239 | { | 235 | { |
240 | mailmime * filePart = 0; | 236 | mailmime * filePart = 0; |
241 | mailmime_fields * fields = 0; | 237 | mailmime_fields * fields = 0; |
242 | mailmime_content * content = 0; | 238 | mailmime_content * content = 0; |
243 | mailmime_parameter * param = 0; | 239 | mailmime_parameter * param = 0; |
244 | char*name = 0; | 240 | char*name = 0; |
245 | char*file = 0; | 241 | char*file = 0; |
246 | int err; | 242 | int err; |
247 | 243 | ||
248 | int pos = filename.findRev( '/' ); | 244 | int pos = filename.findRev( '/' ); |
249 | 245 | ||
250 | if (filename.length()>0) { | 246 | if (filename.length()>0) { |
251 | QString tmp = filename.right( filename.length() - ( pos + 1 ) ); | 247 | QString tmp = filename.right( filename.length() - ( pos + 1 ) ); |
252 | name = strdup( tmp.latin1() ); // just filename | 248 | name = strdup( tmp.latin1() ); // just filename |
253 | file = strdup( filename.latin1() ); // full name with path | 249 | file = strdup( filename.latin1() ); // full name with path |
254 | } | 250 | } |
255 | char *mime = strdup( mimetype.latin1() ); // mimetype -e.g. text/plain | 251 | |
256 | |||
257 | int disptype = MAILMIME_DISPOSITION_TYPE_ATTACHMENT; | 252 | int disptype = MAILMIME_DISPOSITION_TYPE_ATTACHMENT; |
258 | int mechanism = MAILMIME_MECHANISM_BASE64; | 253 | int mechanism = MAILMIME_MECHANISM_BASE64; |
259 | 254 | ||
260 | if ( mimetype.startsWith( "text/" ) ) { | 255 | if ( mimetype.startsWith( "text/" ) ) { |
261 | param = mailmime_parameter_new( strdup( "charset" ), | 256 | param = mailmime_parameter_new( strdup( "charset" ), |
262 | strdup( "iso-8859-1" ) ); | 257 | strdup( "iso-8859-1" ) ); |
263 | disptype = MAILMIME_DISPOSITION_TYPE_INLINE; | ||
264 | mechanism = MAILMIME_MECHANISM_QUOTED_PRINTABLE; | 258 | mechanism = MAILMIME_MECHANISM_QUOTED_PRINTABLE; |
265 | } | 259 | } |
266 | 260 | ||
267 | fields = mailmime_fields_new_filename( | 261 | fields = mailmime_fields_new_filename( |
268 | disptype, name, | 262 | disptype, name, |
269 | mechanism ); | 263 | mechanism ); |
270 | content = mailmime_content_new_with_str( mime ); | 264 | content = mailmime_content_new_with_str( (char*)mimetype.latin1() ); |
271 | if (content!=0 && fields != 0) { | 265 | if (content!=0 && fields != 0) { |
272 | if (param) { | 266 | if (param) { |
273 | clist_append(content->ct_parameters,param); | 267 | clist_append(content->ct_parameters,param); |
274 | param = 0; | 268 | param = 0; |
275 | } | 269 | } |
276 | if (filename.length()>0) { | 270 | if (filename.length()>0) { |
277 | QFileInfo f(filename); | 271 | QFileInfo f(filename); |
278 | param = mailmime_parameter_new(strdup("name"),strdup(f.fileName().latin1())); | 272 | param = mailmime_parameter_new(strdup("name"),strdup(f.fileName().latin1())); |
279 | clist_append(content->ct_parameters,param); | 273 | clist_append(content->ct_parameters,param); |
280 | param = 0; | 274 | param = 0; |
281 | } | 275 | } |
282 | filePart = mailmime_new_empty( content, fields ); | 276 | filePart = mailmime_new_empty( content, fields ); |
283 | } | 277 | } |
284 | if (filePart) { | 278 | if (filePart) { |
285 | if (filename.length()>0) { | 279 | if (filename.length()>0) { |
286 | err = mailmime_set_body_file( filePart, file ); | 280 | err = mailmime_set_body_file( filePart, file ); |
287 | } else { | 281 | } else { |
288 | err = mailmime_set_body_text(filePart,strdup(TextContent.ascii()),TextContent.length()); | 282 | err = mailmime_set_body_text(filePart,strdup(TextContent.data()),TextContent.length()); |
289 | } | 283 | } |
290 | if (err != MAILIMF_NO_ERROR) { | 284 | if (err != MAILIMF_NO_ERROR) { |
291 | qDebug("Error setting body with file %s",file); | 285 | qDebug("Error setting body with file %s",file); |
292 | mailmime_free( filePart ); | 286 | mailmime_free( filePart ); |
293 | filePart = 0; | 287 | filePart = 0; |
294 | } | 288 | } |
295 | } | 289 | } |
296 | 290 | ||
297 | if (!filePart) { | 291 | if (!filePart) { |
298 | if ( param != NULL ) { | 292 | if ( param != NULL ) { |
299 | mailmime_parameter_free( param ); | 293 | mailmime_parameter_free( param ); |
300 | } | 294 | } |
301 | if (content) { | 295 | if (content) { |
302 | mailmime_content_free( content ); | 296 | mailmime_content_free( content ); |
303 | } else { | ||
304 | if (mime) { | ||
305 | free( mime ); | ||
306 | } | ||
307 | } | 297 | } |
308 | if (fields) { | 298 | if (fields) { |
309 | mailmime_fields_free( fields ); | 299 | mailmime_fields_free( fields ); |
310 | } else { | 300 | } else { |
311 | if (name) { | 301 | if (name) { |
312 | free( name ); | 302 | free( name ); |
313 | } | 303 | } |
314 | if (file) { | 304 | if (file) { |
315 | free( file ); | 305 | free( file ); |
316 | } | 306 | } |
317 | } | 307 | } |
318 | } | 308 | } |
319 | return filePart; // Success :) | 309 | return filePart; // Success :) |
320 | 310 | ||
321 | } | 311 | } |
322 | 312 | ||
323 | void SMTPwrapper::addFileParts( mailmime *message,const QList<Attachment>&files ) | 313 | void SMTPwrapper::addFileParts( mailmime *message,const QList<Attachment>&files ) |
324 | { | 314 | { |
325 | const Attachment *it; | 315 | const Attachment *it; |
326 | unsigned int count = files.count(); | 316 | unsigned int count = files.count(); |
327 | qDebug("List contains %i values",count); | 317 | qDebug("List contains %i values",count); |
328 | for ( unsigned int i = 0; i < count; ++i ) { | 318 | for ( unsigned int i = 0; i < count; ++i ) { |
329 | qDebug( "Adding file" ); | 319 | qDebug( "Adding file" ); |
330 | mailmime *filePart; | 320 | mailmime *filePart; |
331 | int err; | 321 | int err; |
332 | it = ((QList<Attachment>)files).at(i); | 322 | it = ((QList<Attachment>)files).at(i); |
333 | 323 | ||
334 | filePart = buildFilePart( it->getFileName(), it->getMimeType(),"" ); | 324 | filePart = buildFilePart( it->getFileName(), it->getMimeType(),"" ); |
335 | if ( filePart == NULL ) { | 325 | if ( filePart == NULL ) { |
336 | qDebug( "addFileParts: error adding file:" ); | 326 | qDebug( "addFileParts: error adding file:" ); |
337 | qDebug( it->getFileName() ); | 327 | qDebug( it->getFileName() ); |
338 | continue; | 328 | continue; |
339 | } | 329 | } |
340 | err = mailmime_smart_add_part( message, filePart ); | 330 | err = mailmime_smart_add_part( message, filePart ); |
341 | if ( err != MAILIMF_NO_ERROR ) { | 331 | if ( err != MAILIMF_NO_ERROR ) { |
342 | mailmime_free( filePart ); | 332 | mailmime_free( filePart ); |
343 | qDebug("error smart add"); | 333 | qDebug("error smart add"); |
344 | } | 334 | } |
345 | } | 335 | } |
346 | } | 336 | } |
347 | 337 | ||
348 | mailmime *SMTPwrapper::createMimeMail(const Mail &mail ) | 338 | mailmime *SMTPwrapper::createMimeMail(const Mail &mail ) |
349 | { | 339 | { |
350 | mailmime *message, *txtPart; | 340 | mailmime *message, *txtPart; |
351 | mailimf_fields *fields; | 341 | mailimf_fields *fields; |
352 | int err; | 342 | int err; |
353 | 343 | ||
354 | fields = createImfFields( mail ); | 344 | fields = createImfFields( mail ); |
355 | if ( fields == NULL ) goto err_free; | 345 | if ( fields == NULL ) goto err_free; |
356 | 346 | ||
357 | message = mailmime_new_message_data( NULL ); | 347 | message = mailmime_new_message_data( NULL ); |
358 | if ( message == NULL ) goto err_free_fields; | 348 | if ( message == NULL ) goto err_free_fields; |
359 | 349 | ||
360 | mailmime_set_imf_fields( message, fields ); | 350 | mailmime_set_imf_fields( message, fields ); |
361 | 351 | ||
362 | if (mail.getAttachments().count()==0) { | 352 | txtPart = buildTxtPart( mail.getMessage() ); |
363 | txtPart = buildTxtPart( mail.getMessage() ); | 353 | |
364 | } else { | ||
365 | txtPart = buildFilePart("","text/plain",mail.getMessage()); | ||
366 | } | ||
367 | if ( txtPart == NULL ) goto err_free_message; | 354 | if ( txtPart == NULL ) goto err_free_message; |
368 | 355 | ||
369 | err = mailmime_smart_add_part( message, txtPart ); | 356 | err = mailmime_smart_add_part( message, txtPart ); |
370 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; | 357 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; |
371 | 358 | ||
372 | addFileParts( message, mail.getAttachments() ); | 359 | addFileParts( message, mail.getAttachments() ); |
373 | 360 | ||
374 | return message; // Success :) | 361 | return message; // Success :) |
375 | 362 | ||
376 | err_free_txtPart: | 363 | err_free_txtPart: |
377 | mailmime_free( txtPart ); | 364 | mailmime_free( txtPart ); |
378 | err_free_message: | 365 | err_free_message: |
379 | mailmime_free( message ); | 366 | mailmime_free( message ); |
380 | err_free_fields: | 367 | err_free_fields: |
381 | mailimf_fields_free( fields ); | 368 | mailimf_fields_free( fields ); |
382 | err_free: | 369 | err_free: |
383 | qDebug( "createMimeMail: error" ); | 370 | qDebug( "createMimeMail: error" ); |
384 | 371 | ||
385 | return NULL; // Error :( | 372 | return NULL; // Error :( |
386 | } | 373 | } |
387 | 374 | ||
388 | mailimf_field *SMTPwrapper::getField( mailimf_fields *fields, int type ) | 375 | mailimf_field *SMTPwrapper::getField( mailimf_fields *fields, int type ) |
389 | { | 376 | { |
390 | mailimf_field *field; | 377 | mailimf_field *field; |
391 | clistiter *it; | 378 | clistiter *it; |
392 | 379 | ||
393 | it = clist_begin( fields->fld_list ); | 380 | it = clist_begin( fields->fld_list ); |
394 | while ( it ) { | 381 | while ( it ) { |
395 | field = (mailimf_field *) it->data; | 382 | field = (mailimf_field *) it->data; |
396 | if ( field->fld_type == type ) { | 383 | if ( field->fld_type == type ) { |
397 | return field; | 384 | return field; |
398 | } | 385 | } |
399 | it = it->next; | 386 | it = it->next; |
400 | } | 387 | } |
401 | 388 | ||
402 | return NULL; | 389 | return NULL; |
403 | } | 390 | } |
404 | 391 | ||
405 | void SMTPwrapper::addRcpts( clist *list, mailimf_address_list *addr_list ) | 392 | void SMTPwrapper::addRcpts( clist *list, mailimf_address_list *addr_list ) |
406 | { | 393 | { |
407 | clistiter *it, *it2; | 394 | clistiter *it, *it2; |
408 | 395 | ||
409 | for ( it = clist_begin( addr_list->ad_list ); it; it = it->next ) { | 396 | for ( it = clist_begin( addr_list->ad_list ); it; it = it->next ) { |
410 | mailimf_address *addr; | 397 | mailimf_address *addr; |
411 | addr = (mailimf_address *) it->data; | 398 | addr = (mailimf_address *) it->data; |
412 | 399 | ||
413 | if ( addr->ad_type == MAILIMF_ADDRESS_MAILBOX ) { | 400 | if ( addr->ad_type == MAILIMF_ADDRESS_MAILBOX ) { |
414 | esmtp_address_list_add( list, addr->ad_data.ad_mailbox->mb_addr_spec, 0, NULL ); | 401 | esmtp_address_list_add( list, addr->ad_data.ad_mailbox->mb_addr_spec, 0, NULL ); |
415 | } else if ( addr->ad_type == MAILIMF_ADDRESS_GROUP ) { | 402 | } else if ( addr->ad_type == MAILIMF_ADDRESS_GROUP ) { |
416 | clist *l = addr->ad_data.ad_group->grp_mb_list->mb_list; | 403 | clist *l = addr->ad_data.ad_group->grp_mb_list->mb_list; |
417 | for ( it2 = clist_begin( l ); it2; it2 = it2->next ) { | 404 | for ( it2 = clist_begin( l ); it2; it2 = it2->next ) { |
418 | mailimf_mailbox *mbox; | 405 | mailimf_mailbox *mbox; |
419 | mbox = (mailimf_mailbox *) it2->data; | 406 | mbox = (mailimf_mailbox *) it2->data; |
420 | esmtp_address_list_add( list, mbox->mb_addr_spec, 0, NULL ); | 407 | esmtp_address_list_add( list, mbox->mb_addr_spec, 0, NULL ); |
421 | } | 408 | } |
422 | } | 409 | } |
423 | } | 410 | } |
424 | } | 411 | } |
425 | 412 | ||
426 | clist *SMTPwrapper::createRcptList( mailimf_fields *fields ) | 413 | clist *SMTPwrapper::createRcptList( mailimf_fields *fields ) |
427 | { | 414 | { |
428 | clist *rcptList; | 415 | clist *rcptList; |
429 | mailimf_field *field; | 416 | mailimf_field *field; |
430 | 417 | ||
431 | rcptList = esmtp_address_list_new(); | 418 | rcptList = esmtp_address_list_new(); |
432 | 419 | ||
433 | field = getField( fields, MAILIMF_FIELD_TO ); | 420 | field = getField( fields, MAILIMF_FIELD_TO ); |
434 | if ( field && (field->fld_type == MAILIMF_FIELD_TO) | 421 | if ( field && (field->fld_type == MAILIMF_FIELD_TO) |
435 | && field->fld_data.fld_to->to_addr_list ) { | 422 | && field->fld_data.fld_to->to_addr_list ) { |
436 | addRcpts( rcptList, field->fld_data.fld_to->to_addr_list ); | 423 | addRcpts( rcptList, field->fld_data.fld_to->to_addr_list ); |
437 | } | 424 | } |
438 | 425 | ||
439 | field = getField( fields, MAILIMF_FIELD_CC ); | 426 | field = getField( fields, MAILIMF_FIELD_CC ); |
440 | if ( field && (field->fld_type == MAILIMF_FIELD_CC) | 427 | if ( field && (field->fld_type == MAILIMF_FIELD_CC) |
441 | && field->fld_data.fld_cc->cc_addr_list ) { | 428 | && field->fld_data.fld_cc->cc_addr_list ) { |
442 | addRcpts( rcptList, field->fld_data.fld_cc->cc_addr_list ); | 429 | addRcpts( rcptList, field->fld_data.fld_cc->cc_addr_list ); |
443 | } | 430 | } |
444 | 431 | ||
445 | field = getField( fields, MAILIMF_FIELD_BCC ); | 432 | field = getField( fields, MAILIMF_FIELD_BCC ); |
446 | if ( field && (field->fld_type == MAILIMF_FIELD_BCC) | 433 | if ( field && (field->fld_type == MAILIMF_FIELD_BCC) |
447 | && field->fld_data.fld_bcc->bcc_addr_list ) { | 434 | && field->fld_data.fld_bcc->bcc_addr_list ) { |
448 | addRcpts( rcptList, field->fld_data.fld_bcc->bcc_addr_list ); | 435 | addRcpts( rcptList, field->fld_data.fld_bcc->bcc_addr_list ); |
449 | } | 436 | } |
450 | 437 | ||
451 | return rcptList; | 438 | return rcptList; |
452 | } | 439 | } |
453 | 440 | ||
454 | char *SMTPwrapper::getFrom( mailimf_field *ffrom) | 441 | char *SMTPwrapper::getFrom( mailimf_field *ffrom) |
455 | { | 442 | { |
456 | char *from = NULL; | 443 | char *from = NULL; |
457 | if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM) | 444 | if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM) |
458 | && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) { | 445 | && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) { |
459 | clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list; | 446 | clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list; |
460 | clistiter *it; | 447 | clistiter *it; |
461 | for ( it = clist_begin( cl ); it; it = it->next ) { | 448 | for ( it = clist_begin( cl ); it; it = it->next ) { |
462 | mailimf_mailbox *mb = (mailimf_mailbox *) it->data; | 449 | mailimf_mailbox *mb = (mailimf_mailbox *) it->data; |
463 | from = strdup( mb->mb_addr_spec ); | 450 | from = strdup( mb->mb_addr_spec ); |
464 | } | 451 | } |
465 | } | 452 | } |
466 | 453 | ||
467 | return from; | 454 | return from; |
468 | } | 455 | } |
469 | 456 | ||
470 | char *SMTPwrapper::getFrom( mailmime *mail ) | 457 | char *SMTPwrapper::getFrom( mailmime *mail ) |
471 | { | 458 | { |
472 | /* no need to delete - its just a pointer to structure content */ | 459 | /* no need to delete - its just a pointer to structure content */ |
473 | mailimf_field *ffrom = 0; | 460 | mailimf_field *ffrom = 0; |
474 | char*f = 0; | ||
475 | ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM ); | 461 | ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM ); |
476 | return getFrom(ffrom); | 462 | return getFrom(ffrom); |
477 | } | 463 | } |
478 | 464 | ||
479 | SMTPaccount *SMTPwrapper::getAccount(const QString&name ) | 465 | SMTPaccount *SMTPwrapper::getAccount(const QString&name ) |
480 | { | 466 | { |
481 | SMTPaccount *smtp; | 467 | SMTPaccount *smtp; |
482 | 468 | ||
483 | QList<Account> list = settings->getAccounts(); | 469 | QList<Account> list = settings->getAccounts(); |
484 | Account *it; | 470 | Account *it; |
485 | for ( it = list.first(); it; it = list.next() ) { | 471 | for ( it = list.first(); it; it = list.next() ) { |
486 | if ( it->getType().compare( "SMTP" ) == 0 ) { | 472 | if ( it->getType().compare( "SMTP" ) == 0 ) { |
487 | smtp = static_cast<SMTPaccount *>(it); | 473 | smtp = static_cast<SMTPaccount *>(it); |
488 | if ( smtp->getName()== name ) { | 474 | if ( smtp->getName()== name ) { |
489 | qDebug( "SMTPaccount found for" ); | 475 | qDebug( "SMTPaccount found for" ); |
490 | qDebug( name ); | 476 | qDebug( name ); |
491 | return smtp; | 477 | return smtp; |
492 | } | 478 | } |
493 | } | 479 | } |
494 | } | 480 | } |
495 | 481 | ||
496 | return NULL; | 482 | return NULL; |
497 | } | 483 | } |
498 | 484 | ||
499 | QString SMTPwrapper::getTmpFile() { | ||
500 | int num = 0; | ||
501 | QString unique; | ||
502 | |||
503 | QDir dir( "/tmp" ); | ||
504 | |||
505 | QStringList list = dir.entryList( "opiemail-tmp-*" ); | ||
506 | |||
507 | do { | ||
508 | unique.setNum( num++ ); | ||
509 | } while ( list.contains( "opiemail-tmp-" + unique ) > 0 ); | ||
510 | |||
511 | return "/tmp/opiemail-tmp-" + unique; | ||
512 | } | ||
513 | |||
514 | void SMTPwrapper::writeToFile(const QString&file, mailmime *mail ) | ||
515 | { | ||
516 | FILE *f; | ||
517 | int err, col = 0; | ||
518 | |||
519 | f = fopen( file.latin1(), "w" ); | ||
520 | if ( f == NULL ) { | ||
521 | qDebug( "writeToFile: error opening file" ); | ||
522 | return; | ||
523 | } | ||
524 | |||
525 | err = mailmime_write( f, &col, mail ); | ||
526 | if ( err != MAILIMF_NO_ERROR ) { | ||
527 | fclose( f ); | ||
528 | qDebug( "writeToFile: error writing mailmime" ); | ||
529 | return; | ||
530 | } | ||
531 | |||
532 | fclose( f ); | ||
533 | } | ||
534 | |||
535 | void SMTPwrapper::readFromFile(const QString&file, char **data, size_t *size ) | ||
536 | { | ||
537 | |||
538 | QFile msg_cache(file); | ||
539 | QString msg = ""; | ||
540 | msg_cache.open(IO_ReadOnly); | ||
541 | char*message = new char[4096]; | ||
542 | memset(message,0,4096); | ||
543 | while (msg_cache.readBlock(message,4095)>0) { | ||
544 | msg+=message; | ||
545 | memset(message,0,4096); | ||
546 | } | ||
547 | delete message; | ||
548 | *data = (char*)malloc(msg.length()+1*sizeof(char)); | ||
549 | memset(*data,0,msg.length()+1); | ||
550 | memcpy(*data,msg.ascii(),msg.length()); | ||
551 | *size=msg.length(); | ||
552 | } | ||
553 | |||
554 | void SMTPwrapper::progress( size_t current, size_t maximum ) | 485 | void SMTPwrapper::progress( size_t current, size_t maximum ) |
555 | { | 486 | { |
556 | if (SMTPwrapper::sendProgress) { | 487 | if (SMTPwrapper::sendProgress) { |
557 | SMTPwrapper::sendProgress->setSingleMail(current, maximum ); | 488 | SMTPwrapper::sendProgress->setSingleMail(current, maximum ); |
558 | qApp->processEvents(); | 489 | qApp->processEvents(); |
559 | } | 490 | } |
560 | } | 491 | } |
561 | 492 | ||
562 | void SMTPwrapper::storeMail(char*mail, size_t length, const QString&box) | 493 | void SMTPwrapper::storeMail(char*mail, size_t length, const QString&box) |
563 | { | 494 | { |
564 | if (!mail) return; | 495 | if (!mail) return; |
565 | QString localfolders = (QString) getenv( "HOME" ) + QString("/Applications/opiemail/localmail/"); | 496 | QString localfolders = (QString) getenv( "HOME" ) + QString("/Applications/opiemail/localmail/"); |
566 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); | 497 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); |
567 | wrap->storeMessage(mail,length,box); | 498 | wrap->storeMessage(mail,length,box); |
568 | delete wrap; | 499 | delete wrap; |
569 | } | 500 | } |
570 | 501 | ||
571 | void SMTPwrapper::smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ) | 502 | void SMTPwrapper::smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ) |
572 | { | 503 | { |
573 | clist *rcpts = 0; | 504 | clist *rcpts = 0; |
574 | char *from, *data; | 505 | char *from, *data; |
575 | size_t size; | 506 | size_t size; |
576 | 507 | ||
577 | if ( smtp == NULL ) { | 508 | if ( smtp == NULL ) { |
578 | return; | 509 | return; |
579 | } | 510 | } |
580 | from = data = 0; | 511 | from = data = 0; |
581 | 512 | ||
582 | QString file = getTmpFile(); | 513 | mailmessage * msg = 0; |
583 | writeToFile( file, mail ); | 514 | msg = mime_message_init(mail); |
584 | readFromFile( file, &data, &size ); | 515 | mime_message_set_tmpdir(msg,getenv( "HOME" )); |
585 | QFile f( file ); | 516 | int r = mailmessage_fetch(msg,&data,&size); |
586 | f.remove(); | 517 | mime_message_detach_mime(msg); |
587 | 518 | mailmessage_free(msg); | |
519 | if (r != MAIL_NO_ERROR || !data) { | ||
520 | if (data) free(data); | ||
521 | qDebug("Error fetching mime..."); | ||
522 | return; | ||
523 | } | ||
524 | QString tmp = data; | ||
525 | tmp.replace(QRegExp("\r+",true,false),""); | ||
526 | msg = 0; | ||
588 | if (later) { | 527 | if (later) { |
589 | storeMail(data,size,"Outgoing"); | 528 | storeMail((char*)tmp.data(),tmp.length(),"Outgoing"); |
590 | if (data) free( data ); | 529 | if (data) free( data ); |
591 | return; | 530 | return; |
592 | } | 531 | } |
593 | from = getFrom( mail ); | 532 | from = getFrom( mail ); |
594 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); | 533 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); |
595 | smtpSend(from,rcpts,data,size,smtp); | 534 | smtpSend(from,rcpts,data,size,smtp); |
596 | if (data) {free(data);} | 535 | if (data) {free(data);} |
597 | if (from) {free(from);} | 536 | if (from) {free(from);} |
598 | if (rcpts) smtp_address_list_free( rcpts ); | 537 | if (rcpts) smtp_address_list_free( rcpts ); |
599 | } | 538 | } |
600 | 539 | ||
601 | int SMTPwrapper::smtpSend(char*from,clist*rcpts,char*data,size_t size, SMTPaccount *smtp ) | 540 | int SMTPwrapper::smtpSend(char*from,clist*rcpts,char*data,size_t size, SMTPaccount *smtp ) |
602 | { | 541 | { |
603 | char *server, *user, *pass; | 542 | char *server, *user, *pass; |
604 | bool ssl; | 543 | bool ssl; |
605 | uint16_t port; | 544 | uint16_t port; |
606 | mailsmtp *session; | 545 | mailsmtp *session; |
607 | int err,result; | 546 | int err,result; |
608 | 547 | ||
609 | result = 1; | 548 | result = 1; |
610 | server = user = pass = 0; | 549 | server = user = pass = 0; |
611 | server = strdup( smtp->getServer().latin1() ); | 550 | server = strdup( smtp->getServer().latin1() ); |
612 | ssl = smtp->getSSL(); | 551 | ssl = smtp->getSSL(); |
613 | port = smtp->getPort().toUInt(); | 552 | port = smtp->getPort().toUInt(); |
614 | 553 | ||
615 | session = mailsmtp_new( 20, &progress ); | 554 | session = mailsmtp_new( 20, &progress ); |
616 | if ( session == NULL ) goto free_mem; | 555 | if ( session == NULL ) goto free_mem; |
617 | 556 | ||
618 | qDebug( "Servername %s at port %i", server, port ); | 557 | qDebug( "Servername %s at port %i", server, port ); |
619 | if ( ssl ) { | 558 | if ( ssl ) { |
620 | qDebug( "SSL session" ); | 559 | qDebug( "SSL session" ); |
621 | err = mailsmtp_ssl_connect( session, server, port ); | 560 | err = mailsmtp_ssl_connect( session, server, port ); |
622 | } else { | 561 | } else { |
623 | qDebug( "No SSL session" ); | 562 | qDebug( "No SSL session" ); |
624 | err = mailsmtp_socket_connect( session, server, port ); | 563 | err = mailsmtp_socket_connect( session, server, port ); |
625 | } | 564 | } |
626 | if ( err != MAILSMTP_NO_ERROR ) {result = 0;goto free_mem_session;} | 565 | if ( err != MAILSMTP_NO_ERROR ) {result = 0;goto free_mem_session;} |
627 | 566 | ||
628 | err = mailsmtp_init( session ); | 567 | err = mailsmtp_init( session ); |
629 | if ( err != MAILSMTP_NO_ERROR ) {result = 0; goto free_con_session;} | 568 | if ( err != MAILSMTP_NO_ERROR ) {result = 0; goto free_con_session;} |
630 | 569 | ||
631 | qDebug( "INIT OK" ); | 570 | qDebug( "INIT OK" ); |
632 | 571 | ||
633 | if ( smtp->getLogin() ) { | 572 | if ( smtp->getLogin() ) { |
634 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { | 573 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { |
635 | // get'em | 574 | // get'em |
636 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); | 575 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); |
637 | login.show(); | 576 | login.show(); |
638 | if ( QDialog::Accepted == login.exec() ) { | 577 | if ( QDialog::Accepted == login.exec() ) { |
639 | // ok | 578 | // ok |
640 | user = strdup( login.getUser().latin1() ); | 579 | user = strdup( login.getUser().latin1() ); |
641 | pass = strdup( login.getPassword().latin1() ); | 580 | pass = strdup( login.getPassword().latin1() ); |
642 | } else { | 581 | } else { |
643 | result = 0; goto free_con_session; | 582 | result = 0; goto free_con_session; |
644 | } | 583 | } |
645 | } else { | 584 | } else { |
646 | user = strdup( smtp->getUser().latin1() ); | 585 | user = strdup( smtp->getUser().latin1() ); |
647 | pass = strdup( smtp->getPassword().latin1() ); | 586 | pass = strdup( smtp->getPassword().latin1() ); |
648 | } | 587 | } |
649 | qDebug( "session->auth: %i", session->auth); | 588 | qDebug( "session->auth: %i", session->auth); |
650 | err = mailsmtp_auth( session, user, pass ); | 589 | err = mailsmtp_auth( session, user, pass ); |
651 | if ( err == MAILSMTP_NO_ERROR ) qDebug("auth ok"); | 590 | if ( err == MAILSMTP_NO_ERROR ) qDebug("auth ok"); |
652 | qDebug( "Done auth!" ); | 591 | qDebug( "Done auth!" ); |
653 | } | 592 | } |
654 | 593 | ||
655 | err = mailsmtp_send( session, from, rcpts, data, size ); | 594 | err = mailsmtp_send( session, from, rcpts, data, size ); |
656 | if ( err != MAILSMTP_NO_ERROR ) { | 595 | if ( err != MAILSMTP_NO_ERROR ) { |
657 | qDebug("Error sending mail: %s",mailsmtpError(err).latin1()); | 596 | qDebug("Error sending mail: %s",mailsmtpError(err).latin1()); |
658 | result = 0; goto free_con_session; | 597 | result = 0; goto free_con_session; |
659 | } | 598 | } |
660 | 599 | ||
661 | qDebug( "Mail sent." ); | 600 | qDebug( "Mail sent." ); |
662 | storeMail(data,size,"Sent"); | 601 | storeMail(data,size,"Sent"); |
663 | 602 | ||
664 | free_con_session: | 603 | free_con_session: |
665 | mailsmtp_quit( session ); | 604 | mailsmtp_quit( session ); |
666 | free_mem_session: | 605 | free_mem_session: |
667 | mailsmtp_free( session ); | 606 | mailsmtp_free( session ); |
668 | free_mem: | 607 | free_mem: |
669 | if (server) free( server ); | 608 | if (server) free( server ); |
670 | if ( smtp->getLogin() ) { | 609 | if ( smtp->getLogin() ) { |
671 | free( user ); | 610 | free( user ); |
672 | free( pass ); | 611 | free( pass ); |
673 | } | 612 | } |
674 | return result; | 613 | return result; |
675 | } | 614 | } |
676 | 615 | ||
677 | void SMTPwrapper::sendMail(const Mail&mail,bool later ) | 616 | void SMTPwrapper::sendMail(const Mail&mail,bool later ) |
678 | { | 617 | { |
679 | mailmime * mimeMail; | 618 | mailmime * mimeMail; |
680 | 619 | ||
681 | SMTPaccount *smtp = getAccount(mail.getName()); | 620 | SMTPaccount *smtp = getAccount(mail.getName()); |
682 | 621 | ||
683 | mimeMail = createMimeMail(mail ); | 622 | mimeMail = createMimeMail(mail ); |
684 | if ( mimeMail == NULL ) { | 623 | if ( mimeMail == NULL ) { |
685 | qDebug( "sendMail: error creating mime mail" ); | 624 | qDebug( "sendMail: error creating mime mail" ); |
686 | } else { | 625 | } else { |
687 | sendProgress = new progressMailSend(); | 626 | sendProgress = new progressMailSend(); |
688 | sendProgress->show(); | 627 | sendProgress->show(); |
689 | sendProgress->setMaxMails(1); | 628 | sendProgress->setMaxMails(1); |
690 | smtpSend( mimeMail,later,smtp); | 629 | smtpSend( mimeMail,later,smtp); |
691 | mailmime_free( mimeMail ); | ||
692 | qDebug("Clean up done"); | 630 | qDebug("Clean up done"); |
693 | sendProgress->hide(); | 631 | sendProgress->hide(); |
694 | delete sendProgress; | 632 | delete sendProgress; |
695 | sendProgress = 0; | 633 | sendProgress = 0; |
634 | mailmime_free( mimeMail ); | ||
696 | } | 635 | } |
697 | } | 636 | } |
698 | 637 | ||
699 | int SMTPwrapper::sendQueuedMail(MBOXwrapper*wrap,SMTPaccount*smtp,RecMail*which) | 638 | int SMTPwrapper::sendQueuedMail(MBOXwrapper*wrap,SMTPaccount*smtp,RecMail*which) |
700 | { | 639 | { |
701 | char*data = 0; | 640 | char*data = 0; |
702 | size_t length = 0; | 641 | size_t length = 0; |
703 | size_t curTok = 0; | 642 | size_t curTok = 0; |
704 | mailimf_fields *fields = 0; | 643 | mailimf_fields *fields = 0; |
705 | mailimf_field*ffrom = 0; | 644 | mailimf_field*ffrom = 0; |
706 | clist *rcpts = 0; | 645 | clist *rcpts = 0; |
707 | char*from = 0; | 646 | char*from = 0; |
708 | int res = 0; | 647 | int res = 0; |
709 | 648 | ||
710 | wrap->fetchRawBody(*which,&data,&length); | 649 | wrap->fetchRawBody(*which,&data,&length); |
711 | if (!data) return 0; | 650 | if (!data) return 0; |
712 | int err = mailimf_fields_parse( data, length, &curTok, &fields ); | 651 | int err = mailimf_fields_parse( data, length, &curTok, &fields ); |
713 | if (err != MAILIMF_NO_ERROR) { | 652 | if (err != MAILIMF_NO_ERROR) { |
714 | free(data); | 653 | free(data); |
715 | delete wrap; | 654 | delete wrap; |
716 | return 0; | 655 | return 0; |
717 | } | 656 | } |
718 | 657 | ||
719 | rcpts = createRcptList( fields ); | 658 | rcpts = createRcptList( fields ); |
720 | ffrom = getField(fields, MAILIMF_FIELD_FROM ); | 659 | ffrom = getField(fields, MAILIMF_FIELD_FROM ); |
721 | from = getFrom(ffrom); | 660 | from = getFrom(ffrom); |
722 | 661 | ||
723 | qDebug("Size: %i vs. %i",length,strlen(data)); | 662 | qDebug("Size: %i vs. %i",length,strlen(data)); |
724 | if (rcpts && from) { | 663 | if (rcpts && from) { |
725 | res = smtpSend(from,rcpts,data,length,smtp ); | 664 | res = smtpSend(from,rcpts,data,length,smtp ); |
726 | } | 665 | } |
727 | if (fields) { | 666 | if (fields) { |
728 | mailimf_fields_free(fields); | 667 | mailimf_fields_free(fields); |
729 | fields = 0; | 668 | fields = 0; |
730 | } | 669 | } |
731 | if (data) { | 670 | if (data) { |
732 | free(data); | 671 | free(data); |
733 | } | 672 | } |
734 | if (from) { | 673 | if (from) { |
735 | free(from); | 674 | free(from); |
736 | } | 675 | } |
737 | if (rcpts) { | 676 | if (rcpts) { |
738 | smtp_address_list_free( rcpts ); | 677 | smtp_address_list_free( rcpts ); |
739 | } | 678 | } |
740 | return res; | 679 | return res; |
741 | } | 680 | } |
742 | 681 | ||
743 | /* this is a special fun */ | 682 | /* this is a special fun */ |
744 | bool SMTPwrapper::flushOutbox(SMTPaccount*smtp) | 683 | bool SMTPwrapper::flushOutbox(SMTPaccount*smtp) |
745 | { | 684 | { |
746 | bool returnValue = true; | 685 | bool returnValue = true; |
747 | 686 | ||
748 | if (!smtp) return false; | 687 | if (!smtp) return false; |
749 | 688 | ||
750 | QString localfolders = (QString) getenv( "HOME" ) + QString("/Applications/opiemail/localmail/"); | 689 | QString localfolders = (QString) getenv( "HOME" ) + QString("/Applications/opiemail/localmail/"); |
751 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); | 690 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); |
752 | if (!wrap) { | 691 | if (!wrap) { |
753 | qDebug("memory error"); | 692 | qDebug("memory error"); |
754 | return false; | 693 | return false; |
755 | } | 694 | } |
756 | QList<RecMail> mailsToSend; | 695 | QList<RecMail> mailsToSend; |
757 | QList<RecMail> mailsToRemove; | 696 | QList<RecMail> mailsToRemove; |
758 | QString mbox("Outgoing"); | 697 | QString mbox("Outgoing"); |
759 | wrap->listMessages(mbox,mailsToSend); | 698 | wrap->listMessages(mbox,mailsToSend); |
760 | if (mailsToSend.count()==0) { | 699 | if (mailsToSend.count()==0) { |
761 | delete wrap; | 700 | delete wrap; |
762 | return false; | 701 | return false; |
763 | } | 702 | } |
764 | mailsToSend.setAutoDelete(false); | 703 | mailsToSend.setAutoDelete(false); |
765 | sendProgress = new progressMailSend(); | 704 | sendProgress = new progressMailSend(); |
766 | sendProgress->show(); | 705 | sendProgress->show(); |
767 | sendProgress->setMaxMails(mailsToSend.count()); | 706 | sendProgress->setMaxMails(mailsToSend.count()); |
768 | 707 | ||
769 | while (mailsToSend.count()>0) { | 708 | while (mailsToSend.count()>0) { |
770 | if (sendQueuedMail(wrap,smtp,mailsToSend.at(0))==0) { | 709 | if (sendQueuedMail(wrap,smtp,mailsToSend.at(0))==0) { |
771 | QMessageBox::critical(0,tr("Error sending mail"), | 710 | QMessageBox::critical(0,tr("Error sending mail"), |
772 | tr("Error sending queued mail - breaking")); | 711 | tr("Error sending queued mail - breaking")); |
773 | |||
774 | returnValue = false; | 712 | returnValue = false; |
775 | break; | 713 | break; |
776 | } | 714 | } |
777 | mailsToRemove.append(mailsToSend.at(0)); | 715 | mailsToRemove.append(mailsToSend.at(0)); |
778 | mailsToSend.removeFirst(); | 716 | mailsToSend.removeFirst(); |
779 | sendProgress->setCurrentMails(mailsToRemove.count()); | 717 | sendProgress->setCurrentMails(mailsToRemove.count()); |
780 | } | 718 | } |
781 | sendProgress->hide(); | 719 | sendProgress->hide(); |
782 | delete sendProgress; | 720 | delete sendProgress; |
783 | sendProgress = 0; | 721 | sendProgress = 0; |
784 | wrap->deleteMails(mbox,mailsToRemove); | 722 | wrap->deleteMails(mbox,mailsToRemove); |
785 | mailsToSend.setAutoDelete(true); | 723 | mailsToSend.setAutoDelete(true); |
786 | delete wrap; | 724 | delete wrap; |
787 | return returnValue; | 725 | return returnValue; |
788 | } | 726 | } |
diff --git a/noncore/net/mail/libmailwrapper/smtpwrapper.h b/noncore/net/mail/libmailwrapper/smtpwrapper.h index 66180b7..f734fa4 100644 --- a/noncore/net/mail/libmailwrapper/smtpwrapper.h +++ b/noncore/net/mail/libmailwrapper/smtpwrapper.h | |||
@@ -1,65 +1,62 @@ | |||
1 | #ifndef SMTPwrapper_H | 1 | #ifndef SMTPwrapper_H |
2 | #define SMTPwrapper_H | 2 | #define SMTPwrapper_H |
3 | 3 | ||
4 | #include <qpe/applnk.h> | 4 | #include <qpe/applnk.h> |
5 | 5 | ||
6 | #include <qbitarray.h> | 6 | #include <qbitarray.h> |
7 | #include <qdatetime.h> | 7 | #include <qdatetime.h> |
8 | #include <libetpan/clist.h> | 8 | #include <libetpan/clist.h> |
9 | 9 | ||
10 | #include "settings.h" | 10 | #include "settings.h" |
11 | 11 | ||
12 | class Mail; | 12 | class Mail; |
13 | class MBOXwrapper; | 13 | class MBOXwrapper; |
14 | class RecMail; | 14 | class RecMail; |
15 | class Attachment; | 15 | class Attachment; |
16 | struct mailimf_fields; | 16 | struct mailimf_fields; |
17 | struct mailimf_field; | 17 | struct mailimf_field; |
18 | struct mailimf_mailbox; | 18 | struct mailimf_mailbox; |
19 | struct mailmime; | 19 | struct mailmime; |
20 | struct mailimf_address_list; | 20 | struct mailimf_address_list; |
21 | class progressMailSend; | 21 | class progressMailSend; |
22 | 22 | ||
23 | class SMTPwrapper : public QObject | 23 | class SMTPwrapper : public QObject |
24 | { | 24 | { |
25 | Q_OBJECT | 25 | Q_OBJECT |
26 | 26 | ||
27 | public: | 27 | public: |
28 | SMTPwrapper( Settings *s ); | 28 | SMTPwrapper( Settings *s ); |
29 | virtual ~SMTPwrapper(){} | 29 | virtual ~SMTPwrapper(){} |
30 | void sendMail(const Mail& mail,bool later=false ); | 30 | void sendMail(const Mail& mail,bool later=false ); |
31 | bool flushOutbox(SMTPaccount*smtp); | 31 | bool flushOutbox(SMTPaccount*smtp); |
32 | 32 | ||
33 | static progressMailSend*sendProgress; | 33 | static progressMailSend*sendProgress; |
34 | protected: | 34 | protected: |
35 | mailimf_mailbox *newMailbox(const QString&name,const QString&mail ); | 35 | mailimf_mailbox *newMailbox(const QString&name,const QString&mail ); |
36 | mailimf_fields *createImfFields(const Mail &mail ); | 36 | mailimf_fields *createImfFields(const Mail &mail ); |
37 | mailmime *createMimeMail(const Mail&mail ); | 37 | mailmime *createMimeMail(const Mail&mail ); |
38 | 38 | ||
39 | mailimf_address_list *parseAddresses(const QString&addr ); | 39 | mailimf_address_list *parseAddresses(const QString&addr ); |
40 | void addFileParts( mailmime *message,const QList<Attachment>&files ); | 40 | void addFileParts( mailmime *message,const QList<Attachment>&files ); |
41 | mailmime *buildTxtPart(const QString&str ); | 41 | mailmime *buildTxtPart(const QString&str ); |
42 | mailmime *buildFilePart(const QString&filename,const QString&mimetype,const QString&content); | 42 | mailmime *buildFilePart(const QString&filename,const QString&mimetype,const QString&content); |
43 | void smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ); | 43 | void smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ); |
44 | clist *createRcptList( mailimf_fields *fields ); | 44 | clist *createRcptList( mailimf_fields *fields ); |
45 | SMTPaccount *getAccount(const QString&from ); | 45 | SMTPaccount *getAccount(const QString&from ); |
46 | void writeToFile(const QString&file, mailmime *mail ); | ||
47 | void readFromFile(const QString&file, char **data, size_t *size ); | ||
48 | 46 | ||
49 | static void storeMail(char*mail, size_t length, const QString&box); | 47 | static void storeMail(char*mail, size_t length, const QString&box); |
50 | static QString mailsmtpError( int err ); | 48 | static QString mailsmtpError( int err ); |
51 | static QString getTmpFile(); | ||
52 | static void progress( size_t current, size_t maximum ); | 49 | static void progress( size_t current, size_t maximum ); |
53 | static void addRcpts( clist *list, mailimf_address_list *addr_list ); | 50 | static void addRcpts( clist *list, mailimf_address_list *addr_list ); |
54 | static char *getFrom( mailmime *mail ); | 51 | static char *getFrom( mailmime *mail ); |
55 | static char *getFrom( mailimf_field *ffrom); | 52 | static char *getFrom( mailimf_field *ffrom); |
56 | static mailimf_field *getField( mailimf_fields *fields, int type ); | 53 | static mailimf_field *getField( mailimf_fields *fields, int type ); |
57 | int smtpSend(char*from,clist*rcpts,char*data,size_t size, SMTPaccount *smtp ); | 54 | int smtpSend(char*from,clist*rcpts,char*data,size_t size, SMTPaccount *smtp ); |
58 | 55 | ||
59 | void storeMail(mailmime*mail, const QString&box); | 56 | void storeMail(mailmime*mail, const QString&box); |
60 | Settings *settings; | 57 | Settings *settings; |
61 | 58 | ||
62 | int sendQueuedMail(MBOXwrapper*wrap,SMTPaccount*smtp,RecMail*which); | 59 | int sendQueuedMail(MBOXwrapper*wrap,SMTPaccount*smtp,RecMail*which); |
63 | }; | 60 | }; |
64 | 61 | ||
65 | #endif | 62 | #endif |
diff --git a/noncore/net/mail/smtpwrapper.cpp b/noncore/net/mail/smtpwrapper.cpp index 285561c..521cd0a 100644 --- a/noncore/net/mail/smtpwrapper.cpp +++ b/noncore/net/mail/smtpwrapper.cpp | |||
@@ -1,788 +1,726 @@ | |||
1 | #include <stdlib.h> | 1 | #include <stdlib.h> |
2 | #include <sys/stat.h> | 2 | #include <sys/stat.h> |
3 | #include <sys/types.h> | 3 | #include <sys/types.h> |
4 | #include <unistd.h> | 4 | #include <unistd.h> |
5 | #include <fcntl.h> | 5 | #include <fcntl.h> |
6 | #include <string.h> | 6 | #include <string.h> |
7 | #include <qdir.h> | 7 | #include <qdir.h> |
8 | #include <qt.h> | 8 | #include <qt.h> |
9 | 9 | ||
10 | #include <libetpan/libetpan.h> | 10 | #include <libetpan/libetpan.h> |
11 | 11 | ||
12 | #include "smtpwrapper.h" | 12 | #include "smtpwrapper.h" |
13 | #include "mailwrapper.h" | 13 | #include "mailwrapper.h" |
14 | #include "mboxwrapper.h" | 14 | #include "mboxwrapper.h" |
15 | #include "logindialog.h" | 15 | #include "logindialog.h" |
16 | #include "mailtypes.h" | 16 | #include "mailtypes.h" |
17 | #include "defines.h" | 17 | #include "defines.h" |
18 | #include "sendmailprogress.h" | 18 | #include "sendmailprogress.h" |
19 | 19 | ||
20 | progressMailSend*SMTPwrapper::sendProgress = 0; | 20 | progressMailSend*SMTPwrapper::sendProgress = 0; |
21 | 21 | ||
22 | SMTPwrapper::SMTPwrapper( Settings *s ) | 22 | SMTPwrapper::SMTPwrapper( Settings *s ) |
23 | : QObject() | 23 | : QObject() |
24 | { | 24 | { |
25 | settings = s; | 25 | settings = s; |
26 | } | 26 | } |
27 | 27 | ||
28 | QString SMTPwrapper::mailsmtpError( int errnum ) | 28 | QString SMTPwrapper::mailsmtpError( int errnum ) |
29 | { | 29 | { |
30 | switch ( errnum ) { | 30 | switch ( errnum ) { |
31 | case MAILSMTP_NO_ERROR: | 31 | case MAILSMTP_NO_ERROR: |
32 | return tr( "No error" ); | 32 | return tr( "No error" ); |
33 | case MAILSMTP_ERROR_UNEXPECTED_CODE: | 33 | case MAILSMTP_ERROR_UNEXPECTED_CODE: |
34 | return tr( "Unexpected error code" ); | 34 | return tr( "Unexpected error code" ); |
35 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: | 35 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: |
36 | return tr( "Service not available" ); | 36 | return tr( "Service not available" ); |
37 | case MAILSMTP_ERROR_STREAM: | 37 | case MAILSMTP_ERROR_STREAM: |
38 | return tr( "Stream error" ); | 38 | return tr( "Stream error" ); |
39 | case MAILSMTP_ERROR_HOSTNAME: | 39 | case MAILSMTP_ERROR_HOSTNAME: |
40 | return tr( "gethostname() failed" ); | 40 | return tr( "gethostname() failed" ); |
41 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: | 41 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: |
42 | return tr( "Not implemented" ); | 42 | return tr( "Not implemented" ); |
43 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: | 43 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: |
44 | return tr( "Error, action not taken" ); | 44 | return tr( "Error, action not taken" ); |
45 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: | 45 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: |
46 | return tr( "Data exceeds storage allocation" ); | 46 | return tr( "Data exceeds storage allocation" ); |
47 | case MAILSMTP_ERROR_IN_PROCESSING: | 47 | case MAILSMTP_ERROR_IN_PROCESSING: |
48 | return tr( "Error in processing" ); | 48 | return tr( "Error in processing" ); |
49 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: | 49 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: |
50 | // return tr( "Insufficient system storage" ); | 50 | // return tr( "Insufficient system storage" ); |
51 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: | 51 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: |
52 | return tr( "Mailbox unavailable" ); | 52 | return tr( "Mailbox unavailable" ); |
53 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: | 53 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: |
54 | return tr( "Mailbox name not allowed" ); | 54 | return tr( "Mailbox name not allowed" ); |
55 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: | 55 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: |
56 | return tr( "Bad command sequence" ); | 56 | return tr( "Bad command sequence" ); |
57 | case MAILSMTP_ERROR_USER_NOT_LOCAL: | 57 | case MAILSMTP_ERROR_USER_NOT_LOCAL: |
58 | return tr( "User not local" ); | 58 | return tr( "User not local" ); |
59 | case MAILSMTP_ERROR_TRANSACTION_FAILED: | 59 | case MAILSMTP_ERROR_TRANSACTION_FAILED: |
60 | return tr( "Transaction failed" ); | 60 | return tr( "Transaction failed" ); |
61 | case MAILSMTP_ERROR_MEMORY: | 61 | case MAILSMTP_ERROR_MEMORY: |
62 | return tr( "Memory error" ); | 62 | return tr( "Memory error" ); |
63 | case MAILSMTP_ERROR_CONNECTION_REFUSED: | 63 | case MAILSMTP_ERROR_CONNECTION_REFUSED: |
64 | return tr( "Connection refused" ); | 64 | return tr( "Connection refused" ); |
65 | default: | 65 | default: |
66 | return tr( "Unknown error code" ); | 66 | return tr( "Unknown error code" ); |
67 | } | 67 | } |
68 | } | 68 | } |
69 | 69 | ||
70 | mailimf_mailbox *SMTPwrapper::newMailbox(const QString&name, const QString&mail ) | 70 | mailimf_mailbox *SMTPwrapper::newMailbox(const QString&name, const QString&mail ) |
71 | { | 71 | { |
72 | return mailimf_mailbox_new( strdup( name.latin1() ), | 72 | return mailimf_mailbox_new( strdup( name.latin1() ), |
73 | strdup( mail.latin1() ) ); | 73 | strdup( mail.latin1() ) ); |
74 | } | 74 | } |
75 | 75 | ||
76 | mailimf_address_list *SMTPwrapper::parseAddresses(const QString&addr ) | 76 | mailimf_address_list *SMTPwrapper::parseAddresses(const QString&addr ) |
77 | { | 77 | { |
78 | mailimf_address_list *addresses; | 78 | mailimf_address_list *addresses; |
79 | 79 | ||
80 | if ( addr.isEmpty() ) return NULL; | 80 | if ( addr.isEmpty() ) return NULL; |
81 | 81 | ||
82 | addresses = mailimf_address_list_new_empty(); | 82 | addresses = mailimf_address_list_new_empty(); |
83 | 83 | ||
84 | bool literal_open = false; | 84 | bool literal_open = false; |
85 | unsigned int startpos = 0; | 85 | unsigned int startpos = 0; |
86 | QStringList list; | 86 | QStringList list; |
87 | QString s; | 87 | QString s; |
88 | unsigned int i = 0; | 88 | unsigned int i = 0; |
89 | for (; i < addr.length();++i) { | 89 | for (; i < addr.length();++i) { |
90 | switch (addr[i]) { | 90 | switch (addr[i]) { |
91 | case '\"': | 91 | case '\"': |
92 | literal_open = !literal_open; | 92 | literal_open = !literal_open; |
93 | break; | 93 | break; |
94 | case ',': | 94 | case ',': |
95 | if (!literal_open) { | 95 | if (!literal_open) { |
96 | s = addr.mid(startpos,i-startpos); | 96 | s = addr.mid(startpos,i-startpos); |
97 | if (!s.isEmpty()) { | 97 | if (!s.isEmpty()) { |
98 | list.append(s); | 98 | list.append(s); |
99 | qDebug("Appended %s",s.latin1()); | 99 | qDebug("Appended %s",s.latin1()); |
100 | } | 100 | } |
101 | // !!!! this is a MUST BE! | 101 | // !!!! this is a MUST BE! |
102 | startpos = ++i; | 102 | startpos = ++i; |
103 | } | 103 | } |
104 | break; | 104 | break; |
105 | default: | 105 | default: |
106 | break; | 106 | break; |
107 | } | 107 | } |
108 | } | 108 | } |
109 | s = addr.mid(startpos,i-startpos); | 109 | s = addr.mid(startpos,i-startpos); |
110 | if (!s.isEmpty()) { | 110 | if (!s.isEmpty()) { |
111 | list.append(s); | 111 | list.append(s); |
112 | qDebug("Appended %s",s.latin1()); | 112 | qDebug("Appended %s",s.latin1()); |
113 | } | 113 | } |
114 | QStringList::Iterator it; | 114 | QStringList::Iterator it; |
115 | for ( it = list.begin(); it != list.end(); it++ ) { | 115 | for ( it = list.begin(); it != list.end(); it++ ) { |
116 | char *str = strdup( (*it).latin1() ); | 116 | int err = mailimf_address_list_add_parse( addresses, (char*)(*it).latin1() ); |
117 | int err = mailimf_address_list_add_parse( addresses, str ); | ||
118 | if ( err != MAILIMF_NO_ERROR ) { | 117 | if ( err != MAILIMF_NO_ERROR ) { |
119 | qDebug( "Error parsing" ); | 118 | qDebug( "Error parsing" ); |
120 | qDebug( *it ); | 119 | qDebug( *it ); |
121 | free( str ); | ||
122 | } else { | 120 | } else { |
123 | qDebug( "Parse success! %s",(*it).latin1()); | 121 | qDebug( "Parse success! %s",(*it).latin1()); |
124 | } | 122 | } |
125 | } | 123 | } |
126 | return addresses; | 124 | return addresses; |
127 | } | 125 | } |
128 | 126 | ||
129 | mailimf_fields *SMTPwrapper::createImfFields(const Mail&mail ) | 127 | mailimf_fields *SMTPwrapper::createImfFields(const Mail&mail ) |
130 | { | 128 | { |
131 | mailimf_fields *fields; | 129 | mailimf_fields *fields; |
132 | mailimf_field *xmailer; | 130 | mailimf_field *xmailer; |
133 | mailimf_mailbox *sender, *fromBox; | 131 | mailimf_mailbox *sender, *fromBox; |
134 | mailimf_mailbox_list *from; | 132 | mailimf_mailbox_list *from; |
135 | mailimf_address_list *to, *cc, *bcc, *reply; | 133 | mailimf_address_list *to, *cc, *bcc, *reply; |
136 | char *subject = strdup( mail.getSubject().latin1() ); | 134 | char *subject = strdup( mail.getSubject().latin1() ); |
137 | int err; | 135 | int err; |
138 | 136 | ||
139 | sender = newMailbox( mail.getName(), mail.getMail() ); | 137 | sender = newMailbox( mail.getName(), mail.getMail() ); |
140 | if ( sender == NULL ) goto err_free; | 138 | if ( sender == NULL ) goto err_free; |
141 | 139 | ||
142 | fromBox = newMailbox( mail.getName(), mail.getMail() ); | 140 | fromBox = newMailbox( mail.getName(), mail.getMail() ); |
143 | if ( fromBox == NULL ) goto err_free_sender; | 141 | if ( fromBox == NULL ) goto err_free_sender; |
144 | 142 | ||
145 | from = mailimf_mailbox_list_new_empty(); | 143 | from = mailimf_mailbox_list_new_empty(); |
146 | if ( from == NULL ) goto err_free_fromBox; | 144 | if ( from == NULL ) goto err_free_fromBox; |
147 | 145 | ||
148 | err = mailimf_mailbox_list_add( from, fromBox ); | 146 | err = mailimf_mailbox_list_add( from, fromBox ); |
149 | if ( err != MAILIMF_NO_ERROR ) goto err_free_from; | 147 | if ( err != MAILIMF_NO_ERROR ) goto err_free_from; |
150 | 148 | ||
151 | to = parseAddresses( mail.getTo() ); | 149 | to = parseAddresses( mail.getTo() ); |
152 | if ( to == NULL ) goto err_free_from; | 150 | if ( to == NULL ) goto err_free_from; |
153 | 151 | ||
154 | cc = parseAddresses( mail.getCC() ); | 152 | cc = parseAddresses( mail.getCC() ); |
155 | bcc = parseAddresses( mail.getBCC() ); | 153 | bcc = parseAddresses( mail.getBCC() ); |
156 | reply = parseAddresses( mail.getReply() ); | 154 | reply = parseAddresses( mail.getReply() ); |
157 | 155 | ||
158 | fields = mailimf_fields_new_with_data( from, sender, reply, to, cc, bcc, | 156 | fields = mailimf_fields_new_with_data( from, sender, reply, to, cc, bcc, |
159 | NULL, NULL, subject ); | 157 | NULL, NULL, subject ); |
160 | if ( fields == NULL ) goto err_free_reply; | 158 | if ( fields == NULL ) goto err_free_reply; |
161 | 159 | ||
162 | xmailer = mailimf_field_new_custom( strdup( "User-Agent" ), | 160 | xmailer = mailimf_field_new_custom( strdup( "User-Agent" ), |
163 | strdup( USER_AGENT ) ); | 161 | strdup( USER_AGENT ) ); |
164 | if ( xmailer == NULL ) goto err_free_fields; | 162 | if ( xmailer == NULL ) goto err_free_fields; |
165 | 163 | ||
166 | err = mailimf_fields_add( fields, xmailer ); | 164 | err = mailimf_fields_add( fields, xmailer ); |
167 | if ( err != MAILIMF_NO_ERROR ) goto err_free_xmailer; | 165 | if ( err != MAILIMF_NO_ERROR ) goto err_free_xmailer; |
168 | 166 | ||
169 | return fields; // Success :) | 167 | return fields; // Success :) |
170 | 168 | ||
171 | err_free_xmailer: | 169 | err_free_xmailer: |
172 | mailimf_field_free( xmailer ); | 170 | mailimf_field_free( xmailer ); |
173 | err_free_fields: | 171 | err_free_fields: |
174 | mailimf_fields_free( fields ); | 172 | mailimf_fields_free( fields ); |
175 | err_free_reply: | 173 | err_free_reply: |
176 | mailimf_address_list_free( reply ); | 174 | mailimf_address_list_free( reply ); |
177 | mailimf_address_list_free( bcc ); | 175 | mailimf_address_list_free( bcc ); |
178 | mailimf_address_list_free( cc ); | 176 | mailimf_address_list_free( cc ); |
179 | mailimf_address_list_free( to ); | 177 | mailimf_address_list_free( to ); |
180 | err_free_from: | 178 | err_free_from: |
181 | mailimf_mailbox_list_free( from ); | 179 | mailimf_mailbox_list_free( from ); |
182 | err_free_fromBox: | 180 | err_free_fromBox: |
183 | mailimf_mailbox_free( fromBox ); | 181 | mailimf_mailbox_free( fromBox ); |
184 | err_free_sender: | 182 | err_free_sender: |
185 | mailimf_mailbox_free( sender ); | 183 | mailimf_mailbox_free( sender ); |
186 | err_free: | 184 | err_free: |
187 | free( subject ); | 185 | free( subject ); |
188 | qDebug( "createImfFields - error" ); | 186 | qDebug( "createImfFields - error" ); |
189 | 187 | ||
190 | return NULL; // Error :( | 188 | return NULL; // Error :( |
191 | } | 189 | } |
192 | 190 | ||
193 | mailmime *SMTPwrapper::buildTxtPart(const QString&str ) | 191 | mailmime *SMTPwrapper::buildTxtPart(const QString&str ) |
194 | { | 192 | { |
195 | mailmime *txtPart; | 193 | mailmime *txtPart; |
196 | mailmime_fields *fields; | 194 | mailmime_fields *fields; |
197 | mailmime_content *content; | 195 | mailmime_content *content; |
198 | mailmime_parameter *param; | 196 | mailmime_parameter *param; |
199 | char *txt = strdup( str.latin1() ); | ||
200 | int err; | 197 | int err; |
201 | 198 | ||
202 | param = mailmime_parameter_new( strdup( "charset" ), | 199 | param = mailmime_parameter_new( strdup( "charset" ), |
203 | strdup( "iso-8859-1" ) ); | 200 | strdup( "iso-8859-1" ) ); |
204 | if ( param == NULL ) goto err_free; | 201 | if ( param == NULL ) goto err_free; |
205 | 202 | ||
206 | content = mailmime_content_new_with_str( "text/plain" ); | 203 | content = mailmime_content_new_with_str( "text/plain" ); |
207 | if ( content == NULL ) goto err_free_param; | 204 | if ( content == NULL ) goto err_free_param; |
208 | 205 | ||
209 | err = clist_append( content->ct_parameters, param ); | 206 | err = clist_append( content->ct_parameters, param ); |
210 | if ( err != MAILIMF_NO_ERROR ) goto err_free_content; | 207 | if ( err != MAILIMF_NO_ERROR ) goto err_free_content; |
211 | 208 | ||
212 | fields = mailmime_fields_new_encoding( MAILMIME_MECHANISM_8BIT ); | 209 | fields = mailmime_fields_new_encoding(MAILMIME_MECHANISM_QUOTED_PRINTABLE); |
213 | if ( fields == NULL ) goto err_free_content; | 210 | if ( fields == NULL ) goto err_free_content; |
214 | 211 | ||
215 | txtPart = mailmime_new_empty( content, fields ); | 212 | txtPart = mailmime_new_empty( content, fields ); |
216 | if ( txtPart == NULL ) goto err_free_fields; | 213 | if ( txtPart == NULL ) goto err_free_fields; |
217 | 214 | ||
218 | err = mailmime_set_body_text( txtPart, txt, strlen( txt ) ); | 215 | err = mailmime_set_body_text( txtPart, (char*)str.data(), str.length() ); |
219 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; | 216 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; |
220 | 217 | ||
221 | return txtPart; // Success :) | 218 | return txtPart; // Success :) |
222 | 219 | ||
223 | err_free_txtPart: | 220 | err_free_txtPart: |
224 | mailmime_free( txtPart ); | 221 | mailmime_free( txtPart ); |
225 | err_free_fields: | 222 | err_free_fields: |
226 | mailmime_fields_free( fields ); | 223 | mailmime_fields_free( fields ); |
227 | err_free_content: | 224 | err_free_content: |
228 | mailmime_content_free( content ); | 225 | mailmime_content_free( content ); |
229 | err_free_param: | 226 | err_free_param: |
230 | mailmime_parameter_free( param ); | 227 | mailmime_parameter_free( param ); |
231 | err_free: | 228 | err_free: |
232 | free( txt ); | ||
233 | qDebug( "buildTxtPart - error" ); | 229 | qDebug( "buildTxtPart - error" ); |
234 | 230 | ||
235 | return NULL; // Error :( | 231 | return NULL; // Error :( |
236 | } | 232 | } |
237 | 233 | ||
238 | mailmime *SMTPwrapper::buildFilePart(const QString&filename,const QString&mimetype,const QString&TextContent ) | 234 | mailmime *SMTPwrapper::buildFilePart(const QString&filename,const QString&mimetype,const QString&TextContent ) |
239 | { | 235 | { |
240 | mailmime * filePart = 0; | 236 | mailmime * filePart = 0; |
241 | mailmime_fields * fields = 0; | 237 | mailmime_fields * fields = 0; |
242 | mailmime_content * content = 0; | 238 | mailmime_content * content = 0; |
243 | mailmime_parameter * param = 0; | 239 | mailmime_parameter * param = 0; |
244 | char*name = 0; | 240 | char*name = 0; |
245 | char*file = 0; | 241 | char*file = 0; |
246 | int err; | 242 | int err; |
247 | 243 | ||
248 | int pos = filename.findRev( '/' ); | 244 | int pos = filename.findRev( '/' ); |
249 | 245 | ||
250 | if (filename.length()>0) { | 246 | if (filename.length()>0) { |
251 | QString tmp = filename.right( filename.length() - ( pos + 1 ) ); | 247 | QString tmp = filename.right( filename.length() - ( pos + 1 ) ); |
252 | name = strdup( tmp.latin1() ); // just filename | 248 | name = strdup( tmp.latin1() ); // just filename |
253 | file = strdup( filename.latin1() ); // full name with path | 249 | file = strdup( filename.latin1() ); // full name with path |
254 | } | 250 | } |
255 | char *mime = strdup( mimetype.latin1() ); // mimetype -e.g. text/plain | 251 | |
256 | |||
257 | int disptype = MAILMIME_DISPOSITION_TYPE_ATTACHMENT; | 252 | int disptype = MAILMIME_DISPOSITION_TYPE_ATTACHMENT; |
258 | int mechanism = MAILMIME_MECHANISM_BASE64; | 253 | int mechanism = MAILMIME_MECHANISM_BASE64; |
259 | 254 | ||
260 | if ( mimetype.startsWith( "text/" ) ) { | 255 | if ( mimetype.startsWith( "text/" ) ) { |
261 | param = mailmime_parameter_new( strdup( "charset" ), | 256 | param = mailmime_parameter_new( strdup( "charset" ), |
262 | strdup( "iso-8859-1" ) ); | 257 | strdup( "iso-8859-1" ) ); |
263 | disptype = MAILMIME_DISPOSITION_TYPE_INLINE; | ||
264 | mechanism = MAILMIME_MECHANISM_QUOTED_PRINTABLE; | 258 | mechanism = MAILMIME_MECHANISM_QUOTED_PRINTABLE; |
265 | } | 259 | } |
266 | 260 | ||
267 | fields = mailmime_fields_new_filename( | 261 | fields = mailmime_fields_new_filename( |
268 | disptype, name, | 262 | disptype, name, |
269 | mechanism ); | 263 | mechanism ); |
270 | content = mailmime_content_new_with_str( mime ); | 264 | content = mailmime_content_new_with_str( (char*)mimetype.latin1() ); |
271 | if (content!=0 && fields != 0) { | 265 | if (content!=0 && fields != 0) { |
272 | if (param) { | 266 | if (param) { |
273 | clist_append(content->ct_parameters,param); | 267 | clist_append(content->ct_parameters,param); |
274 | param = 0; | 268 | param = 0; |
275 | } | 269 | } |
276 | if (filename.length()>0) { | 270 | if (filename.length()>0) { |
277 | QFileInfo f(filename); | 271 | QFileInfo f(filename); |
278 | param = mailmime_parameter_new(strdup("name"),strdup(f.fileName().latin1())); | 272 | param = mailmime_parameter_new(strdup("name"),strdup(f.fileName().latin1())); |
279 | clist_append(content->ct_parameters,param); | 273 | clist_append(content->ct_parameters,param); |
280 | param = 0; | 274 | param = 0; |
281 | } | 275 | } |
282 | filePart = mailmime_new_empty( content, fields ); | 276 | filePart = mailmime_new_empty( content, fields ); |
283 | } | 277 | } |
284 | if (filePart) { | 278 | if (filePart) { |
285 | if (filename.length()>0) { | 279 | if (filename.length()>0) { |
286 | err = mailmime_set_body_file( filePart, file ); | 280 | err = mailmime_set_body_file( filePart, file ); |
287 | } else { | 281 | } else { |
288 | err = mailmime_set_body_text(filePart,strdup(TextContent.ascii()),TextContent.length()); | 282 | err = mailmime_set_body_text(filePart,strdup(TextContent.data()),TextContent.length()); |
289 | } | 283 | } |
290 | if (err != MAILIMF_NO_ERROR) { | 284 | if (err != MAILIMF_NO_ERROR) { |
291 | qDebug("Error setting body with file %s",file); | 285 | qDebug("Error setting body with file %s",file); |
292 | mailmime_free( filePart ); | 286 | mailmime_free( filePart ); |
293 | filePart = 0; | 287 | filePart = 0; |
294 | } | 288 | } |
295 | } | 289 | } |
296 | 290 | ||
297 | if (!filePart) { | 291 | if (!filePart) { |
298 | if ( param != NULL ) { | 292 | if ( param != NULL ) { |
299 | mailmime_parameter_free( param ); | 293 | mailmime_parameter_free( param ); |
300 | } | 294 | } |
301 | if (content) { | 295 | if (content) { |
302 | mailmime_content_free( content ); | 296 | mailmime_content_free( content ); |
303 | } else { | ||
304 | if (mime) { | ||
305 | free( mime ); | ||
306 | } | ||
307 | } | 297 | } |
308 | if (fields) { | 298 | if (fields) { |
309 | mailmime_fields_free( fields ); | 299 | mailmime_fields_free( fields ); |
310 | } else { | 300 | } else { |
311 | if (name) { | 301 | if (name) { |
312 | free( name ); | 302 | free( name ); |
313 | } | 303 | } |
314 | if (file) { | 304 | if (file) { |
315 | free( file ); | 305 | free( file ); |
316 | } | 306 | } |
317 | } | 307 | } |
318 | } | 308 | } |
319 | return filePart; // Success :) | 309 | return filePart; // Success :) |
320 | 310 | ||
321 | } | 311 | } |
322 | 312 | ||
323 | void SMTPwrapper::addFileParts( mailmime *message,const QList<Attachment>&files ) | 313 | void SMTPwrapper::addFileParts( mailmime *message,const QList<Attachment>&files ) |
324 | { | 314 | { |
325 | const Attachment *it; | 315 | const Attachment *it; |
326 | unsigned int count = files.count(); | 316 | unsigned int count = files.count(); |
327 | qDebug("List contains %i values",count); | 317 | qDebug("List contains %i values",count); |
328 | for ( unsigned int i = 0; i < count; ++i ) { | 318 | for ( unsigned int i = 0; i < count; ++i ) { |
329 | qDebug( "Adding file" ); | 319 | qDebug( "Adding file" ); |
330 | mailmime *filePart; | 320 | mailmime *filePart; |
331 | int err; | 321 | int err; |
332 | it = ((QList<Attachment>)files).at(i); | 322 | it = ((QList<Attachment>)files).at(i); |
333 | 323 | ||
334 | filePart = buildFilePart( it->getFileName(), it->getMimeType(),"" ); | 324 | filePart = buildFilePart( it->getFileName(), it->getMimeType(),"" ); |
335 | if ( filePart == NULL ) { | 325 | if ( filePart == NULL ) { |
336 | qDebug( "addFileParts: error adding file:" ); | 326 | qDebug( "addFileParts: error adding file:" ); |
337 | qDebug( it->getFileName() ); | 327 | qDebug( it->getFileName() ); |
338 | continue; | 328 | continue; |
339 | } | 329 | } |
340 | err = mailmime_smart_add_part( message, filePart ); | 330 | err = mailmime_smart_add_part( message, filePart ); |
341 | if ( err != MAILIMF_NO_ERROR ) { | 331 | if ( err != MAILIMF_NO_ERROR ) { |
342 | mailmime_free( filePart ); | 332 | mailmime_free( filePart ); |
343 | qDebug("error smart add"); | 333 | qDebug("error smart add"); |
344 | } | 334 | } |
345 | } | 335 | } |
346 | } | 336 | } |
347 | 337 | ||
348 | mailmime *SMTPwrapper::createMimeMail(const Mail &mail ) | 338 | mailmime *SMTPwrapper::createMimeMail(const Mail &mail ) |
349 | { | 339 | { |
350 | mailmime *message, *txtPart; | 340 | mailmime *message, *txtPart; |
351 | mailimf_fields *fields; | 341 | mailimf_fields *fields; |
352 | int err; | 342 | int err; |
353 | 343 | ||
354 | fields = createImfFields( mail ); | 344 | fields = createImfFields( mail ); |
355 | if ( fields == NULL ) goto err_free; | 345 | if ( fields == NULL ) goto err_free; |
356 | 346 | ||
357 | message = mailmime_new_message_data( NULL ); | 347 | message = mailmime_new_message_data( NULL ); |
358 | if ( message == NULL ) goto err_free_fields; | 348 | if ( message == NULL ) goto err_free_fields; |
359 | 349 | ||
360 | mailmime_set_imf_fields( message, fields ); | 350 | mailmime_set_imf_fields( message, fields ); |
361 | 351 | ||
362 | if (mail.getAttachments().count()==0) { | 352 | txtPart = buildTxtPart( mail.getMessage() ); |
363 | txtPart = buildTxtPart( mail.getMessage() ); | 353 | |
364 | } else { | ||
365 | txtPart = buildFilePart("","text/plain",mail.getMessage()); | ||
366 | } | ||
367 | if ( txtPart == NULL ) goto err_free_message; | 354 | if ( txtPart == NULL ) goto err_free_message; |
368 | 355 | ||
369 | err = mailmime_smart_add_part( message, txtPart ); | 356 | err = mailmime_smart_add_part( message, txtPart ); |
370 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; | 357 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; |
371 | 358 | ||
372 | addFileParts( message, mail.getAttachments() ); | 359 | addFileParts( message, mail.getAttachments() ); |
373 | 360 | ||
374 | return message; // Success :) | 361 | return message; // Success :) |
375 | 362 | ||
376 | err_free_txtPart: | 363 | err_free_txtPart: |
377 | mailmime_free( txtPart ); | 364 | mailmime_free( txtPart ); |
378 | err_free_message: | 365 | err_free_message: |
379 | mailmime_free( message ); | 366 | mailmime_free( message ); |
380 | err_free_fields: | 367 | err_free_fields: |
381 | mailimf_fields_free( fields ); | 368 | mailimf_fields_free( fields ); |
382 | err_free: | 369 | err_free: |
383 | qDebug( "createMimeMail: error" ); | 370 | qDebug( "createMimeMail: error" ); |
384 | 371 | ||
385 | return NULL; // Error :( | 372 | return NULL; // Error :( |
386 | } | 373 | } |
387 | 374 | ||
388 | mailimf_field *SMTPwrapper::getField( mailimf_fields *fields, int type ) | 375 | mailimf_field *SMTPwrapper::getField( mailimf_fields *fields, int type ) |
389 | { | 376 | { |
390 | mailimf_field *field; | 377 | mailimf_field *field; |
391 | clistiter *it; | 378 | clistiter *it; |
392 | 379 | ||
393 | it = clist_begin( fields->fld_list ); | 380 | it = clist_begin( fields->fld_list ); |
394 | while ( it ) { | 381 | while ( it ) { |
395 | field = (mailimf_field *) it->data; | 382 | field = (mailimf_field *) it->data; |
396 | if ( field->fld_type == type ) { | 383 | if ( field->fld_type == type ) { |
397 | return field; | 384 | return field; |
398 | } | 385 | } |
399 | it = it->next; | 386 | it = it->next; |
400 | } | 387 | } |
401 | 388 | ||
402 | return NULL; | 389 | return NULL; |
403 | } | 390 | } |
404 | 391 | ||
405 | void SMTPwrapper::addRcpts( clist *list, mailimf_address_list *addr_list ) | 392 | void SMTPwrapper::addRcpts( clist *list, mailimf_address_list *addr_list ) |
406 | { | 393 | { |
407 | clistiter *it, *it2; | 394 | clistiter *it, *it2; |
408 | 395 | ||
409 | for ( it = clist_begin( addr_list->ad_list ); it; it = it->next ) { | 396 | for ( it = clist_begin( addr_list->ad_list ); it; it = it->next ) { |
410 | mailimf_address *addr; | 397 | mailimf_address *addr; |
411 | addr = (mailimf_address *) it->data; | 398 | addr = (mailimf_address *) it->data; |
412 | 399 | ||
413 | if ( addr->ad_type == MAILIMF_ADDRESS_MAILBOX ) { | 400 | if ( addr->ad_type == MAILIMF_ADDRESS_MAILBOX ) { |
414 | esmtp_address_list_add( list, addr->ad_data.ad_mailbox->mb_addr_spec, 0, NULL ); | 401 | esmtp_address_list_add( list, addr->ad_data.ad_mailbox->mb_addr_spec, 0, NULL ); |
415 | } else if ( addr->ad_type == MAILIMF_ADDRESS_GROUP ) { | 402 | } else if ( addr->ad_type == MAILIMF_ADDRESS_GROUP ) { |
416 | clist *l = addr->ad_data.ad_group->grp_mb_list->mb_list; | 403 | clist *l = addr->ad_data.ad_group->grp_mb_list->mb_list; |
417 | for ( it2 = clist_begin( l ); it2; it2 = it2->next ) { | 404 | for ( it2 = clist_begin( l ); it2; it2 = it2->next ) { |
418 | mailimf_mailbox *mbox; | 405 | mailimf_mailbox *mbox; |
419 | mbox = (mailimf_mailbox *) it2->data; | 406 | mbox = (mailimf_mailbox *) it2->data; |
420 | esmtp_address_list_add( list, mbox->mb_addr_spec, 0, NULL ); | 407 | esmtp_address_list_add( list, mbox->mb_addr_spec, 0, NULL ); |
421 | } | 408 | } |
422 | } | 409 | } |
423 | } | 410 | } |
424 | } | 411 | } |
425 | 412 | ||
426 | clist *SMTPwrapper::createRcptList( mailimf_fields *fields ) | 413 | clist *SMTPwrapper::createRcptList( mailimf_fields *fields ) |
427 | { | 414 | { |
428 | clist *rcptList; | 415 | clist *rcptList; |
429 | mailimf_field *field; | 416 | mailimf_field *field; |
430 | 417 | ||
431 | rcptList = esmtp_address_list_new(); | 418 | rcptList = esmtp_address_list_new(); |
432 | 419 | ||
433 | field = getField( fields, MAILIMF_FIELD_TO ); | 420 | field = getField( fields, MAILIMF_FIELD_TO ); |
434 | if ( field && (field->fld_type == MAILIMF_FIELD_TO) | 421 | if ( field && (field->fld_type == MAILIMF_FIELD_TO) |
435 | && field->fld_data.fld_to->to_addr_list ) { | 422 | && field->fld_data.fld_to->to_addr_list ) { |
436 | addRcpts( rcptList, field->fld_data.fld_to->to_addr_list ); | 423 | addRcpts( rcptList, field->fld_data.fld_to->to_addr_list ); |
437 | } | 424 | } |
438 | 425 | ||
439 | field = getField( fields, MAILIMF_FIELD_CC ); | 426 | field = getField( fields, MAILIMF_FIELD_CC ); |
440 | if ( field && (field->fld_type == MAILIMF_FIELD_CC) | 427 | if ( field && (field->fld_type == MAILIMF_FIELD_CC) |
441 | && field->fld_data.fld_cc->cc_addr_list ) { | 428 | && field->fld_data.fld_cc->cc_addr_list ) { |
442 | addRcpts( rcptList, field->fld_data.fld_cc->cc_addr_list ); | 429 | addRcpts( rcptList, field->fld_data.fld_cc->cc_addr_list ); |
443 | } | 430 | } |
444 | 431 | ||
445 | field = getField( fields, MAILIMF_FIELD_BCC ); | 432 | field = getField( fields, MAILIMF_FIELD_BCC ); |
446 | if ( field && (field->fld_type == MAILIMF_FIELD_BCC) | 433 | if ( field && (field->fld_type == MAILIMF_FIELD_BCC) |
447 | && field->fld_data.fld_bcc->bcc_addr_list ) { | 434 | && field->fld_data.fld_bcc->bcc_addr_list ) { |
448 | addRcpts( rcptList, field->fld_data.fld_bcc->bcc_addr_list ); | 435 | addRcpts( rcptList, field->fld_data.fld_bcc->bcc_addr_list ); |
449 | } | 436 | } |
450 | 437 | ||
451 | return rcptList; | 438 | return rcptList; |
452 | } | 439 | } |
453 | 440 | ||
454 | char *SMTPwrapper::getFrom( mailimf_field *ffrom) | 441 | char *SMTPwrapper::getFrom( mailimf_field *ffrom) |
455 | { | 442 | { |
456 | char *from = NULL; | 443 | char *from = NULL; |
457 | if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM) | 444 | if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM) |
458 | && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) { | 445 | && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) { |
459 | clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list; | 446 | clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list; |
460 | clistiter *it; | 447 | clistiter *it; |
461 | for ( it = clist_begin( cl ); it; it = it->next ) { | 448 | for ( it = clist_begin( cl ); it; it = it->next ) { |
462 | mailimf_mailbox *mb = (mailimf_mailbox *) it->data; | 449 | mailimf_mailbox *mb = (mailimf_mailbox *) it->data; |
463 | from = strdup( mb->mb_addr_spec ); | 450 | from = strdup( mb->mb_addr_spec ); |
464 | } | 451 | } |
465 | } | 452 | } |
466 | 453 | ||
467 | return from; | 454 | return from; |
468 | } | 455 | } |
469 | 456 | ||
470 | char *SMTPwrapper::getFrom( mailmime *mail ) | 457 | char *SMTPwrapper::getFrom( mailmime *mail ) |
471 | { | 458 | { |
472 | /* no need to delete - its just a pointer to structure content */ | 459 | /* no need to delete - its just a pointer to structure content */ |
473 | mailimf_field *ffrom = 0; | 460 | mailimf_field *ffrom = 0; |
474 | char*f = 0; | ||
475 | ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM ); | 461 | ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM ); |
476 | return getFrom(ffrom); | 462 | return getFrom(ffrom); |
477 | } | 463 | } |
478 | 464 | ||
479 | SMTPaccount *SMTPwrapper::getAccount(const QString&name ) | 465 | SMTPaccount *SMTPwrapper::getAccount(const QString&name ) |
480 | { | 466 | { |
481 | SMTPaccount *smtp; | 467 | SMTPaccount *smtp; |
482 | 468 | ||
483 | QList<Account> list = settings->getAccounts(); | 469 | QList<Account> list = settings->getAccounts(); |
484 | Account *it; | 470 | Account *it; |
485 | for ( it = list.first(); it; it = list.next() ) { | 471 | for ( it = list.first(); it; it = list.next() ) { |
486 | if ( it->getType().compare( "SMTP" ) == 0 ) { | 472 | if ( it->getType().compare( "SMTP" ) == 0 ) { |
487 | smtp = static_cast<SMTPaccount *>(it); | 473 | smtp = static_cast<SMTPaccount *>(it); |
488 | if ( smtp->getName()== name ) { | 474 | if ( smtp->getName()== name ) { |
489 | qDebug( "SMTPaccount found for" ); | 475 | qDebug( "SMTPaccount found for" ); |
490 | qDebug( name ); | 476 | qDebug( name ); |
491 | return smtp; | 477 | return smtp; |
492 | } | 478 | } |
493 | } | 479 | } |
494 | } | 480 | } |
495 | 481 | ||
496 | return NULL; | 482 | return NULL; |
497 | } | 483 | } |
498 | 484 | ||
499 | QString SMTPwrapper::getTmpFile() { | ||
500 | int num = 0; | ||
501 | QString unique; | ||
502 | |||
503 | QDir dir( "/tmp" ); | ||
504 | |||
505 | QStringList list = dir.entryList( "opiemail-tmp-*" ); | ||
506 | |||
507 | do { | ||
508 | unique.setNum( num++ ); | ||
509 | } while ( list.contains( "opiemail-tmp-" + unique ) > 0 ); | ||
510 | |||
511 | return "/tmp/opiemail-tmp-" + unique; | ||
512 | } | ||
513 | |||
514 | void SMTPwrapper::writeToFile(const QString&file, mailmime *mail ) | ||
515 | { | ||
516 | FILE *f; | ||
517 | int err, col = 0; | ||
518 | |||
519 | f = fopen( file.latin1(), "w" ); | ||
520 | if ( f == NULL ) { | ||
521 | qDebug( "writeToFile: error opening file" ); | ||
522 | return; | ||
523 | } | ||
524 | |||
525 | err = mailmime_write( f, &col, mail ); | ||
526 | if ( err != MAILIMF_NO_ERROR ) { | ||
527 | fclose( f ); | ||
528 | qDebug( "writeToFile: error writing mailmime" ); | ||
529 | return; | ||
530 | } | ||
531 | |||
532 | fclose( f ); | ||
533 | } | ||
534 | |||
535 | void SMTPwrapper::readFromFile(const QString&file, char **data, size_t *size ) | ||
536 | { | ||
537 | |||
538 | QFile msg_cache(file); | ||
539 | QString msg = ""; | ||
540 | msg_cache.open(IO_ReadOnly); | ||
541 | char*message = new char[4096]; | ||
542 | memset(message,0,4096); | ||
543 | while (msg_cache.readBlock(message,4095)>0) { | ||
544 | msg+=message; | ||
545 | memset(message,0,4096); | ||
546 | } | ||
547 | delete message; | ||
548 | *data = (char*)malloc(msg.length()+1*sizeof(char)); | ||
549 | memset(*data,0,msg.length()+1); | ||
550 | memcpy(*data,msg.ascii(),msg.length()); | ||
551 | *size=msg.length(); | ||
552 | } | ||
553 | |||
554 | void SMTPwrapper::progress( size_t current, size_t maximum ) | 485 | void SMTPwrapper::progress( size_t current, size_t maximum ) |
555 | { | 486 | { |
556 | if (SMTPwrapper::sendProgress) { | 487 | if (SMTPwrapper::sendProgress) { |
557 | SMTPwrapper::sendProgress->setSingleMail(current, maximum ); | 488 | SMTPwrapper::sendProgress->setSingleMail(current, maximum ); |
558 | qApp->processEvents(); | 489 | qApp->processEvents(); |
559 | } | 490 | } |
560 | } | 491 | } |
561 | 492 | ||
562 | void SMTPwrapper::storeMail(char*mail, size_t length, const QString&box) | 493 | void SMTPwrapper::storeMail(char*mail, size_t length, const QString&box) |
563 | { | 494 | { |
564 | if (!mail) return; | 495 | if (!mail) return; |
565 | QString localfolders = (QString) getenv( "HOME" ) + QString("/Applications/opiemail/localmail/"); | 496 | QString localfolders = (QString) getenv( "HOME" ) + QString("/Applications/opiemail/localmail/"); |
566 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); | 497 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); |
567 | wrap->storeMessage(mail,length,box); | 498 | wrap->storeMessage(mail,length,box); |
568 | delete wrap; | 499 | delete wrap; |
569 | } | 500 | } |
570 | 501 | ||
571 | void SMTPwrapper::smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ) | 502 | void SMTPwrapper::smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ) |
572 | { | 503 | { |
573 | clist *rcpts = 0; | 504 | clist *rcpts = 0; |
574 | char *from, *data; | 505 | char *from, *data; |
575 | size_t size; | 506 | size_t size; |
576 | 507 | ||
577 | if ( smtp == NULL ) { | 508 | if ( smtp == NULL ) { |
578 | return; | 509 | return; |
579 | } | 510 | } |
580 | from = data = 0; | 511 | from = data = 0; |
581 | 512 | ||
582 | QString file = getTmpFile(); | 513 | mailmessage * msg = 0; |
583 | writeToFile( file, mail ); | 514 | msg = mime_message_init(mail); |
584 | readFromFile( file, &data, &size ); | 515 | mime_message_set_tmpdir(msg,getenv( "HOME" )); |
585 | QFile f( file ); | 516 | int r = mailmessage_fetch(msg,&data,&size); |
586 | f.remove(); | 517 | mime_message_detach_mime(msg); |
587 | 518 | mailmessage_free(msg); | |
519 | if (r != MAIL_NO_ERROR || !data) { | ||
520 | if (data) free(data); | ||
521 | qDebug("Error fetching mime..."); | ||
522 | return; | ||
523 | } | ||
524 | QString tmp = data; | ||
525 | tmp.replace(QRegExp("\r+",true,false),""); | ||
526 | msg = 0; | ||
588 | if (later) { | 527 | if (later) { |
589 | storeMail(data,size,"Outgoing"); | 528 | storeMail((char*)tmp.data(),tmp.length(),"Outgoing"); |
590 | if (data) free( data ); | 529 | if (data) free( data ); |
591 | return; | 530 | return; |
592 | } | 531 | } |
593 | from = getFrom( mail ); | 532 | from = getFrom( mail ); |
594 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); | 533 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); |
595 | smtpSend(from,rcpts,data,size,smtp); | 534 | smtpSend(from,rcpts,data,size,smtp); |
596 | if (data) {free(data);} | 535 | if (data) {free(data);} |
597 | if (from) {free(from);} | 536 | if (from) {free(from);} |
598 | if (rcpts) smtp_address_list_free( rcpts ); | 537 | if (rcpts) smtp_address_list_free( rcpts ); |
599 | } | 538 | } |
600 | 539 | ||
601 | int SMTPwrapper::smtpSend(char*from,clist*rcpts,char*data,size_t size, SMTPaccount *smtp ) | 540 | int SMTPwrapper::smtpSend(char*from,clist*rcpts,char*data,size_t size, SMTPaccount *smtp ) |
602 | { | 541 | { |
603 | char *server, *user, *pass; | 542 | char *server, *user, *pass; |
604 | bool ssl; | 543 | bool ssl; |
605 | uint16_t port; | 544 | uint16_t port; |
606 | mailsmtp *session; | 545 | mailsmtp *session; |
607 | int err,result; | 546 | int err,result; |
608 | 547 | ||
609 | result = 1; | 548 | result = 1; |
610 | server = user = pass = 0; | 549 | server = user = pass = 0; |
611 | server = strdup( smtp->getServer().latin1() ); | 550 | server = strdup( smtp->getServer().latin1() ); |
612 | ssl = smtp->getSSL(); | 551 | ssl = smtp->getSSL(); |
613 | port = smtp->getPort().toUInt(); | 552 | port = smtp->getPort().toUInt(); |
614 | 553 | ||
615 | session = mailsmtp_new( 20, &progress ); | 554 | session = mailsmtp_new( 20, &progress ); |
616 | if ( session == NULL ) goto free_mem; | 555 | if ( session == NULL ) goto free_mem; |
617 | 556 | ||
618 | qDebug( "Servername %s at port %i", server, port ); | 557 | qDebug( "Servername %s at port %i", server, port ); |
619 | if ( ssl ) { | 558 | if ( ssl ) { |
620 | qDebug( "SSL session" ); | 559 | qDebug( "SSL session" ); |
621 | err = mailsmtp_ssl_connect( session, server, port ); | 560 | err = mailsmtp_ssl_connect( session, server, port ); |
622 | } else { | 561 | } else { |
623 | qDebug( "No SSL session" ); | 562 | qDebug( "No SSL session" ); |
624 | err = mailsmtp_socket_connect( session, server, port ); | 563 | err = mailsmtp_socket_connect( session, server, port ); |
625 | } | 564 | } |
626 | if ( err != MAILSMTP_NO_ERROR ) {result = 0;goto free_mem_session;} | 565 | if ( err != MAILSMTP_NO_ERROR ) {result = 0;goto free_mem_session;} |
627 | 566 | ||
628 | err = mailsmtp_init( session ); | 567 | err = mailsmtp_init( session ); |
629 | if ( err != MAILSMTP_NO_ERROR ) {result = 0; goto free_con_session;} | 568 | if ( err != MAILSMTP_NO_ERROR ) {result = 0; goto free_con_session;} |
630 | 569 | ||
631 | qDebug( "INIT OK" ); | 570 | qDebug( "INIT OK" ); |
632 | 571 | ||
633 | if ( smtp->getLogin() ) { | 572 | if ( smtp->getLogin() ) { |
634 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { | 573 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { |
635 | // get'em | 574 | // get'em |
636 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); | 575 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); |
637 | login.show(); | 576 | login.show(); |
638 | if ( QDialog::Accepted == login.exec() ) { | 577 | if ( QDialog::Accepted == login.exec() ) { |
639 | // ok | 578 | // ok |
640 | user = strdup( login.getUser().latin1() ); | 579 | user = strdup( login.getUser().latin1() ); |
641 | pass = strdup( login.getPassword().latin1() ); | 580 | pass = strdup( login.getPassword().latin1() ); |
642 | } else { | 581 | } else { |
643 | result = 0; goto free_con_session; | 582 | result = 0; goto free_con_session; |
644 | } | 583 | } |
645 | } else { | 584 | } else { |
646 | user = strdup( smtp->getUser().latin1() ); | 585 | user = strdup( smtp->getUser().latin1() ); |
647 | pass = strdup( smtp->getPassword().latin1() ); | 586 | pass = strdup( smtp->getPassword().latin1() ); |
648 | } | 587 | } |
649 | qDebug( "session->auth: %i", session->auth); | 588 | qDebug( "session->auth: %i", session->auth); |
650 | err = mailsmtp_auth( session, user, pass ); | 589 | err = mailsmtp_auth( session, user, pass ); |
651 | if ( err == MAILSMTP_NO_ERROR ) qDebug("auth ok"); | 590 | if ( err == MAILSMTP_NO_ERROR ) qDebug("auth ok"); |
652 | qDebug( "Done auth!" ); | 591 | qDebug( "Done auth!" ); |
653 | } | 592 | } |
654 | 593 | ||
655 | err = mailsmtp_send( session, from, rcpts, data, size ); | 594 | err = mailsmtp_send( session, from, rcpts, data, size ); |
656 | if ( err != MAILSMTP_NO_ERROR ) { | 595 | if ( err != MAILSMTP_NO_ERROR ) { |
657 | qDebug("Error sending mail: %s",mailsmtpError(err).latin1()); | 596 | qDebug("Error sending mail: %s",mailsmtpError(err).latin1()); |
658 | result = 0; goto free_con_session; | 597 | result = 0; goto free_con_session; |
659 | } | 598 | } |
660 | 599 | ||
661 | qDebug( "Mail sent." ); | 600 | qDebug( "Mail sent." ); |
662 | storeMail(data,size,"Sent"); | 601 | storeMail(data,size,"Sent"); |
663 | 602 | ||
664 | free_con_session: | 603 | free_con_session: |
665 | mailsmtp_quit( session ); | 604 | mailsmtp_quit( session ); |
666 | free_mem_session: | 605 | free_mem_session: |
667 | mailsmtp_free( session ); | 606 | mailsmtp_free( session ); |
668 | free_mem: | 607 | free_mem: |
669 | if (server) free( server ); | 608 | if (server) free( server ); |
670 | if ( smtp->getLogin() ) { | 609 | if ( smtp->getLogin() ) { |
671 | free( user ); | 610 | free( user ); |
672 | free( pass ); | 611 | free( pass ); |
673 | } | 612 | } |
674 | return result; | 613 | return result; |
675 | } | 614 | } |
676 | 615 | ||
677 | void SMTPwrapper::sendMail(const Mail&mail,bool later ) | 616 | void SMTPwrapper::sendMail(const Mail&mail,bool later ) |
678 | { | 617 | { |
679 | mailmime * mimeMail; | 618 | mailmime * mimeMail; |
680 | 619 | ||
681 | SMTPaccount *smtp = getAccount(mail.getName()); | 620 | SMTPaccount *smtp = getAccount(mail.getName()); |
682 | 621 | ||
683 | mimeMail = createMimeMail(mail ); | 622 | mimeMail = createMimeMail(mail ); |
684 | if ( mimeMail == NULL ) { | 623 | if ( mimeMail == NULL ) { |
685 | qDebug( "sendMail: error creating mime mail" ); | 624 | qDebug( "sendMail: error creating mime mail" ); |
686 | } else { | 625 | } else { |
687 | sendProgress = new progressMailSend(); | 626 | sendProgress = new progressMailSend(); |
688 | sendProgress->show(); | 627 | sendProgress->show(); |
689 | sendProgress->setMaxMails(1); | 628 | sendProgress->setMaxMails(1); |
690 | smtpSend( mimeMail,later,smtp); | 629 | smtpSend( mimeMail,later,smtp); |
691 | mailmime_free( mimeMail ); | ||
692 | qDebug("Clean up done"); | 630 | qDebug("Clean up done"); |
693 | sendProgress->hide(); | 631 | sendProgress->hide(); |
694 | delete sendProgress; | 632 | delete sendProgress; |
695 | sendProgress = 0; | 633 | sendProgress = 0; |
634 | mailmime_free( mimeMail ); | ||
696 | } | 635 | } |
697 | } | 636 | } |
698 | 637 | ||
699 | int SMTPwrapper::sendQueuedMail(MBOXwrapper*wrap,SMTPaccount*smtp,RecMail*which) | 638 | int SMTPwrapper::sendQueuedMail(MBOXwrapper*wrap,SMTPaccount*smtp,RecMail*which) |
700 | { | 639 | { |
701 | char*data = 0; | 640 | char*data = 0; |
702 | size_t length = 0; | 641 | size_t length = 0; |
703 | size_t curTok = 0; | 642 | size_t curTok = 0; |
704 | mailimf_fields *fields = 0; | 643 | mailimf_fields *fields = 0; |
705 | mailimf_field*ffrom = 0; | 644 | mailimf_field*ffrom = 0; |
706 | clist *rcpts = 0; | 645 | clist *rcpts = 0; |
707 | char*from = 0; | 646 | char*from = 0; |
708 | int res = 0; | 647 | int res = 0; |
709 | 648 | ||
710 | wrap->fetchRawBody(*which,&data,&length); | 649 | wrap->fetchRawBody(*which,&data,&length); |
711 | if (!data) return 0; | 650 | if (!data) return 0; |
712 | int err = mailimf_fields_parse( data, length, &curTok, &fields ); | 651 | int err = mailimf_fields_parse( data, length, &curTok, &fields ); |
713 | if (err != MAILIMF_NO_ERROR) { | 652 | if (err != MAILIMF_NO_ERROR) { |
714 | free(data); | 653 | free(data); |
715 | delete wrap; | 654 | delete wrap; |
716 | return 0; | 655 | return 0; |
717 | } | 656 | } |
718 | 657 | ||
719 | rcpts = createRcptList( fields ); | 658 | rcpts = createRcptList( fields ); |
720 | ffrom = getField(fields, MAILIMF_FIELD_FROM ); | 659 | ffrom = getField(fields, MAILIMF_FIELD_FROM ); |
721 | from = getFrom(ffrom); | 660 | from = getFrom(ffrom); |
722 | 661 | ||
723 | qDebug("Size: %i vs. %i",length,strlen(data)); | 662 | qDebug("Size: %i vs. %i",length,strlen(data)); |
724 | if (rcpts && from) { | 663 | if (rcpts && from) { |
725 | res = smtpSend(from,rcpts,data,length,smtp ); | 664 | res = smtpSend(from,rcpts,data,length,smtp ); |
726 | } | 665 | } |
727 | if (fields) { | 666 | if (fields) { |
728 | mailimf_fields_free(fields); | 667 | mailimf_fields_free(fields); |
729 | fields = 0; | 668 | fields = 0; |
730 | } | 669 | } |
731 | if (data) { | 670 | if (data) { |
732 | free(data); | 671 | free(data); |
733 | } | 672 | } |
734 | if (from) { | 673 | if (from) { |
735 | free(from); | 674 | free(from); |
736 | } | 675 | } |
737 | if (rcpts) { | 676 | if (rcpts) { |
738 | smtp_address_list_free( rcpts ); | 677 | smtp_address_list_free( rcpts ); |
739 | } | 678 | } |
740 | return res; | 679 | return res; |
741 | } | 680 | } |
742 | 681 | ||
743 | /* this is a special fun */ | 682 | /* this is a special fun */ |
744 | bool SMTPwrapper::flushOutbox(SMTPaccount*smtp) | 683 | bool SMTPwrapper::flushOutbox(SMTPaccount*smtp) |
745 | { | 684 | { |
746 | bool returnValue = true; | 685 | bool returnValue = true; |
747 | 686 | ||
748 | if (!smtp) return false; | 687 | if (!smtp) return false; |
749 | 688 | ||
750 | QString localfolders = (QString) getenv( "HOME" ) + QString("/Applications/opiemail/localmail/"); | 689 | QString localfolders = (QString) getenv( "HOME" ) + QString("/Applications/opiemail/localmail/"); |
751 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); | 690 | MBOXwrapper*wrap = new MBOXwrapper(localfolders); |
752 | if (!wrap) { | 691 | if (!wrap) { |
753 | qDebug("memory error"); | 692 | qDebug("memory error"); |
754 | return false; | 693 | return false; |
755 | } | 694 | } |
756 | QList<RecMail> mailsToSend; | 695 | QList<RecMail> mailsToSend; |
757 | QList<RecMail> mailsToRemove; | 696 | QList<RecMail> mailsToRemove; |
758 | QString mbox("Outgoing"); | 697 | QString mbox("Outgoing"); |
759 | wrap->listMessages(mbox,mailsToSend); | 698 | wrap->listMessages(mbox,mailsToSend); |
760 | if (mailsToSend.count()==0) { | 699 | if (mailsToSend.count()==0) { |
761 | delete wrap; | 700 | delete wrap; |
762 | return false; | 701 | return false; |
763 | } | 702 | } |
764 | mailsToSend.setAutoDelete(false); | 703 | mailsToSend.setAutoDelete(false); |
765 | sendProgress = new progressMailSend(); | 704 | sendProgress = new progressMailSend(); |
766 | sendProgress->show(); | 705 | sendProgress->show(); |
767 | sendProgress->setMaxMails(mailsToSend.count()); | 706 | sendProgress->setMaxMails(mailsToSend.count()); |
768 | 707 | ||
769 | while (mailsToSend.count()>0) { | 708 | while (mailsToSend.count()>0) { |
770 | if (sendQueuedMail(wrap,smtp,mailsToSend.at(0))==0) { | 709 | if (sendQueuedMail(wrap,smtp,mailsToSend.at(0))==0) { |
771 | QMessageBox::critical(0,tr("Error sending mail"), | 710 | QMessageBox::critical(0,tr("Error sending mail"), |
772 | tr("Error sending queued mail - breaking")); | 711 | tr("Error sending queued mail - breaking")); |
773 | |||
774 | returnValue = false; | 712 | returnValue = false; |
775 | break; | 713 | break; |
776 | } | 714 | } |
777 | mailsToRemove.append(mailsToSend.at(0)); | 715 | mailsToRemove.append(mailsToSend.at(0)); |
778 | mailsToSend.removeFirst(); | 716 | mailsToSend.removeFirst(); |
779 | sendProgress->setCurrentMails(mailsToRemove.count()); | 717 | sendProgress->setCurrentMails(mailsToRemove.count()); |
780 | } | 718 | } |
781 | sendProgress->hide(); | 719 | sendProgress->hide(); |
782 | delete sendProgress; | 720 | delete sendProgress; |
783 | sendProgress = 0; | 721 | sendProgress = 0; |
784 | wrap->deleteMails(mbox,mailsToRemove); | 722 | wrap->deleteMails(mbox,mailsToRemove); |
785 | mailsToSend.setAutoDelete(true); | 723 | mailsToSend.setAutoDelete(true); |
786 | delete wrap; | 724 | delete wrap; |
787 | return returnValue; | 725 | return returnValue; |
788 | } | 726 | } |
diff --git a/noncore/net/mail/smtpwrapper.h b/noncore/net/mail/smtpwrapper.h index 66180b7..f734fa4 100644 --- a/noncore/net/mail/smtpwrapper.h +++ b/noncore/net/mail/smtpwrapper.h | |||
@@ -1,65 +1,62 @@ | |||
1 | #ifndef SMTPwrapper_H | 1 | #ifndef SMTPwrapper_H |
2 | #define SMTPwrapper_H | 2 | #define SMTPwrapper_H |
3 | 3 | ||
4 | #include <qpe/applnk.h> | 4 | #include <qpe/applnk.h> |
5 | 5 | ||
6 | #include <qbitarray.h> | 6 | #include <qbitarray.h> |
7 | #include <qdatetime.h> | 7 | #include <qdatetime.h> |
8 | #include <libetpan/clist.h> | 8 | #include <libetpan/clist.h> |
9 | 9 | ||
10 | #include "settings.h" | 10 | #include "settings.h" |
11 | 11 | ||
12 | class Mail; | 12 | class Mail; |
13 | class MBOXwrapper; | 13 | class MBOXwrapper; |
14 | class RecMail; | 14 | class RecMail; |
15 | class Attachment; | 15 | class Attachment; |
16 | struct mailimf_fields; | 16 | struct mailimf_fields; |
17 | struct mailimf_field; | 17 | struct mailimf_field; |
18 | struct mailimf_mailbox; | 18 | struct mailimf_mailbox; |
19 | struct mailmime; | 19 | struct mailmime; |
20 | struct mailimf_address_list; | 20 | struct mailimf_address_list; |
21 | class progressMailSend; | 21 | class progressMailSend; |
22 | 22 | ||
23 | class SMTPwrapper : public QObject | 23 | class SMTPwrapper : public QObject |
24 | { | 24 | { |
25 | Q_OBJECT | 25 | Q_OBJECT |
26 | 26 | ||
27 | public: | 27 | public: |
28 | SMTPwrapper( Settings *s ); | 28 | SMTPwrapper( Settings *s ); |
29 | virtual ~SMTPwrapper(){} | 29 | virtual ~SMTPwrapper(){} |
30 | void sendMail(const Mail& mail,bool later=false ); | 30 | void sendMail(const Mail& mail,bool later=false ); |
31 | bool flushOutbox(SMTPaccount*smtp); | 31 | bool flushOutbox(SMTPaccount*smtp); |
32 | 32 | ||
33 | static progressMailSend*sendProgress; | 33 | static progressMailSend*sendProgress; |
34 | protected: | 34 | protected: |
35 | mailimf_mailbox *newMailbox(const QString&name,const QString&mail ); | 35 | mailimf_mailbox *newMailbox(const QString&name,const QString&mail ); |
36 | mailimf_fields *createImfFields(const Mail &mail ); | 36 | mailimf_fields *createImfFields(const Mail &mail ); |
37 | mailmime *createMimeMail(const Mail&mail ); | 37 | mailmime *createMimeMail(const Mail&mail ); |
38 | 38 | ||
39 | mailimf_address_list *parseAddresses(const QString&addr ); | 39 | mailimf_address_list *parseAddresses(const QString&addr ); |
40 | void addFileParts( mailmime *message,const QList<Attachment>&files ); | 40 | void addFileParts( mailmime *message,const QList<Attachment>&files ); |
41 | mailmime *buildTxtPart(const QString&str ); | 41 | mailmime *buildTxtPart(const QString&str ); |
42 | mailmime *buildFilePart(const QString&filename,const QString&mimetype,const QString&content); | 42 | mailmime *buildFilePart(const QString&filename,const QString&mimetype,const QString&content); |
43 | void smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ); | 43 | void smtpSend( mailmime *mail,bool later, SMTPaccount *smtp ); |
44 | clist *createRcptList( mailimf_fields *fields ); | 44 | clist *createRcptList( mailimf_fields *fields ); |
45 | SMTPaccount *getAccount(const QString&from ); | 45 | SMTPaccount *getAccount(const QString&from ); |
46 | void writeToFile(const QString&file, mailmime *mail ); | ||
47 | void readFromFile(const QString&file, char **data, size_t *size ); | ||
48 | 46 | ||
49 | static void storeMail(char*mail, size_t length, const QString&box); | 47 | static void storeMail(char*mail, size_t length, const QString&box); |
50 | static QString mailsmtpError( int err ); | 48 | static QString mailsmtpError( int err ); |
51 | static QString getTmpFile(); | ||
52 | static void progress( size_t current, size_t maximum ); | 49 | static void progress( size_t current, size_t maximum ); |
53 | static void addRcpts( clist *list, mailimf_address_list *addr_list ); | 50 | static void addRcpts( clist *list, mailimf_address_list *addr_list ); |
54 | static char *getFrom( mailmime *mail ); | 51 | static char *getFrom( mailmime *mail ); |
55 | static char *getFrom( mailimf_field *ffrom); | 52 | static char *getFrom( mailimf_field *ffrom); |
56 | static mailimf_field *getField( mailimf_fields *fields, int type ); | 53 | static mailimf_field *getField( mailimf_fields *fields, int type ); |
57 | int smtpSend(char*from,clist*rcpts,char*data,size_t size, SMTPaccount *smtp ); | 54 | int smtpSend(char*from,clist*rcpts,char*data,size_t size, SMTPaccount *smtp ); |
58 | 55 | ||
59 | void storeMail(mailmime*mail, const QString&box); | 56 | void storeMail(mailmime*mail, const QString&box); |
60 | Settings *settings; | 57 | Settings *settings; |
61 | 58 | ||
62 | int sendQueuedMail(MBOXwrapper*wrap,SMTPaccount*smtp,RecMail*which); | 59 | int sendQueuedMail(MBOXwrapper*wrap,SMTPaccount*smtp,RecMail*which); |
63 | }; | 60 | }; |
64 | 61 | ||
65 | #endif | 62 | #endif |