ftp://ftp.krokus.ru/pub/OpenBSD/distfiles/xscreensaver-4.22.tar.gz
[xscreensaver] / driver / xscreensaver-text
1 #!/usr/bin/perl -w
2 # Copyright © 2005 Jamie Zawinski <jwz@jwz.org>
3 #
4 # Permission to use, copy, modify, distribute, and sell this software and its
5 # documentation for any purpose is hereby granted without fee, provided that
6 # the above copyright notice appear in all copies and that both that
7 # copyright notice and this permission notice appear in supporting
8 # documentation.  No representations are made about the suitability of this
9 # software for any purpose.  It is provided "as is" without express or 
10 # implied warranty.
11 #
12 # This program writes some text to stdout, based on preferences in the
13 # .xscreensaver file.  It may load a file, a URL, run a program, or just
14 # print the date.
15 #
16 # Created: 19-Mar-2005.
17
18 require 5;
19 use diagnostics;
20 use strict;
21 use Socket;
22 use POSIX qw(strftime);
23 use Text::Wrap qw(wrap);
24 use bytes;
25
26 my $progname = $0; $progname =~ s@.*/@@g;
27 my $version = q{ $Revision: 1.5 $ }; $version =~ s/^[^0-9]+([0-9.]+).*$/$1/;
28
29 my $verbose = 0;
30 my $http_proxy = undef;
31
32 my $config_file = $ENV{HOME} . "/.xscreensaver";
33 my $text_mode     = 'date';
34 my $text_literal  = '';
35 my $text_file     = '';
36 my $text_program  = '';
37 my $text_url      = '';
38
39 my $wrap_columns  = undef;
40
41
42 # Maps HTML character entities to the corresponding Latin1 characters.
43 #
44 my %entity_table = (
45    "quot"   => '"', "amp"    => '&', "lt"     => '<', "gt"     => '>',
46    "nbsp"   => ' ', "iexcl"  => '¡', "cent"   => '¢', "pound"  => '£',
47    "curren" => '¤', "yen"    => '¥', "brvbar" => '¦', "sect"   => '§',
48    "uml"    => '¨', "copy"   => '©', "ordf"   => 'ª', "laquo"  => '«',
49    "not"    => '¬', "shy"    => '­', "reg"    => '®', "macr"   => '¯',
50    "deg"    => '°', "plusmn" => '±', "sup2"   => '²', "sup3"   => '³',
51    "acute"  => '´', "micro"  => 'µ', "para"   => '¶', "middot" => '·',
52    "cedil"  => '¸', "sup1"   => '¹', "ordm"   => 'º', "raquo"  => '»',
53    "frac14" => '¼', "frac12" => '½', "frac34" => '¾', "iquest" => '¿',
54    "Agrave" => 'À', "Aacute" => 'Á', "Acirc"  => 'Â', "Atilde" => 'Ã',
55    "Auml"   => 'Ä', "Aring"  => 'Å', "AElig"  => 'Æ', "Ccedil" => 'Ç',
56    "Egrave" => 'È', "Eacute" => 'É', "Ecirc"  => 'Ê', "Euml"   => 'Ë',
57    "Igrave" => 'Ì', "Iacute" => 'Í', "Icirc"  => 'Î', "Iuml"   => 'Ï',
58    "ETH"    => 'Ð', "Ntilde" => 'Ñ', "Ograve" => 'Ò', "Oacute" => 'Ó',
59    "Ocirc"  => 'Ô', "Otilde" => 'Õ', "Ouml"   => 'Ö', "times"  => '×',
60    "Oslash" => 'Ø', "Ugrave" => 'Ù', "Uacute" => 'Ú', "Ucirc"  => 'Û',
61    "Uuml"   => 'Ü', "Yacute" => 'Ý', "THORN"  => 'Þ', "szlig"  => 'ß',
62    "agrave" => 'à', "aacute" => 'á', "acirc"  => 'â', "atilde" => 'ã',
63    "auml"   => 'ä', "aring"  => 'å', "aelig"  => 'æ', "ccedil" => 'ç',
64    "egrave" => 'è', "eacute" => 'é', "ecirc"  => 'ê', "euml"   => 'ë',
65    "igrave" => 'ì', "iacute" => 'í', "icirc"  => 'î', "iuml"   => 'ï',
66    "eth"    => 'ð', "ntilde" => 'ñ', "ograve" => 'ò', "oacute" => 'ó',
67    "ocirc"  => 'ô', "otilde" => 'õ', "ouml"   => 'ö', "divide" => '÷',
68    "oslash" => 'ø', "ugrave" => 'ù', "uacute" => 'ú', "ucirc"  => 'û',
69    "uuml"   => 'ü', "yacute" => 'ý', "thorn"  => 'þ', "yuml"   => 'ÿ',
70    "apos"   => '\''
71 );
72
73 # Maps certain UTF8 characters (2 or 3 bytes) to the corresponding
74 # Latin1 characters.
75 #
76 my %unicode_latin1_table = (
77    "\xC2\xA1" => '¡', "\xC2\xA2" => '¢', "\xC2\xA3" => '£', "\xC2\xA4" => '¤',
78    "\xC2\xA5" => '¥', "\xC2\xA6" => '¦', "\xC2\xA7" => '§', "\xC2\xA8" => '¨',
79    "\xC2\xA9" => '©', "\xC2\xAA" => 'ª', "\xC2\xAB" => '«', "\xC2\xAC" => '¬',
80    "\xC2\xAD" => '­', "\xC2\xAE" => '®', "\xC2\xAF" => '¯', "\xC2\xB0" => '°',
81    "\xC2\xB1" => '±', "\xC2\xB2" => '²', "\xC2\xB3" => '³', "\xC2\xB4" => '´',
82    "\xC2\xB5" => 'µ', "\xC2\xB6" => '¶', "\xC2\xB7" => '·', "\xC2\xB8" => '¸',
83    "\xC2\xB9" => '¹', "\xC2\xBA" => 'º', "\xC2\xBB" => '»', "\xC2\xBC" => '¼',
84    "\xC2\xBD" => '½', "\xC2\xBE" => '¾', "\xC2\xBF" => '¿', "\xC3\x80" => 'À',
85    "\xC3\x81" => 'Á', "\xC3\x82" => 'Â', "\xC3\x83" => 'Ã', "\xC3\x84" => 'Ä',
86    "\xC3\x85" => 'Å', "\xC3\x86" => 'Æ', "\xC3\x87" => 'Ç', "\xC3\x88" => 'È',
87    "\xC3\x89" => 'É', "\xC3\x8A" => 'Ê', "\xC3\x8B" => 'Ë', "\xC3\x8C" => 'Ì',
88    "\xC3\x8D" => 'Í', "\xC3\x8E" => 'Î', "\xC3\x8F" => 'Ï', "\xC3\x90" => 'Ð',
89    "\xC3\x91" => 'Ñ', "\xC3\x92" => 'Ò', "\xC3\x93" => 'Ó', "\xC3\x94" => 'Ô',
90    "\xC3\x95" => 'Õ', "\xC3\x96" => 'Ö', "\xC3\x97" => '×', "\xC3\x98" => 'Ø',
91    "\xC3\x99" => 'Ù', "\xC3\x9A" => 'Ú', "\xC3\x9B" => 'Û', "\xC3\x9C" => 'Ü',
92    "\xC3\x9D" => 'Ý', "\xC3\x9E" => 'Þ', "\xC3\x9F" => 'ß', "\xC3\xA0" => 'à',
93    "\xC3\xA1" => 'á', "\xC3\xA2" => 'â', "\xC3\xA3" => 'ã', "\xC3\xA4" => 'ä',
94    "\xC3\xA5" => 'å', "\xC3\xA6" => 'æ', "\xC3\xA7" => 'ç', "\xC3\xA8" => 'è',
95    "\xC3\xA9" => 'é', "\xC3\xAA" => 'ê', "\xC3\xAB" => 'ë', "\xC3\xAC" => 'ì',
96    "\xC3\xAD" => 'í', "\xC3\xAE" => 'î', "\xC3\xAF" => 'ï', "\xC3\xB0" => 'ð',
97    "\xC3\xB1" => 'ñ', "\xC3\xB2" => 'ò', "\xC3\xB3" => 'ó', "\xC3\xB4" => 'ô',
98    "\xC3\xB5" => 'õ', "\xC3\xB6" => 'ö', "\xC3\xB7" => '÷', "\xC3\xB8" => 'ø',
99    "\xC3\xB9" => 'ù', "\xC3\xBA" => 'ú', "\xC3\xBB" => 'û', "\xC3\xBC" => 'ü',
100    "\xC3\xBD" => 'ý', "\xC3\xBE" => 'þ', "\xC3\xBF" => 'ÿ',
101
102    "\xE2\x80\x93" => '--',  "\xE2\x80\x94" => '--',
103    "\xE2\x80\x98" => '`',   "\xE2\x80\x99" => '\'',
104    "\xE2\x80\x9C" => "``",  "\xE2\x80\x9D" => "''",
105    "\xE2\x80\xA6" => '...',
106 );
107
108
109 # Convert any HTML entities to Latin1 characters.
110 #
111 sub de_entify($) {
112   my ($text) = @_;
113   $text =~ s/(&(\#)?([[:alpha:]\d]+);?)/
114     {
115      my $c;
116      if ($2) {
117        $c = chr($3);  # the &#number is always decimal, right?
118      } else {
119        $c = $entity_table{$3};
120      }
121 #    print STDERR "$progname: warning: unknown HTML character entity \"$1\"\n"
122 #     unless $c;
123      ($c ? $c : "[$3]");
124     }
125    /gexi;
126   return $text;
127 }
128
129
130 # Convert any Unicode characters to Latin1 if possible.
131 # Unconvertable bytes are left alone.
132 #
133 sub de_unicoddle($) {
134   my ($text) = @_;
135   foreach my $key (keys (%unicode_latin1_table)) {
136     my $val = $unicode_latin1_table{$key};
137     $text =~ s/$key/$val/gs;
138   }
139   return $text;
140 }
141
142
143 # Reads the prefs we use from ~/.xscreensaver
144 #
145 sub get_prefs() {
146
147   my $got_any_p = 0;
148   local *IN;
149
150   if (open (IN, "<$config_file")) {
151     print STDERR "$progname: reading $config_file\n" if ($verbose > 1);
152     my $body = '';
153     while (<IN>) { $body .= $_; }
154     close IN;
155     $got_any_p = get_prefs_1 ($body);
156
157   } elsif ($verbose > 1) {
158     print STDERR "$progname: $config_file: $!\n";
159   }
160
161   if (! $got_any_p) {
162     # We weren't able to read settings from the .xscreensaver file.
163     # Fall back to any settings in the X resource database
164     # (/usr/X11R6/lib/X11/app-defaults/XScreenSaver)
165     #
166     print STDERR "$progname: reading X resources\n" if ($verbose > 1);
167     my $body = `appres XScreenSaver xscreensaver -1`;
168     $got_any_p = get_prefs_1 ($body);
169   }
170
171   if ($verbose > 1) {
172     printf STDERR "$progname: mode:    $text_mode\n";
173     printf STDERR "$progname: literal: $text_literal\n";
174     printf STDERR "$progname: file:    $text_file\n";
175     printf STDERR "$progname: program: $text_program\n";
176     printf STDERR "$progname: url:     $text_url\n";
177   }
178
179   $text_mode =~ tr/A-Z/a-z/;
180   $text_literal =~ s@\\n@\n@gs;
181 }
182
183
184 sub get_prefs_1($) {
185   my ($body) = @_;
186
187   my $got_any_p = 0;
188   $body =~ s@\\\n@@gs;
189
190   if ($body =~ m/^[.*]*textMode:[ \t]*([^\s]+)\s*$/im) {
191     $text_mode = $1;
192     $got_any_p = 1;
193   }
194   if ($body =~ m/^[.*]*textLiteral:[ \t]*(.*?)[ \t]*$/im) {
195     $text_literal = $1;
196   }
197   if ($body =~ m/^[.*]*textFile:[ \t]*(.*?)[ \t]*$/im) {
198     $text_file = $1;
199   }
200   if ($body =~ m/^[.*]*textProgram:[ \t]*(.*?)[ \t]*$/im) {
201     $text_program = $1;
202   }
203   if ($body =~ m/^[.*]*textURL:[ \t]*(.*?)[ \t]*$/im) {
204     $text_url = $1;
205   }
206
207   return $got_any_p;
208 }
209
210
211 # like system() but checks errors.
212 #
213 sub safe_system(@) {
214   my (@cmd) = @_;
215
216   print STDERR "$progname: executing " . join(' ', @cmd) . "\n"
217     if ($verbose > 3);
218
219   system @cmd;
220   my $exit_value  = $? >> 8;
221   my $signal_num  = $? & 127;
222   my $dumped_core = $? & 128;
223   error ("$cmd[0]: core dumped!") if ($dumped_core);
224   error ("$cmd[0]: signal $signal_num!") if ($signal_num);
225   error ("$cmd[0]: exited with $exit_value!") if ($exit_value);
226 }
227
228
229 sub which($) {
230   my ($cmd) = @_;
231
232   if ($cmd =~ m@^\./|^/@) {
233     error ("cannot execute $cmd") unless (-x $cmd);
234     return $cmd;
235   }
236  
237  foreach my $dir (split (/:/, $ENV{PATH})) {
238     my $cmd2 = "$dir/$cmd";
239     print STDERR "$progname:   checking $cmd2\n" if ($verbose > 3);
240     return $cmd2 if (-x "$cmd2");
241   }
242   error ("$cmd not found on \$PATH");
243 }
244
245
246 sub output() {
247
248   # Do some basic sanity checking (null text, null file names, etc.)
249   #
250   if (($text_mode eq 'literal' && $text_literal =~ m/^\s*$/i) ||
251       ($text_mode eq 'file'    && $text_file    =~ m/^\s*$/i) ||
252       ($text_mode eq 'program' && $text_program =~ m/^\s*$/i) ||
253       ($text_mode eq 'url'     && $text_url     =~ m/^\s*$/i)) {
254     print STDERR "$progname: falling back to 'date'\n" if ($verbose);
255     $text_mode = 'date';
256   }
257
258   if ($text_mode eq 'literal') {
259     $text_literal = strftime ($text_literal, localtime);
260     print STDOUT $text_literal;
261     print STDOUT "\n" unless ($text_literal =~ m/\n$/s);
262
263   } elsif ($text_mode eq 'file') {
264
265     local *IN;
266     if (open (IN, "<$text_file")) {
267       print STDERR "$progname: reading $text_file\n" if ($verbose);
268
269       if ($wrap_columns && $wrap_columns > 0) {
270         # read it, then reformat it.
271         my $body = '';
272         while (<IN>) { $body .= $_; }
273         reformat_text ($body);
274       } else {
275         # stream it
276         while (<IN>) { print $_; }
277       }
278       close IN;
279     } else {
280       error ("$text_file: $!");
281     }
282
283   } elsif ($text_mode eq 'program') {
284
285     $text_program = which ($text_program);
286     print STDERR "$progname: running $text_program\n" if ($verbose);
287
288     if ($wrap_columns && $wrap_columns > 0) {
289       # read it, then reformat it.
290       my $body = `( $text_program ) 2>&1`;
291       reformat_text ($body);
292     } else {
293       # stream it
294       safe_system ("$text_program");
295     }
296
297   } elsif ($text_mode eq 'url') {
298
299     get_url_text ($text_url);
300
301   } else { # $text_mode eq 'date'
302
303     safe_system ("uname", "-n");
304     if (-f "/etc/redhat-release") { system ("cat", "/etc/redhat-release"); }
305     safe_system ("uname", "-sr");
306     print "\n";
307     safe_system ("date", "+%c");
308     print "\n";
309     my $ut = `uptime`;
310     $ut =~ s/^[ \d:]*//;
311     $ut =~ s/,\s*(load)/\n$1/;
312     print "$ut\n";
313   }
314
315 }
316
317
318 # Loads the given URL, returns: $http, $head, $body.
319 #
320 sub get_url_1($;$) {
321   my ($url, $referer) = @_;
322   
323   if (! ($url =~ m@^http://@i)) {
324     error ("not an HTTP URL: $url");
325   }
326
327   my ($url_proto, $dummy, $serverstring, $path) = split(/\//, $url, 4);
328   $path = "" unless $path;
329
330   my ($them,$port) = split(/:/, $serverstring);
331   $port = 80 unless $port;
332
333   my $them2 = $them;
334   my $port2 = $port;
335   if ($http_proxy) {
336     $serverstring = $http_proxy if $http_proxy;
337     $serverstring =~ s@^[a-z]+://@@;
338     ($them2,$port2) = split(/:/, $serverstring);
339     $port2 = 80 unless $port2;
340   }
341
342   my ($remote, $iaddr, $paddr, $proto, $line);
343   $remote = $them2;
344   if ($port2 =~ /\D/) { $port2 = getservbyname($port2, 'tcp') }
345   if (!$port2) {
346     error ("unrecognised port in $url");
347   }
348
349   $iaddr = inet_aton($remote);
350   error ("host not found: $remote") unless ($iaddr);
351
352   $paddr   = sockaddr_in($port2, $iaddr);
353
354
355   my $head = "";
356   my $body = "";
357
358   $proto   = getprotobyname('tcp');
359   if (!socket(S, PF_INET, SOCK_STREAM, $proto)) {
360     error ("socket: $!");
361   }
362   if (!connect(S, $paddr)) {
363     error ("connect($serverstring): $!");
364   }
365
366   select(S); $| = 1; select(STDOUT);
367
368   my $user_agent = "$progname/$version";
369
370   my $hdrs = ("GET " . ($http_proxy ? $url : "/$path") . " HTTP/1.0\r\n" .
371               "Host: $them\r\n" .
372               "User-Agent: $user_agent\r\n");
373   if ($referer) {
374     $hdrs .= "Referer: $referer\r\n";
375   }
376   $hdrs .= "\r\n";
377
378   if ($verbose > 3) {
379     foreach (split('\r?\n', $hdrs)) {
380       print STDERR "  ==> $_\n";
381     }
382   }
383   print S $hdrs;
384   my $http = <S> || "";
385
386   $_  = $http;
387   s/[\r\n]+$//s;
388   print STDERR "  <== $_\n" if ($verbose > 3);
389
390   while (<S>) {
391     $head .= $_;
392     s/[\r\n]+$//s;
393     last if m@^$@;
394     print STDERR "  <== $_\n" if ($verbose > 3);
395   }
396
397   print STDERR "  <== \n" if ($verbose > 4);
398   my $lines = 0;
399   while (<S>) {
400     s/\r\n/\n/gs;
401     print STDERR "  <== $_" if ($verbose > 4);
402     $body .= $_;
403     $lines++;
404   }
405
406   print STDERR "  <== [ body ]: $lines lines, " . length($body) . " bytes\n"
407     if ($verbose == 4);
408
409   close S;
410
411   if (!$http) {
412     error ("null response: $url");
413   }
414
415   return ( $http, $head, $body );
416 }
417
418
419 # Loads the given URL, processes redirects, returns (content-type, body).
420 #
421 sub get_url($;$) {
422   my ($url, $referer) = @_;
423
424   print STDERR "$progname: loading $url\n" if ($verbose > 2);
425
426   my $orig_url = $url;
427   my $loop_count = 0;
428   my $max_loop_count = 10;
429
430   do {
431     my ( $http, $head, $body ) = get_url_1 ($url, $referer);
432
433     $http =~ s/[\r\n]+$//s;
434
435     if ( $http =~ m@^HTTP/[0-9.]+ 30[123]@ ) {
436       $_ = $head;
437
438       my ( $location ) = m@^location:[ \t]*(.*)$@im;
439       if ( $location ) {
440         $location =~ s/[\r\n]$//;
441
442         print STDERR "$progname: redirect from $url to $location\n"
443           if ($verbose > 3);
444
445         $referer = $url;
446         $url = $location;
447
448         if ($url =~ m@^/@) {
449           $referer =~ m@^(http://[^/]+)@i;
450           $url = $1 . $url;
451         } elsif (! ($url =~ m@^[a-z]+:@i)) {
452           $_ = $referer;
453           s@[^/]+$@@g if m@^http://[^/]+/@i;
454           $_ .= "/" if m@^http://[^/]+$@i;
455           $url = $_ . $url;
456         }
457
458       } else {
459         error ("no Location with \"$http\"");
460       }
461
462       if ($loop_count++ > $max_loop_count) {
463         error ("too many redirects ($max_loop_count) from $orig_url");
464       }
465
466     } elsif ( $http =~ m@^HTTP/[0-9.]+ ([4-9][0-9][0-9].*)$@ ) {
467       error ("failed: $1 ($url)");
468
469     } else {
470       my $ct = 'text/plain';
471       $ct = $1 if ($head =~ m/^content-type:\s*([^\s]+)/mi);
472       return ($ct, $body);
473     }
474   } while (1);
475 }
476
477
478 # Make an educated guess as to what's in this document.
479 # We don't necessarily take the Content-Type header at face value.
480 # Returns 'html', 'rss', or 'text';
481 #
482 sub guess_content_type($$) {
483   my ($ct, $body) = @_;
484
485   $body =~ s/^(.{512}).*/$1/s;  # only look in first half K of file
486
487   if ($ct =~ m@^text/.*html@i)          { return 'html'; }
488   if ($ct =~ m@\b(atom|rss|xml)\b@i)    { return 'rss';  }
489
490   if ($body =~ m@^\s*<\?xml@is)         { return 'rss';  }
491   if ($body =~ m@^\s*<!DOCTYPE RSS@is)  { return 'rss';  }
492   if ($body =~ m@^\s*<!DOCTYPE HTML@is) { return 'html'; }
493
494   if ($body =~ m@<(BASE|HTML|HEAD|BODY|SCRIPT|STYLE|TABLE|A\s+HREF)\b@i) {
495     return 'html';
496   }
497
498   if ($body =~ m@<(RSS|CHANNEL|GENERATOR|DESCRIPTION|CONTENT|FEED|ENTRY)\b@i) {
499     return 'rss';
500   }
501
502   return 'text';
503 }
504
505 sub reformat_html($$) {
506   my ($body, $rss_p) = @_;
507   $_ = $body;
508
509   if (! $rss_p) {
510     # In HTML, unfold lines (this breaks PRE.  Sue me.)
511     # In RSS, assume \n means literal line break.
512     s@[\r\n]@ @gsi;
513   }
514
515   s@<!--.*?-->@@gsi;                             # lose comments
516   s@<(STYLE|SCRIPT)\b[^<>]*>.*?</\1\s*>@@gsi;    # lose css and js
517
518   s@</?(BR|TR|TD|LI|DIV)\b[^<>]*>@\n@gsi; # line break at BR, TD, DIV, etc
519   s@</?(P|UL|OL|BLOCKQUOTE)\b[^<>]*>@\n\n@gsi; # two line breaks
520
521   s@<lj\s+user=\"?([^<>\"]+)\"?[^<>]*>?@$1@gsi;  # handle <LJ USER=>
522   s@</?[BI]>@*@gsi;                              # bold, italic => asterisks
523
524
525   s@<[^<>]*>?@@gs;                # lose all other HTML tags
526   $_ = de_entify ($_);            # convert HTML entities
527
528   # elide any remaining non-Latin1 binary data...
529   s/([\177-\377]+(\s*[\177-\377]+)[^a-z\d]*)/«...» /g;
530   #s/([\177-\377]+(\s*[\177-\377]+)[^a-z\d]*)/«$1» /g;
531
532   $_ .= "\n";
533
534   s/[ \t]+$//gm;                  # lose whitespace at end of line
535   s@\n\n\n+@\n\n@gs;              # compress blank lines
536
537   if (!defined($wrap_columns) || $wrap_columns > 0) {
538     $Text::Wrap::columns = ($wrap_columns || 72);
539     $_ = wrap ("", "  ", $_);     # wrap the lines as a paragraph
540     s/[ \t]+$//gm;                # lose whitespace at end of line again
541   }
542
543   print STDOUT $_;
544 }
545
546
547 sub reformat_rss($) {
548   my ($body) = @_;
549
550   $body =~ s/(<(ITEM|ENTRY)\b)/\001\001$1/gsi;
551   my @items = split (/\001\001/, $body);
552
553   print STDERR "$progname: converting RSS ($#items items)...\n"
554     if ($verbose > 2);
555
556   shift @items;
557
558   # Let's skip forward in the stream by a random amount, so that if
559   # two copies of ljlatest are running at the same time (e.g., on a
560   # multi-headed machine), they get different text.  (Put the items
561   # that we take off the front back on the back.)
562   #
563   if ($#items > 7) {
564     my $n = int (rand ($#items - 5));
565     print STDERR "$progname: rotating by $n items...\n" if ($verbose > 2);
566     while ($n-- > 0) {
567       push @items, (shift @items);
568     }
569   }
570
571   my $i = -1;
572   foreach (@items) {
573     $i++;
574
575     my ($title, $body1, $body2, $body3);
576     
577     $title = $3 if (m@<((TITLE)       [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
578     $body1 = $3 if (m@<((DESCRIPTION) [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
579     $body2 = $3 if (m@<((CONTENT)     [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
580     $body3 = $3 if (m@<((SUMMARY)     [^<>\s]*)[^<>]*>\s*(.*?)\s*</\1>@xsi);
581
582     # If there are both <description> and <content> or <content:encoded>,
583     # use whichever one contains more text.
584     #
585     if ($body3 && length($body3) >= length($body2 || '')) {
586       $body2 = $body3;
587     }
588     if ($body2 && length($body2) >= length($body1 || '')) {
589       $body1 = $body2;
590     }
591
592     if (! $body1) {
593       if ($title) {
594         print STDERR "$progname: no body in item $i (\"$title\")\n"
595           if ($verbose > 2);
596       } else {
597         print STDERR "$progname: no body or title in item $i\n"
598           if ($verbose > 2);
599         next;
600       }
601     }
602
603     $title = rss_field_to_html ($title || '');
604     $body1 = rss_field_to_html ($body1 || '');
605
606     reformat_html ("$title<P>$body1", 1);
607     print "\n";
608   }
609 }
610
611
612 sub rss_field_to_html($) {
613   my ($body) = @_;
614
615   # Assume that if <![CDATA[...]]> is present, everything inside that.
616   #
617   if ($body =~ m/^\s*<!\[CDATA\[(.*?)\]\s*\]/is) {
618     $body = $1;
619   } else {
620     $body = de_entify ($body);      # convert entities to get HTML from XML
621   }
622
623   $body = de_unicoddle ($body);     # convert UTF8 to Latin1
624   return $body;
625 }
626
627
628 sub reformat_text($) {
629   my ($body) = @_;
630
631   # only re-wrap if --cols was specified.  Otherwise, dump it as is.
632   #
633   if ($wrap_columns && $wrap_columns > 0) {
634     print STDERR "$progname: wrapping at $wrap_columns...\n" if ($verbose > 2);
635     $Text::Wrap::columns = $wrap_columns;
636     $body = wrap ("", "", $body);
637     $body =~ s/[ \t]+$//gm;
638   }
639
640   print STDOUT $body;
641 }
642
643
644 sub get_url_text($) {
645   my ($url) = @_;
646
647   # historical suckage: the environment variable name is lower case.
648   $http_proxy = $ENV{http_proxy} || $ENV{HTTP_PROXY};
649
650   if ($http_proxy && $http_proxy =~ m@^http://([^/]*)/?$@ ) {
651     # historical suckage: allow "http://host:port" as well as "host:port".
652     $http_proxy = $1;
653   }
654
655   my ($ct, $body) = get_url ($url);
656
657   $ct = guess_content_type ($ct, $body);
658   if ($ct eq 'html') {
659     print STDERR "$progname: converting HTML...\n" if ($verbose > 2);
660     reformat_html ($body, 0);
661   } elsif ($ct eq 'rss')  {
662     reformat_rss ($body);
663   } else {
664     print STDERR "$progname: plain text...\n" if ($verbose > 2);
665     reformat_text ($body);
666   }
667 }
668
669
670
671 sub error($) {
672   my ($err) = @_;
673   print STDERR "$progname: $err\n";
674   exit 1;
675 }
676
677 sub usage() {
678   print STDERR "usage: $progname [ --options ... ]\n" .
679    ("\n" .
680     "       Prints out some text for use by various screensavers,\n" .
681     "       according to the options in the ~/.xscreensaver file.\n" .
682     "       This may dump the contents of a file, run a program,\n" .
683     "       or load a URL.\n".
684     "\n" .
685     "   Options:\n" .
686     "\n" .
687     "       --date           Print the host name and current time.\n" .
688     "\n" .
689     "       --text STRING    Print out the given text.  It may contain %\n" .
690     "                        escape sequences as per strftime(2).\n" .
691     "\n" .
692     "       --file PATH      Print the contents of the given file.\n" .
693     "                        If --cols is specified, re-wrap the lines;\n" .
694     "                        otherwise, print them as-is.\n" .
695     "\n" .
696     "       --program CMD    Run the given program and print its output.\n" .
697     "                        If --cols is specified, re-wrap the output.\n" .
698     "\n" .
699     "       --url HTTP-URL   Download and print the contents of the HTTP\n" .
700     "                        document.  If it contains HTML, RSS, or Atom,\n" .
701     "                        it will be converted to plain-text.\n" .
702     "\n" .
703     "       --cols N         Wrap lines at this column.  Default 72.\n" .
704     "\n");
705   exit 1;
706 }
707
708 sub main() {
709
710   my $load_p = 1;
711
712   while ($#ARGV >= 0) {
713     $_ = shift @ARGV;
714     if ($_ eq "--verbose") { $verbose++; }
715     elsif (m/^-v+$/) { $verbose += length($_)-1; }
716     elsif (m/^--?date$/)    { $text_mode = 'date';
717                               $load_p = 0; }
718     elsif (m/^--?text$/)    { $text_mode = 'literal';
719                               $text_literal = shift @ARGV;
720                               $load_p = 0; }
721     elsif (m/^--?file$/)    { $text_mode = 'file';
722                               $text_file = shift @ARGV;
723                               $load_p = 0; }
724     elsif (m/^--?program$/) { $text_mode = 'program';
725                               $text_program = shift @ARGV;
726                               $load_p = 0; }
727     elsif (m/^--?url$/)     { $text_mode = 'url';
728                               $text_url = shift @ARGV;
729                               $load_p = 0; }
730     elsif (m/^--?col(umn)?s?$/) { $wrap_columns = 0 + shift @ARGV; }
731     elsif (m/^-./) { usage; }
732     else { usage; }
733   }
734
735   get_prefs() if ($load_p);
736   output();
737 }
738
739 main();
740 exit 0;