A webmail client. Forked from https://git.sr.ht/~migadu/alps
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

62 lines
1.4 KiB

  1. package alpscaldav
  2. import (
  3. "fmt"
  4. "net/http"
  5. "net/url"
  6. "git.sr.ht/~emersion/alps"
  7. "github.com/emersion/go-webdav/caldav"
  8. )
  9. var errNoCalendar = fmt.Errorf("caldav: no calendar found")
  10. type authRoundTripper struct {
  11. upstream http.RoundTripper
  12. session *alps.Session
  13. }
  14. func (rt *authRoundTripper) RoundTrip(req *http.Request) (*http.Response, error) {
  15. rt.session.SetHTTPBasicAuth(req)
  16. return rt.upstream.RoundTrip(req)
  17. }
  18. func newClient(u *url.URL, session *alps.Session) (*caldav.Client, error) {
  19. rt := authRoundTripper{
  20. upstream: http.DefaultTransport,
  21. session: session,
  22. }
  23. c, err := caldav.NewClient(&http.Client{Transport: &rt}, u.String())
  24. if err != nil {
  25. return nil, fmt.Errorf("failed to create CalDAV client: %v", err)
  26. }
  27. return c, nil
  28. }
  29. func getCalendar(u *url.URL, session *alps.Session) (*caldav.Client, *caldav.Calendar, error) {
  30. c, err := newClient(u, session)
  31. if err != nil {
  32. return nil, nil, err
  33. }
  34. principal, err := c.FindCurrentUserPrincipal()
  35. if err != nil {
  36. return nil, nil, fmt.Errorf("failed to query CalDAV principal: %v", err)
  37. }
  38. calendarHomeSet, err := c.FindCalendarHomeSet(principal)
  39. if err != nil {
  40. return nil, nil, fmt.Errorf("failed to query CalDAV calendar home set: %v", err)
  41. }
  42. calendars, err := c.FindCalendars(calendarHomeSet)
  43. if err != nil {
  44. return nil, nil, fmt.Errorf("failed to find calendars: %v", err)
  45. }
  46. if len(calendars) == 0 {
  47. return nil, nil, errNoCalendar
  48. }
  49. return c, &calendars[0], nil
  50. }