package net.sf.lab3f.mail;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Properties;
import java.util.Vector;
import java.lang.reflect.Method;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.servlet.ServletException;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.MessagingException;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import org.osgi.service.http.HttpService;
import org.osgi.service.http.HttpContext;
import net.sf.lab3f.util.TuttiFruttiable;
import org.osgi.service.http.HttpService;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletRequest;
public class Main implements Mailable{
private HttpService jetty;
private Properties mailProps = new Properties();
public void start() throws Exception {
mailProps.load(new FileInputStream("conf/mail.properties"));
jetty.registerServlet("/sendmail.cgi", new HttpServlet(){
public void doPost(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
res.setCharacterEncoding("UTF-8");
res.setContentType("text/plain");
String from = req.getParameter("from");
String subj = req.getParameter("subj");
String body = req.getParameter("body");
String domain = req.getParameter("domain");
String ip = req.getRemoteAddr();
res.getWriter().print(sendEmail(from, null, subj, body, domain, ip) == 0 ? "OK" : "");
}
}, null, null);
System.out.println("[INFO] Mailer is started.");
}
public void stop() throws Exception {
jetty.unregister("/sendmail.cgi");
}
public int sendEmail(String from, String subj, String body, String domain, String ip){
return sendEmail(from, null, subj, body, domain);
}
public int sendEmail(String from, String to, String subj, String message, String domain, String ip){
if(ips.contains(ip)) return 1;
new Thread(new IPCleaner(ip)).start();
if(to == null) to = mailProps.getProperty(domain + ".to");
try{
Properties props = new Properties();
props.put("mail.smtp.host", "localhost");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "false");
Authenticator auth = new Authenticator(){
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication("mabel", "m3a4b6e4l8");
}
};
Session sess = Session.getDefaultInstance(props, auth);
sess.setDebug(true);
Message mess = new MimeMessage(sess);
mess.setFrom(new InternetAddress(from));
mess.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
mess.setSubject(subj);
mess.setText(message);
Transport.send(mess);
return 0;
}
catch(Exception ex){ex.printStackTrace(); return 2;}
}
private Vector <String> ips = new Vector <String> ();
private class IPCleaner implements Runnable {
IPCleaner(String s){
ip = s;
ips.add(ip);
}
String ip;
public void run(){
try{Thread.sleep(300000);}
catch(InterruptedException ex){}
ips.remove(ip);
}
}
}