Reworked mail message handling.

Implemented mail delivery on (for now) port 6969 (therefore all servers mjust listen on it).
Reworked mail delivery (as in receiving as the server).
This commit is contained in:
Tristan B. Kildaire 2020-06-18 15:58:59 +02:00
parent 3d03450296
commit 433b4dca6f
2 changed files with 59 additions and 38 deletions

View File

@ -132,7 +132,7 @@ public final class ButterflyClient : Thread
Folder storeFolder = new Folder(mailbox, commandBlock["request"]["folder"].str());
/* Store the message in the mailbox */
storeMail(storeMail, mailBlock);
storeMail(storeFolder, mailBlock);
}
else
{
@ -145,8 +145,7 @@ public final class ButterflyClient : Thread
if(connectionType == ClientType.SERVER)
{
/* TODO: Implement me */
Mail mail = new Mail(commandBlock["request"]["mail"]);
deliverMail(mail);
deliverMail(commandBlock["request"]["mail"]);
}
else
{
@ -312,10 +311,14 @@ public final class ButterflyClient : Thread
/**
* Delivers the mail to the local users
*/
private void deliverMail(Mail mail)
private void deliverMail(JSONValue mailBlock)
{
/* Get a list of the recipients of the mail message */
string[] recipients = mail.getRecipients();
string[] recipients;
foreach(JSONValue recipient; mailBlock["recipients"].array())
{
recipients ~= recipient.str();
}
/* Store the mail to each of the recipients */
foreach(string recipient; recipients)
@ -341,10 +344,10 @@ public final class ButterflyClient : Thread
Mailbox userMailbox = new Mailbox(username);
/* Get the Inbox folder */
// Folder inboxFolder = new Folder(userMailbox, null, "Inbox");
Folder inboxFolder = new Folder(userMailbox, "Inbox");
/* Store the message in their Inbox folder */
// userMailbox.storeMessage(inboxFolder, mail);
Mail.createMail(userMailbox, inboxFolder, mailBlock);
writeln("Stored mail message");
}
@ -355,10 +358,14 @@ public final class ButterflyClient : Thread
* Sends the mail message `mail` to the servers
* listed in the recipients field.
*/
private void sendMail(Mail mail)
private void sendMail(JSONValue mailBlock)
{
/* Get a list of the recipients of the mail message */
string[] recipients = mail.getRecipients();
string[] recipients;
foreach(JSONValue recipient; mailBlock["recipients"].array())
{
recipients ~= recipient.str();
}
/* Send the mail to each of the recipients */
foreach(string recipient; recipients)
@ -384,10 +391,10 @@ public final class ButterflyClient : Thread
Mailbox userMailbox = new Mailbox(username);
/* Get the Inbox folder */
// Folder inboxFolder = new Folder(userMailbox, null, "Inbox");
Folder inboxFolder = new Folder(userMailbox, "Inbox");
/* Store the message in their Inbox folder */
// userMailbox.storeMessage(inboxFolder, mail);
Mail.createMail(userMailbox, inboxFolder, mailBlock);
}
else
{
@ -399,11 +406,15 @@ public final class ButterflyClient : Thread
*/
JSONValue messageBlock;
messageBlock["command"] = "deliverMail";
JSONValue mailBlock;
mailBlock["recipients"] = null; /* TODO: Get JSON array of strings */
mailBlock["message"] = mail.getMessage();
JSONValue requestBlock;
requestBlock["mail"] = mailBlock;
messageBlock["request"]["mail"] = mailBlock;
import std.socket : AddressFamily, SocketType, ProtocolType, parseAddress, Address;
/* Deliver the mail to the remote server */
Socket remoteServer = new Socket(AddressFamily.INET, SocketType.STREAM, ProtocolType.TCP);
remoteServer.connect(parseAddress(domain, 6969));
sendMessage(remoteServer, cast(byte[])toJSON(messageBlock));
writeln("Message delivered to server "~domain);
}
writeln("Sent mail message");

View File

@ -83,6 +83,11 @@ public final class Mailbox
/* TODO: Store the message in file `filename` */
}
public void deleteMessage(Folder folder, string mailID)
{
/* TODO: Implement me */
}
public void deleteMailbox()
{
/* TODO: Run deletion on all folders */
@ -229,11 +234,20 @@ public final class Folder
public final class Mail
{
/**
* The associated Mailbox
*/
/**
* The associated Mailbox
*/
private Mailbox mailbox;
private string id;
/**
* The associated Folder
*/
private Folder folder;
/**
* The mail message's name
*/
private string mailID;
public static Mail createMail(Mailbox mailbox, Folder folder, JSONValue mailBlock)
{
@ -261,28 +275,16 @@ public final class Mail
return "fhjdkshfjsdgfjk";
}
this(Mailbox mailbox, Folder folder, string id)
this(Mailbox mailbox, Folder folder, string mailID)
{
this.id = id;
/* TODO: Fetch mail here, or rather in a method */
this.mailbox = mailbox;
this.folder = folder;
this.mailID = mailID;
}
this(JSONValue mailBlock)
private void deleteMessage()
{
/* TODO: */
this.messageBlock = mailBlock["message"];
/* Populate the array of recipients */
JSONValue[] recipientArray = mailBlock["recipients"].array();
foreach(JSONValue recipient; recipientArray)
{
recipients ~= recipient.str();
}
/* Perform a sanity check on the mail message */
sanityCheck();
mailbox.deleteMessage(folder, mailID);
}
private void sanityCheck()
@ -292,12 +294,20 @@ public final class Mail
public string[] getRecipients()
{
string[] recipients;
/* TODO: Implement me */
return recipients;
}
public JSONValue getMessage()
{
JSONValue messageBlock;
/* TODO: Implement me */
return messageBlock;
}
}