-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
431 lines (380 loc) · 11.5 KB
/
main.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
package main
import (
"bufio"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"net/http"
"os"
"os/signal"
"strings"
"sync/atomic"
"syscall"
"time"
_ "github.com/mattn/go-sqlite3"
"github.com/mdp/qrterminal/v3"
"google.golang.org/protobuf/proto"
"go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/appstate"
waBinary "go.mau.fi/whatsmeow/binary"
"go.mau.fi/whatsmeow/store"
"go.mau.fi/whatsmeow/store/sqlstore"
"go.mau.fi/whatsmeow/types"
"go.mau.fi/whatsmeow/types/events"
waLog "go.mau.fi/whatsmeow/util/log"
)
var cli *whatsmeow.Client
var log waLog.Logger
var logLevel = "INFO"
var debugLogs = flag.Bool("debug", false, "Enable debug logs?")
var dbDialect = flag.String("db-dialect", "sqlite3", "Database dialect (sqlite3 or postgres)")
var dbAddress = flag.String("db-address", "file:db/whatsbot.db?_foreign_keys=on", "Database address")
var requestFullSync = flag.Bool("request-full-sync", false, "Request full (1 year) history sync when logging in?")
var mediaPath = flag.String("media-path", "media", "Path to store media files in")
var historyPath = flag.String("history-path", "history", "Path to store history files in")
var pairRejectChan = make(chan bool, 1)
var getIDSecret string
func main() {
waBinary.IndentXML = true
flag.Parse()
if *debugLogs {
logLevel = "DEBUG"
}
if *requestFullSync {
store.DeviceProps.RequireFullSync = proto.Bool(true)
}
log = waLog.Stdout("Main", logLevel, true)
dbLog := waLog.Stdout("Database", logLevel, true)
storeContainer, err := sqlstore.New(*dbDialect, *dbAddress, dbLog)
if err != nil {
log.Errorf("Failed to connect to database: %v", err)
return
}
device, err := storeContainer.GetFirstDevice()
if err != nil {
log.Errorf("Failed to get device: %v", err)
return
}
cli = whatsmeow.NewClient(device, waLog.Stdout("Client", logLevel, true))
getIDSecret = cli.GenerateMessageID()
var isWaitingForPair atomic.Bool
cli.PrePairCallback = func(jid types.JID, platform, businessName string) bool {
isWaitingForPair.Store(true)
defer isWaitingForPair.Store(false)
log.Infof("Pairing %s (platform: %q, business name: %q). Type r within 3 seconds to reject pair", jid, platform, businessName)
select {
case reject := <-pairRejectChan:
if reject {
log.Infof("Rejecting pair")
return false
}
case <-time.After(3 * time.Second):
}
log.Infof("Accepting pair")
return true
}
ch, err := cli.GetQRChannel(context.Background())
if err != nil {
// This error means that we're already logged in, so ignore it.
if !errors.Is(err, whatsmeow.ErrQRStoreContainsID) {
log.Errorf("Failed to get QR channel: %v", err)
}
} else {
go func() {
for evt := range ch {
if evt.Event == "code" {
qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
} else {
log.Infof("QR channel result: %s", evt.Event)
}
}
}()
}
cli.AddEventHandler(handler)
err = cli.Connect()
if err != nil {
log.Errorf("Failed to connect: %v", err)
return
}
c := make(chan os.Signal, 1)
input := make(chan string)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
defer close(input)
scan := bufio.NewScanner(os.Stdin)
for scan.Scan() {
line := strings.TrimSpace(scan.Text())
if len(line) > 0 {
input <- line
}
}
}()
for {
select {
case <-c:
log.Infof("Interrupt received, exiting")
cli.Disconnect()
return
case cmd := <-input:
if len(cmd) == 0 {
log.Infof("Stdin closed, exiting")
cli.Disconnect()
return
}
if isWaitingForPair.Load() {
if cmd == "r" {
pairRejectChan <- true
} else if cmd == "a" {
pairRejectChan <- false
}
continue
}
args := strings.Fields(cmd)
cmd = args[0]
args = args[1:]
go handleCmd(strings.ToLower(cmd), args)
}
}
}
var historySyncID int32
var startupTime = time.Now().Unix()
func handleCmd(cmd string, args []string) {
handleCmd1(cmd, args, nil)
}
func handleCmd1(cmd string, args []string, evt *events.Message) (output string) {
output = "Command not found"
switch cmd {
case "getgroup":
output = cmdGetGroup(args)
log.Infof("output: %s", output)
case "listgroups":
output = cmdListGroups(args)
log.Infof("output: %s", output)
case "send-spoofed-reply":
print("args: ", args)
output = cmdSendSpoofedReply(args)
log.Infof("output: %s", output)
case "send-spoofed-img-reply":
output = cmdSendSpoofedImgReply(args)
log.Infof("output: %s", output)
case "send-spoofed-demo":
output = cmdSendSpoofedDemo(args)
log.Infof("output: %s", output)
case "send-spoofed-demo-img":
output = cmdSendSpoofedDemoImg(args)
log.Infof("output: %s", output)
case "spoofed-reply-this":
if evt != nil {
if evt.Message.ExtendedTextMessage != nil {
if evt.Message.ExtendedTextMessage.ContextInfo != nil {
if evt.Message.ExtendedTextMessage.ContextInfo.QuotedMessage != nil {
output = cmdSpoofedReplyThis(args, evt.Message)
log.Infof("output: %s", output)
}
}
} else {
output = "You need use this command replying a message"
log.Infof("output: %s", output)
}
} else {
output = "You need to reply a message using your whatsapp client to use this command"
log.Infof("output: %s", output)
}
}
return
}
type RequestSendSpoofed struct {
CID string `json:"chat_id"`
MID string `json:"message_id"`
SID string `json:"spoofed_id"`
SPF_MSG string `json:"spoofed_message"`
RPL_MSG string `json:"reply_message"`
}
type ResponseData struct {
Message string `json:"message"`
}
func spoofedMsgSenderHandler(w http.ResponseWriter, r *http.Request) {
log.Infof("HTTP Request: %s", r.URL.Path)
if r.Method == "POST" {
var requestData RequestSendSpoofed
if err := json.NewDecoder(r.Body).Decode(&requestData); err != nil {
http.Error(w, "Error parsing JSON request body", http.StatusBadRequest)
return
}
if requestData.CID == "" || requestData.MID == "" || requestData.SID == "" || requestData.SPF_MSG == "" || requestData.RPL_MSG == "" {
http.Error(w, "Invalid request", http.StatusBadRequest)
return
}
var args = []string{requestData.CID, requestData.MID, requestData.SID, requestData.SPF_MSG + "|" + requestData.RPL_MSG}
print("args: ", args)
var output = cmdSendSpoofedReply(args)
log.Infof("output: %s", output)
w.WriteHeader(http.StatusOK)
w.Header().Set("Content-Type", "application/json")
responseData := ResponseData{
Message: output,
}
if err := json.NewEncoder(w).Encode(responseData); err != nil {
http.Error(w, "Error encoding JSON response", http.StatusInternalServerError)
return
}
return
}
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Invalid request"))
}
func getGroupsHandler(w http.ResponseWriter, r *http.Request) {
log.Infof("HTTP Request: %s", r.URL.Path)
if r.Method == "GET" {
var output = cmdListGroups([]string{})
log.Infof("output: %s", output)
w.WriteHeader(http.StatusOK)
w.Write([]byte(output))
return
}
w.WriteHeader(http.StatusBadRequest)
w.Write([]byte("Invalid request"))
}
func handler(rawEvt interface{}) {
switch evt := rawEvt.(type) {
case *events.AppStateSyncComplete:
if len(cli.Store.PushName) > 0 && evt.Name == appstate.WAPatchCriticalBlock {
err := cli.SendPresence(types.PresenceAvailable)
log.Errorf("AppStateSyncComplete %s: %v", rawEvt, err)
}
return
case *events.Connected, *events.PushNameSetting:
if len(cli.Store.PushName) == 0 {
return
}
err := cli.SendPresence(types.PresenceAvailable)
log.Errorf("Connected %s: %v", rawEvt, err)
r := http.NewServeMux()
buildHandler := http.FileServer(http.Dir("client/"))
r.Handle("/", buildHandler)
r.HandleFunc("/send-spoofed", spoofedMsgSenderHandler)
r.HandleFunc("/get-groups", getGroupsHandler)
srv := &http.Server{
Handler: r,
Addr: "127.0.0.1:8080",
WriteTimeout: 15 * time.Second,
ReadTimeout: 15 * time.Second,
}
log.Infof("Listening on localhost:8080")
if srv.ListenAndServe() != nil {
log.Errorf("Failed to start server: %v", err)
}
return
case *events.StreamReplaced:
os.Exit(0)
case *events.Message:
log.Infof("Received message %s from %s (%s)", evt.Info.ID, evt.Info.SourceString())
if strings.HasPrefix(getMsg(evt), getIDSecret) {
if evt.Info.IsFromMe {
text := fmt.Sprintf("-> Cmd output: \nChatID %v", evt.Info.Chat)
jid, _ := parseJID(cli.Store.ID.User)
sendConversationMessage(jid, text)
return
}
}
if strings.HasPrefix(getMsg(evt), "/setSecrete ") {
if evt.Info.IsFromMe {
jid, _ := parseJID(cli.Store.ID.User)
if evt.Info.Chat.String() == jid.String() {
words := strings.SplitN(getMsg(evt), " ", 2)
if len(words) > 1 {
strWords := words[1]
getIDSecret = strWords
text := fmt.Sprintf("-> Cmd output: \nbSecret set to %s", getIDSecret)
sendConversationMessage(jid, text)
} else {
text := fmt.Sprintf("-> Cmd output: \nYou need to set a secret")
sendConversationMessage(jid, text)
}
return
}
}
}
if strings.HasPrefix(getMsg(evt), "/cmd ") {
if evt.Info.IsFromMe {
jid, _ := parseJID(cli.Store.ID.User)
if evt.Info.Chat.String() == jid.String() {
words := strings.SplitN(getMsg(evt), " ", 3)
if len(words) > 1 {
strCommand := words[1]
strParameters := ""
out := handleCmd1(strCommand, []string{}, evt)
if len(words) > 2 {
strParameters = words[2]
out = handleCmd1(strCommand, strings.Split(strParameters, " "), evt)
}
text := fmt.Sprintf("-> Cmd output: %s", out)
sendConversationMessage(jid, text)
} else {
text := fmt.Sprintf("-> Cmd output: \nYou need send a valid command")
sendConversationMessage(jid, text)
}
return
}
}
}
img := evt.Message.GetImageMessage()
if img != nil {
ok := download("Message.GetImageMessage", evt.Message.GetImageMessage(), evt.Message.GetImageMessage().GetMimetype(), evt, rawEvt)
if ok == nil {
return
}
}
audio := evt.Message.GetAudioMessage()
if audio != nil {
ok := download("Message.GetAudioMessage", evt.Message.GetAudioMessage(), evt.Message.GetAudioMessage().GetMimetype(), evt, rawEvt)
if ok == nil {
return
}
}
video := evt.Message.GetVideoMessage()
if video != nil {
ok := download("Message.GetVideoMessage", evt.Message.GetVideoMessage(), evt.Message.GetVideoMessage().GetMimetype(), evt, rawEvt)
if ok == nil {
return
}
}
doc := evt.Message.GetDocumentMessage()
if doc != nil {
ok := download("Message.GetDocumentMessage", evt.Message.GetDocumentMessage(), evt.Message.GetDocumentMessage().GetMimetype(), evt, rawEvt)
if ok == nil {
return
}
}
sticker := evt.Message.GetStickerMessage()
if sticker != nil {
ok := download("Message.GetStickerMessage", evt.Message.GetStickerMessage(), evt.Message.GetStickerMessage().GetMimetype(), evt, rawEvt)
if ok == nil {
return
}
}
contact := evt.Message.GetContactMessage()
if contact != nil {
ok := download("Message.GetContactMessage", evt.Message.GetContactMessage(), "text/vcard", evt, rawEvt)
if ok == nil {
return
}
}
postEvent("Message", rawEvt, nil)
return
}
}
func postEventFile(evt_type string, raw interface{}, extra interface{}, file_name string, file_bytes []byte) error {
log.Infof("Event(%s): \n File: %s \n Extra: %+v \n Raw: %+v", evt_type, file_name, extra, raw)
return nil
}
func postEvent(evt_type string, raw interface{}, extra interface{}) error {
log.Infof("Event(%s): \n%+v", evt_type, raw)
return nil
}
func postError(evt_type string, evt_error string, raw interface{}) error {
log.Errorf("Error(%s): %s \n%+v", evt_type, evt_error, raw)
return nil
}