-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsend.go
164 lines (143 loc) · 4.12 KB
/
send.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
package main
import (
"context"
"strings"
"github.com/docopt/docopt-go"
"github.com/fiatjaf/go-lnurl"
"github.com/fiatjaf/lntxbot/t"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
)
func handleSend(ctx context.Context, opts docopt.Opts) {
u := ctx.Value("initiator").(*User)
var g GroupChat
if ig := ctx.Value("group"); ig != nil {
g, _ = ig.(GroupChat)
ctx = context.WithValue(ctx, "spammy", g.isSpammy())
}
// sending money to others
var (
msats int64
receiver *User
username string
description string
)
// get quantity
msats, err := parseSatoshis(opts)
amtraw := opts["<satoshis>"].(string)
if err != nil || msats <= 0 {
send(ctx, u, t.ERROR, t.T{"Err": err.Error()})
return
} else {
username, _ = opts.String("<receiver>")
}
anonymous := false
if opts["anonymously"].(bool) ||
opts["--anonymous"].(bool) || opts["sendanonymously"].(bool) {
anonymous = true
}
if extra, ok := opts["<description>"].([]string); ok {
description = strings.Join(extra, " ")
}
// maybe this is a lightning address like [email protected]?
if _, _, ok := lnurl.ParseInternetIdentifier(username); ok {
// handleLNURL(ctx, username, handleLNURLOpts{
// payAmountWithoutPrompt: &msats,
// forceSendComment: description,
// anonymous: anonymous,
// })
// end here since the flow will proceed on handleLNURL
return
}
// maybe this is an onchain address like bc1...?
if strings.HasPrefix(username, "bc1") {
handleSendToAddress(ctx, username, msats)
// end here since the flow will proceed on handleSendToAddress
return
}
message := ctx.Value("message").(*tgbotapi.Message)
receiver, err = examineTelegramUsername(username)
if receiver != nil {
goto ensured
}
// no username, this may be a reply-tip
if message.ReplyToMessage != nil {
// the <receiver> part is useless as a username,
// but it can part of the tip description
description = username + " " + description
var cas int
rec, cas, err := ensureTelegramUser(message.ReplyToMessage)
receiver = rec
if err != nil {
send(ctx, g, u, t.SAVERECEIVERFAIL)
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-tip")
return
}
goto ensured
}
// if we ever reach this point then it's because the receiver is missing.
if err != nil {
log.Warn().Err(err).Str("username", username).Msg("error parsing username")
}
send(ctx, g, u, t.MISSINGRECEIVER)
return
ensured:
trimmedDescription := strings.TrimSpace(description)
if m := ctx.Value("message"); m != nil && !anonymous {
message := m.(*tgbotapi.Message)
if message.Chat.Type != "private" {
trimmedDescription = strings.TrimSpace(
trimmedDescription + " (" + telegramMessageLink(message) + ")",
)
}
}
err = u.sendInternally(
ctx,
receiver,
anonymous,
msats,
int64(float64(msats)*0.003),
trimmedDescription,
"",
"",
)
if err != nil {
log.Warn().Err(err).
Str("from", u.Username).
Str("to", receiver.AtName(ctx)).
Msg("failed to send/tip")
send(ctx, g, u, t.FAILEDSEND, t.T{"Err": err.Error()})
return
}
// notify sender
send(ctx, u, t.USERSENTTOUSER, t.T{
"User": receiver.AtName(ctx),
"Sats": msats / 1000,
"RawSats": amtraw,
"ReceiverHasNoChat": receiver.TelegramChatId == 0,
})
// notify receiver
if receiver.hasPrivateChat() && !ctx.Value("spammy").(bool) {
// if possible privately
if anonymous {
send(ctx, receiver, t.RECEIVEDSATSANON, t.T{"Sats": msats / 1000})
} else {
send(ctx, receiver, t.USERSENTYOUSATS, t.T{
"User": u.AtName(ctx),
"Sats": msats / 1000,
"RawSats": amtraw,
})
}
}
if !receiver.hasPrivateChat() || ctx.Value("spammy").(bool) {
// publicly if the receiver doesn't have a chat or if the group is spammy
send(ctx, g, u, t.SATSGIVENPUBLIC, t.T{
"From": u.AtName(ctx),
"To": receiver.AtName(ctx),
"Sats": msats / 1000,
"ClaimerHasNoChat": receiver.TelegramChatId == 0,
}, ctx.Value("message"), FORCESPAMMY)
}
}