Blame


1 7583dd64 2022-04-13 o package mailmux
2 7583dd64 2022-04-13 o
3 7583dd64 2022-04-13 o import (
4 7583dd64 2022-04-13 o "encoding/json"
5 7583dd64 2022-04-13 o "errors"
6 7583dd64 2022-04-13 o "io"
7 7583dd64 2022-04-13 o "time"
8 a7e7d1d5 2022-04-13 o
9 a7e7d1d5 2022-04-13 o "mailmux.net/aliases"
10 7583dd64 2022-04-13 o )
11 7583dd64 2022-04-13 o
12 7583dd64 2022-04-13 o const (
13 7583dd64 2022-04-13 o Tregister = 1 + iota
14 7583dd64 2022-04-13 o Rregister
15 7583dd64 2022-04-13 o Rerror
16 7583dd64 2022-04-13 o Tnew
17 7583dd64 2022-04-13 o Rnew
18 7583dd64 2022-04-13 o Tlist
19 7583dd64 2022-04-13 o Rlist
20 7583dd64 2022-04-13 o Tremove
21 7583dd64 2022-04-13 o )
22 7583dd64 2022-04-13 o
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 {
30 5b8260a5 2022-04-13 o Type uint
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
36 7583dd64 2022-04-13 o }
37 7583dd64 2022-04-13 o
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) {
40 7583dd64 2022-04-13 o var mc Mcall
41 7583dd64 2022-04-13 o if err := json.NewDecoder(r).Decode(&mc); err != nil {
42 7583dd64 2022-04-13 o return nil, err
43 7583dd64 2022-04-13 o }
44 a7e7d1d5 2022-04-13 o switch mc.Type {
45 a7e7d1d5 2022-04-13 o case Rerror:
46 a7e7d1d5 2022-04-13 o if mc.Error == "" {
47 a7e7d1d5 2022-04-13 o return nil, errors.New("empty error message")
48 a7e7d1d5 2022-04-13 o }
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")
54 a7e7d1d5 2022-04-13 o }
55 7583dd64 2022-04-13 o }
56 7583dd64 2022-04-13 o return &mc, nil
57 7583dd64 2022-04-13 o }