# @(#) pm-jasrv-daemon.rc -- server request check, subroutine for File Server
# @(#) $Id: pm-jasrv-daemon.rc,v 2.0 2001/07/27 22:45:50 jaalto Exp $
#
#   File id
#
#	.Copyright (C)   1998-2001 Jari Aalto <jari.aalto@poboxes.com>
#	.$Contactid:     jari.aalto@poboxes.com $
#	.$Created:       1998-01 $
#	.$Keywords:      procmail, file server $
#
#       This code is free software in terms of GNU Gen. pub. Lic. v2 or later
#	Refer to http://www.gnu.org/copyleft/gpl.html
#
#   Description
#
#       This subroutine is part of the MPFS file server.
#       Handle BOUNCES to mail server messages, eg if delivery failed
#       due to maximum byte limit.
#
#           552 <foo@site.com>... Message is too large; 100000 bytes max
#
#   Input
#
#       (none) This recipe examines headers and body to see if it's daemon
#       bounce.
#
#   Output
#
#       o   stat, set to "daemon" if message was handled.
#
#   Change Log (none)

# ............................................................ &init ...

id    = "pm-jasrv-daemon.rc"
dummy = "
========================================================================
$id: init: Check if this is bounced server message.
"

stat    = ""
msg     = ""
subject = ""
to      = ""


#   Hm, We wouldn't even need the FROM_DAEMON test, because
#   X-Loop is supposed to be unique. Because we expect to see bounce
#   message the original message is included in the body, that's by "B ??"
#   search.
#
#   Handle only some daemon messages, notify the recipient about an error.

dummy = "$id: The pm-javar.rc daemon status is: $from_daemon"

:0
*$ $from_daemon
*$ B ?? ^X-Loop: $JA_SRV_XLOOP
{

    :0 B
    * ^Subject:\/.*
    {
        subject = $MATCH;
    }

    :0 B
    * ^To:\/.*
    {
        to = $MATCH
    }

    :0 B
    *$ ()\/^$d+ .*Message is too large.*
    {
        msg = $MATCH
    }

    :0
    * msg ?? [a-z]
    {
        stat = "daemon";

        #   Add new header telling that this bouce was auto-notified back
        #   to original reqeastor.

        :0 fhw
        | $FORMAIL -I "X-Mpfs-daemon: [notified $to] $msg"


        #   make sure To has "@" character, otherwise picking of
        #   "To" from the body failed.

        :0 wc
        * to ?? @
        |  ( $FORMAIL                                                       \
             -rtk -p "    >"                                                \
             -I "To: $to"                                                   \
             -I "X-Loop: $JA_SRV_XLOOP "                                    \
             -I "Subject: $msg"                                             \
             ;                                                              \
echo "Your account didn't accept the requested file. " ;                    \
echo "Get server help file with 'send help' and see conversion types etc."; \
echo "Or notify the File Server admin";                                     \
            ) | $SENDMAIL;                                                  \
            echo "    [ja-srv; daemon $to; $msg;]"  >> $JA_SRV_LOG;

    }

}

dummy = "$id: end: STATUS = $stat"


# End of pm-jasrv-daemon.rc
