All pastes #815910 Raw Edit

CS1.6/CSS query

public text v1 · immutable
·published 2007-12-14 14:13 UTC
rendered paste body
;#########################################################
;################# Counter-Strike Query ##################
;#########################################################

alias csquery {
  ; by bobby - bobby921@gmail.com - #script on irc.gamesurge.net
  ; edited by Snp #snp on irc.gamesurge.net
  ; function: queries a half-life server for its information using the new protocol (as of 7/25/05)
  ; usage: /queryhlserver <ip>[:port]  - (port is automatically 27015 unless otherwise specified)
  ; example: /queryhlserver 255.255.255.255
  ;          /queryhlserver 255.255.255.255:27016
  if (*.*.*.* !iswm $1) return $null
  else {
    if (($gettok($1,1,46) !isnum) || ($gettok($1,2,46) !isnum) || ($gettok($1,3,46) !isnum) || ($gettok($gettok($1,4,46),1,58) !isnum)) return $null
    else {
      if ($chr(58) isin $1) {
        var %queryhl.ip = $replace($1,$chr(58),$chr(32))
        if (($gettok(%queryhl.ip,2,32) !isnum) || ($gettok(%queryhl.ip,2,32) > 65535)) return $null
      }
      else {
        if ($2) {
          if ($2 <= 65535) var %queryhl.ip = $1 $2
          else return $null
        }
        else var %queryhl.ip = $1 27015
      }
      set %queryhl.beginticks $ticks
      .timerqueryhltimeout -o 1 2 queryhltimeout
      if ($sock(queryhlserver)) sockclose queryhlserver
      sockudp -k queryhlserver %queryhl.ip $str($chr(255),4) $+ TSource Engine Query
      sockmark queryhlserver %queryhl.ip
    }
  }
}

alias queryhltimeout {
  unset %queryhl.beginticks
  echo -a HL server timed out. Your connection or the server may be experiencing problems.
  sockclose queryhlserver
}

on *:udpread:queryhlserver:{
  var %queryhl.ping = $calc($ticks - %queryhl.beginticks)
  sockread &query
  ;; uncomment the line below if you want to see the raw return
  ;echo -a $bvar(&query,1,$bvar(&query,0))
  breplace &query 0 254 32 160
  var %queryhl.return = $bvar(&query,1,$bvar(&query,0)).text, %x = 1
  ;; uncomment the line below if you want to see the somewhat parsed return
  ;echo -a parsed return: %queryhl.return

  if (*Source* iswm $gettok(%queryhl.return,4,254)) {
    set %source 1
    var %queryhl.hostname = $right($gettok(%queryhl.return,1,254),-6)
    var %queryhl.map = $gettok(%queryhl.return,2,254)
    var %queryhl.gamedir = $gettok(%queryhl.return,4,254)
    var %queryhl.mod = $ifmatch

  }
  else {
    ;; grab hostname, map, gamedir, and mod
    var %queryhl.hostname = $gettok(%queryhl.return,2,254)
    var %queryhl.map = $gettok(%queryhl.return,3,254)
    var %queryhl.gamedir = $gettok(%queryhl.return,4,254)
    var %queryhl.mod = $gettok(%queryhl.return,5,254)
  }

  ;; for position calc (prevents messed up position if mod name is in hostname)
  var %position = $regex(position,%queryhl.hostname,/ $+ %queryhl.mode $+ */g)
  if (!%position) var %position = 1
  var %positionnumber = $pos(%queryhl.return,$gettok(%queryhl.return,5,254),%position)

  ;; parsing for players
  var %gamechars = $len($gettok(%queryhl.return,5,254))
  var %charcalc = $calc(%positionnumber + %gamechars)
  var %players = $mid(%queryhl.return,%charcalc,3)

  ;; grab current connected players and maxplayers
  var %queryhl.players = $iif($asc($mid(%players,2,1)) == 254,0,$ifmatch)
  var %queryhl.maxplayers = $asc($right(%players,1))
  if (%queryhl.maxplayers == 160) var %queryhl.maxplayers = 32

  ;; parse next part and grab protocol number and server type (if players is 0 position it 1 back)
  var %2ndstring = $gettok(%queryhl.return,6,254)
  var %queryhl.protocolnumber = $asc($mid(%2ndstring,$iif(%queryhl.players == 0,2,3),1))
  if (!%queryhl.protocolnumber) var %queryhl.protocolnumber = 0
  var %queryhl.servertype = $iif($mid(%2ndstring,$iif(%queryhl.players == 0,3,4),1) == d,dedicated,listen)

  ;; calculate the current position in the return + 1 (where the password yes/no is)
  var %pwPos = $calc(%charcalc + $pos(%2ndstring,5,1) + 1)

  ;; grab the server operating system (if players is 0 position it 1 back), password 0|1, secure 0|1, and number of bots
  var %queryhl.serveros = $iif($mid(%2ndstring,$iif(%queryhl.players == 0,4,5),1) == l,Linux,Windows)
  if (%source == 1) {
    var %sourcestring = $gettok(%queryhl.return,-2,254)
    set %lewl $gettok(%queryhl.return,-2,254)
    var %queryhl.servertype = $iif($mid(%sourcestring,-2,1) == d,Dedicated,Listen)
    var %queryhl.serveros = $iif($right(%sourcestring,1) == l,Linux,Windows)
    unset %source
  }
  var %queryhl.password = $iif($right($mid(%queryhl.return,%pwPos,6),1) == $chr(254),No,Yes)
  var %queryhl.secure = $iif($mid($calc($len(%queryhl.return) - 1),1) == $chr(254),No,Yes)
  var %queryhl.bots = $iif($asc($right(%queryhl.return,1)) == 254,0,$ifmatch)

  ; echo results
  var %queryhl.ip = $sock($sockname).mark
  tokenize 32 %queryhl.ip
  echo -a IP Address: $+($1,:,$2) (approximate ping: %queryhl.ping $+ ms)
  echo -a Hostname: %queryhl.hostname
  echo -a Map: %queryhl.map
  echo -a Gamedir: %queryhl.gamedir
  echo -a Mod: %queryhl.mod
  echo -a Players: %queryhl.players
  echo -a Maxplayers: %queryhl.maxplayers
  echo -a Protocol Number: %queryhl.protocolnumber
  echo -a Server Type: %queryhl.servertype
  echo -a Operating System: %queryhl.serveros
  echo -a Password Protected: %queryhl.password
  echo -a VAC Secure: %queryhl.secure
  echo -a Bots: %queryhl.bots
  msg %my.chan %skin.start Hostname:15 %queryhl.hostname %skin.mid Game Type:15 %queryhl.gamedir %skin.mid Listen/Dedicated:15 %queryhl.servertype %skin.end
  msg %my.chan %skin.start OS:15 %queryhl.serveros %skin.mid Map:15 %queryhl.map %skin.mid Players:15 %queryhl.players $+ / $+ %queryhl.maxplayers %skin.mid PW:15 %queryhl.password %skin.mid VAC:15 %queryhl.secure %skin.mid Bots:15 %queryhl.bots %skin.end

  unset %queryhl.beginticks
  sockclose queryhlserver
  .timerqueryhltimeout off
}