The check_mail function requires a temporary directory to unpack the parts of the mail into. It is the responsibility of the MTA-specific code to maintain this directory - it is suggested to create one directory per child process and reuse it for each message, as creating and removing directories is a slow operation. You can store the path to the temporary directory in a property of your persistent object. If the preserve_evidence return value of check_mail is 1, then you should leave (not delete) the existing temporary directory and create a new one. This can be done by setting the persistent property to undef immediately, ensuring that the directory will not be deleted if the process terminates, and leaving it until the next message to create the directory since one does not exist already.
You must ensure that your temporary directory has a name which will differ both from other child processes running at the same time and from other directories created by your own process (or even an earlier child with the same PID). A suggested way of achieving this is to include the time and the PID in the temporary directory name: the standard format is amavis- where DATE-PIDDATE is in ISO 8601 format. Amavis::rfc2821_2822_Tools::iso8601_timestamp can be used to create this. The directory should be a subdirectory of the $TEMPBASE directory.
After choosing the directory name, you must ensure that the directory exists and is a directory (it is best to check this before calling check_mail for every message). If it is not a directory, you should return a temporary failure. It is a good idea to check that the device or inode number of the directory remain the same, and if not to report this in the log. If it does not exist, you should create the directory with mode 0750 and record its device and inode number.
Your function may need to create a file to hold the text of the email. This should be a file called email.txt in the temporary directory. Creating it is done after creating the temporary directory, and in a similar way. If the device/inode numbers have changed this is not as serious a problem as for the directory, as the file may have been replaced by some other tool. In this case you should delete and re-create the file.
The temporary directory should be cleaned after calling check_mail, unless evidence is to be preserved. This can be done simply by calling . This deletes all files in the Amavis::Util::strip_tempdir(dir)parts subdirectory (creating this is handled for you by check_mail) and ensures that the temporary directory holds nothing other than a parts subdirectory and file email.txt.
If $can_truncate is 0, you will also need to close and delete the email.txt file.