This commit is contained in:
Andreas Knuth 2025-12-17 16:08:55 -06:00
parent 91e85fa422
commit baaafe711c
1 changed files with 86 additions and 35 deletions

View File

@ -60,11 +60,17 @@ EOF
# Prüft ob Regel existiert # Prüft ob Regel existiert
rule_exists() { rule_exists() {
local email=$1 local email=$1
aws dynamodb get-item \ local result=$(aws dynamodb get-item \
--table-name "$TABLE_NAME" \ --table-name "$TABLE_NAME" \
--key '{"email_address": {"S": "'"$email"'"}}' \ --key '{"email_address": {"S": "'"$email"'"}}' \
--region "$REGION" \ --region "$REGION" \
--output json 2>/dev/null | jq -e '.Item' > /dev/null --output json 2>/dev/null)
if echo "$result" | jq -e '.Item' > /dev/null 2>&1; then
return 0 # Existiert
else
return 1 # Existiert nicht
fi
} }
# Holt aktuelle Regel # Holt aktuelle Regel
@ -74,7 +80,7 @@ get_rule() {
--table-name "$TABLE_NAME" \ --table-name "$TABLE_NAME" \
--key '{"email_address": {"S": "'"$email"'"}}' \ --key '{"email_address": {"S": "'"$email"'"}}' \
--region "$REGION" \ --region "$REGION" \
--output json 2>/dev/null | jq -r '.Item // {}' --output json 2>/dev/null | jq -r '.Item'
} }
# Validiert E-Mail-Adresse (basic) # Validiert E-Mail-Adresse (basic)
@ -104,28 +110,43 @@ cmd_set_ooo() {
validate_email "$email" validate_email "$email"
# Hole bestehende Forwards (falls vorhanden) # Hole bestehende Forwards (falls vorhanden)
local existing_rule=$(get_rule "$email") local forwards_json='[]'
local forwards_json='{"L": []}'
if [ "$existing_rule" != "{}" ]; then if rule_exists "$email"; then
forwards_json=$(echo "$existing_rule" | jq -r '.forwards // {"L": []}') local existing_rule=$(get_rule "$email")
warn "Regel existiert bereits, behalte bestehende Forwards bei"
# Prüfe ob existing_rule nicht null ist
if [ "$existing_rule" != "null" ] && [ -n "$existing_rule" ]; then
# Extrahiere forwards, fallback auf leeres Array
local existing_forwards=$(echo "$existing_rule" | jq -r '.forwards.L // []')
if [ "$existing_forwards" != "[]" ] && [ "$existing_forwards" != "null" ]; then
forwards_json="$existing_forwards"
warn "Regel existiert bereits, behalte bestehende Forwards bei"
fi
fi
fi fi
# Escape JSON-String korrekt
local escaped_message=$(echo "$message" | jq -Rs .)
# Setze Regel # Setze Regel
aws dynamodb put-item \ aws dynamodb put-item \
--table-name "$TABLE_NAME" \ --table-name "$TABLE_NAME" \
--item '{ --item '{
"email_address": {"S": "'"$email"'"}, "email_address": {"S": "'"$email"'"},
"ooo_active": {"BOOL": true}, "ooo_active": {"BOOL": true},
"ooo_message": {"S": "'"${message//\"/\\\"}"'"}, "ooo_message": {"S": '"$escaped_message"'},
"ooo_content_type": {"S": "'"$content_type"'"}, "ooo_content_type": {"S": "'"$content_type"'"},
"forwards": '"$forwards_json"', "forwards": {"L": '"$forwards_json"'},
"last_updated": {"S": "'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"} "last_updated": {"S": "'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"}
}' \ }' \
--region "$REGION" >/dev/null --region "$REGION" 2>&1
info "Out-of-Office aktiviert für $email ($content_type)" if [ $? -eq 0 ]; then
info "Out-of-Office aktiviert für $email ($content_type)"
else
error "Fehler beim Setzen der OOO-Regel"
fi
} }
# ============================================== # ==============================================
@ -144,39 +165,52 @@ cmd_set_forward() {
# Validiere alle Forward-Adressen # Validiere alle Forward-Adressen
IFS=',' read -ra ADDRS <<< "$forward_addresses" IFS=',' read -ra ADDRS <<< "$forward_addresses"
for addr in "${ADDRS[@]}"; do for addr in "${ADDRS[@]}"; do
validate_email "$(echo "$addr" | xargs)" # xargs trimmt whitespace addr=$(echo "$addr" | xargs) # xargs trimmt whitespace
if [ -n "$addr" ]; then
validate_email "$addr"
fi
done done
# Konvertiere zu JSON-Array # Konvertiere zu JSON-Array
local forwards_list=$(echo "$forward_addresses" | jq -R 'split(",") | map(gsub("^\\s+|\\s+$";"")) | map({"S": .})') local forwards_list=$(echo "$forward_addresses" | jq -R 'split(",") | map(gsub("^\\s+|\\s+$";"")) | map(select(length > 0)) | map({"S": .})')
# Hole bestehende OOO-Einstellungen (falls vorhanden) # Hole bestehende OOO-Einstellungen (falls vorhanden)
local existing_rule=$(get_rule "$email")
local ooo_active="false" local ooo_active="false"
local ooo_message="" local ooo_message=""
local ooo_content_type="text" local ooo_content_type="text"
if [ "$existing_rule" != "{}" ]; then if rule_exists "$email"; then
ooo_active=$(echo "$existing_rule" | jq -r '.ooo_active.BOOL // false') local existing_rule=$(get_rule "$email")
ooo_message=$(echo "$existing_rule" | jq -r '.ooo_message.S // ""')
ooo_content_type=$(echo "$existing_rule" | jq -r '.ooo_content_type.S // "text"') if [ "$existing_rule" != "null" ] && [ -n "$existing_rule" ]; then
warn "Regel existiert bereits, behalte bestehende OOO-Einstellungen bei" ooo_active=$(echo "$existing_rule" | jq -r '.ooo_active.BOOL // false')
ooo_message=$(echo "$existing_rule" | jq -r '.ooo_message.S // ""')
ooo_content_type=$(echo "$existing_rule" | jq -r '.ooo_content_type.S // "text"')
warn "Regel existiert bereits, behalte bestehende OOO-Einstellungen bei"
fi
fi fi
# Escape message für JSON
local escaped_ooo_message=$(echo "$ooo_message" | jq -Rs .)
# Setze Regel # Setze Regel
aws dynamodb put-item \ aws dynamodb put-item \
--table-name "$TABLE_NAME" \ --table-name "$TABLE_NAME" \
--item '{ --item '{
"email_address": {"S": "'"$email"'"}, "email_address": {"S": "'"$email"'"},
"ooo_active": {"BOOL": '"$ooo_active"'}, "ooo_active": {"BOOL": '"$ooo_active"'},
"ooo_message": {"S": "'"${ooo_message//\"/\\\"}"'"}, "ooo_message": {"S": '"$escaped_ooo_message"'},
"ooo_content_type": {"S": "'"$ooo_content_type"'"}, "ooo_content_type": {"S": "'"$ooo_content_type"'"},
"forwards": {"L": '"$forwards_list"'}, "forwards": {"L": '"$forwards_list"'},
"last_updated": {"S": "'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"} "last_updated": {"S": "'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"}
}' \ }' \
--region "$REGION" >/dev/null --region "$REGION" 2>&1
info "Forwards gesetzt für $email: ${ADDRS[*]}" if [ $? -eq 0 ]; then
info "Forwards gesetzt für $email: ${ADDRS[*]}"
else
error "Fehler beim Setzen der Forward-Regel"
fi
} }
# ============================================== # ==============================================
@ -198,7 +232,11 @@ cmd_add_forward() {
# Hole bestehende Forwards # Hole bestehende Forwards
local existing_rule=$(get_rule "$email") local existing_rule=$(get_rule "$email")
local existing_forwards=$(echo "$existing_rule" | jq -r '.forwards.L // [] | map(.S) | join(",")') local existing_forwards=""
if [ "$existing_rule" != "null" ] && [ -n "$existing_rule" ]; then
existing_forwards=$(echo "$existing_rule" | jq -r '.forwards.L // [] | map(.S) | join(",")')
fi
# Kombiniere und dedupliziere # Kombiniere und dedupliziere
local combined="$existing_forwards,$new_addresses" local combined="$existing_forwards,$new_addresses"
@ -250,9 +288,13 @@ cmd_remove_ooo() {
":false": {"BOOL": false}, ":false": {"BOOL": false},
":timestamp": {"S": "'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"} ":timestamp": {"S": "'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"}
}' \ }' \
--region "$REGION" >/dev/null --region "$REGION" 2>&1 >/dev/null
info "Out-of-Office entfernt für $email" if [ $? -eq 0 ]; then
info "Out-of-Office entfernt für $email"
else
error "Fehler beim Entfernen der OOO-Regel"
fi
} }
# ============================================== # ==============================================
@ -281,9 +323,13 @@ cmd_remove_forward() {
":empty": {"L": []}, ":empty": {"L": []},
":timestamp": {"S": "'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"} ":timestamp": {"S": "'$(date -u +"%Y-%m-%dT%H:%M:%SZ")'"}
}' \ }' \
--region "$REGION" >/dev/null --region "$REGION" 2>&1 >/dev/null
info "Forwards entfernt für $email" if [ $? -eq 0 ]; then
info "Forwards entfernt für $email"
else
error "Fehler beim Entfernen der Forwards"
fi
} }
# ============================================== # ==============================================
@ -306,9 +352,13 @@ cmd_remove_all() {
aws dynamodb delete-item \ aws dynamodb delete-item \
--table-name "$TABLE_NAME" \ --table-name "$TABLE_NAME" \
--key '{"email_address": {"S": "'"$email"'"}}' \ --key '{"email_address": {"S": "'"$email"'"}}' \
--region "$REGION" >/dev/null --region "$REGION" 2>&1 >/dev/null
info "Regel komplett gelöscht für $email" if [ $? -eq 0 ]; then
info "Regel komplett gelöscht für $email"
else
error "Fehler beim Löschen der Regel"
fi
} }
# ============================================== # ==============================================
@ -339,10 +389,10 @@ cmd_show() {
if [ "$ooo_active" = "true" ]; then if [ "$ooo_active" = "true" ]; then
local ooo_msg=$(echo "$rule" | jq -r '.ooo_message.S // ""') local ooo_msg=$(echo "$rule" | jq -r '.ooo_message.S // ""')
local ooo_type=$(echo "$rule" | jq -r '.ooo_content_type.S // "text"') local ooo_type=$(echo "$rule" | jq -r '.ooo_content_type.S // "text"')
echo "Out-of-Office: ${GREEN}AKTIV${NC} ($ooo_type)" echo -e "Out-of-Office: ${GREEN}AKTIV${NC} ($ooo_type)"
echo "Nachricht: $ooo_msg" echo "Nachricht: $ooo_msg"
else else
echo "Out-of-Office: ${RED}INAKTIV${NC}" echo -e "Out-of-Office: ${RED}INAKTIV${NC}"
fi fi
# Forwards # Forwards
@ -350,7 +400,7 @@ cmd_show() {
if [ -n "$forwards" ]; then if [ -n "$forwards" ]; then
echo "Forwards: $forwards" echo "Forwards: $forwards"
else else
echo "Forwards: ${RED}KEINE${NC}" echo -e "Forwards: ${RED}KEINE${NC}"
fi fi
# Letztes Update # Letztes Update
@ -367,11 +417,12 @@ cmd_list() {
echo "Alle E-Mail-Regeln:" echo "Alle E-Mail-Regeln:"
echo "═════════════════════════════════════════════════════════════════" echo "═════════════════════════════════════════════════════════════════"
local items=$(aws dynamodb scan \ local result=$(aws dynamodb scan \
--table-name "$TABLE_NAME" \ --table-name "$TABLE_NAME" \
--region "$REGION" \ --region "$REGION" \
--output json | jq -r '.Items') --output json 2>/dev/null)
local items=$(echo "$result" | jq -r '.Items // []')
local count=$(echo "$items" | jq 'length') local count=$(echo "$items" | jq 'length')
if [ "$count" -eq 0 ]; then if [ "$count" -eq 0 ]; then