diff options
author | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-12-19 20:09:30 +0100 |
---|---|---|
committer | Christian Mollekopf <chrigi_1@fastmail.fm> | 2016-12-19 20:09:30 +0100 |
commit | 890b12ba3b2889dd0d5331534c8c1f22abf4a80b (patch) | |
tree | c2af162b884fe8bcd7a6addf55d0473524173046 /examples | |
parent | 71d27b4b016240e4a5ae28c59991317dec2fdcd4 (diff) | |
download | sink-890b12ba3b2889dd0d5331534c8c1f22abf4a80b.tar.gz sink-890b12ba3b2889dd0d5331534c8c1f22abf4a80b.zip |
Get mailtransport to work with kolab container.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/mailtransportresource/CMakeLists.txt | 8 | ||||
-rw-r--r-- | examples/mailtransportresource/mailtransport.cpp | 39 | ||||
-rw-r--r-- | examples/mailtransportresource/mailtransport.h | 14 | ||||
-rw-r--r-- | examples/mailtransportresource/mailtransportresource.cpp | 7 | ||||
-rw-r--r-- | examples/mailtransportresource/smtptest.cpp | 20 |
5 files changed, 73 insertions, 15 deletions
diff --git a/examples/mailtransportresource/CMakeLists.txt b/examples/mailtransportresource/CMakeLists.txt index d16e779..03b89db 100644 --- a/examples/mailtransportresource/CMakeLists.txt +++ b/examples/mailtransportresource/CMakeLists.txt | |||
@@ -12,7 +12,11 @@ include_directories(${CURL_INCLUDE_DIRS}) | |||
12 | add_library(${PROJECT_NAME} SHARED mailtransportresource.cpp mailtransport.cpp) | 12 | add_library(${PROJECT_NAME} SHARED mailtransportresource.cpp mailtransport.cpp) |
13 | qt5_use_modules(${PROJECT_NAME} Core Network) | 13 | qt5_use_modules(${PROJECT_NAME} Core Network) |
14 | target_link_libraries(${PROJECT_NAME} sink KF5::Mime ${CURL_LIBRARIES}) | 14 | target_link_libraries(${PROJECT_NAME} sink KF5::Mime ${CURL_LIBRARIES}) |
15 | install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${SINK_RESOURCE_PLUGINS_PATH}) | ||
15 | 16 | ||
16 | add_subdirectory(tests) | 17 | add_executable(sink_smtp_test smtptest.cpp mailtransport.cpp) |
18 | qt5_use_modules(sink_smtp_test Core Network) | ||
19 | target_link_libraries(sink_smtp_test sink KF5::Mime ${CURL_LIBRARIES}) | ||
20 | install(TARGETS sink_smtp_test ${KDE_INSTALL_TARGETS_DEFAULT_ARGS}) | ||
17 | 21 | ||
18 | install(TARGETS ${PROJECT_NAME} LIBRARY DESTINATION ${SINK_RESOURCE_PLUGINS_PATH}) | 22 | add_subdirectory(tests) |
diff --git a/examples/mailtransportresource/mailtransport.cpp b/examples/mailtransportresource/mailtransport.cpp index 4f7e59b..3d56af9 100644 --- a/examples/mailtransportresource/mailtransport.cpp +++ b/examples/mailtransportresource/mailtransport.cpp | |||
@@ -21,6 +21,9 @@ | |||
21 | #include <QByteArray> | 21 | #include <QByteArray> |
22 | #include <QList> | 22 | #include <QList> |
23 | #include <QDebug> | 23 | #include <QDebug> |
24 | #include <common/log.h> | ||
25 | |||
26 | SINK_DEBUG_AREA("mailtransport") | ||
24 | 27 | ||
25 | extern "C" { | 28 | extern "C" { |
26 | 29 | ||
@@ -57,12 +60,18 @@ static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp) | |||
57 | return 0; | 60 | return 0; |
58 | } | 61 | } |
59 | 62 | ||
60 | 63 | static int progress_callback(void *clientp, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow) | |
61 | bool sendMessageCurl(const char *to[], int numTos, const char *cc[], int numCcs, const char *msg, bool useTls, const char* from, const char *username, const char *password, const char *server, bool verifyPeer) | ||
62 | { | 64 | { |
63 | //For ssl use "smtps://mainserver.example.net | 65 | if (ultotal > 0) { |
64 | const char* cacert = 0; // = "/path/to/certificate.pem"; | 66 | auto progress = ulnow * 100.0/ ultotal; |
67 | fprintf(stdout, "Upload progress: %d \n", int(progress)); | ||
68 | } | ||
69 | return 0; | ||
70 | } | ||
65 | 71 | ||
72 | |||
73 | bool sendMessageCurl(const char *to[], int numTos, const char *cc[], int numCcs, const char *msg, bool useTls, const char* from, const char *username, const char *password, const char *server, bool verifyPeer, const QByteArray &cacert) | ||
74 | { | ||
66 | CURL *curl; | 75 | CURL *curl; |
67 | CURLcode res = CURLE_OK; | 76 | CURLcode res = CURLE_OK; |
68 | struct curl_slist *recipients = NULL; | 77 | struct curl_slist *recipients = NULL; |
@@ -86,8 +95,8 @@ bool sendMessageCurl(const char *to[], int numTos, const char *cc[], int numCcs, | |||
86 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); | 95 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); |
87 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); | 96 | curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); |
88 | } | 97 | } |
89 | if (cacert) { | 98 | if (!cacert.isEmpty()) { |
90 | curl_easy_setopt(curl, CURLOPT_CAINFO, cacert); | 99 | curl_easy_setopt(curl, CURLOPT_CAINFO, cacert.constData()); |
91 | } | 100 | } |
92 | 101 | ||
93 | if (from) { | 102 | if (from) { |
@@ -114,6 +123,13 @@ bool sendMessageCurl(const char *to[], int numTos, const char *cc[], int numCcs, | |||
114 | */ | 123 | */ |
115 | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); | 124 | curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L); |
116 | 125 | ||
126 | // curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1L); | ||
127 | //Connection timeout of 10s | ||
128 | curl_easy_setopt(curl, CURLOPT_CONNECTTIMEOUT, 10L); | ||
129 | |||
130 | curl_easy_setopt(curl, CURLOPT_NOPROGRESS, 0); | ||
131 | curl_easy_setopt(curl, CURLOPT_XFERINFOFUNCTION, progress_callback); | ||
132 | |||
117 | res = curl_easy_perform(curl); | 133 | res = curl_easy_perform(curl); |
118 | if(res != CURLE_OK) { | 134 | if(res != CURLE_OK) { |
119 | fprintf(stderr, "curl_easy_perform() failed: %s\n", | 135 | fprintf(stderr, "curl_easy_perform() failed: %s\n", |
@@ -128,10 +144,11 @@ bool sendMessageCurl(const char *to[], int numTos, const char *cc[], int numCcs, | |||
128 | 144 | ||
129 | }; | 145 | }; |
130 | 146 | ||
131 | bool MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteArray &server, const QByteArray &username, const QByteArray &password, const QByteArray &cacert) | 147 | bool MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteArray &server, const QByteArray &username, const QByteArray &password, const QByteArray &cacert, MailTransport::Options options) |
132 | { | 148 | { |
133 | QByteArray msg = message->encodedContent(); | 149 | QByteArray msg = message->encodedContent(); |
134 | qDebug() << "Sending message " << msg; | 150 | SinkLog() << "Sending message " << server << username << password << cacert; |
151 | SinkTrace() << "Sending message " << msg; | ||
135 | 152 | ||
136 | QByteArray from(message->from(true)->mailboxes().isEmpty() ? QByteArray() : message->from(true)->mailboxes().first().address()); | 153 | QByteArray from(message->from(true)->mailboxes().isEmpty() ? QByteArray() : message->from(true)->mailboxes().first().address()); |
137 | QList<QByteArray> toList; | 154 | QList<QByteArray> toList; |
@@ -142,8 +159,8 @@ bool MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteA | |||
142 | for (const auto &mb : message->cc(true)->mailboxes()) { | 159 | for (const auto &mb : message->cc(true)->mailboxes()) { |
143 | ccList << mb.address(); | 160 | ccList << mb.address(); |
144 | } | 161 | } |
145 | bool useTls = true; | 162 | bool verifyPeer = options & VerifyPeers; |
146 | bool verifyPeer = false; | 163 | bool useTls = options & UseTls; |
147 | 164 | ||
148 | const int numTos = toList.size(); | 165 | const int numTos = toList.size(); |
149 | const char* to[numTos]; | 166 | const char* to[numTos]; |
@@ -157,5 +174,5 @@ bool MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteA | |||
157 | cc[i] = ccList.at(i); | 174 | cc[i] = ccList.at(i); |
158 | } | 175 | } |
159 | 176 | ||
160 | return sendMessageCurl(to, numTos, cc, numCcs, msg, useTls, from.isEmpty() ? nullptr : from, username, password, server, verifyPeer); | 177 | return sendMessageCurl(to, numTos, cc, numCcs, msg, useTls, from.isEmpty() ? nullptr : from, username, password, server, verifyPeer, cacert); |
161 | } | 178 | } |
diff --git a/examples/mailtransportresource/mailtransport.h b/examples/mailtransportresource/mailtransport.h index 6cd3452..3ef4a6d 100644 --- a/examples/mailtransportresource/mailtransport.h +++ b/examples/mailtransportresource/mailtransport.h | |||
@@ -20,9 +20,21 @@ | |||
20 | #pragma once | 20 | #pragma once |
21 | 21 | ||
22 | #include <QByteArray> | 22 | #include <QByteArray> |
23 | #include <QFlag> | ||
23 | #include <KMime/Message> | 24 | #include <KMime/Message> |
24 | 25 | ||
25 | namespace MailTransport | 26 | namespace MailTransport |
26 | { | 27 | { |
27 | bool sendMessage(const KMime::Message::Ptr &message, const QByteArray &server, const QByteArray &username, const QByteArray &password, const QByteArray &cacert); | 28 | enum Option { |
29 | UseTls, | ||
30 | VerifyPeers | ||
31 | }; | ||
32 | Q_DECLARE_FLAGS(Options, Option); | ||
33 | |||
34 | /* | ||
35 | * For ssl use "smtps://mainserver.example.net | ||
36 | * @param cacert: "/path/to/certificate.pem"; | ||
37 | */ | ||
38 | bool sendMessage(const KMime::Message::Ptr &message, const QByteArray &server, const QByteArray &username, const QByteArray &password, const QByteArray &cacert, Options flags); | ||
28 | }; | 39 | }; |
40 | Q_DECLARE_OPERATORS_FOR_FLAGS(MailTransport::Options) | ||
diff --git a/examples/mailtransportresource/mailtransportresource.cpp b/examples/mailtransportresource/mailtransportresource.cpp index cf657f3..e475139 100644 --- a/examples/mailtransportresource/mailtransportresource.cpp +++ b/examples/mailtransportresource/mailtransportresource.cpp | |||
@@ -70,7 +70,11 @@ public: | |||
70 | f.write("foo"); | 70 | f.write("foo"); |
71 | f.close(); | 71 | f.close(); |
72 | } else { | 72 | } else { |
73 | if (MailTransport::sendMessage(msg, settings.server.toUtf8(), settings.username.toUtf8(), settings.password.toUtf8(), settings.cacert.toUtf8())) { | 73 | MailTransport::Options options; |
74 | if (settings.server.contains("smtps")) { | ||
75 | options &= MailTransport::UseTls; | ||
76 | } | ||
77 | if (MailTransport::sendMessage(msg, settings.server.toUtf8(), settings.username.toUtf8(), settings.password.toUtf8(), settings.cacert.toUtf8(), options)) { | ||
74 | SinkLog() << "Sent message successfully"; | 78 | SinkLog() << "Sent message successfully"; |
75 | } else { | 79 | } else { |
76 | SinkLog() << "Failed to send message"; | 80 | SinkLog() << "Failed to send message"; |
@@ -97,6 +101,7 @@ public: | |||
97 | for (const auto &m : toSend) { | 101 | for (const auto &m : toSend) { |
98 | job = job.then(send(m, mSettings)) | 102 | job = job.then(send(m, mSettings)) |
99 | .then<void>([this, m] { | 103 | .then<void>([this, m] { |
104 | SinkLog() << "Sent mail, and triggering move to sent mail folder: " << m.identifier(); | ||
100 | auto modifiedMail = ApplicationDomain::Mail(mResourceInstanceIdentifier, m.identifier(), m.revision(), QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); | 105 | auto modifiedMail = ApplicationDomain::Mail(mResourceInstanceIdentifier, m.identifier(), m.revision(), QSharedPointer<Sink::ApplicationDomain::MemoryBufferAdaptor>::create()); |
101 | modifiedMail.setSent(true); | 106 | modifiedMail.setSent(true); |
102 | 107 | ||
diff --git a/examples/mailtransportresource/smtptest.cpp b/examples/mailtransportresource/smtptest.cpp new file mode 100644 index 0000000..079150f --- /dev/null +++ b/examples/mailtransportresource/smtptest.cpp | |||
@@ -0,0 +1,20 @@ | |||
1 | |||
2 | #include <QCoreApplication> | ||
3 | #include <KMime/Message> | ||
4 | #include "mailtransport.h" | ||
5 | |||
6 | int main(int argc, char *argv[]) | ||
7 | { | ||
8 | QCoreApplication app(argc, argv); | ||
9 | app.setApplicationName(argv[0]); | ||
10 | |||
11 | auto args = app.arguments(); | ||
12 | |||
13 | auto msg = KMime::Message::Ptr::create(); | ||
14 | msg->to(true)->from7BitString("doe2@example.org"); | ||
15 | msg->from(true)->from7BitString("doe@example.org"); | ||
16 | msg->subject(true)->from7BitString("Subject"); | ||
17 | msg->assemble(); | ||
18 | |||
19 | MailTransport::sendMessage(msg, "smtp://kolab:25", "doe@example.org", "Welcome2KolabSystems", QByteArray{}, MailTransport::Options{}); | ||
20 | } | ||