#!/bin/bash
# m7 - mailman helper
set -e
source "/home/zwelch/src/mcf/mcsh-release/install/share/mcsh/mcsh.sh"
######
# Configuration
m7_config_init() {
script_setting_vars -ro mm_dir mm_bindir
script_setting_vars mailman_host mailman_server mailman_user
}
m7_config_check() {
mm_bindir=${mm_bindir:-$mm_dir/bin}
mailman_server=${mailman_server:-$(server_name "$mailman_host")}
debug "mailman host: $mailman_host ($mailman_server)"
if [ "$host" != "$mailman_server" ]; then
warn "run this command on a mailman server (not '$host')"
fi
}
######
# Native dependencies
m7_server_packages=( mailman )
######
# Low-level helpers
run_mm_bin() {
local cmd=$1
shift
run_sudo_as_user $mailman_user $mm_bindir/$cmd "$@"
}
mm_change_pw() {
min_args 1 "$@"
local list=$1
shift
run_mm_bin change_pw "$@" -l "$list" |cut -d' ' -f4
}
mm_config() {
local opt=$1
local list=$2
shift 2
local file="$(list_config $list 'list.conf')"
run_mm_bin config_list $opt $file "$@" $list
}
######
# Alias file management
alias_exists() {
has_args 1 "$@"
grep "^$1: " $PREFIX/etc/aliases
}
alias_add() {
has_args 2 "$@"
local name tgt
name=$1
tgt=$2
if alias_exists $name; then
warn "$name: alias exists"
return
fi
local tmp
tmp=$(cmd_tempfile)
run cp /etc/aliases $tmp
app_echo "$name: adding alias..."
echo "$name: $tgt" >>$tmp
run_sudo mv $tmp $PREFIX/etc/aliases
}
alias_remove() {
has_args 1 "$@"
local list tmp
list=$1
tmp=$(cmd_tempfile)
run cp /etc/aliases $tmp
grep -v"'^$list:" $PREFIX/etc/aliases >$tmp
}
aliases() {
grep '^[^:]*:' $PREFIX/etc/aliases |cut -f1 -d':'
}
admin_alias_exists() {
has_args 1 "$@"
alias_exists "${1}-owner"
}
######
# List management
for_each_list() {
min_args 1 "$@"
local cmd=$1
shift
seed_sudo
local -a lists
if [ -z "$*" ]; then
lists=( $(list_lists) )
else
lists=( "$@" )
fi
local list
for list in "${lists[@]}"; do
$cmd $list
done
}
list_config() {
has_args 2 "$@"
echo "$script_confdir/lists.d/$1/$2"
}
list_secret_save() {
has_args 2 "$@"
local list passwd src dst
list=$1
passwd=$2
src=$(cmd_tempfile)
echo $passwd >$src
dst=$(list_config $list 'secret.txt')
run_sudo_install 0770 root list -d "$(dirname $dst)"
run_sudo_install 0660 root list $src $dst
}
list_passwd_show() {
has_args 1 "$@"
local list=$1
local secret
secret=$(list_config $list 'secret.txt')
if sudo test -f "$secret"; then
echo -n "$list: "
sudo cat $secret
else
error "$list: no such list"
fi
}
list_change_pw() {
has_args 1 "$@"
local list passwd
list=$1
app_echo "$list: resetting..."
passwd=$(mm_change_pw $list)
list_secret_save "$list" "$passwd"
}
passwd_gen() {
apg -c $RANDOM -n 1
}
list_new() {
has_args 2 "$@"
local name passwd
name=$1
passwd=$2
run_mm_bin newlist -a \
--urlhost=$mailman_host.$domain \
--emailhost=$domain \
$name list-admin@$domain $passwd
}
list_lists() {
run_mm_bin list_lists -b "$@"
}
list_exists() {
has_args 1 "$@"
list_lists | egrep "^$1$"
}
##
# CLI commands
m7_list() { cmd_dispatch "$@"; }
m7_list_usage() {
cat <<EOF
<cmd> [...]
List Commands:
all
public
private
exists <list>
new <list>
EOF
}
install_as_root() {
min_args 2 "$@"
local src dst mode user
src=$1
dst=$2
mode=${3:-0600}
user=${4:-root:root}
pipe_to_sudo <<EOF
mv $src $dst
chmod $mode $dst
chown $user $dst
EOF
}
install_mail_aliases() {
local src
src=$1
install_as_root $tmp $PREFIX/etc/aliases 0644
sudo newaliases
}
m7_list_new() {
has_args 1 "$@"
local name passwd aliases
name=$1
if list_exists $name; then
error "$name: exists"
fi
passwd=$(passwd_gen)
list_secret_save "$name" "$passwd"
local aliases
aliases=$(cmd_tempfile)
list_new $name $passwd |tail -n11 |head -n10 >>$aliases
local tmp
tmp=$(cmd_tempfile)
cp -a /etc/aliases $tmp
cat $aliases >>$tmp
rm $aliases
install_mail_aliases $tmp
}
m7_list_delete() {
has_args 1 "$@"
local aliases tmp
aliases=$(cmd_tempfile)
run_mm_bin rmlist "$@" |tail -n12 |head -n10 >$aliases
tmp=$(cmd_tempfile)
grep -f $aliases -v /etc/aliases >$tmp
rm $aliases
install_mail_aliases $tmp
}
m7_list_all() {
has_args 0 "$@"
list_lists
}
m7_list_public() {
has_args 0 "$@"
list_lists -a
}
m7_list_private() {
has_args 0 "$@"
local all pub pri a b
all=( $(list_lists) )
pub=( $(list_lists -a) )
for a in "${all[@]}"; do
local found=false
for b in "${pub[@]}"; do
if [ "$a" = "$b" ]; then
found=true
fi
done
$found || echo "$a"
done
}
m7_list_secret() { list_passwd_show "$@"; }
m7_lists() { m7_list_all; }
######
# list passwords
m7_passwd() { cmd_dispatch "$@"; }
m7_passwd_usage() {
cat <<EOF
<cmd> [<name>+]
Password Commands:
show [<name+>]
reset [<name>+]
EOF
}
m7_passwd_show() { for_each_list list_passwd_show "$@"; }
m7_passwd_reset() { for_each_list list_change_pw "$@"; }
######
# list configuration
m7_config() { cmd_dispatch "$@"; }
_m7_config_get() {
mm_config -o $1
run_sudo_as_user $mailman_user chmod 0660 $1
}
m7_config_get() { for_each_list _m7_config_get "$@"; }
_m7_config_validate() { mm_config -ci $1; }
m7_config_validate() { for_each_list _m7_config_validate "$@"; }
_m7_config_set() { mm_config -i $1; }
m7_config_set() { for_each_list _m7_config_set "$@"; }
m7_config_edit() {
[ "$EDITOR" ] || error "\$EDITOR is not set"
has_args 1 "$@"
local list=$1
local conf
conf=$(list_config $list 'list.conf')
run_sudo ${EDITOR} "$conf"
}
m7_config_var() { cmd_dispatch "$@"; }
m7_config_var_list() {
local list=mailman
local conf
conf=$(list_config $list 'list.conf')
run_sudo egrep "^[^ =]+ ?= " "$conf"
}
_m7_config_var_get() {
local list=$1
local conf
conf=$(list_config $list 'list.conf')
out=$(run_sudo egrep "^$var = " "$conf")
if [ "$out" ]; then
echo "$list: $out"
fi
}
m7_config_var_get() {
local var=$1
shift
for_each_list _m7_config_var_get "$@"
}
######
# Main
m7_desc() { echo "Mailman management tool"; }
m7_usage() {
cat <<USAGE
<cmd> ...
Main Commands:
list ...
passwd ...
config ...
USAGE
}
m7_help() {
cat<<HELP
The $script_name tool manages and assists with mailman list creation, bulk site
reconfiguration, and similar tasks.
HELP
}
app_run "$@"
Generated on Thu May 4 18:59:02 PDT 2017 by mcsh i7 v0.19.0.