All pastes #594975 Raw Edit

Untitled

public text v1 · immutable
#594975 ·published 2007-06-29 07:02 UTC
rendered paste body
/*
 * This file is part of DrFTPD, Distributed FTP Daemon.
 *
 * DrFTPD is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * DrFTPD is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with DrFTPD; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
package org.drftpd.sitebot;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.sf.drftpd.ObjectNotFoundException;
import net.sf.drftpd.event.Event;
import net.sf.drftpd.event.FtpListener;
import net.sf.drftpd.util.Blowfish;
import net.tmods.extra.CNuke;

import org.drftpd.plugins.SiteBot;

import f00f.net.irc.martyr.GenericAutoService;
import f00f.net.irc.martyr.InCommand;
import f00f.net.irc.martyr.State;
import f00f.net.irc.martyr.commands.MessageCommand;

/**
 * @author tommie
 * -:BotNuke:- v0.2.1
 */
public class BotNuke extends GenericAutoService {
	private SiteBot sb;
	private String bn_nuker, bn_chars, bn_spaces;
	private boolean bn_strip;
	private CNuke cn;
	private ArrayList<NukeConfig> nukeList;
	private MyListener BotNukeListener;

	public BotNuke(SiteBot sitebot) {
		super(sitebot.getIRCConnection());
		sb = sitebot;
		
		BotNukeListener = new MyListener();
		sb.getGlobalContext().addFtpListener(BotNukeListener);
	}

	public class MyListener extends FtpListener {
		public MyListener() {
			reload();
		}
		public void actionPerformed(Event event) {
			if ("RELOAD".equals(event.getCommand())) {
				reload();
			}
		}
	}

	private void reload() {
		Properties cfg = new Properties();
		FileInputStream fis = null;

		try {
			fis = new FileInputStream("conf/botnuke.conf");
			cfg.load(fis);
			fis.close();
		} catch (IOException e) { return; }

		bn_nuker = cfg.getProperty("bn.nuker", "drftpd");

		String _bn_nukePrefix = cfg.getProperty("bn.nuke.prefix", "[NUKED]-");
		String _bn_nukeReasonPrefix = cfg.getProperty("bn.nuke.reason.prefix", "REASON-");

		bn_chars = cfg.getProperty("bn.chars", "[^a-zA-Z0-9\\[\\]\\._\\-\\(\\)\\s\\?\\!]");
		bn_spaces = cfg.getProperty("bn.spaces", ".");
		bn_strip = Boolean.parseBoolean(cfg.getProperty("bn.strip", "false"));

		cn = new CNuke(sb.getGlobalContext(), _bn_nukePrefix, _bn_nukeReasonPrefix);

		nukeList = new ArrayList<NukeConfig>();
		for (int i = 1;; i++) {        	
			String _nick = cfg.getProperty("bn." + i + ".nick");
			if (_nick == null) break;
			String _user = cfg.getProperty("bn." + i + ".user");
			if (_user == null) break;
			String _host = cfg.getProperty("bn." + i + ".host");
			if (_host == null) break;
			String _text = cfg.getProperty("bn." + i + ".text");
			if (_text == null) break;

			String _nuke_directory = cfg.getProperty("bn." + i + ".nuke.directory");
			if (_nuke_directory == null) break;

			String _nuke_reason = cfg.getProperty("bn." + i + ".nuke.reason", "unknown.fix.conf");
			int _nuke_mult = Integer.parseInt(cfg.getProperty("bn." + i + ".nuke.mult", "0"));

			nukeList.add(new NukeConfig(_nick, _user, _host, _text, _nuke_directory, _nuke_reason, _nuke_mult));
		}
	}

	protected void updateState(State _state) {		
	}

	protected void updateCommand(InCommand _ic) {
		if (_ic instanceof MessageCommand) {
			synchronized (this) {
				MessageCommand msgc = (MessageCommand) _ic;
				String _key, _message = "";

				if (!msgc.isPrivateToUs(getConnection().getClientState())) {
					// get key
					try {
						_key = sb.getBlowfishKey(msgc.getDest(), null);
					} catch (ObjectNotFoundException e) {
						return;
					}

					// decrypt message
					Blowfish bf = new Blowfish(_key);
					try {
						_message = bf.Decrypt(msgc.getMessage());
					} catch (UnsupportedEncodingException e) {
						return;
		ds			}
				}

				// do nukelist
				if (!nukeList.isEmpty()) {
					String _nick = msgc.getSource().getNick();
					String _user = msgc.getSource().getUser();
					String _host = msgc.getSource().getHost();

					Iterator iter = nukeList.iterator();
					while (iter.hasNext()) {
						NukeConfig nc = (NukeConfig) iter.next();
						if (_nick.matches(nc.getNick()) && _user.matches(nc.getUser()) && _host.matches(nc.getHost())) {
							if (bn_strip) _message = stripMessage(_message);
							Pattern pat = Pattern.compile(nc.getText());									
							Matcher mat = pat.matcher(_message);
							if (mat.find()) {
								cn.doNuke(mat.replaceAll(nc.getNukeDirectory()).trim(),
										mat.replaceAll(nc.getNukeReason()).replaceAll(bn_chars, "").replaceAll("\\s", bn_spaces).trim(),
										nc.getNukeMultiplier(), bn_nuker);
								break;
							}
						}
					}
				}
			}
		}
	}

	public class NukeConfig {
		private String nick, user, host, text, nuke_directory, nuke_reason;
		private int nuke_mult;

		public NukeConfig(String _nick, String _user, String _host, String _text, String _nuke_directory, String _nuke_reason, int _nuke_mult) {
			nick = _nick;
			user = _user;
			host = _host;
			text = _text;
			nuke_directory = _nuke_directory;
			nuke_reason = _nuke_reason;
			nuke_mult = _nuke_mult;
		}

		public String getNick() {
			return nick;
		}

		public String getUser() {
			return user;
		}

		public String getHost() {
			return host;
		}

		public String getText() {
			return text;
		}

		public String getNukeDirectory() {
			return nuke_directory;
		}

		public String getNukeReason() {
			return nuke_reason;
		}

		public int getNukeMultiplier() {
			return nuke_mult;
		}
	}

	public String stripMessage(String str) {
		return stripMessage(new StringBuffer(str)).toString();
	}

	public StringBuffer stripMessage(StringBuffer buf) {
		int len = buf.length();
		for (int i = 0, j = 0, c; i < len; i++, j = i) {
			try {
				c = buf.charAt(i);
				if (c == 3) {
					c = buf.charAt(++j);
					if ('0' <= c && c <= '9') {
						c = buf.charAt(++j);
						if ('0' <= c && c <= '9') c = buf.charAt(++j);
					}
					if (c == ',')
						c = buf.charAt(++j);
					if ('0' <= c && c <= '9') {
						c = buf.charAt(++j);
						if ('0' <= c && c <= '9') c = buf.charAt(++j);
					}
				} else if (c == 1 || c == 31 || c == 2 || c == 15 || c == 22) {
					j++;
				}
			} catch (StringIndexOutOfBoundsException exc) {
			}
			if (j > i) {
				buf = buf.delete(i, j);
				len -= (j - i);
				i -= (j - i);
			}
		}
		return buf;
	}
}