URL-logger

From Scriptwiki
Revision as of 14:26, 17 January 2007 by Cail (talk | contribs) (added links to screenshots)

Jump to: navigation, search

A picture is worth 1000 words:
Screenshot 1: Colored links
Screenshot 2: Links search

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 
; 
; URL-logger with colored and underlined links by Cail at irc.quakenet.org -> #help.script
;
; $urlc -alias will color and underline links from the input, and log them into a file for later searching
; Tested and works on mIRC 6.2 and above (i recommend using >6.21 for $regsubex to work correctly)
; 
; This script uses spopup.dll (OPTIONAL! you only need this if you want a new popup with info and some new features.),
; to replace mirc's builtin url-popup
; spopup.dll is made by Saturn, and you can get it from http://www.xise.nl/mirc/
; 
; Formats recognized links like:
;   - "http://google.com" -> http://google.com
;   - url('www.google.com'); -> www.google.com
;   - (www.google.com/ahihi(kiaa)) -> www.google.com/ahihi(kiaa)
;   
;   And some more cleanups, and adds 'http://' in front of 'www.' for storing.
;   so that www.google.com and http://www.google.com are treated as a same link, and stored only once
;
; Uses 3 different colors for coloring links:
;   1) new link - not found from the file
;   2) opened link - is found from the file, and you have opened it via mirc
;   3) old link - is found from the file, but you haven't opened it
;
;
; Thanks to:
;   - Saturn for the spopup.dll
;   - Msmo for the cleanup regex
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 

; alias to open the links window, and filter all of the urls into it
alias urllog { 
  ; if the window isn't open, create it, -ea to create an editbox and activate the window
  ; filter will copy the lines from the file by a matchtext (* = everything), -fw = from File to Window, and -p = wrap the text
  if (!$window(@links)) { 
    window -ea @links 
    filter -fwp links.db @links * 
  } 

  ; else the window is already open, so activate it
  else { window -a @links } 
}

; alias to return colorcodes for the script, change these values to whatever you like.
; new, old and opened are for echo text, log_link and log_info are for the @links window
alias -l url_color {
  if ($1 == new) { return 12 }
  elseif ($1 == old) { return 6 }
  elseif ($1 == opened) { return 10 }
  elseif ($1 == log_link) { return 10 }
  elseif ($1 == log_info) { return 14 }
}

; the path to spopup.dll (you won't need this if you decide not to use this dll, and don't need the popup)
; and clr alias to return $chr(3) and formatted color code, ie. $clr(3) == $+($chr(3),03) == ctrl+k 03
alias -l spopupdll { return $qt(spopup.dll) }
alias -l clr { return $+($chr(3),$base($1,10,10,2)) $+ $iif($2,$+($chr(44),$base($2,10,10,2)),) }

; this alias handles checking the input for links, looping through tokens (words in this case), and replacing links with colored ones
alias urlc {
  var %string = $1-

  ; if the line even has links, useless to loop through it if there are no links
  if ($regex(%string,/(?:\b(?:www.?\.|https?:\/\/|s?ftp:\/\/).)/i)) {

    ; so the line has links, loop through tokens then
    var %x = $numtok(%string,32) 
    while (%x) { 

      ; we'll assign the current token into a variable %tok, for easier use later
      ; and we'll run it through $url_parse, and assign the result into a variable %url 
      ; (it'll be checked if it's a 'valid' url, and useless stuff be removed around it, if it's not an url the alias will return $null
      var %tok = $gettok(%string,%x,32)
      var %url = $url_parse(%tok)

      ; check if there is an url in variable %url
      if (%url) {

        ; $url_log will take care of storing the url into a database, and return appropriate color to use in coloring the link
        var %indb = $url_log(%url)

        ; now we color and underline the url and assign it into %newurl, (%indb will be holding the color code number)
        ; then we replace the url part of the token, so that from "url('www.something.com');" only the "www.something.com" part will be colored and underlined
        var %newurl = $+($clr(%indb),$chr(31),%url,$chr(31),$clr)
        var %newurl = $replace(%tok,%url,%newurl)

        ; and finally, we put the modified token back to the original string
        %string = $puttok(%string,%newurl,%x,32)
      }
      dec %x 
    }
  }

  ; modified or not, return the string
  return %string
}

; alias to trim the given url, returns $null if input isn't an url
; big thanks to Msmo on Quakenet for this regex
alias -l url_parse {
  var %p = /(?:^|[][()<>{}'" ])((?:(?:irc|s?ftp|https?:)\/\/|www\d*\.)[^\s./]+\.(?:\[\S*?]|\(\S*?\)|\{\S*?}|[^][()<>{}'" ])++)/igS
  noop $regex($1,%p)
  return $regml(1)
}

; alias to store the given url into links.db, and return color according to whether it's new, opened, or unopened in database
alias -l url_log {

  ; this first $iif is to append "http://" in front of "www.", so "www.google.com" and "http://www.google.com" will be a same link, and not to be written twice
  ; we'll assign the target channel/query into variable %t
  ; the $read will search links.db for the given url  
  var %url = $iif(www* iswm $1,$+(http://,$1),$1)
  var %t = $iif($left($target,1) != $chr(35),query,$target)
  var %isold = $read(links.db,w,$+(*,$chr(31),%url,$chr(31),*))

  ; if %isold is empty (the link wasn't found from the file), write it in there and return color of "new link"
  if (!%isold) { 
    write links.db $asctime(yyyy.mm.dd HH:nn:ss) $+($clr($url_color(log_link)),$chr(31),%url,$chr(31),$clr()) $+($clr($url_color(log_info)),$iif($nick,$nick,$me),@,%t,$clr()) 
    return $url_color(new)
  }

  ; otherwise, if the link was found, check if it has been opened via mirc (4th word will be !), return color of "opened link"
  elseif ($gettok(%isold,4,32) == !) { 
    return $url_color(opened)
  }

  ; and if none of above, it's in the database, but you haven't opened it, return color of "old link"
  else { 
    return $url_color(old)
  }
}

; this alias uses spopup.dll, so if you don't like to use dll/don't need the popup, you won't need this
alias -l popup_url {

  ; strip colors etc. from given url ($1), and find the url info from links.db
  var %url = $strip($1)
  var %url2 = $read(links.db,w,$+(*,$chr(31),%url,$chr(31),*))

  ; assign the info variables
  var %datetime = $strip($gettok(%url2,1-2,32))
  var %nickchan = $strip($gettok(%url2,4-,32))
  var %nickchan = $iif($left(%nickchan,1) == !,$right(%nickchan,-2),%nickchan)

  ; show the popup
  dll $spopupdll clear
  dll $spopupdll add 1 0 0 open
  dll $spopupdll add 2 0 0 copy url
  dll $spopupdll add 3 0 0 copy info
  dll $spopupdll add 4 0 0 %datetime %nickchan

  ; show additional "remove item" if you are in the linklist
  if ($active == @links) {
    dll $spopupdll add 5 0 0 -
    dll $spopupdll add 6 0 16 remove url from db
    dll $spopupdll add 7 1 0 ok
  }

  ; assign the popup selection into a variable
  var %c =  $dll($spopupdll,popup,$mouse.dx $mouse.dy 1)

  ; check what the selection was
  ; these are the ID's from the popup, 2) copy url 3) copy info 4) the info (echo's the info into active) 7) remove link (only in @links window) 
  if (%c == 1) { url -n %url }
  elseif (%c == 2) { clipboard %url }
  elseif (%c == 3) { clipboard %datetime %nickchan %url }
  elseif (%c == 4) { echo -agi30 url info: %datetime %nickchan %url }
  elseif (%c == 7) { write $+(-dl,$readn) links.db }
}

; this on keydown event triggers every time you press a key in @links window, 
; and filters the links.db into the window with the text in editbox
; so, performs a new search everytime you write something in that window, i like this kind of search, but you can make it with on input
on *:KEYDOWN:@links:*:{ 

  ; if $keychar == $chr(8) (backspace), remove one character from the editbox text
  if ($keychar == $chr(8)) { filter -cpfw links.db @links $+(*,$left($editbox(@links),-1),*) } 

  ; else, read the editbox and add the $keychar into it
  else { filter -cpfw links.db @links $+(*,$editbox(@links),$keychar,*) }
}

; on hotlink to trigger on links, this first one is for doubleclick
on *$:HOTLINK:/((www.?\.|https?:\/\/|ftp:\/\/).)/i:*:{ 

  ; clean up the triggered url with $url_parse
  ; read the url info from links.db, and if it doesn't have '!' as fourth token, add the '!' in there and write it back
  ; then open the url with url -n %url (change this, if you have your own alias for opening urls)
  var %url = $url_parse($1)
  var %oldurl = $read(links.db,w,$+(*,$chr(31),%url,$chr(31),*))
  if ($gettok(%oldurl,4,32) != !) { write $+(-l,$readn) links.db $instok(%oldurl,!,4,32) }
  url -n %url 
}

; this second on hotlink is for the right click, shows the popup
on ^*$:HOTLINK:/((www.?\.|https?:\/\/|ftp:\/\/).)/i:*:{ 
  ; check if the spopup.dll file is in the right place, if not, return
  if (!$exists($spopupdll)) { return }

  ; check mouse position for popup to show in the right place (this part is from chanlinks.mrc by Saturn)
  if ($mouse.key & 16) set -eu60 %cl.coords $mouse.x $mouse.y
  elseif (%cl.coords == $mouse.x $mouse.y) popup_url $url_parse($1)
  else unset %cl.coords
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; the rest of the script is a part of my how to make your own theme, and is only for an example usage of $urlc()
; basically you just need to run a line through $urlc() to color the links and log them
; you can add this into whatever event you like, ie. var %line = $urlc($1-) | echo %line
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

on *:INPUT:*: {
  ;we will check that ctrl wasn't pressed while pressing enter, and that it's not a command
  if ($left($1,1) === $readini(mirc.ini,text,commandchar)) && (!$ctrlenter) { return }

  ;halt the default actions
  haltdef

  ;we'll put the line into a variable, in case we want to modify it for the echo
  var %line = $urlc($1-)

  ;here is the part what we send as msg, we want to send $1- and not the modified variable
  .msg $target $1-

  ;then we have the echo
  echo -at $+(<,$nick($chan,$me).pnick,>) %line 
}

on ^*:TEXT:*:#: {
  ;halt the default actions and assign $1- into a variable, and run it through $urlc() to color the links and log them
  haltdef
  var %line = $urlc($1-)

  ;a regular expression to check if the line contains your nick
  ;triggers for Cail, Caili, Cail:, Caili: basically nick + 2 characters 
  ;('i' in [i,$chr(44),:] is 'cause i'm sometimes referred to Caili, you can ofcourse remove it, or change it/add other characters)

  var %hlpattern = $+(/(^|\s),$me,([i,$chr(44),:]{0,2})?\b/i)
  if ($regex($1-,%hlpattern)) {

    ;if the active window isn't the same as the window where the highlight was; echo it to the active
    if ($active != $chan) { echo $color(highlight) -at $+(<,$nick($chan,$nick).pnick,>) $+($chan,/,$nick,:) %line }

    echo $color(highlight) -mt $chan $+(<,$nick($chan,$nick).pnick,>) %line
  }
  else {
    echo $color(normal) -mt $chan $+(<,$nick($chan,$nick).pnick,>) %line
  }
} 

on ^*:TEXT:*:?: {
  ;halt default actions, and set $1- into a variable, and run it through $urlc()
  haltdef
  var %line = $urlc($1-) 

  echo -mt $nick $+(<,$nick,>) %line
}