#!/usr/bin/env python
# -*- coding: utf-8 -*-

# lsoclean

# copyright © 2010 by Jerry Seeger
# http://muddledramblings.com
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# I'm supposed to have included a copy of the GPL here, but I didn't.
# It's a zillion times longer than the code, for crying out loud.
# To read the license, please see http://www.gnu.org/licenses/

# Instructions
# 1. Save this file to your hard drive in a convenient location.
# 2. Alter the values under User Settings below.
# 3. Tell you system that this is a file that can be executed: chmod +x /path/to/lsoclean
# 4. Run the script by typing /path/to/lsoclean or, better yet, set up a crontab entry to run it automatically

#
# User Settings
#     

paranoid = False        # if set to True, deletes site-specific settings, which could include settings you make.
superParanoid = False   # Not recommended - only matters if paranoid is True.
                        # Deletes the general settings file, which will also undo your Flash privacy and storage settings.
    
# domains you do NOT want to delete.
# In the example, localhost, and pandora.com will not have their LSO's deleted.
keeperDomains = [
        'localhost',
        'pandora.com',
]


#
#    Don't change stuff below this line unless you know how to fix things.
#

import os

def deleteDirectory(path):
    if os.path.isdir(path):
        for item in os.listdir(path):
            itemPath = path+'/'+item;
            if os.path.isdir(itemPath):
                # check if directory is a keeper - folder name might be example.com or #example.com
                if item not in keeperDomains and item[1:] not in keeperDomains:
                    deleteDirectory(itemPath)
            elif os.path.isfile(itemPath):
                # special case - do not delete the base settings file, as that will undo global privacy and storage settings.
                # This file does keep a list of all visited domains, but it's best to clear that list through the Flash settings directly.
                if superParanoid or itemPath != os.path.expanduser('~/Library/Preferences/Macromedia/Flash Player/macromedia.com/support/flashplayer/sys/settings.sol'):
                    os.remove(itemPath);
        
        try:
            os.rmdir(path);
        except OSError:
            pass

# some LSO's are stored in ~/Library/Preferences/Macromedia/Flash Player/#SharedObjects
# which has one or more randomly-named subfolders which in turn have folders named by domain.
deleteDirectory(os.path.expanduser('~/Library/Preferences/Macromedia/Flash Player/#SharedObjects'))

# other LSO's are stored in ~/Library/Preferences/macromedia/Flash Player/macromedia.com/support/flashplayer/sys
# in folders named with a # followed by the domain, e.g., #muddledramblings.com
# These appear to be set by the flash player and not the movie itself, so are less likely to be evil.
# They may even contain settings that limit the power Flash in the given domain.
if paranoid:
    deleteDirectory(os.path.expanduser('~/Library/Preferences/Macromedia/Flash Player/macromedia.com/support/flashplayer/sys'))
