1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
|
/*
Copyright (©) 2003-2025 Teus Benschop.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <email/receive.h>
#include <database/logs.h>
#ifndef HAVE_CLIENT
#include <curl/curl.h>
#endif
#include <database/config/general.h>
#include <filter/string.h>
#include <filter/mail.h>
#include <config/globals.h>
#include <notes/logic.h>
#include <filter/url.h>
namespace email {
void receive ()
{
#ifdef HAVE_CLOUD
// Bail out when the mail storage host has not been defined, rather than giving an error message.
if (database::config::general::get_mail_storage_host () == "") return;
// One email receiver runs at a time.
if (config_globals_mail_receive_running) return;
config_globals_mail_receive_running = true;
// Once this flag is set, the coder should be careful not to exit from the function
// without clearin this flag.
// Email count.
std::string error;
int emailcount = email::receive_count (error);
// Messages start at number 1 instead of 0.
for (int i = 1; i <= emailcount; i++) {
Webserver_Request webserver_request;
Notes_Logic notes_logic (webserver_request);
error.clear ();
std::string message = email::receive_message (error);
if (error.empty ()) {
// Extract "from" and subject, and clean body.
std::string from;
std::string subject;
std::string body;
filter_mail_dissect (message, from, subject, body);
Database_Logs::log ("Processing email from " + from + " with subject " + subject);
if (notes_logic.handleEmailComment (from, subject, body)) {
}
else if (notes_logic.handleEmailNew (from, subject, body)) {
}
else {
Database_Logs::log ("Could not allocate email from " + from + ", subject " + subject);
Database_Logs::log (body);
}
} else {
Database_Logs::log ("Error retrieving mail: " + error);
}
}
config_globals_mail_receive_running = false;
#endif
}
struct cstring {
char *ptr;
size_t len;
};
void init_string (cstring *s) {
s->len = 0;
s->ptr = static_cast<char *>(malloc(s->len+1));
s->ptr[0] = '\0';
}
size_t writefunc(void *ptr, size_t size, size_t nmemb, cstring *s)
{
size_t new_len = s->len + size*nmemb;
s->ptr = static_cast<char *>(realloc (s->ptr, new_len+1));
memcpy(s->ptr+s->len, ptr, size*nmemb);
s->ptr[new_len] = '\0';
s->len = new_len;
return size*nmemb;
}
std::string url ()
{
std::string url;
const char * pop3s = "POP3S";
if (database::config::general::get_mail_storage_protocol() == pop3s) url.append (pop3s);
else url.append ("pop3");
url.append ("://");
url.append (database::config::general::get_mail_storage_host ());
url.append (":");
url.append (database::config::general::get_mail_storage_port ());
return url;
}
// Returns how many emails are waiting in the mail storage host's POP3 email inbox.
int receive_count (std::string& error, bool verbose)
{
#ifdef HAVE_CLIENT
error = "Not implemented with embedded http library";
if (verbose) {}
return 0;
#endif
#ifdef HAVE_CLOUD
CURL *curl;
CURLcode res = CURLE_OK;
cstring s;
init_string (&s);
curl = curl_easy_init ();
curl_easy_setopt (curl, CURLOPT_USERNAME, database::config::general::get_mail_storage_username ().c_str());
curl_easy_setopt (curl, CURLOPT_PASSWORD, database::config::general::get_mail_storage_password ().c_str());
curl_easy_setopt (curl, CURLOPT_URL, url ().c_str());
curl_easy_setopt (curl, CURLOPT_USE_SSL, static_cast<long>(CURLUSESSL_ALL));
curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt (curl, CURLOPT_WRITEDATA, &s);
if (verbose) {
curl_easy_setopt (curl, CURLOPT_DEBUGFUNCTION, filter_url_curl_debug_callback);
curl_easy_setopt (curl, CURLOPT_VERBOSE, 1L);
}
// Some servers need this validation.
curl_easy_setopt (curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
filter_url_curl_set_timeout (curl);
res = curl_easy_perform (curl);
int mailcount = 0;
if (res == CURLE_OK) {
if (s.ptr) {
std::string response = s.ptr;
response = filter::strings::trim (response);
mailcount = static_cast<int>(filter::strings::explode (response, '\n').size());
}
} else {
error = curl_easy_strerror (res);
}
if (s.ptr) free (s.ptr);
curl_easy_cleanup (curl);
return mailcount;
#endif
}
std::string receive_message (std::string& error)
{
#ifdef HAVE_CLIENT
error = "Not implemented with embedded http library";
return std::string();
#endif
#ifdef HAVE_CLOUD
CURL *curl;
CURLcode res = CURLE_OK;
cstring s;
init_string (&s);
curl = curl_easy_init ();
curl_easy_setopt (curl, CURLOPT_USERNAME, database::config::general::get_mail_storage_username ().c_str());
curl_easy_setopt (curl, CURLOPT_PASSWORD, database::config::general::get_mail_storage_password ().c_str());
std::string message_url = url () + "/1";
curl_easy_setopt (curl, CURLOPT_URL, message_url.c_str());
curl_easy_setopt (curl, CURLOPT_USE_SSL, static_cast<long>(CURLUSESSL_ALL));
curl_easy_setopt (curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt (curl, CURLOPT_SSL_VERIFYHOST, 0);
curl_easy_setopt (curl, CURLOPT_WRITEFUNCTION, writefunc);
curl_easy_setopt (curl, CURLOPT_WRITEDATA, &s);
// Some servers need this validation.
curl_easy_setopt (curl, CURLOPT_USERAGENT, "libcurl-agent/1.0");
filter_url_curl_set_timeout (curl);
res = curl_easy_perform (curl);
std::string body {};
if (res == CURLE_OK) {
if (s.ptr) body = s.ptr;
} else {
error = curl_easy_strerror (res);
}
// Set the DELE command.
curl_easy_setopt (curl, CURLOPT_CUSTOMREQUEST, "DELE");
// Do not perform a transfer as DELE returns no data.
curl_easy_setopt (curl, CURLOPT_NOBODY, 1L);
// Perform the custom request.
res = curl_easy_perform(curl);
if (s.ptr) free (s.ptr);
curl_easy_cleanup (curl);
return body;
#endif
}
} // End namespace.
|