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