URL-logger

From Scriptwiki
Revision as of 12:24, 23 March 2007 by Cail (talk | contribs) (small fix)

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 Quakenet -> #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
;
;
; UPDATE 18/01/2007: added on input event for the search (optional), rearranged the dll-stuff into same place, 
;                    fixed a bug that didn't color some opened links without http:// (only www.)
;                    fixed if (!$exists($spopupdll)) { return } from linkpopup to 'halt' (now shows the original popup)
;                    added a $db alias to return the filename, users can choose their own path/filename for the file.
; UPDATE 29/01/2007: added a regex into on keydown event to halt the script if something else than \S was pressed
;                    (now holding down shift/alt/ctrl doesn't affect it), and optimized the on keydown event a bit.
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 

; $db returns the name of the database file, change this to whatever you want
alias -l db { return $qt(links.db) }

; 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 $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 }
}

; $clr alias to return $chr(3) and formatted color code, ie. $clr(3) == $+($chr(3),03) == ctrl+k 03
alias -l clr { if ($isid) { 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 {
  if (!$isid) { return }
  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 file, 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 = /(?:^|[][()<>{}'"\s])((?:(?: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 database file, 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 the database file 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($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 $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 on keydown event triggers every time you press a key in @links window, 
; and filters the database file 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
; if you wish to use search with on input (searches when you press enter), disable #onKEYsearch -group (change 'on' -> 'off')

#onKEYsearch on
on *:KEYUP:@links:*:{
  ; first if checks that if something else than \S is pressed (ie. holding down ctrl, alt or shift won't affect it)
  if (!$regex($keychar,/\S/)) { return }
  ; else, filter with the content of editbox
  else { filter -cpfw $db @links $+(*,$editbox(@links),*) } 
}
#onKEYsearch end

; on input search for the @links window, if you want to use this instead of the on keydown, enable #onINPUTsearch -group (change 'off' -> 'on')
#onINPUTsearch off
on *:INPUT:@links: {
  filter -cpfw $db @links $+(*,$1-,*)
}
#onINPUTsearch end

; 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 and append "http://" in front of "www." 
  ; read the url info from database file, 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 %url = $iif(www* iswm %url,$+(http://,%url),%url)
  var %oldurl = $read($db,w,$+(*,$chr(31),%url,$chr(31),*))
  if ($gettok(%oldurl,4,32) != !) { write $+(-l,$readn) $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, halt so it'll show the default popup
  if (!$exists($spopupdll)) { halt }

  ; 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
}

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; here starts the functionality with the spopup.dll
; you won't need this part if you don't want to use the dll
;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

; the path to spopup.dll
alias -l spopupdll { return $qt(spopup.dll) }

; the new popup to replace the original one with a dll
alias -l popup_url {

  ; strip colors etc. from given url ($1), and find the url info from the database file
  var %url = $strip($1)
  var %url2 = $read($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) $db }
}


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;
; 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, using $urlc() to store & color links if there are any
  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 $color(own text) -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-)

  ; echo the text into a right channel, -m highlights the switchbar button with "message color", -t adds timestamp
  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 the text into a right query 
  echo $color(normal) -mt $nick $+(<,$nick,>) %line
}