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
use crate::UserConfig;

type Stream = tokio::net::TcpStream;

/// Connect to Twitch without TLS. Using the provided [`UserConfig`][UserConfig].
///
/// This registers with the connection before returning it.
///
/// To connect using TLS:
///
/// enable one of:
/// * `tokio_rustls`
/// * `tokio_native_tls`
///
/// and then use the respective:
/// * [`twitchchat::native_tls::connect`][native_tls_connect]
/// * [`twitchchat::rustls::connect`][rustls_tls_connect]
///
/// [native_tls_connect]: ./native_tls/fn.connect.html
/// [rustls_tls_connect]: ./rustls/fn.connect.html
/// [UserConfig]: ./struct.UserConfig.html
///
/// # Example
/// ```rust,no_run
/// # use twitchchat::*;
/// # tokio::runtime::Runtime::new().unwrap().block_on(async move {
/// let user_config = UserConfig::builder().anonymous().build()?;
/// let mut stream = twitchchat::connect_no_tls(&user_config).await?;
/// # Ok::<_, Box<dyn std::error::Error>>(())
/// # }).unwrap();
/// ```
pub async fn connect_no_tls(config: &UserConfig) -> std::io::Result<Stream> {
    let mut stream = tokio::net::TcpStream::connect(crate::TWITCH_IRC_ADDRESS_TLS).await?;
    crate::register(config, &mut stream).await?;
    Ok(stream)
}

/// Connect to Twitch without TLS. Using the provided `name`, `token`.
///
/// This registers with the connection before returning it.
///
/// To connect using TLS:
///
/// enable one of:
/// * `tokio_rustls`
/// * `tokio_native_tls`
///
/// and then use the respective:
/// * [`twitchchat::native_tls::connect`][native_tls_connect]
/// * [`twitchchat::rustls::connect`][rustls_tls_connect]
///
/// [native_tls_connect]: ./native_tls/fn.connect.html
/// [rustls_tls_connect]: ./rustls/fn.connect.html
///
/// # Example
/// ```rust,no_run
/// # use twitchchat::*;
/// # tokio::runtime::Runtime::new().unwrap().block_on(async move {
/// let (name, token) = ANONYMOUS_LOGIN;
/// let mut stream = twitchchat::connect_easy_no_tls(&name, &token).await?;
/// # Ok::<_, Box<dyn std::error::Error>>(())
/// # }).unwrap();
/// ```
pub async fn connect_easy_no_tls(name: &str, token: &str) -> std::io::Result<Stream> {
    use std::io::{Error, ErrorKind};

    let mut stream = tokio::net::TcpStream::connect(crate::TWITCH_IRC_ADDRESS_TLS).await?;

    let config = crate::simple_user_config(name, token) //
        .map_err(|err| Error::new(ErrorKind::Other, err))?;

    crate::register(&config, &mut stream).await?;

    Ok(stream)
}