author | jgf <jgf> | 2003-12-18 02:43:07 (UTC) |
---|---|---|
committer | jgf <jgf> | 2003-12-18 02:43:07 (UTC) |
commit | d7fed5a555f51008db8b96e8b978c3830c59be77 (patch) (unidiff) | |
tree | 9f3a8aa82909ef80cf1c42431abc20d3ac585436 | |
parent | 77e353da0c33bbfabf1b919e25008d62581bf164 (diff) | |
download | opie-d7fed5a555f51008db8b96e8b978c3830c59be77.zip opie-d7fed5a555f51008db8b96e8b978c3830c59be77.tar.gz opie-d7fed5a555f51008db8b96e8b978c3830c59be77.tar.bz2 |
decoding of imap folder names
-rw-r--r-- | noncore/net/mail/defines.h | 11 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/mailwrapper.cpp | 107 | ||||
-rw-r--r-- | noncore/net/mail/libmailwrapper/mailwrapper.h | 2 | ||||
-rw-r--r-- | noncore/net/mail/mailwrapper.cpp | 107 | ||||
-rw-r--r-- | noncore/net/mail/mailwrapper.h | 2 |
5 files changed, 201 insertions, 28 deletions
diff --git a/noncore/net/mail/defines.h b/noncore/net/mail/defines.h index 9036658..d9cdab0 100644 --- a/noncore/net/mail/defines.h +++ b/noncore/net/mail/defines.h | |||
@@ -1,40 +1,51 @@ | |||
1 | #ifndef DEFINE_CONSTANTS_H | 1 | #ifndef DEFINE_CONSTANTS_H |
2 | #define DEFINE_CONSTANTS_H | 2 | #define DEFINE_CONSTANTS_H |
3 | 3 | ||
4 | #include <qpe/resource.h> | 4 | #include <qpe/resource.h> |
5 | 5 | ||
6 | #define USER_AGENT "OpieMail v0.1" | 6 | #define USER_AGENT "OpieMail v0.1" |
7 | 7 | ||
8 | #define PIC_COMPOSEMAIL "mail/composemail" | 8 | #define PIC_COMPOSEMAIL "mail/composemail" |
9 | #define PIC_SENDQUEUED "mail/sendqueued" | 9 | #define PIC_SENDQUEUED "mail/sendqueued" |
10 | #define PIC_SHOWFOLDERS "mail/showfolders" | 10 | #define PIC_SHOWFOLDERS "mail/showfolders" |
11 | #define PIC_SEARCHMAILS "mail/searchmails" | 11 | #define PIC_SEARCHMAILS "mail/searchmails" |
12 | #define PIC_EDITSETTINGS "mail/editsettings" | 12 | #define PIC_EDITSETTINGS "mail/editsettings" |
13 | #define PIC_EDITACCOUNTS "mail/editaccounts" | 13 | #define PIC_EDITACCOUNTS "mail/editaccounts" |
14 | #define PIC_SYNC "mail/sync" | 14 | #define PIC_SYNC "mail/sync" |
15 | #define PIC_IMAPFOLDER "mail/imapfolder" | 15 | #define PIC_IMAPFOLDER "mail/imapfolder" |
16 | #define PIC_POP3FOLDER "mail/pop3folder" | 16 | #define PIC_POP3FOLDER "mail/pop3folder" |
17 | #define PIC_INBOXFOLDER "mail/inbox" | 17 | #define PIC_INBOXFOLDER "mail/inbox" |
18 | 18 | ||
19 | #define ICON_COMPOSEMAIL QIconSet( Resource::loadPixmap( PIC_COMPOSEMAIL ) ) | 19 | #define ICON_COMPOSEMAIL QIconSet( Resource::loadPixmap( PIC_COMPOSEMAIL ) ) |
20 | #define ICON_SENDQUEUED QIconSet( Resource::loadPixmap( PIC_SENDQUEUED ) ) | 20 | #define ICON_SENDQUEUED QIconSet( Resource::loadPixmap( PIC_SENDQUEUED ) ) |
21 | #define ICON_SHOWFOLDERS QIconSet( Resource::loadPixmap( PIC_SHOWFOLDERS ) ) | 21 | #define ICON_SHOWFOLDERS QIconSet( Resource::loadPixmap( PIC_SHOWFOLDERS ) ) |
22 | #define ICON_SEARCHMAILS QIconSet( Resource::loadPixmap( PIC_SEARCHMAILS ) ) | 22 | #define ICON_SEARCHMAILS QIconSet( Resource::loadPixmap( PIC_SEARCHMAILS ) ) |
23 | #define ICON_EDITSETTINGS QIconSet( Resource::loadPixmap( PIC_EDITSETTINGS ) ) | 23 | #define ICON_EDITSETTINGS QIconSet( Resource::loadPixmap( PIC_EDITSETTINGS ) ) |
24 | #define ICON_EDITACCOUNTS QIconSet( Resource::loadPixmap( PIC_EDITACCOUNTS ) ) | 24 | #define ICON_EDITACCOUNTS QIconSet( Resource::loadPixmap( PIC_EDITACCOUNTS ) ) |
25 | #define ICON_SYNC QIconSet( Resource::loadPixmap( PIC_SYNC ) ) | 25 | #define ICON_SYNC QIconSet( Resource::loadPixmap( PIC_SYNC ) ) |
26 | 26 | ||
27 | #define PIXMAP_IMAPFOLDER QPixmap( Resource::loadPixmap( PIC_IMAPFOLDER ) ) | 27 | #define PIXMAP_IMAPFOLDER QPixmap( Resource::loadPixmap( PIC_IMAPFOLDER ) ) |
28 | #define PIXMAP_POP3FOLDER QPixmap( Resource::loadPixmap( PIC_POP3FOLDER ) ) | 28 | #define PIXMAP_POP3FOLDER QPixmap( Resource::loadPixmap( PIC_POP3FOLDER ) ) |
29 | #define PIXMAP_INBOXFOLDER QPixmap( Resource::loadPixmap( PIC_INBOXFOLDER) ) | 29 | #define PIXMAP_INBOXFOLDER QPixmap( Resource::loadPixmap( PIC_INBOXFOLDER) ) |
30 | 30 | ||
31 | #define IMAP_PORT "143" | 31 | #define IMAP_PORT "143" |
32 | #define IMAP_SSL_PORT "993" | 32 | #define IMAP_SSL_PORT "993" |
33 | #define SMTP_PORT "25" | 33 | #define SMTP_PORT "25" |
34 | #define SMTP_SSL_PORT "465" | 34 | #define SMTP_SSL_PORT "465" |
35 | #define POP3_PORT "110" | 35 | #define POP3_PORT "110" |
36 | #define POP3_SSL_PORT "995" | 36 | #define POP3_SSL_PORT "995" |
37 | #define NNTP_PORT "119" | 37 | #define NNTP_PORT "119" |
38 | #define NNTP_SSL_PORT "563" | 38 | #define NNTP_SSL_PORT "563" |
39 | 39 | ||
40 | /* used for decoding imapfoldername */ | ||
41 | #define UNDEFINED 64 | ||
42 | #define MAXLINE 76 | ||
43 | #define UTF16MASK 0x03FFUL | ||
44 | #define UTF16SHIFT 10 | ||
45 | #define UTF16BASE 0x10000UL | ||
46 | #define UTF16HIGHSTART 0xD800UL | ||
47 | #define UTF16HIGHEND 0xDBFFUL | ||
48 | #define UTF16LOSTART 0xDC00UL | ||
49 | #define UTF16LOEND 0xDFFFUL | ||
50 | |||
40 | #endif | 51 | #endif |
diff --git a/noncore/net/mail/libmailwrapper/mailwrapper.cpp b/noncore/net/mail/libmailwrapper/mailwrapper.cpp index 858283f..75c06f9 100644 --- a/noncore/net/mail/libmailwrapper/mailwrapper.cpp +++ b/noncore/net/mail/libmailwrapper/mailwrapper.cpp | |||
@@ -1,644 +1,723 @@ | |||
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 | 8 | ||
9 | #include "mailwrapper.h" | 9 | #include "mailwrapper.h" |
10 | #include "logindialog.h" | 10 | #include "logindialog.h" |
11 | //#include "mail.h" | 11 | //#include "mail.h" |
12 | #include "defines.h" | 12 | #include "defines.h" |
13 | 13 | ||
14 | Attachment::Attachment( DocLnk lnk ) | 14 | Attachment::Attachment( DocLnk lnk ) |
15 | { | 15 | { |
16 | doc = lnk; | 16 | doc = lnk; |
17 | size = QFileInfo( doc.file() ).size(); | 17 | size = QFileInfo( doc.file() ).size(); |
18 | } | 18 | } |
19 | 19 | ||
20 | Folder::Folder(const QString&tmp_name, const QString&sep ) | 20 | Folder::Folder(const QString&tmp_name, const QString&sep ) |
21 | { | 21 | { |
22 | name = tmp_name; | 22 | name = tmp_name; |
23 | nameDisplay = name; | 23 | nameDisplay = name; |
24 | |||
25 | for ( int pos = nameDisplay.find( '&' ); pos != -1; | ||
26 | pos = nameDisplay.find( '&' ) ) { | ||
27 | int end = nameDisplay.find( '-' ); | ||
28 | if ( end == -1 || end <= pos ) break; | ||
29 | QString str64 = nameDisplay.mid( pos + 1, end - pos - 1 ); | ||
30 | // TODO: do real base64 decoding here ! | ||
31 | if ( str64.compare( "APw" ) == 0 ) { | ||
32 | nameDisplay = nameDisplay.replace( pos, end - pos + 1, "ue" ); | ||
33 | } else if ( str64.compare( "APY" ) == 0 ) { | ||
34 | nameDisplay = nameDisplay.replace( pos, end - pos + 1, "oe" ); | ||
35 | } | ||
36 | } | ||
37 | qDebug( "folder " + name + " - displayed as " + nameDisplay ); | ||
38 | separator = sep; | 24 | separator = sep; |
39 | } | 25 | } |
40 | 26 | ||
41 | const QString& Folder::Separator()const | 27 | const QString& Folder::Separator()const |
42 | { | 28 | { |
43 | return separator; | 29 | return separator; |
44 | } | 30 | } |
45 | 31 | ||
46 | IMAPFolder::IMAPFolder(const QString&name,const QString&sep, bool select,const QString&prefix ) | 32 | IMAPFolder::IMAPFolder(const QString&name,const QString&sep, bool select,const QString&prefix ) |
47 | : Folder( name,sep ),m_MaySelect(select) | 33 | : Folder( name,sep ),m_MaySelect(select) |
48 | { | 34 | { |
35 | // Decode IMAP foldername | ||
36 | nameDisplay = IMAPFolder::decodeFolderName( name ); | ||
37 | qDebug( "folder " + name + " - displayed as " + nameDisplay ); | ||
38 | |||
49 | if (prefix.length()>0) { | 39 | if (prefix.length()>0) { |
50 | if (nameDisplay.startsWith(prefix) && nameDisplay.length()>prefix.length()) { | 40 | if (nameDisplay.startsWith(prefix) && nameDisplay.length()>prefix.length()) { |
51 | nameDisplay=nameDisplay.right(nameDisplay.length()-prefix.length()); | 41 | nameDisplay=nameDisplay.right(nameDisplay.length()-prefix.length()); |
52 | } | 42 | } |
53 | } | 43 | } |
54 | } | 44 | } |
55 | 45 | ||
46 | static unsigned char base64chars[] = | ||
47 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,"; | ||
48 | |||
49 | /** | ||
50 | * Decodes base64 encoded parts of the imapfolder name | ||
51 | * Code taken from kde cvs: kdebase/kioslave/imap4/rfcdecoder.cc | ||
52 | */ | ||
53 | QString IMAPFolder::decodeFolderName( const QString &name ) | ||
54 | { | ||
55 | unsigned char c, i, bitcount; | ||
56 | unsigned long ucs4, utf16, bitbuf; | ||
57 | unsigned char base64[256], utf8[6]; | ||
58 | unsigned long srcPtr = 0; | ||
59 | QCString dst; | ||
60 | QCString src = name.ascii(); | ||
61 | |||
62 | /* initialize modified base64 decoding table */ | ||
63 | memset(base64, UNDEFINED, sizeof(base64)); | ||
64 | for (i = 0; i < sizeof(base64chars); ++i) { | ||
65 | base64[(int)base64chars[i]] = i; | ||
66 | } | ||
67 | |||
68 | /* loop until end of string */ | ||
69 | while (srcPtr < src.length ()) { | ||
70 | c = src[srcPtr++]; | ||
71 | /* deal with literal characters and &- */ | ||
72 | if (c != '&' || src[srcPtr] == '-') { | ||
73 | /* encode literally */ | ||
74 | dst += c; | ||
75 | /* skip over the '-' if this is an &- sequence */ | ||
76 | if (c == '&') | ||
77 | srcPtr++; | ||
78 | } else { | ||
79 | /* convert modified UTF-7 -> UTF-16 -> UCS-4 -> UTF-8 -> HEX */ | ||
80 | bitbuf = 0; | ||
81 | bitcount = 0; | ||
82 | ucs4 = 0; | ||
83 | while ((c = base64[(unsigned char) src[srcPtr]]) != UNDEFINED) { | ||
84 | ++srcPtr; | ||
85 | bitbuf = (bitbuf << 6) | c; | ||
86 | bitcount += 6; | ||
87 | /* enough bits for a UTF-16 character? */ | ||
88 | if (bitcount >= 16) { | ||
89 | bitcount -= 16; | ||
90 | utf16 = (bitcount ? bitbuf >> bitcount : bitbuf) & 0xffff; | ||
91 | /* convert UTF16 to UCS4 */ | ||
92 | if (utf16 >= UTF16HIGHSTART && utf16 <= UTF16HIGHEND) { | ||
93 | ucs4 = (utf16 - UTF16HIGHSTART) << UTF16SHIFT; | ||
94 | continue; | ||
95 | } else if (utf16 >= UTF16LOSTART && utf16 <= UTF16LOEND) { | ||
96 | ucs4 += utf16 - UTF16LOSTART + UTF16BASE; | ||
97 | } else { | ||
98 | ucs4 = utf16; | ||
99 | } | ||
100 | /* convert UTF-16 range of UCS4 to UTF-8 */ | ||
101 | if (ucs4 <= 0x7fUL) { | ||
102 | utf8[0] = ucs4; | ||
103 | i = 1; | ||
104 | } else if (ucs4 <= 0x7ffUL) { | ||
105 | utf8[0] = 0xc0 | (ucs4 >> 6); | ||
106 | utf8[1] = 0x80 | (ucs4 & 0x3f); | ||
107 | i = 2; | ||
108 | } else if (ucs4 <= 0xffffUL) { | ||
109 | utf8[0] = 0xe0 | (ucs4 >> 12); | ||
110 | utf8[1] = 0x80 | ((ucs4 >> 6) & 0x3f); | ||
111 | utf8[2] = 0x80 | (ucs4 & 0x3f); | ||
112 | i = 3; | ||
113 | } else { | ||
114 | utf8[0] = 0xf0 | (ucs4 >> 18); | ||
115 | utf8[1] = 0x80 | ((ucs4 >> 12) & 0x3f); | ||
116 | utf8[2] = 0x80 | ((ucs4 >> 6) & 0x3f); | ||
117 | utf8[3] = 0x80 | (ucs4 & 0x3f); | ||
118 | i = 4; | ||
119 | } | ||
120 | /* copy it */ | ||
121 | for (c = 0; c < i; ++c) { | ||
122 | dst += utf8[c]; | ||
123 | } | ||
124 | } | ||
125 | } | ||
126 | /* skip over trailing '-' in modified UTF-7 encoding */ | ||
127 | if (src[srcPtr] == '-') | ||
128 | ++srcPtr; | ||
129 | } | ||
130 | } | ||
131 | |||
132 | return QString::fromUtf8( dst.data() ); | ||
133 | } | ||
134 | |||
56 | MailWrapper::MailWrapper( Settings *s ) | 135 | MailWrapper::MailWrapper( Settings *s ) |
57 | : QObject() | 136 | : QObject() |
58 | { | 137 | { |
59 | settings = s; | 138 | settings = s; |
60 | } | 139 | } |
61 | 140 | ||
62 | QString MailWrapper::mailsmtpError( int errnum ) | 141 | QString MailWrapper::mailsmtpError( int errnum ) |
63 | { | 142 | { |
64 | switch ( errnum ) { | 143 | switch ( errnum ) { |
65 | case MAILSMTP_NO_ERROR: | 144 | case MAILSMTP_NO_ERROR: |
66 | return tr( "No error" ); | 145 | return tr( "No error" ); |
67 | case MAILSMTP_ERROR_UNEXPECTED_CODE: | 146 | case MAILSMTP_ERROR_UNEXPECTED_CODE: |
68 | return tr( "Unexpected error code" ); | 147 | return tr( "Unexpected error code" ); |
69 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: | 148 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: |
70 | return tr( "Service not available" ); | 149 | return tr( "Service not available" ); |
71 | case MAILSMTP_ERROR_STREAM: | 150 | case MAILSMTP_ERROR_STREAM: |
72 | return tr( "Stream error" ); | 151 | return tr( "Stream error" ); |
73 | case MAILSMTP_ERROR_HOSTNAME: | 152 | case MAILSMTP_ERROR_HOSTNAME: |
74 | return tr( "gethostname() failed" ); | 153 | return tr( "gethostname() failed" ); |
75 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: | 154 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: |
76 | return tr( "Not implemented" ); | 155 | return tr( "Not implemented" ); |
77 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: | 156 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: |
78 | return tr( "Error, action not taken" ); | 157 | return tr( "Error, action not taken" ); |
79 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: | 158 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: |
80 | return tr( "Data exceeds storage allocation" ); | 159 | return tr( "Data exceeds storage allocation" ); |
81 | case MAILSMTP_ERROR_IN_PROCESSING: | 160 | case MAILSMTP_ERROR_IN_PROCESSING: |
82 | return tr( "Error in processing" ); | 161 | return tr( "Error in processing" ); |
83 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: | 162 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: |
84 | // return tr( "Insufficient system storage" ); | 163 | // return tr( "Insufficient system storage" ); |
85 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: | 164 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: |
86 | return tr( "Mailbox unavailable" ); | 165 | return tr( "Mailbox unavailable" ); |
87 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: | 166 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: |
88 | return tr( "Mailbox name not allowed" ); | 167 | return tr( "Mailbox name not allowed" ); |
89 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: | 168 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: |
90 | return tr( "Bad command sequence" ); | 169 | return tr( "Bad command sequence" ); |
91 | case MAILSMTP_ERROR_USER_NOT_LOCAL: | 170 | case MAILSMTP_ERROR_USER_NOT_LOCAL: |
92 | return tr( "User not local" ); | 171 | return tr( "User not local" ); |
93 | case MAILSMTP_ERROR_TRANSACTION_FAILED: | 172 | case MAILSMTP_ERROR_TRANSACTION_FAILED: |
94 | return tr( "Transaction failed" ); | 173 | return tr( "Transaction failed" ); |
95 | case MAILSMTP_ERROR_MEMORY: | 174 | case MAILSMTP_ERROR_MEMORY: |
96 | return tr( "Memory error" ); | 175 | return tr( "Memory error" ); |
97 | case MAILSMTP_ERROR_CONNECTION_REFUSED: | 176 | case MAILSMTP_ERROR_CONNECTION_REFUSED: |
98 | return tr( "Connection refused" ); | 177 | return tr( "Connection refused" ); |
99 | default: | 178 | default: |
100 | return tr( "Unknown error code" ); | 179 | return tr( "Unknown error code" ); |
101 | } | 180 | } |
102 | } | 181 | } |
103 | 182 | ||
104 | mailimf_mailbox *MailWrapper::newMailbox(const QString&name, const QString&mail ) | 183 | mailimf_mailbox *MailWrapper::newMailbox(const QString&name, const QString&mail ) |
105 | { | 184 | { |
106 | return mailimf_mailbox_new( strdup( name.latin1() ), | 185 | return mailimf_mailbox_new( strdup( name.latin1() ), |
107 | strdup( mail.latin1() ) ); | 186 | strdup( mail.latin1() ) ); |
108 | } | 187 | } |
109 | 188 | ||
110 | mailimf_address_list *MailWrapper::parseAddresses(const QString&addr ) | 189 | mailimf_address_list *MailWrapper::parseAddresses(const QString&addr ) |
111 | { | 190 | { |
112 | mailimf_address_list *addresses; | 191 | mailimf_address_list *addresses; |
113 | 192 | ||
114 | if ( addr.isEmpty() ) return NULL; | 193 | if ( addr.isEmpty() ) return NULL; |
115 | 194 | ||
116 | addresses = mailimf_address_list_new_empty(); | 195 | addresses = mailimf_address_list_new_empty(); |
117 | 196 | ||
118 | QStringList list = QStringList::split( ',', addr ); | 197 | QStringList list = QStringList::split( ',', addr ); |
119 | QStringList::Iterator it; | 198 | QStringList::Iterator it; |
120 | for ( it = list.begin(); it != list.end(); it++ ) { | 199 | for ( it = list.begin(); it != list.end(); it++ ) { |
121 | char *str = strdup( (*it).latin1() ); | 200 | char *str = strdup( (*it).latin1() ); |
122 | int err = mailimf_address_list_add_parse( addresses, str ); | 201 | int err = mailimf_address_list_add_parse( addresses, str ); |
123 | if ( err != MAILIMF_NO_ERROR ) { | 202 | if ( err != MAILIMF_NO_ERROR ) { |
124 | qDebug( "Error parsing" ); | 203 | qDebug( "Error parsing" ); |
125 | qDebug( *it ); | 204 | qDebug( *it ); |
126 | free( str ); | 205 | free( str ); |
127 | } else { | 206 | } else { |
128 | qDebug( "Parse success! :)" ); | 207 | qDebug( "Parse success! :)" ); |
129 | } | 208 | } |
130 | } | 209 | } |
131 | 210 | ||
132 | return addresses; | 211 | return addresses; |
133 | } | 212 | } |
134 | 213 | ||
135 | mailimf_fields *MailWrapper::createImfFields( Mail *mail ) | 214 | mailimf_fields *MailWrapper::createImfFields( Mail *mail ) |
136 | { | 215 | { |
137 | mailimf_fields *fields; | 216 | mailimf_fields *fields; |
138 | mailimf_field *xmailer; | 217 | mailimf_field *xmailer; |
139 | mailimf_mailbox *sender, *fromBox; | 218 | mailimf_mailbox *sender, *fromBox; |
140 | mailimf_mailbox_list *from; | 219 | mailimf_mailbox_list *from; |
141 | mailimf_address_list *to, *cc, *bcc, *reply; | 220 | mailimf_address_list *to, *cc, *bcc, *reply; |
142 | char *subject = strdup( mail->getSubject().latin1() ); | 221 | char *subject = strdup( mail->getSubject().latin1() ); |
143 | int err; | 222 | int err; |
144 | 223 | ||
145 | sender = newMailbox( mail->getName(), mail->getMail() ); | 224 | sender = newMailbox( mail->getName(), mail->getMail() ); |
146 | if ( sender == NULL ) goto err_free; | 225 | if ( sender == NULL ) goto err_free; |
147 | 226 | ||
148 | fromBox = newMailbox( mail->getName(), mail->getMail() ); | 227 | fromBox = newMailbox( mail->getName(), mail->getMail() ); |
149 | if ( fromBox == NULL ) goto err_free_sender; | 228 | if ( fromBox == NULL ) goto err_free_sender; |
150 | 229 | ||
151 | from = mailimf_mailbox_list_new_empty(); | 230 | from = mailimf_mailbox_list_new_empty(); |
152 | if ( from == NULL ) goto err_free_fromBox; | 231 | if ( from == NULL ) goto err_free_fromBox; |
153 | 232 | ||
154 | err = mailimf_mailbox_list_add( from, fromBox ); | 233 | err = mailimf_mailbox_list_add( from, fromBox ); |
155 | if ( err != MAILIMF_NO_ERROR ) goto err_free_from; | 234 | if ( err != MAILIMF_NO_ERROR ) goto err_free_from; |
156 | 235 | ||
157 | to = parseAddresses( mail->getTo() ); | 236 | to = parseAddresses( mail->getTo() ); |
158 | if ( to == NULL ) goto err_free_from; | 237 | if ( to == NULL ) goto err_free_from; |
159 | 238 | ||
160 | cc = parseAddresses( mail->getCC() ); | 239 | cc = parseAddresses( mail->getCC() ); |
161 | bcc = parseAddresses( mail->getBCC() ); | 240 | bcc = parseAddresses( mail->getBCC() ); |
162 | reply = parseAddresses( mail->getReply() ); | 241 | reply = parseAddresses( mail->getReply() ); |
163 | 242 | ||
164 | fields = mailimf_fields_new_with_data( from, sender, reply, to, cc, bcc, | 243 | fields = mailimf_fields_new_with_data( from, sender, reply, to, cc, bcc, |
165 | NULL, NULL, subject ); | 244 | NULL, NULL, subject ); |
166 | if ( fields == NULL ) goto err_free_reply; | 245 | if ( fields == NULL ) goto err_free_reply; |
167 | 246 | ||
168 | xmailer = mailimf_field_new_custom( strdup( "User-Agent" ), | 247 | xmailer = mailimf_field_new_custom( strdup( "User-Agent" ), |
169 | strdup( USER_AGENT ) ); | 248 | strdup( USER_AGENT ) ); |
170 | if ( xmailer == NULL ) goto err_free_fields; | 249 | if ( xmailer == NULL ) goto err_free_fields; |
171 | 250 | ||
172 | err = mailimf_fields_add( fields, xmailer ); | 251 | err = mailimf_fields_add( fields, xmailer ); |
173 | if ( err != MAILIMF_NO_ERROR ) goto err_free_xmailer; | 252 | if ( err != MAILIMF_NO_ERROR ) goto err_free_xmailer; |
174 | 253 | ||
175 | return fields; // Success :) | 254 | return fields; // Success :) |
176 | 255 | ||
177 | err_free_xmailer: | 256 | err_free_xmailer: |
178 | mailimf_field_free( xmailer ); | 257 | mailimf_field_free( xmailer ); |
179 | err_free_fields: | 258 | err_free_fields: |
180 | mailimf_fields_free( fields ); | 259 | mailimf_fields_free( fields ); |
181 | err_free_reply: | 260 | err_free_reply: |
182 | mailimf_address_list_free( reply ); | 261 | mailimf_address_list_free( reply ); |
183 | mailimf_address_list_free( bcc ); | 262 | mailimf_address_list_free( bcc ); |
184 | mailimf_address_list_free( cc ); | 263 | mailimf_address_list_free( cc ); |
185 | mailimf_address_list_free( to ); | 264 | mailimf_address_list_free( to ); |
186 | err_free_from: | 265 | err_free_from: |
187 | mailimf_mailbox_list_free( from ); | 266 | mailimf_mailbox_list_free( from ); |
188 | err_free_fromBox: | 267 | err_free_fromBox: |
189 | mailimf_mailbox_free( fromBox ); | 268 | mailimf_mailbox_free( fromBox ); |
190 | err_free_sender: | 269 | err_free_sender: |
191 | mailimf_mailbox_free( sender ); | 270 | mailimf_mailbox_free( sender ); |
192 | err_free: | 271 | err_free: |
193 | free( subject ); | 272 | free( subject ); |
194 | qDebug( "createImfFields - error" ); | 273 | qDebug( "createImfFields - error" ); |
195 | 274 | ||
196 | return NULL; // Error :( | 275 | return NULL; // Error :( |
197 | } | 276 | } |
198 | 277 | ||
199 | mailmime *MailWrapper::buildTxtPart( QString str ) | 278 | mailmime *MailWrapper::buildTxtPart( QString str ) |
200 | { | 279 | { |
201 | mailmime *txtPart; | 280 | mailmime *txtPart; |
202 | mailmime_fields *fields; | 281 | mailmime_fields *fields; |
203 | mailmime_content *content; | 282 | mailmime_content *content; |
204 | mailmime_parameter *param; | 283 | mailmime_parameter *param; |
205 | char *txt = strdup( str.latin1() ); | 284 | char *txt = strdup( str.latin1() ); |
206 | int err; | 285 | int err; |
207 | 286 | ||
208 | param = mailmime_parameter_new( strdup( "charset" ), | 287 | param = mailmime_parameter_new( strdup( "charset" ), |
209 | strdup( "iso-8859-1" ) ); | 288 | strdup( "iso-8859-1" ) ); |
210 | if ( param == NULL ) goto err_free; | 289 | if ( param == NULL ) goto err_free; |
211 | 290 | ||
212 | content = mailmime_content_new_with_str( "text/plain" ); | 291 | content = mailmime_content_new_with_str( "text/plain" ); |
213 | if ( content == NULL ) goto err_free_param; | 292 | if ( content == NULL ) goto err_free_param; |
214 | 293 | ||
215 | err = clist_append( content->ct_parameters, param ); | 294 | err = clist_append( content->ct_parameters, param ); |
216 | if ( err != MAILIMF_NO_ERROR ) goto err_free_content; | 295 | if ( err != MAILIMF_NO_ERROR ) goto err_free_content; |
217 | 296 | ||
218 | fields = mailmime_fields_new_encoding( MAILMIME_MECHANISM_8BIT ); | 297 | fields = mailmime_fields_new_encoding( MAILMIME_MECHANISM_8BIT ); |
219 | if ( fields == NULL ) goto err_free_content; | 298 | if ( fields == NULL ) goto err_free_content; |
220 | 299 | ||
221 | txtPart = mailmime_new_empty( content, fields ); | 300 | txtPart = mailmime_new_empty( content, fields ); |
222 | if ( txtPart == NULL ) goto err_free_fields; | 301 | if ( txtPart == NULL ) goto err_free_fields; |
223 | 302 | ||
224 | err = mailmime_set_body_text( txtPart, txt, strlen( txt ) ); | 303 | err = mailmime_set_body_text( txtPart, txt, strlen( txt ) ); |
225 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; | 304 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; |
226 | 305 | ||
227 | return txtPart; // Success :) | 306 | return txtPart; // Success :) |
228 | 307 | ||
229 | err_free_txtPart: | 308 | err_free_txtPart: |
230 | mailmime_free( txtPart ); | 309 | mailmime_free( txtPart ); |
231 | err_free_fields: | 310 | err_free_fields: |
232 | mailmime_fields_free( fields ); | 311 | mailmime_fields_free( fields ); |
233 | err_free_content: | 312 | err_free_content: |
234 | mailmime_content_free( content ); | 313 | mailmime_content_free( content ); |
235 | err_free_param: | 314 | err_free_param: |
236 | mailmime_parameter_free( param ); | 315 | mailmime_parameter_free( param ); |
237 | err_free: | 316 | err_free: |
238 | free( txt ); | 317 | free( txt ); |
239 | qDebug( "buildTxtPart - error" ); | 318 | qDebug( "buildTxtPart - error" ); |
240 | 319 | ||
241 | return NULL; // Error :( | 320 | return NULL; // Error :( |
242 | } | 321 | } |
243 | 322 | ||
244 | mailmime *MailWrapper::buildFilePart( QString filename, QString mimetype ) | 323 | mailmime *MailWrapper::buildFilePart( QString filename, QString mimetype ) |
245 | { | 324 | { |
246 | mailmime * filePart; | 325 | mailmime * filePart; |
247 | mailmime_fields * fields; | 326 | mailmime_fields * fields; |
248 | mailmime_content * content; | 327 | mailmime_content * content; |
249 | mailmime_parameter * param = NULL; | 328 | mailmime_parameter * param = NULL; |
250 | int err; | 329 | int err; |
251 | 330 | ||
252 | int pos = filename.findRev( '/' ); | 331 | int pos = filename.findRev( '/' ); |
253 | QString tmp = filename.right( filename.length() - ( pos + 1 ) ); | 332 | QString tmp = filename.right( filename.length() - ( pos + 1 ) ); |
254 | char *name = strdup( tmp.latin1() ); // just filename | 333 | char *name = strdup( tmp.latin1() ); // just filename |
255 | char *file = strdup( filename.latin1() ); // full name with path | 334 | char *file = strdup( filename.latin1() ); // full name with path |
256 | char *mime = strdup( mimetype.latin1() ); // mimetype -e.g. text/plain | 335 | char *mime = strdup( mimetype.latin1() ); // mimetype -e.g. text/plain |
257 | 336 | ||
258 | fields = mailmime_fields_new_filename( | 337 | fields = mailmime_fields_new_filename( |
259 | MAILMIME_DISPOSITION_TYPE_ATTACHMENT, name, | 338 | MAILMIME_DISPOSITION_TYPE_ATTACHMENT, name, |
260 | MAILMIME_MECHANISM_BASE64 ); | 339 | MAILMIME_MECHANISM_BASE64 ); |
261 | if ( fields == NULL ) goto err_free; | 340 | if ( fields == NULL ) goto err_free; |
262 | 341 | ||
263 | content = mailmime_content_new_with_str( mime ); | 342 | content = mailmime_content_new_with_str( mime ); |
264 | if ( content == NULL ) goto err_free_fields; | 343 | if ( content == NULL ) goto err_free_fields; |
265 | 344 | ||
266 | if ( mimetype.compare( "text/plain" ) == 0 ) { | 345 | if ( mimetype.compare( "text/plain" ) == 0 ) { |
267 | param = mailmime_parameter_new( strdup( "charset" ), | 346 | param = mailmime_parameter_new( strdup( "charset" ), |
268 | strdup( "iso-8859-1" ) ); | 347 | strdup( "iso-8859-1" ) ); |
269 | if ( param == NULL ) goto err_free_content; | 348 | if ( param == NULL ) goto err_free_content; |
270 | 349 | ||
271 | err = clist_append( content->ct_parameters, param ); | 350 | err = clist_append( content->ct_parameters, param ); |
272 | if ( err != MAILIMF_NO_ERROR ) goto err_free_param; | 351 | if ( err != MAILIMF_NO_ERROR ) goto err_free_param; |
273 | } | 352 | } |
274 | 353 | ||
275 | filePart = mailmime_new_empty( content, fields ); | 354 | filePart = mailmime_new_empty( content, fields ); |
276 | if ( filePart == NULL ) goto err_free_param; | 355 | if ( filePart == NULL ) goto err_free_param; |
277 | 356 | ||
278 | err = mailmime_set_body_file( filePart, file ); | 357 | err = mailmime_set_body_file( filePart, file ); |
279 | if ( err != MAILIMF_NO_ERROR ) goto err_free_filePart; | 358 | if ( err != MAILIMF_NO_ERROR ) goto err_free_filePart; |
280 | 359 | ||
281 | return filePart; // Success :) | 360 | return filePart; // Success :) |
282 | 361 | ||
283 | err_free_filePart: | 362 | err_free_filePart: |
284 | mailmime_free( filePart ); | 363 | mailmime_free( filePart ); |
285 | err_free_param: | 364 | err_free_param: |
286 | if ( param != NULL ) mailmime_parameter_free( param ); | 365 | if ( param != NULL ) mailmime_parameter_free( param ); |
287 | err_free_content: | 366 | err_free_content: |
288 | mailmime_content_free( content ); | 367 | mailmime_content_free( content ); |
289 | err_free_fields: | 368 | err_free_fields: |
290 | mailmime_fields_free( fields ); | 369 | mailmime_fields_free( fields ); |
291 | err_free: | 370 | err_free: |
292 | free( name ); | 371 | free( name ); |
293 | free( mime ); | 372 | free( mime ); |
294 | free( file ); | 373 | free( file ); |
295 | qDebug( "buildFilePart - error" ); | 374 | qDebug( "buildFilePart - error" ); |
296 | 375 | ||
297 | return NULL; // Error :( | 376 | return NULL; // Error :( |
298 | } | 377 | } |
299 | 378 | ||
300 | void MailWrapper::addFileParts( mailmime *message, QList<Attachment> files ) | 379 | void MailWrapper::addFileParts( mailmime *message, QList<Attachment> files ) |
301 | { | 380 | { |
302 | Attachment *it; | 381 | Attachment *it; |
303 | for ( it = files.first(); it; it = files.next() ) { | 382 | for ( it = files.first(); it; it = files.next() ) { |
304 | qDebug( "Adding file" ); | 383 | qDebug( "Adding file" ); |
305 | mailmime *filePart; | 384 | mailmime *filePart; |
306 | int err; | 385 | int err; |
307 | 386 | ||
308 | filePart = buildFilePart( it->getFileName(), it->getMimeType() ); | 387 | filePart = buildFilePart( it->getFileName(), it->getMimeType() ); |
309 | if ( filePart == NULL ) goto err_free; | 388 | if ( filePart == NULL ) goto err_free; |
310 | 389 | ||
311 | err = mailmime_smart_add_part( message, filePart ); | 390 | err = mailmime_smart_add_part( message, filePart ); |
312 | if ( err != MAILIMF_NO_ERROR ) goto err_free_filePart; | 391 | if ( err != MAILIMF_NO_ERROR ) goto err_free_filePart; |
313 | 392 | ||
314 | continue; // Success :) | 393 | continue; // Success :) |
315 | 394 | ||
316 | err_free_filePart: | 395 | err_free_filePart: |
317 | mailmime_free( filePart ); | 396 | mailmime_free( filePart ); |
318 | err_free: | 397 | err_free: |
319 | qDebug( "addFileParts: error adding file:" ); | 398 | qDebug( "addFileParts: error adding file:" ); |
320 | qDebug( it->getFileName() ); | 399 | qDebug( it->getFileName() ); |
321 | } | 400 | } |
322 | } | 401 | } |
323 | 402 | ||
324 | mailmime *MailWrapper::createMimeMail( Mail *mail ) | 403 | mailmime *MailWrapper::createMimeMail( Mail *mail ) |
325 | { | 404 | { |
326 | mailmime *message, *txtPart; | 405 | mailmime *message, *txtPart; |
327 | mailimf_fields *fields; | 406 | mailimf_fields *fields; |
328 | int err; | 407 | int err; |
329 | 408 | ||
330 | fields = createImfFields( mail ); | 409 | fields = createImfFields( mail ); |
331 | if ( fields == NULL ) goto err_free; | 410 | if ( fields == NULL ) goto err_free; |
332 | 411 | ||
333 | message = mailmime_new_message_data( NULL ); | 412 | message = mailmime_new_message_data( NULL ); |
334 | if ( message == NULL ) goto err_free_fields; | 413 | if ( message == NULL ) goto err_free_fields; |
335 | 414 | ||
336 | mailmime_set_imf_fields( message, fields ); | 415 | mailmime_set_imf_fields( message, fields ); |
337 | 416 | ||
338 | txtPart = buildTxtPart( mail->getMessage() ); | 417 | txtPart = buildTxtPart( mail->getMessage() ); |
339 | if ( txtPart == NULL ) goto err_free_message; | 418 | if ( txtPart == NULL ) goto err_free_message; |
340 | 419 | ||
341 | err = mailmime_smart_add_part( message, txtPart ); | 420 | err = mailmime_smart_add_part( message, txtPart ); |
342 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; | 421 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; |
343 | 422 | ||
344 | addFileParts( message, mail->getAttachments() ); | 423 | addFileParts( message, mail->getAttachments() ); |
345 | 424 | ||
346 | return message; // Success :) | 425 | return message; // Success :) |
347 | 426 | ||
348 | err_free_txtPart: | 427 | err_free_txtPart: |
349 | mailmime_free( txtPart ); | 428 | mailmime_free( txtPart ); |
350 | err_free_message: | 429 | err_free_message: |
351 | mailmime_free( message ); | 430 | mailmime_free( message ); |
352 | err_free_fields: | 431 | err_free_fields: |
353 | mailimf_fields_free( fields ); | 432 | mailimf_fields_free( fields ); |
354 | err_free: | 433 | err_free: |
355 | qDebug( "createMimeMail: error" ); | 434 | qDebug( "createMimeMail: error" ); |
356 | 435 | ||
357 | return NULL; // Error :( | 436 | return NULL; // Error :( |
358 | } | 437 | } |
359 | 438 | ||
360 | mailimf_field *MailWrapper::getField( mailimf_fields *fields, int type ) | 439 | mailimf_field *MailWrapper::getField( mailimf_fields *fields, int type ) |
361 | { | 440 | { |
362 | mailimf_field *field; | 441 | mailimf_field *field; |
363 | clistiter *it; | 442 | clistiter *it; |
364 | 443 | ||
365 | it = clist_begin( fields->fld_list ); | 444 | it = clist_begin( fields->fld_list ); |
366 | while ( it ) { | 445 | while ( it ) { |
367 | field = (mailimf_field *) it->data; | 446 | field = (mailimf_field *) it->data; |
368 | if ( field->fld_type == type ) { | 447 | if ( field->fld_type == type ) { |
369 | return field; | 448 | return field; |
370 | } | 449 | } |
371 | it = it->next; | 450 | it = it->next; |
372 | } | 451 | } |
373 | 452 | ||
374 | return NULL; | 453 | return NULL; |
375 | } | 454 | } |
376 | 455 | ||
377 | static void addRcpts( clist *list, mailimf_address_list *addr_list ) | 456 | static void addRcpts( clist *list, mailimf_address_list *addr_list ) |
378 | { | 457 | { |
379 | clistiter *it, *it2; | 458 | clistiter *it, *it2; |
380 | 459 | ||
381 | for ( it = clist_begin( addr_list->ad_list ); it; it = it->next ) { | 460 | for ( it = clist_begin( addr_list->ad_list ); it; it = it->next ) { |
382 | mailimf_address *addr; | 461 | mailimf_address *addr; |
383 | addr = (mailimf_address *) it->data; | 462 | addr = (mailimf_address *) it->data; |
384 | 463 | ||
385 | if ( addr->ad_type == MAILIMF_ADDRESS_MAILBOX ) { | 464 | if ( addr->ad_type == MAILIMF_ADDRESS_MAILBOX ) { |
386 | esmtp_address_list_add( list, addr->ad_data.ad_mailbox->mb_addr_spec, 0, NULL ); | 465 | esmtp_address_list_add( list, addr->ad_data.ad_mailbox->mb_addr_spec, 0, NULL ); |
387 | } else if ( addr->ad_type == MAILIMF_ADDRESS_GROUP ) { | 466 | } else if ( addr->ad_type == MAILIMF_ADDRESS_GROUP ) { |
388 | clist *l = addr->ad_data.ad_group->grp_mb_list->mb_list; | 467 | clist *l = addr->ad_data.ad_group->grp_mb_list->mb_list; |
389 | for ( it2 = clist_begin( l ); it2; it2 = it2->next ) { | 468 | for ( it2 = clist_begin( l ); it2; it2 = it2->next ) { |
390 | mailimf_mailbox *mbox; | 469 | mailimf_mailbox *mbox; |
391 | mbox = (mailimf_mailbox *) it2->data; | 470 | mbox = (mailimf_mailbox *) it2->data; |
392 | esmtp_address_list_add( list, mbox->mb_addr_spec, 0, NULL ); | 471 | esmtp_address_list_add( list, mbox->mb_addr_spec, 0, NULL ); |
393 | } | 472 | } |
394 | } | 473 | } |
395 | } | 474 | } |
396 | } | 475 | } |
397 | 476 | ||
398 | clist *MailWrapper::createRcptList( mailimf_fields *fields ) | 477 | clist *MailWrapper::createRcptList( mailimf_fields *fields ) |
399 | { | 478 | { |
400 | clist *rcptList; | 479 | clist *rcptList; |
401 | mailimf_field *field; | 480 | mailimf_field *field; |
402 | 481 | ||
403 | rcptList = esmtp_address_list_new(); | 482 | rcptList = esmtp_address_list_new(); |
404 | 483 | ||
405 | field = getField( fields, MAILIMF_FIELD_TO ); | 484 | field = getField( fields, MAILIMF_FIELD_TO ); |
406 | if ( field && (field->fld_type == MAILIMF_FIELD_TO) | 485 | if ( field && (field->fld_type == MAILIMF_FIELD_TO) |
407 | && field->fld_data.fld_to->to_addr_list ) { | 486 | && field->fld_data.fld_to->to_addr_list ) { |
408 | addRcpts( rcptList, field->fld_data.fld_to->to_addr_list ); | 487 | addRcpts( rcptList, field->fld_data.fld_to->to_addr_list ); |
409 | } | 488 | } |
410 | 489 | ||
411 | field = getField( fields, MAILIMF_FIELD_CC ); | 490 | field = getField( fields, MAILIMF_FIELD_CC ); |
412 | if ( field && (field->fld_type == MAILIMF_FIELD_CC) | 491 | if ( field && (field->fld_type == MAILIMF_FIELD_CC) |
413 | && field->fld_data.fld_cc->cc_addr_list ) { | 492 | && field->fld_data.fld_cc->cc_addr_list ) { |
414 | addRcpts( rcptList, field->fld_data.fld_cc->cc_addr_list ); | 493 | addRcpts( rcptList, field->fld_data.fld_cc->cc_addr_list ); |
415 | } | 494 | } |
416 | 495 | ||
417 | field = getField( fields, MAILIMF_FIELD_BCC ); | 496 | field = getField( fields, MAILIMF_FIELD_BCC ); |
418 | if ( field && (field->fld_type == MAILIMF_FIELD_BCC) | 497 | if ( field && (field->fld_type == MAILIMF_FIELD_BCC) |
419 | && field->fld_data.fld_bcc->bcc_addr_list ) { | 498 | && field->fld_data.fld_bcc->bcc_addr_list ) { |
420 | addRcpts( rcptList, field->fld_data.fld_bcc->bcc_addr_list ); | 499 | addRcpts( rcptList, field->fld_data.fld_bcc->bcc_addr_list ); |
421 | } | 500 | } |
422 | 501 | ||
423 | return rcptList; | 502 | return rcptList; |
424 | } | 503 | } |
425 | 504 | ||
426 | char *MailWrapper::getFrom( mailmime *mail ) | 505 | char *MailWrapper::getFrom( mailmime *mail ) |
427 | { | 506 | { |
428 | char *from = NULL; | 507 | char *from = NULL; |
429 | 508 | ||
430 | mailimf_field *ffrom; | 509 | mailimf_field *ffrom; |
431 | ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM ); | 510 | ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM ); |
432 | if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM) | 511 | if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM) |
433 | && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) { | 512 | && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) { |
434 | clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list; | 513 | clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list; |
435 | clistiter *it; | 514 | clistiter *it; |
436 | for ( it = clist_begin( cl ); it; it = it->next ) { | 515 | for ( it = clist_begin( cl ); it; it = it->next ) { |
437 | mailimf_mailbox *mb = (mailimf_mailbox *) it->data; | 516 | mailimf_mailbox *mb = (mailimf_mailbox *) it->data; |
438 | from = strdup( mb->mb_addr_spec ); | 517 | from = strdup( mb->mb_addr_spec ); |
439 | } | 518 | } |
440 | } | 519 | } |
441 | 520 | ||
442 | return from; | 521 | return from; |
443 | } | 522 | } |
444 | 523 | ||
445 | SMTPaccount *MailWrapper::getAccount( QString from ) | 524 | SMTPaccount *MailWrapper::getAccount( QString from ) |
446 | { | 525 | { |
447 | SMTPaccount *smtp; | 526 | SMTPaccount *smtp; |
448 | 527 | ||
449 | QList<Account> list = settings->getAccounts(); | 528 | QList<Account> list = settings->getAccounts(); |
450 | Account *it; | 529 | Account *it; |
451 | for ( it = list.first(); it; it = list.next() ) { | 530 | for ( it = list.first(); it; it = list.next() ) { |
452 | if ( it->getType().compare( "SMTP" ) == 0 ) { | 531 | if ( it->getType().compare( "SMTP" ) == 0 ) { |
453 | smtp = static_cast<SMTPaccount *>(it); | 532 | smtp = static_cast<SMTPaccount *>(it); |
454 | if ( smtp->getMail().compare( from ) == 0 ) { | 533 | if ( smtp->getMail().compare( from ) == 0 ) { |
455 | qDebug( "SMTPaccount found for" ); | 534 | qDebug( "SMTPaccount found for" ); |
456 | qDebug( from ); | 535 | qDebug( from ); |
457 | return smtp; | 536 | return smtp; |
458 | } | 537 | } |
459 | } | 538 | } |
460 | } | 539 | } |
461 | 540 | ||
462 | return NULL; | 541 | return NULL; |
463 | } | 542 | } |
464 | 543 | ||
465 | QString MailWrapper::getTmpFile() { | 544 | QString MailWrapper::getTmpFile() { |
466 | int num = 0; | 545 | int num = 0; |
467 | QString unique; | 546 | QString unique; |
468 | 547 | ||
469 | QDir dir( "/tmp" ); | 548 | QDir dir( "/tmp" ); |
470 | QStringList::Iterator it; | 549 | QStringList::Iterator it; |
471 | 550 | ||
472 | QStringList list = dir.entryList( "opiemail-tmp-*" ); | 551 | QStringList list = dir.entryList( "opiemail-tmp-*" ); |
473 | do { | 552 | do { |
474 | unique.setNum( num++ ); | 553 | unique.setNum( num++ ); |
475 | } while ( list.contains( "opiemail-tmp-" + unique ) > 0 ); | 554 | } while ( list.contains( "opiemail-tmp-" + unique ) > 0 ); |
476 | 555 | ||
477 | return "/tmp/opiemail-tmp-" + unique; | 556 | return "/tmp/opiemail-tmp-" + unique; |
478 | } | 557 | } |
479 | 558 | ||
480 | void MailWrapper::writeToFile( QString file, mailmime *mail ) | 559 | void MailWrapper::writeToFile( QString file, mailmime *mail ) |
481 | { | 560 | { |
482 | FILE *f; | 561 | FILE *f; |
483 | int err, col = 0; | 562 | int err, col = 0; |
484 | 563 | ||
485 | f = fopen( file.latin1(), "w" ); | 564 | f = fopen( file.latin1(), "w" ); |
486 | if ( f == NULL ) { | 565 | if ( f == NULL ) { |
487 | qDebug( "writeToFile: error opening file" ); | 566 | qDebug( "writeToFile: error opening file" ); |
488 | return; | 567 | return; |
489 | } | 568 | } |
490 | 569 | ||
491 | err = mailmime_write( f, &col, mail ); | 570 | err = mailmime_write( f, &col, mail ); |
492 | if ( err != MAILIMF_NO_ERROR ) { | 571 | if ( err != MAILIMF_NO_ERROR ) { |
493 | fclose( f ); | 572 | fclose( f ); |
494 | qDebug( "writeToFile: error writing mailmime" ); | 573 | qDebug( "writeToFile: error writing mailmime" ); |
495 | return; | 574 | return; |
496 | } | 575 | } |
497 | 576 | ||
498 | fclose( f ); | 577 | fclose( f ); |
499 | } | 578 | } |
500 | 579 | ||
501 | void MailWrapper::readFromFile( QString file, char **data, size_t *size ) | 580 | void MailWrapper::readFromFile( QString file, char **data, size_t *size ) |
502 | { | 581 | { |
503 | char *buf; | 582 | char *buf; |
504 | struct stat st; | 583 | struct stat st; |
505 | int fd, count = 0, total = 0; | 584 | int fd, count = 0, total = 0; |
506 | 585 | ||
507 | fd = open( file.latin1(), O_RDONLY, 0 ); | 586 | fd = open( file.latin1(), O_RDONLY, 0 ); |
508 | if ( fd == -1 ) return; | 587 | if ( fd == -1 ) return; |
509 | 588 | ||
510 | if ( fstat( fd, &st ) != 0 ) goto err_close; | 589 | if ( fstat( fd, &st ) != 0 ) goto err_close; |
511 | if ( !st.st_size ) goto err_close; | 590 | if ( !st.st_size ) goto err_close; |
512 | 591 | ||
513 | buf = (char *) malloc( st.st_size ); | 592 | buf = (char *) malloc( st.st_size ); |
514 | if ( !buf ) goto err_close; | 593 | if ( !buf ) goto err_close; |
515 | 594 | ||
516 | while ( ( total < st.st_size ) && ( count >= 0 ) ) { | 595 | while ( ( total < st.st_size ) && ( count >= 0 ) ) { |
517 | count = read( fd, buf + total, st.st_size - total ); | 596 | count = read( fd, buf + total, st.st_size - total ); |
518 | total += count; | 597 | total += count; |
519 | } | 598 | } |
520 | if ( count < 0 ) goto err_free; | 599 | if ( count < 0 ) goto err_free; |
521 | 600 | ||
522 | *data = buf; | 601 | *data = buf; |
523 | *size = st.st_size; | 602 | *size = st.st_size; |
524 | 603 | ||
525 | close( fd ); | 604 | close( fd ); |
526 | 605 | ||
527 | return; // Success :) | 606 | return; // Success :) |
528 | 607 | ||
529 | err_free: | 608 | err_free: |
530 | free( buf ); | 609 | free( buf ); |
531 | err_close: | 610 | err_close: |
532 | close( fd ); | 611 | close( fd ); |
533 | } | 612 | } |
534 | 613 | ||
535 | void progress( size_t current, size_t maximum ) | 614 | void progress( size_t current, size_t maximum ) |
536 | { | 615 | { |
537 | qDebug( "Current: %i of %i", current, maximum ); | 616 | qDebug( "Current: %i of %i", current, maximum ); |
538 | } | 617 | } |
539 | 618 | ||
540 | void MailWrapper::smtpSend( mailmime *mail ) | 619 | void MailWrapper::smtpSend( mailmime *mail ) |
541 | { | 620 | { |
542 | mailsmtp *session; | 621 | mailsmtp *session; |
543 | clist *rcpts; | 622 | clist *rcpts; |
544 | char *from, *data, *server, *user = NULL, *pass = NULL; | 623 | char *from, *data, *server, *user = NULL, *pass = NULL; |
545 | size_t size; | 624 | size_t size; |
546 | int err; | 625 | int err; |
547 | bool ssl; | 626 | bool ssl; |
548 | uint16_t port; | 627 | uint16_t port; |
549 | 628 | ||
550 | 629 | ||
551 | from = getFrom( mail ); | 630 | from = getFrom( mail ); |
552 | SMTPaccount *smtp = getAccount( from ); | 631 | SMTPaccount *smtp = getAccount( from ); |
553 | if ( smtp == NULL ) { | 632 | if ( smtp == NULL ) { |
554 | free(from); | 633 | free(from); |
555 | return; | 634 | return; |
556 | } | 635 | } |
557 | server = strdup( smtp->getServer().latin1() ); | 636 | server = strdup( smtp->getServer().latin1() ); |
558 | ssl = smtp->getSSL(); | 637 | ssl = smtp->getSSL(); |
559 | port = smtp->getPort().toUInt(); | 638 | port = smtp->getPort().toUInt(); |
560 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); | 639 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); |
561 | 640 | ||
562 | QString file = getTmpFile(); | 641 | QString file = getTmpFile(); |
563 | writeToFile( file, mail ); | 642 | writeToFile( file, mail ); |
564 | readFromFile( file, &data, &size ); | 643 | readFromFile( file, &data, &size ); |
565 | QFile f( file ); | 644 | QFile f( file ); |
566 | f.remove(); | 645 | f.remove(); |
567 | 646 | ||
568 | session = mailsmtp_new( 20, &progress ); | 647 | session = mailsmtp_new( 20, &progress ); |
569 | if ( session == NULL ) goto free_mem; | 648 | if ( session == NULL ) goto free_mem; |
570 | 649 | ||
571 | qDebug( "Servername %s at port %i", server, port ); | 650 | qDebug( "Servername %s at port %i", server, port ); |
572 | if ( ssl ) { | 651 | if ( ssl ) { |
573 | qDebug( "SSL session" ); | 652 | qDebug( "SSL session" ); |
574 | err = mailsmtp_ssl_connect( session, server, port ); | 653 | err = mailsmtp_ssl_connect( session, server, port ); |
575 | } else { | 654 | } else { |
576 | qDebug( "No SSL session" ); | 655 | qDebug( "No SSL session" ); |
577 | err = mailsmtp_socket_connect( session, server, port ); | 656 | err = mailsmtp_socket_connect( session, server, port ); |
578 | } | 657 | } |
579 | if ( err != MAILSMTP_NO_ERROR ) goto free_mem_session; | 658 | if ( err != MAILSMTP_NO_ERROR ) goto free_mem_session; |
580 | 659 | ||
581 | err = mailsmtp_init( session ); | 660 | err = mailsmtp_init( session ); |
582 | if ( err != MAILSMTP_NO_ERROR ) goto free_con_session; | 661 | if ( err != MAILSMTP_NO_ERROR ) goto free_con_session; |
583 | 662 | ||
584 | qDebug( "INIT OK" ); | 663 | qDebug( "INIT OK" ); |
585 | 664 | ||
586 | if ( smtp->getLogin() ) { | 665 | if ( smtp->getLogin() ) { |
587 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { | 666 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { |
588 | // get'em | 667 | // get'em |
589 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); | 668 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); |
590 | login.show(); | 669 | login.show(); |
591 | if ( QDialog::Accepted == login.exec() ) { | 670 | if ( QDialog::Accepted == login.exec() ) { |
592 | // ok | 671 | // ok |
593 | user = strdup( login.getUser().latin1() ); | 672 | user = strdup( login.getUser().latin1() ); |
594 | pass = strdup( login.getPassword().latin1() ); | 673 | pass = strdup( login.getPassword().latin1() ); |
595 | } else { | 674 | } else { |
596 | goto free_con_session; | 675 | goto free_con_session; |
597 | } | 676 | } |
598 | } else { | 677 | } else { |
599 | user = strdup( smtp->getUser().latin1() ); | 678 | user = strdup( smtp->getUser().latin1() ); |
600 | pass = strdup( smtp->getPassword().latin1() ); | 679 | pass = strdup( smtp->getPassword().latin1() ); |
601 | } | 680 | } |
602 | qDebug( "session->auth: %i", session->auth); | 681 | qDebug( "session->auth: %i", session->auth); |
603 | err = mailsmtp_auth( session, user, pass ); | 682 | err = mailsmtp_auth( session, user, pass ); |
604 | if ( err == MAILSMTP_NO_ERROR ) qDebug("auth ok"); | 683 | if ( err == MAILSMTP_NO_ERROR ) qDebug("auth ok"); |
605 | qDebug( "Done auth!" ); | 684 | qDebug( "Done auth!" ); |
606 | } | 685 | } |
607 | 686 | ||
608 | err = mailsmtp_send( session, from, rcpts, data, size ); | 687 | err = mailsmtp_send( session, from, rcpts, data, size ); |
609 | if ( err != MAILSMTP_NO_ERROR ) goto free_con_session; | 688 | if ( err != MAILSMTP_NO_ERROR ) goto free_con_session; |
610 | 689 | ||
611 | qDebug( "Mail sent." ); | 690 | qDebug( "Mail sent." ); |
612 | 691 | ||
613 | free_con_session: | 692 | free_con_session: |
614 | mailsmtp_quit( session ); | 693 | mailsmtp_quit( session ); |
615 | free_mem_session: | 694 | free_mem_session: |
616 | mailsmtp_free( session ); | 695 | mailsmtp_free( session ); |
617 | free_mem: | 696 | free_mem: |
618 | smtp_address_list_free( rcpts ); | 697 | smtp_address_list_free( rcpts ); |
619 | free( data ); | 698 | free( data ); |
620 | free( server ); | 699 | free( server ); |
621 | if ( smtp->getLogin() ) { | 700 | if ( smtp->getLogin() ) { |
622 | free( user ); | 701 | free( user ); |
623 | free( pass ); | 702 | free( pass ); |
624 | } | 703 | } |
625 | free( from ); | 704 | free( from ); |
626 | } | 705 | } |
627 | 706 | ||
628 | void MailWrapper::sendMail( Mail mail ) | 707 | void MailWrapper::sendMail( Mail mail ) |
629 | { | 708 | { |
630 | mailmime *mimeMail; | 709 | mailmime *mimeMail; |
631 | 710 | ||
632 | mimeMail = createMimeMail( &mail ); | 711 | mimeMail = createMimeMail( &mail ); |
633 | if ( mimeMail == NULL ) { | 712 | if ( mimeMail == NULL ) { |
634 | qDebug( "sendMail: error creating mime mail" ); | 713 | qDebug( "sendMail: error creating mime mail" ); |
635 | } else { | 714 | } else { |
636 | smtpSend( mimeMail ); | 715 | smtpSend( mimeMail ); |
637 | mailmime_free( mimeMail ); | 716 | mailmime_free( mimeMail ); |
638 | } | 717 | } |
639 | } | 718 | } |
640 | 719 | ||
641 | Mail::Mail() | 720 | Mail::Mail() |
642 | :name(""), mail(""), to(""), cc(""), bcc(""), reply(""), subject(""), message("") | 721 | :name(""), mail(""), to(""), cc(""), bcc(""), reply(""), subject(""), message("") |
643 | { | 722 | { |
644 | } | 723 | } |
diff --git a/noncore/net/mail/libmailwrapper/mailwrapper.h b/noncore/net/mail/libmailwrapper/mailwrapper.h index d78f8e9..02fe4b7 100644 --- a/noncore/net/mail/libmailwrapper/mailwrapper.h +++ b/noncore/net/mail/libmailwrapper/mailwrapper.h | |||
@@ -1,124 +1,126 @@ | |||
1 | #ifndef MAILWRAPPER_H | 1 | #ifndef MAILWRAPPER_H |
2 | #define MAILWRAPPER_H | 2 | #define MAILWRAPPER_H |
3 | 3 | ||
4 | #include <qpe/applnk.h> | 4 | #include <qpe/applnk.h> |
5 | 5 | ||
6 | #include <libetpan/mailmime.h> | 6 | #include <libetpan/mailmime.h> |
7 | #include <libetpan/mailimf.h> | 7 | #include <libetpan/mailimf.h> |
8 | #include <libetpan/mailsmtp.h> | 8 | #include <libetpan/mailsmtp.h> |
9 | #include <libetpan/mailstorage.h> | 9 | #include <libetpan/mailstorage.h> |
10 | #include <libetpan/maildriver.h> | 10 | #include <libetpan/maildriver.h> |
11 | #include <qbitarray.h> | 11 | #include <qbitarray.h> |
12 | #include <qdatetime.h> | 12 | #include <qdatetime.h> |
13 | 13 | ||
14 | #include "settings.h" | 14 | #include "settings.h" |
15 | 15 | ||
16 | class Attachment | 16 | class Attachment |
17 | { | 17 | { |
18 | public: | 18 | public: |
19 | Attachment( DocLnk lnk ); | 19 | Attachment( DocLnk lnk ); |
20 | virtual ~Attachment(){} | 20 | virtual ~Attachment(){} |
21 | const QString getFileName()const{ return doc.file(); } | 21 | const QString getFileName()const{ return doc.file(); } |
22 | const QString getName()const{ return doc.name(); } | 22 | const QString getName()const{ return doc.name(); } |
23 | const QString getMimeType()const{ return doc.type(); } | 23 | const QString getMimeType()const{ return doc.type(); } |
24 | const QPixmap getPixmap()const{ return doc.pixmap(); } | 24 | const QPixmap getPixmap()const{ return doc.pixmap(); } |
25 | const int getSize()const { return size; } | 25 | const int getSize()const { return size; } |
26 | DocLnk getDocLnk() { return doc; } | 26 | DocLnk getDocLnk() { return doc; } |
27 | 27 | ||
28 | protected: | 28 | protected: |
29 | DocLnk doc; | 29 | DocLnk doc; |
30 | int size; | 30 | int size; |
31 | 31 | ||
32 | }; | 32 | }; |
33 | 33 | ||
34 | class Mail | 34 | class Mail |
35 | { | 35 | { |
36 | public: | 36 | public: |
37 | Mail(); | 37 | Mail(); |
38 | /* Possible that this destructor must not be declared virtual | 38 | /* Possible that this destructor must not be declared virtual |
39 | * 'cause it seems that it will never have some child classes. | 39 | * 'cause it seems that it will never have some child classes. |
40 | * in this case this object will not get a virtual table -> memory and | 40 | * in this case this object will not get a virtual table -> memory and |
41 | * speed will be a little bit better? | 41 | * speed will be a little bit better? |
42 | */ | 42 | */ |
43 | virtual ~Mail(){} | 43 | virtual ~Mail(){} |
44 | void addAttachment( Attachment *att ) { attList.append( att ); } | 44 | void addAttachment( Attachment *att ) { attList.append( att ); } |
45 | const QList<Attachment>& getAttachments()const { return attList; } | 45 | const QList<Attachment>& getAttachments()const { return attList; } |
46 | void removeAttachment( Attachment *att ) { attList.remove( att ); } | 46 | void removeAttachment( Attachment *att ) { attList.remove( att ); } |
47 | const QString&getName()const { return name; } | 47 | const QString&getName()const { return name; } |
48 | void setName( QString s ) { name = s; } | 48 | void setName( QString s ) { name = s; } |
49 | const QString&getMail()const{ return mail; } | 49 | const QString&getMail()const{ return mail; } |
50 | void setMail( const QString&s ) { mail = s; } | 50 | void setMail( const QString&s ) { mail = s; } |
51 | const QString&getTo()const{ return to; } | 51 | const QString&getTo()const{ return to; } |
52 | void setTo( const QString&s ) { to = s; } | 52 | void setTo( const QString&s ) { to = s; } |
53 | const QString&getCC()const{ return cc; } | 53 | const QString&getCC()const{ return cc; } |
54 | void setCC( const QString&s ) { cc = s; } | 54 | void setCC( const QString&s ) { cc = s; } |
55 | const QString&getBCC()const { return bcc; } | 55 | const QString&getBCC()const { return bcc; } |
56 | void setBCC( const QString&s ) { bcc = s; } | 56 | void setBCC( const QString&s ) { bcc = s; } |
57 | const QString&getMessage()const { return message; } | 57 | const QString&getMessage()const { return message; } |
58 | void setMessage( const QString&s ) { message = s; } | 58 | void setMessage( const QString&s ) { message = s; } |
59 | const QString&getSubject()const { return subject; } | 59 | const QString&getSubject()const { return subject; } |
60 | void setSubject( const QString&s ) { subject = s; } | 60 | void setSubject( const QString&s ) { subject = s; } |
61 | const QString&getReply()const{ return reply; } | 61 | const QString&getReply()const{ return reply; } |
62 | void setReply( const QString&a ) { reply = a; } | 62 | void setReply( const QString&a ) { reply = a; } |
63 | 63 | ||
64 | private: | 64 | private: |
65 | QList<Attachment> attList; | 65 | QList<Attachment> attList; |
66 | QString name, mail, to, cc, bcc, reply, subject, message; | 66 | QString name, mail, to, cc, bcc, reply, subject, message; |
67 | }; | 67 | }; |
68 | 68 | ||
69 | class Folder : public QObject | 69 | class Folder : public QObject |
70 | { | 70 | { |
71 | Q_OBJECT | 71 | Q_OBJECT |
72 | 72 | ||
73 | public: | 73 | public: |
74 | Folder( const QString&init_name,const QString&sep ); | 74 | Folder( const QString&init_name,const QString&sep ); |
75 | const QString&getDisplayName()const { return nameDisplay; } | 75 | const QString&getDisplayName()const { return nameDisplay; } |
76 | const QString&getName()const { return name; } | 76 | const QString&getName()const { return name; } |
77 | virtual bool may_select()const{return true;}; | 77 | virtual bool may_select()const{return true;}; |
78 | const QString&Separator()const; | 78 | const QString&Separator()const; |
79 | 79 | ||
80 | protected: | 80 | protected: |
81 | QString nameDisplay, name, separator; | 81 | QString nameDisplay, name, separator; |
82 | 82 | ||
83 | }; | 83 | }; |
84 | 84 | ||
85 | class IMAPFolder : public Folder | 85 | class IMAPFolder : public Folder |
86 | { | 86 | { |
87 | public: | 87 | public: |
88 | IMAPFolder(const QString&name, const QString&sep, bool select=true,const QString&prefix="" ); | 88 | IMAPFolder(const QString&name, const QString&sep, bool select=true,const QString&prefix="" ); |
89 | virtual bool may_select()const{return m_MaySelect;} | 89 | virtual bool may_select()const{return m_MaySelect;} |
90 | private: | 90 | private: |
91 | static QString decodeFolderName( const QString &name ); | ||
91 | bool m_MaySelect; | 92 | bool m_MaySelect; |
93 | |||
92 | }; | 94 | }; |
93 | 95 | ||
94 | class MailWrapper : public QObject | 96 | class MailWrapper : public QObject |
95 | { | 97 | { |
96 | Q_OBJECT | 98 | Q_OBJECT |
97 | 99 | ||
98 | public: | 100 | public: |
99 | MailWrapper( Settings *s ); | 101 | MailWrapper( Settings *s ); |
100 | void sendMail( Mail mail ); | 102 | void sendMail( Mail mail ); |
101 | 103 | ||
102 | private: | 104 | private: |
103 | mailimf_mailbox *newMailbox(const QString&name,const QString&mail ); | 105 | mailimf_mailbox *newMailbox(const QString&name,const QString&mail ); |
104 | mailimf_address_list *parseAddresses(const QString&addr ); | 106 | mailimf_address_list *parseAddresses(const QString&addr ); |
105 | mailimf_fields *createImfFields( Mail *mail ); | 107 | mailimf_fields *createImfFields( Mail *mail ); |
106 | mailmime *buildTxtPart( QString str ); | 108 | mailmime *buildTxtPart( QString str ); |
107 | mailmime *buildFilePart( QString filename, QString mimetype ); | 109 | mailmime *buildFilePart( QString filename, QString mimetype ); |
108 | void addFileParts( mailmime *message, QList<Attachment> files ); | 110 | void addFileParts( mailmime *message, QList<Attachment> files ); |
109 | mailmime *createMimeMail( Mail *mail ); | 111 | mailmime *createMimeMail( Mail *mail ); |
110 | void smtpSend( mailmime *mail ); | 112 | void smtpSend( mailmime *mail ); |
111 | mailimf_field *getField( mailimf_fields *fields, int type ); | 113 | mailimf_field *getField( mailimf_fields *fields, int type ); |
112 | clist *createRcptList( mailimf_fields *fields ); | 114 | clist *createRcptList( mailimf_fields *fields ); |
113 | char *getFrom( mailmime *mail ); | 115 | char *getFrom( mailmime *mail ); |
114 | SMTPaccount *getAccount( QString from ); | 116 | SMTPaccount *getAccount( QString from ); |
115 | void writeToFile( QString file, mailmime *mail ); | 117 | void writeToFile( QString file, mailmime *mail ); |
116 | void readFromFile( QString file, char **data, size_t *size ); | 118 | void readFromFile( QString file, char **data, size_t *size ); |
117 | static QString mailsmtpError( int err ); | 119 | static QString mailsmtpError( int err ); |
118 | static QString getTmpFile(); | 120 | static QString getTmpFile(); |
119 | 121 | ||
120 | Settings *settings; | 122 | Settings *settings; |
121 | 123 | ||
122 | }; | 124 | }; |
123 | 125 | ||
124 | #endif | 126 | #endif |
diff --git a/noncore/net/mail/mailwrapper.cpp b/noncore/net/mail/mailwrapper.cpp index 858283f..75c06f9 100644 --- a/noncore/net/mail/mailwrapper.cpp +++ b/noncore/net/mail/mailwrapper.cpp | |||
@@ -1,644 +1,723 @@ | |||
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 | 8 | ||
9 | #include "mailwrapper.h" | 9 | #include "mailwrapper.h" |
10 | #include "logindialog.h" | 10 | #include "logindialog.h" |
11 | //#include "mail.h" | 11 | //#include "mail.h" |
12 | #include "defines.h" | 12 | #include "defines.h" |
13 | 13 | ||
14 | Attachment::Attachment( DocLnk lnk ) | 14 | Attachment::Attachment( DocLnk lnk ) |
15 | { | 15 | { |
16 | doc = lnk; | 16 | doc = lnk; |
17 | size = QFileInfo( doc.file() ).size(); | 17 | size = QFileInfo( doc.file() ).size(); |
18 | } | 18 | } |
19 | 19 | ||
20 | Folder::Folder(const QString&tmp_name, const QString&sep ) | 20 | Folder::Folder(const QString&tmp_name, const QString&sep ) |
21 | { | 21 | { |
22 | name = tmp_name; | 22 | name = tmp_name; |
23 | nameDisplay = name; | 23 | nameDisplay = name; |
24 | |||
25 | for ( int pos = nameDisplay.find( '&' ); pos != -1; | ||
26 | pos = nameDisplay.find( '&' ) ) { | ||
27 | int end = nameDisplay.find( '-' ); | ||
28 | if ( end == -1 || end <= pos ) break; | ||
29 | QString str64 = nameDisplay.mid( pos + 1, end - pos - 1 ); | ||
30 | // TODO: do real base64 decoding here ! | ||
31 | if ( str64.compare( "APw" ) == 0 ) { | ||
32 | nameDisplay = nameDisplay.replace( pos, end - pos + 1, "ue" ); | ||
33 | } else if ( str64.compare( "APY" ) == 0 ) { | ||
34 | nameDisplay = nameDisplay.replace( pos, end - pos + 1, "oe" ); | ||
35 | } | ||
36 | } | ||
37 | qDebug( "folder " + name + " - displayed as " + nameDisplay ); | ||
38 | separator = sep; | 24 | separator = sep; |
39 | } | 25 | } |
40 | 26 | ||
41 | const QString& Folder::Separator()const | 27 | const QString& Folder::Separator()const |
42 | { | 28 | { |
43 | return separator; | 29 | return separator; |
44 | } | 30 | } |
45 | 31 | ||
46 | IMAPFolder::IMAPFolder(const QString&name,const QString&sep, bool select,const QString&prefix ) | 32 | IMAPFolder::IMAPFolder(const QString&name,const QString&sep, bool select,const QString&prefix ) |
47 | : Folder( name,sep ),m_MaySelect(select) | 33 | : Folder( name,sep ),m_MaySelect(select) |
48 | { | 34 | { |
35 | // Decode IMAP foldername | ||
36 | nameDisplay = IMAPFolder::decodeFolderName( name ); | ||
37 | qDebug( "folder " + name + " - displayed as " + nameDisplay ); | ||
38 | |||
49 | if (prefix.length()>0) { | 39 | if (prefix.length()>0) { |
50 | if (nameDisplay.startsWith(prefix) && nameDisplay.length()>prefix.length()) { | 40 | if (nameDisplay.startsWith(prefix) && nameDisplay.length()>prefix.length()) { |
51 | nameDisplay=nameDisplay.right(nameDisplay.length()-prefix.length()); | 41 | nameDisplay=nameDisplay.right(nameDisplay.length()-prefix.length()); |
52 | } | 42 | } |
53 | } | 43 | } |
54 | } | 44 | } |
55 | 45 | ||
46 | static unsigned char base64chars[] = | ||
47 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+,"; | ||
48 | |||
49 | /** | ||
50 | * Decodes base64 encoded parts of the imapfolder name | ||
51 | * Code taken from kde cvs: kdebase/kioslave/imap4/rfcdecoder.cc | ||
52 | */ | ||
53 | QString IMAPFolder::decodeFolderName( const QString &name ) | ||
54 | { | ||
55 | unsigned char c, i, bitcount; | ||
56 | unsigned long ucs4, utf16, bitbuf; | ||
57 | unsigned char base64[256], utf8[6]; | ||
58 | unsigned long srcPtr = 0; | ||
59 | QCString dst; | ||
60 | QCString src = name.ascii(); | ||
61 | |||
62 | /* initialize modified base64 decoding table */ | ||
63 | memset(base64, UNDEFINED, sizeof(base64)); | ||
64 | for (i = 0; i < sizeof(base64chars); ++i) { | ||
65 | base64[(int)base64chars[i]] = i; | ||
66 | } | ||
67 | |||
68 | /* loop until end of string */ | ||
69 | while (srcPtr < src.length ()) { | ||
70 | c = src[srcPtr++]; | ||
71 | /* deal with literal characters and &- */ | ||
72 | if (c != '&' || src[srcPtr] == '-') { | ||
73 | /* encode literally */ | ||
74 | dst += c; | ||
75 | /* skip over the '-' if this is an &- sequence */ | ||
76 | if (c == '&') | ||
77 | srcPtr++; | ||
78 | } else { | ||
79 | /* convert modified UTF-7 -> UTF-16 -> UCS-4 -> UTF-8 -> HEX */ | ||
80 | bitbuf = 0; | ||
81 | bitcount = 0; | ||
82 | ucs4 = 0; | ||
83 | while ((c = base64[(unsigned char) src[srcPtr]]) != UNDEFINED) { | ||
84 | ++srcPtr; | ||
85 | bitbuf = (bitbuf << 6) | c; | ||
86 | bitcount += 6; | ||
87 | /* enough bits for a UTF-16 character? */ | ||
88 | if (bitcount >= 16) { | ||
89 | bitcount -= 16; | ||
90 | utf16 = (bitcount ? bitbuf >> bitcount : bitbuf) & 0xffff; | ||
91 | /* convert UTF16 to UCS4 */ | ||
92 | if (utf16 >= UTF16HIGHSTART && utf16 <= UTF16HIGHEND) { | ||
93 | ucs4 = (utf16 - UTF16HIGHSTART) << UTF16SHIFT; | ||
94 | continue; | ||
95 | } else if (utf16 >= UTF16LOSTART && utf16 <= UTF16LOEND) { | ||
96 | ucs4 += utf16 - UTF16LOSTART + UTF16BASE; | ||
97 | } else { | ||
98 | ucs4 = utf16; | ||
99 | } | ||
100 | /* convert UTF-16 range of UCS4 to UTF-8 */ | ||
101 | if (ucs4 <= 0x7fUL) { | ||
102 | utf8[0] = ucs4; | ||
103 | i = 1; | ||
104 | } else if (ucs4 <= 0x7ffUL) { | ||
105 | utf8[0] = 0xc0 | (ucs4 >> 6); | ||
106 | utf8[1] = 0x80 | (ucs4 & 0x3f); | ||
107 | i = 2; | ||
108 | } else if (ucs4 <= 0xffffUL) { | ||
109 | utf8[0] = 0xe0 | (ucs4 >> 12); | ||
110 | utf8[1] = 0x80 | ((ucs4 >> 6) & 0x3f); | ||
111 | utf8[2] = 0x80 | (ucs4 & 0x3f); | ||
112 | i = 3; | ||
113 | } else { | ||
114 | utf8[0] = 0xf0 | (ucs4 >> 18); | ||
115 | utf8[1] = 0x80 | ((ucs4 >> 12) & 0x3f); | ||
116 | utf8[2] = 0x80 | ((ucs4 >> 6) & 0x3f); | ||
117 | utf8[3] = 0x80 | (ucs4 & 0x3f); | ||
118 | i = 4; | ||
119 | } | ||
120 | /* copy it */ | ||
121 | for (c = 0; c < i; ++c) { | ||
122 | dst += utf8[c]; | ||
123 | } | ||
124 | } | ||
125 | } | ||
126 | /* skip over trailing '-' in modified UTF-7 encoding */ | ||
127 | if (src[srcPtr] == '-') | ||
128 | ++srcPtr; | ||
129 | } | ||
130 | } | ||
131 | |||
132 | return QString::fromUtf8( dst.data() ); | ||
133 | } | ||
134 | |||
56 | MailWrapper::MailWrapper( Settings *s ) | 135 | MailWrapper::MailWrapper( Settings *s ) |
57 | : QObject() | 136 | : QObject() |
58 | { | 137 | { |
59 | settings = s; | 138 | settings = s; |
60 | } | 139 | } |
61 | 140 | ||
62 | QString MailWrapper::mailsmtpError( int errnum ) | 141 | QString MailWrapper::mailsmtpError( int errnum ) |
63 | { | 142 | { |
64 | switch ( errnum ) { | 143 | switch ( errnum ) { |
65 | case MAILSMTP_NO_ERROR: | 144 | case MAILSMTP_NO_ERROR: |
66 | return tr( "No error" ); | 145 | return tr( "No error" ); |
67 | case MAILSMTP_ERROR_UNEXPECTED_CODE: | 146 | case MAILSMTP_ERROR_UNEXPECTED_CODE: |
68 | return tr( "Unexpected error code" ); | 147 | return tr( "Unexpected error code" ); |
69 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: | 148 | case MAILSMTP_ERROR_SERVICE_NOT_AVAILABLE: |
70 | return tr( "Service not available" ); | 149 | return tr( "Service not available" ); |
71 | case MAILSMTP_ERROR_STREAM: | 150 | case MAILSMTP_ERROR_STREAM: |
72 | return tr( "Stream error" ); | 151 | return tr( "Stream error" ); |
73 | case MAILSMTP_ERROR_HOSTNAME: | 152 | case MAILSMTP_ERROR_HOSTNAME: |
74 | return tr( "gethostname() failed" ); | 153 | return tr( "gethostname() failed" ); |
75 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: | 154 | case MAILSMTP_ERROR_NOT_IMPLEMENTED: |
76 | return tr( "Not implemented" ); | 155 | return tr( "Not implemented" ); |
77 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: | 156 | case MAILSMTP_ERROR_ACTION_NOT_TAKEN: |
78 | return tr( "Error, action not taken" ); | 157 | return tr( "Error, action not taken" ); |
79 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: | 158 | case MAILSMTP_ERROR_EXCEED_STORAGE_ALLOCATION: |
80 | return tr( "Data exceeds storage allocation" ); | 159 | return tr( "Data exceeds storage allocation" ); |
81 | case MAILSMTP_ERROR_IN_PROCESSING: | 160 | case MAILSMTP_ERROR_IN_PROCESSING: |
82 | return tr( "Error in processing" ); | 161 | return tr( "Error in processing" ); |
83 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: | 162 | // case MAILSMTP_ERROR_INSUFFISANT_SYSTEM_STORAGE: |
84 | // return tr( "Insufficient system storage" ); | 163 | // return tr( "Insufficient system storage" ); |
85 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: | 164 | case MAILSMTP_ERROR_MAILBOX_UNAVAILABLE: |
86 | return tr( "Mailbox unavailable" ); | 165 | return tr( "Mailbox unavailable" ); |
87 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: | 166 | case MAILSMTP_ERROR_MAILBOX_NAME_NOT_ALLOWED: |
88 | return tr( "Mailbox name not allowed" ); | 167 | return tr( "Mailbox name not allowed" ); |
89 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: | 168 | case MAILSMTP_ERROR_BAD_SEQUENCE_OF_COMMAND: |
90 | return tr( "Bad command sequence" ); | 169 | return tr( "Bad command sequence" ); |
91 | case MAILSMTP_ERROR_USER_NOT_LOCAL: | 170 | case MAILSMTP_ERROR_USER_NOT_LOCAL: |
92 | return tr( "User not local" ); | 171 | return tr( "User not local" ); |
93 | case MAILSMTP_ERROR_TRANSACTION_FAILED: | 172 | case MAILSMTP_ERROR_TRANSACTION_FAILED: |
94 | return tr( "Transaction failed" ); | 173 | return tr( "Transaction failed" ); |
95 | case MAILSMTP_ERROR_MEMORY: | 174 | case MAILSMTP_ERROR_MEMORY: |
96 | return tr( "Memory error" ); | 175 | return tr( "Memory error" ); |
97 | case MAILSMTP_ERROR_CONNECTION_REFUSED: | 176 | case MAILSMTP_ERROR_CONNECTION_REFUSED: |
98 | return tr( "Connection refused" ); | 177 | return tr( "Connection refused" ); |
99 | default: | 178 | default: |
100 | return tr( "Unknown error code" ); | 179 | return tr( "Unknown error code" ); |
101 | } | 180 | } |
102 | } | 181 | } |
103 | 182 | ||
104 | mailimf_mailbox *MailWrapper::newMailbox(const QString&name, const QString&mail ) | 183 | mailimf_mailbox *MailWrapper::newMailbox(const QString&name, const QString&mail ) |
105 | { | 184 | { |
106 | return mailimf_mailbox_new( strdup( name.latin1() ), | 185 | return mailimf_mailbox_new( strdup( name.latin1() ), |
107 | strdup( mail.latin1() ) ); | 186 | strdup( mail.latin1() ) ); |
108 | } | 187 | } |
109 | 188 | ||
110 | mailimf_address_list *MailWrapper::parseAddresses(const QString&addr ) | 189 | mailimf_address_list *MailWrapper::parseAddresses(const QString&addr ) |
111 | { | 190 | { |
112 | mailimf_address_list *addresses; | 191 | mailimf_address_list *addresses; |
113 | 192 | ||
114 | if ( addr.isEmpty() ) return NULL; | 193 | if ( addr.isEmpty() ) return NULL; |
115 | 194 | ||
116 | addresses = mailimf_address_list_new_empty(); | 195 | addresses = mailimf_address_list_new_empty(); |
117 | 196 | ||
118 | QStringList list = QStringList::split( ',', addr ); | 197 | QStringList list = QStringList::split( ',', addr ); |
119 | QStringList::Iterator it; | 198 | QStringList::Iterator it; |
120 | for ( it = list.begin(); it != list.end(); it++ ) { | 199 | for ( it = list.begin(); it != list.end(); it++ ) { |
121 | char *str = strdup( (*it).latin1() ); | 200 | char *str = strdup( (*it).latin1() ); |
122 | int err = mailimf_address_list_add_parse( addresses, str ); | 201 | int err = mailimf_address_list_add_parse( addresses, str ); |
123 | if ( err != MAILIMF_NO_ERROR ) { | 202 | if ( err != MAILIMF_NO_ERROR ) { |
124 | qDebug( "Error parsing" ); | 203 | qDebug( "Error parsing" ); |
125 | qDebug( *it ); | 204 | qDebug( *it ); |
126 | free( str ); | 205 | free( str ); |
127 | } else { | 206 | } else { |
128 | qDebug( "Parse success! :)" ); | 207 | qDebug( "Parse success! :)" ); |
129 | } | 208 | } |
130 | } | 209 | } |
131 | 210 | ||
132 | return addresses; | 211 | return addresses; |
133 | } | 212 | } |
134 | 213 | ||
135 | mailimf_fields *MailWrapper::createImfFields( Mail *mail ) | 214 | mailimf_fields *MailWrapper::createImfFields( Mail *mail ) |
136 | { | 215 | { |
137 | mailimf_fields *fields; | 216 | mailimf_fields *fields; |
138 | mailimf_field *xmailer; | 217 | mailimf_field *xmailer; |
139 | mailimf_mailbox *sender, *fromBox; | 218 | mailimf_mailbox *sender, *fromBox; |
140 | mailimf_mailbox_list *from; | 219 | mailimf_mailbox_list *from; |
141 | mailimf_address_list *to, *cc, *bcc, *reply; | 220 | mailimf_address_list *to, *cc, *bcc, *reply; |
142 | char *subject = strdup( mail->getSubject().latin1() ); | 221 | char *subject = strdup( mail->getSubject().latin1() ); |
143 | int err; | 222 | int err; |
144 | 223 | ||
145 | sender = newMailbox( mail->getName(), mail->getMail() ); | 224 | sender = newMailbox( mail->getName(), mail->getMail() ); |
146 | if ( sender == NULL ) goto err_free; | 225 | if ( sender == NULL ) goto err_free; |
147 | 226 | ||
148 | fromBox = newMailbox( mail->getName(), mail->getMail() ); | 227 | fromBox = newMailbox( mail->getName(), mail->getMail() ); |
149 | if ( fromBox == NULL ) goto err_free_sender; | 228 | if ( fromBox == NULL ) goto err_free_sender; |
150 | 229 | ||
151 | from = mailimf_mailbox_list_new_empty(); | 230 | from = mailimf_mailbox_list_new_empty(); |
152 | if ( from == NULL ) goto err_free_fromBox; | 231 | if ( from == NULL ) goto err_free_fromBox; |
153 | 232 | ||
154 | err = mailimf_mailbox_list_add( from, fromBox ); | 233 | err = mailimf_mailbox_list_add( from, fromBox ); |
155 | if ( err != MAILIMF_NO_ERROR ) goto err_free_from; | 234 | if ( err != MAILIMF_NO_ERROR ) goto err_free_from; |
156 | 235 | ||
157 | to = parseAddresses( mail->getTo() ); | 236 | to = parseAddresses( mail->getTo() ); |
158 | if ( to == NULL ) goto err_free_from; | 237 | if ( to == NULL ) goto err_free_from; |
159 | 238 | ||
160 | cc = parseAddresses( mail->getCC() ); | 239 | cc = parseAddresses( mail->getCC() ); |
161 | bcc = parseAddresses( mail->getBCC() ); | 240 | bcc = parseAddresses( mail->getBCC() ); |
162 | reply = parseAddresses( mail->getReply() ); | 241 | reply = parseAddresses( mail->getReply() ); |
163 | 242 | ||
164 | fields = mailimf_fields_new_with_data( from, sender, reply, to, cc, bcc, | 243 | fields = mailimf_fields_new_with_data( from, sender, reply, to, cc, bcc, |
165 | NULL, NULL, subject ); | 244 | NULL, NULL, subject ); |
166 | if ( fields == NULL ) goto err_free_reply; | 245 | if ( fields == NULL ) goto err_free_reply; |
167 | 246 | ||
168 | xmailer = mailimf_field_new_custom( strdup( "User-Agent" ), | 247 | xmailer = mailimf_field_new_custom( strdup( "User-Agent" ), |
169 | strdup( USER_AGENT ) ); | 248 | strdup( USER_AGENT ) ); |
170 | if ( xmailer == NULL ) goto err_free_fields; | 249 | if ( xmailer == NULL ) goto err_free_fields; |
171 | 250 | ||
172 | err = mailimf_fields_add( fields, xmailer ); | 251 | err = mailimf_fields_add( fields, xmailer ); |
173 | if ( err != MAILIMF_NO_ERROR ) goto err_free_xmailer; | 252 | if ( err != MAILIMF_NO_ERROR ) goto err_free_xmailer; |
174 | 253 | ||
175 | return fields; // Success :) | 254 | return fields; // Success :) |
176 | 255 | ||
177 | err_free_xmailer: | 256 | err_free_xmailer: |
178 | mailimf_field_free( xmailer ); | 257 | mailimf_field_free( xmailer ); |
179 | err_free_fields: | 258 | err_free_fields: |
180 | mailimf_fields_free( fields ); | 259 | mailimf_fields_free( fields ); |
181 | err_free_reply: | 260 | err_free_reply: |
182 | mailimf_address_list_free( reply ); | 261 | mailimf_address_list_free( reply ); |
183 | mailimf_address_list_free( bcc ); | 262 | mailimf_address_list_free( bcc ); |
184 | mailimf_address_list_free( cc ); | 263 | mailimf_address_list_free( cc ); |
185 | mailimf_address_list_free( to ); | 264 | mailimf_address_list_free( to ); |
186 | err_free_from: | 265 | err_free_from: |
187 | mailimf_mailbox_list_free( from ); | 266 | mailimf_mailbox_list_free( from ); |
188 | err_free_fromBox: | 267 | err_free_fromBox: |
189 | mailimf_mailbox_free( fromBox ); | 268 | mailimf_mailbox_free( fromBox ); |
190 | err_free_sender: | 269 | err_free_sender: |
191 | mailimf_mailbox_free( sender ); | 270 | mailimf_mailbox_free( sender ); |
192 | err_free: | 271 | err_free: |
193 | free( subject ); | 272 | free( subject ); |
194 | qDebug( "createImfFields - error" ); | 273 | qDebug( "createImfFields - error" ); |
195 | 274 | ||
196 | return NULL; // Error :( | 275 | return NULL; // Error :( |
197 | } | 276 | } |
198 | 277 | ||
199 | mailmime *MailWrapper::buildTxtPart( QString str ) | 278 | mailmime *MailWrapper::buildTxtPart( QString str ) |
200 | { | 279 | { |
201 | mailmime *txtPart; | 280 | mailmime *txtPart; |
202 | mailmime_fields *fields; | 281 | mailmime_fields *fields; |
203 | mailmime_content *content; | 282 | mailmime_content *content; |
204 | mailmime_parameter *param; | 283 | mailmime_parameter *param; |
205 | char *txt = strdup( str.latin1() ); | 284 | char *txt = strdup( str.latin1() ); |
206 | int err; | 285 | int err; |
207 | 286 | ||
208 | param = mailmime_parameter_new( strdup( "charset" ), | 287 | param = mailmime_parameter_new( strdup( "charset" ), |
209 | strdup( "iso-8859-1" ) ); | 288 | strdup( "iso-8859-1" ) ); |
210 | if ( param == NULL ) goto err_free; | 289 | if ( param == NULL ) goto err_free; |
211 | 290 | ||
212 | content = mailmime_content_new_with_str( "text/plain" ); | 291 | content = mailmime_content_new_with_str( "text/plain" ); |
213 | if ( content == NULL ) goto err_free_param; | 292 | if ( content == NULL ) goto err_free_param; |
214 | 293 | ||
215 | err = clist_append( content->ct_parameters, param ); | 294 | err = clist_append( content->ct_parameters, param ); |
216 | if ( err != MAILIMF_NO_ERROR ) goto err_free_content; | 295 | if ( err != MAILIMF_NO_ERROR ) goto err_free_content; |
217 | 296 | ||
218 | fields = mailmime_fields_new_encoding( MAILMIME_MECHANISM_8BIT ); | 297 | fields = mailmime_fields_new_encoding( MAILMIME_MECHANISM_8BIT ); |
219 | if ( fields == NULL ) goto err_free_content; | 298 | if ( fields == NULL ) goto err_free_content; |
220 | 299 | ||
221 | txtPart = mailmime_new_empty( content, fields ); | 300 | txtPart = mailmime_new_empty( content, fields ); |
222 | if ( txtPart == NULL ) goto err_free_fields; | 301 | if ( txtPart == NULL ) goto err_free_fields; |
223 | 302 | ||
224 | err = mailmime_set_body_text( txtPart, txt, strlen( txt ) ); | 303 | err = mailmime_set_body_text( txtPart, txt, strlen( txt ) ); |
225 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; | 304 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; |
226 | 305 | ||
227 | return txtPart; // Success :) | 306 | return txtPart; // Success :) |
228 | 307 | ||
229 | err_free_txtPart: | 308 | err_free_txtPart: |
230 | mailmime_free( txtPart ); | 309 | mailmime_free( txtPart ); |
231 | err_free_fields: | 310 | err_free_fields: |
232 | mailmime_fields_free( fields ); | 311 | mailmime_fields_free( fields ); |
233 | err_free_content: | 312 | err_free_content: |
234 | mailmime_content_free( content ); | 313 | mailmime_content_free( content ); |
235 | err_free_param: | 314 | err_free_param: |
236 | mailmime_parameter_free( param ); | 315 | mailmime_parameter_free( param ); |
237 | err_free: | 316 | err_free: |
238 | free( txt ); | 317 | free( txt ); |
239 | qDebug( "buildTxtPart - error" ); | 318 | qDebug( "buildTxtPart - error" ); |
240 | 319 | ||
241 | return NULL; // Error :( | 320 | return NULL; // Error :( |
242 | } | 321 | } |
243 | 322 | ||
244 | mailmime *MailWrapper::buildFilePart( QString filename, QString mimetype ) | 323 | mailmime *MailWrapper::buildFilePart( QString filename, QString mimetype ) |
245 | { | 324 | { |
246 | mailmime * filePart; | 325 | mailmime * filePart; |
247 | mailmime_fields * fields; | 326 | mailmime_fields * fields; |
248 | mailmime_content * content; | 327 | mailmime_content * content; |
249 | mailmime_parameter * param = NULL; | 328 | mailmime_parameter * param = NULL; |
250 | int err; | 329 | int err; |
251 | 330 | ||
252 | int pos = filename.findRev( '/' ); | 331 | int pos = filename.findRev( '/' ); |
253 | QString tmp = filename.right( filename.length() - ( pos + 1 ) ); | 332 | QString tmp = filename.right( filename.length() - ( pos + 1 ) ); |
254 | char *name = strdup( tmp.latin1() ); // just filename | 333 | char *name = strdup( tmp.latin1() ); // just filename |
255 | char *file = strdup( filename.latin1() ); // full name with path | 334 | char *file = strdup( filename.latin1() ); // full name with path |
256 | char *mime = strdup( mimetype.latin1() ); // mimetype -e.g. text/plain | 335 | char *mime = strdup( mimetype.latin1() ); // mimetype -e.g. text/plain |
257 | 336 | ||
258 | fields = mailmime_fields_new_filename( | 337 | fields = mailmime_fields_new_filename( |
259 | MAILMIME_DISPOSITION_TYPE_ATTACHMENT, name, | 338 | MAILMIME_DISPOSITION_TYPE_ATTACHMENT, name, |
260 | MAILMIME_MECHANISM_BASE64 ); | 339 | MAILMIME_MECHANISM_BASE64 ); |
261 | if ( fields == NULL ) goto err_free; | 340 | if ( fields == NULL ) goto err_free; |
262 | 341 | ||
263 | content = mailmime_content_new_with_str( mime ); | 342 | content = mailmime_content_new_with_str( mime ); |
264 | if ( content == NULL ) goto err_free_fields; | 343 | if ( content == NULL ) goto err_free_fields; |
265 | 344 | ||
266 | if ( mimetype.compare( "text/plain" ) == 0 ) { | 345 | if ( mimetype.compare( "text/plain" ) == 0 ) { |
267 | param = mailmime_parameter_new( strdup( "charset" ), | 346 | param = mailmime_parameter_new( strdup( "charset" ), |
268 | strdup( "iso-8859-1" ) ); | 347 | strdup( "iso-8859-1" ) ); |
269 | if ( param == NULL ) goto err_free_content; | 348 | if ( param == NULL ) goto err_free_content; |
270 | 349 | ||
271 | err = clist_append( content->ct_parameters, param ); | 350 | err = clist_append( content->ct_parameters, param ); |
272 | if ( err != MAILIMF_NO_ERROR ) goto err_free_param; | 351 | if ( err != MAILIMF_NO_ERROR ) goto err_free_param; |
273 | } | 352 | } |
274 | 353 | ||
275 | filePart = mailmime_new_empty( content, fields ); | 354 | filePart = mailmime_new_empty( content, fields ); |
276 | if ( filePart == NULL ) goto err_free_param; | 355 | if ( filePart == NULL ) goto err_free_param; |
277 | 356 | ||
278 | err = mailmime_set_body_file( filePart, file ); | 357 | err = mailmime_set_body_file( filePart, file ); |
279 | if ( err != MAILIMF_NO_ERROR ) goto err_free_filePart; | 358 | if ( err != MAILIMF_NO_ERROR ) goto err_free_filePart; |
280 | 359 | ||
281 | return filePart; // Success :) | 360 | return filePart; // Success :) |
282 | 361 | ||
283 | err_free_filePart: | 362 | err_free_filePart: |
284 | mailmime_free( filePart ); | 363 | mailmime_free( filePart ); |
285 | err_free_param: | 364 | err_free_param: |
286 | if ( param != NULL ) mailmime_parameter_free( param ); | 365 | if ( param != NULL ) mailmime_parameter_free( param ); |
287 | err_free_content: | 366 | err_free_content: |
288 | mailmime_content_free( content ); | 367 | mailmime_content_free( content ); |
289 | err_free_fields: | 368 | err_free_fields: |
290 | mailmime_fields_free( fields ); | 369 | mailmime_fields_free( fields ); |
291 | err_free: | 370 | err_free: |
292 | free( name ); | 371 | free( name ); |
293 | free( mime ); | 372 | free( mime ); |
294 | free( file ); | 373 | free( file ); |
295 | qDebug( "buildFilePart - error" ); | 374 | qDebug( "buildFilePart - error" ); |
296 | 375 | ||
297 | return NULL; // Error :( | 376 | return NULL; // Error :( |
298 | } | 377 | } |
299 | 378 | ||
300 | void MailWrapper::addFileParts( mailmime *message, QList<Attachment> files ) | 379 | void MailWrapper::addFileParts( mailmime *message, QList<Attachment> files ) |
301 | { | 380 | { |
302 | Attachment *it; | 381 | Attachment *it; |
303 | for ( it = files.first(); it; it = files.next() ) { | 382 | for ( it = files.first(); it; it = files.next() ) { |
304 | qDebug( "Adding file" ); | 383 | qDebug( "Adding file" ); |
305 | mailmime *filePart; | 384 | mailmime *filePart; |
306 | int err; | 385 | int err; |
307 | 386 | ||
308 | filePart = buildFilePart( it->getFileName(), it->getMimeType() ); | 387 | filePart = buildFilePart( it->getFileName(), it->getMimeType() ); |
309 | if ( filePart == NULL ) goto err_free; | 388 | if ( filePart == NULL ) goto err_free; |
310 | 389 | ||
311 | err = mailmime_smart_add_part( message, filePart ); | 390 | err = mailmime_smart_add_part( message, filePart ); |
312 | if ( err != MAILIMF_NO_ERROR ) goto err_free_filePart; | 391 | if ( err != MAILIMF_NO_ERROR ) goto err_free_filePart; |
313 | 392 | ||
314 | continue; // Success :) | 393 | continue; // Success :) |
315 | 394 | ||
316 | err_free_filePart: | 395 | err_free_filePart: |
317 | mailmime_free( filePart ); | 396 | mailmime_free( filePart ); |
318 | err_free: | 397 | err_free: |
319 | qDebug( "addFileParts: error adding file:" ); | 398 | qDebug( "addFileParts: error adding file:" ); |
320 | qDebug( it->getFileName() ); | 399 | qDebug( it->getFileName() ); |
321 | } | 400 | } |
322 | } | 401 | } |
323 | 402 | ||
324 | mailmime *MailWrapper::createMimeMail( Mail *mail ) | 403 | mailmime *MailWrapper::createMimeMail( Mail *mail ) |
325 | { | 404 | { |
326 | mailmime *message, *txtPart; | 405 | mailmime *message, *txtPart; |
327 | mailimf_fields *fields; | 406 | mailimf_fields *fields; |
328 | int err; | 407 | int err; |
329 | 408 | ||
330 | fields = createImfFields( mail ); | 409 | fields = createImfFields( mail ); |
331 | if ( fields == NULL ) goto err_free; | 410 | if ( fields == NULL ) goto err_free; |
332 | 411 | ||
333 | message = mailmime_new_message_data( NULL ); | 412 | message = mailmime_new_message_data( NULL ); |
334 | if ( message == NULL ) goto err_free_fields; | 413 | if ( message == NULL ) goto err_free_fields; |
335 | 414 | ||
336 | mailmime_set_imf_fields( message, fields ); | 415 | mailmime_set_imf_fields( message, fields ); |
337 | 416 | ||
338 | txtPart = buildTxtPart( mail->getMessage() ); | 417 | txtPart = buildTxtPart( mail->getMessage() ); |
339 | if ( txtPart == NULL ) goto err_free_message; | 418 | if ( txtPart == NULL ) goto err_free_message; |
340 | 419 | ||
341 | err = mailmime_smart_add_part( message, txtPart ); | 420 | err = mailmime_smart_add_part( message, txtPart ); |
342 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; | 421 | if ( err != MAILIMF_NO_ERROR ) goto err_free_txtPart; |
343 | 422 | ||
344 | addFileParts( message, mail->getAttachments() ); | 423 | addFileParts( message, mail->getAttachments() ); |
345 | 424 | ||
346 | return message; // Success :) | 425 | return message; // Success :) |
347 | 426 | ||
348 | err_free_txtPart: | 427 | err_free_txtPart: |
349 | mailmime_free( txtPart ); | 428 | mailmime_free( txtPart ); |
350 | err_free_message: | 429 | err_free_message: |
351 | mailmime_free( message ); | 430 | mailmime_free( message ); |
352 | err_free_fields: | 431 | err_free_fields: |
353 | mailimf_fields_free( fields ); | 432 | mailimf_fields_free( fields ); |
354 | err_free: | 433 | err_free: |
355 | qDebug( "createMimeMail: error" ); | 434 | qDebug( "createMimeMail: error" ); |
356 | 435 | ||
357 | return NULL; // Error :( | 436 | return NULL; // Error :( |
358 | } | 437 | } |
359 | 438 | ||
360 | mailimf_field *MailWrapper::getField( mailimf_fields *fields, int type ) | 439 | mailimf_field *MailWrapper::getField( mailimf_fields *fields, int type ) |
361 | { | 440 | { |
362 | mailimf_field *field; | 441 | mailimf_field *field; |
363 | clistiter *it; | 442 | clistiter *it; |
364 | 443 | ||
365 | it = clist_begin( fields->fld_list ); | 444 | it = clist_begin( fields->fld_list ); |
366 | while ( it ) { | 445 | while ( it ) { |
367 | field = (mailimf_field *) it->data; | 446 | field = (mailimf_field *) it->data; |
368 | if ( field->fld_type == type ) { | 447 | if ( field->fld_type == type ) { |
369 | return field; | 448 | return field; |
370 | } | 449 | } |
371 | it = it->next; | 450 | it = it->next; |
372 | } | 451 | } |
373 | 452 | ||
374 | return NULL; | 453 | return NULL; |
375 | } | 454 | } |
376 | 455 | ||
377 | static void addRcpts( clist *list, mailimf_address_list *addr_list ) | 456 | static void addRcpts( clist *list, mailimf_address_list *addr_list ) |
378 | { | 457 | { |
379 | clistiter *it, *it2; | 458 | clistiter *it, *it2; |
380 | 459 | ||
381 | for ( it = clist_begin( addr_list->ad_list ); it; it = it->next ) { | 460 | for ( it = clist_begin( addr_list->ad_list ); it; it = it->next ) { |
382 | mailimf_address *addr; | 461 | mailimf_address *addr; |
383 | addr = (mailimf_address *) it->data; | 462 | addr = (mailimf_address *) it->data; |
384 | 463 | ||
385 | if ( addr->ad_type == MAILIMF_ADDRESS_MAILBOX ) { | 464 | if ( addr->ad_type == MAILIMF_ADDRESS_MAILBOX ) { |
386 | esmtp_address_list_add( list, addr->ad_data.ad_mailbox->mb_addr_spec, 0, NULL ); | 465 | esmtp_address_list_add( list, addr->ad_data.ad_mailbox->mb_addr_spec, 0, NULL ); |
387 | } else if ( addr->ad_type == MAILIMF_ADDRESS_GROUP ) { | 466 | } else if ( addr->ad_type == MAILIMF_ADDRESS_GROUP ) { |
388 | clist *l = addr->ad_data.ad_group->grp_mb_list->mb_list; | 467 | clist *l = addr->ad_data.ad_group->grp_mb_list->mb_list; |
389 | for ( it2 = clist_begin( l ); it2; it2 = it2->next ) { | 468 | for ( it2 = clist_begin( l ); it2; it2 = it2->next ) { |
390 | mailimf_mailbox *mbox; | 469 | mailimf_mailbox *mbox; |
391 | mbox = (mailimf_mailbox *) it2->data; | 470 | mbox = (mailimf_mailbox *) it2->data; |
392 | esmtp_address_list_add( list, mbox->mb_addr_spec, 0, NULL ); | 471 | esmtp_address_list_add( list, mbox->mb_addr_spec, 0, NULL ); |
393 | } | 472 | } |
394 | } | 473 | } |
395 | } | 474 | } |
396 | } | 475 | } |
397 | 476 | ||
398 | clist *MailWrapper::createRcptList( mailimf_fields *fields ) | 477 | clist *MailWrapper::createRcptList( mailimf_fields *fields ) |
399 | { | 478 | { |
400 | clist *rcptList; | 479 | clist *rcptList; |
401 | mailimf_field *field; | 480 | mailimf_field *field; |
402 | 481 | ||
403 | rcptList = esmtp_address_list_new(); | 482 | rcptList = esmtp_address_list_new(); |
404 | 483 | ||
405 | field = getField( fields, MAILIMF_FIELD_TO ); | 484 | field = getField( fields, MAILIMF_FIELD_TO ); |
406 | if ( field && (field->fld_type == MAILIMF_FIELD_TO) | 485 | if ( field && (field->fld_type == MAILIMF_FIELD_TO) |
407 | && field->fld_data.fld_to->to_addr_list ) { | 486 | && field->fld_data.fld_to->to_addr_list ) { |
408 | addRcpts( rcptList, field->fld_data.fld_to->to_addr_list ); | 487 | addRcpts( rcptList, field->fld_data.fld_to->to_addr_list ); |
409 | } | 488 | } |
410 | 489 | ||
411 | field = getField( fields, MAILIMF_FIELD_CC ); | 490 | field = getField( fields, MAILIMF_FIELD_CC ); |
412 | if ( field && (field->fld_type == MAILIMF_FIELD_CC) | 491 | if ( field && (field->fld_type == MAILIMF_FIELD_CC) |
413 | && field->fld_data.fld_cc->cc_addr_list ) { | 492 | && field->fld_data.fld_cc->cc_addr_list ) { |
414 | addRcpts( rcptList, field->fld_data.fld_cc->cc_addr_list ); | 493 | addRcpts( rcptList, field->fld_data.fld_cc->cc_addr_list ); |
415 | } | 494 | } |
416 | 495 | ||
417 | field = getField( fields, MAILIMF_FIELD_BCC ); | 496 | field = getField( fields, MAILIMF_FIELD_BCC ); |
418 | if ( field && (field->fld_type == MAILIMF_FIELD_BCC) | 497 | if ( field && (field->fld_type == MAILIMF_FIELD_BCC) |
419 | && field->fld_data.fld_bcc->bcc_addr_list ) { | 498 | && field->fld_data.fld_bcc->bcc_addr_list ) { |
420 | addRcpts( rcptList, field->fld_data.fld_bcc->bcc_addr_list ); | 499 | addRcpts( rcptList, field->fld_data.fld_bcc->bcc_addr_list ); |
421 | } | 500 | } |
422 | 501 | ||
423 | return rcptList; | 502 | return rcptList; |
424 | } | 503 | } |
425 | 504 | ||
426 | char *MailWrapper::getFrom( mailmime *mail ) | 505 | char *MailWrapper::getFrom( mailmime *mail ) |
427 | { | 506 | { |
428 | char *from = NULL; | 507 | char *from = NULL; |
429 | 508 | ||
430 | mailimf_field *ffrom; | 509 | mailimf_field *ffrom; |
431 | ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM ); | 510 | ffrom = getField( mail->mm_data.mm_message.mm_fields, MAILIMF_FIELD_FROM ); |
432 | if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM) | 511 | if ( ffrom && (ffrom->fld_type == MAILIMF_FIELD_FROM) |
433 | && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) { | 512 | && ffrom->fld_data.fld_from->frm_mb_list && ffrom->fld_data.fld_from->frm_mb_list->mb_list ) { |
434 | clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list; | 513 | clist *cl = ffrom->fld_data.fld_from->frm_mb_list->mb_list; |
435 | clistiter *it; | 514 | clistiter *it; |
436 | for ( it = clist_begin( cl ); it; it = it->next ) { | 515 | for ( it = clist_begin( cl ); it; it = it->next ) { |
437 | mailimf_mailbox *mb = (mailimf_mailbox *) it->data; | 516 | mailimf_mailbox *mb = (mailimf_mailbox *) it->data; |
438 | from = strdup( mb->mb_addr_spec ); | 517 | from = strdup( mb->mb_addr_spec ); |
439 | } | 518 | } |
440 | } | 519 | } |
441 | 520 | ||
442 | return from; | 521 | return from; |
443 | } | 522 | } |
444 | 523 | ||
445 | SMTPaccount *MailWrapper::getAccount( QString from ) | 524 | SMTPaccount *MailWrapper::getAccount( QString from ) |
446 | { | 525 | { |
447 | SMTPaccount *smtp; | 526 | SMTPaccount *smtp; |
448 | 527 | ||
449 | QList<Account> list = settings->getAccounts(); | 528 | QList<Account> list = settings->getAccounts(); |
450 | Account *it; | 529 | Account *it; |
451 | for ( it = list.first(); it; it = list.next() ) { | 530 | for ( it = list.first(); it; it = list.next() ) { |
452 | if ( it->getType().compare( "SMTP" ) == 0 ) { | 531 | if ( it->getType().compare( "SMTP" ) == 0 ) { |
453 | smtp = static_cast<SMTPaccount *>(it); | 532 | smtp = static_cast<SMTPaccount *>(it); |
454 | if ( smtp->getMail().compare( from ) == 0 ) { | 533 | if ( smtp->getMail().compare( from ) == 0 ) { |
455 | qDebug( "SMTPaccount found for" ); | 534 | qDebug( "SMTPaccount found for" ); |
456 | qDebug( from ); | 535 | qDebug( from ); |
457 | return smtp; | 536 | return smtp; |
458 | } | 537 | } |
459 | } | 538 | } |
460 | } | 539 | } |
461 | 540 | ||
462 | return NULL; | 541 | return NULL; |
463 | } | 542 | } |
464 | 543 | ||
465 | QString MailWrapper::getTmpFile() { | 544 | QString MailWrapper::getTmpFile() { |
466 | int num = 0; | 545 | int num = 0; |
467 | QString unique; | 546 | QString unique; |
468 | 547 | ||
469 | QDir dir( "/tmp" ); | 548 | QDir dir( "/tmp" ); |
470 | QStringList::Iterator it; | 549 | QStringList::Iterator it; |
471 | 550 | ||
472 | QStringList list = dir.entryList( "opiemail-tmp-*" ); | 551 | QStringList list = dir.entryList( "opiemail-tmp-*" ); |
473 | do { | 552 | do { |
474 | unique.setNum( num++ ); | 553 | unique.setNum( num++ ); |
475 | } while ( list.contains( "opiemail-tmp-" + unique ) > 0 ); | 554 | } while ( list.contains( "opiemail-tmp-" + unique ) > 0 ); |
476 | 555 | ||
477 | return "/tmp/opiemail-tmp-" + unique; | 556 | return "/tmp/opiemail-tmp-" + unique; |
478 | } | 557 | } |
479 | 558 | ||
480 | void MailWrapper::writeToFile( QString file, mailmime *mail ) | 559 | void MailWrapper::writeToFile( QString file, mailmime *mail ) |
481 | { | 560 | { |
482 | FILE *f; | 561 | FILE *f; |
483 | int err, col = 0; | 562 | int err, col = 0; |
484 | 563 | ||
485 | f = fopen( file.latin1(), "w" ); | 564 | f = fopen( file.latin1(), "w" ); |
486 | if ( f == NULL ) { | 565 | if ( f == NULL ) { |
487 | qDebug( "writeToFile: error opening file" ); | 566 | qDebug( "writeToFile: error opening file" ); |
488 | return; | 567 | return; |
489 | } | 568 | } |
490 | 569 | ||
491 | err = mailmime_write( f, &col, mail ); | 570 | err = mailmime_write( f, &col, mail ); |
492 | if ( err != MAILIMF_NO_ERROR ) { | 571 | if ( err != MAILIMF_NO_ERROR ) { |
493 | fclose( f ); | 572 | fclose( f ); |
494 | qDebug( "writeToFile: error writing mailmime" ); | 573 | qDebug( "writeToFile: error writing mailmime" ); |
495 | return; | 574 | return; |
496 | } | 575 | } |
497 | 576 | ||
498 | fclose( f ); | 577 | fclose( f ); |
499 | } | 578 | } |
500 | 579 | ||
501 | void MailWrapper::readFromFile( QString file, char **data, size_t *size ) | 580 | void MailWrapper::readFromFile( QString file, char **data, size_t *size ) |
502 | { | 581 | { |
503 | char *buf; | 582 | char *buf; |
504 | struct stat st; | 583 | struct stat st; |
505 | int fd, count = 0, total = 0; | 584 | int fd, count = 0, total = 0; |
506 | 585 | ||
507 | fd = open( file.latin1(), O_RDONLY, 0 ); | 586 | fd = open( file.latin1(), O_RDONLY, 0 ); |
508 | if ( fd == -1 ) return; | 587 | if ( fd == -1 ) return; |
509 | 588 | ||
510 | if ( fstat( fd, &st ) != 0 ) goto err_close; | 589 | if ( fstat( fd, &st ) != 0 ) goto err_close; |
511 | if ( !st.st_size ) goto err_close; | 590 | if ( !st.st_size ) goto err_close; |
512 | 591 | ||
513 | buf = (char *) malloc( st.st_size ); | 592 | buf = (char *) malloc( st.st_size ); |
514 | if ( !buf ) goto err_close; | 593 | if ( !buf ) goto err_close; |
515 | 594 | ||
516 | while ( ( total < st.st_size ) && ( count >= 0 ) ) { | 595 | while ( ( total < st.st_size ) && ( count >= 0 ) ) { |
517 | count = read( fd, buf + total, st.st_size - total ); | 596 | count = read( fd, buf + total, st.st_size - total ); |
518 | total += count; | 597 | total += count; |
519 | } | 598 | } |
520 | if ( count < 0 ) goto err_free; | 599 | if ( count < 0 ) goto err_free; |
521 | 600 | ||
522 | *data = buf; | 601 | *data = buf; |
523 | *size = st.st_size; | 602 | *size = st.st_size; |
524 | 603 | ||
525 | close( fd ); | 604 | close( fd ); |
526 | 605 | ||
527 | return; // Success :) | 606 | return; // Success :) |
528 | 607 | ||
529 | err_free: | 608 | err_free: |
530 | free( buf ); | 609 | free( buf ); |
531 | err_close: | 610 | err_close: |
532 | close( fd ); | 611 | close( fd ); |
533 | } | 612 | } |
534 | 613 | ||
535 | void progress( size_t current, size_t maximum ) | 614 | void progress( size_t current, size_t maximum ) |
536 | { | 615 | { |
537 | qDebug( "Current: %i of %i", current, maximum ); | 616 | qDebug( "Current: %i of %i", current, maximum ); |
538 | } | 617 | } |
539 | 618 | ||
540 | void MailWrapper::smtpSend( mailmime *mail ) | 619 | void MailWrapper::smtpSend( mailmime *mail ) |
541 | { | 620 | { |
542 | mailsmtp *session; | 621 | mailsmtp *session; |
543 | clist *rcpts; | 622 | clist *rcpts; |
544 | char *from, *data, *server, *user = NULL, *pass = NULL; | 623 | char *from, *data, *server, *user = NULL, *pass = NULL; |
545 | size_t size; | 624 | size_t size; |
546 | int err; | 625 | int err; |
547 | bool ssl; | 626 | bool ssl; |
548 | uint16_t port; | 627 | uint16_t port; |
549 | 628 | ||
550 | 629 | ||
551 | from = getFrom( mail ); | 630 | from = getFrom( mail ); |
552 | SMTPaccount *smtp = getAccount( from ); | 631 | SMTPaccount *smtp = getAccount( from ); |
553 | if ( smtp == NULL ) { | 632 | if ( smtp == NULL ) { |
554 | free(from); | 633 | free(from); |
555 | return; | 634 | return; |
556 | } | 635 | } |
557 | server = strdup( smtp->getServer().latin1() ); | 636 | server = strdup( smtp->getServer().latin1() ); |
558 | ssl = smtp->getSSL(); | 637 | ssl = smtp->getSSL(); |
559 | port = smtp->getPort().toUInt(); | 638 | port = smtp->getPort().toUInt(); |
560 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); | 639 | rcpts = createRcptList( mail->mm_data.mm_message.mm_fields ); |
561 | 640 | ||
562 | QString file = getTmpFile(); | 641 | QString file = getTmpFile(); |
563 | writeToFile( file, mail ); | 642 | writeToFile( file, mail ); |
564 | readFromFile( file, &data, &size ); | 643 | readFromFile( file, &data, &size ); |
565 | QFile f( file ); | 644 | QFile f( file ); |
566 | f.remove(); | 645 | f.remove(); |
567 | 646 | ||
568 | session = mailsmtp_new( 20, &progress ); | 647 | session = mailsmtp_new( 20, &progress ); |
569 | if ( session == NULL ) goto free_mem; | 648 | if ( session == NULL ) goto free_mem; |
570 | 649 | ||
571 | qDebug( "Servername %s at port %i", server, port ); | 650 | qDebug( "Servername %s at port %i", server, port ); |
572 | if ( ssl ) { | 651 | if ( ssl ) { |
573 | qDebug( "SSL session" ); | 652 | qDebug( "SSL session" ); |
574 | err = mailsmtp_ssl_connect( session, server, port ); | 653 | err = mailsmtp_ssl_connect( session, server, port ); |
575 | } else { | 654 | } else { |
576 | qDebug( "No SSL session" ); | 655 | qDebug( "No SSL session" ); |
577 | err = mailsmtp_socket_connect( session, server, port ); | 656 | err = mailsmtp_socket_connect( session, server, port ); |
578 | } | 657 | } |
579 | if ( err != MAILSMTP_NO_ERROR ) goto free_mem_session; | 658 | if ( err != MAILSMTP_NO_ERROR ) goto free_mem_session; |
580 | 659 | ||
581 | err = mailsmtp_init( session ); | 660 | err = mailsmtp_init( session ); |
582 | if ( err != MAILSMTP_NO_ERROR ) goto free_con_session; | 661 | if ( err != MAILSMTP_NO_ERROR ) goto free_con_session; |
583 | 662 | ||
584 | qDebug( "INIT OK" ); | 663 | qDebug( "INIT OK" ); |
585 | 664 | ||
586 | if ( smtp->getLogin() ) { | 665 | if ( smtp->getLogin() ) { |
587 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { | 666 | if ( smtp->getUser().isEmpty() || smtp->getPassword().isEmpty() ) { |
588 | // get'em | 667 | // get'em |
589 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); | 668 | LoginDialog login( smtp->getUser(), smtp->getPassword(), NULL, 0, true ); |
590 | login.show(); | 669 | login.show(); |
591 | if ( QDialog::Accepted == login.exec() ) { | 670 | if ( QDialog::Accepted == login.exec() ) { |
592 | // ok | 671 | // ok |
593 | user = strdup( login.getUser().latin1() ); | 672 | user = strdup( login.getUser().latin1() ); |
594 | pass = strdup( login.getPassword().latin1() ); | 673 | pass = strdup( login.getPassword().latin1() ); |
595 | } else { | 674 | } else { |
596 | goto free_con_session; | 675 | goto free_con_session; |
597 | } | 676 | } |
598 | } else { | 677 | } else { |
599 | user = strdup( smtp->getUser().latin1() ); | 678 | user = strdup( smtp->getUser().latin1() ); |
600 | pass = strdup( smtp->getPassword().latin1() ); | 679 | pass = strdup( smtp->getPassword().latin1() ); |
601 | } | 680 | } |
602 | qDebug( "session->auth: %i", session->auth); | 681 | qDebug( "session->auth: %i", session->auth); |
603 | err = mailsmtp_auth( session, user, pass ); | 682 | err = mailsmtp_auth( session, user, pass ); |
604 | if ( err == MAILSMTP_NO_ERROR ) qDebug("auth ok"); | 683 | if ( err == MAILSMTP_NO_ERROR ) qDebug("auth ok"); |
605 | qDebug( "Done auth!" ); | 684 | qDebug( "Done auth!" ); |
606 | } | 685 | } |
607 | 686 | ||
608 | err = mailsmtp_send( session, from, rcpts, data, size ); | 687 | err = mailsmtp_send( session, from, rcpts, data, size ); |
609 | if ( err != MAILSMTP_NO_ERROR ) goto free_con_session; | 688 | if ( err != MAILSMTP_NO_ERROR ) goto free_con_session; |
610 | 689 | ||
611 | qDebug( "Mail sent." ); | 690 | qDebug( "Mail sent." ); |
612 | 691 | ||
613 | free_con_session: | 692 | free_con_session: |
614 | mailsmtp_quit( session ); | 693 | mailsmtp_quit( session ); |
615 | free_mem_session: | 694 | free_mem_session: |
616 | mailsmtp_free( session ); | 695 | mailsmtp_free( session ); |
617 | free_mem: | 696 | free_mem: |
618 | smtp_address_list_free( rcpts ); | 697 | smtp_address_list_free( rcpts ); |
619 | free( data ); | 698 | free( data ); |
620 | free( server ); | 699 | free( server ); |
621 | if ( smtp->getLogin() ) { | 700 | if ( smtp->getLogin() ) { |
622 | free( user ); | 701 | free( user ); |
623 | free( pass ); | 702 | free( pass ); |
624 | } | 703 | } |
625 | free( from ); | 704 | free( from ); |
626 | } | 705 | } |
627 | 706 | ||
628 | void MailWrapper::sendMail( Mail mail ) | 707 | void MailWrapper::sendMail( Mail mail ) |
629 | { | 708 | { |
630 | mailmime *mimeMail; | 709 | mailmime *mimeMail; |
631 | 710 | ||
632 | mimeMail = createMimeMail( &mail ); | 711 | mimeMail = createMimeMail( &mail ); |
633 | if ( mimeMail == NULL ) { | 712 | if ( mimeMail == NULL ) { |
634 | qDebug( "sendMail: error creating mime mail" ); | 713 | qDebug( "sendMail: error creating mime mail" ); |
635 | } else { | 714 | } else { |
636 | smtpSend( mimeMail ); | 715 | smtpSend( mimeMail ); |
637 | mailmime_free( mimeMail ); | 716 | mailmime_free( mimeMail ); |
638 | } | 717 | } |
639 | } | 718 | } |
640 | 719 | ||
641 | Mail::Mail() | 720 | Mail::Mail() |
642 | :name(""), mail(""), to(""), cc(""), bcc(""), reply(""), subject(""), message("") | 721 | :name(""), mail(""), to(""), cc(""), bcc(""), reply(""), subject(""), message("") |
643 | { | 722 | { |
644 | } | 723 | } |
diff --git a/noncore/net/mail/mailwrapper.h b/noncore/net/mail/mailwrapper.h index d78f8e9..02fe4b7 100644 --- a/noncore/net/mail/mailwrapper.h +++ b/noncore/net/mail/mailwrapper.h | |||
@@ -1,124 +1,126 @@ | |||
1 | #ifndef MAILWRAPPER_H | 1 | #ifndef MAILWRAPPER_H |
2 | #define MAILWRAPPER_H | 2 | #define MAILWRAPPER_H |
3 | 3 | ||
4 | #include <qpe/applnk.h> | 4 | #include <qpe/applnk.h> |
5 | 5 | ||
6 | #include <libetpan/mailmime.h> | 6 | #include <libetpan/mailmime.h> |
7 | #include <libetpan/mailimf.h> | 7 | #include <libetpan/mailimf.h> |
8 | #include <libetpan/mailsmtp.h> | 8 | #include <libetpan/mailsmtp.h> |
9 | #include <libetpan/mailstorage.h> | 9 | #include <libetpan/mailstorage.h> |
10 | #include <libetpan/maildriver.h> | 10 | #include <libetpan/maildriver.h> |
11 | #include <qbitarray.h> | 11 | #include <qbitarray.h> |
12 | #include <qdatetime.h> | 12 | #include <qdatetime.h> |
13 | 13 | ||
14 | #include "settings.h" | 14 | #include "settings.h" |
15 | 15 | ||
16 | class Attachment | 16 | class Attachment |
17 | { | 17 | { |
18 | public: | 18 | public: |
19 | Attachment( DocLnk lnk ); | 19 | Attachment( DocLnk lnk ); |
20 | virtual ~Attachment(){} | 20 | virtual ~Attachment(){} |
21 | const QString getFileName()const{ return doc.file(); } | 21 | const QString getFileName()const{ return doc.file(); } |
22 | const QString getName()const{ return doc.name(); } | 22 | const QString getName()const{ return doc.name(); } |
23 | const QString getMimeType()const{ return doc.type(); } | 23 | const QString getMimeType()const{ return doc.type(); } |
24 | const QPixmap getPixmap()const{ return doc.pixmap(); } | 24 | const QPixmap getPixmap()const{ return doc.pixmap(); } |
25 | const int getSize()const { return size; } | 25 | const int getSize()const { return size; } |
26 | DocLnk getDocLnk() { return doc; } | 26 | DocLnk getDocLnk() { return doc; } |
27 | 27 | ||
28 | protected: | 28 | protected: |
29 | DocLnk doc; | 29 | DocLnk doc; |
30 | int size; | 30 | int size; |
31 | 31 | ||
32 | }; | 32 | }; |
33 | 33 | ||
34 | class Mail | 34 | class Mail |
35 | { | 35 | { |
36 | public: | 36 | public: |
37 | Mail(); | 37 | Mail(); |
38 | /* Possible that this destructor must not be declared virtual | 38 | /* Possible that this destructor must not be declared virtual |
39 | * 'cause it seems that it will never have some child classes. | 39 | * 'cause it seems that it will never have some child classes. |
40 | * in this case this object will not get a virtual table -> memory and | 40 | * in this case this object will not get a virtual table -> memory and |
41 | * speed will be a little bit better? | 41 | * speed will be a little bit better? |
42 | */ | 42 | */ |
43 | virtual ~Mail(){} | 43 | virtual ~Mail(){} |
44 | void addAttachment( Attachment *att ) { attList.append( att ); } | 44 | void addAttachment( Attachment *att ) { attList.append( att ); } |
45 | const QList<Attachment>& getAttachments()const { return attList; } | 45 | const QList<Attachment>& getAttachments()const { return attList; } |
46 | void removeAttachment( Attachment *att ) { attList.remove( att ); } | 46 | void removeAttachment( Attachment *att ) { attList.remove( att ); } |
47 | const QString&getName()const { return name; } | 47 | const QString&getName()const { return name; } |
48 | void setName( QString s ) { name = s; } | 48 | void setName( QString s ) { name = s; } |
49 | const QString&getMail()const{ return mail; } | 49 | const QString&getMail()const{ return mail; } |
50 | void setMail( const QString&s ) { mail = s; } | 50 | void setMail( const QString&s ) { mail = s; } |
51 | const QString&getTo()const{ return to; } | 51 | const QString&getTo()const{ return to; } |
52 | void setTo( const QString&s ) { to = s; } | 52 | void setTo( const QString&s ) { to = s; } |
53 | const QString&getCC()const{ return cc; } | 53 | const QString&getCC()const{ return cc; } |
54 | void setCC( const QString&s ) { cc = s; } | 54 | void setCC( const QString&s ) { cc = s; } |
55 | const QString&getBCC()const { return bcc; } | 55 | const QString&getBCC()const { return bcc; } |
56 | void setBCC( const QString&s ) { bcc = s; } | 56 | void setBCC( const QString&s ) { bcc = s; } |
57 | const QString&getMessage()const { return message; } | 57 | const QString&getMessage()const { return message; } |
58 | void setMessage( const QString&s ) { message = s; } | 58 | void setMessage( const QString&s ) { message = s; } |
59 | const QString&getSubject()const { return subject; } | 59 | const QString&getSubject()const { return subject; } |
60 | void setSubject( const QString&s ) { subject = s; } | 60 | void setSubject( const QString&s ) { subject = s; } |
61 | const QString&getReply()const{ return reply; } | 61 | const QString&getReply()const{ return reply; } |
62 | void setReply( const QString&a ) { reply = a; } | 62 | void setReply( const QString&a ) { reply = a; } |
63 | 63 | ||
64 | private: | 64 | private: |
65 | QList<Attachment> attList; | 65 | QList<Attachment> attList; |
66 | QString name, mail, to, cc, bcc, reply, subject, message; | 66 | QString name, mail, to, cc, bcc, reply, subject, message; |
67 | }; | 67 | }; |
68 | 68 | ||
69 | class Folder : public QObject | 69 | class Folder : public QObject |
70 | { | 70 | { |
71 | Q_OBJECT | 71 | Q_OBJECT |
72 | 72 | ||
73 | public: | 73 | public: |
74 | Folder( const QString&init_name,const QString&sep ); | 74 | Folder( const QString&init_name,const QString&sep ); |
75 | const QString&getDisplayName()const { return nameDisplay; } | 75 | const QString&getDisplayName()const { return nameDisplay; } |
76 | const QString&getName()const { return name; } | 76 | const QString&getName()const { return name; } |
77 | virtual bool may_select()const{return true;}; | 77 | virtual bool may_select()const{return true;}; |
78 | const QString&Separator()const; | 78 | const QString&Separator()const; |
79 | 79 | ||
80 | protected: | 80 | protected: |
81 | QString nameDisplay, name, separator; | 81 | QString nameDisplay, name, separator; |
82 | 82 | ||
83 | }; | 83 | }; |
84 | 84 | ||
85 | class IMAPFolder : public Folder | 85 | class IMAPFolder : public Folder |
86 | { | 86 | { |
87 | public: | 87 | public: |
88 | IMAPFolder(const QString&name, const QString&sep, bool select=true,const QString&prefix="" ); | 88 | IMAPFolder(const QString&name, const QString&sep, bool select=true,const QString&prefix="" ); |
89 | virtual bool may_select()const{return m_MaySelect;} | 89 | virtual bool may_select()const{return m_MaySelect;} |
90 | private: | 90 | private: |
91 | static QString decodeFolderName( const QString &name ); | ||
91 | bool m_MaySelect; | 92 | bool m_MaySelect; |
93 | |||
92 | }; | 94 | }; |
93 | 95 | ||
94 | class MailWrapper : public QObject | 96 | class MailWrapper : public QObject |
95 | { | 97 | { |
96 | Q_OBJECT | 98 | Q_OBJECT |
97 | 99 | ||
98 | public: | 100 | public: |
99 | MailWrapper( Settings *s ); | 101 | MailWrapper( Settings *s ); |
100 | void sendMail( Mail mail ); | 102 | void sendMail( Mail mail ); |
101 | 103 | ||
102 | private: | 104 | private: |
103 | mailimf_mailbox *newMailbox(const QString&name,const QString&mail ); | 105 | mailimf_mailbox *newMailbox(const QString&name,const QString&mail ); |
104 | mailimf_address_list *parseAddresses(const QString&addr ); | 106 | mailimf_address_list *parseAddresses(const QString&addr ); |
105 | mailimf_fields *createImfFields( Mail *mail ); | 107 | mailimf_fields *createImfFields( Mail *mail ); |
106 | mailmime *buildTxtPart( QString str ); | 108 | mailmime *buildTxtPart( QString str ); |
107 | mailmime *buildFilePart( QString filename, QString mimetype ); | 109 | mailmime *buildFilePart( QString filename, QString mimetype ); |
108 | void addFileParts( mailmime *message, QList<Attachment> files ); | 110 | void addFileParts( mailmime *message, QList<Attachment> files ); |
109 | mailmime *createMimeMail( Mail *mail ); | 111 | mailmime *createMimeMail( Mail *mail ); |
110 | void smtpSend( mailmime *mail ); | 112 | void smtpSend( mailmime *mail ); |
111 | mailimf_field *getField( mailimf_fields *fields, int type ); | 113 | mailimf_field *getField( mailimf_fields *fields, int type ); |
112 | clist *createRcptList( mailimf_fields *fields ); | 114 | clist *createRcptList( mailimf_fields *fields ); |
113 | char *getFrom( mailmime *mail ); | 115 | char *getFrom( mailmime *mail ); |
114 | SMTPaccount *getAccount( QString from ); | 116 | SMTPaccount *getAccount( QString from ); |
115 | void writeToFile( QString file, mailmime *mail ); | 117 | void writeToFile( QString file, mailmime *mail ); |
116 | void readFromFile( QString file, char **data, size_t *size ); | 118 | void readFromFile( QString file, char **data, size_t *size ); |
117 | static QString mailsmtpError( int err ); | 119 | static QString mailsmtpError( int err ); |
118 | static QString getTmpFile(); | 120 | static QString getTmpFile(); |
119 | 121 | ||
120 | Settings *settings; | 122 | Settings *settings; |
121 | 123 | ||
122 | }; | 124 | }; |
123 | 125 | ||
124 | #endif | 126 | #endif |