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