1#!/bin/sh
2
3# Licensed to the Apache Software Foundation (ASF) under one
4# or more contributor license agreements.  See the NOTICE file
5# distributed with this work for additional information
6# regarding copyright ownership.  The ASF licenses this file
7# to you under the Apache License, Version 2.0 (the
8# "License"); you may not use this file except in compliance
9# with the License.  You may obtain a copy of the License at
10# http://www.apache.org/licenses/LICENSE-2.0
11#
12# Unless required by applicable law or agreed to in writing,
13# software distributed under the License is distributed on an
14# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15# KIND, either express or implied.  See the License for the
16# specific language governing permissions and limitations
17# under the License.
18
19
20
21setup_masscheck() {
22  [ ! -d "$WORKDIR/$TYPE/masses/spamassassin" ] && mkdir -p "$WORKDIR/$TYPE/masses/spamassassin"
23  cd "$WORKDIR/$TYPE/masses" || { echo "ERROR: cd $WORKDIR/$TYPE/masses failed" >&2; exit 1; }
24  rm -f spamassassin/*
25  echo "" > spamassassin/user_prefs # not used, local.cf works better for admin commands also
26  echo "bayes_auto_learn 0" > spamassassin/local.cf
27  echo "lock_method flock" >> spamassassin/local.cf
28  echo "score ANY_BOUNCE_MESSAGE 0" >> spamassassin/local.cf
29  echo "score BOUNCE_MESSAGE 0" >> spamassassin/local.cf
30  [ -n "${TRUSTED_NETWORKS}" -o -n "${INTERNAL_NETWORKS}" ] && \
31    echo "clear_trusted_networks
32clear_internal_networks" >> spamassassin/local.cf
33  [ -n "${TRUSTED_NETWORKS}" ] && echo "trusted_networks ${TRUSTED_NETWORKS}" >> spamassassin/local.cf
34  [ -n "${INTERNAL_NETWORKS}" ] && echo "internal_networks ${INTERNAL_NETWORKS}" >> spamassassin/local.cf
35  [ -n "${CUSTOM_PREFS}" ] && cat ${CUSTOM_PREFS} >> spamassassin/local.cf
36  rm -f "$WORKDIR/$TYPE/rules/99_custom.cf"
37  [ -n "${CUSTOM_RULES}" ] && cat ${CUSTOM_RULES} > "$WORKDIR/$TYPE/rules/99_custom.cf"
38}
39
40setup_checktype() {
41  [ ! -d "$WORKDIR" ] && mkdir "$WORKDIR"
42  DOW=$(date +%w)
43  if [ "$DOW" -ne 6 ] || [ "$1" = "--nightly" ]; then
44    # Run nightly_mass_check
45    TYPE=nightly_mass_check
46    echo "Syncing $TYPE"
47    rsync -qrz --delete rsync://rsync.spamassassin.org/tagged_builds/$TYPE/ "$WORKDIR/$TYPE/"
48    RC=$?
49    NET=
50    LOGTYPE=
51  else
52    # If Saturday, run the weekly_mass_check
53    TYPE=weekly_mass_check
54    echo "Syncing $TYPE"
55    rsync -qrz --delete rsync://rsync.spamassassin.org/tagged_builds/$TYPE/ "$WORKDIR/$TYPE/"
56    RC=$?
57    NET=--net
58    [ ! -z "$REUSE" ] && NET="$REUSE $NET"
59    LOGTYPE=net-
60  fi
61  if [ "$RC" -ne 0 ]; then
62    echo "ERROR: rsync failure $RC, aborting..." >&2
63    exit 1
64  else
65    SVNREV=$(awk '/Revision:/ {print $2}' "$WORKDIR/$TYPE/masses/svninfo.tmp")
66    [ ! -z "$SVNREV" ] && echo "SVN revision = $SVNREV"
67  fi
68}
69
70run_masscheck() {
71  CORPUSNAME=$1
72  shift
73  if [ "$CORPUSNAME" = "single-corpus" ]; then
74    # Use this if you have only a single corpus
75    LOGSUFFIX=
76  else
77    LOGSUFFIX="-${CORPUSNAME}"
78  fi
79  LOGFILE=${LOGTYPE}${LOGPREFIX}${LOGSUFFIX}.log
80  rm -f ham-${LOGFILE} spam-${LOGFILE}
81  set -x
82  $PERL ./mass-check --hamlog=ham-${LOGFILE} --spamlog=spam-${LOGFILE} \
83             -j $JOBS $NET --progress \
84             "$@"
85  LOGLIST="$LOGLIST ham-${LOGFILE} spam-${LOGFILE}"
86  set +x
87  ln -s ham-${LOGFILE} ham.log
88  ln -s spam-${LOGFILE} spam.log
89}
90
91upload_results() {
92  # Occasionally rsync server fails to respond on first attempt,
93  # so attempt upload a few times before giving up.
94  if [ -z "$RSYNC_PASSWORD" ] || [ "$RSYNC_PASSWORD" = "YOUR-PASSWORD" ]; then
95    return 0
96  fi
97  TRY=0
98  while [ "$TRY" -le 5 ]; do
99    TRY=$((TRY+1))
100    ARGS="-qPcvz $LOGLIST $RSYNC_USERNAME@rsync.spamassassin.org::corpus/"
101    echo "rsync $ARGS"
102    RSYNC_PASSWORD=$RSYNC_PASSWORD rsync $ARGS && break
103    sleep 5m
104  done
105}
106
107# Sanitize Environment
108unset LOGPREFIX
109unset RSYNC_USERNAME
110unset RSYNC_PASSWORD
111unset WORKDIR
112unset CHECKDIR
113unset REUSE
114
115# Configure
116if [ -e "$HOME/.automasscheck.cf" ]; then
117  . $HOME/.automasscheck.cf
118else
119  echo "ERROR: Configuration file expected at $HOME/.automasscheck.cf" >&2
120  echo "       See https://wiki.apache.org/spamassassin/NightlyMassCheck" >&2
121  exit 255
122fi
123
124# Run
125JOBS=${JOBS:=8}
126setup_checktype "$@"
127setup_masscheck
128unset LOGLIST
129run_all_masschecks
130upload_results
131exit 0
132