mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
utils
A couple email address related utils
This commit is contained in:
6
.gitignore
vendored
6
.gitignore
vendored
@@ -20,3 +20,9 @@ _cgo_export.*
|
||||
_testmain.go
|
||||
|
||||
*.exe
|
||||
|
||||
# vim swp files
|
||||
*.swp
|
||||
|
||||
# revel tmp
|
||||
app/tmp
|
||||
|
||||
28
app/inbucket/utils.go
Normal file
28
app/inbucket/utils.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package inbucket
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Take "user+ext@host.com" and return "user", aka the mailbox we'll store it in
|
||||
func ParseMailboxName(emailAddress string) (result string) {
|
||||
result = strings.ToLower(emailAddress)
|
||||
if idx := strings.Index(result, "@"); idx > -1 {
|
||||
result = result[0:idx]
|
||||
}
|
||||
if idx := strings.Index(result, "+"); idx > -1 {
|
||||
result = result[0:idx]
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// Take a mailbox name and hash it into the directory we'll store it in
|
||||
func HashMailboxName(mailbox string) string {
|
||||
h := sha1.New()
|
||||
io.WriteString(h, mailbox)
|
||||
return fmt.Sprintf("%x", h.Sum(nil))
|
||||
}
|
||||
|
||||
28
app/inbucket/utils_test.go
Normal file
28
app/inbucket/utils_test.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package inbucket
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestParseMailboxName(t *testing.T) {
|
||||
in, out := "MailBOX", "mailbox"
|
||||
if x := ParseMailboxName(in); x != out {
|
||||
t.Errorf("ParseMailboxName(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
|
||||
in, out = "MailBox@Host.Com", "mailbox"
|
||||
if x := ParseMailboxName(in); x != out {
|
||||
t.Errorf("ParseMailboxName(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
|
||||
in, out = "Mail+extra@Host.Com", "mail"
|
||||
if x := ParseMailboxName(in); x != out {
|
||||
t.Errorf("ParseMailboxName(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHashMailboxName(t *testing.T) {
|
||||
in, out := "mail", "1d6e1cf70ec6f9ab28d3ea4b27a49a77654d370e"
|
||||
if x := HashMailboxName(in); x != out {
|
||||
t.Errorf("HashMailboxName(%v) = %v, want %v", in, x, out)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user