Created
July 11, 2018 08:12
-
-
Save yrashk/63d73e247190b2ac41de851c4265c4d5 to your computer and use it in GitHub Desktop.
sit-inbox.sh
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env bash | |
cfg=/tmp/sit-inbox.json | |
checkmark="✔" | |
echo Starting up sit-inbox | |
if [ -f /etc/sit-inbox/config.toml ]; then | |
echo "Reading config /etc/sit-inbox.toml" | |
toml2json /etc/sit-inbox/config.toml > "${cfg}" | |
else | |
echo /etc/sit-inbox/config.toml not found, aborting | |
exit 1 | |
fi | |
# $1 - inbox | |
setup_files () { | |
echo -n "cron " | |
default_repository=$(jq -r '.inbox.'$1'.default_repository // ""' ${cfg}) | |
path=$(jq -r '.inbox.'$1'.path // "/path/does/not/exist"' ${cfg}) | |
mkdir -p "${path}" | |
cron=$(jq -r '.inbox.'$1'.cron // "*/5 * * * *"' $cfg) | |
crontab -l > /tmp/crontab | |
cat >>/tmp/crontab <<-EOF | |
${cron} INBOX=${path} REPOSITORY=${default_repository} "/files.sh" | |
@reboot INBOX=${path} REPOSITORY=${default_repository} "/files.sh" | |
EOF | |
crontab /tmp/crontab | |
} | |
# $1 - inbox | |
setup_email () { | |
retriever=$(jq -r ".inbox."$1".retriever" ${cfg}) | |
server=$(jq -r ".inbox."$1".server" ${cfg}) | |
username=$(jq -r ".inbox."$1".username" ${cfg}) | |
echo -n "retriever " | |
# Retriever: required | |
cat >"$HOME/.getmail/$1" <<-EOF | |
[retriever] | |
type = ${retriever} | |
server = ${server} | |
username = ${username} | |
EOF | |
port=$(jq -r 'if .inbox.'$1'.port == null then "# no `port` specified" else "port = \(.inbox.'$1'.port)" end' $cfg) | |
password=$(jq -r 'if .inbox.'$1'.password == null then "# no `password` specified" else "password = \(.inbox.'$1'.password)" end' $cfg) | |
password_command=$(jq -r 'if .inbox.'$1'.password_command == null then "# no `password_command` specified" else "password_command = \(.inbox.'$1'.password_command)" end' $cfg | sed -e 's/\[\([^]]*\)\]/( \1 )/g') | |
# Retriever: optional | |
cat >>"$HOME/.getmail/$1" <<-EOF | |
${port} | |
${password} | |
${password_command} | |
EOF | |
echo -n "maildrop " | |
maildrop=$(jq -r '.inbox.'$1'.maildrop // ""' ${cfg}) | |
maildrop_f="/var/run/maildroprc_$1" | |
echo -n ${maildrop} > ${maildrop_f} | |
if [ -z "${maildrop}" ]; then | |
cat /maildroprc >> "${maildrop_f}" | |
fi | |
chmod 0600 "${maildrop_f}" | |
# Destination | |
cat >>"$HOME/.getmail/$1" <<-EOF | |
[destination] | |
type = MDA_external | |
path = /usr/local/bin/maildrop | |
arguments = ( "${maildrop_f}", ) | |
allow_root_commands = true | |
EOF | |
echo -n "cron " | |
default_repository=$(jq -r '.inbox.'$1'.default_repository // ""' ${cfg}) | |
cat >>"/usr/bin/getmail.$1" <<-EOF | |
#! /usr/bin/env bash | |
export REPOSITORY=${default_repository} | |
setlock -n "/var/run/getmail.$1" /usr/bin/getmail --rcfile="$HOME/.getmail/$1" --getmaildir=/var/run/oldmail -n | |
EOF | |
chmod +x "/usr/bin/getmail.$1" | |
cron=$(jq -r '.inbox.'$1'.cron // "*/5 * * * *"' $cfg) | |
crontab -l > /tmp/crontab | |
cat >>/tmp/crontab <<-EOF | |
${cron} "/usr/bin/getmail.$1" | |
@reboot "/usr/bin/getmail.$1" | |
EOF | |
crontab /tmp/crontab | |
} | |
#1 - repo | |
setup_git () { | |
branch=$(jq -r '.repository.'$1'.branch // "master"' ${cfg}) | |
src=$(jq -r '.repository.'$1'.source' ${cfg}) | |
if [ -d "/var/lib/repositories/$1" ]; then | |
echo -n "updating " | |
git -C "/var/lib/repositories/$1" pull -f -q "${src}" "${branch}" || exit 1 | |
else | |
echo -n "cloning " | |
git clone -q "${src}" -b "${branch}" "/var/lib/repositories/$1" || exit 1 | |
fi | |
username=$(jq -r '.repository.'$1'.username // "sit-inbox"' ${cfg}) | |
email=$(jq -r '.repository.'$1'.email // "[email protected]"' ${cfg}) | |
git -C "/var/lib/repositories/$1" config user.name ${username} | |
git -C "/var/lib/repositories/$1" config user.email ${email} | |
echo -n "script " | |
cat >"/usr/bin/email.${1}" <<-EOF | |
#! /usr/bin/env bash | |
file=\$(mktemp) | |
cat <&0 "\${file}" | |
temp=$(mktemp -d) | |
git -C "/var/lib/repositories/$1" pull -f -q "${src}" "${branch}" | |
git -C "/var/lib/repositories/$1" branch -q -D inbox 2>/dev/null >/dev/null | |
git -C "/var/lib/repositories/$1" checkout -b inbox | |
git -C "/var/lib/repositories/$1" am -3 "\${file}" | |
result=\$? | |
if [ "\${result}" == "0" ]; then | |
git -C "/var/lib/repositories/$1" push ${src} inbox:${branch} | |
else | |
git -C "/var/lib/repositories/$1" am --abort 2>/dev/null | |
fi | |
git -C "/var/lib/repositories/$1" checkout ${branch} | |
git -C "/var/lib/repositories/$1" branch -q -D inbox | |
rm -rf "\${temp}" | |
rm -f "\${file}" | |
EOF | |
chmod +x "/usr/bin/email.$1" | |
} | |
echo Setting up inboxes: | |
for inbox in $(jq -r ".inbox | keys | .[]" $cfg); do | |
type=$(jq -r ".inbox."${inbox}".type" $cfg) | |
echo -n "- ${inbox} (type: ${type}): " | |
case ${type} in | |
email) | |
mkdir -m 0700 -p $HOME/.getmail | |
setup_email "${inbox}" | |
echo $checkmark | |
;; | |
files) | |
setup_files "${inbox}" | |
echo $checkmark | |
;; | |
*) | |
echo "ERROR: Unknown type" | |
exit 1 | |
esac | |
done | |
echo Setting up repositories: | |
for repo in $(jq -r ".repository | keys | .[]" $cfg); do | |
type=$(jq -r '.repository.'${repo}'.type // "git"' $cfg) | |
echo -n "- ${repo} (type: ${type}): " | |
case ${type} in | |
git) | |
setup_git "${repo}" | |
echo $checkmark | |
;; | |
*) | |
echo "ERROR: Unknown type" | |
exit 1 | |
esac | |
done | |
echo | |
echo "OK: The inbox is up and running" | |
echo | |
crond -f || exit 1 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment