-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathical.go
147 lines (119 loc) · 3.84 KB
/
ical.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package ian
import (
"bytes"
"io"
"time"
"github.com/emersion/go-ical"
)
func FromIcalEvent(icalEvent ical.Event) (EventProperties, error) {
start, err := icalEvent.DateTimeStart(GetTimeZone())
if err != nil {
return EventProperties{}, err
}
end, err := icalEvent.DateTimeEnd(GetTimeZone())
if err != nil {
return EventProperties{}, err
}
if h, m, s := start.Clock(); h+m+s == 0 && start.Equal(end) {
// Event is an *alternatively* formatted all-day event.
end = start.AddDate(0, 0, 1)
}
var uid, summary, description, location, url, rrule, rdate, exdate string
textPropsMap := map[*string]string{
&uid: ical.PropUID,
&summary: ical.PropSummary,
&description: ical.PropDescription,
&location: ical.PropLocation,
&url: ical.PropURL,
&rrule: ical.PropRecurrenceRule,
&rdate: ical.PropRecurrenceDates,
&exdate: ical.PropExceptionDates,
}
for dest, propName := range textPropsMap {
prop := icalEvent.Props.Get(propName)
if prop != nil {
*dest = prop.Value
}
}
// Ignore errors for these, since they may not exist.
created, _ := icalEvent.Props.DateTime(ical.PropCreated, GetTimeZone())
modified, _ := icalEvent.Props.DateTime(ical.PropLastModified, GetTimeZone())
return EventProperties{
Uid: uid,
Summary: summary,
Description: description,
Location: location,
Url: url,
Start: start,
End: end,
Recurrence: Recurrence{rrule, rdate, exdate},
Created: created,
Modified: modified,
}, nil
}
func FromIcal(cal *ical.Calendar) ([]EventProperties, error) {
eventsProps := []EventProperties{}
for _, icalEvent := range cal.Events() {
props, err := FromIcalEvent(icalEvent)
if err != nil {
return nil, err
}
eventsProps = append(eventsProps, props)
}
return eventsProps, nil
}
const IcalPropGrabTimestamp string = "X-IAN-GRABBED"
func ToIcal(events []Event, calendarName string) *ical.Calendar {
cal := ical.NewCalendar()
cal.Props.SetText(ical.PropVersion, "2.0")
cal.Props.SetText(ical.PropProductID, "-//ian//ian calendar migration")
if calendarName != "" {
cal.Props.SetText("X-WR-CALNAME", calendarName)
}
now := time.Now()
cal.Props.SetDateTime(IcalPropGrabTimestamp, now)
for _, event := range events {
icalEvent := ical.NewEvent()
icalEvent.Props.SetText(ical.PropUID, event.Props.Uid)
icalEvent.Props.SetDateTime(ical.PropCreated, event.Props.Created)
icalEvent.Props.SetDateTime(ical.PropLastModified, event.Props.Modified)
icalEvent.Props.SetDateTime(ical.PropDateTimeStamp, now)
if !event.Props.IsAllDay() {
icalEvent.Props.SetDateTime(ical.PropDateTimeStart, event.Props.Start)
icalEvent.Props.SetDateTime(ical.PropDateTimeEnd, event.Props.End)
} else {
icalEvent.Props.SetDate(ical.PropDateTimeStart, event.Props.Start)
icalEvent.Props.SetDate(ical.PropDateTimeEnd, event.Props.Start.AddDate(0, 0, 1))
}
icalEvent.Props.SetText(ical.PropSummary, event.Props.Summary)
optionalProps := map[string]string{
event.Props.Description: ical.PropDescription,
event.Props.Location: ical.PropLocation,
event.Props.Url: ical.PropURL,
event.Props.Recurrence.RRule: ical.PropRecurrenceRule,
event.Props.Recurrence.RDate: ical.PropRecurrenceDates,
event.Props.Recurrence.ExDate: ical.PropExceptionDates,
}
for value, assignAs := range optionalProps {
if value != "" {
icalEvent.Props.SetText(assignAs, value)
}
}
cal.Children = append(cal.Children, icalEvent.Component)
}
return cal
}
func ParseIcal(r io.Reader) (*ical.Calendar, error) {
ics, err := ical.NewDecoder(r).Decode()
if err != nil {
return nil, err
}
return ics, nil
}
func SerializeIcal(ics *ical.Calendar) (bytes.Buffer, error) {
var buf bytes.Buffer
if err := ical.NewEncoder(&buf).Encode(ics); err != nil {
return bytes.Buffer{}, err
}
return buf, nil
}