home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 98 / Skunkware 98.iso / osr5 / sco / scripts / admin / mmdf / mmdfsel < prev    next >
Encoding:
Text File  |  1997-08-26  |  7.5 KB  |  357 lines

  1. #!/usr/lib/scosh/utilbin/oash
  2. # @(#) mmdfsel.oash 1.1 93/12/24
  3. # 92/09/25 john h. dubois iii (john@armory.com)
  4. # 92/11/22 Added EditAddress
  5. # 93/12/15 Added "deliver" action.
  6. #          Made 'Kill' not be the first action in list.
  7. #          Fixed bug that caused failure if address field truncated too much.
  8. #          Change header to 'Select up to 6...' since oash can't handle more.
  9. # 93/12/20 Pass any options to mmdfq
  10. # 93/12/24 Use $* instead of $@ for cmd line args due to oash bug
  11. #          Added +o to mmdfq options
  12. #          Added Reinject action.
  13.  
  14. # todo: rewrite this in vtcl.
  15.  
  16. PATH=/usr/lib/oa/utilbin:/usr/lib/scosh/utilbin:$PATH
  17.  
  18. # run ourself thru oash 
  19. oadeinit 2>/dev/null || exec oash $0 "$@"
  20.  
  21. : ${TMP:=$TMPDIR}
  22. : ${TMP:=/tmp}
  23. TmpPref=$TMP/#mmdfsel.$$
  24. ActionTmp=$TmpPref.p
  25. ViewFileWin=3
  26. LogTmp=$TmpPref.l
  27.  
  28. CloseWindow() {
  29.     wmove 0
  30.     wmclose $1
  31. }
  32.  
  33. # Print a message centered on the screen.
  34. # Usage: WinMessage "message" ["title"]
  35. # If no title is given, the window has no title.
  36. # WinMessage wait for the use to press <esc>,
  37. # then closes the window and returns.
  38. WinMessage() {
  39.     Message=$1
  40.     Title=$2
  41.     Length=`expr length "$Message" + 2`
  42.     [ $Length -lt 30 ] && Length=30
  43.     Clean_wmopen 38 3 $Length 10
  44.     [ $# -gt 1 ] && wmtitle "$Title"
  45.     wmmessage -r "press <return> to continue"
  46.     echo "$Message"
  47.     wrefresh 38
  48.     read garbage
  49.     CloseWindow 38
  50. }
  51.  
  52. # Usage: Centered_wmopen num lines cols top_row
  53. # Creates a window centered on column 38/39
  54. Centered_wmopen() {
  55.     wmopen $1 $2 $3 $4 `expr 39 - $3 / 2`
  56. }
  57.  
  58. # Usage: Clean_wmopen num lines cols top_row
  59. # Creates a window centered on column 38/39 and clears a border around it.
  60. # Uses window #30 to clear.
  61. Clean_wmopen() {
  62.     N=$1 L=$2 C=$3 X=$4
  63.     set `ksh -c "echo $(($L+2)) $(($C+2)) $(($X-1)) $((38-$C/2)) $((39-$C/2))"`
  64.     newwin 30 $1 $2 $3 $4
  65.     wmclose 30
  66.     wmopen $N $L $C $X $5
  67. }
  68.  
  69. # Usage: ViewFile filename
  70. # Sets LAST_KEY
  71. ViewFile() {
  72.     ViewFileFile=$1
  73.     wmopen $ViewFileWin $LINES 78 0 0
  74.     if [ -d "$ViewFileFile" ]; then
  75.     wmtitle "Viewing directory: $ViewFileFile"
  76.     l -RA "$ViewFileFile" > $DirList
  77.     ViewF=$DirList
  78.     else
  79.     wmtitle "Viewing file: $ViewFileFile"
  80.     ViewF=$ViewFileFile
  81.     fi
  82.     wmmessage -r "Use arrow keys, <esc> when done"
  83.     scan -e"$EOFMSG" "$ViewF"
  84.     CloseWindow $ViewFileWin
  85. }
  86.  
  87. mmdf_kill() {
  88.     oadeinit
  89.     echo "Killing $*..."
  90.     for job
  91.     do
  92.     echo */$job
  93.     done | xargs rm -f
  94.     echo -n "Press return to continue..."
  95.     read garbage
  96.     oainit
  97. }
  98.  
  99. Deliver() {
  100.     oadeinit
  101.     echo "Attempting delivery of $*..."
  102.     /usr/mmdf/bin/deliver -w $*
  103.     echo -n "Press return to continue..."
  104.     read garbage
  105.     oainit
  106. }
  107.  
  108. Reinject() {
  109.     oadeinit
  110.     echo "Moving data files for $*..."
  111.     ReinjectList=
  112.     # First move message files so original messages won't accidently be
  113.     # delivered while being reinjected.
  114.     cd msg
  115.     for job
  116.     do
  117.     if [ ! -f $job ]; then
  118.         echo "Cannot find message file for message $job."
  119.     else
  120.         ReinjectList="$ReinjectList $job"
  121.     fi
  122.     done
  123.     if [ -n "$ReinjectList" ]; then
  124.     mv $ReinjectList ../tmp
  125.     cd ../tmp
  126.     echo Reinjecting $ReinjectList...
  127.     mmdfq +no $ReinjectList | while read q id return recip; do
  128.         /usr/lib/mail/execmail -dv -f $return $recip < msg.$id
  129.     done
  130.     echo -n "Press return to remove original messages & continue..."
  131.     else
  132.     echo "No messages to reinject."
  133.     echo -n "Press return continue..."
  134.     fi
  135.     cd ..
  136.     read garbage
  137.     if [ -n "$ReinjectList" ]; then
  138.     for job in $ReinjectList
  139.     do
  140.         FList="$FList */$job"
  141.     done
  142.     rm $FList
  143.     fi
  144.     oainit
  145. }
  146.  
  147. LogDeliver() {
  148.     oadeinit
  149.     echo "Attempting delivery of $*..."
  150.     /usr/mmdf/bin/deliver -w -VFTR -L$LogTmp $*
  151.     echo -n "Press return to view delivery log..."
  152.     read garbage
  153.     oainit
  154.     ViewFile $LogTmp
  155. }
  156.  
  157. AddrFiles() {
  158.     # Do this with a separate sh to avoid expensive oainit
  159.     sh -c '
  160.     NoLine=-n
  161.     for job
  162.     do
  163.     echo $NoLine ""
  164.     unset NoLine
  165.     if [ -f addr/$job ]; then
  166.         echo $job:
  167.         cat addr/$job
  168.     else 
  169.         set -- q.*/$job
  170.         if [ -f $1 ]; then
  171.         echo $job:
  172.         cat $1
  173.         else
  174.         echo "$job: No address file."
  175.         fi
  176.     fi
  177.     done >'" $ActionTmp" $*
  178.     Clean_wmopen 3 21 60 1
  179.     wmtitle "Address Files"
  180.     wmmessage -r "Use arrow keys, <esc> when done"
  181.     scan -e"$EOFMSG" $ActionTmp
  182.     CloseWindow 3
  183. }
  184.  
  185. EditAddress() {
  186.     # "edit" cmd fails miserably, so use $VISUAL etc.
  187.     if [ -z "$VISUAL" ]; then
  188.     if [ -n "$EDITOR" ]; then
  189.         VISUAL=$EDITOR
  190.     else
  191.         VISUAL=vi
  192.     fi
  193.     fi
  194.     EdList=
  195.     for job
  196.     do
  197.     EdList="$EdList addr/$job"
  198.     done
  199.     oadeinit
  200.     $VISUAL $EdList
  201.     oainit
  202. }
  203.  
  204. ViewData() {
  205.     for job
  206.     do
  207.     if [ -r msg/$job ]; then
  208.         ViewFile msg/$job
  209.     else
  210.         WinMessage "Cannot open msg/$job"
  211.     fi
  212.     done
  213. }
  214.  
  215. # Move message files instead of address files because data file will have
  216. # only one link to restore if neccessary
  217. MoveJobs() {
  218.     oadeinit
  219.     echo "Data files being moved to $PWD/tmp:"
  220.     echo $*
  221.     cd msg
  222.     mv $* ../tmp
  223.     cd ..
  224.     echo "Move them back to $PWD/msg to allow them to be processed."
  225.     echo "Note: files will eventually be removed from tmp by cleanque."
  226.     echo -n "Press return to continue..."
  227.     read garbage
  228.     oainit
  229. }
  230.  
  231. # Returns 0 if the action taken may have changed the queued messages,
  232. # so that the queue should be reexamined.  Returns 1 if 'quit' was selected.
  233. # Returns 2 if no changes made.
  234. Action() {
  235.     while :; do
  236.     Clean_wmopen 2 10 30 7
  237.     wmtitle "Select Action"
  238.     wmmessage -r "<esc> to return"
  239.  
  240. # this option is disabled because the -L option to deliver has no effect,
  241. # even for root & mmdf
  242. #    "Deliver & View Log" \
  243.  
  244.     pp \
  245.     "Attempt Delivery" \
  246.     "Move from queue to tmp" \
  247.     "View Address Files" \
  248.     "Edit Address Files" \
  249.     "View Data Files" \
  250.     "Reinject messages" \
  251.     "Kill Messages" \
  252.     Quit
  253.     [ $PP_NUM -lt 0 ] && return 2
  254.     case $PP_ITEM in
  255.     Attempt*)
  256.         Deliver $MessageIDs; break;;
  257.     Move*)
  258.         MoveJobs $MessageIDs; break;;
  259.     "View Address"*)
  260.         AddrFiles $MessageIDs;;
  261.     Edit*)
  262.         EditAddress $MessageIDs;;
  263.     "View Data"*)
  264.         ViewData $MessageIDs;;
  265.     Reinject*)
  266.         Reinject $MessageIDs; break;;
  267.     Kill*) 
  268.         mmdf_kill $MessageIDs; break;;
  269.     Deliver*)
  270.         LogDeliver $MessageIDs; break;;
  271.     Quit)
  272.         return 1;;
  273.     esac
  274.     done
  275.     return 0
  276. }
  277.  
  278. GetJobs() {
  279.     mmdfq +Vow80 $* > $TmpPref
  280.     {
  281.     read header
  282.     read line
  283.     if [ -z "$line" ]; then
  284.         echo "No MMDF jobs queued."
  285.         return 1
  286.     fi
  287.     } < $TmpPref
  288.     return 0
  289. }
  290.  
  291. Fail() {
  292.     oadeinit
  293.     echo "$*"
  294.     exit 1
  295. }
  296.  
  297. SelectJobs() {
  298.     ret=2
  299.     while [ $ret -eq 2 ]; do
  300.     wmopen 1 $LINES 78 0 0
  301.     wmtitle "Select up to 6 MMDF Jobs"
  302.     wmmessage -r \
  303. "Use arrow keys; <space> selects; <Return> when done selecting; <esc> to quit"
  304.  
  305.     pp -m -f $TmpPref
  306.  
  307.     [ -z "$PP_ITEM" ] && return 1
  308.         
  309.     set -f
  310.     set -- $PP_ITEM
  311.     set +f
  312.     # Get rid of header if it was selected for some silly reason
  313.     if [ "$2" = Message ]; then
  314.         [ $# -lt 6 ] && Fail "Not enough header fields to shift."
  315.         shift 6
  316.     fi
  317.     unset MessageIDs
  318.     while [ $# -gt 2 ]; do
  319.         MessageIDs="$MessageIDs msg.$2"
  320.         # shift until the date field of the next message is in the correct
  321.         # position.  A total of 7 shifts normally works but truncation of
  322.         # address fields can mess this up.
  323.         [ $# -lt 6 ] && Fail "Wrong number of fields."
  324.         shift 5
  325.         while [ $# -ge 4 ]; do
  326.         case "$4" in
  327.         [0-2][0-9]/[0-3][0-9]/[0-9][0-9]|NO_ADDR)
  328.             break;;
  329.         esac
  330.         shift
  331.         done
  332.     done
  333.  
  334.     # Return the return value of Action
  335.     Action
  336.     ret=$?
  337.     done
  338.     return $ret
  339. }
  340.  
  341. # Start of main program
  342.  
  343. [ -z "$LINES" ] && LINES=`tput lines`
  344.  
  345. cd /usr/spool/mmdf/lock/home
  346. GetJobs || exit 0
  347.  
  348. oainit
  349. while SelectJobs; do
  350.     GetJobs || break
  351.     oainit
  352. done
  353. oadeinit
  354.  
  355. rm -f $TmpPref $ActionTmp $LogTmp
  356. exit 0
  357.