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: import java.util.Set;
022: import java.util.Map.Entry;
023:
024: import net.sf.drftpd.ObjectNotFoundException;
025: import net.sf.drftpd.SlaveUnavailableException;
026: import net.sf.drftpd.util.ReplacerUtils;
027:
028: import org.apache.log4j.Level;
029: import org.apache.log4j.Logger;
030: import org.drftpd.GlobalContext;
031: import org.drftpd.master.RemoteSlave;
032: import org.drftpd.plugins.SiteBot;
033: import org.drftpd.slave.SlaveStatus;
034: import org.tanesha.replacer.ReplacerEnvironment;
035:
036: import f00f.net.irc.martyr.commands.MessageCommand;
037:
038: /**
039: * @author mog
040: * @version $Id: Slaves.java 1513 2006-10-13 22:41:08Z tdsoul $
041: */
042: public class Slaves extends IRCCommand {
043: private static final Logger logger = Logger.getLogger(Slaves.class);
044:
045: public Slaves(GlobalContext gctx) {
046: super (gctx);
047: }
048:
049: public ArrayList<String> doSlave(String args, MessageCommand msgc) {
050: ArrayList<String> out = new ArrayList<String>();
051: ReplacerEnvironment env = new ReplacerEnvironment(
052: SiteBot.GLOBAL_ENV);
053: String slaveName = args;
054: try {
055: RemoteSlave rslave = getGlobalContext().getSlaveManager()
056: .getRemoteSlave(slaveName);
057: out.add(makeStatusString(rslave));
058: } catch (ObjectNotFoundException e) {
059: env.add("slave", slaveName);
060: out.add(ReplacerUtils.jprintf("slaves.notfound", env,
061: Slaves.class));
062: }
063:
064: return out;
065: }
066:
067: public ArrayList<String> doSlaves(String args, MessageCommand msgc) {
068: ArrayList<String> out = new ArrayList<String>();
069: for (RemoteSlave rslave : getGlobalContext().getSlaveManager()
070: .getSlaves()) {
071: out.add(makeStatusString(rslave));
072: }
073: if (out.isEmpty()) {
074: ReplacerEnvironment env = new ReplacerEnvironment(
075: SiteBot.GLOBAL_ENV);
076: out.add(ReplacerUtils.jprintf("slaves.notfound", env,
077: Slaves.class));
078: }
079: return out;
080: }
081:
082: private String makeStatusString(RemoteSlave rslave) {
083: ReplacerEnvironment env = new ReplacerEnvironment(
084: SiteBot.GLOBAL_ENV);
085: env.add("slave", rslave.getName());
086: for (Entry<Object, Object> entry : rslave.getProperties()
087: .entrySet()) {
088: env.add((String) entry.getKey(), entry.getValue());
089: }
090: try {
091: SlaveStatus status;
092:
093: try {
094: status = rslave.getSlaveStatusAvailable();
095: } catch (SlaveUnavailableException e1) {
096: return ReplacerUtils.jprintf("slaves.offline", env,
097: Slaves.class);
098: }
099:
100: SiteBot.fillEnvSlaveStatus(env, status, getGlobalContext()
101: .getSlaveManager());
102:
103: return ReplacerUtils.jprintf("slaves", env, Slaves.class);
104: } catch (RuntimeException t) {
105: logger.log(Level.WARN,
106: "Caught RuntimeException in !slaves loop", t);
107:
108: return ReplacerUtils.jprintf("slaves.offline", env,
109: Slaves.class);
110: }
111: }
112: }
|