fix
This commit is contained in:
parent
91e85fa422
commit
baaafe711c
|
|
@ -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,13 +110,24 @@ 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")
|
||||||
|
|
||||||
|
# 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"
|
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 \
|
||||||
|
|
@ -118,14 +135,18 @@ cmd_set_ooo() {
|
||||||
--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
|
||||||
|
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
info "Out-of-Office aktiviert für $email ($content_type)"
|
info "Out-of-Office aktiviert für $email ($content_type)"
|
||||||
|
else
|
||||||
|
error "Fehler beim Setzen der OOO-Regel"
|
||||||
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# ==============================================
|
# ==============================================
|
||||||
|
|
@ -144,24 +165,33 @@ 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
|
||||||
|
local existing_rule=$(get_rule "$email")
|
||||||
|
|
||||||
|
if [ "$existing_rule" != "null" ] && [ -n "$existing_rule" ]; then
|
||||||
ooo_active=$(echo "$existing_rule" | jq -r '.ooo_active.BOOL // false')
|
ooo_active=$(echo "$existing_rule" | jq -r '.ooo_active.BOOL // false')
|
||||||
ooo_message=$(echo "$existing_rule" | jq -r '.ooo_message.S // ""')
|
ooo_message=$(echo "$existing_rule" | jq -r '.ooo_message.S // ""')
|
||||||
ooo_content_type=$(echo "$existing_rule" | jq -r '.ooo_content_type.S // "text"')
|
ooo_content_type=$(echo "$existing_rule" | jq -r '.ooo_content_type.S // "text"')
|
||||||
warn "Regel existiert bereits, behalte bestehende OOO-Einstellungen bei"
|
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 \
|
||||||
|
|
@ -169,14 +199,18 @@ cmd_set_forward() {
|
||||||
--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
|
||||||
|
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
info "Forwards gesetzt für $email: ${ADDRS[*]}"
|
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
|
||||||
|
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
info "Out-of-Office entfernt für $email"
|
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
|
||||||
|
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
info "Forwards entfernt für $email"
|
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
|
||||||
|
|
||||||
|
if [ $? -eq 0 ]; then
|
||||||
info "Regel komplett gelöscht für $email"
|
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
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue