1 18 package net.sf.drftpd.master.command.plugins; 19 20 import java.io.IOException ; 21 import java.util.Iterator ; 22 23 import net.sf.drftpd.FileExistsException; 24 import net.sf.drftpd.event.DirectoryFtpEvent; 25 import net.sf.drftpd.master.BaseFtpConnection; 26 import net.sf.drftpd.master.FtpReply; 27 import net.sf.drftpd.master.command.CommandManager; 28 import net.sf.drftpd.master.command.CommandManagerFactory; 29 import net.sf.drftpd.master.usermanager.NoSuchUserException; 30 import net.sf.drftpd.remotefile.LinkedRemoteFile; 31 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 32 33 import org.apache.log4j.Logger; 34 import org.drftpd.commands.CommandHandler; 35 import org.drftpd.commands.CommandHandlerFactory; 36 import org.drftpd.commands.UnhandledCommandException; 37 38 42 public class Request implements CommandHandlerFactory, CommandHandler { 43 private static final String FILLEDPREFIX = "FILLED-for."; 44 45 private static final Logger logger = Logger.getLogger(Request.class); 46 47 private static final String REQPREFIX = "REQUEST-by."; 48 49 private FtpReply doSITE_REQFILLED(BaseFtpConnection conn) { 50 if (!conn.getRequest().hasArgument()) 51 return FtpReply.RESPONSE_501_SYNTAX_ERROR; 52 53 LinkedRemoteFileInterface currdir = conn.getCurrentDirectory(); 54 String reqname = conn.getRequest().getArgument(); 55 56 for (Iterator iter = currdir.getFiles().iterator(); iter.hasNext();) { 57 LinkedRemoteFile file = (LinkedRemoteFile) iter.next(); 58 59 if (!file.getName().startsWith(REQPREFIX)) 60 continue; 61 String username = file.getName().substring(REQPREFIX.length()); 62 String myreqname = username.substring(username.indexOf('-') + 1); 63 username = username.substring(0, username.indexOf('-')); 64 if (myreqname.equals(reqname)) { 65 String filledname = FILLEDPREFIX + username + "-" + myreqname; 66 LinkedRemoteFile filledfile; 67 try { 68 filledfile = 69 file.renameTo( 70 file.getParentFile().getPath(), 71 filledname); 72 } catch (IOException e) { 73 logger.warn("", e); 74 return new FtpReply(200, e.getMessage()); 75 } 76 conn.getConnectionManager().dispatchFtpEvent( 78 new DirectoryFtpEvent( 79 conn.getUserNull(), 80 "REQFILLED", 81 filledfile)); 82 try { 84 conn.getUser().addRequestsFilled(); 85 } catch (NoSuchUserException e) { 86 e.printStackTrace(); 87 } 88 return new FtpReply( 89 200, 90 "OK, renamed " + myreqname + " to " + filledname); 91 } 92 } 93 return new FtpReply(200, "Couldn't find a request named " + reqname); 94 } 95 private FtpReply doSITE_REQUEST(BaseFtpConnection conn) { 96 97 if (!conn 98 .getConfig() 99 .checkPathPermission( 100 "request", 101 conn.getUserNull(), 102 conn.getCurrentDirectory())) { 103 return FtpReply.RESPONSE_530_ACCESS_DENIED; 104 } 105 106 if (!conn.getRequest().hasArgument()) { 107 return FtpReply.RESPONSE_501_SYNTAX_ERROR; 108 } 109 110 String createdDirName = 111 REQPREFIX 112 + conn.getUserNull().getUsername() 113 + "-" 114 + conn.getRequest().getArgument(); 115 try { 116 LinkedRemoteFile createdDir = 117 conn.getCurrentDirectory().createDirectory( 118 conn.getUserNull().getUsername(), 119 conn.getUserNull().getGroupName(), 120 createdDirName); 121 122 conn.getConnectionManager().dispatchFtpEvent( 124 new DirectoryFtpEvent( 125 conn.getUserNull(), 126 "REQUEST", 127 createdDir)); 128 try { 130 conn.getUser().addRequests(); 131 } catch (NoSuchUserException e) { 132 e.printStackTrace(); 133 } 134 return new FtpReply( 135 257, 136 "\"" + createdDir.getPath() + "\" created."); 137 } catch (FileExistsException ex) { 138 return new FtpReply( 139 550, 140 "directory " + createdDirName + " already exists"); 141 } 142 } 143 144 public FtpReply execute(BaseFtpConnection conn) 145 throws UnhandledCommandException { 146 String cmd = conn.getRequest().getCommand(); 147 if ("SITE REQUEST".equals(cmd)) 148 return doSITE_REQUEST(conn); 149 if ("SITE REQFILLED".equals(cmd)) 150 return doSITE_REQFILLED(conn); 151 throw UnhandledCommandException.create( 152 Request.class, 153 conn.getRequest()); 154 } 155 156 public String [] getFeatReplies() { 157 return null; 158 } 159 160 public CommandHandler initialize( 161 BaseFtpConnection conn, 162 CommandManager initializer) { 163 return this; 164 } 165 public void load(CommandManagerFactory initializer) { 166 } 167 public void unload() { 168 } 169 } 170 | Popular Tags |