home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1999 March B / SCO_CASTOR4RRT.iso / update701 / root.18 / etc / mail / convertcf / convertcf
Encoding:
Text File  |  1998-08-18  |  6.3 KB  |  274 lines

  1. #!/bin/tcl
  2. #******************************************************************************
  3. #
  4. #    Copyright (C) 1993-1997 The Santa Cruz Operation, Inc.
  5. #        All Rights Reserved.
  6. #
  7. #    The information in this file is provided for the exclusive use of
  8. #    the licensees of The Santa Cruz Operation, Inc.  Such users have the
  9. #    right to use, modify, and incorporate this code into other products
  10. #    for purposes authorized by the license agreement provided they include
  11. #    this notice and the associated copyright notice with any such product.
  12. #    The information in this file is provided "AS IS" without warranty.
  13. #
  14. #===============================================================================
  15. proc SCO_CONVERTCF_MSGS {} {}
  16. global SCO_CONVERTCF_MSGS
  17. set SCO_CONVERTCF_MSGS(@catalog@) {convertcf.cat@mail 1}
  18. set SCO_CONVERTCF_MSGS(ERR_USAGE) {1 {Usage: convertcf <file>\n  This script converts the sendmail configuration file,\n  pathname specified by argument <file>, to the latest\n  version.}}
  19. set SCO_CONVERTCF_MSGS(ERR_DETERMINE) {2 {Error: unable to determine version of %1$s}}
  20. set SCO_CONVERTCF_MSGS(ERR_FILENAME) {3 {Error: %1$s non-existent, non-regular, or unreadable}}
  21. set SCO_CONVERTCF_MSGS(ERR_ERROR) {4 {Error: %1$s}}
  22. set SCO_CONVERTCF_MSGS(ERR_UNSUPPORTED) {5 {Error: cannot convert %1$s: %2$s is not a supported version}}
  23. set LATEST "UW7.0.1"
  24. set VALIDVERS [list SCO5 $LATEST]
  25. proc \
  26. usage {} \
  27. {
  28.     puts [intlerr USAGE]
  29.     _exit 1
  30. }
  31. proc \
  32. cleanup {} \
  33. {
  34.     global catHandle
  35.     if {"$catHandle" != ""} {
  36.         catclose $catHandle
  37.     }
  38.     return
  39. }
  40. proc \
  41. _exit { exitval } \
  42. {
  43.     cleanup
  44.     exit $exitval
  45. }
  46. proc \
  47. getmsg {messageId argList} \
  48. {
  49.     global catHandle
  50.     set msgSepIdx [string first _MSG_ $messageId]
  51.     set errSepIdx [string first _ERR_ $messageId]
  52.     if {$msgSepIdx < 0} {
  53.         set sepIdx $errSepIdx
  54.     } else {
  55.         if {$errSepIdx < 0} {
  56.             set sepIdx $msgSepIdx
  57.         } else {
  58.             set sepIdx [min $msgSepIdx $errSepIdx]
  59.         }
  60.     }
  61.     if {$sepIdx <= 0} {
  62.         error "Invalid message ID `$messageId'"
  63.     }
  64.     set modId [csubstr $messageId 0 $sepIdx]
  65.     set msgName [csubstr $messageId [expr $sepIdx+1] end]
  66.     upvar #0 "${modId}_MSGS" moduleDecls
  67.     if {![info exists moduleDecls]} {
  68.         auto_load "${modId}_MSGS"
  69.     }
  70.     append msgEntryVar moduleDecls "(" $msgName ")"
  71.     if {![info exists $msgEntryVar]} {
  72.         error "Unknown message ID `$messageId'"
  73.     }
  74.     append msgCatalogVar moduleDecls "(@catalog@)"
  75.     set msgEntry [concat [set $msgCatalogVar] [set $msgEntryVar]]
  76.     if { "$catHandle" == "" } {
  77.         set catHandle [catopen [lindex $msgEntry 0]]
  78.     }
  79.     set str [catgets $catHandle [lindex $msgEntry 1] \
  80.         [lindex $msgEntry 2] [lindex $msgEntry 3]]
  81.     return [eval format [list [cexpand $str]] $argList]
  82. }
  83. proc \
  84. intlerr {msgid {argList {}}} \
  85. {
  86.     return [getmsg "SCO_CONVERTCF_ERR_${msgid}" $argList]
  87. }
  88. proc \
  89. intlmsg {msgid {argList {}}} \
  90. {
  91.     return [getmsg "SCO_CONVERTCF_MSG_${msgid}" $argList]
  92. }
  93. proc \
  94. getver { filename } \
  95. {
  96.     set version ""
  97.     if {[file exists $filename] && \
  98.         [file isfile $filename] && \
  99.         [file readable $filename]} {
  100.         for_file line $filename {
  101.             if {[regexp {^DZ(.*)$} $line match version] == 1} {
  102.                 set version [string trim $version]
  103.                 break
  104.             }
  105.         }
  106.         if {"$version" == ""} {
  107.             puts [intlerr DETERMINE $filename]
  108.             _exit 1
  109.         } else {
  110.             return $version
  111.         }
  112.     } else {
  113.         puts [intlerr FILENAME $filename]
  114.         _exit 1
  115.     }
  116. }
  117. proc \
  118. validver { version } \
  119. {
  120.     global VALIDVERS
  121.     if {[lsearch -exact $VALIDVERS "$version"] != -1} {
  122.         return 1
  123.     } else {
  124.         return 0
  125.     }
  126. }
  127. proc \
  128. addsum { filename } \
  129. {
  130.     set sumret [exec sum -r $filename]
  131.     set csum [ctoken sumret " \t"]
  132.     set tmp /tmp/addsum.[pid]
  133.     if {[catch {open $filename r} afd] != 0} {
  134.         puts [intlerr ERROR $afd]
  135.         _exit 1
  136.         }
  137.     if {[catch {open $tmp w} tfd] != 0} {
  138.         puts [intlerr ERROR $tfd]
  139.         close $afd
  140.         _exit 1
  141.     }
  142.     puts $tfd "# checksum:$csum"
  143.     if {[catch {copyfile $afd $tfd} ret] != 0} {
  144.         puts [intlerr ERROR $ret]
  145.         close $afd
  146.         close $tfd
  147.         system "rm -f $tmp"
  148.         _exit 1
  149.     }
  150.     close $afd
  151.     close $tfd
  152.     system "cp $tmp $filename"
  153.     system "rm -f $tmp"
  154. }
  155. proc \
  156. conv7to701 { filename } \
  157. {
  158.     set tmp /tmp/conv7to701.[pid]
  159.     if {[catch {open $filename r} cfd] != 0} {
  160.         puts [intlerr ERROR $cfd]
  161.         _exit 1
  162.         }
  163.     if {[catch {open $tmp w+} tfd] != 0} {
  164.         puts [intlerr ERROR $tfd]
  165.         close $cfd
  166.         _exit 1
  167.     }
  168.     while {[gets $cfd line] != -1} {
  169.         # remove the checksum line
  170.         if {[regexp {^# checksum:.*$} $line match] == 1} {
  171.             continue
  172.         }
  173.         # update the version
  174.         if {[regexp {^DZSCO5$} $line match] == 1} {
  175.             puts $tfd "DZUW7.0.1"
  176.             continue
  177.         }
  178.         # change so that unqualified names are qualified in
  179.         # re-writing rules for smtp mail
  180.         if {[regexp {^R\$\*[    ]*\$: \$>71 \$1$} $line match] == 1} {
  181.             puts $tfd "R\$*\t\$: \$>61 \$1"
  182.             continue
  183.         }
  184.         # add -user flag to slocal definition
  185.         if {[regexp {^([     M].*A[     ]*=[     ]*slocal)([^,]*)(.*)$} $line match p1 p2 p3] == 1} {
  186.             set p2 [string trim $p2]
  187.             set arglist [split $p2]
  188.             # remove empty lists created by split
  189.             set tmplist ""
  190.             foreach arg $arglist {
  191.                 if {"$arg" != ""} {
  192.                     lappend tmplist $arg
  193.                 }
  194.             }
  195.             set arglist $tmplist
  196.             set numargs [llength $arglist]
  197.             set newargs ""
  198.             set new 0
  199.             for {set i 0} {$i < $numargs} {incr i} {
  200.                 if {"[lindex $arglist $i]" == "\$u"} {
  201.                     if {$i > 0} {
  202.                         set before [expr $i - 1]
  203.                         if {[regexp {^-(.*)$} "[lindex $arglist $before]" match flag] == 1} {
  204.                             if {"$flag" == "user"} {
  205.                                 break;
  206.                             }
  207.                         } else {
  208.                             lvarcat newargs "-user"
  209.                             set new 1
  210.                         }
  211.                     } else {
  212.                         lvarcat newargs "-user"
  213.                         set new 1
  214.                     }
  215.                 }
  216.                 lvarcat newargs [lindex $arglist $i]
  217.                 if {$i == [expr $numargs - 1] && !$new} {
  218.                     lvarcat newargs "-user"
  219.                     lvarcat newargs "\$u"
  220.                     set new 1
  221.                 }
  222.             }
  223.             if {$new} {
  224.                 puts $tfd "$p1 $newargs$p3"
  225.                 continue
  226.             } 
  227.         }
  228.         puts $tfd $line
  229.     }
  230.     close $cfd
  231.     seek $tfd 0
  232.     if {[catch {open $filename w} cfd] != 0} {
  233.         puts [intlerr ERROR $cfd"]
  234.         close $tfd
  235.         system "rm -f $tmp"
  236.         _exit 1
  237.     }
  238.     if {[catch {copyfile $tfd $cfd} ret] != 0} {
  239.         puts [intlerr ERROR $ret]
  240.         close $cfd
  241.         close $tfd
  242.         system "rm -f $tmp"
  243.         _exit 1
  244.     }
  245.     close $cfd
  246.     close $tfd
  247.     system "rm -f $tmp"
  248. }
  249. proc \
  250. main {} \
  251. {
  252.     global argv cfpath catHandle LATEST
  253.     set catHandle ""
  254.     set cfpath ""
  255.     set cfpath [lindex $argv 0]
  256.     if {"$cfpath" == ""} {
  257.         usage
  258.     }
  259.     set curver [getver $cfpath]
  260.     if {![validver $curver]} {
  261.         puts [intlerr UNSUPPORTED [list $cfpath $curver]]
  262.         _exit 1
  263.     }
  264.     case $curver {
  265.         SCO5 {
  266.             system "cp $cfpath ${cfpath}.bak"
  267.             conv7to701 $cfpath
  268.             addsum $cfpath
  269.         }
  270.     }
  271.     _exit 0
  272. }
  273. main
  274.