URL.pm

filter: Statementstmt 35.6% 46 / 129 Branchbran 5.1% 3 / 58 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 38.8% 7 / 18 Podpod 28.5% 2 / 7 totaltotal 25.0% 58 / 232 CC 35 SCAR 37.2
CC
35
Cov
25%
cmd_urls
110.0
cmd_url
72.0
__ANON__
42.0
CRAP 41.1
« Util.pm ↑ Summary LimitMan.pm »
1 # $Id: URL.pm,v 1.1 2001-07-27 09:06:48-04 roderick Exp $
2 #
3 # Copyright (c) 2000 Roderick Schertler. All rights reserved. This
4 # program is free software; you can redistribute it and/or modify it
5 # under the same terms as Perl itself.
6  
7 # XXX
8 # - add help
9  
10 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 package Sirc::URL;
13  
14 =head1 NAME
15  
16 Sirc::URL - view URLs with an external browser
17  
18 =head1 SYNOPSIS
19  
20 From sirc:
21  
22 /eval use Sirc::URL
23  
24 /url # load most recent URL
25 /url 27 # load URL marked as number 27
26 /url www.argon.org # load named URL
27  
28 /urls -5 # list 5 most recent URLs
29 /urls 23 # list url marked as number 23
30 /urls # list all URLs
31  
32 /set url_browser openvt lynx %s &
33 /set url_mark off # disable URL marking
34 /set url_mark_format %s[%d] # less verbose marking style
35 /set url_max 234 # cycle back to 0 after 234
36  
37 From Perl:
38  
39 use Sirc::URL qw(browse_url urls);
40  
41 browse_url $url; # load named URL
42 ($index, @url) = urls; # index of most recent, and full list
43  
44 =head1 DESCRIPTION
45  
46 This module provides an easy way to view URLs which you see on IRC. Each
47 URL printed to the screen (as told by the L<URI::Find module|URI::Find>)
48 is numbered. For example, you might see
49  
50 <Morgaine> It was either at http://www.memepool.com (/url 23)
51 or http://www.argon.org/~roderick/books.html (/url 24), I'm
52 not sure which.
53  
54 The C<(/url 23)> and C<(/url 24)> tags were added by B<Sirc::URL>.
55  
56 =head1 COMMANDS
57  
58 =over 4
59  
60 =item /url [I<number>]
61  
62 =item /url I<URL>
63  
64 You can load one of the marked URLs into your web browser by typing
65  
66 /url 23 # load http://www.memepool.com
67 /url 24 # load http://www.argon.org/~roderick/books.html
68 /url # same, no number means load the most recent one
69  
70 You can also use the machinery to load an URL you type yourself
71  
72 /url www.leisuretown.com
73  
74 =item /urls [I<number>]
75  
76 This command lists the noted URLs.
77  
78 /urls -5 # list 5 most recent URLs
79 /urls 23 # list URL marked as number 23
80 /urls # list them all
81  
82 =cut
83  
84 1 ▶use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
85 1 ▶use Sirc::Util qw(addcmd addhelp addhook arg_count_error
Subroutine BEGIN: called CC=1 SCAR=0.0
86 settable_boolean settable_int settable_str
87 1 tell_error tell_question xtell);
88  
89 1 ▶use vars qw($VERSION @ISA @EXPORT_OK);
Subroutine BEGIN: called CC=1 SCAR=0.0
90  
91 1 $VERSION = do{my@r=q$Revision: 1.1 $=~/\d+/g;sprintf '%d.'.'%03d'x$#r,@r};
92 1 ▶$VERSION .= '-l' if q$Locker: $ =~ /: \S/;
Branch: 1 branch
BranchTrueFalseTotal
if "Locker: " =~ /: \S/ 0 1 1
93  
94 1 @ISA = qw(Exporter);
95 1 @EXPORT_OK = qw(browse_url urls);
96  
97 # configuration
98  
99 # XXX document
100 # XXX has to be set before load
101 1 my $Module = 'URI::Find::Schemeless';
102  
103 1 my $Browser = 'netscape -remote openURL\\(%s", new-window)" &';
104 1 my $Debug = 0;
105 # XXX don't try to load until she has a chance to override $Module
106 1 my $Enabled = load_find_uris();
107 1 my $Format = '%s (/url %d)';
108 1 my $Max = 99;
109  
110 1 settable_str url_browser => \$Browser, \&validate_code_or_fmt;
111 1 settable_boolean url_debug => \$Debug;
112 1 settable_boolean url_mark => \$Enabled, \&validate_enabled;
113 1 settable_str url_mark_format => \$Format, \&validate_code_or_fmt;
114 0 ▶settable_int url_max => \$Max, sub { $_[1] >= -1 };
Subroutine __ANON__: not called CC=1 SCAR=1.2
115 # XXX
116 #settable_str url_finder => \$Module;
117  
118 1 ▶use vars qw($Inhibit);
Subroutine BEGIN: called CC=1 SCAR=0.0
119  
120 =head1 SETTABLE OPTIONS
121  
122 You can set these variables with B<sirc>'s C</set> command.
123  
124 =over 4
125  
126 =item B<url_browser> I<format>|I<code ref>
127  
128 This sets the command which will be used to load URLs. It can be either
129 an sprintf() format or a code reference. The default value is
130  
131 netscape -remote openURL\(%s", new-window)" &
132  
133 If the value is a string it's passed through sprintf() with a single argument
134 (the URL to be viewed, but already shell-escaped, so you shouldn't quote it
135 yourself). The command is interpred with F</bin/sh>.
136  
137 If the value is a code reference it's called with the URL and it can do
138 what it likes. You have to use doset() to set this up.
139  
140 doset 'url_browser', \&mysub;
141  
142 I'd originally thought to make a number of pre-canned settings for
143 this, such as combinations of C<netscape>, C<netscape -remote>, and
144 C<netscape-remote> with and without C<new-window>, and text-mode
145 browsers launched with a settable X terminal emulation program or in
146 a new B<screen> window, or the like, but I came to believe that was a
147 bad idea. No matter which method you like to use, you'd do well to
148 have a simple shell script which you can pass URLs to in order to
149 load them, and if you have one of those you can just specify it for
150 this.
151  
152 =item B<url_debug> B<on>|B<off>|B<toggle>
153  
154 This controls debugging messages. You can use them to see what command
155 is actually being run to load an URL.
156  
157 =item B<url_mark> B<on>|B<off>|B<toggle>
158  
159 This turns URL marking on and off. It defaults to on, if the L<URI::Find
160 module|URI::Find> is available.
161  
162 =item B<url_mark_format> I<format>|I<code ref>
163  
164 B<url_mark_format> dictates how URLs are marked up on the screen. It
165 can be either an sprintf() format or a code reference. The default
166 value is
167  
168 %s (/url %d)
169  
170 If it's a string it's given to sprintf() with 3 arguments, the original
171 text, the URL number, and the cleaned-up URL.
172  
173 This doesn't give you much flexibility, though, since Perl's sprintf()
174 doesn't support position specifiers (eg, C<%2$s> to print the second
175 argument as a string). You can set B<url_mark_format> to a code
176 reference if you need to do something more fancy. Your sub is called
177 with the same 3 args, it returns the text which will replace the
178 original URL. You have to use doset() to set this up.
179  
180 doset 'url_mark_format', \&mysub;
181  
182 =item B<url_max> I<number>
183  
184 URL numbers cycle back around to 0 once they pass the B<url_max>, the
185 default is 99. If you set this to -1 the list will just keep growing
186 (and your free memory will just keep shrinking).
187  
188 =back
189  
190 =head1 PROGRAMMING
191  
192 B<Sirc::URL> optionally exports some functions and variables.
193  
194 =over 4
195  
196 =cut
197  
198 # internal use
199  
200 1 my $Count = -1;
201 1 my $Loaded_find_uris = 0;
202 1 my @Url;
203  
204 0 ▶sub debug {
Subroutine debug: not called CC=2 SCAR=17.9
Pod undocumented
205 0 ▶ xtell 'url debug ' . join '', @_
Branch: 1 branch
BranchTrueFalseTotal
if $Debug 0 0 0
206 if $Debug;
207 }
208  
209 0 ▶sub waitstat ($) {
Subroutine waitstat: not called CC=2 SCAR=17.9
Pod undocumented
210 0 my ($w) = @_;
211  
212 0 ▶ if (eval { local $SIG{__DIE__}; require Proc::WaitStat }) {
Branch: 1 branch
BranchTrueFalseTotal
if (eval { do { local $SIG{'__DIE__'}; require Proc::WaitStat } }) { } 0 0 0
213 0 return Proc::WaitStat::waitstat($w);
214 }
215 else {
216 0 return "wait status $w";
217 }
218 }
219  
220 =item B<browse_url> I<url>
221  
222 This is the internal machinery which is used to view an URL. You can
223 use it, too, to piggy-back on the user's existing browser configuration.
224 It doesn't return anything meaningful.
225  
226 =cut
227  
228 0 ▶sub browse_url {
Subroutine browse_url: not called CC=4 SCAR=30.0
Pod documented
229 0 ▶ unless (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@_ == 1) 0 0 0
230 0 arg_count_error undef, 1, @_;
231 0 return;
232 }
233 0 my ($url) = @_;
234  
235 0 ▶ if (ref $Browser) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $Browser) { } 0 0 0
236 0 $Browser->($url);
237 }
238 else {
239 0 my $cmd = sprintf $Browser, quotemeta $url;
240 0 debug "running $cmd";
241 0 system $cmd;
242 0 ▶ if ($?) {
Branch: 1 branch
BranchTrueFalseTotal
if ($?) 0 0 0
243 0 tell_error "Non-zero exit (" . waitstat($?) . ") from: $cmd";
244 }
245 }
246 }
247  
248 =item B<urls>
249  
250 This returns the index of the current URL followed by the full list. If
251 no URLs have been noted nothing is returned
252  
253 =cut
254  
255 0 ▶sub urls {
Subroutine urls: not called CC=2 SCAR=17.9
Pod documented
256 0 ▶ if (@Url) {
Branch: 1 branch
BranchTrueFalseTotal
if (@Url) { } 0 0 0
257 0 return $Count, @Url;
258 }
259 else {
260 0 return;
261 }
262 }
263  
264 1 ▶sub load_find_uris {
Subroutine load_find_uris: called CC=3 SCAR=12.7
Pod undocumented
265 # XXX she could change module
266 1 ▶ return 1 if $Loaded_find_uris;
Branch: 1 branch
BranchTrueFalseTotal
if $Loaded_find_uris 0 1 1
267  
268 1 ▶ if (!eval "local \$SIG{__DIE__}; require $Module") {
Branch: 1 branch
BranchTrueFalseTotal
unless (eval "local \$SIG{__DIE__}; require $Module") 1 0 1
269 1 tell_error "$Module module not available, URL marking disabled ($@)";
270 1 return;
271 }
272  
273 0 $Loaded_find_uris = 1;
274 0 return 1;
275 }
276  
277 0 ▶sub validate_code_or_fmt {
Subroutine validate_code_or_fmt: not called CC=2 SCAR=17.9
Pod undocumented
278 0 my ($name, $val) = @_;
279  
280 0 ▶ if (ref $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) { } 0 0 0
281 0 return ref $val eq 'CODE';
282 }
283 else {
284 0 return eval { local $SIG{__DIE__}; my $x = sprintf $val; 1};
285 }
286 }
287  
288 0 ▶sub validate_enabled {
Subroutine validate_enabled: not called CC=3 SCAR=24.8
Pod undocumented
289 0 my ($name, $val) = @_;
290  
291 0 ▶ if ($val && !load_find_uris) {
Branch: 1 branch
BranchTrueFalseTotal
if ($val and not load_find_uris()) { } 0 0 0
Condition0%0/3
$val and not load_find_uris()
!ll&&!rl&&r
000
MC/DC0%0/2
$val and not load_find_uris()
$val not load_find_uris()
Truth table0%0/3
A and not(B)
A= $valB= not load_find_uris()
ABresult
0X0
100
111
292 0 return 0;
293 }
294 else {
295 0 return 1;
296 }
297 }
298  
299 =item B<$Sirc::URL::Inhibit>
300  
301 If this is set to true then URL marking is inhibited. You might want
302 this if you're going to print the existing URL list out to the screen,
303 for example. You could also use C<doset 'url_enabled', 'off'> for that
304 purpose, but I provide this variable so you can conveniently localize
305 it.
306  
307 =cut
308  
309 0 ▶sub main::hook_url_print {
Subroutine hook_url_print: not called CC=3 SCAR=22.8
310 0 ▶ return unless $Enabled;
Branch: 1 branch
BranchTrueFalseTotal
unless $Enabled 0 0 0
311 0 ▶ return if $Inhibit;
Branch: 1 branch
BranchTrueFalseTotal
if $Sirc::URL::Inhibit 0 0 0
312 0 local $Inhibit = 1;
313  
314 0 ▶ my $callback = sub {
Subroutine __ANON__: not called CC=6 SCAR=37.4
315 0 my ($url, $orig_url) = @_;
316 0 ▶ if ($Count >= 0 && $Url[$Count] eq $url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Count >= 0 and $Url[$Count] eq $url) { } 0 0 0
Condition0%0/3
$Count >= 0 and $Url[$Count] eq $url
!ll&&!rl&&r
000
MC/DC0%0/2
$Count >= 0 and $Url[$Count] eq $url
$Count >= 0 $Url[$Count] eq $url
Truth table0%0/3
A and B
A= $Count >= 0B= $Url[$Count] eq $url
ABresult
0X0
100
111
317 # don't add it, it's the same as the last one
318 }
319 else {
320 0 $Count++;
321 0 ▶ $Count = 0 if $Max >= 0 && $Count > $Max;
Branch: 1 branch
BranchTrueFalseTotal
if $Max >= 0 and $Count > $Max 0 0 0
Condition0%0/3
$Max >= 0 and $Count > $Max
!ll&&!rl&&r
000
MC/DC0%0/2
$Max >= 0 and $Count > $Max
$Max >= 0 $Count > $Max
Truth table0%0/3
A and B
A= $Max >= 0B= $Count > $Max
ABresult
0X0
100
111
322 0 $Url[$Count] = '' . $url; # stringify to lose objectness
323 }
324 0 ▶ if (ref $Format) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $Format) { } 0 0 0
325 0 return $Format->($orig_url, $Count, $url);
326 }
327 else {
328 0 return sprintf $Format, $orig_url, $Count, $url;
329 }
330 0 };
331  
332 # XXX cache the finder
333 0 ▶ my $finder = do { no strict 'refs'; $Module->new($callback) };
Subroutine BEGIN: called CC=1 SCAR=0.6
334 0 $finder->find(\$_[0]);
335 }
336 1 addhook 'print', 'url_print';
337  
338 0 ▶sub main::cmd_url {
Subroutine cmd_url: not called CC=8 SCAR=42.8
339 0 my (@spec, $carped);
340  
341 0 @spec = split ' ', $::args;
342 0 ▶ @spec = ($Count) if !@spec;
Branch: 1 branch
BranchTrueFalseTotal
unless @spec 0 0 0
343  
344 0 for my $spec (@spec) {
345 0 ▶ if ($spec =~ /^-?\d+$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /^-?\d+$/) { } 0 0 0
346 # Numbers refer to URLs I've noted.
347 0 ▶ if ($Count == -1) {
Branch: 2 branches
BranchTrueFalseTotal
if ($Count == -1) { } 0 0 0
elsif ($spec < 0 or $spec > @Url) { } 0 0 0
348 0 ▶ tell_question "No URLs have been noted" unless $carped++;
Branch: 1 branch
BranchTrueFalseTotal
unless $carped++ 0 0 0
349 0 next;
350 }
351 ▶ elsif ($spec < 0 || $spec > @Url) {
Condition0%0/3
$spec < 0 or $spec > @Url
l!l&&r!l&&!r
000
MC/DC0%0/2
$spec < 0 or $spec > @Url
$spec < 0 $spec > @Url
Truth table0%0/3
A or B
A= $spec < 0B= $spec > @Url
ABresult
1X1
011
000
352 0 tell_question "URL number `$spec' is out of range";
353 0 next;
354 }
355 else {
356 0 browse_url $Url[$spec];
357 }
358 }
359 else {
360 # Non-numbers are themselves URLs.
361 0 browse_url $spec;
362 }
363 }
364 }
365 1 addcmd 'url';
366  
367 0 ▶sub main::cmd_urls {
Subroutine cmd_urls: not called CC=10 SCAR=47.0
368 0 my @arg = split ' ', $::args;
369  
370 0 ▶ if (!$Count) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($Count) 0 0 0
371 0 tell_question "No URLs noted";
372 0 return;
373 }
374  
375 0 my @out = (0..$#Url);
376 0 ▶ unshift @out, splice @out, $Count + 1 if $Count < $#Url; # oldest first
Branch: 1 branch
BranchTrueFalseTotal
if $Count < $#Url 0 0 0
377 0 ▶ if (@arg == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (@arg == 0) { } 0 0 0
elsif (@arg == 1) { } 0 0 0
378 # output everything
379 }
380 elsif (@arg == 1) {
381 0 my $n = shift @arg;
382 0 ▶ if ($n !~ /^-?\d+$/) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $n =~ /^-?\d+$/) { } 0 0 0
elsif ($n < 0) { } 0 0 0
383 0 tell_question "Argument must be numeric";
384 0 return;
385 }
386 elsif ($n < 0) {
387 0 $n = -$n;
388 0 ▶ splice @out, 0, @out - $n if $n < @out;
Branch: 1 branch
BranchTrueFalseTotal
if $n < @out 0 0 0
389 }
390 else {
391 0 ▶ if ($n > $#Url) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n > $#Url) { } 0 0 0
392 0 tell_question "URL $n hasn't been noted yet";
393 0 return;
394 }
395 else {
396 0 @out = ($n);
397 }
398 }
399 }
400 else {
401 0 tell_question "0 or 1 arg expected";
402 0 return;
403 }
404  
405 0 local $Inhibit = 1;
406 0 xtell sprintf "%3d. %s\n", $_, $Url[$_] for @out;
407 }
408 1 addcmd 'urls';
409  
410 1 1
411  
412 __END__
« Util.pm ↑ Summary LimitMan.pm »