Automatically Archiving New MailAn example of a not-quite-completely generalized DCL procedure for automatically moving new mail to an archive, is given below:
$! Batch command procedure which
$!
$! 1. Checks newmail folder for VMS-WEB-daemon messages.
$! 2. Writes a temporary command file to move that mail
$! to the appropriate archive folder.
$! 3. Resets the protection on the archive.
$! 4. Resubmits itself after 1 hr during business hours
$! (6AM to 6PM), after 6 hrs otherwise.
$!
$! This procedure is based on WATCH_MAIL.COM, by Dan Schullman,
$! Digital Equipment Corporation, Maynard Massachusetts, 1994.
$! It is not yet generalized to account for month or year.
$!
$ SHOW TIME
$! Define or initialize various variables for processing the mail file.
$ COMMAND_FILE = "MOVE_NEWMAIL_TO_ARCHIVE.COM"
$ NEWMAIL_COUNT = 0 !number of new mail message
$ NEWMAIL_FOLDER = "NEWMAIL" !name of folder containing new mail
$ REQUIRED_FROM = "VMS-WEB-daemon"!sender's address must include this
$ MOVE_COUNT = 0 !number of messages to move
$ TARGET_MAILFILE = "[JONATHAN.WEBDAEMON.1997]MAIL.MAI"
$ TARGET_FOLDER = "July" !folder name where newmail is placed
$
$! Process the defalt mail file by reading it directly.
$ WRITE SYS$OUTPUT "Opening mail index..."
$ OPEN/READ/SHARE=WRITE/ERROR=ERROR_OPENING_MAIL_FILE MAILFILE_INDEX -
[JONATHAN]MAIL.MAI
$ READ/NOLOCK/INDEX=1/ERROR=NO_NEWMAIL/KEY="''NEWMAIL_FOLDER'" -
MAILFILE_INDEX LINE !will get error if no new mail
$!
$! It appears that at least one message is present in the new mail folder.
$! Create the header for the temporary command file which will be used to
$! move new mail to the desired folder.
$!
$ WRITE SYS$OUTPUT "New mail found. Open command file."
$ OPEN/WRITE TEMP_COMMAND_FILE 'COMMAND_FILE'
$! WRITE TEMP_COMMAND_FILE "$ DEFINE/USER SYS$OUTPUT _NLA0:"
$ WRITE TEMP_COMMAND_FILE "$ MAIL"
$ WRITE TEMP_COMMAND_FILE "SELECT/NEW"
$!
$! Define names for the fields within the mail file records, and a list of
$! the fields that must be present in every record.
$!
$ MF_0 = "SENDER"
$ MF_1 = "SENT_TO"
$ MF_2 = "SUBJECT"
$ MF_3 = "CC_TO"
$ MF_4 = "TYPE_4"
$ MF_5 = "REC_CNT"
$ MF_6 = "TYPE_6"
$ MF_7 = "TYPE_7"
$ MFP_REQUIRED = "0,2"
$!
$ GOTO GET_RECORD_INFO
$!
$ERROR_OPENING_MAIL_FILE:
$ STATUS = $STATUS !save failure status
$ ERROR_MESSAGE = "E-MAILOPEN, error opening mail file MAIL.MAI -- """ + F$MESSAGE(STATUS) + """"
$ WRITE SYS$OUTPUT ERROR_MESSAGE
$ EXIT
$!
$! Read next record. If the folder name is not that of the new mail folder,
$! assume we're done (since the records are in order by folder name).
$! Obtain the sender's and recipient's addresses, subject, etc.
$GET_NEXT_RECORD:
$ READ/NOLOCK/INDEX=1/END=END_OF_NEWMAIL MAILFILE_INDEX LINE
$GET_RECORD_INFO:
$ FOLDER = "''F$EXTRACT(9,F$CVUI(8*8,8,LINE),LINE)'"
$ IF FOLDER .NES. NEWMAIL_FOLDER THEN GOTO END_OF_NEWMAIL
$ NEWMAIL_COUNT = 'F$STRING( NEWMAIL_COUNT + 1 )'
$ LEN = -4
$ POS = 64
$ GOTO MRF_ON
$MRF_BAD:
$ STATUS = $STATUS
$ ERROR = F$MESSAGE(STATUS)
$ IF ERROR - "-NOCOMD," .EQS. ERROR THEN EXIT
$ PROBLEM = "Error (''STATUS') extracting field at byte ''POS'" -
+ " from ''NEWMAIL_FOLDER' record #''NEWMAIL_COUNT'"
$ WRITE SYS$OUTPUT PROBLEM
$MRF_ON:
$ ON WARNING THEN GOTO MRF_BAD
$MRF:
$ POS = 'F$STRING(POS+4+LEN)'
$ IF 'F$STRING(POS+4)' .GT. 'F$LENGTH(LINE)' THEN GOTO MRF_END
$ LEN = 'F$CVUI(POS*8+16,16,LINE)'
$ &MF_'F$CVUI(POS*8,16,LINE)' = F$EXTRACT(POS+4,LEN,LINE)
$ MFP_'F$CVUI(POS*8,16,LINE)' = 'NEWMAIL_COUNT'
$ GOTO MRF
$MRF_END:
$ ON WARNING THEN EXIT
$ LLINE = "From: <" + SENDER + ">"
$ SHOW SYMBOL LLINE
$! LLINE = " <" + SUBJECT + ">"
$! SHOW SYMBOL LLINE
$!
$! Skip this message if it doesn't contain REQUIRED_FROM string.
$ IF F$LOCATE(REQUIRED_FROM, SENDER) .EQ. F$LENGTH(SENDER) THEN GOTO GET_NEXT_RECORD
$!
$! Generate commands which will subsequently be used to move the message.
$ WRITE TEMP_COMMAND_FILE "READ ''NEWMAIL_COUNT'"
$ WRITE TEMP_COMMAND_FILE "MOVE/NOCONFIRM ''TARGET_FOLDER' [.WEBDAEMON.1997]"
$ MOVE_COUNT = 'F$STRING( MOVE_COUNT + 1 )'
$ GOTO GET_NEXT_RECORD
$!
$! Done processing the new mail folder. Copy and move the mail as necessary.
$END_OF_NEWMAIL:
$ WRITE TEMP_COMMAND_FILE "EXIT"
$ WRITE TEMP_COMMAND_FILE "$!"
$ CLOSE TEMP_COMMAND_FILE
$ WRITE SYS$OUTPUT "Found ''NEWMAIL_COUNT' messages."
$ WRITE SYS$OUTPUT "About to move ''MOVE_COUNT' messages to archive..."
$NO_NEWMAIL:
$ ON WARNING THEN CONTINUE
$ CLOSE MAILFILE_INDEX
$ IF 'MOVE_COUNT' .GT. 0
$ THEN
$ @'COMMAND_FILE'
$ WRITE SYS$OUTPUT "Reset protection."
$ SET FILE/PROT=(G:RE,W:RE)/SINCE=1-JUL [.WEBDAEMON.1997]*.MAI
$ SET FILE/PROT=(G:RE,W:RE)/LOG [.WEBDAEMON.1997]MAIL.MAI
$ ENDIF
$!
$! Clean up a bit.
$ WRITE SYS$OUTPUT "Purge log and command files."
$ PURGE/KEEP=10 'COMMAND_FILE'
$ PURGE/KEEP=10 *.log
$!
$! Resubmit this job after an hour, if during business hours.
$ DAY = F$EXTRACT(0,1,"''F$CVTIME(,,"WEEKDAY")'")
$ HOUR = F$EXTRACT(0,2,"''F$CVTIME(,,"HOUR")'")
$ IF (HOUR .GTS. "18") .OR. (HOUR .LTS. "06") .OR. (DAY .EQS. "S")
$ THEN
$ WRITE SYS$OUTPUT "Resubmit this job to run again in six hours."
$ SUBMIT/AFTER="+6"/NOPRINT 'F$ENVIRONMENT("PROCEDURE")'
$ ELSE
$ WRITE SYS$OUTPUT "Resubmit this job to run again in an hour."
$ SUBMIT/AFTER="+1"/NOPRINT 'F$ENVIRONMENT("PROCEDURE")'
$ ENDIF
$ EXIT
[Hypermail] [Installation] [Customization] [Typical Use] [Security]