2025-08-01 00:54:13 +08:00
|
|
|
package mail
|
2025-05-01 16:32:41 +08:00
|
|
|
|
|
|
|
import (
|
2025-08-01 00:54:13 +08:00
|
|
|
"runtime"
|
2025-05-01 16:32:41 +08:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"apigo.cc/gojs"
|
|
|
|
"apigo.cc/gojs/goja"
|
|
|
|
"github.com/ssgo/config"
|
2025-08-01 00:54:13 +08:00
|
|
|
"github.com/ssgo/log"
|
2025-05-01 16:32:41 +08:00
|
|
|
"github.com/ssgo/u"
|
|
|
|
)
|
|
|
|
|
|
|
|
const pluginName = "mail"
|
|
|
|
|
2025-08-01 00:54:13 +08:00
|
|
|
type Config struct {
|
|
|
|
Mailbox map[string]*MailboxConfig
|
|
|
|
ClientName string
|
|
|
|
ClientVersion string
|
|
|
|
ClientOS string
|
|
|
|
ClientVendor string
|
|
|
|
ClientSupportURL string
|
|
|
|
}
|
|
|
|
|
|
|
|
type MailboxConfig struct {
|
|
|
|
SmtpHost string
|
|
|
|
SmtpPort int
|
|
|
|
ImapHost string
|
|
|
|
ImapPort int
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
SenderName string
|
|
|
|
SenderMail string
|
|
|
|
}
|
|
|
|
|
|
|
|
var conf = &Config{
|
|
|
|
Mailbox: make(map[string]*MailboxConfig),
|
|
|
|
ClientName: "Apigo Mail Client",
|
|
|
|
ClientVersion: "1.0.0",
|
|
|
|
ClientOS: runtime.GOOS,
|
|
|
|
ClientVendor: "apigo.cc",
|
|
|
|
ClientSupportURL: "https://apigo.cc/gojs/mail",
|
|
|
|
}
|
|
|
|
|
2025-05-01 16:32:41 +08:00
|
|
|
var defaultObject = &DefaultObject{}
|
|
|
|
var confAes *u.Aes = u.NewAes([]byte("?GQ$0K0GgLdO=f+~L68PLm$uhKr4'=tV"), []byte("VFs7@sK61cj^f?HZ"))
|
2025-08-01 00:54:13 +08:00
|
|
|
var objects = make(map[string]*Mailbox)
|
2025-05-01 16:32:41 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
tsCode := gojs.MakeTSCode(defaultObject)
|
|
|
|
gojs.Register("apigo.cc/gojs/"+pluginName, gojs.Module{
|
|
|
|
ObjectMaker: func(vm *goja.Runtime) gojs.Map {
|
2025-08-01 00:54:13 +08:00
|
|
|
config.LoadConfig(pluginName, conf)
|
|
|
|
for name, object := range conf.Mailbox {
|
|
|
|
objects[name] = defaultObject.New(object)
|
|
|
|
}
|
|
|
|
if defaultMailbox, err := defaultObject.Get("default"); err == nil {
|
|
|
|
defaultObject.Mailbox = defaultMailbox
|
|
|
|
} else if len(objects) == 0 {
|
|
|
|
log.DefaultLogger.Warning("no mailbox configured", "example for env.yml", "mail:\n mailbox:\n default:\n username: ****@***.***")
|
2025-05-01 16:32:41 +08:00
|
|
|
}
|
|
|
|
return gojs.ToMap(defaultObject)
|
|
|
|
},
|
|
|
|
TsCode: tsCode,
|
2025-08-01 00:54:13 +08:00
|
|
|
Desc: "mail api",
|
2025-05-01 16:32:41 +08:00
|
|
|
SetSSKey: func(key, iv []byte) {
|
|
|
|
confAes = u.NewAes(key, iv)
|
|
|
|
},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type DefaultObject struct {
|
2025-08-01 00:54:13 +08:00
|
|
|
*Mailbox
|
2025-05-01 16:32:41 +08:00
|
|
|
}
|
|
|
|
|
2025-08-01 00:54:13 +08:00
|
|
|
func (obj *DefaultObject) Get(name string) (*Mailbox, error) {
|
2025-05-01 16:32:41 +08:00
|
|
|
if o, ok := objects[name]; ok {
|
2025-08-01 00:54:13 +08:00
|
|
|
return o, nil
|
2025-05-01 16:32:41 +08:00
|
|
|
} else {
|
2025-08-01 00:54:13 +08:00
|
|
|
return nil, gojs.Err("mailbox [" + name + "] not configured")
|
2025-05-01 16:32:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-08-01 00:54:13 +08:00
|
|
|
func (obj *DefaultObject) New(conf *MailboxConfig) *Mailbox {
|
2025-05-01 16:32:41 +08:00
|
|
|
a := strings.SplitN(conf.Username, "@", 2)
|
|
|
|
if len(a) == 2 {
|
|
|
|
if conf.SmtpHost == "" {
|
|
|
|
conf.SmtpHost = "smtp." + a[1]
|
|
|
|
}
|
|
|
|
if conf.SmtpPort == 0 {
|
|
|
|
conf.SmtpPort = 465
|
|
|
|
}
|
|
|
|
if conf.ImapHost == "" {
|
|
|
|
conf.ImapHost = "imap." + a[1]
|
|
|
|
}
|
|
|
|
if conf.ImapPort == 0 {
|
|
|
|
conf.ImapPort = 993
|
|
|
|
}
|
|
|
|
}
|
2025-08-01 00:54:13 +08:00
|
|
|
if conf.SenderMail == "" {
|
|
|
|
conf.SenderMail = conf.Username
|
|
|
|
}
|
2025-05-01 20:40:52 +08:00
|
|
|
|
2025-08-01 00:54:13 +08:00
|
|
|
return &Mailbox{
|
2025-05-01 16:32:41 +08:00
|
|
|
smtpHost: conf.SmtpHost,
|
|
|
|
smtpPort: conf.SmtpPort,
|
|
|
|
imapHost: conf.ImapHost,
|
|
|
|
imapPort: conf.ImapPort,
|
|
|
|
username: conf.Username,
|
|
|
|
password: confAes.DecryptUrlBase64ToString(conf.Password),
|
|
|
|
senderName: conf.SenderName,
|
2025-08-01 00:54:13 +08:00
|
|
|
senderMail: conf.SenderMail,
|
2025-05-01 16:32:41 +08:00
|
|
|
}
|
|
|
|
}
|