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.drmods.plugins.irc;
019:
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.Collection;
025: import java.util.Iterator;
026: import java.util.Properties;
027: import java.util.StringTokenizer;
028:
029: import net.sf.drftpd.master.config.FtpConfig;
030: import net.sf.drftpd.util.ReplacerUtils;
031:
032: import org.apache.log4j.Logger;
033: import org.apache.oro.text.GlobCompiler;
034: import org.drftpd.GlobalContext;
035: import org.drftpd.permissions.GlobPathPermission;
036: import org.drftpd.plugins.SiteBot;
037: import org.drftpd.remotefile.LinkedRemoteFileInterface;
038: import org.drftpd.sitebot.IRCCommand;
039: import org.drftpd.usermanager.NoSuchUserException;
040: import org.drftpd.usermanager.User;
041: import org.drftpd.usermanager.UserFileException;
042: import org.tanesha.replacer.ReplacerEnvironment;
043:
044: import f00f.net.irc.martyr.commands.MessageCommand;
045: import f00f.net.irc.martyr.util.FullNick;
046:
047: /**
048: * @author N3m3Sis-succo . UPDATES by SMAN
049: * @version $Id: Find.java,v 1.2 2-21-2004 12:16:00
050: *
051: */
052: public class Find extends IRCCommand {
053: private static final Logger logger = Logger.getLogger(Find.class);
054:
055: public Find(GlobalContext gctx) {
056: super (gctx);
057: loadConf("conf/drmods.conf");
058: }
059:
060: public void loadConf(String confFile) {
061: Properties cfg = new Properties();
062: FileInputStream file = null;
063: try {
064: file = new FileInputStream(confFile);
065: cfg.load(file);
066: String perm;
067: for (int i = 1;; i++) {
068: perm = cfg.getProperty("find.perms." + i);
069: if (perm == null)
070: break;
071: StringTokenizer st = new StringTokenizer(perm);
072:
073: getGlobalContext().getConfig().addPathPermission(
074: "ircfind",
075: new GlobPathPermission(new GlobCompiler()
076: .compile(st.nextToken()), FtpConfig
077: .makeUsers(st)));
078:
079: }
080: } catch (Exception e) {
081: logger.error("Error reading " + confFile, e);
082: throw new RuntimeException(e.getMessage());
083: } finally {
084: try {
085: if (file != null) {
086: file.close();
087: }
088: } catch (IOException e) {
089: }
090: }
091: }
092:
093: private void findFile(LinkedRemoteFileInterface dir,
094: ArrayList<String> results, Collection searchstrings,
095: User user, boolean files, boolean dirs) {
096: if (!getGlobalContext().getConfig().checkPathPermission(
097: "ircfind", user, dir, true))
098: return;
099:
100: if (!getGlobalContext().getConfig().checkPathPermission(
101: "privpath", user, dir, true))
102: return;
103:
104: for (Iterator iter = dir.getFiles().iterator(); iter.hasNext();) {
105: LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter
106: .next();
107: if (results.size() >= 5)
108: return;
109: if (file.isDirectory()) {
110: findFile(file, results, searchstrings, user, files,
111: dirs);
112: }
113: boolean isFind = false;
114: boolean allFind = true;
115: if (dirs && file.isDirectory() || files && file.isFile()) {
116: for (Iterator iterator = searchstrings.iterator(); iterator
117: .hasNext();) {
118: String searchstring = (String) iterator.next();
119: if (file.getName().toLowerCase().indexOf(
120: searchstring.toLowerCase()) != -1)
121: isFind = true;
122: else
123: allFind = false;
124: }
125:
126: if (isFind && allFind) {
127: ReplacerEnvironment env = new ReplacerEnvironment(
128: SiteBot.GLOBAL_ENV);
129: env.add("path", file.getPath());
130: results.add(ReplacerUtils.jprintf("find.result",
131: env, Find.class));
132: }
133: }
134: }
135: }
136:
137: public ArrayList<String> doFind(String args, MessageCommand msgc) {
138: ArrayList<String> out = new ArrayList<String>();
139: ReplacerEnvironment env = new ReplacerEnvironment(
140: SiteBot.GLOBAL_ENV);
141: env.add("ircnick", msgc.getSource().getNick());
142: FullNick fn = msgc.getSource();
143: String ident = fn.getNick() + "!" + fn.getUser() + "@"
144: + fn.getHost();
145: User user;
146: try {
147: user = getGlobalContext().getUserManager().getUserByIdent(
148: ident);
149: env.add("ftpuser", user.getName());
150: } catch (NoSuchUserException e) {
151: logger.warn("Could not identify " + ident);
152: out.add(ReplacerUtils.jprintf("ident.noident", env,
153: SiteBot.class));
154: return out;
155: } catch (UserFileException e) {
156: logger.error("Could not identify " + ident
157: + " as there was a UserFileException", e);
158: out.add(ReplacerUtils.jprintf("ident.failed", env,
159: SiteBot.class));
160: return out;
161: }
162: if (args.equals("")) {
163: out.add(ReplacerUtils.jprintf("find.syntax", env,
164: Find.class));
165: return out;
166: }
167:
168: Collection searchStrings = Arrays.asList(args.split(" "));
169: ArrayList<String> results = new ArrayList<String>();
170:
171: findFile(getGlobalContext().getRoot(), results, searchStrings,
172: user, false, true);
173:
174: if (!results.isEmpty()) {
175: for (String res : results)
176: out.add(res);
177: } else {
178: out.add(ReplacerUtils.jprintf("find.nodirs", env,
179: Find.class));
180: findFile(getGlobalContext().getRoot(), results,
181: searchStrings, user, true, false);
182: if (results.isEmpty())
183: out.add(ReplacerUtils.jprintf("find.noresult", env,
184: Find.class));
185: else
186: for (String res : results)
187: out.add(res);
188: }
189:
190: if (results.size() >= 5) {
191: out.add("Search limit reached, refine your search!");
192: }
193: return out;
194: }
195: }
|