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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
//! Pokémon Showdown client.
//!
//! # Stability
//!
//! This crate is not stable, not even close. The APIs of this crate are
//! heavily experimented on, and there isn't going to be depreciation period for
//! removed features. Don't use this crate if you aren't prepared for constant
//! breakage.

pub mod message;

use self::message::{Message, Text};
#[cfg(feature = "chrono")]
pub use chrono;
use extension_trait::extension_trait;
use futures_util::future::TryFutureExt;
use futures_util::sink::Sink;
use futures_util::stream::Stream as FuturesStream;
use serde_derive::Deserialize;
use std::error::Error as StdError;
use std::fmt::{self, Display, Formatter};
use std::future::Future;
use std::pin::Pin;
use std::result::Result as StdResult;
use std::task::{Context, Poll};
use tokio::net::TcpStream;
use tokio_native_tls::TlsStream;
use tokio_tungstenite::stream::Stream as TungsteniteStream;
use tokio_tungstenite::tungstenite::{Error as WsError, Message as OwnedMessage};
use tokio_tungstenite::WebSocketStream;
pub use url;
use url::Url;

type SocketStream = WebSocketStream<TungsteniteStream<TcpStream, TlsStream<TcpStream>>>;

/// Message stream.
///
/// # Examples
///
/// ```no_run
/// use futures::SinkExt;
/// use showdown::message::{Kind, UpdateUser};
/// use showdown::{connect, ReceiveExt, Result, RoomId};
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let mut stream = connect("showdown").await?;
///     let message = stream.receive().await?;
///     match message.kind() {
///         Kind::UpdateUser(UpdateUser {
///             username,
///             named: false,
///             ..
///         }) => {
///             assert!(username.starts_with(" Guest "));
///         }
///         _ => panic!(),
///     }
///     Ok(())
/// }
/// ```
pub struct Stream {
    stream: SocketStream,
}

impl fmt::Debug for Stream {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        f.debug_struct("Stream").finish()
    }
}

impl Sink<SendMessage> for Stream {
    type Error = Error;

    fn poll_ready(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
        Pin::new(&mut self.stream)
            .poll_ready(cx)
            .map(Error::from_ws)
    }

    fn start_send(mut self: Pin<&mut Self>, item: SendMessage) -> Result<()> {
        Error::from_ws(Pin::new(&mut self.stream).start_send(OwnedMessage::Text(item.0)))
    }

    fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
        Pin::new(&mut self.stream)
            .poll_flush(cx)
            .map(Error::from_ws)
    }

    fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>> {
        Pin::new(&mut self.stream)
            .poll_close(cx)
            .map(Error::from_ws)
    }
}

impl FuturesStream for Stream {
    type Item = Result<Message>;

    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
        Pin::new(&mut self.stream).poll_next(cx).map(|opt| {
            opt.map(|e| {
                let message = Error::from_ws(e)?;
                if let OwnedMessage::Text(raw) = message {
                    Ok(Message { raw })
                } else {
                    Err(Error(ErrorInner::UnrecognizedMessage(message)))
                }
            })
        })
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        self.stream.size_hint()
    }
}

#[extension_trait(pub)]
impl<St> ReceiveExt for St
where
    St: FuturesStream<Item = Result<Message>> + Unpin,
{
    fn receive(&mut self) -> Receive<'_, Self> {
        Receive { stream: self }
    }
}

pub struct Receive<'a, St>
where
    St: ?Sized,
{
    stream: &'a mut St,
}

impl<St> Future for Receive<'_, St>
where
    St: FuturesStream<Item = Result<Message>> + Unpin,
{
    type Output = Result<Message>;

    fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<Message>> {
        Pin::new(&mut self.stream)
            .poll_next(cx)
            .map(|opt| opt.unwrap_or_else(|| Err(Error(ErrorInner::Disconnected))))
    }
}

#[derive(Debug)]
pub struct SendMessage(String);

impl SendMessage {
    /// Creates a global command.
    ///
    /// # Example
    ///
    /// ```no_run
    /// use futures::SinkExt;
    /// use showdown::message::{Kind, QueryResponse};
    /// use showdown::{connect, ReceiveExt, Result, RoomId, SendMessage};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<()> {
    ///     let mut stream = connect("showdown").await?;
    ///     stream.send(SendMessage::global_command("cmd rooms")).await?;
    ///     loop {
    ///         let received = stream.receive().await?;
    ///         if let Kind::QueryResponse(QueryResponse::Rooms(rooms)) = received.kind() {
    ///             assert!(rooms
    ///                 .official
    ///                 .iter()
    ///                 .any(|room| room.title == "Tournaments"));
    ///             return Ok(());
    ///         }
    ///     }
    /// }
    /// ```
    pub fn global_command(command: impl Display) -> Self {
        SendMessage(format!("|/{}", command))
    }

    /// Creates a command that executes in a chat room.
    ///
    /// # Examples
    ///
    /// ```no_run
    /// use futures::SinkExt;
    /// use showdown::message::{Kind, QueryResponse};
    /// use showdown::{connect, ReceiveExt, Result, RoomId, SendMessage};
    ///
    /// #[tokio::main]
    /// async fn main() -> Result<()> {
    ///     let mut stream = connect("showdown").await?;
    ///     stream.send(SendMessage::global_command("join lobby")).await?;
    ///     stream.send(SendMessage::chat_message(RoomId::LOBBY, "roomdesc")).await;
    ///     loop {
    ///         if let Kind::Html(html) = stream.receive().await?.kind() {
    ///             assert!(html.contains("Relax here amidst the chaos."));
    ///             return Ok(());
    ///         }
    ///     }
    /// }
    /// ```
    pub fn chat_message(room_id: RoomId<'_>, message: impl Display) -> Self {
        Self::prefixed(room_id, ' ', message)
    }

    /// Creates a chat room command message.
    pub fn chat_command(room_id: RoomId<'_>, command: impl Display) -> Self {
        Self::prefixed(room_id, '/', command)
    }

    pub fn broadcast_command(room_id: RoomId<'_>, command: impl Display) -> Self {
        Self::prefixed(room_id, '!', command)
    }

    pub fn reply(text: Text, message: impl Display) -> Self {
        match text {
            Text::Chat(chat) => Self::chat_message(chat.room_id(), message),
            Text::Private(private) => {
                Self::global_command(format_args!("pm {},{}", private.from, message))
            }
        }
    }

    fn prefixed(room_id: RoomId<'_>, prefix: char, message: impl Display) -> Self {
        SendMessage(format!("{}|{}{}", room_id.0, prefix, message))
    }
}

/// Connects to a named Showdown server.
///
/// Returns two structures, [`Sender`] can be used to send messages to Showdown,
/// while [`Receiver`] can be used to retrieve messages from Showdown. Due to
/// borrow checker, those structures are separate - it's practically necessary
/// to implement anything interesting.
///
/// # Examples
///
/// ```no_run
/// use showdown::{connect, Result};
///
/// #[tokio::main]
/// async fn main() {
///     assert!(connect("showdown").await.is_ok());
///     assert!(connect("fakestofservers").await.is_err());
/// }
/// ```
pub async fn connect(name: &str) -> Result<Stream> {
    connect_to_url(&fetch_server_url(name).await?).await
}

/// Connects to an URL.
///
/// This URL is provided by [`fetch_server_url`] function.
///
/// # Examples
///
/// ```no_run
/// use showdown::{connect_to_url, fetch_server_url, Result};
///
/// #[tokio::main]
/// async fn main() -> Result<()> {
///     let url = fetch_server_url("smogtours").await?;
///     assert_eq!(url.as_str(), "ws://sim3.psim.us:8002/showdown/websocket");
///     connect_to_url(&url).await?;
///     Ok(())
/// }
/// ```
pub async fn connect_to_url(url: &Url) -> Result<Stream> {
    let stream = Error::from_ws(tokio_tungstenite::connect_async(url).await)?.0;
    Ok(Stream { stream })
}

pub async fn fetch_server_url(name: &str) -> Result<Url> {
    let owned_url;
    let url = if name == "showdown" {
        "wss://sim3.psim.us/showdown/websocket"
    } else {
        let Server { host, port } = reqwest::get(&format!(
            "https://pokemonshowdown.com/servers/{}.json",
            name
        ))
        .and_then(|r| r.json())
        .await
        .map_err(|e| Error(ErrorInner::Reqwest(e)))?;
        let protocol = if port == 443 { "wss" } else { "ws" };
        // Concatenation is fine, as it's also done by the official Showdown client
        owned_url = format!("{}://{}:{}/showdown/websocket", protocol, host, port);
        &owned_url
    };
    Url::parse(url).map_err(|e| Error(ErrorInner::Url(e)))
}

#[derive(Deserialize)]
struct Server {
    host: String,
    port: u16,
}

#[derive(Copy, Clone, Debug)]
pub struct RoomId<'a>(pub &'a str);

impl RoomId<'_> {
    pub const LOBBY: RoomId<'static> = RoomId("");
}

pub type Result<T> = StdResult<T, Error>;

/// A specialized `Result` type for Showdown client operations.
#[derive(Debug)]
pub struct Error(ErrorInner);

impl Error {
    fn from_ws<T>(r: StdResult<T, tokio_tungstenite::tungstenite::Error>) -> Result<T> {
        r.map_err(|e| Error(ErrorInner::WebSocket(e)))
    }
}

#[derive(Debug)]
enum ErrorInner {
    WebSocket(WsError),
    Reqwest(reqwest::Error),
    Url(url::ParseError),
    Json(serde_json::Error),
    UnrecognizedMessage(OwnedMessage),
    Disconnected,
}

impl Display for Error {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        match &self.0 {
            ErrorInner::WebSocket(e) => e.fmt(f),
            ErrorInner::Reqwest(e) => e.fmt(f),
            ErrorInner::Url(e) => e.fmt(f),
            ErrorInner::Json(e) => e.fmt(f),
            ErrorInner::UnrecognizedMessage(e) => write!(f, "Unrecognized message: {:?}", e),
            ErrorInner::Disconnected => write!(f, "Disconnected"),
        }
    }
}

impl StdError for Error {
    fn source(&self) -> Option<&(dyn StdError + 'static)> {
        match &self.0 {
            ErrorInner::WebSocket(e) => Some(e),
            ErrorInner::Reqwest(e) => Some(e),
            ErrorInner::Url(e) => Some(e),
            ErrorInner::Json(e) => Some(e),
            ErrorInner::UnrecognizedMessage(_) | ErrorInner::Disconnected => None,
        }
    }
}