forked from arran4/golang-ical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalendar.go
560 lines (499 loc) · 18 KB
/
calendar.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
package ics
import (
"bufio"
"bytes"
"errors"
"fmt"
"io"
"time"
)
type ComponentType string
const (
ComponentVCalendar ComponentType = "VCALENDAR"
ComponentVEvent ComponentType = "VEVENT"
ComponentVTodo ComponentType = "VTODO"
ComponentVJournal ComponentType = "VJOURNAL"
ComponentVFreeBusy ComponentType = "VFREEBUSY"
ComponentVTimezone ComponentType = "VTIMEZONE"
ComponentVAlarm ComponentType = "VALARM"
ComponentStandard ComponentType = "STANDARD"
ComponentDaylight ComponentType = "DAYLIGHT"
)
type ComponentProperty Property
const (
ComponentPropertyUniqueId = ComponentProperty(PropertyUid) // TEXT
ComponentPropertyDtstamp = ComponentProperty(PropertyDtstamp)
ComponentPropertyOrganizer = ComponentProperty(PropertyOrganizer)
ComponentPropertyAttendee = ComponentProperty(PropertyAttendee)
ComponentPropertyAttach = ComponentProperty(PropertyAttach)
ComponentPropertyDescription = ComponentProperty(PropertyDescription) // TEXT
ComponentPropertyCategories = ComponentProperty(PropertyCategories) // TEXT
ComponentPropertyClass = ComponentProperty(PropertyClass) // TEXT
ComponentPropertyColor = ComponentProperty(PropertyColor) // TEXT
ComponentPropertyCreated = ComponentProperty(PropertyCreated)
ComponentPropertySummary = ComponentProperty(PropertySummary) // TEXT
ComponentPropertyDtStart = ComponentProperty(PropertyDtstart)
ComponentPropertyDtEnd = ComponentProperty(PropertyDtend)
ComponentPropertyLocation = ComponentProperty(PropertyLocation) // TEXT
ComponentPropertyStatus = ComponentProperty(PropertyStatus) // TEXT
ComponentPropertyFreebusy = ComponentProperty(PropertyFreebusy)
ComponentPropertyLastModified = ComponentProperty(PropertyLastModified)
ComponentPropertyUrl = ComponentProperty(PropertyUrl)
ComponentPropertyGeo = ComponentProperty(PropertyGeo)
ComponentPropertyTransp = ComponentProperty(PropertyTransp)
ComponentPropertySequence = ComponentProperty(PropertySequence)
ComponentPropertyExdate = ComponentProperty(PropertyExdate)
ComponentPropertyExrule = ComponentProperty(PropertyExrule)
ComponentPropertyRdate = ComponentProperty(PropertyRdate)
ComponentPropertyRrule = ComponentProperty(PropertyRrule)
ComponentPropertyAction = ComponentProperty(PropertyAction)
ComponentPropertyTrigger = ComponentProperty(PropertyTrigger)
ComponentPropertyPriority = ComponentProperty(PropertyPriority)
ComponentPropertyResources = ComponentProperty(PropertyResources)
ComponentPropertyCompleted = ComponentProperty(PropertyCompleted)
ComponentPropertyDue = ComponentProperty(PropertyDue)
ComponentPropertyPercentComplete = ComponentProperty(PropertyPercentComplete)
ComponentPropertyTzid = ComponentProperty(PropertyTzid)
ComponentPropertyComment = ComponentProperty(PropertyComment)
)
type Property string
const (
PropertyCalscale Property = "CALSCALE" // TEXT
PropertyMethod Property = "METHOD" // TEXT
PropertyProductId Property = "PRODID" // TEXT
PropertyVersion Property = "VERSION" // TEXT
PropertyXPublishedTTL Property = "X-PUBLISHED-TTL"
PropertyRefreshInterval Property = "REFRESH-INTERVAL;VALUE=DURATION"
PropertyAttach Property = "ATTACH"
PropertyCategories Property = "CATEGORIES" // TEXT
PropertyClass Property = "CLASS" // TEXT
PropertyColor Property = "COLOR" // TEXT
PropertyComment Property = "COMMENT" // TEXT
PropertyDescription Property = "DESCRIPTION" // TEXT
PropertyXWRCalDesc Property = "X-WR-CALDESC"
PropertyGeo Property = "GEO"
PropertyLocation Property = "LOCATION" // TEXT
PropertyPercentComplete Property = "PERCENT-COMPLETE"
PropertyPriority Property = "PRIORITY"
PropertyResources Property = "RESOURCES" // TEXT
PropertyStatus Property = "STATUS" // TEXT
PropertySummary Property = "SUMMARY" // TEXT
PropertyCompleted Property = "COMPLETED"
PropertyDtend Property = "DTEND"
PropertyDue Property = "DUE"
PropertyDtstart Property = "DTSTART"
PropertyDuration Property = "DURATION"
PropertyFreebusy Property = "FREEBUSY"
PropertyTransp Property = "TRANSP" // TEXT
PropertyTzid Property = "TZID" // TEXT
PropertyTzname Property = "TZNAME" // TEXT
PropertyTzoffsetfrom Property = "TZOFFSETFROM"
PropertyTzoffsetto Property = "TZOFFSETTO"
PropertyTzurl Property = "TZURL"
PropertyAttendee Property = "ATTENDEE"
PropertyContact Property = "CONTACT" // TEXT
PropertyOrganizer Property = "ORGANIZER"
PropertyRecurrenceId Property = "RECURRENCE-ID"
PropertyRelatedTo Property = "RELATED-TO" // TEXT
PropertyUrl Property = "URL"
PropertyUid Property = "UID" // TEXT
PropertyExdate Property = "EXDATE"
PropertyExrule Property = "EXRULE"
PropertyRdate Property = "RDATE"
PropertyRrule Property = "RRULE"
PropertyAction Property = "ACTION" // TEXT
PropertyRepeat Property = "REPEAT"
PropertyTrigger Property = "TRIGGER"
PropertyCreated Property = "CREATED"
PropertyDtstamp Property = "DTSTAMP"
PropertyLastModified Property = "LAST-MODIFIED"
PropertyRequestStatus Property = "REQUEST-STATUS" // TEXT
PropertyName Property = "NAME"
PropertyXWRCalName Property = "X-WR-CALNAME"
PropertyXWRTimezone Property = "X-WR-TIMEZONE"
PropertySequence Property = "SEQUENCE"
PropertyXWRCalID Property = "X-WR-RELCALID"
PropertyTimezoneId Property = "TIMEZONE-ID"
)
type Parameter string
const (
ParameterAltrep Parameter = "ALTREP"
ParameterCn Parameter = "CN"
ParameterCutype Parameter = "CUTYPE"
ParameterDelegatedFrom Parameter = "DELEGATED-FROM"
ParameterDelegatedTo Parameter = "DELEGATED-TO"
ParameterDir Parameter = "DIR"
ParameterEncoding Parameter = "ENCODING"
ParameterFmttype Parameter = "FMTTYPE"
ParameterFbtype Parameter = "FBTYPE"
ParameterLanguage Parameter = "LANGUAGE"
ParameterMember Parameter = "MEMBER"
ParameterParticipationStatus Parameter = "PARTSTAT"
ParameterRange Parameter = "RANGE"
ParameterRelated Parameter = "RELATED"
ParameterReltype Parameter = "RELTYPE"
ParameterRole Parameter = "ROLE"
ParameterRsvp Parameter = "RSVP"
ParameterSentBy Parameter = "SENT-BY"
ParameterTzid Parameter = "TZID"
ParameterValue Parameter = "VALUE"
)
type ValueDataType string
const (
ValueDataTypeBinary ValueDataType = "BINARY"
ValueDataTypeBoolean ValueDataType = "BOOLEAN"
ValueDataTypeCalAddress ValueDataType = "CAL-ADDRESS"
ValueDataTypeDate ValueDataType = "DATE"
ValueDataTypeDateTime ValueDataType = "DATE-TIME"
ValueDataTypeDuration ValueDataType = "DURATION"
ValueDataTypeFloat ValueDataType = "FLOAT"
ValueDataTypeInteger ValueDataType = "INTEGER"
ValueDataTypePeriod ValueDataType = "PERIOD"
ValueDataTypeRecur ValueDataType = "RECUR"
ValueDataTypeText ValueDataType = "TEXT"
ValueDataTypeTime ValueDataType = "TIME"
ValueDataTypeUri ValueDataType = "URI"
ValueDataTypeUtcOffset ValueDataType = "UTC-OFFSET"
)
type CalendarUserType string
const (
CalendarUserTypeIndividual CalendarUserType = "INDIVIDUAL"
CalendarUserTypeGroup CalendarUserType = "GROUP"
CalendarUserTypeResource CalendarUserType = "RESOURCE"
CalendarUserTypeRoom CalendarUserType = "ROOM"
CalendarUserTypeUnknown CalendarUserType = "UNKNOWN"
)
func (cut CalendarUserType) KeyValue(s ...interface{}) (string, []string) {
return string(ParameterCutype), []string{string(cut)}
}
type FreeBusyTimeType string
const (
FreeBusyTimeTypeFree FreeBusyTimeType = "FREE"
FreeBusyTimeTypeBusy FreeBusyTimeType = "BUSY"
FreeBusyTimeTypeBusyUnavailable FreeBusyTimeType = "BUSY-UNAVAILABLE"
FreeBusyTimeTypeBusyTentative FreeBusyTimeType = "BUSY-TENTATIVE"
)
type ParticipationStatus string
const (
ParticipationStatusNeedsAction ParticipationStatus = "NEEDS-ACTION"
ParticipationStatusAccepted ParticipationStatus = "ACCEPTED"
ParticipationStatusDeclined ParticipationStatus = "DECLINED"
ParticipationStatusTentative ParticipationStatus = "TENTATIVE"
ParticipationStatusDelegated ParticipationStatus = "DELEGATED"
ParticipationStatusCompleted ParticipationStatus = "COMPLETED"
ParticipationStatusInProcess ParticipationStatus = "IN-PROCESS"
)
func (ps ParticipationStatus) KeyValue(s ...interface{}) (string, []string) {
return string(ParameterParticipationStatus), []string{string(ps)}
}
type ObjectStatus string
const (
ObjectStatusTentative ObjectStatus = "TENTATIVE"
ObjectStatusConfirmed ObjectStatus = "CONFIRMED"
ObjectStatusCancelled ObjectStatus = "CANCELLED"
ObjectStatusNeedsAction ObjectStatus = "NEEDS-ACTION"
ObjectStatusCompleted ObjectStatus = "COMPLETED"
ObjectStatusInProcess ObjectStatus = "IN-PROCESS"
ObjectStatusDraft ObjectStatus = "DRAFT"
ObjectStatusFinal ObjectStatus = "FINAL"
)
func (ps ObjectStatus) KeyValue(s ...interface{}) (string, []string) {
return string(PropertyStatus), []string{ToText(string(ps))}
}
type RelationshipType string
const (
RelationshipTypeChild RelationshipType = "CHILD"
RelationshipTypeParent RelationshipType = "PARENT"
RelationshipTypeSibling RelationshipType = "SIBLING"
)
type ParticipationRole string
const (
ParticipationRoleChair ParticipationRole = "CHAIR"
ParticipationRoleReqParticipant ParticipationRole = "REQ-PARTICIPANT"
ParticipationRoleOptParticipant ParticipationRole = "OPT-PARTICIPANT"
ParticipationRoleNonParticipant ParticipationRole = "NON-PARTICIPANT"
)
func (pr ParticipationRole) KeyValue(s ...interface{}) (string, []string) {
return string(ParameterRole), []string{string(pr)}
}
type Action string
const (
ActionAudio Action = "AUDIO"
ActionDisplay Action = "DISPLAY"
ActionEmail Action = "EMAIL"
ActionProcedure Action = "PROCEDURE"
)
type Classification string
const (
ClassificationPublic Classification = "PUBLIC"
ClassificationPrivate Classification = "PRIVATE"
ClassificationConfidential Classification = "CONFIDENTIAL"
)
type Method string
const (
MethodPublish Method = "PUBLISH"
MethodRequest Method = "REQUEST"
MethodReply Method = "REPLY"
MethodAdd Method = "ADD"
MethodCancel Method = "CANCEL"
MethodRefresh Method = "REFRESH"
MethodCounter Method = "COUNTER"
MethodDeclinecounter Method = "DECLINECOUNTER"
)
type CalendarProperty struct {
BaseProperty
}
type Calendar struct {
Components []Component
CalendarProperties []CalendarProperty
}
func NewCalendar() *Calendar {
return NewCalendarFor("arran4")
}
func NewCalendarFor(service string) *Calendar {
c := &Calendar{
Components: []Component{},
CalendarProperties: []CalendarProperty{},
}
c.SetVersion("2.0")
c.SetProductId("-//" + service + "//Golang ICS Library")
return c
}
func (calendar *Calendar) Serialize() string {
b := bytes.NewBufferString("")
// We are intentionally ignoring the return value. _ used to communicate this to lint.
_ = calendar.SerializeTo(b)
return b.String()
}
func (calendar *Calendar) SerializeTo(w io.Writer) error {
_, _ = fmt.Fprint(w, "BEGIN:VCALENDAR", "\r\n")
for _, p := range calendar.CalendarProperties {
p.serialize(w)
}
for _, c := range calendar.Components {
c.serialize(w)
}
_, _ = fmt.Fprint(w, "END:VCALENDAR", "\r\n")
return nil
}
func (calendar *Calendar) SetMethod(method Method, props ...PropertyParameter) {
calendar.setProperty(PropertyMethod, ToText(string(method)), props...)
}
func (calendar *Calendar) SetXPublishedTTL(s string, props ...PropertyParameter) {
calendar.setProperty(PropertyXPublishedTTL, string(s), props...)
}
func (calendar *Calendar) SetVersion(s string, props ...PropertyParameter) {
calendar.setProperty(PropertyVersion, ToText(s), props...)
}
func (calendar *Calendar) SetProductId(s string, props ...PropertyParameter) {
calendar.setProperty(PropertyProductId, ToText(s), props...)
}
func (calendar *Calendar) SetName(s string, props ...PropertyParameter) {
calendar.setProperty(PropertyName, string(s), props...)
calendar.setProperty(PropertyXWRCalName, string(s), props...)
}
func (calendar *Calendar) SetColor(s string, props ...PropertyParameter) {
calendar.setProperty(PropertyColor, string(s), props...)
}
func (calendar *Calendar) SetXWRCalName(s string, props ...PropertyParameter) {
calendar.setProperty(PropertyXWRCalName, string(s), props...)
}
func (calendar *Calendar) SetXWRCalDesc(s string, props ...PropertyParameter) {
calendar.setProperty(PropertyXWRCalDesc, string(s), props...)
}
func (calendar *Calendar) SetXWRTimezone(s string, props ...PropertyParameter) {
calendar.setProperty(PropertyXWRTimezone, string(s), props...)
}
func (calendar *Calendar) SetXWRCalID(s string, props ...PropertyParameter) {
calendar.setProperty(PropertyXWRCalID, string(s), props...)
}
func (calendar *Calendar) SetDescription(s string, props ...PropertyParameter) {
calendar.setProperty(PropertyDescription, ToText(s), props...)
}
func (calendar *Calendar) SetLastModified(t time.Time, props ...PropertyParameter) {
calendar.setProperty(PropertyLastModified, t.UTC().Format(icalTimestampFormatUtc), props...)
}
func (calendar *Calendar) SetRefreshInterval(s string, props ...PropertyParameter) {
calendar.setProperty(PropertyRefreshInterval, string(s), props...)
}
func (calendar *Calendar) SetCalscale(s string, props ...PropertyParameter) {
calendar.setProperty(PropertyCalscale, string(s), props...)
}
func (calendar *Calendar) SetUrl(s string, props ...PropertyParameter) {
calendar.setProperty(PropertyUrl, string(s), props...)
}
func (calendar *Calendar) SetTzid(s string, props ...PropertyParameter) {
calendar.setProperty(PropertyTzid, string(s), props...)
}
func (calendar *Calendar) SetTimezoneId(s string, props ...PropertyParameter) {
calendar.setProperty(PropertyTimezoneId, string(s), props...)
}
func (calendar *Calendar) setProperty(property Property, value string, props ...PropertyParameter) {
for i := range calendar.CalendarProperties {
if calendar.CalendarProperties[i].IANAToken == string(property) {
calendar.CalendarProperties[i].Value = value
calendar.CalendarProperties[i].ICalParameters = map[string][]string{}
for _, p := range props {
k, v := p.KeyValue()
calendar.CalendarProperties[i].ICalParameters[k] = v
}
return
}
}
r := CalendarProperty{
BaseProperty{
IANAToken: string(property),
Value: value,
ICalParameters: map[string][]string{},
},
}
for _, p := range props {
k, v := p.KeyValue()
r.ICalParameters[k] = v
}
calendar.CalendarProperties = append(calendar.CalendarProperties, r)
}
func ParseCalendar(r io.Reader) (*Calendar, error) {
state := "begin"
c := &Calendar{}
cs := NewCalendarStream(r)
cont := true
for ln := 0; cont; ln++ {
l, err := cs.ReadLine()
if err != nil {
switch err {
case io.EOF:
cont = false
default:
return c, err
}
}
if l == nil || len(*l) == 0 {
continue
}
line, err := ParseProperty(*l)
if err != nil {
return nil, fmt.Errorf("parsing line %d: %w", ln, err)
}
if line == nil {
return nil, fmt.Errorf("parsing calendar line %d", ln)
}
switch state {
case "begin":
switch line.IANAToken {
case "BEGIN":
switch line.Value {
case "VCALENDAR":
state = "properties"
default:
return nil, errors.New("malformed calendar; expected a vcalendar")
}
default:
return nil, errors.New("malformed calendar; expected begin")
}
case "properties":
switch line.IANAToken {
case "END":
switch line.Value {
case "VCALENDAR":
state = "end"
default:
return nil, errors.New("malformed calendar; expected end")
}
case "BEGIN":
state = "components"
default: // TODO put in all the supported types for type switching etc.
c.CalendarProperties = append(c.CalendarProperties, CalendarProperty{*line})
}
if state != "components" {
break
}
fallthrough
case "components":
switch line.IANAToken {
case "END":
switch line.Value {
case "VCALENDAR":
state = "end"
default:
return nil, errors.New("malformed calendar; expected end")
}
case "BEGIN":
co, err := GeneralParseComponent(cs, line)
if err != nil {
return nil, err
}
if co != nil {
c.Components = append(c.Components, co)
}
default:
return nil, errors.New("malformed calendar; expected begin or end")
}
case "end":
return nil, errors.New("malformed calendar; unexpected end")
default:
return nil, errors.New("malformed calendar; bad state")
}
}
return c, nil
}
type CalendarStream struct {
r io.Reader
b *bufio.Reader
}
func NewCalendarStream(r io.Reader) *CalendarStream {
return &CalendarStream{
r: r,
b: bufio.NewReader(r),
}
}
func (cs *CalendarStream) ReadLine() (*ContentLine, error) {
r := []byte{}
c := true
var err error
for c {
var b []byte
b, err = cs.b.ReadBytes('\n')
if len(b) == 0 {
if err == nil {
continue
} else {
c = false
}
} else if b[len(b)-1] == '\n' {
o := 1
if len(b) > 1 && b[len(b)-2] == '\r' {
o = 2
}
p, err := cs.b.Peek(1)
r = append(r, b[:len(b)-o]...)
if err == io.EOF {
c = false
}
if len(p) == 0 {
c = false
} else if p[0] == ' ' || p[0] == '\t' {
_, _ = cs.b.Discard(1) // nolint:errcheck
} else {
c = false
}
} else {
r = append(r, b...)
}
switch err {
case nil:
if len(r) == 0 {
c = true
}
case io.EOF:
c = false
default:
return nil, err
}
}
if len(r) == 0 && err != nil {
return nil, err
}
cl := ContentLine(r)
return &cl, err
}