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.Collection;
025: import java.util.Collections;
026: import java.util.Properties;
027: import java.util.StringTokenizer;
028:
029: import net.sf.drftpd.util.ReplacerUtils;
030: import net.sf.drftpd.util.UserComparator;
031:
032: import org.apache.log4j.Logger;
033: import org.drftpd.Bytes;
034: import org.drftpd.GlobalContext;
035: import org.drftpd.commands.TransferStatistics;
036: import org.drftpd.plugins.SiteBot;
037: import org.drftpd.plugins.Trial;
038: import org.drftpd.sitebot.IRCCommand;
039: import org.drftpd.usermanager.User;
040: import org.drftpd.usermanager.UserFileException;
041: import org.tanesha.replacer.ReplacerEnvironment;
042:
043: import f00f.net.irc.martyr.commands.MessageCommand;
044: import f00f.net.irc.martyr.util.FullNick;
045:
046: /**
047: * @author Teflon
048: * @version $Id$
049: */
050: public class Rank extends IRCCommand {
051: private static final Logger logger = Logger.getLogger(Rank.class);
052: private String _exemptGroups;
053:
054: public Rank(GlobalContext gctx) {
055: super (gctx);
056: loadConf("conf/drmods.conf");
057: }
058:
059: public void loadConf(String confFile) {
060: Properties cfg = new Properties();
061: FileInputStream file = null;
062: try {
063: file = new FileInputStream(confFile);
064: cfg.load(file);
065: _exemptGroups = cfg.getProperty("rank.exempt");
066: if (_exemptGroups == null) {
067: throw new RuntimeException(
068: "Unspecified value 'rank.exempt' in "
069: + confFile);
070: }
071: } catch (FileNotFoundException e) {
072: logger.error("Error reading " + confFile, e);
073: throw new RuntimeException(e.getMessage());
074: } catch (IOException e) {
075: logger.error("Error reading " + confFile, e);
076: throw new RuntimeException(e.getMessage());
077: } finally {
078: if (file != null) {
079: try {
080: file.close();
081: } catch (IOException e) {
082: }
083: }
084: }
085: }
086:
087: public ArrayList<String> doRank(String args, MessageCommand msgc) {
088: ArrayList<String> out = new ArrayList<String>();
089: ReplacerEnvironment env = new ReplacerEnvironment(
090: SiteBot.GLOBAL_ENV);
091: env.add("ircnick", msgc.getSource().getNick());
092:
093: FullNick fn = msgc.getSource();
094: String ident = fn.getNick() + "!" + fn.getUser() + "@"
095: + fn.getHost();
096: User user;
097: if (args.equals("")) {
098: try {
099: user = getGlobalContext().getUserManager()
100: .getUserByIdent(ident);
101: } catch (Exception e) {
102: logger.warn("Could not identify " + ident);
103: out.add(ReplacerUtils.jprintf("ident.noident", env,
104: SiteBot.class));
105: return out;
106: }
107: } else {
108: try {
109: user = getGlobalContext().getUserManager()
110: .getUserByName(args);
111: } catch (Exception e) {
112: logger.error(args + " is not a valid username", e);
113: env.add("user", args);
114: out.add(ReplacerUtils.jprintf("rank.error", env,
115: Rank.class));
116: return out;
117: }
118: }
119:
120: StringTokenizer st = new StringTokenizer(_exemptGroups);
121: while (st.hasMoreTokens()) {
122: if (user.isMemberOf(st.nextToken())) {
123: env.add("eusr", user.getName());
124: env.add("egrp", user.getGroup());
125: out.add(ReplacerUtils.jprintf("rank.exempt", env,
126: Rank.class));
127: return out;
128: }
129: }
130:
131: Collection<User> users;
132: try {
133: users = getGlobalContext().getUserManager().getAllUsers();
134: } catch (UserFileException e) {
135: out.add("Error processing userfiles");
136: return out;
137: }
138: String type = "MONTHUP";
139:
140: boolean allow = false;
141: String exempt[] = _exemptGroups.split(" ");
142: ArrayList<User> filteredusers = new ArrayList<User>();
143: for (User fuser : users) {
144: allow = true;
145: for (int i = 0; i < exempt.length; i++) {
146: if (fuser.isMemberOf(exempt[i]))
147: allow = false;
148: }
149: if (allow && !fuser.isDeleted())
150: filteredusers.add(fuser);
151: }
152:
153: Collections.sort(filteredusers, new UserComparator(type));
154:
155: int pos = filteredusers.indexOf(user);
156: env.add("user", user.getName());
157: env.add("group", user.getGroup());
158: env
159: .add("mnup", Bytes.formatBytes(user
160: .getUploadedBytesMonth()));
161: env.add("upfilesmonth", "" + user.getUploadedFilesMonth());
162: env.add("mnrateup", TransferStatistics.getUpRate(user,
163: Trial.PERIOD_MONTHLY));
164: if (pos == 0) {
165: out.add(ReplacerUtils
166: .jprintf("rank.ontop", env, Rank.class));
167: } else {
168: User prevUser = filteredusers.get(pos - 1);
169: env.add("pos", "" + (pos + 1));
170: env.add("toup", Bytes.formatBytes(prevUser
171: .getUploadedBytesMonth()
172: - user.getUploadedBytesMonth()));
173: env.add("puser", prevUser.getName());
174: env.add("pgroup", prevUser.getGroup());
175: out.add(ReplacerUtils.jprintf("rank.losing", env,
176: Rank.class));
177: }
178: return out;
179: }
180:
181: public ArrayList<String> doTopRank(String args, MessageCommand msgc) {
182: ArrayList<String> out = new ArrayList<String>();
183: ReplacerEnvironment env = new ReplacerEnvironment(
184: SiteBot.GLOBAL_ENV);
185: env.add("ircnick", msgc.getSource().getNick());
186:
187: int count = 10;
188: try {
189: count = Integer.parseInt(args);
190: } catch (NumberFormatException e1) {
191: }
192:
193: Collection<User> users;
194: try {
195: users = getGlobalContext().getUserManager().getAllUsers();
196: } catch (UserFileException e) {
197: out.add("Error processing userfiles");
198: return out;
199: }
200:
201: String type = "MONTHUP";
202: boolean allow = false;
203: String exempt[] = _exemptGroups.split(" ");
204: ArrayList<User> filteredusers = new ArrayList<User>();
205: for (User fuser : users) {
206: allow = true;
207: for (int i = 0; i < exempt.length; i++) {
208: if (fuser.isMemberOf(exempt[i]))
209: allow = false;
210: }
211: if (allow && !fuser.isDeleted())
212: filteredusers.add(fuser);
213: }
214:
215: Collections.sort(filteredusers, new UserComparator(type));
216:
217: for (int pos = 0; pos < filteredusers.size(); pos++) {
218: if (pos >= count)
219: break;
220: User user = filteredusers.get(pos);
221: env.add("user", user.getName());
222: env.add("group", user.getGroup());
223: env.add("mnup", Bytes.formatBytes(user
224: .getUploadedBytesMonth()));
225: env.add("upfilesmonth", "" + user.getUploadedFilesMonth());
226: env.add("mnrateup", TransferStatistics.getUpRate(user,
227: Trial.PERIOD_MONTHLY));
228: if (pos == 0) {
229: out.add(ReplacerUtils.jprintf("rank.ontop", env,
230: Rank.class));
231: } else if (pos > 0) {
232: User prevUser = filteredusers.get(pos - 1);
233: env.add("pos", "" + (pos + 1));
234: env.add("toup", Bytes.formatBytes(prevUser
235: .getUploadedBytesMonth()
236: - user.getUploadedBytesMonth()));
237: env.add("puser", prevUser.getName());
238: env.add("pgroup", prevUser.getGroup());
239: out.add(ReplacerUtils.jprintf("rank.losing", env,
240: Rank.class));
241: } else {
242: env.add("user", args);
243: out.add(ReplacerUtils.jprintf("rank.error", env,
244: Rank.class));
245: return out;
246: }
247: }
248: return out;
249: }
250: }
|