#!/bin/sh
#
# aflib.inc
#
# Biblioteca de funciones para attach-filter
#

# miembrode() <clase> <usuario>
# Esta funcion dice si <usuario> esta en <clase>

function miembrode() {
 if egrep -q "^$2\$" ${AF_CFG}/${1}-usuarios &> /dev/null; then
    return 0
 else
    return 1
 fi
}

################################
# crea_temp()
#
# Crea un archivo temporal

crea_temp() {

# Directorio temporal
local RETFILE=`mktemp ${TMPDIR}/af.XXXXXX`
if [ $? -gt 0 ]; then
   error "No puedo crear un archivo temporal en ${TMPDIR}"
fi

echo $RETFILE

}

######################################
# error()
#
# Sale con un codigo de error opcional

error() {

   if [[ "$2" =~ [[:digit:]] ]]; then
      retval="$2"
   else
      # Por default un error temporal
      retval="111"
   fi

   echo "$1" >&2
   exit $retval

}

######################################
# msg_subst()
#
# Genera en stdin un mensaje que sustituye
# a un mensaje eliminado por politicas.
#
# msg_subst fecha remitente destinatario subject

function msg_subst() {
   local msg_date
   local msg_from
   local msg_to
   local msg_subject

   msg_date="$1"
   msg_from="$2"
   msg_to="$3"
   msg_subject="$4"
   razon="$5"


         cat << EOF
From: postmaster@$(hostname -f)
$msg_date
To: $msg_to
Subject: Mensaje eliminado por politicas
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit

El mensaje ha sido eliminado por el filtro de politicas en $(hostname -f).

   Remitente: $msg_from
   Asunto:    $msg_subject
   Razon:     $razon


EOF
 
}


##### Funciones de conveniencia para obtener datos del mensaje #####
function msg_subject() {
   $REFORMAIL -x "Subject:"      < $MSGFILE
}

function msg_date() {
   $REFORMAIL -X "Date:"         < $MSGFILE
}

function msg_to() {
   $REFORMAIL -x "To:"           < $MSGFILE
}

function msg_from() {
   $REFORMAIL -x "From:"         < $MSGFILE
}

####################################################################
