commit 584326bd9ad19458992754131ec26d7f5c697311 Author: Jason SECULA Date: Tue Jun 17 16:12:53 2025 +0200 initial commit diff --git a/ThunderbirdConfEditor.psm1 b/ThunderbirdConfEditor.psm1 new file mode 100644 index 0000000..3198831 --- /dev/null +++ b/ThunderbirdConfEditor.psm1 @@ -0,0 +1,304 @@ +# Get profile folders +function TBGetProfiles{ + $profilesIni = "$env:APPDATA" + "/Thunderbird/profiles.ini" + $profiles = @() + + foreach($line in (Get-Content $profilesIni)){ + if($line -match '^Path=(.*)$'){ + $path = $matches[1] + + if($path -match '^Profiles/.*$'){ + $path = "$env:APPDATA" + '/Thunderbird/' + $path + if(Test-Path $path){ + $profiles += @($path) + } + }else{ + if(Test-Path $path){ + $profiles += @($path) + } + } + } + } + + # return every profile path for every profile found in Thunderbird profiles directory + return $profiles +} + +# Get prefs.js config content +function TBGetConfig{ + param( + [Parameter(Mandatory=$true)] + [string]$profilePath + ) + + # Verify that profilePath exists + if(Test-Path $profilePath){ + # Check if prefs.js exists + $prefsJS = $profilePath + "/prefs.js" + if((Test-Path $prefsJS)){ + # Return content of prefs.js + return (Get-Content -Path $prefsJS) + } + } +} + +function TBRevertBackup{ + param( + [string]$profilePath + ) + # Searching for old backups + $nums = Get-ChildItem -Path $profilePath | Where Name -Like "prefs-*.js" | foreach {$_.Name.Split("-")[1].Split(".")[0]} + if($nums.length -ne 0){ + $numlist = @() + + # getting backups number + foreach($num in $nums){ + $numlist += [int]$num + } + + # getting last backup number + $lastbackup = $numlist | sort | select -Last 1 + }else{ + # no backup found + Write-Host "No backup found." + } + Move-Item "$profilePath\prefs-$lastbackup.js" "$profilePath\prefs.js" -Force +} + +# Get prefs.js config content +function TBSaveConfig{ + param( + [Parameter(Mandatory=$true)] + [string]$profilePath, + [Parameter(Mandatory=$true)] + [array]$configuration + ) + + # Verify that profilePath exists + if(Test-Path $profilePath){ + # Check if prefs.js exists + $prefsJS = $profilePath + "/prefs.js" + if((Test-Path $prefsJS)){ + TBCreateConfigBackup $profilePath + } + Set-Content $configuration -Path $prefsJS + } +} + +# Create a backup of prefs.js with incrementing number +function TBCreateConfigBackup{ + param( + [Parameter(Mandatory=$true)] + [string]$profilePath + ) + + # Searching for old backups + $nums = Get-ChildItem -Path $profilePath | Where Name -Like "prefs-*.js" | foreach {$_.Name.Split("-")[1].Split(".")[0]} + if($nums.length -ne 0){ + $numlist = @() + + # getting backups number + foreach($num in $nums){ + $numlist += [int]$num + } + + # getting last backup number + $lastbackup = $numlist | sort | select -Last 1 + $lastbackup++ + }else{ + # no backup found + $lastbackup = 1 + } + + $backupSource = $profilePath + "/prefs.js" + $backupDestination = $profilePath + "/prefs-" + $lastbackup + ".js" + + # Creating new backup + Copy-Item -Path $backupSource -Destination $backupDestination +} + +# Retrieve every mail account settings for every account +# or only one with the optional "id" parameter +function TBGetMailAccountSettings{ + param( + [Parameter(Mandatory=$true)] + [array]$configuration, + $id + ) + + # Pattern based on id set or every id + if($id -ne $null){ + $searchPattern = "^user_pref\(`"mail\.identity\.id"+$id+"\." + }else{ + $searchPattern = "^user_pref\(`"mail\.identity\.id[0-9]+\." + } + + $lines = $configuration | Select-String $searchPattern | foreach{ $_.Line } + + $mailAccounts = [ordered]@{} + + foreach($line in $lines){ + # This allow us to used $matches to get account id, parameter name and parameter value + if($line -match "^user_pref\(`"mail\.identity\.id([0-9]+)\.([^`"]+)`", ([^\)]+)\);"){ + $mailAccount = @{$matches[2] = $matches[3]} + } + # We add every account to a hashtable that will be returned + $mailAccounts[$matches[1]] += $mailAccount + } + + return $mailAccounts +} + +function TBGetTagsSettings{ + param( + [Parameter(Mandatory=$true)] + [array]$configuration + ) + + $searchPattern = "^user_pref\(`"mailnews\.tags\." + + $lines = $configuration | Select-String $searchPattern | foreach{ $_.Line } + + return $lines +} + +function TBDeleteTagsSettings{ + param( + [Parameter(Mandatory=$true)] + [array]$configuration + ) + + $searchPattern = "^user_pref\(`"mailnews\.tags\." + + $configuration = $configuration | Select-String $searchPattern -NotMatch | foreach{ $_.Line } + + return $configuration +} + +# Update mail account setting in prefs.js +function TBSetMailAccountSettings{ + param( + [Parameter(Mandatory=$true)] + [array]$configuration, + [Parameter(Mandatory=$true)] + [string]$id, + [Parameter(Mandatory=$true)] + [hashtable]$settings + ) + + foreach($setting in $settings.getEnumerator()){ + $parameterName = $setting.key + + # Create search pattern with id and parameterName + $searchPattern = "user_pref(`"mail.identity.id" + $id +"." + $parameterName + "`"" + # Search for matching line in conf + $lineInConf = $configuration | Select-String $searchPattern -SimpleMatch | foreach{ $_.Line } + + # format new value based on type of value (boolean, int, string) + if((($setting.value -ne "false") -and ($setting.value -ne "true")) -and ($setting.value -is [string])){ + # add "" for strings only + $parameterValue = "`""+$setting.value+"`"" + }else{ + $parameterValue = $setting.value + } + + # Create line to set in conf with new value + $lineToSet = "user_pref(`"mail.identity.id" + $id + "." + $parameterName + "`", " + $parameterValue + ");" + + # Check if the line already exist in the conf with the same value + # and replace it if it does and add it if it does not exist + if($lineInConf -ne $null){ + if($lineInConf -ne $lineToSet){ + $configuration = $configuration.replace($lineInConf, $lineToSet) + } + }else{ + $configuration += $lineToSet + } + } + # return new conf + return $configuration +} + +function TBSetGlobalSettings{ + param( + [Parameter(Mandatory=$true)] + [array]$configuration, + [Parameter(Mandatory=$true)] + [hashtable]$settings + ) + foreach($setting in $settings.getEnumerator()){ + $parameterName = $setting.key + + # Create search pattern with id and parameterName + $searchPattern = "user_pref(`"" + $parameterName + "`"" + # Search for matching line in conf + $lineInConf = $configuration | Select-String $searchPattern -SimpleMatch | foreach{ $_.Line } + + # format new value based on type of value (boolean, int, string) + if((($setting.value -ne "false") -and ($setting.value -ne "true")) -and ($setting.value -is [string])){ + # add "" for strings only + $parameterValue = "`""+$setting.value+"`"" + }else{ + $parameterValue = $setting.value + } + + # Create line to set in conf with new value + $lineToSet = "user_pref(`"" + $parameterName + "`", " + $parameterValue + ");" + + # Check if the line already exist in the conf with the same value + # and replace it if it does and add it if it does not exist + if($lineInConf -ne $null){ + if($lineInConf -ne $lineToSet){ + $configuration = $configuration.replace($lineInConf, $lineToSet) + } + }else{ + $configuration += $lineToSet + } + } + return $configuration +} + +# Delete mail account setting in prefs.js +function TBDeleteMailAccountSettings{ + param( + [Parameter(Mandatory=$true)] + [array]$configuration, + [Parameter(Mandatory=$true)] + [string]$id, + [Parameter(Mandatory=$true)] + [array]$settings + ) + + foreach($setting in $settings.getEnumerator()){ + $parameterName = $setting + + # Create search pattern with id and parameterName + $searchPattern = "user_pref(`"mail.identity.id" + $id +"." + $parameterName + "`"" + # Search for not matching line in conf + $configuration = $configuration | Select-String $searchPattern -NotMatch -SimpleMatch | foreach{ $_.Line } + } + # return new conf + return $configuration +} + +# Delete setting in prefs.js +function TBDeleteGlobalSettings{ + param( + [Parameter(Mandatory=$true)] + [array]$configuration, + [Parameter(Mandatory=$true)] + [array]$settings + ) + + foreach($setting in $settings.getEnumerator()){ + $parameterName = $setting + $setting + + # Create search pattern with id and parameterName + $searchPattern = "user_pref(`"" + $parameterName + "`"" + # Search for not matching line in conf + $configuration = $configuration | Select-String $searchPattern -NotMatch -SimpleMatch | foreach{ $_.Line } + } + # return new conf + return $configuration +}