package com.tableausoftware.tabcmd.commands;

import com.tableausoftware.tabcmd.http.HttpRequest;
import com.tableausoftware.tabcmd.session.Session;
import com.tableausoftware.core.util.SMTPClientWithFile;

import javax.mail.MessagingException;
import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;


public class SendEmail extends Command {

    public String getName() {
        return "sendmail";
    }

    public String getUsage() {
        return "tabcmd sendmail [options]";
    }

    public String getShortDescription() {
        return "Send email from command line using SMTP server.";
    }

    public String getDescription() {
        return getShortDescription();
    }

    public boolean needsSession() {
        return false;
    }

    public HttpRequest execute(Session session, CommandLine commandLine, List<String> remainingArgs) {
        int port = 25;

        if (commandLine.getOptionValue("smtp-port") != null)
            port = Integer.parseInt(commandLine.getOptionValue("smtp-port"));

        try {
            SMTPClientWithFile client = new SMTPClientWithFile(
                    commandLine.getOptionValue("smtp-user"),
                    commandLine.getOptionValue("smtp-passw"),
                    commandLine.getOptionValue("smtp-host"),
                    port,
                    true);

            client.sendMailWithFile(
                    commandLine.getOptionValue("to"),
                    commandLine.getOptionValue("from"),
                    commandLine.getOptionValue("subject"),
                    commandLine.getOptionValue("body"),
                    commandLine.getOptionValue("file"));

        } catch (MessagingException ex) {
            m_logger.debug(ex);
            throw new RuntimeException(ex);
        }
        return null;
    }


    public Options getOptions(Options options) {
        OptionBuilder.withLongOpt("smtp-host");
        OptionBuilder.withDescription("SMTP Server's address");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("SMTPSERVER");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("to");
        OptionBuilder.withDescription("Recipient addresses, separated by semicolon (;) character");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("TO_EMAIL");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("from");
        OptionBuilder.withDescription("From address");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("FROM_EMAIL");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("subject");
        OptionBuilder.withDescription("Subject of the email");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("TO_EMAIL");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("file");
        OptionBuilder.withDescription("File to include");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("FILENAME");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("body");
        OptionBuilder.withDescription("Body Text");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("EMAIL_TEXT_BODY");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("smtp-port");
        OptionBuilder.withDescription("SMTP Server's port for incoming communication");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("PORT");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("smtp-user");
        OptionBuilder.withDescription("User name for SMTP authentication");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("USER");
        options.addOption(OptionBuilder.create(""));

        OptionBuilder.withLongOpt("smtp-passw");
        OptionBuilder.withDescription("SMTP User's password");
        OptionBuilder.hasArg();
        OptionBuilder.withArgName("PASSWORD");
        options.addOption(OptionBuilder.create(""));

        return options;
    }
}

@@ -2086,32 +2086,72 @@
 ilder.hasArg();%0A
+        OptionBuilder.isRequired(true);%0A
         OptionBu
@@ -2222,34 +2222,32 @@
 nBuilder.create(
-%22%22
 ));%0A%0A        Opt
@@ -2400,32 +2400,72 @@
 ilder.hasArg();%0A
+        OptionBuilder.isRequired(true);%0A
         OptionBu
@@ -2534,34 +2534,32 @@
 nBuilder.create(
-%22%22
 ));%0A%0A        Opt
@@ -2669,32 +2669,72 @@
 ilder.hasArg();%0A
+        OptionBuilder.isRequired(true);%0A
         OptionBu
@@ -2805,34 +2805,32 @@
 nBuilder.create(
-%22%22
 ));%0A%0A        Opt
@@ -2951,32 +2951,72 @@
 ilder.hasArg();%0A
+        OptionBuilder.isRequired(true);%0A
         OptionBu
@@ -3085,34 +3085,32 @@
 nBuilder.create(
-%22%22
 ));%0A%0A        Opt
@@ -3223,32 +3223,72 @@
 ilder.hasArg();%0A
+        OptionBuilder.isRequired(true);%0A
         OptionBu
@@ -3357,34 +3357,32 @@
 nBuilder.create(
-%22%22
 ));%0A%0A        Opt
@@ -3489,32 +3489,72 @@
 ilder.hasArg();%0A
+        OptionBuilder.isRequired(true);%0A
         OptionBu
@@ -3630,34 +3630,32 @@
 nBuilder.create(
-%22%22
 ));%0A%0A        Opt
@@ -3803,32 +3803,73 @@
 ilder.hasArg();%0A
+        OptionBuilder.isRequired(false);%0A
         OptionBu
@@ -3934,34 +3934,32 @@
 nBuilder.create(
-%22%22
 ));%0A%0A        Opt
@@ -4095,32 +4095,73 @@
 ilder.hasArg();%0A
+        OptionBuilder.isRequired(false);%0A
         OptionBu
@@ -4226,34 +4226,32 @@
 nBuilder.create(
-%22%22
 ));%0A%0A        Opt
@@ -4375,32 +4375,73 @@
 ilder.hasArg();%0A
+        OptionBuilder.isRequired(false);%0A
         OptionBu
@@ -4467,24 +4467,24 @@
 PASSWORD%22);%0A
+
         opti
@@ -4522,10 +4522,8 @@
 ate(
-%22%22
 ));%0A

Indicate optional arguments