changeset 360:cbb94a7c7a9e

allow mail attachments; add String.to_binary and Binary.to_string;
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 15 Apr 2015 11:32:30 -0600
parents 8848edb0e6bf
children 0581238084ad
files core/src/luan/modules/Binary.luan core/src/luan/modules/BinaryLuan.java core/src/luan/modules/String.luan core/src/luan/modules/StringLuan.java mail/src/luan/modules/mail/SmtpCon.java
diffstat 5 files changed, 66 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
diff -r 8848edb0e6bf -r cbb94a7c7a9e core/src/luan/modules/Binary.luan
--- a/core/src/luan/modules/Binary.luan	Wed Apr 15 08:58:54 2015 -0600
+++ b/core/src/luan/modules/Binary.luan	Wed Apr 15 11:32:30 2015 -0600
@@ -3,3 +3,4 @@
 
 byte = BinaryLuan.byte_
 binary = BinaryLuan.binary
+to_string = BinaryLuan.to_string
diff -r 8848edb0e6bf -r cbb94a7c7a9e core/src/luan/modules/BinaryLuan.java
--- a/core/src/luan/modules/BinaryLuan.java	Wed Apr 15 08:58:54 2015 -0600
+++ b/core/src/luan/modules/BinaryLuan.java	Wed Apr 15 11:32:30 2015 -0600
@@ -62,4 +62,8 @@
 		return bytes;
 	}
 
+	public static String to_string(byte[] binary) {
+		return new String(binary);
+	}
+
 }
diff -r 8848edb0e6bf -r cbb94a7c7a9e core/src/luan/modules/String.luan
--- a/core/src/luan/modules/String.luan	Wed Apr 15 08:58:54 2015 -0600
+++ b/core/src/luan/modules/String.luan	Wed Apr 15 11:32:30 2015 -0600
@@ -14,5 +14,6 @@
 rep = StringLuan.rep
 reverse = StringLuan.reverse
 sub = StringLuan.sub
+to_binary = StringLuan.to_binary
 trim = StringLuan.trim
 upper = StringLuan.upper
diff -r 8848edb0e6bf -r cbb94a7c7a9e core/src/luan/modules/StringLuan.java
--- a/core/src/luan/modules/StringLuan.java	Wed Apr 15 08:58:54 2015 -0600
+++ b/core/src/luan/modules/StringLuan.java	Wed Apr 15 11:32:30 2015 -0600
@@ -70,6 +70,10 @@
 		return new String(a);
 	}
 
+	@LuanMethod public static byte[] to_binary(String s) {
+		return s.getBytes();
+	}
+
 	public static int len(LuanState luan,String s) throws LuanException {
 		Utils.checkNotNull(luan,s);
 		return s.length();
diff -r 8848edb0e6bf -r cbb94a7c7a9e mail/src/luan/modules/mail/SmtpCon.java
--- a/mail/src/luan/modules/mail/SmtpCon.java	Wed Apr 15 08:58:54 2015 -0600
+++ b/mail/src/luan/modules/mail/SmtpCon.java	Wed Apr 15 11:32:30 2015 -0600
@@ -1,5 +1,6 @@
 package luan.modules.mail;
 
+import java.io.IOException;
 import java.util.Map;
 import java.util.HashMap;
 import java.util.Properties;
@@ -9,6 +10,7 @@
 import javax.mail.Transport;
 import javax.mail.Message;
 import javax.mail.MessagingException;
+import javax.mail.Part;
 import javax.mail.internet.MimeMessage;
 import javax.mail.internet.MimeMultipart;
 import javax.mail.internet.MimeBodyPart;
@@ -39,10 +41,10 @@
 			} else if( port instanceof Number ) {
 				Integer i = Luan.asInteger(port);
 				if( i == null )
-					throw luan.exception( "parameter 'port' is must be an integer" );
+					throw luan.exception( "parameter 'port' must be an integer" );
 				s = i.toString();
 			} else {
-				throw luan.exception( "parameter 'port' is must be an integer" );
+				throw luan.exception( "parameter 'port' must be an integer" );
 			}
 			props.setProperty("mail.smtp.socketFactory.port", s);
 			props.setProperty("mail.smtp.port", s);
@@ -111,9 +113,12 @@
 				msg.setSubject(subject);
 
 			Object body = mailParams.remove("body");
+			Object attachments = mailParams.remove("attachments");
+			Part bodyPart = attachments==null ? msg : new MimeBodyPart();
+
 			if( body != null ) {
 				if( body instanceof String ) {
-					msg.setText((String)body);
+					bodyPart.setText((String)body);
 				} else if( body instanceof LuanTable ) {
 					LuanTable bodyTbl = (LuanTable)body;
 					Map<Object,Object> map = new HashMap<Object,Object>(bodyTbl.asMap());
@@ -132,9 +137,55 @@
 					}
 					if( !map.isEmpty() )
 						throw luan.exception( "invalid body types: " + map );
-					msg.setContent(mp);
+					bodyPart.setContent(mp);
 				} else
-					throw luan.exception( "parameter 'body' is must be a string or table" );
+					throw luan.exception( "parameter 'body' must be a string or table" );
+			}
+
+			if( attachments != null ) {
+				if( !(attachments instanceof LuanTable) )
+					throw luan.exception( "parameter 'attachments' must be a table" );
+				LuanTable attachmentsTbl = (LuanTable)attachments;
+				if( !attachmentsTbl.isList() )
+					throw luan.exception( "parameter 'attachments' must be a list" );
+				MimeMultipart mp = new MimeMultipart("mixed");
+				if( body != null )
+					mp.addBodyPart((MimeBodyPart)bodyPart);
+				for( Object attachment : attachmentsTbl.asList() ) {
+					if( !(attachment instanceof LuanTable) )
+						throw luan.exception( "each attachment must be a table" );
+					Map<Object,Object> attachmentMap = new HashMap<Object,Object>(((LuanTable)attachment).asMap());
+					Object obj;
+
+					obj = attachmentMap.remove("filename");
+					if( obj==null )
+						throw luan.exception( "an attachment is missing 'filename'" );
+					if( !(obj instanceof String) )
+						throw luan.exception( "an attachment filename must be a string" );
+					String filename = (String)obj;
+
+					obj = attachmentMap.remove("content_type");
+					if( obj==null )
+						throw luan.exception( "an attachment is missing 'content_type'" );
+					if( !(obj instanceof String) )
+						throw luan.exception( "an attachment content_type must be a string" );
+					String content_type = (String)obj;
+
+					Object content = attachmentMap.remove("content");
+					if( content==null )
+						throw luan.exception( "an attachment is missing 'content'" );
+					if( content_type.startsWith("text/") && content instanceof byte[] )
+						content = new String((byte[])content);
+
+					if( !attachmentMap.isEmpty() )
+						throw luan.exception( "unrecognized attachment parameters: "+attachmentMap );
+
+					MimeBodyPart part = new MimeBodyPart();
+					part.setContent(content,content_type);
+					part.setFileName(filename);
+					mp.addBodyPart(part);
+				}
+				msg.setContent(mp);
 			}
 
 			if( !mailParams.isEmpty() )