mirror of
https://github.com/mjl-/mox.git
synced 2024-12-27 08:53:48 +03:00
09fcc49223
for applications to compose/send messages, receive delivery feedback, and maintain suppression lists. this is an alternative to applications using a library to compose messages, submitting those messages using smtp, and monitoring a mailbox with imap for DSNs, which can be processed into the equivalent of suppression lists. but you need to know about all these standards/protocols and find libraries. by using the webapi & webhooks, you just need a http & json library. unfortunately, there is no standard for these kinds of api, so mox has made up yet another one... matching incoming DSNs about deliveries to original outgoing messages requires keeping history of "retired" messages (delivered from the queue, either successfully or failed). this can be enabled per account. history is also useful for debugging deliveries. we now also keep history of each delivery attempt, accessible while still in the queue, and kept when a message is retired. the queue webadmin pages now also have pagination, to show potentially large history. a queue of webhook calls is now managed too. failures are retried similar to message deliveries. webhooks can also be saved to the retired list after completing. also configurable per account. messages can be sent with a "unique smtp mail from" address. this can only be used if the domain is configured with a localpart catchall separator such as "+". when enabled, a queued message gets assigned a random "fromid", which is added after the separator when sending. when DSNs are returned, they can be related to previously sent messages based on this fromid. in the future, we can implement matching on the "envid" used in the smtp dsn extension, or on the "message-id" of the message. using a fromid can be triggered by authenticating with a login email address that is configured as enabling fromid. suppression lists are automatically managed per account. if a delivery attempt results in certain smtp errors, the destination address is added to the suppression list. future messages queued for that recipient will immediately fail without a delivery attempt. suppression lists protect your mail server reputation. submitted messages can carry "extra" data through the queue and webhooks for outgoing deliveries. through webapi as a json object, through smtp submission as message headers of the form "x-mox-extra-<key>: value". to make it easy to test webapi/webhooks locally, the "localserve" mode actually puts messages in the queue. when it's time to deliver, it still won't do a full delivery attempt, but just delivers to the sender account. unless the recipient address has a special form, simulating a failure to deliver. admins now have more control over the queue. "hold rules" can be added to mark newly queued messages as "on hold", pausing delivery. rules can be about certain sender or recipient domains/addresses, or apply to all messages pausing the entire queue. also useful for (local) testing. new config options have been introduced. they are editable through the admin and/or account web interfaces. the webapi http endpoints are enabled for newly generated configs with the quickstart, and in localserve. existing configurations must explicitly enable the webapi in mox.conf. gopherwatch.org was created to dogfood this code. it initially used just the compose/smtpclient/imapclient mox packages to send messages and process delivery feedback. it will get a config option to use the mox webapi/webhooks instead. the gopherwatch code to use webapi/webhook is smaller and simpler, and developing that shaped development of the mox webapi/webhooks. for issue #31 by cuu508
113 lines
2.3 KiB
Go
113 lines
2.3 KiB
Go
package mox
|
|
|
|
import (
|
|
"reflect"
|
|
)
|
|
|
|
// FillNil returns a modified value with nil maps/slices replaced with empty
|
|
// maps/slices.
|
|
func FillNil(rv reflect.Value) (nv reflect.Value, changed bool) {
|
|
switch rv.Kind() {
|
|
case reflect.Struct:
|
|
for i := 0; i < rv.NumField(); i++ {
|
|
if !rv.Type().Field(i).IsExported() {
|
|
continue
|
|
}
|
|
vv := rv.Field(i)
|
|
nvv, ch := FillNil(vv)
|
|
if ch && !rv.CanSet() {
|
|
// Make struct settable.
|
|
nrv := reflect.New(rv.Type()).Elem()
|
|
for j := 0; j < rv.NumField(); j++ {
|
|
nrv.Field(j).Set(rv.Field(j))
|
|
}
|
|
rv = nrv
|
|
vv = rv.Field(i)
|
|
}
|
|
if ch {
|
|
changed = true
|
|
vv.Set(nvv)
|
|
}
|
|
}
|
|
case reflect.Slice:
|
|
if rv.IsNil() {
|
|
return reflect.MakeSlice(rv.Type(), 0, 0), true
|
|
}
|
|
n := rv.Len()
|
|
for i := 0; i < n; i++ {
|
|
rve := rv.Index(i)
|
|
nrv, ch := FillNil(rve)
|
|
if ch {
|
|
changed = true
|
|
rve.Set(nrv)
|
|
}
|
|
}
|
|
case reflect.Map:
|
|
if rv.IsNil() {
|
|
return reflect.MakeMap(rv.Type()), true
|
|
}
|
|
i := rv.MapRange()
|
|
for i.Next() {
|
|
erv, ch := FillNil(i.Value())
|
|
if ch {
|
|
changed = true
|
|
rv.SetMapIndex(i.Key(), erv)
|
|
}
|
|
}
|
|
case reflect.Pointer:
|
|
if !rv.IsNil() {
|
|
FillNil(rv.Elem())
|
|
}
|
|
}
|
|
return rv, changed
|
|
}
|
|
|
|
// FillExample returns a modified value with nil/empty maps/slices/pointers values
|
|
// replaced with non-empty versions, for more helpful examples of types. Useful for
|
|
// documenting JSON representations of types.
|
|
func FillExample(seen []reflect.Type, rv reflect.Value) reflect.Value {
|
|
if seen == nil {
|
|
seen = make([]reflect.Type, 100)
|
|
}
|
|
|
|
// Prevent recursive filling.
|
|
rvt := rv.Type()
|
|
index := -1
|
|
for i, t := range seen {
|
|
if t == rvt {
|
|
return rv
|
|
} else if t == nil {
|
|
index = i
|
|
}
|
|
}
|
|
if index < 0 {
|
|
return rv
|
|
}
|
|
seen[index] = rvt
|
|
defer func() {
|
|
seen[index] = nil
|
|
}()
|
|
|
|
switch rv.Kind() {
|
|
case reflect.Struct:
|
|
for i := 0; i < rv.NumField(); i++ {
|
|
if !rvt.Field(i).IsExported() {
|
|
continue
|
|
}
|
|
vv := rv.Field(i)
|
|
vv.Set(FillExample(seen, vv))
|
|
}
|
|
case reflect.Slice:
|
|
ev := FillExample(seen, reflect.New(rvt.Elem()).Elem())
|
|
return reflect.Append(rv, ev)
|
|
case reflect.Map:
|
|
vv := FillExample(seen, reflect.New(rvt.Elem()).Elem())
|
|
nv := reflect.MakeMap(rvt)
|
|
nv.SetMapIndex(reflect.ValueOf("example"), vv)
|
|
return nv
|
|
case reflect.Pointer:
|
|
nv := reflect.New(rvt.Elem())
|
|
return FillExample(seen, nv.Elem()).Addr()
|
|
}
|
|
return rv
|
|
}
|