; This is a commented version of my stats script for those who are learning and would like to understand the methods used in this script.
; First off, we have our trigger. Note the $ in the "on $*" part. This switch tells mirc that the trigger is a regular expression, so that we can do some error checking before the script even triggers, and make it a bit easier on us. I won't go too heavily into the regex description, as it is a whole other language in itself, but basically it tells mirc "starting at the beginning, look for @stats or !stats, a space, and at the end, look for 12 characters (any characters). The regex switches /Si tell mirc to ignore control codes (S) and to make the regex match case-insensitice (i).
on $*:TEXT:/^[!@]stats .{1,12}$/Si:*:{
; As this script was meant for distrobution, I tried to make it a little use friendly by putting a big glaring instruction on how to change the highlight color. Later on in my $_col alias I go even further to verify that the color is not messed up.
;################################
;# CHANGE THE NUMBER IN THE #
;# VARIABLE BELOW TO EDIT THE #
;# COLOR USED IN MESSAGES. #
;################################
set %_lookupmsgcol 07
;################################
; I first check if the syntax of the command is valid. I didn't realize it until typing this, but this check is redundant and could be removed. Well, I'll save that for next version, it's not hurting anything.
if ($2) && ($len($2) < 13) {
; First, for reasons of identification (passing variables) i increment a global variable (to prevent the sockets being "overwritten" as it were)
inc %_lookups 1
; Next, I set a variable to give me the info I will need later on. The variable ends up looking like: "user+name msg #channel"
; I'll break it down for you.
; set $+(%,lookup.,%_lookups) --> This portion sets a -unique- variable to be used later on.
; $replace($2-,$chr(32),+) --> This portion replaces spaces in the second token with +, so that we can insert it into the socket URL without problems.
; $iif($chan,$iif($left($1,1) == @,msg $chan,notice $nick),msg $nick) --> This is a little more confusing.. Here's a flow chart type thing to show what goes on.
; Is there a channel?
; `Yes
; | `Is the starting character @?
; | `Yes
; | | `--do "msg $chan"
; | `No
; | `--do "notice $nick"
; `No
; `--do "msg $nick"
set $+(%,lookup.,%_lookups) $replace($2-,$chr(32),+) $iif($chan,$iif($left($1,1) == @,msg $chan,notice $nick),msg $nick)
; Now that we have all our variables set, we go ahead and open our [unique] socket to our website, port 80.
sockopen $+(lookup.,%_lookups) www.rscript.org 80
}
; This else is also redundant, and will never trigger.
else { notice $nick Syntax: !stats <username>. The username must be 12 characters or less. }
}
; This event triggers after we open our socket.
on *:SOCKOPEN:lookup.*:{
; We need to send the server some headers so that we can request the page. We use the -nt switches in our /sockwrites so that we don't have to append and $crlf and &things are not evaluated as binvars.
sockwrite -nt $sockname GET /statpage.php?user= $+ $gettok($($+(%,lookup.,%_lookups),2),1,32) HTTP/1.0
sockwrite -nt $sockname User-Agent: RuneScript Public (Crawler Bot; rscript.org;)
sockwrite -nt $sockname Host: rscript.org $+ $crlf $+ $crlf
; Now we wait for some "stuff"!
}
; Here comes the "stuff"!
on *:SOCKREAD:lookup.*:{
; This line sets our ID by removing the first 7 characters of the socket name (which will be lookup.###). We need this ID so that we can recall our variable.
var %id = $right($sockname,-7)
; Recall our variable, using $gettok to get the first space delimited token (the username).
var %username = $gettok($($+(%,lookup.,%id),2),1,32)
; Recall our variable again to get our target.
var %target = $gettok($($+(%,lookup.,%id),2),2-,32)
; Make sure there is no socket error, and let someone know if there is.
if ($sockerr) { %target Socket error: $sockerr (BUILD ID: %id $+ ) }
else {
; Put whatever the socket is telling us into %read
var %read | sockread %read
; Remove the beginning of the lines starting with Stat: so that we can put it into our message. We also tokenize it by space so that we can reference it easier.
tokenize 32 $remove(%read,Stat:)
; We make sure that the format is correct by checking the number of tokens returned. We know that only a correct stat return will have 4 tokens, so $0 == 4. After we check to make sure our tokens are correct, we append the skill ($1) and the [highlighted] level ($3) to our unique message variable. Note that we need to make this a real variable (a la /set) rather than a temporary variable (a la /var) because the sockread is a new trigger each line. /vars would be reset.
if ($0 == 4) { set $+(%,lookupmsg.,%id) $($+(%,lookupmsg.,%id),2) $chr(124) $+($1,:) $_col($3) }
; We know that if we already have some skills, and our $0 token is not 4, that we are at the end of the page. So, we send out a message to the appropriate place, and close the socket off.
elseif ($($+(%,lookupmsg.,%id),2)) { %target Stats for $replace($_col(%username),+,$chr(32)) $+ : $right($($+(%,lookupmsg.,%id),2),-2) | sockclose $sockname }
}
}
; This alias is pretty complicated. I'll make another flowchart..
alias -l _col { return $+($chr(3),$iif($len(%_lookupmsgcol) > 1 && $len(%_lookupmsgcol) < 3 && %_lookupmsgcol isnum,$iif($len(%_lookupmsgcol) == 2,%_lookupmsgcol,$+(0,%_lookupmsgcol)),07),$1-,$chr(3)) }
; Is the length of the color var greater than one, less than three, and a number?
; `Yes
; | `Is the length 2?
; | `Yes
; | | `--Great! Return the tokens wrapped in our color controls as such: []<colorvar>tokens[]
; | `No
; | `--That means it's one. In that case, return our tokens as such: []0<colorvar>tokens[]
; `No
; `--Something got weird. Default to using 07 as our color.