/
fetchsms.go
159 lines (148 loc) · 3.14 KB
/
fetchsms.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
package main
import (
"fmt"
"io"
"strings"
"time"
"github.com/tarm/serial"
)
// ChatLine contains a single chat line
type ChatLine struct {
Method string
Command string
Response string
Enforce bool
}
var setupChat = []ChatLine{
ChatLine{
Method: "Send",
Command: "ATE0",
Response: "OK",
Enforce: false,
},
ChatLine{
Method: "Send",
Command: "AT^CURC=0",
Response: "OK",
Enforce: true,
},
ChatLine{
Method: "Send",
Command: "AT+CPMS=\"SM\"",
Response: "+CPMS: 0,30,0,30,0,30\r\n\r\nOK",
Enforce: false,
},
ChatLine{
Method: "Send",
Command: "AT+CMGF=1",
Response: "OK",
Enforce: true,
},
ChatLine{
Method: "Send",
Command: "AT+CNMI=1,1,0,1,0",
Response: "OK",
Enforce: true,
},
ChatLine{
Method: "Wait",
Command: "",
Response: "+CMTI:",
Enforce: true,
},
}
// SMSClient implements an SMSClient
type SMSClient struct {
Port *serial.Port
Routing *Router
}
// New returns an SMSClient
func New(name string, baud int, routes []byte) (*SMSClient, error) {
var err error
sc := new(SMSClient)
sc.Port, err = serial.OpenPort(&serial.Config{Name: name, Baud: baud, ReadTimeout: 3})
if err != nil {
return nil, err
}
sc.Routing = LoadRouter(routes)
return sc, nil
}
// SendCommand sends a command
func (sc *SMSClient) SendCommand(command string) (resp string, err error) {
if len(command) > 0 {
sendString := []byte(command + "\r\n")
_, err = sc.Port.Write(sendString)
if err != nil {
return "", err
}
}
buf := make([]byte, 4096)
n, err := sc.Port.Read(buf)
if err != nil {
return "", err
}
return strings.Trim(string(buf[:n]), " \t\r\n"), nil
}
// Wait for a line containing match
func (sc *SMSClient) Wait(match string) (resp string) {
for {
buf := make([]byte, 4096)
n, err := sc.Port.Read(buf)
if err != nil {
if err == io.EOF {
time.Sleep(time.Second / 4)
}
}
res := strings.Trim(string(buf[:n]), " \t\r\n")
if len(res) >= len(match) && match == res[:len(match)] {
return res
}
}
}
func (sc *SMSClient) ForwardSMS() error {
for _, e := range setupChat {
if e.Method == "Send" {
resp, err := sc.SendCommand(e.Command)
if err != nil {
return err
}
if e.Enforce {
if resp != e.Response {
return fmt.Errorf("%s -> '%s' != '%s'", e.Command, e.Response, resp)
}
}
} else if e.Method == "Wait" {
for {
resp := parseCMTI(sc.Wait(e.Response))
message, err := sc.SendCommand("AT+CMGR=" + resp)
if err == nil {
sc.SendCommand("AT+CMGD=" + resp)
// fmt.Printf("Message received: %s\n", message)
sender, text := parseMessage(message)
sc.Routing.SendMail(sender, text)
}
}
}
}
return sc.Port.Close()
}
func parseMessage(str string) (sender, message string) {
parts := strings.Split(str, "\r\n")
header := parts[0]
headerParts := strings.Split(header, "\"")
if len(headerParts) >= 4 {
sender = headerParts[3]
}
if len(parts) < 4 {
return sender, ""
}
message = strings.Join(parts[1:len(parts)-2], " ")
return sender, message
}
func parseCMTI(str string) string {
pos := strings.Index(str, ",")
if pos > 0 && pos+1 < len(str) {
return str[pos+1:]
}
return ""
}