forked from asternic/wuzapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wmiau.go
586 lines (544 loc) · 18.5 KB
/
wmiau.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
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
package main
import (
"context"
"crypto/tls"
"database/sql"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"os"
"strconv"
"strings"
"time"
"github.com/go-resty/resty/v2"
"github.com/mdp/qrterminal/v3"
"github.com/patrickmn/go-cache"
"github.com/skip2/go-qrcode"
"go.mau.fi/whatsmeow"
"go.mau.fi/whatsmeow/appstate"
waProto "go.mau.fi/whatsmeow/binary/proto"
"go.mau.fi/whatsmeow/store"
_ "modernc.org/sqlite"
// "go.mau.fi/whatsmeow/store/sqlstore"
"go.mau.fi/whatsmeow/types"
"go.mau.fi/whatsmeow/types/events"
waLog "go.mau.fi/whatsmeow/util/log"
//"google.golang.org/protobuf/proto"
)
// var wlog waLog.Logger
var clientPointer = make(map[int]*whatsmeow.Client)
var clientHttp = make(map[int]*resty.Client)
var historySyncID int32
type MyClient struct {
WAClient *whatsmeow.Client
eventHandlerID uint32
userID int
token string
subscriptions []string
db *sql.DB
}
// Connects to Whatsapp Websocket on server startup if last state was connected
func (s *server) connectOnStartup() {
rows, err := s.db.Query("SELECT id,token,jid,webhook,events FROM users WHERE connected=1")
if err != nil {
log.Error().Err(err).Msg("DB Problem")
return
}
defer rows.Close()
for rows.Next() {
txtid := ""
token := ""
jid := ""
webhook := ""
events := ""
err = rows.Scan(&txtid, &token, &jid, &webhook, &events)
if err != nil {
log.Error().Err(err).Msg("DB Problem")
return
} else {
log.Info().Str("token", token).Msg("Connect to Whatsapp on startup")
v := Values{map[string]string{
"Id": txtid,
"Jid": jid,
"Webhook": webhook,
"Token": token,
"Events": events,
}}
userinfocache.Set(token, v, cache.NoExpiration)
userid, _ := strconv.Atoi(txtid)
// Gets and set subscription to webhook events
eventarray := strings.Split(events, ",")
var subscribedEvents []string
if len(eventarray) < 1 {
if !Find(subscribedEvents, "All") {
subscribedEvents = append(subscribedEvents, "All")
}
} else {
for _, arg := range eventarray {
if !Find(messageTypes, arg) {
log.Warn().Str("Type", arg).Msg("Message type discarded")
continue
}
if !Find(subscribedEvents, arg) {
subscribedEvents = append(subscribedEvents, arg)
}
}
}
eventstring := strings.Join(subscribedEvents, ",")
log.Info().Str("events", eventstring).Str("jid", jid).Msg("Attempt to connect")
killchannel[userid] = make(chan bool)
go s.startClient(userid, jid, token, subscribedEvents)
}
}
err = rows.Err()
if err != nil {
log.Error().Err(err).Msg("DB Problem")
}
}
func parseJID(arg string) (types.JID, bool) {
if arg[0] == '+' {
arg = arg[1:]
}
if !strings.ContainsRune(arg, '@') {
return types.NewJID(arg, types.DefaultUserServer), true
} else {
recipient, err := types.ParseJID(arg)
if err != nil {
log.Error().Err(err).Msg("Invalid JID")
return recipient, false
} else if recipient.User == "" {
log.Error().Err(err).Msg("Invalid JID no server specified")
return recipient, false
}
return recipient, true
}
}
func (s *server) startClient(userID int, textjid string, token string, subscriptions []string) {
log.Info().Str("userid", strconv.Itoa(userID)).Str("jid", textjid).Msg("Starting websocket connection to Whatsapp")
var deviceStore *store.Device
var err error
if clientPointer[userID] != nil {
isConnected := clientPointer[userID].IsConnected()
if isConnected == true {
return
}
}
if textjid != "" {
jid, _ := parseJID(textjid)
// If you want multiple sessions, remember their JIDs and use .GetDevice(jid) or .GetAllDevices() instead.
//deviceStore, err := container.GetFirstDevice()
deviceStore, err = container.GetDevice(jid)
if err != nil {
panic(err)
}
} else {
log.Warn().Msg("No jid found. Creating new device")
deviceStore = container.NewDevice()
}
if deviceStore == nil {
log.Warn().Msg("No store found. Creating new one")
deviceStore = container.NewDevice()
}
//store.CompanionProps.PlatformType = waProto.CompanionProps_CHROME.Enum()
//store.CompanionProps.Os = proto.String("Mac OS")
osName := "macOS"
store.DeviceProps.PlatformType = waProto.DeviceProps_CHROME.Enum()
store.DeviceProps.Os = &osName
clientLog := waLog.Stdout("Client", *waDebug, *colorOutput)
var client *whatsmeow.Client
if *waDebug != "" {
client = whatsmeow.NewClient(deviceStore, clientLog)
} else {
client = whatsmeow.NewClient(deviceStore, nil)
}
clientPointer[userID] = client
mycli := MyClient{client, 1, userID, token, subscriptions, s.db}
mycli.eventHandlerID = mycli.WAClient.AddEventHandler(mycli.myEventHandler)
//clientHttp[userID] = resty.New().EnableTrace()
clientHttp[userID] = resty.New()
clientHttp[userID].SetRedirectPolicy(resty.FlexibleRedirectPolicy(15))
if *waDebug == "DEBUG" {
clientHttp[userID].SetDebug(true)
}
clientHttp[userID].SetTimeout(5 * time.Second)
clientHttp[userID].SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true})
clientHttp[userID].OnError(func(req *resty.Request, err error) {
if v, ok := err.(*resty.ResponseError); ok {
// v.Response contains the last response from the server
// v.Err contains the original error
log.Debug().Str("response", v.Response.String()).Msg("resty error")
log.Error().Err(v.Err).Msg("resty error")
}
})
if client.Store.ID == nil {
// No ID stored, new login
qrChan, err := client.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.Error().Err(err).Msg("Failed to get QR channel")
}
} else {
err = client.Connect() // Si no conectamos no se puede generar QR
if err != nil {
panic(err)
}
for evt := range qrChan {
if evt.Event == "code" {
// Display QR code in terminal (useful for testing/developing)
if *logType != "json" {
qrterminal.GenerateHalfBlock(evt.Code, qrterminal.L, os.Stdout)
fmt.Println("QR code:\n", evt.Code)
}
// Store encoded/embeded base64 QR on database for retrieval with the /qr endpoint
image, _ := qrcode.Encode(evt.Code, qrcode.Medium, 256)
base64qrcode := "data:image/png;base64," + base64.StdEncoding.EncodeToString(image)
sqlStmt := `UPDATE users SET qrcode=? WHERE id=?`
_, err := s.db.Exec(sqlStmt, base64qrcode, userID)
if err != nil {
log.Error().Err(err).Msg(sqlStmt)
}
} else if evt.Event == "timeout" {
// Clear QR code from DB on timeout
sqlStmt := `UPDATE users SET qrcode=? WHERE id=?`
_, err := s.db.Exec(sqlStmt, "", userID)
if err != nil {
log.Error().Err(err).Msg(sqlStmt)
}
log.Warn().Msg("QR timeout killing channel")
delete(clientPointer, userID)
killchannel[userID] <- true
} else if evt.Event == "success" {
log.Info().Msg("QR pairing ok!")
// Clear QR code after pairing
sqlStmt := `UPDATE users SET qrcode=? WHERE id=?`
_, err := s.db.Exec(sqlStmt, "", userID)
if err != nil {
log.Error().Err(err).Msg(sqlStmt)
}
} else {
log.Info().Str("event", evt.Event).Msg("Login event")
}
}
}
} else {
// Already logged in, just connect
log.Info().Msg("Already logged in, just connect")
err = client.Connect()
if err != nil {
panic(err)
}
}
// Keep connected client live until disconnected/killed
for {
select {
case <-killchannel[userID]:
log.Info().Str("userid", strconv.Itoa(userID)).Msg("Received kill signal")
client.Disconnect()
delete(clientPointer, userID)
sqlStmt := `UPDATE users SET connected=0 WHERE id=?`
_, err := s.db.Exec(sqlStmt, userID)
if err != nil {
log.Error().Err(err).Msg(sqlStmt)
}
return
default:
time.Sleep(1000 * time.Millisecond)
//log.Info().Str("jid",textjid).Msg("Loop the loop")
}
}
}
func (mycli *MyClient) myEventHandler(rawEvt interface{}) {
// txtid := strconv.Itoa(mycli.userID)
postmap := make(map[string]interface{})
postmap["event"] = rawEvt
dowebhook := 0
path := ""
// ex, err := os.Executable()
// if err != nil {
// panic(err)
// }
// exPath := filepath.Dir(ex)
switch evt := rawEvt.(type) {
case *events.AppStateSyncComplete:
if len(mycli.WAClient.Store.PushName) > 0 && evt.Name == appstate.WAPatchCriticalBlock {
err := mycli.WAClient.SendPresence(types.PresenceAvailable)
if err != nil {
log.Warn().Err(err).Msg("Failed to send available presence")
} else {
log.Info().Msg("Marked self as available")
}
}
case *events.Connected, *events.PushNameSetting:
if len(mycli.WAClient.Store.PushName) == 0 {
return
}
// Send presence available when connecting and when the pushname is changed.
// This makes sure that outgoing messages always have the right pushname.
err := mycli.WAClient.SendPresence(types.PresenceAvailable)
if err != nil {
log.Warn().Err(err).Msg("Failed to send available presence")
} else {
log.Info().Msg("Marked self as available")
}
sqlStmt := `UPDATE users SET connected=1 WHERE id=?`
_, err = mycli.db.Exec(sqlStmt, mycli.userID)
if err != nil {
log.Error().Err(err).Msg(sqlStmt)
return
}
case *events.PairSuccess:
log.Info().Str("userid", strconv.Itoa(mycli.userID)).Str("token", mycli.token).Str("ID", evt.ID.String()).Str("BusinessName", evt.BusinessName).Str("Platform", evt.Platform).Msg("QR Pair Success")
jid := evt.ID
sqlStmt := `UPDATE users SET jid=? WHERE id=?`
_, err := mycli.db.Exec(sqlStmt, jid, mycli.userID)
if err != nil {
log.Error().Err(err).Msg(sqlStmt)
return
}
myuserinfo, found := userinfocache.Get(mycli.token)
if !found {
log.Warn().Msg("No user info cached on pairing?")
} else {
txtid := myuserinfo.(Values).Get("Id")
token := myuserinfo.(Values).Get("Token")
v := updateUserInfo(myuserinfo, "Jid", fmt.Sprintf("%s", jid))
userinfocache.Set(token, v, cache.NoExpiration)
log.Info().Str("jid", jid.String()).Str("userid", txtid).Str("token", token).Msg("User information set")
}
case *events.StreamReplaced:
log.Info().Msg("Received StreamReplaced event")
return
case *events.Message:
postmap["type"] = "Message"
dowebhook = 0
metaParts := []string{fmt.Sprintf("pushname: %s", evt.Info.PushName), fmt.Sprintf("timestamp: %s", evt.Info.Timestamp)}
if evt.Info.Type != "" {
metaParts = append(metaParts, fmt.Sprintf("type: %s", evt.Info.Type))
}
if evt.Info.Category != "" {
metaParts = append(metaParts, fmt.Sprintf("category: %s", evt.Info.Category))
}
if evt.IsViewOnce {
metaParts = append(metaParts, "view once")
}
if evt.IsViewOnce {
metaParts = append(metaParts, "ephemeral")
}
log.Info().Str("id", evt.Info.ID).Str("source", evt.Info.SourceString()).Str("parts", strings.Join(metaParts, ", ")).Msg("Message Received")
// // try to get Image if any
// img := evt.Message.GetImageMessage()
// if img != nil {
// // check/creates user directory for files
// userDirectory := filepath.Join(exPath, "files", "user_"+txtid)
// _, err := os.Stat(userDirectory)
// if os.IsNotExist(err) {
// errDir := os.MkdirAll(userDirectory, 0751)
// if errDir != nil {
// log.Error().Err(errDir).Msg("Could not create user directory")
// return
// }
// }
// data, err := mycli.WAClient.Download(img)
// if err != nil {
// log.Error().Err(err).Msg("Failed to download image")
// return
// }
// exts, _ := mime.ExtensionsByType(img.GetMimetype())
// path = filepath.Join(userDirectory, evt.Info.ID+exts[0])
// err = os.WriteFile(path, data, 0600)
// if err != nil {
// log.Error().Err(err).Msg("Failed to save image")
// return
// }
// log.Info().Str("path", path).Msg("Image saved")
// }
// // try to get Audio if any
// audio := evt.Message.GetAudioMessage()
// if audio != nil {
// // check/creates user directory for files
// userDirectory := filepath.Join(exPath, "files", "user_"+txtid)
// _, err := os.Stat(userDirectory)
// if os.IsNotExist(err) {
// errDir := os.MkdirAll(userDirectory, 0751)
// if errDir != nil {
// log.Error().Err(errDir).Msg("Could not create user directory")
// return
// }
// }
// data, err := mycli.WAClient.Download(audio)
// if err != nil {
// log.Error().Err(err).Msg("Failed to download audio")
// return
// }
// exts, _ := mime.ExtensionsByType(audio.GetMimetype())
// path = filepath.Join(userDirectory, evt.Info.ID+exts[0])
// err = os.WriteFile(path, data, 0600)
// if err != nil {
// log.Error().Err(err).Msg("Failed to save audio")
// return
// }
// log.Info().Str("path", path).Msg("Audio saved")
// }
// // try to get Document if any
// document := evt.Message.GetDocumentMessage()
// if document != nil {
// // check/creates user directory for files
// userDirectory := filepath.Join(exPath, "files", "user_"+txtid)
// _, err := os.Stat(userDirectory)
// if os.IsNotExist(err) {
// errDir := os.MkdirAll(userDirectory, 0751)
// if errDir != nil {
// log.Error().Err(errDir).Msg("Could not create user directory")
// return
// }
// }
// data, err := mycli.WAClient.Download(document)
// if err != nil {
// log.Error().Err(err).Msg("Failed to download document")
// return
// }
// extension := ""
// exts, err := mime.ExtensionsByType(document.GetMimetype())
// if err != nil {
// extension = exts[0]
// } else {
// filename := document.FileName
// extension = filepath.Ext(*filename)
// }
// path = filepath.Join(userDirectory, evt.Info.ID+extension)
// err = os.WriteFile(path, data, 0600)
// if err != nil {
// log.Error().Err(err).Msg("Failed to save document")
// return
// }
// log.Info().Str("path", path).Msg("Document saved")
// }
case *events.Receipt:
postmap["type"] = "ReadReceipt"
dowebhook = 1
if evt.Type == events.ReceiptTypeRead || evt.Type == events.ReceiptTypeReadSelf {
log.Info().Strs("id", evt.MessageIDs).Str("source", evt.SourceString()).Str("timestamp", fmt.Sprintf("%d", evt.Timestamp)).Msg("Message was read")
if evt.Type == events.ReceiptTypeRead {
postmap["state"] = "Read"
} else {
postmap["state"] = "ReadSelf"
}
} else if evt.Type == events.ReceiptTypeDelivered {
postmap["state"] = "Delivered"
log.Info().Str("id", evt.MessageIDs[0]).Str("source", evt.SourceString()).Str("timestamp", fmt.Sprintf("%d", evt.Timestamp)).Msg("Message delivered")
} else {
// Discard webhooks for inactive or other delivery types
return
}
case *events.Presence:
postmap["type"] = "Presence"
dowebhook = 1
if evt.Unavailable {
postmap["state"] = "offline"
if evt.LastSeen.IsZero() {
log.Info().Str("from", evt.From.String()).Msg("User is now offline")
} else {
log.Info().Str("from", evt.From.String()).Str("lastSeen", fmt.Sprintf("%d", evt.LastSeen)).Msg("User is now offline")
}
} else {
postmap["state"] = "online"
log.Info().Str("from", evt.From.String()).Msg("User is now online")
}
case *events.HistorySync:
// postmap["type"] = "HistorySync"
// dowebhook = 1
// // check/creates user directory for files
// userDirectory := filepath.Join(exPath, "files", "user_"+txtid)
// _, err := os.Stat(userDirectory)
// if os.IsNotExist(err) {
// errDir := os.MkdirAll(userDirectory, 0751)
// if errDir != nil {
// log.Error().Err(errDir).Msg("Could not create user directory")
// return
// }
// }
// id := atomic.AddInt32(&historySyncID, 1)
// fileName := filepath.Join(userDirectory, "history-"+strconv.Itoa(int(id))+".json")
// file, err := os.OpenFile(fileName, os.O_WRONLY|os.O_CREATE, 0600)
// if err != nil {
// log.Error().Err(err).Msg("Failed to open file to write history sync")
// return
// }
// enc := json.NewEncoder(file)
// enc.SetIndent("", " ")
// err = enc.Encode(evt.Data)
// if err != nil {
// log.Error().Err(err).Msg("Failed to write history sync")
// return
// }
log.Info().Str("History Sync", evt.Data.String()).Msg("Wrote history sync")
// _ = file.Close()
case *events.AppState:
log.Info().Str("index", fmt.Sprintf("%+v", evt.Index)).Str("actionValue", fmt.Sprintf("%+v", evt.SyncActionValue)).Msg("App state event received")
case *events.LoggedOut:
log.Info().Str("reason", evt.Reason.String()).Msg("Logged out")
killchannel[mycli.userID] <- true
sqlStmt := `UPDATE users SET connected=0 WHERE id=?`
_, err := mycli.db.Exec(sqlStmt, mycli.userID)
if err != nil {
log.Error().Err(err).Msg(sqlStmt)
return
}
case *events.ChatPresence:
postmap["type"] = "ChatPresence"
dowebhook = 1
log.Info().Str("state", fmt.Sprintf("%s", evt.State)).Str("media", fmt.Sprintf("%s", evt.Media)).Str("chat", evt.MessageSource.Chat.String()).Str("sender", evt.MessageSource.Sender.String()).Msg("Chat Presence received")
case *events.CallOffer:
log.Info().Str("event", fmt.Sprintf("%+v", evt)).Msg("Got call offer")
case *events.CallAccept:
log.Info().Str("event", fmt.Sprintf("%+v", evt)).Msg("Got call accept")
case *events.CallTerminate:
log.Info().Str("event", fmt.Sprintf("%+v", evt)).Msg("Got call terminate")
case *events.CallOfferNotice:
log.Info().Str("event", fmt.Sprintf("%+v", evt)).Msg("Got call offer notice")
case *events.CallRelayLatency:
log.Info().Str("event", fmt.Sprintf("%+v", evt)).Msg("Got call relay latency")
default:
log.Warn().Str("event", fmt.Sprintf("%+v", evt)).Msg("Unhandled event")
}
if dowebhook == 1 {
// call webhook
webhookurl := ""
myuserinfo, found := userinfocache.Get(mycli.token)
if !found {
log.Warn().Str("token", mycli.token).Msg("Could not call webhook as there is no user for this token")
} else {
webhookurl = myuserinfo.(Values).Get("Webhook")
}
if !Find(mycli.subscriptions, postmap["type"].(string)) && !Find(mycli.subscriptions, "All") {
log.Warn().Str("type", postmap["type"].(string)).Msg("Skipping webhook. Not subscribed for this type")
return
}
if webhookurl != "" {
log.Info().Str("url", webhookurl).Msg("Calling webhook")
values, _ := json.Marshal(postmap)
data := map[string]string{
"jsonData": string(values),
"token": mycli.token,
}
if path == "" {
go callHook(webhookurl, data, mycli.userID)
} else {
// Create a channel to capture error from the goroutine
errChan := make(chan error, 1)
go func() {
err := callHookFile(webhookurl, data, mycli.userID, path)
errChan <- err
}()
// Optionally handle the error from the channel
if err := <-errChan; err != nil {
log.Error().Err(err).Msg("Error calling hook file")
}
}
} else {
log.Warn().Str("userid", strconv.Itoa(mycli.userID)).Msg("No webhook set for user")
}
}
}