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.commands;
019:
020: import f00f.net.irc.martyr.commands.RawCommand;
021:
022: import net.sf.drftpd.ObjectNotFoundException;
023: import net.sf.drftpd.master.BaseFtpConnection;
024: import net.sf.drftpd.master.FtpRequest;
025: import net.sf.drftpd.master.command.CommandManager;
026: import net.sf.drftpd.master.command.CommandManagerFactory;
027:
028: import org.apache.log4j.Logger;
029:
030: import org.drftpd.plugins.SiteBot;
031:
032: import org.drftpd.usermanager.NoSuchUserException;
033: import org.tanesha.replacer.ReplacerEnvironment;
034:
035: /**
036: * @author mog
037: * @version $Id: SiteBotManagement.java 1182 2005-04-26 22:28:38Z zubov $
038: */
039: public class SiteBotManagement implements CommandHandler,
040: CommandHandlerFactory {
041: private static final Logger logger = Logger
042: .getLogger(SiteBotManagement.class);
043:
044: public SiteBotManagement() {
045: super ();
046: }
047:
048: public Reply execute(BaseFtpConnection conn)
049: throws UnhandledCommandException, ImproperUsageException {
050:
051: SiteBot sitebot;
052:
053: try {
054: sitebot = (SiteBot) conn.getGlobalContext()
055: .getConnectionManager().getGlobalContext()
056: .getFtpListener(SiteBot.class);
057: } catch (ObjectNotFoundException e) {
058: return new Reply(500, "SiteBot not loaded");
059: }
060:
061: if (conn.getRequest().getCommand().startsWith("SITE BLOWFISH")) {
062: return doSITE_BLOWFISH(conn, sitebot);
063: }
064:
065: if (!conn.getRequest().hasArgument()) {
066: throw new ImproperUsageException();
067: }
068:
069: FtpRequest req2 = new FtpRequest(conn.getRequest()
070: .getArgument());
071:
072: try {
073: if (!conn.getUser().isAdmin()) {
074: return Reply.RESPONSE_530_ACCESS_DENIED;
075: }
076: } catch (NoSuchUserException e1) {
077: throw new RuntimeException(e1);
078: }
079:
080: if (req2.getCommand().equals("RECONNECT")) {
081: sitebot.reconnect();
082:
083: return new Reply(200,
084: "Told bot to disconnect, auto-reconnect should handle the rest");
085: } else if (req2.getCommand().equals("DISCONNECT")) {
086: sitebot.disconnect();
087:
088: return new Reply(200, "Told bot to disconnect");
089: } else if (req2.getCommand().equals("CONNECT")) {
090: try {
091: sitebot.connect();
092:
093: return new Reply(200, "Sitebot connected");
094: } catch (Exception e) {
095: logger.warn("", e);
096:
097: return new Reply(500, e.getMessage());
098: }
099: } else if (req2.getCommand().equals("SAY")) {
100: sitebot.sayGlobal(req2.getArgument());
101:
102: return new Reply(200, "Said: " + req2.getArgument());
103: } else if (req2.getCommand().equals("RAW")) {
104: sitebot.getIRCConnection().sendCommand(
105: new RawCommand(req2.getArgument()));
106:
107: return new Reply(200, "Sent raw: " + req2.getArgument());
108: }
109:
110: return new Reply(501, conn.jprintf(SiteBotManagement.class,
111: "sitebot.usage"));
112: }
113:
114: private Reply doSITE_BLOWFISH(BaseFtpConnection conn,
115: SiteBot sitebot) {
116: if (!conn.isSecure()) {
117: return new Reply(200, conn.jprintf(SiteBotManagement.class,
118: "blowfish.reject"));
119: }
120: try {
121: String _key = null;
122: if (conn.getRequest().hasArgument()) {
123: _key = sitebot.getBlowfishKey(conn.getRequest()
124: .getArgument(), conn.getUserNull());
125: } else {
126: _key = sitebot.getBlowfishKey(conn.getUserNull());
127: }
128: ReplacerEnvironment env = new ReplacerEnvironment(
129: SiteBot.GLOBAL_ENV);
130: env.add("key", _key);
131: return new Reply(200, conn.jprintf(SiteBotManagement.class,
132: "blowfish.accept", env));
133: } catch (ObjectNotFoundException e) {
134: return new Reply(200, conn.jprintf(SiteBotManagement.class,
135: "blowfish.notenabled"));
136: }
137: }
138:
139: public CommandHandler initialize(BaseFtpConnection conn,
140: CommandManager initializer) {
141: return this ;
142: }
143:
144: public String[] getFeatReplies() {
145: return null;
146: }
147:
148: public void load(CommandManagerFactory initializer) {
149: }
150:
151: public void unload() {
152: }
153: }
|