-
Notifications
You must be signed in to change notification settings - Fork 25
/
cliche.go
159 lines (134 loc) · 3.2 KB
/
cliche.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 (
"context"
"encoding/json"
"os"
"strings"
"time"
"github.com/fiatjaf/go-cliche"
"github.com/fiatjaf/lntxbot/t"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/kballard/go-shellquote"
)
func setupCliche() {
ln = &cliche.Control{
BinaryPath: s.ClicheBinaryPath,
JARPath: s.ClicheJARPath,
DataDir: s.ClicheDataDir,
}
log.Info().Msg("starting cliche and waiting for a 'ready' event")
if err := ln.Start(); err != nil {
log.Fatal().Err(err).Msg("failed to start cliche")
}
log.Info().Msg("cliche is ready")
if nodeinfo, err := ln.GetInfo(); err != nil {
log.Fatal().Err(err).Msg("can't talk to cliche")
} else {
log.Info().
Int("blockHeight", nodeinfo.BlockHeight).
Int("channels", len(nodeinfo.Channels)).
Msg("cliche connected")
}
}
func handleClicheEvents() {
ctx := context.WithValue(context.Background(), "origin", "cliche")
go func() {
for event := range ln.IncomingPayments {
go paymentReceived(ctx, event.PaymentHash, event.Msatoshi)
}
}()
go func() {
for event := range ln.PaymentSuccesses {
go paymentHasSucceeded(
ctx,
event.Msatoshi,
event.FeeMsatoshi,
event.Preimage,
"",
event.PaymentHash,
)
}
}()
go func() {
for event := range ln.PaymentFailures {
go paymentHasFailed(ctx, event.PaymentHash, event.Failure)
}
}()
}
func handleClicheCommand(
ctx context.Context,
message *tgbotapi.Message,
messageText string,
) {
u := ctx.Value("initiator").(*User)
spl := strings.SplitN(messageText[8:], " ", 2)
method := spl[0]
params := make(map[string]interface{})
if len(spl) == 2 {
argv, err := shellquote.Split(spl[1])
if err != nil {
send(ctx, u, t.ERROR, t.T{"Err": err.Error()})
return
}
if len(argv)%2 != 0 {
send(ctx, u, t.ERROR, t.T{"Err": "invalid number of args"})
return
}
for i := range argv {
if i%2 != 0 || !strings.HasPrefix(argv[i], "--") {
continue
}
key := argv[i][2:]
val := argv[i+1]
var value interface{}
if err := json.Unmarshal([]byte(val), &value); err != nil {
value = val
}
params[key] = value
}
}
resp, err := ln.Call(method, params)
if err != nil {
send(ctx, u, t.ERROR, t.T{"Err": err.Error()})
return
}
var jresp interface{}
pretty := resp
if err := json.Unmarshal(resp, &jresp); err == nil {
pretty, _ = json.MarshalIndent(jresp, "", " ")
}
send(ctx, u, "<pre><code class=\"language-json\">\n"+string(pretty)+"\n</code></pre>")
}
func clicheCheckingRoutine() {
ctx := context.Background()
for {
time.Sleep(5 * time.Minute)
select {
case err := <-clichePing():
if err != nil {
log.Error().Err(err).Msg("cliche ping returned error")
break
} else {
log.Debug().Msg("cliche is fine")
continue
}
case <-time.After(3 * time.Minute):
log.Error().Msg("cliche is not responding after 3 minutes")
break
}
// message admin
if admin, err := loadUser(s.AdminAccount); err == nil {
send(ctx, admin, "cliche has failed, bot restarting")
}
// exit with a failure so systemd can restart us
os.Exit(7)
}
}
func clichePing() chan error {
ch := make(chan error)
go func() {
_, err := ln.Call("ping", map[string]interface{}{})
ch <- err
}()
return ch
}