28 November 2012

sed to the rescue

Don't know how, but I managed to delete all the emails in my Thunderbird's inbox this morning: it was like I hit Ctrl + A and then Shift + Del, without noticing it. I only noticed there are no messages. The first thing to check was the Inbox file ~/.thunderbird/[random_chars\/Mail/Local Folders/Inbox. It was there and big (1.75GB) with readable (vi or such) content. OK, it should be recoverable.

What happens here: when you delete an email in Thunderbird (or SeaMonkey), it is not deleted completely, only the value of one of its headers, X-Mozilla-Status is changed. The actual content is removed only when you compact the folder. I used to have the mail client to automatically compact its folders when some space can be saved, but it feels like it start compacting (and freezing the app) always when you have work to do. So I disabled that.

So the first step was to investigate the status of the messages in my Inbox:

grep "X-Mozilla-Status:" Inbox | sort -u

The result was something like:

X-Mozilla-Status: 0009
X-Mozilla-Status: 000b
X-Mozilla-Status: 0019
X-Mozilla-Status: 001b
X-Mozilla-Status: 1009
X-Mozilla-Status: 100b
X-Mozilla-Status: 1019
X-Mozilla-Status: 101b

You need a bit of knowledge about those statuses: 0009 means "read and deleted", 000b means "read, replied and deleted" and so on.

So I had to parse the Inbox file and replace messages. Wile a tool like mboxgrep may make some things easier, sed is good enough. I created a statuses.txt file with the following content:

s/X-Mozilla-Status: 0009/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 000b/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 0019/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 001b/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 1009/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 1009/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 100b/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 1019/X-Mozilla-Status: 0001/g
s/X-Mozilla-Status: 101b/X-Mozilla-Status: 0001/g

Then feed it to seed:

sed -f statuses.txt Inbox > Inbox1

Wait for a while and you have a new Inbox1 with all the messages undeleted (0001 is "read", if you want "unread", use 0000 instead). Delete the old Inbox and rename Inbox1 to Inbox. Open Thunderbird and everything works.

 
{"cc_re__cc_re__cc_by_sa_3_0": ["</span>\n<a class='home-link' href='http://nicubunu.blogspot.com/'>Home</a>\n</div>\n<div class='clear'></div>\n<div class='post-feeds'>\n<div class='feed-links'>\nSubscribe to:\n<a class='feed-link' href='http://nicubunu.blogspot.com/feeds/6282218708693344359/comments/default' target='_blank' type='application/atom+xml'>Post Comments (Atom)</a>\n</div>\n</div>\n<script type=\"text/javascript\">window.___gcfg = {'lang': 'en'};</script>\n</div><div class='widget HTML' id='HTML3'>\n<div class='widget-content'>\n<a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">\n<img alt=\"Creative Commons License\" style=\"border-width:0\" src=\"http://i.creativecommons.org/l/by-sa/3.0/88x31.png\"/>\n</a>\n<br/>\n<span xmlns:dc=\"http://purl.org/dc/elements/1.1/\" property=\"dc:title\" href=\"http://purl.org/dc/dcmitype/InteractiveResource\" rel=\"dc:type\">Nicu's Blog</span> by \n<a property=\"cc:attributionName\" href=\"http://nicubunu.ro/\" rel=\"cc:attributionURL\" xmlns:cc=\"http://creativecommons.org/ns#\">Nicu Buculei</a> is licensed under a \n<a href=\"http://creativecommons.org/licenses", "mons.org/licenses/by-sa/3.0/\" rel=\"license\">\n<img alt=\"Creative Commons License\" style=\"border-width:0\" src=\"http://i.creativecommons.org/l/by-sa/3.0/88x31.png\"/>\n</a>\n<br/>\n<span xmlns:dc=\"http://purl.org/dc/elements/1.1/\" property=\"dc:title\" href=\"http://purl.org/dc/dcmitype/InteractiveResource\" rel=\"dc:type\">Nicu's Blog</span> by \n<a property=\"cc:attributionName\" href=\"http://nicubunu.ro/\" rel=\"cc:attributionURL\" xmlns:cc=\"http://creativecommons.org/ns#\">Nicu Buculei</a> is licensed under a \n<a href=\"http://creativecommons.org/licenses/by-sa/3.0/\" rel=\"license\">Creative Commons Attribution-Share Alike 3.0 License</a>.\n</div>\n<div class='clear'></div>\n<span class='widget-item-control'>\n<span class='item-control blog-admin'>\n<a class='quickedit' href='//www.blogger.com/rearrange?blogID=5797212&widgetType=HTML&widgetId=HTML3&action=editWidget&sectionId=main' onclick='return _WidgetManager._PopupConfig(document.getElementById(\"HTML3\"));' target='configHTML3' title='Edit'>\n<img alt='' height='18' src='http://img1.blogblog.com/img/icon18_wrench_allbkg.png' w"]}