# Default aliases file

#
# TODO:
#	ignore, notify and unignore should go in the core
#
#	If possible, get rid of $str.* from the default popups
#	avoiding to load the module at every channel popup open action...
#	(Find a core function that retrieves the number of nicknames passed ?)
#	Another solution would be to increase the str.module core lock-time ?
#

#
# Backward compatibility stuff
#
alias(avatar)
{
	# avatar target file
	if(!$0)
	{
		echo "Missing target, no action taken"
		return
	}
	if($1)
	{
		avatar.set $1
	}
	avatar.notify $0
}
alias(clear)
{
	window.clearOutput
}
alias(connect)
{
	objects.connect $0 $1 $2 $3
}
alias(disconnect)
{
	objects.disconnect $0 $1 $2 $3
}
alias(killclass)
{
	objects.killclass $0
}
alias(clearobjects)
{
	objects.clear $0
}
alias(nickserv)
{
	# Silent
	raw -q NickServ $0-
}

alias(chanserv)
{
	# Silent
	raw -q ChanServ $0-
}

alias(memoserv)
{
	# Silent
	raw -q MemoServ $0-
}

alias(ns)
{
	# Silent
	raw -q nickserv $0-
}


alias(cs)
{
	# Silent
	raw -q chanserv $0-
}

alias(ms)
{
	# Silent
	raw -q memoserv $0-
}

alias(gnotice)
{
	# Global notice

	#Check null notice
	if(!$0){
		echo $tr("Usage"): /gnotice [-waohvq] <$tr("message")>
		echo $tr("-m: send a message instead of a notice")
		echo $tr("-w: send notice to all channel owners")
		echo $tr("-a: send notice to all channel admins")
		echo $tr("-o: send notice to all channel ops")
		echo $tr("-h: send notice to all channel halfops")
		echo $tr("-v: send notice to all channel voices")
		echo $tr("-q: quiet mode")
		halt
	}

	# Detect message type
	if($sw(m)) %cmd="msg"
	else %cmd="notice"

	# Check for quiet message
	if($sw(q)) %cmd=$str.append(%cmd," -q")

	# Check null switches
	if((!$sw(w))&&(!$sw(a))&&(!$sw(o))&&(!$sw(h))&&(!$sw(v))){
		eval %cmd $chan.name($active) $0-
		halt
	}

	# Check switches passed
	if($sw(w)) eval %cmd \~$chan.name($active) $0-
	if($sw(a)) eval %cmd \!$chan.name($active) $0-
	if($sw(o)) eval %cmd \@$chan.name($active) $0-
	if($sw(h)) eval %cmd \%$chan.name($active) $0-
	if($sw(v)) eval %cmd \+$chan.name($active) $0-
}
alias(wallop)
{
	if(!$0){
		echo $tr("Usage"): /wallop <$tr("message")>
		halt
	}

	# Due to server flood, timing may be very high
	echo -w=$active $tr("Sending wallop to") $chan.name($active)
	timer(wallop,5000,$chan.users(,,o),$0-){
		if(%:localOps == ""){ %:localOps = $chan.users(,,o); %:curIndex = 0; }
		if(%:curIndex == %:localOps[]#){ killtimer wallop; return; }

		for(%i=0;%i<1;%i++){
			if(%:localOps[%:curIndex] != $my.nick){
				if(%:curIndex == %:localOps[]#){ killtimer wallop; return; }
				msg -q %:localOps[%:curIndex] $1-
				%:curIndex++
			}
		}
	}
}

alias(kill){
	raw -q kill $0 :$1-
}

alias(ms)
{
	# Silent
	quote ms $0-
}

# j: This is a really common one

alias(j)
{
	#first we do a test if a chan is given as parameter
	if(!$0)
	{
		echo $tr("Usage:")
		echo "	  /j <chan>"
	} else {
		join $0-
	}
}

# s: This too

alias (s)
{
	if(!$0)
	{
		echo $tr("Usage:")
		echo "		/s <server>"
	} else {
		server $0-
	}
}

# q: Ok, I am lazy :P

alias (q)
{
	if(!$0)
	{
		echo $tr("Usage:")
		echo "		/q <nick>"
	} else {
		query $0-
	}
}

# Mmmm, this could be improved...

alias (lag)
{
	if(!$server)
	{
		echo "[LAG]: " $tr("You must be connected to a server")
		halt
	}
	echo $tr("Lag is") $lag.lag() $tr("milliseconds")
}

# names: maybe this should be implemented on the core?

alias(names)
{
	if(!$0)
	{
		if($window.type($active)!="channel")
		{
		        echo $tr("Usage:") "/names <channel>"
		} else {
			raw names $active
		}
	} else {
		raw names $0-
	}
}

# kickban

alias (kb)
{
	if(!$0)
	{
		echo $tr("Usage:")
		echo "	  /kb <nick_list> [" $tr("reason") "]"
		halt;
	}

	if(!$channel)
	{
		echo "[KB]: " $tr("This is not a channel window")
		halt;
	}

	foreach(%nick,$str.split(",",$0))
	{
		if(%nick)
		{
			%mask = $mask(%nick,$option(uintDefaultBanType));

			if(!%mask)%mask = %nick; # plain ban

			%reason = $1-
			#if(!%reason)%reason = "You're not welcome here!"

			# This is tricky : we send everything
			# as a single raw message just because
			# we do not want delays between kick and ban
			# if anti-server-flood is enabled

			%raw = "kick $target %nick :%reason"

			# kick %nick %reason

			if($chan.isOp(%nick))%raw2 = "mode $target -o+b %nick %mask"
			else %raw2 = "mode $target +b %mask"

			raw -q %raw2$cr$lf%raw
		}
	}
}

# chanowner

alias (chanowner)
{
	if(!$0)
	{
		echo $tr("Usage:")
		echo "	  /chanowner <nick_list>"
		halt;
	}
	if(!$channel)
	{
		echo "[CHANOWNER]: " $tr("This is not a channel window")
		halt;
	}

	foreach(%nick,$str.split(",",$0))
	{
		if(%nick)
		{
			mode $chan.name() +q %nick
		}
	}
}

# chanadmin

alias (chanadmin)
{
	if(!$0)
	{
		echo $tr("Usage:")
		echo "	  /chanadmin <nick_list>"
		halt;
	}
	if(!$channel)
	{
		echo "[CHANADMIN]: " $tr("This is not a channel window")
		halt;
	}

	foreach(%nick,$str.split(",",$0))
	{
		if(%nick)
		{
			mode $chan.name() +a %nick
		}
	}
}

# dechanowner

alias (dechanowner)
{
	if(!$0)
	{
		echo $tr("Usage:")
		echo "	  /dechanowner <nick_list>"
		halt;
	}
	if(!$channel)
	{
		echo "[DECHANOWNER]: " $tr("This is not a channel window")
		halt;
	}

	foreach(%nick,$str.split(",",$0))
	{
		if(%nick)
		{
			mode $chan.name() -q %nick
		}
	}
}

# dechanadmin

alias (dechanadmin)
{
	if(!$0)
	{
		echo $tr("Usage:")
		echo "	  /dechanadmin <nick_list>"
		halt;
	}
	if(!$channel)
	{
		echo "[DECHANADMIN]: " $tr("This is not a channel window")
		halt;
	}

	foreach(%nick,$str.split(",",$0))
	{
		if(%nick)
		{
			mode $chan.name() -a %nick
		}
	}
}

# halfop

alias (halfop)
{
	if(!$0)
	{
		echo $tr("Usage:")
		echo "	  /halfop <nick_list>"
		halt;
	}
	if(!$channel)
	{
		echo "[HALFOP]: " $tr("This is not a channel window")
		halt;
	}

	foreach(%nick,$str.split(",",$0))
	{
		if(%nick)
		{
			mode $chan.name() +h %nick
		}
	}
}

# dehalfop

alias (dehalfop)
{
	if(!$0)
	{
		echo $tr("Usage:")
		echo "	  /dehalfop <nick_list>"
		halt;
	}
	if(!$channel)
	{
		echo "[DEHALFOP]: " $tr("This is not a channel window")
		halt;
	}

	foreach(%nick,$str.split(",",$0))
	{
		if(%nick)
		{
			mode $chan.name() -h %nick
		}
	}
}

# userop

alias (userop)
{
	if(!$0)
	{
		echo $tr("Usage:")
		echo "	  /userop <nick_list>"
		halt;
	}
	if(!$channel)
	{
		echo "[USEROP]: " $tr("This is not a channel window")
		halt;
	}

	foreach(%nick,$str.split(",",$0))
	{
		if(%nick)
		{
			mode $chan.name() +u %nick
		}
	}
}

# deuserop

alias (deuserop)
{
	if(!$0)
	{
		echo $tr("Usage:")
		echo "	  /deuserop <nick_list>"
		halt;
	}
	if(!$channel)
	{
		echo "[DEUSEROP]: " $tr("This is not a channel window")
		halt;
	}

	foreach(%nick,$str.split(",",$0))
	{
		if(%nick)
		{
			mode $chan.name() -u %nick
		}
	}
}

# ame and amsg

alias(ame)
{
	foreach(%i,$window.list(channel))if(%i!="") me -r=%i $0-
}

alias(amsg)
{
	foreach(%i,$window.list(channel))msg $target(%i) $0-
}

alias(amsgall)
{
	foreach(%i,$window.list(channel),$window.list(query))msg $target(%i) $0-
}


alias(ameall)
{
	foreach(%i,$window.list(channel),$window.list(query)){
	if(%i!="") me -r=%i $0-
	}
}

alias(awayall)
{
	foreach(%w,$window.list(console,all))away -r=%w $0-;
}

# notify: this is a bit more complex
# We want to add an user to the notify list with a single command
# The notify lists are handled thru the registered users database

alias(notify)
{
	# This one adds a registered users database entry
	# For the specified nick and uses the specified mask
	# If no mask is specified , a default one is constructed
	# Then the "notify" property is set for the user

	if(!$0)
	{
		echo $tr("Usage:")
		echo "	  /notify <nick> [mask]"
		halt
	}

	# Get the mask

	%mask = $1

	# no mask specified ? build it

	if(!%mask)%mask = $0!*@*

	# Now add the user (but do not replace old entries)

	reguser.add -f -q $0 %mask

	# Merge the notify property part with the old one (if any)

	%oldNicks = $reguser.property($0,notify)

	# It would be a good idea to check if the nikckname is already in the %oldNicks list
	# but this is left as exercise for the reader.

	%oldNicks << $0

	# And finally set the property (use -n so the notify lists will be restarted)

	reguser.setproperty -n $0 notify $0

	# done...the notify list should be restarted now
}

alias(ignore)
{
	# This one adds a registered users database entry
	# For the specified nick and uses the specified mask
	# If no mask is specified , a default one is constructed
	# Then the "ignore" property is set for the user to 1

	if(!$0)
	{
		echo $tr("Usage:")
		echo "	  /ignore [-r] <nick>"
		halt
	}

	# Now add the user (but do not replace old entries)

	reguser.add $0 $mask($0,15);

	# And finally set the property (use -n so the notify lists will be restarted)
	# Check if ignore or unignore the user

	if(!$sw(r))
	{
		reguser.setproperty $0 IgnoreEnabled 1
		reguser.setproperty $0 IgnoreAll 1
	} else reguser.setproperty $0 IgnoreEnabled 0
}

# Dcc...people commonly use "dcc send" instead of "dcc.send"

alias(dcc)
{
	switch($0)
	{
		case(send):
		{
			dcc.send $1-
			break
		}
		case(chat):
		{
			dcc.chat $1-
			break
		}
		case(voice):
		{
			dcc.voice $1-
			break
		}
		case(allow):
		{
			quote dccallow $1-
			break
		}
		default:
		{
			echo "[DCC]: " $tr("Unsupported type: $0")
		}
	}
}

#
# return a string with the name of the curerent usermode
#
alias(usermodename)
{
	if(!$0)
	{
		echo $tr("Usage:")
		echo "	  /usermodename <nick>"
		halt;
	}
	if(!$channel)
	{
		echo "[USERMODENAME]: " $tr("This is not a channel window")
		halt;
	}

	if($chan.isOwner($0,$channel)){
		return $tr("Channel Owner")
	} elseif($chan.isAdmin($0,$channel)){
		return $tr("Channel Administrator")
	} elseif($chan.isOp($0,$channel)){
		return $tr("Channel Operator")
	} elseif($chan.isHalfOp($0,$channel)){
		return $tr("Half Operator")
	} elseif($chan.isVoice($0,$channel)){
		return $tr("Voice")
	} elseif($chan.isUserOp($0,$channel)){
		return $tr("User Operator")
	} else {
		return $tr("Normal User")
	}
}

alias(dccallow)
{
	quote dccallow \+$0
}

#shortcut for commonly called algorithm
alias(md5)
{
	return $str.digest($0-,md5)
}

alias(sha1)
{
	return $str.digest($0-,sha1)
}

# DNS : people like it

alias(dns)
{
	if(!$context)
	{
		echo "[DNS]: " $tr("You must use DNS in an IRC context")
		halt
	}

	if(!$server)
	{
		echo "[DNS]: " $tr("You must be connected to a server")
		halt
	}

	if(!$0)
	{
		echo $tr("Usage:")
		echo echo "  /dns <nickname>"
		halt
	}

	if($isWellKnown($0))host -a $hostname($0)
	else {
		awhois($0)
		{
			if(!$2)
			{
				echo "[DNS]: " $tr("No such user: $0")
			} else {
				host -a $2
			}
		}
	}
}

alias(ss)
{
	# Silent
        quote ss $0-
}

alias(seenserv)
{
	# Silent
	msg -q SeenServ $0-
}

alias(hs)
{
	#Silent
	quote hs $0-
}

alias(hop)
{
	// Retrieve channel
	%chan=$0
	if(!%chan) %chan=$chan.name($active)

	// Retrieve channel key
	%key=$chan.key($channel(%chan))

	// Exit channel
	if($window.type($channel(%chan))=="channel")
	part --keep %chan $option(stringPartMessage)

	// Rejoin channel
	if(!%key) join %chan
	else join %chan %key
}
