880dfb933f6125a3d9494783765932e19616a830
[xscreensaver] / hacks / webcollage
1 #!/usr/local/bin/perl5 -w
2 #
3 # webcollage, Copyright (c) 1999 by Jamie Zawinski <jwz@jwz.org>
4 # This program decorates the screen with random images from the web.
5 # One satisfied customer described it as "a nonstop pop culture brainbath."
6 #
7 # Permission to use, copy, modify, distribute, and sell this software and its
8 # documentation for any purpose is hereby granted without fee, provided that
9 # the above copyright notice appear in all copies and that both that
10 # copyright notice and this permission notice appear in supporting
11 # documentation.  No representations are made about the suitability of this
12 # software for any purpose.  It is provided "as is" without express or 
13 # implied warranty.
14
15 # To run this as a display mode with xscreensaver, add this to `programs':
16 #
17 #   default-n:  webcollage -root                                        \n\
18 #   default-n:  webcollage -root -filter 'vidwhacker -stdin -stdout'    \n\
19
20 require 5;
21 #use diagnostics;
22 use strict;
23
24 use Socket;
25 require Time::Local;
26 require POSIX;
27 use Fcntl ':flock'; # import LOCK_* constants
28
29
30 my $version = q{ $Revision: 1.44 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
31 my $copyright = "WebCollage $version, Copyright (c) 1999" .
32     " Jamie Zawinski <jwz\@jwz.org>\n" .
33     "            http://www.jwz.org/xscreensaver/\n";
34
35 my $argv0 = $0;
36 my $progname = $argv0; $progname =~ s@.*/@@g;
37
38 my $random_redirector = "http://random.yahoo.com/bin/ryl";
39 my $image_randomizer_1 = "http://www.altavista.com/query" .
40                          "?mmdo=3" .
41                          "&nbq=12" .
42                          "&stype=simage" .
43                          "&iclr=1" .
44                          "&ibw=1" .
45                          "&iexc=1" .
46                          "&what=web" .
47                          "&q=";
48 my $image_randomizer_2 = "http://www.hotbot.com/?clickSrc=search" .
49                          "&submit=SEARCH&SM=SC&LG=any" .
50                          "&AM0=MC&AT0=words&AW0=" .
51                          "&AM1=MN&AT1=words&AW1=" .
52                          "&savenummod=2&date=within" .
53                          "&DV=0&DR=newer&DM=1&DD=1&DY=99&FVI=1&FS=&RD=RG" .
54                          "&RG=all&Domain=&PS=A&PD=&STEM=1&DC=50&DE=0&_v=2" .
55                          "&OPs=MDRTP&NUMMOD=2" .
56                          "&MT=";
57 my $image_randomizer_3 = "http://www.altavista.com/cgi-bin/query?pg=q" .
58                          "&text=yes&kl=XX&stype=stext&q=";
59
60 # I guess Photopoint got wise to me, because now they are doing error
61 # checking on the user ("u=") and album ("a=") parameters.  Oh well.
62 #
63 #my $photo_randomizer   = "http://albums.photopoint.com/j/View?u=1&a=1&p=";
64 #my $photo_randomizer_lo = 10000001;
65 #my $photo_randomizer_hi = 12400000;
66
67 my $image_ppm   = ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp") . "/webcollage." . $$;
68 my $image_tmp1  = $image_ppm . "-1";
69 my $image_tmp2  = $image_ppm . "-2";
70
71 my $img_width;            # size of the image being generated.
72 my $img_height;
73
74 my $http_proxy = undef;
75 my $http_timeout = 30;
76 my $cvt_timeout = 10;
77
78 # programs we can use to write to the root window (tried in ascending order.)
79 my $ppm_to_root_window_cmd_1 = "xloadimage -onroot -quiet %%PPM%%";
80 my $ppm_to_root_window_cmd_2 = "xli -quiet -onroot -center" .
81                                " -border black %%PPM%%";
82 my $ppm_to_root_window_cmd_3 = "xv -root -rmode 5 -viewonly" .
83                                " +noresetroot %%PPM%% -quit";
84
85 my $ppm_to_root_window_cmd = undef;     # initialized by x_output()
86
87 my $filter_cmd = undef;
88 my $post_filter_cmd = undef;
89 my $background = undef;
90 my $no_output_p = 0;
91 my $urls_only_p = 0;
92 my $delay = 0;
93
94 my $wordlist = "/usr/dict/words";
95
96 if (!-r $wordlist) {
97     $wordlist = "/usr/share/lib/dict/words";    # irix
98 }
99 die "$wordlist doesn't exist!\n" unless (-r $wordlist);
100
101
102 my $min_width = 50;
103 my $min_height = 50;
104 my $min_ratio = 1/5;
105
106 my $verbose = 0;
107
108 my %rejected_urls;
109 my @tripwire_words = ("aberrate", "abode", "amorphous", "antioch",
110                       "arrhenius", "arteriole", "blanket", "brainchild",
111                       "burdensome", "carnival", "cherub", "chord", "clever",
112                       "dedicate", "dilogarithm", "dolan", "dryden",
113                       "eggplant");
114
115
116
117
118 ##############################################################################
119 #
120 # Retrieving URLs
121 #
122 ##############################################################################
123
124 # returns three values: the HTTP response line; the document headers;
125 # and the document body.
126 #
127 sub get_document_1 {
128     my ( $url, $referer, $timeout ) = @_;
129
130     if (!defined($timeout)) { $timeout = $http_timeout; }
131     if ($timeout <= 0) { return (); }
132     if ($timeout > $http_timeout) { $timeout = $http_timeout; }
133
134     if ( $verbose > 3 ) {
135         print STDERR "$progname: get_document_1 $url " .
136             ($referer ? $referer : "") . "\n";
137     }
138
139     my($url_proto, $dummy, $serverstring, $path) = split(/\//, $url, 4);
140     if (! ($url_proto && $url_proto =~ m/^http:$/i)) {
141         if ($verbose) { print STDERR "$progname: not an HTTP URL: $url\n"; }
142         return ();
143     }
144
145     $path = "" unless $path;
146
147     my($them,$port) = split(/:/, $serverstring);
148     $port = 80 unless $port;
149
150     my $them2 = $them;
151     my $port2 = $port;
152     if ($http_proxy) {
153         $serverstring = $http_proxy if $http_proxy;
154         ($them2,$port2) = split(/:/, $serverstring);
155         $port2 = 80 unless $port2;
156     }
157
158     my ($remote, $iaddr, $paddr, $proto, $line);
159     $remote = $them2;
160     if ($port2 =~ /\D/) { $port2 = getservbyname($port2, 'tcp') }
161     return unless $port2;
162     $iaddr   = inet_aton($remote) || return;
163     $paddr   = sockaddr_in($port2, $iaddr);
164
165
166     my $head = "";
167     my $body = "";
168
169     @_ =
170     eval {
171         local $SIG{ALRM}  = sub {
172             if ($verbose > 0) {
173                 print STDERR "$progname: timed out ($timeout) for $url\n";
174             }
175             die "alarm\n"
176             };
177         alarm $timeout;
178
179         $proto   = getprotobyname('tcp');
180         if (!socket(S, PF_INET, SOCK_STREAM, $proto)) {
181             print STDERR "$progname: socket: $!\n" if ($verbose);
182             return;
183         }
184         if (!connect(S, $paddr)) {
185             print STDERR "$progname: connect($serverstring): $!\n"
186                 if ($verbose);
187             return;
188         }
189
190         select(S); $| = 1; select(STDOUT);
191
192         my $cookie;
193         if ($remote =~ m/\baltavista\.com$/i) {
194             # kludge to tell the various altavista sites to be uncensored.
195             $cookie = "AV_ALL=1";
196         }
197
198         print S ("GET " . ($http_proxy ? $url : "/$path") . " HTTP/1.0\r\n" .
199                  "Host: $them\r\n" .
200                  "User-Agent: $progname/$version\r\n" .
201                  ($referer ? "Referer: $referer\r\n" : "") .
202                  ($cookie ? "Cookie: $cookie\r\n" : "") .
203                  "\r\n");
204         my $http = <S>;
205
206         while (<S>) {
207             $head .= $_;
208             last if m@^[\r\n]@;
209         }
210         while (<S>) {
211             $body .= $_;
212         }
213
214         close S;
215
216         if ( $verbose > 3 ) {
217             print STDERR "$progname:    ==> $http\n";
218         }
219
220         return ( $http, $head, $body );
221     };
222     die if ($@ && $@ ne "alarm\n");       # propagate errors
223     if ($@) {
224         # timed out
225         $head = undef;
226         $body = undef;
227         return ();
228     } else {
229         # didn't
230         alarm 0;
231         return @_;
232     }
233 }
234
235
236 # returns two values: the document headers; and the document body.
237 # if the given URL did a redirect, returns the redirected-to document.
238 #
239 sub get_document {
240     my ( $url, $referer, $timeout ) = @_;
241     my $start = time;
242
243     my $orig_url = $url;
244     my $loop_count = 0;
245     my $max_loop_count = 4;
246
247     do {
248         if (defined($timeout) && $timeout <= 0) { return (); }
249
250         my ( $http, $head, $body ) = get_document_1 ($url, $referer, $timeout);
251
252         if (defined ($timeout)) {
253             my $now = time;
254             my $elapsed = $now - $start;
255             $timeout -= $elapsed;
256             $start = $now;
257         }
258
259         return () if ( ! $body );
260
261         if ( $http =~ m@HTTP/[0-9.]+ 30[23]@ ) {
262             $_ = $head;
263             my ( $location ) = m@^location:[ \t]*(.*)$@im;
264             if ( $location ) {
265                 $location =~ s/[\r\n]$//;
266
267                 if ( $verbose > 3 ) {
268                     print STDERR "$progname: redirect from " .
269                         "$url to $location\n";
270                 }
271                 $referer = $url;
272                 $url = $location;
273
274                 if ($url =~ m@^/@) {
275                     $referer =~ m@^(http://[^/]+)@i;
276                     $url = $1 . $url;
277                 } elsif (! ($url =~ m@^[a-z]+:@i)) {
278                     $_ = $referer;
279                     s@[^/]+$@@g if m@^http://[^/]+/@i;
280                     $_ .= "/" if m@^http://[^/]+$@i;
281                     $url = $_ . $url;
282                 }
283
284             } else {
285                 return ( $url, $body );
286             }
287
288             if ($loop_count++ > $max_loop_count) {
289                 if ( $verbose > 1 ) {
290                     print STDERR "$progname: too many redirects " .
291                         "($max_loop_count) from $orig_url\n";
292                 }
293                 $body = undef;
294                 return ();
295             }
296
297         } elsif ( $http =~ m@HTTP/[0-9.]+ [4-9][0-9][0-9]@ ) {
298             # http errors -- return nothing.
299             $body = undef;
300             return ();
301
302         } else {
303
304             return ( $url, $body );
305         }
306
307     } while (1);
308 }
309
310
311 # given a URL and the body text at that URL, selects and returns a random
312 # image from it.  returns () if no suitable images found.
313 #
314 sub pick_image_from_body {
315     my ( $url, $body ) = @_;
316
317     my $base = $url;
318     $_ = $url;
319
320     # if there's at least one slash after the host, take off the last
321     # pathname component
322     if ( m@^http://[^/]+/@io ) {
323         $base =~ s@[^/]+$@@go;
324     }
325
326     # if there are no slashes after the host at all, put one on the end.
327     if ( m@^http://[^/]+$@io ) {
328         $base .= "/";
329     }
330
331     if ( $verbose > 3 ) {
332         print STDERR "$progname: base is $base\n";
333     }
334
335
336     $_ = $body;
337
338     # strip out newlines, compress whitespace
339     s/[\r\n\t ]+/ /go;
340
341     # nuke comments
342     s/<!--.*?-->//go;
343
344
345     # There are certain web sites that list huge numbers of dictionary
346     # words in their bodies or in their <META NAME=KEYWORDS> tags (surprise!
347     # Porn sites tend not to be reputable!)
348     #
349     # I do not want webcollage to filter on content: I want it to select
350     # randomly from the set of images on the web.  All the logic here for
351     # rejecting some images is really a set of heuristics for rejecting
352     # images that are not really images: for rejecting *text* that is in
353     # GIF/JPEG form.  I don't want text, I want pictures, and I want the
354     # content of the pictures to be randomly selected from among all the
355     # available content.
356     #
357     # So, filtering out "dirty" pictures by looking for "dirty" keywords
358     # would be wrong: dirty pictures exist, like it or not, so webcollage
359     # should be able to select them.
360     #
361     # However, picking a random URL is a hard thing to do.  The mechanism I'm
362     # using is to search for a selection of random words.  This is not
363     # perfect, but works ok most of the time.  The way it breaks down is when
364     # some URLs get precedence because their pages list *every word* as
365     # related -- those URLs come up more often than others.
366     #
367     # So, after we've retrieved a URL, if it has too many keywords, reject
368     # it.  We reject it not on the basis of what those keywords are, but on
369     # the basis that by having so many, the page has gotten an unfair
370     # advantage against our randomizer.
371     #
372     my $trip_count = 0;
373     foreach my $trip (@tripwire_words) {
374         $trip_count++ if m/$trip/i;
375     }
376     if ($trip_count >= $#tripwire_words - 2) {
377         if ($verbose > 1) {
378             print STDERR "$progname: there is probably a dictionary in" .
379                 " \"$url\": rejecting.\n";
380         }
381         $rejected_urls{$url} = -1;
382         $body = undef;
383         $_ = undef;
384         return ();
385     }
386
387
388     my @urls;
389     my %unique_urls;
390
391     foreach (split(/ *</)) {
392         if ( m/^meta /i ) {
393
394             # Likewise, reject any web pages that have a KEYWORDS meta tag
395             # that is too long.
396             #
397             if (m/name ?= ?\"?keywords\"?/i &&
398                 m/content ?= ?\"([^\"]+)\"/) {
399                 my $L = length($1);
400                 if ($L > 1000) {
401                     if ($verbose > 1) {
402                         print STDERR "$progname: keywords of" .
403                             " length $L in $url: rejecting.\n";
404                     }
405                     $rejected_urls{$url} = $L;
406                     $body = undef;
407                     $_ = undef;
408                     return ();
409                 } elsif ( $verbose > 2 ) {
410                     print STDERR "$progname: keywords of length $L" .
411                         " in $url (ok.)\n";
412                 }
413             }
414
415         } elsif ( m/^(img|a) .*(src|href) ?= ?\"? ?(.*?)[ >\"]/io ) {
416
417             my $was_inline = ( "$1" eq "a" || "$1" eq "A" );
418             my $link = $3;
419             my ( $width )  = m/width ?=[ \"]*(\d+)/oi;
420             my ( $height ) = m/height ?=[ \"]*(\d+)/oi;
421             $_ = $link;
422
423             if ( m@^/@o ) {
424                 my $site;
425                 ( $site = $base ) =~ s@^(http://[^/]*).*@$1@gio;
426                 $_ = "$site$link";
427             } elsif ( ! m@^[^/:?]+:@ ) {
428                 $_ = "$base$link";
429                 s@/\./@/@g;
430                 while (s@/\.\./@/@g) {
431                 }
432             }
433
434             # skip non-http
435             if ( ! m@^http://@io ) {
436                 next;
437             }
438
439             # skip non-image
440             if ( ! m@[.](gif|jpg|jpeg|pjpg|pjpeg)$@io ) {
441                 next;
442             }
443
444             # skip really short or really narrow images
445             if ( $width && $width < $min_width) {
446                 if ( $verbose > 2 ) {
447                     if (!$height) { $height = "?"; }
448                     print STDERR "$progname: skip narrow image " .
449                         "$_ (${width}x$height)\n";
450                 }
451                 next;
452             }
453
454             if ( $height && $height < $min_height) {
455                 if ( $verbose > 2 ) {
456                     if (!$width) { $width = "?"; }
457                     print STDERR "$progname: skip short image " .
458                         "$_ (${width}x$height)\n";
459                 }
460                 next;
461             }
462
463             # skip images with ratios that make them look like banners.
464             if ( $min_ratio && $width && $height &&
465                 ($width * $min_ratio ) > $height ) {
466                 if ( $verbose > 2 ) {
467                     if (!$height) { $height = "?"; }
468                     print STDERR "$progname: skip bad ratio " .
469                         "$_ (${width}x$height)\n";
470                 }
471                 next;
472             }
473
474             my $url = $_;
475
476             if ( $unique_urls{$url} ) {
477                 if ( $verbose > 2 ) {
478                     print STDERR "$progname: skip duplicate image $_\n";
479                 }
480                 next;
481             }
482
483             if ( $verbose > 2 ) {
484                 print STDERR "$progname: got $url" . 
485                     ($width && $height ? " (${width}x${height})" : "") .
486                     ($was_inline ? " (inline)" : "") . "\n";
487             }
488
489             $urls[++$#urls] = $url;
490             $unique_urls{$url}++;
491
492             # jpegs are preferable to gifs.
493             $_ = $url;
494             if ( ! m@[.]gif$@io ) {
495                 $urls[++$#urls] = $url;
496             }
497
498             # pointers to images are preferable to inlined images.
499             if ( ! $was_inline ) {
500                 $urls[++$#urls] = $url;
501                 $urls[++$#urls] = $url;
502             }
503         }
504     }
505
506     $_ = undef;
507     $body = undef;
508
509     if ( $#urls == 0 ) {
510         if ( $verbose > 2 ) {
511             print STDERR "$progname: no images on $base\n";
512         }
513         return ();
514     }
515
516     return () if ( $#urls < 1 );
517
518     # pick a random element of the table
519     my $i = ((rand() * 99999) % $#urls);
520     $url = $urls[$i];
521
522     if ( $verbose > 2 ) {
523         print STDERR "$progname: picked $url\n";
524     }
525
526     return $url;
527 }
528
529
530 # Using the URL-randomizer, picks a random image on a random page, and
531 # returns two URLs: the page containing the image, and the image.
532 # Returns () if nothing found this time.
533 #
534 sub pick_from_url_randomizer {
535     my ( $timeout ) = @_;
536
537     if ( $verbose > 3 ) {
538         print STDERR "\n\n$progname: picking from $random_redirector...\n\n";
539     }
540
541     my ( $base, $body ) = get_document ($random_redirector, undef, $timeout);
542
543     if (!$base || !$body) {
544         $body = undef;
545         return;
546     }
547     my $img = pick_image_from_body ($base, $body);
548     $body = undef;
549
550     if ($img) {
551         return ($base, $img, "yahoo");
552     } else {
553         return ();
554     }
555 }
556
557
558 sub random_word {
559     
560     my $word = 0;
561     if (open (IN, "<$wordlist")) {
562         my $size = (stat(IN))[7];
563         my $pos = rand $size;
564         if (seek (IN, $pos, 0)) {
565             $word = <IN>;   # toss partial line
566             $word = <IN>;   # keep next line
567         }
568         if (!$word) {
569           seek( IN, 0, 0 );
570           $word = <IN>;
571         }
572         close (IN);
573     }
574
575     return 0 if (!$word);
576
577     $word =~ s/^[ \t\n\r]+//;
578     $word =~ s/[ \t\n\r]+$//;
579     $word =~ s/ys$/y/;
580     $word =~ s/ally$//;
581     $word =~ s/ly$//;
582     $word =~ s/ies$/y/;
583     $word =~ s/ally$/al/;
584     $word =~ s/izes$/ize/;
585     $word =~ tr/A-Z/a-z/;
586
587     if ( $word =~ s/[ \t\n\r]/\+/g ) {  # convert intra-word spaces to "+".
588       $word = "\%22$word\%22";          # And put quotes (%22) around it.
589     }
590
591     return $word;
592 }
593
594
595
596 # Using the image-randomizer, picks a random image on a random page, and
597 # returns two URLs: the page containing the image, and the image.
598 # Returns () if nothing found this time.
599 #
600 sub pick_from_image_randomizer {
601     my ( $timeout, $which ) = @_;
602
603     my $words = random_word;
604     $words .= "%20" . random_word;
605     $words .= "%20" . random_word;
606     $words .= "%20" . random_word;
607     $words .= "%20" . random_word;
608
609     my $search_url = ($which == 0 ? $image_randomizer_1 :
610                       $which == 1 ? $image_randomizer_2 :
611                       $image_randomizer_3) .
612         $words;
613
614     # Pick a random search-result page instead of always taking the first.
615     # This assumes there are at least 10 pages...
616     if ($which == 0) {
617         $search_url .= "&pgno=" . (int(rand(9)) + 1);
618     } elsif ($which == 2) {
619         $search_url .= "&stq=" . (10 * (int(rand(9)) + 1));
620     }
621
622     if ( $verbose > 3 ) {
623         $_ = $words; s/%20/ /g; print STDERR "$progname: search words: $_\n";
624     }
625
626     if ( $verbose > 3 ) {
627         print STDERR "\n\n$progname: picking from $search_url\n";
628     }
629
630     my $start = time;
631     my ( $base, $body ) = get_document ($search_url, undef, $timeout);
632     if (defined ($timeout)) {
633         $timeout -= (time - $start);
634         if ($timeout <= 0) {
635             $body = undef;
636             return ();
637         }
638     }
639
640     return () if (! $body);
641
642
643     my @subpages;
644     my $skipped = 0;
645
646     my $search_count = "?";
647     if ($which == 0 &&
648         $body =~ m@found (approximately |about )?(<B>)?(\d+)(</B>)? image@) {
649         $search_count = $3;
650     } elsif ($which == 1 && $body =~ m@<NOBR>((\d{1,3})(,\d{3})*)&nbsp;@i) {
651         $search_count = $1;
652     } elsif ($which == 2 && $body =~ m@found ((\d{1,3})(,\d{3})*|\d+) Web p@) {
653         $search_count = $1;
654     }
655     1 while ($search_count =~ s/^(\d+)(\d{3})/$1,$2/);
656
657     my $length = length($body);
658     my $href_count = 0;
659
660     $_ = $body;
661
662     s/Result [Pp]ages:.*$//s;            # trim off page footer
663     s/^.*?IMAGE RESULTS//s;              # trim off page header
664
665     s/[\r\n\t ]+/ /g;
666
667     s/(<A )/\n$1/gi;
668     foreach (split(/\n/)) {
669         $href_count++;
670         my ($u) = m@<A\s.*\bHREF\s*=\s*([^>]+)>@i;
671         next unless $u;
672         if ($u =~ m/^\"([^\"]*)\"/) { $u = $1; }   # quoted string
673         elsif ($u =~ m/^([^\s]*)\s/) { $u = $1; }  # or token
674
675         if ($which == 1) {
676             # Kludge to decode HotBot pages
677             next unless ($u =~ m@/director\.asp\?target=(http%3A[^&>]+)@);
678             $u = url_decode($1);
679         }
680
681         next unless ($u =~ m@^http://@i);  # skip non-http and relative urls.
682
683         next if ($u =~ m@[/.]altavista\.com@i);  # skip altavista builtins
684         next if ($u =~ m@[/.]av\.com@i);
685         next if ($u =~ m@[/.]virage\.com@i);
686         next if ($u =~ m@[/.]photoloft\.com@i);
687         next if ($u =~ m@[/.]shopping\.com@i);
688         next if ($u =~ m@[/.]thetrip\.com@i);
689         next if ($u =~ m@[/.]cmgi\.com@i);
690         next if ($u =~ m@[/.]intelihealth\.com@i);
691         next if ($u =~ m@[/.]wildweb\.com@i);
692         next if ($u =~ m@[/.]digital\.com@i);
693         next if ($u =~ m@[/.]doubleclick\.net@i);
694
695         if ($which == 0 && $u =~ m@[/.]corbis\.com@) {
696             $skipped = 1;
697             if ( $verbose > 3 ) {
698                 print STDERR "$progname: skipping corbis URL: $u\n";
699             }
700             next;
701
702         } elsif ( $rejected_urls{$u} ) {
703             if ( $verbose > 3 ) {
704                 my $L = $rejected_urls{$u};
705                 print STDERR "$progname: pre-rejecting sub-page: $u\n";
706             }
707             next;
708
709         } elsif ( $verbose > 3 ) {
710             print STDERR "$progname: sub-page: $u\n";
711         }
712
713         $subpages[++$#subpages] = $u;
714     }
715
716     if ( $#subpages < 0 ) {
717         if (!$skipped && $verbose > 1) {
718             print STDERR "$progname: found nothing on $base " .
719                 "($length bytes, $href_count links).\n";
720         }
721         $body = undef;
722         $_ = undef;
723         return ();
724     }
725
726     # pick a random element of the table
727     my $i = ((rand() * 99999) % ($#subpages + 1));
728     my $subpage = $subpages[$i];
729
730     if ( $verbose > 3 ) {
731         print STDERR "$progname: picked page $subpage\n";
732     }
733
734
735     $body = undef;
736     $_ = undef;
737
738     my ( $base2, $body2 ) = get_document ($subpage, $base, $timeout);
739
740     if (!$base2 || !$body2) {
741         $body2 = undef;
742         return ();
743     }
744
745     my $img = pick_image_from_body ($base2, $body2);
746     $body2 = undef;
747
748     if ($img) {
749         return ($base2, $img,
750                 ($which == 0 ? "imagevista" :
751                  $which == 1 ? "hotbot" : "altavista") .
752                 "/$search_count");
753     } else {
754         return ();
755     }
756 }
757
758
759 # Using the photo site, generate a random URL that will hopefully point
760 # to an image.  Returns two URLs, both of which are the URL of the image.
761 # Returns () if nothing found this time.
762 #
763 #sub pick_from_photo_randomizer {
764 #    my ( $timeout ) = @_;
765 #    my $n = ($photo_randomizer_lo +
766 #             int(rand() * ($photo_randomizer_hi - $photo_randomizer_lo)));
767 #    my $url = $photo_randomizer . $n;
768 #    return ( $url, $url, "photopoint" );
769 #}
770
771
772 # Picks a random image on a random page, and returns two URLs:
773 # the page containing the image, and the image. 
774 # Returns () if nothing found this time.
775 # Uses the url-randomizer 1 time in 5, else the image randomizer.
776 #
777 my $total_0 = 0;
778 my $total_1 = 0;
779 my $total_2 = 0;
780 my $total_3 = 0;
781 my $total_4 = 0;
782 my $count_0 = 0;
783 my $count_1 = 0;
784 my $count_2 = 0;
785 my $count_3 = 0;
786 my $count_4 = 0;
787
788 sub pick_image {
789     my ( $timeout ) = @_;
790
791     my $r = int(rand(100));
792     my ($base, $img, $source, $total, $count);
793
794     if ($r < 20) {
795         ($base, $img, $source) = pick_from_url_randomizer ($timeout);
796         $total = ++$total_0;
797         $count = ++$count_0 if $img;
798
799     } elsif ($r < 60) {
800         ($base, $img, $source) = pick_from_image_randomizer ($timeout, 0);
801         $total = ++$total_1;
802         $count = ++$count_1 if $img;
803
804 #    } elsif ($r < 70) {
805 #        ($base, $img, $source) = pick_from_photo_randomizer ($timeout);
806 #        $total = ++$total_4;
807 #        $count = ++$count_4 if $img;
808
809 #    } elsif ($r < 80) {
810 #        # HotBot sucks: 98% of the time, it says "no pages match your
811 #        # search", and then if I load the URL again by hand, it works.
812 #        # I don't understand what's going wrong here, but we're not getting
813 #        # any good data back from them, so forget it for now.
814 #
815 #        ($base, $img, $source) = pick_from_image_randomizer ($timeout, 1);
816 #        $total = ++$total_2;
817 #        $count = ++$count_2 if $img;
818
819     } else {
820         ($base, $img, $source) = pick_from_image_randomizer ($timeout, 2);
821         $total = ++$total_3;
822         $count = ++$count_3 if $img;
823     }
824
825     if ($source && $total > 0) {
826         $source .= " " . int(($count / $total) * 100) . "%";
827     }
828     return ($base, $img, $source);
829 }
830
831
832 # Does %-decoding.
833 #
834 sub url_decode {
835     ($_) = @_;
836     tr/+/ /;
837     s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
838     return $_;
839 }
840
841
842 # Given the raw body of a GIF document, returns the dimensions of the image.
843 #
844 sub gif_size {
845     my ($body) = @_;
846     my $type = substr($body, 0, 6);
847     my $s;
848     return () unless ($type =~ /GIF8[7,9]a/);
849     $s = substr ($body, 6, 10);
850     my ($a,$b,$c,$d) = unpack ("C"x4, $s);
851     return (($b<<8|$a), ($d<<8|$c));
852 }
853
854 # Given the raw body of a JPEG document, returns the dimensions of the image.
855 #
856 sub jpeg_size {
857     my ($body) = @_;
858     my $i = 0;
859     my $L = length($body);
860     
861     my $c1 = substr($body, $i, 1); $i++;
862     my $c2 = substr($body, $i, 1); $i++;
863     return () unless (ord($c1) == 0xFF && ord($c2) == 0xD8);
864
865     my $ch = "0";
866     while (ord($ch) != 0xDA && $i < $L) {
867         # Find next marker, beginning with 0xFF.
868         while (ord($ch) != 0xFF) {
869             $ch = substr($body, $i, 1); $i++;
870         }
871         # markers can be padded with any number of 0xFF.
872         while (ord($ch) == 0xFF) {
873             $ch = substr($body, $i, 1); $i++;
874         }
875
876         # $ch contains the value of the marker.
877         my $marker = ord($ch);
878
879         if (($marker >= 0xC0) &&
880             ($marker <= 0xCF) &&
881             ($marker != 0xC4) &&
882             ($marker != 0xCC)) {  # it's a SOFn marker
883             $i += 3;
884             my $s = substr($body, $i, 4); $i += 4;
885             my ($a,$b,$c,$d) = unpack("C"x4, $s);
886             return (($c<<8|$d), ($a<<8|$b));
887
888         } else {
889             # We must skip variables, since FFs in variable names aren't
890             # valid JPEG markers.
891             my $s = substr($body, $i, 2); $i += 2;
892             my ($c1, $c2) = unpack ("C"x2, $s); 
893             my $length = ($c1 << 8) | $c2;
894             return () if ($length < 2);
895             $i += $length-2;
896         }
897     }
898     return ();
899 }
900
901 # Given the raw body of a GIF or JPEG document, returns the dimensions of
902 # the image.
903 #
904 sub image_size {
905     my ($body) = @_;
906     my ($w, $h) = gif_size ($body);
907     if ($w && $h) { return ($w, $h); }
908     return jpeg_size ($body);
909 }
910
911
912 # returns the full path of the named program, or undef.
913 #
914 sub which {
915     my ($prog) = @_;
916     foreach (split (/:/, $ENV{PATH})) {
917         if (-x "$_/$prog") {
918             return $prog;
919         }
920     }
921     return undef;
922 }
923
924
925 # Like rand(), but chooses numbers with a bell curve distribution.
926 sub bellrand {
927     ($_) = @_;
928     $_ = 1.0 unless defined($_);
929     $_ /= 3.0;
930     return (rand($_) + rand($_) + rand($_));
931 }
932
933
934 ##############################################################################
935 #
936 # Generating a list of urls only
937 #
938 ##############################################################################
939
940 sub url_only_output {
941     do {
942         my ($base, $img) = pick_image;
943         if ($img) {
944             $base =~ s/ /%20/g;
945             $img  =~ s/ /%20/g;
946             print "$img $base\n";
947         }
948     } while (1);
949 }
950
951 ##############################################################################
952 #
953 # Running as an xscreensaver module
954 #
955 ##############################################################################
956
957 sub x_cleanup {
958     my ($sig) = @_;
959     if ($verbose > 0) { print STDERR "$progname: caught signal $sig.\n"; }
960     unlink $image_ppm, $image_tmp1, $image_tmp2;
961     exit 1;
962 }
963
964
965 # Like system, but prints status about exit codes, and kills this process
966 # with whatever signal killed the sub-process, if any.
967 #
968 sub nontrapping_system {
969     $! = 0;
970     
971     if ($verbose > 1) {
972         $_ = join(" ", @_);
973         s/\"[^\"]+\"/\"...\"/g;
974         print STDERR "$progname: executing \"$_\"\n";
975     }
976
977     my $rc = system @_;
978
979     if ($rc == 0) {
980         if ($verbose > 1) {
981             print STDERR "$progname: subproc exited normally.\n";
982         }
983     } elsif (($rc & 0xff) == 0) {
984         $rc >>= 8;
985         if ($verbose) {
986             print "$progname: subproc exited with status $rc.\n";
987         }
988     } else {
989         if ($rc & 0x80) {
990             if ($verbose) {
991                 print "$progname: subproc dumped core.\n";
992             }
993             $rc &= ~0x80;
994         }
995         if ($verbose) {
996             print "$progname: subproc died with signal $rc.\n";
997         }
998         # die that way ourselves.
999         kill $rc, $$;
1000     }
1001
1002     return $rc;
1003 }
1004
1005
1006 # Given the URL of a GIF or JPEG image, and the body of that image, writes a
1007 # PPM to the given output file.  Returns the width/height of the image if 
1008 # successful.
1009 #
1010 sub image_to_pnm {
1011     my ($url, $body, $output) = @_;
1012     my ($cmd, $cmd2, $w, $h);
1013
1014     if ((@_ = gif_size ($body))) {
1015         ($w, $h) = @_;
1016         $cmd = "giftopnm";
1017     } elsif ((@_ = jpeg_size ($body))) {
1018         ($w, $h) = @_;
1019         $cmd = "djpeg";
1020     } else {
1021         return ();
1022     }
1023
1024     $cmd2 = "exec $cmd";        # yes, this really is necessary.  if we don't
1025                                 # do this, the process doesn't die properly.
1026     if ($verbose == 0) {
1027         $cmd2 .= " 2>/dev/null";
1028     }
1029
1030     # There exist corrupted GIF and JPEG files that can make giftopnm and
1031     # djpeg lose their minds and go into a loop.  So this gives those programs
1032     # a small timeout -- if they don't complete in time, kill them.
1033     #
1034     my $pid;
1035     @_ = eval {
1036         my $timed_out;
1037
1038         local $SIG{ALRM}  = sub {
1039             if ($verbose > 0) {
1040                 print STDERR "$progname: timed out ($cvt_timeout) for " .
1041                     "$cmd on \"$url\" in pid $pid\n";
1042             }
1043             kill ('TERM', $pid) if ($pid);
1044             $timed_out = 1;
1045             $body = undef;
1046         };
1047
1048         if (($pid = open(PIPE, "| $cmd2 > $output"))) {
1049             $timed_out = 0;
1050             alarm $cvt_timeout;
1051             print PIPE $body;
1052             $body = undef;
1053             close PIPE;
1054
1055             if ($verbose > 3) { print STDERR "$progname: awaiting $pid\n"; }
1056             waitpid ($pid, 0);
1057             if ($verbose > 3) { print STDERR "$progname: $pid completed\n"; }
1058
1059
1060             my $size = (stat($output))[7];
1061             if ($size < 5) {
1062                 if ($verbose) {
1063                     print STDERR "$progname: $cmd on ${w}x$h \"$url\" failed" .
1064                         " ($size bytes)\n";
1065                 }
1066                 return ();
1067             }
1068
1069             if ($verbose > 1) {
1070                 print STDERR "$progname: created ${w}x$h $output ($cmd)\n";
1071             }
1072             return ($w, $h);
1073         } else {
1074             print STDERR "$progname: $cmd failed: $!\n";
1075             return ();
1076         }
1077     };
1078     die if ($@ && $@ ne "alarm\n");       # propagate errors
1079     if ($@) {
1080         # timed out
1081         $body = undef;
1082         return ();
1083     } else {
1084         # didn't
1085         alarm 0;
1086         $body = undef;
1087         return @_;
1088     }
1089 }
1090
1091 sub x_output {
1092
1093     my $win_cmd_1 = $ppm_to_root_window_cmd_1;
1094     my $win_cmd_2 = $ppm_to_root_window_cmd_2;
1095     my $win_cmd_3 = $ppm_to_root_window_cmd_3;
1096     $win_cmd_1 =~ s/^([^ \t\r\n]+).*$/$1/;
1097     $win_cmd_2 =~ s/^([^ \t\r\n]+).*$/$1/;
1098     $win_cmd_3 =~ s/^([^ \t\r\n]+).*$/$1/;
1099
1100     # make sure the various programs we execute exist, right up front.
1101     foreach ("ppmmake", "giftopnm", "djpeg", "pnmpaste", "pnmscale",
1102              "pnmcut") {
1103         which ($_) || die "$progname: $_ not found on \$PATH.\n";
1104     }
1105
1106     if (which($win_cmd_1)) {
1107         $ppm_to_root_window_cmd = $ppm_to_root_window_cmd_1;
1108     } elsif (which($win_cmd_2)) {
1109         $ppm_to_root_window_cmd = $ppm_to_root_window_cmd_2;
1110     } elsif (which($win_cmd_3)) {
1111         $ppm_to_root_window_cmd = $ppm_to_root_window_cmd_3;
1112      } else {
1113         die "$progname: didn't find $win_cmd_1, $win_cmd_2, or $win_cmd_3 on \$PATH.\n";
1114     }
1115
1116     $SIG{HUP}  = \&x_cleanup;
1117     $SIG{INT}  = \&x_cleanup;
1118     $SIG{QUIT} = \&x_cleanup;
1119     $SIG{ABRT} = \&x_cleanup;
1120     $SIG{KILL} = \&x_cleanup;
1121     $SIG{TERM} = \&x_cleanup;
1122
1123     # Need this so that if giftopnm dies, we don't die.
1124     $SIG{PIPE} = 'IGNORE';
1125
1126     if (!$img_width || !$img_height) {
1127         $_ = "xdpyinfo";
1128         which ($_) || die "$progname: $_ not found on \$PATH.\n";
1129         $_ = `$_`;
1130         ($img_width, $img_height) = m/dimensions: *(\d+)x(\d+) /;
1131         if (!defined($img_height)) {
1132             die "$progname: xdpyinfo failed.\n";
1133         }
1134     }
1135
1136     my $bgcolor = "#000000";
1137     my $bgimage = undef;
1138
1139     if ($background) {
1140         if ($background =~ m/^\#[0-9a-f]+$/i) {
1141             $bgcolor = $background;
1142         } elsif (-r $background) {
1143             $bgimage = $background;
1144             
1145         } elsif (! $background =~ m@^[-a-z0-9 ]+$@i) {
1146             print STDERR "$progname: not a color or readable file: " .
1147                 "$background\n";
1148             exit 1;
1149         } else {
1150             # default to assuming it's a color
1151             $bgcolor = $background;
1152         }
1153     }
1154
1155     # Create the sold-colored base image.
1156     #
1157     $_ = "ppmmake '$bgcolor' $img_width $img_height";
1158     if ($verbose > 1) {
1159         print STDERR "$progname: creating base image: $_\n";
1160     }
1161     nontrapping_system "$_ > $image_ppm";
1162
1163     # Paste the default background image in the middle of it.
1164     #
1165     if ($bgimage) {
1166         my ($iw, $ih);
1167
1168         my $body = "";
1169         local *IMG;
1170         open(IMG, "<$bgimage") || die ("couldn't open $bgimage: $!\n");
1171         my $cmd;
1172         while (<IMG>) { $body .= $_; }
1173         close (IMG);
1174         if ((@_ = gif_size ($body))) {
1175             ($iw, $ih) = @_;
1176             $cmd = "giftopnm |";
1177         } elsif ((@_ = jpeg_size ($body))) {
1178             ($iw, $ih) = @_;
1179             $cmd = "djpeg |";
1180         } elsif ($body =~ "^P\d\n(\d+) (\d+)\n") {
1181             $iw = $1;
1182             $ih = $2;
1183             $cmd = "";
1184         } else {
1185             die "$progname: $bgimage is not a GIF, JPEG, or PPM.\n";
1186         }
1187
1188         my $x = int (($img_width  - $iw) / 2);
1189         my $y = int (($img_height - $ih) / 2);
1190         if ($verbose > 1) {
1191             print STDERR "$progname: pasting $bgimage (${iw}x$ih) into base ".
1192                 "image at $x,$y\n";
1193         }
1194
1195         $cmd .= "pnmpaste - $x $y $image_ppm > $image_tmp1";
1196         open (IMG, "| $cmd") || die ("running $cmd: $!\n");
1197         print IMG $body;
1198         $body = undef;
1199         close (IMG);
1200         if ($verbose > 1) {
1201             print STDERR "$progname: subproc exited normally.\n";
1202         }
1203         rename ($image_tmp1, $image_ppm) ||
1204             die ("renaming $image_tmp1 to $image_ppm: $!\n");
1205     }
1206
1207     while (1) {
1208         my ($base, $img, $source) = pick_image();
1209         if ($img) {
1210             my ($headers, $body) = get_document ($img, $base);
1211             if ($body) {
1212                 handle_image ($base, $img, $body, $source);
1213                 $body = undef;
1214             }
1215         }
1216         unlink $image_tmp1, $image_tmp2;
1217         sleep $delay;
1218     }
1219 }
1220
1221 sub handle_image {
1222     my ($base, $img, $body, $source) = @_;
1223
1224     if ($verbose > 1) {
1225         print STDERR "$progname: got $img (" . length($body) . ")\n";
1226     }
1227
1228     my ($iw, $ih) = image_to_pnm ($img, $body, $image_tmp1);
1229     $body = undef;
1230     return 0 unless ($iw && $ih);
1231
1232     my $ow = $iw;  # used only for error messages
1233     my $oh = $ih;
1234
1235     # don't just tack this onto the front of the pipeline -- we want it to
1236     # be able to change the size of the input image.
1237     #
1238     if ($filter_cmd) {
1239         if ($verbose > 1) {
1240             print STDERR "$progname: running $filter_cmd\n";
1241         }
1242
1243         my $rc = nontrapping_system "($filter_cmd) < $image_tmp1 >$image_tmp2";
1244         if ($rc != 0) {
1245             if ($verbose) {
1246                 print STDERR "$progname: failed command: \"$filter_cmd\"\n";
1247                 print STDERR "$progname: failed url: \"$img\" (${ow}x$oh)\n";
1248             }
1249             return;
1250         }
1251         rename ($image_tmp2, $image_tmp1);
1252
1253         # re-get the width/height in case the filter resized it.
1254         local *IMG;
1255         open(IMG, "<$image_tmp1") || return 0;
1256         $_ = <IMG>;
1257         $_ = <IMG>;
1258         ($iw, $ih) = m/^(\d+) (\d+)$/;
1259         close (IMG);
1260         return 0 unless ($iw && $ih);
1261     }
1262
1263     my $target_w = $img_width;
1264     my $target_h = $img_height;
1265
1266     my $cmd = "";
1267
1268
1269     # Usually scale the image to fit on the screen -- but sometimes scale it
1270     # to fit on half or a quarter of the screen.  Note that we don't merely
1271     # scale it to fit, we instead cut it in half until it fits -- that should
1272     # give a wider distribution of sizes.
1273     #
1274     if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; }
1275     if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; }
1276
1277     if ($iw > $target_w || $ih > $target_h) {
1278         while ($iw > $target_w ||
1279                $ih > $target_h) {
1280             $iw = int($iw / 2);
1281             $ih = int($ih / 2);
1282         }
1283         if ($iw <= 10 || $ih <= 10) {
1284             if ($verbose > 1) {
1285                 print STDERR "$progname: scaling to ${iw}x$ih would " .
1286                     "have been bogus.\n";
1287             }
1288             return 0;
1289         }
1290
1291         if ($verbose > 1) {
1292             print STDERR "$progname: scaling to ${iw}x$ih\n";
1293         }
1294
1295         $cmd .= " | pnmscale -xsize $iw -ysize $ih";
1296     }
1297
1298
1299     my $src = $image_tmp1;
1300
1301     my $crop_x = 0;     # the sub-rectangle of the image
1302     my $crop_y = 0;     # that we will actually paste.
1303     my $crop_w = $iw;
1304     my $crop_h = $ih;
1305
1306     # The chance that we will randomly crop out a section of an image starts
1307     # out fairly low, but goes up for images that are very large, or images
1308     # that have ratios that make them look like banners (we try to avoid
1309     # banner images entirely, but they slip through when the IMG tags didn't
1310     # have WIDTH and HEIGHT specified.)
1311     #
1312     my $crop_chance = 0.2;
1313     if ($iw > $img_width * 0.4 || $ih > $img_height * 0.4) {
1314         $crop_chance += 0.2;
1315     }
1316     if ($iw > $img_width * 0.7 || $ih > $img_height * 0.7) {
1317         $crop_chance += 0.2;
1318     }
1319     if ($min_ratio && ($iw * $min_ratio) > $ih) {
1320         $crop_chance += 0.7;
1321     }
1322
1323     if ($verbose > 2 && $crop_chance > 0.1) {
1324         print STDERR "$progname: crop chance: $crop_chance\n";
1325     }
1326
1327     if (rand() < $crop_chance) {
1328
1329         my $ow = $crop_w;
1330         my $oh = $crop_h;
1331
1332         if ($crop_w > $min_width) {
1333             # if it's a banner, select the width linearly.
1334             # otherwise, select a bell.
1335             my $r = (($min_ratio && ($iw * $min_ratio) > $ih)
1336                      ? rand()
1337                      : bellrand());
1338             $crop_w = $min_width + int ($r * ($crop_w - $min_width));
1339             $crop_x = int (rand() * ($ow - $crop_w));
1340         }
1341         if ($crop_h > $min_height) {
1342             # height always selects as a bell.
1343             $crop_h = $min_height + int (bellrand() * ($crop_h - $min_height));
1344             $crop_y = int (rand() * ($oh - $crop_h));
1345         }
1346
1347         if ($verbose > 1 &&
1348             ($crop_x != 0   || $crop_y != 0 ||
1349              $crop_w != $iw || $crop_h != $ih)) {
1350             print STDERR "$progname: randomly cropping to " .
1351                 "${crop_w}x$crop_h \@ $crop_x,$crop_y\n";
1352         }
1353     }
1354
1355     # Where the image should logically land -- this might be negative.
1356     #
1357     my $x = int((rand() * ($img_width  + $crop_w/2)) - $crop_w*3/4);
1358     my $y = int((rand() * ($img_height + $crop_h/2)) - $crop_h*3/4);
1359
1360     # if we have chosen to paste the image outside of the rectangle of the
1361     # screen, then we need to crop it.
1362     #
1363     if ($x < 0 ||
1364         $y < 0 ||
1365         $x + $crop_w > $img_width ||
1366         $y + $crop_h > $img_height) {
1367
1368         if ($verbose > 1) {
1369             print STDERR "$progname: cropping for effective paste of " .
1370                 "${crop_w}x$crop_h \@ $x,$y\n";
1371         }
1372
1373         if ($x < 0) { $crop_x -= $x; $crop_w += $x; $x = 0; }
1374         if ($y < 0) { $crop_y -= $y; $crop_h += $y; $y = 0; }
1375
1376         if ($x + $crop_w >= $img_width)  { $crop_w = $img_width  - $x - 1; }
1377         if ($y + $crop_h >= $img_height) { $crop_h = $img_height - $y - 1; }
1378     }
1379
1380     # If any cropping needs to happen, add pnmcut.
1381     #
1382     if ($crop_x != 0   || $crop_y != 0 ||
1383         $crop_w != $iw || $crop_h != $ih) {
1384         $iw = $crop_w;
1385         $ih = $crop_h;
1386         $cmd .= " | pnmcut $crop_x $crop_y $iw $ih";
1387         if ($verbose > 1) {
1388             print STDERR "$progname: cropping to ${crop_w}x$crop_h \@ " .
1389                 "$crop_x,$crop_y\n";
1390         }
1391     }
1392
1393     if ($verbose > 1) {
1394         print STDERR "$progname: pasting ${iw}x$ih \@ $x,$y in $image_ppm\n";
1395     }
1396
1397     $cmd .= " | pnmpaste - $x $y $image_ppm";
1398
1399     $cmd =~ s@^ *\| *@@;
1400     my $rc = nontrapping_system "($cmd) < $image_tmp1 > $image_tmp2";
1401
1402     if ($rc != 0) {
1403         if ($verbose) {
1404             print STDERR "$progname: failed command: \"$cmd\"\n";
1405             print STDERR "$progname: failed url: \"$img\" (${ow}x$oh)\n";
1406         }
1407         return;
1408     }
1409
1410     rename ($image_tmp2, $image_ppm) || return;
1411
1412     my $target = "$image_ppm";
1413
1414     # don't just tack this onto the end of the pipeline -- we don't want it
1415     # to end up in $image_ppm, because we don't want the results to be
1416     # cumulative.
1417     #
1418     if ($post_filter_cmd) {
1419         $target = $image_tmp1;
1420         $rc = nontrapping_system "($post_filter_cmd) < $image_ppm > $target";
1421         if ($rc != 0) {
1422             if ($verbose) {
1423                 print STDERR "$progname: filter failed: " .
1424                     "\"$post_filter_cmd\"\n";
1425             }
1426             return;
1427         }
1428     }
1429
1430     if (!$no_output_p) {
1431         my $tsize = (stat($target))[7];
1432         if ($tsize > 200) {
1433             $cmd = $ppm_to_root_window_cmd;
1434             $cmd =~ s/%%PPM%%/$target/;
1435
1436             # xv seems to hate being killed.  it tends to forget to clean
1437             # up after itself, and leaves windows around and colors allocated.
1438             # I had this same problem with vidwhacker, and I'm not entirely
1439             # sure what I did to fix it.  But, let's try this: launch xv
1440             # in the background, so that killing this process doesn't kill it.
1441             # it will die of its own accord soon enough.  So this means we
1442             # start pumping bits to the root window in parallel with starting
1443             # the next network retrieval, which is probably a better thing
1444             # to do anyway.
1445             #
1446             $cmd .= "&";
1447
1448             $rc = nontrapping_system ($cmd);
1449
1450             if ($rc != 0) {
1451                 if ($verbose) {
1452                     print STDERR "$progname: display failed: \"$cmd\"\n";
1453                 }
1454                 return;
1455             }
1456
1457         } elsif ($verbose > 1) {
1458             print STDERR "$progname: $target size is $tsize\n";
1459         }
1460     }
1461
1462     if ($verbose > 0) {
1463         print STDOUT "image: ${iw}x${ih} @ $x,$y $base $source\n";
1464     }
1465
1466     return 1;
1467 }
1468
1469
1470 sub main {
1471     $| = 1;
1472     srand(time ^ $$);
1473
1474     my $root_p = 0;
1475
1476     # historical suckage: the environment variable name is lower case.
1477     $http_proxy = $ENV{http_proxy} || $ENV{HTTP_PROXY};
1478
1479     while ($_ = $ARGV[0]) {
1480         shift @ARGV;
1481         if ($_ eq "-display" ||
1482             $_ eq "-displ" ||
1483             $_ eq "-disp" ||
1484             $_ eq "-dis" ||
1485             $_ eq "-dpy" ||
1486             $_ eq "-d") {
1487             $ENV{DISPLAY} = shift @ARGV;
1488         } elsif ($_ eq "-root") {
1489             $root_p = 1;
1490         } elsif ($_ eq "-no-output") {
1491             $no_output_p = 1;
1492         } elsif ($_ eq "-urls-only") {
1493             $urls_only_p = 1;
1494             $no_output_p = 1;
1495         } elsif ($_ eq "-verbose") {
1496             $verbose++;
1497         } elsif (m/^-v+$/) {
1498             $verbose += length($_)-1;
1499         } elsif ($_ eq "-delay") {
1500             $delay = shift @ARGV;
1501         } elsif ($_ eq "-timeout") {
1502             $http_timeout = shift @ARGV;
1503         } elsif ($_ eq "-filter") {
1504             $filter_cmd = shift @ARGV;
1505         } elsif ($_ eq "-filter2") {
1506             $post_filter_cmd = shift @ARGV;
1507         } elsif ($_ eq "-background" || $_ eq "-bg") {
1508             $background = shift @ARGV;
1509         } elsif ($_ eq "-size") {
1510             $_ = shift @ARGV;
1511             if (m@^(\d+)x(\d+)$@) {
1512                 $img_width = $1;
1513                 $img_height = $2;
1514             } else {
1515                 die "$progname: argument to \"-size\" must be" .
1516                     " of the form \"640x400\"\n";
1517             }
1518         } elsif ($_ eq "-proxy" || $_ eq "-http-proxy") {
1519             $http_proxy = shift @ARGV;
1520         } else {
1521             die "$copyright\nusage: $progname [-root]" .
1522                 " [-display dpy] [-root] [-verbose] [-timeout secs]\n" .
1523                 "\t\t  [-delay secs] [-filter cmd] [-filter2 cmd]\n" .
1524                 "\t\t  [-http-proxy host[:port]]\n";
1525         }
1526     }
1527
1528     if ($http_proxy && $http_proxy eq "") {
1529         $http_proxy = undef;
1530     }
1531     if ($http_proxy && $http_proxy =~ m@^http://([^/]*)/?$@ ) {
1532         # historical suckage: allow "http://host:port" as well as "host:port".
1533         $http_proxy = $1;
1534     }
1535
1536     if (!$root_p && !$no_output_p) {
1537         die "$copyright" .
1538             "$progname: the -root argument is mandatory (for now.)\n";
1539     }
1540
1541     if (!$no_output_p && !$ENV{DISPLAY}) {
1542         die "$progname: \$DISPLAY is not set.\n";
1543     }
1544
1545     if ($urls_only_p) {
1546         url_only_output;
1547     } else {
1548         x_output;
1549     }
1550 }
1551
1552 main;
1553 exit (0);