KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > master > command > plugins > Textoutput


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.master.command.plugins;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.FileInputStream JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.IOException JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25
26 import net.sf.drftpd.master.BaseFtpConnection;
27 import net.sf.drftpd.master.FtpReply;
28 import net.sf.drftpd.master.command.CommandManager;
29 import net.sf.drftpd.master.command.CommandManagerFactory;
30
31 import org.drftpd.commands.CommandHandler;
32 import org.drftpd.commands.CommandHandlerFactory;
33 import org.drftpd.commands.UnhandledCommandException;
34 import org.tanesha.replacer.FormatterException;
35 import org.tanesha.replacer.ReplacerEnvironment;
36 import org.tanesha.replacer.SimplePrintf;
37
38 import f00f.net.irc.martyr.IRCConnection;
39 import f00f.net.irc.martyr.commands.MessageCommand;
40
41 /**
42  * @author mog
43  * @version $Id: Textoutput.java,v 1.12.2.1 2004/06/19 23:37:26 mog Exp $
44  */

45 public class Textoutput implements CommandHandlerFactory, CommandHandler {
46
47     public static void addTextToResponse(FtpReply reply, String JavaDoc file)
48         throws FileNotFoundException JavaDoc, IOException JavaDoc {
49         reply.addComment(
50             new BufferedReader JavaDoc(
51                 new InputStreamReader JavaDoc(
52                     new FileInputStream JavaDoc("text/" + file + ".txt"),
53                     "ISO-8859-1")));
54     }
55     protected static void sendTextToIRC(
56         IRCConnection conn,
57         String JavaDoc destination,
58         BufferedReader JavaDoc in)
59         throws IOException JavaDoc {
60         String JavaDoc line;
61         while ((line = in.readLine()) != null) {
62             ReplacerEnvironment env = new ReplacerEnvironment();
63             try {
64                 conn.sendCommand(
65                     new MessageCommand(
66                         destination,
67                         SimplePrintf.jprintf(line, env)));
68             } catch (FormatterException e1) {
69                 conn.sendCommand(
70                     new MessageCommand(
71                         destination,
72                         "Error in formatting of line - " + line));
73             }
74         }
75     }
76
77     public static void sendTextToIRC(
78         IRCConnection conn,
79         String JavaDoc destination,
80         String JavaDoc file) {
81         BufferedReader JavaDoc fileReader = null;
82         try {
83             fileReader =
84                 new BufferedReader JavaDoc(
85                     new InputStreamReader JavaDoc(
86                         new FileInputStream JavaDoc("text/" + file + ".txt")));
87             sendTextToIRC(conn, destination, fileReader);
88         } catch (IOException JavaDoc e) {
89             conn.sendCommand(
90                 new MessageCommand(
91                     destination,
92                     "IOException opening text/" + file + ".txt"));
93             return;
94         }
95     }
96
97     public FtpReply execute(BaseFtpConnection conn)
98         throws UnhandledCommandException {
99
100         if (conn.getRequest().hasArgument()) {
101             return FtpReply.RESPONSE_501_SYNTAX_ERROR;
102         }
103
104         try {
105             FtpReply reply = new FtpReply(200);
106             addTextToResponse(
107                 reply,
108                 conn
109                     .getRequest()
110                     .getCommand()
111                     .substring("SITE ".length())
112                     .toLowerCase());
113             return reply;
114         } catch (IOException JavaDoc e) {
115             return new FtpReply(200, "IO Error: " + e.getMessage());
116         }
117     }
118
119     public String JavaDoc[] getFeatReplies() {
120         return null;
121     }
122     public CommandHandler initialize(
123         BaseFtpConnection conn,
124         CommandManager initializer) {
125         return this;
126     }
127     public void load(CommandManagerFactory initializer) {
128     }
129     public void unload() {
130     }
131 }
132
Popular Tags