/*
 * Copyright (C) The Apache Software Foundation. All rights reserved.
 *
 * This software is published under the terms of the Apache Software License
 * version 1.1, a copy of which has been included with this distribution in
 * the LICENSE file.
 */
package org.apache.james.transport.mailets;

import org.apache.mailet.GenericMailet;
import org.apache.mailet.Mail;
import org.apache.mailet.MailAddress;

import javax.mail.Address;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.HashSet;
import java.util.Set;

/**
 * Returns the current time for the mail server.  Sample configuration:
 * <mailet match="RecipientIs=time@cadenza.lokitech.com" class="ServerTime">
 * </mailet>
 *
 * @author  Serge Knystautas <sergek@lokitech.com>
 */
public class ServerTime extends GenericMailet {
    /**
     * Sends a message back to the sender indicating what time the server thinks it is.
     *
     * @param mail the mail being processed
     *
     * @throws MessagingException if an error is encountered while formulating the reply message
     */
    public void service(Mail mail) throws javax.mail.MessagingException {
        MimeMessage response = (MimeMessage)mail.getMessage().reply(false);
        response.setSubject("The time is now...");
        StringBuffer textBuffer =
            new StringBuffer(128)
                    .append("This mail server thinks it's ")
                    .append((new java.util.Date()).toString())
                    .append(".");
        response.setText(textBuffer.toString());

        Set recipients = new HashSet();
        Address addresses[] = response.getAllRecipients();
        for (int i = 0; i < addresses.length; i++) {
            recipients.add(new MailAddress((InternetAddress)addresses[0]));
        }

        MailAddress sender = new MailAddress((InternetAddress)response.getFrom()[0]);
        getMailetContext().sendMail(sender, recipients, response);
    }

    /**
     * Return a string describing this mailet.
     *
     * @return a string describing this mailet
     */
    public String getMailetInfo() {
        return "ServerTime Mailet";
    }
}


@@ -1582,196 +1582,400 @@
     
-Set recipients = new HashSet();%0A        Address addresses%5B%5D = response.getAllRecipients();%0A        for (int i = 0; i %3C addresses.length; i++) %7B%0A            recipients.add(new MailAddress((
+// Someone manually checking the server time by hand may send%0A        // an formatted message, lacking From and To headers.  If the%0A        // response fields are null, try setting them from the SMTP%0A        // MAIL FROM/RCPT TO commands used to send the inquiry.%0A%0A        if (response.getFrom() == null) %7B%0A            response.setFrom(((MailAddress)mail.getRecipients().iterator().next()).to
 Inte
@@ -1989,21 +1989,9 @@
 ress
-)addresses%5B0%5D
+(
 ));%0A
@@ -2013,83 +2013,186 @@
     
-MailAddress sender
+if (response.getAllRecipients()
  =
+=
  n
-ew MailAddress((InternetAddress)response.getFrom()%5B0%5D
+ull) %7B%0A            response.setRecipients(MimeMessage.RecipientType.TO, mail.getSender().toString());%0A        %7D%0A%0A        response.saveChanges(
 );%0A 
@@ -2230,28 +2230,8 @@
 ail(
-sender, recipients, 
 resp

Use addresses from MAIL FROM/RCPT TO if headers are missing. Start to use saveChanges(), since we'll eventually remove the internal 'convenience' calls.