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.sitebot;
019:
020: import java.util.ArrayList;
021: import java.util.Collection;
022: import java.util.Collections;
023: import java.util.Iterator;
024:
025: import net.sf.drftpd.master.BaseFtpConnection;
026: import net.sf.drftpd.master.config.FtpConfig;
027: import net.sf.drftpd.util.ReplacerUtils;
028: import net.sf.drftpd.util.UserComparator;
029:
030: import org.apache.log4j.Logger;
031: import org.drftpd.Bytes;
032: import org.drftpd.GlobalContext;
033: import org.drftpd.commands.TransferStatistics;
034: import org.drftpd.permissions.Permission;
035: import org.drftpd.plugins.SiteBot;
036: import org.drftpd.plugins.Trial;
037: import org.drftpd.usermanager.User;
038: import org.drftpd.usermanager.UserFileException;
039: import org.tanesha.replacer.FormatterException;
040: import org.tanesha.replacer.ReplacerEnvironment;
041: import org.tanesha.replacer.SimplePrintf;
042:
043: import f00f.net.irc.martyr.commands.MessageCommand;
044:
045: /**
046: * @author zubov
047: * @version $Id: Stats.java 1513 2006-10-13 22:41:08Z tdsoul $
048: */
049: public class Stats extends IRCCommand {
050: private static final Logger logger = Logger.getLogger(Stats.class);
051: private SiteBot _listener;
052:
053: public Stats(GlobalContext gctx) {
054: super (gctx);
055: }
056:
057: public ArrayList<String> doALUP(String args, MessageCommand msgc) {
058: return doStats(args, "ALUP");
059: }
060:
061: public ArrayList<String> doALDN(String args, MessageCommand msgc) {
062: return doStats(args, "ALDN");
063: }
064:
065: public ArrayList<String> doMONTHUP(String args, MessageCommand msgc) {
066: return doStats(args, "MONTHUP");
067: }
068:
069: public ArrayList<String> doMONTHDN(String args, MessageCommand msgc) {
070: return doStats(args, "MONTHDN");
071: }
072:
073: public ArrayList<String> doWKUP(String args, MessageCommand msgc) {
074: return doStats(args, "WKUP");
075: }
076:
077: public ArrayList<String> doWKDN(String args, MessageCommand msgc) {
078: return doStats(args, "WKDN");
079: }
080:
081: public ArrayList<String> doDAYUP(String args, MessageCommand msgc) {
082: return doStats(args, "DAYUP");
083: }
084:
085: public ArrayList<String> doDAYDN(String args, MessageCommand msgc) {
086: return doStats(args, "DAYDN");
087: }
088:
089: public ArrayList<String> doStats(String args, String type) {
090: ArrayList<String> out = new ArrayList<String>();
091: ReplacerEnvironment env = new ReplacerEnvironment(
092: SiteBot.GLOBAL_ENV);
093:
094: Collection<User> users = null;
095: try {
096: users = getGlobalContext().getUserManager().getAllUsers();
097: } catch (UserFileException e) {
098: out.add("Error processing userfiles");
099: logger.error("Error processing userfiles", e);
100: return out;
101: }
102:
103: int number = fixNumberAndUserlist(args, users);
104:
105: ArrayList<User> users2 = new ArrayList<User>(users);
106: Collections.sort(users2, new UserComparator(type));
107:
108: int i = 0;
109: for (Iterator iter = users2.iterator(); iter.hasNext();) {
110: if (++i > number) {
111: break;
112: }
113:
114: //TODO .jprintf() has most of this afaik
115: User user = (User) iter.next();
116: env = BaseFtpConnection.getReplacerEnvironment(env, user);
117: env.add("pos", "" + i);
118: env.add("user", user.getName());
119: env.add("upbytesday", Bytes.formatBytes(user
120: .getUploadedBytesDay()));
121: env.add("upfilesday", "" + user.getUploadedFilesDay());
122: env.add("uprateday", TransferStatistics.getUpRate(user,
123: Trial.PERIOD_DAILY));
124: env.add("upbytesweek", Bytes.formatBytes(user
125: .getUploadedBytesWeek()));
126: env.add("upfilesweek", "" + user.getUploadedFilesWeek());
127: env.add("uprateweek", TransferStatistics.getDownRate(user,
128: Trial.PERIOD_WEEKLY));
129: env.add("upbytesmonth", Bytes.formatBytes(user
130: .getUploadedBytesMonth()));
131: env.add("upfilesmonth", "" + user.getUploadedFilesMonth());
132: env.add("upratemonth", TransferStatistics.getUpRate(user,
133: Trial.PERIOD_MONTHLY));
134: env.add("upbytes", Bytes.formatBytes(user
135: .getUploadedBytes()));
136: env.add("upfiles", "" + user.getUploadedFiles());
137: env.add("uprate", TransferStatistics.getUpRate(user,
138: Trial.PERIOD_ALL));
139:
140: env.add("dnbytesday", Bytes.formatBytes(user
141: .getDownloadedBytesDay()));
142: env.add("dnfilesday", "" + user.getDownloadedFilesDay());
143: env.add("dnrateday", TransferStatistics.getDownRate(user,
144: Trial.PERIOD_DAILY));
145: env.add("dnbytesweek", Bytes.formatBytes(user
146: .getDownloadedBytesWeek()));
147: env.add("dnfilesweek", "" + user.getDownloadedFilesWeek());
148: env.add("dnrateweek", TransferStatistics.getDownRate(user,
149: Trial.PERIOD_WEEKLY));
150: env.add("dnbytesmonth", Bytes.formatBytes(user
151: .getDownloadedBytesMonth()));
152: env
153: .add("dnfilesmonth", ""
154: + user.getDownloadedFilesMonth());
155: env.add("dnratemonth", TransferStatistics.getDownRate(user,
156: Trial.PERIOD_MONTHLY));
157: env.add("dnbytes", Bytes.formatBytes(user
158: .getDownloadedBytes()));
159: env.add("dnfiles", "" + user.getDownloadedFiles());
160: env.add("dnrate", TransferStatistics.getDownRate(user,
161: Trial.PERIOD_ALL));
162: type = type.toLowerCase();
163:
164: try {
165: out.add(SimplePrintf.jprintf(ReplacerUtils.jprintf(
166: "transferstatistics" + type, env, Stats.class),
167: env));
168: } catch (FormatterException e) {
169: out.add("FormatterException for transferstatistics"
170: + type);
171:
172: break;
173: }
174: }
175:
176: return out;
177: }
178:
179: public static int fixNumberAndUserlist(String params,
180: Collection userList) {
181: int number = 10;
182: com.Ostermiller.util.StringTokenizer st = new com.Ostermiller.util.StringTokenizer(
183: params);
184: //st.nextToken(); // !alup
185:
186: if (!st.hasMoreTokens()) {
187: return 10;
188: }
189:
190: if (st.hasMoreTokens()) {
191: //StringTokenizer st2 = st.clone();
192: try {
193: number = Integer.parseInt(st.peek());
194: st.nextToken();
195: } catch (NumberFormatException ex) {
196: }
197:
198: while (st.hasMoreTokens()) {
199: Permission perm = new Permission(FtpConfig
200: .makeUsers(st));
201:
202: for (Iterator iter = userList.iterator(); iter
203: .hasNext();) {
204: User user = (User) iter.next();
205:
206: if (!perm.check(user)) {
207: iter.remove();
208: }
209: }
210: }
211: }
212:
213: return number;
214: }
215: }
|