/*
Written by: Jaap Blom/ThaPulp
thanks to Silver for his  emote plugin and everybody from irc://irc/mozilla.org/chatzilla for their help
*/
plugin.id = "channel-modes";

plugin.init =
function _init(glob) {
	this.major = 1;  // Major version number.
	this.minor = 3;  // Minor version number.
	this.version = this.major + "." + this.minor;
	this.description = "A script that lets you change channel modes";
	/*c
	No ANSI color can be sent to the channel
C
	No CTCP's allowed in the channel
i
	Invite required
k <key>
	Sets a key needed to joinm
	Moderated channel. Only +v/o/h users may speak
l <##>
	Sets max number of users
N
	No nick name changes permitted
n
	No messages from outside channels
p
	Makes channel private
S
	Strips all incoming colors
s
	Makes channel secret
t
	Only chanops can set topic
T
	No NOTICE's allowed in the channel*/

	// Command definitions.
	this.cmds = [		
		["colorban", cmdColorBan, CMD_CONSOLE | CMD_NO_HELP | CMD_NEED_CHAN],
		["ctcpban", cmdCtcpBan, CMD_CONSOLE | CMD_NO_HELP | CMD_NEED_CHAN],
		["inviteonly", cmdInviteOnly, CMD_CONSOLE | CMD_NO_HELP | CMD_NEED_CHAN],
		["key", cmdKey, CMD_CONSOLE | CMD_NO_HELP | CMD_NEED_CHAN],
		["maxusers", cmdMaxUsers, CMD_CONSOLE | CMD_NO_HELP | CMD_NEED_CHAN],
		["moderatechannel", cmdModerateChannel, CMD_CONSOLE | CMD_NO_HELP | CMD_NEED_CHAN],
		["nickchangeban", cmdNickChangeBan, CMD_CONSOLE | CMD_NO_HELP | CMD_NEED_CHAN],
		["outsidemessagesban", cmdOutsideMessagesBan, CMD_CONSOLE | CMD_NO_HELP | CMD_NEED_CHAN],
		["private", cmdPrivate, CMD_CONSOLE | CMD_NO_HELP | CMD_NEED_CHAN],
		["stripcolors", cmdStripColors, CMD_CONSOLE | CMD_NO_HELP | CMD_NEED_CHAN],
		["secret", cmdSecret, CMD_CONSOLE | CMD_NO_HELP | CMD_NEED_CHAN],
		["opstopic", cmdOpsTopic, CMD_CONSOLE | CMD_NO_HELP | CMD_NEED_CHAN],
		["noticeban", cmdNoticeBan, CMD_CONSOLE | CMD_NO_HELP | CMD_NEED_CHAN]
	];
	
	// Menu item definitions.
	//var bla = "i"
	//the submenu
	this.menuItems = 
	[
		["-", {visibleif: "(cx.TYPE == 'IRCChannel') && (cx.channel.iAmOp() || cx.channel.iAmHalfOp())"}],
        [">chanmod:channelmodes", {visibleif: "(cx.TYPE == 'IRCChannel') && (cx.channel.iAmOp() || cx.channel.iAmHalfOp())"}]
        //[">chanmod:channelmodes", {visibleif: "(cx.channel.iAmOp() || cx.channel.iAmHalfOp()) && (cx.TYPE == 'IRCChannel')"}]
	];

	window.getChannelModes = getChannelModes;
	
	client.menuSpecs["chanmod:channelmodes"] = {
			label: "Channel modes",
			//visibleif: "(cx.channel.iAmOp() || cx.channel.iAmHalfOp())",
			items:
			[
				["colorban", {type: "checkbox", checkedif: "(getChannelModes(cx)).modes.indexOf(\"c\",0)>0", visibleif: "(cx.channel.iAmOp() || cx.channel.iAmHalfOp())", label: "'c' No colors allowed"}],
				["ctcpban", {type: "checkbox", checkedif: "(getChannelModes(cx)).modes.indexOf(\"C\",0)>0", visibleif: "(cx.channel.iAmOp() || cx.channel.iAmHalfOp())", label: "'C' No CTCP allowed"}],
				["inviteonly", {type: "checkbox", checkedif: "(getChannelModes(cx)).modes.indexOf(\"i\",0)>0", visibleif: "(cx.channel.iAmOp() || cx.channel.iAmHalfOp())", label: "'i' Invite only"}],
				["key", {type: "checkbox", checkedif: "(getChannelModes(cx)).modes.indexOf(\"k\",0)>0", visibleif: "(cx.channel.iAmOp() || cx.channel.iAmHalfOp())", label: "'k' Join key..."}],
				["maxusers", {type: "checkbox", checkedif: "(getChannelModes(cx)).modes.indexOf(\"l\",0)>0", visibleif: "(cx.channel.iAmOp() || cx.channel.iAmHalfOp())", label: "'l' Set maximum users..."}],
				["moderatechannel", {type: "checkbox", checkedif: "(getChannelModes(cx)).modes.indexOf(\"m\",0)>0", visibleif: "(cx.channel.iAmOp() || cx.channel.iAmHalfOp())", label: "'m' Moderate channel"}],
				//["nickchangeban", {visibleif: "(cx.channel.iAmOp() || cx.channel.iAmHalfOp())", label: "'N' No nick changes"}],
				["outsidemessagesban", {type: "checkbox", checkedif: "(getChannelModes(cx)).modes.indexOf(\"n\",0)>0", visibleif: "(cx.channel.iAmOp() || cx.channel.iAmHalfOp())", label: "'n' No external messages"}],
				["private", {type: "checkbox", checkedif: "(getChannelModes(cx)).modes.indexOf(\"p\",0)>0", visibleif: "(cx.channel.iAmOp() || cx.channel.iAmHalfOp())", label: "'p' Mark as private"}],
				["stripcolors", {type: "checkbox", checkedif: "(getChannelModes(cx)).modes.indexOf(\"S\",0)>0", visibleif: "(cx.channel.iAmOp() || cx.channel.iAmHalfOp())", label: "'S' Strip colors"}],
				["secret", {type: "checkbox", checkedif: "(getChannelModes(cx)).modes.indexOf(\"s\",0)>0", visibleif: "(cx.channel.iAmOp() || cx.channel.iAmHalfOp())", label: "'s' Secret channel"}],
				["opstopic", {type: "checkbox", checkedif: "(getChannelModes(cx)).modes.indexOf(\"t\",0)>0", visibleif: "(cx.channel.iAmOp() || cx.channel.iAmHalfOp())", label: "'t' Topic change ops only"}]
				//["noticeban", {visibleif: "(cx.channel.iAmOp() || cx.channel.iAmHalfOp())", label: "'T' No notices allowed"}]
			]
		}
	
	// Menus to add items to.
	this.menuNames = {
		"context:tab": true
	};
	
	return "OK";
}

plugin.enable =
function _enable() {
	this.commands = client.commandManager.defineCommands(this.cmds);
	for (var i = 0; i < this.menuItems.length; i++) {
		this.menuItems[i].plugin = this.id; // Self-reference, for easy removal.
		for (var menu in this.menuNames) {
			client.menuSpecs[menu].items.push(this.menuItems[i]);
		}
	}
	/*this.menuItems[0].plugin = this.id; // Self-reference, for easy removal.
	this.menuItems[1].plugin = this.id; // Self-reference, for easy removal.
	client.menuManager.appendMenuItems("context:messages",this.menuItems);*/
	client.updateMenus();
	return true;
}

plugin.disable =
function _disable() {
	client.commandManager.removeCommands(this.commands);
	for (var menu in this.menuNames) {
		for (var i = 0; i < client.menuSpecs[menu].items.length; i++) {
			if (client.menuSpecs[menu].items[i].plugin == this.id) {
				client.menuSpecs[menu].items.splice(i--, 1);
			}
		}
	}
	client.updateMenus();
	return true;
}

function cmdColorBan(e)
{
	//dispatch("echo modes?: "+e.channel.mode.modeD.length);
	//dispatch("echo Mode c: no colors allowed in channel");
	display("Mode c: no colors allowed in channel", MT_INFO);
	changeMode("c", e);
}

function cmdCtcpBan(e)
{
	//dispatch("echo Mode C: no CTCP messages allowed in channel");
	display("Mode C: no CTCP messages allowed in channel", MT_INFO);
	changeMode("C", e);
}

function cmdModerateChannel(e)
{
	//dispatch("echo Mode m: moderated channel (ops and voice can speak)");
	display("Mode m: moderated channel (ops and voice can speak)", MT_INFO);
	changeMode("m", e);
}

function cmdNickChangeBan(e)
{
	//dispatch("echo Mode N: no nickchanges allowed in channel");
	display("Mode N: no nickchanges allowed in channel", MT_INFO);
	changeMode("N", e);
}

function cmdOutsideMessagesBan(e)
{
	//dispatch("echo Mode n: no outside messages allowed in channel");
	display("Mode n: no outside messages allowed in channel", MT_INFO);
	changeMode("n", e);
}

function cmdPrivate(e)
{
	//dispatch("echo Mode p: marks channel as private");
	display("Mode p: marks channel as private", MT_INFO);
	changeMode("p", e);
}

function cmdStripColors(e)
{
	//dispatch("echo Mode S: strips all colors from the channel");
	display("Mode S: strips all colors from the channel", MT_INFO);
	changeMode("S", e);
}

function cmdSecret(e)
{
	//dispatch("echo Mode s: channel made secret");
	display("Mode s: channel made secret", MT_INFO);
	changeMode("s", e);
}

function cmdOpsTopic(e)
{
	//dispatch("echo Mode t: only ops can change the topic");
	display("Mode t: only ops can change the topic", MT_INFO);
	changeMode("t", e);
}

function cmdNoticeBan(e)
{
	//dispatch("echo Mode T: no notices allowed in channel");
	display("Mode T: no notices allowed in channel", MT_INFO);
	changeMode("T", e);
}

function cmdInviteOnly(e) 
{
	//dispatch("echo Mode i: channel set to invite only");
	display("Mode i: channel set to invite only", MT_INFO);
	changeMode("i", e);
}

function cmdKey(e)
{
	//dispatch("echo Mode k: key to join the channel");
	display("Mode k: key to join the channel", MT_INFO);
	//var ModeParameters;
	var modes = getChannelModes(e);
	//dispatch("say ModeParameters: "+ModeParameters);
	//dispatch("say modes: "+modes);
	if(modes.modes.indexOf("k",0)>0)
	{
		//var ModeParameters;
		//var ActiveModes = getChannelModes(e, ModeParameters);
		//dispatch("say removing channel key");
		if(modes.modes.indexOf("l",0)>0)
		{
			var split = modes.attributes.indexOf(" ",0);
			//dispatch("say modes.attributes.length: "+modes.attributes.length);
			//dispatch("say modes.attributes.substr(split+1): "+modes.attributes.substr(split+1));
			modes.attributes = modes.attributes.substr(split+1);
			//dispatch("say modes.attributes.length: "+modes.attributes.length);
			//dispatch("say split: "+split);
			//dispatch("say modes.attributes.substr(split+1): "+modes.attributes.substr(split+1));
			//dispatch("say modes.attributes: "+modes.attributes);
		}
		
		//dispatch("say "+e.channel.canonicalName+" -k "+modes.attributes );
		dispatch("mode "+e.channel.canonicalName+" -k "+modes.attributes );
	}
	else
	{
		var neededkey = prompt("Give key, please:");
		//dispatch("say adding channel key");
		if(neededkey!=null)
		{
			dispatch("mode "+e.channel.canonicalName+" +k "+neededkey);
		}
	}
}

function cmdMaxUsers(e)
{
	//dispatch("echo Mode l: set maximum number of users that can join the channel");
	dispatch("echo Mode l: set maximum number of users that can join the channel");
	var modes = getChannelModes(e);
	if(modes.modes.indexOf("l",0)>0)
	{
		dispatch("mode "+e.channel.canonicalName+" -l");
	}
	else
	{
		var maxusers = prompt("Give maximum users, please:");
		//dispatch("say adding channel key");
		if(validateInteger(maxusers))
		{
			dispatch("mode "+e.channel.canonicalName+" +l "+maxusers);
		}
		else
		{
			//dispatch("echo "+maxusers+" is not a valid number");
			display(maxusers+" is not a valid number", MT_ERROR);
		}
	}
}



function changeMode(ChangeMode, e)
{
	var modes = getChannelModes(e);
	if(modes.modes.indexOf(ChangeMode,0)>0)
	{
		//dispatch("say mode -i");
		dispatch("mode "+e.channel.canonicalName+" -"+ChangeMode);
	}
	else
	{
		//dispatch("say mode +i");
		dispatch("mode "+e.channel.canonicalName+" +"+ChangeMode);
	}
}

function getChannelModes(e)
{
	var str = "+";
	var params = "";
	var mode = e.channel.mode;
	for (var m in mode.modeD) 
	{ 
		if (mode.modeD[m])
			str += m; 
	} 
	for (var m in mode.modeC) 
	{ 
		if (mode.modeC[m]) 
		{ 
			str += m; 
			params += " " + mode.modeC[m]; 
		} 
	} 
	for (var m in mode.modeB) 
	{ 
		if (mode.modeB[m]) 
		{ 
			str += m; 
			params += " " + mode.modeB[m]; 
		} 
	}
	str + params;
	params = params.substr(1);
	//attributes = params;
	//return str;
	return { modes: str, attributes: params };
}

function validateInteger( strValue ) {
  var objRegExp  = /(^-?\d\d*$)/;

  //check for integer characters
  return objRegExp.test(strValue);
}
