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.FileNotFoundException;
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.Properties;
026: import java.util.StringTokenizer;
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.dynamicdata.Key;
034: import org.drftpd.plugins.SiteBot;
035: import org.drftpd.remotefile.LinkedRemoteFileInterface;
036: import org.drftpd.sitebot.IRCCommand;
037: import org.drftpd.usermanager.User;
038: import org.tanesha.replacer.ReplacerEnvironment;
039:
040: import f00f.net.irc.martyr.commands.MessageCommand;
041: import f00f.net.irc.martyr.util.FullNick;
042:
043: /**
044: * @author Kolor & Teflon
045: * @version $Id$
046: */
047: public class Request extends IRCCommand {
048: private static final Logger logger = Logger
049: .getLogger(Request.class);
050: public static final Key REQUESTS = new Key(Request.class,
051: "requests", Integer.class);
052: public static final Key REQFILLED = new Key(Request.class,
053: "reqfilled", Integer.class);
054: public static final Key WEEKREQS = new Key(Request.class,
055: "weekreq", Integer.class);
056:
057: private String _requestPath;
058:
059: public Request(GlobalContext gctx) {
060: super (gctx);
061: loadConf("conf/drmods.conf");
062: }
063:
064: public void loadConf(String confFile) {
065: Properties cfg = new Properties();
066: FileInputStream file = null;
067: try {
068: file = new FileInputStream(confFile);
069: cfg.load(file);
070: _requestPath = cfg.getProperty("request.dirpath");
071: if (_requestPath == null) {
072: throw new RuntimeException(
073: "Unspecified value 'request.dirpath' in "
074: + confFile);
075: }
076: } catch (FileNotFoundException e) {
077: logger.error("Error reading " + confFile, e);
078: throw new RuntimeException(e.getMessage());
079: } catch (IOException e) {
080: logger.error("Error reading " + confFile, e);
081: throw new RuntimeException(e.getMessage());
082: } finally {
083: if (file != null) {
084: try {
085: file.close();
086: } catch (IOException e) {
087: }
088: }
089: }
090: }
091:
092: public ArrayList<String> doRequests(String args, MessageCommand msgc) {
093: ArrayList<String> out = new ArrayList<String>();
094: ReplacerEnvironment env = new ReplacerEnvironment(
095: SiteBot.GLOBAL_ENV);
096: env.add("ircnick", msgc.getSource().getNick());
097:
098: User user = getUser(msgc.getSource());
099: if (user == null) {
100: out.add(ReplacerUtils.jprintf("ident.noident", env,
101: SiteBot.class));
102: return out;
103: }
104: env.add("ftpuser", user.getName());
105: env.add("reqfilled", new StringTokenizer(msgc.getMessage())
106: .nextToken());
107:
108: try {
109: LinkedRemoteFileInterface rdir = getGlobalContext()
110: .getRoot().lookupFile(_requestPath);
111: out.add(ReplacerUtils.jprintf("requests.header", env,
112: Request.class));
113: int i = 1;
114: for (Iterator iter = rdir.getDirectories().iterator(); iter
115: .hasNext();) {
116: LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter
117: .next();
118: if (file.isDirectory()) {
119: // if (file.getName().startsWith("REQUEST")) {
120: StringTokenizer st = new StringTokenizer(file
121: .getName(), "-");
122: if (st.nextToken().equals("REQUEST")) {
123: String byuser = st.nextToken();
124: String request = st.nextToken();
125: while (st.hasMoreTokens()) {
126: request = request + "-" + st.nextToken();
127: }
128: byuser = byuser.replace('.', ' ');
129: String num = Integer.toString(i);
130: env.add("num", num);
131: env
132: .add("requser", byuser.replaceAll(
133: "by ", ""));
134: env.add("reqrequest", request);
135: i = i + 1;
136: out.add(ReplacerUtils.jprintf("requests.list",
137: env, Request.class));
138: }
139: }
140: }
141: out.add(ReplacerUtils.jprintf("requests.footer", env,
142: Request.class));
143: } catch (FileNotFoundException e) {
144: env.add("rdirname", _requestPath);
145: out.add(ReplacerUtils.jprintf("request.error", env,
146: Request.class));
147: return out;
148: }
149: return out;
150: }
151:
152: public ArrayList<String> doReqfilled(String args,
153: MessageCommand msgc) {
154: ArrayList<String> out = new ArrayList<String>();
155: ReplacerEnvironment env = new ReplacerEnvironment(
156: SiteBot.GLOBAL_ENV);
157: env.add("ircnick", msgc.getSource().getNick());
158:
159: User user = getUser(msgc.getSource());
160: if (user == null) {
161: out.add(ReplacerUtils.jprintf("ident.noident", env,
162: SiteBot.class));
163: return out;
164: }
165: env.add("ftpuser", user.getName());
166:
167: String dirName;
168: try {
169: dirName = args;
170: if (dirName.length() == 0) {
171: out.add(ReplacerUtils.jprintf("reqfilled.usage", env,
172: Request.class));
173: return out;
174: }
175: } catch (ArrayIndexOutOfBoundsException e) {
176: logger.warn("", e);
177: out.add(ReplacerUtils.jprintf("reqfilled.usage", env,
178: Request.class));
179: return out;
180: } catch (StringIndexOutOfBoundsException e) {
181: logger.warn("", e);
182: out.add(ReplacerUtils.jprintf("reqfilled.usage", env,
183: Request.class));
184: return out;
185: }
186:
187: env.add("fdirname", dirName);
188:
189: boolean nodir = false;
190: boolean fdir = false;
191:
192: try {
193: LinkedRemoteFileInterface dir = getGlobalContext()
194: .getRoot().lookupFile(_requestPath);
195: for (Iterator iter = dir.getDirectories().iterator(); iter
196: .hasNext();) {
197: LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter
198: .next();
199: if (file.isDirectory()) {
200: if (file.getName().endsWith(dirName)) {
201: nodir = false;
202: String fdirname = file.getName();
203: fdirname = fdirname.replaceAll("REQUEST-by.",
204: "FILLED-for.");
205: user.getKeyedMap().setObject(
206: Request.REQFILLED,
207: user.getKeyedMap().getObjectInt(
208: Request.REQFILLED) + 1);
209: ;
210: try {
211: file.renameTo(file.getParentFile()
212: .getPath(), fdirname);
213: fdir = true;
214: out.add(ReplacerUtils.jprintf(
215: "reqfilled.success", env,
216: Request.class));
217: break;
218: } catch (IOException e) {
219: logger.warn("", e);
220: }
221:
222: } else
223: nodir = true;
224: }
225: }
226:
227: if (nodir && !fdir)
228: out.add(ReplacerUtils.jprintf("reqfilled.error", env,
229: Request.class));
230:
231: } catch (FileNotFoundException e) {
232: env.add("rdirname", _requestPath);
233: out.add(ReplacerUtils.jprintf("request.error", env,
234: Request.class));
235: return out;
236: }
237: return out;
238: }
239:
240: public ArrayList<String> doRequest(String args, MessageCommand msgc) {
241: ArrayList<String> out = new ArrayList<String>();
242: ReplacerEnvironment env = new ReplacerEnvironment(
243: SiteBot.GLOBAL_ENV);
244: env.add("ircnick", msgc.getSource().getNick());
245:
246: User user = getUser(msgc.getSource());
247: if (user == null) {
248: out.add(ReplacerUtils.jprintf("ident.noident", env,
249: SiteBot.class));
250: return out;
251: }
252: env.add("ftpuser", user.getName());
253:
254: String dirName = args;
255: if (dirName.length() == 0) {
256: out.add(ReplacerUtils.jprintf("request.usage", env,
257: Request.class));
258: return out;
259: }
260:
261: env.add("rdirname", dirName);
262: String requser = user.getName();
263:
264: try {
265: LinkedRemoteFileInterface dir = getGlobalContext()
266: .getRoot().lookupFile(_requestPath);
267: dir
268: .createDirectory("REQUEST-by." + requser + "-"
269: + dirName);
270: LinkedRemoteFileInterface reqdir = dir
271: .getFile("REQUEST-by." + requser + "-" + dirName);
272: reqdir.setOwner(requser);
273: user.getKeyedMap()
274: .setObject(
275: Request.REQUESTS,
276: user.getKeyedMap().getObjectInt(
277: Request.REQUESTS) + 1);
278: ;
279: out.add(ReplacerUtils.jprintf("request.success", env,
280: Request.class));
281: } catch (FileNotFoundException e) {
282: env.add("rdirname", _requestPath);
283: out.add(ReplacerUtils.jprintf("request.error", env,
284: Request.class));
285: return out;
286: } catch (FileExistsException e1) {
287: out.add(ReplacerUtils.jprintf("request.exists", env,
288: Request.class));
289: return out;
290: }
291: return out;
292: }
293:
294: public ArrayList<String> doReqdel(String args, MessageCommand msgc) {
295: ArrayList<String> out = new ArrayList<String>();
296: ReplacerEnvironment env = new ReplacerEnvironment(
297: SiteBot.GLOBAL_ENV);
298: env.add("ircnick", msgc.getSource().getNick());
299:
300: User user = getUser(msgc.getSource());
301: if (user == null) {
302: out.add(ReplacerUtils.jprintf("ident.noident", env,
303: SiteBot.class));
304: return out;
305: }
306: env.add("ftpuser", user.getName());
307:
308: String dirName = args;
309: if (dirName.length() == 0) {
310: out.add(ReplacerUtils.jprintf("reqdel.usage", env,
311: Request.class));
312: return out;
313: }
314:
315: env.add("ddirname", dirName);
316:
317: boolean nodir = false;
318: boolean deldir = false;
319: try {
320: LinkedRemoteFileInterface dir = getGlobalContext()
321: .getRoot().lookupFile(_requestPath);
322: for (Iterator iter = dir.getDirectories().iterator(); iter
323: .hasNext();) {
324: LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter
325: .next();
326: if (file.isDirectory()) {
327: if (file.getName().endsWith(dirName)) {
328: nodir = false;
329: if (file.getUsername().equals(user.getName())
330: || user.isAdmin()) {
331: file.delete();
332: deldir = true;
333: out.add(ReplacerUtils.jprintf(
334: "reqdel.success", env,
335: Request.class));
336: break;
337: } else {
338: out.add(ReplacerUtils.jprintf(
339: "reqdel.notowner", env,
340: Request.class));
341: break;
342: }
343: } else
344: nodir = true;
345: }
346: }
347:
348: if (nodir && !deldir)
349: out.add(ReplacerUtils.jprintf("reqdel.error", env,
350: Request.class));
351:
352: } catch (FileNotFoundException e) {
353: out.add(ReplacerUtils.jprintf("reqdel.error", env,
354: Request.class));
355: return out;
356: }
357: return out;
358: }
359:
360: /* private static LinkedRemoteFileInterface findDir(
361: GlobalContext gctx,
362: LinkedRemoteFileInterface dir,
363: User user,
364: String searchstring) {
365:
366: if (!gctx.getConfig().checkPathPermission("privpath", user, dir, true)) {
367: Logger.getLogger(Approve.class).debug("privpath: "+dir.getPath());
368: return null;
369: }
370:
371: for (Iterator iter = dir.getDirectories().iterator(); iter.hasNext();) {
372: LinkedRemoteFileInterface file = (LinkedRemoteFileInterface) iter.next();
373: if (file.isDirectory()) {
374: if (file.getName().toLowerCase().equals(searchstring.toLowerCase())) {
375: logger.info("Found " + file.getPath());
376: return file;
377: }
378: LinkedRemoteFileInterface dir2 = findDir(gctx, file, user, searchstring);
379: if (dir2 != null) {
380: return dir2;
381: }
382: }
383: }
384: return null;
385: }*/
386:
387: private User getUser(FullNick fn) {
388: String ident = fn.getNick() + "!" + fn.getUser() + "@"
389: + fn.getHost();
390: User user = null;
391: try {
392: user = getGlobalContext().getUserManager().getUserByIdent(
393: ident);
394: } catch (Exception e) {
395: logger.warn("Could not identify " + ident);
396: }
397: return user;
398: }
399: }
|