comparison src/luan/modules/mail/SmtpCon.java @ 1267:9fa8b8389578

add LuanTable.luan; support metatable __gc(); add luan.sql;
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 12 Nov 2018 02:10:41 -0700
parents 23e8f93055a4
children cc3dabc05f72
comparison
equal deleted inserted replaced
1266:05934fbf635a 1267:9fa8b8389578
22 22
23 23
24 public final class SmtpCon { 24 public final class SmtpCon {
25 private final Session session; 25 private final Session session;
26 26
27 public SmtpCon(LuanState luan,LuanTable paramsTbl) throws LuanException { 27 public SmtpCon(LuanTable paramsTbl) throws LuanException {
28 Map<Object,Object> params = new HashMap<Object,Object>(paramsTbl.asMap(luan)); 28 Map<Object,Object> params = new HashMap<Object,Object>(paramsTbl.asMap());
29 Properties props = new Properties(System.getProperties()); 29 Properties props = new Properties(System.getProperties());
30 30
31 String host = getString(params,"host"); 31 String host = getString(params,"host");
32 if( host==null ) 32 if( host==null )
33 throw new LuanException( "parameter 'host' is required" ); 33 throw new LuanException( "parameter 'host' is required" );
69 69
70 if( !params.isEmpty() ) 70 if( !params.isEmpty() )
71 throw new LuanException( "unrecognized parameters: "+params ); 71 throw new LuanException( "unrecognized parameters: "+params );
72 } 72 }
73 73
74 private String getString(Map<Object,Object> params,String key) throws LuanException { 74 private static String getString(Map<Object,Object> params,String key) throws LuanException {
75 Object val = params.remove(key); 75 Object val = params.remove(key);
76 if( val!=null && !(val instanceof String) ) 76 if( val!=null && !(val instanceof String) )
77 throw new LuanException( "parameter '"+key+"' must be a string" ); 77 throw new LuanException( "parameter '"+key+"' must be a string" );
78 return (String)val; 78 return (String)val;
79 } 79 }
80 80
81 81
82 public void send(LuanState luan,LuanTable mailTbl) throws LuanException { 82 public void send(LuanTable mailTbl) throws LuanException {
83 try { 83 try {
84 Map<Object,Object> mailParams = new HashMap<Object,Object>(mailTbl.asMap(luan)); 84 Map<Object,Object> mailParams = new HashMap<Object,Object>(mailTbl.asMap());
85 MimeMessage msg = new MimeMessage(session); 85 MimeMessage msg = new MimeMessage(session);
86 86
87 String from = getString(mailParams,"from"); 87 String from = getString(mailParams,"from");
88 if( from != null ) 88 if( from != null )
89 msg.setFrom(from); 89 msg.setFrom(from);
111 if( body != null ) { 111 if( body != null ) {
112 if( body instanceof String ) { 112 if( body instanceof String ) {
113 bodyPart.setText((String)body); 113 bodyPart.setText((String)body);
114 } else if( body instanceof LuanTable ) { 114 } else if( body instanceof LuanTable ) {
115 LuanTable bodyTbl = (LuanTable)body; 115 LuanTable bodyTbl = (LuanTable)body;
116 Map<Object,Object> map = new HashMap<Object,Object>(bodyTbl.asMap(luan)); 116 Map<Object,Object> map = new HashMap<Object,Object>(bodyTbl.asMap());
117 MimeMultipart mp = new MimeMultipart("alternative"); 117 MimeMultipart mp = new MimeMultipart("alternative");
118 String text = (String)map.remove("text"); 118 String text = (String)map.remove("text");
119 if( text != null ) { 119 if( text != null ) {
120 MimeBodyPart part = new MimeBodyPart(); 120 MimeBodyPart part = new MimeBodyPart();
121 part.setText(text); 121 part.setText(text);
144 if( body != null ) 144 if( body != null )
145 mp.addBodyPart((MimeBodyPart)bodyPart); 145 mp.addBodyPart((MimeBodyPart)bodyPart);
146 for( Object attachment : attachmentsTbl.asList() ) { 146 for( Object attachment : attachmentsTbl.asList() ) {
147 if( !(attachment instanceof LuanTable) ) 147 if( !(attachment instanceof LuanTable) )
148 throw new LuanException( "each attachment must be a table" ); 148 throw new LuanException( "each attachment must be a table" );
149 Map<Object,Object> attachmentMap = new HashMap<Object,Object>(((LuanTable)attachment).asMap(luan)); 149 Map<Object,Object> attachmentMap = new HashMap<Object,Object>(((LuanTable)attachment).asMap());
150 Object obj; 150 Object obj;
151 151
152 obj = attachmentMap.remove("filename"); 152 obj = attachmentMap.remove("filename");
153 if( obj==null ) 153 if( obj==null )
154 throw new LuanException( "an attachment is missing 'filename'" ); 154 throw new LuanException( "an attachment is missing 'filename'" );