changeset 289:ae7ae2755b48

improve Mail; add String.concat(); git-svn-id: https://luan-java.googlecode.com/svn/trunk@290 21e917c8-12df-6dd8-5cb6-c86387c605b9
author fschmidt@gmail.com <fschmidt@gmail.com@21e917c8-12df-6dd8-5cb6-c86387c605b9>
date Mon, 08 Dec 2014 07:45:40 +0000
parents c4ed33e95889
children 9a0cc6d9b27b
files core/src/luan/modules/StringLuan.java mail/src/luan/modules/mail/SmtpCon.java
diffstat 2 files changed, 29 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/core/src/luan/modules/StringLuan.java	Fri Dec 05 03:49:54 2014 +0000
+++ b/core/src/luan/modules/StringLuan.java	Mon Dec 08 07:45:40 2014 +0000
@@ -20,6 +20,7 @@
 				add( module, "to_binary", String.class );
 				module.put( "byte", new LuanJavaFunction(StringLuan.class.getMethod( "byte_", String.class ),null) );
 				module.put( "char", new LuanJavaFunction(StringLuan.class.getMethod( "char_", new int[0].getClass() ),null) );
+				add( module, "concat", LuanState.class, new Object[0].getClass() );
 				add( module, "find", String.class, String.class, Integer.class, Boolean.class );
 				add( module, "format", String.class, new Object[0].getClass() );
 				add( module, "gmatch", LuanState.class, String.class, String.class );
@@ -256,4 +257,12 @@
 		return String.format(format,args);
 	}
 
+	public static String concat(LuanState luan,Object... args) throws LuanException {
+		StringBuilder sb = new StringBuilder();
+		for( Object arg : args ) {
+			sb.append( luan.toString(arg) );
+		}
+		return sb.toString();
+	}
+
 }
--- a/mail/src/luan/modules/mail/SmtpCon.java	Fri Dec 05 03:49:54 2014 +0000
+++ b/mail/src/luan/modules/mail/SmtpCon.java	Mon Dec 08 07:45:40 2014 +0000
@@ -22,7 +22,8 @@
 public final class SmtpCon {
 	private final Session session;
 
-	public SmtpCon(LuanState luan,LuanTable params) throws LuanException {
+	public SmtpCon(LuanState luan,LuanTable paramsTbl) throws LuanException {
+		Map<Object,Object> params = new HashMap<Object,Object>(paramsTbl.asMap());
 		Properties props = new Properties(System.getProperties());
 
 		String host = getString(luan,params,"host");
@@ -30,7 +31,7 @@
 			throw luan.exception( "parameter 'host' is required" );
 		props.setProperty("mail.smtp.host",host);
 
-		Object port = params.get("port");
+		Object port = params.remove("port");
 		if( port != null ) {
 			String s;
 			if( port instanceof String ) {
@@ -63,10 +64,13 @@
 			};
 			session = Session.getInstance(props,auth);
 		}
+
+		if( !params.isEmpty() )
+			throw luan.exception( "unrecognized parameters: "+params );
 	}
 
-	private String getString(LuanState luan,LuanTable params,String key) throws LuanException {
-		Object val = params.get(key);
+	private String getString(LuanState luan,Map<Object,Object> params,String key) throws LuanException {
+		Object val = params.remove(key);
 		if( val!=null && !(val instanceof String) )
 			throw luan.exception( "parameter '"+key+"' must be a string" );
 		return (String)val;
@@ -87,21 +91,26 @@
 
 	public void send(LuanState luan,LuanTable mailTbl) throws LuanException {
 		try {
+			Map<Object,Object> mailParams = new HashMap<Object,Object>(mailTbl.asMap());
 			MimeMessage msg = new MimeMessage(session);
 
-			String from = getString(luan,mailTbl,"from");
+			String from = getString(luan,mailParams,"from");
 			if( from != null )
 				msg.setFrom(from);
 
-			String to = getString(luan,mailTbl,"to");
+			String to = getString(luan,mailParams,"to");
 			if( to != null )
 				msg.setRecipients(Message.RecipientType.TO,to);
 
-			String subject = getString(luan,mailTbl,"subject");
+			String cc = getString(luan,mailParams,"cc");
+			if( cc != null )
+				msg.setRecipients(Message.RecipientType.CC,cc);
+
+			String subject = getString(luan,mailParams,"subject");
 			if( subject != null )
 				msg.setSubject(subject);
 
-			Object body = mailTbl.get("body");
+			Object body = mailParams.remove("body");
 			if( body != null ) {
 				if( body instanceof String ) {
 					msg.setText((String)body);
@@ -128,6 +137,9 @@
 					throw luan.exception( "parameter 'body' is must be a string or table" );
 			}
 
+			if( !mailParams.isEmpty() )
+				throw luan.exception( "unrecognized parameters: "+mailParams );
+
 			Transport.send(msg);
 		} catch(MessagingException e) {
 			throw luan.exception(e);