Continued work.

This commit is contained in:
Brad Arant committed 2022-12-07 15:00:04 -08:00
1 parent 30cd31c171
commit c9cdc822a2
21 files changed
+1531 -148

No files matched your search

+44 -32
View File
@@ -2,6 +2,8 @@
# define __SMTPServer_h__
# include <string>
# include <vector>
# include <filesystem>
# include "EPoll.h"
# include "TCPServer.h"
# include "SMTPSession.h"
@@ -18,14 +20,15 @@
# include "__SMTP_VRFY.h"
# include "INotify.h"
# include "Log.h"
# include "SendMail.h"
namespace mail {
class SMTPServer : public core::TCPServer, public core::INotify {
public:
SMTPServer(core::EPoll &ePoll, std::string hostName, MailFileSystem &mailFileSystem, core::IPAddress ipAddress)
: core::TCPServer(ePoll, ipAddress, " ", 1, "SMTP Server"), core::INotify(ePoll), hostName(hostName), mailFileSystem(mailFileSystem) {
SMTPServer(core::EPoll &ePoll, std::string hostName, MailFileSystem &mailFileSystem, core::IPAddress ipAddress)
: core::TCPServer(ePoll, ipAddress, " ", 1, "SMTP Server"), core::INotify(ePoll), ePoll(ePoll), hostName(hostName), mailFileSystem(mailFileSystem) {
commands.add(_smtp_auth, "AUTH");
commands.add(_smtp_data, "DATA");
commands.add(_smtp_ehlo, "EHLO");
@@ -37,37 +40,41 @@ namespace mail {
commands.add(_smtp_rset, "RSET");
commands.add(_smtp_vrfy, "VRFY");
processExisting();
wd = addWatch(mailPath + "/.queue/");
}
wd = addWatch(mailFileSystem.getQueuePath());
}
MailFileSystem &mailFileSystem;
std::string hostName;
std::string mailPath;
SMTPSession * getSocketAccept(core::EPoll &ePoll) override {
core::EPoll &ePoll;
SMTPSession * getSocketAccept(core::EPoll &ePoll) override {
return new SMTPSession(ePoll, *this);
}
void sessionErrorHandler(std::string errorString, std::stringstream &out) {
out << "252 " << errorString << CRLF;
}
void sessionErrorHandler(std::string errorString, std::stringstream &out) {
out << "252 " << errorString << CRLF;
}
protected:
void inCreate(std::string name) {
int pos = name.find(".") + 10;
std::string mail = name.substr(0, pos);
coreutils::ZString recipient = name.substr(pos);
if(recipient != "") {
coreutils::MString fileName;
fileName << mailPath << "/.queue/" << name;
coreutils::MString path;
path << mailFileSystem.getMailBoxPath(recipient) << "/Inbox/" << mail;
int rc = link(fileName.c_str(), path.c_str());
rc = unlink(fileName.c_str());
coreutils::Log(coreutils::LOG_INFO) << "Message " << mail << " delivered to " << recipient << ".";
}
void inCreate(coreutils::ZString &name) {
coreutils::Log(coreutils::LOG_DEBUG_2) << "inCreate; [" << name << "]";
name.split("|");
if(name.getList().size() > 1) {
bool exists = mailFileSystem.ifMailBoxExists(name[1]);
coreutils::MString path;
path << mailFileSystem.getMailBoxPath(name[1]) << "/Inbox/" << name[0];
coreutils::MString from;
from << mailFileSystem.getQueuePath() << "/" << name;
if(exists) {
int rc = link(from.c_str(), path.c_str());
rc = unlink(from.c_str());
coreutils::Log(coreutils::LOG_INFO) << "Message " << name[0] << " delivered to recipient " << name[1] << ".";
} else {
mailQueue.emplace_back(SendMail(ePoll, name[1].str(), name[1].str(), from.str()));
}
}
}
private:
__SMTP_AUTH _smtp_auth;
__SMTP_DATA _smtp_data;
@@ -80,11 +87,16 @@ namespace mail {
__SMTP_RSET _smtp_rset;
__SMTP_VRFY _smtp_vrfy;
int wd;
std::vector<SendMail> mailQueue;
void processExisting() {
for (const auto & entry : std::filesystem::directory_iterator(mailFileSystem.getQueuePath().str())) {
std::string temp(entry.path().filename().string());
coreutils::ZString temp2(temp);
inCreate(temp2);
}
}
};
}