| 1 | // # Copyright (C) 2025 Francois Marier |
| 2 | // |
| 3 | // Email-Reminder is free software; you can redistribute it and/or |
| 4 | // modify it under the terms of the GNU General Public License as |
| 5 | // published by the Free Software Foundation; either version 3 of the |
| 6 | // License, or (at your option) any later version. |
| 7 | // |
| 8 | // Email-Reminder is distributed in the hope that it will be useful, |
| 9 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | // General Public License for more details. |
| 12 | // |
| 13 | // You should have received a copy of the GNU General Public License |
| 14 | // along with Email-Reminder; if not, write to the Free Software |
| 15 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 16 | // 02110-1301, USA. |
| 17 | package events |
| 18 | |
| 19 | import ( |
| 20 | "encoding/xml" |
| 21 | "fmt" |
| 22 | "log" |
| 23 | "strings" |
| 24 | |
| 25 | "launchpad.net/email-reminder/internal/config" |
| 26 | "launchpad.net/email-reminder/internal/util" |
| 27 | ) |
| 28 | |
| 29 | type User struct { |
| 30 | XMLName xml.Name `xml:"email-reminder_user"` |
| 31 | FirstName string `xml:"first_name,omitempty"` |
| 32 | LastName string `xml:"last_name,omitempty"` |
| 33 | Email string `xml:"email"` |
| 34 | Events []Event `xml:"events>event,omitempty"` |
| 35 | } |
| 36 | |
| 37 | func (u User) ToEmailRecipient() util.EmailRecipient { |
| 38 | email := strings.TrimSpace(u.Email) |
| 39 | if err := util.ValidateEmail(email); err != nil { |
| 40 | log.Printf("Rejecting invalid email '%s': %s\n", email, err) |
| 41 | email = "" |
| 42 | } |
| 43 | |
| 44 | return util.EmailRecipient{Email: email, FirstName: strings.TrimSpace(u.FirstName), LastName: strings.TrimSpace(u.LastName)} |
| 45 | } |
| 46 | |
| 47 | func LoadUserEvents(filename string, contents []byte, conf config.Config) (User, error) { |
| 48 | v := User{} |
| 49 | err := xml.Unmarshal(contents, &v) |
| 50 | if err != nil { |
| 51 | return v, fmt.Errorf("error parsing XML: %w", err) |
| 52 | } |
| 53 | if len(v.Events) > conf.MaxEventsPerUser { |
| 54 | return v, fmt.Errorf("too many events (%d), maximum allowed is %d", len(v.Events), conf.MaxEventsPerUser) |
| 55 | } |
| 56 | return v, nil |
| 57 | } |