Aliases

Aliases Aliases : user definable command sequences
An alias is a user defined command. It can be used to rename the builtin KVIrc commands or functions, to automate complex tasks or as structured programming mean. Aliases can be created or destroyed by using the scriptcenter (graphic interface) or from the commandline (or script) by using the alias command. Once created, an alias remains stored permanently in the KVIrc configuration files until it is explicitly deleted. A couple of examples will make the things clear. join is a really commonly used command. It might be a good idea to rename it to simply j .. just to type it faster. Nothing easier in KVIrc: just try this commandline:
alias(j){ join $0-; };
This will create the alias j. From this moment you can use /j as it was a normal command.
j #kvirc
You may have notices the strange $0- function in the alias body: it stands for all parameters passed to the alias. This means that when you call
j #kvirc testpassword
then both the parameters (#kvirc and testpassword) are passed to the join command. The $N functions are special functions that return the positional parameters passed to the current script context. In an alias the script context is the script body and it is the alias caller that generates the parameters. $N (where N is a digit) returns the (N-1)-th positional parameter passed by the caller. It returns the parameter numbered N-1 and not N since the parameters are indexed starting from zero ($0 is the first parameter!). $N-M returns the parameters from (N-1)-th to the (M-1)-th (a parameter range) and $N- returns all the parameters from (N-1)-th to the last one. In the example above $0- stands for all the parameters starting from the first one. To remove an alias use again the alias command with an empty body:
alias(j){}
This will remove the alias j defined above. A common task in channel management is the kick & ban action. You first ban a user from the channel and then eventually kick him (obviously assuming that he is actually on the channel). This involves using two commands: ban and then kick. It could be a nice idea to have a single kb command to perform this action. Well...easy:
alias(kb){ ban $0; kick $0-; };
This adds the kb alias: it can be called as a normal command:
kb spammer You're not welcome here!
This will first execute ban spammer and then kick spammer with; You're not welcome here. Our kb is a really simple example... it doesn't check for the validity of the parameters: the server will warn us if the parameters passed to kb were empty. The alias can be modified at any time by re-using the alias command. Let's make our kb a bit more intelligent and add a check for the parameters. TIP: It is a good idea to write the following examples in a text file and then use /parse <filename> to execute it.
alias(kb) { if("$0" == "") { echo "Usage: /kb <nickname> <kick reason>" return } ban $0 %reason = $1- if("%reason" == "")%reason = "You're not welcome here!" kick $0 %reason }
The example above will first check the validity of the <nickname> passed to kb: if no nickname was passed, it will warn the user and stop. The next step will be the ban <nickname> call. Another enhancement is the default reason: we first assign the remaining parameters ($1- means from $1 to the end) to a temporary variable, if the variable is empty, a default kick reason is assigned. Finally the kick <nickname> <reason> will be executed. Get used to looking at the single command documentation pages, they will give you the hints necessary to fully understand the above piece of code. Aliases can be used as a mean for structured programming. In large scripts you will surely have common tasks to perform (like having specially colored output or calculating a value from a set of other values)... Aliases are the way of writing the common tasks: they are equivalent to the procedures or functions in many high-level programming languages. The alias as a procedure (subroutine or sub-task) has been shown in the kb example above: it might be commonly called from more complex scripts or other aliases in case that a kick & ban action is needed. The aliases can be used also as functions. Assume that you need really often to calculate the sum of three numbers: a function-alias is the way.
alias(sum3){ return $($0 + $1 + $2); };
This will add the alias sum3 and make it available both as a command and a function. The return command sets the return value of a sequence of commands (an alias is a sequence of commands...remember?) and terminates the execution (by returning the control to the caller). So return $($0 + $1 + $2); will set the return value of the alias to the value computed by $($0 + $1 + $2) that actually is the sum of the first three parameters passed. You will then use it in the following way:
... %myfirstsum = $sum3(%somevalue,%someothervalue,4) %anothersum = $sum3(12,%somevalue,%anothervalue) ...
Oops! I've used some variables without actually explaining them, hehe... please forgive me and read on. This example is again really simple, but you might have more complex function-aliases. The function-aliases are also normal aliases... you can use it as a command:
/sum3 1 2 3
Is a perfectly valid call... it's just that it will have no visible results (just because a command call implies ignoring the return value. In fact there is no difference al all between function-aliases and normal-aliases: the caller makes the difference: by calling an alias as a command the return value just disappears in hyperspace, by calling an alias as a function, the return value is propagated (and in fact used). (There are some nice exceptions to this rule... but you don't need to care about it, for now). If return is not called inside an alias body, the return value will be just a null value. Aliases can accept switches just like any other command. The $sw is there exactly for that purpose. Check it out.

Index, Language Overview