;Host Mode configuration utility

;***********************************************************************
;*                                                                     *   
;* HOSTUTIL.WAS                                                        *
;* Copyright (C) 1992 Datastorm Technologies, Inc.                     *     
;* All rights reserverd.                                               * 
;*                                                                     * 
;* Purpose: A script for...                                            *
;*                                                                     * 
;*                                                                     * 
;***********************************************************************

;***********************************************************************
;*                                                                     *
;*  WARNING!!!!                                                        *
;*                                                                     *
;*  Do not modify this script file unless you have a good under-       *
;*  standing of the Windows ASPECT language.  If you do modify this    *
;*  script, PLEASE MAKE A BACKUP before doing so.                      *
;*                                                                     *
;***********************************************************************

;***********************************************************************
;*                                                                     *
;*   GLOBAL VARIABLES                                                  *
;*                                                                     *
;***********************************************************************

string tempfile, line, hdrfile, msgfile, msg_count, recvd, flag, msg
string destination, from, subject, _date, _time, deleted, tempfil2, tempfil1
string welcome_msg, upload_path, dnload_path, filename
integer msg_num ,msg_length, msg_flag, dummy, msg_total
integer blocksize, chars_to_read, msg_number
integer new_usr_level, connect_type, system_type, goodbye_option,status
long offset

;***********************************************************************
;*                                                                     *   
;* MAIN                                                                *
;*                                                                     *
;* This procedure is ths main program loop.                            *
;*                                                                     *
;*                                                                     * 
;* Calls: HOSTPRM_SAVE, MAIL_MAN                                       *
;*                                                                     *
;* Called by: NONE                                                     *
;*                                                                     *
;* Modifies globals: filename, welcome_msg, upload_path, dnload_path,  *
;*                   new_usr_level, connect_type, system_type,         *
;*                   goodbye_option, status                            *
;*                                                                     *
;***********************************************************************
proc main
   string junk;
   fetch aspect scriptpath filename
   addfilename filename "HOST.PRM"
   if isfile filename
      fopen 0 filename READWRITE TEXT
      if not success
         errormsg "Can't open HOST.PRM!"
         halt
      endif
      fgets 0 welcome_msg
      fgets 0 junk
      fgets 0 junk
      fgetc 0 new_usr_level
      fgetc 0 connect_type
      fgetc 0 system_type
      fgetc 0 goodbye_option
      fclose 0
   else
      welcome_msg="Welcome to RAYBAN UV Network for Windows Host!"
      fetch dnldpath upload_path
      strupr upload_path
      fetch upldpath dnload_path
      strupr dnload_path
      new_usr_level=2
      connect_type=1
      system_type=1
      goodbye_option=1
      hostprm_save()
   endif

   while 1
      dialogbox -33 -1 350 200 11 "Host Mode Utility"
         pushbutton 258 81 40 18 "Save" update default
         text 6 12 68 8 left "Welcome Message: "
         editbox 77 9 256 13 welcome_msg
;         text 13 33 62 8 left "Upload Directory:"
;         editbox 77 30 256 13 upload_path
;         text 4 55 69 8 left "Download Directory:"
;         editbox 77 52 256 13 dnload_path
         groupbox 12 81 90 43 "New User Level"
         radiobutton 22 94 68 10 "0 - Limited User" new_usr_level
         radiobutton 22 108 65 10 "1 - Normal User"  endgroup
         groupbox 133 81 83 43 "Connection Type"
         radiobutton 146 94 42 10 "Modem" connect_type
         radiobutton 146 108 42 10 "Direct"  endgroup
         groupbox 22 130 67 40 "System Type"
         radiobutton 33 141 32 10 "Open" system_type
         radiobutton 33 155 35 10 "Closed"  endgroup
         groupbox 127 131 96 38 "Goodbye Option"
         radiobutton 140 141 47 10 "Recycle" goodbye_option
         radiobutton 140 153 38 10 "Exit"  endgroup
         pushbutton 258 108 40 18 "Exit" cancel
         pushbutton 236 135 90 18 "Mail Manager" update
      enddialog
    
      status=$DIALOG
      if $CHILD
         disable CTRL 1
         disable CTRL 31
      endif
      while 1
         switch status
            case 1
               destroydlg
               halt
            endcase

            case 30
               hostprm_save()
               if $CHILD
                  destroydlg
                  exit
               endif
               status=0
               loopwhile
            endcase

            case 31
               destroydlg
               mail_man()
               exitwhile
            endcase

            case 50        ; radio button update
            case 51
            case 52
            case 53
               updatedlg 2
            endcase
         endswitch
         status=$DIALOG
      endwhile
   endwhile
endproc   

;***********************************************************************
;*                                                                     *   
;* HOSTPRMSAVE                                                         *
;*                                                                     *
;* This procedure saves the HOST.PRM file to disk.                     *
;*                                                                     * 
;* Calls: NONE                                                         *
;*                                                                     *
;* Called by: 2                                                        *
;*                                                                     *
;* Modifies globals: filename, welcome_msg, upload_path, dnload_path,  *
;*                   new_usr_level, connect_type, system_type,         *
;*                   goodbye_option                                    *
;*                                                                     *
;***********************************************************************
proc hostprm_save
   fopen 0 filename create TEXT
   if not success
      errormsg "Can't open HOST.PRM !"
      halt
   endif
   fputs 0 welcome_msg
   strupr upload_path
   fputs 0 upload_path
   strupr dnload_path
   fputs 0 dnload_path
   fputc 0 new_usr_level
   fputc 0 connect_type
   fputc 0 system_type
   fputc 0 goodbye_option
   fclose 0
endproc

;***********************************************************************
;*                                                                     *   
;* MAIL_MAN                                                            *
;*                                                                     *
;* This procedure allows the SYSOP to view, delete, and pack the       *
;* mail message database.                                              *
;*                                                                     * 
;* Calls: COUNTMSG, READMSG, DELETEMSG, PACK                           *
;*                                                                     *
;* Called by: 1                                                        *
;*                                                                     *
;* Modifies globals: hdrfile, msgfile, tempfile, tempfil1, tempfil2,   *
;*                   msg_total, msg_count, msg_number, msg, status,    *
;*                   destination, from, subject, recvd, deleted, dummy *
;*                                                                     *
;***********************************************************************
proc mail_man
integer number
string input

   fetch aspect scriptpath hdrfile
   addfilename hdrfile "HOST.HDR"
    
   fetch aspect scriptpath msgfile
   addfilename msgfile "HOST.MSG"

   fetch aspect scriptpath tempfile
   addfilename tempfile "~HOST.TMP"

   fetch aspect scriptpath tempfil1
   addfilename tempfil1 "~HOST1.TMP"

   fetch aspect scriptpath tempfil2
   addfilename tempfil2 "~HOST2.TMP"

   CountMsg(&msg_total)
   if msg_total==0
      errormsg "No messages!"
      return
   endif
   itoa msg_total msg_count
   msg_number=1

   dialogbox 0 0 342 214 11 "Host Mail Manager"
      pushbutton 289 6 40 14 "&Next" update default
      pushbutton 289 26 40 14 "&Goto" update
      pushbutton 289 46 40 14 "&Delete" update
      pushbutton 289 65 40 14 "&Pack" update
      pushbutton 289 86 40 14 "E&xit" cancel
      ftext 5 108 328 80 tempfile
      text  2 27 31 8 right "Msg #:"
      vtext 37 27 217 9 left msg
      text  17 44 16 8 right "To:"
      vtext 36 44 81 9 left destination
      text  11 62 23 8 right "From:"
      vtext 37 62 74 9 left from
      text  1 81 35 8 right "Subject:"
      vtext 39 81 146 9 left subject
      text  9 6 56 8 right "Total Messages:"
      vtext 68 6 46 9 left msg_count
      text  185 44 41 8 right "Received:"
      vtext 228 44 37 9 left recvd
      text  158 62 68 8 right "Marked for deletion:"
      vtext 228 62 26 9 left deleted
   enddialog

   status=$DIALOG
   status=30
   while status!=1
      switch status
         case 30
            ReadMsg()
         endcase
         case 31
            sdlginput "Goto message" "Enter message number" input
            if success
               atoi input number
               if number>msg_total
                  errormsg "Msg# %d is the last message!" msg_total
                  exitswitch
               endif
               if (number>0) && (number<32768)
                  msg_number=number
                  Readmsg()
               endif
            endif
         endcase
         case 32
            msg_number--
            sdlgmsgbox "Warning!" "Do you want to mark this message for deletion?" QUESTION YESNO dummy BEEP
            if dummy ==6
               DeleteMsg()     ;delete msg
               ReadMsg()
            endif
         endcase
         case 33
            pack()
            CountMsg(&msg_total)
            if msg_total==0
               usermsg "No messages!"
               destroydlg
               return
            endif
            itoa msg_total msg_count
            updatedlg -1
            Readmsg()
         endcase
      endswitch
      status=$DIALOG
   endwhile
endproc

;***********************************************************************
;*                                                                     *   
;* READMSG                                                             *
;*                                                                     *
;* This procedure opens ans displays a message in a FTEXT box.         *
;*                                                                     *
;*                                                                     * 
;* Calls: NONE                                                         *
;*                                                                     *
;* Called by: 4                                                        *
;*                                                                     *
;* Modifies globals: hdrfile, msg_number, msg_total, msg_num, offset,  *
;*                   msg_length, msg_flag, flag, recvd, deleted, from, *
;*                   destination, dummy, subject, _date, _time, msg,   *
;*                   msgfile, tempfile, chars_to_read, blocksize, line *
;*                                                                     *
;***********************************************************************
proc ReadMsg
long hdr_offset

   isfile hdrfile
   if not success
      usermsg "No mail messages!"
      destroydlg
      halt
   else
      fopen 0 hdrfile READWRITE
   endif

   if msg_number > msg_total
      msg_number=1
   endif
    
   hdr_offset=(msg_number-1)*128      ; goto a specific record in .HDR file
   fseek 0 hdr_offset 0
    
   fgeti 0 msg_num
   if FEOF 0
      msg_number=1
      return
   endif
   fgetl 0 offset
    
   fgeti 0 msg_length
   fgetc 0 msg_flag

   if (msg_flag & 1) ==1
      flag="Private"              ;remove new mail flag
   else
      flag="Public"
   endif
   if (msg_flag & 2) == 2
      recvd="No"
   else
      recvd="Yes"
   endif
   
   if (msg_flag & 4) == 4
      deleted="Yes"
   else
      deleted="No"
   endif
                    
   fread 0 destination 31 dummy
   fread 0 from 31 dummy
   fread 0 subject 37 dummy
   fread 0 _date 9 dummy
   fread 0 _time 11 dummy

   fclose 0

   strfmt msg "%d  (%s, sent %s at %s)`r`n" msg_num flag _date _time

   isfile msgfile
   if not success
      usermsg "No message file!"
      destroydlg
      halt
   else
      fopen 1 msgfile READWRITE text
      if not success
         usermsg "Can't open message file!"
         destroydlg
         halt
      endif
      fopen 2 tempfile create text
      if not success
         usermsg "Can't open temp file!"
         destroydlg
         halt
      endif

      fseek 1 offset 1

      chars_to_read = msg_length
      while chars_to_read > 0
         if chars_to_read > 128
            blocksize = 128
         else
            blockSize = chars_to_read
         endif
         fread 1 line blocksize dummy
         fwrite 2 line blocksize
         if not success
            errormsg "can't write to temp file!"
            halt
         endif
         chars_to_read = chars_to_read - blocksize
      endwhile

      fclose 1
      fclose 2
      updatedlg -1
      delfile tempfile
      inc msg_number
   endif
endproc

;***********************************************************************
;*                                                                     *   
;* COUNTMSG                                                            *
;*                                                                     *
;* This procedure count the message in the message database.           *
;*                                                                     *
;*                                                                     * 
;* Calls: NONE                                                         *
;*                                                                     *
;* Called by: 2                                                        *
;*                                                                     *
;* Modifies globals: hdrfile, dummy                                    *
;*                                                                     *
;***********************************************************************
proc CountMsg
intparm num
string msg_entry
num=0

   fopen 4 hdrfile READ
   if not success
      errormsg "FATAL ERROR - Can't open HOST.HDR file!"
      return
   endif
   while 1
      fread 4 msg_entry 128 dummy
      if FEOF 4
         fclose 4
         return
      endif
      inc num
   endwhile
endproc

;***********************************************************************
;*                                                                     *   
;* CHANGEFLAG                                                          *
;*                                                                     *
;* This procedure modifies the flag byte for a message.                *
;*                                                                     *
;*                                                                     * 
;* Calls: NONE                                                         *
;*                                                                     *
;* Called by: 1                                                        *
;*                                                                     *
;* Modifies globals: hdrfile, msg_number                               *
;*                                                                     *
;***********************************************************************
proc ChangeFlag
intparm flagbyte
long hdr_offset

   fopen 0 hdrfile WRITE
   hdr_offset=((msg_number-1)*128)+8
   fseek 0 hdr_offset 0
   fputc 0 flagbyte
   fclose 0
endproc

;***********************************************************************
;*                                                                     *   
;* DELETEMSG                                                           *
;*                                                                     *
;* This procedure marks a message for deletion by modifying the        *
;* flag byte.                                                          *
;*                                                                     * 
;* Calls: CHANGEFLAG                                                   *
;*                                                                     *
;* Called by: 1                                                        *
;*                                                                     *
;* Modifies globals: hdrfile, msg_number, msg_num, offset, msg_length, *
;*                   msg_flag, destination, dummy, from, subject,      *
;*                   _date, _time                                      *
;*                                                                     *
;***********************************************************************
proc DeleteMsg
long hdr_offset

   fopen 0 hdrfile READ
   if not success
      errormsg "FATAL ERROR - Can't open HOST.HDR"
      halt
   endif

   hdr_offset=(msg_number-1)*128
   fseek 0 hdr_offset 0

   fgeti 0 msg_num
   if FEOF 0
      fclose 0
      return
   endif
   fgetl 0 offset
   fgeti 0 msg_length
   fgetc 0 msg_flag
   fread 0 destination 31 dummy
   fread 0 from 31 dummy
   fread 0 subject 37 dummy
   fread 0 _date 9 dummy
   fread 0 _time 11 dummy
   fclose 0
   msg_flag=msg_flag | 4
   ChangeFlag(msg_flag)
endproc

;***********************************************************************
;*                                                                     *   
;* PACK                                                                *
;*                                                                     *
;* This procedure compresses the message database by removing mail     *
;* that is marked for deletion.                                        *
;*                                                                     * 
;* Calls:NONE                                                          *
;*                                                                     *
;* Called by: 1                                                        *
;*                                                                     *
;* Modifies globals: hdrfile, tempfil2, msg_num, msgfile, tempfil1,    *
;*                   msg_number, offset, msg_length, msg_flag, line    *
;*                   destination, dummy, from, subject, _date, _time,  *
;*                   blocksize                                         *
;*                                                                     *
;***********************************************************************
proc Pack
long newoffset
integer number=1
integer chars, counter=0

   isfile hdrfile
   if not success
      usermsg "No mail messages!"
      halt
   else
      fopen 0 hdrfile READ
      fopen 1 tempfil2 CREATE
   endif

   while 1
      fgeti 0 msg_num
      if FEOF 0
         fclose 0
         fclose 1
         delfile "HDR.BAK"
         rename hdrfile "HDR.BAK"
         rename tempfil2 hdrfile
         delfile "MSG.BAK"
         rename msgfile "MSG.BAK"
         rename tempfil1 msgfile
         msg_number=1
         usermsg "%d message(s) deleted!" counter
         return
      endif

      fgetl 0 offset
      fgeti 0 msg_length
      fgetc 0 msg_flag
      fread 0 destination 31 dummy
      fread 0 from 31 dummy
      fread 0 subject 37 dummy
      fread 0 _date 9 dummy
      fread 0 _time 11 dummy

      if (msg_flag & 4) == 4
         counter++
         loopwhile
      endif

      isfile msgfile
      if not success
         usermsg "No message file!"
         halt ;return
      endif
      fopen 2 msgfile READ text
      if not success
         usermsg "Can't open message file!"
         halt  ; return
      endif
      if isfile tempfil1
         fopen 3 tempfil1 WRITE text
         fseek 3 0 2
         ftell 3 newoffset
      else
         fopen 3 tempfil1 CREATE text
         newoffset=0
      endif
      if not success
         usermsg "Can't open temp file!"
         halt ;return
      endif

      fseek 2 offset 1
      chars = msg_length
      while chars != 0
         if chars > 128
            blocksize = 128
         else
            blockSize = chars
         endif
         fread 2 line blocksize dummy
         fwrite 3 line blocksize
         if not success
            errormsg "can't write to temp file!"
            halt
         endif
         chars -= blocksize
      endwhile
  
      fclose 2
      fclose 3

      fputi 1 number
      fputl 1 newoffset
      fputi 1 msg_length
      fputc 1 msg_flag
      fwrite 1 destination 31
      fwrite 1 from 31
      fwrite 1 subject 37
      fwrite 1 _date 9
      fwrite 1 _time 11
      number++
   endwhile
endproc

;**** End of HOSTUTIL.WAS ****
