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 org.drftpd.plugins;
019:
020: import java.io.FileNotFoundException;
021: import java.util.Collection;
022: import java.util.Iterator;
023:
024: import net.sf.drftpd.NoAvailableSlaveException;
025: import net.sf.drftpd.ObjectNotFoundException;
026: import net.sf.drftpd.event.Event;
027: import net.sf.drftpd.event.FtpListener;
028: import net.sf.drftpd.event.TransferEvent;
029:
030: import org.apache.log4j.Logger;
031: import org.drftpd.Bytes;
032: import org.drftpd.Time;
033: import org.drftpd.plugins.SiteBot.Ret;
034: import org.drftpd.remotefile.LinkedRemoteFileInterface;
035: import org.tanesha.replacer.FormatterException;
036: import org.tanesha.replacer.ReplacerEnvironment;
037: import org.tanesha.replacer.SimplePrintf;
038:
039: public class DIZPlugin extends FtpListener {
040: private static final String _ehdr = "DIZFile exception:";
041:
042: private static final Logger logger = Logger
043: .getLogger(DIZPlugin.class);
044:
045: public DIZPlugin() {
046: logger.info("DIZPlugin loaded successfully");
047: }
048:
049: public void actionPerformed(Event event) {
050: // If this transfer event is STOR
051: if (event.getCommand().equals("STOR")) {
052: actionPerformedSTOR((TransferEvent) event);
053: }
054:
055: }
056:
057: public synchronized void actionPerformedSTOR(TransferEvent xferEvent) {
058: // Is this a zip file?
059: if (!isZipFile(xferEvent.getDirectory())) {
060: return;
061: }
062:
063: // Are we logging this directory for this user?
064: if (!getGlobalContext().getConfig()
065: .checkPathPermission("dirlog", xferEvent.getUser(),
066: xferEvent.getDirectory())) {
067: return;
068: }
069: DIZFile diz = null;
070: try {
071: diz = new DIZFile(xferEvent.getDirectory());
072: } catch (FileNotFoundException e) {
073: return;
074: } catch (NoAvailableSlaveException e) {
075: return;
076: }
077:
078: if (zipFilesPresent(diz.getParent()) == diz.getTotal()) {
079: if ((diz.getTotal() - DIZPlugin.zipFilesPresent(diz
080: .getParent())) <= 0) {
081: ReplacerEnvironment env = new ReplacerEnvironment(
082: SiteBot.GLOBAL_ENV);
083:
084: try {
085: Ret ret = getSiteBot().getPropertyFileSuffix(
086: "store.complete.diz", diz.getParent());
087:
088: Collection racers = SiteBot.userSort(diz
089: .getParent().getFiles(), "bytes", "high");
090:
091: Collection groups = SiteBot.topFileGroup(diz
092: .getParent().getFiles());
093:
094: env.add("section", "" + ret.getSection().getName());
095: env.add("path", "" + diz.getParent().getName());
096: env.add("size", ""
097: + Bytes.formatBytes(zipDirSize(diz
098: .getParent())));
099: env.add("files", "" + diz.getTotal());
100: env
101: .add(
102: "speed",
103: ""
104: + (zipDirSize(diz
105: .getParent()) / zipDirTime(diz
106: .getParent()))
107: + "KB/s");
108: env.add("time", ""
109: + Time.formatTime(zipDirTime(diz
110: .getParent())));
111: env.add("racers", "" + racers.size());
112: env.add("groups", "" + groups.size());
113:
114: getSiteBot().say(ret.getSection(),
115: SimplePrintf.jprintf(ret.getFormat(), env));
116: } catch (FormatterException e) {
117: logger.warn("", e);
118: } catch (ObjectNotFoundException e) {
119: return; // sitebot not loaded
120: }
121: }
122: }
123:
124: return;
125: }
126:
127: public SiteBot getSiteBot() throws ObjectNotFoundException {
128: return (SiteBot) getGlobalContext().getFtpListener(
129: SiteBot.class);
130: }
131:
132: public static boolean isZipFile(LinkedRemoteFileInterface file) {
133: return file.getName().toLowerCase().endsWith(".zip")
134: && file.isFile();
135: }
136:
137: public static long zipDirSize(LinkedRemoteFileInterface dir) {
138:
139: long size = 0;
140: for (LinkedRemoteFileInterface aFile : dir.getFiles2()) {
141: if (isZipFile(aFile)) {
142: size += aFile.length();
143: }
144: }
145: return size;
146: }
147:
148: public static long zipDirTime(LinkedRemoteFileInterface dir) {
149: long time = 0;
150: for (LinkedRemoteFileInterface aFile : dir.getFiles2()) {
151: if (isZipFile(aFile)) {
152: time += aFile.getXfertime();
153: }
154: }
155: return time;
156: }
157:
158: public static int zipFilesPresent(LinkedRemoteFileInterface dir) {
159: int total = 0;
160: for (LinkedRemoteFileInterface aFile : dir.getFiles2()) {
161: if (isZipFile(aFile) && aFile.length() != 0) {
162: total++;
163: }
164: }
165: return total;
166: }
167:
168: public static int zipFilesOnline(LinkedRemoteFileInterface dir) {
169: int total = 0;
170: for (LinkedRemoteFileInterface aFile : dir.getFiles2()) {
171: if (isZipFile(aFile) && aFile.length() != 0
172: && aFile.isAvailable()) {
173: total++;
174: }
175: }
176: return total;
177: }
178:
179: public static int zipFilesOffline(LinkedRemoteFileInterface dir) {
180: return zipFilesPresent(dir) - zipFilesOnline(dir);
181: }
182:
183: public static LinkedRemoteFileInterface getZipFile(
184: LinkedRemoteFileInterface dir)
185: throws NoAvailableSlaveException {
186: for (LinkedRemoteFileInterface aFile : dir.getFiles2()) {
187: if (isZipFile(aFile) && aFile.isAvailable()
188: && (aFile.length() != 0)
189: && (aFile.getXfertime() != -1)) {
190: return aFile;
191: }
192:
193: }
194: throw new NoAvailableSlaveException();
195: }
196: }
|