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.FileExistsException;
021: import net.sf.drftpd.event.DirectoryFtpEvent;
022: import net.sf.drftpd.master.BaseFtpConnection;
023: import net.sf.drftpd.master.command.CommandManager;
024: import net.sf.drftpd.master.command.CommandManagerFactory;
025:
026: import org.apache.log4j.Logger;
027:
028: import org.drftpd.dynamicdata.Key;
029: import org.drftpd.remotefile.LinkedRemoteFile;
030: import org.drftpd.remotefile.LinkedRemoteFileInterface;
031: import org.drftpd.usermanager.NoSuchUserException;
032:
033: import java.io.IOException;
034:
035: import java.util.Iterator;
036:
037: /**
038: * @author mog
039: * @version $Id: Request.java 1160 2005-03-19 17:00:49Z teflon $
040: */
041: public class Request implements CommandHandler, CommandHandlerFactory {
042: public static final Key REQUESTSFILLED = new Key(Request.class,
043: "requestsFilled", Integer.class);
044: public static final Key REQUESTS = new Key(Request.class,
045: "requests", Integer.class);
046: private static final String FILLEDPREFIX = "FILLED-for.";
047: private static final Logger logger = Logger
048: .getLogger(Request.class);
049: private static final String REQPREFIX = "REQUEST-by.";
050:
051: private Reply doSITE_REQFILLED(BaseFtpConnection conn) {
052: if (!conn.getRequest().hasArgument()) {
053: return Reply.RESPONSE_501_SYNTAX_ERROR;
054: }
055:
056: LinkedRemoteFileInterface currdir = conn.getCurrentDirectory();
057: String reqname = conn.getRequest().getArgument().trim();
058:
059: for (Iterator iter = currdir.getFiles().iterator(); iter
060: .hasNext();) {
061: LinkedRemoteFile file = (LinkedRemoteFile) iter.next();
062:
063: if (!file.getName().startsWith(REQPREFIX)) {
064: continue;
065: }
066:
067: String username = file.getName().substring(
068: REQPREFIX.length());
069: String myreqname = username
070: .substring(username.indexOf('-') + 1);
071: username = username.substring(0, username.indexOf('-'));
072:
073: if (myreqname.equals(reqname)) {
074: String filledname = FILLEDPREFIX + username + "-"
075: + myreqname;
076:
077: try {
078: file.renameTo(file.getParentFile().getPath(),
079: filledname);
080: } catch (IOException e) {
081: logger.warn("", e);
082:
083: return new Reply(200, e.getMessage());
084: }
085:
086: //if (conn.getConfig().checkDirLog(conn.getUserNull(), file)) {
087: conn.getGlobalContext().dispatchFtpEvent(
088: new DirectoryFtpEvent(conn.getUserNull(),
089: "REQFILLED", file));
090:
091: //}
092: try {
093: conn.getUser().getKeyedMap().incrementObjectLong(
094: REQUESTSFILLED);
095:
096: //conn.getUser().addRequestsFilled();
097: } catch (NoSuchUserException e) {
098: e.printStackTrace();
099: }
100:
101: return new Reply(200, "OK, renamed " + myreqname
102: + " to " + filledname);
103: }
104: }
105:
106: return new Reply(200, "Couldn't find a request named "
107: + reqname);
108: }
109:
110: private Reply doSITE_REQUEST(BaseFtpConnection conn) {
111: if (!conn.getGlobalContext().getConfig().checkPathPermission(
112: "request", conn.getUserNull(),
113: conn.getCurrentDirectory())) {
114: return Reply.RESPONSE_530_ACCESS_DENIED;
115: }
116:
117: if (!conn.getRequest().hasArgument()) {
118: return Reply.RESPONSE_501_SYNTAX_ERROR;
119: }
120:
121: String createdDirName = REQPREFIX
122: + conn.getUserNull().getName() + "-"
123: + conn.getRequest().getArgument().trim();
124:
125: try {
126: LinkedRemoteFile createdDir = conn.getCurrentDirectory()
127: .createDirectory(conn.getUserNull().getName(),
128: conn.getUserNull().getGroup(),
129: createdDirName);
130:
131: //if (conn.getConfig().checkDirLog(conn.getUserNull(), createdDir)) {
132: conn.getGlobalContext().dispatchFtpEvent(
133: new DirectoryFtpEvent(conn.getUserNull(),
134: "REQUEST", createdDir));
135:
136: conn.getUserNull().getKeyedMap().incrementObjectLong(
137: REQUESTS);
138:
139: //conn.getUser().addRequests();
140: return new Reply(257, "\"" + createdDir.getPath()
141: + "\" created.");
142: } catch (FileExistsException ex) {
143: return new Reply(550, "directory " + createdDirName
144: + " already exists");
145: }
146: }
147:
148: public Reply execute(BaseFtpConnection conn)
149: throws UnhandledCommandException {
150: String cmd = conn.getRequest().getCommand();
151:
152: if ("SITE REQUEST".equals(cmd)) {
153: return doSITE_REQUEST(conn);
154: }
155:
156: if ("SITE REQFILLED".equals(cmd)) {
157: return doSITE_REQFILLED(conn);
158: }
159:
160: throw UnhandledCommandException.create(Request.class, conn
161: .getRequest());
162: }
163:
164: public String[] getFeatReplies() {
165: return null;
166: }
167:
168: public CommandHandler initialize(BaseFtpConnection conn,
169: CommandManager initializer) {
170: return this ;
171: }
172:
173: public void load(CommandManagerFactory initializer) {
174: }
175:
176: public void unload() {
177: }
178: }
|