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 net.sf.drftpd.master.command.plugins;
019:
020: import java.util.Arrays;
021: import java.util.Collection;
022: import java.util.Iterator;
023:
024: import net.sf.drftpd.master.BaseFtpConnection;
025: import net.sf.drftpd.master.FtpRequest;
026: import net.sf.drftpd.master.command.CommandManager;
027: import net.sf.drftpd.master.command.CommandManagerFactory;
028:
029: import org.apache.log4j.Logger;
030: import org.drftpd.commands.CommandHandler;
031: import org.drftpd.commands.CommandHandlerFactory;
032: import org.drftpd.commands.Reply;
033: import org.drftpd.remotefile.LinkedRemoteFileInterface;
034:
035: /**
036: * @author mog
037: * @version $Id: Search.java 1114 2005-03-13 01:26:58Z zubov $
038: */
039: public class Search implements CommandHandler, CommandHandlerFactory {
040: public void unload() {
041: }
042:
043: public void load(CommandManagerFactory initializer) {
044: }
045:
046: private static void findFile(BaseFtpConnection conn,
047: Reply response, LinkedRemoteFileInterface dir,
048: Collection searchstrings, boolean files, boolean dirs) {
049: //TODO optimize me, checking using regexp for all dirs is possibly slow
050: if (!conn.getGlobalContext().getConfig().checkPathPermission(
051: "privpath", conn.getUserNull(), dir, true)) {
052: Logger.getLogger(Search.class).debug(
053: "privpath: " + dir.getPath());
054:
055: return;
056: }
057:
058: for (Iterator iter = dir.getFiles().iterator(); iter.hasNext();) {
059: LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter
060: .next();
061:
062: if (file.isDirectory()) {
063: findFile(conn, response, file, searchstrings, files,
064: dirs);
065: }
066:
067: boolean isFind = false;
068: boolean allFind = true;
069:
070: if ((dirs && file.isDirectory())
071: || (files && file.isFile())) {
072: for (Iterator iterator = searchstrings.iterator(); iterator
073: .hasNext();) {
074: if (response.size() >= 100) {
075: return;
076: }
077:
078: String searchstring = (String) iterator.next();
079:
080: if (file.getName().toLowerCase().indexOf(
081: searchstring) != -1) {
082: isFind = true;
083: } else {
084: allFind = false;
085: }
086: }
087:
088: if (isFind && allFind) {
089: response.addComment(file.getPath());
090:
091: if (response.size() >= 100) {
092: response.addComment("<snip>");
093:
094: return;
095: }
096: }
097: }
098: }
099: }
100:
101: public Reply execute(BaseFtpConnection conn) {
102: FtpRequest request = conn.getRequest();
103:
104: if (!request.hasArgument()) {
105: return Reply.RESPONSE_501_SYNTAX_ERROR;
106: }
107:
108: String[] args = request.getArgument().toLowerCase().split(" ");
109:
110: if (args.length == 0) {
111: return Reply.RESPONSE_501_SYNTAX_ERROR;
112: }
113:
114: Collection searchstrings = Arrays.asList(args);
115: Reply response = (Reply) Reply.RESPONSE_200_COMMAND_OK.clone();
116: findFile(conn, response, conn.getCurrentDirectory(),
117: searchstrings,
118: "SITE DUPE".equals(request.getCommand()), "SITE SEARCH"
119: .equals(request.getCommand()));
120:
121: return response;
122: }
123:
124: public CommandHandler initialize(BaseFtpConnection conn,
125: CommandManager initializer) {
126: return this ;
127: }
128:
129: public String[] getFeatReplies() {
130: return null;
131: }
132: }
|