package caldav import ( "bufio" "strings" "time" ) // VEvent is the minimal projection of one iCalendar VEVENT (RFC 5545): // enough to title and place a bookmark. Parsing is line-oriented with the // RFC's unfolding rule (a CRLF followed by space/tab continues the previous // line), which is all Superbrain needs — full iCalendar semantics live on // the server, and the time-range REPORT already decided which events matter. // Only the FIRST VEVENT is parsed: calendar-query returns one event per // resource. type VEvent struct { UID string Summary string // Start is DTSTART parsed best-effort; all-day dates land at midnight. Start time.Time // AllDay marks a DATE (not DATE-TIME) DTSTART, for display honesty. AllDay bool } // ParseEvent decodes the first VEVENT of an iCalendar payload. Returns a // zero-ish VEvent (no UID and no Summary) when nothing parses, so the caller // can drop the resource rather than fabricate a title. func ParseEvent(raw []byte) VEvent { lines := unfold(string(raw)) var ev VEvent inEvent := false depth := 0 // nested components (VEVENT inside VCALENDAR) for _, line := range lines { lower := strings.ToLower(line.name) switch { case lower == "begin": comp := strings.ToUpper(valueOf(line)) if comp == "VEVENT" { inEvent = true } else if comp != "VCALENDAR" { depth++ // a nested BEGIN other than VCALENDAR/VEVENT } case lower == "end": comp := strings.ToUpper(valueOf(line)) if comp == "VEVENT" { return ev // first event complete; later ones belong to other resources } if comp != "VCALENDAR" { depth-- } case !inEvent: continue case depth > 0: // inside a nested component (VALARM etc.) — ignore its properties continue case lower == "uid": ev.UID = strings.TrimSpace(valueOf(line)) case lower == "summary": ev.Summary = unescapeICS(valueOf(line)) case lower == "dtstart": t, allDay := parseICSTime(line.params, valueOf(line)) ev.Start, ev.AllDay = t, allDay } } return ev } type icsLine struct { name string params string value string } // unfold splits the payload into logical lines applying RFC 5545 §3.1 line // unfolding, tolerating bare LF endings (many servers normalize). func unfold(s string) []icsLine { var folded []string sc := bufio.NewScanner(strings.NewReader(s)) sc.Buffer(make([]byte, 1<<20), 1<<20) for sc.Scan() { line := strings.TrimRight(sc.Text(), "\r") if line == "" { continue } if (line[0] == ' ' || line[0] == '\t') && len(folded) > 0 { folded[len(folded)-1] += line[1:] // strip the single fold space continue } folded = append(folded, line) } out := make([]icsLine, 0, len(folded)) for _, l := range folded { i := strings.IndexByte(l, ':') if i < 0 { continue } head := l[:i] j := strings.IndexByte(head, ';') nl := icsLine{value: l[i+1:]} if j < 0 { nl.name = head } else { nl.name = head[:j] nl.params = head[j+1:] } out = append(out, nl) } return out } func valueOf(l icsLine) string { return l.value } // parseICSTime understands DATE (20260113), DATE-TIME with Z // (20260113T090000Z) and floating/TZID DATE-TIME (20260113T090000 or with // parameters we do not resolve). A TZID parameter means the wall time is in // the named zone; without the zone database here we record it as a naive // UTC instant and let the bookmark's URL (the calendar app) show the truth. func parseICSTime(params, val string) (time.Time, bool) { val = strings.TrimSpace(val) if len(val) == 8 { t, err := time.Parse("20060102", val) return t, err == nil } v := strings.TrimSuffix(val, "Z") t, err := time.Parse("20060102T150405", v) if err != nil { return time.Time{}, false } if strings.HasSuffix(val, "Z") { return t.UTC(), false } // TZID (or floating): keep the wall time, stamped UTC. return t.UTC(), false } // unescapeICS resolves RFC 5545 §3.3.11 TEXT escapes in SUMMARY-like values. func unescapeICS(s string) string { r := strings.NewReplacer(`\\`, `\`, `\,`, `,`, `\;`, `;`, `\n`, "\n", `\N`, "\n") return strings.TrimSpace(r.Replace(s)) }