-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcall.go
83 lines (69 loc) · 1.57 KB
/
call.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
package norobo
import (
"encoding/csv"
"fmt"
"os"
"strings"
"time"
"github.com/dgnorton/norobo/hayes"
)
// Call represents the current in-progress call.
type Call struct {
*hayes.Call
FilterResult *FilterResult
}
// CallEntry represents a completed (ended) call log entry.
type CallEntry struct {
Time time.Time `json:"time"`
Name string `json:"name"`
Number string `json:"number"`
Action string `json:"action"`
Filter string `json:"filter"`
Reason string `json:"reason"`
}
// CallLog represents a list of completed (ended) calls.
type CallLog struct {
Calls []*CallEntry `json:"calls"`
}
// LastTime returns the time of the last call in the log.
func (l *CallLog) LastTime() time.Time {
return l.Calls[len(l.Calls)-1].Time
}
// LoadCallLog loads a call log from file.
func LoadCallLog(filename string) (*CallLog, error) {
f, err := os.Open(filename)
if err != nil {
if !os.IsNotExist(err) {
return nil, err
}
return &CallLog{
Calls: make([]*CallEntry, 0),
}, nil
}
records, err := csv.NewReader(f).ReadAll()
if err != nil {
return nil, err
}
calls := &CallLog{
Calls: make([]*CallEntry, 0, len(records)),
}
for _, r := range records {
if len(r) != 6 {
return nil, fmt.Errorf("expected 6 fields but got %d: %s", len(r), strings.Join(r, ","))
}
t, err := time.Parse(time.RFC3339Nano, r[0])
if err != nil {
return nil, err
}
call := &CallEntry{
Time: t,
Name: r[1],
Number: r[2],
Action: r[3],
Filter: r[4],
Reason: r[5],
}
calls.Calls = append(calls.Calls, call)
}
return calls, nil
}