-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathguerrillamail.py
executable file
·346 lines (268 loc) · 11.1 KB
/
guerrillamail.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
#!/usr/bin/env python
# Copyright Nathan Jones 2014, 2016
#
# 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.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from __future__ import print_function
from __future__ import unicode_literals
import argparse
from datetime import tzinfo, timedelta, datetime
from time import time
import json
from os.path import expanduser
import sys
import requests
# UTC timezone implementation from
# http://docs.python.org/2/library/datetime.html#tzinfo-objects
ZERO = timedelta(0)
class UTC(tzinfo):
"""UTC"""
#
def utcoffset(self, dt):
return ZERO
def tzname(self, dt):
return "UTC"
def dst(self, dt):
return ZERO
utc = UTC()
class GuerrillaMailException(Exception):
def __init__(self, message):
self.message = message
def _transform_dict(original, key_map):
result = {}
for (new_key, (old_key, transform_fn)) in list(key_map.items()):
try:
result[new_key] = transform_fn(original[old_key])
except KeyError:
pass
return result
class Mail(object):
@classmethod
def from_response(cls, response_data):
"""
Factory method to create a Mail instance from a Guerrillamail response
dict.
"""
identity = lambda x: x
return Mail(**_transform_dict(response_data, {
'guid': ('mail_id', identity),
'subject': ('mail_subject', identity),
'sender': ('mail_from', identity),
'datetime': ('mail_timestamp', lambda x: datetime.utcfromtimestamp(int(x)).replace(tzinfo=utc)),
'read': ('mail_read', int),
'excerpt': ('mail_excerpt', identity),
'body': ('mail_body', identity),
}))
def __init__(self, guid=None, subject=None, sender=None, datetime=None,
read=False, exerpt=None, excerpt=None, body=None):
self.guid = guid
self.subject = subject
self.sender = sender
self.datetime = datetime
self.read = read
# legacy broken "exerpt" property maintained for backwards compatibility
self.exerpt = None
self.excerpt = excerpt
self.body = body
@property
def time(self):
return self.datetime.time().replace(tzinfo=self.datetime.tzinfo) if self.datetime else None
SESSION_TIMEOUT_SECONDS = 3600
class GuerrillaMailSession(object):
"""
An abstraction over a GuerrillamailClient which maintains session state.
This class is not thread safe.
"""
def __init__(self, session_id=None, email_address=None, email_timestamp=0, **kwargs):
self.client = GuerrillaMailClient(**kwargs)
self.session_id = session_id
self.email_timestamp = email_timestamp
self.email_address = email_address
def _update_session_state(self, response_data):
try:
self.session_id = response_data['sid_token']
except KeyError:
pass
try:
self.email_address = response_data['email_addr']
except KeyError:
pass
try:
self.email_timestamp = response_data['email_timestamp']
except KeyError:
pass
def is_expired(self):
current_time = int(time())
expiry_time = self.email_timestamp + SESSION_TIMEOUT_SECONDS - 5
return current_time >= expiry_time
def _delegate_to_client(self, method_name, *args, **kwargs):
client_method = getattr(self.client, method_name)
response_data = client_method(session_id=self.session_id, *args, **kwargs)
self._update_session_state(response_data)
return response_data
def get_session_state(self):
self._ensure_valid_session(fully_populate=True)
return {
'email_address': self.email_address
}
def set_email_address(self, address_local_part):
self._delegate_to_client('set_email_address', address_local_part=address_local_part)
def _renew_session(self):
if self.email_address:
self.set_email_address(self.email_address)
else:
self._delegate_to_client('get_email_address')
def _ensure_valid_session(self, fully_populate=False):
if self.session_id is None or self.is_expired() or fully_populate and not self.email_address:
self._renew_session()
if self.session_id is None:
raise GuerrillaMailException('Failed to obtain session id')
def get_email_list(self, offset=0):
self._ensure_valid_session()
response_data = self._delegate_to_client('get_email_list', offset=offset)
email_list = response_data.get('list')
return [Mail.from_response(e) for e in email_list] if email_list else []
def get_email(self, email_id):
return Mail.from_response(self._delegate_to_client('get_email', email_id=email_id))
class GuerrillaMailClient(object):
"""
A client to the Guerrillamail web service API
(https://www.guerrillamail.com/GuerrillaMailAPI.html).
"""
def __init__(self, base_url='http://api.guerrillamail.com', client_ip='127.0.0.1'):
self.base_url = base_url
self.client_ip = client_ip
def _do_request(self, session_id, **kwargs):
url = self.base_url + '/ajax.php'
kwargs['ip'] = self.client_ip
if session_id is not None:
kwargs['sid_token'] = session_id
response = requests.get(url, params=kwargs)
try:
response.raise_for_status()
except requests.HTTPError as e:
raise GuerrillaMailException(('Request failed: {e.request.url} ' +
'{e.response.status_code} {e.response.reason}').format(e=e))
data = json.loads(response.text)
return data
def get_email_address(self, session_id=None):
return self._do_request(session_id, f='get_email_address')
def get_email_list(self, session_id, offset=0):
if session_id is None:
raise ValueError('session_id is None')
return self._do_request(session_id, f='get_email_list', offset=offset)
def get_email(self, email_id, session_id=None):
response_data = self._do_request(session_id, f='fetch_email', email_id=email_id)
if not response_data:
raise GuerrillaMailException('Not found: ' + str(email_id))
return response_data
def set_email_address(self, address_local_part, session_id=None):
return self._do_request(session_id, f='set_email_user', email_user=address_local_part)
SETTINGS_FILE = '~/.guerrillamail'
def load_settings():
try:
with open(expanduser(SETTINGS_FILE)) as f:
return json.load(f)
except IOError:
return {}
def save_settings(settings):
with open(expanduser(SETTINGS_FILE), 'w+') as f:
json.dump(settings, f, indent=4)
f.write('\n')
class Command(object):
params = []
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']
class SetAddressCommand(Command):
name = 'setaddr'
help = 'Set the email address for the current session.'
description = '''Set the email address for the current session. This
address will be used when listing inbox contents.'''
params = [{
'name': 'address',
'help': 'an email address "local part". The domain, if provided, will be ignored.'
}]
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 = ''
for email in email_list:
output += self.format_email_summary(email) + '\n'
return output
def format_email_summary(self, email):
unread_indicator = '*' if not email.read else ' '
email_format = '({unread_indicator}) {email.guid:<8} {email.time} {email.sender}\n{email.subject}\n'
return email_format.format(email=email, unread_indicator=unread_indicator)
class GetEmailCommand(Command):
name = 'get'
help = 'Get an email message by id.'
description = '''Get an email message by id. The requested email does not
need to belong to the inbox associated with the current session.'''
params = [{
'name': 'id',
'help': 'an email id'
}]
def invoke(self, session, args):
email = session.get_email(args.id)
return self.format_email(email)
def format_email(self, email):
email_format = '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]
def parse_args(args):
parser = argparse.ArgumentParser(description='''Call a Guerrillamail web service.
All commands operate on the current Guerrillamail session which is stored in {0}. If a session does not exist
or has timed out a new one will be created.'''.format(SETTINGS_FILE))
subparsers = parser.add_subparsers(dest='command', metavar='<command>')
subparsers.required = True
for Command in COMMAND_TYPES:
command_parser = subparsers.add_parser(Command.name, help=Command.help, description=Command.description)
for param in Command.params:
param_name = param['name']
command_parser.add_argument(param_name, metavar='<{0}>'.format(param_name), help=param['help'])
return parser.parse_args(args)
def get_command(command_name):
try:
return [C() for C in COMMAND_TYPES if C.name == command_name][0]
except IndexError:
raise ValueError('Invalid command: ' + command_name)
def update_settings(settings, session):
settings['session_id'] = session.session_id
settings['email_timestamp'] = session.email_timestamp
settings['email_address'] = session.email_address
def cli(*args):
args = parse_args(args)
settings = load_settings()
session = GuerrillaMailSession(**settings)
try:
output = get_command(args.command).invoke(session, args)
except GuerrillaMailException as e:
print(e.message, file=sys.stderr)
else:
if output is not None:
print(output)
update_settings(settings, session)
save_settings(settings)
def main():
cli(*sys.argv[1:])
if __name__ == '__main__':
main()