/*
 * 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.MessagingException;
import java.util.Collection;
import java.util.HashSet;
import java.util.StringTokenizer;

/**
 * Replace incoming recipient with specified ones.
 *
 * @author Federico Barbieri <scoobie@pop.systemy.it>
 * @author Serge Knystautas <sergek@lokitech.com>
 */
public class Forward extends GenericMailet {

    private Collection newRecipients;

    public void init() throws MessagingException {
        newRecipients = new HashSet();
        StringTokenizer st = new StringTokenizer(getMailetConfig().getInitParameter("forwardto"), ",", false);
        while (st.hasMoreTokens()) {
            newRecipients.add(new MailAddress(st.nextToken()));
        }
    }

    public void service(Mail mail) throws MessagingException {
        getMailetContext().sendMail(mail.getSender(), newRecipients, mail.getMessage());
       if(! (new Boolean(getInitParameter("passThrough"))).booleanValue()) {
            mail.setState(Mail.GHOST);
       }
    }

    public String getMailetInfo() {
        return "Forward Mailet";
    }
}


@@ -544,16 +544,17 @@
  Replace
+s
  incomin
@@ -564,22 +564,29 @@
 ecipient
+s
  with 
+those 
 specifie
@@ -590,13 +590,8 @@
 fied
- ones
 .%0A *
@@ -1177,89 +1177,1166 @@
     
- getMailetContext().sendMail(mail.getSender(), newRecipients, mail.getMessage());
+if (mail.getSender() == null %7C%7C getMailetContext().getMailServers(mail.getSender().getHost()).size() != 0) %7B%0A           // If we do not do this check, and somone uses Forward in a%0A           // processor initiated by SenderInFakeDomain, then a fake%0A           // sender domain will cause an infinite loop (the forwarded%0A           // e-mail still appears to come from a fake domain).%0A           // Although this can be viewed as a configuration error, the%0A           // consequences of such a mis-configuration are severe enough%0A           // to warrant protecting against the infinite loop.%0A           getMailetContext().sendMail(mail.getSender(), newRecipients, mail.getMessage());%0A       %7D%0A       else %7B%0A           StringBuffer logBuffer = new StringBuffer(256)%0A                                   .append(%22Forward mailet cannot forward %22)%0A                                   .append(mail)%0A                                   .append(%22. Invalid sender domain for %22)%0A                                   .append(mail.getSender())%0A                                   .append(%22. Consider using the Redirect mailet.%22);%0A           log(logBuffer.toString());%0A       %7D
 %0A   

Check for valid sender address before forwarding.