-
Notifications
You must be signed in to change notification settings - Fork 0
/
actor.go
194 lines (163 loc) · 5.3 KB
/
actor.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
// actor.go
package main
import (
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/url"
"net/http"
"strings"
"time"
"github.com/golang/glog"
)
func init() {
RegisterInternalFunc(ActorFunc, "GoHomeExit", GoHomeExit)
RegisterInternalFunc(ActorFunc, "SendSMS", SendSMS)
RegisterInternalFunc(ActorFunc, "SendMail", SendMail)
}
// triggerActorById : trigger actor function using ActCmd, restirered parameter 'ActParam' and dynamic param 'param'
func triggerActorById(actorId int, userId int, param string) (result string, err error) {
objs, err := getHomeObjects(nil, ItemIdNone, actorId)
if err != nil {
return
}
if len(objs) <= 0 {
err = errors.New(fmt.Sprintf("No actor with id = %d", actorId))
glog.Error(err)
}
actor := objs[0]
result, err = triggerObjActor(actor, userId, param)
return
}
// triggerObjActor : trigger actor function using ActCmd, registered parameter 'ActParam' and dynamic param 'param'
func triggerObjActor(actor HomeObject, userId int, param string) (result string, err error) {
result = "Failed"
actName, err := actor.getStrVal("Name")
if err != nil {
return
}
actCmd, err := actor.getStrVal("ActCmd")
if err != nil {
return
}
actParam, err := actor.getStrVal("ActParam")
if err != nil {
return
}
isInternal, err := actor.getIntVal("IsInternal")
if err != nil {
return
}
if isInternal != 0 {
result, err = CallInternalFunc(ActorFunc, actCmd, actParam, param)
} else {
result, err = ExecExternalCmd(actCmd, actParam, param)
}
glog.Infof("Actor : user#%d : %s(%s) => %s", userId, actName, param, result)
if glog.V(2) {
glog.Infof("Actor : %s('%s','%s')", actCmd, actParam, param)
}
go recordActorResult(actor, userId, param, result)
return
}
// recordActorResult : store in DB param and result for an actor
func recordActorResult(actor HomeObject, userId int, param string, result string) {
db, err := openDB()
if err != nil {
return
}
defer db.Close()
actorId := actor.getId()
_, err = db.Exec("insert into HistoActor values ( ?, ?, ?, ?, ?);", time.Now().Unix(), actorId, userId, param, result)
if err != nil {
glog.Errorf("Fail to store result (%s) for actor %d : %s ", result, actorId, err)
}
if glog.V(2) {
glog.Infof("recordActorResult : %d - %s - %s", time.Now().Unix(), param, result)
}
}
// -----------------------------------------------
// -----------------------------------------------
// GoHomeExit : End server execution.
// No built-in autorestart, should use systemd for restart (see setup/goHome.service)
// param2 can be use to give a delay before exit
func GoHomeExit(param1 string, param2 string) (result string, err error) {
wait := time.Second
if param1 != "" {
wait, err = time.ParseDuration(param2)
if err != nil {
glog.Errorf("GoHomeExit : error parsigng duration parm : %s", err)
wait = time.Second
}
}
time.AfterFunc(wait, func() { goHomeExitChan <- true })
return fmt.Sprintf("Server exit in %v", wait), nil
}
// -----------------------------------------------
// -----------------------------------------------
// SendSMS : send a SMS using a Android device with SMS Gateway Ultimate (https://play.google.com/store/apps/details?id=com.icecoldapps.smsgatewayultimate)
// param1 : SMS Gateway <serveur[:port]> i.e. 192.168.43.1:1116
// param2 : "<phoneNum> <message>" with phoneNum := "[0-9]+"
func SendSMS(param1 string, param2 string) (result string, err error) {
var Url *url.URL
Url, err = url.Parse("http://" + param1 + "/send.html")
if err != nil {
err = errors.New("SendSMS bad url '" + param1 + "', " + err.Error())
glog.Errorf(err.Error())
result = "bad parameter"
return
}
pTab := strings.Split(param2, " ")
if len(pTab) <= 1 {
err = errors.New("SendSMS bad parameter '" + param2 + "', expecting '<phoneNum> <message>'")
glog.Errorf(err.Error())
result = "bad parameter"
return
}
parameters := url.Values{}
parameters.Add("smstype", "sms")
parameters.Add("smsto", pTab[0])
parameters.Add("smsbody", strings.Join(pTab[1:], " "))
Url.RawQuery = parameters.Encode()
resp, err := http.Get(Url.String())
if err != nil {
err = errors.New("SendSMS gateway err '" + Url.String() + "', " + err.Error())
glog.Errorf(err.Error())
result = "SendSMS gateway err"
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if ! strings.Contains(fmt.Sprintf("%s",body),"The SMS has been sent") {
result = "Missing Gateway confirmation"
return
}
result = "Done"
return
}
// -----------------------------------------------
// -----------------------------------------------
// SendMail : send a mail
// param1 : JSON param : {"server":"smtp.gmail.com","port":"587","tls":true,"account":"sender@gmail.com","password":"****",......}
// param2 : "<to>|<subject>|<message>" ... so subject can't include char '|'
func SendMail(param1 string, param2 string) (result string, err error) {
var mail MailInfo
err = json.Unmarshal([]byte(param1), &mail)
if err != nil {
glog.Errorf("Fail to unmarshal MailInfo (%s) : %s", param1, err)
return
}
pTab := strings.Split(param2, "|")
if len(pTab) <= 1 {
err = errors.New("SendMail bad parameter '" + param2 + "', expecting '<to>|<subject>|<message>'")
glog.Errorf(err.Error())
result = "bad parameter"
return
}
mail.To = pTab[0]
mail.Subject = pTab[1]
mail.Message = strings.Join(pTab[2:], "|")
result, err = smtpSendMail(mail)
return
}