Cookies class

By sunslayer on Jan 08, 2011

shows how to use http://www.hawkee.com/snippet/8275/ with sockets to retrieve cookies from a webpage.

usage: /login website username password [clearCache=false]

example: /login www.hawkee.com/phpBB2/login.php sunslayer my_pass $true

if clearCache is true it will remove all previous cookies for the given domain

all cookies are saved in cookies.ini in your mIRC directory and can easily be retrieve with $ini

alias cookies.addData.PRIVATE

alias cookies {
  var %this = cookies       | ; Name of object (alias name)
  var %hashSize = 5         | ; Default size of hash table that stores properties

  /*
  * Start of data parsing
  * Do not edit
  */
  .noop $regex(prop,$prop,/^(\w+)(\x28(.+)?\x29)?/)
  var %prop = $iif($regml(prop,1),$regml(prop,1),init),%param = $iif($regml(prop,3),$v1,$null),%func = $+($,%this,.),%addData = $+(%this,.addData %this)
  if (%param) { .tokenize 44 %param }
  if (!$hget(%this) && %prop != init) { echo -a * Error: Object not initialized | halt }
  if ($isalias($+(%this,.,%prop,.PRIVATE))) { echo -a * ERROR: Unable to access Method $qt(%prop) | halt }
  if ($regml(prop,0) == 1) { $iif($1,.hadd %this %prop $1,return $hget(%this,$prop)) | halt }
  $iif($isalias($+(%this,.,%prop)),noop $+($,%this,.,%prop,$chr(40),%prop,$chr(44),$1-,$chr(41)),.goto %prop)
  halt  | ; prevent script from passing this point

  /*
  * Class methods
  * Start editing here
  */
  :init
  if ($hget(%this) || $socket(cookies)) {
    echo -a * Error: Object $qt(%this) already exists.
    halt
  }
  .hmake %this %hashSize
  .noop $regex($1,/^(?:https?\x3A\/\/)?((?:\w+\x2E)?[^\W]+\x2E\w{2,3})([\w\/]+(?:\x2E\w{1,3})?(?:[#?].+)?)?/)
  if ($regml(1) && $3) {
    %addData POST $regml(2) HTTP/1.1
    %addData HOST: $regml(1)
    %addData username $2
    %addData password $3
    %addData clearCache $iif($4,$4,0)
    .sockopen cookies $hget(%this,HOST:) 80
    return 1
  }
  return 0

  :onReady
  var %a = .sockwrite -n $1,%send = $+(username=,$urlencode($hget(%this,username)),&password=,$urlencode($hget(%this,password))),%len = $len(%send),%sect = $+($hget(%this,HOST:),$nofile($gettok($hget(%this,POST),1,32)))
  if ($hget(%this,clearCache) && $ini(cookies.ini,%sect,0)) {
    var %b = $v1
    echo -a %b cookie(s) found, clearing Cache...
    while (%b > 0 && $ini(cookies.ini,%sect,%b)) {
      .remini cookies.ini %sect $v1
      dec %b
    }
  }
  %a POST $hget(%this,POST) HTTP/1.1
  %a HOST: $hget(%this,HOST:)
  %a Content-Length: %len
  %a Content-Type: application/x-www-form-urlencoded 
  %a $crlf %send
  return 1

  :onSockRead
  var %cookies | sockread %cookies
  if ($sockbr == 0) { .goto destroy }
  if ($regex(%cookies,/Set-Cookie: (.+?)=(.+?);/)) { .writeini cookies.ini $+($hget(%this,HOST:),$nofile($gettok($hget(%this,POST),1,32))) $regml(1) $regml(2) }
  return 1

  :destroy
  if ($sock($1)) { .sockclose $1 }
  echo -a $iif($ini(cookies.ini,$+($hget(%this,HOST:),$nofile($gettok($hget(%this,POST),1,32))),0),$v1,0) Cookies saved.
  if ($hget(%this)) { .hfree %this }
  return 1

  :error
  echo -a $iif($error,$v1,Unknown error) in Class: $qt(%this)
  if ($hget(%this)) { .hfree %this }
  .reseterror
  halt
}
alias -l cookies.addData { .hadd -m $$1 $2 $3- }
on *:sockopen:cookies:.noop $cookies($sockname).onReady()
on *:sockread:cookies:.noop $cookies().onSockRead()
on *:sockclose:cookies:.noop $cookies().destroy()

alias login {
  var %a = $cookies($1,$2,$$3,$iif($4,$4,0))
  if (!%a) {
    echo -a Error connecting to the server.
  }
}

alias -l urlencode {
  var %a = $regsubex($$1,/([^\w\s])/Sg,$+(%,$base($asc(\t),10,16,2)))
  return $replace(%a,$chr(32),$chr(43))
}

Comments

Sign in to comment.
sunslayer   -  Jan 12, 2011

thanks

 Respond  
napa182   -  Jan 12, 2011

nice work sunslayer 9/10 +like

 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.