changeset 228:7580379cdc79

implement basic mail smtp git-svn-id: https://luan-java.googlecode.com/svn/trunk@229 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Wed, 24 Sep 2014 03:39:34 +0000
parents c0f87c1ba99f
children 2a54cb7d1cf4
files core/src/luan/impl/ReturnStmt.java mail/ext/javax.mail.jar mail/src/luan/modules/mail/Mail.luan mail/src/luan/modules/mail/SmtpCon.java
diffstat 4 files changed, 132 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
diff -r c0f87c1ba99f -r 7580379cdc79 core/src/luan/impl/ReturnStmt.java
--- a/core/src/luan/impl/ReturnStmt.java	Wed Jul 23 03:50:57 2014 +0000
+++ b/core/src/luan/impl/ReturnStmt.java	Wed Sep 24 03:39:34 2014 +0000
@@ -26,11 +26,12 @@
 	@Override public void eval(LuanStateImpl luan) throws LuanException {
 		luan.returnValues = expressions.eval(luan);
 		if( tailFnExpr != null ) {
-			LuanFunction tailFn = luan.bit(se).checkFunction( tailFnExpr.eval(luan) );
+			LuanSource.Element seTail = tailFnExpr.se();
+			LuanFunction tailFn = luan.bit(seTail).checkFunction( tailFnExpr.eval(luan) );
 			if( tailFn instanceof Closure ) {
 				luan.tailFn = (Closure)tailFn;
 			} else {
-				luan.returnValues =  luan.bit(tailFnExpr.se()).call(tailFn,tailFnExpr.se().text(),Luan.array(luan.returnValues));
+				luan.returnValues =  luan.bit(seTail).call(tailFn,seTail.text(),Luan.array(luan.returnValues));
 			}
 		}
 		if( throwReturnException )
diff -r c0f87c1ba99f -r 7580379cdc79 mail/ext/javax.mail.jar
Binary file mail/ext/javax.mail.jar has changed
diff -r c0f87c1ba99f -r 7580379cdc79 mail/src/luan/modules/mail/Mail.luan
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mail/src/luan/modules/mail/Mail.luan	Wed Sep 24 03:39:34 2014 +0000
@@ -0,0 +1,19 @@
+import "Package"
+
+if Package.is_blocked "mail/Mail" then
+	error "Mail is blocked"
+end
+
+
+import "Java"
+import "java.lang.System"
+import "luan.modules.mail.SmtpCon"
+
+
+System.setProperty( "mail.mime.charset", "UTF-8" )
+
+
+function Sender(params)
+	assert_table(params)
+	return SmtpCon.new(params).table()
+end
diff -r c0f87c1ba99f -r 7580379cdc79 mail/src/luan/modules/mail/SmtpCon.java
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mail/src/luan/modules/mail/SmtpCon.java	Wed Sep 24 03:39:34 2014 +0000
@@ -0,0 +1,110 @@
+package luan.modules.mail;
+
+import java.util.Properties;
+import javax.mail.Authenticator;
+import javax.mail.PasswordAuthentication;
+import javax.mail.Session;
+import javax.mail.Transport;
+import javax.mail.Message;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeMessage;
+import luan.Luan;
+import luan.LuanState;
+import luan.LuanTable;
+import luan.LuanJavaFunction;
+import luan.LuanException;
+
+
+public final class SmtpCon {
+	private final Session session;
+
+	public SmtpCon(LuanState luan,LuanTable params) throws LuanException {
+		Properties props = new Properties(System.getProperties());
+
+		String host = getString(luan,params,"host");
+		if( host==null )
+			throw luan.exception( "parameter 'host' is required" );
+		props.setProperty("mail.smtp.host",host);
+
+		Object port = params.get("port");
+		if( port != null ) {
+			String s;
+			if( port instanceof String ) {
+				s = (String)port;
+			} else if( port instanceof Number ) {
+				Integer i = Luan.asInteger(port);
+				if( i == null )
+					throw luan.exception( "parameter 'port' is must be an integer" );
+				s = i.toString();
+			} else {
+				throw luan.exception( "parameter 'port' is must be an integer" );
+			}
+			props.setProperty("mail.smtp.socketFactory.port", s);
+			props.setProperty("mail.smtp.port", s);
+		}
+
+		String username = getString(luan,params,"username");
+		if( username == null ) {
+			session = Session.getInstance(props);
+		} else {
+			String password = getString(luan,params,"password");
+			if( password==null )
+				throw luan.exception( "parameter 'password' is required with 'username'" );
+			props.setProperty("mail.smtp.auth","true");
+			final PasswordAuthentication pa = new PasswordAuthentication(username,password);
+			Authenticator auth = new Authenticator() {
+				protected PasswordAuthentication getPasswordAuthentication() {
+					return pa;
+				}
+			};
+			session = Session.getInstance(props,auth);
+		}
+	}
+
+	private String getString(LuanState luan,LuanTable params,String key) throws LuanException {
+		Object val = params.get(key);
+		if( val!=null && !(val instanceof String) )
+			throw luan.exception( "parameter '"+key+"' is must be a string" );
+		return (String)val;
+	}
+
+	public LuanTable table() {
+		LuanTable tbl = Luan.newTable();
+		try {
+			tbl.put( "send", new LuanJavaFunction(
+				SmtpCon.class.getMethod( "send", LuanState.class, LuanTable.class ), this
+			) );
+		} catch(NoSuchMethodException e) {
+			throw new RuntimeException(e);
+		}
+		return tbl;
+	}
+
+
+	public void send(LuanState luan,LuanTable mailTbl) throws LuanException {
+		try {
+			MimeMessage msg = new MimeMessage(session);
+
+			String from = getString(luan,mailTbl,"from");
+			if( from != null )
+				msg.setFrom(from);
+
+			String to = getString(luan,mailTbl,"to");
+			if( to != null )
+				msg.setRecipients(Message.RecipientType.TO,to);
+
+			String subject = getString(luan,mailTbl,"subject");
+			if( subject != null )
+				msg.setSubject(subject);
+
+			String body = getString(luan,mailTbl,"body");
+			if( body != null )
+				msg.setText(body);
+
+			Transport.send(msg);
+		} catch(MessagingException e) {
+			throw luan.exception(e);
+		}
+	}
+
+}