|
| 1 | +package update |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "strings" |
| 6 | + |
| 7 | + "github.com/pixel365/goepp/command" |
| 8 | + "github.com/pixel365/goepp/command/create" |
| 9 | +) |
| 10 | + |
| 11 | +type ContactData struct { |
| 12 | + Add *ContactAdd `xml:"add,omitempty"` |
| 13 | + Remove *ContactRemove `xml:"rem,omitempty"` |
| 14 | + Change *ContactChange `xml:"chg,omitempty"` |
| 15 | + command.ContactRef |
| 16 | +} |
| 17 | + |
| 18 | +type ContactAdd struct { |
| 19 | + Statuses []ContactStatus `xml:"status,omitempty"` |
| 20 | +} |
| 21 | + |
| 22 | +type ContactRemove struct { |
| 23 | + Statuses []ContactStatus `xml:"status,omitempty"` |
| 24 | +} |
| 25 | + |
| 26 | +type ContactChange struct { |
| 27 | + Voice *string `xml:"voice,omitempty"` |
| 28 | + Fax *string `xml:"fax,omitempty"` |
| 29 | + Email *string `xml:"email,omitempty"` |
| 30 | + AuthInfo *command.AuthInfo `xml:"authInfo,omitempty"` |
| 31 | + Disclosure *Disclosure `xml:"disclose,omitempty"` |
| 32 | + PostalInfo []create.PostalInfo `xml:"postalInfo,omitempty"` |
| 33 | +} |
| 34 | + |
| 35 | +type ContactStatus struct { |
| 36 | + Value string `xml:"s,attr"` |
| 37 | +} |
| 38 | + |
| 39 | +type Disclosure struct { |
| 40 | + Fields []DisclosureElement `xml:",any,omitempty"` |
| 41 | + Flag bool `xml:"flag,attr"` |
| 42 | +} |
| 43 | + |
| 44 | +type DisclosureElement struct { |
| 45 | + XMLName struct{} `xml:"-"` |
| 46 | + Type string `xml:"type,attr,omitempty"` |
| 47 | + Value string `xml:",chardata"` |
| 48 | +} |
| 49 | + |
| 50 | +// Validate https://datatracker.ietf.org/doc/html/rfc5733#section-3.2.5 |
| 51 | +func (c *ContactData) Validate() error { |
| 52 | + if c.ID == "" { |
| 53 | + return errors.New("contact:id is required") |
| 54 | + } |
| 55 | + |
| 56 | + if c.Add == nil && c.Remove == nil && c.Change == nil { |
| 57 | + return errors.New("contact:add, rem or chg is required") |
| 58 | + } |
| 59 | + |
| 60 | + if c.Add != nil { |
| 61 | + if err := c.Add.Validate(); err != nil { |
| 62 | + return err |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + if c.Remove != nil { |
| 67 | + if err := c.Remove.Validate(); err != nil { |
| 68 | + return err |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + if c.Change != nil { |
| 73 | + if err := c.Change.Validate(); err != nil { |
| 74 | + return err |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return nil |
| 79 | +} |
| 80 | + |
| 81 | +func (a *ContactAdd) Validate() error { |
| 82 | + if len(a.Statuses) == 0 { |
| 83 | + return errors.New("add:status is required") |
| 84 | + } |
| 85 | + |
| 86 | + for _, s := range a.Statuses { |
| 87 | + if err := s.Validate(); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return nil |
| 93 | +} |
| 94 | + |
| 95 | +func (r *ContactRemove) Validate() error { |
| 96 | + if len(r.Statuses) == 0 { |
| 97 | + return errors.New("rem:status is required") |
| 98 | + } |
| 99 | + |
| 100 | + for _, s := range r.Statuses { |
| 101 | + if err := s.Validate(); err != nil { |
| 102 | + return err |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + return nil |
| 107 | +} |
| 108 | + |
| 109 | +func (c *ContactChange) Validate() error { |
| 110 | + if err := c.validateNotEmpty(); err != nil { |
| 111 | + return err |
| 112 | + } |
| 113 | + |
| 114 | + if err := c.validatePostalInfo(); err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + if c.Voice != nil && strings.TrimSpace(*c.Voice) == "" { |
| 119 | + return errors.New("chg:voice is empty") |
| 120 | + } |
| 121 | + |
| 122 | + if c.Fax != nil && strings.TrimSpace(*c.Fax) == "" { |
| 123 | + return errors.New("chg:fax is empty") |
| 124 | + } |
| 125 | + |
| 126 | + if c.Email != nil && strings.TrimSpace(*c.Email) == "" { |
| 127 | + return errors.New("chg:email is empty") |
| 128 | + } |
| 129 | + |
| 130 | + if c.AuthInfo != nil { |
| 131 | + if err := c.AuthInfo.Validate(); err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + if c.Disclosure != nil { |
| 137 | + if err := c.Disclosure.Validate(); err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + return nil |
| 143 | +} |
| 144 | + |
| 145 | +func (c *ContactChange) validateNotEmpty() error { |
| 146 | + if len(c.PostalInfo) == 0 && |
| 147 | + c.Voice == nil && |
| 148 | + c.Fax == nil && |
| 149 | + c.Email == nil && |
| 150 | + c.AuthInfo == nil && |
| 151 | + c.Disclosure == nil { |
| 152 | + return errors.New("chg:postalInfo, voice, fax, email, authInfo or disclose is required") |
| 153 | + } |
| 154 | + |
| 155 | + return nil |
| 156 | +} |
| 157 | + |
| 158 | +func (c *ContactChange) validatePostalInfo() error { |
| 159 | + for i := range c.PostalInfo { |
| 160 | + if err := c.PostalInfo[i].Validate(); err != nil { |
| 161 | + return err |
| 162 | + } |
| 163 | + } |
| 164 | + |
| 165 | + return nil |
| 166 | +} |
| 167 | + |
| 168 | +func (s ContactStatus) Validate() error { |
| 169 | + if strings.TrimSpace(s.Value) == "" { |
| 170 | + return errors.New("status value is required") |
| 171 | + } |
| 172 | + |
| 173 | + return nil |
| 174 | +} |
| 175 | + |
| 176 | +func (d *Disclosure) Validate() error { |
| 177 | + if len(d.Fields) == 0 { |
| 178 | + return errors.New("disclose:name, org, addr, voice, fax or email is required") |
| 179 | + } |
| 180 | + |
| 181 | + return nil |
| 182 | +} |
0 commit comments