| 1 |
package calendar |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"net/mail" |
| 8 |
"regexp" |
| 9 |
"strings" |
| 10 |
"time" |
| 11 |
|
| 12 |
ics "github.com/arran4/golang-ical" |
| 13 |
) |
| 14 |
|
| 15 |
type Reply struct { |
| 16 |
MimeType string |
| 17 |
Params map[string]string |
| 18 |
CalendarText io.ReadWriter |
| 19 |
PlainText io.ReadWriter |
| 20 |
Organizers []string |
| 21 |
} |
| 22 |
|
| 23 |
func (cr *Reply) AddOrganizer(o string) { |
| 24 |
cr.Organizers = append(cr.Organizers, o) |
| 25 |
} |
| 26 |
|
| 27 |
// CreateReply parses a ics request and return a ics reply (RFC 2446, Section 3.2.3) |
| 28 |
func CreateReply(reader io.Reader, from *mail.Address, partstat string) (*Reply, error) { |
| 29 |
cr := Reply{ |
| 30 |
MimeType: "text/calendar", |
| 31 |
Params: map[string]string{ |
| 32 |
"charset": "UTF-8", |
| 33 |
"method": "REPLY", |
| 34 |
}, |
| 35 |
CalendarText: &bytes.Buffer{}, |
| 36 |
PlainText: &bytes.Buffer{}, |
| 37 |
} |
| 38 |
|
| 39 |
var ( |
| 40 |
status ics.ParticipationStatus |
| 41 |
action string |
| 42 |
) |
| 43 |
|
| 44 |
switch partstat { |
| 45 |
case "accept": |
| 46 |
status = ics.ParticipationStatusAccepted |
| 47 |
action = "accepted" |
| 48 |
case "accept-tentative": |
| 49 |
status = ics.ParticipationStatusTentative |
| 50 |
action = "tentatively accepted" |
| 51 |
case "decline": |
| 52 |
status = ics.ParticipationStatusDeclined |
| 53 |
action = "declined" |
| 54 |
default: |
| 55 |
return nil, fmt.Errorf("participation status %s is not implemented", partstat) |
| 56 |
} |
| 57 |
|
| 58 |
name := from.Name |
| 59 |
if name == "" { |
| 60 |
name = from.Address |
| 61 |
} |
| 62 |
fmt.Fprintf(cr.PlainText, "%s has %s this invitation.", name, action) |
| 63 |
|
| 64 |
invite, err := parse(reader) |
| 65 |
if err != nil { |
| 66 |
return nil, err |
| 67 |
} |
| 68 |
|
| 69 |
if ok := invite.request(); !ok { |
| 70 |
return nil, fmt.Errorf("no reply is requested") |
| 71 |
} |
| 72 |
|
| 73 |
// update invite as a reply |
| 74 |
reply := invite |
| 75 |
reply.SetMethod(ics.MethodReply) |
| 76 |
reply.SetProductId("aerc") |
| 77 |
|
| 78 |
// check all events |
| 79 |
for _, vevent := range reply.Events() { |
| 80 |
e := event{vevent} |
| 81 |
|
| 82 |
// check if we should answer |
| 83 |
if err := e.isReplyRequested(from.Address); err != nil { |
| 84 |
return nil, err |
| 85 |
} |
| 86 |
|
| 87 |
// make sure we send our reply to the meeting organizer |
| 88 |
if organizer := e.GetProperty(ics.ComponentPropertyOrganizer); organizer != nil { |
| 89 |
cr.AddOrganizer(organizer.Value) |
| 90 |
} |
| 91 |
|
| 92 |
// update attendee participation status |
| 93 |
e.updateAttendees(status, from.Address) |
| 94 |
|
| 95 |
// update timestamp |
| 96 |
e.SetDtStampTime(time.Now()) |
| 97 |
|
| 98 |
// remove any subcomponents of event |
| 99 |
e.Components = nil |
| 100 |
} |
| 101 |
|
| 102 |
// keep only timezone and event components |
| 103 |
reply.clean() |
| 104 |
|
| 105 |
if len(reply.Events()) == 0 { |
| 106 |
return nil, fmt.Errorf("no events to respond to") |
| 107 |
} |
| 108 |
|
| 109 |
if err := reply.SerializeTo(cr.CalendarText); err != nil { |
| 110 |
return nil, err |
| 111 |
} |
| 112 |
return &cr, nil |
| 113 |
} |
| 114 |
|
| 115 |
type calendar struct { |
| 116 |
*ics.Calendar |
| 117 |
} |
| 118 |
|
| 119 |
func parse(reader io.Reader) (*calendar, error) { |
| 120 |
// fix capitalized mailto for parsing of ics file |
| 121 |
var sb strings.Builder |
| 122 |
_, err := io.Copy(&sb, reader) |
| 123 |
if err != nil { |
| 124 |
return nil, fmt.Errorf("failed to copy calendar data: %w", err) |
| 125 |
} |
| 126 |
re := regexp.MustCompile("MAILTO:(.+@)") |
| 127 |
str := re.ReplaceAllString(sb.String(), "mailto:${1}") |
| 128 |
|
| 129 |
// parse calendar |
| 130 |
invite, err := ics.ParseCalendar(strings.NewReader(str)) |
| 131 |
if err != nil { |
| 132 |
return nil, err |
| 133 |
} |
| 134 |
return &calendar{invite}, nil |
| 135 |
} |
| 136 |
|
| 137 |
func (cal *calendar) request() (ok bool) { |
| 138 |
ok = false |
| 139 |
for i := range cal.CalendarProperties { |
| 140 |
if cal.CalendarProperties[i].IANAToken == string(ics.PropertyMethod) { |
| 141 |
if cal.CalendarProperties[i].Value == string(ics.MethodRequest) { |
| 142 |
ok = true |
| 143 |
return |
| 144 |
} |
| 145 |
} |
| 146 |
} |
| 147 |
return |
| 148 |
} |
| 149 |
|
| 150 |
func (cal *calendar) clean() { |
| 151 |
var clean []ics.Component |
| 152 |
for _, comp := range cal.Components { |
| 153 |
switch comp.(type) { |
| 154 |
case *ics.VTimezone, *ics.VEvent: |
| 155 |
clean = append(clean, comp) |
| 156 |
default: |
| 157 |
continue |
| 158 |
} |
| 159 |
} |
| 160 |
cal.Components = clean |
| 161 |
} |
| 162 |
|
| 163 |
type event struct { |
| 164 |
*ics.VEvent |
| 165 |
} |
| 166 |
|
| 167 |
func (e *event) isReplyRequested(from string) error { |
| 168 |
var present bool = false |
| 169 |
var rsvp bool = false |
| 170 |
from = strings.ToLower(from) |
| 171 |
for _, a := range e.Attendees() { |
| 172 |
if strings.ToLower(a.Email()) == from { |
| 173 |
present = true |
| 174 |
if r, ok := a.ICalParameters[string(ics.ParameterRsvp)]; ok { |
| 175 |
if len(r) > 0 && strings.ToLower(r[0]) == "true" { |
| 176 |
rsvp = true |
| 177 |
} |
| 178 |
} |
| 179 |
} |
| 180 |
} |
| 181 |
if !present { |
| 182 |
return fmt.Errorf("we are not invited") |
| 183 |
} |
| 184 |
if !rsvp { |
| 185 |
return fmt.Errorf("we don't have to rsvp") |
| 186 |
} |
| 187 |
return nil |
| 188 |
} |
| 189 |
|
| 190 |
func (e *event) updateAttendees(status ics.ParticipationStatus, from string) { |
| 191 |
var clean []ics.IANAProperty |
| 192 |
for _, prop := range e.Properties { |
| 193 |
if prop.IANAToken == string(ics.ComponentPropertyAttendee) { |
| 194 |
att := ics.Attendee{IANAProperty: prop} |
| 195 |
if att.Email() != from { |
| 196 |
continue |
| 197 |
} |
| 198 |
prop.ICalParameters[string(ics.ParameterParticipationStatus)] = []string{string(status)} |
| 199 |
delete(prop.ICalParameters, string(ics.ParameterRsvp)) |
| 200 |
} |
| 201 |
clean = append(clean, prop) |
| 202 |
} |
| 203 |
e.Properties = clean |
| 204 |
} |