Chris Pollett > Students >
Ajita

    ( Print View)

    [Bio]

    [Blog]

    [C297 Proposal]

    [Deliverable 1]

    [Deliverable 2]

    [Deliverable 3]

    [Deliverable 4]

    [Presentation 1 - PDF]

    [Presentation 2 - PDF]

    [Presentation 3 - PDF]

    [Final Report 297 - PDF]

    [C298 Proposal]

Deliverable 3 - Feature Addition: MailSite

Deliverable 3 - Feature Addition: MailSite

Description: Another command feature that can be added to MailSite.php is the HELP command. This command causes the server to send helpful information to the client. The command may take an argument (e.g., any command name) and return more specific information as a response. This command has no effect on the reverse-path buffer, the forward-path buffer, or the mail data buffer, and it may be issued at anytime. A response has the following format:

The HELP Command


    214- Available commands:

        [COMMAND 1] 

         <--description-->

        [COMMAND 2] 

         <--description-->
         .
         .
         .

    214 End of HELP response

Step-1: Creating a JSON list of all commands and their descriptions:

  • JSON is the best format when it comes to saving type information in a file.
  • A JSON file 'cmds.json' is added into the codebase that contains a list of existing commands and their descriptions.

Sample JSON : cmds.json


    {
        "Cmd 1": "...description...",
        "Cmd 2": "...description...",
        "Cmd 3": "...description...",
        "Cmd 4": "...description..."
    }

Step-2: Implementing the main function

This function serves to read the JSON file generated previously, decoding its contents into a printable string format. Subsequently, it incorporates all key-value pairs into this parsed string and proceeds to return it. Furthermore, the function diligently manages any file-related errors that may arise during its execution.

parseHelp()

returns the response string created with the response code added.

214- Available commands: + <---commands---> + 214 End of HELP response

Step-3: Integration into parseRequest

The integration process entails merging the aforementioned function into the parseRequest function, complemented by meticulous checks to ensure seamless operation. Subsequently, the response generated by the function is appended to the 'out_stream', facilitating its transmission back to the client for further processing.

Step-4: Integration into ProcessRequestStreams

The process involves updating the processRequestStreams function, incorporating pertinent checks to accommodate the newly introduced command effectively. Additionally, the resultant data stream, encapsulated within the 'out_stream', is then forwarded back to the client, thereby completing the request-response cycle.

Example Code:

<?php
    protected function parseHelp() 
    {
        $filename = '/cmds.json';
        if (!file_exists($filename)) {
            return "Error: File '$filename' not found.";
        }
        $jsonContent = file_get_contents($filename);
        $commands = json_decode($jsonContent, true);
        if ($commands === null) {
            return "Error: Unable to decode JSON from file '$filename'.";
        }
        $helpResponse = "214- Available commands:\r\n";
       
        foreach ($commands as $command => $desc) {
            $helpResponse .= "[$command] \r\n\r\n $desc\r\n\r\n\r\n";
        }
        $helpResponse .= "214 End of HELP response\r\n\r\n";
    
        return $helpResponse;
    }
?>

Results

Result Page Screenshot

Find the full code below:

MailSite Github Project