URL Grabber

From Gwen Morse's Wiki
Jump to: navigation, search
;; TinyFugue URL grabber
;;
;; Version 0.9.2-alpha
;;
;; This script is copyrighted April 26, 1999 by Christian J.  Robinson
;; <heptite at gmail dot com>.  It is protected and distributed under the
;; terms of the GNU General Public License, version 2 or later.
;;
;; No messages will be printed when this script captures URLs.
;
;;; Usage: 
;
; /addurl <URL>         Add URL to the list, if it's not already there.
; /delurl <#>           Delete URL number # from the list.
;
; /urllist
; /listurl
; /listurls             Show the URL list.
;
; /urlwipe 
; /wipeurl
; /wipeurls             Completely wipe out the URL list.
;
; /urlview [args] <#>   Launch a web browser to URL number # from the list.
;                       [args] are passed to the shell command.
; /urlview              View the last URL in the URL list.
;
; /urlgrab              Show whether URLs are being grabbed.
; /urlgrab <on|1|off|0> Turn URL grabbing on/off.
;
; /graburl & /graburls  Same as /urlgrab
;
;
; You -must- set the variable 'URL_Viewer' to the shell command that
; launches, or remote controls the web browser.  It is your responsibility
; to make sure this shell command knows how to handle the browser.  That is
; -not- the job of this script.
;
; You can set the variable 'URL_Method' to 'sh' (default) or 'quote'.  Use 'sh'
; if you plan on having the program run under the current TTY.  Use 'quote' if
; it launches a GUI program.
;
; You can use the variable 'URL_N' to determine the number of URLs in the
; list.  If you use it in the status line, make sure you load this file
; before you define the status line, or set the variable to 0 yourself
; first.
;
; You can use the variable 'URL_Grab' to determine wether the URL grabbing
; trigger is active or not.  If you plan to use it in the status line, load
; this file first.
; -----------------------------------------------------------------------------
; RCS:
; $Id: urlgrab.tf,v 1.26 2009/06/23 14:09:48 infynity Exp $
; $Log: urlgrab.tf,v $
; Revision 1.26  2009/06/23 14:09:48  infynity
; Update email address
;
; Revision 1.25  2007/07/17 05:30:39  infynity
; Escape {}s when adding an URL
;
; Revision 1.24  2004/09/11 14:57:23  infynity
; Make the /urllist timestamps include the month and day.
;
; Revision 1.23  2004/08/09 19:44:46  infynity
; Better handling for TF5.
;
; Revision 1.22  2004/02/18 10:15:33  infynity
; *** empty log message ***
;
; Revision 1.21  2003/11/30 11:14:35  infynity
; Up the priority of the trigger so it's more likely to catch URLs.
;
; Revision 1.20  2003/10/31 18:10:13  infynity
; Properly escape some stuff.
;
; Revision 1.19  2003/10/31 16:37:53  infynity
; Use strip_attr() on captured URLs for tf5.0a13 and later.
;
; Revision 1.18  2003/08/19 09:54:43  infynity
; *** empty log message ***
;
; Revision 1.17  2003/07/22 11:46:01  infynity
; Simplified the trigger code using regmatch(), and strip > at the end of a match.
;
; Revision 1.16  2003/06/29 12:31:57  infynity
; *** empty log message ***
;
; Revision 1.15  2003/05/28 11:51:07  infynity
; *** empty log message ***
;
; Revision 1.14  2003/05/27 13:30:30  infynity
; Tweaks for TF5.0 alpha 10.
;
; Revision 1.13  2002/01/17 05:50:24  infynity
; Escape shell metacharacters in the URL,
;  otherwise malicious code could be executed.
;
; Revision 1.12  2002/01/08 20:44:12  infynity
; $[/escape] changed to escape()
; Other minor tweaks.
;
; Revision 1.11  2001/02/17 06:20:11  infynity
; *htm'. and *html'. have the last two characters stripped now.
;
; Revision 1.10  2000/11/13 03:47:45  infynity
; Email address change, again.
;
; Revision 1.9  2000/11/03 06:03:56  infynity
; Email address change.
;
; Revision 1.8  2000/10/29 09:49:26  infynity
; Yet another pattern tweak.
;
; Revision 1.7  2000/10/18 18:14:07  infynity
; Another pattern tweak.
;
; Revision 1.6  1999/08/30 11:37:20  infynity
; Pattern tweak.
;
; Revision 1.5  1999/07/16 12:29:48  infynity
; I habitually forget to increment the version number.  Bah.
;
; Revision 1.4  1999/07/16 12:28:11  infynity
; Fixed TF's warning about %{P2} being legal, but confusing.
;
; Adding an URL with no foreground world could be a problem.  Use "NONE" in this case.
;
; Revision 1.3  1999/07/13 06:09:35  infynity
; Added URL_Method.
;
; Revision 1.2  1999/07/13 05:07:57  infynity
; Removed unnecessary global variable "_URL_List".
;
; Added timestamp and world name.
;
; Revision 1.1  1999/07/12 08:24:36  infynity
; Initial revision
;

/loaded __OWNLIB__/urlgrab.tf

/require lisp.tf


; A parameter that contains the number of URL's grabbed:
/eval /set URL_N=$(/length %URL_List)

; A state variable that determines whether to grab URL's:
/if ({URL_Grab} =~ "") \
	/set URL_Grab=1 %; \
/endif

; Add the url to the list.  We want to eliminate spaces and commas, so we just
; translate them to hex numbers.
/def -i addurl = \
	/if ($(/ver) >= 5.0 ) \
		/let URL=$[strip_attr({*})] %; \
	/else \
		/let URL=%* %; \
	/endif %; \
	/let URL=$[replace(" ", "%20", {URL})] %;\
	/let URL=$[replace(",", "%2C", {URL})] %;\
	/let URLPAT=$[strcat('.+,[0-9.]+,', escape("*+.?[]{}\\(\\)\\\\", {URL}))] %; \
	/let WName=$[${world_name} !~ "" ? ${world_name} : "NONE"] %; \
	/for i 1 $(/length %{URL_List}) \
		/if ( regmatch(URLPAT, \$(/nth \%{i} \%{URL_List})) ) \
			/return 999 \%; \
		/endif %; \
	/if ( {?} != 999 ) \
		/set URL_List=%URL_List %WName,$[time()],%URL %; \
		/set URL_N=$(/length %URL_List) %; \
	/endif

; Delete an URL:
/def -i delurl = \
	/let _URL_List=%URL_List %; \
	/let _arg=%1 %; \
	/if ( $(/nth %{1} %{_URL_List}) =~ "" ) \
		/echo %% No URL %1 in the list. Type /urllist to see the list.%; \
		/return 1 %; \
	/endif %; \
	/set URL_List= %; \
	/for i 1 $(/length %{_URL_List}) \
		/if ( {i} != {_arg} ) \
			/set URL_List=\%URL_List \$(/nth \%{i} \%{_URL_List}) \%; \
		/endif %; \
	/set URL_N=$(/length %{URL_List})

; List the URLs:
/def -i urllist = \
	/if ( {URL_List} !~ "" ) \
		/for i 1 $(/length %{URL_List}) \
			/test \$[regmatch("(.+),([0-9.]+),(.*)", \$(/nth \%{i} \%{URL_List}))] \%; \
			/echo \%i) \$[ftime('\%a \%b \%d \%I:\%M \%p', {P2})] @ \%P1 -> \%P3 %; \
	/else \
		/echo %% No URLs. %; \
	/endif
/def -i listurl = /urllist
/def -i listurls = /urllist

; Wipe the entire list:
/def -i urlwipe = /set URL_List= %; /set URL_N=0
/def -i wipeurl = /urlwipe
/def -i wipeurls = /urlwipe

; Macros to turn URL grabbing on/off:
/def -i urlgrab = \
	/if ({*} =~ "") \
		/if ({URL_Grab} == 1) \
			/echo %% URL grabbing is on. %;\
		/else \
			/echo %% URL grabbing is off. %;\
		/endif %; \
		/return %; \
	/elseif ({*} =~ "1" | {*} =/ "on") \
		/set URL_Grab=1 %;\
		/echo %% URL grabbing is now on. %;\
	/elseif ({*} =~ "0" | {*} =/ "off") \
		/set URL_Grab=0 %;\
		/echo %% URL grabbing is now off. %;\
	/else \
		/echo %% Usage: %0 [on|1|off|0] %;\
	/endif
/def -i graburl = /urlgrab %*
/def -i graburls = /urlgrab %*

; View the URL, this command requires you to define the shell command that will
; actually run the browser:
/def -i urlview = \
	/if ( {URL_Viewer} =~ "" ) \
		/echo %% No URL viewer is defined. %; \
		/return 1 %; \
	/endif %; \
	/if ( {L} =~ "" ) \
		/let URL=$(/last %{URL_List}) %; \
	/elseif ( $(/nth %{L} %{URL_List}) !~ "" ) \
		/let URL=$(/nth %{L} %{URL_List}) %; \
	/else \
		/echo %% No URL # %L in the list. Type /urllist to see the list.%; \
		/return 1 %; \
	/endif %; \
	/test regmatch(".+,[0-9.]+,(.*)", URL) %; \
	/echo %% Viewing URL: %P1 %; \
	/let URL=$[escape("*?[](){}\\\\$'`\";&<>|", {P1})] %; \
	/if ({URL_Method} =~ "sh" | {URL_Method} =~ "") \
		/sh %URL_Viewer %-L %URL %; \
	/elseif ({URL_Method} =~ "quote") \
		/quote -0 /echo - !%URL_Viewer %-L %URL %; \
	/else \
		/echo %% Unknown value for \%URL_Method: %URL_Method %; \
		/return 1 %; \
	/endif
/def -i viewurl = /urlview %*

; Snag URL's, but let the trigger fall through:
;
; This thing tries to be smart about the end of an URL, but it won't be perfect
; about it.
/def -p100 -i -F -mregexp -t'((http|ftp)://[^ ]+|(www|ftp)\\.[^ ]+)' -q URL_Grab = \
	/let _match=%P1 %; \
	/if ({URL_Grab} != 1) \
		/return %; \
	/endif %; \
	/if (regmatch("(html?|/)(\\.\"|'\\.)$", {_match})) \
		/let URL=$[substr({_match}, 0, -2)] %; \
	/elseif (regmatch("(html?|/)[.,)']$", {_match}) | {_match} =/ "*[>\"]") \
		/let URL=$[substr({_match}, 0, -1)] %; \
	/else \
		/let URL=%_match %; \
	/endif %; \
	/if ( ! regmatch("^(http|ftp)://", {URL}) ) \
		/addurl http://%URL %; \
	/else \
		/addurl %URL %; \
	/endif