This repository has been archived by the owner on Feb 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
wiimail.go
executable file
·155 lines (132 loc) · 4.01 KB
/
wiimail.go
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
package main
import (
"bufio"
"bytes"
"encoding/base64"
"fmt"
"golang.org/x/image/draw"
"io/ioutil"
"log"
"strings"
"image"
// We use jpeg to actually send to the Wii.
"image/jpeg"
// We don't actually use the following formats for encoding,
// they're here for image format detection.
_ "image/gif"
_ "image/png"
_ "golang.org/x/image/bmp"
_ "golang.org/x/image/tiff"
_ "golang.org/x/image/webp"
)
const CRLF = "\r\n"
func FormulateMail(from string, to string, subject string, body string, potentialImage []byte) (string, error) {
boundary := GenerateBoundary()
// Set up headers and set up first boundary with body.
// The body could be empty: that's fine, it'll have no value
// (compared to nil) and the Wii will ignore that section.
mailContent := fmt.Sprint("From: ", from, CRLF,
"Subject: ", subject, CRLF,
"To: ", to, CRLF,
"MIME-Version: 1.0", CRLF,
`Content-Type: MULTIPART/mixed; BOUNDARY="`, boundary, `"`, CRLF,
CRLF,
"--", boundary, CRLF,
"Content-Type: TEXT/plain; CHARSET=utf-8", CRLF,
"Content-Description: wiimail", CRLF,
CRLF,
)
normalMailFormat := fmt.Sprint(mailContent,
body,
strings.Repeat(CRLF, 3),
"--", boundary, "--")
// If there's an attachment, we need to factor that in.
// Otherwise we're done.
if potentialImage == nil {
return normalMailFormat, nil
}
// The image library interprets known file types automatically.
givenImg, _, err := image.Decode(bytes.NewReader(potentialImage))
if err != nil {
log.Printf("Error transforming image: %v %s", err, potentialImage)
return normalMailFormat, nil
}
// The Wii has a max image size of 8192x8192px.
// If any dimension exceeds that, scale to fit.
outputImg := resize(givenImg, 8192, 8192)
// Encode image as JPEG for the Wii to handle.
var outputImgWriter bytes.Buffer
err = jpeg.Encode(bufio.NewWriter(&outputImgWriter), outputImg, nil)
if err != nil {
log.Printf("Error transforming image: %v", err)
return genError(mailContent, body, boundary), err
}
outputImgBytes, err := ioutil.ReadAll(bufio.NewReader(&outputImgWriter))
if err != nil {
log.Printf("Error transforming image: %v", err)
return genError(mailContent, body, boundary), err
}
// The Wii's mailbox is roughly 7.3mb.
// We'll cap any generated image at 7mb.
if len(outputImgBytes) > 7*1024*1024 {
return genError(mailContent, body, boundary), nil
}
encodedImage := base64.StdEncoding.EncodeToString(outputImgBytes)
var splitEncoding string
// 76 is a widely accepted base64 newline max char standard for mail.
for {
if len(encodedImage) >= 76 {
// Separate the next 73.
splitEncoding += encodedImage[:76] + CRLF
encodedImage = encodedImage[76:]
} else {
// To the end.
splitEncoding += encodedImage[:]
break
}
}
return fmt.Sprint(mailContent,
body,
strings.Repeat(CRLF, 3),
"--", boundary, CRLF,
// Now we can put our image data.
"Content-Type: IMAGE/jpeg; name=converted.jpeg", CRLF,
"Content-Transfer-Encoding: BASE64", CRLF,
"Content-Disposition: attachment; filename=converted.jpeg", CRLF,
CRLF,
splitEncoding, CRLF,
CRLF,
"--", boundary, "--",
), nil
}
func genError(mailContent string, body string, boundary string) string {
return fmt.Sprint(mailContent,
body,
CRLF,
"---",
CRLF,
"An error occurred processing the attached image.", CRLF,
"For more information, ask the sender to forward this mail to ",
global.SupportEmail,
strings.Repeat(CRLF, 3),
"--", boundary, "--")
}
func resize(origImage image.Image, maxWidth int, maxHeight int) image.Image {
width := origImage.Bounds().Size().X
height := origImage.Bounds().Size().Y
if width > maxWidth {
height = height * maxWidth / width
width = maxWidth
}
if height > maxHeight {
width = width * maxHeight / height
height = maxHeight
}
if width != maxWidth && height != maxHeight {
// No resize needs to occur.
return origImage
}
newImage := image.NewRGBA(image.Rect(0, 0, width, height))
draw.BiLinear.Scale(newImage, newImage.Bounds(), origImage, origImage.Bounds(), draw.Over, nil)
return newImage
}