'''
firefox_session_textareas.py - Recover text area content from Firefox session files

Print out text area content cached by Firefox in the saved session file.

Sample usage:

python firefox_session_textareas.py "~/.firefox/Profiles/*/sessionstore.js"
    -- Print out all text area content found (including the corresponding page URL)

python firefox_session_textareas.py "~/.firefox/Profiles/*/sessionstore.js" spam
    -- Print out all only text areas found to contain the string "spam"
       (or whose corresponding page URL contains that string)
'''

import urllib
import sys
import re

TEXTAREA_PAT = re.compile('{url:"([^"]*)"[^}]*}[^}]*text:"#editor-textarea=([^"]*)"')
HEADING_TPL = '------------ firefox_session_textareas.py - %s ------------'

def textarea_content(s):
    return urllib.unquote(s)

#FIXME: Use optparse, etc to clean up command line handling
sessioninfo = open(sys.argv[1]).read()
try:
    search = sys.argv[2]
except IndexError:
    search = None


for match in TEXTAREA_PAT.finditer(sessioninfo):
    if search is None or search in match.group(0):
        print HEADING_TPL%match.group(1)
        print
        print textarea_content(match.group(2))
        print

#

