PhiLho
Joined: 27 Dec 2005 Posts: 6721 Location: France (near Paris)
|
Posted: Tue May 13, 2008 10:50 am Post subject: Undelete Thunderbird messages |
|
|
Hello, long time not coming back here!
I fear I won't come back often, so I might miss questions, requests and such...
Oh, I will just activate the notify option, for once.
Recently, I goofed in Thunderbird. I don't know how, but I managed to do Ctrl+A quickly followed by Shift+Delete in my main Inbox... o_O
If you don't know what it does, I advise not to try: it selects all messages, then delete them straight, without putting them to the trash can.
After few seconds of confusion, I didn't lose my self-control and went to my Thunderbird profile folder. I made a copy of the Inbox file (15MB! and I dispatch to lot of folders...).
If you don't know yet, a folder in Thunderbird is just a file in the Unix mbox (mailbox) format, which is plain text in Mime format. That's one of the main reasons why I switched from Outlook (proprietary binary format) to Thunderbird.
One nice thing in Thunderbird is that it doesn't delete messages immediately from the file, as it can be time consuming (moving up some MB of data in the file). So instead it overwrites some bytes, to mark the message as deleted.
That's why from time to time, it asks you if you want to compact your folders: it rewrites the file after removing all the messages marked as deleted.
Note it is a good idea to backup these files from time to time (I didn't do it... ).
So I had this big file full of dead messages: if I rename it, it appears as a new folder, but empty. A quick glance showed there were these X-Mozilla-Status and X-Mozilla-Status2 fields in the headers. First hit in Google is very helpful: http://www.eyrich-net.org/mozilla/X-Mozilla-Status.html?en
So to restore all my messages, all I had to do is to change in the file all the X-Mozilla-Status fields to set off the bit 0x0008.
Boy, I am rusty in my AHK coding! I made several bloopers before making it working (writing "line" all over the file, forgetting to set the hexa mode, etc.).
Advice: make a copy, no, several copies of the file you need to manipulate. I am not responsible if you loose all your messages!
And operate while Thunderbird is closed.
Note: you can delete the .msf file of same name as your folder. That's the Mail Summary File, ie. an index for quick access to essential data of the folder (number of messages, of unread messages, size, etc.). If absent, it is automatically re-created.
No more chatting, the code:
| Code: | /*
UndeleteMessages.ahk
Read a mailbox file from Thunderbird (mbox format, with X-Mozilla-Status lines)
and undelete all messages.
See http://www.eyrich-net.org/mozilla/X-Mozilla-Status.html?en for the meaning of the bits of this status.
// by Philippe Lhoste <PhiLho(a)GMX.net> http://Phi.Lho.free.fr
// File/Project history:
1.00.000 -- 2008/05/13 (PL) -- Creation.
*/
/* Copyright notice: See the PhiLhoSoftLicence.txt file for details.
This file is distributed under the zlib/libpng license.
Copyright (c) 2008 Philippe Lhoste / PhiLhoSoft
*/
#SingleInstance Force
#NoEnv
SetFormat Integer, Hex
; The mail folder (a file), of the name of your folder in TB
fileIn = E:\Documents\Net\Thunderbird\Profiles\ProfileCode.default\Mail\AccountName\Inbox
; A different file! Can go elsewhere, at the end you will replace the previous file with this one
fileOut = E:\Documents\Net\Thunderbird\Profiles\ProfileCode.default\Mail\AccountName\InboxUD
; ProfileCode and AccountName depends on your config.
Loop Read, %fileIn%, %fileOut%
{
line := A_LoopReadLine
If (RegExMatch(line, "^X-Mozilla-Status: (\d{4})$", digits) > 0)
{
digits := digits1 & 0xFFF7 ; Erase bit (flag) indicating the message is deleted
digits = 0000%digits%
StringRight digits, digits, 4 ; Format to 4 digits
line = X-Mozilla-Status: %digits%
}
FileAppend %line%`r`n
} |
This will undelete messages voluntarily deleted since last compacting, but that's the least of my concerns... It can also alter body of messages if you have X-Mozilla-Status: nnnn alone in a line in a message, which is rather unlikely...
That's where I see that AHK is very fast: the 15MB file is processed in a few seconds on a 4 years old computer.
If you know what you are doing, you can use this base for other processes: reset all the marked messages, change flags on messages matching a given subject or sender (a bit harder, need buffering of headers), etc.
I hope nobody will have the need to use it, but in case of emergency, it is available! _________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2") |
|