This appendix describes the simple API available to allow the embedding of MHonArc within other Perl programs.
Before calling any MHonarc routines, you must initialize the MHonArc library. The following code snippet shows you how to initialize MHonArc:
# Require MHonArc library require 'mhamain.pl'; # Initialize MHonArc mhonarc::initialize();
NOTE | The mhonarc::initialize() routine should only be called once within your program. |
NOTE | If mhamain.pl is not in perl's library search path, you will need to add the directory path to perl's search path before calling require. |
To instruct MHonArc to process input, use the following routine:
# Tell MHonArc to start processing mhonarc::process_input();
When mhonarc::process_input() is called with no arguments, it parses @ARGV for command-line arguments. If you pass a list of arguments into mhonarc::process_input() then that list will be processed for the command-line arguments. For example:
mhonarc::process_input( '-quiet', '-outdir', $archive_path, '-rcfile', $rcfile, $mailbox_filename );
The return value of mhonarc::process_input() will be the CPU time, in seconds, MHonArc used. Example usage:
$cpu_time = mhonarc::process_input();
To determine what the status of the processing was, you can query the $mhonarc::CODE variable. The value of this variable reflects what the exit status of MHonArc would be if invoked from the shell. I.e. If $mhonarc::CODE is equal to 0, then no errors occured during processing. A non-zero value indicates some error occured. Example usage:
mhonarc::process_input( '-quiet', '-outdir', $archive_path, '-rcfile', $rcfile, $mailbox_filename ); if ($mhonarc::CODE) { # error code here }
NOTE | If $mhonarc::CODE is equal to 75, this indicates that MHonArc was unable to obtain a lock on the archive. This exit code is recognized by MTAs like sendmail to requeue a message and try to deliver it again later. This is useful when MHonArc is invoked by a sendmail alias. |
It is okay to call mhonarc::process_input() multiple times within a single program. This is useful if your program wants to process multiple archives.
Support is available for registering callbacks to be invoked when MHonArc is processing input. To register a callback, all you need to do is set the appriopriate MHonArc variable to a routine reference (hard or symbolic). For example, to set the callback when a message header is read, you can do something like the following:
$mhonarc::CBMessageHeadRead = \&my_callback_routine;
NOTE | The mhasiteinit.pl site initialization library can be used to register callbacks. The advantages for using mhasiteinit.pl is that is executed each time MHonArc is executed, and you do not have to create custom front-ends to MHonArc if all you want to do is register callbacks. See Installation and the example mhasiteinit.pl provided in the examples/ directory of the MHonArc distribution for more information about mhasiteinit.pl. |
What follows is the type of callbacks supported by MHonArc:
The callback function after a mail message header is read and before any other processing is done. Note, the function is called after any exclusion checks are performed by MHonArc.
Synopsis:
$boolean = &$mhonarc::CBMessageHeadRead( $fields_hash_ref, $raw_header_txt);
Arguments:
Reference to hash containing parsed message header. Keys are the lowercase field names and the values are references to array contain the values for each field. If a field is only declared once in the header, the array will only contain one item.
The hash also contains special keys represented the values MHonArc has extracted when parsing the message header. The values of these keys are regular scalars and NOT array references. The following summarizes the keys made available:
The raw header data of the message. This data may be useful if pattern matches are desired against header data.
Return Value:
The return value is used by MHonArc to determine if the message should be excluded from any further processing. If the return value evaluates to true, then MHonArc will continue processing of the message. If the return value evaluates to false, the message will be excluded.
Notes:
To distinquish between SINGLE operation mode and archive operation mode, you can check the $mhonarc::SINGLE variable. For example:
if ($mhonarc::SINGLE) { # single message-based processing here } else { # archive-based processing here }
MHonArc resources exist that allow message exclusion capabilities: CHECKNOARCHIVE, EXPIREAGE, EXPIREDATE, and MSGEXCFILTER. If possible, use these resources to perform message exclusion filtering.
The callback function after a mail message body has been read a converted.
Synopsis:
&$mhonarc::CBMessageBodyRead( $fields_hash_ref, $html_text_ref, $files_array_ref);
Arguments:
Reference to hash containing parsed message header. The structure of this hash is the same as described for the $mhonarc::CBMessageHeadRead callback.
Reference to string contain the HTML markup for the body. Modifications to the referenced data will be reflected in the message page generated. Therefore, care should be observed when doing any modification.
If MHonArc was unable to convert the body of the message, the following expression will evaluate to true:
$$html_text_ref eq ""
If this is the case, you could set the value of $$html_text_ref to something else to customize the warning text MHonArc uses in the message page written.
Reference to array of derived files when the body was converted. Each file is typically relative to $mhonarc::OUTDIR, unless it is a full pathname. the mhonarc::OSis_absolute_path($filename) can be used to determine if a file is an absolute pathname or not. Note, it is possible that a file could designate a directory; this indicates that the directory, and all files in the directory, are derived.
Modifications to the array will affect the list of derived files MHonArc stores for the message. You can add files to the array if your routine creates files, but you can also delete items if your routine removes files; CAUTION: the HTML markup typically contains links to derived files so removing files could cause broken links unless $html_text_ref is modified to reflect the file deletions.
Return Value:
N/A
Notes:
To distinquish between SINGLE operation mode and archive operation mode, you can check the $mhonarc::SINGLE variable. For example:
if ($mhonarc::SINGLE) { # single message-based processing here } else { # archive-based processing here }
The $mhonarc::CBMessageBodyRead routine can be used to trigger automatic virus scanning of attachments.
This API documention is not complete. To get a better idea of what you may be able to do, have a look at the source code for the commands provided in the MHonArc distribution: mhonarc, mha-dbedit, mha-dbrecover, and mha-decode. You may also want to look at the source of mhamain.pl.
Only a single archive can be processed at any given time.