Skip to content

Commit d97e42e

Browse files
committed
mclient: init
1 parent 1385a4f commit d97e42e

File tree

4 files changed

+101
-0
lines changed

4 files changed

+101
-0
lines changed

pkg/connector/client.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (wa *WhatsAppConnector) LoadUserLogin(ctx context.Context, login *bridgev2.
4646
w := &WhatsAppClient{
4747
Main: wa,
4848
UserLogin: login,
49+
MC: noopMCInstance,
4950

5051
historySyncs: make(chan *waHistorySync.HistorySync, 64),
5152
historySyncWakeup: make(chan struct{}, 1),
@@ -103,6 +104,7 @@ type WhatsAppClient struct {
103104
Client *whatsmeow.Client
104105
Device *store.Device
105106
JID types.JID
107+
MC mClient
106108

107109
historySyncs chan *waHistorySync.HistorySync
108110
historySyncWakeup chan struct{}
@@ -196,6 +198,7 @@ func (wa *WhatsAppClient) Connect(ctx context.Context) {
196198
if err := wa.Main.updateProxy(ctx, wa.Client, false); err != nil {
197199
zerolog.Ctx(ctx).Err(err).Msg("Failed to update proxy")
198200
}
201+
wa.initMC()
199202
wa.startLoops()
200203
wa.Client.BackgroundEventCtx = wa.UserLogin.Log.WithContext(wa.Main.Bridge.BackgroundCtx)
201204
if err := wa.Client.Connect(); err != nil {

pkg/connector/handlewhatsapp.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"go.mau.fi/util/ptr"
2828
"go.mau.fi/whatsmeow/appstate"
2929
"go.mau.fi/whatsmeow/proto/waE2E"
30+
"go.mau.fi/whatsmeow/store"
3031
"go.mau.fi/whatsmeow/types"
3132
"go.mau.fi/whatsmeow/types/events"
3233
"maunium.net/go/mautrix/bridgev2"
@@ -170,6 +171,7 @@ func (wa *WhatsAppClient) handleWAEvent(rawEvt any) (success bool) {
170171
}()
171172
go wa.syncRemoteProfile(log.WithContext(context.Background()), nil)
172173
}
174+
wa.MC.OnConnect(store.GetWAVersion()[2], wa.Device.Platform)
173175
case *events.OfflineSyncPreview:
174176
log.Info().
175177
Int("message_count", evt.Messages).

pkg/connector/mclient.go

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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+
}

pkg/waid/dbmeta.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type UserLoginMetadata struct {
3838
LoggedInAt jsontime.Unix `json:"logged_in_at,omitempty"`
3939

4040
HistorySyncPortalsNeedCreating bool `json:"history_sync_portals_need_creating,omitempty"`
41+
42+
MData json.RawMessage `json:"mdata,omitempty"`
4143
}
4244

4345
type PushKeys struct {

0 commit comments

Comments
 (0)