Skip to content

Commit

Permalink
Vendors Arkenfox updater/prefsCleaner scripts
Browse files Browse the repository at this point in the history
Also see related : arkenfox/user.js#910

> closes #1, #10, #29, #35 and #41
  • Loading branch information
HorlogeSkynet committed Nov 7, 2024
1 parent e2b683a commit ae9edff
Show file tree
Hide file tree
Showing 4 changed files with 1,052 additions and 0 deletions.
134 changes: 134 additions & 0 deletions prefsCleaner.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
@ECHO OFF & SETLOCAL DisableDelayedExpansion
TITLE prefs.js cleaner

REM ## prefs.js cleaner for Windows
REM ## /!\ This script is a vendored/adapted version of Arkenfox own prefsCleaner.bat /!\
REM ## author: @claustromaniac
REM ## version: 2.7

CD /D "%~dp0"

IF /I "%~1"=="-unattended" (SET _ua=1)

:begin
ECHO:
ECHO:
ECHO ########################################
ECHO #### prefs.js cleaner for Windows ####
ECHO #### by claustromaniac ####
ECHO #### v2.7 ####
ECHO ########################################
ECHO:
CALL :message "This script should be run from your Thunderbird profile directory."
ECHO It will remove any entries from prefs.js that also exist in user.js.
CALL :message "This will allow inactive preferences to be reset to their default values."
ECHO This Thunderbird profile shouldn't be in use during the process.
CALL :message ""
TIMEOUT 1 /nobreak >nul

IF NOT DEFINED _ua (
CHOICE /C SHE /N /M "Start [S] Help [H] Exit [E]"
CLS
IF ERRORLEVEL 3 (EXIT /B)
IF ERRORLEVEL 2 (GOTO :showhelp)
)
IF NOT EXIST "user.js" (CALL :abort "user.js not found in the current directory." 30)
IF NOT EXIST "prefs.js" (CALL :abort "prefs.js not found in the current directory." 30)
CALL :strlenCheck
CALL :FFcheck

CALL :message "Backing up prefs.js..."
FOR /F "delims=" %%# IN ('powershell get-date -format "{yyyyMMdd_HHmmss}"') DO @SET ldt=%%#
COPY /B /V /Y prefs.js "prefs-backup-%ldt%.js"

CALL :message "Cleaning prefs.js..."
CALL :cleanup
CALL :message "All done!"
TIMEOUT 5 >nul
ENDLOCAL
EXIT /B

REM ########## Abort Function ###########
:abort
CALL :message %1
TIMEOUT %~2 >nul
EXIT
REM ########## Message Function #########
:message
ECHO:
ECHO: %~1
ECHO:
GOTO :EOF
REM ### string length Check Function ####
:strlenCheck
SET /a cnt=0
setlocal ENABLEDELAYEDEXPANSION
FOR /F "tokens=1,* delims=:" %%G IN ('FINDSTR /N "^" prefs.js') DO (
ECHO:%%H >nul
SET /a cnt += 1
IF /I "%%G" NEQ "!cnt!" (
ECHO:
CALL :message "ERROR: line !cnt! in prefs.js is too long."
(CALL :abort "Aborting ..." 30)
)
)
endlocal
GOTO :EOF
REM ####### Thunderbird Check Function ######
:FFcheck
TASKLIST /FI "IMAGENAME eq thunderbird.exe" 2>NUL | FIND /I /N "thunderbird.exe">NUL
IF NOT ERRORLEVEL 1 (
CLS
CALL :message "Thunderbird is still running."
ECHO If you're not currently using this profile you can continue, otherwise
CALL :message "close Thunderbird first!"
ECHO:
PAUSE
CLS
CALL :message "Resuming..."
TIMEOUT 5 /nobreak >nul
)
GOTO :EOF
REM ######### Cleanup Function ##########
:cleanup
FOR /F tokens^=2^ delims^=^'^" %%G IN ('FINDSTR /R /C:"^[^\"']*user_pref[ ]*\([ ]*[\"'][^\"']*[\"'][ ]*," user.js') DO (
IF NOT ""=="%%G" (SET "[%%G]=1")
)
(
FOR /F "tokens=1,* delims=:" %%G IN ('FINDSTR /N "^" prefs.js') DO (
IF ""=="%%H" (
ECHO:
) ELSE (
FOR /F tokens^=1^,2^ delims^=^"^' %%I IN ("%%H") DO (
IF NOT DEFINED [%%J] (ECHO:%%H)
)
)
)
)>tempcleanedprefs
MOVE /Y tempcleanedprefs prefs.js
GOTO :EOF
REM ############### Help ##################
:showhelp
MODE 80,34
CLS
CALL :message "This script creates a backup of your prefs.js file before doing anything."
ECHO It should be safe, but you can follow these steps if something goes wrong:
ECHO:
CALL :message " 1. Make sure Thunderbird is closed."
ECHO 2. Delete prefs.js in your profile folder.
CALL :message " 3. Delete Invalidprefs.js if you have one in the same folder."
ECHO 4. Rename or copy your latest backup to prefs.js.
CALL :message " 5. Run Thunderbird and see if you notice anything wrong with it."
ECHO 6. If you do notice something wrong, especially with your extensions,
CALL :message " and/or with the UI, go to about:support, and restart Thunderbird with"
ECHO add-ons disabled. Then, restart it again normally, and see if the
CALL :message " problems were solved."
ECHO:
CALL :message "If you are able to identify the cause of your issues, please bring it up"
ECHO on thunderbird user.js GitHub repository.
ECHO:
ECHO:
PAUSE
CLS
GOTO :begin
REM #####################################
186 changes: 186 additions & 0 deletions prefsCleaner.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
#!/usr/bin/env bash

## prefs.js cleaner for Linux/Mac
## /!\ This script is a vendored/adapted version of Arkenfox own prefsCleaner.sh /!\
## author: @claustromaniac
## version: 2.1

## special thanks to @overdodactyl and @earthlng for a few snippets that I stol..*cough* borrowed from the updater.sh

## DON'T GO HIGHER THAN VERSION x.9 !! ( because of ASCII comparison in update_prefsCleaner() )

readonly CURRDIR=$(pwd)

## get the full path of this script (readlink for Linux, greadlink for Mac with coreutils installed)
SCRIPT_FILE=$(readlink -f "${BASH_SOURCE[0]}" 2>/dev/null || greadlink -f "${BASH_SOURCE[0]}" 2>/dev/null)

## fallback for Macs without coreutils
[ -z "$SCRIPT_FILE" ] && SCRIPT_FILE=${BASH_SOURCE[0]}


AUTOUPDATE=true
QUICKSTART=false

## download method priority: curl -> wget
DOWNLOAD_METHOD=''
if command -v curl >/dev/null; then
DOWNLOAD_METHOD='curl --max-redirs 3 -so'
elif command -v wget >/dev/null; then
DOWNLOAD_METHOD='wget --max-redirect 3 --quiet -O'
else
AUTOUPDATE=false
echo -e "No curl or wget detected.\nAutomatic self-update disabled!"
fi

fQuit() {
## change directory back to the original working directory
cd "${CURRDIR}"
[ "$1" -eq 0 ] && echo -e "\n$2" || echo -e "\n$2" >&2
exit $1
}

fUsage() {
echo -e "\nUsage: $0 [-ds]"
echo -e "
Optional Arguments:
-s Start immediately
-d Don't auto-update prefsCleaner.sh"
}

download_file() { # expects URL as argument ($1)
declare -r tf=$(mktemp)

$DOWNLOAD_METHOD "${tf}" "$1" &>/dev/null && echo "$tf" || echo '' # return the temp-filename or empty string on error
}

fFF_check() {
# there are many ways to see if thunderbird is running or not, some more reliable than others
# this isn't elegant and might not be future-proof but should at least be compatible with any environment
while [ -e lock ]; do
echo -e "\nThis Thunderbird profile seems to be in use. Close Thunderbird and try again.\n" >&2
read -r -p "Press any key to continue."
done
}

## returns the version number of a prefsCleaner.sh file
get_prefsCleaner_version() {
echo "$(sed -n '5 s/.*[[:blank:]]\([[:digit:]]*\.[[:digit:]]*\)/\1/p' "$1")"
}

## updates the prefsCleaner.sh file based on the latest public version
update_prefsCleaner() {
declare -r tmpfile="$(download_file 'https://raw.githubusercontent.com/HorlogeSkynet/thunderbird-user.js/master/prefsCleaner.sh')"
[ -z "$tmpfile" ] && echo -e "Error! Could not download prefsCleaner.sh" && return 1 # check if download failed

[[ $(get_prefsCleaner_version "$SCRIPT_FILE") == $(get_prefsCleaner_version "$tmpfile") ]] && return 0

mv "$tmpfile" "$SCRIPT_FILE"
chmod u+x "$SCRIPT_FILE"
"$SCRIPT_FILE" "$@" -d
exit 0
}

fClean() {
# the magic happens here
prefs="@@"
prefexp="user_pref[ ]*\([ ]*[\"']([^\"']+)[\"'][ ]*,"
while read -r line; do
if [[ "$line" =~ $prefexp && $prefs != *"@@${BASH_REMATCH[1]}@@"* ]]; then
prefs="${prefs}${BASH_REMATCH[1]}@@"
fi
done <<< "$(grep -E "$prefexp" user.js)"

while IFS='' read -r line || [[ -n "$line" ]]; do
if [[ "$line" =~ ^$prefexp ]]; then
if [[ $prefs != *"@@${BASH_REMATCH[1]}@@"* ]]; then
echo "$line"
fi
else
echo "$line"
fi
done < "$1" > prefs.js
}

fStart() {
if [ ! -e user.js ]; then
fQuit 1 "user.js not found in the current directory."
elif [ ! -e prefs.js ]; then
fQuit 1 "prefs.js not found in the current directory."
fi

fFF_check
mkdir -p prefsjs_backups
bakfile="prefsjs_backups/prefs.js.backup.$(date +"%Y-%m-%d_%H%M")"
mv prefs.js "${bakfile}" || fQuit 1 "Operation aborted.\nReason: Could not create backup file $bakfile"
echo -e "\nprefs.js backed up: $bakfile"
echo "Cleaning prefs.js..."
fClean "$bakfile"
fQuit 0 "All done!"
}


while getopts "sd" opt; do
case $opt in
s)
QUICKSTART=true
;;
d)
AUTOUPDATE=false
;;
esac
done

## change directory to the Thunderbird profile directory
cd "$(dirname "${SCRIPT_FILE}")"

# Check if running as root and if any files have the owner as root/wheel.
if [ "${EUID:-"$(id -u)"}" -eq 0 ]; then
fQuit 1 "You shouldn't run this with elevated privileges (such as with doas/sudo)."
elif [ -n "$(find ./ -user 0)" ]; then
printf 'It looks like this script was previously run with elevated privileges,
you will need to change ownership of the following files to your user:\n'
find . -user 0
fQuit 1
fi

[ "$AUTOUPDATE" = true ] && update_prefsCleaner "$@"

echo -e "\n\n"
echo " ╔══════════════════════════╗"
echo " ║ prefs.js cleaner ║"
echo " ║ by claustromaniac ║"
echo " ║ v2.1 ║"
echo " ╚══════════════════════════╝"
echo -e "\nThis script should be run from your Thunderbird profile directory.\n"
echo "It will remove any entries from prefs.js that also exist in user.js."
echo "This will allow inactive preferences to be reset to their default values."
echo -e "\nThis Thunderbird profile shouldn't be in use during the process.\n"

[ "$QUICKSTART" = true ] && fStart

echo -e "\nIn order to proceed, select a command below by entering its corresponding number.\n"

select option in Start Help Exit; do
case $option in
Start)
fStart
;;
Help)
fUsage
echo -e "\nThis script creates a backup of your prefs.js file before doing anything."
echo -e "It should be safe, but you can follow these steps if something goes wrong:\n"
echo "1. Make sure Thunderbird is closed."
echo "2. Delete prefs.js in your profile folder."
echo "3. Delete Invalidprefs.js if you have one in the same folder."
echo "4. Rename or copy your latest backup to prefs.js."
echo "5. Run Thunderbird and see if you notice anything wrong with it."
echo "6. If you do notice something wrong, especially with your extensions, and/or with the UI, go to about:support, and restart Thunderbird with add-ons disabled. Then, restart it again normally, and see if the problems were solved."
echo -e "If you are able to identify the cause of your issues, please bring it up on the thunderbird user.js GitHub repository.\n"
;;
Exit)
fQuit 0
;;
esac
done

fQuit 0
Loading

0 comments on commit ae9edff

Please sign in to comment.