plugins/caldav: new plugin

For now it can only list events for the current month.

References: https://todo.sr.ht/~sircmpwn/koushin/60
This commit is contained in:
Simon Ser 2020-02-05 18:08:00 +01:00
parent 1bd930f043
commit 5b78cdc104
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48
8 changed files with 235 additions and 3 deletions

1
.gitignore vendored
View File

@ -3,5 +3,6 @@
!/public/themes/sourcehut
/plugins/*
!/plugins/base
!/plugins/caldav
!/plugins/carddav
!/plugins/lua

View File

@ -13,6 +13,7 @@ import (
"github.com/labstack/gommon/log"
_ "git.sr.ht/~emersion/koushin/plugins/base"
_ "git.sr.ht/~emersion/koushin/plugins/caldav"
_ "git.sr.ht/~emersion/koushin/plugins/carddav"
_ "git.sr.ht/~emersion/koushin/plugins/lua"
)

2
go.mod
View File

@ -13,7 +13,7 @@ require (
github.com/emersion/go-sasl v0.0.0-20191210011802-430746ea8b9b
github.com/emersion/go-smtp v0.12.1
github.com/emersion/go-vcard v0.0.0-20191221110513-5f81fa0d3cc7
github.com/emersion/go-webdav v0.2.0
github.com/emersion/go-webdav v0.2.1-0.20200205170548-baf63fb1b759
github.com/gorilla/css v1.0.0 // indirect
github.com/labstack/echo/v4 v4.1.15-0.20200203180927-504f39abaf32
github.com/labstack/gommon v0.3.0

11
go.sum
View File

@ -6,6 +6,7 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
@ -28,14 +29,16 @@ github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe h1:40SWqY0
github.com/emersion/go-textwrapper v0.0.0-20160606182133-d0e65e56babe/go.mod h1:aqO8z8wPrjkscevZJFVE1wXJrLpC5LtJG7fqLOsPb2U=
github.com/emersion/go-vcard v0.0.0-20191221110513-5f81fa0d3cc7 h1:SE+tcd+0kn0cT4MqTo66gmkjqWHF1Z+Yha5/rhLs/H8=
github.com/emersion/go-vcard v0.0.0-20191221110513-5f81fa0d3cc7/go.mod h1:HMJKR5wlh/ziNp+sHEDV2ltblO4JD2+IdDOWtGcQBTM=
github.com/emersion/go-webdav v0.2.0 h1:w6S3ebKLPa49uqzi5CZgV13Bxb8gPXPcaI6D3bfzcXQ=
github.com/emersion/go-webdav v0.2.0/go.mod h1:5xPS/W9bnqkHMLtgygkgGFvm6uAsbeVRSIZk0ewu4d4=
github.com/emersion/go-webdav v0.2.1-0.20200205170548-baf63fb1b759 h1:8joXUXgdaV935H/AUKhnyvUf754VcjmQAenNsakT4pA=
github.com/emersion/go-webdav v0.2.1-0.20200205170548-baf63fb1b759/go.mod h1:dfzmdPRkPLLUQU00fDfwPJomgFmdblZpMoh3CaUsGCc=
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/labstack/echo/v4 v4.1.15-0.20200203180927-504f39abaf32 h1:UiIEDYxPPmjl6mMIY4QPDguD/QAtK+gR4IRMSrjWmeA=
github.com/labstack/echo/v4 v4.1.15-0.20200203180927-504f39abaf32/go.mod h1:mbsytw7LXzfWxMLdvzjjeERCwTL3PI03GIPpUpaMnfQ=
github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
github.com/luxifer/ical v0.0.0-20191105105432-84b768eb6395 h1:H7Rbug09BDCzr6k/PASDjSMjbJGzXswwqesaDiBNJz8=
github.com/luxifer/ical v0.0.0-20191105105432-84b768eb6395/go.mod h1:TGCe94U3u9fXQl4yHJ8W87z1czj057aVLxYnFYTDC08=
github.com/martinlindhe/base36 v1.0.0 h1:eYsumTah144C0A8P1T/AVSUk5ZoLnhfYFM3OGQxB52A=
github.com/martinlindhe/base36 v1.0.0/go.mod h1:+AtEs8xrBpCeYgSLoY/aJ6Wf37jtBuR0s35750M27+8=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
@ -48,9 +51,11 @@ github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHX
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU=
github.com/microcosm-cc/bluemonday v1.0.2 h1:5lPfLTTAvAbtS0VqT+94yOtFnGfUWYyx0+iToC3Os3s=
github.com/microcosm-cc/bluemonday v1.0.2/go.mod h1:iVP4YcDBq+n/5fb23BhYFvIMq/leAFZyRl6bYmGDlGc=
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/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
@ -83,7 +88,9 @@ golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
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.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
layeh.com/gopher-luar v1.0.7 h1:53iv6CCkRs5wyofZ+qVXcyAYQOIG52s6pt4xkqZdq7k=
layeh.com/gopher-luar v1.0.7/go.mod h1:TPnIVCZ2RJBndm7ohXyaqfhzjlZ+OA2SZR/YwL8tECk=

61
plugins/caldav/caldav.go Normal file
View File

@ -0,0 +1,61 @@
package koushincaldav
import (
"fmt"
"net/http"
"net/url"
"git.sr.ht/~emersion/koushin"
"github.com/emersion/go-webdav/caldav"
)
var errNoCalendar = fmt.Errorf("caldav: no calendar found")
type authRoundTripper struct {
upstream http.RoundTripper
session *koushin.Session
}
func (rt *authRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
rt.session.SetHTTPBasicAuth(req)
return rt.upstream.RoundTrip(req)
}
func newClient(u *url.URL, session *koushin.Session) (*caldav.Client, error) {
rt := authRoundTripper{
upstream: http.DefaultTransport,
session: session,
}
c, err := caldav.NewClient(&http.Client{Transport: &rt}, u.String())
if err != nil {
return nil, fmt.Errorf("failed to create CalDAV client: %v", err)
}
return c, nil
}
func getCalendar(u *url.URL, session *koushin.Session) (*caldav.Client, *caldav.Calendar, error) {
c, err := newClient(u, session)
if err != nil {
return nil, nil, err
}
principal, err := c.FindCurrentUserPrincipal()
if err != nil {
return nil, nil, fmt.Errorf("failed to query CalDAV principal: %v", err)
}
calendarHomeSet, err := c.FindCalendarHomeSet(principal)
if err != nil {
return nil, nil, fmt.Errorf("failed to query CalDAV calendar home set: %v", err)
}
calendars, err := c.FindCalendars(calendarHomeSet)
if err != nil {
return nil, nil, fmt.Errorf("failed to find calendars: %v", err)
}
if len(calendars) == 0 {
return nil, nil, errNoCalendar
}
return c, &calendars[0], nil
}

71
plugins/caldav/plugin.go Normal file
View File

@ -0,0 +1,71 @@
package koushincaldav
import (
"fmt"
"net/http"
"net/url"
"git.sr.ht/~emersion/koushin"
)
func sanityCheckURL(u *url.URL) error {
req, err := http.NewRequest(http.MethodOptions, u.String(), nil)
if err != nil {
return err
}
resp, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
resp.Body.Close()
// Servers might require authentication to perform an OPTIONS request
if resp.StatusCode/100 != 2 && resp.StatusCode != http.StatusUnauthorized {
return fmt.Errorf("HTTP request failed: %v %v", resp.StatusCode, resp.Status)
}
return nil
}
func newPlugin(srv *koushin.Server) (koushin.Plugin, error) {
u, err := srv.Upstream("caldavs", "caldav+insecure", "https", "http+insecure")
if _, ok := err.(*koushin.NoUpstreamError); ok {
return nil, nil
} else if err != nil {
return nil, fmt.Errorf("caldav: failed to parse upstream caldav server: %v", err)
}
switch u.Scheme {
case "caldavs":
u.Scheme = "https"
case "caldav+insecure", "http+insecure":
u.Scheme = "http"
}
if u.Scheme == "" {
return nil, fmt.Errorf("caldav: discovery not yet implemented") // TODO
}
if err := sanityCheckURL(u); err != nil {
return nil, fmt.Errorf("caldav: failed to connect to CalDAV server %q: %v", u, err)
}
srv.Logger().Printf("Configured upstream CalDAV server: %v", u)
p := koushin.GoPlugin{Name: "caldav"}
registerRoutes(&p, u)
return p.Plugin(), nil
}
func init() {
koushin.RegisterPluginLoader(func(s *koushin.Server) ([]koushin.Plugin, error) {
p, err := newPlugin(s)
if err != nil {
return nil, err
}
if p == nil {
return nil, nil
}
return []koushin.Plugin{p}, err
})
}

View File

@ -0,0 +1,25 @@
{{template "head.html"}}
<h1>koushin</h1>
<p>
<a href="/">Back</a>
</p>
<h2>Calendar: {{.Calendar.Name}}</h2>
{{if .Events}}
<ul>
{{range .Events}}
{{$event := index .Data.Events 0}}
<li>
<a href="/calendar/{{$event.UID | pathescape}}">{{$event.Summary}}</a>
({{$event.StartDate | formatdate}} — {{$event.EndDate | formatdate}})
</li>
{{end}}
</ul>
{{else}}
<p>No events.</p>
{{end}}
{{template "foot.html"}}

66
plugins/caldav/routes.go Normal file
View File

@ -0,0 +1,66 @@
package koushincaldav
import (
"fmt"
"net/http"
"net/url"
"time"
"git.sr.ht/~emersion/koushin"
"github.com/emersion/go-webdav/caldav"
)
type CalendarRenderData struct {
koushin.BaseRenderData
Calendar *caldav.Calendar
Events []caldav.CalendarObject
}
func registerRoutes(p *koushin.GoPlugin, u *url.URL) {
p.GET("/calendar", func(ctx *koushin.Context) error {
// TODO: multi-calendar support
c, calendar, err := getCalendar(u, ctx.Session)
if err != nil {
return err
}
now := time.Now()
start := time.Date(now.Year(), now.Month(), 1, 0, 0, 0, 0, now.Location())
end := start.AddDate(0, 1, 0)
query := caldav.CalendarQuery{
CompRequest: caldav.CalendarCompRequest{
Name: "VCALENDAR",
Props: []string{"VERSION"},
Comps: []caldav.CalendarCompRequest{{
Name: "VEVENT",
Props: []string{
"SUMMARY",
"UID",
"DTSTART",
"DTEND",
"DURATION",
},
}},
},
CompFilter: caldav.CompFilter{
Name: "VCALENDAR",
Comps: []caldav.CompFilter{{
Name: "VEVENT",
Start: start,
End: end,
}},
},
}
events, err := c.QueryCalendar(calendar.Path, &query)
if err != nil {
return fmt.Errorf("failed to query calendar: %v", err)
}
return ctx.Render(http.StatusOK, "calendar.html", &CalendarRenderData{
BaseRenderData: *koushin.NewBaseRenderData(ctx),
Calendar: calendar,
Events: events,
})
})
}