sunslayer commented on a Page, mIRC Objects  -  Jan 06, 2011

example demonstrating its use in sockets to retrieve the title of a web page

alias socket {
  ; name of object (alias name)
  var %this = socket

  /*
  * 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)
  if (%param) { .tokenize 44 %param }
  if (!$hget(%this)) { .hmake %this 5 }
  if (!$1 && $regml(prop,0) == 1) { return $hget(%this,$prop) }
  .goto %prop

  /*
  * start of class methods
  */
  :init
  if (!$sock($$1) || !$3) {
    .noop $regex($2,/(?:https?\x3A\/\/)?(?:www\.)?([\w]+\..{2,3})(\/[\w]+(?:\..{1,})?)?/)
    if (!$regml(1)) {
      echo -a Invalid URI 
      .noop $socket().destroy() 
      return 0 
    }
    var %a = $+(www.,$regml(1)),%b = $iif($regml(2),$v1,/)
    .sockopen $1 %a $iif($4,$4,80)
    .hadd %this sockData $(GET %b HTTP/1.1,|,HOST: %a)
    .hadd %this sockname $1
    .hadd %this cb_funct $3
  }
  else {
    echo -a socket $qt($1) already exists
    .noop $socket().destroy()
    return 0
  }
  return 1

  :addData
  if (!$3) { .hadd %this $1 $$2 }
  elseif ($3) { .hadd %this $1 $addtok($hget(%this,$1),$2 $3,124) }
  return 1

  :destroy
  if ($sock($hget(%this,sockname))) { .sockclose $hget(%this,sockname) }
  if ($hget(%this)) { .hfree %this }
  return 1

  :open
  var %a = .sockwrite -n $1,%b = $hget(%this,sockData),%c = 1
  .tokenize 124 %b
  while ($ [ $+ [ %c ] ]) {
    %a $v1
    inc %c
  }
  %a $crlf
  return 1

  :read
  var %socket = $hget(%this,sockname),%cb = $hget(%this,cb_funct)
  %cb %socket
  return 1

  :error
  echo -a $iif($error,$v1,Unknown error) in Class: $qt(%this)
  .noop $socket().destroy()
  .reseterror
  return 0
}

alias socket_example {
  var %a = $socket(sockex,$$1,socket_read)
  if (!%a) {
    echo -a Unable to connect.
    .noop $socket().destroy()
  }
}

alias socket_read {
  sockread &data
  if ($regex($bvar(&data,1-).text,/<title>(.+?)<\/title>/)) {
    echo -a $regml(1)
    .noop $socket().destroy()
  }
}

on *:sockopen:sockex:.noop $socket($sockname).open()
on *:sockread:sockex:.noop $socket().read()

usage: /socket_example www.google.com

 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.