From e3cafeaf9292f67459ff1d186f68283bfaedf2ae Mon Sep 17 00:00:00 2001 From: Wim Date: Mon, 31 Jan 2022 00:27:37 +0100 Subject: Add dependencies/vendor (whatsapp) --- vendor/go.mau.fi/whatsmeow/mediaconn.go | 93 +++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 vendor/go.mau.fi/whatsmeow/mediaconn.go (limited to 'vendor/go.mau.fi/whatsmeow/mediaconn.go') diff --git a/vendor/go.mau.fi/whatsmeow/mediaconn.go b/vendor/go.mau.fi/whatsmeow/mediaconn.go new file mode 100644 index 00000000..6faa27a6 --- /dev/null +++ b/vendor/go.mau.fi/whatsmeow/mediaconn.go @@ -0,0 +1,93 @@ +// 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) +} + +func (cli *Client) refreshMediaConn(force bool) error { + cli.mediaConnLock.Lock() + defer cli.mediaConnLock.Unlock() + if cli.mediaConn == nil || force || time.Now().After(cli.mediaConn.Expiry()) { + var err error + cli.mediaConn, err = cli.queryMediaConn() + if err != nil { + return err + } + } + return nil +} + +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 +} -- cgit v1.2.3