Compare commits

..
5 Commits
Author SHA1 Message Date
gawells 2e5814e46d get calandars and there names 2026-07-27 22:01:10 -07:00
gawells cdb79f75b5 actually marshal XML data 2026-07-27 21:52:42 -07:00
gawells b263f0b0c1 rearragne some stuff 2026-07-26 17:21:54 -07:00
gawells 5f7aa21daa remove DS Store from git 2026-07-26 17:14:37 -07:00
gawells 29cddc3555 format things a little nicer 2026-07-26 17:14:10 -07:00
4 changed files with 120 additions and 39 deletions
+1
View File
@@ -1 +1,2 @@
config.json
.DS_Store
+25
View File
@@ -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"`
}
+86
View File
@@ -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
View File
@@ -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))
}