9 a7e7d1d5 2022-04-13 o "mailmux.net/aliases"
13 7583dd64 2022-04-13 o Tregister = 1 + iota
23 c7ecbae7 2022-04-13 o // Mcall represents a message passed between mailmux clients and servers.
24 7583dd64 2022-04-13 o // Operations requested by clients are T-messages (such as Tregister).
25 c7ecbae7 2022-04-13 o // Servers respond with the corresponding R-message (such as Rregister).
26 c7ecbae7 2022-04-13 o // Servers may instead respond with Rerror to inform the client, with a diagnostic message,
27 c7ecbae7 2022-04-13 o // that the request was not completed successfully.
28 7583dd64 2022-04-13 o // This design is loosely based on the Plan 9 network file protocol 9P.
29 7583dd64 2022-04-13 o type Mcall struct {
31 a7e7d1d5 2022-04-13 o Username string // Tregister, Rregister
32 a7e7d1d5 2022-04-13 o Password string // Tregister
33 a7e7d1d5 2022-04-13 o Error string // Rerror
34 a7e7d1d5 2022-04-13 o Aliases aliases.Aliases // Rnew, Rlist, Tremove
35 a7e7d1d5 2022-04-13 o Expiry time.Time // Tnew, Rnew
38 c7ecbae7 2022-04-13 o // ParseMcall parses and validates a JSON-encoded Mcall from r.
39 7583dd64 2022-04-13 o func ParseMcall(r io.Reader) (*Mcall, error) {
41 7583dd64 2022-04-13 o if err := json.NewDecoder(r).Decode(&mc); err != nil {
42 7583dd64 2022-04-13 o return nil, err
44 a7e7d1d5 2022-04-13 o switch mc.Type {
46 a7e7d1d5 2022-04-13 o if mc.Error == "" {
47 a7e7d1d5 2022-04-13 o return nil, errors.New("empty error message")
49 a7e7d1d5 2022-04-13 o case Tregister, Tnew, Tlist, Tremove:
50 a7e7d1d5 2022-04-13 o if mc.Username == "" {
51 a7e7d1d5 2022-04-13 o return nil, errors.New("empty username")
52 a7e7d1d5 2022-04-13 o } else if mc.Password == "" {
53 a7e7d1d5 2022-04-13 o return nil, errors.New("empty password")
56 7583dd64 2022-04-13 o return &mc, nil