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.Iterator;
022:
023: import net.sf.drftpd.ObjectNotFoundException;
024: import net.sf.drftpd.master.BaseFtpConnection;
025: import net.sf.drftpd.util.ReplacerUtils;
026:
027: import org.apache.log4j.Logger;
028: import org.drftpd.Bytes;
029: import org.drftpd.GlobalContext;
030: import org.drftpd.plugins.SiteBot;
031: import org.drftpd.slave.Transfer;
032: import org.drftpd.usermanager.NoSuchUserException;
033: import org.drftpd.usermanager.User;
034: import org.tanesha.replacer.FormatterException;
035: import org.tanesha.replacer.ReplacerEnvironment;
036: import org.tanesha.replacer.ReplacerFormat;
037: import org.tanesha.replacer.SimplePrintf;
038:
039: import f00f.net.irc.martyr.commands.MessageCommand;
040:
041: /**
042: * @author mog
043: * @version $Id: Who.java 1513 2006-10-13 22:41:08Z tdsoul $
044: */
045: public class Who extends IRCCommand {
046: private static final Logger logger = Logger.getLogger(Who.class);
047:
048: public Who(GlobalContext gctx) {
049: super (gctx);
050: }
051:
052: private ArrayList<String> getData(boolean idle, boolean up,
053: boolean down) {
054: ArrayList<String> out = new ArrayList<String>();
055: ReplacerEnvironment env = new ReplacerEnvironment(
056: SiteBot.GLOBAL_ENV);
057: try {
058: ReplacerFormat formatup = ReplacerUtils.finalFormat(
059: Who.class, "who.up");
060: ReplacerFormat formatdown = ReplacerUtils.finalFormat(
061: Who.class, "who.down");
062: ReplacerFormat formatidle = ReplacerUtils.finalFormat(
063: Who.class, "who.idle");
064:
065: ArrayList<BaseFtpConnection> conns = new ArrayList<BaseFtpConnection>(
066: getGlobalContext().getConnectionManager()
067: .getConnections());
068: int i = 0;
069:
070: for (Iterator iter = conns.iterator(); iter.hasNext();) {
071: BaseFtpConnection conn = (BaseFtpConnection) iter
072: .next();
073: User user;
074:
075: try {
076: user = conn.getUser();
077: } catch (NoSuchUserException e) {
078: continue;
079: }
080:
081: if (getGlobalContext().getConfig().checkPathPermission(
082: "hideinwho", user, conn.getCurrentDirectory())) {
083: continue;
084: }
085:
086: env.add("idle", ((System.currentTimeMillis() - conn
087: .getLastActive()) / 1000)
088: + "s");
089: env.add("targetuser", user.getName());
090:
091: synchronized (conn.getDataConnectionHandler()) {
092: if (!conn.getDataConnectionHandler()
093: .isTransfering()) {
094: if (idle) {
095: out.add(SimplePrintf.jprintf(formatidle,
096: env));
097: }
098: } else {
099: try {
100: env.add("speed", Bytes.formatBytes(conn
101: .getDataConnectionHandler()
102: .getTransfer().getXferSpeed())
103: + "/s");
104: } catch (ObjectNotFoundException e) {
105: logger.debug(
106: "This is a bug, please report it",
107: e);
108: }
109: env.add("file", conn.getDataConnectionHandler()
110: .getTransferFile().getName());
111: env.add("slave", conn
112: .getDataConnectionHandler()
113: .getTranferSlave().getName());
114: switch (conn.getDirection()) {
115: case Transfer.TRANSFER_RECEIVING_UPLOAD:
116: if (up) {
117: out.add(SimplePrintf.jprintf(formatup,
118: env));
119: }
120: break;
121: case Transfer.TRANSFER_SENDING_DOWNLOAD:
122: if (down) {
123: out.add(SimplePrintf.jprintf(
124: formatdown, env));
125: }
126: break;
127: default:
128: if (idle) {
129: out.add(SimplePrintf.jprintf(
130: formatidle, env));
131: }
132: }
133: }
134: }
135:
136: }
137: } catch (FormatterException e) {
138: logger.warn("", e);
139: }
140: if (out.isEmpty()) {
141: out.add(ReplacerUtils.jprintf("who.none", env, Who.class));
142: }
143: return out;
144: }
145:
146: public ArrayList<String> doWho(String cmd, MessageCommand msgc) {
147: return getData(true, true, true);
148: }
149:
150: public ArrayList<String> doIdlers(String cmd, MessageCommand msgc) {
151: return getData(true, false, false);
152: }
153:
154: public ArrayList<String> doLeechers(String cmd, MessageCommand msgc) {
155: return getData(false, false, true);
156: }
157:
158: public ArrayList<String> doUploaders(String cmd, MessageCommand msgc) {
159: return getData(false, true, false);
160: }
161: }
|