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.text.SimpleDateFormat;
023: import java.util.ArrayList;
024: import java.util.Collection;
025: import java.util.Collections;
026: import java.util.Date;
027: import java.util.Iterator;
028: import java.util.Properties;
029: import java.util.StringTokenizer;
030:
031: import net.sf.drftpd.util.ReplacerUtils;
032:
033: import org.apache.log4j.Logger;
034: import org.drftpd.Bytes;
035: import org.drftpd.GlobalContext;
036: import org.drftpd.plugins.SiteBot;
037: import org.drftpd.remotefile.LinkedRemoteFileInterface;
038: import org.drftpd.remotefile.TimeComparator;
039: import org.drftpd.sections.SectionInterface;
040: import org.drftpd.sitebot.IRCCommand;
041: import org.tanesha.replacer.ReplacerEnvironment;
042:
043: import f00f.net.irc.martyr.commands.MessageCommand;
044:
045: /**
046: * @author Teflon
047: */
048: public class New extends IRCCommand {
049: private static final Logger logger = Logger.getLogger(Kick.class);
050: private int _defaultCount;
051: private int _maxCount;
052: private String _dateFormat;
053: private ArrayList<String> _excludeSections;
054:
055: public New(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 defaultCount = cfg.getProperty("new.default");
067: String maxCount = cfg.getProperty("new.max");
068: _dateFormat = cfg.getProperty("new.dateformat");
069: String excludeSections = cfg.getProperty("new.exclude");
070: if (defaultCount == null) {
071: throw new RuntimeException(
072: "Unspecified value 'new.default' in "
073: + confFile);
074: }
075: if (maxCount == null) {
076: throw new RuntimeException(
077: "Unspecified value 'new.max' in " + confFile);
078: }
079: if (excludeSections == null) {
080: throw new RuntimeException(
081: "Unspecified value 'new.exclude' in "
082: + confFile);
083: }
084: if (_dateFormat == null) {
085: throw new RuntimeException(
086: "Unspecified value 'new.dateformat' in "
087: + confFile);
088: }
089: _defaultCount = Integer.parseInt(defaultCount);
090: _maxCount = Integer.parseInt(maxCount);
091: StringTokenizer st = new StringTokenizer(excludeSections);
092: _excludeSections = new ArrayList<String>();
093: while (st.hasMoreTokens())
094: _excludeSections.add(st.nextToken());
095: } catch (Exception e) {
096: logger.error("Error reading " + confFile, e);
097: throw new RuntimeException(e.getMessage());
098: } finally {
099: try {
100: if (file != null) {
101: file.close();
102: }
103: } catch (IOException e) {
104: }
105: }
106: }
107:
108: public ArrayList<String> doNew(String args, MessageCommand msgc) {
109: ArrayList<String> out = new ArrayList<String>();
110: ReplacerEnvironment env = new ReplacerEnvironment(
111: SiteBot.GLOBAL_ENV);
112:
113: String secname = "*";
114: int count = _defaultCount;
115: StringTokenizer st = new StringTokenizer(args);
116: if (st.countTokens() != 0) {
117: String arg1 = st.nextToken();
118: try {
119: count = Integer.parseInt(arg1);
120: } catch (NumberFormatException e) {
121: secname = arg1;
122: if (st.hasMoreTokens()) {
123: try {
124: count = Integer.parseInt(st.nextToken());
125: } catch (NumberFormatException e1) {
126: }
127: }
128: }
129: }
130:
131: if (count > _maxCount)
132: count = _maxCount;
133:
134: Collection<SectionInterface> sections;
135: if (secname.equals("*")) {
136: sections = getGlobalContext().getSectionManager()
137: .getSections();
138: } else {
139: sections = new ArrayList<SectionInterface>();
140: SectionInterface si = getGlobalContext()
141: .getSectionManager().getSection(secname);
142: if (si.getName().equals("")) {
143: env.add("input", secname);
144: out.add(ReplacerUtils.jprintf("badsection", env,
145: New.class));
146: return out;
147: }
148: sections.add(si);
149: }
150:
151: ArrayList<LinkedRemoteFileInterface> dirs = new ArrayList<LinkedRemoteFileInterface>();
152: for (Iterator iter = sections.iterator(); iter.hasNext();) {
153: SectionInterface si = (SectionInterface) iter.next();
154: if (_excludeSections.contains(si.getName()))
155: continue;
156: for (LinkedRemoteFileInterface dir : si.getFile()
157: .getDirectories()) {
158: dirs.add(dir);
159: }
160: }
161:
162: SimpleDateFormat dateFormat = new SimpleDateFormat(_dateFormat);
163:
164: Collections.sort(dirs, new TimeComparator());
165: int index = 0;
166: for (LinkedRemoteFileInterface dir : dirs) {
167: if (index >= count)
168: break;
169: env.add("dir", dir.getName());
170: env.add("path", dir.getPath());
171: env.add("date", dateFormat.format(new Date(dir
172: .lastModified())));
173: env.add("owner", dir.getUsername());
174: env.add("group", dir.getGroupname());
175: env.add("section", getGlobalContext().getSectionManager()
176: .lookup(dir.getPath()).getName());
177: env.add("size", Bytes.formatBytes(dir.length()));
178: env.add("pos", "" + (index + 1));
179: env.add("files", "" + dir.getFiles().size());
180: out.add(ReplacerUtils.jprintf("announce", env, New.class));
181: index++;
182: }
183:
184: if (out.isEmpty())
185: out.add(ReplacerUtils.jprintf("usage", env, New.class));
186:
187: return out;
188: }
189:
190: }
|