-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathconfirmations.go
177 lines (147 loc) · 4.22 KB
/
confirmations.go
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package steam
import (
"encoding/json"
"errors"
"io"
"net/http"
"fmt"
"net/url"
"strconv"
"github.com/PuerkitoBio/goquery"
)
type Confirmation struct {
ID uint64
Key uint64
Title string
Receiving string
Since string
OfferID uint64
}
var (
ErrConfirmationsUnknownError = errors.New("unknown error occurered finding confirmations")
ErrCannotFindConfirmations = errors.New("unable to find confirmations")
ErrCannotFindDescriptions = errors.New("unable to find confirmation descriptions")
ErrConfiramtionsDescMismatch = errors.New("cannot match confirmations with their respective descriptions")
)
func (session *Session) execConfirmationRequest(request, key, tag string, current int64, values map[string]interface{}) (*http.Response, error) {
params := url.Values{
"p": {session.deviceID},
"a": {session.oauth.SteamID.ToString()},
"k": {key},
"t": {strconv.FormatInt(current, 10)},
"m": {"android"},
"tag": {tag},
}
if values != nil {
for k, v := range values {
switch v := v.(type) {
case string:
params.Add(k, v)
case uint64:
params.Add(k, strconv.FormatUint(v, 10))
default:
return nil, fmt.Errorf("execConfirmationRequest: missing implementation for type %v", v)
}
}
}
return session.client.Get("https://steamcommunity.com/mobileconf/" + request + params.Encode())
}
func (session *Session) GetConfirmations(identitySecret string, current int64) ([]*Confirmation, error) {
key, err := GenerateConfirmationCode(identitySecret, "conf", current)
if err != nil {
return nil, err
}
resp, err := session.execConfirmationRequest("conf?", key, "conf", current, nil)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return nil, err
}
doc, err := goquery.NewDocumentFromReader(io.Reader(resp.Body))
if err != nil {
return nil, err
}
/* FIXME: broken
if empty := doc.Find(".mobileconf_empty"); empty != nil {
if done := doc.Find(".mobileconf_done"); done != nil {
return nil, nil
}
return nil, ErrConfirmationsUnknownError // FIXME
}
*/
entries := doc.Find(".mobileconf_list_entry")
if entries == nil {
return nil, ErrCannotFindConfirmations
}
descriptions := doc.Find(".mobileconf_list_entry_description")
if descriptions == nil {
return nil, ErrCannotFindDescriptions
}
if len(entries.Nodes) != len(descriptions.Nodes) {
return nil, ErrConfiramtionsDescMismatch
}
confirmations := []*Confirmation{}
for k, sel := range entries.Nodes {
confirmation := &Confirmation{}
for _, attr := range sel.Attr {
if attr.Key == "data-confid" {
confirmation.ID, _ = strconv.ParseUint(attr.Val, 10, 64)
} else if attr.Key == "data-key" {
confirmation.Key, _ = strconv.ParseUint(attr.Val, 10, 64)
} else if attr.Key == "data-creator" {
confirmation.OfferID, _ = strconv.ParseUint(attr.Val, 10, 64)
}
}
descSel := descriptions.Nodes[k]
depth := 0
for child := descSel.FirstChild; child != nil; child = child.NextSibling {
for n := child.FirstChild; n != nil; n = n.NextSibling {
switch depth {
case 0:
confirmation.Title = n.Data
case 1:
confirmation.Receiving = n.Data
case 2:
confirmation.Since = n.Data
}
depth++
}
}
confirmations = append(confirmations, confirmation)
}
return confirmations, nil
}
func (session *Session) AnswerConfirmation(confirmation *Confirmation, identitySecret, answer string, current int64) error {
key, err := GenerateConfirmationCode(identitySecret, answer, current)
if err != nil {
return err
}
op := map[string]interface{}{
"op": answer,
"cid": uint64(confirmation.ID),
"ck": confirmation.Key,
}
resp, err := session.execConfirmationRequest("ajaxop?", key, answer, current, op)
if resp != nil {
defer resp.Body.Close()
}
if err != nil {
return err
}
type Response struct {
Success bool `json:"success"`
Message string `json:"message"`
}
var response Response
if err := json.NewDecoder(resp.Body).Decode(&response); err != nil {
return err
}
if !response.Success {
return errors.New(response.Message)
}
return nil
}
func (confirmation *Confirmation) Answer(session *Session, key, answer string, current int64) error {
return session.AnswerConfirmation(confirmation, key, answer, current)
}