2017-09-03 21:11:01 +03:00
|
|
|
package carddav
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/emersion/hydroxide/protonmail"
|
|
|
|
"github.com/emersion/go-vcard"
|
|
|
|
"github.com/emersion/go-webdav/carddav"
|
|
|
|
)
|
|
|
|
|
|
|
|
type addressObject struct {
|
|
|
|
c *protonmail.Client
|
|
|
|
contact *protonmail.ContactExport
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ao *addressObject) ID() string {
|
|
|
|
return ao.contact.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ao *addressObject) Card() (vcard.Card, error) {
|
|
|
|
card := make(vcard.Card)
|
|
|
|
|
|
|
|
for _, c := range ao.contact.Cards {
|
|
|
|
if c.Type.Encrypted() {
|
|
|
|
// TODO: decrypt
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if c.Type.Signed() {
|
|
|
|
// TODO: check signature
|
|
|
|
}
|
|
|
|
|
|
|
|
decoded, err := vcard.NewDecoder(strings.NewReader(c.Data)).Decode()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, fields := range decoded {
|
|
|
|
for _, f := range fields {
|
|
|
|
card.Add(k, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return card, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type addressBook struct {
|
2017-09-04 12:41:26 +03:00
|
|
|
c *protonmail.Client
|
|
|
|
cache map[string]carddav.AddressObject
|
|
|
|
total int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ab *addressBook) cacheComplete() bool {
|
|
|
|
return ab.total >= 0 && len(ab.cache) == ab.total
|
2017-09-03 21:11:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ab *addressBook) ListAddressObjects() ([]carddav.AddressObject, error) {
|
2017-09-04 12:41:26 +03:00
|
|
|
if ab.cacheComplete() {
|
|
|
|
aos := make([]carddav.AddressObject, 0, len(ab.cache))
|
|
|
|
for _, ao := range ab.cache {
|
|
|
|
aos = append(aos, ao)
|
|
|
|
}
|
|
|
|
return aos, nil
|
|
|
|
}
|
|
|
|
|
2017-09-03 21:11:01 +03:00
|
|
|
// TODO: paging support
|
2017-09-04 12:41:26 +03:00
|
|
|
total, contacts, err := ab.c.ListContactsExport(0, 0)
|
2017-09-03 21:11:01 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:41:26 +03:00
|
|
|
ab.total = total
|
|
|
|
|
2017-09-03 21:11:01 +03:00
|
|
|
aos := make([]carddav.AddressObject, len(contacts))
|
|
|
|
for i, contact := range contacts {
|
2017-09-04 12:41:26 +03:00
|
|
|
ao := &addressObject{c: ab.c, contact: contact}
|
|
|
|
ab.cache[contact.ID] = ao
|
|
|
|
aos[i] = ao
|
2017-09-03 21:11:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return aos, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ab *addressBook) GetAddressObject(id string) (carddav.AddressObject, error) {
|
2017-09-04 12:41:26 +03:00
|
|
|
if ao, ok := ab.cache[id]; ok {
|
|
|
|
return ao, nil
|
|
|
|
} else if ab.cacheComplete() {
|
|
|
|
return nil, carddav.ErrNotFound
|
|
|
|
}
|
|
|
|
|
2017-09-03 21:11:01 +03:00
|
|
|
contact, err := ab.c.GetContact(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-09-04 12:41:26 +03:00
|
|
|
ao := &addressObject{
|
2017-09-03 21:11:01 +03:00
|
|
|
c: ab.c,
|
|
|
|
contact: &protonmail.ContactExport{
|
|
|
|
ID: contact.ID,
|
|
|
|
Cards: contact.Cards,
|
|
|
|
},
|
2017-09-04 12:41:26 +03:00
|
|
|
}
|
|
|
|
ab.cache[id] = ao
|
|
|
|
return ao, nil
|
2017-09-03 21:11:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewHandler(c *protonmail.Client) http.Handler {
|
2017-09-04 12:41:26 +03:00
|
|
|
return carddav.NewHandler(&addressBook{
|
|
|
|
c: c,
|
|
|
|
cache: make(map[string]carddav.AddressObject),
|
|
|
|
total: -1,
|
|
|
|
})
|
2017-09-03 21:11:01 +03:00
|
|
|
}
|