feat(shell-sync): sync_account
This commit is contained in:
1 parent
072cd3f28b
commit
b19dea7ca4
1 file changed
+147
@@ -144,3 +144,150 @@ source_oauth_provider() {
|
||||
esac
|
||||
}
|
||||
|
||||
sync_account() {
|
||||
local i="$1"
|
||||
local src_host="$(get_var "ACCOUNT_${i}_SOURCE_HOST")"
|
||||
local src_user="$(get_var "ACCOUNT_${i}_SOURCE_USER")"
|
||||
local src_auth="$(get_var "ACCOUNT_${i}_SOURCE_AUTH")"
|
||||
local src_pass="$(get_var "ACCOUNT_${i}_SOURCE_PASS")"
|
||||
local src_token="$(get_var "ACCOUNT_${i}_SOURCE_TOKEN")"
|
||||
local src_provider="$(get_var "ACCOUNT_${i}_SOURCE_PROVIDER")"
|
||||
local dest_host="$(get_var "ACCOUNT_${i}_DEST_HOST")"
|
||||
local dest_user="$(get_var "ACCOUNT_${i}_DEST_USER")"
|
||||
local dest_pass="$(get_var "ACCOUNT_${i}_DEST_PASS")"
|
||||
local delete_opt="$(get_var "ACCOUNT_${i}_DELETE")"
|
||||
local dry_run="$(get_var "ACCOUNT_${i}_DRY_RUN")"
|
||||
local extra_opts="$(get_var "ACCOUNT_${i}_EXTRA_OPTS")"
|
||||
local logfile="${LOG_DIR}/account${i}.log"
|
||||
local pidfile="${PID_DIR}/account${i}.pid"
|
||||
local oauth_provider
|
||||
local oauth_log
|
||||
local oauth_authorization_detected=false
|
||||
local sync_exit
|
||||
local -a args
|
||||
|
||||
SYNC_ACCOUNT_RESULT="skipped"
|
||||
if [ -z "$src_host" ] && [ -z "$dest_host" ]; then
|
||||
log "Account $i: SKIP (not configured)"
|
||||
return 0
|
||||
fi
|
||||
if [ -z "$src_host" ] || [ -z "$src_user" ] || [ -z "$dest_host" ] || [ -z "$dest_user" ] || [ -z "$dest_pass" ]; then
|
||||
log "Account $i: CONFIGURATION FAILED (source host/user and destination host/user/password are required)"
|
||||
SYNC_ACCOUNT_RESULT="failure"
|
||||
echo "$(date +%s)" > "${LOG_DIR}/account${i}.last_failure"
|
||||
return 1
|
||||
fi
|
||||
|
||||
if [ -f "$pidfile" ]; then
|
||||
local old_pid
|
||||
old_pid="$(cat "$pidfile")"
|
||||
if kill -0 "$old_pid" 2>/dev/null; then
|
||||
log "Account $i: SKIP (already running, PID $old_pid)"
|
||||
return 0
|
||||
fi
|
||||
fi
|
||||
echo $$ > "$pidfile"
|
||||
|
||||
log "Account $i: SYNC START - $src_user ($src_host) -> $dest_user ($dest_host)"
|
||||
args=(
|
||||
--host1 "$src_host" --user1 "$src_user" --ssl1
|
||||
--host2 "$dest_host" --user2 "$dest_user" --password2 "$dest_pass" --ssl2
|
||||
--noauthmd5
|
||||
)
|
||||
|
||||
case "$src_auth" in
|
||||
oauth2)
|
||||
if ! oauth_provider="$(source_oauth_provider "$src_provider")"; then
|
||||
log "Account $i: CONFIGURATION FAILED (OAuth2 provider must be office365, outlook, hotmail, gmail, or google)"
|
||||
SYNC_ACCOUNT_RESULT="failure"
|
||||
echo "$(date +%s)" > "${LOG_DIR}/account${i}.last_failure"
|
||||
rm -f "$pidfile"
|
||||
return 1
|
||||
fi
|
||||
if [ -z "$src_token" ]; then
|
||||
log "Account $i: CONFIGURATION FAILED (ACCOUNT_${i}_SOURCE_TOKEN is required for OAuth2)"
|
||||
SYNC_ACCOUNT_RESULT="failure"
|
||||
echo "$(date +%s)" > "${LOG_DIR}/account${i}.last_failure"
|
||||
rm -f "$pidfile"
|
||||
return 1
|
||||
fi
|
||||
if [ ! -s "$src_token" ]; then
|
||||
log "Account $i: OAuth2 token is missing. Authorize with: docker compose run --rm imapsync authorize $i"
|
||||
SYNC_ACCOUNT_RESULT="failure"
|
||||
echo "$(date +%s)" > "${LOG_DIR}/account${i}.last_failure"
|
||||
rm -f "$pidfile"
|
||||
return 1
|
||||
fi
|
||||
# The helper prints access and refresh tokens. Keep that output in a
|
||||
# short-lived, root-only file instead of the mounted account log.
|
||||
oauth_log="$(mktemp "${PID_DIR}/oauth.XXXXXX")"
|
||||
chmod 600 "$oauth_log"
|
||||
# The upstream helper opens an interactive browser flow when refresh
|
||||
# fails. A scheduled container must never wait indefinitely for that.
|
||||
timeout --kill-after=5s 90s oauth2_imap --provider "$oauth_provider" --application imapsync --token_file "$src_token" "$src_user" > "$oauth_log" 2>&1 || true
|
||||
if oauth_authorization_invalid "$oauth_log"; then
|
||||
log_oauth_authorization_guidance "$i" "$src_provider" "$oauth_log"
|
||||
oauth_authorization_detected=true
|
||||
fi
|
||||
rm -f "$oauth_log"
|
||||
if [ ! -s "$src_token" ]; then
|
||||
log "Account $i: OAuth2 token refresh or authorization failed"
|
||||
log_oauth_authorization_guidance "$i" "$src_provider" "$logfile"
|
||||
SYNC_ACCOUNT_RESULT="failure"
|
||||
echo "$(date +%s)" > "${LOG_DIR}/account${i}.last_failure"
|
||||
rm -f "$pidfile"
|
||||
return 1
|
||||
fi
|
||||
args+=(--oauthaccesstoken1 "$src_token")
|
||||
;;
|
||||
password)
|
||||
if [ -z "$src_pass" ]; then
|
||||
log "Account $i: CONFIGURATION FAILED (ACCOUNT_${i}_SOURCE_PASS is required for password authentication)"
|
||||
SYNC_ACCOUNT_RESULT="failure"
|
||||
echo "$(date +%s)" > "${LOG_DIR}/account${i}.last_failure"
|
||||
rm -f "$pidfile"
|
||||
return 1
|
||||
fi
|
||||
args+=(--password1 "$src_pass")
|
||||
;;
|
||||
*)
|
||||
log "Account $i: CONFIGURATION FAILED (ACCOUNT_${i}_SOURCE_AUTH must be oauth2 or password)"
|
||||
SYNC_ACCOUNT_RESULT="failure"
|
||||
echo "$(date +%s)" > "${LOG_DIR}/account${i}.last_failure"
|
||||
rm -f "$pidfile"
|
||||
return 1
|
||||
;;
|
||||
esac
|
||||
|
||||
[ "$delete_opt" = "true" ] && args+=(--delete2)
|
||||
[ "$dry_run" = "true" ] && args+=(--dry)
|
||||
if [ -n "$extra_opts" ]; then
|
||||
# Intentional option splitting: this variable supplies extra CLI flags.
|
||||
# shellcheck disable=SC2206
|
||||
args+=($extra_opts)
|
||||
fi
|
||||
|
||||
# Capture the real exit status. Reading $? after an `if` block returns the
|
||||
# status of the `if` itself (always 0), which silently reported every
|
||||
# failure as exit 0 and let sync_all treat it as success.
|
||||
sync_exit=0
|
||||
imapsync "${args[@]}" >> "$logfile" 2>&1 || sync_exit=$?
|
||||
|
||||
if [ "$sync_exit" -eq 0 ]; then
|
||||
log "Account $i: SYNC OK"
|
||||
echo "$(date +%s)" > "${LOG_DIR}/account${i}.last_success"
|
||||
SYNC_ACCOUNT_RESULT="success"
|
||||
rm -f "$pidfile"
|
||||
return 0
|
||||
fi
|
||||
|
||||
log "Account $i: SYNC FAILED (exit $sync_exit)"
|
||||
echo "$(date +%s)" > "${LOG_DIR}/account${i}.last_failure"
|
||||
if [ "$src_auth" = "oauth2" ] && [ "$oauth_authorization_detected" = "false" ]; then
|
||||
log_oauth_authorization_guidance "$i" "$src_provider" "$logfile"
|
||||
fi
|
||||
SYNC_ACCOUNT_RESULT="failure"
|
||||
rm -f "$pidfile"
|
||||
return "$sync_exit"
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user