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:
022: import net.sf.drftpd.ObjectNotFoundException;
023: import net.sf.drftpd.master.BaseFtpConnection;
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.Time;
030: import org.drftpd.master.RemoteTransfer;
031: import org.drftpd.plugins.SiteBot;
032: import org.drftpd.slave.SlaveStatus;
033: import org.drftpd.slave.Transfer;
034: import org.drftpd.usermanager.NoSuchUserException;
035: import org.drftpd.usermanager.User;
036: import org.drftpd.usermanager.UserFileException;
037: import org.tanesha.replacer.ReplacerEnvironment;
038:
039: import f00f.net.irc.martyr.commands.MessageCommand;
040:
041: /**
042: * @author flowman
043: * @version $Id: Bandwidth.java 1513 2006-10-13 22:41:08Z tdsoul $
044: */
045: public class Bandwidth extends IRCCommand {
046: private static final Logger logger = Logger
047: .getLogger(Bandwidth.class);
048:
049: public Bandwidth(GlobalContext gctx) {
050: super (gctx);
051: }
052:
053: public ArrayList<String> doBandwidth(String args,
054: MessageCommand msgc) {
055: ArrayList<String> out = new ArrayList<String>();
056: ReplacerEnvironment env = new ReplacerEnvironment(
057: SiteBot.GLOBAL_ENV);
058: SlaveStatus status = getGlobalContext().getSlaveManager()
059: .getAllStatus();
060: SiteBot.fillEnvSlaveStatus(env, status, getGlobalContext()
061: .getSlaveManager());
062: out.add(ReplacerUtils.jprintf("bw", env, Bandwidth.class));
063: return out;
064: }
065:
066: public ArrayList<String> doSpeed(String args, MessageCommand msgc) {
067: ArrayList<String> out = new ArrayList<String>();
068: ReplacerEnvironment env = new ReplacerEnvironment(
069: SiteBot.GLOBAL_ENV);
070: String username = args;
071: User user = null;
072:
073: env.add("user", username);
074: try {
075: user = getGlobalContext().getUserManager().getUserByName(
076: username);
077: } catch (NoSuchUserException e1) {
078: out.add(ReplacerUtils.jprintf("speed.usernotfound", env,
079: Bandwidth.class));
080: return out;
081: } catch (UserFileException e1) {
082: out.add(ReplacerUtils.jprintf("speed.usererror", env,
083: Bandwidth.class));
084: return out;
085: }
086:
087: out.add(ReplacerUtils
088: .jprintf("speed.pre", env, Bandwidth.class));
089:
090: ArrayList<BaseFtpConnection> conns = new ArrayList<BaseFtpConnection>(
091: getGlobalContext().getConnectionManager()
092: .getConnections());
093:
094: boolean found = false;
095: for (BaseFtpConnection conn : conns) {
096: try {
097: User connUser = conn.getUser();
098: if (connUser.equals(user)) {
099: found = true;
100: env.add("idle", Time.formatTime(System
101: .currentTimeMillis()
102: - conn.getLastActive()));
103:
104: if (getGlobalContext().getConfig()
105: .checkPathPermission("hideinwho", connUser,
106: conn.getCurrentDirectory())) {
107: continue;
108: }
109: synchronized (conn.getDataConnectionHandler()) {
110: if (!conn.isExecuting()) {
111: out.add(ReplacerUtils.jprintf("speed.idle",
112: env, Bandwidth.class));
113: } else if (conn.getDataConnectionHandler()
114: .isTransfering()) {
115: try {
116: RemoteTransfer transfer = conn
117: .getDataConnectionHandler()
118: .getTransfer();
119: env.add("speed", Bytes
120: .formatBytes(transfer
121: .getXferSpeed())
122: + "/s");
123: env.add("transfered", Bytes
124: .formatBytes(transfer
125: .getTransfered()));
126: } catch (ObjectNotFoundException e) {
127: logger
128: .debug(
129: "This is a bug, please report it",
130: e);
131: }
132: env.add("file", conn
133: .getDataConnectionHandler()
134: .getTransferFile().getName());
135: env.add("slave", conn
136: .getDataConnectionHandler()
137: .getTranferSlave().getName());
138: if (conn.getDirection() == Transfer.TRANSFER_RECEIVING_UPLOAD) {
139: out.add(ReplacerUtils.jprintf(
140: "speed.up", env,
141: Bandwidth.class));
142: } else if (conn.getDirection() == Transfer.TRANSFER_SENDING_DOWNLOAD) {
143: out.add(ReplacerUtils.jprintf(
144: "speed.down", env,
145: Bandwidth.class));
146: }
147: }
148: }
149: }
150: } catch (NoSuchUserException e) {
151: //just continue.. we aren't interested in connections without logged-in users
152: }
153: } // for
154:
155: if (!found) {
156: out.clear();
157: out.add(ReplacerUtils.jprintf("speed.error", env,
158: Bandwidth.class));
159: return out;
160: }
161: out.add(ReplacerUtils.jprintf("speed.post", env,
162: Bandwidth.class));
163:
164: return out;
165: }
166: }
|