;NEWSEDIT.INC Edits posts/replies in Offline.was.

;*****************************************************************************
;*                                                                           *
;* NEWSEDIT.INC                                                              *
;* Copyright (C) 1996 Datastorm Technologies, Inc.                           *
;* All rights reserved.                                                      *
;*                                                                           *
;* This script allows the user to configure reply options for OFFLINE.WAX.   *
;*                                                                           *
;* This ASPECT SCRIPT is intended only as a sample of ASPECT programming.    *
;* DATASTORM makes no warranty of any kind, express or implied, including    *
;* without limitation, any warranties of merchantability and/or fitness      *
;* for a particular purpose.  Use of this program is at your own risk.       *
;*****************************************************************************



;****************************************************************************
;*     GLOBAL VARIABLES                                                     *
;****************************************************************************

string EditChoice
string SubjectEdit
string ContentFile



proc NewsEdit

;*****************************************************************************
;*                                                                           *
;* NEWSEDIT                                                                  *
;* This procedure edits posts/replies created with OFFLINE.WAS.              *
;*                                                                           *
;* Calls:                                                                    *
;* Called by: Main                                                           *
;*                                                                           *
;*****************************************************************************

   string MailFiles                    ;Mail replies.
   string NewsFiles                    ;News posts.
   string ReplyFiles                   ;News replies.
   integer Event                       ;Holds latest dialog event.
   integer MainEdit                    ;Main loop for this procedure.
   integer DlgID                       ;ID of the main dialog.
   integer Editing                     ;Loop for events in main dialog.
   integer FoundMail                   ;Result of FINDFIRST command.
   integer FoundNews                   ;Result of FINDFIRST command.
   integer FoundReply                  ;Result of FINDFIRST command.
   
   strfmt MailFiles "%s`\MAIL*.*" NewsOut
   strfmt NewsFiles "%s`\POST.*" NewsOut
   strfmt ReplyFiles "%s`\RPLY*.*" NewsOut
   
   MainEdit = 1                        ;Initialize main loop.
   While MainEdit                      ;Start main loop.
   
      if findfirst MailFiles           ;Check for presence of mail replies.
         FoundMail = 1
      else 
         FoundMail = 0
      endif
      
      if findfirst NewsFiles           ;Check for presence of new news posts.
         FoundNews = 1
      else
         FoundNews = 0
      endif
      
      if findfirst ReplyFiles          ;Check for presence of news replies.
         FoundReply = 1
      else
         FoundReply = 0
      endif
      
      dialogbox 61 183 161 89 84 7 "Select FileType"
         pushbutton 1 12 4 62 15 "New Post" DEFAULT
         pushbutton 2 13 24 63 14 "Author Reply"
         pushbutton 3 12 44 64 15 "Group Reply"
         pushbutton 4 12 64 64 14 "Exit" CANCEL
      enddialog
      
      if ! FoundNews                   ;Disable New Post option if no new posts
                                       ; were found.
         disable DLGCTRL 61 1
      endif
      
      if ! FoundMail                   ;Disable Author Reply option if no mail
                                       ; replies were found.
         disable DLGCTRL 61 2
      endif
      
      if ! FoundReply                  ;Disable Group Reply option if no news
                                       ; replies were found.
         disable DLGCTRL 61 3
      endif
      
      dlgwin 61 DlgID                  ;Get the Windows ID of the main dialog.
      Editing = 1                      ;Initialize DLGEVENT loop.
      While Editing                    ;Start DLGEVENT loop.
         dlgevent 61 Event             ;Pull latest value from DLGEVENT queue.
         switch Event                  ;See which button was selected.                  
            case 1                     ;If New Post,
               Editing = 0
               dlgdestroy 61 CANCEL    ;Exit this loop,
               ChooseArticle(NewsFiles, 1)
                                       ;Tell ChooseArticle which file type to
                                       ; look for.
            endcase
            case 2
               Editing = 0
               dlgdestroy 61 CANCEL    ;Exit this loop,
               ChooseArticle(MailFiles, 2)
                                       ;Tell ChooseArticle which file type to
                                       ; look for.
            endcase
            case 3
               Editing = 0
               dlgdestroy 61 CANCEL    ;Exit this loop,
               ChooseArticle(ReplyFiles, 3)
                                       ;Tell ChooseArticle which file type to 
                                       ; look for.
            endcase
            case 4
               dlgdestroy 61 CANCEL    ;Destroy the main dialog.
               Editing = 0             ;Exit this loop.
               MainEdit = 0            ;Exit this procedure's main loop.
            endcase
            default                    ;If no button was chosen but an event 
                                       ; still registers,
               winexists DlgID         ;See if the dialog box still exists.
               if FAILURE              ;If the main dialog is gone,
                  dlgdestroy 61 CANCEL ;Exit back to OFFLINE.WAS.
                  Editing = 0
                  MainEdit = 0
               endif
            endcase
         endswitch
      endwhile
   endwhile
   delfile ContentFile
endproc
      
      
proc ChooseArticle

;*****************************************************************************
;*                                                                           *
;* CHOOSEARTICLE                                                             *
;* This procedure allows the user to choose which article is to be edited.   *
;*                                                                           *
;* Calls:                                                                    *
;* Called by: NewsEdit                                                       *
;*                                                                           *
;*****************************************************************************

param string ArticleType
param integer TypeChoice
   
   string Message                      ;Text field of message dialog.
   string Trash                        ;File to be deleted.
   integer Choice                      ;User choice in message dialog.
   integer ChoiceLoop                  ;Loop for list dialog.
   integer Event                       ;Event handle for list dialog.
   integer EventLoop                   ;DLGEVENT loop for list dialog.
   integer DlgID                       ;Windows ID of list dialog.
   integer RefreshLoop                 ;Loop for finding directory contents.
   integer SelectCount                 ;Number of selected items in list box.
   
   strfmt ContentFile "%s`\ARTICLES.LST" NewsOut
                                       ;Format the path to ContentFile.
   
   RefreshLoop = 1
   while RefreshLoop
      fopen FID_EDITLIST ContentFile CREATE
                                       ;Open ContentFile.
      while FAILURE
         Choice = 8
         strfmt Message "Unable to open file:`r`n`r`n%s" ContentFile
         sdlgmsgbox "File Error!" Message EXCLAMATION RETRYCANCEL Choice
         if Choice != 4
            CleanUp()
         endif
         fopen FID_EDITLIST ContentFile CREATE
      endwhile
   
      findfirst ArticleType            ;Grab the first filename.
      fputs FID_EDITLIST $FILENAME     ;Put the filename into ContentFile.
      while findnext                   ;Loop while there are more articles to
                                       ; be added to the list.
         fputs FID_EDITLIST $FILENAME  ;Put each article name in ContentFile.
      endwhile
   
      fclose FID_EDITLIST              ;Close ContentFile when the list is done.
      
      EditChoice = $NULLSTR
      ChoiceLoop = 1                   ;Initialize the dialog loop.
      while ChoiceLoop                 ;Start the dialog loop.
         dialogbox 101 162 125 175 131 7 "Please Choose File to Edit:"
            flistbox 1 10 9 153 96 ContentFile single EditChoice sort
            pushbutton 2 17 112 40 14 "Edit" default
            pushbutton 3 113 112 40 14 "Done"
            pushbutton 4 66 112 40 14 "Delete"
         enddialog
         
         strlen EditChoice SelectCount
         if SelectCount == 0           ;Disable non-usable controls.
            disable DLGCTRL 101 2
            disable DLGCTRL 101 4
         else
            enable DLGCTRL 101 2
            enable DLGCTRL 101 3
         endif
         dlgupdate 101 2
         dlgupdate 101 4
         dlgwin 101 DlgID              ;Get the windows ID of the list dialog.
         EventLoop = 1                 ;Initialize DLGEVENT loop.
         while EventLoop               ;Start DLGEVENT loop.
            dlgevent 101 Event         ;Get event from queue.
            switch Event               ;See which control was selected.
               case 1                  ;Ignore listbox access.
                  strlen EditChoice SelectCount
                  if SelectCount == 0
                     disable DLGCTRL 101 2
                     disable DLGCTRL 101 4
                  else
                     enable DLGCTRL 101 2
                     enable DLGCTRL 101 4
                  endif
                  dlgupdate 101 2
                  dlgupdate 101 4
               endcase
               case 2                  ;If OK is selected,
                  dlgdestroy 101 CANCEL
                                       ;Destroy the list dialog.
                  EventLoop = 0        ;Exit the DLGEVENT loop.
                  if TypeChoice == 2   ;If working on mail replies,
                     EditMail()        ;Call the mail editor.
                  else                 ;If working on news articles,
                     EditNews(TypeChoice) 
                                       ;Call the news editor.
                  endif
               endcase
               case 3                  ;If Done is chosen,
                  dlgdestroy 101 CANCEL
                                       ;Destroy the list dialog.
                  ChoiceLoop = 0       ;Exit ChooseArticle.
                  EventLoop = 0
                  RefreshLoop = 0
               endcase
               case 4
                  strlen EditChoice SelectCount
                  if SelectCount
                     Choice = 8
                     strfmt Message "Are you sure you want to delete %s?"\
                     EditChoice
                     sdlgmsgbox "Delete File" Message STOP YESNO Choice
                     if Choice == 6
                        dlgdestroy 101 CANCEL	
                        EventLoop = 0
                        ChoiceLoop = 0
                        strfmt Trash "%s`\%s" NewsOut EditChoice
                        delfile Trash
                     endif
                  endif
               endcase
               default                 ;If a non-specified event occurs,
                  winexists DlgID      ;See if the list dialog still exists.
                  if FAILURE           ;If the list dialog is gone,
                     dlgdestroy 101 CANCEL
                                       ;Make sure the script knows it's gone.
                     ChoiceLoop = 0    ;Exit ChooseArticle.
                     EventLoop = 0
                     RefreshLoop = 0
                  endif
               endcase
            endswitch
         endwhile
      endwhile
   endwhile
endproc
  


proc EditNews

;*****************************************************************************
;*                                                                           *
;* EDITNEWS                                                                  *
;* This procedure edits news files created with OFFLINE.WAS.                 *
;*                                                                           *
;* Calls:                                                                    *
;* Called by: ChooseArticle                                                  *
;*                                                                           *
;*****************************************************************************

param integer TypeChoice
   
   string FromLine                     ;From: line for NNTP.
   string GroupsLine                   ;Newsgroups: line for NNTP.
   string ReplyToLine                  ;ReplyTo: line for NNTP.
   string Distribution                 ;Distribution: line for NNTP.
   string References                   ;References:line for NNTP.
   string ReadArticle                  ;Holds text read from files.
   string Message                      ;Text field in message dialogs.
   string NewsTemp                     ;Temporary edit file.
   string NewsFile                     ;Actual article for posting.
   integer Choice                      ;User choice in message dialogs.
   integer EditNews                    ;Main loop for this procedure.
   integer EditEvent                   ;Loop for dlgevent queue.
   integer Event                       ;Holds latest event from queue.
   integer DlgID                       ;Windows ID from dialog box.
   integer Continue                    ;Flag for continuing copy to edit file.
   integer StopCopy                    ;Flag for continuing copy to post file.
   
   strfmt NewsFile "%s`\%s" NewsOut EditChoice
                                       ;Format full path to posting file.
   strfmt NewsTemp "%s`\NEWS.TMP" NewsOut
                                       ;Format full path to edit file.
   
   fopen FID_REPLYFILE NewsFile READWRITE
                                       ;Open the source file.
   while FAILURE
      Choice = 8
      strfmt Message "Unable to open file:`r`n`r`n%s" NewsFile
      sdlgmsgbox "File Error!" Message EXCLAMATION RETRYCANCEL Choice
      if Choice != 4
         CleanUp()
      endif
      fopen FID_REPLYFILE NewsFile READWRITE
   endwhile
   
   fopen FID_BODYTEMP NewsTemp CREATE  ;Create the edit file.
   while FAILURE
      Choice = 8
      strfmt Message "Unable to open file:`r`n`r`n%s" NewsFile
      sdlgmsgbox "File Error!" Message EXCLAMATION RETRYCANCEL Choice
      if Choice != 4
         CleanUp()
      endif
      fopen FID_BODYTEMP NewsTemp CREATE
   endwhile
   
   fgets FID_REPLYFILE FromLine        ;Read the various headers from source.
   fgets FID_REPLYFILE GroupsLine
   fgets FID_REPLYFILE SubjectEdit
   fgets FID_REPLYFILE Distribution
   if TypeChoice == 3                  ;Grab the extra References: line if this
                                       ; is a 'group reply' file.
      fgets FID_REPLYFILE References
   endif
   fgets FID_REPLYFILE ReplyToLine
   fgets FID_REPLYFILE ReadArticle     ;This line is the space between the 
                                       ; headers and the body of the text.
   
   strncmp ReadArticle ".`r`n" 3 Continue
                                       ;Check for End of Text line.
   while Continue != 0                 ;While Not End of Text,
      fgets FID_REPLYFILE ReadArticle  ;Get a line from the post file,
      strncmp ReadArticle ".`r`n" 3 Continue
                                       ;See if it's the last line.
      if Continue != 0                 ;Copy the line if not last.
         fputs FID_BODYTEMP ReadArticle
      endif
   endwhile
   fclose FID_BODYTEMP                 ;Close both files when done copy.
   fclose FID_REPLYFILE   
   
   EditNews = 1
   while EditNews
      
      dialogbox 60 110 92 350 268 7 "PROCOMMM PLUS v3.0 Off-line News Reader\
 - Edit New Post"                      ;Draw dialog for editing edit file.
         text 6 2 3 270 8 FromLine LEFT
         text 7 2 14 270 8 GroupsLine LEFT
         text 8 2 25 290 8 SubjectEdit LEFT
         feditbox 9 4 51 341 185 NewsTemp
         pushbutton 10 232 246 40 14 "Cancel" CANCEL
         pushbutton 11 79 246 40 14 "Done"
      enddialog
      dlgwin 60 DlgID                  ;Get Windows ID of dialog box.
      sendkey CTRL 0x23                ;Start edit at end of text.
      EditEvent = 1                    ;Initialize event loop.
      while EditEvent                  ;Start event loop.
         dlgevent 60 Event             ;Pull event from queue.
         switch Event                  ;Find out which event was accessed.
            case 6
            case 7
            case 8
            case 9
            endcase
            case 10
               dlgdestroy 60 CANCEL    ;Exit this procedure on Cancel.
               delfile NewsTemp
               EditEvent = 0
               EditNews = 0
            endcase
            case 11                    ;On DONE, create a new post file with the
                                       ; the same file name, copy the headers 
                                       ; into this file, then copy the edited 
                                       ; text into this file.  The edit file is
                                       ; then deleted, and this procedure is
                                       ; exited.
               EditEvent = 0
               EditNews = 0
               dlgdestroy 60 OK
               fopen FID_REPLYFILE NewsFile CREATE
               while FAILURE
                  Choice = 8
                  strfmt Message "Unable to open file:`r`n`r`n%s" NewsFile
                  sdlgmsgbox "File Error!" Message EXCLAMATION RETRYCANCEL Choice
                  if Choice != 4
                     CleanUp()
                  endif
                  fopen FID_REPLYFILE Newsfile CREATE
               endwhile
               
               fopen FID_BODYTEMP NewsTemp READWRITE
               while FAILURE
                  Choice = 8
                  strfmt Message "Unable to open file:`r`n`r`n%s" NewsTemp
                  sdlgmsgbox "File Error!" Message EXCLAMATION RETRYCANCEL Choice
                  if Choice != 4
                     CleanUp()
                  endif
                  fopen FID_BODYTEMP NewsTemp READWRITE
               endwhile
               
               fputs FID_REPLYFILE FromLine
               fputs FID_REPLYFILE GroupsLine
               fputs FID_REPLYFILE SubjectEdit
               fputs FID_REPLYFILE Distribution
               if TypeChoice == 3
                  fputs FID_REPLYFILE References
               endif
               fputs FID_REPLYFILE ReplyToLine
               fputs FID_REPLYFILE "`r`n"
               
               feof FID_BODYTEMP StopCopy
               while ! StopCopy
                  fgets FID_BODYTEMP ReadArticle
                  fputs FID_REPLYFILE ReadArticle
                  feof FID_BODYTEMP StopCopy
               endwhile
               
               fputs FID_REPLYFILE "`r`n.`r`n"
                                       ;Insert End of Text line.
               
               fclose FID_REPLYFILE
               fclose FID_BODYTEMP
               delfile NewsTemp
            endcase
         endswitch
      endwhile
   endwhile
endproc
   
   
proc EditMail

;*****************************************************************************
;*                                                                           *
;* EDITMAIL                                                                  *
;* This procedure edits mail files created with OFFLINE.WAS.                 *
;*                                                                           *
;* Calls:                                                                    *
;* Called by: ChooseArticle                                                  *
;*                                                                           *
;*****************************************************************************

   string DateLine                     ;Date: header for SMTP.
   string FromLine                     ;From: header for SMTP. 
   string ToLine                       ;To: header for SMTP.
   string MailFile                     ;Full path to reply file.
   string ReadArticle                  ;String for text copy.
   string Message                      ;Text field in message dialogs.
   string MailTemp                     ;Temporary edit file.
   integer Choice                      ;User choice in message dialogs.
   integer EditMail                    ;Loop for main dialog box.
   integer EditEvent                   ;Loop for dlgevent queue.
   integer Event                       ;Holds latest dlgevent.
   integer DlgID                       ;Windows ID for dialog box.
   integer Continue                    ;Loop for creating edit file.
   integer StopCopy                    ;Loop for creating reply file.
   
   strfmt MailFile "%s`\%s" NewsOut EditChoice
                                       ;Format full path to reply file.
   strfmt MailTemp "%s`\MAIL.TMP" NewsOut
                                       ;Format full path to edit file.
   
   fopen FID_REPLYFILE MailFile READWRITE
                                       ;Open the reply file.
   while FAILURE
      Choice = 8
      strfmt Message "Unable to open file:`r`n`r`n%s" MailFile
      sdlgmsgbox "File Error!" Message EXCLAMATION RETRYCANCEL Choice
      if Choice != 4
         CleanUp()
      endif
      fopen FID_REPLYFILE MailFile READWRITE
   endwhile
   
   fopen FID_BODYTEMP MailTemp CREATE  ;Open the edit file.
   while FAILURE
      Choice = 8
      strfmt Message "Unable to open file:`r`n`r`n%s" MailFile
      sdlgmsgbox "File Error!" Message EXCLAMATION RETRYCANCEL Choice
      if Choice != 4
         CleanUp()
      endif
      fopen FID_BODYTEMP MailTemp CREATE
   endwhile
   
   fgets FID_REPLYFILE DateLine        ;Read the headers from the reply file.
   fgets FID_REPLYFILE FromLine
   fgets FID_REPLYFILE ToLine
   fgets FID_REPLYFILE SubjectEdit
   fgets FID_REPLYFILE ReadArticle     ;This line is the space between headers
                                       ; and the body of the text.
   
   strncmp ReadArticle ".`r`n" 3 Continue
                                       ;Check for End of Text.
   while Continue != 0                 ;While NOT End of Text,
      fgets FID_REPLYFILE ReadArticle  ;Read the next line from reply file.
      strncmp ReadArticle ".`r`n" 3 Continue
                                       ;Check for end of text.
      if Continue != 0                 ;If NOT End of Text,
         fputs FID_BODYTEMP ReadArticle
                                       ;Copy line to edit file.
      endif
   endwhile
   fclose FID_BODYTEMP                 ;Close both files when finished.
   fclose FID_REPLYFILE   
   
   EditMail = 1                        ;Initialize main loop.
   while EditMail                      ;Start main loop.
      
      dialogbox 60 110 92 350 268 7 "PROCOMMM PLUS v3.0 Off-line News Reader\
 - Edit New Post"                      ;Draw editing dialog.
         text 6 2 3 270 8 DateLine LEFT
         text 7 2 14 270 8 FromLine LEFT
         text 8 2 25 290 8 ToLine LEFT
         text 5 2 36 270 8 SubjectEdit LEFT
         feditbox 9 4 51 341 185 MailTemp
         pushbutton 10 232 246 40 14 "Cancel" CANCEL
         pushbutton 11 79 246 40 14 "Done"
      enddialog
      dlgwin 60 DlgID                  ;Get the windows ID of the dialog box.
      sendkey CTRL 0x23                ;Start editing at end of text.
      EditEvent = 1                    ;Initialize dlgevent loop.
      while EditEvent                  ;Start dlgevent loop.
         dlgevent 60 Event             ;Get next event from queue.
         switch Event                  ;See which control was accessed.
            case 6
            case 7
            case 8
            case 9
            endcase
            case 10                    ;Exit this procedure on Cancel.
               dlgdestroy 60 CANCEL    
               delfile MailTemp
               EditEvent = 0
               EditMail = 0
            endcase
            case 11                    ;When DONE is selected, create a new post
                                       ; file with the same name.  Copy all 
                                       ; headers into the new file, then copy
                                       ; all edited text into the new file.  The
                                       ; edit file can then be deleted, and this
                                       ; procedure exited.
               EditEvent = 0
               EditMail = 0
               dlgdestroy 60 OK
               fopen FID_REPLYFILE MailFile CREATE
               while FAILURE
                  Choice = 8
                  strfmt Message "Unable to open file:`r`n`r`n%s" MailFile
                  sdlgmsgbox "File Error!" Message EXCLAMATION RETRYCANCEL Choice
                  if Choice != 4
                     CleanUp()
                  endif
                  fopen FID_REPLYFILE MailFile CREATE
               endwhile
               
               fopen FID_BODYTEMP MailTemp READWRITE
               while FAILURE
                  Choice = 8
                  strfmt Message "Unable to open file:`r`n`r`n%s" MailTemp
                  sdlgmsgbox "File Error!" Message EXCLAMATION RETRYCANCEL Choice
                  if Choice != 4
                     CleanUp()
                  endif
                  fopen FID_BODYTEMP MailTemp READWRITE
               endwhile
               
               fputs FID_REPLYFILE DateLine
               fputs FID_REPLYFILE FromLine
               fputs FID_REPLYFILE ToLine
               fputs FID_REPLYFILE SubjectEdit
               
               feof FID_BODYTEMP StopCopy
               while ! StopCopy
                  fgets FID_BODYTEMP ReadArticle
                  fputs FID_REPLYFILE ReadArticle
                  feof FID_BODYTEMP StopCopy
               endwhile
               
               fputs FID_REPLYFILE "`r`n.`r`n" 
                                       ;Insert End of Text line.
               
               fclose FID_REPLYFILE
               fclose FID_BODYTEMP
               delfile MailTemp
            endcase
         endswitch
      endwhile
   endwhile
endproc

