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.io.BufferedReader;
021: import java.io.FileInputStream;
022: import java.io.FileNotFoundException;
023: import java.io.IOException;
024: import java.io.InputStreamReader;
025: import java.io.UnsupportedEncodingException;
026: import java.util.ArrayList;
027: import java.util.HashMap;
028: import java.util.Iterator;
029:
030: import org.apache.log4j.Logger;
031: import org.drftpd.GlobalContext;
032:
033: import f00f.net.irc.martyr.commands.MessageCommand;
034:
035: /**
036: * @author zubov
037: * @version $Id: GenericTextOutput.java 1513 2006-10-13 22:41:08Z tdsoul $
038: */
039: public class GenericTextOutput extends IRCCommand {
040: private static final Logger logger = Logger
041: .getLogger(GenericTextOutput.class);
042: private HashMap<String, String> _commands;
043:
044: public GenericTextOutput(GlobalContext gctx) {
045: super (gctx);
046: reload();
047: }
048:
049: private void reload() {
050: _commands = new HashMap<String, String>();
051:
052: BufferedReader in;
053:
054: try {
055: in = new BufferedReader(new InputStreamReader(
056: new FileInputStream("conf/generictextoutput.conf")));
057: } catch (FileNotFoundException e) {
058: throw new RuntimeException(
059: "conf/generictextoutput.conf could not be opened",
060: e);
061: }
062:
063: String line;
064: try {
065: while ((line = in.readLine()) != null) {
066: if (line.startsWith("#") || line.trim().equals("")) {
067: continue;
068: }
069:
070: String[] args = line.split(" ");
071:
072: if (args.length < 2) {
073: continue;
074: }
075:
076: _commands.put(args[0], args[1]);
077:
078: }
079: } catch (IOException e1) {
080: throw new RuntimeException(e1);
081: } finally {
082: try {
083: if (in != null) {
084: in.close();
085: }
086: } catch (IOException e2) {
087: }
088: }
089: }
090:
091: public ArrayList<String> doText(String args, MessageCommand msgc) {
092: ArrayList<String> out = new ArrayList<String>();
093: String msg = msgc.getMessage();
094:
095: for (Iterator iter = _commands.keySet().iterator(); iter
096: .hasNext();) {
097: String trigger = (String) iter.next();
098:
099: if (msg.startsWith(trigger)) {
100: try {
101: BufferedReader rd = new BufferedReader(
102: new InputStreamReader(new FileInputStream(
103: (String) _commands.get(trigger)),
104: "ISO-8859-1"));
105: String line;
106:
107: try {
108: while ((line = rd.readLine()) != null) {
109: if (!line.startsWith("#")) {
110: out.add(line);
111: }
112: }
113: } finally {
114: rd.close();
115: }
116: break;
117: } catch (UnsupportedEncodingException e) {
118: logger.warn(e);
119: } catch (FileNotFoundException e) {
120: logger.warn(e);
121: out.add(e.getMessage());
122: } catch (IOException e) {
123: logger.warn(e);
124: }
125: }
126: }
127: return out;
128: }
129: }
|