home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / cnews.tar / inject / defhdrs.awk < prev    next >
Text File  |  1994-08-29  |  4KB  |  139 lines

  1. # defhdrs.awk
  2. # pass 1 - note presence | absence of certain headers
  3. # a header keyword: remember it and its value
  4. BEGIN    { status = 0; hdrval["Trash:"] = "Trash:" }
  5. /^[^\t ]*:/ {
  6.     hdrval[$1] = $0
  7.     keyword=$1
  8.     next
  9. }
  10. # a continuation: concatenate this line to the value
  11.     { hdrval[keyword] = hdrval[keyword] "\n" $0 }
  12.  
  13. END {
  14.     # pass 2 - cogitate & omit & emit headers
  15.     emptyhdrre = "^[^\t ]*:[\t ]*$"
  16.     subjname = "Subject:"
  17.     ctlname = "Control:"
  18.     ngname = "Newsgroups:"
  19.     msgidname = "Message-ID:"
  20.     typoname =  "Message-Id:"
  21.     pathname = "Path:"
  22.     datename = "Date:"
  23.     fromname = "From:"
  24.     orgname = "Organization:"
  25.     distrname = "Distribution:"
  26.     sendername = "Sender:"
  27.     expiresname = "Expires:"
  28.  
  29.     # nullify headers with empty contents
  30.     for (i in hdrval)
  31.         if (hdrval[i] ~ /^[^\t ]*:[\t ]*$/)
  32.             hdrval[i] = ""
  33.  
  34.     # fill in missing headers
  35.     if (hdrval[typoname] != "") {    # spelling hack
  36.         hdrval[msgidname] = hdrval[typoname]
  37.         hdrval[typoname] = ""
  38.         # fix spelling: Message-Id: -> Message-ID:
  39.         nf = split(hdrval[msgidname], fields)    # bust up
  40.         fields[1] = msgidname        # fix spelling
  41.         hdrval[msgidname] = fields[1]    # reassemble...
  42.         for (i = 2; i <= nf; i++)
  43.             hdrval[msgidname] = hdrval[msgidname] " " fields[i]
  44.     }
  45.     if (hdrval[pathname] == "")
  46.         hdrval[pathname] = pathname " " defpath
  47.     if (hdrval[msgidname] == "")
  48.         hdrval[msgidname] = msgidname " " defmsgid
  49.     if (hdrval[datename] == "")
  50.         hdrval[datename] = datename " " defdate
  51.     if (hdrval[expiresname] == "" && defexpiry != "")
  52.         hdrval[expiresname] = expiresname " " defexpiry
  53.     if (hdrval[orgname] == "" && deforg != "")
  54.         hdrval[orgname] = orgname " " deforg
  55.     if (hdrval[fromname] == "")
  56.         hdrval[fromname] = fromname " " deffrom
  57.     else if (hdrval[sendername] == "")
  58.         hdrval[sendername] = sendername " " deffrom
  59.  
  60.     # replace user's headers (if any) [this is not currently done]
  61.  
  62.     # snuff some headers
  63.     distworld = distrname " world"
  64.     if (hdrval[distrname] == distworld)
  65.         hdrval[distrname] = ""
  66.  
  67.     # the vile cmsg hack, for the sake of the news readers *only*
  68.     if (hdrval[ctlname] == "" && \
  69.         substr(hdrval[subjname], 1, 14) == "Subject: cmsg ")
  70.         hdrval[ctlname] = ctlname " " substr(hdrval[subjname], 15)
  71.  
  72.     # warn if no Subject:
  73.     if (hdrval[subjname] == "") {
  74.         print "defhdrs.awk: no " subjname " header!" | "cat >&2"
  75.         status = 1
  76.     }
  77.  
  78.     # warn if mailname was never initialized
  79.     if (hdrval[fromname] ~ /@no\.such\.domain$/) {
  80.         print "defhdrs.awk: can't construct " fromname " header!" | "cat >&2"
  81.         status = 1
  82.     }
  83.  
  84.     # warn if no Newsgroups:
  85.     if (hdrval[ngname] == "") {
  86.         print "defhdrs.awk: no " ngname " header!" | "cat >&2"
  87.         status = 1
  88.     }
  89.     if (hdrval[ngname] ~ /^Newsgroups:  *.*[\t ]/) {
  90.         print "defhdrs.awk: whitespace in " ngname " header" | "cat >&2"
  91.         status = 1
  92.     }
  93.  
  94.     # field the all.all.ctl hack, for the sake of the backward only:
  95.     # clone Subject: to make Control:
  96.     if (hdrval[ctlname] == "" && hdrval[ngname] ~ /\.ctl(,|$)/)
  97.         hdrval[ctlname] = ctlname " " substr(hdrval[subjname], 8)
  98.  
  99.     # rewrite "Path: ME!blah" to "Path: blah", repeatedly
  100.     while (substr(hdrval[pathname], 1+6, length(me)+1) == (me "!"))
  101.         hdrval[pathname] = pathname " " \
  102.             substr(hdrval[pathname], 1+6+length(me)+1)
  103.  
  104.     # reorder & emit headers
  105.  
  106.     # favour Control: & Newsgroups: for future benefit of rnews
  107.     if (hdrval[ctlname] != "") {
  108.         print hdrval[ctlname]
  109.         hdrval[ctlname] = ""    # no Control: to print now
  110.     }
  111.     if (hdrval[ngname] != "") {
  112.         print hdrval[ngname]
  113.         hdrval[ngname] = ""    # no Newsgroups: to print now
  114.     }
  115.  
  116.     # B inews kludgery: print Path: before From: to avoid confusing it
  117.     if (hdrval[pathname] != "") {
  118.         print hdrval[pathname]
  119.         hdrval[pathname] = ""    # no Path: to print now
  120.     }
  121.     if (hdrval[fromname] != "") {
  122.         print hdrval[fromname]
  123.         hdrval[fromname] = ""    # no From: to print now
  124.     }
  125.  
  126.     # have pity on readers: put Subject: next
  127.     if (hdrval[subjname] != "") {
  128.         print hdrval[subjname]
  129.         hdrval[subjname] = ""    # no Subject: to print now
  130.     }
  131.  
  132.     # print misc. non-empty headers in random order
  133.     for (i in hdrval)
  134.         if (hdrval[i] != "" && hdrval[i] !~ /^[^\t ]*:[\t ]*$/)
  135.             print hdrval[i]
  136.  
  137.     exit status
  138. }
  139.