Conversion of MailFileSystem object to MString.
This commit is contained in:
parent
8e7834a320
commit
09c6163a74
@ -4,40 +4,38 @@
|
||||
|
||||
namespace mail {
|
||||
|
||||
MailFileSystem::MailFileSystem(std::string mailPath) : mailPath(mailPath) {}
|
||||
|
||||
bool MailFileSystem::ifMailBoxExists(std::string mailbox) {
|
||||
MailFileSystem::MailFileSystem(coreutils::ZString &mailPath) : mailPath(mailPath) {}
|
||||
|
||||
bool MailFileSystem::ifMailBoxExists(coreutils::ZString &mailbox) {
|
||||
std::cout << "{" << mailbox << "}" << std::endl;
|
||||
|
||||
if(stat(getMailBoxPath(mailbox).c_str(), &statbuf) != -1)
|
||||
if(S_ISDIR(statbuf.st_mode))
|
||||
return true;
|
||||
if(stat(getMailBoxPath(mailbox).c_str(), &statbuf) != -1)
|
||||
if(S_ISDIR(statbuf.st_mode))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool MailFileSystem::ifAliasExists(std::string alias) {
|
||||
if(stat(getMailBoxPath(alias).c_str(), &statbuf) != -1)
|
||||
if(S_ISREG(statbuf.st_mode))
|
||||
return true;
|
||||
|
||||
bool MailFileSystem::ifAliasExists(coreutils::ZString &alias) {
|
||||
if(stat(getMailBoxPath(alias).c_str(), &statbuf) != -1)
|
||||
if(S_ISREG(statbuf.st_mode))
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
std::string MailFileSystem::getMailBoxPath(std::string mailbox) {
|
||||
coreutils::ZString email(mailbox);
|
||||
email.split("@", 1);
|
||||
std::cout << email[0] << "-" << email[1] << std::endl;
|
||||
std::string path = mailPath + "/" + email[1].str() + "/" + email[0].str();
|
||||
|
||||
coreutils::MString MailFileSystem::getMailBoxPath(coreutils::ZString &mailbox) {
|
||||
mailbox.split("@", 1);
|
||||
std::cout << mailbox[0] << "-" << mailbox[1] << std::endl;
|
||||
coreutils::MString path(mailPath + "/" + mailbox[1] + "/" + mailbox[0]);
|
||||
std::cout << "{" << path << "}" << std::endl;
|
||||
return path;
|
||||
}
|
||||
|
||||
std::vector<std::string> MailFileSystem::getAliasMailboxes(std::string alias) {
|
||||
std::vector<std::string> returnValue;
|
||||
|
||||
std::vector<coreutils::MString> MailFileSystem::getAliasMailboxes(coreutils::ZString &alias) {
|
||||
std::vector<coreutils::MString> returnValue;
|
||||
return returnValue;
|
||||
}
|
||||
|
||||
std::string MailFileSystem::getMailPath() {
|
||||
}
|
||||
|
||||
coreutils::ZString& MailFileSystem::getMailPath() {
|
||||
return mailPath;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,29 +1,29 @@
|
||||
#ifndef __MailFileSystem_h__
|
||||
# define __MailFileSystem_h__
|
||||
|
||||
# include "ZString.h"
|
||||
# include "MString.h"
|
||||
# include <sys/types.h>
|
||||
# include <sys/stat.h>
|
||||
# include <unistd.h>
|
||||
|
||||
namespace mail {
|
||||
|
||||
|
||||
class MailFileSystem {
|
||||
|
||||
public:
|
||||
MailFileSystem(std::string mailPath);
|
||||
bool ifMailBoxExists(std::string mailbox);
|
||||
bool ifAliasExists(std::string alias);
|
||||
std::string getMailBoxPath(std::string mailbox);
|
||||
std::vector<std::string> getAliasMailboxes(std::string alias);
|
||||
std::string getMailPath();
|
||||
|
||||
MailFileSystem(coreutils::ZString &mailPath);
|
||||
bool ifMailBoxExists(coreutils::ZString &mailbox);
|
||||
bool ifAliasExists(coreutils::ZString &alias);
|
||||
coreutils::MString getMailBoxPath(coreutils::ZString &mailbox);
|
||||
std::vector<coreutils::MString> getAliasMailboxes(coreutils::ZString &alias);
|
||||
coreutils::ZString& getMailPath();
|
||||
|
||||
private:
|
||||
std::string mailPath;
|
||||
coreutils::ZString &mailPath;
|
||||
struct stat statbuf;
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
@ -1,56 +1,56 @@
|
||||
#include "__SMTP_AUTH.h"
|
||||
#include "Base64.h"
|
||||
#include "SMTPServer.h"
|
||||
|
||||
namespace mail {
|
||||
|
||||
|
||||
int __SMTP_AUTH::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
||||
|
||||
switch(session.authState) {
|
||||
|
||||
|
||||
switch(session.authState) {
|
||||
|
||||
case USER_UNKNOWN:
|
||||
if(request[1].equals("LOGIN")) {
|
||||
session.out << "334 VXNlcm5hbWU6" << CRLF;
|
||||
session.out << "334 VXNlcm5hbWU6" << CRLF;
|
||||
// setTimer(10.0f);
|
||||
session.authState = USER_QUERY;
|
||||
session.authState = USER_QUERY;
|
||||
grabInput(session);
|
||||
}
|
||||
else {
|
||||
session.out << "504 AUTH method not supported." << CRLF;
|
||||
session.out << "504 AUTH method not supported." << CRLF;
|
||||
}
|
||||
return 1;
|
||||
|
||||
|
||||
case USER_QUERY:
|
||||
session.userName = request[0].str();
|
||||
session.userName = request[0];
|
||||
// setTimer(0.0f);
|
||||
session.out << "334 UGFzc3dvcmQ6" << CRLF;
|
||||
session.out << "334 UGFzc3dvcmQ6" << CRLF;
|
||||
// setTimer(10.0f);
|
||||
session.authState = USER_SECRET_QUERY;
|
||||
return 1;
|
||||
|
||||
session.authState = USER_SECRET_QUERY;
|
||||
return 1;
|
||||
|
||||
case USER_SECRET_QUERY:
|
||||
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;
|
||||
}
|
||||
session.password = request[0];
|
||||
// setTimer(0.0f);
|
||||
coreutils::Base64 base64;
|
||||
if(authLogin(session.userName, session.password, server)) {
|
||||
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) {
|
||||
// std::string secretPath = server.mailFileSystem.getMailBoxPath(userName) + "/.password";
|
||||
// coreutils::File secret(secretPath);
|
||||
// secret.read();
|
||||
// return secret.asString() == password;
|
||||
return true;
|
||||
|
||||
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 password == secret.asString();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -9,8 +9,8 @@ namespace mail {
|
||||
|
||||
public:
|
||||
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
|
||||
bool authLogin(coreutils::ZString userName, coreutils::ZString password);
|
||||
|
||||
bool authLogin(coreutils::ZString &userName, coreutils::ZString &password, SMTPServer &server);
|
||||
|
||||
};
|
||||
|
||||
}
|
||||
|
@ -5,85 +5,85 @@
|
||||
#include <iomanip>
|
||||
|
||||
namespace mail {
|
||||
|
||||
|
||||
int __SMTP_DATA::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
||||
|
||||
switch(session.mode) {
|
||||
|
||||
|
||||
case WAIT_FOR_DATA:
|
||||
|
||||
|
||||
switch(session.state) {
|
||||
|
||||
|
||||
case RCPT:
|
||||
session.out << "354 Enter the mail message terminated by <CRLF>.<CRLF>" << CRLF;
|
||||
session.out << "354 Enter the mail message terminated by <CRLF>.<CRLF>" << CRLF;
|
||||
session.mailData.str("");
|
||||
session.mode = RECEIVE_DATA;
|
||||
grabInput(session);
|
||||
grabInput(session);
|
||||
// session.setTimer(120.0f);
|
||||
break;
|
||||
|
||||
|
||||
case MAIL:
|
||||
session.out << "503 Please use RCPT first" << CRLF;
|
||||
session.out << "503 Please use RCPT first" << CRLF;
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
session.out << "503 Please use MAIL first" << CRLF;
|
||||
session.out << "503 Please use MAIL first" << CRLF;
|
||||
break;
|
||||
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
|
||||
case RECEIVE_DATA:
|
||||
if(request != ".")
|
||||
session.mailData << request << CRLF;
|
||||
else {
|
||||
session.mode = WAIT_FOR_DATA;
|
||||
if(request != ".")
|
||||
session.mailData << request << 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) {
|
||||
// if(filterMessage(session.mailData)) {
|
||||
if(session.recipientList.size() > 0) {
|
||||
if(session.mailData.str().size() > 0) {
|
||||
coreutils::MString ID = queueMail(server, session.sender, session.recipientList, session.mailData);
|
||||
if(ID != "") {
|
||||
if(ID != "") {
|
||||
coreutils::Log(coreutils::LOG_INFO) << "Queued message " << ID << ".";
|
||||
session.out << "250 OK Queued message " << ID << CRLF;
|
||||
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
|
||||
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;
|
||||
break;
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
coreutils::MString __SMTP_DATA::generateMailFileName() {
|
||||
struct timespec timex;
|
||||
clock_gettime(CLOCK_REALTIME, &timex);
|
||||
std::stringstream temp;
|
||||
temp << timex.tv_sec << "." << std::setfill('0') << std::setw(9) << timex.tv_nsec;
|
||||
return coreutils::MString(temp.str());
|
||||
}
|
||||
|
||||
temp << timex.tv_sec << "." << std::setfill('0') << std::setw(9) << timex.tv_nsec;
|
||||
return coreutils::MString(temp.str());
|
||||
}
|
||||
|
||||
coreutils::MString __SMTP_DATA::queueMail(SMTPServer &server, coreutils::MString &sender, std::vector<coreutils::MString> &recipientList, std::stringstream &mailData) {
|
||||
coreutils::MString fileName(server.mailFileSystem.getMailPath() + "/.queue/" + generateMailFileName().str());
|
||||
coreutils::File mailFile(fileName, O_CREAT | O_WRONLY, 0660);
|
||||
mailFile.write(mailData.str());
|
||||
|
||||
mailFile.write(mailData.str());
|
||||
|
||||
for(coreutils::MString recipient: recipientList) {
|
||||
coreutils::MString newName(fileName.write(recipient));
|
||||
link(fileName.c_str(), newName.c_str());
|
||||
}
|
||||
|
||||
|
||||
unlink(fileName.c_str());
|
||||
|
||||
|
||||
return fileName;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -9,7 +9,7 @@ namespace mail {
|
||||
class __SMTP_DATA : public SMTPCommand {
|
||||
|
||||
public:
|
||||
|
||||
|
||||
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
|
||||
|
||||
coreutils::MString generateMailFileName();
|
||||
|
@ -1,30 +1,30 @@
|
||||
#include "__SMTP_MAIL.h"
|
||||
|
||||
namespace mail {
|
||||
|
||||
|
||||
int __SMTP_MAIL::processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) {
|
||||
|
||||
if(request[1].ifNext("FROM:")) {
|
||||
|
||||
if(request[1].ifNext("FROM:")) {
|
||||
request[1].skipWhitespace();
|
||||
if(request[1].ifNext("<")) {
|
||||
session.sender = request[1].getTokenExclude(">");
|
||||
session.sender = request[1].getTokenExclude(">");
|
||||
if(session.authState = USER_KNOWN) {
|
||||
session.out << "250 OK" << CRLF;
|
||||
session.recipientList.clear();
|
||||
session.state = MAIL;
|
||||
}
|
||||
} else
|
||||
session.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
|
||||
} else
|
||||
session.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
|
||||
} else
|
||||
session.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
|
||||
} else
|
||||
session.out << "550 Usage: MAIL FROM:<email-address>" << CRLF;
|
||||
return 1;
|
||||
}
|
||||
|
||||
|
||||
std::string domainOnly(std::string email) {
|
||||
coreutils::ZString split(email);
|
||||
split.split("@");
|
||||
return split[1].str();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
@ -4,15 +4,13 @@
|
||||
# include "SMTPCommand.h"
|
||||
|
||||
namespace mail {
|
||||
|
||||
// class SMTPServer;
|
||||
|
||||
|
||||
class __SMTP_MAIL : public SMTPCommand {
|
||||
|
||||
|
||||
public:
|
||||
int processCommand(coreutils::ZString &request, SMTPSession &session, SMTPServer &server) override;
|
||||
};
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
Loading…
x
Reference in New Issue
Block a user