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.config.FtpConfig; 28 import net.sf.drftpd.master.usermanager.NoSuchUserException; 29 import net.sf.drftpd.master.usermanager.User; 30 import net.sf.drftpd.slave.Transfer; 31 import net.sf.drftpd.util.ReplacerUtils; 32 33 import org.apache.log4j.Logger; 34 import org.drftpd.plugins.SiteBot; 35 import org.tanesha.replacer.FormatterException; 36 import org.tanesha.replacer.ReplacerEnvironment; 37 import org.tanesha.replacer.ReplacerFormat; 38 import org.tanesha.replacer.SimplePrintf; 39 40 import f00f.net.irc.martyr.GenericAutoService; 41 import f00f.net.irc.martyr.InCommand; 42 import f00f.net.irc.martyr.State; 43 import f00f.net.irc.martyr.commands.MessageCommand; 44 45 49 public class Who extends GenericAutoService implements IRCPluginInterface { 50 private static final Logger logger = Logger.getLogger(Who.class); 51 52 private SiteBot _listener; 53 54 public Who(SiteBot listener) { 55 super(listener.getIRCConnection()); 56 _listener = listener; 57 } 58 59 public String getCommands() { 60 return "!who"; 61 } 62 63 private FtpConfig getConfig() { 64 return _listener.getConfig(); 65 } 66 67 private ConnectionManager getConnectionManager() { 68 return _listener.getConnectionManager(); 69 } 70 71 protected void updateCommand(InCommand inCommand) { 72 if (!(inCommand instanceof MessageCommand)) 73 return; 74 MessageCommand msgc = (MessageCommand) inCommand; 75 if(msgc.isPrivateToUs(_listener.getIRCConnection().getClientState())) return; 76 String cmd = msgc.getMessage(); 77 78 boolean up, dn, idle; 79 if (cmd.equals("!who")) { 80 up = dn = idle = true; 81 } else if ( 82 cmd.equals("!leechers") 83 || cmd.equals("!uploaders") 84 || cmd.equals("!idlers")) { 85 dn = cmd.equals("!leechers"); 86 up = cmd.equals("!uploaders"); 87 idle = cmd.equals("!idlers"); 88 } else { 89 return; 90 } 91 92 try { 93 ReplacerFormat formatup = 94 ReplacerUtils.finalFormat(Who.class, "who.up"); 95 ReplacerFormat formatdown = 96 ReplacerUtils.finalFormat(Who.class, "who.down"); 97 ReplacerFormat formatidle = 98 ReplacerUtils.finalFormat(Who.class, "who.idle"); 99 100 ReplacerEnvironment env = 101 new ReplacerEnvironment(SiteBot.GLOBAL_ENV); 102 ArrayList conns = 103 new ArrayList (getConnectionManager().getConnections()); 104 int i = 0; 105 for (Iterator iter = conns.iterator(); iter.hasNext();) { 106 BaseFtpConnection conn = (BaseFtpConnection) iter.next(); 107 User user; 108 try { 109 user = conn.getUser(); 110 } catch (NoSuchUserException e) { 111 continue; 112 } 113 if (getConfig() 114 .checkHideInWho(user, conn.getCurrentDirectory())) { 115 continue; 116 } 117 env.add( 118 "idle", 119 (System.currentTimeMillis() - conn.getLastActive()) / 1000 120 + "s"); 121 env.add("targetuser", user.getUsername()); 122 123 if (!conn.getDataConnectionHandler().isTransfering()) { 124 if (idle) { 125 _listener.sayChannel(msgc.getDest(), SimplePrintf.jprintf(formatidle, env)); 126 } 127 } else { 128 try { 129 env.add( 130 "speed", 131 Bytes.formatBytes( 132 conn 133 .getDataConnectionHandler() 134 .getTransfer() 135 .getXferSpeed()) 136 + "/s"); 137 } catch (RemoteException e2) { 138 logger.warn("", e2); 139 } 140 env.add( 141 "file", 142 conn 143 .getDataConnectionHandler() 144 .getTransferFile() 145 .getName()); 146 env.add( 147 "slave", 148 conn 149 .getDataConnectionHandler() 150 .getTranferSlave() 151 .getName()); 152 153 if (conn.getTransferDirection() 154 == Transfer.TRANSFER_RECEIVING_UPLOAD) { 155 if (up) { 156 _listener.sayChannel(msgc.getDest(), SimplePrintf.jprintf(formatup, env)); 157 i++; 158 } 159 160 } else if ( 161 conn.getTransferDirection() 162 == Transfer.TRANSFER_SENDING_DOWNLOAD) { 163 if (dn) { 164 _listener.sayChannel(msgc.getDest(), SimplePrintf.jprintf(formatdown, env)); 165 i++; 166 } 167 } 168 } 169 } 170 } catch (FormatterException e) { 171 logger.warn("", e); 172 } 173 } 174 175 protected void updateState(State state) { 176 } 177 } 178 | Popular Tags |