~psic4t/qcal

ref: 0d13a8bdd8cfb56a00a419502dbeb92c258a92d0 qcal/main.go -rw-r--r-- 9.2 KiB
0d13a8bdpsic4t fix deletion behaviour 7 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package main

import (
	// 	"bytes"
	"encoding/xml"
	"flag"
	"fmt"
	"io/ioutil"
	"log"
	"net/http"
	"sort"
	"strconv"
	"strings"
	"sync"
	"time"
)

var config = getConf()

func fetchCalData(calNo int, wg *sync.WaitGroup) {
	xmlBody := `<c:calendar-query xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav">
			<d:prop><c:calendar-data /><d:getetag /></d:prop>
			<c:filter><c:comp-filter name="VCALENDAR"><c:comp-filter name="VEVENT">
				<c:time-range start="` + startDate + `Z" end="` + endDate + `Z" />
			 </c:comp-filter></c:comp-filter></c:filter>
		    </c:calendar-query>`

	//fmt.Println(xmlBody)
	req, err := http.NewRequest("REPORT", config.Calendars[calNo].Url, strings.NewReader(xmlBody))
	req.SetBasicAuth(config.Calendars[calNo].Username, config.Calendars[calNo].Password)
	req.Header.Add("Content-Type", "application/xml; charset=utf-8")
	req.Header.Add("Depth", "1") // needed for SabreDAV
	req.Header.Add("Prefer", "return-minimal")

	/*tr := &http.Transport{
		TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
	}
	cli := &http.Client{Transport: tr}*/

	cli := &http.Client{}
	resp, err := cli.Do(req)
	if err != nil {
		log.Fatal(err)
	}

	xmlContent, _ := ioutil.ReadAll(resp.Body)
	defer resp.Body.Close()
	//fmt.Println(string(xmlContent))

	cald := Caldata{}
	err = xml.Unmarshal(xmlContent, &cald)
	if err != nil { // if no XML
		//log.Fatal(err)
		//fmt.Println("noxml")
		eventData := splitIcal(string(xmlContent))
		//fmt.Println(eventData)
		//log.Fatal(err)
		for i := range eventData {
			eventHref := ""
			eventColor := Colors[calNo]
			parseMain(&eventData[i], &elements, eventHref, eventColor)
		}
	} else {
		for i := range cald.Caldata {
			eventData := cald.Caldata[i].Data
			eventHref := cald.Caldata[i].Href
			eventColor := Colors[calNo]
			//fmt.Println(eventData)
			//fmt.Println(i)

			eventData, _ = explodeEvent(&eventData) // vevent only
			parseMain(&eventData, &elements, eventHref, eventColor)
		}
	}

	wg.Done()
}

func showAppointments(singleCal string) {
	var wg sync.WaitGroup         // use waitgroups to fetch calendars in parallel
	wg.Add(len(config.Calendars)) // waitgroup length = num calendars
	for i := range config.Calendars {
		if singleCal == fmt.Sprintf("%v", i) || singleCal == "all" { // sprintf because convert int to string
			go fetchCalData(i, &wg)
		} else {
			wg.Done()
		}
	}
	wg.Wait()

	sort.Slice(elements, func(i, j int) bool {
		return elements[i].Start.Before(elements[j].Start) // time.Time sort by start time for events
	})

	if len(elements) == 0 {
		log.Fatal("no events") // get out if nothing found
	}

	for _, e := range elements {
		e.fancyOutput() // pretty print
	}
}

func createAppointment(calNumber string, appointmentData string, recurrence string) {
	curTime := time.Now()
	dataArr := strings.Split(appointmentData, " ")
	var startDate string
	var endDate string
	var startTime string
	var endTime string
	var summaryIdx int
	var isDayEvent bool
	var calRec string
	var dtStartString string
	var dtEndString string

	calNo, _ := strconv.ParseInt(calNumber, 0, 64)
	// no username, no write.
	if config.Calendars[calNo].Username == "" {
		log.Fatal("You can't write to iCal calendars")
	}

	// first block - start (and possible end-) date
	if (isNumeric(dataArr[0])) && (len(dataArr[0]) == 8) {
		startDate = dataArr[0]
	} else {
		log.Fatal("Wrong date/time syntax. Please check help.")
	}
	// second block - start time or end date
	if isNumeric(dataArr[1]) {
		// if is start time
		if len(dataArr[1]) == 4 {
			endDate = dataArr[0]
			startTime = dataArr[1]
			summaryIdx = 3
			// if second block is start time, third block needs to be end time
			if len(dataArr[2]) == 4 {
				endTime = dataArr[2]
			} else {
				log.Fatal("Wrong date/time syntax. Please check help.")
			}
			// if is end date
		} else if len(dataArr[1]) == 8 {
			endDate = dataArr[1]
			isDayEvent = true
			summaryIdx = 2
		}
	} else {
		// if only start date is given treat as one whole day appointment
		endDateObject, _ := time.Parse(IcsFormatWholeDay, startDate)
		endDate = endDateObject.AddDate(0, 0, 1).Format(IcsFormatWholeDay)
		isDayEvent = true
		summaryIdx = 1
	}
	//fmt.Println(summaryIdx)

	summary := dataArr[summaryIdx]
	for i := range dataArr {
		if i > summaryIdx {
			summary = summary + ` ` + dataArr[i]
		}
	}
	newElem := genUUID() + `.ics`
	//tzName, _ := time.Now().Zone()
	//fmt.Printf("name: [%v]\toffset: [%v]\n", tzName, tzOffset)
	tzName, e := time.LoadLocation(config.Timezone)
	checkError(e)

	timezoneString := fmt.Sprintf("%v", tzName)

	if isDayEvent {
		dtStartString = fmt.Sprintf("VALUE=DATE:%v", startDate)
		dtEndString = fmt.Sprintf("VALUE=DATE:%v", endDate)
	} else {
		dtStartString = fmt.Sprintf("TZID=%v:%vT%v00", tzName, startDate, startTime)
		dtEndString = fmt.Sprintf("TZID=%v:%vT%v00", tzName, endDate, endTime)
	}
	//check frequency
	switch recurrence {
	case "d":
		calRec = "\nRRULE:FREQ=DAILY"
	case "w":
		calRec = "\nRRULE:FREQ=WEEKLY"
	case "m":
		calRec = "\nRRULE:FREQ=MONTHLY"
	case "y":
		calRec = "\nRRULE:FREQ=YEARLY"
	default:
		calRec = ""
	}

	var calSkel = `BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//qcal
METHOD:PUBLISH
BEGIN:VTIMEZONE
TZID:` + timezoneString + `
BEGIN:STANDARD
DTSTART:16011028T030000
RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=10
TZOFFSETFROM:+0200
TZOFFSETTO:+0100
END:STANDARD
BEGIN:DAYLIGHT
DTSTART:16010325T020000
RRULE:FREQ=YEARLY;BYDAY=-1SU;BYMONTH=3
TZOFFSETFROM:+0100
TZOFFSETTO:+0200
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
UID:` + curTime.UTC().Format(IcsFormat) + `-` + newElem + `
DTSTART;` + dtStartString + ` 
DTEND;` + dtEndString + `
DTSTAMP:` + curTime.UTC().Format(IcsFormat) + `Z
SUMMARY:` + summary + calRec + `
END:VEVENT
END:VCALENDAR`
	//fmt.Println(calSkel)
	//os.Exit(3)

	req, _ := http.NewRequest("PUT", config.Calendars[calNo].Url+newElem, strings.NewReader(calSkel))
	req.SetBasicAuth(config.Calendars[calNo].Username, config.Calendars[calNo].Password)
	req.Header.Add("Content-Type", "text/calendar; charset=utf-8")

	cli := &http.Client{}
	resp, err := cli.Do(req)
	defer resp.Body.Close()
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(resp.Status)

}

func main() {
	curTime := time.Now()
	//curTimeDay := curTime.Truncate(24 * time.Hour).Add(-1) // NO! UTC only
	// remove time, substract 1 msec for whole day appointments
	curTimeDay := time.Date(curTime.Year(), curTime.Month(), curTime.Day(), 0, 0, 0, 0, time.Local).Add(-1)
	//fmt.Println(curTimeDay)
	toFile := false

	flag.StringVar(&startDate, "s", curTimeDay.UTC().Format(IcsFormat), "Start date")                                  // default today
	flag.StringVar(&endDate, "e", curTimeDay.UTC().AddDate(0, 0, config.DefaultNumDays).Format(IcsFormat), "End date") // default 1 month
	flag.BoolVar(&showInfo, "i", false, "Show additional info like description and location for appointments")
	flag.BoolVar(&showFilename, "f", false, "Show appointment filename for editing or deletion")
	flag.BoolVar(&displayFlag, "p", false, "Print ICS file piped to qcal (for CLI mail tools like mutt)")
	calNumber := flag.String("c", "all", "Show only single calendar (number)")
	showToday := flag.Bool("t", false, "Show appointments for today")
	show7days := flag.Bool("7", false, "Show 7 days from now")
	version := flag.Bool("v", false, "Show version")
	showMinutes := flag.Int("cron", 15, "Crontab mode. Show only appointments in the next n minutes.")
	recurrence := flag.String("r", "", "Recurrency for new appointments. Use d,w,m,y with \"-n\"")
	showCalendars := flag.Bool("l", false, "List configured calendars with their corresponding numbers (for \"-c\")")
	appointmentFile := flag.String("u", "", "Upload appointment file. Provide filename and use with \"-c\"")
	appointmentDelete := flag.String("delete", "", "Delete appointment. Get filename with \"-f\" and use with \"-c\"")
	appointmentDump := flag.String("d", "", "Dump raw appointment data. Get filename with \"-f\" and use with \"-c\"")
	appointmentEdit := flag.String("edit", "", "Edit + upload appointment data. Get filename with \"-f\" and use with \"-c\"")
	appointmentData := flag.String("n", "", "Add a new appointment. Check README.md for syntax")
	flag.Parse()
	flagset := make(map[string]bool) // map for flag.Visit. get bools to determine set flags
	flag.Visit(func(f *flag.Flag) { flagset[f.Name] = true })

	if *showToday {
		endDate = curTimeDay.UTC().AddDate(0, 0, 1).Format(IcsFormat) // today till tomorrow
	}
	if *show7days {
		endDate = curTimeDay.UTC().AddDate(0, 0, 7).Format(IcsFormat) // today till 7 days
	}
	if *showCalendars {
	}
	if flagset["cron"] {
		startDate = curTime.UTC().Format(IcsFormat)
		endDate = curTime.UTC().Add(time.Minute * time.Duration(*showMinutes)).Format(IcsFormat)
		showColor = false
	}

	if flagset["l"] {
		getProp()
	} else if flagset["n"] {
		createAppointment(*calNumber, *appointmentData, *recurrence)
	} else if flagset["delete"] {
		deleteEvent(*calNumber, *appointmentDelete)
	} else if flagset["d"] {
		dumpEvent(*calNumber, *appointmentDump, toFile)
	} else if flagset["p"] {
		displayICS()
	} else if flagset["edit"] {
		editEvent(*calNumber, *appointmentEdit)
	} else if flagset["u"] {
		eventEdit := false
		uploadICS(*calNumber, *appointmentFile, eventEdit)
	} else if *version {
		fmt.Print("qcal ")
		fmt.Println(qcalversion)
	} else {
		//startDate = "20210301"
		//endDate = "20210402"
		showAppointments(*calNumber)
	}
}