-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathfine.go
245 lines (215 loc) · 6.49 KB
/
fine.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
package main
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/docopt/docopt-go"
"github.com/fiatjaf/lntxbot/t"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/lucsky/cuid"
)
func handleFine(ctx context.Context, opts docopt.Opts) {
chatOwner := ctx.Value("initiator").(*User)
switch message := ctx.Value("message").(type) {
case *tgbotapi.Message:
if message.Chat.Type == "private" {
send(ctx, chatOwner, t.MUSTBEGROUP)
return
}
msats, err := parseSatoshis(opts)
if err != nil || msats == 0 {
send(ctx, chatOwner, t.ERROR, t.T{"Err": err.Error()})
return
}
reason := getVariadicFieldOrReplyToContent(ctx, opts, "<reason>")
if message.ReplyToMessage == nil || message.ReplyToMessage.From == nil {
send(ctx, chatOwner, t.MISSINGRECEIVER)
return
}
if message.ReplyToMessage.From.ID == message.From.ID {
// can't fine yourself
return
}
if !isAdmin(message.Chat, message.From) {
send(ctx, chatOwner, t.MUSTBEADMIN)
return
}
target, cas, err := ensureTelegramUser(message.ReplyToMessage)
if err != nil {
send(ctx, chatOwner, t.ERROR, t.T{"Err": err.Error()})
log.Warn().Err(err).Int("case", cas).
Str("username", message.ReplyToMessage.From.UserName).
Int("id", message.ReplyToMessage.From.ID).
Msg("failed to ensure user on reply-fine")
return
}
fineKey := cuid.Slug()
// for registered users we will send a keyboard
// for unregistered users an invoice
var (
keyboard *tgbotapi.InlineKeyboardMarkup
bolt11 string
hash string
)
if info, err := target.getInfo(); err == nil && info.BalanceMsat < msats {
expiry := 15 * time.Minute
bolt11, hash, err = chatOwner.makeInvoice(ctx, &MakeInvoiceArgs{
IgnoreRateLimit: true,
Msatoshi: msats,
Description: fmt.Sprintf(
"fine for %s (%s).",
target.AtName(ctx), reason,
),
Tag: "fine",
Extra: InvoiceExtra{Message: message},
Expiry: &expiry,
})
if err != nil {
log.Warn().Err(err).
Stringer("chat-owner", chatOwner).Stringer("target", target).
Msg("failed to create a fine invoice.")
return
}
go chatOwner.track("fine issued", map[string]interface{}{
"sats": msats / 1000,
"group": message.Chat.ID,
"invoice": true,
})
} else {
keyboard = &tgbotapi.InlineKeyboardMarkup{
[][]tgbotapi.InlineKeyboardButton{
{
tgbotapi.NewInlineKeyboardButtonData(
fmt.Sprintf(translateTemplate(ctx,
t.PAYAMOUNT, t.T{"Sats": float64(msats) / 1000})),
fmt.Sprintf("fine=%s", fineKey),
),
},
},
}
go chatOwner.track("fine issued", map[string]interface{}{
"sats": msats / 1000,
"group": message.Chat.ID,
"balance": true,
})
}
notifyMessageId := send(ctx, message.Chat.ID, keyboard, t.FINEMESSAGE, t.T{
"FinedUser": target.AtName(ctx),
"Sats": msats / 1000,
"Reason": reason,
}, FORCESPAMMY)
var invoiceMessage *tgbotapi.Message
if bolt11 != "" {
invoiceMessageId := send(ctx, message.Chat.ID,
qrURL(bolt11), "<code>"+bolt11+"</code>", FORCESPAMMY)
if invoiceMessageId == nil {
log.Error().Int64("group", message.Chat.ID).
Msg("failed to send invoice message on fine")
send(ctx, message.Chat.ID,
t.ERROR, t.T{"Err": "Telegram has refused a message from us."}, FORCESPAMMY)
return
}
invoiceMessage = &tgbotapi.Message{
Chat: &tgbotapi.Chat{ID: message.Chat.ID},
MessageID: invoiceMessageId.(int),
}
}
// reuse the kick stuff we have for tickets
kickdata := KickData{
"fine",
invoiceMessage,
&tgbotapi.Message{
Chat: &tgbotapi.Chat{ID: message.Chat.ID},
MessageID: notifyMessageId.(int),
},
nil,
tgbotapi.ChatMemberConfig{
UserID: int(target.TelegramId),
ChatID: message.Chat.ID,
},
chatOwner,
target.Id,
target.AtName(ctx),
hash,
int(msats / 1000),
}
kickdatajson, _ := json.Marshal(kickdata)
err = rds.HSet("ticket-pending", fineKey, string(kickdatajson)).Err()
if err != nil {
log.Warn().Err(err).Str("kickdata", string(kickdatajson)).
Msg("error saving kickdata")
}
go waitToKick(ctx, fineKey, kickdata)
}
}
func handleFineClickPay(ctx context.Context, fineKey string) {
payer := ctx.Value("initiator").(*User)
log := log.With().Str("fine-key", fineKey).Logger()
kickdatastr, err := rds.HGet("ticket-pending", fineKey).Result()
if err != nil {
log.Warn().Err(err).Msg("error getting fine pending on callback")
return
}
var kickdata KickData
if err := json.Unmarshal([]byte(kickdatastr), &kickdata); err != nil {
log.Warn().Err(err).Msg("failed to unmarshal kickdata from redis")
return
}
if payer.Id != kickdata.TargetId {
return
}
err = payer.sendInternally(
ctx,
kickdata.ChatOwner,
false,
int64(kickdata.Sats*1000),
0,
fmt.Sprintf("Fine at %s.", telegramMessageLink(kickdata.NotifyMessage)),
"",
"fine",
)
if err != nil {
send(ctx, payer, t.ERROR, t.T{"Err": err.Error()})
return
}
dispatchGeneric(fineKey, nil)
finePaid(ctx, fineKey, kickdata)
}
func fineNotPaid(ctx context.Context, fineKey string, kickdata KickData) {
log.Info().Str("fine-key", fineKey).Interface("chat-member", kickdata.ChatMemberConfig).
Msg("fine expired, kicking user")
rds.HDel("ticket-pending", fineKey)
// delete invoice message if it exists
if kickdata.InvoiceMessage != nil {
deleteMessage(kickdata.InvoiceMessage)
}
// remove keyboard if any
send(ctx, kickdata.NotifyMessage, EDIT, &tgbotapi.InlineKeyboardMarkup{
InlineKeyboard: [][]tgbotapi.InlineKeyboardButton{},
})
// send a new message notifying the group of the failure to pay
send(ctx, kickdata.NotifyMessage.Chat.ID, kickdata.NotifyMessage, t.FINEFAILURE, t.T{
"User": kickdata.TargetUsername,
}, FORCESPAMMY)
}
func finePaid(ctx context.Context, fineKey string, kickdata KickData) {
log.Debug().Str("fine-key", fineKey).Msg("fine paid")
rds.HDel("ticket-pending", fineKey)
// delete the invoice message
if kickdata.InvoiceMessage != nil {
deleteMessage(kickdata.InvoiceMessage)
}
// remove keyboard if any
send(ctx, kickdata.NotifyMessage, EDIT, &tgbotapi.InlineKeyboardMarkup{
InlineKeyboard: [][]tgbotapi.InlineKeyboardButton{},
})
// send a new message notifying the group of the success paying
send(ctx, kickdata.NotifyMessage.Chat.ID, kickdata.NotifyMessage, t.FINESUCCESS, t.T{
"User": kickdata.TargetUsername,
}, FORCESPAMMY)
go kickdata.ChatOwner.track("fine paid", map[string]interface{}{
"sats": kickdata.Sats,
"group": kickdata.JoinMessage.Chat.ID,
})
}