summaryrefslogtreecommitdiff
path: root/archives/bin/mk-mhonarc
blob: 8c28c6ab7a18ba5815019f602606259a18f0e413 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#!/bin/sh
#
# $Id$
# 
# Script to refresh the HTML archives from the Majordomo mboxes.
#

. $HOME/etc/archives.conf

export TZ=GMT
export LC_ALL=C

temp=""
heldlock=""
retval=1	# unless overridden later
# Need single quotes here to avoid expansion of variables until execution
trap 'rm -f $heldlock $REMOVE_FORCE_FILE $temp; exit $retval' \
	EXIT QUIT INT TERM HUP

lockfile -r 0 $LOCKFILE || {
        echo "Can't create mhonarc lockfile: $?" >&2
        exit 1
}
heldlock=$LOCKFILE

if [ ! -d $ARCHIVES_ROOT ]; then
	mkdir -p $ARCHIVES_ROOT || exit 1
fi
if [ ! -d $ARCHIVES_TMP ]; then
	mkdir -p $ARCHIVES_TMP || exit 1
fi
cd $ARCHIVES_ROOT

if [ -f $FORCE_FILE ]; then
	force=1
else
	force=0
fi

# $ARCHIVES_BIN/generate-list-descriptions

process_one_mbox() {
    local listdir path mbox ymon year month ydashm rcfile doit gzipped
    listdir=$1
    mbox=$2
    path=$listdir/$mbox
    ymon=${mbox##*.}		# extract yyyymm
    # Some lists have weekly archives, so we'd get something like 2009032 here
    # Split the week number for later
    if /usr/bin/test -l $ymon -gt 6; then
      submbox=${ymon##??????}
      ymon=${ymon%%?}
    else
      submbox=
    fi
    year=${ymon%%??}		# extract yyyy
    month=${ymon##????}		# extract mm
    if [ -z "$submbox" ]; then
      ydashm=${year}-${month}	# construct yyyy-mm
    else
      ydashm=${year}-${month}-${submbox} # yyyy-mm-s
    fi

    if [ "$ymon" -le 200607 ]; then
      rcfile=$ARCHIVES_BIN/resource-2006-07
    else
      rcfile=$ARCHIVES_BIN/resource-current
    fi

    if [ ! -d "$ydashm" ]; then
      mkdir $ydashm
    fi

    doit=0

    # In forced mode we clean the dir previous to running mhonarc, to cause it
    # to completely regenerate the archive for this month.  If we run it over
    # the existing directory, it will skip rewriting the message pages which is
    # not what we want.  In the normal case this is a good thing, because it
    # saves a lot of work.
    if [ $force -eq 1 -a ! -f $ydashm/forced_run_done ]; then
      doit=1
      rm -f $ydashm/*
    fi
    if [ $doit -eq 0 -a ! -f $ydashm/index.php ]; then
      doit=1
    fi
    if [ $doit -eq 0 -a $path -nt $ydashm/index.php ]; then
      doit=1
    fi
    if [ $doit -eq 1 ]; then
      temp=`mktemp -p $ARCHIVES_TMP`
      $MHONARC -add -quiet -rcfile $rcfile -outdir $ydashm $path \
	      -definevar LIST=$list \
	      -definevar YEAR=$year \
	      -definevar MONTH=$month \
	      > $temp 2>&1
      $ARCHIVES_BIN/msgid2link.pl $ydashm >> $temp 2>/dev/null
      $ARCHIVES_BIN/createmsgid.pl -i $ydashm -o $ARCHIVES_ROOT/msgid \
              >> $temp 2>/dev/null
      if [ -s $temp ]; then
	echo "START -- error processing $mbox"
	cat $temp
	echo -e "END   -- error processing $mbox\n\n"
      fi
      rm $temp
      temp=
      date > $ydashm/last-updated
      if [ ! -d "mbox" ]; then
        mkdir mbox
      fi
      gzipped=mbox/${list}.${ydashm}.gz
      if [ -f $path.gz ]; then
        cp $path.gz $gzipped
      else
        gzip $path -f -c > $gzipped
      fi
      chmod 755 $gzipped
      $ARCHIVES_BIN/splitmbox $gzipped
      if [ $force -eq 1 ]; then
	touch $ydashm/forced_run_done
      fi
    fi
    date > $ydashm/last-checked
}

for list in `ls $MAJORDOMO_FILES`; do
  cd $ARCHIVES_ROOT
  listdir=$MAJORDOMO_FILES/$list/files/public/archive

  if [ ! -d $listdir ]; then
    continue
  fi
  # If the list is present in the "blacklist file", do not archive it, and
  # send a complain by email, but throttle it so that it's not too obnoxious.
  if grep "$list" $ARCHIVES_BIN/blacklist >/dev/null 2>&1; then
    complainfile=${list}_complained
    if [ -f $complainfile ]; then
      # already complained, but remove file if it's too old.  This makes us
      # complain once every 12 hours
      age=`stat --format=%Y $complainfile`
      now=`date +%s`
      if [ $(($now - $age)) -gt $((60 * 60 * 12)) ]; then
	rm $complainfile
      fi
      continue
    fi
    # use a subscribed address so that it is not held for moderation
    echo "warning: sensitive list $list found on archives" | mail -s "sensitive list found" $DESTINATION_ADDRESS -- -f $SOURCE_ADDRESS
    touch $complainfile
    continue
  fi
  if [ ! -d "$list" ]; then
    mkdir $list
  fi
  cd $list

  for mbox in `ls --reverse $listdir`; do
    mbox=${mbox%%.gz}		# remove possible trailing .gz

    process_one_mbox $listdir $mbox
  done

  # XXX maybe this can be done better with mod_rewrite?
  # Ensure that index.php exists and is a symlink
  test -f index.php && rm index.php
  test ! -e index.php && ln -s ../list_index.php index.php

  # create the list of extant months
  $ARCHIVES_BIN/list_dates.pl $listdir $list > months

  date > last-updated
done

touch $ARCHIVES_ROOT/timestamp
date > $ARCHIVES_ROOT/last-updated

if [ $force -eq 1 ]; then
  find $ARCHIVES_ROOT -name forced_run_done -type f -delete
  REMOVE_FORCE_FILE=$FORCE_FILE
fi
retval=0