2018-12-29 01:42:50 +01:00
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
2018-12-29 18:09:44 +01:00
|
|
|
"bufio"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-12-29 22:02:23 +01:00
|
|
|
"io"
|
2018-12-29 18:09:44 +01:00
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"time"
|
2018-12-29 01:42:50 +01:00
|
|
|
)
|
|
|
|
|
2018-12-29 18:16:54 +01:00
|
|
|
type QEntry struct {
|
2018-12-31 16:32:23 +01:00
|
|
|
Id string
|
|
|
|
Status string
|
|
|
|
Date time.Time
|
|
|
|
Size int
|
|
|
|
Sender string
|
|
|
|
Recipients []string
|
|
|
|
Reason string
|
2018-12-29 01:42:50 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 23:28:58 +01:00
|
|
|
const sortableDateFormat = "2006-01-02 15:04:05"
|
|
|
|
|
|
|
|
func (m QEntry) ShortString() string {
|
2018-12-31 16:32:23 +01:00
|
|
|
return fmt.Sprintf("[%s] %s <%s> -> %d recipients (%d bytes)", m.Date.Format(sortableDateFormat), m.Id, m.Sender, len(m.Recipients), m.Size)
|
2018-12-29 23:28:58 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 18:16:54 +01:00
|
|
|
func (m QEntry) String() string {
|
2018-12-30 13:15:14 +01:00
|
|
|
var recipientSuffix string
|
2018-12-31 16:32:23 +01:00
|
|
|
if len(m.Recipients) > 1 {
|
2018-12-30 13:15:14 +01:00
|
|
|
recipientSuffix = ",..."
|
|
|
|
}
|
2018-12-31 16:32:23 +01:00
|
|
|
return fmt.Sprintf("[%s] %s <%s> -> {%d}<%s>%s (%s, %d bytes)", m.Date.Format(sortableDateFormat), m.Id, m.Sender, len(m.Recipients), m.Recipients[0], recipientSuffix, m.Status, m.Size)
|
2018-12-29 23:28:58 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m QEntry) DetailedString() string {
|
2018-12-30 13:23:30 +01:00
|
|
|
var reasonStr string
|
2018-12-31 16:32:23 +01:00
|
|
|
if m.Reason == "" {
|
2018-12-30 13:23:30 +01:00
|
|
|
reasonStr = "-/-"
|
|
|
|
}
|
2018-12-31 16:32:23 +01:00
|
|
|
return fmt.Sprintf("Id: %s\nDate: %s\nStatus: %s\nReason: %s\nSize: %d\nSender: %s\nRecipients: %s", m.Id, m.Date.Format(sortableDateFormat), m.Status, reasonStr, m.Size, m.Sender, strings.Join(m.Recipients, ", "))
|
2018-12-29 01:42:50 +01:00
|
|
|
}
|
|
|
|
|
2018-12-29 22:02:23 +01:00
|
|
|
func ParseMailQ(dataSource io.Reader) ([]QEntry, error) {
|
2018-12-29 18:09:44 +01:00
|
|
|
const dateFormat = "2006 Mon Jan _2 15:04:05"
|
|
|
|
var messageIdStart = regexp.MustCompile("^[0-9A-Za-z]+[*!]? ")
|
2018-12-29 22:02:23 +01:00
|
|
|
scanner := bufio.NewScanner(dataSource)
|
2018-12-30 12:54:22 +01:00
|
|
|
var line string
|
|
|
|
scanner.Scan()
|
|
|
|
line = scanner.Text()
|
|
|
|
if strings.HasPrefix(line, "Mail queue is empty") {
|
2018-12-29 18:09:44 +01:00
|
|
|
// If mail queue is empty, there is nothing to do
|
2018-12-31 15:05:13 +01:00
|
|
|
return []QEntry{}, nil
|
2018-12-29 18:09:44 +01:00
|
|
|
} else if strings.HasPrefix(line, "-Queue ID-") == false {
|
|
|
|
// Abort if input does not look like output from mailq(1)
|
|
|
|
return nil, errors.New("Sorry, this does not look like output from mailq(1).")
|
|
|
|
}
|
2018-12-29 18:16:54 +01:00
|
|
|
var currentMail QEntry
|
|
|
|
var queueEntries []QEntry
|
2018-12-29 01:42:50 +01:00
|
|
|
|
2018-12-29 18:09:44 +01:00
|
|
|
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, "*") {
|
2018-12-31 16:32:23 +01:00
|
|
|
currentMail.Status = "active"
|
2018-12-29 18:09:44 +01:00
|
|
|
} else if strings.HasSuffix(messageId, "!") {
|
2018-12-31 16:32:23 +01:00
|
|
|
currentMail.Status = "hold"
|
2018-12-29 18:09:44 +01:00
|
|
|
} else {
|
2018-12-31 16:32:23 +01:00
|
|
|
currentMail.Status = "deferred"
|
2018-12-29 18:09:44 +01:00
|
|
|
}
|
|
|
|
dateString := strconv.Itoa(time.Now().Year()) + " " + strings.Join(fields[2:6], " ")
|
|
|
|
mailDate, _ := time.Parse(dateFormat, dateString)
|
2018-12-31 16:32:23 +01:00
|
|
|
currentMail.Id = strings.TrimRight(messageId, "*!")
|
|
|
|
currentMail.Size, _ = strconv.Atoi(fields[1])
|
|
|
|
currentMail.Date = mailDate
|
|
|
|
currentMail.Sender = fields[6]
|
2018-12-29 18:09:44 +01:00
|
|
|
continue
|
|
|
|
} else if strings.HasPrefix(line, "(") && strings.HasSuffix(line, ")") {
|
|
|
|
// Handle reason for deferred status (if deferred at all, may be missing)
|
2018-12-31 16:32:23 +01:00
|
|
|
currentMail.Reason = strings.Trim(strings.TrimSpace(line), "()")
|
2018-12-29 18:09:44 +01:00
|
|
|
continue
|
|
|
|
} else if len(fields) == 1 {
|
|
|
|
// Handle line with one of the mail recipients
|
2018-12-31 16:32:23 +01:00
|
|
|
currentMail.Recipients = append(currentMail.Recipients, fields[0])
|
2018-12-29 18:09:44 +01:00
|
|
|
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
|
|
|
|
queueEntries = append(queueEntries, currentMail)
|
2018-12-29 18:16:54 +01:00
|
|
|
currentMail = QEntry{}
|
2018-12-29 18:09:44 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if scanner.Err() != nil {
|
|
|
|
// If the scanner failed, let our caller know.
|
|
|
|
return nil, errors.New("Something went wrong with reading from stdin. Sorry!")
|
|
|
|
}
|
|
|
|
return queueEntries, nil
|
2018-12-29 01:42:50 +01:00
|
|
|
}
|