summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorChristian Mollekopf <chrigi_1@fastmail.fm>2016-02-24 12:38:49 +0100
committerChristian Mollekopf <chrigi_1@fastmail.fm>2016-02-24 12:38:49 +0100
commite569a96e2c68954e9eacbe84ecc7963e462d1f0f (patch)
tree06e0b4ccd45642c2b6d782ef0d54bdcad7313807
parent4b094beb151373abb4a24d473c6d69a11674c5ba (diff)
downloadkube-e569a96e2c68954e9eacbe84ecc7963e462d1f0f.tar.gz
kube-e569a96e2c68954e9eacbe84ecc7963e462d1f0f.zip
Basic but working mailtransport.
-rw-r--r--framework/mail/CMakeLists.txt8
-rw-r--r--framework/mail/actions/mailactions.cpp48
-rw-r--r--framework/mail/mailtransport.cpp160
-rw-r--r--framework/mail/mailtransport.h28
4 files changed, 243 insertions, 1 deletions
diff --git a/framework/mail/CMakeLists.txt b/framework/mail/CMakeLists.txt
index 13788cfa..94d15f4c 100644
--- a/framework/mail/CMakeLists.txt
+++ b/framework/mail/CMakeLists.txt
@@ -6,19 +6,25 @@ set(mailplugin_SRCS
6 folderlistmodel.cpp 6 folderlistmodel.cpp
7 folderlistcontroller.cpp 7 folderlistcontroller.cpp
8 actions/sinkactions.cpp 8 actions/sinkactions.cpp
9 actions/mailactions.cpp
9 objecttreesource.cpp 10 objecttreesource.cpp
10 stringhtmlwriter.cpp 11 stringhtmlwriter.cpp
11 csshelper.cpp 12 csshelper.cpp
12 composer.cpp 13 composer.cpp
13 messageparser.cpp 14 messageparser.cpp
15 mailtransport.cpp
14) 16)
15add_definitions(-DMAIL_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data") 17add_definitions(-DMAIL_DATA_DIR="${CMAKE_CURRENT_SOURCE_DIR}/data")
16 18
19find_package(CURL 7.20.0 REQUIRED)
20
21include_directories(${CURL_INCLUDE_DIRS})
22
17add_library(mailplugin SHARED ${mailplugin_SRCS}) 23add_library(mailplugin SHARED ${mailplugin_SRCS})
18 24
19qt5_use_modules(mailplugin Core Quick Qml) 25qt5_use_modules(mailplugin Core Quick Qml)
20 26
21target_link_libraries(mailplugin actionplugin sink KF5::Otp) 27target_link_libraries(mailplugin actionplugin sink KF5::Otp ${CURL_LIBRARIES})
22 28
23install(TARGETS mailplugin DESTINATION ${QML_INSTALL_DIR}/org/kde/kube/mail) 29install(TARGETS mailplugin DESTINATION ${QML_INSTALL_DIR}/org/kde/kube/mail)
24install(FILES qmldir DESTINATION ${QML_INSTALL_DIR}/org/kde/kube/mail) 30install(FILES qmldir DESTINATION ${QML_INSTALL_DIR}/org/kde/kube/mail)
diff --git a/framework/mail/actions/mailactions.cpp b/framework/mail/actions/mailactions.cpp
new file mode 100644
index 00000000..dab0533e
--- /dev/null
+++ b/framework/mail/actions/mailactions.cpp
@@ -0,0 +1,48 @@
1/*
2 Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#include <actions/context.h>
20#include <actions/actionhandler.h>
21
22#include "../mailtransport.h"
23#include <KMime/Message>
24#include <QByteArray>
25#include <QVariant>
26#include <QDebug>
27
28using namespace Kube;
29
30static ActionHandlerHelper sendMailHandler("org.kde.kube.actions.sendmail",
31 [](Context *context) -> bool {
32 auto username = context->property("username").value<QByteArray>();
33 auto password = context->property("password").value<QByteArray>();
34 auto server = context->property("server").value<QByteArray>();
35 auto message = context->property("message").value<KMime::Message::Ptr>();
36 return !username.isEmpty() && !password.isEmpty() && !server.isEmpty() && message;
37 },
38 [](Context *context) {
39 auto username = context->property("username").value<QByteArray>();
40 auto password = context->property("password").value<QByteArray>();
41 auto server = context->property("server").value<QByteArray>();
42 //For ssl use "smtps://mainserver.example.net
43 QByteArray cacert; // = "/path/to/certificate.pem";
44 auto message = context->property("message").value<KMime::Message::Ptr>();
45 qWarning() << "Sending a mail: ";
46 MailTransport::sendMessage(message, server, username, password, cacert);
47 }
48);
diff --git a/framework/mail/mailtransport.cpp b/framework/mail/mailtransport.cpp
new file mode 100644
index 00000000..49d858e1
--- /dev/null
+++ b/framework/mail/mailtransport.cpp
@@ -0,0 +1,160 @@
1/*
2 Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19#include "mailtransport.h"
20
21#include <QByteArray>
22#include <QList>
23#include <QDebug>
24
25extern "C" {
26
27#include <stdio.h>
28#include <string.h>
29#include <curl/curl.h>
30
31struct upload_status {
32 int offset;
33 const char *data;
34};
35
36static size_t payload_source(void *ptr, size_t size, size_t nmemb, void *userp)
37{
38 struct upload_status *upload_ctx = (struct upload_status *)userp;
39 const char *data;
40
41 if((size == 0) || (nmemb == 0) || ((size*nmemb) < 1)) {
42 return 0;
43 }
44
45 data = &upload_ctx->data[upload_ctx->offset];
46 if(data) {
47 size_t len = strlen(data);
48 if (len > size * nmemb) {
49 len = size * nmemb;
50 }
51 fprintf(stderr, "read n bytes: %d\n",len);
52 memcpy(ptr, data, len);
53 upload_ctx->offset += len;
54 return len;
55 }
56
57 return 0;
58}
59
60
61void 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{
63 //For ssl use "smtps://mainserver.example.net
64 const char* cacert = 0; // = "/path/to/certificate.pem";
65
66 CURL *curl;
67 CURLcode res = CURLE_OK;
68 struct curl_slist *recipients = NULL;
69 struct upload_status upload_ctx;
70
71 upload_ctx.offset = 0;
72 upload_ctx.data = msg;
73
74 curl = curl_easy_init();
75 if(curl) {
76 curl_easy_setopt(curl, CURLOPT_USERNAME, username);
77 curl_easy_setopt(curl, CURLOPT_PASSWORD, password);
78
79 curl_easy_setopt(curl, CURLOPT_URL, server);
80
81 if (useTls) {
82 curl_easy_setopt(curl, CURLOPT_USE_SSL, (long)CURLUSESSL_TRY);
83 }
84
85 if (!verifyPeer) {
86 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L);
87 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L);
88 }
89 if (cacert) {
90 curl_easy_setopt(curl, CURLOPT_CAINFO, cacert);
91 }
92
93 if (from) {
94 curl_easy_setopt(curl, CURLOPT_MAIL_FROM, from);
95 }
96
97 for (int i = 0; i < numTos; i++) {
98 recipients = curl_slist_append(recipients, to[i]);
99 }
100 for (int i = 0; i < numCcs; i++) {
101 recipients = curl_slist_append(recipients, cc[i]);
102 }
103 curl_easy_setopt(curl, CURLOPT_MAIL_RCPT, recipients);
104
105 /* We're using a callback function to specify the payload (the headers and
106 * body of the message). You could just use the CURLOPT_READDATA option to
107 * specify a FILE pointer to read from. */
108 curl_easy_setopt(curl, CURLOPT_READFUNCTION, payload_source);
109 curl_easy_setopt(curl, CURLOPT_READDATA, &upload_ctx);
110 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
111
112 /* Since the traffic will be encrypted, it is very useful to turn on debug
113 * information within libcurl to see what is happening during the transfer.
114 */
115 curl_easy_setopt(curl, CURLOPT_VERBOSE, 1L);
116
117 res = curl_easy_perform(curl);
118 if(res != CURLE_OK) {
119 fprintf(stderr, "curl_easy_perform() failed: %s\n",
120 curl_easy_strerror(res));
121 }
122 curl_slist_free_all(recipients);
123 curl_easy_cleanup(curl);
124 }
125}
126
127};
128
129void MailTransport::sendMessage(const KMime::Message::Ptr &message, const QByteArray &server, const QByteArray &username, const QByteArray &password, const QByteArray &cacert)
130{
131 QByteArray msg = message->encodedContent();
132 qWarning() << "Sending message " << msg;
133
134 QByteArray from(message->from(true)->mailboxes().isEmpty() ? QByteArray() : message->from(true)->mailboxes().first().address());
135 QList<QByteArray> toList;
136 for (const auto &mb : message->to(true)->mailboxes()) {
137 toList << mb.address();
138 }
139 QList<QByteArray> ccList;
140 for (const auto &mb : message->cc(true)->mailboxes()) {
141 ccList << mb.address();
142 }
143 bool useTls = true;
144 bool verifyPeer = false;
145
146 const int numTos = toList.size();
147 const char* to[numTos];
148 for (int i = 0; i < numTos; i++) {
149 to[i] = toList.at(i);
150 }
151
152 const int numCcs = ccList.size();
153 const char* cc[numCcs];
154 for (int i = 0; i < numCcs; i++) {
155 cc[i] = ccList.at(i);
156 }
157
158 sendMessageCurl(to, numTos, cc, numCcs, msg, useTls, from.isEmpty() ? nullptr : from, username, password, server, verifyPeer);
159 qWarning() << "Message sent";
160}
diff --git a/framework/mail/mailtransport.h b/framework/mail/mailtransport.h
new file mode 100644
index 00000000..2eb30a03
--- /dev/null
+++ b/framework/mail/mailtransport.h
@@ -0,0 +1,28 @@
1/*
2 Copyright (c) 2016 Christian Mollekopf <mollekopf@kolabsys.com>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#pragma once
21
22#include <QByteArray>
23#include <KMime/Message>
24
25namespace MailTransport
26{
27 void sendMessage(const KMime::Message::Ptr &message, const QByteArray &server, const QByteArray &username, const QByteArray &password, const QByteArray &cacert);
28};