package main import ( "context" "errors" "fmt" "strings" "go.mau.fi/whatsmeow" "go.mau.fi/whatsmeow/proto/waE2E" "go.mau.fi/whatsmeow/types" "google.golang.org/protobuf/proto" ) // Send sends one text the owner typed in Cotton to a chat, optionally as a reply. This is the only // file that sends a text (see readonly_test.go). His other kinds (pictures, videos, voice notes, // documents, reactions, edits, deletes for everyone, polls, votes) go from send_kinds.go, and a block or a // chat's delete from source_acts.go; nothing in the helper sends a receipt, a presence or a group act. // // messageID is chosen by Cotton and passed through unchanged: WhatsApp dedupes by id, so a send // retried after a restart with the same id is delivered once. func (h *Hub) Send(ctx context.Context, account, chat, text, messageID string, quote Quote) (map[string]any, error) { if strings.TrimSpace(text) == "" { return nil, errors.New("send needs text") } if strings.TrimSpace(messageID) == "" { return nil, errors.New("send needs a message_id") } jid, err := types.ParseJID(chat) if err != nil || jid.User == "" { return nil, fmt.Errorf("send needs a chat jid, got %q", chat) } if !h.mu.TryLock() { return nil, errors.New("busy") } held := h.held[account] h.mu.Unlock() if held { return nil, errors.New("busy") } cli, err := h.client(account) if err != nil { return nil, err } // A number with no chat yet is fine: WhatsApp starts the chat with the first message. One it does // not know is refused in words before anything is sent. jid, err = recipient(ctx, jid, cli.IsOnWhatsApp) if err != nil { return nil, err } resp, err := cli.SendMessage(ctx, jid, buildText(text, quote), whatsmeow.SendRequestExtra{ID: types.MessageID(messageID)}) if err != nil { return nil, err } // WhatsApp never echoes a message this device sent, so the helper keeps it as one of the owner's // messages itself: Cotton lands it once, under the id it went out with, like any other message. if h.outbox != nil { h.durable(sentEvent(account, jid, text, string(resp.ID), resp.Timestamp.Unix(), quote.ID)) } return map[string]any{"id": string(resp.ID), "ts": resp.Timestamp.Unix()}, nil } // NotOnWhatsApp starts the refusal for a number WhatsApp does not know; the importer turns it into // "+ is not on WhatsApp." (crates/cotton-mac/src/outbox.rs). const NotOnWhatsApp = "not_on_whatsapp" // recipient is the address a text to jid goes to. A person's number is asked of WhatsApp first (as its own // app does when a number is typed): a number it does not know is refused, and one it knows is sent to as // WhatsApp writes it, so the chat lands under the number WhatsApp keeps. A group or a hidden id goes as given. func recipient(ctx context.Context, jid types.JID, lookup func(context.Context, []string) ([]types.IsOnWhatsAppResponse, error)) (types.JID, error) { if jid.Server != types.DefaultUserServer { return jid, nil } answers, err := lookup(ctx, []string{"+" + jid.User}) if err != nil { return types.EmptyJID, fmt.Errorf("could not ask WhatsApp about +%s: %w", jid.User, err) } for _, a := range answers { if !a.IsIn { continue } if a.PhoneNumber.Server == types.DefaultUserServer && a.PhoneNumber.User != "" { return a.PhoneNumber.ToNonAD(), nil } if a.JID.Server == types.DefaultUserServer && a.JID.User != "" { return a.JID.ToNonAD(), nil } return jid.ToNonAD(), nil } return types.EmptyJID, fmt.Errorf("%s: +%s", NotOnWhatsApp, jid.User) } // sentEvent is the owner's own message as Cotton stores it, for a text this helper just sent. func sentEvent(account string, chat types.JID, text, id string, ts int64, quotedID string) *Event { chat = chat.ToNonAD() return &Event{Event: "message", Account: account, Message: &Message{ ID: id, Chat: chat.String(), FromMe: true, IsGroup: chat.Server == types.GroupServer, Timestamp: ts, Kind: "text", Text: text, QuotedID: quotedID, Origin: "live", }} } // Quote is the message a reply answers, as Cotton's copy holds it: its WhatsApp id, who wrote it (a jid; his own // account's for his own), what it is and its words (CONTRACT "Replies and reactions"). type Quote struct { ID, Sender, Kind, Text string } func quoteOf(req *Request) Quote { return Quote{ID: req.QuotedID, Sender: req.QuotedSender, Kind: req.QuotedKind, Text: req.QuotedText} } // context is the reply's ContextInfo, with what WhatsApp draws in the quote by its kind; nil when no reply. func (q Quote) context() *waE2E.ContextInfo { if q.ID == "" { return nil } var quoted *waE2E.Message switch q.Kind { case "image": quoted = &waE2E.Message{ImageMessage: &waE2E.ImageMessage{Caption: proto.String(q.Text)}} case "video": quoted = &waE2E.Message{VideoMessage: &waE2E.VideoMessage{Caption: proto.String(q.Text)}} case "voice": quoted = &waE2E.Message{AudioMessage: &waE2E.AudioMessage{PTT: proto.Bool(true)}} case "audio": quoted = &waE2E.Message{AudioMessage: &waE2E.AudioMessage{}} case "document": quoted = &waE2E.Message{DocumentMessage: &waE2E.DocumentMessage{Title: proto.String(q.Text)}} case "contact": quoted = &waE2E.Message{ContactMessage: &waE2E.ContactMessage{DisplayName: proto.String(q.Text)}} default: quoted = &waE2E.Message{Conversation: proto.String(q.Text)} } ci := &waE2E.ContextInfo{StanzaID: proto.String(q.ID), QuotedMessage: quoted} if q.Sender != "" { ci.Participant = proto.String(q.Sender) } return ci } // buildText is the message for a plain text, or for a reply when the quote names a message. func buildText(text string, quote Quote) *waE2E.Message { ci := quote.context() if ci == nil { return &waE2E.Message{Conversation: proto.String(text)} } return &waE2E.Message{ExtendedTextMessage: &waE2E.ExtendedTextMessage{Text: proto.String(text), ContextInfo: ci}} }