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