import java.io.*;
import java.net.*;
import java.util.*;

public class Util
{
	public static void addUser(String UserId,String UserPasswd,long UserPerm)
	{
		IO.outputFile(UserId + "\n" + UserPasswd + "\n" + UserPerm + "\n",Config.BBSHOME + File.separatorChar + "etc" + File.separatorChar + "passwd");
	}
	public static long getPerm(String UserId)
	{
		String TempId = null,TempPasswd = null,TempPerm = null;
		try
		{
			BufferedReader in = IO.inputFile(Config.BBSHOME + File.separatorChar + "etc" + File.separatorChar + "passwd");
			while((TempId = in.readLine()) != null)
			{
				TempPasswd = in.readLine();
				TempPerm = in.readLine();
				if(TempId.equals(UserId))
					return Long.parseLong(TempPerm,10);
			}
			in.close();
		}
		catch(IOException e){}
		return 0;
	}
	public static boolean hasPerm(long Perm,long UserPerm)
	{
		if(UserPerm >= Perm)
			return true;
		return false;
	}

	public static boolean isOnLine(String Id,Vector UserList)
	{
		for(int i = 0;i < UserList.size();i++)
		{
			if(((User)(UserList.elementAt(i))).UserId.equalsIgnoreCase(Id))
				return true;
		}
		return false;
	}

	public static boolean isIdUsed(String Id)
	{
		File file = new File(Config.BBSHOME + File.separatorChar + "home" + File.separatorChar + Id.toLowerCase());
		return file.exists();
	}

	public static boolean isBoardExit(String BoardName)
	{
		File file = new File(Config.BBSHOME + File.separatorChar + "boards" + File.separatorChar + BoardName.toLowerCase());
		return file.exists();
	}

	public static boolean isFileExists(String FileName)
	{
		File file = new File(FileName);
		return file.exists();
	}

	public static void mkdir(String DirName)
	{
		File file = new File(DirName);
		file.mkdir();
	}

	public static boolean checkPasswd(String UserId,String UserPasswd)
	{
		String TempId = null,TempPasswd = null,TempPerm = null;
		try
		{
			BufferedReader in = IO.inputFile(Config.BBSHOME + File.separatorChar + "etc" + File.separatorChar + "passwd");
			while((TempId = in.readLine()) != null)
			{
				TempPasswd = in.readLine();
				TempPerm = in.readLine();
				if(TempId.equals(UserId) && TempPasswd.equals(UserPasswd))
					return true;
			}
			in.close();
		}
		catch(IOException e){}
		return false;
	}
}
