Compilable SMTP

This commit is contained in:
Brad Arant 2022-07-08 19:30:00 -07:00
parent 6c844983d9
commit e1f33291f7
70 changed files with 308 additions and 362 deletions

BIN
BMAMail

Binary file not shown.

View File

@ -5,10 +5,8 @@
namespace mail {
int IMAPCommand::processCommand(std::string request, core::TCPSession *session, std::stringstream &data) {
coreutils::PString parser(request);
parser.split(" ");
return processCommand(parser, (IMAPSession &)*session, (IMAPServer &)session->server, data);
int IMAPCommand::processCommand(coreutils::ZString &request, core::TCPSession &session) {
return processCommand(request, (IMAPSession &)session, (IMAPServer &)session.server);
}
}

View File

@ -11,8 +11,8 @@ namespace mail {
class IMAPCommand : public core::Command {
public:
virtual int processCommand(std::string request, core::TCPSession *session, std::stringstream &data) override;
virtual int processCommand(coreutils::PString request, IMAPSession &session, IMAPServer &server, std::stringstream &data) { return 0; }
virtual int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
virtual int processCommand(coreutils::ZString &request, IMAPSession &session, IMAPServer &server) { return 0; }
};

View File

@ -33,7 +33,7 @@ namespace mail {
public:
IMAPServer(core::EPoll &epoll, std::string hostName, MailFileSystem &mailFileSystem, core::IPAddress ipAddress)
: TCPServer(epoll, ipAddress, "IMAP Server"), mailFileSystem(mailFileSystem), hostName(hostName) {
: TCPServer(epoll, ipAddress, " ", 10, "IMAP Server"), mailFileSystem(mailFileSystem), hostName(hostName) {
commands.add(_imap_append, "APPEND");
commands.add(_imap_authenticate, "AUTHENTCATE");
commands.add(_imap_check, "CHECK");

View File

@ -4,8 +4,8 @@
# include <sys/types.h>
# include <sys/stat.h>
# include <unistd.h>
# include "EPoll.h"
# include "INotify.h"
# include "ZString.h"
namespace mail {
@ -29,7 +29,7 @@ namespace mail {
}
std::string getMailBoxPath(std::string mailbox) {
coreutils::PString email(mailbox);
coreutils::ZString email(mailbox);
email.split("@");
std::string path = mailPath + "/" + email[1].str() + "/" + email[0].str();
return path;

View File

@ -6,16 +6,17 @@
# include "POP3Session.h"
namespace mail {
class POP3Server;
class POP3Command : public core::Command {
public:
virtual int processCommand(std::string request, core::TCPSession *session, std::stringstream &data) override {
coreutils::PString parser(request);
return processCommand(parser, (POP3Session &)*session, data);
virtual int processCommand(coreutils::ZString &request, core::TCPSession &session) override {
return processCommand(request, (POP3Session &)session, (POP3Server &)session.server);
}
virtual int processCommand(coreutils::PString request, POP3Session &session, std::stringstream &data) {
virtual int processCommand(coreutils::ZString request, POP3Session &session, POP3Server &server) {
return 0;
}

View File

@ -22,7 +22,7 @@ namespace mail {
public:
POP3Server(core::EPoll &epoll, std::string hostName, MailFileSystem &mailFileSystem, core::IPAddress ipAddress)
: TCPServer(epoll, ipAddress, "POP3 Server"), mailFileSystem(mailFileSystem), hostName(hostName) {
: TCPServer(epoll, ipAddress, " ", 10, "POP3 Server"), mailFileSystem(mailFileSystem), hostName(hostName) {
commands.add(_pop3_dele, "DELE");
commands.add(_pop3_last, "LAST");
commands.add(_pop3_list, "LIST");

View File

@ -10,9 +10,8 @@ namespace mail {
std::string userName;
std::string password;
AuthState authstate = USER_UNKNOWN;
bool relay = false;
bool relay = false;
};
}

37
SMTPCommand.h Normal file
View File

@ -0,0 +1,37 @@
#ifndef __SMTPCommand_h__
# define __SMTPCommand_h__
# include "Command.h"
# include "SMTPSession.h"
#define CRLF "\r\n"
namespace mail {
class SMTPServer;
class SMTPCommand : public core::Command {
public:
virtual int processCommand(coreutils::ZString &request, core::TCPSession &session) override {
return processCommand(request, (SMTPSession &)session, (SMTPServer &)session.server);
}
virtual int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
return 0;
}
void grabInput(SMTPSession &session) {
session.server.commands.grabInput(session, *this);
}
void clearGrab(SMTPSession &session) {
session.server.commands.clearGrab(session);
}
};
}
#endif

View File

@ -25,7 +25,7 @@ namespace mail {
public:
SMTPServer(core::EPoll &ePoll, std::string hostName, MailFileSystem &mailFileSystem, core::IPAddress ipAddress)
: core::TCPServer(ePoll, ipAddress, "SMTP Server"), core::INotify(ePoll), hostName(hostName), mailFileSystem(mailFileSystem) {
: core::TCPServer(ePoll, ipAddress, " ", 10, "SMTP Server"), core::INotify(ePoll), hostName(hostName), mailFileSystem(mailFileSystem) {
commands.add(_smtp_auth, "AUTH");
commands.add(_smtp_data, "DATA");
commands.add(_smtp_ehlo, "EHLO");
@ -49,23 +49,23 @@ namespace mail {
}
void sessionErrorHandler(std::string errorString, std::stringstream &out) {
out << "252 " << errorString << CRLF;
out << "252 " << errorString << CRLF;
}
protected:
void inCreate(std::string name) {
int pos = name.find(".") + 10;
std::string mail = name.substr(0, pos);
std::string recipient = name.substr(pos);
if(recipient != "") {
std::string fileName = mailPath + "/.queue/" + name;
std::string 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 << ".";
}
int pos = name.find(".") + 10;
std::string mail = name.substr(0, pos);
std::string recipient = name.substr(pos);
if(recipient != "") {
std::string fileName = mailPath + "/.queue/" + name;
std::string 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 << ".";
}
}
private:
__SMTP_AUTH _smtp_auth;
__SMTP_DATA _smtp_data;

View File

@ -2,8 +2,8 @@
namespace mail {
int __IMAP_APPEND::processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int __IMAP_APPEND::processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,13 +2,12 @@
# define ____IMAP_APPEND_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_APPEND : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data);
int processCommand(coreutils::ZString request, IMAPSession &session);
};

View File

@ -2,15 +2,13 @@
# define ____IMAP_AUTHENTICATE_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
# include "PString.h"
namespace mail {
class __IMAP_AUTHENTICATE : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_CHECK_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_CHECK : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_CLOSE_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_CLOSE : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_COPY_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_COPY : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_CREATE_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_CREATE : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_DELETE_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_DELETE : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_EXAMINE_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_EXAMINE : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_EXPUNGE_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_EXPUNGE : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_FETCH_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_FETCH : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_LIST_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_LIST : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_LOGIN_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_LOGIN : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_LSUB_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_LSUB : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_RENAME_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_RENAME : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_SEARCH_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_SEARCH : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_SELECT_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_SELECT : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_STARTTLS_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_STARTTLS : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_STATUS_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_STATUS : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_STORE_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_STORE : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_SUBSCRIBE_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_SUBSCRIBE : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_UID_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_UID : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____IMAP_UNSUBSCRIBE_h__
# include "IMAPCommand.h"
# include "IMAPSession.h"
namespace mail {
class __IMAP_UNSUBSCRIBE : public IMAPCommand {
int processCommand(coreutils::PString request, IMAPSession &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, IMAPSession &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____POP3_DELE_h__
# include "POP3Command.h"
# include "POP3Session.h"
namespace mail {
class __POP3_DELE : public POP3Command {
int processCommand(coreutils::PString request, POP3Session &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString &request, POP3Session &session, POP3Server &server) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____POP3_LAST_h__
# include "POP3Command.h"
# include "POP3Session.h"
namespace mail {
class __POP3_LAST : public POP3Command {
int processCommand(coreutils::PString request, POP3Session &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString &request, POP3Session &session, POP3Server &server) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____POP3_LIST_h__
# include "POP3Command.h"
# include "POP3Session.h"
namespace mail {
class __POP3_LIST : public POP3Command {
int processCommand(coreutils::PString request, POP3Session &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, POP3Session &session, POP3Server &server) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____POP3_NOOP_h__
# include "POP3Command.h"
# include "POP3Session.h"
namespace mail {
class __POP3_NOOP : public POP3Command {
int processCommand(coreutils::PString request, POP3Session &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, POP3Session &session, POP3Server &server) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____POP3_PASS_h__
# include "POP3Command.h"
# include "POP3Session.h"
namespace mail {
class __POP3_PASS : public POP3Command {
int processCommand(coreutils::PString request, POP3Session &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, POP3Session &session, POP3Server &server) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____POP3_QUIT_h__
# include "POP3Command.h"
# include "POP3Session.h"
namespace mail {
class __POP3_QUIT : public POP3Command {
int processCommand(coreutils::PString request, POP3Session &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, POP3Session &session, POP3Server &server) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____POP3_RETR_h__
# include "POP3Command.h"
# include "POP3Session.h"
namespace mail {
class __POP3_RETR : public POP3Command {
int processCommand(coreutils::PString request, POP3Session &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, POP3Session &session, POP3Server &server) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____POP3_RPOP_h__
# include "POP3Command.h"
# include "POP3Session.h"
namespace mail {
class __POP3_RPOP : public POP3Command {
int processCommand(coreutils::PString request, POP3Session &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString &request, POP3Session &session, POP3Server &server) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____POP3_RSET_h__
# include "POP3Command.h"
# include "POP3Session.h"
namespace mail {
class __POP3_RSET : public POP3Command {
int processCommand(coreutils::PString request, POP3Session &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString &request, POP3Session &session, POP3Server &server) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____POP3_STAT_h__
# include "POP3Command.h"
# include "POP3Session.h"
namespace mail {
class __POP3_STAT : public POP3Command {
int processCommand(coreutils::PString request, POP3Session &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString &request, POP3Session &session, POP3Server &server) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____POP3_TOP_h__
# include "POP3Command.h"
# include "POP3Session.h"
namespace mail {
class __POP3_TOP : public POP3Command {
int processCommand(coreutils::PString request, POP3Session &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, POP3Session &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -2,14 +2,13 @@
# define ____POP3_USER_h__
# include "POP3Command.h"
# include "POP3Session.h"
namespace mail {
class __POP3_USER : public POP3Command {
int processCommand(coreutils::PString request, POP3Session &session, std::stringstream &data) {
data << "250 OK" << CRLF;
int processCommand(coreutils::ZString request, POP3Session &session) {
session.out << "250 OK" << CRLF;
return 1;
}

View File

@ -1,59 +1,56 @@
#include "__SMTP_AUTH.h"
#include "SMTPServer.h"
#include "Base64.h"
namespace mail {
int __SMTP_AUTH::processCommand(coreutils::ZString &request, core::TCPSession &session) {
SMTPSession &s = dynamic_cast<SMTPSession &>(session);
SMTPServer &server = session.server;
switch((SMTPSession &)session.authState) {
case USER_UNKNOWN:
if(request[1].equals("LOGIN") {
data << "334 VXNlcm5hbWU6" << CRLF;
// setTimer(10.0f);
s.authState = USER_QUERY;
server.commands.grabInput(&session, *this);
}
else {
s.out << "504 AUTH method not supported." << CRLF;
}
return 1;
int __SMTP_AUTH::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
switch(session.authState) {
case USER_UNKNOWN:
if(request[1].equals("LOGIN")) {
session.out << "334 VXNlcm5hbWU6" << CRLF;
// setTimer(10.0f);
session.authState = USER_QUERY;
grabInput(session);
}
else {
session.out << "504 AUTH method not supported." << CRLF;
}
return 1;
case USER_QUERY:
s.userName = request[0].str();
// setTimer(0.0f);
s.out << "334 UGFzc3dvcmQ6" << CRLF;
// setTimer(10.0f);
s.authState = USER_SECRET_QUERY;
return 1;
session.userName = request[0].str();
// setTimer(0.0f);
session.out << "334 UGFzc3dvcmQ6" << CRLF;
// setTimer(10.0f);
session.authState = USER_SECRET_QUERY;
return 1;
case USER_SECRET_QUERY:
s.password = request[0].str();
// setTimer(0.0f);
coreutils::Base64 base64;
if(authLogin(s.userName, s.password, server)) {
s.out << "235 2.7.0 Authentication successful" << CRLF;
s.relay = true;
s.authState = USER_KNOWN;
server.commands.clearGrab(&session);
}
else {
s.out << "530 Login was unsuccessful." << CRLF;
}
return 1;
session.password = request[0].str();
// setTimer(0.0f);
coreutils::Base64 base64;
if(authLogin(session.userName, session.password)) {
session.out << "235 2.7.0 Authentication successful" << CRLF;
session.relay = true;
session.authState = USER_KNOWN;
clearGrab(session);
}
else {
session.out << "530 Login was unsuccessful." << CRLF;
}
return 1;
}
return 0;
}
bool __SMTP_AUTH::authLogin(coreutils::ZString userName, coreutils::ZString password, SMTPServer &server) {
std::string secretPath = server.mailFileSystem.getMailBoxPath(userName) + "/.password";
coreutils::File secret(secretPath);
secret.read();
return secret.asString() == password;
bool __SMTP_AUTH::authLogin(coreutils::ZString userName, coreutils::ZString password) {
// std::string secretPath = server.mailFileSystem.getMailBoxPath(userName) + "/.password";
// coreutils::File secret(secretPath);
// secret.read();
// return secret.asString() == password;
return true;
}
}

View File

@ -1,18 +1,16 @@
#ifndef ____SMTP_AUTH_h__
# define ____SMTP_AUTH_h__
# include "Command.h"
# include "SMTPCommand.h"
namespace mail {
class SMTPServer;
class __SMTP_AUTH : public core::Command {
class __SMTP_AUTH : public SMTPCommand {
public:
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
bool authLogin(coreutils::ZString userName, coreutils::ZString password, SMTPServer &server);
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
bool authLogin(coreutils::ZString userName, coreutils::ZString password);
};
}

View File

@ -1,5 +1,4 @@
#include "__SMTP_DATA.h"
#include "SMTPSession.h"
#include "SMTPServer.h"
#include "Log.h"
#include <chrono>
@ -9,63 +8,60 @@
namespace mail {
int __SMTP_DATA::processCommand(coreutils::ZString &request, core::TCPSession &session) {
SMTPSession &s = dynamic_cast<SMTPSession &>(session);
SMTPServer &server = session.server;
int __SMTP_DATA::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
switch(session.mode) {
case WAIT_FOR_DATA:
switch(session.state) {
case RCPT:
s.out << "354 Enter the mail message terminated by <CRLF>.<CRLF>" << CRLF;
s.mailData.str("");
s.mode = RECEIVE_DATA;
server.commands.grabInput(&session, *this);
// setTimer(120.0f);
break;
case MAIL:
s.out << "503 Please use RCPT first" << CRLF;
break;
default:
s.out << "503 Please use MAIL first" << CRLF;
break;
}
switch(session.state) {
case RCPT:
session.out << "354 Enter the mail message terminated by <CRLF>.<CRLF>" << CRLF;
session.mailData.str("");
session.mode = RECEIVE_DATA;
grabInput(session);
// setTimer(120.0f);
break;
case RECEIVE_DATA:
if(request.str() != ".")
s.mailData << request.str() << CRLF;
else {
s.mode = WAIT_FOR_DATA;
s.state = READY;
server.commands.clearGrab(&session);
// if(filterMessage(session.mailData)) {
if(s.recipientList.size() > 0) {
if(s.mailData.str().size() > 0) {
std::string ID = queueMail(server, session.sender, session.recipientList, session.mailData);
if(ID != "") {
coreutils::Log(coreutils::LOG_INFO) << "Queued message " << ID << ".";
s.out << "250 OK Queued message " << ID << CRLF;
}
else
s.out << "550 Mail message too big" << CRLF;
} else
s.out << "550 Mail message was empty" << CRLF;
} else
s.out << "250 OK Queued message " << CRLF;
// } else
// data << "550 Message is probably spam" << CRLF;
}
break;
}
return 1;
case MAIL:
session.out << "503 Please use RCPT first" << CRLF;
break;
default:
session.out << "503 Please use MAIL first" << CRLF;
break;
}
break;
case RECEIVE_DATA:
if(request.str() != ".")
session.mailData << request.str() << CRLF;
else {
session.mode = WAIT_FOR_DATA;
session.state = READY;
clearGrab(session);
// if(filterMessage(session.mailData)) {
if(session.recipientList.size() > 0) {
if(session.mailData.str().size() > 0) {
std::string ID = queueMail(server, session.sender, session.recipientList, session.mailData);
if(ID != "") {
coreutils::Log(coreutils::LOG_INFO) << "Queued message " << ID << ".";
session.out << "250 OK Queued message " << ID << CRLF;
}
else
session.out << "550 Mail message too big" << CRLF;
} else
session.out << "550 Mail message was empty" << CRLF;
} else
session.out << "250 OK Queued message " << CRLF;
// } else
// data << "550 Message is probably spam" << CRLF;
}
break;
}
return 1;
}
std::string __SMTP_DATA::generateMailFileName() {
@ -80,17 +76,16 @@ namespace mail {
std::string fileName = server.mailFileSystem.getMailPath() + "/.queue/" + generateMailFileName();
coreutils::File mailFile(fileName, O_CREAT | O_WRONLY, 0660);
mailFile.write(mailData.str());
for(std::string recipient: recipientList) {
std::string newName = fileName + recipient;
link(fileName.c_str(), newName.c_str());
std::string newName = fileName + recipient;
link(fileName.c_str(), newName.c_str());
}
unlink(fileName.c_str());
return fileName;
}
}

View File

@ -6,12 +6,11 @@
namespace mail {
class SMTPServer;
class __SMTP_DATA : public SMTPCommand {
public:
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
std::string generateMailFileName();
std::string queueMail(SMTPServer &server, std::string sender, std::vector<std::string> recipientList, std::stringstream &mailData);

View File

@ -1,23 +1,19 @@
#include "__SMTP_EHLO.h"
#include "SMTPSession.h"
#define CRLF "\r\n"
#include "SMTPServer.h"
namespace mail {
int __SMTP_EHLO::processCommand(coreutils::ZString &request, core::TCPSession &session) {
int __SMTP_EHLO::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
SMTPSession &s = dynamic_cast<SMTPSession &>(session);
s.clientDomainName = request[1].str();
session.clientDomainName = request[1].str();
s.out << "250-" << server.hostName << CRLF;
session.out << "250-" << server.hostName << CRLF;
// cout << "250-STARTTLS" << CRLF;
// cout << "250-PIPELINING" << CRLF;
// cout << "250-8BITMIME" << CRLF;
s.out << "250-AUTH LOGIN" << CRLF;
s.out << "250 HELP" << CRLF;
s.state = READY;
session.out << "250-AUTH LOGIN" << CRLF;
session.out << "250 HELP" << CRLF;
session.state = READY;
return 1;
}

View File

@ -2,7 +2,6 @@
# define ____SMTP_EHLO_h__
# include "SMTPCommand.h"
# include "SMTPSession.h"
namespace mail {
@ -11,7 +10,7 @@ namespace mail {
class __SMTP_EHLO : public SMTPCommand {
public:
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
};

View File

@ -1,14 +1,11 @@
#include "__SMTP_EXPN.h"
#include "SMTPServer.h"
#define CRLF "\r\n"
namespace mail {
int __SMTP_EXPN::processCommand(coreutils::ZString &request, core::TCPSession &session) {
SMTPSession &s = dynamic_cast<SMTPSession &>(session);
s.out << "252 You must know recipient." << CRLF;
s.state = READY;
int __SMTP_EXPN::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
session.out << "252 You must know recipient." << CRLF;
session.state = READY;
return 1;
}

View File

@ -2,7 +2,6 @@
# define ____SMTP_EXPN_h__
# include "SMTPCommand.h"
# include "SMTPSession.h"
namespace mail {
@ -11,7 +10,7 @@ namespace mail {
class __SMTP_EXPN : public SMTPCommand {
public:
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
};
}

View File

@ -3,11 +3,10 @@
namespace mail {
int __SMTP_HELO::processCommand(coreutils::ZString &request, core::TCPSession &session) {
SMTPSession &s = dynamic_cast<SMTPSession &>(session);
s.clientName = request[1].str();
s.out << "250 " << server.hostName << CRLF;
s.state = READY;
int __SMTP_HELO::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
session.clientDomainName = request[1].str();
session.out << "250 " << server.hostName << CRLF;
session.state = READY;
return 1;
}

View File

@ -1,17 +1,16 @@
#ifndef ____SMTP_HELO_h__
# define ____SMTP_HELO_h__
# include "Command.h"
# include "SMTPSession.h"
# include "SMTPCommand.h"
namespace mail {
class SMTPServer;
class __SMTP_HELO : public core::Command {
class __SMTP_HELO : public SMTPCommand {
public:
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
};
}

View File

@ -1,13 +1,11 @@
#include "__SMTP_HELP.h"
#include "SMTPServer.h"
#define CRLF "\r\n"
namespace mail {
int __SMTP_HELP::processCommand(coreutils::ZString &request, core::TCPSession &session) {
s.out << "250 Sure you need help." << CRLF;
s.state = READY;
int __SMTP_HELP::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
session.out << "250 Sure you need help." << CRLF;
session.state = READY;
return 1;
}

View File

@ -2,12 +2,11 @@
# define ____SMTP_HELP_h__
# include "SMTPCommand.h"
# include "SMTPSession.h"
namespace mail {
class __SMTP_HELP : public SMTPCommand {
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
};
}

View File

@ -1,27 +1,22 @@
#include "__SMTP_MAIL.h"
#include "SMTPSession.h"
#define CRLF "\r\n"
namespace mail {
int __SMTP_MAIL::processCommand(coreutils::ZString &request, core::TCPSession &session) {
SMTPSession &s = dynamic_cast<SMTPSession &>(session);
int __SMTP_MAIL::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
if(request.ifNext("MAIL FROM:")) {
request.skipWhitespace();
if(request.ifNext("<")) {
s.sender = request.getTokenExclude(">");
if(s.authState = USER_KNOWN) {
s.out << "250 OK" << CRLF;
s.recipientList.clear();
s.state = MAIL;
session.sender = request.getTokenExclude(">").str();
if(session.authState = USER_KNOWN) {
session.out << "250 OK" << CRLF;
session.recipientList.clear();
session.state = MAIL;
}
} else
s.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
session.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
} else
s.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
session.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
return 1;
}

View File

@ -2,16 +2,15 @@
# define ____SMTP_MAIL_h__
# include "SMTPCommand.h"
# include "SMTPSession.h"
namespace mail {
class SMTPServer;
// class SMTPServer;
class __SMTP_MAIL : public Command {
class __SMTP_MAIL : public SMTPCommand {
public:
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
};
}

View File

@ -1,17 +1,10 @@
#ifndef ____SMTP_NOOP_h__
# define ____SMTP_NOOP_h__
# include "SMTPSession.h"
# include "__SMTP_NOOP.h"
# include "ZString.h"
namespace mail {
int __SMTP_NOOP::processCommand(coreutils::ZString &request, core::TCPSession &session) {
SMTPSession &s = dynamic_cast<SMTPSession &>(session);
s.out << "250 OK" << CRLF;
return 1;
}
}
int __SMTP_NOOP::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
session.out << "250 OK" << CRLF;
return 1;
}
#endif
}

View File

@ -1,13 +1,12 @@
#ifndef ____SMTP_NOOP_h__
# define ____SMTP_NOOP_h__
# include "SMTPSession.h"
# include "ZString.h"
# include "SMTPCommand.h"
namespace mail {
class __SMTP_NOOP : public Command {
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
class __SMTP_NOOP : public SMTPCommand {
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
};
}

View File

@ -1,10 +1,11 @@
# include "__SMTP_QUIT.h"
# include "SMTPServer.h"
#include "SMTPServer.h"
namespace mail {
int __SMTP_QUIT::processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data) {
data << "221 " << server.hostName << CRLF;
int __SMTP_QUIT::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
session.out << "221 " << server.hostName << CRLF;
session.terminate();
return 1;
}

View File

@ -2,16 +2,15 @@
# define ____SMTP_QUIT_h__
# include "SMTPCommand.h"
# include "SMTPSession.h"
namespace mail {
class SMTPServer;
class __SMTP_QUIT : public Command {
class __SMTP_QUIT : public SMTPCommand {
public:
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
};
}

View File

@ -3,30 +3,30 @@
namespace mail {
int __SMTP_RCPT::processCommand(coreutils::PString request, SMTPSession &session, SMTPServer &server, std::stringstream &data) {
int __SMTP_RCPT::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
if((session.state == MAIL) || (session.state == RCPT)) {
std::string recipient;
if(request.ifNext("RCPT TO:")) {
request.skipWhitespace();
if(request.ifNext("<")) {
recipient = request.getTokenExclude(">");
recipient = request.getTokenExclude(">").str();
if(server.mailFileSystem.ifMailBoxExists(recipient)) {
session.recipientList.push_back(recipient);
data << "250 OK" << CRLF;
session.out << "250 OK" << CRLF;
session.state = RCPT;
} else if(session.relay) {
session.recipientList.push_back(recipient);
data << "250 OK" << CRLF;
session.out << "250 OK" << CRLF;
session.state = RCPT;
} else
data << "550 Mailbox does not exist" << CRLF;
session.out << "550 Mailbox does not exist" << CRLF;
} else
data << "550 Usage: RCPT TO:<email-address>" << CRLF;
session.out << "550 Usage: RCPT TO:<email-address>" << CRLF;
} else
data << "550 Usage: RCPT TO:<email-address>" << CRLF;
session.out << "550 Usage: RCPT TO:<email-address>" << CRLF;
} else
data << "503 Please use MAIL first" << CRLF;
session.out << "503 Please use MAIL first" << CRLF;
return 1;
}

View File

@ -1,15 +1,14 @@
#ifndef ____SMTP_RCPT_h__
# define ____SMTP_RCPT_h__
# include "Command.h"
# include "TCPSession.h"
# include "SMTPCommand.h"
namespace mail {
class __SMTP_RCPT : public core::Command {
class __SMTP_RCPT : public SMTPCommand {
public:
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
};
}

View File

@ -1,20 +1,11 @@
#ifndef ____SMTP_RSET_h__
#define ____SMTP_RSET_h__
#include "SMTPCommand.h"
#include "__SMTP_RSET.h"
namespace mail {
class __SMTP_RSET : public SMTPCommand {
int processCommand(coreutils::PString request, SMTPSession &session, std::stringstream &data) {
session.state = READY;
data << "250 OK" << CRLF;
return 0;
}
};
int __SMTP_RSET::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
session.state = READY;
session.out << "250 OK" << CRLF;
return 1;
}
}
#endif

View File

@ -1,14 +1,12 @@
#ifndef ____SMTP_RSET_h__
#define ____SMTP_RSET_h__
#include "Command.h"
#include "TCPSession.h"
#include "ZString.h"
#include "SMTPCommand.h"
namespace mail {
class __SMTP_RSET : public core::Command {
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
class __SMTP_RSET : public SMTPCommand {
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
};
}

View File

@ -1,9 +1,8 @@
#include "__SMTP_VRFY.h"
#define CRLF "\r\n"
namespace mail {
int __SMTP_VRFY::processCommand(coreutils::ZString &request, core::TCPSession &session) {
int __SMTP_VRFY::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
session.out << "252 You must know who the mail is for" << CRLF;
return 1;
}

View File

@ -1,14 +1,12 @@
#ifndef ____SMTP_VRFY_h__
# define ____SMTP_VRFY_h__
# include "Command.h"
# include "TCPSession.h"
# include "ZString.h"
# include "SMTPCommand.h"
namespace mail {
class __SMTP_VRFY : public core::Command {
int processCommand(coreutils::ZString &request, core::TCPSession &session) override;
class __SMTP_VRFY : public SMTPCommand {
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
};
}

View File

@ -29,7 +29,7 @@ int main(int argc, char **argv) {
mail::IMAPServer imapServer(ePoll, hostName, mailFileSystem, core::IPAddress(ipAddress, 143));
core::ConsoleServer consoleServer(ePoll, core::IPAddress(ipAddress, 1027));
consoleServer.commands.add(consoleServer.commands, "help");
ePoll.start(8, 1000);
ePoll.start(2, 1000);
while(true)
sleep(300);