package com.fuse.access_control;

/*
	FUSE Light Server - multiuser server application
	Copyright (C) 2000 Aapo Kyrola / Sulake Oy  Helsinki, Finland

	This program 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.

	This program 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 this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/


import java.io.*;
import java.util.*;
import java.text.*;

import com.fuse.*;
import com.fuse.net.*;
import com.fuse.storage.data_objects.FUSEUser;

/**
  * AccessControl-system enables trusted users to throw
  * other users out of the system and do temporary bannings
  * for them. 
  * @author Aapo Kyrola
  */
public class AccessControl {

	protected File groupsFile;
	protected File banningFile;
	protected SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy HH:mm");
	
	public AccessControl() {
	     groupsFile = new File("groups");
		 banningFile = 	new File("bannings");
	}	
	
	/**
	  * Returns true if the given IP-address is banned
	  */	
	public synchronized boolean getIpBanned(String ip) {
		try {
			Properties bannings = new Properties();
			bannings.load(new FileInputStream(banningFile));
		
			String banningsTime = bannings.getProperty(ip);
			if (banningsTime == null) return false;
		
			Date d = sdf.parse(banningsTime);	
			if (d.getTime() > (new Date()).getTime()) {
				Log.status("IP=" + ip + " banned until " + banningsTime);
				return true;
			} else {
				bannings.remove(ip);
				bannings.save(new FileOutputStream(banningFile), "FUSE bannings");
				return false;
			}
			
		} catch (Exception e) {
			Log.error(e);
			return false;
		}
	}
	
	/**
	  * Bans an IP-address until Date d
	  */
	public synchronized void setIpBanned(String ip, Date d) {
		try {
			Properties bannings = new Properties();
			bannings.load(new FileInputStream(banningFile));
			bannings.put(ip, sdf.format(d));
			bannings.save(new FileOutputStream(banningFile), "FUSE bannings");
			Log.status("IP ip=" + ip + " set to be banned until " + sdf.format(d));
		} catch (Exception e) {
			Log.error(e);
		}
	}	
	
	/**
	  * Returns whether a user is a member of given group
	  */
	public synchronized boolean getIsMemberOfGroup(FUSEUser user, String groupName) {
		try {
		   Properties groups = new Properties();
		   groups.load(new FileInputStream(groupsFile));
		   String userGroups = groups.getProperty(user.getName());
		   if (userGroups == null) return false;
		   Log.status("user groups=" + userGroups);
		   StringTokenizer zt = new StringTokenizer(userGroups, ",");
		   while(zt.hasMoreTokens()) {
		   		if (zt.nextToken().equals(groupName)) return true;	
		   }
		} catch (Exception e) {
			Log.error(e);
		}
		return false;
	}  
	
}
