-
Notifications
You must be signed in to change notification settings - Fork 0
/
emails.py
121 lines (92 loc) · 3.13 KB
/
emails.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
from google.appengine.api import mail
import re
def base_email(game):
message = mail.EmailMessage()
message.sender = "chess-%[email protected]" % game.key()
message.to = game.whose_go_email()
return message
def textify(html):
return re.sub(r'<[^>]*?>', '', html).replace(' ', ' ')
def footer(game):
history_html = '<ol><li>Game started by <strong>%s</strong> (black), playing <strong>%s</strong> (white).</li>' % (game.black_email, game.white_email)
if game.history:
history_html += '<li> %s </li>' % '</li><li>'.join(game.history)
history_html += '</ol>'
return """
<p>
Here's the current board:
</p>
<center>
<font size="5" face="mono" color="blue">
%s
</font>
</center>
<p>
You're <strong>%s</strong> and it's your move...
</p>
<h3>Instructions:</h3>
<ul>
<li>Reply to this email, entering your move in the first line of the response.</li>
<li>A move is the form of "a4 to b5". Be sure to get it in the right format.</li>
<li>If you think your opponent has made an invalid move, reply with a reason inclduing the word "undo" in the first line. This will undo their move and give control back to them.</li>
<li>Castling is a special case, you can specify that by saying something like "e1 to g1 and h1 to f1". This will move both pieces in one go for you.</li>
<li>Known things that don't work - "en passent" pawn taking, pawn promotion.</li>
</ul>
<h3>Game History</h3>
%s
""" % (unicode(game.board).replace("\n", "<br/>").replace(" ", " "), game.whose_go, history_html)
def start_game_email(game):
message = base_email(game)
message.subject = "%s would like to start a chess game with you." % game.other_email()
message.html = "<p><strong>%s</strong> challenges you to a game of chess!</p>" % (textify(game.other_email()))
message.html += footer(game)
message.body = textify(message.html)
return message
def play_game_email(game, move):
message = base_email(game)
message.subject="Your chess game with %s" % game.other_email()
message.html = """
<p>
Here's the latest move from <strong>%s</strong>:
</p>
<center>
<font size="5" face="mono" color="red">
%s
</font>
</center>
""" % (textify(game.other_email()), move)
message.html += footer(game)
message.body = textify(message.html)
return message
def invalid_move_email(game, move):
message = base_email(game)
message.subject="Your chess game with %s" % game.other_email()
message.html = """
<p>
Your last move was invalid. Please resend a valid move. Your last move was:
</p>
<center>
<font size="5" face="mono" color="red">
%s
</font>
</center>
""" % (move)
message.html += footer(game)
message.body = textify(message.html)
return message
def undo_move_email(game, move):
message = base_email(game)
message.subject="Your chess game with %s" % game.other_email()
message.html = """
<p>
%s said that your last move was invalid, so it has been undone. They said:
</p>
<center>
<font size="5" face="mono" color="red">
%s
</font>
</center>
""" % (textify(game.other_email()), move)
message.html += footer(game)
message.body = textify(message.html)
return message