mailq-inspector/mailq/queue.go

62 lines
1.1 KiB
Go
Raw Normal View History

2019-01-01 16:02:04 +01:00
package mailq
import (
2019-01-01 21:11:39 +01:00
"errors"
"fmt"
2019-01-01 16:02:04 +01:00
"time"
)
type MailQ struct {
NumTotal int
NumActive int
NumHold int
NumDeferred int
Entries []QEntry
}
2019-01-01 16:02:04 +01:00
type QEntry struct {
Id string
Status string
Date time.Time
Size int
Sender string
Recipients []string
Reason string
}
2019-01-01 21:11:39 +01:00
2019-01-06 00:32:17 +01:00
func NewQueue() MailQ {
return MailQ{}
}
func (q *MailQ) AddEntry(entry QEntry) {
2019-01-06 00:32:17 +01:00
q.NumTotal += 1
if entry.Status == "active" {
q.NumActive += 1
} else if entry.Status == "deferred" {
q.NumDeferred += 1
} else if entry.Status == "hold" {
q.NumHold += 1
}
q.Entries = append(q.Entries, entry)
}
func (q *MailQ) RemoveEntry(entry QEntry) {
2019-01-06 00:32:17 +01:00
var currentIndex int
var currentEntry QEntry
for currentIndex, currentEntry = range q.Entries {
if currentEntry.Id == entry.Id {
break
}
}
q.Entries = append(q.Entries[:currentIndex], q.Entries[currentIndex+1:]...)
}
func (q *MailQ) GetMessageById(id string) (QEntry, error) {
2019-01-01 21:11:39 +01:00
for _, entry := range q.Entries {
if entry.Id == id {
return entry, nil
}
}
return QEntry{}, errors.New(fmt.Sprintf("No message with id '%s' available!", id))
}