Here's another bit of mIRC programming for you. This is a routine that will take a string that you type in, and output each different word in a random colour. It demonstrates a few of the string processing capabilities of mIRC and gives you a taste of what can be done with them.
The spellchecker I described earlier was designed to run in the background, all the time, on everything you enter. We don't want that to happen with this script, we only want this one to run when we specify it. There are two ways of doing this:
Each has its pros and cons, but I believe the easiest way is as an alias. I'll leave it as an exercise for the reader to convert it to work on a pop-up menu!
Firstly, we need to choose a short, easily remembered alias definition which
isn't already in use by mIRC. I'm going to call the alias /cw
so
open up the mIRC Editor, select the aliases window and enter
this at the bottom.
/cw /say $colwords($1-)
colwords {
; declare some variables
var %newstring = ""
var %entered = $1-
var %counter = 1
var %word = " "
while (%word != $null) {
; extract a word
%word = $gettok(%entered,%counter,32)
; make up a random number
var %coltext = $rand(1,15)
; build up the new phrase, with colour codes in
%newstring = %newstring $+ $chr(3) $+ %coltext $+ %word $+ $chr(1)
inc %counter
}
; put the spaces back in to our sentence
%newstring = $replace(%newstring,$chr(1),$chr(32))
return %newstring
}
Ok, let's take this set by step.
/cw /say $colwords($1-)
This defines an alias, called cw, which outputs the results of an
identifier called colwords. As yet we don't know what colwords is, or what it
does, but we do know it works with the text you type in ($1-
is an identifier that means "the first word and
everything following it").
Here are the new ideas in the $colwords script:
while (%word != $null) {
%word = $gettok(%entered,%counter,32)
These two lines sort of go together, so I'll explain them both in one
go. The !=
in the first line means "not equal to", so this line
checks the contents of %word to see if it is not equal to %null.
The $gettok
identifier is
part of mIRC's built-in language; here we're using it to extract token number
%counter from the string %entered. In our case we want our tokens to be
words, and words are separated by spaces. In general, though, tokens can be
anything, and they can be separated by anything - spaces, commas, semicolons,
slashes, etc. Some identifiers, like $gettok
, need
to be given the code of the character rather than the character itself, so we
either have to know that the ASCII code of a space is 32. If you want to know
what the ASCII codes are, your PC or printer manual should contain a list of
them.
For example, assume that %entered contains "This is a test"
$gettok(%entered,0,32)
returns 4 (there are 4 words in the string)
$gettok(%entered,1,32)
returns 'This'
$gettok(%entered,2,32)
returns 'is'
$gettok(%entered,3,32)
returns 'a'
$gettok(%entered,4,32)
returns 'test'
$gettok(%entered,5,32)
returns $null (see below)
If we want our separator to be the letter 's' instead, this is what we get (note that
the ASCII code of 's' is 115):
$gettok(%entered,1,115)
returns 'Thi'
$gettok(%entered,2,115)
returns ' i'
$gettok(%entered,3,115)
returns ' a te'
$gettok(%entered,4,115)
returns 't'
Think about it a little, it does make sense.
If we try to get a word past the end of the sentence, the $gettok identifier
returns $null
, which means
nothing. Variables can contain anything at all, even nothing. Note that
'nothing' is not the same as zero, or "", and if a variable contains nothing
at all it returns $null. (Aside: if you think of a variable as a shoebox,
which can contain a piece of paper with the variable's contents written on
it, then $null is an empty shoebox. A variable containing zero can be thought
of as a shoebox containing a piece of paper with zero written on it.) If
we've reached the end then we don't want to process this non-existent word,
we want to finish up. The while
command makes sure that we only
process the commands between the curly brackets while we have a word to
process.
%coltext = $rand(1,15)
If we've got this far then %word does in fact contain a word, so now we use
another built-in identifier to return a random number between 1 and 15.
Colour codes actually start at 0, but we don't want to use zero as that is
white, and we don't want white text on a white background! We allocate the
random number to a variable called %coltext
.
%newstring = %newstring $+ $chr(3) $+ %coltext $+ %word $+ $chr(1)
This actually starts building up the string we want to output. We want to
start with the %newstring we already have, since we want to add each word to
the end of it. The $+
means
'concatenate', which is a very fancy word meaning 'stick this on the end of
that'. As an example, "hel" $+ "lo" is "hello". The $chr(3)
is actually the Ctrl-K code which we use to set
colours (how did I know this? I read some other people's scripts and
experimented!). Then we append our random colour code, then the actual word
and finally a $chr(1)
. Why the $chr(1)? Well, if
you have another look at the $gettok examples I gave earlier, you can see
that it doesn't give us the separator character. We've got to put them back
in, otherwise allthewordswillruntogether. However, I've discovered that mIRC
doesn't like adding a space to the end of a string - they just don't get put
there. So we think laterally and put a different character there (it could be
anything as long as there's a reasonable chance it won't occur anywhere else
in the string) and we'll replace them with spaces later. By the way, it took
me an hour to figure this out!
Writing this line of code without a lot of the symbols in it, we have
newstring = newstring + ctrl-K + random colour code + word + temporary
character representing a space, which I hope makes a bit more sense to you.
inc %counter
This line of code increments the %counter variable by one. If we don't do
this, we'll end up processing the same word over and over again. (If we
said inc %counter 2
instead, then only every other word will appear on the screen at the end!)
%newstring = $replace(%newstring,$chr(1),$chr(32))
We now replace our
temporary space character (the $chr(1)
) with an actual space
character, which is $chr(32)
.
return %newstring
}
Finally, we return %newstring
to the routine which called
%colwords
(in this case, our
alias).
To use the alias, type something like
/cw These words are all in different colours!
New Techniques introduced in this section - see mIRC help file for
more information This script changes the colours of the nicknames in a channel based on
what mode the user has. Operators are shown in red, people with voices on
moderated channels are shown in green and everyone else is shown in
black. The only new idea in this script is the The script as it stands is an alias, which means that every time you type
The script presented above to colour the nicknames is not the best
approach. For a start, it is large and cumbersome. Also, it only works when
the channel window is the active window, and it only works for one channel
(i.e. if you have two channels and a query window open, the script will only
work if the current window is one of the channel windows, and it won't update
the other channel). Here is a better (but more complicated) version, based on
a script found in alt.irc.mirc posted by Adam. The script is a bit shorter for two reasons: One, we're using a neat little
shortcut of starting at the bottom of the nickname list and working our way
up to the top, instead of going from top to bottom. Two, some of the
Also note that the script uses two loops, one inside the other. The inner
loop does all the nicknames on a channel, the outer loop does all the
channels we're currently on. New Techniques introduced in this section - see mIRC help file for
more information
$gettok
identifier
$null
identifier
$rand
identifier
mIRC Colourful Nicknames Script
colnames {
; declare some variables
; $opnick(#,0) tells us the number of ops on the channel
; $vnick(#,0) tells us the number of voiced people on the channel
; %numcol will be the number of ops and voiced people
; %numnix will be the total number of people on the channel
; %curline will be the line number we're looking at
var %numops = $opnick(#,0)
var %numv = $vnick(#,0)
var %numcol = %numops + %numv
var %numnix = $nick(#,0)
var %curline = 0
; first check to see we actually have some ops
if %numops > 0 {
while (%curline <= %numops) {
; change the current line to 4 (red) - you can use your own colour if you wish
cline 4 # %curline
; next line
inc %curline 1
}
}
; check to see we have voiced people
if %numv > 0 {
while (%curline <= %numcol) {
; change the current line to 3 (green)
cline 3 # %curline
inc %curline 1
}
}
while (%curline <= %numnix) {
; change the current line to 1 (black)
cline 1 # %curline
inc %curline 1
}
}
/cline
command which
changes the colour of a line in a custom window. Check the on-line help for
more information. Everything else in this script has been covered in other
areas of the tutorial./colnames
the nickname list is scanned and coloured. What we want,
however, is mIRC to do this for us every time the nickname list changes. The
list changes when people are opped, deopped, given voice, had voice taken
away, join the channel, or leave it, either by PARTing it, or being kicked,
banned or QUITting IRC. Each of these events can be picked up and acted upon
by mIRC. Add the following to the Remote Events section: (Notice that the on
quit event doesn't have a # - this is because it is triggered when someone
quits IRC, and it not specific to a channel).
on 1:JOIN:#:colnames
on 1:PART:#:colnames
on 1:QUIT:colnames
on 1:OP:#:colnames
on 1:DEOP:#:colnames
on 1:VOICE:#:colnames
on 1:DEVOICE:#:colnames
on 1:BAN:#:colnames
on 1:KICK:#:colnames
An improved Colourful Nicknames Script
colnames {
; find out how many channels we're on
%channo = $chan(0)
while (%channo > 0) {
%currentchannel = $chan(%channo)
; find out how many nicknames are on the current channel
%i = $nick(%currentchannel,0)
while (%i > 0) {
; if the current nickname on the current channel is an op, colour it red
if ($nick(%currentchannel,%i) isop %currentchannel) { cline 4 %currentchannel %i }
; if the current nickname on the current channel is voiced, colour it green
elseif ($nick(%currentchannel,%i) isvo %currentchannel) { cline 3 %currentchannel %i }
; otherwise colour it grey
else { cline 14 %currentchannel %i }
; next nickname
dec %i
}
dec %channo
}
halt
}
if
statements are on one line, whereas they used to be on two
lines or more.
$chan(0)
tells us the number of channels we're on
$chan(1)
tells us the name of the first channel
$chan(2)
tells us the name of the second channel
$nick(%channel,0)
tells us the number of nicknames on the channel stored in
the %channel variable
$nick(%channel,1)
tells us the first nickname on %channel
if Danny isop #mIRC
returns true if Danny is an op on the
specified channel
cline
changes the colour of a line in a window