{Utilities for posting a message}

%external %string (79) Copyright %alias "POST_(C)_RMM_FDC_85" = -
   "Copyright (C) 1985 Richard M. Marshall and Frank D. Cringle"

%include "News.Inc"
%include "Inc:Util.Imp"
%include "Inc:FS.Imp"
%include "Inc:FSUtil.Imp"
%include "Managr:AdDefs.Inc"
%include "Managr:Admin.Inc"

%external %string (80) User Name = "", 
                       Full Name, 
                       First Name,
                       Mail Address


{SET USER}
%external %routine Set User

   %record (Admin fm) Admin

   User Name = Current User %if User Name = ""
   Open Admin DB
   %if Search DB (User Name, Admin) %start
      Full Name = Admin_Prenames . " " . Admin_Surname
      First Name = Admin_Prenames
   %else
      Full Name = User Name
      First Name = ""
   %finish
!!!   Mail Address = Admin_Mail Addr
   Close DB
%end {Set User}


{POST}
%external %predicate Post(%string (255) File, Topic)

   %integer Done, Len, Lines, Position, Size
   %string (255) Line, Save File


   {COUNT PRINT SYMBOL}

   %routine Count Print Symbol (%integer Ch)
      Select Host (News Host)
      Print Symbol (Ch)
      Select Host (Home Host)
      Size = Size + 1
   %end {Count Print Symbol}

   {COUNT PRINT STRING}

   %routine Count Print String (%string (255) S)
      %integer I
   
      Select Host (News Host)
      %for I = 1, 1, Length (S) %cycle
         Print Symbol (Char No (S, I))
      %repeat
      Select Host (Home Host)
      Size = Size + Length (S)
   %end {Count Print String}
   
   {COUNT PRINT STRING NL}

   %routine Count Print String NL (%string (255) S)
      Count Print String (S)
      Count Print Symbol (NL)
   %end {Count Print String}

   {HEX2}
   %string (2) %function Hex2 (%integer n)
      %constant %string (16) hex = "0123456789ABCDEF"

      %result = Tostring (Charno (hex, n >> 4 + 1)) . %c
                Tostring (Charno (hex, n & 15 + 1))
   %end {Hex2}

   {READ LINE}
   %routine Read Line (%string (255) %name s)
      %integer Ch

      s = ""
      %cycle
         Read Symbol(Ch)
         Len = Len + 1
         %return %if Ch = NL
         s = s.tostring(Ch)
      %repeat
   %end {Read Line}

   {COUNT LINES}
   %integer %function Count Lines
      %integer l
      %string (255) Junk

      %on %event 9 %start
         Done = Yes
         %result = l
      %finish

      l = 0
      Read Line (Junk)                             { Subject line }
      Read Line (Junk) %if Junk = Tostring (12)    { Ignore initial ^L }
      Read Line (Junk)                             { Normally empty }
      Read Line (Junk) %if Junk -> ("In-reply-to:")
      l = 1 %if Length (Junk) # 0
      %cycle
         Read Line (Junk)
         %return l %if Junk = Tostring (12)
         l = l + 1
      %repeat
   %end {Count Lines}

   {APPEND SIGNATURE}
   %routine Append Signature

      %on %event 9 %start
         Select Host (Home Host)
         Close Input
         %return
      %finish

      %return %unless Exists (User Name . ":" . "NEWS.Sig")
      Select Host (Home Host)
      Open Input (3, User Name . ":" . "NEWS.Sig")
      Select Input (3)
      Count Print String NL ("---")
      %cycle
         Read Line (Line)
         Count Print String NL (Line)
      %repeat
   %end {Append Signature}


   {POST MESSAGE}
   %routine Post Message

      %string (255) Line1, Line2, Line3

      Size = 0
      
      Select Host (News Host)
      Select Output (News Stream)
      Set Output (File Length)
      Count Print String ("From: ")
      Count Print String (User Name)
      %if Full Name # User Name %start
         Count Print String ("   (")
         Count Print String (Full Name)
         Count Print Symbol (')')
      %finish
      Count Print Symbol (NL)
      
      Count Print String ("Origin: ")
      Count Print String (Filestore Name (RDTE))
      Count Print String ("!Fred")
      Count Print String (Hex2 (LDTE))
      Count Print Symbol ('!')
      Count Print String NL (User Name)
   
      Count Print String ("Date: ")
      Count Print String (Date)        { Note that Date and    }
      Count Print String (" at ")
      Count Print String NL (Time)     { Time come from News Host }
      
      Select Input (2)
      %begin
         %on 9 %start
            Done = Yes
            -> Eof
         %finish

         Line1 = ""
         Line2 = ""
         Line3 = ""
         Read Line (Line)
         Read Line (Line) %if Line = Tostring (12)     { Ignore initial ^L }
         %if Line -> ("From:" . Tostring(9)) %start    { Looks like VAX mail }
            Line1 = Line
            Read Line (Line)
            %if Line -> ("To:" . Tostring(9)) %start
               Line2 = Line
               Read Line (Line)
               %if Line -> ("Subj:" . Tostring(9)) . Line %start
                  Line1 = Tostring (6) . "(Forwarded VAX Mail)" . %c
                          Tostring (14) . Tostring (NL) . Tostring (NL) . Line1
               %else
                  Line3 = Line
                  Line = Tostring (6) . "(Forwarded VAX Mail)" . Tostring (14)
               %finish
            %else
               Line3 = Line
               Line = Line1
               Line1 = Line3
               Line3 = ""
            %finish
         %finish
            
         Line = "Subject: " . Line %unless line -> ("Subject:")
         Count Print String NL (Line)
         Read Line (Line)
         %if Line -> ("In-reply-to:") %start
            Count Print String NL (Line)
            Read Line (Line)
         %finish
         Count Print Symbol ('(')
         Count Print String (Itos (Lines, 0))
         %if lines = 1 %then Count Print String (" line)") %c
                       %else Count Print String (" lines)")
         Count Print Symbol (NL)
         Count Print Symbol (NL)
   
         Count Print String NL (Line1) %unless Line1 = ""
         Count Print String NL (Line2) %unless Line2 = ""
         Count Print String NL (Line3) %unless Line3 = ""

         Read Line (Line) %if Line = ""
         %cycle
            Count Print String NL (Line)
            Read Line (Line)
            %exit %if Line = Tostring (12)
         %repeat
      Eof:
      %end
   %end {Post Message}
   
   {DO ONE}
   %routine Do One

      Len = 0
      Select Input (2)
      Lines = Count Lines
      %if Lines = 0 %start
         Position = Position + Len
      %else
         Done = No
         Select Input (2)
         Set Input (Position)
         Len = 0
         Post Message
         Position = Position + Len
         Append Signature
         Log Message (Size)
      %finish
   %end {Do One}

   {MAIN CODE OF POST}

   Select Host (News Host)
   %if %not Open News File (Topic, 1) %start
      Select Host (Home Host)
      Select Output (0)
      Newline
      Print String ("It's hopeless. Try to post the file ")
      Save File = File
      %if Charno (File, 1) = '$' %start
         Save File = Topic.".MSG"
         Charno (Save File, 0) = 12 %if Length (Save File) > 12
         %if Exists (Save File) %start
            Save File = File
         %else
            Rename (File, Save File)
         %finish
      %finish
      Print String (Save File." again later.")
      Newline
      Newline
      %false
   %finish
                  
   Select Output (News Stream)
   Set Output (File Length)

   Select Host (Home Host)
   Open Input (2, File)
   Position = 0

   Done = No
   %cycle
      Do One
   %repeat %until Done = Yes

   Select Input (2)
   Select Host (Home Host)
   Close Input
   Close News File
   %true
%end {Post}
