| 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 | "log" |
| 22 | "strings" |
| 23 | |
| 24 | "launchpad.net/email-reminder/internal/util" |
| 25 | ) |
| 26 | |
| 27 | type Recipient struct { |
| 28 | XMLName xml.Name `xml:"recipient"` |
| 29 | Name string `xml:"name,attr,omitempty"` |
| 30 | Email string `xml:"email,attr"` |
| 31 | } |
| 32 | |
| 33 | // Splits Name into FirstName/LastName when needed |
| 34 | func (r Recipient) ToEmailRecipient() util.EmailRecipient { |
| 35 | email := strings.TrimSpace(r.Email) |
| 36 | if err := util.ValidateEmail(email); err != nil { |
| 37 | log.Printf("Rejecting invalid email '%s': %s\n", email, err) |
| 38 | email = "" |
| 39 | } |
| 40 | |
| 41 | first := "" |
| 42 | last := "" |
| 43 | name := strings.TrimSpace(r.Name) |
| 44 | if name != "" && len(name) < 255 { // ignore excessively long names |
| 45 | parts := strings.Fields(name) |
| 46 | if len(parts) > 0 { |
| 47 | first = parts[0] |
| 48 | } |
| 49 | if len(parts) > 1 { |
| 50 | last = strings.Join(parts[1:], " ") |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return util.EmailRecipient{Email: email, FirstName: first, LastName: last} |
| 55 | } |