initial caldav support

This commit is contained in:
Casual 2024-06-19 22:27:13 +03:00
parent 69fdf9635d
commit 10cdeaae12
5 changed files with 354 additions and 16 deletions

219
caldav.go Normal file
View File

@ -0,0 +1,219 @@
package main
import (
// "github.com/studio-b12/gowebdav"
"context"
// "fmt"
webdav "github.com/emersion/go-webdav"
"github.com/emersion/go-webdav/caldav"
// "log"
"time"
"strings"
)
type TODO struct {
Name string
Description string
Time string
// Priority int //TODO
// Subtasks []TODO //TODO
// Repeat //TODO
// Alarm //TODO
}
var clientWebDAV *webdav.Client
var client *caldav.Client // clientCalDAV
// var calendarObjects []caldav.CalendarObject
var ctx = context.Background()
// var authSession caldav.Client // clientCalDAV
func (options *Options) InitDAVclients() error {
var err error
authSession := webdav.HTTPClientWithBasicAuth(nil, options.User, options.Password)
clientWebDAV, err = webdav.NewClient(authSession, options.URL)
if err != nil {
// Handle error
return err
}
client, err = caldav.NewClient(authSession, options.URL)
if err != nil {
// Handle error
return err
}
return nil
}
func GetCalendars() ([]caldav.Calendar,error) {
principal, err := clientWebDAV.FindCurrentUserPrincipal(ctx)
if err != nil {
// Handle error
return nil,err
}
// fmt.Println("principal: ",principal) TODO log
// Find the calendar home set
calendarHomeSet, err := client.FindCalendarHomeSet(ctx, principal)
if err != nil {
return nil,err
}
// Find calendars in the calendar home set
calendars, err := client.FindCalendars(ctx, calendarHomeSet)
if err != nil {
return nil,err
}
return calendars, nil
}
func GetTODOs(calendarPath string) (calendarObjects []caldav.CalendarObject,err error) {
date := time.Now()
dateStart:= time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 0, date.Location()).AddDate(0, 0, -1) //TODO too complex - time.Now().Add(-92 * time.Hour),
// dateEnd:= time.Date(date.Year(), date.Month(), date.Day(), 23, 59, 59, 0, date.Location()) //date +1 day
dateEnd:= date.AddDate(0,1,0) //TODO we hard limit pulling events only for this month, to prevent getting tooo much events
calQuery := caldav.CalendarQuery{
CompRequest: caldav.CalendarCompRequest{
Name: "VCALENDAR",
Comps: []caldav.CalendarCompRequest{{
Name: "VTODO",
Props: []string{
"UID",
"SUMMARY",
"COMPLETED",
"DESCRIPTION",
"DUE",
"PRIORITY",
"RELATED-TO",
},
},
},
},
CompFilter: caldav.CompFilter{
Name: "VCALENDAR",
Comps: []caldav.CompFilter{
{
Name: "VTODO",
Start: dateStart,
End: dateEnd,
// FYI: server-side filtering is not supported for Nextcloud
},
},
},
}
calendarObjects, err = client.QueryCalendar(ctx, calendarPath, &calQuery)
if err != nil {
return nil,err
}
return calendarObjects,nil
}
func ParseDueDateTODOs(calObjs []caldav.CalendarObject,date time.Time) ([]TODO,error) {
var output []TODO
for _,calObj := range calObjs {
// fmt.Println((*(*calObj.Data).Children[0]).Name)
// TODO STATUS map[] COMPLETED
for _,event := range (*calObj.Data).Children {
// if (*event).Name == "VTODO" {
var notCompletedTODO, withDate, fromToday bool
//TODO we can optimize there if we encounter wrong state to forcefully stop next analysis
// notCompletedTODO
if (*event).Props["COMPLETED"] == nil {
if ((*event).Props["STATUS"] == nil) { notCompletedTODO = true } else {
if ((*event).Props["STATUS"][0].Value != "COMPLETED") { notCompletedTODO = true }
}
}
// withTodayDate
if (*event).Props["DUE"] != nil {
withDate = true
// fromToday
if strings.HasPrefix((*event).Props["DUE"][0].Value, date.Format("20060102")) { fromToday = true }
}
if notCompletedTODO && withDate && fromToday {
var tmpTODO TODO
if ((*event).Props["SUMMARY"] != nil) {
name := (*event).Props["SUMMARY"][0].Value
tmpTODO.Name = name
} else {
tmpTODO.Name = "<EMPTY>"
}
if ((*event).Props["DESCRIPTION"] != nil) {
description := (*event).Props["DESCRIPTION"][0].Value
tmpTODO.Description = description
}
var todoTime string
due := (*event).Props["DUE"][0].Value
index := strings.Index(due, "T")
if index != -1 {
str := due[index+1:]
todoTime = str[:2] + ":" + str[2:4]
tmpTODO.Time = todoTime
}
output = append(output, tmpTODO)
}
// }
}
}
// //TODO sort: time, priority (if no time)
// // it means, put DUE at first, other DUE;VALUE=DATE in priority order
// //TODO color: priority, add time icon if have time
// //TODO UID:7ed30f40-fce1-422c-be3b-0486dcfe8943
// //TODO RELATED-TO:7ed30f40-fce1-422c-be3b-0486dcfe8943 # subtask
// //TODO PRIORITY:1 #1-high, 5-mid, 9-low
//TODO repeat function???
return output,nil
}
//TODO on complete -repeat function
// RRULE:FREQ=WEEKLY;INTERVAL=1
//TODO if no repeat - mark as complted
// STATUS:COMPLETED
// COMPLETED:20240421T065323Z
// PERCENT-COMPLETE:100
//TODO support notifcations/alarms???
// BEGIN:VTODO
// ...
// BEGIN:VALARM
// TRIGGER;RELATED=END:-PT15M
// ACTION:DISPLAY
// DESCRIPTION:Default Tasks.org description
// END:VALARM
// END:VTODO

34
go.mod Normal file
View File

@ -0,0 +1,34 @@
module git.sual.in/casual/tempus
go 1.22.3
require (
github.com/emersion/go-webdav v0.5.0
github.com/projectdiscovery/goflags v0.1.56
)
require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/aymerick/douceur v0.2.0 // indirect
github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08 // indirect
github.com/emersion/go-ical v0.0.0-20220601085725-0864dccc089f // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/gorilla/css v1.0.0 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/microcosm-cc/bluemonday v1.0.25 // indirect
github.com/miekg/dns v1.1.56 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/projectdiscovery/blackrock v0.0.1 // indirect
github.com/projectdiscovery/utils v0.1.3 // indirect
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d // indirect
github.com/teambition/rrule-go v1.8.2 // indirect
github.com/tidwall/gjson v1.14.3 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db // indirect
golang.org/x/mod v0.12.0 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/tools v0.13.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)

63
go.sum Normal file
View File

@ -0,0 +1,63 @@
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 h1:DklsrG3dyBCFEj5IhUbnKptjxatkF07cF2ak3yi77so=
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuPk=
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08 h1:ox2F0PSMlrAAiAdknSRMDrAr8mfxPCfSZolH+/qQnyQ=
github.com/cnf/structhash v0.0.0-20201127153200-e1b16c1ebc08/go.mod h1:pCxVEbcm3AMg7ejXyorUXi6HQCzOIBf7zEDVPtw0/U4=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emersion/go-ical v0.0.0-20220601085725-0864dccc089f h1:feGUUxxvOtWVOhTko8Cbmp33a+tU0IMZxMEmnkoAISQ=
github.com/emersion/go-ical v0.0.0-20220601085725-0864dccc089f/go.mod h1:2MKFUgfNMULRxqZkadG1Vh44we3y5gJAtTBlVsx1BKQ=
github.com/emersion/go-vcard v0.0.0-20230815062825-8fda7d206ec9/go.mod h1:HMJKR5wlh/ziNp+sHEDV2ltblO4JD2+IdDOWtGcQBTM=
github.com/emersion/go-webdav v0.5.0 h1:Ak/BQLgAihJt/UxJbCsEXDPxS5Uw4nZzgIMOq3rkKjc=
github.com/emersion/go-webdav v0.5.0/go.mod h1:ycyIzTelG5pHln4t+Y32/zBvmrM7+mV7x+V+Gx4ZQno=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4=
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ=
github.com/gorilla/css v1.0.0 h1:BQqNyPTi50JCFMTw/b67hByjMVXZRwGha6wxVGkeihY=
github.com/gorilla/css v1.0.0/go.mod h1:Dn721qIggHpt4+EFCcTLTU/vk5ySda2ReITrtgBl60c=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/microcosm-cc/bluemonday v1.0.25 h1:4NEwSfiJ+Wva0VxN5B8OwMicaJvD8r9tlJWm9rtloEg=
github.com/microcosm-cc/bluemonday v1.0.25/go.mod h1:ZIOjCQp1OrzBBPIJmfX4qDYFuhU02nx4bn030ixfHLE=
github.com/miekg/dns v1.1.56 h1:5imZaSeoRNvpM9SzWNhEcP9QliKiz20/dA2QabIGVnE=
github.com/miekg/dns v1.1.56/go.mod h1:cRm6Oo2C8TY9ZS/TqsSrseAcncm74lfK5G+ikN2SWWY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/projectdiscovery/blackrock v0.0.1 h1:lHQqhaaEFjgf5WkuItbpeCZv2DUIE45k0VbGJyft6LQ=
github.com/projectdiscovery/blackrock v0.0.1/go.mod h1:ANUtjDfaVrqB453bzToU+YB4cUbvBRpLvEwoWIwlTss=
github.com/projectdiscovery/goflags v0.1.56 h1:tJYiZN7s9Jk9DxfYOUiqOoybaIDlXyX4ZgT4B/06SyU=
github.com/projectdiscovery/goflags v0.1.56/go.mod h1:DsGF0NPpM5hGg75N3MTSvWJ4MIT7HFEAOEeWZ074+Fg=
github.com/projectdiscovery/utils v0.1.3 h1:yhHkrbYZA1eOO8e+fPDUvRMS5aUIalyM3Nab7rK4tpg=
github.com/projectdiscovery/utils v0.1.3/go.mod h1:gny8RbNYXE55IoamF6thRDQ8tcJEw+r0FOGAvncz/oQ=
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d h1:hrujxIzL1woJ7AwssoOcM/tq5JjjG2yYOc8odClEiXA=
github.com/saintfish/chardet v0.0.0-20230101081208-5e3ef4b5456d/go.mod h1:uugorj2VCxiV1x+LzaIdVa9b4S4qGAcH6cbhh4qVxOU=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/teambition/rrule-go v1.7.2/go.mod h1:mBJ1Ht5uboJ6jexKdNUJg2NcwP8uUMNvStWXlJD3MvU=
github.com/teambition/rrule-go v1.8.2 h1:lIjpjvWTj9fFUZCmuoVDrKVOtdiyzbzc93qTmRVe/J8=
github.com/teambition/rrule-go v1.8.2/go.mod h1:Ieq5AbrKGciP1V//Wq8ktsTXwSwJHDD5mD/wLBGl3p4=
github.com/tidwall/gjson v1.14.3 h1:9jvXn7olKEHU1S9vwoMGliaT8jq1vJ7IH/n9zD9Dnlw=
github.com/tidwall/gjson v1.14.3/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db h1:D/cFflL63o2KSLJIwjlcIt8PR064j/xsmdEJL/YvY/o=
golang.org/x/exp v0.0.0-20221205204356-47842c84f3db/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/mod v0.12.0 h1:rmsUpXtvNzj340zd98LZ4KntptpfRHwpFOHG188oHXc=
golang.org/x/mod v0.12.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs=
golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg=
golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ=
golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.18.0 h1:DBdB3niSjOA/O0blCZBqDefyWNYveAYMNF1Wum0DYQ4=
golang.org/x/sys v0.18.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/tools v0.13.0 h1:Iey4qkscZuv0VvIt8E0neZjtPVQFSc870HQ448QgEmQ=
golang.org/x/tools v0.13.0/go.mod h1:HvlwmtVNQAhOuCjW7xxvovg8wbNq7LwfXh/k7wXUl58=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

41
main.go
View File

@ -4,32 +4,41 @@ import (
"fmt" "fmt"
"os" "os"
"sync" "sync"
"time"
) )
var waitGroup sync.WaitGroup var waitGroup sync.WaitGroup
func errHandler(err error, message string) {
if err != nil {
fmt.Printf("\n\n%s: %s\n", message, err)
os.Exit(1)
}
}
func main() { func main() {
options, err := ParseOptions() options, err := ParseOptions()
errHandler(err, "Error parsing options") errHandler(err, "Error parsing options")
fmt.Println("hello world", options) options.InitDAVclients()
// start workers in parallel calendars, err := GetCalendars()
for i := 0; i < options.Threads; i++ { errHandler(err, "Error getting calendars")
waitGroup.Add(1)
go func() {
fmt.Println("do parallel stuff")
defer waitGroup.Done()
}()
}
waitGroup.Wait()
for _,calendar := range calendars {
fmt.Println(calendar.Name, "-", calendar.Path)
} }
func errHandler(err error, message string) {
if err != nil { calendarObjects, err := GetTODOs(calendars[1].Path)
fmt.Printf("%s: %s\n", message, err) errHandler(err, "Error getting TODOs")
os.Exit(1)
} today := time.Now()
todos,err := ParseDueDateTODOs(calendarObjects ,today)
fmt.Println(todos)
fmt.Println("In total we have",len(calendarObjects), "todos")
} }

View File

@ -13,6 +13,9 @@ type Options struct {
URL string URL string
Threads int Threads int
// Verbose bool // Verbose bool
User string
Password string
} }
func ParseOptions() (*Options, error) { func ParseOptions() (*Options, error) {
@ -28,6 +31,16 @@ func ParseOptions() (*Options, error) {
flagSet.IntVarP(&options.Threads, "t", "threads", 10, "threads to run"), //TODO add estimate counter to packets/s flagSet.IntVarP(&options.Threads, "t", "threads", 10, "threads to run"), //TODO add estimate counter to packets/s
// flagSet.StringVarP(&options.URL, "u", "url", "", "verbose"), // flagSet.StringVarP(&options.URL, "u", "url", "", "verbose"),
) )
flagSet.CreateGroup("debug", "WebDAV Debug",
flagSet.StringVarP(&options.User, "l", "login", "", "WebDAV login"),
flagSet.StringVarP(&options.Password, "p", "password", "", "WebDAV password (forbid filesystem access!!!)"),
)
// flagSet.CreateGroup("debug", "Debug",
// flagSet.BoolVarP(&options.Verbose, "v", "verbose", false, "verbose output with debugging information"),
// )
_ = flagSet.Parse() _ = flagSet.Parse()
err = options.SanityCheck() err = options.SanityCheck()