Randomness ....

Useful items for all visitors - virus warnings, firewalls, IRC scripts etc

Moderators: Ehlanna, Twerlinger

Randomness ....

Postby Ehlanna on Sat Feb 09, 2008 12:45 am

How to generate a random quit message when using the mIRC client.

I will try and make no assumptions here, so much of this may be well known to you. Bear with me!

Ok, how do we quit IRC? You can 'pull the plug' - either physically disconnecting the computer from whatever network connect it is using, you can click on the little 'lightning bolt' icon at the left of the tool bar in mIRC to close the connection to the IRC server. Both of those are rather inelegant, so most people type a command - the default command is /quit.

A peek at the help file for /quit shows us:

/QUIT [message]


Disconnects you from IRC and will give the optional message as the reason for your departure. (this message only appears to people who are on the same channels as you).

This gives us hope! What that means is that if you just type /quit people will just see YourNick has quit IRC (Client Quit) but if you were to type: /quit BYE!!! what they will see is: YourNick has quit IRC(Quit: BYE!!!) thus showing your message.

Assuming you don't have a vast array of quotes at the tip of your fingers nor the inclination to type one out each time you depart then you need some place to keep a list of them. Better would be a way for mIRC to be able to read that file and make use of the message as part of the quit.

Another quick peek at the help file finds us:

$read(filename, [ntswrp], [matchtext], [N])


Returns a single line of text from a file.


Which looks rather splendid just what we want - a way to read a file! But, how do we make it random? We could read each line of the file and then make our own 'random' choice. However, reading a little further we see:
If the first line in the file is a number, it must represent the total number of lines in the file. If you specify N = 0, mIRC returns the value of the first line if it's a number.



So, what this means is if we just tell mIRC the name of the file, having made sure the first line of the file is equal to the number of lines in the file then mIRC will do ALL the work for us!
Let's make a test file, called Test.txt and put it in the same folder as your mIRC program!
5
This is line 1
This is line 2
This is line 3
This is line 4
This is line 5

Yes, alright it IS boring, but it is enough to prove the point!

Let's have a stab at the code, but as we are making a quit command let's, for the purposes of testing, just echo the message by itself without doing the actual quit. Another look at the help file tells us that to output text to the current channel (or Private Message window) we need to use the /say command. As we are building this command up bit by bit, let's make sure things will do what we expect.

We will be writing something called either a script or an alias. If you look at the toolbar strip in mIRC you will see that the fifth one from the left looks like a little scroll with a green 'blob' at the bottom right (this is current latest version of mIRC, earlier versiosn the icon will look a white /a on a green background). Click on the icon and you will see a Scripts Editor window open up.
To make sure we enter the code in the correct place ensure that you have selected the tab labelled Aliases.

If it's anything like the default it will start with something like:
Code: Select all
/op /mode # +ooo $$1 $2 $3
/dop /mode # -ooo $$1 $2 $3
/j /join #$$1 $2-
/p /part #
/n /names #$$1
/w /whois $$1
/k /kick # $$1
/q /query $$1


It acts mainly like a text editor, so 'open' a new line and enter:

Code: Select all
/testing /say this is a test!

Click on File, Save and we can then start testing! You will need to be connected to do this. To save bothering people it is useful to create your own temporary channel, so once you are online and connected type: /join #MyTestLab
Now we are in your little test channel, fire up your new command by typing: /testing - with lick you will see this is a test! being shown in the channel. Now, onto the next step - changing the text you show from being the literal text to text from the file you created.
Back in the Scripts Editor change your command to:
Code: Select all
/testing /say $read(Test.txt)

What this means is run the function called $read(), telling it where to read from and return the line read. As we have a number as the first line and did not give the $read() function any other information or parameters, according to the help file this will return a random line.
Save the change and go and test it. Run it a few times - in your testing channel, of course!!!!

Assuming no errors happen, you should get a random line each time. As we only have five lines there is a quite large chance (20%) that we will get the same line printed twice in succession, so do run it a few times to show it can, and will, produce all lines (another reason for the small file!).

Now, what do you think would happen if you were to edit that command and change the /say to a /quit?

My own alias is:
Code: Select all
/rquit /quit $read(Quotes.txt)


Leading on from this, once you have happily populated your own file full of lines to be shown comes the obvious ... "I've just added a new line which I think is dead funny and I want to be sure it gets used the next time I quit ..."
That is quite easy to do.

But, what happens if you start thinking ... "I haven't seen the 7th entry for a while, let's have that instead of a random one"?
That gets a little more complex.

Let's combine the ideas for an all encompassing quit command!

Code: Select all
/rxquit {
  var %txt
  if ($0 == 0) { set %txt $read(Quotes.txt) | /say Read %txt }
  elseif ($0 == 1) {
    var %numline
    %numline = $read(Quotes.txt, 0)
    var %num = 0
    if ($1 == last) { set %num %numline }
    else { set %num $int($calc($1 * 1)) }
    if (%num < 1 || %num > %numline) { set %txt Number out of range }
    else { set %txt $read(Quotes.txt, %num) }
  }
  else { set %txt Invalid parameter - specify none or a single number }
  quit %txt
}

Ok, now you have finished blinking let's go through that line by line ... And I shall leave you to look up the help file as and when necessary from this point!

Code: Select all
1:  /rxquit {
2:    var %txt
3:    if ($0 == 0) { set %txt $read(Quotes.txt) }
4:    elseif ($0 == 1) {
5:      var %numline
6:      %numline = $read(Quotes.txt, 0)
7:      var %num = 0
8:      if ($1 == last) { set %num %numline }
9:      else { set %num $int($calc($1)) }
10:     if (%num < 1 || %num > %numline) { set %txt Number out of range }
11:     else { set %txt $read(Quotes.txt, %num) }
12:   }
13:   else { set %txt Invalid parameter - specify none or a single number }
14:   quit %txt
15: }


Lines 1 and 15 are the start and end lines of a multi-line alias called /rxquit, the { and } being used to 'group' the lines of code together - keep track of those curly brackets, missing one or mis-typing one are one of the most commom errors you can make.

Line 2 declares a variable called %txt (all user variables start with %) which is (due to the var command) 'local' to just this function - which means that once the function ends the variable will disappear and can not be accessed by any other alias. If you don't have to or want to share information across commands using this method is a good idea.

Line 3 is a test. It is looking at a variable called $0 and asking if it is equal to 0. If it is is runs the command following within the { and } on the same line. A quick check of the help file will show us what $0 is and what the if does and how. If $0 is 0 (you just typed in the alias name) then we want a random message - which we accomplish by the same method we used above. The difference is instead of immediately showing the message, or issuing a /quit with the message, we set the %txt variable to the returned value.

Lines 4 to 12. This is where the indenting of code pays dividends, and yes I know, I have not mentioned this before! Lining code up like it is helps reading it. Now, what these lines are all about is the elseif - which means we must have had an if before this. And as luck would have it we did - see line 3! The test is for is $0 equal to 1, which means did we type in just one word/thing after the command. If so all the code between the matching { and } (on lines 4 and 12) will be run.
Line 5 declares another 'local' variable, this one called %num.
Line 6 assigns a value to that variable using the $read() function that we have used before. This time, however, in addition to the name of the file to read we also give it another parameter - the 0. If you check the help file (or look above) you will see that this means return the first line of the file if that first line is a number (the number of lines in the file). Thus, we put the number of lines in the variable %numline.
Line 7 declares a variable we are calling %num and, at the same time, giving it an initial value of 0.
Lines 8 and 9 look to see of the thing we entered is the word "last" (if you check the help file for if you will see that using == for the test means that we can enter "last", "Last", "LaST" or any combination of upper and lower case letters). If it is that word it means we want to use the last entry to to be used, so we set the variable %num to the value of the variable %numline - which, if you recall, we have just set to the number of lines on the file - handy, eh? If it isn't "last" we run teh code that follows the else statement on line 9 which is one of those nice little cheats you can do. It uses the mIRC function $calc() to evaluate what was entered (which, you will remember, is just one thing - it could be 3, or 34, or 3+6, or 34.7 or "fred"). What the $calc() function will do is to ensure that a number of some form is returned - if you typed in "fred" or any other characters that are non-numeric $calc() will return 0. We then use that result as a parameter to the $int() function which further ensures we only get an integer number - that is a whole number: 1, 4, 69 and not a 'real' or floating point number like 4.5.
Lines 10 and 11 takes this number, and we can be sure it is a proper, whole number at this point (unless you have something like 12.34 as the fisrt line of your file!) and tests it twice - is it less than (the <) 1 or (the || means or) is it greater than (the >) the number of lines in the file. if either of those is true it means the number you entered will not point to a valid line on the file so we set the %txt variable to a suitable message. If the number is within the valid range we perform the else part on line 11 which sets the %txt variable to the specified line from the file using the $read() function again.
Line 12 ends the elseif part started at line 4.
Line 13 is an else linked to elseif on line 4 which itself is linked to the if on line 3. What those ifs and elses are saying is: Have I entered no parameters, elseif I have entered one word/thing, else I have entered more than one, which is not right, as we are only looking for a singel number so set %txt to a suitable message.
Linr 14: And finally ... we make use of the %txt variable as part of the quit command - not elegant we we made an error and entered and invalid or out of range value, but that is for you to deal with ...!
User avatar
Ehlanna
High Council
High Council
 
Posts: 2494
Joined: Sat Apr 09, 2005 9:23 pm

Postby Ehlanna on Sat Feb 09, 2008 2:00 pm

A slightly updates version. I was thinking - as you do! - what about wishing to use a specific quit message, this you can now use last as a keyword for the last line in the file, a specific number or a keyword that appears in a record, or just random ...

Code: Select all
/rxquit {
  var %valid = /say
  var %txt
  var %num = 0
  if ($0 == 0) { %txt = $read(Quotes.txt) | %valid = /quit }
  elseif ($0 == 1) {
    var %numline
    %numline = $read(Quotes.txt, 0)
    if ( $1 == last ) { %num = %numline }
    elseif ( $1 isnum ) { %num = $int($1) }
    else {
      %txt = $read(Quotes.txt,w, * $+ $1 $+ *)
      %num = $readn
    }
    if (%num < 1 || %num > %numline) { %txt = Number out of range }
    else {
      %txt = $read(Quotes.txt, %num)
      %valid = /quit
    }
  }
  else { %txt = Invalid parameter - specify none or a single number }
  %valid %txt
}
User avatar
Ehlanna
High Council
High Council
 
Posts: 2494
Joined: Sat Apr 09, 2005 9:23 pm

Postby Greywind on Sat Feb 09, 2008 5:40 pm

Im having, like, 24 or such quits, so, this will come incredibly handy!
Thank you, Ehl ^^
Image
User avatar
Greywind
Moderator
Moderator
 
Posts: 403
Joined: Tue Jan 09, 2007 12:05 am
Location: Beyond the Edge of the City of Forever

Postby Ehlanna on Sun Feb 10, 2008 11:35 pm

A couple of points that may need explanation, and may not be clear as extensions from the previous version.

Code: Select all
  var %valid = /say

Defines a variable that is logically used for two things - one is an indicator if if what has been requested is valid (hence the name!) and the other what to do (see later)

Code: Select all
{ %txt = $read(Quotes.txt) | %valid = /quit }

The vertical bar (or pipe character) is used to specify two commands on the same line, this could also have been written as:
Code: Select all
{
  %txt = $read(Quotes.txt)
  %valid = /quit
}



Code: Select all
%valid %txt

What this does is ... care to guess? Have a little think before looking down.





%valid is set, initially to be /say - which is the command to output text to the current window - channel or query/PM window.
%txt contains either the selected message to display on quit, or an error message. If we believe that we have found a proper and valid message to show we not only set %txt to that, but we also set the %valid variable to /quit.
This, when mIRC evaluates the line it changes %valid to either /quit or /say so will either run the /quit process or the /say one - each of them with the contents of %txt - so we either quit with a (hopefully) witty message, or we stay put in channel and see a little error message instead!
User avatar
Ehlanna
High Council
High Council
 
Posts: 2494
Joined: Sat Apr 09, 2005 9:23 pm


Return to Public Service

Who is online

Users browsing this forum: No registered users and 3 guests


cron