/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you under the Apache License, Version 2.0 (the            *
 * "License"); you may not use this file except in compliance   *
 * with the License.  You may obtain a copy of the License at   *
 *                                                              *
 *   http://www.apache.org/licenses/LICENSE-2.0                 *
 *                                                              *
 * Unless required by applicable law or agreed to in writing,   *
 * software distributed under the License is distributed on an  *
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY       *
 * KIND, either express or implied.  See the License for the    *
 * specific language governing permissions and limitations      *
 * under the License.                                           *
 ****************************************************************/



package org.apache.james.transport.mailets;

import javax.mail.Address;
import javax.mail.internet.MimeMessage;

import org.apache.mailet.Experimental;
import org.apache.mailet.Mail;
import org.apache.mailet.base.GenericMailet;

import com.github.fge.lambdas.Throwing;

/**
 * Returns the current time for the mail server.  Sample configuration:
 * <pre><code>
 * &lt;mailet match="RecipientIs=time@cadenza.lokitech.com" class="ServerTime"&gt;
 * &lt;/mailet&gt;
 * </code></pre>
 *
 */
@Experimental
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 javax.mail.MessagingException if an error is encountered while formulating the reply message
     */
    @Override
    public void service(Mail mail) throws javax.mail.MessagingException {
        if (!mail.hasSender()) {
            return;
        }

        MimeMessage response = (MimeMessage)mail.getMessage().reply(false);
        response.setSubject("The time is now...");
        String textBuffer = "This mail server thinks it's " + (new java.util.Date()).toString() + ".";
        response.setText(textBuffer);

        // Someone manually checking the server time by hand may send
        // an formatted message, lacking From and To headers.  If the
        // response fields are null, try setting them from the SMTP
        // MAIL FROM/RCPT TO commands used to send the inquiry.

        if (response.getFrom() == null) {
            mail.getRecipients().iterator().next().toInternetAddress()
                .ifPresent(Throwing.<Address>consumer(response::setFrom).sneakyThrow());
        }

        if (response.getAllRecipients() == null) {
            response.setRecipients(MimeMessage.RecipientType.TO, mail.getMaybeSender().get().toString());
        }

        response.saveChanges();
        getMailetContext().sendMail(response);
    }

    @Override
    public String getMailetInfo() {
        return "ServerTime Mailet";
    }
}

