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