Compare commits
5
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
2e5814e46d | ||
|
|
cdb79f75b5 | ||
|
|
b263f0b0c1 | ||
|
|
5f7aa21daa | ||
|
|
29cddc3555 |
@@ -1 +1,2 @@
|
||||
config.json
|
||||
.DS_Store
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
package main
|
||||
|
||||
type MultiStatus struct {
|
||||
Responses []Response `xml:"response"`
|
||||
}
|
||||
|
||||
type Response struct {
|
||||
Href string `xml:"href"`
|
||||
PropStat []PropStat `xml:"propstat"`
|
||||
}
|
||||
|
||||
type PropStat struct {
|
||||
Prop Prop `xml:"prop"`
|
||||
Status string `xml:"status"`
|
||||
}
|
||||
|
||||
type Prop struct {
|
||||
DisplayName string `xml:"displayname"`
|
||||
Type ResourceType `xml:"resourcetype"`
|
||||
}
|
||||
|
||||
type ResourceType struct {
|
||||
Collection *struct{} `xml:"DAV: collection"`
|
||||
Calendar *struct{} `xml:"urn:ietf:params:xml:ns:caldav calendar"`
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"encoding/xml"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// simple test XML
|
||||
const neededXML string = `
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<d:propfind xmlns:d="DAV:">
|
||||
<d:prop>
|
||||
<d:displayname/>
|
||||
<d:resourcetype/>
|
||||
</d:prop>
|
||||
</d:propfind>
|
||||
`
|
||||
|
||||
var client *http.Client
|
||||
|
||||
func init_dav_client() {
|
||||
client = &http.Client{}
|
||||
}
|
||||
|
||||
type Calandar struct {
|
||||
DisplayName string
|
||||
}
|
||||
|
||||
type SimpleCalDavData struct {
|
||||
DisplayName string
|
||||
Calandars []Calandar
|
||||
}
|
||||
|
||||
// this makes stalwart assumptions
|
||||
func get_caldav_data(username string, password string) SimpleCalDavData {
|
||||
req, _ := http.NewRequest(
|
||||
"PROPFIND",
|
||||
serverConfig.URL+"/dav/cal/"+convert_username_into_url(username),
|
||||
strings.NewReader(neededXML),
|
||||
)
|
||||
|
||||
req.SetBasicAuth(username, password)
|
||||
req.Header.Set("Depth", "1")
|
||||
req.Header.Set("Content-Type", "application/xml")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Print(err.Error())
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
var davResp MultiStatus
|
||||
err = xml.Unmarshal(body, &davResp)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Print(string(body))
|
||||
|
||||
calandars := []Calandar{}
|
||||
for i := range len(davResp.Responses) {
|
||||
if davResp.Responses[i].PropStat[0].Prop.Type.Calendar == nil {
|
||||
continue
|
||||
}
|
||||
|
||||
var new_cal Calandar = Calandar{
|
||||
DisplayName: davResp.Responses[i].PropStat[0].Prop.DisplayName,
|
||||
}
|
||||
|
||||
calandars = append(calandars, new_cal)
|
||||
}
|
||||
|
||||
return SimpleCalDavData{
|
||||
DisplayName: davResp.Responses[0].PropStat[0].Prop.DisplayName,
|
||||
Calandars: calandars,
|
||||
}
|
||||
}
|
||||
+8
-39
@@ -1,48 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
)
|
||||
import "fmt"
|
||||
|
||||
func main() {
|
||||
read_config()
|
||||
init_dav_client()
|
||||
calDavData := get_caldav_data(serverConfig.Username, serverConfig.Password)
|
||||
|
||||
client := &http.Client{}
|
||||
fmt.Println()
|
||||
|
||||
xml := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<d:propfind xmlns:d="DAV:">
|
||||
<d:prop>
|
||||
<d:displayname/>
|
||||
<d:resourcetype/>
|
||||
</d:prop>
|
||||
</d:propfind>`
|
||||
|
||||
body := strings.NewReader(xml)
|
||||
|
||||
req, _ := http.NewRequest(
|
||||
"PROPFIND",
|
||||
serverConfig.URL+"/dav/cal/"+convert_username_into_url(serverConfig.Username),
|
||||
body,
|
||||
)
|
||||
|
||||
req.SetBasicAuth(serverConfig.Username, serverConfig.Password)
|
||||
req.Header.Set("Depth", "1")
|
||||
req.Header.Set("Content-Type", "application/xml")
|
||||
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Print(err.Error())
|
||||
fmt.Printf("Users display name: %s\n", calDavData.DisplayName)
|
||||
fmt.Printf("Calandar Count: %d\n", len(calDavData.Calandars))
|
||||
for i := range len(calDavData.Calandars) {
|
||||
fmt.Printf("Cal %d is named %s\n", i, calDavData.Calandars[i].DisplayName)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
body2, err := io.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
fmt.Println(string(body2))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user