001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.runtime.net.mail; 020 021import lucee.commons.lang.StringUtil; 022import lucee.runtime.exp.ExpressionException; 023import lucee.runtime.op.Caster; 024 025 026/** 027 * 028 */ 029public final class ServerImpl implements Server { 030 031 private String hostName; 032 private String username; 033 private String password; 034 private int port=DEFAULT_PORT; 035 private boolean readOnly=false; 036 private boolean tls; 037 private boolean ssl; 038 private final boolean reuse; 039 private final long life; 040 private final long idle; 041 //private static Pattern[] patterns=new Pattern[3]; 042 043 //[user:password@]server[:port],[ 044 /*static { 045 patterns[0]=Pattern.compile("^([^:\\s)]+)\\s*:\\s*([^@\\s)]+)\\s*@\\s*([^:\\s)]+)\\s*:\\s*(.+)$"); 046 patterns[1]=Pattern.compile("^([^:\\s)]+)\\s*:\\s*([^@\\s)]+)\\s*@\\s*(.+)$"); 047 patterns[2]=Pattern.compile("^([^:\\s)]+)\\s*:\\s*(.+)$"); 048 049 }*/ 050 051 public static ServerImpl getInstance(String host, int defaultPort,String defaultUsername,String defaultPassword, long defaultLifeTimespan, long defaultIdleTimespan, boolean defaultTls, boolean defaultSsl) throws MailException { 052 053 String userpass,user=defaultUsername,pass=defaultPassword,tmp; 054 int port=defaultPort; 055 056 // [user:password@]server[:port] 057 int index=host.indexOf('@'); 058 059 // username:password 060 if(index!=-1) { 061 userpass=host.substring(0,index); 062 host=host.substring(index+1); 063 064 index=userpass.indexOf(':'); 065 if(index!=-1) { 066 user=userpass.substring(0,index).trim(); 067 pass=userpass.substring(index+1).trim(); 068 } 069 else user=userpass.trim(); 070 } 071 072 // server:port 073 index=host.indexOf(':'); 074 if(index!=-1) { 075 tmp=host.substring(index+1).trim(); 076 if(!StringUtil.isEmpty(tmp)){ 077 try { 078 port=Caster.toIntValue(tmp); 079 } catch (ExpressionException e) { 080 throw new MailException("port definition is invalid ["+tmp+"]"); 081 } 082 } 083 host=host.substring(0,index).trim(); 084 } 085 else host=host.trim(); 086 087 088 return new ServerImpl(host,port,user,pass,defaultLifeTimespan,defaultIdleTimespan,defaultTls,defaultSsl,true); 089 } 090 091 092 /*public ServerImpl(String server,int port) { 093 this.hostName=server; 094 this.port=port; 095 }*/ 096 097 public ServerImpl(String hostName,int port,String username,String password, long lifeTimespan, long idleTimespan, boolean tls, boolean ssl, boolean reuseConnections) { 098 this.hostName=hostName; 099 this.username=username; 100 this.password=password; 101 this.life=lifeTimespan; 102 this.idle=idleTimespan; 103 this.port=port; 104 this.tls=tls; 105 this.ssl=ssl; 106 this.reuse=reuseConnections; 107 } 108 109 /*public ServerImpl(String strServer) throws MailException { 110 strServer=strServer.trim(); 111 boolean hasMatch=false; 112 outer:for(int i=0;i<patterns.length;i++) { 113 Pattern p = patterns[i]; 114 Matcher m = p.matcher(strServer); 115 116 if(m.matches()) { 117 try { 118 switch(m.groupCount()) { 119 case 2: 120 hostName=m.group(1).trim(); 121 port=Caster.toIntValue(m.group(2).trim()); 122 break; 123 case 4: 124 username=m.group(1).trim(); 125 password=m.group(2).trim(); 126 hostName=m.group(3).trim(); 127 port=Caster.toIntValue(m.group(4).trim()); 128 break; 129 } 130 } 131 catch(ExpressionException e) { 132 throw new MailException(e.getMessage()); 133 } 134 hasMatch=true; 135 break outer; 136 } 137 } 138 if(!hasMatch) hostName=strServer; 139 }*/ 140 141 /*public static Server[] factory(String strServers) throws MailException { 142 StringTokenizer tokens=new StringTokenizer(strServers,",;"); 143 ArrayList list=new ArrayList(); 144 145 while(tokens.hasMoreTokens()) { 146 list.add(new ServerImpl(tokens.nextToken())); 147 } 148 Server[] pairs=(Server[])list.toArray(new Server[list.size()]); 149 return pairs; 150 151 152 }*/ 153 154 @Override 155 public String getPassword() { 156 if(password==null && hasAuthentication()) return ""; 157 return password; 158 } 159 @Override 160 public int getPort() { 161 return port; 162 } 163 @Override 164 public String getHostName() { 165 return hostName; 166 } 167 @Override 168 public String getUsername() { 169 return username; 170 } 171 @Override 172 public boolean hasAuthentication() { 173 return username!=null && username.length()>0; 174 } 175 176 @Override 177 public String toString() { 178 if(username!=null) { 179 return username+":"+password+"@"+hostName+":"+port; 180 } 181 return hostName+":"+port; 182 } 183 184 @Override 185 public Server cloneReadOnly() { 186 ServerImpl s = new ServerImpl(hostName, port,username, password,life,idle,tls,ssl,reuse); 187 s.readOnly=true; 188 return s; 189 } 190 191 @Override 192 public boolean isReadOnly() { 193 return readOnly; 194 } 195 196 @Override 197 public boolean verify() throws SMTPException { 198 return SMTPVerifier.verify(hostName,username,password,port); 199 } 200 201 public boolean isTLS() { 202 return tls; 203 } 204 205 public boolean isSSL() { 206 return ssl; 207 } 208 209 public void setSSL(boolean ssl) { 210 this.ssl=ssl; 211 } 212 213 public void setTLS(boolean tls) { 214 this.tls=tls; 215 } 216 217 public boolean reuseConnections() { 218 return reuse; 219 } 220 221 public long getLifeTimeSpan() { 222 return life; 223 } 224 public long getIdleTimeSpan() { 225 return idle; 226 } 227}