001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package org.drftpd.commands;
019:
020: import net.sf.drftpd.event.DirectoryFtpEvent;
021: import net.sf.drftpd.master.BaseFtpConnection;
022: import net.sf.drftpd.master.FtpRequest;
023: import net.sf.drftpd.master.command.CommandManager;
024: import net.sf.drftpd.master.command.CommandManagerFactory;
025:
026: import org.apache.log4j.Level;
027: import org.apache.log4j.Logger;
028:
029: import org.drftpd.Bytes;
030: import org.drftpd.remotefile.LinkedRemoteFile;
031: import org.drftpd.remotefile.LinkedRemoteFileInterface;
032: import org.drftpd.sections.SectionInterface;
033:
034: import org.drftpd.usermanager.NoSuchUserException;
035: import org.drftpd.usermanager.User;
036: import org.drftpd.usermanager.UserFileException;
037:
038: import java.io.FileNotFoundException;
039: import java.io.IOException;
040:
041: import java.util.Hashtable;
042: import java.util.Iterator;
043: import java.util.Map;
044:
045: /**
046: * @author mog
047: *
048: * @version $Id: Pre.java 979 2005-02-09 02:49:17Z zubov $
049: */
050: public class Pre implements CommandHandler, CommandHandlerFactory {
051: private static final Logger logger = Logger.getLogger(Pre.class);
052:
053: private static void recursiveRemoveOwnership(
054: LinkedRemoteFileInterface dir, long lastModified) {
055: dir.setOwner("drftpd");
056: dir.setLastModified(lastModified);
057: for (Iterator iter = dir.getFiles().iterator(); iter.hasNext();) {
058: LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter
059: .next();
060: file.setOwner("drftpd");
061: file.setLastModified(lastModified);
062: if (file.isDirectory()) {
063: recursiveRemoveOwnership(file, lastModified);
064: }
065: }
066: }
067:
068: /**
069: * Syntax: SITE PRE <RELEASEDIR> <SECTION>
070: */
071: public Reply execute(BaseFtpConnection conn)
072: throws UnhandledCommandException {
073: FtpRequest request = conn.getRequest();
074:
075: if (!"SITE PRE".equals(request.getCommand())) {
076: throw UnhandledCommandException.create(Pre.class, request);
077: }
078:
079: if (!request.hasArgument()) {
080: return Reply.RESPONSE_501_SYNTAX_ERROR;
081: }
082:
083: String[] args = request.getArgument().split(" ");
084:
085: if (args.length != 2) {
086: return Reply.RESPONSE_501_SYNTAX_ERROR;
087: }
088:
089: SectionInterface section = conn.getGlobalContext()
090: .getConnectionManager().getGlobalContext()
091: .getSectionManager().getSection(args[1]);
092:
093: if (section.getName().equals("")) {
094: return new Reply(200,
095: "Invalid section, see SITE SECTIONS for a list of available sections");
096: }
097:
098: LinkedRemoteFileInterface preDir;
099:
100: try {
101: preDir = conn.getCurrentDirectory().lookupFile(args[0]);
102: } catch (FileNotFoundException e) {
103: return Reply.RESPONSE_550_REQUESTED_ACTION_NOT_TAKEN;
104: }
105:
106: if (!conn.getGlobalContext().getConnectionManager()
107: .getGlobalContext().getConfig().checkPathPermission(
108: "pre", conn.getUserNull(), preDir)) {
109: return Reply.RESPONSE_530_ACCESS_DENIED;
110: }
111:
112: Reply response = new Reply(200);
113:
114: //AWARD CREDITS
115: Hashtable awards = new Hashtable();
116: preAwardCredits(conn, preDir, awards);
117:
118: for (Iterator iter = awards.entrySet().iterator(); iter
119: .hasNext();) {
120: Map.Entry entry = (Map.Entry) iter.next();
121: User owner = (User) entry.getKey();
122:
123: if (conn.getGlobalContext().getConnectionManager()
124: .getGlobalContext().getConfig()
125: .getCreditCheckRatio(preDir, owner) == 0) {
126: Long award = (Long) entry.getValue();
127: owner.updateCredits(award.longValue());
128: response.addComment("Awarded "
129: + Bytes.formatBytes(award.longValue()) + " to "
130: + owner.getName());
131: }
132: }
133:
134: //RENAME
135: recursiveRemoveOwnership(preDir, System.currentTimeMillis());
136:
137: LinkedRemoteFile toDir;
138:
139: try {
140: preDir.renameTo(section.getPath(), preDir.getName());
141: } catch (IOException ex) {
142: logger.warn("", ex);
143:
144: return new Reply(200, ex.getMessage());
145: }
146:
147: //ANNOUNCE
148: conn.getGlobalContext()
149: .dispatchFtpEvent(
150: new DirectoryFtpEvent(conn.getUserNull(),
151: "PRE", preDir));
152:
153: return Reply.RESPONSE_200_COMMAND_OK;
154: }
155:
156: public String[] getFeatReplies() {
157: return null;
158: }
159:
160: public CommandHandler initialize(BaseFtpConnection conn,
161: CommandManager initializer) {
162: return this ;
163: }
164:
165: public void load(CommandManagerFactory initializer) {
166: }
167:
168: private void preAwardCredits(BaseFtpConnection conn,
169: LinkedRemoteFileInterface preDir, Hashtable awards) {
170: for (Iterator iter = preDir.getFiles().iterator(); iter
171: .hasNext();) {
172: LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter
173: .next();
174: User owner;
175:
176: try {
177: owner = conn.getGlobalContext().getConnectionManager()
178: .getGlobalContext().getUserManager()
179: .getUserByName(file.getUsername());
180: } catch (NoSuchUserException e) {
181: logger
182: .log(
183: Level.INFO,
184: "PRE: Cannot award credits to non-existing user",
185: e);
186:
187: continue;
188: } catch (UserFileException e) {
189: logger.log(Level.WARN, "", e);
190:
191: continue;
192: }
193:
194: Long total = (Long) awards.get(owner);
195:
196: if (total == null) {
197: total = new Long(0);
198: }
199:
200: total = new Long(total.longValue()
201: + (long) (file.length() * owner.getKeyedMap()
202: .getObjectFloat(UserManagement.RATIO)));
203: awards.put(owner, total);
204: }
205: }
206:
207: public void unload() {
208: }
209: }
|