diff --git a/CHANGELOG.md b/CHANGELOG.md index c01c0f5..5ae08a9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -22,6 +22,11 @@ (pacote inexistente); `ClientPayload_UserAgent_AppVersion` vem de `go.mau.fi/whatsmeow/proto/waWa6`. +- **Nono dígito (BR)** — envios para celulares brasileiros consultam no + WhatsApp as duas variantes (com e sem o 9) e usam a que está registrada. + Contas antigas sem o 9 (ex.: `554291389691`) passam a receber mesmo quando o + sistema envia `5542991389691`, e vice-versa (upstream issue #214 / PR #208). + ### 🛡️ Estabilidade (correções da comunidade incorporadas pelo EvoGO) - **Crash `concurrent map writes`** — `clientPointer`, `myClientPointer` e `killChannel` agora são `safemap.Map` com RWMutex (upstream PR #196, issue #203). diff --git a/pkg/sendMessage/service/send_service.go b/pkg/sendMessage/service/send_service.go index 245a9c6..48462dd 100644 --- a/pkg/sendMessage/service/send_service.go +++ b/pkg/sendMessage/service/send_service.go @@ -531,6 +531,16 @@ func (s *sendService) validateAndCheckUserExists(phone string, formatJid *bool, return types.NewJID("", types.DefaultUserServer), fmt.Errorf("failed to connect client: %v", err) } + // Brazilian mobiles: ask WhatsApp for both forms (with and without the 9th + // digit) and use the one that is registered. + if variants := utils.BRPhoneVariants(phone); len(variants) > 1 { + if remoteJID, found := s.checkBRPhoneVariants(client, variants, instance.Id); found { + s.loggerWrapper.GetLogger(instance.Id).LogInfo("[%s] Number %s verified as valid WhatsApp user, using remoteJID: %s", instance.Id, phone, remoteJID) + formatJidFalse := false + return validateMessageFields(remoteJID, &formatJidFalse, messageID, participant) + } + } + // Use CheckUser approach: formatJid=false by default formatJidForCheck := false @@ -592,6 +602,40 @@ func (s *sendService) checkSingleUserExists(client *whatsmeow.Client, phone stri return remoteJID, true, nil } +// checkBRPhoneVariants queries every variant in a single IsOnWhatsApp call and +// returns the JID of the first one that is registered, in the given order. +// Errors are logged and reported as not found so the regular check still runs. +func (s *sendService) checkBRPhoneVariants(client *whatsmeow.Client, variants []string, instanceId string) (string, bool) { + phones := make([]string, len(variants)) + for i, v := range variants { + phones[i] = "+" + v + } + + ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second) + defer cancel() + + resp, err := client.IsOnWhatsApp(ctx, phones) + if err != nil { + s.loggerWrapper.GetLogger(instanceId).LogWarn("[%s] Failed to check BR number variants %v: %v", instanceId, variants, err) + return "", false + } + + for _, v := range variants { + for _, r := range resp { + if r.IsIn && strings.TrimPrefix(r.Query, "+") == v { + return r.JID.String(), true + } + } + } + // Query not echoed back as sent: any registered result is one of our variants. + for _, r := range resp { + if r.IsIn { + return r.JID.String(), true + } + } + return "", false +} + func findURL(text string) string { urlRegex := `http[s]?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\\(\\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+` re := regexp.MustCompile(urlRegex) diff --git a/pkg/utils/utils.go b/pkg/utils/utils.go index 1cb9338..ca05302 100644 --- a/pkg/utils/utils.go +++ b/pkg/utils/utils.go @@ -187,6 +187,49 @@ func formatBRNumber(jid string) string { return jid } +// BRPhoneVariants returns both forms of a Brazilian mobile number — with and +// without the 9th digit — with the received form first, or nil when the number +// is not a Brazilian mobile. +// +// Accounts created before the 9th digit rollout are still registered without +// it (e.g. 554291389691), newer ones with it (5542991389691). No fixed rule +// derived from the DDD is right for every account, so callers should ask +// WhatsApp which of the two exists. +func BRPhoneVariants(number string) []string { + if strings.Contains(number, "@") { + if !strings.HasSuffix(number, "@"+whatsmeow_types.DefaultUserServer) { + return nil + } + number = strings.Split(number, "@")[0] + } + number = strings.Split(number, ":")[0] + + digits := strings.Map(func(r rune) rune { + if r >= '0' && r <= '9' { + return r + } + return -1 + }, number) + + // 55 + DDD (11-99, never starting with 0) + subscriber + if !strings.HasPrefix(digits, "55") || len(digits) < 12 || digits[2] == '0' { + return nil + } + + switch len(digits) { + case 13: + if digits[4] == '9' { + return []string{digits, digits[:4] + digits[5:]} + } + case 12: + // 8-digit mobile numbers start with 6, 7, 8 or 9; landlines with 2-5 + if digits[4] >= '6' { + return []string{digits, digits[:4] + "9" + digits[4:]} + } + } + return nil +} + // ParseJID parses a number string into a WhatsApp JID with validation func ParseJID(arg string) (whatsmeow_types.JID, bool) { if arg == "" { diff --git a/pkg/utils/utils_test.go b/pkg/utils/utils_test.go index 31cde21..7191ba2 100644 --- a/pkg/utils/utils_test.go +++ b/pkg/utils/utils_test.go @@ -384,3 +384,34 @@ func TestCanonicalJID(t *testing.T) { }) } } + +func TestBRPhoneVariants(t *testing.T) { + tests := []struct { + name string + input string + want []string + }{ + {"13 digits with 9", "5542991389691", []string{"5542991389691", "554291389691"}}, + {"12 digits without 9", "554291389691", []string{"554291389691", "5542991389691"}}, + {"plus and JID suffix", "+5511987654321@s.whatsapp.net", []string{"5511987654321", "551187654321"}}, + {"formatted", "+55 (42) 99138-9691", []string{"5542991389691", "554291389691"}}, + {"landline", "554232221234", nil}, + {"not Brazil", "15551234567", nil}, + {"group", "120363123456789012@g.us", nil}, + {"lid", "255284433936406@lid", nil}, + {"13 digits without 9 after DDD", "5542891389691", nil}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := BRPhoneVariants(tt.input) + if len(got) != len(tt.want) { + t.Fatalf("BRPhoneVariants(%q) = %v, want %v", tt.input, got, tt.want) + } + for i := range got { + if got[i] != tt.want[i] { + t.Fatalf("BRPhoneVariants(%q) = %v, want %v", tt.input, got, tt.want) + } + } + }) + } +}