forked from rjhorniii/ical2org
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathical2org.go
316 lines (283 loc) · 8.61 KB
/
ical2org.go
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
package main
import (
"bufio"
"flag"
"fmt"
"github.com/rjhorniii/ics-golang"
"log"
"os"
"regexp"
"strings"
"time"
)
type args struct {
dupfile string
appfile string
outfile string
afterfile string
label string
dupflag bool
sched bool
active bool
inactive bool
repeats bool
dead bool
count bool
after bool
args []string
}
func main() {
var a args
var dupfilePtr *string
var appPtr *string
var outPtr *string
var afterPtr *string
var labelPtr *string
// define flags
dupfilePtr = flag.String("d", "", "Filename for duplicate removal")
appPtr = flag.String("a", "", "Filename to append new events")
outPtr = flag.String("o", "", "Filename for event output, default stdout")
afterPtr = flag.String("after", "", "Only use events at and after this date")
labelPtr = flag.String("label", "", "Label word in drawer to identify conversion")
flag.BoolVar(&a.sched, "scheduled", false, "Event time should be scheduled")
flag.BoolVar(&a.dead, "deadline", false, "Event time should be deadline")
flag.BoolVar(&a.active, "active", true, "Headline timestamp should be active")
flag.BoolVar(&a.inactive, "inactive", false, "Headline timestamp should be inactive")
flag.BoolVar(&a.repeats, "repeats", true, "Generate an event per repeat")
flag.BoolVar(&a.dupflag, "dupinput", false, "Do not generate duplicates from input")
flag.BoolVar(&a.count, "count", false, "Report number of new events found on stdout")
// parse flags and arguments
flag.Parse()
a.dupfile = *dupfilePtr
a.appfile = *appPtr
a.outfile = *outPtr
a.afterfile = *afterPtr
a.label = *labelPtr
a.args = flag.Args()
process(a)
}
func process(a args) {
var after bool
afterTime := time.Now()
if a.afterfile != "" {
if strings.HasPrefix(a.afterfile, "-") {
d, err := time.ParseDuration(a.afterfile)
if err != nil {
fmt.Printf("After option, duration format error %s\n", err)
return
} else {
afterTime = afterTime.Add(d)
after = true
}
} else {
t, err := time.Parse("2006-01-02", a.afterfile)
if err != nil {
fmt.Printf("After option, time format error %s\n", err)
return
} else {
afterTime = t
after = true
}
}
}
// Verify that input and output are present
if len(a.args) == 0 {
fmt.Println("At least one input argument is required.")
return
}
if a.appfile != "" && a.outfile != "" {
fmt.Println("can't have both output and append files")
os.Exit(1)
}
ics.RepeatRuleApply = a.repeats
// Collect duplicate IDs before parsing inputs
dupIDs := map[string]bool{"": true}
if a.dupfile != "" {
dupIDs = dups(a.dupfile)
a.dupflag = true
}
// create new parser
parser := ics.New()
// get the input chan
inputChan := parser.GetInputChan()
// send referenced arguments
for _, url := range a.args {
// fmt.Printf( "send filename: %s\n", url)
parser.Add() // another calendar to wait for
inputChan <- url
}
close(inputChan)
// wait for the final calendar to be parsed
parser.Wait()
// get all calendars in this parser
cal, err := parser.GetCalendars()
eventsSaved := 0
// check for errors
if err == nil {
// set output file when there are events
var f *os.File
if a.outfile != "" {
f, err = os.OpenFile(a.outfile, os.O_TRUNC|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
log.Fatal(err)
}
} else if a.appfile != "" {
f, err = os.OpenFile(a.appfile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0600)
if err != nil {
log.Fatal(err)
}
} else {
f = os.Stdout
}
for _, calendar := range cal {
allEvents := calendar.GetEvents()
for _, event := range allEvents {
// eliminate duplicates
if dupIDs[event.GetID()] {
continue
}
if a.dupflag {
dupIDs[event.GetID()] = true
// fmt.Printf(" duplicate ID added %s\n", event.GetID())
}
// eliminate events before after
if after {
if event.GetStart().Before(afterTime) {
continue
}
}
eventsSaved++
// print the event
// choose active or inactive timestamp
format := "* %s <%s>\n"
switch {
case a.inactive:
format = "* %s [%s]\n"
case a.active:
format = "* %s <%s>\n"
}
fmt.Fprintf(f, format, strings.Replace(event.GetSummary(), `\,`, ",", -1), event.GetStart().Format("2006-01-02 Mon 15:04"))
// Scheduled, Deadline, or nothing depending upon switches
switch {
case a.dead:
fmt.Fprintf(f, " DEADLINE: <%s-%s>\n", event.GetStart().Format("2006-01-02 Mon 15:04"), event.GetEnd().Format("15:04"))
case a.sched:
fmt.Fprintf(f, " SCHEDULED: <%s-%s>\n", event.GetStart().Format("2006-01-02 Mon 15:04"), event.GetEnd().Format("15:04"))
default:
}
// Print drawer contents
fmt.Fprintln(f, " :ICALCONTENTS:")
fmt.Fprintf(f, " :ORGUID: %s\n", event.GetID())
if a.label != "" {
fmt.Fprintf(f, " :CONVERTLABEL: %s\n", a.label)
}
fmt.Fprintf(f, " :ORIGINAL-UID: %s\n", event.GetImportedID())
fmt.Fprintf(f, " :DTSTART: %s\n", event.GetStart().Format("2006-01-02 15:04"))
fmt.Fprintf(f, " :DTEND: %s\n", event.GetEnd().Format("2006-01-02 15:04"))
fmt.Fprintf(f, " :DTSTAMP: %s\n", event.GetDTStamp().Format("2006-01-02 15:04"))
for _, attendee := range event.GetAttendees() {
fmt.Fprintf(f, " :ATTENDEE: %v\n", attendee)
}
fmt.Fprintf(f, " :ORGANIZER: %s\n", event.GetOrganizer())
if event.GetGeo() != nil {
fmt.Fprintf(f, " :GEO: %v, \n", event.GetGeo())
}
tzids := ""
for _, tz := range event.GetDTZID() {
if !strings.Contains(tzids, tz) {
tzids = tzids + tz
}
}
fmt.Fprintf(f, " :TZIDS: %s\n", tzids)
fmt.Fprintf(f, " :RRULE: %s\n", event.GetRRule())
fmt.Fprintln(f, " :END:")
// Print Description and location
fmt.Fprintf(f, "** Description\n\n")
for _, line := range strings.Split(event.GetDescription(), `\n`) {
fmt.Fprintf(f, " %s\n", strings.Replace(line, `\,`, ",", -1)) //remove escape from commas (a CSV thing)
}
if event.GetLocation() != "" {
fmt.Fprintf(f, "** Location %s \n", event.GetLocation())
}
}
}
if a.count {
fmt.Fprintf(os.Stdout, " New events written: %d\n", eventsSaved)
errors, _ := parser.GetErrors()
if( len(errors) != 0) {
fmt.Printf( "errors occurred %v\n", errors)
}
}
} else {
// error
fmt.Fprintln(os.Stderr, err)
}
}
/* make this parallel later
type Duplicates struct {
inputChan chan string
outputChan chan map[string] bool
}
*/
func dups(dupname string) map[string]bool {
// Basic state machine to find ORGID in ICALCONTENTS drawer. It
// accepts org-mode full syntax, but takes lots of shortcuts to combine
// and ignore many irrelevant fields. It will tolerate incorrect syntax,
// although it might not get recognition right.
// It uses a state machine that processes one line at a time.
//
// States are:
// Body - somewhere in body. Only a headline will depart this state
// Head - somewhere in headline material. Looking for drawers.
// Drawer - in a drawer that is not ICALCONTENTS. Loofing for end.
// Contents - in the ICALCONTENTS drawer, looking for ORGID
//
const Body = 1
const Head = 2
const Drawer = 3
const Contents = 4
state := Body // State begins in Body
// Pattern matches
rHeadline, _ := regexp.Compile(`^\*`) // first character is "*"
rBody, _ := regexp.Compile(`^[^:]*$`) // no colon anywhere on the line (also catches blank lines)
rContents, _ := regexp.Compile(`^\s*:ICALCONTENTS:`) //start of content drawer
rOrgID, _ := regexp.Compile(`^\s*:ORGUID:\s*(\S*)\s*$`) // the orgID
rOther, _ := regexp.Compile(`^\s*:\S*:`) // start of another drawer
rEnd, _ := regexp.Compile(`^\s*:END:`) // end of any drawer
found := make(map[string]bool)
// read lines until the end
dupfile, err := os.Open(dupname)
if err != nil {
if os.IsNotExist(err) {
return (found)
} else {
log.Fatal(err)
}
}
defer dupfile.Close()
scanner := bufio.NewScanner(dupfile)
for scanner.Scan() {
line := scanner.Text()
switch {
case err != nil:
case rHeadline.MatchString(line):
state = Head
case rBody.MatchString(line):
state = Body
case state == Body:
// break. Rest only apply if in header
case state == Head && rContents.MatchString(line):
state = Contents
case state == Contents && rOrgID.MatchString(line):
// extract UID add to map
found[rOrgID.FindStringSubmatch(line)[1]] = true // extract the word after :ORGID:
case state == Head && rOther.MatchString(line):
state = Drawer
case state == Drawer && rEnd.MatchString(line):
state = Head
case state == Contents && rEnd.MatchString(line):
state = Head
}
}
return (found)
}