Implemented 'deleteMessage'

This commit is contained in:
Tristan B. Kildaire 2020-07-27 21:10:23 +02:00
parent 1e8cd9117d
commit 478cafd229
4 changed files with 95 additions and 4 deletions

View File

@ -52,6 +52,10 @@ private ButterflyListener[] constructListeners(JSONValue listenersBlock)
{
listeners ~= new IPv4Listener(listener, listenersBlock[listener]);
}
else if(cmp(listenersBlock[listener]["type"].str(), "ipv6") == 0)
{
listeners ~= new IPv6Listener(listener, listenersBlock[listener]);
}
writeln("Listener \"" ~ listener ~ "\" constructed");
}

View File

@ -179,6 +179,30 @@ public final class ButterflyClient : Thread
/* TODO: Add error handling */
}
}
else if(cmp(command, "editMail") == 0)
{
/* Make sure the connection is from a client */
if(connectionType == ClientType.CLIENT)
{
/* Get the mail block */
JSONValue mailBlock = commandBlock["request"]["mail"];
/* Get the folder the mail message wanting to be edited resides in */
Folder storeFolder = new Folder(mailbox, commandBlock["request"]["folder"].str());
/* Get the mail message wanting to be edited */
Mail messageOriginal = new Mail(mailbox, storeFolder, commandBlock["request"]["mailID"].str());
/* Update the message with the new data */
Mail updatedMail = editMail(messageOriginal, storeFolder, mailBlock);
responseBlock["response"]["mailID"] = updatedMail.getMailID();
}
else
{
/* TODO: Add error handling */
}
}
else if(cmp(command, "deliverMail") == 0)
{
/* Make sure the connection is from a server */
@ -380,6 +404,23 @@ public final class ButterflyClient : Thread
return savedMail;
}
/**
* Updates the given mail message in the
* provided folder with a new message.
*/
private Mail editMail(Mail messageOriginal, Folder storeFolder, JSONValue mailBlock)
{
Mail updatedMail;
/* Delete the old message */
mailbox.deleteMessage(storeFolder, messageOriginal.getMailID());
/* Store the new message in the same folder */
updatedMail = Mail.createMail(mailbox, storeFolder, mailBlock);
return updatedMail;
}
private bool authenticate(string username, string password)
{
@ -716,4 +757,4 @@ public final class ButterflyClient : Thread
writeln("Saved mail message to sent folder");
}
}
}

View File

@ -94,7 +94,11 @@ public final class Mailbox
public void deleteMessage(Folder folder, string mailID)
{
/* TODO: Implement me */
/* Generate the filename to store the message under */
string filename = "mailboxes/"~username~"/"~folder.folderPath~"/"~mailID;
/* Delete the mail message */
remove(filename);
}
public void deleteMailbox()
@ -343,4 +347,4 @@ public final class Mail
{
return mailID;
}
}
}

View File

@ -7,6 +7,8 @@ import std.json : JSONValue;
import client.client;
import std.conv : to;
/* TODO: Enforce IPv4 on address */
public class IPv4Listener : ButterflyListener
{
@ -44,4 +46,44 @@ public class IPv4Listener : ButterflyListener
client.start();
}
}
}
}
/* TODO: Enforce IPv6 on address */
public class IPv6Listener : ButterflyListener
{
private Socket serverSocket;
this(string name, JSONValue config)
{
super(name, config["domain"].str(), config);
Address bindAddress = parseAddress(config["address"].str(), to!(ushort)(config["port"].str()));
/**
* Instantiate a new Socket for the given Address
* `bindAddress` of which it will bind to.
*/
serverSocket = new Socket(bindAddress.addressFamily, SocketType.STREAM, ProtocolType.TCP);
serverSocket.bind(bindAddress);
}
public override void run()
{
serverSocket.listen(1); //TODO: backlog
while(true)
{
/* Accept the queued connection */
Socket clientConnection = serverSocket.accept();
ButterflyClient client = new ButterflyClient(this, clientConnection);
/* Start the client handler */
client.start();
}
}
}