|
| 1 | +// mautrix-whatsapp - A Matrix-WhatsApp puppeting bridge. |
| 2 | +// Copyright (C) 2025 Tulir Asokan |
| 3 | +// |
| 4 | +// This program is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Affero General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// This program is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Affero General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Affero General Public License |
| 15 | +// along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package connector |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "encoding/json" |
| 22 | + "os" |
| 23 | + "plugin" |
| 24 | + "time" |
| 25 | + |
| 26 | + "go.mau.fi/util/exerrors" |
| 27 | + "go.mau.fi/whatsmeow" |
| 28 | + waBinary "go.mau.fi/whatsmeow/binary" |
| 29 | + "go.mau.fi/whatsmeow/types" |
| 30 | + |
| 31 | + "go.mau.fi/mautrix-whatsapp/pkg/waid" |
| 32 | +) |
| 33 | + |
| 34 | +type newMCFunc func(json.RawMessage, mWAClient) mClient |
| 35 | + |
| 36 | +var newMC newMCFunc |
| 37 | + |
| 38 | +func init() { |
| 39 | + path := os.Getenv("WM_PLUGIN_PATH") |
| 40 | + if path == "" { |
| 41 | + return |
| 42 | + } |
| 43 | + plug := exerrors.Must(plugin.Open(path)) |
| 44 | + sym := exerrors.Must(plug.Lookup("NewClient")) |
| 45 | + newMC = sym.(newMCFunc) |
| 46 | +} |
| 47 | + |
| 48 | +func (wa *WhatsAppClient) initMC() { |
| 49 | + if newMC != nil { |
| 50 | + wa.MC = newMC(wa.UserLogin.Metadata.(*waid.UserLoginMetadata).MData, wa) |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +type mClient = interface { |
| 55 | + OnConnect(version uint32, platform string) |
| 56 | +} |
| 57 | + |
| 58 | +type noopMC struct{} |
| 59 | + |
| 60 | +var noopMCInstance mClient = &noopMC{} |
| 61 | + |
| 62 | +func (n *noopMC) OnConnect(version uint32, platform string) {} |
| 63 | + |
| 64 | +type mWAClient = interface { |
| 65 | + MSend(data []byte) |
| 66 | + MSave(data json.RawMessage) |
| 67 | +} |
| 68 | + |
| 69 | +var _ mWAClient = (*WhatsAppClient)(nil) |
| 70 | + |
| 71 | +// Deprecated: ignore DangerousInternal error |
| 72 | +func (wa *WhatsAppClient) MSend(bytes []byte) { |
| 73 | + _, err := wa.Client.DangerousInternals().SendIQAsync(whatsmeow.DangerousInfoQuery{ |
| 74 | + Namespace: "w:stats", |
| 75 | + Type: "set", |
| 76 | + To: types.ServerJID, |
| 77 | + Content: []waBinary.Node{{ |
| 78 | + Tag: "add", |
| 79 | + Attrs: waBinary.Attrs{"t": time.Now().Unix()}, |
| 80 | + Content: bytes, |
| 81 | + }}, |
| 82 | + }) |
| 83 | + if err != nil { |
| 84 | + wa.UserLogin.Log.Err(err).Msg("Failed to send stats") |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +func (wa *WhatsAppClient) MSave(s json.RawMessage) { |
| 89 | + wa.UserLogin.Metadata.(*waid.UserLoginMetadata).MData = s |
| 90 | + err := wa.UserLogin.Save(context.Background()) |
| 91 | + if err != nil { |
| 92 | + wa.UserLogin.Log.Err(err).Msg("Failed to save MC data") |
| 93 | + } |
| 94 | +} |
0 commit comments