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.Calendar;
022: import java.util.Iterator;
023:
024: import net.sf.drftpd.ObjectNotFoundException;
025: import net.sf.drftpd.master.BaseFtpConnection;
026: import net.sf.drftpd.util.ReplacerUtils;
027:
028: import org.apache.log4j.Logger;
029: import org.drftpd.Bytes;
030: import org.drftpd.GlobalContext;
031: import org.drftpd.plugins.SiteBot;
032: import org.drftpd.plugins.Trial;
033: import org.drftpd.plugins.Trial.Limit;
034: import org.drftpd.usermanager.User;
035: import org.tanesha.replacer.ReplacerEnvironment;
036:
037: import f00f.net.irc.martyr.commands.MessageCommand;
038: import f00f.net.irc.martyr.util.FullNick;
039:
040: public class Trials extends IRCCommand {
041: private static final Logger logger = Logger.getLogger(Trials.class);
042:
043: public Trials(GlobalContext gctx) {
044: super (gctx);
045: }
046:
047: protected String jprintf(String key, User user, Limit limit,
048: long bytesleft, boolean unique) {
049: ReplacerEnvironment env = new ReplacerEnvironment(
050: SiteBot.GLOBAL_ENV);
051: env.add("user", user.getName());
052:
053: if (limit != null) {
054: env.add("period", Trial.getPeriodName(limit.getPeriod()));
055: env.add("name", limit.getName());
056:
057: if (unique) {
058: env.add("bonusexpires", Trial.getCalendarForEndOfBonus(
059: user, limit.getPeriod()).getTime());
060: env.add("uniqueexpires", Trial
061: .getCalendarForEndOfFirstPeriod(user,
062: limit.getPeriod()).getTime());
063: }
064:
065: env.add("expires", Trial.getCalendarForEndOfPeriod(
066: limit.getPeriod()).getTime());
067: }
068:
069: env.add("bytesleft", Bytes.formatBytes(bytesleft));
070: env.add("bytespassed", Bytes.formatBytes(-bytesleft));
071:
072: return BaseFtpConnection.jprintf(Trials.class, key, env, user);
073: }
074:
075: public ArrayList<String> doPassed(String args, MessageCommand msgc) {
076: ArrayList<String> out = new ArrayList<String>();
077: ReplacerEnvironment env = new ReplacerEnvironment(
078: SiteBot.GLOBAL_ENV);
079:
080: Trial trial;
081: try {
082: trial = (Trial) getGlobalContext().getFtpListener(
083: Trial.class);
084: } catch (ObjectNotFoundException e) {
085: logger
086: .warn("Trial plugin not loaded ... !passed will be disabled.");
087: trial = null;
088: }
089:
090: if (trial == null) {
091: out.add(ReplacerUtils
092: .jprintf("disabled", env, Trials.class));
093: return out;
094: }
095:
096: FullNick fn = msgc.getSource();
097: String ident = fn.getNick() + "!" + fn.getUser() + "@"
098: + fn.getHost();
099: User user;
100: if (args.equals("")) {
101: try {
102: user = getGlobalContext().getUserManager()
103: .getUserByIdent(ident);
104: } catch (Exception e) {
105: logger.warn("Could not identify " + ident);
106: out.add(ReplacerUtils.jprintf("ident.noident", env,
107: SiteBot.class));
108: return out;
109: }
110: } else {
111: try {
112: user = getGlobalContext().getUserManager()
113: .getUserByName(args);
114: } catch (Exception e) {
115: env.add("user", args);
116: out.add(ReplacerUtils.jprintf("nosuchuser", env,
117: Trials.class));
118: return out;
119: }
120: }
121: env.add("user", user.getName());
122:
123: int i = 0;
124:
125: for (Iterator iter = trial.getLimits().iterator(); iter
126: .hasNext();) {
127: Limit limit = (Limit) iter.next();
128:
129: if (limit.getPerm().check(user)) {
130: i++;
131:
132: Calendar endofbonus = Trial.getCalendarForEndOfBonus(
133: user, limit.getPeriod());
134:
135: if (System.currentTimeMillis() <= endofbonus
136: .getTimeInMillis()) {
137: //in bonus or unique period
138: long bytesleft = limit.getBytes()
139: - user.getUploadedBytes();
140:
141: if (bytesleft <= 0) {
142: //in bonus or passed
143: out.add(jprintf("passedunique", user, limit,
144: bytesleft, true));
145: } else {
146: //in unique period
147: out.add(jprintf("trialunique", user, limit,
148: bytesleft, true));
149: }
150: } else {
151: //plain trial
152: long bytesleft = limit.getBytes()
153: - Trial.getUploadedBytesForPeriod(user,
154: limit.getPeriod());
155:
156: if (bytesleft <= 0) {
157: out.add(jprintf("passed", user, limit,
158: bytesleft, false));
159: } else {
160: out.add(jprintf("trial", user, limit,
161: bytesleft, false));
162: }
163: }
164: }
165: }
166:
167: if (i == 0) {
168: out.add(jprintf("exempt", user, null, 0, false));
169: }
170:
171: return out;
172: }
173: }
|