Skip to content

Commit

Permalink
getall command to save emails locally
Browse files Browse the repository at this point in the history
  • Loading branch information
morhekil committed Nov 2, 2014
1 parent cebc2fa commit 63967cc
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions guerrillamail.py
Expand Up @@ -21,7 +21,7 @@
from datetime import tzinfo, timedelta, datetime
from time import time
import json
from os.path import expanduser
from os.path import expanduser, isfile
import sys

import requests
Expand Down Expand Up @@ -232,7 +232,7 @@ class GetInfoCommand(Command):
name = 'info'
help = 'Show information about the current session.'
description = 'Show information about the current session.'

def invoke(self, session, args):
return 'Email: ' + session.get_session_state()['email_address']

Expand All @@ -249,13 +249,13 @@ class SetAddressCommand(Command):

def invoke(self, session, args):
session.set_email_address(args.address)


class ListEmailCommand(Command):
name = 'list'
help = 'Get the current inbox contents.'
description = 'Get the contents of the inbox associated with the current session'

def invoke(self, session, args):
email_list = session.get_email_list()
output = ''
Expand All @@ -278,7 +278,7 @@ class GetEmailCommand(Command):
'name': 'id',
'help': 'an email id'
}]

def invoke(self, session, args):
email = session.get_email(args.id)
return self.format_email(email)
Expand All @@ -287,8 +287,46 @@ def format_email(self, email):
email_format = u'From: {email.sender}\nDate: {email.datetime}\nSubject: {email.subject}\n\n{email.body}\n'
return email_format.format(email=email)

class GetAllEmailCommand(Command):
name = 'getall'
help = 'Get and save all emails from the current inbox.'
description = 'Get all emails from the inbox and save them into files.'
params = [{
'name': 'dir',
'help': 'output directory for emails'
}]

def invoke(self, session, args):
email_list = session.get_email_list()
output = ''
for email in email_list:
dumpfile = args.dir + '/' + email.guid
if isfile(dumpfile):
continue

output += self.format_email_summary(email)
self.save_email(session, email.guid, dumpfile)

output = None if output == '' else output.strip()
return output

def save_email(self, session, guid, dumpfile):
f = open(dumpfile, 'w')
email = session.get_email(guid)
f.write(self.format_email_body(email))
f.close()

def format_email_summary(self, email):
unread_indicator = '*' if not email.read else ' '
email_format = u'({unread_indicator}) {email.guid:<8} {email.time} {email.sender} {email.subject}\n'
return email_format.format(email=email, unread_indicator=unread_indicator)

def format_email_body(self, email):
email_format = u'From: {email.sender}\nDate: {email.datetime}\nSubject: {email.subject}\n\n{email.body}\n'
return email_format.format(email=email)


COMMAND_TYPES = [GetInfoCommand, SetAddressCommand, ListEmailCommand, GetEmailCommand]
COMMAND_TYPES = [GetInfoCommand, SetAddressCommand, ListEmailCommand, GetEmailCommand, GetAllEmailCommand]


def parse_args(args):
Expand Down

0 comments on commit 63967cc

Please sign in to comment.