summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2018-01-25 14:43:07 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2018-01-25 14:43:07 +0100
commit31dbe72afb5e04cd86d4f107e7257fb60aaa087b (patch)
tree9df6ad125f80d74fb720a1994415c0c279c5f902
parent102bb38ed89da2295a01d707db954e47a6cb8d7d (diff)
downloadsink-31dbe72afb5e04cd86d4f107e7257fb60aaa087b.tar.gz
sink-31dbe72afb5e04cd86d4f107e7257fb60aaa087b.zip
Do the logging in the resource code.
-rw-r--r--examples/mailtransportresource/mailtransport.cpp18
-rw-r--r--examples/mailtransportresource/mailtransport.h7
-rw-r--r--examples/mailtransportresource/mailtransportresource.cpp8
3 files changed, 17 insertions, 16 deletions
diff --git a/examples/mailtransportresource/mailtransport.cpp b/examples/mailtransportresource/mailtransport.cpp
index afe0257..65299a2 100644
--- a/examples/mailtransportresource/mailtransport.cpp
+++ b/examples/mailtransportresource/mailtransport.cpp
@@ -21,7 +21,6 @@
21#include <QByteArray> 21#include <QByteArray>
22#include <QList> 22#include <QList>
23#include <QDebug> 23#include <QDebug>
24#include <common/log.h>
25 24
26extern "C" { 25extern "C" {
27 26
@@ -142,13 +141,14 @@ bool sendMessageCurl(const char *to[], int numTos, const char *cc[], int numCcs,
142 141
143 res = curl_easy_perform(curl); 142 res = curl_easy_perform(curl);
144 if(res != CURLE_OK) { 143 if(res != CURLE_OK) {
144 errorMessage += "Error code: " + QByteArray::number(res) + ", ";
145 errorMessage += curl_easy_strerror(res); 145 errorMessage += curl_easy_strerror(res);
146 errorMessage += "; "; 146 errorMessage += "; ";
147 } 147 }
148 long http_code = 0; 148 long http_code = 0;
149 curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code); 149 curl_easy_getinfo (curl, CURLINFO_RESPONSE_CODE, &http_code);
150 if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK) { 150 if (http_code == 200 && res != CURLE_ABORTED_BY_CALLBACK) {
151 //Succeeded 151 //Succeeded
152 } else { 152 } else {
153 errorMessage += errorBuffer; 153 errorMessage += errorBuffer;
154 } 154 }
@@ -161,10 +161,8 @@ bool sendMessageCurl(const char *to[], int numTos, const char *cc[], int numCcs,
161 161
162}; 162};
163 163
164bool MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteArray &server, const QByteArray &username, const QByteArray &password, const QByteArray &cacert, MailTransport::Options options) 164MailTransport::SendResult MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteArray &server, const QByteArray &username, const QByteArray &password, const QByteArray &cacert, MailTransport::Options options)
165{ 165{
166 QByteArray msg = message->encodedContent();
167
168 QByteArray from(message->from(true)->mailboxes().isEmpty() ? QByteArray() : message->from(true)->mailboxes().first().address()); 166 QByteArray from(message->from(true)->mailboxes().isEmpty() ? QByteArray() : message->from(true)->mailboxes().first().address());
169 QList<QByteArray> toList; 167 QList<QByteArray> toList;
170 for (const auto &mb : message->to(true)->mailboxes()) { 168 for (const auto &mb : message->to(true)->mailboxes()) {
@@ -177,9 +175,6 @@ bool MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteA
177 const bool verifyPeer = options.testFlag(VerifyPeers); 175 const bool verifyPeer = options.testFlag(VerifyPeers);
178 const bool useTls = options.testFlag(UseTls); 176 const bool useTls = options.testFlag(UseTls);
179 177
180 SinkLog() << "Sending message " << server << username << password << "CaCert: " << cacert << "Use tls: " << useTls << " Verify peer: " << verifyPeer;
181 SinkTrace() << "Sending message " << msg;
182
183 const int numTos = toList.size(); 178 const int numTos = toList.size();
184 const char* to[numTos]; 179 const char* to[numTos];
185 for (int i = 0; i < numTos; i++) { 180 for (int i = 0; i < numTos; i++) {
@@ -196,9 +191,6 @@ bool MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteA
196 serverAddress.replace("smtps://", "smtp://"); 191 serverAddress.replace("smtps://", "smtp://");
197 192
198 QByteArray errorMessage; 193 QByteArray errorMessage;
199 auto ret = sendMessageCurl(to, numTos, cc, numCcs, msg, useTls, from.isEmpty() ? nullptr : from, username, password, serverAddress, verifyPeer, cacert, errorMessage); 194 auto ret = sendMessageCurl(to, numTos, cc, numCcs, message->encodedContent(), useTls, from.isEmpty() ? nullptr : from, username, password, serverAddress, verifyPeer, cacert, errorMessage);
200 if (!ret) { 195 return {ret, errorMessage};
201 SinkWarning() << "Failed to send message: " << errorMessage;
202 }
203 return ret;
204} 196}
diff --git a/examples/mailtransportresource/mailtransport.h b/examples/mailtransportresource/mailtransport.h
index 662fdc9..0fa5a66 100644
--- a/examples/mailtransportresource/mailtransport.h
+++ b/examples/mailtransportresource/mailtransport.h
@@ -31,10 +31,15 @@ namespace MailTransport
31 }; 31 };
32 Q_DECLARE_FLAGS(Options, Option); 32 Q_DECLARE_FLAGS(Options, Option);
33 33
34 struct SendResult {
35 bool error;
36 QString errorMessage;
37 };
38
34 /* 39 /*
35 * For ssl use "smtps://mainserver.example.net 40 * For ssl use "smtps://mainserver.example.net
36 * @param cacert: "/path/to/certificate.pem"; 41 * @param cacert: "/path/to/certificate.pem";
37 */ 42 */
38 bool sendMessage(const KMime::Message::Ptr &message, const QByteArray &server, const QByteArray &username, const QByteArray &password, const QByteArray &cacert, Options flags); 43 SendResult sendMessage(const KMime::Message::Ptr &message, const QByteArray &server, const QByteArray &username, const QByteArray &password, const QByteArray &cacert, Options flags);
39}; 44};
40Q_DECLARE_OPERATORS_FOR_FLAGS(MailTransport::Options) 45Q_DECLARE_OPERATORS_FOR_FLAGS(MailTransport::Options)
diff --git a/examples/mailtransportresource/mailtransportresource.cpp b/examples/mailtransportresource/mailtransportresource.cpp
index 1d90adf..516ff8e 100644
--- a/examples/mailtransportresource/mailtransportresource.cpp
+++ b/examples/mailtransportresource/mailtransportresource.cpp
@@ -129,8 +129,12 @@ public:
129 if (settings.server.contains("smtps")) { 129 if (settings.server.contains("smtps")) {
130 options |= MailTransport::UseTls; 130 options |= MailTransport::UseTls;
131 } 131 }
132 if (!MailTransport::sendMessage(msg, settings.server.toUtf8(), settings.username.toUtf8(), secret().toUtf8(), settings.cacert.toUtf8(), options)) { 132
133 SinkWarning() << "Failed to send message: " << mail; 133 SinkLog() << "Sending message " << settings.server << settings.username << "CaCert: " << settings.cacert;
134 SinkTrace() << "Sending message " << msg;
135 auto result = MailTransport::sendMessage(msg, settings.server.toUtf8(), settings.username.toUtf8(), secret().toUtf8(), settings.cacert.toUtf8(), options);
136 if (!result.error) {
137 SinkWarning() << "Failed to send message: " << mail << "\n" << result.errorMessage;
134 emitNotification(Notification::Warning, ApplicationDomain::SyncError, "Failed to send message.", {}, {mail.identifier()}); 138 emitNotification(Notification::Warning, ApplicationDomain::SyncError, "Failed to send message.", {}, {mail.identifier()});
135 emitNotification(Notification::Warning, ApplicationDomain::TransmissionError, "Failed to send message.", {}, {mail.identifier()}); 139 emitNotification(Notification::Warning, ApplicationDomain::TransmissionError, "Failed to send message.", {}, {mail.identifier()});
136 return KAsync::error("Failed to send the message."); 140 return KAsync::error("Failed to send the message.");