/* *********************************************************************
 *
 *  This file is part of Full Metal Galaxy.
 *  http://www.fullmetalgalaxy.com
 *
 *  Full Metal Galaxy is free software: you can redistribute it and/or 
 *  modify it under the terms of the GNU Affero General Public License
 *  as published by the Free Software Foundation, either version 3 of 
 *  the License, or (at your option) any later version.
 *
 *  Full Metal Galaxy is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public 
 *  License along with Full Metal Galaxy.  
 *  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Copyright 2010, 2011, 2012 Vincent Legendre
 *
 * *********************************************************************/
package com.fullmetalgalaxy.server;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;

import com.fullmetalgalaxy.model.AuthProvider;

/**
 * @author Vincent
 *
 */
public class PMServlet extends HttpServlet
{
  private static final long serialVersionUID = 533579014067656255L;
  private final static FmpLogger log = FmpLogger.getLogger( PMServlet.class.getName() );

  /**
   * 
   */
  public PMServlet()
  {
  }

  /* (non-Javadoc)
   * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
   */
  @Override
  protected void doGet(HttpServletRequest p_req, HttpServletResponse p_resp)
      throws ServletException, IOException
  {
    EbAccount account = ServerUtil.findRequestedAccount(p_req);
    if( account == null ) 
    { 
      p_resp.sendRedirect( "/genericmsg.jsp?title=Le profil n'a pas été trouvé." );
      return;
    }
    String subject = p_req.getParameter("subject");
    p_resp.sendRedirect( account.getEMailUrl( subject ) );
  }

  /* (non-Javadoc)
   * @see javax.servlet.http.HttpServlet#doPost(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
   */
  @Override
  protected void doPost(HttpServletRequest p_request, HttpServletResponse p_response)
      throws ServletException, IOException
  {
    ServletFileUpload upload = new ServletFileUpload();
    try
    {
      // build message to send
      Properties props = new Properties();
      Session session = Session.getDefaultInstance( props, null );
      MimeMessage msg = new MimeMessage( session );
      msg.setSubject( "[FMG] no subject", "text/plain" );
      msg.setSender( new InternetAddress( "admin@fullmetalgalaxy.com", "FMG Admin" ) );
      msg.setFrom( new InternetAddress( "admin@fullmetalgalaxy.com", "FMG Admin" ) );
      
      // Parse the request
      FileItemIterator iter = upload.getItemIterator( p_request );
      while( iter.hasNext() )
      {
        FileItemStream item = iter.next();
        if( item.isFormField() )
        {
          if( "msg".equalsIgnoreCase( item.getFieldName() ) )
          {
            msg.setContent( Streams.asString( item.openStream(), "UTF-8" ), "text/plain" );
          }
          if( "subject".equalsIgnoreCase( item.getFieldName() ) )
          {
            msg.setSubject( "[FMG] " + Streams.asString( item.openStream(), "UTF-8" ), "text/plain" );
          }
          if( "toid".equalsIgnoreCase( item.getFieldName() ) )
          {
            EbAccount account = null;
            try {
              account = FmgDataStore.dao().get( EbAccount.class, Long.parseLong( Streams.asString( item.openStream(), "UTF-8" ) ) );
            } catch(NumberFormatException e) {}
            if( account != null )
            {
              msg.addRecipient( Message.RecipientType.TO, new InternetAddress( account.getEmail(),
                  account.getPseudo() ) );
            }
          }
          if( "fromid".equalsIgnoreCase( item.getFieldName() ) )
          {
            EbAccount account = null;
            try {
              account = FmgDataStore.dao().get( EbAccount.class, Long.parseLong( Streams.asString( item.openStream(), "UTF-8" ) ) );
            } catch(NumberFormatException e) {}
            if( account != null && account.getAuthProvider() == AuthProvider.Google )
            {
              msg.setFrom( new InternetAddress( account.getEmail(), account.getPseudo() ) );
            }
          }
        }
      }

      msg.addRecipients( Message.RecipientType.BCC, InternetAddress.parse( "archive@fullmetalgalaxy.com" ) );
      Transport.send( msg );

    } catch( Exception e )
    {
      log.error( e );
      p_response.sendRedirect( "/genericmsg.jsp?title=Error&text="+e.getMessage() );
      return;
    } 

    p_response.sendRedirect( "/genericmsg.jsp?title=Message envoye" );
  }



}

@@ -3369,16 +3369,46 @@
 ;%0A      
+EbAccount fromAccount = null;%0A
 %0A      /
@@ -4598,46 +4598,8 @@
   %7B%0A
-            EbAccount account = null;%0A
     
@@ -4618,33 +4618,37 @@
 %7B%0A              
-a
+fromA
 ccount = FmgData
@@ -4805,33 +4805,37 @@
             if( 
-a
+fromA
 ccount != null &
@@ -4836,17 +4836,21 @@
 null && 
-a
+fromA
 ccount.g
@@ -4945,33 +4945,37 @@
 nternetAddress( 
-a
+fromA
 ccount.getEmail(
@@ -4977,17 +4977,21 @@
 mail(), 
-a
+fromA
 ccount.g
@@ -5051,16 +5051,447 @@
      %7D%0A%0A
+      // add identity of sender%0A      // TODO we should create mail like pseudo@fullmetalgalaxy2.appspot.com%0A      // instead of this trick%0A      if( fromAccount != null )%0A      %7B%0A        msg.setContent( %22This message is sended by %22 + fromAccount.getPseudo()%0A            + %22, answer here: http://www.fullmetalgalaxy.com/email.jsp?id=%22 + fromAccount.getId()%0A            + %22%5Cn%5Cn%22 + (String)msg.getContent(), %22text/plain%22 );%0A      %7D%0A%0A
       ms

add identity of sender in private message