/*
Copyright 2002-2003 Steve Jolly

This file is part of MiniMe

MiniMe 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; specifically version 2 of the License.

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

import java.util.*;

public class ChannelInfo {

	private boolean secret;
	private boolean limited;
	private boolean keyed;
	private boolean moderated;
	private boolean blockingExternalMessages;
	private boolean inviteOnly;
	private boolean privat;
	private boolean topicProtection;

	private int limit;
	private String key=new String();
	private String name=new String();
	private String topic=new String();

	public ChannelInfo(String channelName) { //creates a channel with the given name
		name=channelName;
		secret=false;
		limited=false;
		keyed=false;
		moderated=false;
		blockingExternalMessages=false;
		inviteOnly=false;
		privat=false;
		topicProtection=false;
		limit=0;
	}

	public String getName() {
		return name;
	}

	public void setTopic(String newTopic) {
		topic=newTopic;
	}

	public String getTopic() {
		return topic;
	}

	public void setTopicProtection(boolean newValue) {
		topicProtection=newValue;
	}

	public boolean isTopicProtected() {
		return topicProtection;
	}

	public void setPrivate(boolean newValue) {
		privat=newValue;
	}

	public boolean isPrivate() {
		return privat;
	}

	public void setInviteOnly(boolean newValue) {
		inviteOnly=newValue;
	}

	public boolean isInviteOnly() {
		return inviteOnly;
	}

	public void setBlockingExternalMessages(boolean newValue) {
		blockingExternalMessages=newValue;
	}

	public boolean isBlockingExternalMessages() {
		return blockingExternalMessages;
	}

	public void setModerated(boolean newValue) {
		moderated=newValue;
	}

	public boolean isModerated() {
		return moderated;
	}

	public void setSecret(boolean newValue) {
		secret=newValue;
	}

	public boolean isSecret() {
		return secret;
	}

	public void setLimit(int newLimit) {
		if (newLimit>0) {
			limited=true;
		} else {
			limited=false;
		}
		limit=newLimit;
	}

	public int getLimit() {
		return limit;
	}

	public boolean isLimited() {
		return limited;
	}

	public void setKey(String newKey) {
		if (newKey.length()>0) {
			keyed=true;
		} else {
			keyed=false;
		}
		key=newKey;
	}

	public String getKey() {
		return key;
	}

	public boolean isKeyed() {
		return keyed;
	}

	//OK, this method sets up all the channel modes from the server CHANNELMODEIS response,
	//shorn of the initial number, recipient and channel name
	public void setFromModeLine(String modeLine) {
		StringTokenizer response=new StringTokenizer(modeLine, " ");
		String modes;
		try {
			modes=response.nextToken();
		} catch (NoSuchElementException e) {
			return;
		}
		secret=false;
		limited=false;
		moderated=false;
		blockingExternalMessages=false;
		inviteOnly=false;
		privat=false;
		topicProtection=false;
		limit=0;
		keyed=false;
		key=new String();
		
		for(int i=1;i<modes.length();i++) {
			String currentMode=modes.substring(i,i+1);
			if (currentMode.equals("s")) {
				secret=true;
			} else if (currentMode.equals("m")) {
				moderated=true;
			} else if (currentMode.equals("n")) {
				blockingExternalMessages=true;
			} else if (currentMode.equals("i")) {
				inviteOnly=true;
			} else if (currentMode.equals("p")) {
				privat=true;
			} else if (currentMode.equals("t")) {
				topicProtection=true;
			} else if (currentMode.equals("l")) {
				limited=true;
				try {
					limit=Integer.parseInt(response.nextToken());
				} catch (NumberFormatException e) {
					//ok, we probably don't deal very well with non-RFC-compliant responses here
					limited=false;
				} catch (NoSuchElementException f) {
					//or here
					limited=false;
				}
			} else if (currentMode.equals("k")) {
				keyed=true;
				try {
					key=response.nextToken();
				}	catch (NoSuchElementException e) {
					//this probably isn't very good either
					keyed=false;
				}
			}
		}
	}

	public String toString() {
		String returnValue=new String("ChannelInfo object for "+name);
		returnValue=returnValue.concat(" topic: '"+topic+"'");
		returnValue=returnValue.concat(" secret: '"+boolToString(secret)+"'");
		returnValue=returnValue.concat(" moderated: '"+boolToString(moderated)+"'");
		returnValue=returnValue.concat(" noexternals: '"+boolToString(blockingExternalMessages)+"'");
		returnValue=returnValue.concat(" inviteonly: '"+boolToString(inviteOnly)+"'");
		returnValue=returnValue.concat(" private: '"+boolToString(privat)+"'");
		returnValue=returnValue.concat(" topicprotection: '"+boolToString(topicProtection)+"'");
		returnValue=returnValue.concat(" limited: '"+boolToString(limited)+"'");
		returnValue=returnValue.concat(" limit: '"+limit+"'");
		returnValue=returnValue.concat(" keyed: '"+boolToString(keyed)+"'");
		returnValue=returnValue.concat(" key: '"+key+"'");
		return returnValue;
	}

	private String boolToString(boolean bool) {
		if (bool) return "true";
		else return "false";
	}

}