home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / ctl / newgroup < prev    next >
Text File  |  1995-04-27  |  5KB  |  216 lines

  1. #! /bin/sh
  2. # newgroup group flag - create group (4-field version: B-2.10.3+ compatible)
  3. #    subject to our sys file group pattern
  4. # also subject to $NEWSCTL/controlperm:  four fields per line, first
  5. # a newsgroup pattern, second an author name (or "any"), third a set of
  6. # operations ("n" newgroup, "r" rmgroup), and fourth a set of
  7. # flags ("y" do it, "n" don't, "q" don't report at all, "v" include
  8. # entire control message in report) (default "yv").
  9.  
  10. # =()<. ${NEWSCONFIG-@<NEWSCONFIG>@}>()=
  11. . ${NEWSCONFIG-/etc/news/bin/config}
  12.  
  13. PATH=$NEWSCTL/bin:$NEWSBIN:$NEWSPATH; export PATH
  14. umask $NEWSUMASK
  15.  
  16. g="$1"
  17. f="$2"
  18.  
  19. posting=/tmp/np$$
  20. hdr=/tmp/nc$$
  21. tmp=/tmp/nt$$
  22.  
  23. # get the full article, and its header, into files for inspection
  24. trap "rm -f $posting $hdr $tmp ; trap 0 ; exit 0" 0
  25. cat >$posting
  26. canonhdr $posting >$hdr
  27.  
  28. # who sent it?
  29. author="`egrep '^From:' $hdr | sed 's/^[^:]*: *//'`"
  30. authorid="`echo \"$author\" | sed '/.*<\(.*\)>.*/s//\1/
  31.                    /\([^ ][^ ]*\)  *(.*).*/s//\1/'`"
  32.  
  33. # was it approved?
  34. case "`egrep '^Approved:' $hdr`" in
  35. '')    reject=${reject-'no Approved header'}    ;;
  36. esac
  37.  
  38. # verify acceptable name syntax
  39. verdict="`echo \"$g\" | awk -f $NEWSBIN/namecheck.awk`"
  40. if test " $verdict" != " "
  41. then
  42.     reject=${reject-"$verdict"}
  43. fi
  44.  
  45. # is this just a change of moderation status, or even a no-op?
  46. getst="\$1 == \"$g\" { print \$4 }"
  47. oldstatus="`awk \"$getst\" $NEWSCTL/active`"
  48. # nonempty $oldstatus means it's just a status change
  49. case "$oldstatus" in
  50. '')    old=e    ;;
  51. m*)    old=m    ;;
  52. x*)    old=x    ;;
  53. *)    old=y    ;;
  54. esac
  55. case "$f" in
  56. moderated)    new=m    ;;
  57. unmoderated)    new=y    ;;
  58. '')        new=y    ;;
  59. *)        new=x
  60.         reject=${reject-'invalid moderation setting'}
  61.         ;;
  62. esac
  63. case "$old-$new" in
  64. m-m|y-y)    exit    ;;    # no-op
  65. x-*)        exit    ;;    # disregard, group nulled out locally
  66. esac
  67. case "$oldstatus" in
  68. ''|m|y)    ;;
  69. *)    reject=${reject-'moderation status is not m or y, cannot change it'}    ;;
  70. esac
  71. newstatus="$new"
  72.  
  73. # consult control file, if present
  74. # Putting this after the no-op check is a heuristic:  in practice, there are
  75. # a lot of no-ops and many of them would flunk the control-file check, but
  76. # if it doesn't matter, why pollute the sysadmin's mailbox...
  77. perms=$NEWSCTL/controlperm
  78. action=yv
  79. if test -r $perms
  80. then
  81.     newaction=`gngp -a -r "$g" $perms | awk '$3 ~ /n/' |
  82.         awk '$2 == "any" || $2 == "'"$authorid"'" { print $4 }' |
  83.         sed -n 1p`
  84.     case "$newaction" in
  85.     ?*)    action=$newaction    ;;
  86.     esac
  87. fi
  88. case "$action" in
  89. *n*)    reject=${reject-'controlperm file denies permission'}    ;;
  90. esac
  91.  
  92. # check that my sys file allows this group
  93. # This is redundant if it's just a status change, but it's easier just
  94. # to do it anyway.
  95. me="`newshostname`"
  96. gngppat=`awk -f $NEWSBIN/canonsys.awk $NEWSCTL/sys |
  97.     egrep "^($me|ME)[:/]" |
  98.     awk -F: '
  99. {
  100.     fields = split($2, field2, "/")        # split ngs/dists
  101.     nngs = split(field2[1], ngs, ",")    # split ng,ng,ng
  102.     for (i = 1; i < nngs; i++)        # print field2[1] robustly
  103.         printf "%s,", ngs[i]
  104.     printf "%s\n", ngs[nngs]
  105.     exit
  106. }' `
  107. case "`echo \"$g\" | gngp -a \"$gngppat\"`" in
  108. '')    reject=${reject-'unsubscribed group'}    ;;
  109. esac
  110.  
  111. # the verdict
  112. case "$reject" in
  113. ?*)    case "$action" in
  114.     *q*)    ;;
  115.     *)    (
  116.             echo "newgroup: \`$author' tried"
  117.             case "$oldstatus" in
  118.             '')    echo "to create newsgroup \`$g'."    ;;
  119.             *)    echo "to set newsgroup \`$g' to \`$f'."    ;;
  120.             esac
  121.             echo "Request was refused:"
  122.             echo "    $reject"
  123.             case "$action" in
  124.             *v*)    echo '==='
  125.                 cat $posting
  126.                 echo '==='
  127.                 ;;
  128.             esac
  129.         ) | report 'rejected newgroup request'
  130.         ;;
  131.     esac
  132.     exit
  133.     ;;
  134. esac
  135.  
  136. # do the job
  137. case "$oldstatus" in
  138. '')    # new group
  139.     max=0000000000
  140.     d=`echo $g | tr . /`
  141.     hi=
  142.     if test -d $NEWSARTS/$d            # recreating old group
  143.     then
  144.         hi=`ls -f $NEWSARTS/$d | egrep '^[0-9]+$' | sort -n | tail -1`
  145.     fi
  146.     if test " $hi" != " "            # old articles still there
  147.     then
  148.         max=000$hi
  149.     fi
  150.     mkpdir $NEWSARTS/$d $NEWSOV/$d
  151.     touch $NEWSOV/$d/.overview
  152.     was="`wc -l <$NEWSCTL/active`"
  153.     echo "$g $max 00001 $newstatus" >>$NEWSCTL/active
  154.     echo "$g `now` $author" >>$NEWSCTL/active.times
  155.     if test `wc -l <$NEWSCTL/active` -le $was
  156.     then
  157.         fail='unable to append new entry to active file'
  158.     fi
  159.     # and a newsgroups entry, if any...
  160.     awk '    BEGIN { printit = 0 }
  161.         $0 == "For your newsgroups file:" { printit = NR+1 }
  162.         NR == printit { print }' $posting | tail -1 >$tmp
  163.     if test ! -s $tmp
  164.     then
  165.         echo "$g    --- no description supplied ---" >$tmp
  166.     fi
  167.     cat $tmp >>$NEWSCTL/newsgroups
  168.     ;;
  169. *)    # status change
  170.     awk '$1 == "'"$g"'" { print $1, $2, $3, "'"$newstatus"'" ; next }
  171.         { print }' $NEWSCTL/active >$NEWSCTL/active.tmp
  172.     if test `wc -l <$NEWSCTL/active.tmp` -lt `wc -l <$NEWSCTL/active`
  173.     then
  174.         fail="active.tmp is shorter than active, something's wrong"
  175.     elif mv $NEWSCTL/active.tmp $NEWSCTL/active
  176.     then
  177.         : okay
  178.     else
  179.         fail='cannot rename active.tmp to active'
  180.     fi
  181.     ;;
  182. esac
  183.  
  184. # and report it, if appropriate
  185. case "$action" in
  186. *q*)    ;;
  187. *)    case "$oldstatus" in
  188.     '')    title="newgroup $g"    ;;
  189.     *)    title="status change for $g"    ;;
  190.     esac
  191.     (
  192.         echo "$author said to"
  193.         case "$oldstatus" in
  194.         '')    echo "create \`$g'."    ;;
  195.         *)    case "$newstatus" in
  196.             m)    p=moderated    ;;
  197.             *)    p=unmoderated    ;;
  198.             esac
  199.             echo "set \`$g' to $p."    ;;
  200.         esac
  201.         case "$fail" in
  202.         '')    echo "This was done."    ;;
  203.         *)    echo "This failed:"
  204.             echo "    $fail"
  205.             ;;
  206.         esac
  207.         case "$action" in
  208.         *v*)    echo '==='
  209.             cat $posting
  210.             echo '==='
  211.             ;;
  212.         esac
  213.     ) | report "$title"
  214.     ;;
  215. esac
  216.