upgrade-config.pl
上传用户:seven77cht
上传日期:2007-01-04
资源大小:486k
文件大小:17k
源码类别:

浏览器

开发平台:

Unix_Linux

  1. #!/bin/sh
  2. #
  3. # WWWOFFLE - World Wide Web Offline Explorer - Version 2.5b.
  4. #
  5. # A Perl script to update the configuration file to version 2.5 standard.
  6. #
  7. # Written by Andrew M. Bishop
  8. #
  9. # This file Copyright 1998,99 Andrew M. Bishop
  10. # It may be distributed under the GNU Public License, version 2, or
  11. # any higher version.  See section COPYING of the GNU Public license
  12. # for conditions under which this file may be redistributed.
  13. #
  14. exec perl -x $0 $1
  15. exit 1
  16. #!perl
  17. $#ARGV==0 || die "Usage: $0 wwwoffle.confn";
  18. $conf=$ARGV[0];
  19. #
  20. %new_MIMETypes=(".js"     ,  "application/x-javascript",
  21.                 ".htm"    ,  "text/html",
  22.                 ".pac"    ,  "application/x-ns-proxy-autoconfig",
  23.                 ".png"    ,  "image/png",
  24.                 ".class"  ,  "application/java",
  25.                 ".wrl"    ,  "model/vrml",
  26.                 ".vr"     ,  "model/vrml",
  27.                 ".css"    ,  "text/css",
  28.                 ".xml"    ,  "application/xml",
  29.                 ".dtd"    ,  "application/xml");
  30. #
  31. if(open(INST,"<wwwoffle.conf.install") || open(INST,"<$conf.install"))
  32.   {
  33.    $section=$comment='';
  34.    while(<INST>)
  35.        {
  36.         if(!$section && (/^[ t]*[#{]/ || /^[ trn]*$/))
  37.              {
  38.               $comment.=$_;
  39.               next;
  40.              }
  41.        if(/^[ t]*}/)
  42.             {
  43.              $section=$comment='';
  44.              next;
  45.             }
  46.         if(!$section && /^[ t]*([a-zA-Z]+)[ trn]*$/)
  47.             {
  48.              $section=$1;
  49.              $comment{$section}=$comment;
  50.              $comment='';
  51.             }
  52.        }
  53.    close(INST);
  54.   }
  55. else
  56.   {
  57.    print "Cannot open wwwoffle.conf.install or $conf.install - no new format commentsn";
  58.   }
  59. $comment{"TAIL"}=$comment;
  60. #
  61. system "mv $conf $conf.old" || die "Cannot rename config file $conf to $conf.oldn";
  62. open(OLD,"<$conf.old") || die "Cannot open old config file $conf.old to readn";
  63. open(NEW,">$conf") || die "Cannot open new config file $conf to writen";
  64. #
  65. $add_info_refresh="no";
  66. $fetch_images="no";
  67. $fetch_frames="no";
  68. #
  69. $prevsection='';
  70. $section='';
  71. $includedfile=0;
  72. $filenamepending=0;
  73. $OLD=OLD;
  74. $NEW=NEW;
  75. while(<$OLD>)
  76.   {
  77.    $wildcards=1 if(m/^# WWWOFFLE - World Wide Web Offline Explorer - Version 2.[45]/);
  78.    next if(!$section && (/^[ t]*[#{[]/ || /^[ trn]*$/));
  79.    if($filenamepending)
  80.        {
  81.         print $NEW $_;
  82.         if(/^[ trn]*([^ trn#]+)/)
  83.             {
  84.              @conf=split('/',$conf);
  85.              pop(@conf);
  86.              $incconf=join('/',(@conf,$1));
  87.              system "mv $incconf $incconf.old" || die "Cannot rename included config file $incconf to $incconf.oldn";
  88.              open(INCOLD,"<$incconf.old") || die "Cannot open old included config file $incconf.old to readn";
  89.              open(INCNEW,">$incconf") || die "Cannot open new included config file $incconf to writen";
  90.              $OLD=INCOLD;
  91.              $NEW=INCNEW;
  92.              $filenamepending=0;
  93.              $includedfile=1;
  94.             }
  95.         next;
  96.        }
  97.    if(/^[ t]*{/)
  98.        {
  99.         print $NEW $_;
  100.         next;
  101.        }
  102.    if($section && /^[ t]*[/)
  103.        {
  104.         print $NEW $_;
  105.         $filenamepending=1;
  106.         next;
  107.        }
  108.    if(!$section && /^[ t]*([a-zA-Z]+)[ trn]*$/)
  109.        {
  110.         $section=$1;
  111.         $line=$_;
  112.         if($prevsection eq "Options" && $section ne "FetchOptions") # Added in version 2.2
  113.             {
  114.              print "New Section - FetchOptionsn";
  115.              print $NEW $comment{"FetchOptions"};
  116.              print $NEW "FetchOptionsn";
  117.              print $NEW "{n";
  118.              print $NEW " images = $fetch_imagesn";
  119.              print $NEW " frames = $fetch_framesn";
  120.              print $NEW "}n";
  121.             }
  122.         if(($prevsection eq "Options" && $section ne "FetchOptions") ||
  123.            ($prevsection eq "FetchOptions" && $section ne "ModifyHTML")) # Added in version 2.4, Modified in version 2.5
  124.             {
  125.              print "New Section - ModifyHTMLn";
  126.              print $NEW $comment{"ModifyHTML"};
  127.              print $NEW "ModifyHTMLn";
  128.              print $NEW "{n";
  129.              print $NEW " enable-modify-html = non";
  130.              print $NEW "n";
  131.              print $NEW " add-cache-info = $add_info_refreshn";
  132.              print $NEW "n";
  133.              print $NEW "#anchor-cached-begin     = <font color="#00B000">n";
  134.              print $NEW "#anchor-cached-end       = </font>n";
  135.              print $NEW "#anchor-requested-begin  = <font color="#B0B000">n";
  136.              print $NEW "#anchor-requested-end    = </font>n";
  137.              print $NEW "#anchor-not-cached-begin = <font color="#B00000">n";
  138.              print $NEW "#anchor-not-cached-end   = </font>n";
  139.              print $NEW "n";
  140.              print $NEW " disable-script          = non";
  141.              print $NEW " disable-blink           = non";
  142.              print $NEW "n";
  143.              print $NEW " disable-animated-gif    = non";
  144.              print $NEW "}n";
  145.             }
  146.         if($section eq "AllowedConnect") # Renamed in version 2.4
  147.             {
  148.              print "Renamed Section - AllowedConnectHostsn";
  149.              $section="AllowedConnectHosts";
  150.             }
  151.         if($prevsection eq "AllowedConnectHosts" && $section ne "AllowedConnectUsers") # Added in version 2.4
  152.             {
  153.              print "New Section - AllowedConnectUsersn";
  154.              print $NEW $comment{"AllowedConnectUsers"};
  155.              print $NEW "AllowedConnectUsersn";
  156.              print $NEW "{n";
  157.              print $NEW "}n";
  158.             }
  159.         if($prevsection eq "Proxy" && $section ne "DontIndex") # Added in version 2.3
  160.             {
  161.              print "New Section - DontIndexn";
  162.              print $NEW $comment{"DontIndex"};
  163.              print $NEW "DontIndexn";
  164.              print $NEW "{n";
  165.              print $NEW "}n";
  166.             }
  167.         if($section eq "Mirror") # Renamed in version 2.3
  168.             {
  169.              print "Renamed Section - Aliasn";
  170.              $section="Alias";
  171.             }
  172.         if($prevsection eq "DontGetRecursive" && $section ne "DontRequestOffline") # Added in version 2.4
  173.             {
  174.              print "New Section - DontRequestOfflinen";
  175.              print $NEW $comment{"DontRequestOffline"};
  176.              print $NEW "DontRequestOfflinen";
  177.              print $NEW "{n";
  178.              print $NEW " *://*n" if($no_offline_requests);
  179.              print $NEW "}n";
  180.             }
  181.         print "Section - $sectionn";
  182.         print $NEW $comment{$section};
  183.         print $NEW "$sectionn";
  184.         next;
  185.        }
  186.    if($section && !/^[ t]*}/)
  187.        {
  188.         $line=$_;
  189.         if($section eq "Options") # Changed in version 2.1 / 2.2 / 2.4 / 2.5
  190.             {
  191.              if($line =~ /request-expired[ t]*=[ t]*(yes|no|1|0|true|false)/) # Added in version 2.5
  192.                  {
  193.                   $gotrequestexpired=1;
  194.                  }
  195.              elsif($line =~ /request-no-cache[ t]*=[ t]*(yes|no|1|0|true|false)/) # Added in version 2.5
  196.                  {
  197.                   $gotrequestnocache=1;
  198.                  }
  199.              elsif($line =~ /intr-download-keep[ t]*=[ t]*(yes|no|1|0|true|false)/) # Added in version 2.5
  200.                  {
  201.                   $gotintrdownloadkeep=1;
  202.                  }
  203.              elsif($line =~ /connect-timeout[ t]*=[ t]*[0-9]+/) # Added in version 2.5
  204.                  {
  205.                   $gotconnecttimeout=1;
  206.                  }
  207.              elsif($line =~ /ssl-allow-port[ t]*=[ t]*[0-9]+/) # Added in version 2.4
  208.                  {
  209.                   $gotsslallowport=1;
  210.                  }
  211.              elsif($line =~ /request-changed[ t]*=[ t]*([-0-9])/) # Checked in version 2.4
  212.                  {
  213.                   $requestchanged=$1;
  214.                  }
  215.              elsif($line =~ /request-changed-once[ t]*=[ t]*/) # Added in version 2.4
  216.                  {
  217.                   $gotrequestchangedonce=1;
  218.                  }
  219.              if($line =~ /request-changed[ t]*=[ t]*(yes|no|1|0|true|false)/) # Changed in version 2.1
  220.                  {
  221.                   print "Changed Option - request-changedn";
  222.                   $requestchanged=-1;
  223.                   $requestchanged=600 if($1 eq "yes" || $1 eq "1" || $1 eq "true");
  224.                   print $NEW " request-changed = $requestchangedn";
  225.                  }
  226.              elsif($line =~ /fetch-images[ t]*=[ t]*(yes|no|1|0|true|false)/) # Changed in version 2.2
  227.                  {
  228.                   print "Removed Option - fetch-imagesn";
  229.                   $fetch_images=$1;
  230.                  }
  231.              elsif($line =~ /fetch-frames[ t]*=[ t]*(yes|no|1|0|true|false)/) # Changed in version 2.2
  232.                  {
  233.                   print "Removed Option - fetch-framesn";
  234.                   $fetch_frames=$1;
  235.                  }
  236.              elsif($line =~ /offline-requests[ t]*=[ t]*(yes|no|1|0|true|false)/) # Changed in version 2.4
  237.                  {
  238.                   print "Removed Option - offline-requestsn";
  239.                   $no_offline_requests=1 if($1 eq "no" || $1 eq "0" || $1 eq "false");
  240.                  }
  241.              elsif($line =~ /monitor-interval[ t]*=[ t]*/) # Removed in version 2.4
  242.                  {
  243.                   print "Removed Option - monitor-intervaln";
  244.                  }
  245.              elsif($line =~ /add-info-refresh[ t]*=[ t]*(yes|no|1|0|true|false)/) # Moved in version 2.4
  246.                  {
  247.                   print "Removed Option add-info-refreshn";
  248.                   $add_info_refresh=$1;
  249.                  }
  250.              else
  251.                  {
  252.                   print $NEW $line;
  253.                  }
  254.             }
  255.         elsif($section eq "AllowedConnectHosts" || $section eq "LocalNet") # Changed in version 2.4
  256.             {
  257.              if($line =~ /^([ t]*)([-a-zA-Z0-9.]+)[ trn]*$/)
  258.                  {
  259.                   $line=$1 . &wildcard_host($2) . "n";
  260.                  }
  261.              print $NEW $line;
  262.             }
  263.         elsif($section eq "CensorHeader") # Changed in version 2.2
  264.             {
  265.              if($line =~ /^[ t]*([-a-zA-Z0-9]+)[ trn]*$/)
  266.                  {
  267.                   $line=" $1 = n";
  268.                   print "Changed CensorHeader - header line '$1'n";
  269.                  }
  270.              print $NEW $line;
  271.             }
  272.         elsif($section eq "MIMETypes") # New ones added in various versions
  273.             {
  274.              $gotmimetype{$1}=1 if($line =~ /^[ t]*(.[^ ]+)[ t]*=/);
  275.              print $NEW $line;
  276.             }
  277.         elsif($section eq "DontCache" || $section eq "DontGet" ||
  278.               $section eq "DontGetRecursive" || $section eq "DontGetOffline") # Changed in Version 2.3 / 2.4
  279.             {
  280.              if($section eq "DontGet" && $line =~ /^[ t]*#?[ t]*replacement[ t]*=/)
  281.                  {
  282.                   $gotreplacement=1;
  283.                  }
  284.              elsif($line =~ /^[ t]*([^ t#=]+)[ t]*=[ t]*([^ trn=]+)/)
  285.                  {
  286.                   $line=" ".&url_spec($1,$2)."n";
  287.                  }
  288.              elsif($line =~ /^[ t]*([^ trn#=]+)/)
  289.                  {
  290.                   $line=" ".&url_spec($1,"")."n";
  291.                  }
  292.              print $NEW $line;
  293.             }
  294.         elsif($section eq "Proxy") # Changed in version 2.3 / 2.4
  295.             {
  296.              if($line =~ /^[ t]*([^ t#=]+)[ t]*=[ t]*([^ trn=]+)/ && $1 !~ /^auth-/)
  297.                  {
  298.                   $line=" ".&url_spec($1,"")." = $2n";
  299.                  }
  300.              print $NEW $line;
  301.             }
  302.         elsif($section eq "Mirror") # Changed in version 2.3
  303.             {
  304.              if($line =~ /^[ t]*([^ t#=]+)[ t]*=[ t]*([^ trn=]+)/)
  305.                  {
  306.                   $line1=&url_spec($1,"");
  307.                   $line2=&url_spec($2,"");
  308.                   $line=" $line1 = $line2n";
  309.                  }
  310.              print $NEW $line;
  311.             }
  312.         elsif($section eq "Purge") # Changed in version 2.3 / 2.4
  313.             {
  314.              if($line =~ /^[ t]*#?[ t]*use-url[ t]*=/)
  315.                  {
  316.                   $gotuseurl=1;
  317.                  }
  318.              elsif($line =~ /^[ t]*([^ t#=]+)[ t]*=[ t]*([^ trn=]+)/ && $1 !~ /^[ t]*[a-z]+-[a-z]+/)
  319.                  {
  320.                   $line=" ".&url_spec($1,"")." = $2n";
  321.                  }
  322.              print $NEW $line;
  323.             }
  324.         else
  325.             {
  326.              print $NEW $line;
  327.             }
  328.        }
  329.    if(/^[ t]*]/)
  330.        {
  331.         print $NEW $_;
  332.         next;
  333.        }
  334.    if(/^[ t]*}/ || ($includedfile && eof($OLD)))
  335.        {
  336.         $prevsection=$section;
  337.         $section='';
  338.         if($prevsection eq "MIMETypes") # Updated in several versions
  339.             {
  340.              foreach $fext (keys(%new_MIMETypes))
  341.                  {
  342.                   if(!$gotmimetype{$fext})
  343.                       {
  344.                        $mime=$new_MIMETypes{$fext};
  345.                        print "New MIME Types - $mimen";
  346.                        print $NEW " $fext   = $mimen";
  347.                       }
  348.                  }
  349.             }
  350.         if($prevsection eq "Purge" && !$gotuseurl) # Added in version 2.3
  351.             {
  352.              print "New Option - use-urln";
  353.              print $NEW "n use-url = non";
  354.             }
  355.         if($prevsection eq "DontGet" && !$gotreplacement) # Added in version 2.3
  356.             {
  357.              print "New Option - replacementn";
  358.              print $NEW "n# replacement = /local/images/trans-1x1.gifn";
  359.             }
  360.         if($prevsection eq "Options" && !$gotintrdownloadkeep) # Added in version 2.5
  361.             {
  362.              print "New Option - intr-download-keepn";
  363.              print $NEW "n intr-download-keep    = non";
  364.              print "New Option - intr-download-sizen";
  365.              print $NEW " intr-download-size    = 1n";
  366.              print "New Option - intr-download-percentn";
  367.              print $NEW " intr-download-percent = 80n";
  368.              print "New Option - timeout-download-keepn";
  369.              print $NEW "n timeout-download-keep = non";
  370.             }
  371.         if($prevsection eq "Options" && !$gotconnecttimeout) # Added in version 2.5
  372.             {
  373.              print "New Option - connect-timeoutn";
  374.              print $NEW "n connect-timeout = 30n";
  375.             }
  376.         if($prevsection eq "Options" && !$gotrequestexpired) # Added in version 2.5
  377.             {
  378.              print "New Option - request-expiredn";
  379.              print $NEW "n request-expired = non";
  380.             }
  381.         if($prevsection eq "Options" && !$gotrequestnocache) # Added in version 2.5
  382.             {
  383.              print "New Option - request-no-cachen";
  384.              print $NEW "n request-no-cache = non";
  385.             }
  386.         if($prevsection eq "Options" && !$gotsslallowport) # Added in version 2.4
  387.             {
  388.              print "New Option - ssl-allow-portn";
  389.              print $NEW "n ssl-allow-port = 443n";
  390.             }
  391.         if($prevsection eq "Options" && !$gotrequestchangedonce) # Added in version 2.4
  392.             {
  393.              print "New Option - request-changed-oncen";
  394.              print $NEW "n request-changed-once = non" if($requestchanged < 0);
  395.              print $NEW "n request-changed-once = yesn" if($requestchanged >= 0);
  396.             }
  397.         print $NEW $_ if(!$includedfile);
  398.         if($includedfile)
  399.             {
  400.              close($OLD);
  401.              close($NEW);
  402.              $OLD=OLD;
  403.              $NEW=NEW;
  404.              $includedfile=0;
  405.             }
  406.         next;
  407.        }
  408.   }
  409. print $NEW $comment{"TAIL"};
  410. #
  411. close($NEW);
  412. close($OLD);
  413. #
  414. # Subroutine to convert HOST-SPECIFICATION and FILE-SPECIFICATION
  415. # or URL-SPECIFICATION into URL-SPECIFICATION with wildcards.
  416. #
  417. sub url_spec
  418. {
  419.  ($protohost,$file)=@_;
  420.  $url="";
  421.  if($file eq "")
  422.      {
  423.       return("default") if($protohost eq "default");
  424.       return("# $protohost") if($protohost !~ m%^([^:/]+)://([^:/]+)(|:[^/]*)(|/.*)$%);
  425.       $proto=$1;
  426.       $host=$2;
  427.       $port=$3;
  428.       $path=$4;
  429.       $path.="*" if(!$wildcards && $path ne "" && $path !~ /*/);
  430.       $path="/" if($path eq "");
  431.      }
  432.  else
  433.      {
  434.       $proto="";
  435.       $proto="*",$host=$1,$port=$3   if($protohost =~ m%^/?([^:/]+)(|:.*)$%);        # [/]hostname[:*]
  436.       $proto=$1,$host=$2,$port=$3    if($protohost =~ m%^([^:/]+)/([^:/]+)(|:.*)$%); # protocol/hostname[:*]
  437.       $proto=$1,$host="*",$port=$3   if($protohost =~ m%^([^:/]+)/(|:.*)$%);         # protocol/[:*]
  438.       $proto="*",$host="*",$port=""  if($protohost eq "/:" || $protohost eq "default");
  439.       $proto="*",$host="*",$port=":" if($protohost eq "/");
  440.       return("# $host $file") if(!$proto);
  441.       $path="$file*"  if($file =~ m%^/%);
  442.       $path="/*$file" if($file =~ m%^.%);
  443.      }
  444.  $host=&wildcard_host($host);
  445.  $url="$proto://$host$port$path";
  446.  return($url);
  447. }
  448. #
  449. # Subroutine to convert a hostname into a host name with wildcards.
  450. #
  451. sub wildcard_host
  452. {
  453.  ($old)=@_;
  454.  $new=$old;
  455.  return($new) if($wildcards);
  456.  $new="*$old" if($old =~ /^[a-z0-9-.]+$/);
  457.  $new="$old*" if($old =~ /^[0-9.]+$/);
  458.  $new="$old"  if($old =~ /^[0-9]+.[0-9]+.[0-9]+.[0-9]+$/);
  459.  return($new);
  460. }