package domain import ( "fmt" "sort" "strings" "time" ) // CalendarEvent is one appointment for an iCalendar export. Start is written as // a floating local time so calendar applications show the church's local time // regardless of the device time zone. type CalendarEvent struct { UID string Start time.Time DurationMinutes int Summary string Location string Description string } // CalendarICS builds a published iCalendar document with one VEVENT per // assignment. Timed events make the personal assignments useful in the common // calendar applications. func CalendarICS(calendarName string, events []CalendarEvent) string { sorted := append([]CalendarEvent(nil), events...) sort.SliceStable(sorted, func(i, j int) bool { return sorted[i].Start.Before(sorted[j].Start) }) var builder strings.Builder writeICSLine(&builder, "BEGIN:VCALENDAR") writeICSLine(&builder, "VERSION:2.0") writeICSLine(&builder, "PRODID:-//LektorenPlan//Gottesdienstplan//DE") writeICSLine(&builder, "CALSCALE:GREGORIAN") writeICSLine(&builder, "METHOD:PUBLISH") writeICSLine(&builder, "X-WR-CALNAME:"+escapeICSText(calendarName)) stamp := time.Now().UTC().Format("20060102T150405Z") for _, event := range sorted { duration := event.DurationMinutes if duration <= 0 { duration = 60 } writeICSLine(&builder, "BEGIN:VEVENT") writeICSLine(&builder, "UID:"+escapeICSText(event.UID)+"@lektorenplan") writeICSLine(&builder, "DTSTAMP:"+stamp) writeICSLine(&builder, "DTSTART:"+event.Start.Format("20060102T150405")) writeICSLine(&builder, "DTEND:"+event.Start.Add(time.Duration(duration)*time.Minute).Format("20060102T150405")) writeICSLine(&builder, "SUMMARY:"+escapeICSText(event.Summary)) if strings.TrimSpace(event.Location) != "" { writeICSLine(&builder, "LOCATION:"+escapeICSText(event.Location)) } if strings.TrimSpace(event.Description) != "" { writeICSLine(&builder, "DESCRIPTION:"+escapeICSText(event.Description)) } writeICSLine(&builder, "END:VEVENT") } writeICSLine(&builder, "END:VCALENDAR") return builder.String() } // writeICSLine folds content lines at 75 octets as required by RFC 5545. func writeICSLine(builder *strings.Builder, line string) { const limit = 75 bytesUsed := 0 for index, r := range line { width := len(string(r)) if bytesUsed+width > limit { builder.WriteString("\r\n ") bytesUsed = 1 } if index == 0 { builder.WriteRune(r) bytesUsed += width continue } builder.WriteRune(r) bytesUsed += width } builder.WriteString("\r\n") } // escapeICSText applies the escaping rules for TEXT values in RFC 5545. func escapeICSText(value string) string { value = strings.ReplaceAll(value, "\\", "\\\\") value = strings.ReplaceAll(value, ";", "\\;") value = strings.ReplaceAll(value, ",", "\\,") value = strings.ReplaceAll(value, "\r\n", "\\n") value = strings.ReplaceAll(value, "\n", "\\n") value = strings.ReplaceAll(value, "\r", "\\n") return value } // CalendarFilename is a filesystem-safe attachment name. func CalendarFilename(prefix, start string) string { name := "LektorenPlan" if trimmed := strings.TrimSpace(prefix); trimmed != "" { name += "-" + trimmed } return fmt.Sprintf("%s-Kalender-%s.ics", name, start) }