Kings Of Chaos Stats Checker.

By Linuxuser on Mar 12, 2011

Simple script to pull data from a database using !stats

for those who do not play a test nick to use is !stats Independence

it opens a link to the database reads the html selects relevant information then posts them to the channel may not be useful to many but it a good example of how sockets and regex can be used to find and pull information.

on *:TEXT:!stats*:#:{
  set %url /mstats.php?n= $+ $2
  set %chan $chan
  set %nick $2
  set %reg (All|Act|Ra|Fo|St|per|Unt|Cov)
  timer 1 5 sockopen mstats www.relentless.ws 80
  /msg $chan Searching....
}
on *:SOCKOPEN:mstats:{
  sockwrite -n $sockname GET %url HTTP/1.1
  sockwrite -n $sockname HOST: www.relentless.ws $+ $crlf $+ $crlf
}
on *:SOCKREAD:mstats:{
  sockread -f %mstats
  if ($regex(%mstats,%reg)) {
    /msg %chan 12 %mstats
  }
}
}

Comments

Sign in to comment.
Jethro   -  Mar 14, 2011

The "abusive nature" of using global variables, to me, at least, is that they get set without getting unset later when the socket has finished its routine. If Linux user must use global variables, he should unset them using the sockclose event. This way you don't have them occupy the space of variables unnecessarily.

 Respond  
Jethro   -  Mar 13, 2011

Yes you have a valid point, SunnyD. You can put the info in both locations the way it appears, but as a matter of personal preference, and as abusive as it seems in the snippet, I've seen people store them with variables, which one should be encouraged to ditch and be adapted to the sockmark and $sock().mark as mentioned.

 Respond  
Sorasyn   -  Mar 13, 2011

I realize $nick and $chan respectively collapse when used outside the native event, but what I was getting at is that he abuses global variables for no reason.

  sockwrite -n $sockname GET %url HTTP/1.1

and

if ($regex(%mstats,%reg)) {

These are the only instances of the two variables and would function perfectly if the contents of these variables simply replace rather than circumvent the data that belongs there.

 Respond  
Jethro   -  Mar 13, 2011

SunnyD wrote:> Instead of setting a global variable and using it once in one line of code wouldn't it be more beneficial to just take the contents of the variable and place it where it is used, and take out the whole variable all together?because the $nick and $chan become $null when used throughout the socket routine. In other words, you cannot place them directly in the socket events; they simply won't work. You have to have a gateway to store them in order for the socket to reference them successfully. The sockmark command and $sock($sockname).mark have been created for this reason, so you can use them to store info.

Hash table, exceptionally, can be used for storing binary info when you fetch data to store in the binary variables. The -b switch for the hash table is there for such purpose:

hadd -mb hashtable item &data

for instance.

Other than that, I don't get why people keep avoiding them and using variable and hash tables for socket operation.

SunnyD wrote:> I am still trying to find out where exactly you use %nick because i can't find one instance of it other than stating it.> set %url /mstats.php?n= $+ $2
set %nick $2As you can see, the $2 should have been the %nick, but it was disregarded for some reason.

 Respond  
Sorasyn   -  Mar 13, 2011

Instead of setting a global variable and using it once in one line of code wouldn't it be more beneficial to just take the contents of the variable and place it where it is used, and take out the whole variable all together? This in particular applies to %url %reg and %nick. I am still trying to find out where exactly you use %nick because i can't find one instance of it other than stating it.

The way I look at it is the fewer variables the better. I usually go the extra mile to use hash tables just to avoid them. Although for smaller stuff it is unavoidable. :[ /var > /set

 Respond  
Jethro   -  Mar 12, 2011

Nice effort, Linuxuser, with the socket. One helpful advice for you is to use the /sockmark command instead of setting a bunch of global variables:

on *:text:$($iif(!stats * iswm $1,$1)):#:{
  if !%f { set -z %f 5
    sockopen mstats www.relentless.ws 80
    sockmark mstats $+(/mstats.php?n=,$2,-,msg #,-,$2,-,/All|Act|Ra|Fo|St|per|Unt|Cov/)
  }
}
on *:SOCKOPEN:mstats:{
  tokenize 45 $sock(mstats).mark
  sockwrite -nt $sockname GET $1 HTTP/1.1                                  
  sockwrite -nt $sockname Host: $+($sock(mstats).addr,$str($crlf,2))
  sockwrite -nt $sockname Connection: close
}
on *:SOCKREAD:mstats:{
  tokenize 45 $sock(mstats).mark
  var %mstats | sockread %mstats
  if ($regex(%mstats,$4)) {
    $2 12 %mstats
  }
}

it does look a lot cleaner, doesn't it? No timer is necessary...
Although the script gets a bit bigger, it's better this way.

 Respond  
Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.