cdist-contrib/type/__mail_alias/explorer/aliases

73 lines
1.8 KiB
Bash
Executable File

#!/bin/sh -e
#
# 2020 Dennis Camera (dennis.camera at ssrq-sds-fds.ch)
#
# This file is part of cdist.
#
# cdist is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# cdist is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with cdist. If not, see <http://www.gnu.org/licenses/>.
#
# Find aliases for a given name and print the aliases line separated
aliases_file=$("${__type_explorer}/aliases_file")
test -r "${aliases_file}" || exit 0
awk -F ':[ \t]*' '
function print_aliases(aliases, matches) {
split(aliases, matches, /,[ \t]*/)
for (i in matches) {
gsub(/^[ \t]*|[ \t]*$/, "", matches[i])
print matches[i]
}
}
/^#/ {
# comment line (ignore)
select = 0; cont = 0 # comments terminate alias lists and continuations
next
}
/^[ \t]/ || cont {
# continuation line (either the previous line ended in a backslash or the
# line starts with whitespace)
if (select)
print_aliases($0)
}
{
# detect if the line is a line to be continued (ends with a backslash)
cont = ($0 ~ /\\$/)
# if it is, we drop the backslash from the line and skip to next line
# (the contents have been printed above if they should)
if (cont) {
sub(/[ \t]*\\$/, "", $0)
next
}
}
$1 == ENVIRON["__object_id"] && !select {
# "target" user -> print alias list
# (only if !select; because of whitespacecontinuation lines)
select = 1
print_aliases($2)
next
}
{
# other user
select = 0
}
' "${aliases_file}"