···6868printf "Tagging remaining new emails as to-screen...\n"
6969notmuch tag +to-screen -new -- tag:new and not tag:screened-in and not tag:screened-out and not tag:feed and not tag:papertrail
70707171-# Move previously screened emails if sender status changed
7272-printf "Updating tags for emails in to-screen based on current lists...\n"
7171+# Efficiently process to-screen emails against current lists
7272+printf "Processing to-screen emails against screening lists...\n"
7373+7474+# Process screened_in list against to-screen emails (most efficient: bulk operations)
7575+while IFS= read -r email; do
7676+ [ -z "$email" ] && continue
7777+ count=$(notmuch count from:"$email" tag:to-screen)
7878+ if [ "$count" -gt 0 ]; then
7979+ printf " Screening in %d emails from %s\n" "$count" "$email"
8080+ notmuch tag +screened-in +inbox -to-screen -- from:"$email" tag:to-screen
8181+ fi
8282+done < "$screened_in"
8383+8484+# Process screened_out list against to-screen emails
8585+while IFS= read -r email; do
8686+ [ -z "$email" ] && continue
8787+ count=$(notmuch count from:"$email" tag:to-screen)
8888+ if [ "$count" -gt 0 ]; then
8989+ printf " Screening out %d emails from %s\n" "$count" "$email"
9090+ notmuch tag +screened-out +spam -to-screen -inbox -- from:"$email" tag:to-screen
9191+ fi
9292+done < "$screened_out"
73937474-# Check to-screen emails and retag if sender is now in a list
7575-for msg_id in $(notmuch search --output=messages tag:to-screen); do
7676- sender=$(notmuch show --format=json "$msg_id" | grep -o '"From":.*"' | head -1 | grep -Eo '[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}')
9494+# Process feed list against to-screen emails
9595+while IFS= read -r email; do
9696+ [ -z "$email" ] && continue
9797+ count=$(notmuch count from:"$email" tag:to-screen)
9898+ if [ "$count" -gt 0 ]; then
9999+ printf " Tagging %d emails from %s as feed\n" "$count" "$email"
100100+ notmuch tag +feed -to-screen -inbox -- from:"$email" tag:to-screen
101101+ fi
102102+done < "$feed_list"
771037878- if grep -Fxq "$sender" "$screened_in"; then
7979- notmuch tag +screened-in +inbox -to-screen -- id:"$msg_id"
8080- printf "Moved %s back to inbox (screened-in)\n" "$msg_id"
8181- elif grep -Fxq "$sender" "$screened_out"; then
8282- notmuch tag +screened-out +spam -to-screen -inbox -- id:"$msg_id"
8383- printf "Tagged %s as screened-out\n" "$msg_id"
8484- elif grep -Fxq "$sender" "$feed_list"; then
8585- notmuch tag +feed -to-screen -inbox -- id:"$msg_id"
8686- printf "Tagged %s as feed\n" "$msg_id"
8787- elif grep -Fxq "$sender" "$papertrail_list"; then
8888- notmuch tag +papertrail -to-screen -inbox -- id:"$msg_id"
8989- printf "Tagged %s as papertrail\n" "$msg_id"
104104+# Process papertrail list against to-screen emails
105105+while IFS= read -r email; do
106106+ [ -z "$email" ] && continue
107107+ count=$(notmuch count from:"$email" tag:to-screen)
108108+ if [ "$count" -gt 0 ]; then
109109+ printf " Tagging %d emails from %s as papertrail\n" "$count" "$email"
110110+ notmuch tag +papertrail -to-screen -inbox -- from:"$email" tag:to-screen
90111 fi
9191-done
112112+done < "$papertrail_list"
9211393114printf "Notmuch screening completed at $(date '+%Y-%m-%d %H:%M:%S')\n"
94115printf -- "####################################\n"