1 18 package net.sf.drftpd.master.command.plugins; 19 20 import java.util.Arrays ; 21 import java.util.Collection ; 22 import java.util.Iterator ; 23 24 import net.sf.drftpd.master.BaseFtpConnection; 25 import net.sf.drftpd.master.FtpReply; 26 import net.sf.drftpd.master.FtpRequest; 27 import net.sf.drftpd.master.command.CommandManager; 28 import net.sf.drftpd.master.command.CommandManagerFactory; 29 import net.sf.drftpd.remotefile.LinkedRemoteFileInterface; 30 31 import org.apache.log4j.Logger; 32 import org.drftpd.commands.CommandHandler; 33 import org.drftpd.commands.CommandHandlerFactory; 34 35 39 public class Search implements CommandHandlerFactory, CommandHandler { 40 public void unload() {} 41 public void load(CommandManagerFactory initializer) {} 42 43 private static void findFile( 44 BaseFtpConnection conn, 45 FtpReply response, 46 LinkedRemoteFileInterface dir, 47 Collection searchstrings, 48 boolean files, 49 boolean dirs) { 50 if (!conn.getConfig().checkPrivPath(conn.getUserNull(), dir)) { 52 Logger.getLogger(Search.class).debug("privpath: "+dir.getPath()); 53 return; 54 } 55 for (Iterator iter = dir.getFiles().iterator(); iter.hasNext();) { 56 LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter.next(); 57 if (file.isDirectory()) { 58 findFile(conn, response, file, searchstrings, files, dirs); 59 } 60 if (dirs && file.isDirectory() || files && file.isFile()) { 61 for (Iterator iterator = searchstrings.iterator(); 62 iterator.hasNext(); 63 ) { 64 if(response.size() >= 100) return; 65 String searchstring = (String ) iterator.next(); 66 if (file.getName().toLowerCase().indexOf(searchstring) 67 != -1) { 68 response.addComment(file.getPath()); 69 if(response.size() >= 100) { 70 response.addComment("<snip>"); 71 return; 72 } 73 break; 74 } 75 } 76 } 77 } 78 } 79 80 public FtpReply execute(BaseFtpConnection conn) { 81 FtpRequest request = conn.getRequest(); 82 if (!request.hasArgument()) { 83 return FtpReply.RESPONSE_501_SYNTAX_ERROR; 84 } 85 String args[] = request.getArgument().toLowerCase().split(" "); 86 if (args.length == 0) { 87 return FtpReply.RESPONSE_501_SYNTAX_ERROR; 88 } 89 Collection searchstrings = Arrays.asList(args); 90 FtpReply response = (FtpReply) FtpReply.RESPONSE_200_COMMAND_OK.clone(); 91 findFile( 92 conn, 93 response, 94 conn.getCurrentDirectory(), 95 searchstrings, 96 "SITE DUPE".equals(request.getCommand()), 97 "SITE SEARCH".equals(request.getCommand())); 98 return response; 99 } 100 101 public CommandHandler initialize(BaseFtpConnection conn, CommandManager initializer) { 102 return this; 103 } 104 105 public String [] getFeatReplies() { 106 return null; 107 } 108 109 } 110 | Popular Tags |