Smaller URL — File Browser

SmallerURLv0.03 / SmallerURLv0.03 / s.url.mrc

s.url.mrc — 7.84 KB — Download this file

;
; Smaller URL v0.03
; By TBF.
;
; -
; Usage: /s.url http://www.example.org/
; View 'readme.txt' for additional information.
;

; Default output method, echo to active window emulating
; the 'feel' of mIRCs internal command feedback
alias -l s.url.echo {
  echo $color(info) -a * /s.url: $1-
}

; Main focal alias, triggers sockopen event
alias s.url {

  ; Is input blank?
  if (!$1) {
    s.url.echo Invalid parameters - syntax: /s.url http://www.example.org/
    return
  }

  ; Check to see if the URL is cached in db
  s.url.db check $replace($1-, http://www., www.)

  ; Do we have a active socket open?
  if ($sock(s.url).status) {
    s.url.echo Error: Active session already in progress, retry later.
    return
  }

  ; Store requested URL for usage in output
  .set %s.url $1-

  ; Connect to metamark.net
  sockopen s.url metamark.net 80

  ; END s.url alias
}

; Provide alternative aliases to trigger script
alias surl { s.url $1- }
alias small.link { s.url $1- }
alias smalllink { s.url $1- }

on *:sockopen:s.url:{

  ; Check for possible socket errors
  if ($sockerr > 0) {
    s.url.echo Error: Could not connect to xrl.us: $+($left($gettok($sock(s.url).wsmsg, 2-, 32), 1), $right($lower($gettok($sock(s.url).wsmsg, 2-, 32)),-1)), $+($chr(40), $sock(s.url).wserr, $chr(41), .))
    sockclose s.url | unset %s.url* | return
  }

  ; Copy %s.url and use that to encode on
  var %s.url.encoded = %s.url

  ; Safely encode input URL
  %s.url.encode = 37 43 59 39 61 38 58 32 47 36 63 34 60 62 35 123 125 92 94 126 91 93 96 64 42 45 124 40 41 33 46 95
  %i = 1

  ; Cycle through encode array to replace marked
  ; characters with linked hex references
  ;
  ; Rich_NL <http://xrl.us/pyj> recommended $base()
  ; for the following URL HEX encoding, thank you to
  ; BJayD <http://xrl.us/pyi> for $chr() bug fix :)
  while (%i <= $gettok(%encode, 0, 32)) {
    %s.url.encoded = $replace(%s.url.encoded, $chr($gettok(%s.url.encode, %i, 32)), $+(%,$base($gettok(%s.url.encode, %i, 32),10,16,2)))
    inc %i
  }

  ; Send HTTP POST header to server
  sockwrite -n s.url POST /api/rest/simple HTTP/1.1
  sockwrite -n s.url User-Agent: s.url v0.03
  sockwrite -n s.url Host: metamark.net
  sockwrite -n s.url Referer: http://metamark.net/add
  sockwrite -n s.url Connection: close
  sockwrite -n s.url Content-type: application/x-www-form-urlencoded
  sockwrite -n s.url Content-length: $len($+(long_url=, %s.url.encoded))
  sockwrite -n s.url
  sockwrite -n s.url $+(long_url=, %s.url.encoded)
  sockwrite -n s.url

  ; END sockopen event
}

on *:sockread:s.url:{

  ; Check for possible socket errors
  if ($sockerr > 0) {
    s.url.echo Error: Could not read from xrl.us: $+($left($gettok($sock(s.url).wsmsg, 2-, 32), 1), $right($lower($gettok($sock(s.url).wsmsg, 2-, 32)),-1)), $+($chr(40), $sock(s.url).wserr, $chr(41), .))
    return
  }

  ; Check for header status
  if (!%s.url.header) {

    ; Restrain variable to local usage, no need
    ; for global access
    var %s.url.stream
    sockread %s.url.stream

    while ($sockbr) {

      ; Check for HTTP reponse code
      if (HTTP/1.1* iswm %s.url.stream) && ($gettok(%s.url.stream, 2, 32) != 200) {
        s.url.echo Error: Unexpected HTTP response from xrl.us: $+($left($gettok(%s.url.stream, 3-, 32), 1), $right($lower($gettok(%s.url.stream, 3-, 32)),-1)), $+($chr(40), $gettok(%s.url.stream, 2, 32), $chr(41), .))
        sockclose s.url | unset %s.url* | return
      }

      ; Server sent a set of $crlf's, end of reponse header
      if ($len(%s.url.stream) < 4) {
        set %s.url.header done
        break
      }

      ; Read stream buffer for while loop
      sockread %s.url.stream

      ; END while loop
    }

    ; END header isnull
  }
  else {

    ; Read stream buffer to fetch non-header content
    sockread %s.url.stream

    ; Check for errors reported by site
    if (ERROR:* iswm %s.url.stream) {
      s.url.echo Error: Unexpected response from xrl.us: $+($gettok(%s.url.stream, 2-, 32), .)
      sockclose s.url | unset %s.url* | return
    }

    ; Output URL
    if (http://xrl.us/* iswm %s.url.stream) {

      ; Clean the input URL
      if ($len($replace(%s.url, http://www., www.)) >= 54) {
        s.url.echo $+(', $left($replace(%s.url, http://www., www.), 54), $chr(32), ...', $chr(32), -->, $chr(32), %s.url.stream)
      }
      else {
        s.url.echo $+(', %s.url,', $chr(32), -->, $chr(32), %s.url.stream)
      }
      
      ; Store URL into cache
      s.url.db add $remove(%s.url.stream, http://xrl.us/) $replace(%s.url, http://www., www.)

      ; Close the socket, retrieved output URL
      sockclose s.url | unset %s.url* | return

      ; END xrl.us iswm check
    }

    ; END else block
  }

  ; END sockread event
}

; Just in case we didn't issue a /sockclose and therefore
; did not remove our global variables, clean them here
on *:sockclose:s.url:{
  unset %s.url*
}

alias s.url.db {

  ; Check to see if db hash exists
  if (!$hget(s.url.db)) {

    if ($isfile($+($scriptdir, s.url.db)) == $true) {

      ; s.url.db exists
      ; Form a dynamically sized hash table
      hmake s.url.db $int($calc($lines($+($scriptdir, s.url.db))/15+1))

      ; Load db file
      hload s.url.db $+(", $+($scriptdir, s.url.db), ")

    }
    else {

      ; s.url.db doesn't exist
      ; Form generically sized hash table
      hmake s.url.db 50

    }

  ; END hash existence check
  }

  ; Check cache request
  if ($1 == check) {
    
      ; Loop the db to locate URL
      %i = 1
      while (%i <= $hget(s.url.db,0).item) {
      
        ; Check for cache URL, separate the ctime we stored for reference
        if ($gettok($hget(s.url.db, $hget(s.url.db,%i).item), 2, 01) == $2-) {
        
              ; Tidy up the output info'
              if ($len($2-) >= 54) {
                s.url.echo $+(', $left($2-, 54), $chr(32), ...', $chr(32), -->, $chr(32), $+(http://xrl.us/, $hget(s.url.db,%i).item))
              }
              else {
                s.url.echo $+(', %s.url,', $chr(32), -->, $chr(32), $+(http://xrl.us/, $hget(s.url.db,%i).item))
              }
        
          ; Located a match, halt script
          halt
        }
        
        inc %i

      ; END while loop
      }
    
  }
  
  ; Add input to table
  if ($1 == add) {
    hadd s.url.db $2 $+($ctime, $chr(01), $3-)
  }

  ; END s.url.db alias
}

; Display the data which is cached by s.url
alias s.url.cache {

  ; Load up the hash table
  s.url.db
  
  ; Check to see if window exist
  if (!$window(@s.url.db.cache).state) {
    window -ke0 @s.url.db.cache
  }

  ; Print header
  aline -p @s.url.db.cache s.url v0.03 - db.cache
  aline -p @s.url.db.cache --
  aline -p @s.url.db.cache 

  ; Check to see if we have anything to display
  if ($hget(s.url.db,0).item == 0) {
  aline -p @s.url.db.cache No cache data found.
  }
  else {

    ; Display cache by looping
    %i = 1
    while (%i <= $hget(s.url.db,0).item) {

    aline -p @s.url.db.cache $asctime($gettok($hget(s.url.db, $hget(s.url.db,%i).item), 1, 01),dd/mm/yy) - $+($asctime($gettok($hget(s.url.db, $hget(s.url.db,%i).item), 1, 01), hh:nntt), :) $+(, $color(info), http://xrl.us/, $hget(s.url.db,%i).item, )
    aline -p @s.url.db.cache Original URL: $+(, $color(info), $gettok($hget(s.url.db, $hget(s.url.db,%i).item), 2, 01), )
    aline -p @s.url.db.cache 

    inc %i

    ; END while loop
    }

    ; Print footer
    aline -p @s.url.db.cache --
    aline -p @s.url.db.cache Total URLs cached: $hget(s.url.db,0).item

  ; END $hget null check
  }
  
; END s.url.cache alias
}

; User is closing IRC client, save the s.url.db cache
on *:exit:{

  if ($hget(s.url.db,0).item != 0) {
    hsave -o s.url.db $+(", $+($scriptdir, s.url.db), ")
  }

}