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.drmods.plugins.irc;
019:
020: import java.io.FileInputStream;
021: import java.io.IOException;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.Properties;
025:
026: import net.sf.drftpd.master.BaseFtpConnection;
027: import net.sf.drftpd.util.ReplacerUtils;
028:
029: import org.apache.log4j.Logger;
030: import org.drftpd.GlobalContext;
031: import org.drftpd.Time;
032: import org.drftpd.plugins.SiteBot;
033: import org.drftpd.sitebot.IRCCommand;
034: import org.drftpd.usermanager.NoSuchUserException;
035: import org.drftpd.usermanager.User;
036: import org.tanesha.replacer.FormatterException;
037: import org.tanesha.replacer.ReplacerEnvironment;
038: import org.tanesha.replacer.ReplacerFormat;
039: import org.tanesha.replacer.SimplePrintf;
040:
041: import f00f.net.irc.martyr.State;
042: import f00f.net.irc.martyr.commands.MessageCommand;
043:
044: /**
045: * @author Teflon
046: * @version $Id$
047: */
048: public class Kick extends IRCCommand {
049: private static final Logger logger = Logger.getLogger(Kick.class);
050: private long _idleTimeout;
051: private int _usersPerLine;
052:
053: public Kick(GlobalContext gctx) {
054: super (gctx);
055: loadConf("conf/drmods.conf");
056: }
057:
058: private void loadConf(String confFile) {
059: Properties cfg = new Properties();
060: FileInputStream file = null;
061: try {
062: file = new FileInputStream(confFile);
063: cfg.load(file);
064: String idleTimeout = cfg.getProperty("kick.idlelimit");
065: String usersPerLine = cfg.getProperty("kick.usersperline");
066: if (idleTimeout == null) {
067: throw new RuntimeException(
068: "Unspecified value 'kick.idlelimit' in "
069: + confFile);
070: }
071: if (usersPerLine == null) {
072: throw new RuntimeException(
073: "Unspecified value 'kick.usersperline' in "
074: + confFile);
075: }
076:
077: _idleTimeout = Long.parseLong(idleTimeout) * 1000;
078: _usersPerLine = Integer.parseInt(usersPerLine);
079: } catch (Exception e) {
080: logger.error("Error reading " + confFile, e);
081: throw new RuntimeException(e.getMessage());
082: } finally {
083: if (file != null) {
084: try {
085: file.close();
086: } catch (IOException e) {
087: }
088: }
089: }
090: }
091:
092: public ArrayList<String> doKick(String args, MessageCommand msgc) {
093: ArrayList<String> out = new ArrayList<String>();
094: ReplacerEnvironment env = new ReplacerEnvironment(
095: SiteBot.GLOBAL_ENV);
096:
097: String cmduser = msgc.getSource().getNick();
098: String cmdchan = msgc.getDest();
099:
100: try {
101: ReplacerFormat kickirc = ReplacerUtils.finalFormat(
102: Kick.class, "kick.ircmsg");
103: ReplacerFormat kickftp = ReplacerUtils.finalFormat(
104: Kick.class, "kick.ftpmsg");
105: ReplacerFormat userformat = ReplacerUtils.finalFormat(
106: Kick.class, "kick.userformat");
107:
108: env.add("idlelimit", Time.formatTime(_idleTimeout));
109:
110: ArrayList<BaseFtpConnection> conns = new ArrayList<BaseFtpConnection>(
111: getGlobalContext().getConnectionManager()
112: .getConnections());
113: int count = 0;
114: String msg = "";
115: boolean found = false;
116: for (Iterator iter = conns.iterator(); iter.hasNext();) {
117: BaseFtpConnection conn = (BaseFtpConnection) iter
118: .next();
119: User cuser;
120:
121: try {
122: cuser = conn.getUser();
123: } catch (NoSuchUserException e) {
124: continue;
125: }
126:
127: long idletime = System.currentTimeMillis()
128: - conn.getLastActive();
129: env.add("idletime", Time.formatTime(idletime));
130: env.add("idleuser", cuser.getName());
131: env.add("ircuser", cmduser);
132: env.add("ircchan", cmdchan);
133:
134: if (!conn.getDataConnectionHandler().isTransfering()
135: && idletime > _idleTimeout) {
136: conn.stop(SimplePrintf.jprintf(kickftp, env));
137: msg += SimplePrintf.jprintf(userformat, env) + " ";
138: count++;
139: found = true;
140: }
141: if ((count >= _usersPerLine || !iter.hasNext())
142: && !msg.trim().equals("")) {
143: env.add("users", msg.trim());
144: out.add(SimplePrintf.jprintf(kickirc, env));
145: count = 0;
146: msg = "";
147: }
148: }
149: if (!found) {
150: out.add(ReplacerUtils.jprintf("kick.none", env,
151: Kick.class));
152: }
153: } catch (FormatterException e) {
154: logger.warn("", e);
155: }
156:
157: return out;
158: }
159:
160: protected void updateState(State state) {
161: }
162: }
|