-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.go
228 lines (200 loc) · 6.16 KB
/
parser.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
216
217
218
219
220
221
222
223
224
225
226
227
228
// showdown2irc - use Showdown chat with an IRC client
// Copyright (C) 2016 Konrad Borowski
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package showdown
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"strings"
)
// LoginData represents authentication information for a bot
type LoginData struct {
Nickname string
Password string
Rooms []string
}
// BotConnection represents a websocket communication with a bot
type BotConnection struct {
loginData LoginData
rooms map[RoomID]*Room
commandCallback func(command, argument string, room *Room)
onSuccess chan<- struct{}
*connection
}
// Room retrieves a room an user is connected to.
//
// If a room doesn't exist, a placeholder room is provided on which
// operations can be provided, in event you need a room to run command
// in, but not actually need to be in it.
//
// This command doesn't handle room aliases.
func (bc *BotConnection) Room(id RoomID) *Room {
if roomWithUsers, ok := bc.rooms[id]; ok {
return roomWithUsers
}
return &Room{BotConnection: bc, ID: id}
}
func (bc *BotConnection) parseMessage(message string) {
var roomID RoomID
if message[0] == '>' {
parts := strings.SplitN(message[1:], "\n", 2)
roomID = RoomID(parts[0])
message = parts[1]
}
for {
messages := strings.SplitN(message, "\n", 2)
currentMessage := messages[0]
if currentMessage == "" {
return
} else if currentMessage[0] == '|' {
parts := strings.SplitN(currentMessage[1:], "|", 2)
command := parts[0]
var argument string
if len(parts) > 1 {
argument = parts[1]
}
if handler, ok := serverCommandHandlers[command]; ok {
handler(argument, bc.Room(roomID))
}
bc.commandCallback(command, argument, bc.Room(roomID))
} else {
bc.commandCallback("", currentMessage, bc.Room(roomID))
}
if len(messages) != 2 {
return
}
message = messages[1]
}
}
// SendGlobalCommand sends a command does not care about a room in which
// it is used.
func (bc *BotConnection) SendGlobalCommand(command string, value string) {
bc.sendCommand(command, value, "")
}
// SendCommand uses a command in a room.
func (bc *BotConnection) sendCommand(command string, value string, room RoomID) {
if room == "lobby" {
room = ""
}
bc.write(fmt.Sprintf("%s|/%s %s", room, command, value))
}
// Say says a text message in a specified room.
func (bc *BotConnection) say(message string, room RoomID) {
if message == "" {
return
}
if message[0] == '/' {
message = "/" + message
} else if message[0] == '!' || strings.HasPrefix(message, ">> ") || strings.HasPrefix(message, ">>> ") {
message = " " + message
}
bc.write(fmt.Sprintf("%s|%s", room, message))
}
func handleConnection(botConnection *BotConnection) {
for message := range botConnection.messageChannel {
log.Println(message)
botConnection.parseMessage(message)
}
}
// ConnectToServer connects to a Showdown server by using its
// client location or its name.
func ConnectToServer(loginData LoginData, name string, commandCallback func(command, argument string, room *Room)) (*BotConnection, <-chan struct{}, error) {
conf, err := findConfiguration(name)
if err != nil {
return nil, nil, err
}
return ConnectToKnownServer(loginData, conf, commandCallback)
}
// ConnectToKnownServer connects to a Showdown server with known
// configuration.
func ConnectToKnownServer(loginData LoginData, conf ServerAddress, commandCallback func(command, argument string, room *Room)) (*BotConnection, <-chan struct{}, error) {
connection, err := webSocketConnect(&conf)
if err != nil {
return nil, nil, err
}
onSuccess := make(chan struct{}, 1)
botConnection := &BotConnection{
connection: connection,
loginData: loginData,
rooms: map[RoomID]*Room{},
commandCallback: commandCallback,
onSuccess: onSuccess,
}
go handleConnection(botConnection)
return botConnection, onSuccess, nil
}
var serverCommandHandlers = map[string]func(string, *Room){
"challstr": challStr,
"init": initializeChatRoom,
"title": setTitle,
"users": setUsers,
"j": joinRoom,
"J": joinRoom,
"l": leaveRoom,
"L": leaveRoom,
"N": renameNick,
}
const actionURL = "https://play.pokemonshowdown.com/action.php"
func challStr(challenge string, room *Room) {
botConnection := room.BotConnection
loginData := botConnection.loginData
parameters := url.Values{}
parameters.Set("act", "login")
parameters.Set("name", loginData.Nickname)
parameters.Set("pass", loginData.Password)
parameters.Set("challstr", challenge)
res, err := http.PostForm(actionURL, parameters)
if err != nil {
log.Fatal(err)
}
contents, err := ioutil.ReadAll(res.Body)
if err != nil {
log.Fatal(err)
}
res.Body.Close()
var assertion struct {
Assertion string
}
json.Unmarshal(contents[1:], &assertion)
value := fmt.Sprintf("%s,0,%s", loginData.Nickname, assertion.Assertion)
botConnection.SendGlobalCommand("trn", value)
botConnection.onSuccess <- struct{}{}
for _, room := range loginData.Rooms {
botConnection.SendGlobalCommand("join", room)
}
}
func initializeChatRoom(rawMessage string, room *Room) {
room.BotConnection.rooms[room.ID] = room
}
func setTitle(rawMessage string, room *Room) {
room.Title = rawMessage
}
func setUsers(rawMessage string, room *Room) {
room.onUserList(rawMessage)
}
func joinRoom(rawMessage string, room *Room) {
room.onJoin(rawMessage)
}
func leaveRoom(rawMessage string, room *Room) {
room.onLeave(rawMessage)
}
func renameNick(rawMessage string, room *Room) {
parts := strings.SplitN(rawMessage, "|", 2)
room.onRename(parts[0], UserID(parts[1]))
}