Skip to content

Commit 1161369

Browse files
committed
clean up and lint
1 parent 7457b9d commit 1161369

File tree

8 files changed

+35
-84
lines changed

8 files changed

+35
-84
lines changed

go.mod

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ go 1.23.0
55
toolchain go1.23.3
66

77
require (
8-
github.com/google/go-querystring v1.1.0
9-
github.com/mattn/go-colorable v0.1.13
10-
github.com/rs/zerolog v1.33.0
118
go.mau.fi/util v0.8.2
12-
golang.org/x/net v0.31.0
139
google.golang.org/protobuf v1.35.2
1410
maunium.net/go/mautrix v0.22.1-0.20241126202918-4b970e0ea7e6
1511
)
@@ -20,10 +16,12 @@ require (
2016
github.com/gorilla/mux v1.8.0 // indirect
2117
github.com/gorilla/websocket v1.5.0 // indirect
2218
github.com/lib/pq v1.10.9 // indirect
19+
github.com/mattn/go-colorable v0.1.13 // indirect
2320
github.com/mattn/go-isatty v0.0.19 // indirect
2421
github.com/mattn/go-sqlite3 v1.14.24 // indirect
2522
github.com/petermattis/goid v0.0.0-20241025130422-66cb2e6d7274 // indirect
2623
github.com/rs/xid v1.6.0 // indirect
24+
github.com/rs/zerolog v1.33.0 // indirect
2725
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
2826
github.com/tidwall/gjson v1.18.0 // indirect
2927
github.com/tidwall/match v1.1.1 // indirect
@@ -33,6 +31,7 @@ require (
3331
go.mau.fi/zeroconfig v0.1.3 // indirect
3432
golang.org/x/crypto v0.29.0 // indirect
3533
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
34+
golang.org/x/net v0.31.0 // indirect
3635
golang.org/x/sync v0.9.0 // indirect
3736
golang.org/x/sys v0.27.0 // indirect
3837
golang.org/x/text v0.20.0 // indirect

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
77
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
88
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
99
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
10-
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1110
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
1211
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
13-
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
14-
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
1512
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
1613
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
1714
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
@@ -69,7 +66,6 @@ golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
6966
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
7067
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
7168
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
72-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
7369
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
7470
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
7571
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

pkg/connector/client.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -100,21 +100,13 @@ func (c *GChatClient) onStreamEvent(ctx context.Context, raw any) {
100100
fmt.Println("Invalid event", raw)
101101
return
102102
}
103-
fmt.Println("- onEvent", evt)
104103
switch *evt.Type {
105104
case proto.Event_MESSAGE_POSTED:
106105
msg := evt.Body.GetMessagePosted().Message
107106
senderId := *msg.Creator.UserId.Id
108107
c.UserLogin.Bridge.QueueRemoteEvent(c.UserLogin, &simplevent.Message[*proto.Message]{
109108
EventMeta: simplevent.EventMeta{
110109
Type: bridgev2.RemoteEventMessage,
111-
// LogContext: func(c zerolog.Context) zerolog.Context {
112-
// return c.
113-
// Str("message_id", evtData.MessageID).
114-
// Str("sender", sender.IDStr).
115-
// Str("sender_login", sender.ScreenName).
116-
// Bool("is_from_me", isFromMe)
117-
// },
118110
PortalKey: networkid.PortalKey{
119111
ID: networkid.PortalID(evt.GroupId.String()),
120112
Receiver: c.UserLogin.ID,
@@ -127,9 +119,7 @@ func (c *GChatClient) onStreamEvent(ctx context.Context, raw any) {
127119
},
128120
// Timestamp: evtData.CreatedAt,
129121
},
130-
ID: networkid.MessageID(*msg.LocalId),
131-
// TargetMessage: networkid.MessageID(evtData.MessageID),
132-
// Data: XMDFromEventMessage(&evtData),
122+
ID: networkid.MessageID(*msg.LocalId),
133123
Data: msg,
134124
ConvertMessageFunc: func(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI, data *proto.Message) (*bridgev2.ConvertedMessage, error) {
135125
return c.convertToMatrix(ctx, portal, intent, data), nil

pkg/gchatmeow/api.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import (
55
"fmt"
66
"net/http"
77
"net/url"
8+
"strconv"
89

910
pb "google.golang.org/protobuf/proto"
1011
"google.golang.org/protobuf/reflect/protoreflect"
1112

13+
"go.mau.fi/util/ptr"
14+
1215
"go.mau.fi/mautrix-googlechat/pkg/gchatmeow/proto"
1316
)
1417

@@ -27,7 +30,7 @@ func (c *Client) gcRequest(ctx context.Context, endpoint string, requestPB proto
2730
}
2831

2932
params := url.Values{}
30-
params.Set("c", string(c.apiReqID))
33+
params.Set("c", strconv.FormatInt(c.apiReqID, 10))
3134
params.Set("rt", "b")
3235
res, err := c.baseRequest(
3336
ctx,
@@ -37,7 +40,7 @@ func (c *Client) gcRequest(ctx context.Context, endpoint string, requestPB proto
3740
requestData,
3841
headers,
3942
params,
40-
"POST",
43+
http.MethodPost,
4144
)
4245
if err != nil {
4346
return err
@@ -114,7 +117,7 @@ func (c *Client) getMembers(ctx context.Context, gcid *string) (*proto.GetMember
114117
func (c *Client) paginatedWorld(ctx context.Context) (*proto.PaginatedWorldResponse, error) {
115118
request := &proto.PaginatedWorldRequest{
116119
RequestHeader: c.gcRequestHeader,
117-
FetchFromUserSpaces: GetPointer(true),
120+
FetchFromUserSpaces: ptr.Ptr(true),
118121
FetchOptions: []proto.PaginatedWorldRequest_FetchOptions{
119122
proto.PaginatedWorldRequest_EXCLUDE_GROUP_LITE,
120123
},

pkg/gchatmeow/channel.go

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package gchatmeow
22

33
import (
4-
_ "bytes"
54
"context"
65
"encoding/base64"
76
"encoding/binary"
@@ -18,7 +17,6 @@ import (
1817
"strconv"
1918
"strings"
2019
"time"
21-
_ "unicode/utf16"
2220
"unicode/utf8"
2321

2422
"go.mau.fi/util/pblite"
@@ -205,12 +203,12 @@ func (c *Channel) Listen(ctx context.Context, maxAge time.Duration) error {
205203
}
206204

207205
if retries > 0 && !skipBackoff {
208-
backoffSeconds := time.Duration(pow(c.retryBackoffBase, retries)) * time.Second
209-
log.Printf("Backing off for %v seconds", backoffSeconds)
206+
backoffTime := time.Duration(pow(c.retryBackoffBase, retries)) * time.Second
207+
log.Printf("Backing off for %v seconds", backoffTime)
210208
select {
211209
case <-ctx.Done():
212210
return ctx.Err()
213-
case <-time.After(backoffSeconds):
211+
case <-time.After(backoffTime):
214212
}
215213
}
216214
skipBackoff = false
@@ -256,7 +254,7 @@ func (c *Channel) register(ctx context.Context) (string, error) {
256254
c.aid = 0
257255
c.ofs = 0
258256

259-
resp, err := c.session.FetchRaw(ctx, "GET", channelURLBase+"register?ignore_compass_cookie=1", nil, http.Header{
257+
resp, err := c.session.FetchRaw(ctx, http.MethodGet, channelURLBase+"register?ignore_compass_cookie=1", nil, http.Header{
260258
"Content-Type": {"application/x-protobuf"},
261259
}, true, nil)
262260
if err != nil {
@@ -281,13 +279,13 @@ func (c *Channel) register(ctx context.Context) (string, error) {
281279
return "", nil
282280
}
283281

284-
func (c *Channel) sendStreamEvent(ctx context.Context, request proto.StreamEventsRequest) error {
282+
func (c *Channel) sendStreamEvent(ctx context.Context, request *proto.StreamEventsRequest) error {
285283
params := url.Values{
286284
"VER": []string{"8"}, // channel protocol version
287285
"RID": []string{fmt.Sprintf("%d", c.rid)}, // request identifier
288286
"t": []string{"1"}, // trial
289287
"SID": []string{c.sidParam}, // session ID
290-
"AID": []string{string(c.aid)}, // last acknowledged id
288+
"AID": []string{strconv.Itoa(c.aid)}, // last acknowledged id
291289
}
292290
c.rid++
293291

@@ -296,7 +294,7 @@ func (c *Channel) sendStreamEvent(ctx context.Context, request proto.StreamEvent
296294
"Content-Type": []string{"application/x-www-form-urlencoded"},
297295
}
298296

299-
protoBytes, err := pblite.Marshal(&request)
297+
protoBytes, err := pblite.Marshal(request)
300298
if err != nil {
301299
return fmt.Errorf("failed to marshal events request: %w", err)
302300
}
@@ -314,7 +312,7 @@ func (c *Channel) sendStreamEvent(ctx context.Context, request proto.StreamEvent
314312
}
315313
c.ofs++
316314

317-
res, err := c.session.FetchRaw(ctx, "POST", channelURLBase+"events", params, headers, true, []byte(formData.Encode()))
315+
res, err := c.session.FetchRaw(ctx, http.MethodPost, channelURLBase+"events", params, headers, true, []byte(formData.Encode()))
318316
if err != nil {
319317
return err
320318
}
@@ -328,7 +326,7 @@ func (c *Channel) sendInitialPing(ctx context.Context) error {
328326
interactiveState := proto.PingEvent_INTERACTIVE
329327
notificationsEnabled := true
330328
event := proto.PingEvent{State: &state, ApplicationFocusState: &focusState, ClientInteractiveState: &interactiveState, ClientNotificationsEnabled: &notificationsEnabled}
331-
return c.sendStreamEvent(ctx, proto.StreamEventsRequest{PingEvent: &event})
329+
return c.sendStreamEvent(ctx, &proto.StreamEventsRequest{PingEvent: &event})
332330
}
333331

334332
// longPollRequest opens a long-polling request and receives arrays
@@ -353,7 +351,7 @@ func (c *Channel) longPollRequest(ctx context.Context) error {
353351
params.Set("SID", c.sidParam)
354352
}
355353

356-
resp, err := c.session.FetchRaw(ctx, "GET", channelURLBase+"events", params, http.Header{
354+
resp, err := c.session.FetchRaw(ctx, http.MethodGet, channelURLBase+"events", params, http.Header{
357355
"referer": {"https://chat.google.com/"},
358356
}, true, nil)
359357
if err != nil {
@@ -389,14 +387,14 @@ func (c *Channel) longPollRequest(ctx context.Context) error {
389387
"VER": []string{"8"},
390388
"RID": []string{"rpc"},
391389
"SID": []string{c.sidParam},
392-
"AID": []string{string(c.aid)},
390+
"AID": []string{strconv.Itoa(c.aid)},
393391
"CI": []string{"0"},
394392
"TYPE": []string{"xmlhttp"},
395393
"zx": []string{uniqueID()},
396394
"t": []string{"1"},
397395
}
398396

399-
if _, err := c.session.FetchRaw(ctx, "GET", channelURLBase+"events", params, nil, true, nil); err != nil {
397+
if _, err := c.session.FetchRaw(ctx, http.MethodGet, channelURLBase+"events", params, nil, true, nil); err != nil {
400398
return fmt.Errorf("failed to acknowledge sid")
401399
}
402400

@@ -478,7 +476,7 @@ func (c *Channel) onPushData(dataBytes []byte) error {
478476

479477
dataArray := innerArray[1]
480478

481-
log.Printf("Chunk contains data array with id %d:\n%v", arrayID, dataArray)
479+
log.Printf("Chunk contains data array with id %f:\n%v", arrayID, dataArray)
482480

483481
// Fire receive array event
484482
c.OnReceiveArray.Fire(dataArray)

pkg/gchatmeow/client.go

Lines changed: 9 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
package gchatmeow
22

33
import (
4-
_ "bytes"
54
"context"
6-
_ "encoding/base64"
75
"encoding/json"
86
"fmt"
9-
_ "io"
107
"log"
11-
_ "math/rand"
12-
_ "mime"
138
"net/http"
149
"net/url"
1510
"os"
1611
"regexp"
17-
_ "strings"
1812
"sync"
1913
"time"
2014

@@ -50,14 +44,10 @@ type Client struct {
5044
OnStreamEvent *Event
5145

5246
// State
53-
gcRequestHeader *proto.RequestHeader
54-
clientID string
55-
email string
56-
lastActiveSecs float64
57-
activeClientState int
58-
apiReqID int64
59-
xsrfToken string
60-
lastTokenRefresh float64
47+
gcRequestHeader *proto.RequestHeader
48+
apiReqID int64
49+
xsrfToken string
50+
lastTokenRefresh float64
6151

6252
// Mutex for thread safety
6353
mu sync.RWMutex
@@ -91,10 +81,10 @@ func NewClient(cookies *Cookies, userAgent string, maxRetries, retryBackoffBase
9181
session: session,
9282

9383
gcRequestHeader: &proto.RequestHeader{
94-
ClientType: GetPointer(proto.RequestHeader_WEB),
95-
ClientVersion: GetPointer(int64(2440378181258)),
84+
ClientType: ptr.Ptr(proto.RequestHeader_WEB),
85+
ClientVersion: ptr.Ptr(int64(2440378181258)),
9686
ClientFeatureCapabilities: &proto.ClientFeatureCapabilities{
97-
SpamRoomInvitesLevel: GetPointer(proto.ClientFeatureCapabilities_FULLY_SUPPORTED),
87+
SpamRoomInvitesLevel: ptr.Ptr(proto.ClientFeatureCapabilities_FULLY_SUPPORTED),
9888
},
9989
},
10090
}
@@ -124,24 +114,6 @@ func (c *Client) Connect(ctx context.Context, maxAge time.Duration) error {
124114
c.OnConnect.Fire(nil)
125115
})
126116
c.channel.OnReceiveArray.AddObserver(c.onReceiveArray)
127-
// go func() {
128-
// for {
129-
// select {
130-
// case <-c.channel.OnConnect:
131-
// c.OnConnect <- struct{}{}
132-
// case <-c.channel.OnReconnect:
133-
// c.OnReconnect <- struct{}{}
134-
// case <-c.channel.OnDisconnect:
135-
// c.OnDisconnect <- struct{}{}
136-
// case arr := <-c.channel.OnReceiveArray:
137-
// if err := c.handleReceiveArray(arr); err != nil {
138-
// logger.Printf("Error handling receive array: %v", err)
139-
// }
140-
// case <-ctx.Done():
141-
// return
142-
// }
143-
// }
144-
// }()
145117

146118
return c.channel.Listen(ctx, maxAge)
147119
}
@@ -161,14 +133,11 @@ func (c *Client) RefreshTokens(ctx context.Context) error {
161133
"referer": {"https://mail.google.com/"},
162134
}
163135

164-
resp, err := c.session.Fetch(ctx, "GET", fmt.Sprintf("%s/mole/world", gcBaseURL), params, headers, true, nil)
136+
resp, err := c.session.Fetch(ctx, http.MethodGet, fmt.Sprintf("%s/mole/world", gcBaseURL), params, headers, true, nil)
165137
if err != nil {
166138
return err
167139
}
168140

169-
os.WriteFile("body.html", resp.Body, 0644)
170-
// fmt.Println(string(resp.Body))
171-
172141
matches := wizPattern.FindSubmatch(resp.Body)
173142
if len(matches) != 2 {
174143
return fmt.Errorf("didn't find WIZ_global_data in /mole/world response")
@@ -229,7 +198,7 @@ func (c *Client) onReceiveArray(arg interface{}) {
229198
// Create and decode protobuf response
230199
resp := &proto.StreamEventsResponse{}
231200
if err := pblite.Unmarshal(bytes, resp); err != nil {
232-
fmt.Printf("failed to decode proto: %w", err)
201+
fmt.Println(fmt.Errorf("failed to decode proto: %w", err))
233202
return
234203
}
235204

pkg/gchatmeow/proto/googlechat.pb.go

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pkg/gchatmeow/utils.go

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)