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.plugins;
019:
020: import java.io.FileInputStream;
021: import java.io.FileNotFoundException;
022: import java.io.IOException;
023: import java.util.ArrayList;
024: import java.util.Iterator;
025: import java.util.Properties;
026:
027: import net.sf.drftpd.FatalException;
028: import net.sf.drftpd.ObjectNotFoundException;
029: import net.sf.drftpd.event.DirectoryFtpEvent;
030: import net.sf.drftpd.event.Event;
031: import net.sf.drftpd.event.FtpListener;
032:
033: import org.apache.log4j.Logger;
034: import org.drftpd.GlobalContext;
035: import org.drftpd.PropertyHelper;
036: import org.drftpd.master.ConnectionManager;
037:
038: import f00f.net.irc.martyr.GenericCommandAutoService;
039: import f00f.net.irc.martyr.InCommand;
040: import f00f.net.irc.martyr.commands.MessageCommand;
041:
042: /**
043: * @version $Id: PreTime.java 1513 2006-10-13 22:41:08Z tdsoul $
044: */
045: public class PreTime extends FtpListener {
046: private static final Logger logger = Logger
047: .getLogger(PreTime.class);
048: private ConnectionManager _cm;
049: private SiteBot _irc;
050: private PreSiteBot _siteBot;
051: private ArrayList datedDirs;
052: private String prebot;
053:
054: public PreTime() throws FileNotFoundException, IOException {
055: super ();
056: }
057:
058: public void actionPerformed(Event event) {
059: if ("RELOAD".equals(event.getCommand())) {
060: reload();
061:
062: return;
063: }
064:
065: if (!(event instanceof DirectoryFtpEvent)) {
066: return;
067: }
068:
069: DirectoryFtpEvent dfe = (DirectoryFtpEvent) event;
070:
071: if (!getConnectionManager().getGlobalContext().getConfig()
072: .checkPathPermission("dirlog", dfe.getUser(),
073: dfe.getDirectory())) {
074: return;
075: }
076:
077: try {
078: if (dfe.getCommand().startsWith("MKD")) {
079: String[] release = dfe.getDirectory().getPath().split(
080: "/");
081: String releaseName;
082:
083: if (isDatedDir(release[1])) {
084: releaseName = release[3];
085:
086: if (release.length > 4) {
087: return; // CD1 || CD2 type directories
088: }
089: } else {
090: releaseName = release[2];
091:
092: if (release.length > 3) {
093: return; // CD1 || CD2 type directories
094: }
095: }
096:
097: if (releaseName == null) {
098: return; // DatedDir section created date dir
099: }
100:
101: _irc.getIRCConnection().sendCommand(
102: new MessageCommand(getPreBot(), "!pred "
103: + releaseName));
104: }
105: } catch (ArrayIndexOutOfBoundsException ex) {
106: // do nothing just ignore, it's a directory created in /
107: }
108: }
109:
110: public ConnectionManager getConnectionManager() {
111: return _cm;
112: }
113:
114: public String getPreBot() {
115: return prebot;
116: }
117:
118: public void init(GlobalContext gctx) {
119: super .init(gctx);
120: reload();
121: }
122:
123: public boolean isDatedDir(String section) {
124: for (Iterator iter = datedDirs.iterator(); iter.hasNext();) {
125: if (((String) iter.next()).equals(section)) {
126: return true;
127: }
128: }
129:
130: return false;
131: }
132:
133: private void reload() {
134: Properties props = new Properties();
135:
136: try {
137: props.load(new FileInputStream("conf/pretime.conf"));
138: } catch (IOException e) {
139: throw new RuntimeException(e);
140: }
141:
142: prebot = PropertyHelper.getProperty(props, "prebot");
143:
144: if (prebot == null) {
145: throw new FatalException("prebot not set in prebot.conf");
146: }
147:
148: datedDirs = new ArrayList();
149:
150: for (int i = 1;; i++) {
151: String temp = null;
152:
153: try {
154: temp = PropertyHelper.getProperty(props, "DatedDir."
155: + i);
156: } catch (NullPointerException e2) {
157: break;
158: }
159:
160: datedDirs.add(temp);
161: }
162:
163: if (_siteBot != null) {
164: _siteBot.disable();
165: }
166:
167: try {
168: _irc = (SiteBot) _cm.getGlobalContext().getFtpListener(
169: PreSiteBot.class);
170: _siteBot = new PreSiteBot(_irc, this );
171: } catch (ObjectNotFoundException e1) {
172: logger.warn("Error loading sitebot component", e1);
173: }
174: }
175:
176: public void unload() {
177: }
178:
179: public static class PreSiteBot extends GenericCommandAutoService {
180: private SiteBot _irc;
181: private PreTime _parent;
182:
183: protected PreSiteBot(SiteBot irc, PreTime parent) {
184: super (irc.getIRCConnection());
185: _irc = irc;
186: _parent = parent;
187: }
188:
189: protected void updateCommand(InCommand command) {
190: if (!(command instanceof MessageCommand)) {
191: return;
192: }
193:
194: MessageCommand msgc = (MessageCommand) command;
195:
196: if (msgc.getSource().getNick().equals(_parent.getPreBot())) {
197: if (msgc.isPrivateToUs(_irc.getIRCConnection()
198: .getClientState())) {
199: String[] msg = msgc.getMessage().split(" ");
200:
201: if (msg[0].equals("!preds")) {
202: String releaseName = msg[1];
203: int releaseTime = Integer.parseInt(msg[2]);
204: int weeks = releaseTime / 604800;
205: releaseTime = releaseTime % 604800;
206:
207: int days = releaseTime / 86400;
208: releaseTime = releaseTime % 86400;
209:
210: int hours = releaseTime / 3600;
211: releaseTime = releaseTime % 3600;
212:
213: int minutes = releaseTime / 60;
214: int seconds = releaseTime % 60;
215: String time = "-=PRETiME=- " + releaseName
216: + " was pred ";
217:
218: if (weeks != 0) {
219: time = time + weeks + " weeks ";
220: }
221:
222: if (days != 0) {
223: time = time + days + " days ";
224: }
225:
226: if (hours != 0) {
227: time = time + hours + " hours ";
228: }
229:
230: if (minutes != 0) {
231: time = time + minutes + " minutes ";
232: }
233:
234: if (seconds != 0) {
235: time = time + seconds + " seconds ";
236: }
237:
238: //TODO send to originating destination channel, must store a cookie either locally or in message
239: _irc.say("", time + "ago");
240: }
241: }
242: }
243: }
244: }
245: }
|