Versendung mit .ics Kalenderdatei
This commit is contained in:
1 parent
c08d43e7dd
commit
dc20d7ec5a
13 files changed
+650
-54
No files matched your search
@@ -0,0 +1,101 @@
|
||||
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)
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestCalendarICSWritesTimedEvents(t *testing.T) {
|
||||
start := time.Date(2026, 11, 22, 18, 0, 0, 0, time.Local)
|
||||
ics := CalendarICS("Gottesdienstplan Pfarrei", []CalendarEvent{
|
||||
{
|
||||
UID: "assignment-1", Start: start, DurationMinutes: 60,
|
||||
Summary: "Abendmesse, Hochfest", Location: "St. Andreas",
|
||||
Description: "Dienst: Lektor; Bitte 10 Minuten früher da sein.",
|
||||
},
|
||||
})
|
||||
for _, want := range []string{
|
||||
"BEGIN:VCALENDAR\r\n",
|
||||
"VERSION:2.0\r\n",
|
||||
"BEGIN:VEVENT\r\n",
|
||||
"UID:assignment-1@lektorenplan\r\n",
|
||||
"DTSTART:20261122T180000\r\n",
|
||||
"DTEND:20261122T190000\r\n",
|
||||
"SUMMARY:Abendmesse\\, Hochfest\r\n",
|
||||
"LOCATION:St. Andreas\r\n",
|
||||
"DESCRIPTION:Dienst: Lektor\\; Bitte 10 Minuten früher da sein.\r\n",
|
||||
"END:VCALENDAR\r\n",
|
||||
} {
|
||||
if !strings.Contains(ics, want) {
|
||||
t.Fatalf("calendar misses %q:\n%s", want, ics)
|
||||
}
|
||||
}
|
||||
if strings.Contains(ics, "\n\n") {
|
||||
t.Fatal("calendar must use CRLF line endings")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalendarICSFoldsLongLines(t *testing.T) {
|
||||
start := time.Date(2026, 11, 22, 10, 0, 0, 0, time.Local)
|
||||
long := strings.Repeat("ä", 90)
|
||||
ics := CalendarICS("Plan", []CalendarEvent{{UID: "x", Start: start, Summary: long}})
|
||||
for _, line := range strings.Split(ics, "\r\n") {
|
||||
if len([]byte(line)) > 75 {
|
||||
t.Fatalf("line exceeds 75 octets: %q", line)
|
||||
}
|
||||
}
|
||||
if !strings.Contains(ics, "\r\n ") {
|
||||
t.Fatal("expected folded continuation line")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalendarICSEscapesBackslashesAndNewlines(t *testing.T) {
|
||||
ics := CalendarICS("Plan", []CalendarEvent{{
|
||||
UID: "x", Start: time.Now(), Summary: "A\\B", Description: "Zeile 1\nZeile 2",
|
||||
}})
|
||||
if !strings.Contains(ics, "SUMMARY:A\\\\B\r\n") {
|
||||
t.Fatalf("backslash not escaped:\n%s", ics)
|
||||
}
|
||||
if !strings.Contains(ics, "DESCRIPTION:Zeile 1\\nZeile 2\r\n") {
|
||||
t.Fatalf("newline not escaped:\n%s", ics)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user