-
Notifications
You must be signed in to change notification settings - Fork 0
/
conn.go
215 lines (193 loc) · 5.04 KB
/
conn.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
package sechat
import (
"errors"
"fmt"
"net/http"
"net/http/cookiejar"
"net/url"
"regexp"
"strconv"
"sync"
"github.com/gorilla/websocket"
"github.com/sirupsen/logrus"
)
const (
AccessReadWrite = "read-write"
AccessReadOnly = "read-only"
AccessRequest = "request"
)
var ErrRoomID = errors.New("unable to find room ID")
// forceRedirect is an internal header that prevents redirects from being
// inhibited.
const forceRedirect = "X-Force-Redirect"
// roomRegexp matches a "room" URL.
var roomRegexp = regexp.MustCompile(`^(?:https?://chat.stackexchange.com)?/rooms(?:/info)?/(\d+)`)
// Conn represents a connection to the Stack Exchange chat network. HTTP
// requests are used to trigger actions and websockets are used for event
// notifications.
type Conn struct {
Events <-chan *Event
connectedCh chan bool
closeCh chan bool
closedCh chan bool
client *http.Client
conn *websocket.Conn
log *logrus.Entry
mutex sync.Mutex
email string
password string
fkey string
room int
user int
}
// atoi removes the error handling from Atoi() and ensures a value is always
// returned.
func atoi(s string) int {
v, _ := strconv.Atoi(s)
return v
}
// checkRedirect prevents a redirect from taking place if the URL matches a
// room URL. This is necessary for the new room methods.
func checkRedirect(req *http.Request, via []*http.Request) error {
if len(via[0].Header.Get(forceRedirect)) == 0 &&
roomRegexp.MatchString(req.URL.String()) {
return http.ErrUseLastResponse
}
return nil
}
// New creates a new Conn instance.
func New(email, password string, room int) (*Conn, error) {
jar, err := cookiejar.New(nil)
if err != nil {
return nil, err
}
var (
ch = make(chan *Event)
c = &Conn{
Events: ch,
connectedCh: make(chan bool),
closeCh: make(chan bool),
closedCh: make(chan bool),
client: &http.Client{
CheckRedirect: checkRedirect,
Jar: jar,
},
log: logrus.WithField("context", "sechat"),
email: email,
password: password,
room: room,
}
)
go c.run(ch)
return c, nil
}
// UserID returns the chat ID of the current user.
func (c *Conn) UserID() int {
return c.user
}
// WaitForConnected waits until authentication is complete and the websocket is
// connected.
func (c *Conn) WaitForConnected() bool {
return <-c.connectedCh
}
// Join listens for events in the specified room in addition to those already
// joined.
func (c *Conn) Join(room int) error {
_, err := c.postForm(
"/events",
&url.Values{fmt.Sprintf("r%d", room): {"999999999999"}},
)
return err
}
// Leave stops listening for events in the specified room.
func (c *Conn) Leave(room int) error {
_, err := c.postForm(
fmt.Sprintf("/chats/leave/%d", room),
&url.Values{"quiet": {"true"}},
)
return err
}
// newRoom eliminates the redundant code in NewRoom and NewRoomWithUser.
func (c *Conn) newRoom(path string, data *url.Values) (int, error) {
res, err := c.postForm(path, data)
if err != nil {
return 0, err
}
m := roomRegexp.FindStringSubmatch(res.Header.Get("Location"))
if m == nil {
return 0, ErrRoomID
}
return atoi(m[1]), nil
}
// NewRoom creates a new room with the specified parameters. defaultAccess
// should normally be set to AccessReadWrite.
func (c *Conn) NewRoom(name, description, host, defaultAccess string) (int, error) {
return c.newRoom(
"/rooms/save",
&url.Values{
"name": {name},
"description": {description},
"host": {host},
"defaultAccess": {defaultAccess},
"noDupeCheck": {"true"},
},
)
}
// NewRoomWithUser creates a new room with the specified name and invites the
// specifed user to the new room. The ID of the new room is returned.
func (c *Conn) NewRoomWithUser(user int, name string) (int, error) {
return c.newRoom(
"/rooms/pairoff",
&url.Values{
"withUserId": {strconv.Itoa(user)},
"name": {name},
},
)
}
// Invite sends an invitation to a user inviting them to join a room.
func (c *Conn) Invite(user, room int) error {
_, err := c.postForm(
"/users/invite",
&url.Values{
"UserId": {strconv.Itoa(user)},
"RoomId": {strconv.Itoa(room)},
},
)
return err
}
// Send posts the specified message to the specified room.
func (c *Conn) Send(room int, text string) error {
_, err := c.postForm(
fmt.Sprintf("/chats/%d/messages/new", room),
&url.Values{"text": {text}},
)
return err
}
// Reply sends a reply for the specified event.
func (c *Conn) Reply(e *Event, text string) error {
return c.Send(
e.RoomID,
fmt.Sprintf(":%d %s", e.MessageID, text),
)
}
// Star stars the specified message.
func (c *Conn) Star(message int) error {
_, err := c.postForm(
fmt.Sprintf("/messages/%d/star", message),
&url.Values{},
)
return err
}
// Close disconnects the websocket and shuts down the connection.
func (c *Conn) Close() {
// Indicate that the connection is closing
close(c.closeCh)
// If the websocket is connected, close it
c.mutex.Lock()
if c.conn != nil {
c.conn.Close()
}
c.mutex.Unlock()
// Wait for the loop to finish
<-c.closedCh
}