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 net.sf.drftpd.master.command.plugins;
019:
020: import f00f.net.irc.martyr.IRCConnection;
021: import f00f.net.irc.martyr.commands.MessageCommand;
022:
023: import net.sf.drftpd.master.BaseFtpConnection;
024: import net.sf.drftpd.master.command.CommandManager;
025: import net.sf.drftpd.master.command.CommandManagerFactory;
026:
027: import org.drftpd.commands.CommandHandler;
028: import org.drftpd.commands.CommandHandlerFactory;
029: import org.drftpd.commands.Reply;
030: import org.drftpd.commands.UnhandledCommandException;
031:
032: import org.tanesha.replacer.FormatterException;
033: import org.tanesha.replacer.ReplacerEnvironment;
034: import org.tanesha.replacer.SimplePrintf;
035:
036: import java.io.BufferedReader;
037: import java.io.FileInputStream;
038: import java.io.FileNotFoundException;
039: import java.io.IOException;
040: import java.io.InputStreamReader;
041:
042: /**
043: * @author mog
044: * @version $Id: Textoutput.java 953 2005-02-04 01:17:59Z teflon $
045: */
046: public class Textoutput implements CommandHandler,
047: CommandHandlerFactory {
048: public static void addTextToResponse(Reply reply, String file)
049: throws FileNotFoundException, IOException {
050: reply.addComment(new BufferedReader(new InputStreamReader(
051: new FileInputStream("text/" + file + ".txt"),
052: "ISO-8859-1")));
053: }
054:
055: public static String getText(String file)
056: throws FileNotFoundException, IOException {
057: BufferedReader rd = new BufferedReader(new InputStreamReader(
058: new FileInputStream("text/" + file + ".txt"),
059: "ISO-8859-1"));
060: String text = "";
061: String line;
062:
063: try {
064: while ((line = rd.readLine()) != null) {
065: if (!line.startsWith("#")) {
066: text += (line + "\n");
067: }
068: }
069: } finally {
070: rd.close();
071: }
072:
073: return text;
074: }
075:
076: protected static void sendTextToIRC(IRCConnection conn,
077: String destination, BufferedReader in) throws IOException {
078: String line;
079:
080: while ((line = in.readLine()) != null) {
081: ReplacerEnvironment env = new ReplacerEnvironment();
082:
083: try {
084: conn.sendCommand(new MessageCommand(destination,
085: SimplePrintf.jprintf(line, env)));
086: } catch (FormatterException e1) {
087: conn.sendCommand(new MessageCommand(destination,
088: "Error in formatting of line - " + line));
089: }
090: }
091: }
092:
093: /**
094: * @param Path is a complete working path, not just a filename, for example "text/file.txt"
095: */
096: public static void sendTextToIRC(IRCConnection conn,
097: String destination, String path) {
098: BufferedReader fileReader = null;
099:
100: try {
101: fileReader = new BufferedReader(new InputStreamReader(
102: new FileInputStream(path)));
103: sendTextToIRC(conn, destination, fileReader);
104: } catch (IOException e) {
105: conn.sendCommand(new MessageCommand(destination,
106: "IOException opening file " + path
107: + ", check generictextoutput.conf"));
108:
109: return;
110: }
111: }
112:
113: public Reply execute(BaseFtpConnection conn)
114: throws UnhandledCommandException {
115: if (conn.getRequest().hasArgument()) {
116: return Reply.RESPONSE_501_SYNTAX_ERROR;
117: }
118:
119: try {
120: Reply reply = new Reply(200);
121: addTextToResponse(reply, conn.getRequest().getCommand()
122: .substring("SITE ".length()).toLowerCase());
123:
124: return reply;
125: } catch (IOException e) {
126: return new Reply(200, "IO Error: " + e.getMessage());
127: }
128: }
129:
130: public String[] getFeatReplies() {
131: return null;
132: }
133:
134: public CommandHandler initialize(BaseFtpConnection conn,
135: CommandManager initializer) {
136: return this ;
137: }
138:
139: public void load(CommandManagerFactory initializer) {
140: }
141:
142: public void unload() {
143: }
144: }
|