Thunderbird Filter Rules Fix Script (Post-Migration) on ubuntu.
#!/bin/bash
# Thunderbird Filter Rules Fix Script (Post-Migration)
#
# When migrating Thunderbird to a new machine and copying 'msgFilterRules.dat'
# inside the Mail directory, the destination folders (such as Inbox, Sent, etc.)
# often appear blank in the Message Filters UI. This script was created to fix that issue.
#
# Tested as a Bash script on Ubuntu, it successfully restores folder mappings,
# allowing filters to sort messages perfectly without throwing any folder-not-found errors.
#
# This could be incredibly useful for power users (like myself, a user since the Netscape era)
# who manage hundreds of complex filter rules.
#
# *Please note: This is an experimental version.*
# ==============================================================================
# Thunderbird msgFilterRules.dat Auto-Fix Script (English Version)
# ==============================================================================
TARGET_FILE="msgFilterRules.dat"
BACKUP_FILE="msgFilterRules.dat-bkup$(date +%Y%m%d_%H%M%S)"
# Parse arguments (Check for -i option)
INTERACTIVE_MODE=0
while getopts "i" opt; do
case "$opt" in
i) INTERACTIVE_MODE=1 ;;
*) echo "Usage: $0 [-i]"; exit 1 ;;
esac
done
clear
echo "=================================================================="
echo "⚠️ [CRITICAL] BEFORE YOU PROCEED!"
echo " Please CLOSE Thunderbird completely."
echo " If it remains open, changes will be overwritten and lost."
echo "=================================================================="
read -p "Have you closed Thunderbird? (y/n): " CONFIRM
if [ "$CONFIRM" != "y" ] && [ "$CONFIRM" != "Y" ]; then
echo "[Aborted] Operation cancelled. Close Thunderbird and try again."
exit 1
fi
if [ ! -f "$TARGET_FILE" ]; then
echo "[Error] $TARGET_FILE not found."
echo "Please run this script in the directory containing $TARGET_FILE."
exit 1
fi
echo ""
echo "--- Starting Thunderbird Filter Fix ---"
CURRENT_DIR=$(pwd -P)
CURRENT_FOLDER=$(basename "$CURRENT_DIR")
HINT_DOMAIN=$(echo "$CURRENT_FOLDER" | sed -E 's/^(pop|mail|imap|smtp)\.//I')
echo "[INPUT] Enter your NEW (current) email address."
echo " (Example based on your current folder: someone@${HINT_DOMAIN} )"
read -p "Email Address: " EMAIL_INPUT
if [[ "$EMAIL_INPUT" != *@* ]]; then
echo "[Error] Invalid email address format."
exit 1
fi
# Extract username and domain
USER_PART=$(echo "$EMAIL_INPUT" | cut -d'@' -f1)
SERVER_DOMAIN=$(echo "$EMAIL_INPUT" | cut -d'@' -f2)
# Verify if the email matches the current directory name
MATCH_OK=0
case "$CURRENT_FOLDER" in
*"$SERVER_DOMAIN"*) MATCH_OK=1 ;;
*"$USER_PART"*) MATCH_OK=1 ;;
esac
if [ "$MATCH_OK" -eq 0 ]; then
echo ""
echo "❌ [Error] The entered email address does not match the current directory!"
echo " Current Directory: $CURRENT_FOLDER"
echo " Entered Email : $EMAIL_INPUT"
echo " Please make sure you are running the script in the correct profile folder."
exit 1
fi
# Generate the correct virtual internal path for Thunderbird
NEW_PATH="mailbox://${USER_PART}@${CURRENT_FOLDER}/"
cp "$TARGET_FILE" "$BACKUP_FILE"
echo "[Info] Backup created successfully: $BACKUP_FILE"
TMP_FILE="msgFilterRules.tmp"
> "$TMP_FILE"
# ==============================================================================
# Process Execution Modes
# ==============================================================================
if [ "$INTERACTIVE_MODE" -eq 1 ]; then
echo "=================================================================="
echo "🔍 [-i] Interactive Mode: Confirming modifications line by line."
echo "=================================================================="
# Save standard input to file descriptor 3 to handle user input during file read
exec 3<&0
while IFS= read -r line; do
# Enable rules (Fix enabled="no" to "yes")
if echo "$line" | grep -q '^enabled="no"'; then
new_line='enabled="yes"'
echo ""
echo "【Enable Filter】Would you like to activate this filter?"
echo " Old: $line"
echo " New: $new_line"
read -p " -> Apply change? (y/n): " ans <&3
if [ "$ans" = "y" ] || [ "$ans" = "Y" ]; then line="$new_line"; fi
fi
# Fix actionValue path (mailbox://)
if echo "$line" | grep -q 'mailbox://'; then
new_line=$(echo "$line" | sed -E "s|mailbox://[^\"/]+/|${NEW_PATH}|g")
if [ "$line" != "$new_line" ]; then
echo ""
echo "【Path Change】Is it OK to fix this actionValue path?"
echo " Old: $line"
echo " New: $new_line"
read -p " -> Apply change? (y/n): " ans <&3
if [ "$ans" = "y" ] || [ "$ans" = "Y" ]; then
line="$new_line"
echo " => [Applied]"
else
echo " => [Skipped]"
fi
fi
fi
echo "$line" >> "$TMP_FILE"
done < "$BACKUP_FILE"
exec 3<&-
else
# Batch Mode (Default)
echo "=================================================================="
echo "🚀 Batch Mode: Applying changes automatically..."
echo "=================================================================="
cat "$BACKUP_FILE" | \
sed 's/^enabled="no"/enabled="yes"/' | \
sed -E "s|mailbox://[^\"/]+/|${NEW_PATH}|g" > "$TMP_FILE"
fi
# Replace the target file with the fixed one
mv "$TMP_FILE" "$TARGET_FILE"
echo ""
echo "[Success] Finished! $TARGET_FILE has been successfully updated."
echo "Please start Thunderbird and verify your message filter destinations."