KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > event > irc > Who


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.event.irc;
19
20 import java.rmi.RemoteException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Iterator JavaDoc;
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 /**
46  * @author mog
47  * @version $Id: Who.java,v 1.7 2004/04/09 19:05:04 mog Exp $
48  */

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 JavaDoc 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 JavaDoc 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 JavaDoc conns =
103                 new ArrayList JavaDoc(getConnectionManager().getConnections());
104             int i = 0;
105             for (Iterator JavaDoc 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 JavaDoc 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