-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsimple.go
335 lines (284 loc) · 7.45 KB
/
simple.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main
import (
//"crypto/tls"
"fmt"
"io/ioutil"
"log"
"net/http"
"strings"
"time"
"github.com/thoj/go-ircevent"
)
const serverssl = "irc.oftc.net:6667"
const ownnick = "spacephone"
// When is a battery considered low?
const battery_low = 42
// When to check if the battery is low
const battery_check_time = time.Minute * 10
// How often to remind about a low battery
const battery_reminder_time = time.Hour * 1
// When to check if the battery charging state has changed
const battery_charge_check_time = time.Second * 30
var battery_charging = false
const commandprefix = "!"
//const serverssl = "irc.oftc.net:6697"
type cmdFunc = func(*irc.Event, string, string) error
type recFunc = func() error
var commands = map[string]cmdFunc{
"say": say,
"sayfr": sayfr,
"sayde": sayde,
"saynl": saynl,
"saybg": saybg,
"silence": silence,
"spacestate": spacestate,
"alert": alert,
"battery": battery,
"vibrate": vibrate,
"mpd": mpdf,
"powersocketon": powersocketon,
"powersocketoff": powersocketoff,
"help": help,
}
var irccon = irc.IRC("", "") // FIXME
var silenced = time.Unix(0, 0)
var last_battery_warning = time.Unix(0, 0)
func say(e *irc.Event, parsed_message, reply string) error {
if time.Now().Before(silenced) {
return fmt.Errorf("Currently silenced")
}
Espeak(parsed_message)
return nil
}
func saylang(e *irc.Event, parsed_message, reply, lang string) error {
if time.Now().Before(silenced) {
return fmt.Errorf("Currently silenced")
}
EspeakLang(lang, parsed_message)
return nil
}
func sayfr(e *irc.Event, parsed_message, reply string) error {
return saylang(e, parsed_message, reply, "fr")
}
func sayde(e *irc.Event, parsed_message, reply string) error {
return saylang(e, parsed_message, reply, "de")
}
func saynl(e *irc.Event, parsed_message, reply string) error {
return saylang(e, parsed_message, reply, "nl")
}
func saybg(e *irc.Event, parsed_message, reply string) error {
return saylang(e, parsed_message, reply, "bg")
}
func silence(e *irc.Event, parsed_message, reply string) error {
duration := time.Duration(1) * time.Minute
if len(parsed_message) > 1 {
dur, err := time.ParseDuration(parsed_message)
if err != nil {
return err
}
if dur.Minutes() > 5 {
return fmt.Errorf("Duration too long")
}
duration = dur
}
silenced = time.Now().Add(duration)
return nil
}
func spacestate(e *irc.Event, parsed_message, reply string) error {
resp, err := http.Get("http://techinc.nl/space/spacestate")
if err != nil {
return err
}
defer resp.Body.Close()
state, err := ioutil.ReadAll(resp.Body)
irccon.Privmsg(reply, string(state))
return nil
}
func alert(e *irc.Event, parsed_message, reply string) error {
go Espeak(parsed_message)
go MqttSay(parsed_message, e.Nick)
return nil
}
func battery(e *irc.Event, parsed_message, reply string) error {
go func() {
percentage, err := BatteryPercentage()
if err != nil {
irccon.Privmsg(reply, fmt.Sprintf("Error getting battery percentage: %v", err))
return
}
charging, err := BatteryCharging()
if err != nil {
irccon.Privmsg(reply, fmt.Sprintf("Error getting battery percentage: %v", err))
return
}
is_charging := "yes"
if !charging {
is_charging = "no"
}
irccon.Privmsg(reply, fmt.Sprintf("Percentage: %d. Charging: %s", percentage, is_charging))
}()
return nil
}
func vibrate(e *irc.Event, parsed_message, reply string) error {
Vibrate()
return nil
}
func mpdf(e *irc.Event, parsed_message, reply string) error {
if parsed_message == "ping" {
err := MpdPing()
if err != nil {
return err
}
irccon.Privmsg(reply, "pong")
} else if parsed_message == "status" {
attrs, err := MpdStatus()
if err != nil {
return err
}
irccon.Privmsg(reply, fmt.Sprintf("%v", attrs))
} else if parsed_message == "np" {
attrs, err := MpdCurrentSong()
if err != nil {
return err
}
irccon.Privmsg(reply, fmt.Sprintf("%v", attrs))
} else if parsed_message == "play" {
err := MpdPause(false)
if err != nil {
return err
}
irccon.Privmsg(reply, "Now playing")
} else if parsed_message == "pause" {
err := MpdPause(true)
if err != nil {
return err
}
irccon.Privmsg(reply, "Now paused")
}
return nil
}
func powersocketon(e *irc.Event, parsed_message, reply string) error {
err := PowerSocketSet(parsed_message, true)
if err != nil {
return err
}
irccon.Privmsg(reply, "consider it done")
return nil
}
func powersocketoff(e *irc.Event, parsed_message, reply string) error {
err := PowerSocketSet(parsed_message, false)
if err != nil {
return err
}
irccon.Privmsg(reply, "consider it done")
return nil
}
func help(e *irc.Event, parsed_message, reply string) error {
irccon.Privmsg(reply, commandprefix+"{say,silence,spacestate,alert,battery,vibrate,mpd,powersocketon,powersocketoff}")
return nil
}
func recurring(interval time.Duration, fu recFunc) {
for true {
time.Sleep(interval)
err := fu()
if err != nil {
log.Println("recurring error:", err)
}
}
}
func join(e *irc.Event) {
// TODO CHANNEL
go MqttIrcJoin(e.Nick)
}
func part(e *irc.Event) {
// TODO CHANNEL
go MqttIrcPart(e.Nick)
}
func privmsg(e *irc.Event) {
user_or_chan := e.Arguments[0]
// XXX: This is really ugly and will not work if we get a different nick
// somehow
if user_or_chan == ownnick {
user_or_chan = e.Nick
}
msg := e.Arguments[1]
go MqttIrc(e.Nick, msg)
nameref := false
if strings.HasPrefix(msg, ownnick+": ") {
msg = msg[len(ownnick+" "):]
nameref = true
}
if strings.HasPrefix(msg, commandprefix) || nameref {
s := strings.SplitN(msg[1:], " ", 2)
cmd := s[0]
parsed_msg := ""
if len(s) > 1 {
parsed_msg = s[1]
}
if cmdfunc, found := commands[cmd]; found {
err := cmdfunc(e, parsed_msg, user_or_chan)
if err != nil {
irccon.Privmsg(user_or_chan, fmt.Sprintf("Error: %v", err))
} else {
//irccon.Privmsg(user_or_chan, fmt.Sprintf("OK/Executed"))
}
} else {
irccon.Privmsg(user_or_chan, "Invalid command")
log.Println("Unknown command:", cmd)
}
}
}
func main() {
MqttInit()
ircnick1 := ownnick
irccon = irc.IRC(ircnick1, ownnick)
irccon.VerboseCallbackHandler = false
irccon.Debug = false
irccon.AddCallback("001", func(e *irc.Event) {
irccon.Join("#techinc")
irccon.Join("#techinc-spacephone")
})
irccon.AddCallback("366", func(e *irc.Event) {})
irccon.AddCallback("PRIVMSG", privmsg)
irccon.AddCallback("JOIN", join)
irccon.AddCallback("PART", part)
err := irccon.Connect(serverssl)
if err != nil {
fmt.Printf("Err %s", err)
return
}
go recurring(battery_check_time, func() error {
percentage, err := BatteryPercentage()
if err != nil {
return err
}
if percentage < battery_low {
if !battery_charging && time.Now().After(last_battery_warning.Add(battery_reminder_time)) {
last_battery_warning = time.Now()
msg := fmt.Sprintf("WARNING: battery percentage is less than %d: %d", battery_low, percentage)
irccon.Privmsg("#techinc-spacephone", msg)
Espeak(msg)
}
}
return nil
})
go recurring(battery_charge_check_time, func() error {
charge, err := BatteryCharging()
if err != nil {
return err
}
if charge != battery_charging {
msg := "battery is now "
if charge {
irccon.Privmsg("#techinc-spacephone", msg+"charging")
Espeak(msg + "charging")
} else {
irccon.Privmsg("#techinc-spacephone", msg+"discharging")
Espeak(msg + "discharging")
}
}
battery_charging = charge
return nil
})
irccon.Loop()
}