From 293afcd5ee8813e28afd6cf341994eaa9cff0103 Mon Sep 17 00:00:00 2001 From: hoelee Date: Tue, 23 Jun 2026 09:00:00 +0800 Subject: [PATCH] feat(shell-sync): sync_account() part 2/3 --- platform/shell-sync/sync.sh | 79 +++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/platform/shell-sync/sync.sh b/platform/shell-sync/sync.sh index 9f6e5502..63c9d577 100644 --- a/platform/shell-sync/sync.sh +++ b/platform/shell-sync/sync.sh @@ -188,3 +188,82 @@ sync_account() { 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 +