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
use crate::{register::register, simple_user_config, UserConfig, TWITCH_IRC_ADDRESS};

#[cfg(any(feature = "tokio_native_tls", feature = "tokio_rustls"))]
use crate::TWITCH_IRC_ADDRESS_TLS;

#[cfg(any(feature = "tokio_native_tls", feature = "tokio_rustls"))]
const TWITCH_DOMAIN: &str = "irc.chat.twitch.tv";

type TokioStream = tokio::net::TcpStream;

#[cfg(feature = "tokio_native_tls")]
type TokioTlsStream = tokio_tls::TlsStream<tokio::net::TcpStream>;

#[cfg(feature = "tokio_rustls")]
type TokioTlsStream = tokio_rustls::client::TlsStream<tokio::net::TcpStream>;

/**
Opens an ***async*** TCP connection using the provided `UserConfig`.

# Example
```rust,no_run
# use twitchchat::*;
# tokio::runtime::Runtime::new().unwrap().block_on(async move {
let user_config = UserConfig::builder().anonymous().build().unwrap();
let stream = connect(&user_config).await.unwrap();
# });
```
*/
pub async fn connect(user_config: &UserConfig) -> std::io::Result<TokioStream> {
    let mut stream = tokio::net::TcpStream::connect(TWITCH_IRC_ADDRESS).await?;
    register(user_config, &mut stream).await?;
    Ok(stream)
}

/**
Opens an ***async*** TCP connection with _TLS_ using the provided `UserConfig`.

# Example
```rust,no_run
# use twitchchat::*;
# tokio::runtime::Runtime::new().unwrap().block_on(async move {
let user_config = UserConfig::builder().anonymous().build().unwrap();
let stream = connect_tls(&user_config).await.unwrap();
# });
```
*/
#[cfg(any(feature = "tokio_native_tls", feature = "tokio_rustls"))]
#[cfg_attr(
    docsrs,
    doc(cfg(any(feature = "tokio_native_tls", feature = "tokio_rustls")))
)]
pub async fn connect_tls(user_config: &UserConfig) -> std::io::Result<TokioTlsStream> {
    let stream = tokio::net::TcpStream::connect(TWITCH_IRC_ADDRESS_TLS).await?;
    let mut stream = tls_connect(stream).await?;
    register(user_config, &mut stream).await?;
    Ok(stream)
}

/**
Opens an ***async*** TCP connection using the provided `name`, `token`.

This enables all of the [Capabilities]

[Capabilities]: ./enum.Capability.html
# Example
```rust,no_run
# use twitchchat::*;
# tokio::runtime::Runtime::new().unwrap().block_on(async move {
let (nick, pass) = ANONYMOUS_LOGIN;
let stream = connect_easy(nick, pass).await.unwrap();
# });
```
*/
pub async fn connect_easy(name: &str, token: &str) -> std::io::Result<TokioStream> {
    let config = simple_user_config(name, token).unwrap();
    connect(&config).await
}

/**
Opens an ***async*** TCP connection with _TLS_ using the provided `name`, `token`.

This enables all of the [Capabilities]

[Capabilities]: ./enum.Capability.html
# Example
```rust,no_run
# use twitchchat::*;
# tokio::runtime::Runtime::new().unwrap().block_on(async move {
let (nick, pass) = ANONYMOUS_LOGIN;
let stream = connect_easy_tls(nick, pass).await.unwrap();
# });
```
*/
#[cfg(any(feature = "tokio_native_tls", feature = "tokio_rustls"))]
#[cfg_attr(
    docsrs,
    doc(cfg(any(feature = "tokio_native_tls", feature = "tokio_rustls")))
)]
pub async fn connect_easy_tls(name: &str, token: &str) -> std::io::Result<TokioTlsStream> {
    let config = simple_user_config(name, token).unwrap();
    connect_tls(&config).await
}

#[cfg(feature = "tokio_native_tls")]
async fn tls_connect(stream: TokioStream) -> std::io::Result<TokioTlsStream> {
    use std::io::{Error, ErrorKind};

    let conn: tokio_tls::TlsConnector = native_tls::TlsConnector::new()
        .map_err(|err| Error::new(ErrorKind::Other, err))?
        .into();

    let stream = conn
        .connect(TWITCH_DOMAIN, stream)
        .await
        .map_err(|err| Error::new(ErrorKind::Other, err))?;

    Ok(stream)
}

#[cfg(feature = "tokio_rustls")]
async fn tls_connect(stream: TokioStream) -> std::io::Result<TokioTlsStream> {
    use std::io::{Error, ErrorKind};

    // This isn't actually used by rustls, but is required 'for the future'.
    let domain_name = tokio_rustls::webpki::DNSNameRef::try_from_ascii_str(TWITCH_DOMAIN)
        .expect("valid twitch domain dns/ref");

    // Not sure which default roots to use, so lets trust the server
    let mut config = tokio_rustls::rustls::ClientConfig::new();
    config
        .root_store
        .add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);

    let connector = tokio_rustls::TlsConnector::from(std::sync::Arc::new(config));
    let stream = connector
        .connect(domain_name, stream)
        .await
        .map_err(|err| Error::new(ErrorKind::Other, err))?;

    Ok(stream)
}