summaryrefslogtreecommitdiffstats
path: root/src/uscxml/plugins/invoker/imap
diff options
context:
space:
mode:
authorStefan Radomski <radomski@tk.informatik.tu-darmstadt.de>2014-01-22 22:03:00 (GMT)
committerStefan Radomski <radomski@tk.informatik.tu-darmstadt.de>2014-01-22 22:03:00 (GMT)
commita1ad371e696289b022484c87c9d191180b132d18 (patch)
tree45c230bcd32675468d62b7bba5cd49cf4e7cdeb8 /src/uscxml/plugins/invoker/imap
parent4206a3f8629da7005a6154912ccd690a38541930 (diff)
downloaduscxml-a1ad371e696289b022484c87c9d191180b132d18.zip
uscxml-a1ad371e696289b022484c87c9d191180b132d18.tar.gz
uscxml-a1ad371e696289b022484c87c9d191180b132d18.tar.bz2
Ported IM Invoker for pre 3.0 and beautified code
Diffstat (limited to 'src/uscxml/plugins/invoker/imap')
-rw-r--r--src/uscxml/plugins/invoker/imap/IMAPInvoker.cpp55
-rw-r--r--src/uscxml/plugins/invoker/imap/IMAPInvoker.h8
2 files changed, 32 insertions, 31 deletions
diff --git a/src/uscxml/plugins/invoker/imap/IMAPInvoker.cpp b/src/uscxml/plugins/invoker/imap/IMAPInvoker.cpp
index 22e32da..ffe00a9 100644
--- a/src/uscxml/plugins/invoker/imap/IMAPInvoker.cpp
+++ b/src/uscxml/plugins/invoker/imap/IMAPInvoker.cpp
@@ -61,16 +61,16 @@ size_t IMAPInvoker::writeCurlData(void *ptr, size_t size, size_t nmemb, void *us
size_t toWrite = std::min(ctx->content.length() - ctx->readPtr, size * nmemb);
if (toWrite > 0) {
- memcpy (ptr, ctx->content.c_str() + ctx->readPtr, toWrite);
+ memcpy (ptr, ctx->content.c_str() + ctx->readPtr, toWrite);
ctx->readPtr += toWrite;
}
-
+
return toWrite;
}
std::list<std::string> IMAPInvoker::getAtoms(std::list<Data> list) {
std::list<std::string> atoms;
-
+
std::list<Data>::const_iterator iter = list.begin();
while(iter != list.end()) {
const Data& data = *iter;
@@ -106,7 +106,7 @@ void IMAPInvoker::getAttachments(std::list<Data> list, std::list<Data>& attachme
att.compound["mimetype"] = Data("text/plain", Data::VERBATIM);
}
}
-
+
if (!att.hasKey("filename")) {
std::stringstream filenameSS;
filenameSS << "attachment" << attachments.size() + 1;
@@ -119,19 +119,19 @@ void IMAPInvoker::getAttachments(std::list<Data> list, std::list<Data>& attachme
}
attachments.push_back(att);
-
+
} else if (data.binary) {
// a single binary blob
Data att;
att.compound["data"].binary = data.binary;
-
+
if (data.binary->mimeType.size() > 0) {
att.compound["mimetype"] = Data(attachments.back()["data"].binary->mimeType, Data::VERBATIM);
} else {
att.compound["mimetype"] = Data("application/octet-stream", Data::VERBATIM);
}
-
+
std::stringstream filenameSS;
filenameSS << "attachment" << attachments.size() + 1;
if (boost::starts_with(att.compound["mimetype"].atom, "text")) {
@@ -140,7 +140,7 @@ void IMAPInvoker::getAttachments(std::list<Data> list, std::list<Data>& attachme
filenameSS << ".bin";
}
att.compound["filename"] = Data(filenameSS.str(), Data::VERBATIM);
-
+
attachments.push_back(att);
} else if (data.compound.size() > 0) {
@@ -159,10 +159,10 @@ void IMAPInvoker::getAttachments(std::list<Data> list, std::list<Data>& attachme
iter++;
}
}
-
+
void IMAPInvoker::send(const SendRequest& req) {
if (iequals(req.name, "mail.send")) {
-
+
struct curl_slist* recipients = NULL;
CURLcode curlError;
std::string multipartSep;
@@ -177,7 +177,7 @@ void IMAPInvoker::send(const SendRequest& req) {
std::list<Data> ccParams;
std::list<Data> bccParams;
std::list<Data> attachmentParams;
-
+
Event::getParam(req.params, "verbose", verbose);
Event::getParam(req.params, "ssl", useSSL);
Event::getParam(req.params, "Content-Type", contentType);
@@ -191,7 +191,7 @@ void IMAPInvoker::send(const SendRequest& req) {
if (contentType.size() == 0)
contentType = "text/plain; charset=\"UTF-8\"";
-
+
IMAPContext* ctx = new IMAPContext();
std::stringstream contentSS;
@@ -200,11 +200,12 @@ void IMAPInvoker::send(const SendRequest& req) {
std::list<std::string> cc = getAtoms(ccParams);
std::list<std::string> bcc = getAtoms(bccParams);
std::list<std::string> headers = getAtoms(headerParams);
- std::list<Data> attachments; getAttachments(attachmentParams, attachments);
+ std::list<Data> attachments;
+ getAttachments(attachmentParams, attachments);
if (to.size() == 0)
return;
-
+
recIter = to.begin();
recIter++; // skip first as we need it in CURLOPT_MAIL_RCPT
while(recIter != to.end()) {
@@ -235,7 +236,7 @@ void IMAPInvoker::send(const SendRequest& req) {
boost::replace_all(subject, "\r", " ");
contentSS << "Subject: " << subject << "\n";
}
-
+
// content type is different when we have attachments
if (attachments.size() > 0) {
multipartSep = UUID::getUUID();
@@ -249,10 +250,10 @@ void IMAPInvoker::send(const SendRequest& req) {
// when we have no attachment, respect user-defined or use text/plain
contentSS << "Content-Type: " << contentType << "\n";
}
-
+
contentSS << "\n";
contentSS << req.content;
-
+
std::list<Data>::iterator attIter = attachments.begin();
while(attIter != attachments.end()) {
// only send valid attachments
@@ -263,7 +264,7 @@ void IMAPInvoker::send(const SendRequest& req) {
contentSS << "--" << multipartSep << "\n";
contentSS << "Content-Disposition: attachment; filename=\"" << attIter->compound["filename"].atom << "\"";
contentSS << "\n";
-
+
contentSS << "Content-Type: " << attIter->compound["mimetype"].atom << "; ";
contentSS << "name=\"" << attIter->compound["filename"].atom << "\"";
contentSS << "\n";
@@ -280,10 +281,10 @@ void IMAPInvoker::send(const SendRequest& req) {
}
attIter++;
}
-
+
ctx->content = contentSS.str();
ctx->invoker = this;
-
+
// see http://curl.haxx.se/libcurl/c/imap-tls.html
_curl = curl_easy_init();
@@ -294,7 +295,7 @@ void IMAPInvoker::send(const SendRequest& req) {
LOG(ERROR) << "Cannot set password: " << curl_easy_strerror(curlError);
(curlError = curl_easy_setopt(_curl, CURLOPT_URL, _server.c_str())) == CURLE_OK ||
LOG(ERROR) << "Cannot set server string: " << curl_easy_strerror(curlError);
-
+
if (useSSL) {
(curlError = curl_easy_setopt(_curl, CURLOPT_USE_SSL, (long)CURLUSESSL_ALL)) == CURLE_OK ||
LOG(ERROR) << "Cannot use SSL: " << curl_easy_strerror(curlError);
@@ -309,12 +310,12 @@ void IMAPInvoker::send(const SendRequest& req) {
#endif
}
-
+
// this is needed, even if we have a callback function
recipients = curl_slist_append(recipients, to.begin()->c_str());
(curlError = curl_easy_setopt(_curl, CURLOPT_MAIL_RCPT, recipients)) == CURLE_OK ||
LOG(ERROR) << "Cannot set mail recipient: " << curl_easy_strerror(curlError);
-
+
(curlError = curl_easy_setopt(_curl, CURLOPT_READFUNCTION, IMAPInvoker::writeCurlData)) == CURLE_OK ||
LOG(ERROR) << "Cannot register read function: " << curl_easy_strerror(curlError);
(curlError = curl_easy_setopt(_curl, CURLOPT_READDATA, ctx)) == CURLE_OK ||
@@ -326,16 +327,16 @@ void IMAPInvoker::send(const SendRequest& req) {
(curlError = curl_easy_setopt(_curl, CURLOPT_MAIL_FROM, from.c_str())) == CURLE_OK ||
LOG(ERROR) << "Cannot set from parameter: " << curl_easy_strerror(curlError);
}
-
+
if (verbose) {
(curlError = curl_easy_setopt(_curl, CURLOPT_VERBOSE, 1L)) == CURLE_OK ||
LOG(ERROR) << "Cannot set curl to verbose: " << curl_easy_strerror(curlError);
}
CURLcode res = curl_easy_perform(_curl);
-
+
/* Check for errors */
- if(res != CURLE_OK){
+ if(res != CURLE_OK) {
LOG(ERROR) << "curl_easy_perform() failed: " << curl_easy_strerror(res);
returnErrorExecution("error.mail.send");
} else {
@@ -344,7 +345,7 @@ void IMAPInvoker::send(const SendRequest& req) {
/* Free the list of recipients */
if (recipients)
curl_slist_free_all(recipients);
-
+
/* Always cleanup */
curl_easy_cleanup(_curl);
diff --git a/src/uscxml/plugins/invoker/imap/IMAPInvoker.h b/src/uscxml/plugins/invoker/imap/IMAPInvoker.h
index b772bdd..5ffa727 100644
--- a/src/uscxml/plugins/invoker/imap/IMAPInvoker.h
+++ b/src/uscxml/plugins/invoker/imap/IMAPInvoker.h
@@ -49,20 +49,20 @@ public:
virtual void invoke(const InvokeRequest& req);
protected:
-
+
class IMAPContext {
public:
IMAPContext() : readPtr(0) {}
std::string content;
- size_t readPtr;
+ size_t readPtr;
IMAPInvoker* invoker;
};
-
+
CURL* _curl;
std::string _username;
std::string _password;
std::string _server;
-
+
std::list<std::string> getAtoms(std::list<Data> list);
void getAttachments(std::list<Data> list, std::list<Data>& attachments);
static size_t writeCurlData(void *ptr, size_t size, size_t nmemb, void *userdata);