Python script to recover shift-end-deleted mails from Thunderbird inbox file

The Bad

Until recently I was having a bad behavior. I found it easy to delete stuffs from my pc by using shift-delete instead of regular delete. And I learnt my lesson.

Every day at work, I spend some quality time dedicated to filter out my inbox from the unwanted invitations to join ‘Daily frogs'

After years of experience, I had mastered the art of detecting spam only by looking at the subject and senders line. Last Monday morning, I had deleted some of the daily spam from my inbox. Things we so natural.

But later that day, I realized that, during the process of pressing the ‘delete’ key, I had accidentally pressed the ‘End’ key also!!!! so the whole thing became, ‘click’ and then ‘Shift-End-Delete’ and then ‘Enter’, so that every message from the beginning to end was gone in 60 seconds.
I just closed the Thunderbird in panic. :-/

But, Thunderbird is no beast a match for my Python skills!!

I am sharing my lazy Python script that I have managed to scramble to recover my Inbox.
This peice of cake, sorry code, is released under GPL.


 #!/usr/bin/python --tt

import sys
import re


# Python Script to recover deleted Thunderbird mails.
# When you shift-delete the mails in Thunderbird inbox. The mails are not immedeately deleted from the inbox file,
# Instead the status bit field of X-Mozilla-Status: of each message is set as deleted.
# So what we need is, just read all the value in after X-Mozilla-Status: and then clear the bit3 of the binary of the value.
# this script goes through the inbox and, reads status of each message and clear the delete bit in the status.
#The new status is written along with the mail to a new file.So the old one is unchanged....

def main():

fr = open ('./inbox' , 'r')
fw = open('./inbox-fixed', 'w')

for line in fr:
 #print line
 ms_match =re.search(r'X-Mozilla-Status: (\w+)',line)
 if (ms_match):
  msgstatus = ms_match.group(1)
  print "status extracted =", msgstatus
  msgstatus_in_int = int(msgstatus,16) #convert string to number for easy anding
  msgstatus_in_int &= ~(0x8) # clear the bit3 ie 3 in [3210]
  msgstatus_new = hex(msgstatus_in_int) #convert back to string
  msgstatus_new = msgstatus_new[2:] # remove the 0x in the string
  #print "length of len(msgstatus_new) ", len(msgstatus_new)
  if ( len(msgstatus_new) ==1 ):
   msgstatus_new = "000" + msgstatus_new
  elif ( len(msgstatus_new) ==2 ):
   msgstatus_new = "00" + msgstatus_new
  elif ( len(msgstatus_new) ==3 ):
   msgstatus_new = "0" + msgstatus_new
  print "new status=", msgstatus_new
  # line.replace(msgstatus,msgstatus_new) #replace the status
  fw.write( line.replace(msgstatus,msgstatus_new)) #replace text and write to file
 else:
  fw.write(line)
  


if __name__ == "__main__":
main()
  

# end of the python script

2011 വേള്‍ഡ് കപ്പ്‌ ജയിച്ച ടീമില്‍ അങ്കമായ ശ്രീശാന്തി ന്‍റെ പ്രതേക ഇന്റര്‍വ്യൂ

നികേഷ് : എന്ത് തോന്നുന്നു ശ്രീശാന്ത്‌?

ശ്രീ : വളരെ സന്തോഷം തോന്നുന്നു നികേഷ്. എന്‍റെ സ്വഭാവം വെച്ച് ഇതു വരെ ജീവിച്ചിരിക്കുമെന്നു പോലും ഞാന്‍ വിചാരിച്ചതല്ല. മാത്രമല്ല നോക്ക് കൂലി വാങ്ങുന്നത് കേരളത്തില്‍ വലിയ കുറ്റമൊന്നും അലെല്ലോ, അവകാശം അല്ലെ. ജയിച്ചതിനെ കാളും, ഇന്ത്യ തോറ്റില്ല എന്നതാണ് എനിക്ക് കുടുതല്‍ സന്തോഷം തന്നത്. എങ്ങാനും ഞങ്ങള്‍ തൊപ്പി ഇട്ടിരുന്നെങ്ങില്‍, കുഴിച്ചിട്ട മൊട്ടയുടെ എണ്ണം വേച്ചാണേല്‍, ശ്രീലങ്ക ദ്വീപു മൊത്തമായി സുനാമി അടിച്ചു പോയേനെ. മുംബൈയില്‍ കളികാണാന്‍ വന്ന പ്രസിഡന്റ്‌ രാജപക്ഷെ, ഒരു പക്ഷെ അടുത്ത കേരള അസ്സെംബ്ലി തിരഞെടുപിന്നു നമ്മുടെ കൂടെ കേരളത്തില്‍ വോട്ട്
ചെയിതേനെ.

നികേഷ് : ഹഹഹ , അതിരിക്കെട്ടെ ശ്രീശാന്ത്‌ , തങ്ങളുടെ വേള്‍ഡ് കപ്പ്‌ പെര്‍ഫോര്‍മന്‍സ്..... തങ്ങള്‍ എങ്ങനെ വിലയിരുത്തുന്നു ?

ശ്രീ : പൂനം പാണ്ട്ടെ ഇതുവരെ വാക്ക് പാലിച്ചിലെങ്ങിലും, ഞങ്ങള്‍ സച്ചിന് വേണ്ടി ഈ വേള്‍ഡ് കപ്പ്‌ ജയിച്ചു....


Nokia N900 captured a Jaguar in HDR

31st March 2011
Trivandrum

I have been carrying a Nokia N900 mobile phone for some time. I brought it so that I could do some programming stuffs and things like that. But lately I came across some sweet surprises. The N900 can take awesome pictures too.


Nokia N900 has 5 MP sensor with Carl Zeiss lens. The inbuilt camera takes average though decent pictures, I would rather say.
But a Maemo application 'BlessN900' turned my N900 into a something which most DSLR users can't even think of. N900 could take in-camera HDR pictures!! For people who don't know what an HDR or High dynamic range photography is, there is Google.

In-camera HDRs are different stuff. Most of the time an HDR picture is taken using several bracketed shots taken in 3-5 exposures and later combined and tone mapped using a computer. And that is one heck of a boring thing to do, it takes atleast 1/2 hrs to setup the PC, download photos and then the processing itself takes some time depending upon the number of pics and size. BlessN900 app does the bracketing, clicking, and processing, everything from the device itself . The result is, we will be taking more HDR pictures when the circumstances are right for an HDR.

Some of the conditions I have seen that produces awesome HDR photos are.
  1. High contrast areas, eg a bright sky and dark church with dark green moss on the walls.
  2. Metal (cars, bulldozers, train engine) in direct sunlight (Jaguar picture below)
  3. Old deserted alleys with cracked walls.
  4. Shiny glass buildings and bright sky with clouds.
  5. Inside of a room with windows allowing harsh light in.
Moving things are bad for HDR. Taking 3 shots in succession can create problem with moving things like moving cars, walking people, flying birds, trees in wind etc.
Similarly I have seen pitch black areas create too much noise in the dark places in pics, especially in a smaller sensor like in N900's.

Enjoy some of the photos I took using Nokia N900....

Jaguar in Trivandrum






Somelinks
http://maemo.org/downloads/product/Maemo5/blessn900/
http://www.stuckincustoms.com/hdr-tutorial