120 lines
2.1 KiB
Go
120 lines
2.1 KiB
Go
package main
|
|
|
|
import (
|
|
"fmt"
|
|
"os"
|
|
"qmail/internal/config"
|
|
"qmail/internal/mail"
|
|
|
|
"github.com/jessevdk/go-flags"
|
|
"github.com/olekukonko/tablewriter"
|
|
)
|
|
|
|
type appOptions struct {
|
|
Config string `short:"c" long:"config" description:"Config file"`
|
|
|
|
Server string `short:"s" long:"server" description:"SMTP server for sending"`
|
|
PrintList bool `short:"l" long:"list" description:"Print servers"`
|
|
|
|
From string `long:"from" description:"e-mail address indicated as sender"`
|
|
To []string `long:"to" description:""`
|
|
}
|
|
|
|
type helpOptions struct {
|
|
PrintHelp bool `short:"h" long:"help" description:"Show this help message"`
|
|
}
|
|
|
|
func main() {
|
|
appOpt := appOptions{
|
|
Config: config.DefaultConfigPath,
|
|
}
|
|
|
|
f := flags.NewParser(&appOpt, flags.PassDoubleDash)
|
|
f.Usage = "[OPTIONS] [FILE]"
|
|
f.LongDescription = "The qmail utility is a tool designed for simple, fast, and convenient email sending"
|
|
|
|
helpOpt := helpOptions{}
|
|
|
|
f.AddGroup("Help options", "", &helpOpt)
|
|
|
|
args, err := f.ParseArgs(os.Args)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
if helpOpt.PrintHelp {
|
|
f.WriteHelp(os.Stderr)
|
|
return
|
|
}
|
|
|
|
cfg, err := config.LoadConfig(appOpt.Config)
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
return
|
|
}
|
|
|
|
if appOpt.PrintList {
|
|
table := tablewriter.NewWriter(os.Stdout)
|
|
|
|
table.SetHeader([]string{"Name", "Host", "Port", "TLS", "Login"})
|
|
|
|
for _, s := range cfg.Servers {
|
|
login := ""
|
|
|
|
if s.Auth != nil {
|
|
login = s.Auth.Login
|
|
}
|
|
|
|
useTLS := "-"
|
|
if s.UseTLS {
|
|
useTLS = "+"
|
|
}
|
|
|
|
table.Append([]string{
|
|
s.Name,
|
|
s.Host,
|
|
fmt.Sprintf("%d", s.Port),
|
|
useTLS,
|
|
login,
|
|
})
|
|
}
|
|
|
|
table.Render()
|
|
|
|
return
|
|
}
|
|
|
|
if appOpt.Server != "" {
|
|
var server *config.Server
|
|
|
|
for _, s := range cfg.Servers {
|
|
if s.Name == appOpt.Server {
|
|
server = &s
|
|
}
|
|
}
|
|
|
|
if server == nil {
|
|
panic("omg")
|
|
}
|
|
|
|
var bodyFile string
|
|
|
|
if len(args) > 1 {
|
|
bodyFile = args[1]
|
|
} else {
|
|
bodyFile = "/dev/stdin"
|
|
}
|
|
|
|
body, err := os.ReadFile(bodyFile)
|
|
if err != nil {
|
|
panic("qq")
|
|
}
|
|
|
|
err = mail.SendMail(server, appOpt.From, appOpt.To, body)
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
}
|
|
}
|