Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Factor out use of log package #4

Merged
merged 1 commit into from
Aug 3, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions message.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,18 @@ import (
"time"
)

// LogHandler is called for each log message. If nil, log messages will
// be output using log.Printf instead.
var LogHandler func(message string, args ...interface{})

func logf(message string, args ...interface{}) {
if LogHandler != nil {
LogHandler(message, args...)
} else {
log.Printf(message, args...)
}
}

// MessageID represents the ID of an SMTP message including the hostname part
type MessageID string

Expand Down Expand Up @@ -90,7 +102,7 @@ func (m *SMTPMessage) Parse(hostname string) *Message {
}

if msg.Content.IsMIME() {
log.Printf("Parsing MIME body")
logf("Parsing MIME body")
msg.MIME = msg.Content.ParseMIMEBody()
}

Expand Down Expand Up @@ -120,16 +132,16 @@ func (content *Content) ParseMIMEBody() *MIMEBody {
var p []string
if len(boundary) > 0 {
p = strings.Split(content.Body, "--"+boundary)
log.Printf("Got boundary: %s", boundary)
logf("Got boundary: %s", boundary)
} else {
log.Printf("Boundary not found: %s", hdr[0])
logf("Boundary not found: %s", hdr[0])
}

for _, s := range p {
if len(s) > 0 {
part := ContentFromString(strings.Trim(s, "\r\n"))
if part.IsMIME() {
log.Printf("Parsing inner MIME body")
logf("Parsing inner MIME body")
part.MIME = part.ParseMIMEBody()
}
parts = append(parts, part)
Expand Down Expand Up @@ -171,7 +183,7 @@ func PathFromString(path string) *Path {

// ContentFromString parses SMTP content into separate headers and body
func ContentFromString(data string) *Content {
log.Printf("Parsing Content from string: '%s'", data)
logf("Parsing Content from string: '%s'", data)
x := strings.SplitN(data, "\r\n\r\n", 2)
h := make(map[string][]string, 0)

Expand All @@ -189,7 +201,7 @@ func ContentFromString(data string) *Content {
h[key] = []string{value}
lastHdr = key
} else {
log.Printf("Found invalid header: '%s'", hdr)
logf("Found invalid header: '%s'", hdr)
}
}
return &Content{
Expand Down