2020-02-07 17:27:45 +01:00
|
|
|
// Vikunja is a to-do list application to facilitate your life.
|
2021-02-02 20:19:13 +01:00
|
|
|
// Copyright 2018-2021 Vikunja and contributors. All rights reserved.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is free software: you can redistribute it and/or modify
|
2020-12-23 16:41:52 +01:00
|
|
|
// it under the terms of the GNU Affero General Public Licensee as published by
|
2019-12-04 20:39:56 +01:00
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2019-12-04 20:39:56 +01:00
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2020-12-23 16:41:52 +01:00
|
|
|
// GNU Affero General Public Licensee for more details.
|
2018-11-26 21:17:33 +01:00
|
|
|
//
|
2020-12-23 16:41:52 +01:00
|
|
|
// You should have received a copy of the GNU Affero General Public Licensee
|
2019-12-04 20:39:56 +01:00
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
2018-11-26 21:17:33 +01:00
|
|
|
|
2018-11-03 16:05:45 +01:00
|
|
|
package caldav
|
|
|
|
|
|
|
|
import (
|
2020-10-21 23:03:19 +02:00
|
|
|
"regexp"
|
2018-11-03 16:05:45 +01:00
|
|
|
"strconv"
|
2020-06-27 19:04:01 +02:00
|
|
|
"strings"
|
2018-11-03 16:05:45 +01:00
|
|
|
"time"
|
2020-10-11 22:10:03 +02:00
|
|
|
|
|
|
|
"code.vikunja.io/api/pkg/config"
|
|
|
|
"code.vikunja.io/api/pkg/user"
|
|
|
|
"code.vikunja.io/api/pkg/utils"
|
2018-11-03 16:05:45 +01:00
|
|
|
)
|
|
|
|
|
2020-10-19 11:11:15 +02:00
|
|
|
// DateFormat is the caldav date format
|
2019-05-22 19:48:48 +02:00
|
|
|
const DateFormat = `20060102T150405`
|
|
|
|
|
2018-11-03 16:05:45 +01:00
|
|
|
// Event holds a single caldav event
|
|
|
|
type Event struct {
|
|
|
|
Summary string
|
|
|
|
Description string
|
|
|
|
UID string
|
|
|
|
Alarms []Alarm
|
2020-12-18 13:04:27 +01:00
|
|
|
Color string
|
2018-11-03 16:05:45 +01:00
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
Timestamp time.Time
|
|
|
|
Start time.Time
|
|
|
|
End time.Time
|
2018-11-03 16:05:45 +01:00
|
|
|
}
|
|
|
|
|
2019-05-22 19:48:48 +02:00
|
|
|
// Todo holds a single VTODO
|
|
|
|
type Todo struct {
|
|
|
|
// Required
|
2020-06-27 19:04:01 +02:00
|
|
|
Timestamp time.Time
|
2020-02-08 13:48:49 +01:00
|
|
|
UID string
|
2019-05-22 19:48:48 +02:00
|
|
|
|
|
|
|
// Optional
|
2020-02-08 13:48:49 +01:00
|
|
|
Summary string
|
|
|
|
Description string
|
2020-06-27 19:04:01 +02:00
|
|
|
Completed time.Time
|
2020-02-08 13:48:49 +01:00
|
|
|
Organizer *user.User
|
|
|
|
Priority int64 // 0-9, 1 is highest
|
|
|
|
RelatedToUID string
|
2020-12-18 13:04:27 +01:00
|
|
|
Color string
|
2020-02-08 13:48:49 +01:00
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
Start time.Time
|
|
|
|
End time.Time
|
|
|
|
DueDate time.Time
|
2020-02-08 13:48:49 +01:00
|
|
|
Duration time.Duration
|
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
Created time.Time
|
|
|
|
Updated time.Time // last-mod
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
|
2018-11-03 16:05:45 +01:00
|
|
|
// Alarm holds infos about an alarm from a caldav event
|
|
|
|
type Alarm struct {
|
2020-06-27 19:04:01 +02:00
|
|
|
Time time.Time
|
2018-11-03 16:05:45 +01:00
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config is the caldav calendar config
|
|
|
|
type Config struct {
|
|
|
|
Name string
|
|
|
|
ProdID string
|
2020-12-18 13:04:27 +01:00
|
|
|
Color string
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCaldavColor(color string) (caldavcolor string) {
|
|
|
|
if color == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.HasPrefix(color, "#") {
|
|
|
|
color = "#" + color
|
|
|
|
}
|
|
|
|
|
|
|
|
color += "FF"
|
|
|
|
|
|
|
|
return `
|
|
|
|
X-APPLE-CALENDAR-COLOR:` + color + `
|
|
|
|
X-OUTLOOK-COLOR:` + color + `
|
|
|
|
X-FUNAMBOL-COLOR:` + color
|
2018-11-03 16:05:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// ParseEvents parses an array of caldav events and gives them back as string
|
|
|
|
func ParseEvents(config *Config, events []*Event) (caldavevents string) {
|
|
|
|
caldavevents += `BEGIN:VCALENDAR
|
|
|
|
VERSION:2.0
|
|
|
|
METHOD:PUBLISH
|
|
|
|
X-PUBLISHED-TTL:PT4H
|
|
|
|
X-WR-CALNAME:` + config.Name + `
|
2020-12-18 13:04:27 +01:00
|
|
|
PRODID:-//` + config.ProdID + `//EN` + getCaldavColor(config.Color)
|
2018-11-03 16:05:45 +01:00
|
|
|
|
|
|
|
for _, e := range events {
|
|
|
|
|
|
|
|
if e.UID == "" {
|
2020-02-08 13:48:49 +01:00
|
|
|
e.UID = makeCalDavTimeFromTimeStamp(e.Timestamp) + utils.Sha256(e.Summary)
|
2018-11-03 16:05:45 +01:00
|
|
|
}
|
|
|
|
|
2020-10-21 23:03:19 +02:00
|
|
|
formattedDescription := ""
|
|
|
|
if e.Description != "" {
|
|
|
|
re := regexp.MustCompile(`\r?\n`)
|
|
|
|
formattedDescription = re.ReplaceAllString(e.Description, "\\n")
|
|
|
|
}
|
|
|
|
|
2018-11-03 16:05:45 +01:00
|
|
|
caldavevents += `
|
|
|
|
BEGIN:VEVENT
|
|
|
|
UID:` + e.UID + `
|
2020-12-18 13:04:27 +01:00
|
|
|
SUMMARY:` + e.Summary + getCaldavColor(e.Color) + `
|
2020-10-21 23:03:19 +02:00
|
|
|
DESCRIPTION:` + formattedDescription + `
|
2020-02-08 13:48:49 +01:00
|
|
|
DTSTAMP:` + makeCalDavTimeFromTimeStamp(e.Timestamp) + `
|
|
|
|
DTSTART:` + makeCalDavTimeFromTimeStamp(e.Start) + `
|
|
|
|
DTEND:` + makeCalDavTimeFromTimeStamp(e.End)
|
2018-11-03 16:05:45 +01:00
|
|
|
|
|
|
|
for _, a := range e.Alarms {
|
|
|
|
if a.Description == "" {
|
|
|
|
a.Description = e.Summary
|
|
|
|
}
|
|
|
|
|
|
|
|
caldavevents += `
|
|
|
|
BEGIN:VALARM
|
2020-02-08 13:48:49 +01:00
|
|
|
TRIGGER:` + calcAlarmDateFromReminder(e.Start, a.Time) + `
|
2018-11-03 16:05:45 +01:00
|
|
|
ACTION:DISPLAY
|
|
|
|
DESCRIPTION:` + a.Description + `
|
|
|
|
END:VALARM`
|
|
|
|
}
|
|
|
|
caldavevents += `
|
|
|
|
END:VEVENT`
|
|
|
|
}
|
|
|
|
|
|
|
|
caldavevents += `
|
|
|
|
END:VCALENDAR` // Need a line break
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-07 23:20:37 +02:00
|
|
|
func formatDuration(duration time.Duration) string {
|
|
|
|
seconds := duration.Seconds() - duration.Minutes()*60
|
|
|
|
minutes := duration.Minutes() - duration.Hours()*60
|
|
|
|
|
|
|
|
return strconv.FormatFloat(duration.Hours(), 'f', 0, 64) + `H` +
|
|
|
|
strconv.FormatFloat(minutes, 'f', 0, 64) + `M` +
|
|
|
|
strconv.FormatFloat(seconds, 'f', 0, 64) + `S`
|
|
|
|
}
|
|
|
|
|
2019-05-22 19:48:48 +02:00
|
|
|
// ParseTodos returns a caldav vcalendar string with todos
|
|
|
|
func ParseTodos(config *Config, todos []*Todo) (caldavtodos string) {
|
|
|
|
caldavtodos = `BEGIN:VCALENDAR
|
|
|
|
VERSION:2.0
|
|
|
|
METHOD:PUBLISH
|
|
|
|
X-PUBLISHED-TTL:PT4H
|
|
|
|
X-WR-CALNAME:` + config.Name + `
|
2020-12-18 13:04:27 +01:00
|
|
|
PRODID:-//` + config.ProdID + `//EN` + getCaldavColor(config.Color)
|
2019-05-22 19:48:48 +02:00
|
|
|
|
|
|
|
for _, t := range todos {
|
|
|
|
if t.UID == "" {
|
2020-02-08 13:48:49 +01:00
|
|
|
t.UID = makeCalDavTimeFromTimeStamp(t.Timestamp) + utils.Sha256(t.Summary)
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
caldavtodos += `
|
|
|
|
BEGIN:VTODO
|
|
|
|
UID:` + t.UID + `
|
2020-02-08 13:48:49 +01:00
|
|
|
DTSTAMP:` + makeCalDavTimeFromTimeStamp(t.Timestamp) + `
|
2020-12-18 13:04:27 +01:00
|
|
|
SUMMARY:` + t.Summary + getCaldavColor(t.Color)
|
2019-05-22 19:48:48 +02:00
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
if t.Start.Unix() > 0 {
|
2019-05-22 19:48:48 +02:00
|
|
|
caldavtodos += `
|
2022-07-07 23:20:37 +02:00
|
|
|
DTSTART:` + makeCalDavTimeFromTimeStamp(t.Start)
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
2020-06-27 19:04:01 +02:00
|
|
|
if t.End.Unix() > 0 {
|
2019-05-22 19:48:48 +02:00
|
|
|
caldavtodos += `
|
2022-07-07 23:20:37 +02:00
|
|
|
DTEND:` + makeCalDavTimeFromTimeStamp(t.End)
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
if t.Description != "" {
|
2020-10-21 23:03:19 +02:00
|
|
|
re := regexp.MustCompile(`\r?\n`)
|
|
|
|
formattedDescription := re.ReplaceAllString(t.Description, "\\n")
|
2019-05-22 19:48:48 +02:00
|
|
|
caldavtodos += `
|
2020-10-21 23:03:19 +02:00
|
|
|
DESCRIPTION:` + formattedDescription
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
2020-06-27 19:04:01 +02:00
|
|
|
if t.Completed.Unix() > 0 {
|
2019-05-22 19:48:48 +02:00
|
|
|
caldavtodos += `
|
2020-10-21 23:03:19 +02:00
|
|
|
COMPLETED:` + makeCalDavTimeFromTimeStamp(t.Completed) + `
|
|
|
|
STATUS:COMPLETED`
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
if t.Organizer != nil {
|
|
|
|
caldavtodos += `
|
|
|
|
ORGANIZER;CN=:` + t.Organizer.Username
|
|
|
|
}
|
|
|
|
|
|
|
|
if t.RelatedToUID != "" {
|
|
|
|
caldavtodos += `
|
|
|
|
RELATED-TO:` + t.RelatedToUID
|
|
|
|
}
|
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
if t.DueDate.Unix() > 0 {
|
2019-05-22 19:48:48 +02:00
|
|
|
caldavtodos += `
|
2020-02-08 13:48:49 +01:00
|
|
|
DUE:` + makeCalDavTimeFromTimeStamp(t.DueDate)
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
if t.Created.Unix() > 0 {
|
2019-05-22 19:48:48 +02:00
|
|
|
caldavtodos += `
|
2020-02-08 13:48:49 +01:00
|
|
|
CREATED:` + makeCalDavTimeFromTimeStamp(t.Created)
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if t.Duration != 0 {
|
|
|
|
caldavtodos += `
|
2022-07-07 23:20:37 +02:00
|
|
|
DURATION:PT` + formatDuration(t.Duration)
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if t.Priority != 0 {
|
|
|
|
caldavtodos += `
|
2021-07-11 15:03:50 +02:00
|
|
|
PRIORITY:` + strconv.Itoa(mapPriorityToCaldav(t.Priority))
|
2019-05-22 19:48:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
caldavtodos += `
|
2020-02-08 13:48:49 +01:00
|
|
|
LAST-MODIFIED:` + makeCalDavTimeFromTimeStamp(t.Updated)
|
2019-05-22 19:48:48 +02:00
|
|
|
|
|
|
|
caldavtodos += `
|
|
|
|
END:VTODO`
|
|
|
|
}
|
|
|
|
|
|
|
|
caldavtodos += `
|
|
|
|
END:VCALENDAR` // Need a line break
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
func makeCalDavTimeFromTimeStamp(ts time.Time) (caldavtime string) {
|
|
|
|
return ts.In(config.GetTimeZone()).Format(DateFormat)
|
2018-11-03 16:05:45 +01:00
|
|
|
}
|
|
|
|
|
2020-06-27 19:04:01 +02:00
|
|
|
func calcAlarmDateFromReminder(eventStart, reminder time.Time) (alarmTime string) {
|
|
|
|
diff := reminder.Sub(eventStart)
|
|
|
|
diffStr := strings.ToUpper(diff.String())
|
|
|
|
if diff < 0 {
|
2018-11-03 16:05:45 +01:00
|
|
|
alarmTime += `-`
|
2020-06-27 19:04:01 +02:00
|
|
|
// We append the - at the beginning of the caldav flag, that would get in the way if the minutes
|
|
|
|
// themselves are also containing it
|
|
|
|
diffStr = diffStr[1:]
|
2018-11-03 16:05:45 +01:00
|
|
|
}
|
2020-06-27 19:04:01 +02:00
|
|
|
alarmTime += `PT` + diffStr
|
2018-11-03 16:05:45 +01:00
|
|
|
return
|
|
|
|
}
|