import pickle
import logging

from parseMaildir import Email

logging.basicConfig(level=logging.INFO)  # Set to .DEBUG for gory details

# Parse the pickles generated by parseMaildir.py
parsedLogsFolder = 'Enron/parsing/'
social = pickle.load(open(parsedLogsFolder + "social.pkl", "rb"))
log = pickle.load(open(parsedLogsFolder + "replay_log.pkl", "rb"))


def do_replay_log():
    # Set of users we know the social graph for
    enron_users = set([])

    # Dictionary to keep track of the latest head known to people
    chain_head_dict = {}

    # Initialize the latest known head dictionary
    for user in social:
        enron_users.add(user['user'])
        chain_head_dict[user['user'], user['user']] = 1
        for friend in user['friends']:
            chain_head_dict[(user['user'], friend)] = 0

    for email in log:
        if email.From not in enron_users:
            continue

        for recipient in email.To | email.Cc | email.Bcc - {email.From}:
            # For all recipients, update the dict entry for the sender
            chain_head_dict[(recipient, email.From)] = chain_head_dict[email.From, email.From]
            logging.debug("User %s updated the head for user %s" % (recipient, email.From))

            # For all recipients, update the dict entries for the public recipients
            for other in email.To | email.Cc - {recipient}:
                if (email.From, other) in chain_head_dict:
                    if not (recipient, other) in chain_head_dict:
                        chain_head_dict[(recipient, other)] = chain_head_dict[recipient, other]
                        logging.debug("User %s updated the head for user %s" % (recipient, other))

    return chain_head_dict


def main():
    known_head = do_replay_log()

    updated = 0
    not_updated = 0
    for user, friend in known_head:
        if user == friend:
            continue
        if known_head[(user, friend)] == known_head[(user, user)]:
            updated += 1
        else:
            not_updated += 1

    print "There were %s updates on the latest head dictionary, but %s entries are stale." % (updated, not_updated)

if __name__ == "__main__":
    main()
Add simulation script for ClaimChain in Autocrypt mode