forked from jrk/remindify
-
Notifications
You must be signed in to change notification settings - Fork 1
/
encode.py
48 lines (38 loc) · 1.26 KB
/
encode.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
from google.appengine.api import app_identity
# Base32 encode into a shuffled alphabet
_i2a = [ 's', '5', 'u', 'x', 'n', 'q', '2', 'j', 't', 'y', '4', 'p', 'l',
'g', 'e', 'r', 'k', '1', 'z', 'a', 'f', 'w', 'h', '3', 'v', 'i',
'0', 'o', 'b', 'd', 'm', 'c' ]
_a2i = dict( zip( _i2a, range( len( _i2a ) ) ) )
def _encode( x ):
assert( x > 0 )
s = ''
while x:
s = _i2a[ x & 0x1F ] + s
x = x >> 5
return s
def _decode( s ):
x = 0
started = False
for i in range( len( s ) ):
if i > 0:
x = x << 5
x = x | _a2i[ s[i] ]
return x
_address_prefix = 'r.'
import os
def mail_domain():
#app_id = os.environ.get('APPLICATION_ID', '')
app_id = app_identity.get_application_id()
return '%s.appspotmail.com' % app_id
def from_field( addr ):
if '@%s'%mail_domain() not in addr:
addr = addr + '@' + mail_domain()
return 'Ping <%s>' % addr
def address_to_id( addr ):
# pull out just the username portion of the email address
addr = addr.split('<')[-1].split('>')[0].split('@')[0]
assert addr.startswith( _address_prefix )
return _decode( addr.split( _address_prefix )[-1] )
def id_to_address( id ):
return _address_prefix + _encode( id ) + '@' + mail_domain()