http://packetstormsecurity.org/UNIX/admin/xscreensaver-4.03.tar.gz
[xscreensaver] / hacks / webcollage
1 #!/usr/bin/perl -w
2 #
3 # webcollage, Copyright (c) 1999-2002 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
21 require 5;
22 use strict;
23
24 # We can't "use diagnostics" here, because that library malfunctions if
25 # you signal and catch alarms: it says "Uncaught exception from user code"
26 # and exits, even though I damned well AM catching it!
27 #use diagnostics;
28
29
30 use Socket;
31 require Time::Local;
32 require POSIX;
33 use Fcntl ':flock'; # import LOCK_* constants
34 use POSIX qw(strftime);
35
36
37 my $progname = $0; $progname =~ s@.*/@@g;
38 my $version = q{ $Revision: 1.90 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
39 my $copyright = "WebCollage $version, Copyright (c) 1999-2002" .
40     " Jamie Zawinski <jwz\@jwz.org>\n" .
41     "            http://www.jwz.org/xscreensaver/\n";
42
43
44
45 my @search_methods = (  40, "imagevista", \&pick_from_alta_vista_images,
46                         30, "altavista",  \&pick_from_alta_vista_text,
47                         19, "yahoorand",  \&pick_from_yahoo_random_link,
48                          9, "lycos",      \&pick_from_lycos_text,
49                          2, "yahoonews",  \&pick_from_yahoo_news_text,
50
51                      # Hotbot gives me "no matches" just about every time.
52                      # Then I try the same URL again, and it works.  I guess
53                      # it caches searches, and webcollage always busts its
54                      # cache and time out?  Or it just sucks.
55                      #   0, "hotbot",     \&pick_from_hotbot_text,
56
57                      # Google asked (nicely) for me to stop searching them.
58                      #   0, "googlenums", \&pick_from_google_image_numbers,
59                      #   0, "googleimgs", \&pick_from_google_images,
60
61                       );
62
63 # programs we can use to write to the root window (tried in ascending order.)
64 #
65 my @root_displayers = (
66   "chbg       -once -xscreensaver -max_size 100",
67   "xv         -root -quit -viewonly +noresetroot -quick24 -rmode 5" .
68   "           -rfg black -rbg black",
69   "xli        -quiet -onroot -center -border black",
70   "xloadimage -quiet -onroot -center -border black",
71
72 # this lame program wasn't built with vroot.h:
73 # "xsri       -scale -keep-aspect -center-horizontal -center-vertical",
74 );
75
76
77 # Some sites need cookies to work properly.   These are they.
78 #
79 my %cookies = (
80   "www.altavista.com"  =>  "AV_ALL=1",   # request uncensored searches
81   "web.altavista.com"  =>  "AV_ALL=1",
82
83                                          # log in as "cipherpunk"
84   "www.nytimes.com"    =>  'NYT-S=18cHMIlJOn2Y1bu5xvEG3Ufuk6E1oJ.' .
85                            'FMxWaQV0igaB5Yi/Q/guDnLeoL.pe7i1oakSb' .
86                            '/VqfdUdb2Uo27Vzt1jmPn3cpYRlTw9',
87 );
88
89
90 # If this is set, it's a helper program to use for pasting images together:
91 # this is a lot faster and more efficient than using PPM pipelines, which is
92 # what we do if this program doesn't exist.  (We check for "webcollage-helper"
93 # on $PATH at startup, and set this variable appropriately.)
94 #
95 my $webcollage_helper = undef;
96
97
98 # If we have the webcollage-helper program, then it will paste the images
99 # together with transparency!  0.0 is invisible, 1.0 is totally opaque.
100 #
101 my $opacity = 0.85;
102
103
104 # Some sites have  managed to poison the search engines.  These are they.
105 # (We auto-detect sites that have poisoned the search engines via excessive
106 # keywords or dictionary words,  but these are ones that slip through
107 # anyway.)
108 #
109 # This can contain full host names, or 2 or 3 component domains.
110 #
111 my %poisoners = (
112   "die.net"                 => 1,  # 'l33t h4ck3r d00dz.
113   "genforum.genealogy.com"  => 1,  # Cluttering altavista with human names.
114   "rootsweb.com"            => 1,  # Cluttering altavista with human names.
115   "akamai.net"              => 1,  # Lots of sites have their images on Akamai.
116                                    # But those are pretty much all banners.
117                                    # Since Akamai is super-expensive, let's
118                                    # go out on a limb and assume that all of
119                                    # their customers are rich-and-boring.
120   "bartleby.com"            => 1,  # Dictionary, cluttering altavista.
121   "encyclopedia.com"        => 1,  # Dictionary, cluttering altavista.
122   "onlinedictionary.datasegment.com" => 1,  # Dictionary, cluttering altavista.
123   "hotlinkpics.com"         => 1,  # Porn site that has poisoned imagevista
124                                    # (I don't see how they did it, though!)
125   "alwayshotels.com"        => 1,  # Poisoned Lycos pretty heavily.
126 );
127
128
129 # When verbosity is turned on, we warn about sites that we seem to be hitting
130 # a lot: usually this means some new poisoner has made it into the search
131 # engines.  But sometimes, the warning is just because that site has a lot
132 # of stuff on it.  So these are the sites that are immune to the "frequent
133 # site" diagnostic message.
134 #
135 my %warningless_sites = (
136   "home.earthlink.net"      => 1,  # Lots of home pages here.
137   "www.geocities.com"       => 1,
138   "www.angelfire.com"       => 1,
139   "members.aol.com"         => 1,
140
141   "yimg.com"                => 1,  # This is where dailynews.yahoo.com stores
142   "eimg.com"                => 1,  # its images, so pick_from_yahoo_news_text()
143                                    # hits this every time.
144 );
145
146
147 ##############################################################################
148 #
149 # Various global flags set by command line parameters, or computed
150 #
151 ##############################################################################
152
153
154 my $current_state = "???";      # for diagnostics
155 my $load_method;
156 my $last_search;
157 my $image_succeeded = -1;
158 my $suppress_audit = 0;
159
160 my $verbose_imgmap = 0;         # print out rectangles and URLs only (stdout)
161 my $verbose_warnings = 0;       # print out warnings when things go wrong
162 my $verbose_load = 0;           # diagnostics about loading of URLs
163 my $verbose_filter = 0;         # diagnostics about page selection/rejection
164 my $verbose_net = 0;            # diagnostics about network I/O
165 my $verbose_pbm = 0;            # diagnostics about PBM pipelines
166 my $verbose_http = 0;           # diagnostics about all HTTP activity
167 my $verbose_exec = 0;           # diagnostics about executing programs
168
169 my $report_performance_interval = 60 * 15;  # print some stats every 15 minutes
170
171 my $http_proxy = undef;
172 my $http_timeout = 30;
173 my $cvt_timeout = 10;
174
175 my $min_width = 50;
176 my $min_height = 50;
177 my $min_ratio = 1/5;
178
179 my $min_gif_area = (120 * 120);
180
181
182 my $no_output_p = 0;
183 my $urls_only_p = 0;
184
185 my $wordlist;
186
187 my %rejected_urls;
188 my @tripwire_words = ("aberrate", "abode", "amorphous", "antioch",
189                       "arrhenius", "arteriole", "blanket", "brainchild",
190                       "burdensome", "carnival", "cherub", "chord", "clever",
191                       "dedicate", "dilogarithm", "dolan", "dryden",
192                       "eggplant");
193
194
195 ##############################################################################
196 #
197 # Retrieving URLs
198 #
199 ##############################################################################
200
201 # returns three values: the HTTP response line; the document headers;
202 # and the document body.
203 #
204 sub get_document_1 {
205   my ( $url, $referer, $timeout ) = @_;
206
207   if (!defined($timeout)) { $timeout = $http_timeout; }
208   if ($timeout > $http_timeout) { $timeout = $http_timeout; }
209
210   if ($timeout <= 0) {
211     LOG (($verbose_net || $verbose_load), "timed out for $url");
212     return ();
213   }
214
215   LOG ($verbose_net, "get_document_1 $url " . ($referer ? $referer : ""));
216
217   if (! ($url =~ m@^http://@i)) {
218     LOG ($verbose_net, "not an HTTP URL: $url");
219     return ();
220   }
221
222   my ($url_proto, $dummy, $serverstring, $path) = split(/\//, $url, 4);
223   $path = "" unless $path;
224
225   my ($them,$port) = split(/:/, $serverstring);
226   $port = 80 unless $port;
227
228   my $them2 = $them;
229   my $port2 = $port;
230   if ($http_proxy) {
231     $serverstring = $http_proxy if $http_proxy;
232     ($them2,$port2) = split(/:/, $serverstring);
233     $port2 = 80 unless $port2;
234   }
235
236   my ($remote, $iaddr, $paddr, $proto, $line);
237   $remote = $them2;
238   if ($port2 =~ /\D/) { $port2 = getservbyname($port2, 'tcp') }
239   if (!$port2) {
240     LOG (($verbose_net || $verbose_load), "unrecognised port in $url");
241     return ();
242   }
243   $iaddr   = inet_aton($remote);
244   if (!$iaddr) {
245     LOG (($verbose_net || $verbose_load), "host not found: $remote");
246     return ();
247   }
248   $paddr   = sockaddr_in($port2, $iaddr);
249
250
251   my $head = "";
252   my $body = "";
253
254   @_ =
255     eval {
256       local $SIG{ALRM} = sub {
257         LOG (($verbose_net || $verbose_load), "timed out ($timeout) for $url");
258         die "alarm\n";
259       };
260       alarm $timeout;
261
262       $proto   = getprotobyname('tcp');
263       if (!socket(S, PF_INET, SOCK_STREAM, $proto)) {
264         LOG (($verbose_net || $verbose_load), "socket: $!");
265         return ();
266       }
267       if (!connect(S, $paddr)) {
268         LOG (($verbose_net || $verbose_load), "connect($serverstring): $!");
269         return ();
270       }
271
272       select(S); $| = 1; select(STDOUT);
273
274       my $cookie = $cookies{$them};
275
276       my $user_agent = "$progname/$version";
277       if ($url =~ m@^http://www\.altavista\.com/@) {
278         # block this, you turkeys.
279         $user_agent = "Mozilla/4.76 [en] (X11; U; Linux 2.2.16-22 i686; Nav)";
280       }
281
282       my $hdrs = "GET " . ($http_proxy ? $url : "/$path") . " HTTP/1.0\r\n" .
283                  "Host: $them\r\n" .
284                  "User-Agent: $user_agent\r\n";
285       if ($referer) {
286         $hdrs .= "Referer: $referer\r\n";
287       }
288       if ($cookie) {
289         my @cc = split(/\r?\n/, $cookie);
290         $hdrs .= "Cookie: " . join('; ', @cc) . "\r\n";
291       }
292       $hdrs .= "\r\n";
293
294       foreach (split('\r?\n', $hdrs)) {
295         LOG ($verbose_http, "  ==> $_");
296       }
297       print S $hdrs;
298       my $http = <S> || "";
299
300       $_  = $http;
301       s/[\r\n]+$//s;
302       LOG ($verbose_http, "  <== $_");
303
304       while (<S>) {
305         $head .= $_;
306         s/[\r\n]+$//s;
307         last if m@^$@;
308         LOG ($verbose_http, "  <== $_");
309
310         if (m@^Set-cookie:\s*([^;\r\n]+)@i) {
311           set_cookie($them, $1)
312         }
313       }
314
315       my $lines = 0;
316       while (<S>) {
317         $body .= $_;
318         $lines++;
319       }
320
321       LOG ($verbose_http,
322            "  <== [ body ]: $lines lines, " . length($body) . " bytes");
323
324       close S;
325
326       if (!$http) {
327         LOG (($verbose_net || $verbose_load), "null response: $url");
328         return ();
329       }
330
331       return ( $http, $head, $body );
332     };
333   die if ($@ && $@ ne "alarm\n");       # propagate errors
334   if ($@) {
335     # timed out
336     $head = undef;
337     $body = undef;
338     $suppress_audit = 1;
339     return ();
340   } else {
341     # didn't
342     alarm 0;
343     return @_;
344   }
345 }
346
347
348 # returns two values: the document headers; and the document body.
349 # if the given URL did a redirect, returns the redirected-to document.
350 #
351 sub get_document {
352   my ( $url, $referer, $timeout ) = @_;
353   my $start = time;
354
355   my $orig_url = $url;
356   my $loop_count = 0;
357   my $max_loop_count = 4;
358
359   do {
360     if (defined($timeout) && $timeout <= 0) {
361       LOG (($verbose_net || $verbose_load), "timed out for $url");
362       $suppress_audit = 1;
363       return ();
364     }
365
366     my ( $http, $head, $body ) = get_document_1 ($url, $referer, $timeout);
367
368     if (defined ($timeout)) {
369       my $now = time;
370       my $elapsed = $now - $start;
371       $timeout -= $elapsed;
372       $start = $now;
373     }
374
375     return () unless $http; # error message already printed
376
377     $http =~ s/[\r\n]+$//s;
378
379     if ( $http =~ m@^HTTP/[0-9.]+ 30[123]@ ) {
380       $_ = $head;
381       my ( $location ) = m@^location:[ \t]*(.*)$@im;
382       if ( $location ) {
383         $location =~ s/[\r\n]$//;
384
385         LOG ($verbose_net, "redirect from $url to $location");
386         $referer = $url;
387         $url = $location;
388
389         if ($url =~ m@^/@) {
390           $referer =~ m@^(http://[^/]+)@i;
391           $url = $1 . $url;
392         } elsif (! ($url =~ m@^[a-z]+:@i)) {
393           $_ = $referer;
394           s@[^/]+$@@g if m@^http://[^/]+/@i;
395           $_ .= "/" if m@^http://[^/]+$@i;
396           $url = $_ . $url;
397         }
398
399       } else {
400         LOG ($verbose_net, "no Location with \"$http\"");
401         return ( $url, $body );
402       }
403
404       if ($loop_count++ > $max_loop_count) {
405         LOG ($verbose_net,
406              "too many redirects ($max_loop_count) from $orig_url");
407         $body = undef;
408         return ();
409       }
410
411     } elsif ( $http =~ m@^HTTP/[0-9.]+ ([4-9][0-9][0-9].*)$@ ) {
412
413       LOG (($verbose_net || $verbose_load), "failed: $1 ($url)");
414
415       # http errors -- return nothing.
416       $body = undef;
417       return ();
418
419     } elsif (!$body) {
420
421       LOG (($verbose_net || $verbose_load), "document contains no data: $url");
422       return ();
423
424     } else {
425
426       # ok!
427       return ( $url, $body );
428     }
429
430   } while (1);
431 }
432
433 # If we already have a cookie defined for this site, and the site is trying
434 # to overwrite that very same cookie, let it do so.  This is because nytimes
435 # expires its cookies - it lets you upgrade to a new cookie without logging
436 # in again, but you have to present the old cookie to get the new cookie.
437 # So, by doing this, the built-in cypherpunks cookie will never go "stale".
438 #
439 sub set_cookie {
440   my ($host, $cookie) = @_;
441   my $oc = $cookies{$host};
442   return unless $oc;
443   $_ = $oc;
444   my ($oc_name, $oc_value) = m@^([^= \t\r\n]+)=(.*)$@;
445   $_ = $cookie;
446   my ($nc_name, $nc_value) = m@^([^= \t\r\n]+)=(.*)$@;
447
448   if ($oc_name eq $nc_name &&
449       $oc_value ne $nc_value) {
450     $cookies{$host} = $cookie;
451     LOG ($verbose_net, "overwrote ${host}'s $oc_name cookie");
452   }
453 }
454
455
456 ############################################################################
457 #
458 # Extracting image URLs from HTML
459 #
460 ############################################################################
461
462 # given a URL and the body text at that URL, selects and returns a random
463 # image from it.  returns () if no suitable images found.
464 #
465 sub pick_image_from_body {
466   my ( $url, $body ) = @_;
467
468   my $base = $url;
469   $_ = $url;
470
471   # if there's at least one slash after the host, take off the last
472   # pathname component
473   if ( m@^http://[^/]+/@io ) {
474     $base =~ s@[^/]+$@@go;
475   }
476
477   # if there are no slashes after the host at all, put one on the end.
478   if ( m@^http://[^/]+$@io ) {
479     $base .= "/";
480   }
481
482   $_ = $body;
483
484   # strip out newlines, compress whitespace
485   s/[\r\n\t ]+/ /go;
486
487   # nuke comments
488   s/<!--.*?-->//go;
489
490
491   # There are certain web sites that list huge numbers of dictionary
492   # words in their bodies or in their <META NAME=KEYWORDS> tags (surprise!
493   # Porn sites tend not to be reputable!)
494   #
495   # I do not want webcollage to filter on content: I want it to select
496   # randomly from the set of images on the web.  All the logic here for
497   # rejecting some images is really a set of heuristics for rejecting
498   # images that are not really images: for rejecting *text* that is in
499   # GIF/JPEG form.  I don't want text, I want pictures, and I want the
500   # content of the pictures to be randomly selected from among all the
501   # available content.
502   #
503   # So, filtering out "dirty" pictures by looking for "dirty" keywords
504   # would be wrong: dirty pictures exist, like it or not, so webcollage
505   # should be able to select them.
506   #
507   # However, picking a random URL is a hard thing to do.  The mechanism I'm
508   # using is to search for a selection of random words.  This is not
509   # perfect, but works ok most of the time.  The way it breaks down is when
510   # some URLs get precedence because their pages list *every word* as
511   # related -- those URLs come up more often than others.
512   #
513   # So, after we've retrieved a URL, if it has too many keywords, reject
514   # it.  We reject it not on the basis of what those keywords are, but on
515   # the basis that by having so many, the page has gotten an unfair
516   # advantage against our randomizer.
517   #
518   my $trip_count = 0;
519   foreach my $trip (@tripwire_words) {
520     $trip_count++ if m/$trip/i;
521   }
522
523   if ($trip_count >= $#tripwire_words - 2) {
524     LOG (($verbose_filter || $verbose_load),
525          "there is probably a dictionary in \"$url\": rejecting.");
526     $rejected_urls{$url} = -1;
527     $body = undef;
528     $_ = undef;
529     return ();
530   }
531
532
533   my @urls;
534   my %unique_urls;
535
536   foreach (split(/ *</)) {
537     if ( m/^meta /i ) {
538
539       # Likewise, reject any web pages that have a KEYWORDS meta tag
540       # that is too long.
541       #
542       if (m/name ?= ?\"?keywords\"?/i &&
543           m/content ?= ?\"([^\"]+)\"/) {
544         my $L = length($1);
545         if ($L > 1000) {
546           LOG (($verbose_filter || $verbose_load),
547                "excessive keywords ($L bytes) in $url: rejecting.");
548           $rejected_urls{$url} = $L;
549           $body = undef;
550           $_ = undef;
551           return ();
552         } else {
553           LOG ($verbose_filter, "  keywords ($L bytes) in $url (ok)");
554         }
555       }
556
557     } elsif ( m/^(img|a) .*(src|href) ?= ?\"? ?(.*?)[ >\"]/io ) {
558
559       my $was_inline = ( "$1" eq "a" || "$1" eq "A" );
560       my $link = $3;
561       my ( $width )  = m/width ?=[ \"]*(\d+)/oi;
562       my ( $height ) = m/height ?=[ \"]*(\d+)/oi;
563       $_ = $link;
564
565       if ( m@^/@o ) {
566         my $site;
567         ( $site = $base ) =~ s@^(http://[^/]*).*@$1@gio;
568         $_ = "$site$link";
569       } elsif ( ! m@^[^/:?]+:@ ) {
570         $_ = "$base$link";
571         s@/\./@/@g;
572         while (s@/\.\./@/@g) {
573         }
574       }
575
576       # skip non-http
577       if ( ! m@^http://@io ) {
578         next;
579       }
580
581       # skip non-image
582       if ( ! m@[.](gif|jpg|jpeg|pjpg|pjpeg)$@io ) {
583         next;
584       }
585
586       # skip really short or really narrow images
587       if ( $width && $width < $min_width) {
588         if (!$height) { $height = "?"; }
589         LOG ($verbose_filter, "  skip narrow image $_ (${width}x$height)");
590         next;
591       }
592
593       if ( $height && $height < $min_height) {
594         if (!$width) { $width = "?"; }
595         LOG ($verbose_filter, "  skip short image $_ (${width}x$height)");
596         next;
597       }
598
599       # skip images with ratios that make them look like banners.
600       if ($min_ratio && $width && $height &&
601           ($width * $min_ratio ) > $height) {
602         if (!$height) { $height = "?"; }
603         LOG ($verbose_filter, "  skip bad ratio $_ (${width}x$height)");
604         next;
605       }
606
607       # skip GIFs with a small number of pixels -- those usually suck.
608       if ($width && $height &&
609           m/\.gif$/io &&
610           ($width * $height) < $min_gif_area) {
611         LOG ($verbose_filter, "  skip small GIF $_ (${width}x$height)");
612         next;
613       }
614       
615
616       my $url = $_;
617
618       if ($unique_urls{$url}) {
619         LOG ($verbose_filter, "  skip duplicate image $_");
620         next;
621       }
622
623       LOG ($verbose_filter,
624            "  image $url" .
625            ($width && $height ? " (${width}x${height})" : "") .
626            ($was_inline ? " (inline)" : ""));
627
628       $urls[++$#urls] = $url;
629       $unique_urls{$url}++;
630
631       # jpegs are preferable to gifs.
632       $_ = $url;
633       if ( ! m@[.]gif$@io ) {
634         $urls[++$#urls] = $url;
635       }
636
637       # pointers to images are preferable to inlined images.
638       if ( ! $was_inline ) {
639         $urls[++$#urls] = $url;
640         $urls[++$#urls] = $url;
641       }
642     }
643   }
644
645   my $fsp = ($body =~ m@<frameset@i);
646
647   $_ = undef;
648   $body = undef;
649
650   @urls = depoison (@urls);
651
652   if ( $#urls < 0 ) {
653     LOG ($verbose_load, "no images on $base" . ($fsp ? " (frameset)" : ""));
654     return ();
655   }
656
657   # pick a random element of the table
658   my $i = int(rand($#urls+1));
659   $url = $urls[$i];
660
661   LOG ($verbose_load, "picked image " .($i+1) . "/" . ($#urls+1) . ": $url");
662
663   return $url;
664 }
665
666
667 \f
668 ############################################################################
669 #
670 # Subroutines for getting pages and images out of search engines
671 #
672 ############################################################################
673
674
675 sub pick_dictionary {
676   my @dicts = ("/usr/dict/words",
677                "/usr/share/dict/words",
678                "/usr/share/lib/dict/words");
679   foreach my $f (@dicts) {
680     if (-f $f) {
681       $wordlist = $f;
682       last;
683     }
684   }
685   error ("$dicts[0] does not exist") unless defined($wordlist);
686 }
687
688 # returns a random word from the dictionary
689 #
690 sub random_word {
691     my $word = 0;
692     if (open (IN, "<$wordlist")) {
693         my $size = (stat(IN))[7];
694         my $pos = rand $size;
695         if (seek (IN, $pos, 0)) {
696             $word = <IN>;   # toss partial line
697             $word = <IN>;   # keep next line
698         }
699         if (!$word) {
700           seek( IN, 0, 0 );
701           $word = <IN>;
702         }
703         close (IN);
704     }
705
706     return 0 if (!$word);
707
708     $word =~ s/^[ \t\n\r]+//;
709     $word =~ s/[ \t\n\r]+$//;
710     $word =~ s/ys$/y/;
711     $word =~ s/ally$//;
712     $word =~ s/ly$//;
713     $word =~ s/ies$/y/;
714     $word =~ s/ally$/al/;
715     $word =~ s/izes$/ize/;
716     $word =~ tr/A-Z/a-z/;
717
718     if ( $word =~ s/[ \t\n\r]/\+/g ) {  # convert intra-word spaces to "+".
719       $word = "\%22$word\%22";          # And put quotes (%22) around it.
720     }
721
722     return $word;
723 }
724
725 sub random_words {
726   my ($or_p) = @_;
727   my $sep = ($or_p ? "%20OR%20" : "%20");
728   return (random_word . $sep .
729           random_word . $sep .
730           random_word . $sep .
731           random_word . $sep .
732           random_word);
733 }
734
735
736 sub url_quote {
737   my ($s) = @_;
738   $s =~ s|([^-a-zA-Z0-9.\@/_\r\n])|sprintf("%%%02X", ord($1))|ge;
739   return $s;
740 }
741
742 sub url_unquote {
743   my ($s) = @_;
744   $s =~ s/[+]/ /g;
745   $s =~ s/%([a-z0-9]{2})/chr(hex($1))/ige;
746   return $s;
747 }
748
749
750 # Loads the given URL (a search on some search engine) and returns:
751 # - the total number of hits the search engine claimed it had;
752 # - a list of URLs from the page that the search engine returned;
753 # Note that this list contains all kinds of internal search engine
754 # junk URLs too -- caller must prune them.
755 #
756 sub pick_from_search_engine {
757   my ( $timeout, $search_url, $words ) = @_;
758
759   $_ = $words;
760   s/%20/ /g;
761
762   print STDERR "\n\n" if ($verbose_load);
763
764   LOG ($verbose_load, "words: $_");
765   LOG ($verbose_load, "URL: $search_url");
766
767   $last_search = $search_url;   # for warnings
768
769   my $start = time;
770   my ( $base, $body ) = get_document ($search_url, undef, $timeout);
771   if (defined ($timeout)) {
772     $timeout -= (time - $start);
773     if ($timeout <= 0) {
774       $body = undef;
775       LOG (($verbose_net || $verbose_load),
776            "timed out (late) for $search_url");
777       $suppress_audit = 1;
778       return ();
779     }
780   }
781
782   return () if (! $body);
783
784
785   my @subpages;
786
787   my $search_count = "?";
788   if ($body =~ m@found (approximately |about )?(<B>)?(\d+)(</B>)? image@) {
789     $search_count = $3;
790   } elsif ($body =~ m@<NOBR>((\d{1,3})(,\d{3})*)&nbsp;@i) {
791     $search_count = $1;
792   } elsif ($body =~ m@found ((\d{1,3})(,\d{3})*|\d+) Web p@) {
793     $search_count = $1;
794   } elsif ($body =~ m@found about ((\d{1,3})(,\d{3})*|\d+) results@) {
795     $search_count = $1;
796   } elsif ($body =~ m@\b\d+ - \d+ of (\d+)\b@i) { # imagevista
797     $search_count = $1;
798   } elsif ($body =~ m@About ((\d{1,3})(,\d{3})*) images@i) { # imagevista
799     $search_count = $1;
800   } elsif ($body =~ m@We found ((\d{1,3})(,\d{3})*|\d+) results@i) { # *vista
801     $search_count = $1;
802   } elsif ($body =~ m@ of about <B>((\d{1,3})(,\d{3})*)<@i) { # googleimages
803     $search_count = $1;
804   } elsif ($body =~ m@<B>((\d{1,3})(,\d{3})*)</B> Web sites were found@i) {
805     $search_count = $1;    # lycos
806   } elsif ($body =~ m@WEB.*?RESULTS.*?\b((\d{1,3})(,\d{3})*)\b.*?Matches@i) {
807     $search_count = $1;                          # hotbot
808   } elsif ($body =~ m@no photos were found containing@i) { # imagevista
809     $search_count = "0";
810   } elsif ($body =~ m@found no document matching@i) { # altavista
811     $search_count = "0";
812   }
813   1 while ($search_count =~ s/^(\d+)(\d{3})/$1,$2/);
814
815 #  if ($search_count eq "?" || $search_count eq "0") {
816 #    local *OUT;
817 #    my $file = "/tmp/wc.html";
818 #    open(OUT, ">$file") || error ("writing $file: $!");
819 #    print OUT $body;
820 #    close OUT;
821 #    print STDERR  blurb() . "###### wrote $file\n";
822 #  }
823
824
825   my $length = length($body);
826   my $href_count = 0;
827
828   $_ = $body;
829
830   s/[\r\n\t ]+/ /g;
831
832
833   s/(<A )/\n$1/gi;
834   foreach (split(/\n/)) {
835     $href_count++;
836     my ($u) = m@<A\s.*\bHREF\s*=\s*([^>]+)>@i;
837     next unless $u;
838
839     if ($u =~ m/^\"([^\"]*)\"/) { $u = $1; }   # quoted string
840     elsif ($u =~ m/^([^\s]*)\s/) { $u = $1; }  # or token
841
842     if ( $rejected_urls{$u} ) {
843       LOG ($verbose_filter, "  pre-rejecting candidate: $u");
844       next;
845     }
846
847     LOG ($verbose_http, "    HREF: $u");
848
849     $subpages[++$#subpages] = $u;
850   }
851
852   if ( $#subpages < 0 ) {
853     LOG ($verbose_filter,
854          "found nothing on $base ($length bytes, $href_count links).");
855     return ();
856   }
857
858   LOG ($verbose_filter, "" . $#subpages+1 . " links on $search_url");
859
860   return ($search_count, @subpages);
861 }
862
863
864 sub depoison {
865   my (@urls) = @_;
866   my @urls2 = ();
867   foreach (@urls) {
868     my ($h) = m@^http://([^/: \t\r\n]+)@i;
869
870     next unless defined($h);
871
872     if ($poisoners{$h}) {
873       LOG (($verbose_filter), "  rejecting poisoner: $_");
874       next;
875     }
876     if ($h =~ m@([^.]+\.[^.]+\.[^.]+)$@ &&
877         $poisoners{$1}) {
878       LOG (($verbose_filter), "  rejecting poisoner: $_");
879       next;
880     }
881     if ($h =~ m@([^.]+\.[^.]+)$@ &&
882         $poisoners{$1}) {
883       LOG (($verbose_filter), "  rejecting poisoner: $_");
884       next;
885     }
886
887     push @urls2, $_;
888   }
889   return @urls2;
890 }
891
892
893 # given a list of URLs, picks one at random; loads it; and returns a
894 # random image from it.
895 # returns the url of the page loaded; the url of the image chosen;
896 # and a debugging description string.
897 #
898 sub pick_image_from_pages {
899   my ($base, $total_hit_count, $unfiltered_link_count, $timeout, @pages) = @_;
900
901   $total_hit_count = "?" unless defined($total_hit_count);
902
903   @pages = depoison (@pages);
904   LOG ($verbose_load,
905        "" . ($#pages+1) . " candidates of $unfiltered_link_count links" .
906        " ($total_hit_count total)");
907
908   return () if ($#pages < 0);
909
910   my $i = int(rand($#pages+1));
911   my $page = $pages[$i];
912
913   LOG ($verbose_load, "picked page $page");
914
915   $suppress_audit = 1;
916
917   my ( $base2, $body2 ) = get_document ($page, $base, $timeout);
918
919   if (!$base2 || !$body2) {
920     $body2 = undef;
921     return ();
922   }
923
924   my $img = pick_image_from_body ($base2, $body2);
925   $body2 = undef;
926
927   if ($img) {
928     return ($base2, $img);
929   } else {
930     return ();
931   }
932 }
933
934 \f
935 ############################################################################
936 #
937 # Pick images from random pages returned by the Yahoo Random Link
938 #
939 ############################################################################
940
941 # yahoorand
942 my $yahoo_random_link = "http://random.yahoo.com/bin/ryl";
943
944
945 # Picks a random page; picks a random image on that page;
946 # returns two URLs: the page containing the image, and the image.
947 # Returns () if nothing found this time.
948 #
949 sub pick_from_yahoo_random_link {
950   my ( $timeout ) = @_;
951
952   print STDERR "\n\n" if ($verbose_load);
953   LOG ($verbose_load, "URL: $yahoo_random_link");
954
955   $last_search = $yahoo_random_link;   # for warnings
956
957   $suppress_audit = 1;
958
959   my ( $base, $body ) = get_document ($yahoo_random_link, undef, $timeout);
960   if (!$base || !$body) {
961     $body = undef;
962     return;
963   }
964
965   LOG ($verbose_load, "redirected to: $base");
966
967   my $img = pick_image_from_body ($base, $body);
968   $body = undef;
969
970   if ($img) {
971     return ($base, $img);
972   } else {
973     return ();
974   }
975 }
976
977 \f
978 ############################################################################
979 #
980 # Pick images by feeding random words into Alta Vista Image Search
981 #
982 ############################################################################
983
984
985 my $alta_vista_images_url = "http://www.altavista.com/cgi-bin/query" .
986                             "?ipht=1" .       # photos
987                             "&igrph=1" .      # graphics
988                             "&iclr=1" .       # color
989                             "&ibw=1" .        # b&w
990                             "&micat=1" .      # no partner sites
991                             "&imgset=1" .     # no partner sites
992                             "&stype=simage" . # do image search
993                             "&mmW=1" .        # unknown, but required
994                             "&q=";
995
996 # imagevista
997 sub pick_from_alta_vista_images {
998   my ( $timeout ) = @_;
999
1000   my $words = random_words(1);
1001   my $page = (int(rand(9)) + 1);
1002   my $search_url = $alta_vista_images_url . $words;
1003
1004   if ($page > 1) {
1005     $search_url .= "&pgno=" . $page;            # page number
1006     $search_url .= "&stq=" . (($page-1) * 12);  # first hit result on page
1007   }
1008
1009   my ($search_hit_count, @subpages) =
1010     pick_from_search_engine ($timeout, $search_url, $words);
1011
1012   my @candidates = ();
1013   foreach my $u (@subpages) {
1014
1015     # altavista is encoding their URLs now.
1016     next unless ($u =~
1017     m@^/r\?ck_sm=[a-zA-Z0-9]+\&ref=[a-zA-Z0-9]+(\&uid=[a-zA-Z0-9]+)?\&r=(.*)@);
1018     $u = url_unquote($2);
1019
1020     next unless ($u =~ m@^http://@i);    #  skip non-HTTP or relative URLs
1021     next if ($u =~ m@[/.]altavista\.com\b@i);     # skip altavista builtins
1022     next if ($u =~ m@[/.]doubleclick\.net\b@i);   # you cretins
1023     next if ($u =~ m@[/.]clicktomarket\.com\b@i); # more cretins
1024
1025     next if ($u =~ m@[/.]viewimages\.com\b@i);    # stacked deck
1026     next if ($u =~ m@[/.]gettyimages\.com\b@i);
1027
1028     LOG ($verbose_filter, "  candidate: $u");
1029     push @candidates, $u;
1030   }
1031
1032   return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1033                                 $timeout, @candidates);
1034 }
1035
1036
1037 \f
1038 ############################################################################
1039 #
1040 # Pick images by feeding random words into Google Image Search.
1041 # By Charles Gales <gales@us.ibm.com>
1042 #
1043 ############################################################################
1044
1045
1046 my $google_images_url =     "http://images.google.com/images" .
1047                             "?site=images" .  # photos
1048                             "&btnG=Search" .  # graphics
1049                             "&safe=off" .     # no screening
1050                             "&imgsafe=off" .
1051                             "&q=";
1052
1053 # googleimgs
1054 sub pick_from_google_images {
1055   my ( $timeout ) = @_;
1056
1057   my $words = random_word;   # only one word for Google
1058   my $page = (int(rand(9)) + 1);
1059   my $num = 20;     # 20 images per page
1060   my $search_url = $google_images_url . $words;
1061
1062   if ($page > 1) {
1063     $search_url .= "&start=" . $page*$num;      # page number
1064     $search_url .= "&num="   . $num;            #images per page
1065   }
1066
1067   my ($search_hit_count, @subpages) =
1068     pick_from_search_engine ($timeout, $search_url, $words);
1069
1070   my @candidates = ();
1071   foreach my $u (@subpages) {
1072     next unless ($u =~ m@imgres\?imgurl@i);    #  All pics start with this
1073     next if ($u =~ m@[/.]google\.com\b@i);     # skip google builtins
1074
1075     if ($u =~ m@^/imgres\?imgurl=(.*?)\&imgrefurl=(.*?)\&@) {
1076       my $urlf = $2;
1077       LOG ($verbose_filter, "  candidate: $urlf");
1078       push @candidates, $urlf;
1079     }
1080   }
1081
1082   return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1083                                 $timeout, @candidates);
1084 }
1085
1086
1087 \f
1088 ############################################################################
1089 #
1090 # Pick images by feeding random *numbers* into Google Image Search.
1091 # By jwz, suggested by from Ian O'Donnell.
1092 #
1093 ############################################################################
1094
1095
1096 # googlenums
1097 sub pick_from_google_image_numbers {
1098   my ( $timeout ) = @_;
1099
1100   my $max = 9999;
1101   my $number = int(rand($max));
1102
1103   $number = sprintf("%04d", $number)
1104     if (rand() < 0.3);
1105
1106   my $words = "$number";
1107   my $page = (int(rand(40)) + 1);
1108   my $num = 20;     # 20 images per page
1109   my $search_url = $google_images_url . $words;
1110
1111   if ($page > 1) {
1112     $search_url .= "&start=" . $page*$num;      # page number
1113     $search_url .= "&num="   . $num;            #images per page
1114   }
1115
1116   my ($search_hit_count, @subpages) =
1117     pick_from_search_engine ($timeout, $search_url, $words);
1118
1119   my @candidates = ();
1120   my %referers;
1121   foreach my $u (@subpages) {
1122     next unless ($u =~ m@imgres\?imgurl@i);    #  All pics start with this
1123     next if ($u =~ m@[/.]google\.com\b@i);     # skip google builtins
1124
1125     if ($u =~ m@^/imgres\?imgurl=(.*?)\&imgrefurl=(.*?)\&@) {
1126       my $ref = $2;
1127       my $img = "http://$1";
1128
1129       LOG ($verbose_filter, "  candidate: $ref");
1130       push @candidates, $img;
1131       $referers{$img} = $ref;
1132     }
1133   }
1134
1135   @candidates = depoison (@candidates);
1136   return () if ($#candidates < 0);
1137   my $i = int(rand($#candidates+1));
1138   my $img = $candidates[$i];
1139   my $ref = $referers{$img};
1140
1141   LOG ($verbose_load, "picked image " . ($i+1) . ": $img (on $ref)");
1142   return ($ref, $img);
1143 }
1144
1145
1146 \f
1147 ############################################################################
1148 #
1149 # Pick images by feeding random words into Alta Vista Text Search
1150 #
1151 ############################################################################
1152
1153
1154 my $alta_vista_url_1 = "http://www.altavista.com/cgi-bin/query?pg=q" .
1155                        "&text=yes&kl=XX&stype=stext&q=";
1156 my $alta_vista_url_2 = "http://www.altavista.com/sites/search/web?pg=q" .
1157                        "&kl=XX&search=Search&q=";
1158
1159 my $alta_vista_url = $alta_vista_url_2;
1160
1161 # altavista
1162 sub pick_from_alta_vista_text {
1163   my ( $timeout ) = @_;
1164
1165   my $words = random_words(1);
1166   my $page = (int(rand(9)) + 1);
1167   my $search_url = $alta_vista_url . $words;
1168
1169   if ($page > 1) {
1170     $search_url .= "&pgno=" . $page;
1171     $search_url .= "&stq=" . (($page-1) * 10);
1172   }
1173
1174   my ($search_hit_count, @subpages) =
1175     pick_from_search_engine ($timeout, $search_url, $words);
1176
1177   my @candidates = ();
1178   foreach my $u (@subpages) {
1179
1180     # Those altavista fuckers are playing really nasty redirection games
1181     # these days: the filter your clicks through their site, but use
1182     # onMouseOver to make it look like they're not!  Well, it makes it
1183     # easier for us to identify search results...
1184     #
1185     next unless ($u =~
1186       m@^/r\?ck_sm=[a-zA-Z0-9]+\&ref=[a-zA-Z0-9]+\&uid=[a-zA-Z0-9]+\&r=(.*)@);
1187     $u = url_unquote($1);
1188
1189     LOG ($verbose_filter, "  candidate: $u");
1190     push @candidates, $u;
1191   }
1192
1193   return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1194                                 $timeout, @candidates);
1195 }
1196
1197
1198 \f
1199 ############################################################################
1200 #
1201 # Pick images by feeding random words into Hotbot
1202 #
1203 ############################################################################
1204
1205 my $hotbot_search_url = "http://hotbot.lycos.com/" .
1206                         "?SM=SC" .
1207                         "&DV=0" .
1208                         "&LG=any" .
1209                         "&FVI=1" .
1210                         "&DC=100" .
1211                         "&DE=0" .
1212                         "&SQ=1" .
1213                         "&TR=13" .
1214                         "&AM1=MC" .
1215                         "&MT=";
1216
1217 sub pick_from_hotbot_text {
1218   my ( $timeout ) = @_;
1219
1220   my $words = random_words(0);
1221   my $search_url = $hotbot_search_url . $words;
1222
1223   my ($search_hit_count, @subpages) =
1224     pick_from_search_engine ($timeout, $search_url, $words);
1225
1226   my @candidates = ();
1227   foreach my $u (@subpages) {
1228
1229     # Hotbot plays redirection games too
1230     next unless ($u =~ m@^/director.asp\?target=([^&]+)@);
1231     $u = url_decode($1);
1232
1233     LOG ($verbose_filter, "  candidate: $u");
1234     push @candidates, $u;
1235   }
1236
1237   return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1238                                 $timeout, @candidates);
1239 }
1240
1241
1242 \f
1243 ############################################################################
1244 #
1245 # Pick images by feeding random words into Lycos
1246 #
1247 ############################################################################
1248
1249 my $lycos_search_url = "http://lycospro.lycos.com/srchpro/" .
1250                        "?lpv=1" .
1251                        "&t=any" .
1252                        "&query=";
1253
1254 sub pick_from_lycos_text {
1255   my ( $timeout ) = @_;
1256
1257   my $words = random_words(0);
1258   my $start = int(rand(8)) * 10 + 1;
1259   my $search_url = $lycos_search_url . $words . "&start=$start";
1260
1261   my ($search_hit_count, @subpages) =
1262     pick_from_search_engine ($timeout, $search_url, $words);
1263
1264   my @candidates = ();
1265   foreach my $u (@subpages) {
1266
1267     # Lycos plays exact the same redirection game as hotbot.
1268     # Note that "id=0" is used for internal advertising links,
1269     # and 1+ are used for  search results.
1270     next unless ($u =~ m@^http://click.hotbot.com/director.asp\?id=[1-9]\d*&target=([^&]+)@);
1271     $u = url_decode($1);
1272
1273     LOG ($verbose_filter, "  candidate: $u");
1274     push @candidates, $u;
1275   }
1276
1277   return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1278                                 $timeout, @candidates);
1279 }
1280
1281
1282 \f
1283 ############################################################################
1284 #
1285 # Pick images by feeding random words into news.yahoo.com
1286 #
1287 ############################################################################
1288
1289 my $yahoo_news_url = "http://search.news.yahoo.com/search/news_photos?" .
1290                      "&z=&n=100&o=o&2=&3=&p=";
1291
1292 # yahoonews
1293 sub pick_from_yahoo_news_text {
1294   my ( $timeout ) = @_;
1295
1296   my $words = random_words(1);
1297   my $search_url = $yahoo_news_url . $words;
1298
1299   my ($search_hit_count, @subpages) =
1300     pick_from_search_engine ($timeout, $search_url, $words);
1301
1302   my @candidates = ();
1303   foreach my $u (@subpages) {
1304     # only accept URLs on Yahoo's news site
1305     next unless ($u =~ m@^http://dailynews\.yahoo\.com/@i ||
1306                  $u =~ m@^http://story\.news\.yahoo\.com/@i);
1307
1308     LOG ($verbose_filter, "  candidate: $u");
1309     push @candidates, $u;
1310   }
1311
1312   return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1313                                 $timeout, @candidates);
1314 }
1315
1316
1317
1318 \f
1319 ############################################################################
1320 #
1321 # Pick a random image in a random way
1322 #
1323 ############################################################################
1324
1325
1326 # Picks a random image on a random page, and returns two URLs:
1327 # the page containing the image, and the image.
1328 # Returns () if nothing found this time.
1329 # Uses the url-randomizer 1 time in 5, else the image randomizer.
1330 #
1331
1332 sub pick_image {
1333   my ( $timeout ) = @_;
1334
1335   $current_state = "select";
1336   $load_method = "none";
1337
1338   my $n = int(rand(100));
1339   my $fn = undef;
1340   my $total = 0;
1341   my @rest = @search_methods;
1342
1343   while (@rest) {
1344     my $pct  = shift @rest;
1345     my $name = shift @rest;
1346     my $tfn  = shift @rest;
1347     $total += $pct;
1348     if ($total > $n && !defined($fn)) {
1349       $fn = $tfn;
1350       $current_state = $name;
1351       $load_method = $current_state;
1352     }
1353   }
1354
1355   if ($total != 100) {
1356     error ("internal error: \@search_methods totals to $total%!");
1357   }
1358
1359   record_attempt ($current_state);
1360   return $fn->($timeout);
1361 }
1362
1363
1364 \f
1365 ############################################################################
1366 #
1367 # Statistics and logging
1368 #
1369 ############################################################################
1370
1371 sub timestr {
1372   return strftime ("%H:%M:%S: ", localtime);
1373 }
1374
1375 sub blurb {
1376   return "$progname: " . timestr() . "$current_state: ";
1377 }
1378
1379 sub error {
1380   my ($err) = @_;
1381   print STDERR blurb() . "$err\n";
1382   exit 1;
1383 }
1384
1385
1386 my $lastlog = "";
1387
1388 sub clearlog {
1389   $lastlog = "";
1390 }
1391
1392 sub showlog {
1393   my $head = "$progname: DEBUG: ";
1394   foreach (split (/\n/, $lastlog)) {
1395     print STDERR "$head$_\n";
1396   }
1397   $lastlog = "";
1398 }
1399
1400 sub LOG {
1401   my ($print, $msg) = @_;
1402   my $blurb = timestr() . "$current_state: ";
1403   $lastlog .= "$blurb$msg\n";
1404   print STDERR "$progname: $blurb$msg\n" if $print;
1405 }
1406
1407
1408 my %stats_attempts;
1409 my %stats_successes;
1410 my %stats_elapsed;
1411
1412 my $last_state = undef;
1413 sub record_attempt {
1414   my ($name) = @_;
1415
1416   if ($last_state) {
1417     record_failure($last_state) unless ($image_succeeded > 0);
1418   }
1419   $last_state = $name;
1420
1421   clearlog();
1422   report_performance();
1423
1424   start_timer($name);
1425   $image_succeeded = 0;
1426   $suppress_audit = 0;
1427 }
1428
1429 sub record_success {
1430   my ($name, $url, $base) = @_;
1431   if (defined($stats_successes{$name})) {
1432     $stats_successes{$name}++;
1433   } else {
1434     $stats_successes{$name} = 1;
1435   }
1436
1437   stop_timer ($name, 1);
1438   my $o = $current_state;
1439   $current_state = $name;
1440   save_recent_url ($url, $base);
1441   $current_state = $o;
1442   $image_succeeded = 1;
1443   clearlog();
1444 }
1445
1446
1447 sub record_failure {
1448   my ($name) = @_;
1449
1450   return if $image_succeeded;
1451
1452   stop_timer ($name, 0);
1453   if ($verbose_load && !$verbose_exec) {
1454
1455     if ($suppress_audit) {
1456       print STDERR "$progname: " . timestr() . "(audit log suppressed)\n";
1457       return;
1458     }
1459
1460     my $o = $current_state;
1461     $current_state = "DEBUG";
1462
1463     my $line =  "#" x 78;
1464     print STDERR "\n\n\n";
1465     print STDERR ("#" x 78) . "\n";
1466     print STDERR blurb() . "failed to get an image.  Full audit log:\n";
1467     print STDERR "\n";
1468     showlog();
1469     print STDERR ("-" x 78) . "\n";
1470     print STDERR "\n\n";
1471
1472     $current_state = $o;
1473   }
1474   $image_succeeded = 0;
1475 }
1476
1477
1478
1479 sub stats_of {
1480   my ($name) = @_;
1481   my $i = $stats_successes{$name};
1482   my $j = $stats_attempts{$name};
1483   $i = 0 unless $i;
1484   $j = 0 unless $j;
1485   return "" . ($j ? int($i * 100 / $j) : "0") . "%";
1486 }
1487
1488
1489 my $current_start_time = 0;
1490
1491 sub start_timer {
1492   my ($name) = @_;
1493   $current_start_time = time;
1494
1495   if (defined($stats_attempts{$name})) {
1496     $stats_attempts{$name}++;
1497   } else {
1498     $stats_attempts{$name} = 1;
1499   }
1500   if (!defined($stats_elapsed{$name})) {
1501     $stats_elapsed{$name} = 0;
1502   }
1503 }
1504
1505 sub stop_timer {
1506   my ($name, $success) = @_;
1507   $stats_elapsed{$name} += time - $current_start_time;
1508 }
1509
1510
1511 my $last_report_time = 0;
1512 sub report_performance {
1513
1514   return unless $verbose_warnings;
1515
1516   my $now = time;
1517   return unless ($now >= $last_report_time + $report_performance_interval);
1518   my $ot = $last_report_time;
1519   $last_report_time = $now;
1520
1521   return if ($ot == 0);
1522
1523   my $blurb = "$progname: " . timestr();
1524
1525   print STDERR "\n";
1526   print STDERR "${blurb}Current standings:\n";
1527
1528   foreach my $name (sort keys (%stats_attempts)) {
1529     my $try = $stats_attempts{$name};
1530     my $suc = $stats_successes{$name} || 0;
1531     my $pct = int($suc * 100 / $try);
1532     my $secs = $stats_elapsed{$name};
1533     my $secs_link = int($secs / $try);
1534     print STDERR sprintf ("$blurb   %-12s %4s (%d/%d);\t %2d secs/link\n",
1535                           "$name:", "$pct%", $suc, $try, $secs_link);
1536   }
1537 }
1538
1539
1540
1541 my $max_recent_images = 400;
1542 my $max_recent_sites  = 20;
1543 my @recent_images = ();
1544 my @recent_sites = ();
1545
1546 sub save_recent_url {
1547   my ($url, $base) = @_;
1548
1549   return unless ($verbose_warnings);
1550
1551   $_ = $url;
1552   my ($site) = m@^http://([^ \t\n\r/:]+)@;
1553
1554   my $done = 0;
1555   foreach (@recent_images) {
1556     if ($_ eq $url) {
1557       print STDERR blurb() . "WARNING: recently-duplicated image: $url" .
1558         " (on $base via $last_search)\n";
1559       $done = 1;
1560       last;
1561     }
1562   }
1563
1564   # suppress "duplicate site" warning via %warningless_sites.
1565   #
1566   if ($warningless_sites{$site}) {
1567     $done = 1;
1568   } elsif ($site =~ m@([^.]+\.[^.]+\.[^.]+)$@ &&
1569            $warningless_sites{$1}) {
1570     $done = 1;
1571   } elsif ($site =~ m@([^.]+\.[^.]+)$@ &&
1572            $warningless_sites{$1}) {
1573     $done = 1;
1574   }
1575
1576   if (!$done) {
1577     foreach (@recent_sites) {
1578       if ($_ eq $site) {
1579         print STDERR blurb() . "WARNING: recently-duplicated site: $site" .
1580         " ($url on $base via $last_search)\n";
1581         last;
1582       }
1583     }
1584   }
1585
1586   push @recent_images, $url;
1587   push @recent_sites,  $site;
1588   shift @recent_images if ($#recent_images >= $max_recent_images);
1589   shift @recent_sites  if ($#recent_sites  >= $max_recent_sites);
1590 }
1591
1592
1593 \f
1594 ##############################################################################
1595 #
1596 # other utilities
1597 #
1598 ##############################################################################
1599
1600 # Does %-decoding.
1601 #
1602 sub url_decode {
1603   ($_) = @_;
1604   tr/+/ /;
1605   s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
1606   return $_;
1607 }
1608
1609
1610 # Given the raw body of a GIF document, returns the dimensions of the image.
1611 #
1612 sub gif_size {
1613   my ($body) = @_;
1614   my $type = substr($body, 0, 6);
1615   my $s;
1616   return () unless ($type =~ /GIF8[7,9]a/);
1617   $s = substr ($body, 6, 10);
1618   my ($a,$b,$c,$d) = unpack ("C"x4, $s);
1619   return (($b<<8|$a), ($d<<8|$c));
1620 }
1621
1622 # Given the raw body of a JPEG document, returns the dimensions of the image.
1623 #
1624 sub jpeg_size {
1625   my ($body) = @_;
1626   my $i = 0;
1627   my $L = length($body);
1628
1629   my $c1 = substr($body, $i, 1); $i++;
1630   my $c2 = substr($body, $i, 1); $i++;
1631   return () unless (ord($c1) == 0xFF && ord($c2) == 0xD8);
1632
1633   my $ch = "0";
1634   while (ord($ch) != 0xDA && $i < $L) {
1635     # Find next marker, beginning with 0xFF.
1636     while (ord($ch) != 0xFF) {
1637       return () if (length($body) <= $i);
1638       $ch = substr($body, $i, 1); $i++;
1639     }
1640     # markers can be padded with any number of 0xFF.
1641     while (ord($ch) == 0xFF) {
1642       return () if (length($body) <= $i);
1643       $ch = substr($body, $i, 1); $i++;
1644     }
1645
1646     # $ch contains the value of the marker.
1647     my $marker = ord($ch);
1648
1649     if (($marker >= 0xC0) &&
1650         ($marker <= 0xCF) &&
1651         ($marker != 0xC4) &&
1652         ($marker != 0xCC)) {  # it's a SOFn marker
1653       $i += 3;
1654       return () if (length($body) <= $i);
1655       my $s = substr($body, $i, 4); $i += 4;
1656       my ($a,$b,$c,$d) = unpack("C"x4, $s);
1657       return (($c<<8|$d), ($a<<8|$b));
1658
1659     } else {
1660       # We must skip variables, since FFs in variable names aren't
1661       # valid JPEG markers.
1662       return () if (length($body) <= $i);
1663       my $s = substr($body, $i, 2); $i += 2;
1664       my ($c1, $c2) = unpack ("C"x2, $s);
1665       my $length = ($c1 << 8) | $c2;
1666       return () if ($length < 2);
1667       $i += $length-2;
1668     }
1669   }
1670   return ();
1671 }
1672
1673 # Given the raw body of a GIF or JPEG document, returns the dimensions of
1674 # the image.
1675 #
1676 sub image_size {
1677   my ($body) = @_;
1678   my ($w, $h) = gif_size ($body);
1679   if ($w && $h) { return ($w, $h); }
1680   return jpeg_size ($body);
1681 }
1682
1683
1684 # returns the full path of the named program, or undef.
1685 #
1686 sub which {
1687   my ($prog) = @_;
1688   foreach (split (/:/, $ENV{PATH})) {
1689     if (-x "$_/$prog") {
1690       return $prog;
1691     }
1692   }
1693   return undef;
1694 }
1695
1696
1697 # Like rand(), but chooses numbers with a bell curve distribution.
1698 sub bellrand {
1699   ($_) = @_;
1700   $_ = 1.0 unless defined($_);
1701   $_ /= 3.0;
1702   return (rand($_) + rand($_) + rand($_));
1703 }
1704
1705
1706 ##############################################################################
1707 #
1708 # Generating a list of urls only
1709 #
1710 ##############################################################################
1711
1712 sub url_only_output {
1713   do {
1714     my ($base, $img) = pick_image;
1715     if ($img) {
1716       $base =~ s/ /%20/g;
1717       $img  =~ s/ /%20/g;
1718       print "$img $base\n";
1719     }
1720   } while (1);
1721 }
1722
1723 ##############################################################################
1724 #
1725 # Running as an xscreensaver module
1726 #
1727 ##############################################################################
1728
1729 my $image_ppm   = ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp") . "/webcollage." . $$;
1730 my $image_tmp1  = $image_ppm . "-1";
1731 my $image_tmp2  = $image_ppm . "-2";
1732
1733 my $filter_cmd = undef;
1734 my $post_filter_cmd = undef;
1735 my $background = undef;
1736
1737 my $img_width;            # size of the image being generated.
1738 my $img_height;
1739
1740 my $delay = 2;
1741
1742
1743 sub x_cleanup {
1744   my ($sig) = @_;
1745   print STDERR blurb() . "caught signal $sig.\n" if ($verbose_exec);
1746   unlink $image_ppm, $image_tmp1, $image_tmp2;
1747   exit 1;
1748 }
1749
1750
1751 # Like system, but prints status about exit codes, and kills this process
1752 # with whatever signal killed the sub-process, if any.
1753 #
1754 sub nontrapping_system {
1755   $! = 0;
1756
1757   $_ = join(" ", @_);
1758   s/\"[^\"]+\"/\"...\"/g;
1759
1760   LOG ($verbose_exec, "executing \"$_\"");
1761
1762   my $rc = system @_;
1763
1764   if ($rc == 0) {
1765     LOG ($verbose_exec, "subproc exited normally.");
1766   } elsif (($rc & 0xff) == 0) {
1767     $rc >>= 8;
1768     LOG ($verbose_exec, "subproc exited with status $rc.");
1769   } else {
1770     if ($rc & 0x80) {
1771       LOG ($verbose_exec, "subproc dumped core.");
1772       $rc &= ~0x80;
1773     }
1774     LOG ($verbose_exec, "subproc died with signal $rc.");
1775     # die that way ourselves.
1776     kill $rc, $$;
1777   }
1778
1779   return $rc;
1780 }
1781
1782
1783 # Given the URL of a GIF or JPEG image, and the body of that image, writes a
1784 # PPM to the given output file.  Returns the width/height of the image if
1785 # successful.
1786 #
1787 sub image_to_pnm {
1788   my ($url, $body, $output) = @_;
1789   my ($cmd, $cmd2, $w, $h);
1790
1791   if ((@_ = gif_size ($body))) {
1792     ($w, $h) = @_;
1793     $cmd = "giftopnm";
1794   } elsif ((@_ = jpeg_size ($body))) {
1795     ($w, $h) = @_;
1796     $cmd = "djpeg";
1797   } else {
1798     LOG (($verbose_pbm || $verbose_load),
1799          "not a GIF or JPG" .
1800          (($body =~ m@<(base|html|head|body|script|table|a href)>@i)
1801           ? " (looks like HTML)" : "") .
1802          ": $url");
1803     $suppress_audit = 1;
1804     return ();
1805   }
1806
1807   $cmd2 = "exec $cmd";        # yes, this really is necessary.  if we don't
1808                               # do this, the process doesn't die properly.
1809   if (!$verbose_pbm) {
1810     #
1811     # We get a "giftopnm: got a 'Application Extension' extension"
1812     # warning any time it's an animgif.
1813     #
1814     # Note that "giftopnm: EOF / read error on image data" is not
1815     # always a fatal error -- sometimes the image looks fine anyway.
1816     #
1817     $cmd2 .= " 2>/dev/null";
1818   }
1819
1820   # There exist corrupted GIF and JPEG files that can make giftopnm and
1821   # djpeg lose their minds and go into a loop.  So this gives those programs
1822   # a small timeout -- if they don't complete in time, kill them.
1823   #
1824   my $pid;
1825   @_ = eval {
1826     my $timed_out;
1827
1828     local $SIG{ALRM}  = sub {
1829       LOG ($verbose_pbm,
1830            "timed out ($cvt_timeout) for $cmd on \"$url\" in pid $pid");
1831       kill ('TERM', $pid) if ($pid);
1832       $timed_out = 1;
1833       $body = undef;
1834     };
1835
1836     if (($pid = open(PIPE, "| $cmd2 > $output"))) {
1837       $timed_out = 0;
1838       alarm $cvt_timeout;
1839       print PIPE $body;
1840       $body = undef;
1841       close PIPE;
1842
1843       LOG ($verbose_exec, "awaiting $pid");
1844       waitpid ($pid, 0);
1845       LOG ($verbose_exec, "$pid completed");
1846
1847       my $size = (stat($output))[7];
1848       $size = -1 unless defined($size);
1849       if ($size < 5) {
1850         LOG ($verbose_pbm, "$cmd on ${w}x$h \"$url\" failed ($size bytes)");
1851         return ();
1852       }
1853
1854       LOG ($verbose_pbm, "created ${w}x$h $output ($cmd)");
1855       return ($w, $h);
1856     } else {
1857       print STDERR blurb() . "$cmd failed: $!\n";
1858       return ();
1859     }
1860   };
1861   die if ($@ && $@ ne "alarm\n");       # propagate errors
1862   if ($@) {
1863     # timed out
1864     $body = undef;
1865     return ();
1866   } else {
1867     # didn't
1868     alarm 0;
1869     $body = undef;
1870     return @_;
1871   }
1872 }
1873
1874 sub pick_root_displayer {
1875   my @names = ();
1876
1877   foreach my $cmd (@root_displayers) {
1878     $_ = $cmd;
1879     my ($name) = m/^([^ ]+)/;
1880     push @names, "\"$name\"";
1881     LOG ($verbose_exec, "looking for $name...");
1882     foreach my $dir (split (/:/, $ENV{PATH})) {
1883       LOG ($verbose_exec, "  checking $dir/$name");
1884       return $cmd if (-x "$dir/$name");
1885     }
1886   }
1887
1888   $names[$#names] = "or " . $names[$#names];
1889   error "none of: " . join (", ", @names) . " were found on \$PATH.";
1890 }
1891
1892
1893 my $ppm_to_root_window_cmd = undef;
1894
1895
1896 sub x_or_pbm_output {
1897
1898   # Check for our helper program, to see whether we need to use PPM pipelines.
1899   #
1900   $_ = "webcollage-helper";
1901   if (defined ($webcollage_helper) || which ($_)) {
1902     $webcollage_helper = $_ unless (defined($webcollage_helper));
1903     LOG ($verbose_pbm, "found \"$webcollage_helper\"");
1904     $webcollage_helper .= " -v";
1905   } else {
1906     LOG (($verbose_pbm || $verbose_load), "no $_ program");
1907   }
1908
1909   # make sure the various programs we execute exist, right up front.
1910   #
1911   my @progs = ("ppmmake");  # always need this one
1912
1913   if (!defined($webcollage_helper)) {
1914     # Only need these others if we don't have the helper.
1915     @progs = (@progs, "giftopnm", "djpeg", "pnmpaste", "pnmscale", "pnmcut");
1916   }
1917
1918   foreach (@progs) {
1919     which ($_) || error "$_ not found on \$PATH.";
1920   }
1921
1922   # find a root-window displayer program.
1923   #
1924   $ppm_to_root_window_cmd = pick_root_displayer();
1925
1926
1927   $SIG{HUP}  = \&x_cleanup;
1928   $SIG{INT}  = \&x_cleanup;
1929   $SIG{QUIT} = \&x_cleanup;
1930   $SIG{ABRT} = \&x_cleanup;
1931   $SIG{KILL} = \&x_cleanup;
1932   $SIG{TERM} = \&x_cleanup;
1933
1934   # Need this so that if giftopnm dies, we don't die.
1935   $SIG{PIPE} = 'IGNORE';
1936
1937   if (!$img_width || !$img_height) {
1938     $_ = "xdpyinfo";
1939     which ($_) || error "$_ not found on \$PATH.";
1940     $_ = `$_`;
1941     ($img_width, $img_height) = m/dimensions: *(\d+)x(\d+) /;
1942     if (!defined($img_height)) {
1943       error "xdpyinfo failed.";
1944     }
1945   }
1946
1947   my $bgcolor = "#000000";
1948   my $bgimage = undef;
1949
1950   if ($background) {
1951     if ($background =~ m/^\#[0-9a-f]+$/i) {
1952       $bgcolor = $background;
1953
1954     } elsif (-r $background) {
1955       $bgimage = $background;
1956
1957     } elsif (! $background =~ m@^[-a-z0-9 ]+$@i) {
1958       error "not a color or readable file: $background";
1959
1960     } else {
1961       # default to assuming it's a color
1962       $bgcolor = $background;
1963     }
1964   }
1965
1966   # Create the sold-colored base image.
1967   #
1968   $_ = "ppmmake '$bgcolor' $img_width $img_height";
1969   LOG ($verbose_pbm, "creating base image: $_");
1970   nontrapping_system "$_ > $image_ppm";
1971
1972   # Paste the default background image in the middle of it.
1973   #
1974   if ($bgimage) {
1975     my ($iw, $ih);
1976
1977     my $body = "";
1978     local *IMG;
1979     open(IMG, "<$bgimage") || error "couldn't open $bgimage: $!";
1980     my $cmd;
1981     while (<IMG>) { $body .= $_; }
1982     close (IMG);
1983
1984     if ((@_ = gif_size ($body))) {
1985       ($iw, $ih) = @_;
1986       $cmd = "giftopnm |";
1987
1988     } elsif ((@_ = jpeg_size ($body))) {
1989       ($iw, $ih) = @_;
1990       $cmd = "djpeg |";
1991
1992     } elsif ($body =~ m/^P\d\n(\d+) (\d+)\n/) {
1993       $iw = $1;
1994       $ih = $2;
1995       $cmd = "";
1996
1997     } else {
1998       error "$bgimage is not a GIF, JPEG, or PPM.";
1999     }
2000
2001     my $x = int (($img_width  - $iw) / 2);
2002     my $y = int (($img_height - $ih) / 2);
2003     LOG ($verbose_pbm,
2004          "pasting $bgimage (${iw}x$ih) into base image at $x,$y");
2005
2006     $cmd .= "pnmpaste - $x $y $image_ppm > $image_tmp1";
2007     open (IMG, "| $cmd") || error "running $cmd: $!";
2008     print IMG $body;
2009     $body = undef;
2010     close (IMG);
2011     LOG ($verbose_exec, "subproc exited normally.");
2012     rename ($image_tmp1, $image_ppm) ||
2013       error "renaming $image_tmp1 to $image_ppm: $!";
2014   }
2015
2016   clearlog();
2017
2018   while (1) {
2019     my ($base, $img) = pick_image();
2020     my $source = $current_state;
2021     $current_state = "loadimage";
2022     if ($img) {
2023       my ($headers, $body) = get_document ($img, $base);
2024       if ($body) {
2025         paste_image ($base, $img, $body, $source);
2026         $body = undef;
2027       }
2028     }
2029     $current_state = "idle";
2030     $load_method = "none";
2031
2032     unlink $image_tmp1, $image_tmp2;
2033     sleep $delay;
2034   }
2035 }
2036
2037 sub paste_image {
2038   my ($base, $img, $body, $source) = @_;
2039
2040   $current_state = "paste";
2041
2042   $suppress_audit = 0;
2043
2044   LOG ($verbose_pbm, "got $img (" . length($body) . ")");
2045
2046   my ($iw, $ih);
2047
2048   # If we are using the webcollage-helper, then we do not need to convert this
2049   # image to a PPM.  But, if we're using a filter command, we still must, since
2050   # that's what the filters expect (webcollage-helper can read PPMs, so that's
2051   # fine.)
2052   #
2053   if (defined ($webcollage_helper) &&
2054       !defined ($filter_cmd)) {
2055
2056     ($iw, $ih) = image_size ($body);
2057     if (!$iw || !$ih) {
2058       LOG (($verbose_pbm || $verbose_load),
2059            "not a GIF or JPG" .
2060            (($body =~ m@<(base|html|head|body|script|table|a href)>@i)
2061             ? " (looks like HTML)" : "") .
2062            ": $img");
2063       $suppress_audit = 1;
2064       $body = undef;
2065       return 0;
2066     }
2067
2068     local *OUT;
2069     open (OUT, ">$image_tmp1") || error ("writing $image_tmp1: $!");
2070     print OUT $body || error ("writing $image_tmp1: $!");
2071     close OUT || error ("writing $image_tmp1: $!");
2072
2073   } else {
2074     ($iw, $ih) = image_to_pnm ($img, $body, $image_tmp1);
2075     $body = undef;
2076     if (!$iw || !$ih) {
2077       LOG ($verbose_pbm, "unable to make PBM from $img");
2078       return 0;
2079     }
2080   }
2081
2082   record_success ($load_method, $img, $base);
2083
2084
2085   my $ow = $iw;  # used only for error messages
2086   my $oh = $ih;
2087
2088   # don't just tack this onto the front of the pipeline -- we want it to
2089   # be able to change the size of the input image.
2090   #
2091   if ($filter_cmd) {
2092     LOG ($verbose_pbm, "running $filter_cmd");
2093
2094     my $rc = nontrapping_system "($filter_cmd) < $image_tmp1 >$image_tmp2";
2095     if ($rc != 0) {
2096       LOG(($verbose_pbm || $verbose_load), "failed command: \"$filter_cmd\"");
2097       LOG(($verbose_pbm || $verbose_load), "failed URL: \"$img\" (${ow}x$oh)");
2098       return;
2099     }
2100     rename ($image_tmp2, $image_tmp1);
2101
2102     # re-get the width/height in case the filter resized it.
2103     local *IMG;
2104     open(IMG, "<$image_tmp1") || return 0;
2105     $_ = <IMG>;
2106     $_ = <IMG>;
2107     ($iw, $ih) = m/^(\d+) (\d+)$/;
2108     close (IMG);
2109     return 0 unless ($iw && $ih);
2110   }
2111
2112   my $target_w = $img_width;
2113   my $target_h = $img_height;
2114
2115   my $cmd = "";
2116   my $scale = 1.0;
2117
2118
2119   # Usually scale the image to fit on the screen -- but sometimes scale it
2120   # to fit on half or a quarter of the screen.  Note that we don't merely
2121   # scale it to fit, we instead cut it in half until it fits -- that should
2122   # give a wider distribution of sizes.
2123   #
2124   if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; $scale /= 2; }
2125   if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; $scale /= 2; }
2126
2127   if ($iw > $target_w || $ih > $target_h) {
2128     while ($iw > $target_w ||
2129            $ih > $target_h) {
2130       $iw = int($iw / 2);
2131       $ih = int($ih / 2);
2132     }
2133     if ($iw <= 10 || $ih <= 10) {
2134       LOG ($verbose_pbm, "scaling to ${iw}x$ih would have been bogus.");
2135       return 0;
2136     }
2137
2138     LOG ($verbose_pbm, "scaling to ${iw}x$ih");
2139
2140     $cmd .= " | pnmscale -xsize $iw -ysize $ih";
2141   }
2142
2143
2144   my $src = $image_tmp1;
2145
2146   my $crop_x = 0;     # the sub-rectangle of the image
2147   my $crop_y = 0;     # that we will actually paste.
2148   my $crop_w = $iw;
2149   my $crop_h = $ih;
2150
2151   # The chance that we will randomly crop out a section of an image starts
2152   # out fairly low, but goes up for images that are very large, or images
2153   # that have ratios that make them look like banners (we try to avoid
2154   # banner images entirely, but they slip through when the IMG tags didn't
2155   # have WIDTH and HEIGHT specified.)
2156   #
2157   my $crop_chance = 0.2;
2158   if ($iw > $img_width * 0.4 || $ih > $img_height * 0.4) {
2159     $crop_chance += 0.2;
2160   }
2161   if ($iw > $img_width * 0.7 || $ih > $img_height * 0.7) {
2162     $crop_chance += 0.2;
2163   }
2164   if ($min_ratio && ($iw * $min_ratio) > $ih) {
2165     $crop_chance += 0.7;
2166   }
2167
2168   if ($crop_chance > 0.1) {
2169     LOG ($verbose_pbm, "crop chance: $crop_chance");
2170   }
2171
2172   if (rand() < $crop_chance) {
2173
2174     my $ow = $crop_w;
2175     my $oh = $crop_h;
2176
2177     if ($crop_w > $min_width) {
2178       # if it's a banner, select the width linearly.
2179       # otherwise, select a bell.
2180       my $r = (($min_ratio && ($iw * $min_ratio) > $ih)
2181                ? rand()
2182                : bellrand());
2183       $crop_w = $min_width + int ($r * ($crop_w - $min_width));
2184       $crop_x = int (rand() * ($ow - $crop_w));
2185     }
2186     if ($crop_h > $min_height) {
2187       # height always selects as a bell.
2188       $crop_h = $min_height + int (bellrand() * ($crop_h - $min_height));
2189       $crop_y = int (rand() * ($oh - $crop_h));
2190     }
2191
2192     if ($crop_x != 0   || $crop_y != 0 ||
2193         $crop_w != $iw || $crop_h != $ih) {
2194       LOG ($verbose_pbm,
2195            "randomly cropping to ${crop_w}x$crop_h \@ $crop_x,$crop_y");
2196     }
2197   }
2198
2199   # Where the image should logically land -- this might be negative.
2200   #
2201   my $x = int((rand() * ($img_width  + $crop_w/2)) - $crop_w*3/4);
2202   my $y = int((rand() * ($img_height + $crop_h/2)) - $crop_h*3/4);
2203
2204   # if we have chosen to paste the image outside of the rectangle of the
2205   # screen, then we need to crop it.
2206   #
2207   if ($x < 0 ||
2208       $y < 0 ||
2209       $x + $crop_w > $img_width ||
2210       $y + $crop_h > $img_height) {
2211
2212     LOG ($verbose_pbm,
2213          "cropping for effective paste of ${crop_w}x$crop_h \@ $x,$y");
2214
2215     if ($x < 0) { $crop_x -= $x; $crop_w += $x; $x = 0; }
2216     if ($y < 0) { $crop_y -= $y; $crop_h += $y; $y = 0; }
2217
2218     if ($x + $crop_w >= $img_width)  { $crop_w = $img_width  - $x - 1; }
2219     if ($y + $crop_h >= $img_height) { $crop_h = $img_height - $y - 1; }
2220   }
2221
2222   # If any cropping needs to happen, add pnmcut.
2223   #
2224   if ($crop_x != 0   || $crop_y != 0 ||
2225         $crop_w != $iw || $crop_h != $ih) {
2226     $iw = $crop_w;
2227     $ih = $crop_h;
2228     $cmd .= " | pnmcut $crop_x $crop_y $iw $ih";
2229     LOG ($verbose_pbm, "cropping to ${crop_w}x$crop_h \@ $crop_x,$crop_y");
2230   }
2231
2232   LOG ($verbose_pbm, "pasting ${iw}x$ih \@ $x,$y in $image_ppm");
2233
2234   $cmd .= " | pnmpaste - $x $y $image_ppm";
2235
2236   $cmd =~ s@^ *\| *@@;
2237
2238   if (defined ($webcollage_helper)) {
2239     $cmd = "$webcollage_helper $image_tmp1 $image_ppm " .
2240                               "$scale $opacity " .
2241                               "$crop_x $crop_y $x $y " .
2242                               "$iw $ih";
2243     $_ = $cmd;
2244
2245   } else {
2246     # use a PPM pipeline
2247     $_ = "($cmd)";
2248     $_ .= " < $image_tmp1 > $image_tmp2";
2249   }
2250
2251   if ($verbose_pbm) {
2252     $_ = "($_) 2>&1 | sed s'/^/" . blurb() . "/'";
2253   } else {
2254     $_ .= " 2> /dev/null";
2255   }
2256
2257   my $rc = nontrapping_system ($_);
2258
2259   if (defined ($webcollage_helper) && -z $image_ppm) {
2260     LOG (1, "failed command: \"$cmd\"");
2261     print STDERR "\naudit log:\n\n\n";
2262     print STDERR ("#" x 78) . "\n";
2263     print STDERR blurb() . "$image_ppm has zero size\n";
2264     showlog();
2265     print STDERR "\n\n";
2266     exit (1);
2267   }
2268
2269   if ($rc != 0) {
2270     LOG (($verbose_pbm || $verbose_load), "failed command: \"$cmd\"");
2271     LOG (($verbose_pbm || $verbose_load), "failed URL: \"$img\" (${ow}x$oh)");
2272     return;
2273   }
2274
2275   if (!defined ($webcollage_helper)) {
2276     rename ($image_tmp2, $image_ppm) || return;
2277   }
2278
2279   my $target = "$image_ppm";
2280
2281   # don't just tack this onto the end of the pipeline -- we don't want it
2282   # to end up in $image_ppm, because we don't want the results to be
2283   # cumulative.
2284   #
2285   if ($post_filter_cmd) {
2286
2287     my $cmd;
2288
2289     $target = $image_tmp1;
2290     if (!defined ($webcollage_helper)) {
2291       $cmd = "($post_filter_cmd) < $image_ppm > $target";
2292     } else {
2293       # Blah, my scripts need the JPEG data, but some other folks need
2294       # the PPM data -- what to do?  Ignore the problem, that's what!
2295 #     $cmd = "djpeg < $image_ppm | ($post_filter_cmd) > $target";
2296       $cmd = "($post_filter_cmd) < $image_ppm > $target";
2297     }
2298
2299     $rc = nontrapping_system ($cmd);
2300     if ($rc != 0) {
2301       LOG ($verbose_pbm, "filter failed: \"$post_filter_cmd\"\n");
2302       return;
2303     }
2304   }
2305
2306   if (!$no_output_p) {
2307     my $tsize = (stat($target))[7];
2308     if ($tsize > 200) {
2309       $cmd = "$ppm_to_root_window_cmd $target";
2310
2311       # xv seems to hate being killed.  it tends to forget to clean
2312       # up after itself, and leaves windows around and colors allocated.
2313       # I had this same problem with vidwhacker, and I'm not entirely
2314       # sure what I did to fix it.  But, let's try this: launch xv
2315       # in the background, so that killing this process doesn't kill it.
2316       # it will die of its own accord soon enough.  So this means we
2317       # start pumping bits to the root window in parallel with starting
2318       # the next network retrieval, which is probably a better thing
2319       # to do anyway.
2320       #
2321       $cmd .= " &";
2322
2323       $rc = nontrapping_system ($cmd);
2324
2325       if ($rc != 0) {
2326         LOG (($verbose_pbm || $verbose_load), "display failed: \"$cmd\"");
2327         return;
2328       }
2329
2330     } else {
2331       LOG ($verbose_pbm, "$target size is $tsize");
2332     }
2333   }
2334
2335   $source .= "-" . stats_of($source);
2336   print STDOUT "image: ${iw}x${ih} @ $x,$y $base $source\n"
2337     if ($verbose_imgmap);
2338
2339   clearlog();
2340
2341   return 1;
2342 }
2343
2344
2345 sub main {
2346   $| = 1;
2347   srand(time ^ $$);
2348
2349   my $verbose = 0;
2350   my $dict;
2351
2352   $current_state = "init";
2353   $load_method = "none";
2354
2355   my $root_p = 0;
2356
2357   # historical suckage: the environment variable name is lower case.
2358   $http_proxy = $ENV{http_proxy} || $ENV{HTTP_PROXY};
2359
2360   while ($_ = $ARGV[0]) {
2361     shift @ARGV;
2362     if ($_ eq "-display" ||
2363         $_ eq "-displ" ||
2364         $_ eq "-disp" ||
2365         $_ eq "-dis" ||
2366         $_ eq "-dpy" ||
2367         $_ eq "-d") {
2368       $ENV{DISPLAY} = shift @ARGV;
2369     } elsif ($_ eq "-root") {
2370       $root_p = 1;
2371     } elsif ($_ eq "-no-output") {
2372       $no_output_p = 1;
2373     } elsif ($_ eq "-urls-only") {
2374       $urls_only_p = 1;
2375       $no_output_p = 1;
2376     } elsif ($_ eq "-verbose") {
2377       $verbose++;
2378     } elsif (m/^-v+$/) {
2379       $verbose += length($_)-1;
2380     } elsif ($_ eq "-delay") {
2381       $delay = shift @ARGV;
2382     } elsif ($_ eq "-timeout") {
2383       $http_timeout = shift @ARGV;
2384     } elsif ($_ eq "-filter") {
2385       $filter_cmd = shift @ARGV;
2386     } elsif ($_ eq "-filter2") {
2387       $post_filter_cmd = shift @ARGV;
2388     } elsif ($_ eq "-background" || $_ eq "-bg") {
2389       $background = shift @ARGV;
2390     } elsif ($_ eq "-size") {
2391       $_ = shift @ARGV;
2392       if (m@^(\d+)x(\d+)$@) {
2393         $img_width = $1;
2394         $img_height = $2;
2395       } else {
2396         error "argument to \"-size\" must be of the form \"640x400\"";
2397       }
2398     } elsif ($_ eq "-proxy" || $_ eq "-http-proxy") {
2399       $http_proxy = shift @ARGV;
2400     } elsif ($_ eq "-dictionary" || $_ eq "-dict") {
2401       $dict = shift @ARGV;
2402     } elsif ($_ eq "-debug" || $_ eq "--debug") {
2403       my $which = shift @ARGV;
2404       my @rest = @search_methods;
2405       my $ok = 0;
2406       while (@rest) {
2407         my $pct  = shift @rest;
2408         my $name = shift @rest;
2409         my $tfn  = shift @rest;
2410
2411         if ($name eq $which) {
2412           @search_methods = (100, $name, $tfn);
2413           $ok = 1;
2414           last;
2415         }
2416       }
2417       error "no such search method as \"$which\"" unless ($ok);
2418       LOG (1, "DEBUG: using only \"$which\"");
2419
2420     } else {
2421       print STDERR "$copyright\nusage: $progname [-root]" .
2422                  " [-display dpy] [-root] [-verbose] [-timeout secs]\n" .
2423                  "\t\t  [-delay secs] [-filter cmd] [-filter2 cmd]\n" .
2424                  "\t\t  [-dictionary dictionary-file]\n" .
2425                  "\t\t  [-http-proxy host[:port]]\n";
2426       exit 1;
2427     }
2428   }
2429
2430   if ($http_proxy && $http_proxy eq "") {
2431     $http_proxy = undef;
2432   }
2433   if ($http_proxy && $http_proxy =~ m@^http://([^/]*)/?$@ ) {
2434     # historical suckage: allow "http://host:port" as well as "host:port".
2435     $http_proxy = $1;
2436   }
2437
2438   if (!$root_p && !$no_output_p) {
2439     print STDERR $copyright;
2440     error "the -root argument is mandatory (for now.)";
2441   }
2442
2443   if (!$no_output_p && !$ENV{DISPLAY}) {
2444     error "\$DISPLAY is not set.";
2445   }
2446
2447
2448   if ($verbose == 1) {
2449     $verbose_imgmap   = 1;
2450     $verbose_warnings = 1;
2451
2452   } elsif ($verbose == 2) {
2453     $verbose_imgmap   = 1;
2454     $verbose_warnings = 1;
2455     $verbose_load     = 1;
2456
2457   } elsif ($verbose == 3) {
2458     $verbose_imgmap   = 1;
2459     $verbose_warnings = 1;
2460     $verbose_load     = 1;
2461     $verbose_filter   = 1;
2462
2463   } elsif ($verbose == 4) {
2464     $verbose_imgmap   = 1;
2465     $verbose_warnings = 1;
2466     $verbose_load     = 1;
2467     $verbose_filter   = 1;
2468     $verbose_net      = 1;
2469
2470   } elsif ($verbose == 5) {
2471     $verbose_imgmap   = 1;
2472     $verbose_warnings = 1;
2473     $verbose_load     = 1;
2474     $verbose_filter   = 1;
2475     $verbose_net      = 1;
2476     $verbose_pbm      = 1;
2477
2478   } elsif ($verbose == 6) {
2479     $verbose_imgmap   = 1;
2480     $verbose_warnings = 1;
2481     $verbose_load     = 1;
2482     $verbose_filter   = 1;
2483     $verbose_net      = 1;
2484     $verbose_pbm      = 1;
2485     $verbose_http     = 1;
2486
2487   } elsif ($verbose >= 7) {
2488     $verbose_imgmap   = 1;
2489     $verbose_warnings = 1;
2490     $verbose_load     = 1;
2491     $verbose_filter   = 1;
2492     $verbose_net      = 1;
2493     $verbose_pbm      = 1;
2494     $verbose_http     = 1;
2495     $verbose_exec     = 1;
2496   }
2497
2498   if ($dict) {
2499     error ("$dict does not exist") unless (-f $dict);
2500     $wordlist = $dict;
2501   } else {
2502     pick_dictionary();
2503   }
2504
2505   if ($urls_only_p) {
2506     url_only_output;
2507   } else {
2508     x_or_pbm_output;
2509   }
2510 }
2511
2512 main;
2513 exit (0);