1 18 package net.sf.drftpd.event.irc; 19 20 import java.rmi.RemoteException ; 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 24 import net.sf.drftpd.Bytes; 25 import net.sf.drftpd.master.BaseFtpConnection; 26 import net.sf.drftpd.master.ConnectionManager; 27 import net.sf.drftpd.master.usermanager.NoSuchUserException; 28 import net.sf.drftpd.master.usermanager.User; 29 import net.sf.drftpd.slave.SlaveStatus; 30 import net.sf.drftpd.slave.Transfer; 31 import net.sf.drftpd.util.ReplacerUtils; 32 import net.sf.drftpd.util.Time; 33 34 import org.apache.log4j.Logger; 35 import org.drftpd.plugins.SiteBot; 36 import org.tanesha.replacer.ReplacerEnvironment; 37 38 import f00f.net.irc.martyr.GenericCommandAutoService; 39 import f00f.net.irc.martyr.InCommand; 40 import f00f.net.irc.martyr.commands.MessageCommand; 41 42 46 47 public class Bandwidth 48 extends GenericCommandAutoService 49 implements IRCPluginInterface { 50 51 private static final Logger logger = Logger.getLogger(Bandwidth.class); 52 53 private SiteBot _listener; 54 55 public Bandwidth(SiteBot listener) { 56 super(listener.getIRCConnection()); 57 _listener = listener; 58 } 59 60 public String getCommands() { 61 return "!bw !speed"; 62 } 63 64 private ConnectionManager getConnectionManager() { 65 return _listener.getConnectionManager(); 66 } 67 68 protected void updateCommand(InCommand command) { 69 if (!(command instanceof MessageCommand)) { 70 return; 71 } 72 MessageCommand msgc = (MessageCommand) command; 73 if (msgc.isPrivateToUs(_listener.getIRCConnection().getClientState())) { 74 return; 75 } 76 77 String msg = msgc.getMessage(); 78 if (msg.startsWith("!bw")) { 79 SlaveStatus status = 80 getConnectionManager().getSlaveManager().getAllStatus(); 81 82 ReplacerEnvironment env = 83 new ReplacerEnvironment(SiteBot.GLOBAL_ENV); 84 85 SiteBot.fillEnvSlaveStatus(env, status, _listener.getSlaveManager()); 86 87 _listener.sayChannel(msgc.getDest(), ReplacerUtils.jprintf("bw", env, SiteBot.class)); 88 } else if (msg.startsWith("!speed ")) { 89 String username; 90 try { 91 username = msgc.getMessage().substring("!speed ".length()); 92 } catch (ArrayIndexOutOfBoundsException e) { 93 logger.warn("", e); 94 return; 95 } catch (StringIndexOutOfBoundsException e) { 96 logger.warn("", e); 97 return; 98 } 99 ReplacerEnvironment env = 100 new ReplacerEnvironment(SiteBot.GLOBAL_ENV); 101 env.add("user", username); 102 String status = 103 ReplacerUtils.jprintf("speed.pre", env, SiteBot.class); 104 105 String separator = 106 ReplacerUtils.jprintf("speed.separator", env, SiteBot.class); 107 108 boolean first = true; 109 110 ArrayList conns = 111 new ArrayList (getConnectionManager().getConnections()); 112 for (Iterator iter = conns.iterator(); iter.hasNext();) { 113 BaseFtpConnection conn = (BaseFtpConnection) iter.next(); 114 try { 115 User connUser = conn.getUser(); 116 if (!first) { 117 status = status + separator; 118 } 119 if (connUser.getUsername().equals(username)) { 120 121 env.add( 122 "idle", 123 Time.formatTime( 124 System.currentTimeMillis() 125 - conn.getLastActive())); 126 127 if (getConnectionManager() 128 .getConfig() 129 .checkHideInWho( 130 connUser, 131 conn.getCurrentDirectory())) 132 continue; 133 first = false; 134 if (!conn.isExecuting()) { 135 status 136 += ReplacerUtils.jprintf( 137 "speed.idle", 138 env, 139 SiteBot.class); 140 141 } else if ( 142 conn.getDataConnectionHandler().isTransfering()) { 143 try { 144 env.add( 145 "speed", 146 Bytes.formatBytes( 147 conn 148 .getDataConnectionHandler() 149 .getTransfer() 150 .getXferSpeed()) 151 + "/s"); 152 } catch (RemoteException e2) { 153 logger.warn("", e2); 154 } 155 env.add( 156 "file", 157 conn 158 .getDataConnectionHandler() 159 .getTransferFile() 160 .getName()); 161 env.add( 162 "slave", 163 conn 164 .getDataConnectionHandler() 165 .getTranferSlave() 166 .getName()); 167 168 if (conn.getTransferDirection() 169 == Transfer.TRANSFER_RECEIVING_UPLOAD) { 170 status 171 += ReplacerUtils.jprintf( 172 "speed.up", 173 env, 174 SiteBot.class); 175 176 } else if ( 177 conn.getTransferDirection() 178 == Transfer.TRANSFER_SENDING_DOWNLOAD) { 179 status 180 += ReplacerUtils.jprintf( 181 "speed.down", 182 env, 183 SiteBot.class); 184 } 185 } 186 } 187 } catch (NoSuchUserException e) { 188 } 190 } status += ReplacerUtils.jprintf("speed.post", env, SiteBot.class); 192 if (first) { 193 status = 194 ReplacerUtils.jprintf("speed.error", env, SiteBot.class); 195 } 196 _listener.sayChannel(msgc.getDest(), status); 197 } 198 } 199 } 200 | Popular Tags |