Repository navigation
Expand file tree
/
Copy pathcall.go
More file actions
130 lines (124 loc) · 3.64 KB
/
Copy pathcall.go
File metadata and controls
130 lines (124 loc) · 3.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// 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 (
"context"
waBinary "go.mau.fi/whatsmeow/binary"
"go.mau.fi/whatsmeow/types"
"go.mau.fi/whatsmeow/types/events"
)
func (cli *Client) handleCallEvent(ctx context.Context, node *waBinary.Node) {
defer cli.maybeDeferredAck(ctx, node)()
if len(node.GetChildren()) != 1 {
cli.dispatchEvent(&events.UnknownCallEvent{Node: node})
return
}
ag := node.AttrGetter()
child := node.GetChildren()[0]
cag := child.AttrGetter()
basicMeta := types.BasicCallMeta{
From: ag.JID("from"),
Timestamp: ag.UnixTime("t"),
CallCreator: cag.JID("call-creator"),
CallID: cag.String("call-id"),
GroupJID: cag.OptionalJIDOrEmpty("group-jid"),
}
if basicMeta.CallCreator.Server == types.HiddenUserServer {
basicMeta.CallCreatorAlt = cag.OptionalJIDOrEmpty("caller_pn")
} else {
// This may not actually exist
basicMeta.CallCreatorAlt = cag.OptionalJIDOrEmpty("caller_lid")
}
switch child.Tag {
case "offer":
cli.dispatchEvent(&events.CallOffer{
BasicCallMeta: basicMeta,
CallRemoteMeta: types.CallRemoteMeta{
RemotePlatform: ag.String("platform"),
RemoteVersion: ag.String("version"),
},
Data: &child,
})
case "offer_notice":
cli.dispatchEvent(&events.CallOfferNotice{
BasicCallMeta: basicMeta,
Media: cag.String("media"),
Type: cag.String("type"),
Data: &child,
})
case "relaylatency":
cli.dispatchEvent(&events.CallRelayLatency{
BasicCallMeta: basicMeta,
Data: &child,
})
case "accept":
cli.dispatchEvent(&events.CallAccept{
BasicCallMeta: basicMeta,
CallRemoteMeta: types.CallRemoteMeta{
RemotePlatform: ag.String("platform"),
RemoteVersion: ag.String("version"),
},
Data: &child,
})
case "preaccept":
cli.dispatchEvent(&events.CallPreAccept{
BasicCallMeta: basicMeta,
CallRemoteMeta: types.CallRemoteMeta{
RemotePlatform: ag.String("platform"),
RemoteVersion: ag.String("version"),
},
Data: &child,
})
case "transport":
cli.dispatchEvent(&events.CallTransport{
BasicCallMeta: basicMeta,
CallRemoteMeta: types.CallRemoteMeta{
RemotePlatform: ag.String("platform"),
RemoteVersion: ag.String("version"),
},
Data: &child,
})
case "terminate":
cli.dispatchEvent(&events.CallTerminate{
BasicCallMeta: basicMeta,
Reason: cag.String("reason"),
Data: &child,
})
case "reject":
cli.dispatchEvent(&events.CallReject{
BasicCallMeta: basicMeta,
Data: &child,
})
default:
cli.dispatchEvent(&events.UnknownCallEvent{Node: node})
}
}
// RejectCall reject an incoming call.
func (cli *Client) RejectCall(ctx context.Context, callFrom types.JID, callID string) error {
ownID := cli.getOwnID()
if ownID.IsEmpty() {
return ErrNotLoggedIn
}
ownID, callFrom = ownID.ToNonAD(), callFrom.ToNonAD()
rejectNode := waBinary.Node{
Tag: "reject",
Attrs: waBinary.Attrs{"call-id": callID, "call-creator": callFrom, "count": "0"},
Content: nil,
}
if token, err := cli.ensureTCToken(ctx, callFrom); err != nil {
cli.Log.Warnf("Failed to get privacy token for call reject to %s: %v", callFrom, err)
} else if len(token) > 0 {
rejectNode.Content = []waBinary.Node{{
Tag: "tctoken",
Content: token,
}}
}
return cli.sendNode(ctx, waBinary.Node{
Tag: "call",
Attrs: waBinary.Attrs{"id": cli.GenerateMessageID(), "from": ownID, "to": callFrom},
Content: []waBinary.Node{rejectNode},
})
}