1 18 package net.sf.drftpd.master.command.plugins; 19 20 import java.io.FileNotFoundException ; 21 import java.io.IOException ; 22 import java.util.Hashtable ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 import net.sf.drftpd.Bytes; 27 import net.sf.drftpd.event.DirectoryFtpEvent; 28 import net.sf.drftpd.master.BaseFtpConnection; 29 import net.sf.drftpd.master.FtpReply; 30 import net.sf.drftpd.master.FtpRequest; 31 import net.sf.drftpd.master.command.CommandManager; 32 import net.sf.drftpd.master.command.CommandManagerFactory; 33 import net.sf.drftpd.master.usermanager.NoSuchUserException; 34 import net.sf.drftpd.master.usermanager.User; 35 import net.sf.drftpd.master.usermanager.UserFileException; 36 import net.sf.drftpd.remotefile.LinkedRemoteFile; 37 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 38 39 import org.apache.log4j.Level; 40 import org.apache.log4j.Logger; 41 import org.drftpd.commands.CommandHandler; 42 import org.drftpd.commands.CommandHandlerFactory; 43 import org.drftpd.commands.UnhandledCommandException; 44 import org.drftpd.sections.SectionInterface; 45 46 51 public class Pre implements CommandHandlerFactory, CommandHandler { 52 53 private static final Logger logger = Logger.getLogger(Pre.class); 54 55 60 public FtpReply execute(BaseFtpConnection conn) 61 throws UnhandledCommandException { 62 FtpRequest request = conn.getRequest(); 63 if (!"SITE PRE".equals(request.getCommand())) 64 throw UnhandledCommandException.create(Pre.class, request); 65 if (!request.hasArgument()) 66 return FtpReply.RESPONSE_501_SYNTAX_ERROR; 67 68 String args[] = request.getArgument().split(" "); 69 if (args.length != 2) { 70 return FtpReply.RESPONSE_501_SYNTAX_ERROR; 71 } 72 73 SectionInterface section = conn.getConnectionManager().getSectionManager().getSection(args[1]); 74 75 if(section.getName().equals("")) { 76 return new FtpReply(200, "Invalid section, see SITE SECTIONS for a list of available sections"); 77 } 78 79 LinkedRemoteFileInterface preDir; 80 try { 81 preDir = conn.getCurrentDirectory().lookupFile(args[0]); 82 } catch (FileNotFoundException e) { 83 return FtpReply.RESPONSE_550_REQUESTED_ACTION_NOT_TAKEN; 84 } 85 if (!conn 86 .getConfig() 87 .checkPathPermission("pre", conn.getUserNull(), preDir)) { 88 return FtpReply.RESPONSE_530_ACCESS_DENIED; 89 } 90 91 FtpReply response = new FtpReply(200); 92 93 Hashtable awards = new Hashtable (); 95 preAwardCredits(conn, preDir, awards); 96 for (Iterator iter = awards.entrySet().iterator(); iter.hasNext();) { 97 Map.Entry entry = (Map.Entry ) iter.next(); 98 User owner = (User) entry.getKey(); 99 if (conn.getConfig().getCreditCheckRatio(preDir, owner) == 0) { 100 Long award = (Long ) entry.getValue(); 101 owner.updateCredits(award.longValue()); 102 response.addComment( 103 "Awarded " 104 + Bytes.formatBytes(award.longValue()) 105 + " to " 106 + owner.getUsername()); 107 } 108 } 109 110 LinkedRemoteFile toDir; 112 try { 113 toDir = preDir.renameTo(section.getPath(), preDir.getName()); 114 } catch (IOException ex) { 115 logger.warn("", ex); 116 return new FtpReply(200, ex.getMessage()); 117 } 118 119 conn.getConnectionManager().dispatchFtpEvent( 121 new DirectoryFtpEvent(conn.getUserNull(), "PRE", toDir)); 122 123 return FtpReply.RESPONSE_200_COMMAND_OK; 124 } 125 126 public String [] getFeatReplies() { 127 return null; 128 } 129 130 public CommandHandler initialize( 131 BaseFtpConnection conn, 132 CommandManager initializer) { 133 return this; 134 } 135 public void load(CommandManagerFactory initializer) { 136 } 137 138 private void preAwardCredits( 139 BaseFtpConnection conn, 140 LinkedRemoteFileInterface preDir, 141 Hashtable awards) { 142 for (Iterator iter = preDir.getFiles().iterator(); iter.hasNext();) { 143 LinkedRemoteFileInterface file = 144 (LinkedRemoteFileInterface) iter.next(); 145 User owner; 146 try { 147 owner = conn.getConnectionManager().getUserManager().getUserByName(file.getUsername()); 148 } catch (NoSuchUserException e) { 149 logger.log( 150 Level.INFO, 151 "PRE: Cannot award credits to non-existing user", 152 e); 153 continue; 154 } catch (UserFileException e) { 155 logger.log(Level.WARN, "", e); 156 continue; 157 } 158 Long total = (Long ) awards.get(owner); 159 if (total == null) 160 total = new Long (0); 161 total = 162 new Long ( 163 total.longValue() 164 + (long) (file.length() * owner.getRatio())); 165 awards.put(owner, total); 166 } 167 } 168 public void unload() { 169 } 170 171 } 172 | Popular Tags |