1 /** The parsers for the chat and multiplayer protocol. The main parser is `message`. |
|
2 * # Protocol |
|
3 * All messages consist of `\n`-separated strings. The end of a message is |
|
4 * indicated by a double newline - `\n\n`. |
|
5 * |
|
6 * For example, a nullary command like PING will be actually sent as `PING\n\n`. |
|
7 * A unary command, such as `START_GAME nick` will be actually sent as `START_GAME\nnick\n\n`. |
|
8 */ |
|
9 |
|
10 use nom::*; |
|
11 |
|
12 use std::{ |
|
13 str, str::FromStr, |
|
14 ops::Range |
|
15 }; |
|
16 use super::{ |
|
17 messages::{HWProtocolMessage, HWProtocolMessage::*} |
|
18 }; |
|
19 #[cfg(test)] |
|
20 use { |
|
21 super::test::gen_proto_msg, |
|
22 proptest::{proptest, proptest_helper} |
|
23 }; |
|
24 use crate::server::coretypes::{ |
|
25 HedgehogInfo, TeamInfo, GameCfg, VoteType, MAX_HEDGEHOGS_PER_TEAM |
|
26 }; |
|
27 |
|
28 named!(end_of_message, tag!("\n\n")); |
|
29 named!(str_line<&[u8], &str>, map_res!(not_line_ending, str::from_utf8)); |
|
30 named!( a_line<&[u8], String>, map!(str_line, String::from)); |
|
31 named!(cmd_arg<&[u8], String>, |
|
32 map!(map_res!(take_until_either!(" \n"), str::from_utf8), String::from)); |
|
33 named!( u8_line<&[u8], u8>, map_res!(str_line, FromStr::from_str)); |
|
34 named!(u16_line<&[u8], u16>, map_res!(str_line, FromStr::from_str)); |
|
35 named!(u32_line<&[u8], u32>, map_res!(str_line, FromStr::from_str)); |
|
36 named!(yes_no_line<&[u8], bool>, alt!( |
|
37 do_parse!(tag_no_case!("YES") >> (true)) |
|
38 | do_parse!(tag_no_case!("NO") >> (false)))); |
|
39 named!(opt_param<&[u8], Option<String> >, alt!( |
|
40 do_parse!(peek!(tag!("\n\n")) >> (None)) |
|
41 | do_parse!(tag!("\n") >> s: str_line >> (Some(s.to_string()))))); |
|
42 named!(spaces<&[u8], &[u8]>, preceded!(tag!(" "), eat_separator!(" "))); |
|
43 named!(opt_space_param<&[u8], Option<String> >, alt!( |
|
44 do_parse!(peek!(tag!("\n\n")) >> (None)) |
|
45 | do_parse!(spaces >> s: str_line >> (Some(s.to_string()))))); |
|
46 named!(hog_line<&[u8], HedgehogInfo>, |
|
47 do_parse!(name: str_line >> eol >> hat: str_line >> |
|
48 (HedgehogInfo{name: name.to_string(), hat: hat.to_string()}))); |
|
49 named!(_8_hogs<&[u8], [HedgehogInfo; MAX_HEDGEHOGS_PER_TEAM as usize]>, |
|
50 do_parse!(h1: hog_line >> eol >> h2: hog_line >> eol >> |
|
51 h3: hog_line >> eol >> h4: hog_line >> eol >> |
|
52 h5: hog_line >> eol >> h6: hog_line >> eol >> |
|
53 h7: hog_line >> eol >> h8: hog_line >> |
|
54 ([h1, h2, h3, h4, h5, h6, h7, h8]))); |
|
55 named!(voting<&[u8], VoteType>, alt!( |
|
56 do_parse!(tag_no_case!("KICK") >> spaces >> n: a_line >> |
|
57 (VoteType::Kick(n))) |
|
58 | do_parse!(tag_no_case!("MAP") >> |
|
59 n: opt!(preceded!(spaces, a_line)) >> |
|
60 (VoteType::Map(n))) |
|
61 | do_parse!(tag_no_case!("PAUSE") >> |
|
62 (VoteType::Pause)) |
|
63 | do_parse!(tag_no_case!("NEWSEED") >> |
|
64 (VoteType::NewSeed)) |
|
65 | do_parse!(tag_no_case!("HEDGEHOGS") >> spaces >> n: u8_line >> |
|
66 (VoteType::HedgehogsPerTeam(n))))); |
|
67 |
|
68 /** Recognizes messages which do not take any parameters */ |
|
69 named!(basic_message<&[u8], HWProtocolMessage>, alt!( |
|
70 do_parse!(tag!("PING") >> (Ping)) |
|
71 | do_parse!(tag!("PONG") >> (Pong)) |
|
72 | do_parse!(tag!("LIST") >> (List)) |
|
73 | do_parse!(tag!("BANLIST") >> (BanList)) |
|
74 | do_parse!(tag!("GET_SERVER_VAR") >> (GetServerVar)) |
|
75 | do_parse!(tag!("TOGGLE_READY") >> (ToggleReady)) |
|
76 | do_parse!(tag!("START_GAME") >> (StartGame)) |
|
77 | do_parse!(tag!("ROUNDFINISHED") >> _m: opt_param >> (RoundFinished)) |
|
78 | do_parse!(tag!("TOGGLE_RESTRICT_JOINS") >> (ToggleRestrictJoin)) |
|
79 | do_parse!(tag!("TOGGLE_RESTRICT_TEAMS") >> (ToggleRestrictTeams)) |
|
80 | do_parse!(tag!("TOGGLE_REGISTERED_ONLY") >> (ToggleRegisteredOnly)) |
|
81 )); |
|
82 |
|
83 /** Recognizes messages which take exactly one parameter */ |
|
84 named!(one_param_message<&[u8], HWProtocolMessage>, alt!( |
|
85 do_parse!(tag!("NICK") >> eol >> n: a_line >> (Nick(n))) |
|
86 | do_parse!(tag!("INFO") >> eol >> n: a_line >> (Info(n))) |
|
87 | do_parse!(tag!("CHAT") >> eol >> m: a_line >> (Chat(m))) |
|
88 | do_parse!(tag!("PART") >> msg: opt_param >> (Part(msg))) |
|
89 | do_parse!(tag!("FOLLOW") >> eol >> n: a_line >> (Follow(n))) |
|
90 | do_parse!(tag!("KICK") >> eol >> n: a_line >> (Kick(n))) |
|
91 | do_parse!(tag!("UNBAN") >> eol >> n: a_line >> (Unban(n))) |
|
92 | do_parse!(tag!("EM") >> eol >> m: a_line >> (EngineMessage(m))) |
|
93 | do_parse!(tag!("TEAMCHAT") >> eol >> m: a_line >> (TeamChat(m))) |
|
94 | do_parse!(tag!("ROOM_NAME") >> eol >> n: a_line >> (RoomName(n))) |
|
95 | do_parse!(tag!("REMOVE_TEAM") >> eol >> n: a_line >> (RemoveTeam(n))) |
|
96 |
|
97 | do_parse!(tag!("PROTO") >> eol >> d: u16_line >> (Proto(d))) |
|
98 |
|
99 | do_parse!(tag!("QUIT") >> msg: opt_param >> (Quit(msg))) |
|
100 )); |
|
101 |
|
102 /** Recognizes messages preceded with CMD */ |
|
103 named!(cmd_message<&[u8], HWProtocolMessage>, preceded!(tag!("CMD\n"), alt!( |
|
104 do_parse!(tag_no_case!("STATS") >> (Stats)) |
|
105 | do_parse!(tag_no_case!("FIX") >> (Fix)) |
|
106 | do_parse!(tag_no_case!("UNFIX") >> (Unfix)) |
|
107 | do_parse!(tag_no_case!("RESTART_SERVER") >> spaces >> tag!("YES") >> (RestartServer)) |
|
108 | do_parse!(tag_no_case!("REGISTERED_ONLY") >> (ToggleServerRegisteredOnly)) |
|
109 | do_parse!(tag_no_case!("SUPER_POWER") >> (SuperPower)) |
|
110 | do_parse!(tag_no_case!("PART") >> m: opt_space_param >> (Part(m))) |
|
111 | do_parse!(tag_no_case!("QUIT") >> m: opt_space_param >> (Quit(m))) |
|
112 | do_parse!(tag_no_case!("DELEGATE") >> spaces >> n: a_line >> (Delegate(n))) |
|
113 | do_parse!(tag_no_case!("SAVE") >> spaces >> n: cmd_arg >> spaces >> l: cmd_arg >> (Save(n, l))) |
|
114 | do_parse!(tag_no_case!("DELETE") >> spaces >> n: a_line >> (Delete(n))) |
|
115 | do_parse!(tag_no_case!("SAVEROOM") >> spaces >> r: a_line >> (SaveRoom(r))) |
|
116 | do_parse!(tag_no_case!("LOADROOM") >> spaces >> r: a_line >> (LoadRoom(r))) |
|
117 | do_parse!(tag_no_case!("GLOBAL") >> spaces >> m: a_line >> (Global(m))) |
|
118 | do_parse!(tag_no_case!("WATCH") >> spaces >> i: a_line >> (Watch(i))) |
|
119 | do_parse!(tag_no_case!("GREETING") >> spaces >> m: a_line >> (Greeting(m))) |
|
120 | do_parse!(tag_no_case!("VOTE") >> spaces >> m: yes_no_line >> (Vote(m))) |
|
121 | do_parse!(tag_no_case!("FORCE") >> spaces >> m: yes_no_line >> (ForceVote(m))) |
|
122 | do_parse!(tag_no_case!("INFO") >> spaces >> n: a_line >> (Info(n))) |
|
123 | do_parse!(tag_no_case!("MAXTEAMS") >> spaces >> n: u8_line >> (MaxTeams(n))) |
|
124 | do_parse!(tag_no_case!("CALLVOTE") >> |
|
125 v: opt!(preceded!(spaces, voting)) >> (CallVote(v))) |
|
126 | do_parse!( |
|
127 tag_no_case!("RND") >> alt!(spaces | peek!(end_of_message)) >> |
|
128 v: str_line >> |
|
129 (Rnd(v.split_whitespace().map(String::from).collect()))) |
|
130 ))); |
|
131 |
|
132 named!(complex_message<&[u8], HWProtocolMessage>, alt!( |
|
133 do_parse!(tag!("PASSWORD") >> eol >> |
|
134 p: a_line >> eol >> |
|
135 s: a_line >> |
|
136 (Password(p, s))) |
|
137 | do_parse!(tag!("CHECKER") >> eol >> |
|
138 i: u16_line >> eol >> |
|
139 n: a_line >> eol >> |
|
140 p: a_line >> |
|
141 (Checker(i, n, p))) |
|
142 | do_parse!(tag!("CREATE_ROOM") >> eol >> |
|
143 n: a_line >> |
|
144 p: opt_param >> |
|
145 (CreateRoom(n, p))) |
|
146 | do_parse!(tag!("JOIN_ROOM") >> eol >> |
|
147 n: a_line >> |
|
148 p: opt_param >> |
|
149 (JoinRoom(n, p))) |
|
150 | do_parse!(tag!("ADD_TEAM") >> eol >> |
|
151 name: a_line >> eol >> |
|
152 color: u8_line >> eol >> |
|
153 grave: a_line >> eol >> |
|
154 fort: a_line >> eol >> |
|
155 voice_pack: a_line >> eol >> |
|
156 flag: a_line >> eol >> |
|
157 difficulty: u8_line >> eol >> |
|
158 hedgehogs: _8_hogs >> |
|
159 (AddTeam(Box::new(TeamInfo{ |
|
160 name, color, grave, fort, |
|
161 voice_pack, flag, difficulty, |
|
162 hedgehogs, hedgehogs_number: 0 |
|
163 })))) |
|
164 | do_parse!(tag!("HH_NUM") >> eol >> |
|
165 n: a_line >> eol >> |
|
166 c: u8_line >> |
|
167 (SetHedgehogsNumber(n, c))) |
|
168 | do_parse!(tag!("TEAM_COLOR") >> eol >> |
|
169 n: a_line >> eol >> |
|
170 c: u8_line >> |
|
171 (SetTeamColor(n, c))) |
|
172 | do_parse!(tag!("BAN") >> eol >> |
|
173 n: a_line >> eol >> |
|
174 r: a_line >> eol >> |
|
175 t: u32_line >> |
|
176 (Ban(n, r, t))) |
|
177 | do_parse!(tag!("BAN_IP") >> eol >> |
|
178 n: a_line >> eol >> |
|
179 r: a_line >> eol >> |
|
180 t: u32_line >> |
|
181 (BanIP(n, r, t))) |
|
182 | do_parse!(tag!("BAN_NICK") >> eol >> |
|
183 n: a_line >> eol >> |
|
184 r: a_line >> eol >> |
|
185 t: u32_line >> |
|
186 (BanNick(n, r, t))) |
|
187 )); |
|
188 |
|
189 named!(cfg_message<&[u8], HWProtocolMessage>, preceded!(tag!("CFG\n"), map!(alt!( |
|
190 do_parse!(tag!("THEME") >> eol >> |
|
191 name: a_line >> |
|
192 (GameCfg::Theme(name))) |
|
193 | do_parse!(tag!("SCRIPT") >> eol >> |
|
194 name: a_line >> |
|
195 (GameCfg::Script(name))) |
|
196 | do_parse!(tag!("AMMO") >> eol >> |
|
197 name: a_line >> |
|
198 value: opt_param >> |
|
199 (GameCfg::Ammo(name, value))) |
|
200 | do_parse!(tag!("SCHEME") >> eol >> |
|
201 name: a_line >> |
|
202 values: opt!(preceded!(eol, separated_list!(eol, a_line))) >> |
|
203 (GameCfg::Scheme(name, values.unwrap_or_default()))) |
|
204 | do_parse!(tag!("FEATURE_SIZE") >> eol >> |
|
205 value: u32_line >> |
|
206 (GameCfg::FeatureSize(value))) |
|
207 | do_parse!(tag!("MAP") >> eol >> |
|
208 value: a_line >> |
|
209 (GameCfg::MapType(value))) |
|
210 | do_parse!(tag!("MAPGEN") >> eol >> |
|
211 value: u32_line >> |
|
212 (GameCfg::MapGenerator(value))) |
|
213 | do_parse!(tag!("MAZE_SIZE") >> eol >> |
|
214 value: u32_line >> |
|
215 (GameCfg::MazeSize(value))) |
|
216 | do_parse!(tag!("SEED") >> eol >> |
|
217 value: a_line >> |
|
218 (GameCfg::Seed(value))) |
|
219 | do_parse!(tag!("TEMPLATE") >> eol >> |
|
220 value: u32_line >> |
|
221 (GameCfg::Template(value))) |
|
222 | do_parse!(tag!("DRAWNMAP") >> eol >> |
|
223 value: a_line >> |
|
224 (GameCfg::DrawnMap(value))) |
|
225 ), Cfg))); |
|
226 |
|
227 named!(malformed_message<&[u8], HWProtocolMessage>, |
|
228 do_parse!(separated_list!(eol, a_line) >> (Malformed))); |
|
229 |
|
230 named!(empty_message<&[u8], HWProtocolMessage>, |
|
231 do_parse!(alt!(end_of_message | eol) >> (Empty))); |
|
232 |
|
233 named!(message<&[u8], HWProtocolMessage>, alt!(terminated!( |
|
234 alt!( |
|
235 basic_message |
|
236 | one_param_message |
|
237 | cmd_message |
|
238 | complex_message |
|
239 | cfg_message |
|
240 ), end_of_message |
|
241 ) |
|
242 | terminated!(malformed_message, end_of_message) |
|
243 | empty_message |
|
244 ) |
|
245 ); |
|
246 |
|
247 named!(pub extract_messages<&[u8], Vec<HWProtocolMessage> >, many0!(complete!(message))); |
|
248 |
|
249 #[cfg(test)] |
|
250 proptest! { |
|
251 #[test] |
|
252 fn is_parser_composition_idempotent(ref msg in gen_proto_msg()) { |
|
253 println!("!! Msg: {:?}, Bytes: {:?} !!", msg, msg.to_raw_protocol().as_bytes()); |
|
254 assert_eq!(message(msg.to_raw_protocol().as_bytes()), Ok((&b""[..], msg.clone()))) |
|
255 } |
|
256 } |
|
257 |
|
258 #[test] |
|
259 fn parse_test() { |
|
260 assert_eq!(message(b"PING\n\n"), Ok((&b""[..], Ping))); |
|
261 assert_eq!(message(b"START_GAME\n\n"), Ok((&b""[..], StartGame))); |
|
262 assert_eq!(message(b"NICK\nit's me\n\n"), Ok((&b""[..], Nick("it's me".to_string())))); |
|
263 assert_eq!(message(b"PROTO\n51\n\n"), Ok((&b""[..], Proto(51)))); |
|
264 assert_eq!(message(b"QUIT\nbye-bye\n\n"), Ok((&b""[..], Quit(Some("bye-bye".to_string()))))); |
|
265 assert_eq!(message(b"QUIT\n\n"), Ok((&b""[..], Quit(None)))); |
|
266 assert_eq!(message(b"CMD\nwatch demo\n\n"), Ok((&b""[..], Watch("demo".to_string())))); |
|
267 assert_eq!(message(b"BAN\nme\nbad\n77\n\n"), Ok((&b""[..], Ban("me".to_string(), "bad".to_string(), 77)))); |
|
268 |
|
269 assert_eq!(message(b"CMD\nPART\n\n"), Ok((&b""[..], Part(None)))); |
|
270 assert_eq!(message(b"CMD\nPART _msg_\n\n"), Ok((&b""[..], Part(Some("_msg_".to_string()))))); |
|
271 |
|
272 assert_eq!(message(b"CMD\nRND\n\n"), Ok((&b""[..], Rnd(vec![])))); |
|
273 assert_eq!( |
|
274 message(b"CMD\nRND A B\n\n"), |
|
275 Ok((&b""[..], Rnd(vec![String::from("A"), String::from("B")]))) |
|
276 ); |
|
277 |
|
278 assert_eq!(extract_messages(b"QUIT\n1\n2\n\n"), Ok((&b""[..], vec![Malformed]))); |
|
279 |
|
280 assert_eq!(extract_messages(b"PING\n\nPING\n\nP"), Ok((&b"P"[..], vec![Ping, Ping]))); |
|
281 assert_eq!(extract_messages(b"SING\n\nPING\n\n"), Ok((&b""[..], vec![Malformed, Ping]))); |
|
282 assert_eq!(extract_messages(b"\n\n\n\nPING\n\n"), Ok((&b""[..], vec![Empty, Empty, Ping]))); |
|
283 assert_eq!(extract_messages(b"\n\n\nPING\n\n"), Ok((&b""[..], vec![Empty, Empty, Ping]))); |
|
284 } |
|