From http://www.jwz.org/xscreensaver/xscreensaver-5.35.tar.gz
[xscreensaver] / hacks / webcollage
1 #!/usr/bin/perl -w
2 #
3 # webcollage, Copyright © 1999-2015 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 #     webcollage --root
19 #     webcollage --root --filter 'vidwhacker --stdin --stdout'
20 #
21 #
22 # You can see this in action at https://www.jwz.org/webcollage/ --
23 # it auto-reloads about once a minute.  To make a page similar to
24 # that on your own system, do this:
25 #
26 #     webcollage --size '800x600' --imagemap $HOME/www/webcollage/index
27 #
28 #
29 # If you have the "driftnet" program installed, webcollage can display a
30 # collage of images sniffed off your local ethernet, instead of pulled out
31 # of search engines: in that way, your screensaver can display the images
32 # that your co-workers are downloading!
33 #
34 # Driftnet is available here: http://www.ex-parrot.com/~chris/driftnet/
35 # Use it like so:
36 #
37 #     webcollage --root --driftnet
38 #
39 # Driftnet is the Unix implementation of the MacOS "EtherPEG" program.
40
41
42 require 5;
43 use strict;
44
45 # We can't "use diagnostics" here, because that library malfunctions if
46 # you signal and catch alarms: it says "Uncaught exception from user code"
47 # and exits, even though I damned well AM catching it!
48 #use diagnostics;
49
50
51 require Time::Local;
52 require POSIX;
53 use Fcntl ':flock'; # import LOCK_* constants
54 use POSIX qw(strftime);
55 use LWP::UserAgent;
56 use bytes;
57
58
59 my $progname = $0; $progname =~ s@.*/@@g;
60 my ($version) = ('$Revision: 1.173 $' =~ m/\s(\d[.\d]+)\s/s);
61 my $copyright = "WebCollage $version, Copyright (c) 1999-2015" .
62     " Jamie Zawinski <jwz\@jwz.org>\n" .
63     "                  https://www.jwz.org/webcollage/\n";
64
65
66
67 my @search_methods = (
68                       # Google is rate-limiting us now, so this works ok from
69                       # a short-running screen saver, but not as a batch job.
70                       # I haven't found a workaround.
71                       #
72                         7, "googlephotos",  \&pick_from_google_image_photos,
73                         5, "googleimgs",    \&pick_from_google_images,
74                         5, "googlenums",    \&pick_from_google_image_numbers,
75
76                       # So let's try Bing instead. No rate limiting yet!
77                       #
78                         7, "bingphotos",    \&pick_from_bing_image_photos,
79                         6, "bingimgs",      \&pick_from_bing_images,
80                         6, "bingnums",      \&pick_from_bing_image_numbers,
81
82                        21, "flickr_recent", \&pick_from_flickr_recent,
83                        16, "flickr_random", \&pick_from_flickr_random,
84                        23, "instagram",     \&pick_from_instagram,
85                         4, "livejournal",   \&pick_from_livejournal_images,
86
87                      # No longer exists, as of Apr 2014
88                      #  4, "yahoorand",     \&pick_from_yahoo_random_link,
89
90                      # Twitter destroyed their whole API in 2013.
91                      #  0, "twitpic",       \&pick_from_twitpic_images,
92                      #  0, "twitter",       \&pick_from_twitter_images,
93
94                      # This is a cute way to search for a certain webcams.
95                      # Not included in default methods, since these images
96                      # aren't terribly interesting by themselves.
97                      # See also "SurveillanceSaver".
98                      #
99                         0, "securitycam",   \&pick_from_security_camera,
100
101                      # Nonfunctional as of June 2011.
102                      #  0, "altavista",     \&pick_from_alta_vista_random_link,
103
104                      # In Apr 2002, Google asked me to stop searching them.
105                      # I asked them to add a "random link" url.  They said
106                      # "that would be easy, we'll think about it" and then
107                      # never wrote back.  Booo Google!  Booooo!  So, screw
108                      # those turkeys, I've turned Google searching back on.
109                      # I'm sure they can take it.  (Jan 2005.)
110
111                      # Jan 2005: Yahoo fucked up their search form so that
112                      # it's no longer possible to do "or" searches on news
113                      # images, so we rarely get any hits there any more.
114                      # 
115                      #  0, "yahoonews",     \&pick_from_yahoo_news_text,
116
117                      # Dec 2004: the ircimages guy's server can't take the
118                      # heat, so he started banning the webcollage user agent.
119                      # I tried to convince him to add a lighter-weight page to
120                      # support webcollage better, but he doesn't care.
121                      #
122                      #  0, "ircimages",     \&pick_from_ircimages,
123
124                      # Dec 2002: Alta Vista has a new "random link" URL now.
125                      # They added it specifically to better support webcollage!
126                      # That was super cool of them.  This is how we used to do
127                      # it, before:
128                      #
129                      #  0, "avimages",      \&pick_from_alta_vista_images,
130                      #  0, "avtext",        \&pick_from_alta_vista_text,
131
132                      # This broke in 2004.  Eh, Lycos sucks anyway.
133                      #
134                      #  0, "lycos",         \&pick_from_lycos_text,
135
136                      # This broke in 2003, I think.  I suspect Hotbot is
137                      # actually the same search engine data as Lycos.
138                      #
139                      #  0, "hotbot",        \&pick_from_hotbot_text,
140                       );
141
142 # programs we can use to write to the root window (tried in ascending order.)
143 #
144 my @root_displayers = (
145   "xscreensaver-getimage -root -file",
146   "chbg       -once -xscreensaver -max_size 100",
147   "xv         -root -quit -viewonly +noresetroot -quick24 -rmode 5" .
148   "           -rfg black -rbg black",
149   "xli        -quiet -onroot -center -border black",
150   "xloadimage -quiet -onroot -center -border black",
151
152 # this lame program wasn't built with vroot.h:
153 # "xsri       -scale -keep-aspect -center-horizontal -center-vertical",
154 );
155
156
157 # Some sites need cookies to work properly.   These are they.
158 #
159 my %cookies = (
160   "www.altavista.com"  =>  "AV_ALL=1",   # request uncensored searches
161   "web.altavista.com"  =>  "AV_ALL=1",
162
163                                          # log in as "cipherpunk"
164   "www.nytimes.com"    =>  'NYT-S=18cHMIlJOn2Y1bu5xvEG3Ufuk6E1oJ.' .
165                            'FMxWaQV0igaB5Yi/Q/guDnLeoL.pe7i1oakSb' .
166                            '/VqfdUdb2Uo27Vzt1jmPn3cpYRlTw9',
167
168   "ircimages.com"      =>  'disclaimer=1',
169 );
170
171
172 # If this is set, it's a helper program to use for pasting images together:
173 # this is a lot faster and more efficient than using PPM pipelines, which is
174 # what we do if this program doesn't exist.  (We check for "webcollage-helper"
175 # on $PATH at startup, and set this variable appropriately.)
176 #
177 my $webcollage_helper = undef;
178
179
180 # If we have the webcollage-helper program, then it will paste the images
181 # together with transparency!  0.0 is invisible, 1.0 is totally opaque.
182 #
183 my $opacity = 0.85;
184
185
186 # Some sites have  managed to poison the search engines.  These are they.
187 # (We auto-detect sites that have poisoned the search engines via excessive
188 # keywords or dictionary words,  but these are ones that slip through
189 # anyway.)
190 #
191 # This can contain full host names, or 2 or 3 component domains.
192 #
193 my %poisoners = (
194   "die.net"                 => 1,  # 'l33t h4ck3r d00dz.
195   "genforum.genealogy.com"  => 1,  # Cluttering avtext with human names.
196   "rootsweb.com"            => 1,  # Cluttering avtext with human names.
197   "akamai.net"              => 1,  # Lots of sites have their images on Akamai.
198   "akamaitech.net"          => 1,  # But those are pretty much all banners.
199                                    # Since Akamai is super-expensive, let's
200                                    # go out on a limb and assume that all of
201                                    # their customers are rich-and-boring.
202   "bartleby.com"            => 1,  # Dictionary, cluttering avtext.
203   "encyclopedia.com"        => 1,  # Dictionary, cluttering avtext.
204   "onlinedictionary.datasegment.com" => 1,  # Dictionary, cluttering avtext.
205   "hotlinkpics.com"         => 1,  # Porn site that has poisoned avimages
206                                    # (I don't see how they did it, though!)
207   "alwayshotels.com"        => 1,  # Poisoned Lycos pretty heavily.
208   "nextag.com"              => 1,  # Poisoned Alta Vista real good.
209   "ghettodriveby.com"       => 1,  # Poisoned Google Images.
210   "crosswordsolver.org"     => 1,  # Poisoned Google Images.
211   "xona.com"                => 1,  # Poisoned Google Images.
212   "freepatentsonline.com"   => 1,  # Poisoned Google Images.
213   "herbdatanz.com"          => 1,  # Poisoned Google Images.
214 );
215
216
217 # When verbosity is turned on, we warn about sites that we seem to be hitting
218 # a lot: usually this means some new poisoner has made it into the search
219 # engines.  But sometimes, the warning is just because that site has a lot
220 # of stuff on it.  So these are the sites that are immune to the "frequent
221 # site" diagnostic message.
222 #
223 my %warningless_sites = (
224   "home.earthlink.net"      => 1,
225   "www.angelfire.com"       => 1,
226   "members.aol.com"         => 1,
227   "img.photobucket.com"     => 1,
228   "pics.livejournal.com"    => 1,
229   "tinypic.com"             => 1,
230   "flickr.com"              => 1,
231   "staticflickr.com"        => 1,
232   "pbase.com"               => 1,
233   "blogger.com"             => 1,
234   "multiply.com"            => 1,
235   "wikimedia.org"           => 1,
236   "twitpic.com"             => 1,
237   "amazonaws.com"           => 1,
238   "blogspot.com"            => 1,
239   "photoshelter.com"        => 1,
240   "myspacecdn.com"          => 1,
241   "feedburner.com"          => 1,
242   "wikia.com"               => 1,
243   "ljplus.ru"               => 1,
244   "yandex.ru"               => 1,
245   "imgur.com"               => 1,
246   "yfrog.com"               => 1,
247   "cdninstagram.com"        => 1,
248
249   "yimg.com"                => 1,  # This is where dailynews.yahoo.com stores
250   "eimg.com"                => 1,  # its images, so pick_from_yahoo_news_text()
251                                    # hits this every time.
252
253   "images.quizfarm.com"     => 1,  # damn those LJ quizzes...
254   "images.quizilla.com"     => 1,
255   "images.quizdiva.net"     => 1,
256
257   "driftnet"                => 1,  # builtin...
258   "local-directory"         => 1,  # builtin...
259 );
260
261
262 # For decoding HTML-encoded character entities to URLs.
263 #
264 my %entity_table = (
265    "apos"   => '\'',
266    "quot"   => '"',    "amp"    => '&',    "lt"     => '<',
267    "gt"     => '>',    "nbsp"   => ' ',    "iexcl"  => '',
268    "cent"   => "\xA2", "pound"  => "\xA3", "curren" => "\xA4",
269    "yen"    => "\xA5", "brvbar" => "\xA6", "sect"   => "\xA7",
270    "uml"    => "\xA8", "copy"   => "\xA9", "ordf"   => "\xAA",
271    "laquo"  => "\xAB", "not"    => "\xAC", "shy"    => "\xAD",
272    "reg"    => "\xAE", "macr"   => "\xAF", "deg"    => "\xB0",
273    "plusmn" => "\xB1", "sup2"   => "\xB2", "sup3"   => "\xB3",
274    "acute"  => "\xB4", "micro"  => "\xB5", "para"   => "\xB6",
275    "middot" => "\xB7", "cedil"  => "\xB8", "sup1"   => "\xB9",
276    "ordm"   => "\xBA", "raquo"  => "\xBB", "frac14" => "\xBC",
277    "frac12" => "\xBD", "frac34" => "\xBE", "iquest" => "\xBF",
278    "Agrave" => "\xC0", "Aacute" => "\xC1", "Acirc"  => "\xC2",
279    "Atilde" => "\xC3", "Auml"   => "\xC4", "Aring"  => "\xC5",
280    "AElig"  => "\xC6", "Ccedil" => "\xC7", "Egrave" => "\xC8",
281    "Eacute" => "\xC9", "Ecirc"  => "\xCA", "Euml"   => "\xCB",
282    "Igrave" => "\xCC", "Iacute" => "\xCD", "Icirc"  => "\xCE",
283    "Iuml"   => "\xCF", "ETH"    => "\xD0", "Ntilde" => "\xD1",
284    "Ograve" => "\xD2", "Oacute" => "\xD3", "Ocirc"  => "\xD4",
285    "Otilde" => "\xD5", "Ouml"   => "\xD6", "times"  => "\xD7",
286    "Oslash" => "\xD8", "Ugrave" => "\xD9", "Uacute" => "\xDA",
287    "Ucirc"  => "\xDB", "Uuml"   => "\xDC", "Yacute" => "\xDD",
288    "THORN"  => "\xDE", "szlig"  => "\xDF", "agrave" => "\xE0",
289    "aacute" => "\xE1", "acirc"  => "\xE2", "atilde" => "\xE3",
290    "auml"   => "\xE4", "aring"  => "\xE5", "aelig"  => "\xE6",
291    "ccedil" => "\xE7", "egrave" => "\xE8", "eacute" => "\xE9",
292    "ecirc"  => "\xEA", "euml"   => "\xEB", "igrave" => "\xEC",
293    "iacute" => "\xED", "icirc"  => "\xEE", "iuml"   => "\xEF",
294    "eth"    => "\xF0", "ntilde" => "\xF1", "ograve" => "\xF2",
295    "oacute" => "\xF3", "ocirc"  => "\xF4", "otilde" => "\xF5",
296    "ouml"   => "\xF6", "divide" => "\xF7", "oslash" => "\xF8",
297    "ugrave" => "\xF9", "uacute" => "\xFA", "ucirc"  => "\xFB",
298    "uuml"   => "\xFC", "yacute" => "\xFD", "thorn"  => "\xFE",
299    "yuml"   => "\xFF",
300
301    # HTML 4 entities that do not have 1:1 Latin1 mappings.
302    "bull"  => "*",    "hellip"=> "...",  "prime" => "'",  "Prime" => "\"",
303    "frasl" => "/",    "trade" => "[tm]", "larr"  => "<-", "rarr"  => "->",
304    "harr"  => "<->",  "lArr"  => "<=",   "rArr"  => "=>", "hArr"  => "<=>",
305    "empty" => "\xD8", "minus" => "-",    "lowast"=> "*",  "sim"   => "~",
306    "cong"  => "=~",   "asymp" => "~",    "ne"    => "!=", "equiv" => "==",
307    "le"    => "<=",   "ge"    => ">=",   "lang"  => "<",  "rang"  => ">",
308    "loz"   => "<>",   "OElig" => "OE",   "oelig" => "oe", "Yuml"  => "Y",
309    "circ"  => "^",    "tilde" => "~",    "ensp"  => " ",  "emsp"  => " ",
310    "thinsp"=> " ",    "ndash" => "-",    "mdash" => "--", "lsquo" => "`",
311    "rsquo" => "'",    "sbquo" => "'",    "ldquo" => "\"", "rdquo" => "\"",
312    "bdquo" => "\"",   "lsaquo"=> "<",    "rsaquo"=> ">",
313 );
314
315
316 ##############################################################################
317 #
318 # Various global flags set by command line parameters, or computed
319 #
320 ##############################################################################
321
322
323 my $current_state = "???";      # for diagnostics
324 my $load_method;
325 my $last_search;
326 my $image_succeeded = -1;
327 my $suppress_audit = 0;
328
329 my $verbose_imgmap = 0;         # print out rectangles and URLs only (stdout)
330 my $verbose_warnings = 0;       # print out warnings when things go wrong
331 my $verbose_load = 0;           # diagnostics about loading of URLs
332 my $verbose_filter = 0;         # diagnostics about page selection/rejection
333 my $verbose_net = 0;            # diagnostics about network I/O
334 my $verbose_pbm = 0;            # diagnostics about PBM pipelines
335 my $verbose_http = 0;           # diagnostics about all HTTP activity
336 my $verbose_exec = 0;           # diagnostics about executing programs
337
338 my $report_performance_interval = 60 * 15;  # print some stats every 15 minutes
339
340 my $http_proxy = undef;
341 my $http_timeout = 20;
342 my $cvt_timeout = 10;
343
344 my $min_width = 50;
345 my $min_height = 50;
346 my $min_ratio = 1/5;
347
348 my $min_gif_area = (120 * 120);
349
350
351 my $no_output_p = 0;
352 my $urls_only_p = 0;
353 my $cocoa_p = 0;
354 my $imagemap_base = undef;
355
356 my @pids_to_kill = ();  # forked pids we should kill when we exit, if any.
357
358 my $driftnet_magic = 'driftnet';
359 my $driftnet_dir = undef;
360 my $default_driftnet_cmd = "driftnet -a -m 100";
361
362 my $local_magic = 'local-directory';
363 my $local_dir = undef;
364
365 my $wordlist;
366
367 my %rejected_urls;
368 my @tripwire_words = ("aberrate", "abode", "amorphous", "antioch",
369                       "arrhenius", "arteriole", "blanket", "brainchild",
370                       "burdensome", "carnival", "cherub", "chord", "clever",
371                       "dedicate", "dilogarithm", "dolan", "dryden",
372                       "eggplant");
373
374
375 ##############################################################################
376 #
377 # Retrieving URLs
378 #
379 ##############################################################################
380
381 # returns three values: the HTTP response line; the document headers;
382 # and the document body.
383 #
384 sub get_document_1($$$) {
385   my ($url, $referer, $timeout) = @_;
386
387   if (!defined($timeout)) { $timeout = $http_timeout; }
388   if ($timeout > $http_timeout) { $timeout = $http_timeout; }
389
390   my $user_agent = "$progname/$version";
391
392   if ($url =~ m@^https?://www\.altavista\.com/@s ||
393       $url =~ m@^https?://random\.yahoo\.com/@s  ||
394       $url =~ m@^https?://[^./]+\.google\.com/@s ||
395       $url =~ m@^https?://www\.livejournal\.com/@s) {
396     # block this, you turkeys.
397     $user_agent = 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.7)' .
398                   ' Gecko/20070914 Firefox/2.0.0.7';
399   }
400
401   my $ua = LWP::UserAgent->new ( agent => $user_agent,
402                                  keep_alive => 0,
403                                  env_proxy => 0,
404                                );
405   $ua->proxy ('http', $http_proxy) if $http_proxy;
406   $ua->default_header ('Referer' => $referer) if $referer;
407   $ua->default_header ('Accept' => '*/*');
408   $ua->timeout($timeout) if $timeout;
409
410   if (0) {
411     $ua->add_handler ("request_send",
412                       sub($$$) {
413                         my ($req, $ua, $h) = @_;
414                         print "\n>>[[\n"; $req->dump; print "\n]]\n";
415                         return;
416                       });
417     $ua->add_handler ("response_data",
418                       sub($$$$) {
419                         my ($req, $ua, $h, $data) = @_;
420                         #print "\n<<[[\n"; print $data; print "\n]]\n";
421                         return 1;
422                       });
423     $ua->add_handler ("request_done",
424                       sub($$$) {
425                         my ($req, $ua, $h) = @_;
426                         print "\n<<[[\n"; $req->dump; print "\n]]\n";
427                         return;
428                       });
429   }
430
431   if ($verbose_http) {
432     LOG (1, "  ==> GET $url");
433     LOG (1, "  ==> User-Agent: $user_agent");
434     LOG (1, "  ==> Referer: $referer") if $referer;
435   }
436
437   my $res = $ua->get ($url);
438
439   my $http = ($res ? $res->status_line : '') || '';
440   my $head = ($res ? $res->headers()   : '') || '';
441   $head = $head->as_string() if $head;
442   my $body = ($res && $res->is_success ? $res->decoded_content : '') || '';
443
444   LOG ($verbose_net, "get_document_1 $url " . ($referer ? $referer : ""));
445
446   $head =~ s/\r\n/\n/gs;
447   $head =~ s/\r/\n/gs;
448   if ($verbose_http) {
449     foreach (split (/\n/, $head)) {
450       LOG ($verbose_http, "  <== $_");
451     }
452   }
453
454   my @L = split(/\r\n|\r|\n/, $body);
455   my $lines = @L;
456   LOG ($verbose_http,
457        "  <== [ body ]: $lines lines, " . length($body) . " bytes");
458
459   if (!$http) {
460     LOG (($verbose_net || $verbose_load), "null response: $url");
461     return ();
462   }
463
464   return ( $http, $head, $body );
465 }
466
467
468 # returns two values: the document headers; and the document body.
469 # if the given URL did a redirect, returns the redirected-to document.
470 #
471 sub get_document($$;$) {
472   my ($url, $referer, $timeout) = @_;
473   my $start = time;
474
475   if (defined($referer) && $referer eq $driftnet_magic) {
476     return get_driftnet_file ($url);
477   }
478
479   if (defined($referer) && $referer eq $local_magic) {
480     return get_local_file ($url);
481   }
482
483   my $orig_url = $url;
484   my $loop_count = 0;
485   my $max_loop_count = 4;
486
487   do {
488     if (defined($timeout) && $timeout <= 0) {
489       LOG (($verbose_net || $verbose_load), "timed out for $url");
490       $suppress_audit = 1;
491       return ();
492     }
493
494     my ( $http, $head, $body ) = get_document_1 ($url, $referer, $timeout);
495
496     if (defined ($timeout)) {
497       my $now = time;
498       my $elapsed = $now - $start;
499       $timeout -= $elapsed;
500       $start = $now;
501     }
502
503     return () unless $http; # error message already printed
504
505     $http =~ s/[\r\n]+$//s;
506
507     if ( $http =~ m@^HTTP/[0-9.]+ 30[123]@ ) {
508       $_ = $head;
509
510       my ( $location ) = m@^location:[ \t]*(.*)$@im;
511       if ( $location ) {
512         $location =~ s/[\r\n]$//;
513
514         LOG ($verbose_net, "redirect from $url to $location");
515         $referer = $url;
516         $url = $location;
517
518         if ($url =~ m@^/@) {
519           $referer =~ m@^(https?://[^/]+)@i;
520           $url = $1 . $url;
521         } elsif (! ($url =~ m@^[a-z]+:@i)) {
522           $_ = $referer;
523           s@[^/]+$@@g if m@^https?://[^/]+/@i;
524           $_ .= "/" if m@^https?://[^/]+$@i;
525           $url = $_ . $url;
526         }
527
528       } else {
529         LOG ($verbose_net, "no Location with \"$http\"");
530         return ( $url, $body );
531       }
532
533       if ($loop_count++ > $max_loop_count) {
534         LOG ($verbose_net,
535              "too many redirects ($max_loop_count) from $orig_url");
536         $body = undef;
537         return ();
538       }
539
540     } elsif ( $http =~ m@^HTTP/[0-9.]+ ([4-9][0-9][0-9].*)$@ ) {
541
542       LOG (($verbose_net || $verbose_load), "failed: $1 ($url)");
543
544       # http errors -- return nothing.
545       $body = undef;
546       return ();
547
548     } elsif (!$body) {
549
550       LOG (($verbose_net || $verbose_load), "document contains no data: $url");
551       return ();
552
553     } else {
554
555       # ok!
556       return ( $url, $body );
557     }
558
559   } while (1);
560 }
561
562 # If we already have a cookie defined for this site, and the site is trying
563 # to overwrite that very same cookie, let it do so.  This is because nytimes
564 # expires its cookies - it lets you upgrade to a new cookie without logging
565 # in again, but you have to present the old cookie to get the new cookie.
566 # So, by doing this, the built-in cypherpunks cookie will never go "stale".
567 #
568 sub set_cookie($$) {
569   my ($host, $cookie) = @_;
570   my $oc = $cookies{$host};
571   return unless $oc;
572   $_ = $oc;
573   my ($oc_name, $oc_value) = m@^([^= \t\r\n]+)=(.*)$@;
574   $_ = $cookie;
575   my ($nc_name, $nc_value) = m@^([^= \t\r\n]+)=(.*)$@;
576
577   if ($oc_name eq $nc_name &&
578       $oc_value ne $nc_value) {
579     $cookies{$host} = $cookie;
580     LOG ($verbose_net, "overwrote ${host}'s $oc_name cookie");
581   }
582 }
583
584
585 ############################################################################
586 #
587 # Extracting image URLs from HTML
588 #
589 ############################################################################
590
591 # given a URL and the body text at that URL, selects and returns a random
592 # image from it.  returns () if no suitable images found.
593 #
594 sub pick_image_from_body($$) {
595   my ($url, $body) = @_;
596
597   my $base = $url;
598   $_ = $url;
599
600   # if there's at least one slash after the host, take off the last
601   # pathname component
602   if ( m@^https?://[^/]+/@io ) {
603     $base =~ s@[^/]+$@@go;
604   }
605
606   # if there are no slashes after the host at all, put one on the end.
607   if ( m@^https?://[^/]+$@io ) {
608     $base .= "/";
609   }
610
611   $_ = $body;
612
613   # strip out newlines, compress whitespace
614   s/[\r\n\t ]+/ /go;
615
616   # nuke comments
617   s/<!--.*?-->//go;
618
619
620   # There are certain web sites that list huge numbers of dictionary
621   # words in their bodies or in their <META NAME=KEYWORDS> tags (surprise!
622   # Porn sites tend not to be reputable!)
623   #
624   # I do not want webcollage to filter on content: I want it to select
625   # randomly from the set of images on the web.  All the logic here for
626   # rejecting some images is really a set of heuristics for rejecting
627   # images that are not really images: for rejecting *text* that is in
628   # GIF/JPEG/PNG form.  I don't want text, I want pictures, and I want
629   # the content of the pictures to be randomly selected from among all
630   # the available content.
631   #
632   # So, filtering out "dirty" pictures by looking for "dirty" keywords
633   # would be wrong: dirty pictures exist, like it or not, so webcollage
634   # should be able to select them.
635   #
636   # However, picking a random URL is a hard thing to do.  The mechanism I'm
637   # using is to search for a selection of random words.  This is not
638   # perfect, but works ok most of the time.  The way it breaks down is when
639   # some URLs get precedence because their pages list *every word* as
640   # related -- those URLs come up more often than others.
641   #
642   # So, after we've retrieved a URL, if it has too many keywords, reject
643   # it.  We reject it not on the basis of what those keywords are, but on
644   # the basis that by having so many, the page has gotten an unfair
645   # advantage against our randomizer.
646   #
647   my $trip_count = 0;
648   foreach my $trip (@tripwire_words) {
649     $trip_count++ if m/$trip/i;
650   }
651
652   if ($trip_count >= $#tripwire_words - 2) {
653     LOG (($verbose_filter || $verbose_load),
654          "there is probably a dictionary in \"$url\": rejecting.");
655     $rejected_urls{$url} = -1;
656     $body = undef;
657     $_ = undef;
658     return ();
659   }
660
661
662   my @urls;
663   my %unique_urls;
664
665   foreach (split(/ *</)) {
666     if ( m/^meta.*["']keywords["']/i ) {
667
668       # Likewise, reject any web pages that have a KEYWORDS meta tag
669       # that is too long.
670       #
671       my $L = length($_);
672       if ($L > 1000) {
673         LOG (($verbose_filter || $verbose_load),
674              "excessive keywords ($L bytes) in $url: rejecting.");
675         $rejected_urls{$url} = $L;
676         $body = undef;
677         $_ = undef;
678         return ();
679       } else {
680         LOG ($verbose_filter, "  keywords ($L bytes) in $url (ok)");
681       }
682
683     } elsif (m/^ (IMG|A) \b .* (SRC|HREF) \s* = \s* ["']? (.*?) [ "'<>] /six ||
684              m/^ (LINK|META) \b .* (REL|PROPERTY) \s* = \s*
685                  ["']? (image_src|og:image) ["']? /six) {
686
687       my $was_inline = (lc($1) eq 'img');
688       my $was_meta   = (lc($1) eq 'link' || lc($1) eq 'meta');
689       my $link = $3;
690
691       # For <link rel="image_src" href="...">
692       # and <meta property="og:image" content="...">
693       #
694       if ($was_meta) {
695         next unless (m/ (HREF|CONTENT) \s* = \s* ["']? (.*?) [ "'<>] /six);
696         $link = $2;
697       }
698
699       my ( $width )  = m/width ?=[ \"]*(\d+)/oi;
700       my ( $height ) = m/height ?=[ \"]*(\d+)/oi;
701       $_ = $link;
702
703       if ( m@^/@o ) {
704         my $site;
705         ( $site = $base ) =~ s@^(https?://[^/]*).*@$1@gio;
706         $_ = "$site$link";
707       } elsif ( ! m@^[^/:?]+:@ ) {
708         $_ = "$base$link";
709         s@/\./@/@g;
710         1 while (s@/[^/]+/\.\./@/@g);
711       }
712
713       # skip non-http
714       if ( ! m@^https?://@io ) {
715         next;
716       }
717
718       # skip non-image
719       if ( ! m@[.](gif|jpg|jpeg|pjpg|pjpeg|png)$@io ) {
720         next;
721       }
722
723       # skip really short or really narrow images
724       if ( $width && $width < $min_width) {
725         if (!$height) { $height = "?"; }
726         LOG ($verbose_filter, "  skip narrow image $_ (${width}x$height)");
727         next;
728       }
729
730       if ( $height && $height < $min_height) {
731         if (!$width) { $width = "?"; }
732         LOG ($verbose_filter, "  skip short image $_ (${width}x$height)");
733         next;
734       }
735
736       # skip images with ratios that make them look like banners.
737       if ($min_ratio && $width && $height &&
738           ($width * $min_ratio ) > $height) {
739         if (!$height) { $height = "?"; }
740         LOG ($verbose_filter, "  skip bad ratio $_ (${width}x$height)");
741         next;
742       }
743
744       # skip GIFs with a small number of pixels -- those usually suck.
745       if ($width && $height &&
746           m/\.gif$/io &&
747           ($width * $height) < $min_gif_area) {
748         LOG ($verbose_filter, "  skip small GIF $_ (${width}x$height)");
749         next;
750       }
751       
752       # skip images with a URL that indicates a Yahoo thumbnail.
753       if (m@\.yimg\.com/.*/t/@) {
754         if (!$width)  { $width  = "?"; }
755         if (!$height) { $height = "?"; }
756         LOG ($verbose_filter, "  skip yahoo thumb $_ (${width}x$height)");
757         next;
758       }
759
760       my $url = $_;
761
762       if ($unique_urls{$url}) {
763         LOG ($verbose_filter, "  skip duplicate image $_");
764         next;
765       }
766
767       LOG ($verbose_filter,
768            "  image $url" .
769            ($width && $height ? " (${width}x${height})" : "") .
770            ($was_meta ? " (meta)" : $was_inline ? " (inline)" : ""));
771
772
773       my $weight = 1;
774
775       if ($was_meta) {
776         $weight = 20;    # meta tag images are far preferable to inline images.
777       } else {
778         if ($url !~ m@[.](gif|png)$@io ) {
779           $weight += 2;  # JPEGs are preferable to GIFs and PNGs.
780         }
781         if (! $was_inline) {
782           $weight += 4;  # pointers to images are preferable to inlined images.
783         }
784       }
785
786       $unique_urls{$url}++;
787       for (my $i = 0; $i < $weight; $i++) {
788         $urls[++$#urls] = $url;
789       }
790     }
791   }
792
793   my $fsp = ($body =~ m@<frameset@i);
794
795   $_ = undef;
796   $body = undef;
797
798   @urls = depoison (@urls);
799
800   if ( $#urls < 0 ) {
801     LOG ($verbose_load, "no images on $base" . ($fsp ? " (frameset)" : ""));
802     return ();
803   }
804
805   # pick a random element of the table
806   my $i = int(rand($#urls+1));
807   $url = $urls[$i];
808
809   LOG ($verbose_load, "picked image " .($i+1) . "/" . ($#urls+1) . ": $url");
810
811   return $url;
812 }
813
814
815 # Given a URL and the RSS feed from that URL, pick a random image from
816 # the feed.  This is a lot simpler than extracting images out of a page:
817 # we already know we have reasonable images, so we just pick one.
818 # Returns: the real URL of the page (preferably not the RSS version),
819 # and the image.
820
821 sub pick_image_from_rss($$) {
822   my ( $url, $body ) = @_;
823   my @suitable = ($body =~ m/<enclosure url="(.*?)"/g);
824
825   my ($base) = ($body =~ m@<link>([^<>]+)</link>@i);
826   $base = $url unless $base;
827
828   # pick a random element of the table
829   if (@suitable) {
830     my $i = int(rand(scalar @suitable));
831     my $url = $suitable[$i];
832     LOG ($verbose_load, "picked image " .($i+1) . "/" . 
833                         ($#suitable+1) . ": $url");
834     return ($base, $url);
835   }
836   return;
837 }
838
839 \f
840 ############################################################################
841 #
842 # Subroutines for getting pages and images out of search engines
843 #
844 ############################################################################
845
846
847 sub pick_dictionary() {
848   my @dicts = ("/usr/dict/words",
849                "/usr/share/dict/words",
850                "/usr/share/lib/dict/words",
851                "/usr/share/dict/cracklib-small",
852                "/usr/share/dict/cracklib-words"
853                );
854   foreach my $f (@dicts) {
855     if (-f $f) {
856       $wordlist = $f;
857       last;
858     }
859   }
860   error ("$dicts[0] does not exist") unless defined($wordlist);
861 }
862
863 # returns a random word from the dictionary
864 #
865 sub random_word() {
866
867   return undef unless open (my $in, '<', $wordlist);
868
869   my $size = (stat($in))[7];
870   my $word = undef;
871   my $count = 0;
872
873   while (1) {
874     error ("looping ($count) while reading $wordlist")
875       if (++$count > 100);
876
877     my $pos = int (rand ($size));
878     if (seek ($in, $pos, 0)) {
879       $word = <$in>;   # toss partial line
880       $word = <$in>;   # keep next line
881     }
882
883     next unless ($word);
884     next if ($word =~ m/^[-\']/);
885
886     $word = lc($word);
887     $word =~ s/^.*-//s;
888     $word =~ s/^[^a-z]+//s;
889     $word =~ s/[^a-z]+$//s;
890     $word =~ s/\'s$//s;
891     $word =~ s/ys$/y/s;
892     $word =~ s/ally$//s;
893     $word =~ s/ly$//s;
894     $word =~ s/ies$/y/s;
895     $word =~ s/ally$/al/s;
896     $word =~ s/izes$/ize/s;
897     $word =~ s/esses$/ess/s;
898     $word =~ s/(.{5})ing$/$1/s;
899
900     next if (length ($word) > 14);
901     last if ($word);
902   }
903
904   close ($in);
905
906   if ( $word =~ s/\s/\+/gs ) {  # convert intra-word spaces to "+".
907     $word = "\%22$word\%22";    # And put quotes (%22) around it.
908   }
909
910   return $word;
911 }
912
913
914 sub random_words($) {
915   my ($sep) = @_;
916   return (random_word() . $sep .
917           random_word() . $sep .
918           random_word() . $sep .
919           random_word() . $sep .
920           random_word());
921 }
922
923
924 sub url_quote($) {
925   my ($s) = @_;
926   $s =~ s|([^-a-zA-Z0-9.\@/_\r\n])|sprintf("%%%02X", ord($1))|ge;
927   return $s;
928 }
929
930 sub url_unquote($) {
931   my ($s) = @_;
932   $s =~ s/[+]/ /g;
933   $s =~ s/%([a-z0-9]{2})/chr(hex($1))/ige;
934   return $s;
935 }
936
937 sub html_quote($) {
938   my ($s) = @_;
939   $s =~ s/&/&amp;/gi;
940   $s =~ s/</&lt;/gi;
941   $s =~ s/>/&gt;/gi;
942   $s =~ s/\"/&quot;/gi;
943   return $s;
944 }
945
946 sub html_unquote($) {
947   my ($s) = @_;
948   $s =~ s/(&([a-z]+);)/{ $entity_table{$2} || $1; }/gexi;  # e.g., &apos;
949   $s =~ s/(&\#(\d+);)/{ chr($2) }/gexi;                    # e.g., &#39;
950   return $s;
951 }
952
953
954 # Loads the given URL (a search on some search engine) and returns:
955 # - the total number of hits the search engine claimed it had;
956 # - a list of URLs from the page that the search engine returned;
957 # Note that this list contains all kinds of internal search engine
958 # junk URLs too -- caller must prune them.
959 #
960 sub pick_from_search_engine($$$) {
961   my ( $timeout, $search_url, $words ) = @_;
962
963   $_ = $words;
964   s/%20/ /g;
965
966   print STDERR "\n\n" if ($verbose_load);
967
968   LOG ($verbose_load, "words: $_");
969   LOG ($verbose_load, "URL: $search_url");
970
971   $last_search = $search_url;   # for warnings
972
973   my $start = time;
974   my ( $base, $body ) = get_document ($search_url, undef, $timeout);
975   if (defined ($timeout)) {
976     $timeout -= (time - $start);
977     if ($timeout <= 0) {
978       $body = undef;
979       LOG (($verbose_net || $verbose_load),
980            "timed out (late) for $search_url");
981       $suppress_audit = 1;
982       return ();
983     }
984   }
985
986   return () if (! $body);
987
988
989   my @subpages;
990
991   if ($body =~ m/^\{\"/s) {                     # Google AJAX JSON response.
992
993     my @chunks = split (/"GsearchResultClass"/, $body);
994     shift @chunks;
995     my $body2 = '';
996     my $n = 1;
997     foreach (@chunks) {
998       my ($img) = m/"unescapedUrl":"(.*?)"/si;
999       my ($url) = m/"originalContextUrl":"(.*?)"/si;
1000       next unless ($img && $url);
1001       $url = ("/imgres" .
1002               "?imgurl="    . url_quote($img) .
1003               "&imgrefurl=" . url_quote($url) .
1004               "&...");
1005       $body2 .= "<A HREF=\"" . html_quote($url) . "\">$n</A>\n";
1006       $n++;
1007     }
1008     $body = $body2 if $body2;
1009   }
1010
1011   my $search_count = "?";
1012   if ($body =~ m@found (approximately |about )?(<B>)?(\d+)(</B>)? image@) {
1013     $search_count = $3;
1014   } elsif ($body =~ m@<NOBR>((\d{1,3})(,\d{3})*)&nbsp;@i) {
1015     $search_count = $1;
1016   } elsif ($body =~ m@found ((\d{1,3})(,\d{3})*|\d+) Web p@) {
1017     $search_count = $1;
1018   } elsif ($body =~ m@found about ((\d{1,3})(,\d{3})*|\d+) results@) {
1019     $search_count = $1;
1020   } elsif ($body =~ m@\b\d+ - \d+ of (\d+)\b@i) { # avimages
1021     $search_count = $1;
1022   } elsif ($body =~ m@About ((\d{1,3})(,\d{3})*) images@i) { # avimages
1023     $search_count = $1;
1024   } elsif ($body =~ m@We found ((\d{1,3})(,\d{3})*|\d+) results@i) { # *vista
1025     $search_count = $1;
1026   } elsif ($body =~ m@ of about <B>((\d{1,3})(,\d{3})*)<@i) { # googleimages
1027     $search_count = $1;
1028   } elsif ($body =~ m@<B>((\d{1,3})(,\d{3})*)</B> Web sites were found@i) {
1029     $search_count = $1;    # lycos
1030   } elsif ($body =~ m@WEB.*?RESULTS.*?\b((\d{1,3})(,\d{3})*)\b.*?Matches@i) {
1031     $search_count = $1;                          # hotbot
1032   } elsif ($body =~ m@no photos were found containing@i) { # avimages
1033     $search_count = "0";
1034   } elsif ($body =~ m@found no document matching@i) { # avtext
1035     $search_count = "0";
1036   }
1037   1 while ($search_count =~ s/^(\d+)(\d{3})/$1,$2/);
1038
1039 #  if ($search_count eq "?" || $search_count eq "0") {
1040 #    my $file = "/tmp/wc.html";
1041 #    open (my $out, '>', $file) || error ("writing $file: $!");
1042 #    print $out $body;
1043 #    close $out;
1044 #    print STDERR  blurb() . "###### wrote $file\n";
1045 #  }
1046
1047
1048   my $length = length($body);
1049   my $href_count = 0;
1050
1051   $_ = $body;
1052
1053   s/[\r\n\t ]+/ /g;
1054
1055
1056   s/(<A )/\n$1/gi;
1057   foreach (split(/\n/)) {
1058     $href_count++;
1059     my ($u) = m@<A\s.*\bHREF\s*=\s*([^>]+)>@i;
1060     next unless $u;
1061
1062     if (m/\bm="\{(.*?)\}"/s) {          # Bing info is inside JSON crud
1063       my $json = html_unquote($1);
1064       my ($href) = ($json =~ m/\bsurl:"(.*?)"/s);
1065       my ($img)  = ($json =~ m/\bimgurl:"(.*?)"/s);
1066       $u = "$img\t$href" if ($img && $href);
1067
1068     } elsif ($u =~ m/^\"([^\"]*)\"/) { $u = $1   # quoted string
1069     } elsif ($u =~ m/^([^\s]*)\s/) { $u = $1;    # or token
1070     }
1071
1072     if ( $rejected_urls{$u} ) {
1073       LOG ($verbose_filter, "  pre-rejecting candidate: $u");
1074       next;
1075     }
1076
1077     LOG ($verbose_http, "    HREF: $u");
1078
1079     $subpages[++$#subpages] = $u;
1080   }
1081
1082   if ( $#subpages < 0 ) {
1083     LOG ($verbose_filter,
1084          "found nothing on $base ($length bytes, $href_count links).");
1085     return ();
1086   }
1087
1088   LOG ($verbose_filter, "" . $#subpages+1 . " links on $search_url");
1089
1090   return ($search_count, @subpages);
1091 }
1092
1093
1094 sub depoison(@) {
1095   my (@urls) = @_;
1096   my @urls2 = ();
1097   foreach (@urls) {
1098     my ($h) = m@^https?://([^/: \t\r\n]+)@i;
1099
1100     next unless defined($h);
1101
1102     if ($poisoners{$h}) {
1103       LOG (($verbose_filter), "  rejecting poisoner: $_");
1104       next;
1105     }
1106     if ($h =~ m@([^.]+\.[^.]+\.[^.]+)$@ &&
1107         $poisoners{$1}) {
1108       LOG (($verbose_filter), "  rejecting poisoner: $_");
1109       next;
1110     }
1111     if ($h =~ m@([^.]+\.[^.]+)$@ &&
1112         $poisoners{$1}) {
1113       LOG (($verbose_filter), "  rejecting poisoner: $_");
1114       next;
1115     }
1116
1117     push @urls2, $_;
1118   }
1119   return @urls2;
1120 }
1121
1122
1123 # given a list of URLs, picks one at random; loads it; and returns a
1124 # random image from it.
1125 # returns the url of the page loaded; the url of the image chosen.
1126 #
1127 sub pick_image_from_pages($$$$@) {
1128   my ($base, $total_hit_count, $unfiltered_link_count, $timeout, @pages) = @_;
1129
1130   $total_hit_count = "?" unless defined($total_hit_count);
1131
1132   @pages = depoison (@pages);
1133   LOG ($verbose_load,
1134        "" . ($#pages+1) . " candidates of $unfiltered_link_count links" .
1135        " ($total_hit_count total)");
1136
1137   return () if ($#pages < 0);
1138
1139   my $i = int(rand($#pages+1));
1140   my $page = $pages[$i];
1141
1142   LOG ($verbose_load, "picked page $page");
1143
1144   $suppress_audit = 1;
1145
1146   my ( $base2, $body2 ) = get_document ($page, $base, $timeout);
1147
1148   if (!$base2 || !$body2) {
1149     $body2 = undef;
1150     return ();
1151   }
1152
1153   my $img = pick_image_from_body ($base2, $body2);
1154   $body2 = undef;
1155
1156   if ($img) {
1157     return ($base2, $img);
1158   } else {
1159     return ();
1160   }
1161 }
1162
1163 \f
1164 #############################################################################
1165 ##
1166 ## Pick images from random pages returned by the Yahoo Random Link
1167 ##
1168 #############################################################################
1169 #
1170 ## yahoorand
1171 #my $yahoo_random_link = "http://random.yahoo.com/fast/ryl";
1172 #
1173 #
1174 # Picks a random page; picks a random image on that page;
1175 # returns two URLs: the page containing the image, and the image.
1176 # Returns () if nothing found this time.
1177 #
1178 #sub pick_from_yahoo_random_link($) {
1179 #  my ($timeout) = @_;
1180 #
1181 #  print STDERR "\n\n" if ($verbose_load);
1182 #  LOG ($verbose_load, "URL: $yahoo_random_link");
1183 #
1184 #  $last_search = $yahoo_random_link;   # for warnings
1185 #
1186 #  $suppress_audit = 1;
1187 #
1188 #  my ( $base, $body ) = get_document ($yahoo_random_link, undef, $timeout);
1189 #  if (!$base || !$body) {
1190 #    $body = undef;
1191 #    return;
1192 #  }
1193 #
1194 #  LOG ($verbose_load, "redirected to: $base");
1195 #
1196 #  my $img = pick_image_from_body ($base, $body);
1197 #  $body = undef;
1198 #
1199 #  if ($img) {
1200 #    return ($base, $img);
1201 #  } else {
1202 #    return ();
1203 #  }
1204 #}
1205
1206 \f
1207 ############################################################################
1208 #
1209 # Pick images from random pages returned by the Alta Vista Random Link
1210 # Note: this seems to have gotten a *lot* less random lately (2007).
1211 #
1212 ############################################################################
1213
1214 # altavista
1215 my $alta_vista_random_link = "http://www.altavista.com/image/randomlink";
1216
1217
1218 # Picks a random page; picks a random image on that page;
1219 # returns two URLs: the page containing the image, and the image.
1220 # Returns () if nothing found this time.
1221 #
1222 sub pick_from_alta_vista_random_link($) {
1223   my ($timeout) = @_;
1224
1225   print STDERR "\n\n" if ($verbose_load);
1226   LOG ($verbose_load, "URL: $alta_vista_random_link");
1227
1228   $last_search = $alta_vista_random_link;   # for warnings
1229
1230   $suppress_audit = 1;
1231
1232   my ( $base, $body ) = get_document ($alta_vista_random_link,
1233                                       undef, $timeout);
1234   if (!$base || !$body) {
1235     $body = undef;
1236     return;
1237   }
1238
1239   LOG ($verbose_load, "redirected to: $base");
1240
1241   my $img = pick_image_from_body ($base, $body);
1242   $body = undef;
1243
1244   if ($img) {
1245     return ($base, $img);
1246   } else {
1247     return ();
1248   }
1249 }
1250
1251 \f
1252 ############################################################################
1253 #
1254 # Pick images by feeding random words into Alta Vista Image Search
1255 #
1256 ############################################################################
1257
1258
1259 my $alta_vista_images_url = "http://www.altavista.com/image/results" .
1260                             "?ipht=1" .       # photos
1261                             "&igrph=1" .      # graphics
1262                             "&iclr=1" .       # color
1263                             "&ibw=1" .        # b&w
1264                             "&micat=1" .      # no partner sites
1265                             "&sc=on" .        # "site collapse"
1266                             "&q=";
1267
1268 # avimages
1269 sub pick_from_alta_vista_images($) {
1270   my ($timeout) = @_;
1271
1272   my $words = random_word();
1273   my $page = (int(rand(9)) + 1);
1274   my $search_url = $alta_vista_images_url . $words;
1275
1276   if ($page > 1) {
1277     $search_url .= "&pgno=" . $page;            # page number
1278     $search_url .= "&stq=" . (($page-1) * 12);  # first hit result on page
1279   }
1280
1281   my ($search_hit_count, @subpages) =
1282     pick_from_search_engine ($timeout, $search_url, $words);
1283
1284   my @candidates = ();
1285   foreach my $u (@subpages) {
1286
1287     # avimages is encoding their URLs now.
1288     next unless ($u =~ s/^.*\*\*(http%3a.*$)/$1/gsi);
1289     $u = url_unquote($u);
1290
1291     next unless ($u =~ m@^https?://@i);    #  skip non-HTTP or relative URLs
1292     next if ($u =~ m@[/.]altavista\.com\b@i);     # skip altavista builtins
1293     next if ($u =~ m@[/.]yahoo\.com\b@i);         # yahoo and av in cahoots?
1294     next if ($u =~ m@[/.]doubleclick\.net\b@i);   # you cretins
1295     next if ($u =~ m@[/.]clicktomarket\.com\b@i); # more cretins
1296
1297     next if ($u =~ m@[/.]viewimages\.com\b@i);    # stacked deck
1298     next if ($u =~ m@[/.]gettyimages\.com\b@i);
1299
1300     LOG ($verbose_filter, "  candidate: $u");
1301     push @candidates, $u;
1302   }
1303
1304   return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1305                                 $timeout, @candidates);
1306 }
1307
1308
1309 \f
1310 ############################################################################
1311 #
1312 # Pick images from Aptix security cameras
1313 # Cribbed liberally from google image search code.
1314 # By Jason Sullivan <jasonsul@us.ibm.com>
1315 #
1316 ############################################################################
1317
1318 my $aptix_images_url = ("http://www.google.com/search" .
1319                         "?q=inurl:%22jpg/image.jpg%3Fr%3D%22");
1320
1321 # securitycam
1322 sub pick_from_security_camera($) {
1323   my ($timeout) = @_;
1324
1325   my $page = (int(rand(9)) + 1);
1326   my $num = 20;                                 # 20 images per page
1327   my $search_url = $aptix_images_url;
1328
1329   if ($page > 1) {
1330     $search_url .= "&start=" . $page*$num;      # page number
1331     $search_url .= "&num="   . $num;            #images per page
1332   }
1333
1334   my ($search_hit_count, @subpages) =
1335     pick_from_search_engine ($timeout, $search_url, '');
1336
1337   my @candidates = ();
1338   my %referers;
1339   foreach my $u (@subpages) {
1340     next if ($u =~ m@[/.]google\.com\b@i);        # skip google builtins (most links)
1341     next unless ($u =~ m@jpg/image.jpg\?r=@i);    #  All pics contain this
1342
1343     LOG ($verbose_filter, "  candidate: $u");
1344     push @candidates, $u;
1345     $referers{$u} = $u;
1346     }
1347
1348   @candidates = depoison (@candidates);
1349   return () if ($#candidates < 0);
1350   my $i = int(rand($#candidates+1));
1351   my $img = $candidates[$i];
1352   my $ref = $referers{$img};
1353
1354   LOG ($verbose_load, "picked image " . ($i+1) . ": $img (on $ref)");
1355   return ($ref, $img);
1356 }
1357
1358 \f
1359 ############################################################################
1360 #
1361 # Pick images by feeding random words into Google Image Search.
1362 # By Charles Gales <gales@us.ibm.com>
1363 #
1364 ############################################################################
1365
1366
1367 my $google_images_url =     "http://ajax.googleapis.com/ajax/services/" .
1368                             "search/images" .
1369                             "?v=1.0" .
1370                             "&rsz=large" .
1371                             "&q=";
1372
1373 # googleimgs
1374 sub pick_from_google_images($;$$) {
1375   my ($timeout, $words, $max_page) = @_;
1376
1377   if (!defined($words)) {
1378     $words = random_word();   # only one word for Google
1379   }
1380
1381   my $off = int(rand(40));
1382   my $search_url = $google_images_url . $words . "&start=" . $off;
1383
1384   my ($search_hit_count, @subpages) =
1385     pick_from_search_engine ($timeout, $search_url, $words);
1386
1387   my @candidates = ();
1388   my %referers;
1389   foreach my $u (@subpages) {
1390     next unless ($u =~ m@imgres\?imgurl@i);    #  All pics start with this
1391     next if ($u =~ m@[/.]google\.com\b@i);     # skip google builtins
1392
1393     $u = html_unquote($u);
1394     if ($u =~ m@^/imgres\?imgurl=(.*?)&imgrefurl=(.*?)\&@) {
1395       my $ref = $2;
1396       my $img = $1;
1397       $ref = url_decode($ref);
1398       $img = url_decode($img);
1399
1400       $img = "http://$img" unless ($img =~ m/^https?:/i);
1401
1402       LOG ($verbose_filter, "  candidate: $ref");
1403       push @candidates, $img;
1404       $referers{$img} = $ref;
1405     }
1406   }
1407
1408   @candidates = depoison (@candidates);
1409   return () if ($#candidates < 0);
1410   my $i = int(rand($#candidates+1));
1411   my $img = $candidates[$i];
1412   my $ref = $referers{$img};
1413
1414   LOG ($verbose_load, "picked image " . ($i+1) . ": $img (on $ref)");
1415   return ($ref, $img);
1416 }
1417
1418
1419 \f
1420 ############################################################################
1421 #
1422 # Pick images by feeding random numbers into Google Image Search.
1423 # By jwz, suggested by Ian O'Donnell.
1424 #
1425 ############################################################################
1426
1427
1428 # googlenums
1429 sub pick_from_google_image_numbers($) {
1430   my ($timeout) = @_;
1431
1432   my $max = 9999;
1433   my $number = int(rand($max));
1434
1435   $number = sprintf("%04d", $number)
1436     if (rand() < 0.3);
1437
1438   pick_from_google_images ($timeout, "$number");
1439 }
1440
1441
1442 \f
1443 ############################################################################
1444 #
1445 # Pick images by feeding random digital camera file names into 
1446 # Google Image Search.
1447 # By jwz, inspired by the excellent Random Personal Picture Finder
1448 # at http://www.diddly.com/random/
1449 #
1450 ############################################################################
1451
1452 my @photomakers = (
1453   #
1454   # Common digital camera file name formats, as described at
1455   # http://www.diddly.com/random/about.html
1456   #
1457   sub { sprintf ("dcp%05d.jpg",  int(rand(4000))); },   # Kodak
1458   sub { sprintf ("dsc%05d.jpg",  int(rand(4000))); },   # Nikon
1459   sub { sprintf ("dscn%04d.jpg", int(rand(4000))); },   # Nikon
1460   sub { sprintf ("mvc-%03d.jpg", int(rand(999)));  },   # Sony Mavica
1461   sub { sprintf ("mvc%05d.jpg",  int(rand(9999))); },   # Sony Mavica
1462   sub { sprintf ("P101%04d.jpg", int(rand(9999))); },   # Olympus w/ date=101
1463   sub { sprintf ("P%x%02d%04d.jpg",                     # Olympus
1464                  int(rand(0xC)), int(rand(30))+1,
1465                  rand(9999)); },
1466   sub { sprintf ("IMG_%03d.jpg",  int(rand(999))); },   # ?
1467   sub { sprintf ("IMAG%04d.jpg",  int(rand(9999))); },  # RCA and Samsung
1468   sub { my $n = int(rand(9999));                        # Canon
1469           sprintf ("1%02d-%04d.jpg", int($n/100), $n); },
1470   sub { my $n = int(rand(9999));                        # Canon
1471           sprintf ("1%02d-%04d_IMG.jpg",
1472                    int($n/100), $n); },
1473   sub { sprintf ("IMG_%04d.jpg", int(rand(9999))); },   # Canon
1474   sub { sprintf ("dscf%04d.jpg", int(rand(9999))); },   # Fuji Finepix
1475   sub { sprintf ("pdrm%04d.jpg", int(rand(9999))); },   # Toshiba PDR
1476   sub { sprintf ("IM%06d.jpg", int(rand(9999))); },     # HP Photosmart
1477   sub { sprintf ("EX%06d.jpg", int(rand(9999))); },     # HP Photosmart
1478 #  sub { my $n = int(rand(3));                          # Kodak DC-40,50,120
1479 #        sprintf ("DC%04d%s.jpg", int(rand(9999)),
1480 #                 $n == 0 ? 'S' : $n == 1 ? 'M' : 'L'); },
1481   sub { sprintf ("pict%04d.jpg", int(rand(9999))); },   # Minolta Dimage
1482   sub { sprintf ("P%07d.jpg", int(rand(9999))); },      # Kodak DC290
1483 #  sub { sprintf ("%02d%02d%04d.jpg",                   # Casio QV3000, QV4000
1484 #                 int(rand(12))+1, int(rand(31))+1,
1485 #                 int(rand(999))); },
1486 #  sub { sprintf ("%02d%x%02d%04d.jpg",                 # Casio QV7000
1487 #                 int(rand(6)), # year
1488 #                 int(rand(12))+1, int(rand(31))+1,
1489 #                 int(rand(999))); },
1490   sub { sprintf ("IMGP%04d.jpg", int(rand(9999))); },   # Pentax Optio S
1491   sub { sprintf ("PANA%04d.jpg", int(rand(9999))); },   # Panasonic vid still
1492   sub { sprintf ("HPIM%04d.jpg", int(rand(9999))); },   # HP Photosmart
1493   sub { sprintf ("PCDV%04d.jpg", int(rand(9999))); },   # ?
1494  );
1495
1496
1497 # googlephotos
1498 sub pick_from_google_image_photos($) {
1499   my ($timeout) = @_;
1500
1501   my $i = int(rand($#photomakers + 1));
1502   my $fn = $photomakers[$i];
1503   my $file = &$fn;
1504   #$file .= "%20filetype:jpg";
1505
1506   pick_from_google_images ($timeout, $file);
1507 }
1508
1509 \f
1510 ############################################################################
1511 #
1512 # Pick images by feeding random words into Google Image Search.
1513 # By the way: fuck Microsoft.
1514 #
1515 ############################################################################
1516
1517 my $bing_images_url =   "http://www.bing.com/images/async?q=";
1518
1519
1520 # bingimgs
1521 sub pick_from_bing_images($;$$) {
1522   my ($timeout, $words, $max_page) = @_;
1523
1524   if (!defined($words)) {
1525     $words = random_word();   # only one word for Bing
1526   }
1527
1528   my $off = int(rand(300));
1529   my $search_url = $bing_images_url . $words . "&first=" . $off;
1530
1531   my ($search_hit_count, @subpages) =
1532     pick_from_search_engine ($timeout, $search_url, $words);
1533
1534   my @candidates = ();
1535   my %referers;
1536   foreach my $u (@subpages) {
1537     my ($img, $ref) = ($u =~ m/^(.*?)\t(.*)$/s);
1538     next unless $img;
1539     LOG ($verbose_filter, "  candidate: $ref");
1540     push @candidates, $img;
1541     $referers{$img} = $ref;
1542   }
1543
1544   @candidates = depoison (@candidates);
1545   return () if ($#candidates < 0);
1546   my $i = int(rand($#candidates+1));
1547   my $img = $candidates[$i];
1548   my $ref = $referers{$img};
1549
1550   LOG ($verbose_load, "picked image " . ($i+1) . ": $img (on $ref)");
1551   return ($ref, $img);
1552 }
1553
1554
1555
1556 \f
1557 ############################################################################
1558 #
1559 # Pick images by feeding random numbers into Bing Image Search.
1560 #
1561 ############################################################################
1562
1563 # bingnums
1564 sub pick_from_bing_image_numbers($) {
1565   my ($timeout) = @_;
1566
1567   my $max = 9999;
1568   my $number = int(rand($max));
1569
1570   $number = sprintf("%04d", $number)
1571     if (rand() < 0.3);
1572
1573   pick_from_bing_images ($timeout, "$number");
1574 }
1575
1576 \f
1577 ############################################################################
1578 #
1579 # Pick images by feeding random numbers into Bing Image Search.
1580 #
1581 ############################################################################
1582
1583 # bingphotos
1584 sub pick_from_bing_image_photos($) {
1585   my ($timeout) = @_;
1586
1587   my $i = int(rand($#photomakers + 1));
1588   my $fn = $photomakers[$i];
1589   my $file = &$fn;
1590
1591   pick_from_bing_images ($timeout, $file);
1592 }
1593
1594 \f
1595 ############################################################################
1596 #
1597 # Pick images by feeding random words into Alta Vista Text Search
1598 #
1599 ############################################################################
1600
1601
1602 my $alta_vista_url = "http://www.altavista.com/web/results" .
1603                      "?pg=aq" .
1604                      "&aqmode=s" .
1605                      "&filetype=html" .
1606                      "&sc=on" .        # "site collapse"
1607                      "&nbq=50" .
1608                      "&aqo=";
1609
1610 # avtext
1611 sub pick_from_alta_vista_text($) {
1612   my ($timeout) = @_;
1613
1614   my $words = random_words('%20');
1615   my $page = (int(rand(9)) + 1);
1616   my $search_url = $alta_vista_url . $words;
1617
1618   if ($page > 1) {
1619     $search_url .= "&pgno=" . $page;
1620     $search_url .= "&stq=" . (($page-1) * 10);
1621   }
1622
1623   my ($search_hit_count, @subpages) =
1624     pick_from_search_engine ($timeout, $search_url, $words);
1625
1626   my @candidates = ();
1627   foreach my $u (@subpages) {
1628
1629     # Those altavista fuckers are playing really nasty redirection games
1630     # these days: the filter your clicks through their site, but use
1631     # onMouseOver to make it look like they're not!  Well, it makes it
1632     # easier for us to identify search results...
1633     #
1634     next unless ($u =~ s/^.*\*\*(http%3a.*$)/$1/gsi);
1635     $u = url_unquote($u);
1636
1637     next unless ($u =~ m@^https?://@i);    #  skip non-HTTP or relative URLs
1638     next if ($u =~ m@[/.]altavista\.com\b@i);     # skip altavista builtins
1639     next if ($u =~ m@[/.]yahoo\.com\b@i);         # yahoo and av in cahoots?
1640
1641     LOG ($verbose_filter, "  candidate: $u");
1642     push @candidates, $u;
1643   }
1644
1645   return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1646                                 $timeout, @candidates);
1647 }
1648
1649
1650 \f
1651 ############################################################################
1652 #
1653 # Pick images by feeding random words into Hotbot
1654 #
1655 ############################################################################
1656
1657 my $hotbot_search_url =("http://hotbot.lycos.com/default.asp" .
1658                         "?ca=w" .
1659                         "&descriptiontype=0" .
1660                         "&imagetoggle=1" .
1661                         "&matchmode=any" .
1662                         "&nummod=2" .
1663                         "&recordcount=50" .
1664                         "&sitegroup=1" .
1665                         "&stem=1" .
1666                         "&cobrand=undefined" .
1667                         "&query=");
1668
1669 sub pick_from_hotbot_text($) {
1670   my ($timeout) = @_;
1671
1672   $last_search = $hotbot_search_url;   # for warnings
1673
1674   # lycos seems to always give us back dictionaries and word lists if
1675   # we search for more than one word...
1676   #
1677   my $words = random_word();
1678
1679   my $start = int(rand(8)) * 10 + 1;
1680   my $search_url = $hotbot_search_url . $words . "&first=$start&page=more";
1681
1682   my ($search_hit_count, @subpages) =
1683     pick_from_search_engine ($timeout, $search_url, $words);
1684
1685   my @candidates = ();
1686   foreach my $u (@subpages) {
1687
1688     # Hotbot plays redirection games too
1689     # (not any more?)
1690 #    next unless ($u =~ m@/director.asp\?.*\btarget=([^&]+)@);
1691 #    $u = url_decode($1);
1692
1693     next unless ($u =~ m@^https?://@i);    #  skip non-HTTP or relative URLs
1694     next if ($u =~ m@[/.]hotbot\.com\b@i);     # skip hotbot builtins
1695     next if ($u =~ m@[/.]lycos\.com\b@i);      # skip hotbot builtins
1696     next if ($u =~ m@[/.]inktomi\.com\b@i);    # skip hotbot builtins
1697
1698     LOG ($verbose_filter, "  candidate: $u");
1699     push @candidates, $u;
1700   }
1701
1702   return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1703                                 $timeout, @candidates);
1704 }
1705
1706
1707 \f
1708 ############################################################################
1709 #
1710 # Pick images by feeding random words into Lycos
1711 #
1712 ############################################################################
1713
1714 my $lycos_search_url = "http://search.lycos.com/default.asp" .
1715                        "?lpv=1" .
1716                        "&loc=searchhp" .
1717                        "&tab=web" .
1718                        "&query=";
1719
1720 sub pick_from_lycos_text($) {
1721   my ($timeout) = @_;
1722
1723   $last_search = $lycos_search_url;   # for warnings
1724
1725   # lycos seems to always give us back dictionaries and word lists if
1726   # we search for more than one word...
1727   #
1728   my $words = random_word();
1729
1730   my $start = int(rand(8)) * 10 + 1;
1731   my $search_url = $lycos_search_url . $words . "&first=$start&page=more";
1732
1733   my ($search_hit_count, @subpages) =
1734     pick_from_search_engine ($timeout, $search_url, $words);
1735
1736   my @candidates = ();
1737   foreach my $u (@subpages) {
1738
1739     # Lycos plays redirection games.
1740     # (not any more?)
1741 #    next unless ($u =~ m@^https?://click.lycos.com/director.asp
1742 #                         .*
1743 #                         \btarget=([^&]+)
1744 #                         .*
1745 #                        @x);
1746 #    $u = url_decode($1);
1747
1748     next unless ($u =~ m@^https?://@i);    #  skip non-HTTP or relative URLs
1749     next if ($u =~ m@[/.]hotbot\.com\b@i);     # skip lycos builtins
1750     next if ($u =~ m@[/.]lycos\.com\b@i);      # skip lycos builtins
1751     next if ($u =~ m@[/.]terralycos\.com\b@i); # skip lycos builtins
1752     next if ($u =~ m@[/.]inktomi\.com\b@i);    # skip lycos builtins
1753
1754
1755     LOG ($verbose_filter, "  candidate: $u");
1756     push @candidates, $u;
1757   }
1758
1759   return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1760                                 $timeout, @candidates);
1761 }
1762
1763
1764 \f
1765 ############################################################################
1766 #
1767 # Pick images by feeding random words into news.yahoo.com
1768 #
1769 ############################################################################
1770
1771 my $yahoo_news_url = "http://news.search.yahoo.com/search/news" .
1772                      "?c=news_photos" .
1773                      "&p=";
1774
1775 # yahoonews
1776 sub pick_from_yahoo_news_text($) {
1777   my ($timeout) = @_;
1778
1779   $last_search = $yahoo_news_url;   # for warnings
1780
1781   my $words = random_word();
1782   my $search_url = $yahoo_news_url . $words;
1783
1784   my ($search_hit_count, @subpages) =
1785     pick_from_search_engine ($timeout, $search_url, $words);
1786
1787   my @candidates = ();
1788   foreach my $u (@subpages) {
1789
1790     # de-redirectize the URLs
1791     $u =~ s@^https?://rds\.yahoo\.com/.*-http%3A@http:@s;
1792
1793     # only accept URLs on Yahoo's news site
1794     next unless ($u =~ m@^https?://dailynews\.yahoo\.com/@i ||
1795                  $u =~ m@^https?://story\.news\.yahoo\.com/@i);
1796     next unless ($u =~ m@&u=/@);
1797
1798     LOG ($verbose_filter, "  candidate: $u");
1799     push @candidates, $u;
1800   }
1801
1802   return pick_image_from_pages ($search_url, $search_hit_count, $#subpages+1,
1803                                 $timeout, @candidates);
1804 }
1805
1806
1807 \f
1808 ############################################################################
1809 #
1810 # Pick images from LiveJournal's list of recently-posted images.
1811 #
1812 ############################################################################
1813
1814 my $livejournal_img_url = "http://www.livejournal.com/stats/latest-img.bml";
1815
1816 # With most of our image sources, we get a random page and then select
1817 # from the images on it.  However, in the case of LiveJournal, the page
1818 # of images tends to update slowly; so we'll remember the last N entries
1819 # on it and randomly select from those, to get a wider variety each time.
1820
1821 my $lj_cache_size = 1000;
1822 my @lj_cache = (); # fifo, for ordering by age
1823 my %lj_cache = (); # hash, for detecting dups
1824
1825 # livejournal
1826 sub pick_from_livejournal_images($) {
1827   my ($timeout) = @_;
1828
1829   $last_search = $livejournal_img_url;   # for warnings
1830
1831   my ( $base, $body ) = get_document ($livejournal_img_url, undef, $timeout);
1832
1833   # Often the document comes back empty. If so, just use the cache.
1834   # return () unless $body;
1835   $body = '' unless defined($body);
1836
1837   $body =~ s/\n/ /gs;
1838   $body =~ s/(<recent-image)\b/\n$1/gsi;
1839
1840   foreach (split (/\n/, $body)) {
1841     next unless (m/^<recent-image\b/);
1842     next unless (m/\bIMG=[\'\"]([^\'\"]+)[\'\"]/si);
1843     my $img = html_unquote ($1);
1844
1845     next if ($lj_cache{$img}); # already have it
1846
1847     next unless (m/\bURL=[\'\"]([^\'\"]+)[\'\"]/si);
1848     my $page = html_unquote ($1);
1849     my @pair = ($img, $page);
1850     LOG ($verbose_filter, "  candidate: $img");
1851     push @lj_cache, \@pair;
1852     $lj_cache{$img} = \@pair;
1853   }
1854
1855   return () if ($#lj_cache == -1);
1856
1857   my $n = $#lj_cache+1;
1858   my $i = int(rand($n));
1859   my ($img, $page) = @{$lj_cache[$i]};
1860
1861   # delete this one from @lj_cache and from %lj_cache.
1862   #
1863   @lj_cache = ( @lj_cache[0 .. $i-1],
1864                 @lj_cache[$i+1 .. $#lj_cache] );
1865   delete $lj_cache{$img};
1866
1867   # Keep the size of the cache under the limit by nuking older entries
1868   #
1869   while ($#lj_cache >= $lj_cache_size) {
1870     my $pairP = shift @lj_cache;
1871     my $img = $pairP->[0];
1872     delete $lj_cache{$img};
1873   }
1874
1875   LOG ($verbose_load, "picked image " .($i+1) . "/$n: $img");
1876
1877   return ($page, $img);
1878 }
1879
1880 \f
1881 ############################################################################
1882 #
1883 # Pick images from ircimages.com (images that have been in the /topic of
1884 # various IRC channels.)
1885 #
1886 ############################################################################
1887
1888 my $ircimages_url = "http://ircimages.com/";
1889
1890 # ircimages
1891 sub pick_from_ircimages($) {
1892   my ($timeout) = @_;
1893
1894   $last_search = $ircimages_url;   # for warnings
1895
1896   my $n = int(rand(2900));
1897   my $search_url = $ircimages_url . "page-$n";
1898
1899   my ( $base, $body ) = get_document ($search_url, undef, $timeout);
1900   return () unless $body;
1901
1902   my @candidates = ();
1903
1904   $body =~ s/\n/ /gs;
1905   $body =~ s/(<A)\b/\n$1/gsi;
1906
1907   foreach (split (/\n/, $body)) {
1908
1909     my ($u) = m@<A\s.*\bHREF\s*=\s*([^>]+)>@i;
1910     next unless $u;
1911
1912     if ($u =~ m/^\"([^\"]*)\"/) { $u = $1; }   # quoted string
1913     elsif ($u =~ m/^([^\s]*)\s/) { $u = $1; }  # or token
1914
1915     next unless ($u =~ m/^https?:/i);
1916     next if ($u =~ m@^https?://(searchirc\.com\|ircimages\.com)@i);
1917     next unless ($u =~ m@[.](gif|jpg|jpeg|pjpg|pjpeg|png)$@i);
1918
1919     LOG ($verbose_http, "    HREF: $u");
1920     push @candidates, $u;
1921   }
1922
1923   LOG ($verbose_filter, "" . $#candidates+1 . " links on $search_url");
1924
1925   return () if ($#candidates == -1);
1926
1927   my $i = int(rand($#candidates+1));
1928   my $img = $candidates[$i];
1929
1930   LOG ($verbose_load, "picked image " .($i+1) . "/" . ($#candidates+1) .
1931        ": $img");
1932
1933   $search_url = $img;  # hmm...
1934   return ($search_url, $img);
1935 }
1936
1937 \f
1938 ############################################################################
1939 #
1940 # Pick images from Twitpic's list of recently-posted images.
1941 #
1942 ############################################################################
1943
1944 my $twitpic_img_url = "http://twitpic.com/public_timeline/feed.rss";
1945
1946 # With most of our image sources, we get a random page and then select
1947 # from the images on it.  However, in the case of Twitpic, the page
1948 # of images tends to update slowly; so we'll remember the last N entries
1949 # on it and randomly select from those, to get a wider variety each time.
1950
1951 my $twitpic_cache_size = 1000;
1952 my @twitpic_cache = (); # fifo, for ordering by age
1953 my %twitpic_cache = (); # hash, for detecting dups
1954
1955 # twitpic
1956 sub pick_from_twitpic_images($) {
1957   my ($timeout) = @_;
1958
1959   $last_search = $twitpic_img_url;   # for warnings
1960
1961   my ( $base, $body ) = get_document ($twitpic_img_url, undef, $timeout);
1962
1963   # Update the cache.
1964
1965   if ($body) {
1966     $body =~ s/\n/ /gs;
1967     $body =~ s/(<item)\b/\n$1/gsi;
1968
1969     my @items = split (/\n/, $body);
1970     shift @items;
1971     foreach (@items) {
1972       next unless (m@<link>([^<>]*)</link>@si);
1973       my $page = html_unquote ($1);
1974
1975       $page =~ s@/$@@s;
1976       $page .= '/full';
1977
1978       next if ($twitpic_cache{$page}); # already have it
1979
1980       LOG ($verbose_filter, "  candidate: $page");
1981       push @twitpic_cache, $page;
1982       $twitpic_cache{$page} = $page;
1983     }
1984   }
1985
1986   # Pull from the cache.
1987
1988   return () if ($#twitpic_cache == -1);
1989
1990   my $n = $#twitpic_cache+1;
1991   my $i = int(rand($n));
1992   my $page = $twitpic_cache[$i];
1993
1994   # delete this one from @twitpic_cache and from %twitpic_cache.
1995   #
1996   @twitpic_cache = ( @twitpic_cache[0 .. $i-1],
1997                      @twitpic_cache[$i+1 .. $#twitpic_cache] );
1998   delete $twitpic_cache{$page};
1999
2000   # Keep the size of the cache under the limit by nuking older entries
2001   #
2002   while ($#twitpic_cache >= $twitpic_cache_size) {
2003     my $page = shift @twitpic_cache;
2004     delete $twitpic_cache{$page};
2005   }
2006
2007   ( $base, $body ) = get_document ($page, undef, $timeout);
2008   my $img = undef;
2009   $body = '' unless defined($body);
2010
2011   foreach (split (/<img\s+/, $body)) {
2012     my ($src) = m/\bsrc=[\"\'](.*?)[\"\']/si;
2013     next unless $src;
2014     next if m@/js/@s;
2015     next if m@/images/@s;
2016
2017     $img = $src;
2018
2019     $img = "http:$img" if ($img =~ m@^//@s);  # Oh come on
2020
2021     # Sometimes these images are hosted on twitpic, sometimes on Amazon.
2022     if ($img =~ m@^/@) {
2023       $base =~ s@^(https?://[^/]+)/.*@$1@s;
2024       $img = $base . $img;
2025     }
2026     last;
2027   }
2028
2029   if (!$img) {
2030     LOG ($verbose_load, "no matching images on $page\n");
2031     return ();
2032   }
2033
2034   LOG ($verbose_load, "picked image " .($i+1) . "/$n: $img");
2035
2036   return ($page, $img);
2037 }
2038
2039 \f
2040 ############################################################################
2041 #
2042 # Pick images from Twitter's list of recently-posted updates.
2043 #
2044 ############################################################################
2045
2046 # With most of our image sources, we get a random page and then select
2047 # from the images on it.  However, in the case of Twitter, the page
2048 # of images only updates once a minute; so we'll remember the last N entries
2049 # on it and randomly select from those, to get a wider variety each time.
2050
2051 my $twitter_img_url = "http://api.twitter.com/1/statuses/" .
2052                       "public_timeline.json" .
2053                       "?include_entities=true" .
2054                       "&include_rts=true" .
2055                       "&count=200";
2056
2057 my $twitter_cache_size = 1000;
2058
2059 my @twitter_cache = (); # fifo, for ordering by age
2060 my %twitter_cache = (); # hash, for detecting dups
2061
2062
2063 # twitter
2064 sub pick_from_twitter_images($) {
2065   my ($timeout) = @_;
2066
2067   $last_search = $twitter_img_url;   # for warnings
2068
2069   my ( $base, $body ) = get_document ($twitter_img_url, undef, $timeout);
2070   # Update the cache.
2071
2072   if ($body) {
2073     $body =~ s/[\r\n]+/ /gs;
2074
2075     # Parsing JSON is a pain in the ass.  So we halfass it as usual.
2076     $body =~ s/^\[|\]$//s;
2077     $body =~ s/(\[.*?\])/{ $_ = $1; s@\},@\} @gs; $_; }/gsexi;
2078     my @items = split (/\},\{/, $body);
2079     foreach (@items) {
2080       my ($name) = m@"screen_name":"([^\"]+)"@si;
2081       my ($img)  = m@"media_url":"([^\"]+)"@si;
2082       my ($page) = m@"display_url":"([^\"]+)"@si;
2083       next unless ($name && $img && $page);
2084       foreach ($img, $page) {
2085         s/\\//gs;
2086         $_ = "http://$_" unless (m/^http/si);
2087       }
2088
2089       next if ($twitter_cache{$page}); # already have it
2090
2091       LOG ($verbose_filter, "  candidate: $page - $img");
2092       push @twitter_cache, $page;
2093       $twitter_cache{$page} = $img;
2094     }
2095   }
2096
2097   # Pull from the cache.
2098
2099   return () if ($#twitter_cache == -1);
2100
2101   my $n = $#twitter_cache+1;
2102   my $i = int(rand($n));
2103   my $page = $twitter_cache[$i];
2104   my $url  = $twitter_cache{$page};
2105
2106   # delete this one from @twitter_cache and from %twitter_cache.
2107   #
2108   @twitter_cache = ( @twitter_cache[0 .. $i-1],
2109                      @twitter_cache[$i+1 .. $#twitter_cache] );
2110   delete $twitter_cache{$page};
2111
2112   # Keep the size of the cache under the limit by nuking older entries
2113   #
2114   while ($#twitter_cache >= $twitter_cache_size) {
2115     my $page = shift @twitter_cache;
2116     delete $twitter_cache{$page};
2117   }
2118
2119   LOG ($verbose_load, "picked page $url");
2120
2121   $suppress_audit = 1;
2122
2123   return ($page, $url);
2124 }
2125
2126 \f
2127 ############################################################################
2128 #
2129 # Pick images from Flickr's page of recently-posted photos.
2130 #
2131 ############################################################################
2132
2133 my $flickr_img_url = "http://www.flickr.com/explore/";
2134
2135 # Like LiveJournal, the Flickr page of images tends to update slowly,
2136 # so remember the last N entries on it and randomly select from those.
2137
2138 # I know that Flickr has an API (http://www.flickr.com/services/api/)
2139 # but it was easy enough to scrape the HTML, so I didn't bother exploring.
2140
2141 my $flickr_cache_size = 1000;
2142 my @flickr_cache = (); # fifo, for ordering by age
2143 my %flickr_cache = (); # hash, for detecting dups
2144
2145
2146 # flickr_recent
2147 sub pick_from_flickr_recent($) {
2148   my ($timeout) = @_;
2149
2150   my $start = 16 * int(rand(100));
2151
2152   $last_search = $flickr_img_url;   # for warnings
2153   $last_search .= "?start=$start" if ($start > 0);
2154
2155   my ( $base, $body ) = get_document ($last_search, undef, $timeout);
2156
2157   # If the document comes back empty. just use the cache.
2158   # return () unless $body;
2159   $body = '' unless defined($body);
2160
2161   my $count = 0;
2162   my $count2 = 0;
2163
2164   if ($body =~ m@{ *"_data": \[ ( .*? \} ) \]@six) {
2165     $body = $1;
2166   } else {
2167     LOG ($verbose_load, "flickr unparsable: $last_search");
2168     return ();
2169   }
2170
2171   $body =~ s/[\r\n]/ /gs;
2172   $body =~ s/(\},) *(\{)/$1\n$2/gs;     # "_flickrModelRegistry"
2173
2174   foreach my $chunk (split (/\n/, $body)) {
2175     my ($img) = ($chunk =~ m@"displayUrl": *"(.*?)"@six);
2176     next unless defined ($img);
2177     $img =~ s/\\//gs;
2178     $img = "//" unless ($img =~ m@^/@s);
2179     $img = "http:$img" unless ($img =~ m/^http/s);
2180
2181     my ($user) = ($chunk =~ m/"pathAlias": *"(.*?)"/si);
2182     next unless defined ($user);
2183
2184     my ($id) = ($img =~ m@/\d+/(\d+)_([\da-f]+)_@si);
2185     my ($page) = "https://www.flickr.com/photos/$user/$id/";
2186
2187     # $img =~ s/_[a-z](\.[a-z\d]+)$/$1/si;  # take off "thumb" suffix
2188
2189     $count++;
2190     next if ($flickr_cache{$img}); # already have it
2191
2192     my @pair = ($img, $page, $start);
2193     LOG ($verbose_filter, "  candidate: $img");
2194     push @flickr_cache, \@pair;
2195     $flickr_cache{$img} = \@pair;
2196     $count2++;
2197   }
2198
2199   return () if ($#flickr_cache == -1);
2200
2201   my $n = $#flickr_cache+1;
2202   my $i = int(rand($n));
2203   my ($img, $page) = @{$flickr_cache[$i]};
2204
2205   # delete this one from @flickr_cache and from %flickr_cache.
2206   #
2207   @flickr_cache = ( @flickr_cache[0 .. $i-1],
2208                     @flickr_cache[$i+1 .. $#flickr_cache] );
2209   delete $flickr_cache{$img};
2210
2211   # Keep the size of the cache under the limit by nuking older entries
2212   #
2213   while ($#flickr_cache >= $flickr_cache_size) {
2214     my $pairP = shift @flickr_cache;
2215     my $img = $pairP->[0];
2216     delete $flickr_cache{$img};
2217   }
2218
2219   LOG ($verbose_load, "picked image " .($i+1) . "/$n: $img");
2220
2221   return ($page, $img);
2222 }
2223
2224 \f
2225 ############################################################################
2226 #
2227 # Pick images from a random RSS feed on Flickr.
2228 #
2229 ############################################################################
2230
2231 my $flickr_rss_base = ("http://www.flickr.com/services/feeds/photos_public.gne".
2232                        "?format=rss_200_enc&tagmode=any&tags=");
2233
2234 # Picks a random RSS feed; picks a random image from that feed;
2235 # returns 2 URLs: the page containing the image, and the image.
2236 # Mostly by Joe Mcmahon <mcmahon@yahoo-inc.com>
2237 #
2238 # flickr_random
2239 sub pick_from_flickr_random($) {
2240   my $timeout = shift;
2241
2242   my $words = random_words(',');
2243   my $rss = $flickr_rss_base . $words;
2244   $last_search = $rss;
2245
2246   $_ = $words;
2247   s/,/ /g;
2248
2249   print STDERR "\n\n" if ($verbose_load);
2250   LOG ($verbose_load, "words: $_");
2251   LOG ($verbose_load, "URL: $last_search");
2252
2253   $suppress_audit = 1;
2254
2255   my ( $base, $body ) = get_document ($last_search, undef, $timeout);
2256   if (!$base || !$body) {
2257     $body = undef;
2258     return;
2259   }
2260
2261   my $img;
2262   ($base, $img) = pick_image_from_rss ($base, $body);
2263   $body = undef;
2264   return () unless defined ($img);
2265
2266   LOG ($verbose_load, "redirected to: $base");
2267   return ($base, $img);
2268 }
2269
2270 \f
2271 ############################################################################
2272 #
2273 # Pick random images from Instagram.
2274 #
2275 ############################################################################
2276
2277 my $instagram_url_base = "https://api.instagram.com/v1/media/popular";
2278
2279 # instagram_random
2280 sub pick_from_instagram($) {
2281   my $timeout = shift;
2282
2283   # Liberated access tokens.
2284   # jsdo.it search for: instagram client_id
2285   # Google search for: instagram "&client_id=" site:jsfiddle.net
2286   my @tokens = ('b59fbe4563944b6c88cced13495c0f49', # gramfeed.com
2287                 'fa26679250df49c48a33fbcf30aae989', # instac.at
2288                 'd9494686198d4dfeb954979a3e270e5e', # iconosquare.com
2289                 '793ef48bb18e4197b61afce2d799b81c', # jsdo.it
2290                 '67b8a3e0073449bba70600d0fc68e6cb', # jsdo.it
2291                 '26a098e0df4d4b9ea8b4ce6c505b7742', # jsdo.it
2292                 '2437cbcd906a4c10940f990d283d3cd5', # jsdo.it
2293                 '191c7d7d5312464cbd92134f36ffdab5', # jsdo.it
2294                 'acfec809437b4340b2c38f66503af774', # jsdo.it
2295                 'e9f77604a3a24beba949c12d18130988', # jsdo.it
2296                 '2cd7bcf68ae346529770073d311575b3', # jsdo.it
2297                 '830c600fe8d742e2ab3f3b94f9bb22b7', # jsdo.it
2298                 '55865a0397ad41e5997dd95ef4df8da1', # jsdo.it
2299                 '192a5742f3644ea8bed1d25e439286a8', # jsdo.it
2300                 '38ed1477e7a44595861b8842cdb8ba23', # jsdo.it
2301                 'e52f79f645f54488ad0cc47f6f55ade6', # jsfiddle.net
2302                 );
2303
2304   my $tok = $tokens[int(rand($#tokens+1))];
2305   $last_search = $instagram_url_base . "?client_id=" . $tok;
2306
2307   print STDERR "\n\n" if ($verbose_load);
2308   LOG ($verbose_load, "URL: $last_search");
2309
2310   my ( $base, $body ) = get_document ($last_search, undef, $timeout);
2311   if (!$base || !$body) {
2312     $body = undef;
2313     return;
2314   }
2315
2316   $body =~ s/("link")/\001$1/gs;
2317   my @chunks = split(/\001/, $body);
2318   shift @chunks;
2319   my @urls = ();
2320   foreach (@chunks) {
2321     s/\\//gs;
2322     my ($url) = m/"link":\s*"(.*?)"/s;
2323     my ($img) = m/"standard_resolution":\{"url":\s*"(.*?)"/s;
2324        ($img) = m/"url":\s*"(.*?)"/s unless $url;
2325     next unless ($url && $img);
2326     push @urls, [ $url, $img ];
2327   }
2328
2329   if ($#urls < 0) {
2330     LOG ($verbose_load, "no images on $last_search");
2331     return ();
2332   }
2333
2334   my $i = int(rand($#urls+1));
2335   my ($url, $img) = @{$urls[$i]};
2336
2337   LOG ($verbose_load, "picked image " .($i+1) . "/" . ($#urls+1) . ": $url");
2338   return ($url, $img);
2339 }
2340
2341 \f
2342 ############################################################################
2343 #
2344 # Pick images by waiting for driftnet to populate a temp dir with files.
2345 # Requires driftnet version 0.1.5 or later.
2346 # (Driftnet is a program by Chris Lightfoot that sniffs your local ethernet
2347 # for images being downloaded by others.)
2348 # Driftnet/webcollage integration by jwz.
2349 #
2350 ############################################################################
2351
2352 # driftnet
2353 sub pick_from_driftnet($) {
2354   my ($timeout) = @_;
2355
2356   my $id = $driftnet_magic;
2357   my $dir = $driftnet_dir;
2358   my $start = time;
2359   my $now;
2360
2361   error ("\$driftnet_dir unset?") unless ($dir);
2362   $dir =~ s@/+$@@;
2363
2364   error ("$dir unreadable") unless (-d "$dir/.");
2365
2366   $timeout = $http_timeout unless ($timeout);
2367   $last_search = $id;
2368
2369   while ($now = time, $now < $start + $timeout) {
2370     opendir (my $dir, $dir) || error ("$dir: $!");
2371     while (my $file = readdir($dir)) {
2372       next if ($file =~ m/^\./);
2373       $file = "$dir/$file";
2374       closedir ($dir);
2375       LOG ($verbose_load, "picked file $file ($id)");
2376       return ($id, $file);
2377     }
2378     closedir ($dir);
2379   }
2380   LOG (($verbose_net || $verbose_load), "timed out for $id");
2381   return ();
2382 }
2383
2384
2385 sub get_driftnet_file($) {
2386   my ($file) = @_;
2387
2388   error ("\$driftnet_dir unset?") unless ($driftnet_dir);
2389
2390   my $id = $driftnet_magic;
2391   error ("$id: $file not in $driftnet_dir?")
2392     unless ($file =~ m@^\Q$driftnet_dir@o);
2393
2394   open (my $in, '<', $file) || error ("$id: $file: $!");
2395   my $body = '';
2396   local $/ = undef;  # read entire file
2397   $body = <$in>;
2398   close ($in) || error ("$id: $file: $!");
2399   unlink ($file) || error ("$id: $file: rm: $!");
2400   return ($id, $body);
2401 }
2402
2403
2404 sub spawn_driftnet($) {
2405   my ($cmd) = @_;
2406
2407   # make a directory to use.
2408   while (1) {
2409     my $tmp = $ENV{TEMPDIR} || "/tmp";
2410     $driftnet_dir = sprintf ("$tmp/driftcollage-%08x", rand(0xffffffff));
2411     LOG ($verbose_exec, "mkdir $driftnet_dir");
2412     last if mkdir ($driftnet_dir, 0700);
2413   }
2414
2415   if (! ($cmd =~ m/\s/)) {
2416     # if the command didn't have any arguments in it, then it must be just
2417     # a pointer to the executable.  Append the default args to it.
2418     my $dargs = $default_driftnet_cmd;
2419     $dargs =~ s/^[^\s]+//;
2420     $cmd .= $dargs;
2421   }
2422
2423   # point the driftnet command at our newly-minted private directory.
2424   #
2425   $cmd .= " -d $driftnet_dir";
2426   $cmd .= ">/dev/null" unless ($verbose_exec);
2427
2428   my $pid = fork();
2429   if ($pid < 0) { error ("fork: $!\n"); }
2430   if ($pid) {
2431     # parent fork
2432     push @pids_to_kill, $pid;
2433     LOG ($verbose_exec, "forked for \"$cmd\"");
2434   } else {
2435     # child fork
2436     nontrapping_system ($cmd) || error ("exec: $!");
2437   }
2438
2439   # wait a bit, then make sure the process actually started up.
2440   #
2441   sleep (1);
2442   error ("pid $pid failed to start \"$cmd\"")
2443     unless (1 == kill (0, $pid));
2444 }
2445
2446 # local-directory
2447 sub pick_from_local_dir($) {
2448   my ($timeout) = @_;
2449
2450   my $id = $local_magic;
2451   $last_search = $id;
2452
2453   my $dir = $local_dir;
2454   error ("\$local_dir unset?") unless ($dir);
2455   $dir =~ s@/+$@@;
2456
2457   error ("$dir unreadable") unless (-d "$dir/.");
2458
2459   my $v = ($verbose_exec ? "-v" : "");
2460   my $pick = `xscreensaver-getimage-file $v "$dir"`;
2461   $pick =~ s/\s+$//s;
2462   $pick = "$dir/$pick" unless ($pick =~ m@^/@s);       # relative path
2463
2464   LOG ($verbose_load, "picked file $pick ($id)");
2465   return ($id, $pick);
2466 }
2467
2468
2469 sub get_local_file($) {
2470   my ($file) = @_;
2471
2472   error ("\$local_dir unset?") unless ($local_dir);
2473
2474   my $id = $local_magic;
2475   error ("$id: $file not in $local_dir?")
2476     unless ($file =~ m@^\Q$local_dir@o);
2477
2478   open (my $in, '<', $file) || error ("$id: $file: $!");
2479   local $/ = undef;  # read entire file
2480   my $body = <$in>;
2481   close ($in) || error ("$id: $file: $!");
2482   return ($id, $body);
2483 }
2484
2485
2486 \f
2487 ############################################################################
2488 #
2489 # Pick a random image in a random way
2490 #
2491 ############################################################################
2492
2493
2494 # Picks a random image on a random page, and returns two URLs:
2495 # the page containing the image, and the image.
2496 # Returns () if nothing found this time.
2497 #
2498
2499 sub pick_image(;$) {
2500   my ($timeout) = @_;
2501
2502   $current_state = "select";
2503   $load_method = "none";
2504
2505   my $n = int(rand(100));
2506   my $fn = undef;
2507   my $total = 0;
2508   my @rest = @search_methods;
2509
2510   while (@rest) {
2511     my $pct  = shift @rest;
2512     my $name = shift @rest;
2513     my $tfn  = shift @rest;
2514     $total += $pct;
2515     if ($total > $n && !defined($fn)) {
2516       $fn = $tfn;
2517       $current_state = $name;
2518       $load_method = $current_state;
2519     }
2520   }
2521
2522   if ($total != 100) {
2523     error ("internal error: \@search_methods totals to $total%!");
2524   }
2525
2526   record_attempt ($current_state);
2527   return $fn->($timeout);
2528 }
2529
2530
2531 \f
2532 ############################################################################
2533 #
2534 # Statistics and logging
2535 #
2536 ############################################################################
2537
2538 sub timestr() {
2539   return strftime ("%H:%M:%S: ", localtime);
2540 }
2541
2542 sub blurb() {
2543   return "$progname: " . timestr() . "$current_state: ";
2544 }
2545
2546 sub error($) {
2547   my ($err) = @_;
2548   print STDERR blurb() . "$err\n";
2549   exit 1;
2550 }
2551
2552 sub stacktrace() {
2553   my $i = 1;
2554   print STDERR "$progname: stack trace:\n";
2555   while (1) {
2556     my ($package, $filename, $line, $subroutine) = caller($i++);
2557     last unless defined($package);
2558     $filename =~ s@^.*/@@;
2559     print STDERR "  $filename#$line, $subroutine\n";
2560   }
2561 }
2562
2563
2564 my $lastlog = "";
2565
2566 sub clearlog() {
2567   $lastlog = "";
2568 }
2569
2570 sub showlog() {
2571   my $head = "$progname: DEBUG: ";
2572   foreach (split (/\n/, $lastlog)) {
2573     print STDERR "$head$_\n";
2574   }
2575   $lastlog = "";
2576 }
2577
2578 sub LOG($$) {
2579   my ($print, $msg) = @_;
2580   my $blurb = timestr() . "$current_state: ";
2581   $lastlog .= "$blurb$msg\n";
2582   print STDERR "$progname: $blurb$msg\n" if $print;
2583 }
2584
2585
2586 my %stats_attempts;
2587 my %stats_successes;
2588 my %stats_elapsed;
2589
2590 my $last_state = undef;
2591 sub record_attempt($) {
2592   my ($name) = @_;
2593
2594   if ($last_state) {
2595     record_failure($last_state) unless ($image_succeeded > 0);
2596   }
2597   $last_state = $name;
2598
2599   clearlog();
2600   report_performance();
2601
2602   start_timer($name);
2603   $image_succeeded = 0;
2604   $suppress_audit = 0;
2605 }
2606
2607 sub record_success($$$) {
2608   my ($name, $url, $base) = @_;
2609   if (defined($stats_successes{$name})) {
2610     $stats_successes{$name}++;
2611   } else {
2612     $stats_successes{$name} = 1;
2613   }
2614
2615   stop_timer ($name, 1);
2616   my $o = $current_state;
2617   $current_state = $name;
2618   save_recent_url ($url, $base);
2619   $current_state = $o;
2620   $image_succeeded = 1;
2621   clearlog();
2622 }
2623
2624
2625 sub record_failure($) {
2626   my ($name) = @_;
2627
2628   return if $image_succeeded;
2629
2630   stop_timer ($name, 0);
2631   if ($verbose_load && !$verbose_exec) {
2632
2633     if ($suppress_audit) {
2634       print STDERR "$progname: " . timestr() . "(audit log suppressed)\n";
2635       return;
2636     }
2637
2638     my $o = $current_state;
2639     $current_state = "DEBUG";
2640
2641     my $line =  "#" x 78;
2642     print STDERR "\n\n\n";
2643     print STDERR ("#" x 78) . "\n";
2644     print STDERR blurb() . "failed to get an image.  Full audit log:\n";
2645     print STDERR "\n";
2646     showlog();
2647     print STDERR ("-" x 78) . "\n";
2648     print STDERR "\n\n";
2649
2650     $current_state = $o;
2651   }
2652   $image_succeeded = 0;
2653 }
2654
2655
2656
2657 sub stats_of($) {
2658   my ($name) = @_;
2659   my $i = $stats_successes{$name};
2660   my $j = $stats_attempts{$name};
2661   $i = 0 unless $i;
2662   $j = 0 unless $j;
2663   return "" . ($j ? int($i * 100 / $j) : "0") . "%";
2664 }
2665
2666
2667 my $current_start_time = 0;
2668
2669 sub start_timer($) {
2670   my ($name) = @_;
2671   $current_start_time = time;
2672
2673   if (defined($stats_attempts{$name})) {
2674     $stats_attempts{$name}++;
2675   } else {
2676     $stats_attempts{$name} = 1;
2677   }
2678   if (!defined($stats_elapsed{$name})) {
2679     $stats_elapsed{$name} = 0;
2680   }
2681 }
2682
2683 sub stop_timer($$) {
2684   my ($name, $success) = @_;
2685   $stats_elapsed{$name} += time - $current_start_time;
2686 }
2687
2688
2689 my $last_report_time = 0;
2690 sub report_performance() {
2691
2692   return unless $verbose_warnings;
2693
2694   my $now = time;
2695   return unless ($now >= $last_report_time + $report_performance_interval);
2696   my $ot = $last_report_time;
2697   $last_report_time = $now;
2698
2699   return if ($ot == 0);
2700
2701   my $blurb = "$progname: " . timestr();
2702
2703   print STDERR "\n";
2704   print STDERR "${blurb}Current standings:\n";
2705
2706   foreach my $name (sort keys (%stats_attempts)) {
2707     my $try = $stats_attempts{$name};
2708     my $suc = $stats_successes{$name} || 0;
2709     my $pct = int($suc * 100 / $try);
2710     my $secs = $stats_elapsed{$name};
2711     my $secs_link = $secs / $try;
2712     print STDERR sprintf ("$blurb %-14s %4s (%d/%d);" .
2713                           "       \t %.1f secs/link\n",
2714                           "$name:", "$pct%", $suc, $try, $secs_link);
2715   }
2716 }
2717
2718
2719
2720 my $max_recent_images = 400;
2721 my $max_recent_sites  = 20;
2722 my @recent_images = ();
2723 my @recent_sites = ();
2724
2725 sub save_recent_url($$) {
2726   my ($url, $base) = @_;
2727
2728   return unless ($verbose_warnings);
2729
2730   $_ = $url;
2731   my ($site) = m@^https?://([^ \t\n\r/:]+)@;
2732   return unless defined ($site);
2733
2734   if ($base eq $driftnet_magic || $base eq $local_magic) {
2735     $site = $base;
2736     @recent_images = ();
2737   }
2738
2739   my $done = 0;
2740   foreach (@recent_images) {
2741     if ($_ eq $url) {
2742       print STDERR blurb() . "WARNING: recently-duplicated image: $url" .
2743         " (on $base via $last_search)\n";
2744       $done = 1;
2745       last;
2746     }
2747   }
2748
2749   # suppress "duplicate site" warning via %warningless_sites.
2750   #
2751   if ($warningless_sites{$site}) {
2752     $done = 1;
2753   } elsif ($site =~ m@([^.]+\.[^.]+\.[^.]+)$@ &&
2754            $warningless_sites{$1}) {
2755     $done = 1;
2756   } elsif ($site =~ m@([^.]+\.[^.]+)$@ &&
2757            $warningless_sites{$1}) {
2758     $done = 1;
2759   }
2760
2761   if (!$done) {
2762     foreach (@recent_sites) {
2763       if ($_ eq $site) {
2764         print STDERR blurb() . "WARNING: recently-duplicated site: $site" .
2765         " ($url on $base via $last_search)\n";
2766         last;
2767       }
2768     }
2769   }
2770
2771   push @recent_images, $url;
2772   push @recent_sites,  $site;
2773   shift @recent_images if ($#recent_images >= $max_recent_images);
2774   shift @recent_sites  if ($#recent_sites  >= $max_recent_sites);
2775 }
2776
2777
2778 \f
2779 ##############################################################################
2780 #
2781 # other utilities
2782 #
2783 ##############################################################################
2784
2785 # Does %-decoding.
2786 #
2787 sub url_decode($) {
2788   ($_) = @_;
2789   tr/+/ /;
2790   s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
2791   return $_;
2792 }
2793
2794
2795 # Given the raw body of a GIF document, returns the dimensions of the image.
2796 #
2797 sub gif_size($) {
2798   my ($body) = @_;
2799   my $type = substr($body, 0, 6);
2800   my $s;
2801   return () unless ($type =~ /GIF8[7,9]a/);
2802   $s = substr ($body, 6, 10);
2803   my ($a,$b,$c,$d) = unpack ("C"x4, $s);
2804   return () unless defined ($d);
2805   return (($b<<8|$a), ($d<<8|$c));
2806 }
2807
2808 # Given the raw body of a JPEG document, returns the dimensions of the image.
2809 #
2810 sub jpeg_size($) {
2811   my ($body) = @_;
2812   my $i = 0;
2813   my $L = length($body);
2814
2815   my $c1 = substr($body, $i, 1); $i++;
2816   my $c2 = substr($body, $i, 1); $i++;
2817   return () unless (ord($c1) == 0xFF && ord($c2) == 0xD8);
2818
2819   my $ch = "0";
2820   while (ord($ch) != 0xDA && $i < $L) {
2821     # Find next marker, beginning with 0xFF.
2822     while (ord($ch) != 0xFF) {
2823       return () if (length($body) <= $i);
2824       $ch = substr($body, $i, 1); $i++;
2825     }
2826     # markers can be padded with any number of 0xFF.
2827     while (ord($ch) == 0xFF) {
2828       return () if (length($body) <= $i);
2829       $ch = substr($body, $i, 1); $i++;
2830     }
2831
2832     # $ch contains the value of the marker.
2833     my $marker = ord($ch);
2834
2835     if (($marker >= 0xC0) &&
2836         ($marker <= 0xCF) &&
2837         ($marker != 0xC4) &&
2838         ($marker != 0xCC)) {  # it's a SOFn marker
2839       $i += 3;
2840       return () if (length($body) <= $i);
2841       my $s = substr($body, $i, 4); $i += 4;
2842       my ($a,$b,$c,$d) = unpack("C"x4, $s);
2843       return (($c<<8|$d), ($a<<8|$b));
2844
2845     } else {
2846       # We must skip variables, since FFs in variable names aren't
2847       # valid JPEG markers.
2848       return () if (length($body) <= $i);
2849       my $s = substr($body, $i, 2); $i += 2;
2850       my ($c1, $c2) = unpack ("C"x2, $s);
2851       my $length = ($c1 << 8) | $c2;
2852       return () if ($length < 2);
2853       $i += $length-2;
2854     }
2855   }
2856   return ();
2857 }
2858
2859 # Given the raw body of a PNG document, returns the dimensions of the image.
2860 #
2861 sub png_size($) {
2862   my ($body) = @_;
2863   return () unless ($body =~ m/^\211PNG\r/);
2864   my ($bits) = ($body =~ m/^.{12}(.{12})/s);
2865   return () unless defined ($bits);
2866   return () unless ($bits =~ /^IHDR/);
2867   my ($ign, $w, $h) = unpack("a4N2", $bits);
2868   return ($w, $h);
2869 }
2870
2871
2872 # Given the raw body of a GIF, JPEG, or PNG document, returns the dimensions
2873 # of the image.
2874 #
2875 sub image_size($) {
2876   my ($body) = @_;
2877   my ($w, $h) = gif_size ($body);
2878   if ($w && $h) { return ($w, $h); }
2879   ($w, $h) = jpeg_size ($body);
2880   if ($w && $h) { return ($w, $h); }
2881   return png_size ($body);
2882 }
2883
2884
2885 # returns the full path of the named program, or undef.
2886 #
2887 sub which($) {
2888   my ($prog) = @_;
2889   foreach (split (/:/, $ENV{PATH})) {
2890     my $path = "$_/$prog";
2891     if (-x $path) {
2892       return $path;
2893     }
2894   }
2895   return undef;
2896 }
2897
2898
2899 # Like rand(), but chooses numbers with a bell curve distribution.
2900 sub bellrand(;$) {
2901   ($_) = @_;
2902   $_ = 1.0 unless defined($_);
2903   $_ /= 3.0;
2904   return (rand($_) + rand($_) + rand($_));
2905 }
2906
2907
2908 sub exit_cleanup() {
2909   x_cleanup();
2910   print STDERR "$progname: exiting\n" if ($verbose_warnings);
2911   if (@pids_to_kill) {
2912     print STDERR blurb() . "killing: " . join(' ', @pids_to_kill) . "\n";
2913     kill ('TERM', @pids_to_kill);
2914   }
2915 }
2916
2917 sub signal_cleanup($) {
2918   my ($sig) = @_;
2919   print STDERR blurb() . (defined($sig)
2920                           ? "caught signal $sig."
2921                           : "exiting.")
2922                        . "\n"
2923     if ($verbose_exec || $verbose_warnings);
2924   exit 1;
2925 }
2926
2927
2928
2929 ##############################################################################
2930 #
2931 # Generating a list of urls only
2932 #
2933 ##############################################################################
2934
2935 sub url_only_output() {
2936   do {
2937     my ($base, $img) = pick_image;
2938     if ($img) {
2939       $base =~ s/ /%20/g;
2940       $img  =~ s/ /%20/g;
2941       print "$img $base\n";
2942     }
2943   } while (1);
2944 }
2945
2946 ##############################################################################
2947 #
2948 # Running as an xscreensaver module, or as a web page imagemap
2949 #
2950 ##############################################################################
2951
2952 my ($image_ppm, $image_tmp1, $image_tmp2);
2953 {
2954   my $seed = rand(0xFFFFFFFF);
2955   $image_ppm = sprintf ("%s/webcollage-%08x",
2956                         ($ENV{TMPDIR} ? $ENV{TMPDIR} : "/tmp"),
2957                         $seed);
2958   $image_tmp1 = $image_ppm . '-1.ppm';
2959   $image_tmp2 = $image_ppm . '-2.ppm';
2960   $image_ppm .= '.ppm';
2961 }
2962
2963
2964 my $filter_cmd = undef;
2965 my $post_filter_cmd = undef;
2966 my $background = undef;
2967
2968 my @imagemap_areas = ();
2969 my $imagemap_html_tmp = undef;
2970 my $imagemap_jpg_tmp = undef;
2971
2972
2973 my $img_width;            # size of the image being generated.
2974 my $img_height;
2975
2976 my $delay = 2;
2977
2978 sub x_cleanup() {
2979   unlink $image_ppm, $image_tmp1, $image_tmp2;
2980   unlink $imagemap_html_tmp, $imagemap_jpg_tmp
2981     if (defined ($imagemap_html_tmp));
2982 }
2983
2984
2985 # Like system, but prints status about exit codes, and kills this process
2986 # with whatever signal killed the sub-process, if any.
2987 #
2988 sub nontrapping_system(@) {
2989   $! = 0;
2990
2991   $_ = join(" ", @_);
2992   s/\"[^\"]+\"/\"...\"/g;
2993
2994   LOG ($verbose_exec, "executing \"$_\"");
2995
2996   my $rc = system @_;
2997
2998   if ($rc == 0) {
2999     LOG ($verbose_exec, "subproc exited normally.");
3000   } elsif (($rc & 0xff) == 0) {
3001     $rc >>= 8;
3002     LOG ($verbose_exec, "subproc exited with status $rc.");
3003   } else {
3004     if ($rc & 0x80) {
3005       LOG ($verbose_exec, "subproc dumped core.");
3006       $rc &= ~0x80;
3007     }
3008     LOG ($verbose_exec, "subproc died with signal $rc.");
3009     # die that way ourselves.
3010     kill $rc, $$;
3011   }
3012
3013   return $rc;
3014 }
3015
3016
3017 # Given the URL of a GIF, JPEG, or PNG image, and the body of that image,
3018 # writes a PPM to the given output file.  Returns the width/height of the
3019 # image if successful.
3020 #
3021 sub image_to_pnm($$$) {
3022   my ($url, $body, $output) = @_;
3023   my ($cmd, $cmd2, $w, $h);
3024
3025   if ((@_ = gif_size ($body))) {
3026     ($w, $h) = @_;
3027     $cmd = "giftopnm";
3028   } elsif ((@_ = jpeg_size ($body))) {
3029     ($w, $h) = @_;
3030     $cmd = "djpeg";
3031   } elsif ((@_ = png_size ($body))) {
3032     ($w, $h) = @_;
3033     $cmd = "pngtopnm";
3034   } else {
3035     LOG (($verbose_pbm || $verbose_load),
3036          "not a GIF, JPG, or PNG" .
3037          (($body =~ m@<(base|html|head|body|script|table|a href)\b@i)
3038           ? " (looks like HTML)" : "") .
3039          ": $url");
3040     $suppress_audit = 1;
3041     return ();
3042   }
3043
3044   $cmd2 = "exec $cmd";        # yes, this really is necessary.  if we don't
3045                               # do this, the process doesn't die properly.
3046   if (!$verbose_pbm) {
3047     #
3048     # We get a "giftopnm: got a 'Application Extension' extension"
3049     # warning any time it's an animgif.
3050     #
3051     # Note that "giftopnm: EOF / read error on image data" is not
3052     # always a fatal error -- sometimes the image looks fine anyway.
3053     #
3054     $cmd2 .= " 2>/dev/null";
3055   }
3056
3057   # There exist corrupted GIF and JPEG files that can make giftopnm and
3058   # djpeg lose their minds and go into a loop.  So this gives those programs
3059   # a small timeout -- if they don't complete in time, kill them.
3060   #
3061   my $pid;
3062   @_ = eval {
3063     my $timed_out;
3064
3065     local $SIG{ALRM}  = sub {
3066       LOG ($verbose_pbm,
3067            "timed out ($cvt_timeout) for $cmd on \"$url\" in pid $pid");
3068       kill ('TERM', $pid) if ($pid);
3069       $timed_out = 1;
3070       $body = undef;
3071     };
3072
3073     if (($pid = open (my $pipe, "| $cmd2 > $output"))) {
3074       $timed_out = 0;
3075       alarm $cvt_timeout;
3076       print $pipe $body;
3077       $body = undef;
3078       close $pipe;
3079
3080       LOG ($verbose_exec, "awaiting $pid");
3081       waitpid ($pid, 0);
3082       LOG ($verbose_exec, "$pid completed");
3083
3084       my $size = (stat($output))[7];
3085       $size = -1 unless defined($size);
3086       if ($size < 5) {
3087         LOG ($verbose_pbm, "$cmd on ${w}x$h \"$url\" failed ($size bytes)");
3088         return ();
3089       }
3090
3091       LOG ($verbose_pbm, "created ${w}x$h $output ($cmd)");
3092       return ($w, $h);
3093     } else {
3094       print STDERR blurb() . "$cmd failed: $!\n";
3095       return ();
3096     }
3097   };
3098   die if ($@ && $@ ne "alarm\n");       # propagate errors
3099   if ($@) {
3100     # timed out
3101     $body = undef;
3102     return ();
3103   } else {
3104     # didn't
3105     alarm 0;
3106     $body = undef;
3107     return @_;
3108   }
3109 }
3110
3111
3112 # Same as the "ppmmake" command: creates a solid-colored PPM.
3113 # Does not understand the rgb.txt color names except "black" and "white".
3114 #
3115 sub ppmmake($$$$) {
3116   my ($outfile, $bgcolor, $w, $h) = @_;
3117
3118   my ($r, $g, $b);
3119   if ($bgcolor =~ m/^\#?([\dA-F][\dA-F])([\dA-F][\dA-F])([\dA-F][\dA-F])$/i ||
3120       $bgcolor =~ m/^\#?([\dA-F])([\dA-F])([\dA-F])$/i) {
3121     ($r, $g, $b) = (hex($1), hex($2), hex($3));
3122   } elsif ($bgcolor =~ m/^black$/i) {
3123     ($r, $g, $b) = (0, 0, 0);
3124   } elsif ($bgcolor =~ m/^white$/i) {
3125     ($r, $g, $b) = (0xFF, 0xFF, 0xFF);
3126   } else {
3127     error ("unparsable color name: $bgcolor");
3128   }
3129
3130   my $pixel = pack('CCC', $r, $g, $b);
3131   my $bits = "P6\n$w $h\n255\n" . ($pixel x ($w * $h));
3132
3133   open (my $out, '>', $outfile) || error ("$outfile: $!");
3134   print $out $bits;
3135   close $out;
3136 }
3137
3138
3139 sub pick_root_displayer() {
3140   my @names = ();
3141
3142   if ($cocoa_p) {
3143     # see "xscreensaver/hacks/webcollage-cocoa.m"
3144     return "echo COCOA LOAD ";
3145   }
3146
3147   foreach my $cmd (@root_displayers) {
3148     $_ = $cmd;
3149     my ($name) = m/^([^ ]+)/;
3150     push @names, "\"$name\"";
3151     LOG ($verbose_exec, "looking for $name...");
3152     foreach my $dir (split (/:/, $ENV{PATH})) {
3153       LOG ($verbose_exec, "  checking $dir/$name");
3154       return $cmd if (-x "$dir/$name");
3155     }
3156   }
3157
3158   $names[$#names] = "or " . $names[$#names];
3159   error "none of: " . join (", ", @names) . " were found on \$PATH.";
3160 }
3161
3162
3163 my $ppm_to_root_window_cmd = undef;
3164
3165
3166 sub x_or_pbm_output($) {
3167   my ($window_id) = @_;
3168
3169   # Adjust the PATH for OS X 10.10.
3170   #
3171   $_ = $0;
3172   s:/[^/]*$::;
3173   s/([^a-zA-Z0-9._\-+\/])/\\$1/g;
3174   $ENV{PATH} = "$_:$ENV{PATH}";
3175
3176   # Check for our helper program, to see whether we need to use PPM pipelines.
3177   #
3178   $_ = "webcollage-helper";
3179
3180   if (! defined ($webcollage_helper)) {
3181     $webcollage_helper = which ($_);
3182   }
3183
3184   if (defined ($webcollage_helper)) {
3185     LOG ($verbose_pbm, "found \"$webcollage_helper\"");
3186     $webcollage_helper = "'$webcollage_helper' -v";
3187   } else {
3188     LOG (($verbose_pbm || $verbose_load), "no $_ program");
3189   }
3190
3191   if ($cocoa_p && !defined ($webcollage_helper)) {
3192     error ("webcollage-helper not found in Cocoa-mode!");
3193   }
3194
3195
3196   # make sure the various programs we execute exist, right up front.
3197   #
3198   my @progs = ();
3199
3200   if (!defined($webcollage_helper)) {
3201     # Only need these others if we don't have the helper.
3202     @progs = (@progs,
3203               "giftopnm", "pngtopnm", "djpeg",
3204               "pnmpaste", "pnmscale", "pnmcut");
3205   }
3206
3207   foreach (@progs) {
3208     which ($_) || error "$_ not found on \$PATH.";
3209   }
3210
3211   # If we're using webcollage-helper and not a filter, then the tmp files
3212   # are JPEGs, not PPMs.
3213   #
3214   if (defined ($webcollage_helper) && !defined ($filter_cmd)) {
3215     foreach ($image_ppm, $image_tmp1, $image_tmp2) {
3216       s/\.ppm$/.jpg/s;
3217     }
3218   }
3219
3220
3221   # find a root-window displayer program.
3222   #
3223   if (!$no_output_p) {
3224     $ppm_to_root_window_cmd = pick_root_displayer();
3225   }
3226
3227   if (defined ($window_id)) {
3228     error ("-window-id only works if xscreensaver-getimage is installed")
3229       unless ($ppm_to_root_window_cmd =~ m/^xscreensaver-getimage\b/);
3230
3231     error ("unparsable window id: $window_id")
3232       unless ($window_id =~ m/^\d+$|^0x[\da-f]+$/i);
3233     $ppm_to_root_window_cmd =~ s/--?root\b/$window_id/ ||
3234       error ("unable to munge displayer: $ppm_to_root_window_cmd");
3235   }
3236
3237   if (!$img_width || !$img_height) {
3238
3239     if (!defined ($window_id) &&
3240         defined ($ENV{XSCREENSAVER_WINDOW})) {
3241       $window_id = $ENV{XSCREENSAVER_WINDOW};
3242     }
3243
3244     if (!defined ($window_id)) {
3245       $_ = "xdpyinfo";
3246       which ($_) || error "$_ not found on \$PATH.";
3247       $_ = `$_`;
3248       ($img_width, $img_height) = m/dimensions: *(\d+)x(\d+) /;
3249       if (!defined($img_height)) {
3250         error "xdpyinfo failed.";
3251       }
3252     } else {  # we have a window id
3253       $_ = "xwininfo";
3254       which ($_) || error "$_ not found on \$PATH.";
3255       $_ .= " -id $window_id";
3256       $_ = `$_`;
3257       ($img_width, $img_height) = m/^\s*Width:\s*(\d+)\n\s*Height:\s*(\d+)\n/m;
3258
3259       if (!defined($img_height)) {
3260         error "xwininfo failed.";
3261       }
3262     }
3263   }
3264
3265   my $bgcolor = "#000000";
3266   my $bgimage = undef;
3267
3268   if ($background) {
3269     if ($background =~ m/^\#[0-9a-f]+$/i) {
3270       $bgcolor = $background;
3271
3272     } elsif (-r $background) {
3273       $bgimage = $background;
3274
3275     } elsif (! $background =~ m@^[-a-z0-9 ]+$@i) {
3276       error "not a color or readable file: $background";
3277
3278     } else {
3279       # default to assuming it's a color
3280       $bgcolor = $background;
3281     }
3282   }
3283
3284   # Create the sold-colored base image.
3285   #
3286   LOG ($verbose_pbm, "creating base image: ${img_width}x${img_height}");
3287   $_ = ppmmake ($image_ppm, $bgcolor, $img_width, $img_height);
3288
3289   # Paste the default background image in the middle of it.
3290   #
3291   if ($bgimage) {
3292     my ($iw, $ih);
3293
3294     my $body = "";
3295     open (my $imgf, '<', $bgimage) || error "couldn't open $bgimage: $!";
3296     local $/ = undef;  # read entire file
3297     $body = <$imgf>;
3298     close ($imgf);
3299
3300     my $cmd;
3301     if ((@_ = gif_size ($body))) {
3302       ($iw, $ih) = @_;
3303       $cmd = "giftopnm |";
3304
3305     } elsif ((@_ = jpeg_size ($body))) {
3306       ($iw, $ih) = @_;
3307       $cmd = "djpeg |";
3308
3309     } elsif ((@_ = png_size ($body))) {
3310       ($iw, $ih) = @_;
3311       $cmd = "pngtopnm |";
3312
3313     } elsif ($body =~ m/^P\d\n(\d+) (\d+)\n/) {
3314       $iw = $1;
3315       $ih = $2;
3316       $cmd = "";
3317
3318     } else {
3319       error "$bgimage is not a GIF, JPEG, PNG, or PPM.";
3320     }
3321
3322     my $x = int (($img_width  - $iw) / 2);
3323     my $y = int (($img_height - $ih) / 2);
3324     LOG ($verbose_pbm,
3325          "pasting $bgimage (${iw}x$ih) into base image at $x,$y");
3326
3327     $cmd .= "pnmpaste - $x $y $image_ppm > $image_tmp1";
3328     open ($imgf, "| $cmd") || error "running $cmd: $!";
3329     print $imgf $body;
3330     $body = undef;
3331     close ($imgf);
3332     LOG ($verbose_exec, "subproc exited normally.");
3333     rename ($image_tmp1, $image_ppm) ||
3334       error "renaming $image_tmp1 to $image_ppm: $!";
3335   }
3336
3337   clearlog();
3338
3339   while (1) {
3340     my ($base, $img) = pick_image();
3341     my $source = $current_state;
3342     $current_state = "loadimage";
3343     if ($img) {
3344       my ($headers, $body) = get_document ($img, $base);
3345       if ($body) {
3346         paste_image ($base, $img, $body, $source);
3347         $body = undef;
3348       }
3349     }
3350     $current_state = "idle";
3351     $load_method = "none";
3352
3353     unlink $image_tmp1, $image_tmp2;
3354     sleep $delay;
3355   }
3356 }
3357
3358 sub paste_image($$$$) {
3359   my ($base, $img, $body, $source) = @_;
3360
3361   $current_state = "paste";
3362
3363   $suppress_audit = 0;
3364
3365   LOG ($verbose_pbm, "got $img (" . length($body) . ")");
3366
3367   my ($iw, $ih);
3368
3369   # If we are using the webcollage-helper, then we do not need to convert this
3370   # image to a PPM.  But, if we're using a filter command, we still must, since
3371   # that's what the filters expect (webcollage-helper can read PPMs, so that's
3372   # fine.)
3373   #
3374   if (defined ($webcollage_helper) &&
3375       !defined ($filter_cmd)) {
3376
3377     ($iw, $ih) = image_size ($body);
3378     if (!$iw || !$ih) {
3379       LOG (($verbose_pbm || $verbose_load),
3380            "not a GIF, JPG, or PNG" .
3381            (($body =~ m@<(base|html|head|body|script|table|a href)>@i)
3382             ? " (looks like HTML)" : "") .
3383            ": $img");
3384       $suppress_audit = 1;
3385       $body = undef;
3386       return 0;
3387     }
3388
3389     if ($iw <= 0 || $ih <= 0 || $iw > 9999 || $ih > 9999) {
3390       LOG (($verbose_pbm || $verbose_load),
3391            "ludicrous image dimensions: $iw x $ih (" . length($body) .
3392            "): $img");
3393       $body = undef;
3394       return 0;
3395     }
3396
3397     open (my $out, '>', $image_tmp1) || error ("writing $image_tmp1: $!");
3398     (print $out $body) || error ("writing $image_tmp1: $!");
3399     close ($out) || error ("writing $image_tmp1: $!");
3400
3401   } else {
3402     ($iw, $ih) = image_to_pnm ($img, $body, $image_tmp1);
3403     $body = undef;
3404     if (!$iw || !$ih) {
3405       LOG ($verbose_pbm, "unable to make PBM from $img");
3406       return 0;
3407     }
3408   }
3409
3410   record_success ($load_method, $img, $base);
3411
3412
3413   my $ow = $iw;  # used only for error messages
3414   my $oh = $ih;
3415
3416   # don't just tack this onto the front of the pipeline -- we want it to
3417   # be able to change the size of the input image.
3418   #
3419   if ($filter_cmd) {
3420     LOG ($verbose_pbm, "running $filter_cmd");
3421
3422     my $rc = nontrapping_system "($filter_cmd) < $image_tmp1 >$image_tmp2";
3423     if ($rc != 0) {
3424       LOG(($verbose_pbm || $verbose_load), "failed command: \"$filter_cmd\"");
3425       LOG(($verbose_pbm || $verbose_load), "failed URL: \"$img\" (${ow}x$oh)");
3426       return;
3427     }
3428     rename ($image_tmp2, $image_tmp1);
3429
3430     # re-get the width/height in case the filter resized it.
3431     open (my $imgf, '<', $image_tmp1) || return 0;
3432     $_ = <$imgf>;
3433     $_ = <$imgf>;
3434     ($iw, $ih) = m/^(\d+) (\d+)$/;
3435     close ($imgf);
3436     return 0 unless ($iw && $ih);
3437   }
3438
3439   my $target_w = $img_width;   # max rectangle into which the image must fit
3440   my $target_h = $img_height;
3441
3442   my $cmd = "";
3443   my $scale = 1.0;
3444
3445
3446   # Usually scale the image to fit on the screen -- but sometimes scale it
3447   # to fit on half or a quarter of the screen.  (We do this by reducing the
3448   # size of the target rectangle.)  Note that the image is not merely scaled
3449   # to fit; we instead cut the image in half repeatedly until it fits in the
3450   # target rectangle -- that gives a wider distribution of sizes.
3451   #
3452   if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; } # reduce target rect
3453   if (rand() < 0.3) { $target_w /= 2; $target_h /= 2; }
3454
3455   if ($iw > $target_w || $ih > $target_h) {
3456     while ($iw > $target_w ||
3457            $ih > $target_h) {
3458       $iw = int($iw / 2);
3459       $ih = int($ih / 2);
3460       $scale /= 2;
3461     }
3462     if ($iw <= 10 || $ih <= 10) {
3463       LOG ($verbose_pbm, "scaling to ${iw}x$ih would have been bogus.");
3464       return 0;
3465     }
3466
3467     LOG ($verbose_pbm, "scaling to ${iw}x$ih ($scale)");
3468
3469     $cmd .= " | pnmscale -xsize $iw -ysize $ih";
3470   }
3471
3472
3473   my $src = $image_tmp1;
3474
3475   my $crop_x = 0;     # the sub-rectangle of the image
3476   my $crop_y = 0;     # that we will actually paste.
3477   my $crop_w = $iw;
3478   my $crop_h = $ih;
3479
3480   # The chance that we will randomly crop out a section of an image starts
3481   # out fairly low, but goes up for images that are very large, or images
3482   # that have ratios that make them look like banners (we try to avoid
3483   # banner images entirely, but they slip through when the IMG tags didn't
3484   # have WIDTH and HEIGHT specified.)
3485   #
3486   my $crop_chance = 0.2;
3487   if ($iw > $img_width * 0.4 || $ih > $img_height * 0.4) {
3488     $crop_chance += 0.2;
3489   }
3490   if ($iw > $img_width * 0.7 || $ih > $img_height * 0.7) {
3491     $crop_chance += 0.2;
3492   }
3493   if ($min_ratio && ($iw * $min_ratio) > $ih) {
3494     $crop_chance += 0.7;
3495   }
3496
3497   if ($crop_chance > 0.1) {
3498     LOG ($verbose_pbm, "crop chance: $crop_chance");
3499   }
3500
3501   if (rand() < $crop_chance) {
3502
3503     my $ow = $crop_w;
3504     my $oh = $crop_h;
3505
3506     if ($crop_w > $min_width) {
3507       # if it's a banner, select the width linearly.
3508       # otherwise, select a bell.
3509       my $r = (($min_ratio && ($iw * $min_ratio) > $ih)
3510                ? rand()
3511                : bellrand());
3512       $crop_w = $min_width + int ($r * ($crop_w - $min_width));
3513       $crop_x = int (rand() * ($ow - $crop_w));
3514     }
3515     if ($crop_h > $min_height) {
3516       # height always selects as a bell.
3517       $crop_h = $min_height + int (bellrand() * ($crop_h - $min_height));
3518       $crop_y = int (rand() * ($oh - $crop_h));
3519     }
3520
3521     if ($crop_x != 0   || $crop_y != 0 ||
3522         $crop_w != $iw || $crop_h != $ih) {
3523       LOG ($verbose_pbm,
3524            "randomly cropping to ${crop_w}x$crop_h \@ $crop_x,$crop_y");
3525     }
3526   }
3527
3528   # Where the image should logically land -- this might be negative.
3529   #
3530   my $x = int((rand() * ($img_width  + $crop_w/2)) - $crop_w*3/4);
3531   my $y = int((rand() * ($img_height + $crop_h/2)) - $crop_h*3/4);
3532
3533   # if we have chosen to paste the image outside of the rectangle of the
3534   # screen, then we need to crop it.
3535   #
3536   if ($x < 0 ||
3537       $y < 0 ||
3538       $x + $crop_w > $img_width ||
3539       $y + $crop_h > $img_height) {
3540
3541     LOG ($verbose_pbm,
3542          "cropping for effective paste of ${crop_w}x$crop_h \@ $x,$y");
3543
3544     if ($x < 0) { $crop_x -= $x; $crop_w += $x; $x = 0; }
3545     if ($y < 0) { $crop_y -= $y; $crop_h += $y; $y = 0; }
3546
3547     if ($x + $crop_w >= $img_width)  { $crop_w = $img_width  - $x - 1; }
3548     if ($y + $crop_h >= $img_height) { $crop_h = $img_height - $y - 1; }
3549   }
3550
3551   # If any cropping needs to happen, add pnmcut.
3552   #
3553   if ($crop_x != 0   || $crop_y != 0 ||
3554       $crop_w != $iw || $crop_h != $ih) {
3555     $iw = $crop_w;
3556     $ih = $crop_h;
3557     $cmd .= " | pnmcut $crop_x $crop_y $iw $ih";
3558     LOG ($verbose_pbm, "cropping to ${crop_w}x$crop_h \@ $crop_x,$crop_y");
3559   }
3560
3561   LOG ($verbose_pbm, "pasting ${iw}x$ih \@ $x,$y in $image_ppm");
3562
3563   $cmd .= " | pnmpaste - $x $y $image_ppm";
3564
3565   $cmd =~ s@^ *\| *@@;
3566
3567   if (defined ($webcollage_helper)) {
3568     $cmd = "$webcollage_helper $image_tmp1 $image_ppm " .
3569                               "$scale $opacity " .
3570                               "$crop_x $crop_y $x $y " .
3571                               "$iw $ih";
3572     $_ = $cmd;
3573
3574   } else {
3575     # use a PPM pipeline
3576     $_ = "($cmd)";
3577     $_ .= " < $image_tmp1 > $image_tmp2";
3578   }
3579
3580   if ($verbose_pbm) {
3581     $_ = "($_) 2>&1 | sed s'/^/" . blurb() . "/'";
3582   } else {
3583     $_ .= " 2> /dev/null";
3584   }
3585
3586   my $rc = nontrapping_system ($_);
3587
3588   if (defined ($webcollage_helper) && -z $image_ppm) {
3589     LOG (1, "failed command: \"$cmd\"");
3590     print STDERR "\naudit log:\n\n\n";
3591     print STDERR ("#" x 78) . "\n";
3592     print STDERR blurb() . "$image_ppm has zero size\n";
3593     showlog();
3594     print STDERR "\n\n";
3595     exit (1);
3596   }
3597
3598   if ($rc != 0) {
3599     LOG (($verbose_pbm || $verbose_load), "failed command: \"$cmd\"");
3600     LOG (($verbose_pbm || $verbose_load), "failed URL: \"$img\" (${ow}x$oh)");
3601     return;
3602   }
3603
3604   if (!defined ($webcollage_helper)) {
3605     rename ($image_tmp2, $image_ppm) || return;
3606   }
3607
3608   my $target = "$image_ppm";
3609
3610   # don't just tack this onto the end of the pipeline -- we don't want it
3611   # to end up in $image_ppm, because we don't want the results to be
3612   # cumulative.
3613   #
3614   if ($post_filter_cmd) {
3615
3616     my $cmd;
3617
3618     $target = $image_tmp1;
3619     if (!defined ($webcollage_helper)) {
3620       $cmd = "($post_filter_cmd) < $image_ppm > $target";
3621     } else {
3622       # Blah, my scripts need the JPEG data, but some other folks need
3623       # the PPM data -- what to do?  Ignore the problem, that's what!
3624 #     $cmd = "djpeg < $image_ppm | ($post_filter_cmd) > $target";
3625       $cmd = "($post_filter_cmd) < $image_ppm > $target";
3626     }
3627
3628     $rc = nontrapping_system ($cmd);
3629     if ($rc != 0) {
3630       LOG ($verbose_pbm, "filter failed: \"$post_filter_cmd\"\n");
3631       return;
3632     }
3633   }
3634
3635   if (!$no_output_p) {
3636     my $tsize = (stat($target))[7];
3637     if ($tsize > 200) {
3638       $cmd = "$ppm_to_root_window_cmd $target";
3639
3640       # xv seems to hate being killed.  it tends to forget to clean
3641       # up after itself, and leaves windows around and colors allocated.
3642       # I had this same problem with vidwhacker, and I'm not entirely
3643       # sure what I did to fix it.  But, let's try this: launch xv
3644       # in the background, so that killing this process doesn't kill it.
3645       # it will die of its own accord soon enough.  So this means we
3646       # start pumping bits to the root window in parallel with starting
3647       # the next network retrieval, which is probably a better thing
3648       # to do anyway.
3649       #
3650       $cmd .= " &" unless ($cocoa_p);
3651
3652       $rc = nontrapping_system ($cmd);
3653
3654       if ($rc != 0) {
3655         LOG (($verbose_pbm || $verbose_load), "display failed: \"$cmd\"");
3656         return;
3657       }
3658
3659     } else {
3660       LOG ($verbose_pbm, "$target size is $tsize");
3661     }
3662   }
3663
3664   $source .= "-" . stats_of($source);
3665   print STDOUT "image: ${iw}x${ih} @ $x,$y $base $source\n"
3666     if ($verbose_imgmap);
3667   if ($imagemap_base) {
3668     update_imagemap ($base, $x, $y, $iw, $ih,
3669                      $image_ppm, $img_width, $img_height);
3670   }
3671
3672   clearlog();
3673
3674   return 1;
3675 }
3676
3677
3678 sub update_imagemap($$$$$$$$) {
3679   my ($url, $x, $y, $w, $h, $image_ppm, $image_width, $image_height) = @_;
3680
3681   $current_state = "imagemap";
3682
3683   my $max_areas = 200;
3684
3685   $url = html_quote ($url);
3686   my $x2 = $x + $w;
3687   my $y2 = $y + $h;
3688   my $area = "<AREA SHAPE=RECT COORDS=\"$x,$y,$x2,$y2\" HREF=\"$url\">";
3689   unshift @imagemap_areas, $area;       # put one on the front
3690   if ($#imagemap_areas >= $max_areas) {
3691     pop @imagemap_areas;                # take one off the back.
3692   }
3693
3694   LOG ($verbose_pbm, "area: $x,$y,$x2,$y2 (${w}x$h)");
3695
3696   my $map_name = $imagemap_base;
3697   $map_name =~ s@^.*/@@;
3698   $map_name = 'collage' if ($map_name eq '');
3699
3700   my $imagemap_html = $imagemap_base . ".html";
3701   my $imagemap_jpg  = $imagemap_base . ".jpg";
3702   my $imagemap_jpg2 = $imagemap_jpg;
3703   $imagemap_jpg2 =~ s@^.*/@@gs;
3704
3705   if (!defined ($imagemap_html_tmp)) {
3706     $imagemap_html_tmp = $imagemap_html . sprintf (".%08x", rand(0xffffffff));
3707     $imagemap_jpg_tmp  = $imagemap_jpg  . sprintf (".%08x", rand(0xffffffff));
3708   }
3709
3710   # Read the imagemap html file (if any) to get a template.
3711   #
3712   my $template_html = '';
3713   {
3714     if (open (my $in, '<', $imagemap_html)) {
3715       local $/ = undef;  # read entire file
3716       $template_html = <$in>;
3717       close $in;
3718       LOG ($verbose_pbm, "read template $imagemap_html");
3719     }
3720
3721     if ($template_html =~ m/^\s*$/s) {
3722       $template_html = ("<MAP NAME=\"$map_name\"></MAP>\n" .
3723                         "<IMG SRC=\"$imagemap_jpg2\"" .
3724                         " USEMAP=\"$map_name\">\n");
3725       LOG ($verbose_pbm, "created dummy template");
3726     }
3727   }
3728
3729   # Write the jpg to a tmp file
3730   #
3731   {
3732     my $cmd;
3733     if (defined ($webcollage_helper)) {
3734       $cmd = "cp -p $image_ppm $imagemap_jpg_tmp";
3735     } else {
3736       $cmd = "cjpeg < $image_ppm > $imagemap_jpg_tmp";
3737     }
3738     my $rc = nontrapping_system ($cmd);
3739     if ($rc != 0) {
3740       error ("imagemap jpeg failed: \"$cmd\"\n");
3741     }
3742   }
3743
3744   # Write the html to a tmp file
3745   #
3746   {
3747     my $body = $template_html;
3748     my $areas = join ("\n\t", @imagemap_areas);
3749     my $map = ("<MAP NAME=\"$map_name\">\n\t$areas\n</MAP>");
3750     my $img = ("<IMG SRC=\"$imagemap_jpg2\" " .
3751                "BORDER=0 " .
3752                "WIDTH=$image_width HEIGHT=$image_height " .
3753                "USEMAP=\"#$map_name\">");
3754     $body =~ s@(<MAP\s+NAME=\"[^\"]*\"\s*>).*?(</MAP>)@$map@is;
3755     $body =~ s@<IMG\b[^<>]*\bUSEMAP\b[^<>]*>@$img@is;
3756
3757     # if there are magic webcollage spans in the html, update those too.
3758     #
3759     {
3760       my @st = stat ($imagemap_jpg_tmp);
3761       my $date = strftime("%d-%b-%Y %l:%M:%S %p %Z", localtime($st[9]));
3762       my $size = int(($st[7] / 1024) + 0.5) . "K";
3763       $body =~ s@(<SPAN\s+CLASS=\"webcollage_date\">).*?(</SPAN>)@$1$date$2@si;
3764       $body =~ s@(<SPAN\s+CLASS=\"webcollage_size\">).*?(</SPAN>)@$1$size$2@si;
3765     }
3766
3767     open (my $out, '>', $imagemap_html_tmp) || error ("$imagemap_html_tmp: $!");
3768     (print $out $body)                      || error ("$imagemap_html_tmp: $!");
3769     close ($out)                            || error ("$imagemap_html_tmp: $!");
3770     LOG ($verbose_pbm, "wrote $imagemap_html_tmp");
3771   }
3772
3773   # Rename the two tmp files to the real files
3774   #
3775   rename ($imagemap_html_tmp, $imagemap_html) ||
3776     error "renaming $imagemap_html_tmp to $imagemap_html";
3777   LOG ($verbose_pbm, "wrote $imagemap_html");
3778   rename ($imagemap_jpg_tmp,  $imagemap_jpg) ||
3779     error "renaming $imagemap_jpg_tmp to $imagemap_jpg";
3780   LOG ($verbose_pbm, "wrote $imagemap_jpg");
3781 }
3782
3783
3784 # Figure out what the proxy server should be, either from environment
3785 # variables or by parsing the output of the (MacOS) program "scutil",
3786 # which tells us what the system-wide proxy settings are.
3787 #
3788 sub set_proxy() {
3789
3790   if (! defined($http_proxy)) {
3791     # historical suckage: the environment variable name is lower case.
3792     $http_proxy = $ENV{http_proxy} || $ENV{HTTP_PROXY};
3793   }
3794
3795   if (defined ($http_proxy)) {
3796     if ($http_proxy && $http_proxy =~ m@^https?://([^/]*)/?$@ ) {
3797       # historical suckage: allow "http://host:port" as well as "host:port".
3798       $http_proxy = $1;
3799     }
3800
3801   } else {
3802     my $proxy_data = `scutil --proxy 2>/dev/null`;
3803     my ($server) = ($proxy_data =~ m/\bHTTPProxy\s*:\s*([^\s]+)/s);
3804     my ($port)   = ($proxy_data =~ m/\bHTTPPort\s*:\s*([^\s]+)/s);
3805     # Note: this ignores the "ExceptionsList".
3806     if ($server) {
3807       $http_proxy = $server;
3808       $http_proxy .= ":$port" if $port;
3809     }
3810   }
3811
3812   delete $ENV{http_proxy};
3813   delete $ENV{HTTP_PROXY};
3814   delete $ENV{https_proxy};
3815   delete $ENV{HTTPS_PROXY};
3816   delete $ENV{PERL_LWP_ENV_PROXY};
3817
3818   if ($http_proxy) {
3819     $http_proxy = 'http://' . $http_proxy;
3820     LOG ($verbose_net, "proxy server: $http_proxy");
3821   } else {
3822     $http_proxy = undef;  # for --proxy ''
3823   }
3824 }
3825
3826
3827 sub init_signals() {
3828
3829   $SIG{HUP}  = \&signal_cleanup;
3830   $SIG{INT}  = \&signal_cleanup;
3831   $SIG{QUIT} = \&signal_cleanup;
3832   $SIG{ABRT} = \&signal_cleanup;
3833   $SIG{KILL} = \&signal_cleanup;
3834   $SIG{TERM} = \&signal_cleanup;
3835
3836   # Need this so that if giftopnm dies, we don't die.
3837   $SIG{PIPE} = 'IGNORE';
3838 }
3839
3840 END { exit_cleanup(); }
3841
3842
3843 sub main() {
3844   $| = 1;
3845   srand(time ^ $$);
3846
3847   my $verbose = 0;
3848   my $dict;
3849   my $driftnet_cmd = 0;
3850
3851   $current_state = "init";
3852   $load_method = "none";
3853
3854   my $root_p = 0;
3855   my $window_id = undef;
3856
3857   while ($#ARGV >= 0) {
3858     $_ = shift @ARGV;
3859     if (m/^--?d(i(s(p(l(a(y)?)?)?)?)?)?$/s) {
3860       $ENV{DISPLAY} = shift @ARGV;
3861     } elsif (m/^--?root$/s) {
3862       $root_p = 1;
3863     } elsif (m/^--?window-id$/s) {
3864       $window_id = shift @ARGV;
3865       $root_p = 1;
3866     } elsif (m/^--?no-output$/s) {
3867       $no_output_p = 1;
3868     } elsif (m/^--?urls(-only)?$/s) {
3869       $urls_only_p = 1;
3870       $no_output_p = 1;
3871     } elsif (m/^--?cocoa$/s) {
3872       $cocoa_p = 1;
3873     } elsif (m/^--?imagemap$/s) {
3874       $imagemap_base = shift @ARGV;
3875       $no_output_p = 1;
3876     } elsif (m/^--?verbose$/s) {
3877       $verbose++;
3878     } elsif (m/^-v+$/) {
3879       $verbose += length($_)-1;
3880     } elsif (m/^--?delay$/s) {
3881       $delay = shift @ARGV;
3882     } elsif (m/^--?timeout$/s) {
3883       $http_timeout = shift @ARGV;
3884     } elsif (m/^--?filter$/s) {
3885       $filter_cmd = shift @ARGV;
3886     } elsif (m/^--?filter2$/s) {
3887       $post_filter_cmd = shift @ARGV;
3888     } elsif (m/^--?(background|bg)$/s) {
3889       $background = shift @ARGV;
3890     } elsif (m/^--?size$/s) {
3891       $_ = shift @ARGV;
3892       if (m@^(\d+)x(\d+)$@) {
3893         $img_width = $1;
3894         $img_height = $2;
3895       } else {
3896         error "argument to \"--size\" must be of the form \"640x400\"";
3897       }
3898     } elsif (m/^--?(http-)?proxy$/s) {
3899       $http_proxy = shift @ARGV;
3900     } elsif (m/^--?dict(ionary)?$/s) {
3901       $dict = shift @ARGV;
3902     } elsif (m/^--?opacity$/s) {
3903       $opacity = shift @ARGV;
3904       error ("opacity must be between 0.0 and 1.0")
3905         if ($opacity <= 0 || $opacity > 1);
3906     } elsif (m/^--?driftnet$/s) {
3907       @search_methods = ( 100, "driftnet", \&pick_from_driftnet );
3908       if (! ($ARGV[0] =~ m/^-/)) {
3909         $driftnet_cmd = shift @ARGV;
3910       } else {
3911         $driftnet_cmd = $default_driftnet_cmd;
3912       }
3913     } elsif (m/^--?dir(ectory)?$/s) {
3914       @search_methods = ( 100, "local", \&pick_from_local_dir );
3915       if (! ($ARGV[0] =~ m/^-/)) {
3916         $local_dir = shift @ARGV;
3917       } else {
3918         error ("local directory path must be set")
3919       }
3920     } elsif (m/^--?fps$/s) {
3921       # -fps only works on MacOS, via "webcollage-cocoa.m".
3922       # Ignore it if passed to this script in an X11 context.
3923     } elsif (m/^--?debug$/s) {
3924       my $which = shift @ARGV;
3925       my @rest = @search_methods;
3926       my $ok = 0;
3927       while (@rest) {
3928         my $pct  = shift @rest;
3929         my $name = shift @rest;
3930         my $tfn  = shift @rest;
3931
3932         if ($name eq $which) {
3933           @search_methods = (100, $name, $tfn);
3934           $ok = 1;
3935           last;
3936         }
3937       }
3938       error "no such search method as \"$which\"" unless ($ok);
3939       LOG (1, "DEBUG: using only \"$which\"");
3940       $report_performance_interval = 30;
3941
3942     } else {
3943       print STDERR "unknown option: $_\n\n";
3944       print STDERR "$copyright\nusage: $progname " .
3945               "[--root] [--display dpy] [--verbose] [--debug which]\n" .
3946         "\t\t  [--timeout secs] [--delay secs] [--size WxH]\n" .
3947         "\t\t  [--no-output] [--urls-only] [--imagemap filename]\n" .
3948         "\t\t  [--background color] [--opacity f]\n" .
3949         "\t\t  [--filter cmd] [--filter2 cmd]\n" .
3950         "\t\t  [--dictionary dictionary-file] [--http-proxy host[:port]]\n" .
3951         "\t\t  [--driftnet [driftnet-program-and-args]]\n" .
3952         "\t\t  [--directory local-image-directory]\n" .
3953         "\n";
3954       exit 1;
3955     }
3956   }
3957
3958   if (!$root_p && !$no_output_p && !$cocoa_p) {
3959     print STDERR $copyright;
3960     error "the --root argument is mandatory (for now.)";
3961   }
3962
3963   if (!$no_output_p && !$cocoa_p && !$ENV{DISPLAY}) {
3964     error "\$DISPLAY is not set.";
3965   }
3966
3967
3968   if ($verbose == 1) {
3969     $verbose_imgmap   = 1;
3970     $verbose_warnings = 1;
3971
3972   } elsif ($verbose == 2) {
3973     $verbose_imgmap   = 1;
3974     $verbose_warnings = 1;
3975     $verbose_load     = 1;
3976
3977   } elsif ($verbose == 3) {
3978     $verbose_imgmap   = 1;
3979     $verbose_warnings = 1;
3980     $verbose_load     = 1;
3981     $verbose_filter   = 1;
3982
3983   } elsif ($verbose == 4) {
3984     $verbose_imgmap   = 1;
3985     $verbose_warnings = 1;
3986     $verbose_load     = 1;
3987     $verbose_filter   = 1;
3988     $verbose_net      = 1;
3989
3990   } elsif ($verbose == 5) {
3991     $verbose_imgmap   = 1;
3992     $verbose_warnings = 1;
3993     $verbose_load     = 1;
3994     $verbose_filter   = 1;
3995     $verbose_net      = 1;
3996     $verbose_pbm      = 1;
3997
3998   } elsif ($verbose == 6) {
3999     $verbose_imgmap   = 1;
4000     $verbose_warnings = 1;
4001     $verbose_load     = 1;
4002     $verbose_filter   = 1;
4003     $verbose_net      = 1;
4004     $verbose_pbm      = 1;
4005     $verbose_http     = 1;
4006
4007   } elsif ($verbose >= 7) {
4008     $verbose_imgmap   = 1;
4009     $verbose_warnings = 1;
4010     $verbose_load     = 1;
4011     $verbose_filter   = 1;
4012     $verbose_net      = 1;
4013     $verbose_pbm      = 1;
4014     $verbose_http     = 1;
4015     $verbose_exec     = 1;
4016   }
4017
4018   if ($dict) {
4019     error ("$dict does not exist") unless (-f $dict);
4020     $wordlist = $dict;
4021   } else {
4022     pick_dictionary();
4023   }
4024
4025   if ($imagemap_base && !($img_width && $img_height)) {
4026     error ("--size WxH is required with --imagemap");
4027   }
4028
4029   if (defined ($local_dir)) {
4030     $_ = "xscreensaver-getimage-file";
4031     which ($_) || error "$_ not found on \$PATH.";
4032   }
4033
4034   init_signals();
4035   set_proxy();
4036
4037   spawn_driftnet ($driftnet_cmd) if ($driftnet_cmd);
4038
4039   if ($urls_only_p) {
4040     url_only_output ();
4041   } else {
4042     x_or_pbm_output ($window_id);
4043   }
4044 }
4045
4046 main();
4047 exit (0);