# we are called from spamfilter and the following cases could be:
#
# 1.  spamfilters are unsure
#     symptom:  $SPAM_UNSURE is set (sa-unsure)
#     reaction: file as unsure
#
# 2.  spamfilters disagree
#     symptom:  $SPAM_DISAGREE is set (sa-ham or sa-spam)
#     reaction: file as disagreed
#
# 3.  message is spam for sure
#     symptom:  $IS_SPAM is set
#     reaction: file as spam
#
# 4. message needs to be retrained as spam
#     symptom:  $RETRAIN is set to spam
#     reaction: retrain filters, report, and file as spam
#
# 5.  message needs to be retrained as ham
#     symptom:  $RETRAIN is set to ham
#     reaction: retrain crm114 and resubmit the message to the spamfilter
#
# 6.  spam degree is unknown:
#     symptom:  $SPAM_UNKNOWN contains the reason for skipping the checks
#     reaction: pass the message
#
# 7.  message is not spam
#     symptom:  none of the above
#     reaction: pass the message
#

:0
* SPAM_UNSURE ?? .
{
  LOG="spamfilter:  filing as unsure$NL"
  :0
  {
    DEST=retrain.unsure
    DELAY=$DELAY_TONIGHT
  }
}

:0 E
* SPAM_DISAGREE ?? .
{
  LOG="spamfilter:  filing as disagreed$NL"
  :0
  {
    DEST=retrain.disagree
    DELAY=$DELAY_TONIGHT
  }
}

:0 E
* RETRAIN ?? spam
{
  LOG="spamfilter:  (re)training as spam$NL"
  :0 fw
  |$TRAINER --spam 2>/dev/null
  IS_SPAM=trained
}

:0 E
* RETRAIN ?? ham
{
  LOG="spamfilter:  retraining as ham$NL"
  :0 fw
  |$TRAINER --ham 2>/dev/null
#  :0
#  |$TRAINER --ham 2>/dev/null | $PROCMAIL
}

# not using flag E because IS_SPAM could have been set further up
:0
* IS_SPAM ?? .
{
  LOG="spamfilter:  filing as spam$NL"
  :0
  $SPAM
}

:0 E
{ LOG="spamfilter:  allowed to pass$NL" }

#:0 E
#*$ $SPAM_UNKNOWN
#{ do nothing }

#:0 E
#{ do nothing}