Initial version of backup-manager.
[clinton/scripts.git] / import-list
1 #!/bin/bash
2 #
3 # Import a mailing list from a user.
4 #
5 # This must be run as root.
6
7 MMDIR=/var/lib/mailman
8 ARCHIVEDIR=$MMDIR/archives
9 BINDIR=$MMDIR/bin
10 LISTDIR=$MMDIR/lists
11 WORKDIR=/tmp/mmdir
12
13 function usage() {
14 echo >&2 "Usage: import-list [--new] [--archive MBOX-FILE]" \
15 "[--conf CONF-FILE]"
16 echo >&2 " [--domain DOMAIN] [--url WEB-PAGE-URL] LIST"
17 }
18
19 function 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
39 if test "$UID" != "0"; then
40 echo "This program must be run as root"
41 usage
42 exit 1
43 fi
44
45 # Argument processing
46 ARCH=
47 CONF=
48 DOM=
49 NEW=n
50 URL=
51
52 while 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
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
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
82 done
83
84 if test -z "$1"; then
85 echo "Not enough arguments"
86 usage
87 exit 1
88 elif test -n "$2"; then
89 echo "Too many arguments"
90 usage
91 exit 1
92 else
93 LIST=$1
94 fi
95
96 # Create list if --new passed
97 if test "$NEW" = "y"; then
98 newlist $LIST
99 fi
100
101 # Check to see whether list exists yet
102 if test ! -d $LISTDIR/$LIST; then
103 echo "List $LIST does not exist yet. Please make it using the" \
104 "web interface."
105 exit 1
106 fi
107
108 # Make working directory
109 if test ! -d $WORKDIR; then
110 mkdir -p $WORKDIR
111 chown list:list $WORKDIR
112 chmod ug=rwX,o= $WORKDIR
113 fi
114
115 # Import configuration
116 if 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
123 fi
124
125 # Change domain for accepting email
126 if 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
133 fi
134
135 # Change URL for web interface
136 if test -n "$URL"; then
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
143 fi
144
145 # Import list archives
146 if 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
156 fi
157
158 echo "Reloading mailman ..."
159 /etc/init.d/mailman reload
160 echo
161
162 if test -n "$CONF" || test -n "$DOM"; then
163 echo "Updating mailman definitions for Exim ..."
164 /afs/hcoop.net/common/etc/scripts/mailman-update-exim-db
165 echo
166 fi
167
168 echo "Sucessfully imported list $LIST"