diff --git a/lib/wmchat/go/gowm.go b/lib/wmchat/go/gowm.go index 15f3bdd9..9cc36f1c 100644 --- a/lib/wmchat/go/gowm.go +++ b/lib/wmchat/go/gowm.go @@ -20,6 +20,7 @@ import ( "os" "os/exec" "path/filepath" + "regexp" "runtime" "runtime/debug" "slices" @@ -1592,6 +1593,92 @@ func (handler *WmEventHandler) HandleMessage(messageInfo types.MessageInfo, msg case msg.PinInChatMessage != nil: handler.HandlePinInChatMessage(messageInfo, msg) + case msg.ContactMessage != nil: + displayName := msg.ContactMessage.GetDisplayName() + vcard := msg.ContactMessage.GetVcard() + + reTel := regexp.MustCompile(`(?i)TEL[^:]*:(.+)`) + telMatches := reTel.FindAllStringSubmatch(vcard, -1) + + var phones []string + for _, match := range telMatches { + if len(match) > 1 { + // Clean up any carriage returns or spaces + phones = append(phones, strings.TrimSpace(match[1])) + } + } + + reEmail := regexp.MustCompile(`(?i)EMAIL[^:]*:(.+)`) + emailMatches := reEmail.FindAllStringSubmatch(vcard, -1) + + var emails []string + for _, match := range emailMatches { + if len(match) > 1 { + emails = append(emails, strings.TrimSpace(match[1])) + } + } + + // Construct the text display layout for nchat + var sb strings.Builder + sb.WriteString(fmt.Sprintf("[Contact Card] %s", displayName)) + + if len(phones) > 0 { + sb.WriteString(fmt.Sprintf("\nPhone: %s", strings.Join(phones, ", "))) + } + if len(emails) > 0 { + sb.WriteString(fmt.Sprintf("\nEmail: %s", strings.Join(emails, ", "))) + } + + contactText := sb.String() + msg.Conversation = &contactText + handler.HandleTextMessage(messageInfo, msg, isSyncRead) + + case msg.ContactsArrayMessage != nil: + var sb strings.Builder + sb.WriteString("[Multiple Contact Cards]") + + // Loop through every contact in the array + for _, contact := range msg.ContactsArrayMessage.Contacts { + displayName := contact.GetDisplayName() + vcard := contact.GetVcard() + + if displayName == "" && vcard == "" { + continue + } + + // Parse phone numbers from this specific contact's vcard + reTel := regexp.MustCompile(`(?i)TEL[^:]*:(.+)`) + telMatches := reTel.FindAllStringSubmatch(vcard, -1) + var phones []string + for _, match := range telMatches { + if len(match) > 1 { + phones = append(phones, strings.TrimSpace(match[1])) + } + } + + // Parse emails from this specific contact's vcard + reEmail := regexp.MustCompile(`(?i)EMAIL[^:]*:(.+)`) + emailMatches := reEmail.FindAllStringSubmatch(vcard, -1) + var emails []string + for _, match := range emailMatches { + if len(match) > 1 { + emails = append(emails, strings.TrimSpace(match[1])) + } + } + + // Format this individual contact entry + sb.WriteString(fmt.Sprintf("\n\n%s", displayName)) + if len(phones) > 0 { + sb.WriteString(fmt.Sprintf("\nPhone: %s", strings.Join(phones, ", "))) + } + if len(emails) > 0 { + sb.WriteString(fmt.Sprintf("\nEmail: %s", strings.Join(emails, ", "))) + } + } + + contactText := sb.String() + msg.Conversation = &contactText + handler.HandleTextMessage(messageInfo, msg, isSyncRead) default: handler.HandleUnsupportedMessage(messageInfo, msg, isSyncRead) }