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.tag; 020 021import lucee.commons.io.res.Resource; 022import lucee.runtime.exp.ApplicationException; 023import lucee.runtime.exp.PageException; 024import lucee.runtime.ext.tag.TagImpl; 025import lucee.runtime.net.mail.MailClient; 026import lucee.runtime.op.Caster; 027import lucee.runtime.type.util.ArrayUtil; 028import lucee.runtime.type.util.ListUtil; 029 030/** 031 * Retrieves and deletes e-mail messages from a POP mail server. 032 */ 033public abstract class _Mail extends TagImpl { 034 035 private String server; 036 private int port=-1; 037 038 private String username; 039 private String password; 040 private String action="getheaderonly"; 041 private String name; 042 private String[] messageNumber; 043 private String[] uid; 044 private Resource attachmentPath; 045 private int timeout=60; 046 private int startrow=1; 047 private int maxrows=-1; 048 private boolean generateUniqueFilenames=false; 049 private boolean secure=false; 050 051 @Override 052 public void release() { 053 port=-1; 054 username=null; 055 password=null; 056 action="getheaderonly"; 057 name=null; 058 messageNumber=null; 059 uid=null; 060 attachmentPath=null; 061 timeout=60; 062 startrow=1; 063 maxrows=-1; 064 generateUniqueFilenames=false; 065 secure=false; 066 super.release(); 067 068 } 069 070 /** 071 * @param server The server to set. 072 */ 073 public void setServer(String server) { 074 this.server = server; 075 } 076 077 /** 078 * @param port The port to set. 079 */ 080 public void setPort(double port) { 081 this.port = (int)port; 082 } 083 084 /** 085 * @param username The username to set. 086 */ 087 public void setUsername(String username) { 088 this.username = username; 089 } 090 091 /** 092 * @param password The password to set. 093 */ 094 public void setPassword(String password) { 095 this.password = password; 096 } 097 098 /** 099 * @param action The action to set. 100 */ 101 public void setAction(String action) { 102 this.action = action.trim().toLowerCase(); 103 } 104 105 /** 106 * @param name The name to set. 107 */ 108 public void setName(String name) { 109 this.name = name; 110 } 111 112 /** 113 * @param messageNumber The messageNumber to set. 114 * @throws PageException 115 */ 116 public void setMessagenumber(String messageNumber) throws PageException { 117 this.messageNumber = ArrayUtil.trim(ListUtil.toStringArray(ListUtil.listToArrayRemoveEmpty(messageNumber,','))); 118 if(this.messageNumber.length==0)this.messageNumber=null; 119 } 120 121 /** 122 * @param uid The uid to set. 123 * @throws PageException 124 */ 125 public void setUid(String uid) throws PageException { 126 this.uid = ArrayUtil.trim(ListUtil.toStringArray(ListUtil.listToArrayRemoveEmpty(uid,','))); 127 if(this.uid.length==0)this.uid=null; 128 } 129 130 /** 131 * @param attachmentPath The attachmentPath to set. 132 * @throws PageException 133 */ 134 public void setAttachmentpath(String attachmentPath) throws PageException { 135 //try { 136 Resource attachmentDir=pageContext.getConfig().getResource(attachmentPath); 137 if(!attachmentDir.exists() && !attachmentDir.mkdir()) { 138 attachmentDir=pageContext.getConfig().getTempDirectory().getRealResource(attachmentPath); 139 if(!attachmentDir.exists() && !attachmentDir.mkdir()) 140 throw new ApplicationException("directory ["+attachmentPath+"] doesent exist and can't created"); 141 } 142 if(!attachmentDir.isDirectory())throw new ApplicationException("file ["+attachmentPath+"] is not a directory"); 143 pageContext.getConfig().getSecurityManager().checkFileLocation(attachmentDir); 144 this.attachmentPath = attachmentDir; 145 /*} 146 catch(IOException ioe) { 147 throw Caster.toPageException(ioe); 148 }*/ 149 } 150 151 /** 152 * @param maxrows The maxrows to set. 153 */ 154 public void setMaxrows(double maxrows) { 155 this.maxrows = (int)maxrows; 156 } 157 158 /** 159 * @param startrow The startrow to set. 160 */ 161 public void setStartrow(double startrow) { 162 this.startrow = (int)startrow; 163 } 164 165 /** 166 * @param secure The secure to set. 167 */ 168 public void setSecure(boolean secure) { 169 this.secure = secure; 170 } 171 172 /** 173 * @param timeout The timeout to set. 174 */ 175 public void setTimeout(double timeout) { 176 this.timeout = (int)timeout; 177 } 178 179 /** 180 * @param generateUniqueFilenames The generateUniqueFilenames to set. 181 */ 182 public void setGenerateuniquefilenames(boolean generateUniqueFilenames) { 183 this.generateUniqueFilenames = generateUniqueFilenames; 184 } 185 186 /** 187 * @param debug The debug to set. 188 */ 189 public void setDebug(boolean debug) { 190 // does nothing this.debug = debug; 191 } 192 193 @Override 194 public int doStartTag() throws PageException { 195 196 // check attrs 197 if(port==-1)port=getDefaultPort(); 198 199 //PopClient client = new PopClient(server,port,username,password); 200 MailClient client = MailClient.getInstance(getType(),server,port,username,password,secure); 201 client.setTimeout(timeout*1000); 202 client.setMaxrows(maxrows); 203 if(startrow>1)client.setStartrow(startrow-1); 204 client.setUniqueFilenames(generateUniqueFilenames); 205 if(attachmentPath!=null)client.setAttachmentDirectory(attachmentPath); 206 207 if(uid!=null)messageNumber=null; 208 209 try { 210 client.connect(); 211 212 if(action.equals("getheaderonly")) { 213 required(getTagName(),action,"name",name); 214 pageContext.setVariable(name,client.getMails(messageNumber,uid,false)); 215 } 216 else if(action.equals("getall")) { 217 required(getTagName(),action,"name",name); 218 pageContext.setVariable(name,client.getMails(messageNumber,uid,true)); 219 } 220 else if(action.equals("delete")) { 221 client.deleteMails(messageNumber,uid); 222 } 223 else throw new ApplicationException("invalid value for attribute action, valid values are [getHeaderOnly,getAll,delete]"); 224 } 225 catch(Exception e) { 226 throw Caster.toPageException(e); 227 } 228 finally{ 229 client.disconnectEL(); 230 } 231 return SKIP_BODY; 232 } 233 234 protected abstract int getType(); 235 protected abstract int getDefaultPort(); 236 protected abstract String getTagName(); 237}