5
0
mirror of https://github.com/cwinfo/matterbridge.git synced 2024-11-23 01:11:37 +00:00
matterbridge/vendor/go.mau.fi/whatsmeow/mediaconn.go

94 lines
2.5 KiB
Go
Raw Normal View History

2022-01-30 23:27:37 +00:00
// Copyright (c) 2021 Tulir Asokan
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package whatsmeow
import (
"fmt"
"time"
waBinary "go.mau.fi/whatsmeow/binary"
"go.mau.fi/whatsmeow/types"
)
//type MediaConnIP struct {
// IP4 net.IP
// IP6 net.IP
//}
// MediaConnHost represents a single host to download media from.
type MediaConnHost struct {
Hostname string
//IPs []MediaConnIP
}
// MediaConn contains a list of WhatsApp servers from which attachments can be downloaded from.
type MediaConn struct {
Auth string
AuthTTL int
TTL int
MaxBuckets int
FetchedAt time.Time
Hosts []MediaConnHost
}
// Expiry returns the time when the MediaConn expires.
func (mc *MediaConn) Expiry() time.Time {
return mc.FetchedAt.Add(time.Duration(mc.TTL) * time.Second)
}
2022-03-31 22:23:19 +00:00
func (cli *Client) refreshMediaConn(force bool) (*MediaConn, error) {
2022-01-30 23:27:37 +00:00
cli.mediaConnLock.Lock()
defer cli.mediaConnLock.Unlock()
2022-03-31 22:23:19 +00:00
if cli.mediaConnCache == nil || force || time.Now().After(cli.mediaConnCache.Expiry()) {
2022-01-30 23:27:37 +00:00
var err error
2022-03-31 22:23:19 +00:00
cli.mediaConnCache, err = cli.queryMediaConn()
2022-01-30 23:27:37 +00:00
if err != nil {
2022-03-31 22:23:19 +00:00
return nil, err
2022-01-30 23:27:37 +00:00
}
}
2022-03-31 22:23:19 +00:00
return cli.mediaConnCache, nil
2022-01-30 23:27:37 +00:00
}
func (cli *Client) queryMediaConn() (*MediaConn, error) {
resp, err := cli.sendIQ(infoQuery{
Namespace: "w:m",
Type: "set",
To: types.ServerJID,
Content: []waBinary.Node{{Tag: "media_conn"}},
})
if err != nil {
return nil, fmt.Errorf("failed to query media connections: %w", err)
} else if len(resp.GetChildren()) == 0 || resp.GetChildren()[0].Tag != "media_conn" {
return nil, fmt.Errorf("failed to query media connections: unexpected child tag")
}
respMC := resp.GetChildren()[0]
var mc MediaConn
ag := respMC.AttrGetter()
mc.FetchedAt = time.Now()
mc.Auth = ag.String("auth")
mc.TTL = ag.Int("ttl")
mc.AuthTTL = ag.Int("auth_ttl")
mc.MaxBuckets = ag.Int("max_buckets")
if !ag.OK() {
return nil, fmt.Errorf("failed to parse media connections: %+v", ag.Errors)
}
for _, child := range respMC.GetChildren() {
if child.Tag != "host" {
cli.Log.Warnf("Unexpected child in media_conn element: %s", child.XMLString())
continue
}
cag := child.AttrGetter()
mc.Hosts = append(mc.Hosts, MediaConnHost{
Hostname: cag.String("hostname"),
})
if !cag.OK() {
return nil, fmt.Errorf("failed to parse media connection host: %+v", ag.Errors)
}
}
return &mc, nil
}