001: /*
002: *
003: * This file is part of DrFTPD, Distributed FTP Daemon.
004: *
005: * DrFTPD is free software; you can redistribute it and/or modify
006: * it under the terms of the GNU General Public License as published by
007: * the Free Software Foundation; either version 2 of the License, or
008: * (at your option) any later version.
009: *
010: * DrFTPD is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with DrFTPD; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: */
019: package net.drmods.plugins.irc;
020:
021: import java.util.ArrayList;
022: import java.util.Iterator;
023:
024: import net.sf.drftpd.util.ReplacerUtils;
025:
026: import org.apache.log4j.Logger;
027: import org.drftpd.Bytes;
028: import org.drftpd.GlobalContext;
029: import org.drftpd.plugins.SiteBot;
030: import org.drftpd.sitebot.IRCCommand;
031: import org.drftpd.usermanager.User;
032: import org.drftpd.usermanager.UserFileException;
033: import org.tanesha.replacer.ReplacerEnvironment;
034:
035: import f00f.net.irc.martyr.commands.MessageCommand;
036: import f00f.net.irc.martyr.util.FullNick;
037:
038: /**
039: * @author Teflon
040: * @version $Id$
041: */
042: public class Credits extends IRCCommand {
043: private static final Logger logger = Logger
044: .getLogger(Credits.class);
045:
046: public Credits(GlobalContext gctx) {
047: super (gctx);
048: }
049:
050: public ArrayList<String> doCredits(String args, MessageCommand msgc) {
051: ArrayList<String> out = new ArrayList<String>();
052: ReplacerEnvironment env = new ReplacerEnvironment(
053: SiteBot.GLOBAL_ENV);
054: env.add("ircnick", msgc.getSource().getNick());
055:
056: FullNick fn = msgc.getSource();
057: String ident = fn.getNick() + "!" + fn.getUser() + "@"
058: + fn.getHost();
059: User user;
060: if (args.equals("")) {
061: try {
062: user = getGlobalContext().getUserManager()
063: .getUserByIdent(ident);
064: } catch (Exception e) {
065: logger.warn("Could not identify " + ident);
066: out.add(ReplacerUtils.jprintf("ident.noident", env,
067: SiteBot.class));
068: return out;
069: }
070: } else if (args.equals("*")) {
071: showAllUserCredits(out);
072: return out;
073: } else {
074: try {
075: user = getGlobalContext().getUserManager()
076: .getUserByName(args);
077: } catch (Exception e) {
078: env.add("user", args);
079: out.add(ReplacerUtils.jprintf("credits.error", env,
080: Credits.class));
081: return out;
082: }
083: }
084: env.add("user", user.getName());
085: env.add("credits", Bytes.formatBytes(user.getCredits()));
086: out.add(ReplacerUtils.jprintf("credits.user", env,
087: Credits.class));
088: return out;
089: }
090:
091: protected void showAllUserCredits(ArrayList<String> out) {
092: long totalcredz = 0;
093: ReplacerEnvironment env = new ReplacerEnvironment(
094: SiteBot.GLOBAL_ENV);
095: try {
096: ArrayList<User> users = new ArrayList<User>(
097: getGlobalContext().getUserManager().getAllUsers());
098: for (Iterator iter = users.iterator(); iter.hasNext();) {
099: User user = (User) iter.next();
100: totalcredz += user.getCredits();
101: }
102: env.add("usercount", Integer.toString(users.size()));
103: env.add("totalcredits", Bytes.formatBytes(totalcredz));
104: out.add(ReplacerUtils.jprintf("credits.total", env,
105: Credits.class));
106: } catch (UserFileException e) {
107: logger.warn(e);
108: }
109: }
110:
111: }
|