Sync changes in current working tree
[clinton/scripts.git] / import-list
CommitLineData
a630b57a 1#!/bin/bash
2#
3# Import a mailing list from a user.
4#
5# This must be run as root.
6
7MMDIR=/var/lib/mailman
8ARCHIVEDIR=$MMDIR/archives
9BINDIR=$MMDIR/bin
10LISTDIR=$MMDIR/lists
11WORKDIR=/tmp/mmdir
12
13function usage() {
477bfaa1 14 echo >&2 "Usage: import-list [--new] [--archive MBOX-FILE]" \
15 "[--conf CONF-FILE]"
16 echo >&2 " [--domain DOMAIN] [--url WEB-PAGE-URL] LIST"
a630b57a 17}
18
19function newlist() {
20 if test -z "$1" || test -n "$2"; then
21 echo "Programmer error"
22 exit 1
23 fi
24
25 local LIST=$1
26
27 mkdir $MMDIR/lists/$LIST
28 chown list:list $MMDIR/lists/$LIST
29 chmod g+rwx $MMDIR/lists/$LIST
30
31 mkdir $MMDIR/archives/private/$LIST
32 chown list:list $MMDIR/archives/private/$LIST
33 chmod g+rwx $MMDIR/archives/private/$LIST
34
35 echo "Created list $LIST"
36 echo
37}
38
477bfaa1 39if test "$UID" != "0"; then
40 echo "This program must be run as root"
41 usage
42 exit 1
43fi
44
a630b57a 45# Argument processing
46ARCH=
47CONF=
477bfaa1 48DOM=
a630b57a 49NEW=n
50URL=
51
52while test "forever"; do
53 if test "$1" = "--archive"; then
54 shift
55 ARCH=$1
56 shift
57 elif test "$1" = "--conf"; then
58 shift
59 CONF=$1
60 shift
61 continue
477bfaa1 62 elif test "$1" = "--domain"; then
63 shift
64 DOM=$1
65 shift
66 continue
67 elif test "$1" = "--help"; then
68 usage
69 exit 0
a630b57a 70 elif test "$1" = "--new"; then
71 NEW=y
72 shift
73 continue
74 elif test "$1" = "--url"; then
75 shift
76 URL=$1
77 shift
78 continue
79 else
80 break
81 fi
82done
83
84if test -z "$1"; then
85 echo "Not enough arguments"
86 usage
87 exit 1
88elif test -n "$2"; then
89 echo "Too many arguments"
90 usage
91 exit 1
92else
93 LIST=$1
a630b57a 94fi
95
96# Create list if --new passed
97if test "$NEW" = "y"; then
98 newlist $LIST
99fi
100
101# Check to see whether list exists yet
102if test ! -d $LISTDIR/$LIST; then
103 echo "List $LIST does not exist yet. Please make it using the" \
104 "web interface."
105 exit 1
106fi
107
108# Make working directory
109if test ! -d $WORKDIR; then
110 mkdir -p $WORKDIR
111 chown list:list $WORKDIR
112 chmod ug=rwX,o= $WORKDIR
113fi
114
115# Import configuration
116if test -n "$CONF"; then
117 echo "Importing list configuration ..."
118 cp $CONF $WORKDIR/$LIST.cfg
119 chown list:list $WORKDIR/$LIST.cfg
120 chmod u=rw,g=rw,o= $WORKDIR/$LIST.cfg
121 mv $WORKDIR/$LIST.cfg $LISTDIR/$LIST/config.pck
122 echo
123fi
124
477bfaa1 125# Change domain for accepting email
126if test -n "$DOM"; then
127 echo "Changing email domain for $LIST to $DOM ..."
128 echo "host_name = '$DOM'" > $WORKDIR/$LIST.cfg
129 chown list:list $WORKDIR/$LIST.cfg
130 sudo -u list $BINDIR/config_list -i $WORKDIR/$LIST.cfg $LIST
131 rm -f $WORKDIR/$LIST.cfg
132 echo
133fi
134
a630b57a 135# Change URL for web interface
477bfaa1 136if test -n "$URL"; then
a630b57a 137 echo "Changing web page URL for $LIST to $URL ..."
138 echo "web_page_url = '$URL'" > $WORKDIR/$LIST.cfg
139 chown list:list $WORKDIR/$LIST.cfg
140 sudo -u list $BINDIR/config_list -i $WORKDIR/$LIST.cfg $LIST
141 rm -f $WORKDIR/$LIST.cfg
142 echo
143fi
144
145# Import list archives
146if test -n "$ARCH"; then
147 echo "Importing list archives ..."
148 cp $ARCH $WORKDIR/$LIST.mbox
149 rm -fr $ARCHIVEDIR/private/$LIST.mbox
150 mkdir $ARCHIVEDIR/private/$LIST.mbox
151 mv $WORKDIR/$LIST.mbox $ARCHIVEDIR/private/$LIST.mbox
152 chown -R list:list $ARCHIVEDIR/private/$LIST.mbox
153 chmod ug=rw,o=r $ARCHIVEDIR/private/$LIST.mbox/$LIST.mbox
154 sudo -u list $BINDIR/arch --quiet --wipe $LIST
155 echo
156fi
157
158echo "Reloading mailman ..."
159/etc/init.d/mailman reload
160echo
161
ab34cf27 162if test -n "$CONF" || test -n "$DOM"; then
a630b57a 163 echo "Updating mailman definitions for Exim ..."
164 /afs/hcoop.net/common/etc/scripts/mailman-update-exim-db
165 echo
166fi
167
168echo "Sucessfully imported list $LIST"