| 1 |
package xmpp |
| 2 |
|
| 3 |
import ( |
| 4 |
"crypto/sha1" |
| 5 |
"encoding/base64" |
| 6 |
"encoding/hex" |
| 7 |
"encoding/xml" |
| 8 |
"errors" |
| 9 |
"strconv" |
| 10 |
) |
| 11 |
|
| 12 |
type clientAvatarData struct { |
| 13 |
XMLName xml.Name `xml:"data"` |
| 14 |
Data []byte `xml:",innerxml"` |
| 15 |
} |
| 16 |
|
| 17 |
type clientAvatarInfo struct { |
| 18 |
XMLName xml.Name `xml:"info"` |
| 19 |
Bytes string `xml:"bytes,attr"` |
| 20 |
Width string `xml:"width,attr"` |
| 21 |
Height string `xml:"height,attr"` |
| 22 |
ID string `xml:"id,attr"` |
| 23 |
Type string `xml:"type,attr"` |
| 24 |
URL string `xml:"url,attr"` |
| 25 |
} |
| 26 |
|
| 27 |
type clientAvatarMetadata struct { |
| 28 |
XMLName xml.Name `xml:"metadata"` |
| 29 |
XMLNS string `xml:"xmlns,attr"` |
| 30 |
Info clientAvatarInfo `xml:"info"` |
| 31 |
} |
| 32 |
|
| 33 |
type AvatarData struct { |
| 34 |
Data []byte |
| 35 |
From string |
| 36 |
} |
| 37 |
|
| 38 |
type AvatarMetadata struct { |
| 39 |
From string |
| 40 |
Bytes int |
| 41 |
Width int |
| 42 |
Height int |
| 43 |
ID string |
| 44 |
Type string |
| 45 |
URL string |
| 46 |
} |
| 47 |
|
| 48 |
func handleAvatarData(itemsBody []byte, from, id string) (AvatarData, error) { |
| 49 |
var data clientAvatarData |
| 50 |
err := xml.Unmarshal(itemsBody, &data) |
| 51 |
if err != nil { |
| 52 |
return AvatarData{}, err |
| 53 |
} |
| 54 |
|
| 55 |
// Base64-decode the avatar data to check its SHA1 hash |
| 56 |
dataRaw, err := base64.StdEncoding.DecodeString( |
| 57 |
string(data.Data), |
| 58 |
) |
| 59 |
if err != nil { |
| 60 |
return AvatarData{}, err |
| 61 |
} |
| 62 |
|
| 63 |
hash := sha1.Sum(dataRaw) //nolint: gosec,G401 // It is not about security. |
| 64 |
hashStr := hex.EncodeToString(hash[:]) |
| 65 |
if hashStr != id { |
| 66 |
return AvatarData{}, errors.New("SHA1 hashes do not match") |
| 67 |
} |
| 68 |
|
| 69 |
return AvatarData{ |
| 70 |
Data: dataRaw, |
| 71 |
From: from, |
| 72 |
}, nil |
| 73 |
} |
| 74 |
|
| 75 |
func handleAvatarMetadata(body []byte, from string) (AvatarMetadata, error) { |
| 76 |
var meta clientAvatarMetadata |
| 77 |
err := xml.Unmarshal(body, &meta) |
| 78 |
if err != nil { |
| 79 |
return AvatarMetadata{}, err |
| 80 |
} |
| 81 |
|
| 82 |
return AvatarMetadata{ |
| 83 |
From: from, |
| 84 |
Bytes: atoiw(meta.Info.Bytes), |
| 85 |
Width: atoiw(meta.Info.Width), |
| 86 |
Height: atoiw(meta.Info.Height), |
| 87 |
ID: meta.Info.ID, |
| 88 |
Type: meta.Info.Type, |
| 89 |
URL: meta.Info.URL, |
| 90 |
}, nil |
| 91 |
} |
| 92 |
|
| 93 |
// A wrapper for atoi which just returns -1 if an error occurs |
| 94 |
func atoiw(str string) int { |
| 95 |
i, err := strconv.Atoi(str) |
| 96 |
if err != nil { |
| 97 |
return -1 |
| 98 |
} |
| 99 |
|
| 100 |
return i |
| 101 |
} |
| 102 |
|
| 103 |
func (c *Client) AvatarSubscribeMetadata(jid string) error { |
| 104 |
return c.PubsubSubscribeNode(XMPPNS_AVATAR_PEP_METADATA, jid) |
| 105 |
} |
| 106 |
|
| 107 |
func (c *Client) AvatarUnsubscribeMetadata(jid string) error { |
| 108 |
return c.PubsubUnsubscribeNode(XMPPNS_AVATAR_PEP_METADATA, jid) |
| 109 |
} |
| 110 |
|
| 111 |
func (c *Client) AvatarRequestData(jid string) error { |
| 112 |
return c.PubsubRequestLastItems(XMPPNS_AVATAR_PEP_DATA, jid) |
| 113 |
} |
| 114 |
|
| 115 |
func (c *Client) AvatarRequestDataByID(jid, id string) error { |
| 116 |
return c.PubsubRequestItem(XMPPNS_AVATAR_PEP_DATA, jid, id) |
| 117 |
} |
| 118 |
|
| 119 |
func (c *Client) AvatarRequestMetadata(jid string) error { |
| 120 |
return c.PubsubRequestLastItems(XMPPNS_AVATAR_PEP_METADATA, jid) |
| 121 |
} |