mailq-inspector/mailq/parser.go

74 lines
2.4 KiB
Go

package mailq
import (
"bufio"
"errors"
"io"
"regexp"
"strconv"
"strings"
"time"
)
func ParseFromReader(dataSource io.Reader) (MailQ, error) {
const dateFormat = "2006 Mon Jan _2 15:04:05"
var messageIdStart = regexp.MustCompile("^[0-9A-Za-z]+[*!]? ")
scanner := bufio.NewScanner(dataSource)
var line string
scanner.Scan()
line = scanner.Text()
if strings.HasPrefix(line, "Mail queue is empty") {
// If mail queue is empty, there is nothing to do
return NewQueue(), nil
} else if strings.HasPrefix(line, "-Queue ID-") == false {
// Abort if input does not look like output from mailq(1)
return NewQueue(), errors.New("Sorry, this does not look like output from mailq(1).")
}
queue := NewQueue()
var currentMail QEntry
for scanner.Scan() {
// Read input line by line
line := scanner.Text()
fields := strings.Fields(line)
if strings.HasPrefix(line, "--") {
// Handle the summary line at the end of mailq output
break
} else if messageIdStart.MatchString(line) {
// Handle line starting next mail in queue
messageId := fields[0]
if strings.HasSuffix(messageId, "*") {
currentMail.Status = "active"
} else if strings.HasSuffix(messageId, "!") {
currentMail.Status = "hold"
} else {
currentMail.Status = "deferred"
}
dateString := strconv.Itoa(time.Now().Year()) + " " + strings.Join(fields[2:6], " ")
mailDate, _ := time.Parse(dateFormat, dateString)
currentMail.Id = strings.TrimRight(messageId, "*!")
currentMail.Size, _ = strconv.Atoi(fields[1])
currentMail.Date = mailDate
currentMail.Sender = fields[6]
continue
} else if strings.HasPrefix(line, "(") && strings.HasSuffix(line, ")") {
// Handle reason for deferred status (if deferred at all, may be missing)
currentMail.Reason = strings.Trim(strings.TrimSpace(line), "()")
continue
} else if len(fields) == 1 {
// Handle line with one of the mail recipients
currentMail.Recipients = append(currentMail.Recipients, fields[0])
continue
} else if len(fields) == 0 {
// If the next line is empty, make sure to push current mail to list
// and create a new struct for the next mail to process
queue.AddEntry(currentMail)
currentMail = QEntry{}
}
}
if scanner.Err() != nil {
// If the scanner failed, let our caller know.
return NewQueue(), errors.New("Something went wrong with reading from stdin. Sorry!")
}
return queue, nil
}