forked from beenote/ical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ical.go
239 lines (202 loc) · 5.29 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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
package ical
// https://www.ietf.org/rfc/rfc2445.txt
import (
"bufio"
"io"
"time"
)
const (
stampLayout = "20060102T150405Z"
dateLayout = "20060102"
dateTimeLayout = "20060102T150405"
)
/*
http://tools.ietf.org/html/draft-daboo-icalendar-extensions-09
http://stackoverflow.com/a/17187346/195141
BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//My Company//NONSGML Event Calendar//EN
URL:http://my.calendar/url
NAME:My Calendar Name
X-WR-CALNAME:My Calendar Name
DESCRIPTION:A description of my calendar
X-WR-CALDESC:A description of my calendar
TIMEZONE-ID:Europe/London
X-WR-TIMEZONE:Europe/London
REFRESH-INTERVAL;VALUE=DURATION:PT12H
X-PUBLISHED-TTL:PT12H
COLOR:34:50:105
CALSCALE:GREGORIAN
METHOD:PUBLISH
*/
type VCalendar struct {
VERSION string // 2.0
PRODID string // -//My Company//NONSGML Event Calendar//EN
URL string // http://my.calendar/url
NAME string // My Calendar Name
X_WR_CALNAME string // My Calendar Name
DESCRIPTION string // A description of my calendar
X_WR_CALDESC string // A description of my calendar
TIMEZONE_ID string // Europe/London
X_WR_TIMEZONE string // Europe/London
REFRESH_INTERVAL string // PT12H
X_PUBLISHED_TTL string // PT12H
COLOR string // 34:50:105
CALSCALE string // GREGORIAN
METHOD string // PUBLISH
VComponent []VComponent
}
func NewBasicVCalendar() *VCalendar {
return &VCalendar{
VERSION: "2.0",
CALSCALE: "GREGORIAN",
}
}
func (c *VCalendar) Encode(w io.Writer) error {
var b = bufio.NewWriter(w)
if _, err := b.WriteString("BEGIN:VCALENDAR\r\n"); err != nil {
return err
}
// use a slice map to preserve order during for range
attrs := []map[string]string{
{"VERSION:": c.VERSION},
{"PRODID:": c.PRODID},
{"URL:": c.URL},
{"NAME:": c.NAME},
{"X-WR-CALNAME:": c.X_WR_CALNAME},
{"DESCRIPTION:": c.DESCRIPTION},
{"X-WR-CALDESC:": c.X_WR_CALDESC},
{"TIMEZONE-ID:": c.TIMEZONE_ID},
{"X-WR-TIMEZONE:": c.X_WR_TIMEZONE},
{"REFRESH-INTERVAL;VALUE=DURATION:": c.REFRESH_INTERVAL},
{"X-PUBLISHED-TTL:": c.X_PUBLISHED_TTL},
{"COLOR:": c.COLOR},
{"CALSCALE:": c.CALSCALE},
{"METHOD:": c.METHOD},
}
for _, item := range attrs {
for k, v := range item {
if len(v) == 0 {
continue
}
if _, err := b.WriteString(k + v + "\r\n"); err != nil {
return err
}
}
}
for _, component := range c.VComponent {
if err := component.EncodeIcal(b); err != nil {
return err
}
}
if _, err := b.WriteString("END:VCALENDAR\r\n"); err != nil {
return err
}
return b.Flush()
}
type VComponent interface {
EncodeIcal(w io.Writer) error
}
type VEvent struct {
UID string
DTSTAMP time.Time
DTSTART time.Time
DTEND time.Time
ORGANIZER string
ATTENDEE []string
SUMMARY string
DESCRIPTION string
TZID string
SEQUENCE string
STATUS string
ALARM string
LOCATION string
TRANSP string
AllDay bool
}
func (e *VEvent) EncodeIcal(w io.Writer) error {
var timeStampLayout, timeStampType, tzidTxt string
if e.AllDay {
timeStampLayout = dateLayout
timeStampType = "DATE"
} else {
timeStampLayout = dateTimeLayout
timeStampType = "DATE-TIME"
if len(e.TZID) == 0 || e.TZID == "UTC" {
timeStampLayout = timeStampLayout + "Z"
}
}
if len(e.TZID) != 0 && e.TZID != "UTC" {
tzidTxt = "TZID=" + e.TZID + ";"
}
b := bufio.NewWriter(w)
if _, err := b.WriteString("BEGIN:VEVENT\r\n"); err != nil {
return err
}
if _, err := b.WriteString("DTSTAMP:" + e.DTSTAMP.UTC().Format(stampLayout) + "\r\n"); err != nil {
return err
}
if _, err := b.WriteString("UID:" + e.UID + "\r\n"); err != nil {
return err
}
if len(e.TZID) != 0 && e.TZID != "UTC" {
if _, err := b.WriteString("TZID:" + e.TZID + "\r\n"); err != nil {
return err
}
}
if e.ORGANIZER != "" {
if _, err := b.WriteString("ORGANIZER" + e.ORGANIZER + "\r\n"); err != nil {
return err
}
}
if len(e.ATTENDEE) > 0 {
for _, attendee := range e.ATTENDEE {
if _, err := b.WriteString("ATTENDEE;CN=" + attendee + ":MAILTO:" + attendee + "\r\n"); err != nil {
return err
}
}
}
if e.SEQUENCE != "" {
if _, err := b.WriteString("SEQUENCE:" + e.SEQUENCE + "\r\n"); err != nil {
return err
}
}
if e.STATUS != "" {
if _, err := b.WriteString("STATUS:" + e.STATUS + "\r\n"); err != nil {
return err
}
}
if _, err := b.WriteString("SUMMARY:" + e.SUMMARY + "\r\n"); err != nil {
return err
}
if e.DESCRIPTION != "" {
if _, err := b.WriteString("DESCRIPTION:" + e.DESCRIPTION + "\r\n"); err != nil {
return err
}
}
if e.LOCATION != "" {
if _, err := b.WriteString("LOCATION:" + e.LOCATION + "\r\n"); err != nil {
return err
}
}
if e.TRANSP != "" {
if _, err := b.WriteString("TRANSP:" + e.TRANSP + "\r\n"); err != nil {
return err
}
}
if _, err := b.WriteString("DTSTART;" + tzidTxt + "VALUE=" + timeStampType + ":" + e.DTSTART.Format(timeStampLayout) + "\r\n"); err != nil {
return err
}
if _, err := b.WriteString("DTEND;" + tzidTxt + "VALUE=" + timeStampType + ":" + e.DTEND.Format(timeStampLayout) + "\r\n"); err != nil {
return err
}
if e.ALARM != "" {
if _, err := b.WriteString("BEGIN:VALARM\r\nTRIGGER:" + e.ALARM + "\r\nACTION:DISPLAY\r\nEND:VALARM\r\n"); err != nil {
return err
}
}
if _, err := b.WriteString("END:VEVENT\r\n"); err != nil {
return err
}
return b.Flush()
}