2020-05-13 12:07:44 +00:00
|
|
|
package alpscarddav
|
2020-02-05 13:58:56 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2020-02-27 11:17:23 +00:00
|
|
|
"net/url"
|
2020-02-12 20:13:51 +00:00
|
|
|
"path"
|
|
|
|
"strings"
|
2020-02-05 13:58:56 +00:00
|
|
|
|
2020-05-13 12:07:44 +00:00
|
|
|
"git.sr.ht/~emersion/alps"
|
2020-02-05 13:58:56 +00:00
|
|
|
"github.com/emersion/go-vcard"
|
|
|
|
"github.com/emersion/go-webdav/carddav"
|
2020-02-12 20:13:51 +00:00
|
|
|
"github.com/google/uuid"
|
2020-02-27 11:17:23 +00:00
|
|
|
"github.com/labstack/echo/v4"
|
2020-02-05 13:58:56 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type AddressBookRenderData struct {
|
2020-05-13 12:07:44 +00:00
|
|
|
alps.BaseRenderData
|
2020-02-05 13:58:56 +00:00
|
|
|
AddressBook *carddav.AddressBook
|
2020-02-27 11:17:23 +00:00
|
|
|
AddressObjects []AddressObject
|
2020-02-05 13:58:56 +00:00
|
|
|
Query string
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddressObjectRenderData struct {
|
2020-05-13 12:07:44 +00:00
|
|
|
alps.BaseRenderData
|
2020-02-27 11:17:23 +00:00
|
|
|
AddressObject AddressObject
|
2020-02-05 13:58:56 +00:00
|
|
|
}
|
|
|
|
|
2020-02-12 20:13:51 +00:00
|
|
|
type UpdateAddressObjectRenderData struct {
|
2020-05-13 12:07:44 +00:00
|
|
|
alps.BaseRenderData
|
2020-02-12 20:13:51 +00:00
|
|
|
AddressObject *carddav.AddressObject // nil if creating a new contact
|
2020-02-12 20:35:18 +00:00
|
|
|
Card vcard.Card
|
2020-02-12 20:13:51 +00:00
|
|
|
}
|
|
|
|
|
2020-02-27 11:17:23 +00:00
|
|
|
func parseObjectPath(s string) (string, error) {
|
|
|
|
p, err := url.PathUnescape(s)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("failed to parse path: %v", err)
|
|
|
|
return "", echo.NewHTTPError(http.StatusBadRequest, err)
|
|
|
|
}
|
|
|
|
return string(p), nil
|
|
|
|
}
|
|
|
|
|
2020-02-11 18:14:05 +00:00
|
|
|
func registerRoutes(p *plugin) {
|
2020-05-13 12:07:44 +00:00
|
|
|
p.GET("/contacts", func(ctx *alps.Context) error {
|
2020-02-05 13:58:56 +00:00
|
|
|
queryText := ctx.QueryParam("query")
|
|
|
|
|
2020-02-11 18:14:05 +00:00
|
|
|
c, addressBook, err := p.clientWithAddressBook(ctx.Session)
|
2020-02-05 13:58:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
query := carddav.AddressBookQuery{
|
|
|
|
DataRequest: carddav.AddressDataRequest{
|
|
|
|
Props: []string{
|
|
|
|
vcard.FieldFormattedName,
|
|
|
|
vcard.FieldEmail,
|
|
|
|
vcard.FieldUID,
|
|
|
|
},
|
|
|
|
},
|
2020-02-12 16:31:14 +00:00
|
|
|
PropFilters: []carddav.PropFilter{{
|
|
|
|
Name: vcard.FieldFormattedName,
|
|
|
|
}},
|
2020-02-05 13:58:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if queryText != "" {
|
|
|
|
query.PropFilters = []carddav.PropFilter{
|
|
|
|
{
|
|
|
|
Name: vcard.FieldFormattedName,
|
|
|
|
TextMatches: []carddav.TextMatch{{Text: queryText}},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
Name: vcard.FieldEmail,
|
|
|
|
TextMatches: []carddav.TextMatch{{Text: queryText}},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 11:17:23 +00:00
|
|
|
aos, err := c.QueryAddressBook(addressBook.Path, &query)
|
2020-02-05 13:58:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to query CardDAV addresses: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Render(http.StatusOK, "address-book.html", &AddressBookRenderData{
|
2020-05-13 12:07:44 +00:00
|
|
|
BaseRenderData: *alps.NewBaseRenderData(ctx),
|
2020-02-05 13:58:56 +00:00
|
|
|
AddressBook: addressBook,
|
2020-02-27 11:17:23 +00:00
|
|
|
AddressObjects: newAddressObjectList(aos),
|
2020-02-05 13:58:56 +00:00
|
|
|
Query: queryText,
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-05-13 12:07:44 +00:00
|
|
|
p.GET("/contacts/:path", func(ctx *alps.Context) error {
|
2020-02-27 11:17:23 +00:00
|
|
|
path, err := parseObjectPath(ctx.Param("path"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-05 13:58:56 +00:00
|
|
|
|
2020-02-27 11:17:23 +00:00
|
|
|
c, err := p.client(ctx.Session)
|
2020-02-05 13:58:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-27 11:17:23 +00:00
|
|
|
multiGet := carddav.AddressBookMultiGet{
|
2020-02-05 13:58:56 +00:00
|
|
|
DataRequest: carddav.AddressDataRequest{
|
|
|
|
Props: []string{
|
|
|
|
vcard.FieldFormattedName,
|
|
|
|
vcard.FieldEmail,
|
|
|
|
vcard.FieldUID,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2020-02-27 11:17:23 +00:00
|
|
|
aos, err := c.MultiGetAddressBook(path, &multiGet)
|
2020-02-05 13:58:56 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to query CardDAV address: %v", err)
|
|
|
|
}
|
2020-02-27 11:17:23 +00:00
|
|
|
if len(aos) != 1 {
|
|
|
|
return fmt.Errorf("expected exactly one address object with path %q, got %v", path, len(aos))
|
2020-02-05 13:58:56 +00:00
|
|
|
}
|
2020-02-27 11:17:23 +00:00
|
|
|
ao := &aos[0]
|
2020-02-05 13:58:56 +00:00
|
|
|
|
|
|
|
return ctx.Render(http.StatusOK, "address-object.html", &AddressObjectRenderData{
|
2020-05-13 12:07:44 +00:00
|
|
|
BaseRenderData: *alps.NewBaseRenderData(ctx),
|
2020-02-27 11:17:23 +00:00
|
|
|
AddressObject: AddressObject{ao},
|
2020-02-05 13:58:56 +00:00
|
|
|
})
|
|
|
|
})
|
2020-02-12 20:13:51 +00:00
|
|
|
|
2020-05-13 12:07:44 +00:00
|
|
|
updateContact := func(ctx *alps.Context) error {
|
2020-02-27 11:17:23 +00:00
|
|
|
addressObjectPath, err := parseObjectPath(ctx.Param("path"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-02-12 20:35:18 +00:00
|
|
|
|
|
|
|
c, addressBook, err := p.clientWithAddressBook(ctx.Session)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ao *carddav.AddressObject
|
|
|
|
var card vcard.Card
|
2020-02-27 11:17:23 +00:00
|
|
|
if addressObjectPath != "" {
|
|
|
|
ao, err := c.GetAddressObject(addressObjectPath)
|
2020-02-12 20:35:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to query CardDAV address: %v", err)
|
|
|
|
}
|
|
|
|
card = ao.Card
|
|
|
|
} else {
|
|
|
|
card = make(vcard.Card)
|
|
|
|
}
|
2020-02-12 20:13:51 +00:00
|
|
|
|
|
|
|
if ctx.Request().Method == "POST" {
|
|
|
|
fn := ctx.FormValue("fn")
|
|
|
|
emails := strings.Split(ctx.FormValue("emails"), ",")
|
|
|
|
|
|
|
|
if _, ok := card[vcard.FieldVersion]; !ok {
|
2020-02-27 11:56:06 +00:00
|
|
|
// Some CardDAV servers (e.g. Google) don't support vCard 4.0
|
|
|
|
var version = "4.0"
|
|
|
|
if !addressBook.SupportsAddressData(vcard.MIMEType, version) {
|
|
|
|
version = "3.0"
|
|
|
|
}
|
|
|
|
if !addressBook.SupportsAddressData(vcard.MIMEType, version) {
|
|
|
|
return fmt.Errorf("upstream CardDAV server doesn't support vCard %v", version)
|
|
|
|
}
|
|
|
|
card.SetValue(vcard.FieldVersion, version)
|
2020-02-12 20:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if field := card.Preferred(vcard.FieldFormattedName); field != nil {
|
|
|
|
field.Value = fn
|
|
|
|
} else {
|
|
|
|
card.Add(vcard.FieldFormattedName, &vcard.Field{Value: fn})
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: Google wants a "N" field, fails with a 400 otherwise
|
|
|
|
|
|
|
|
// TODO: params are lost here
|
|
|
|
var emailFields []*vcard.Field
|
|
|
|
for _, email := range emails {
|
|
|
|
emailFields = append(emailFields, &vcard.Field{
|
|
|
|
Value: strings.TrimSpace(email),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
card[vcard.FieldEmail] = emailFields
|
|
|
|
|
|
|
|
id := uuid.New()
|
|
|
|
if _, ok := card[vcard.FieldUID]; !ok {
|
|
|
|
card.SetValue(vcard.FieldUID, id.URN())
|
|
|
|
}
|
|
|
|
|
2020-02-12 20:35:18 +00:00
|
|
|
var p string
|
|
|
|
if ao != nil {
|
|
|
|
p = ao.Path
|
|
|
|
} else {
|
|
|
|
p = path.Join(addressBook.Path, id.String()+".vcf")
|
2020-02-12 20:13:51 +00:00
|
|
|
}
|
2020-02-27 11:17:23 +00:00
|
|
|
ao, err = c.PutAddressObject(p, card)
|
2020-02-12 20:13:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to put address object: %v", err)
|
|
|
|
}
|
|
|
|
|
2020-02-27 11:17:23 +00:00
|
|
|
return ctx.Redirect(http.StatusFound, AddressObject{ao}.URL())
|
2020-02-12 20:13:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Render(http.StatusOK, "update-address-object.html", &UpdateAddressObjectRenderData{
|
2020-05-13 12:07:44 +00:00
|
|
|
BaseRenderData: *alps.NewBaseRenderData(ctx),
|
2020-02-12 20:35:18 +00:00
|
|
|
AddressObject: ao,
|
|
|
|
Card: card,
|
2020-02-12 20:13:51 +00:00
|
|
|
})
|
|
|
|
}
|
2020-02-12 20:35:18 +00:00
|
|
|
|
|
|
|
p.GET("/contacts/create", updateContact)
|
|
|
|
p.POST("/contacts/create", updateContact)
|
|
|
|
|
2020-02-27 11:17:23 +00:00
|
|
|
p.GET("/contacts/:path/edit", updateContact)
|
|
|
|
p.POST("/contacts/:path/edit", updateContact)
|
2020-05-13 15:59:04 +00:00
|
|
|
|
|
|
|
p.POST("/contacts/:path/delete", func(ctx *alps.Context) error {
|
|
|
|
path, err := parseObjectPath(ctx.Param("path"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := p.client(ctx.Session)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := c.RemoveAll(path); err != nil {
|
|
|
|
return fmt.Errorf("failed to delete address object: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ctx.Redirect(http.StatusFound, "/contacts")
|
|
|
|
})
|
2020-02-05 13:58:56 +00:00
|
|
|
}
|