KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
21
22 import net.sf.drftpd.SlaveUnavailableException;
23 import net.sf.drftpd.master.ConnectionManager;
24 import net.sf.drftpd.master.RemoteSlave;
25 import net.sf.drftpd.slave.SlaveStatus;
26 import net.sf.drftpd.util.ReplacerUtils;
27
28 import org.apache.log4j.Level;
29 import org.apache.log4j.Logger;
30 import org.drftpd.plugins.SiteBot;
31 import org.tanesha.replacer.ReplacerEnvironment;
32
33 import f00f.net.irc.martyr.GenericAutoService;
34 import f00f.net.irc.martyr.InCommand;
35 import f00f.net.irc.martyr.State;
36 import f00f.net.irc.martyr.commands.MessageCommand;
37
38 /**
39  * @author mog
40  * @version $Id: Slaves.java,v 1.13 2004/05/17 11:27:23 mog Exp $
41  */

42 public class Slaves extends GenericAutoService implements IRCPluginInterface {
43
44     private static final Logger logger = Logger.getLogger(Slaves.class);
45
46     private SiteBot _listener;
47
48     public Slaves(SiteBot listener) {
49         super(listener.getIRCConnection());
50         _listener = listener;
51     }
52
53     public String JavaDoc getCommands() {
54         return "!slaves";
55     }
56
57     private ConnectionManager getConnectionManager() {
58         return _listener.getConnectionManager();
59     }
60
61     protected void updateCommand(InCommand inCommand) {
62         if (!(inCommand instanceof MessageCommand))
63             return;
64         MessageCommand msgc = (MessageCommand) inCommand;
65         if (!((MessageCommand) inCommand).getMessage().equals("!slaves"))
66             return;
67         if (msgc.isPrivateToUs(_listener.getIRCConnection().getClientState()))
68             return;
69         String JavaDoc chan = msgc.getDest();
70
71         for (Iterator JavaDoc iter =
72             getConnectionManager().getSlaveManager().getSlaves().iterator();
73             iter.hasNext();
74             ) {
75             RemoteSlave rslave = (RemoteSlave) iter.next();
76             String JavaDoc statusString;
77
78             ReplacerEnvironment env =
79                 new ReplacerEnvironment(SiteBot.GLOBAL_ENV);
80             env.add("slave", rslave.getName());
81
82             try {
83                 SlaveStatus status;
84                 try {
85                     status = rslave.getStatus();
86                 } catch (SlaveUnavailableException e1) {
87                     String JavaDoc chan1 = chan;
88                     _listener.sayChannel(
89                         chan1,
90                         ReplacerUtils.jprintf(
91                             "slaves.offline",
92                             env,
93                             Slaves.class));
94                     continue;
95                 }
96                 SiteBot.fillEnvSlaveStatus(env, status, _listener.getSlaveManager());
97
98                 statusString =
99                     ReplacerUtils.jprintf("slaves", env, Slaves.class);
100             } catch (RuntimeException JavaDoc t) {
101                 logger.log(
102                     Level.WARN,
103                     "Caught RuntimeException in !slaves loop",
104                     t);
105                 statusString = ReplacerUtils.jprintf(
106                         "slaves.offline",
107                         env,
108                         Slaves.class);
109             }
110             String JavaDoc chan1 = chan;
111             String JavaDoc string = statusString;
112             _listener.sayChannel(chan1, string);
113         }
114
115     }
116
117     protected void updateState(State state) {
118     }
119
120 }
121
Popular Tags