KVS has many internal functions that can be used as command parameters.
The $window.caption function
is evaluated before the command executes,
and it is changed into the current window caption text.
All the function names start with a literal '$' character.
The functions can be used also as switch parameters.
The -w switch allows to redirect the echo text to a specified window --- in this
case the one that you are typing in.
echo -w = $window This text will be surely \
printed in the current window
(Surprise: in this case the -w switch is useless ,
since echo prints text to the current window by default...
but it will work correctly. :)
Normal function names can be made of "anycase" letters, digits and underscores,
with the restriction that the first character is not a digit.
Some kind of functions can contain a dot '.' character inside the name
and these are assumed to be module references (see the modules documentation).
By now we have seen only simple functions, but there's more...
The function $str.len accepts a single parameter and returns the
length in characters of the parameter string. The returned value is always
a string: in this case it can be also interpreted as a number.
The functions can accept parameters; the general syntax for a function call is:
where <parameter_list> is a list of comma separated parameters,
When passing an empty list you can avoid the parenthesis.
(And you have found the "simple" functions shown above).
So the followind two calls are equal:
If you want to pass an "empty" string as the first parameter you have to use
the following syntax:
Obviously a function is valid as a function parameter.
If you want to place a literal '(' or ')' in the function parameters
you must escape it.
A special case for when you want to use 'matching' parentheses:
an opened '(' corresponds to a closed ')'.
In this case you can omit the 'escape' character.
This is useful for algebraic and boolean expressions , like the ones
accepted by the special function $() (see next paragraphs).
An alias is an user defined command. It can be used to rename the builtin kvirc commands or functions,
to automatize 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:
This will create the alias "j". From this moment you can use /j as it was a normal command.
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
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:
This will remove the alias "j" defined above.
A common task in channel management is the kick & ban action.
You first ban an 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.
This adds the "kb" alias: it can be called as a normal command:
This will first execute "ban spammer" and then "kick spammer 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.
kb spammer You're not welcome here!
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.
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 enchancement 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.
if("$0" == "")
echo "Usage: /kb <nickname> <kick reason>"
%reason = $1-
if("%reason" == "")%reason = "You're not welcome here!"
kick $0 %reason
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 complexier 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.
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:
Ops.. 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 complexier function-aliases.
The function-aliases are also normal aliases.... you can use it as a command:
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.
%myfirstsum = $sum3(%somevalue,%someothervalue,4)
%anothersum = $sum3(12,%somevalue,%anothervalue)
Aliases can accept switches just like any other command. The $sw is there
exactly for that purpose. Check it out.