001: /*
002: *
003: * This file is part of DrFTPD, Distributed FTP Daemon.
004: *
005: * DrFTPD is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * DrFTPD is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with DrFTPD; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.drmods.plugins.irc;
020:
021: import java.io.FileInputStream;
022: import java.io.FileNotFoundException;
023: import java.io.IOException;
024: import java.util.ArrayList;
025: import java.util.Iterator;
026: import java.util.Properties;
027:
028: import net.sf.drftpd.FileExistsException;
029: import net.sf.drftpd.util.ReplacerUtils;
030:
031: import org.apache.log4j.Logger;
032: import org.drftpd.GlobalContext;
033: import org.drftpd.plugins.SiteBot;
034: import org.drftpd.remotefile.LinkedRemoteFileInterface;
035: import org.drftpd.sitebot.IRCCommand;
036: import org.drftpd.usermanager.User;
037: import org.tanesha.replacer.FormatterException;
038: import org.tanesha.replacer.ReplacerEnvironment;
039: import org.tanesha.replacer.SimplePrintf;
040:
041: import f00f.net.irc.martyr.commands.MessageCommand;
042: import f00f.net.irc.martyr.util.FullNick;
043:
044: /**
045: * @author Teflon
046: * @version $Id$
047: */
048: public class Approve extends IRCCommand {
049: private static final Logger logger = Logger
050: .getLogger(Approve.class);
051: private String _dirName;
052:
053: public Approve(GlobalContext gctx) {
054: super (gctx);
055: loadConf("conf/drmods.conf");
056: }
057:
058: public void loadConf(String confFile) {
059: Properties cfg = new Properties();
060: FileInputStream file = null;
061: try {
062: file = new FileInputStream(confFile);
063: cfg.load(file);
064: _dirName = cfg.getProperty("approve.dirname");
065: if (_dirName == null) {
066: throw new RuntimeException(
067: "Unspecified value 'approve.dirname' in "
068: + confFile);
069: }
070: } catch (FileNotFoundException e) {
071: logger.error("Error reading " + confFile, e);
072: throw new RuntimeException(e.getMessage());
073: } catch (IOException e) {
074: logger.error("Error reading " + confFile, e);
075: throw new RuntimeException(e.getMessage());
076: } finally {
077: if (file != null) {
078: try {
079: file.close();
080: } catch (IOException e) {
081: }
082: }
083: }
084: }
085:
086: public ArrayList<String> doApprove(String args, MessageCommand msgc) {
087: ArrayList<String> out = new ArrayList<String>();
088: ReplacerEnvironment env = new ReplacerEnvironment(
089: SiteBot.GLOBAL_ENV);
090: env.add("ircnick", msgc.getSource().getNick());
091: env.add("sdirname", args);
092:
093: if (args.equals("")) {
094: out.add(ReplacerUtils.jprintf("approve.usage", env,
095: Approve.class));
096: return out;
097: }
098:
099: FullNick fn = msgc.getSource();
100: String ident = fn.getNick() + "!" + fn.getUser() + "@"
101: + fn.getHost();
102: User user;
103: try {
104: user = getGlobalContext().getUserManager().getUserByIdent(
105: ident);
106: env.add("ftpuser", user.getName());
107: } catch (Exception e) {
108: logger.warn("Could not identify " + ident);
109: out.add(ReplacerUtils.jprintf("ident.noident", env,
110: SiteBot.class));
111: return out;
112: }
113:
114: LinkedRemoteFileInterface dir = findDir(getGlobalContext(),
115: getGlobalContext().getRoot(), user, args);
116:
117: if (dir != null) {
118: env.add("sdirpath", dir.getPath());
119: String approveDirName;
120: try {
121: approveDirName = SimplePrintf.jprintf(_dirName, env);
122: } catch (FormatterException e) {
123: out.add(e.getMessage());
124: return out;
125: }
126: env.add("adirname", approveDirName);
127: env.add("adirpath", dir.getPath() + "/" + approveDirName);
128: try {
129: LinkedRemoteFileInterface newdir = dir
130: .createDirectory(approveDirName);
131: newdir.setOwner(user.getName());
132: newdir.setGroup(user.getGroup());
133: out.add(ReplacerUtils.jprintf("approve.success", env,
134: Approve.class));
135: } catch (FileExistsException e1) {
136: out.add(ReplacerUtils.jprintf("approve.exists", env,
137: Approve.class));
138: }
139: } else {
140: out.add(ReplacerUtils.jprintf("approve.error", env,
141: Approve.class));
142: }
143: return out;
144: }
145:
146: private static LinkedRemoteFileInterface findDir(
147: GlobalContext gctx, LinkedRemoteFileInterface dir,
148: User user, String searchstring) {
149:
150: if (!gctx.getConfig().checkPathPermission("privpath", user,
151: dir, true)) {
152: Logger.getLogger(Approve.class).debug(
153: "privpath: " + dir.getPath());
154: return null;
155: }
156:
157: for (Iterator iter = dir.getDirectories().iterator(); iter
158: .hasNext();) {
159: LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter
160: .next();
161: if (file.isDirectory()) {
162: if (file.getName().toLowerCase().equals(
163: searchstring.toLowerCase())) {
164: logger.info("Found " + file.getPath());
165: return file;
166: }
167: LinkedRemoteFileInterface dir2 = findDir(gctx, file,
168: user, searchstring);
169: if (dir2 != null) {
170: return dir2;
171: }
172: }
173: }
174: return null;
175: }
176: }
|