tmi 0.10.0

twitch.tv messaging interface
Documentation
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
//! ## Twitch IRC Client
//!
//! This is the main interface for interacting with Twitch IRC.
//! The entrypoint to this module is the [`Client`].
//!
//! The simplest way to get started is using [`Client::anonymous`],
//! which will connect to Twitch IRC anonymously.
//!
//! ```rust
//! # async fn run() -> anyhow::Result<()> {
//! let client = tmi::Client::anonymous().await?;
//! # Ok(())
//! # }
//! ```
//!
//! If you wish to be able to send messages, you have to generate an oauth2 token,
//! and provide it to the client:
//!
//! ```rust
//! # async fn run() -> anyhow::Result<()> {
//! let credentials = tmi::Credentials::new("your_username", "oauth:your_token");
//! let client = tmi::Client::builder().credentials(credentials).connect().await?;
//! # Ok(())
//! # }
//! ```
//!
//! and then use [`Client::builder`] followed by [`ClientBuilder::credentials`].
//!
//! Generating an oauth2 token is out of scope for this library.
//! Head over to the [official documentation](https://dev.twitch.tv/docs/irc/authenticate-bot/#getting-an-access-token)
//! to see how you can generate one. [twitch_oauth2](https://crates.io/crates/twitch_oauth2) may be used to automate most of it.
//!
//! ⚠ Note: [`Client`] is a fairly low-level interface! It does not automatically handle:
//! - Rate limiting (both for JOINs and PRIVMSGs)
//! - Same message bypass
//! - `RECONNECT` commands
//! - Rejoining channels
//! - Latency measurement
//!
//! What it _does_ provide is:
//! - Opening a TCP connection (with TLS) to Twitch.
//! - Performing the handshake (authentication, capability negotiation)
//! - Reconnect with backoff
//! - A polling interface for receiving messages
//! - Sending commands (PRIVMSG, JOIN, PONG, etc.)

#[macro_use]
mod macros;

pub mod conn;
pub mod read;
pub mod util;
pub mod write;

use self::conn::{OpenStreamError, TlsConfig, TlsConfigError};
use self::read::{ReadStream, RecvError};
use self::write::WriteStream;
use crate::irc::Command;
use crate::IrcMessage;
use futures_util::StreamExt;
use rand::{thread_rng, Rng};
use std::fmt::{Display, Write};
use std::future::Future;
use std::io;
use std::time::Duration;
use tokio::io::{AsyncBufReadExt, AsyncWriteExt, BufReader};
use tokio_rustls::rustls::client::InvalidDnsNameError;
use tokio_rustls::rustls::ServerName;
use tokio_stream::wrappers::LinesStream;
use util::Timeout;

/// The default timeout used when connecting to Twitch IRC.
pub const DEFAULT_TIMEOUT: Duration = Duration::from_secs(30);

/// Credentials used to authenticate to Twitch IRC.
///
/// The [`Default`] impl uses [`Credentials::anon`].
#[derive(Clone)]
pub struct Credentials {
  pub login: String,
  pub token: Option<String>,
}

impl Credentials {
  const ANON_RANGE: std::ops::Range<u32> = 10000..99999;

  /// Credentials using an OAuth token.
  ///
  /// `token` should be a User Access Token.
  ///
  /// You can generate one by following the instructions on [Authorization Code Grant Flow](https://dev.twitch.tv/docs/authentication/getting-tokens-oauth/#authorization-code-grant-flow).
  ///
  /// Make sure the token is valid before attempting to use it, and refresh it or generate a new one if it expires.
  ///
  /// [twitch_oauth2](https://crates.io/crates/twitch_oauth2) can help automate most of this.
  pub fn new(login: impl ToString, token: impl ToString) -> Self {
    Self {
      login: login.to_string(),
      token: Some(token.to_string()),
    }
  }

  /// An anonymous login.
  ///
  /// Twitch allows logging in using any username in the form `justinfan?????`
  /// where `?` is any digit. For example, `justinfan11824` is a valid username.
  ///
  /// If you login anonymously, you won't be able to send messages, but you
  /// will still be able to read them, including all the usual tags,
  /// membership commands, etc.
  pub fn anon() -> Self {
    Self {
      token: None,
      login: format!("justinfan{}", thread_rng().gen_range(Self::ANON_RANGE)),
    }
  }

  pub fn is_anon(&self) -> bool {
    self.token.is_none()
  }

  pub fn login(&self) -> &str {
    self.login.as_str()
  }

  pub fn token(&self) -> Option<&str> {
    self.token.as_deref()
  }
}

impl Default for Credentials {
  fn default() -> Self {
    Self::anon()
  }
}

impl std::fmt::Debug for Credentials {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.debug_struct("Credentials")
      .field("nick", &self.login)
      .finish_non_exhaustive()
  }
}

/// Reconnect backoff configuration.
#[derive(Clone, Copy, Debug)]
pub struct Backoff {
  /// The maximum number of reconnect attempts to make.
  pub max_tries: Option<u64>,

  /// Before attempting the first reconnect, the client will wait this long.
  pub initial_delay: Duration,

  /// After each failed reconnect attempt, the delay will be multiplied by this value.
  pub delay_multiplier: u32,

  /// The maximum delay to wait inbetween connection attempts.
  pub max_delay: Duration,
}

impl Default for Backoff {
  fn default() -> Self {
    Self {
      max_tries: Some(8),
      initial_delay: Duration::from_secs(1),
      delay_multiplier: 3,
      max_delay: Duration::from_secs(12),
    }
  }
}

/// Client configuration.
#[derive(Clone, Debug)]
pub struct Config {
  /// Credentials to use when logging in to Twitch IRC.
  pub credentials: Credentials,

  /// Connect and reconnect timeout.
  pub timeout: Duration,

  /// Reconnect backoff.
  pub backoff: Backoff,
}

impl Default for Config {
  fn default() -> Self {
    Self {
      credentials: Default::default(),
      timeout: DEFAULT_TIMEOUT,
      backoff: Default::default(),
    }
  }
}

/// Builder for a [`Client`].
pub struct ClientBuilder {
  config: Config,
}

impl ClientBuilder {
  /// Set the credentials.
  pub fn credentials(mut self, credentials: Credentials) -> Self {
    self.config.credentials = credentials;
    self
  }

  /// Set the timeout used on various operations, such as connecting and reconnecting.
  pub fn timeout(mut self, timeout: Duration) -> Self {
    self.config.timeout = timeout;
    self
  }

  /// Set the backoff settings used when reconnecting.
  pub fn backoff(mut self, backoff: Backoff) -> Self {
    self.config.backoff = backoff;
    self
  }

  /// Attempts to connect to Twitch IRC using this configuration.
  pub fn connect(self) -> impl Future<Output = Result<Client, ConnectError>> {
    Client::connect(self.config)
  }
}

/// Twitch IRC client.
///
/// This is the main interface for interacting with Twitch IRC.
///
/// This interface provides:
/// - Connection handshake
/// - Reconnect with backoff
/// - Receiving and sending messages
///
/// It is a low-level interface, which means it does not automatically handle:
/// - Rate limiting
/// - Same message bypass
/// - Reconnects / rejoining channels
/// - Latency measurement
pub struct Client {
  reader: ReadStream,
  writer: WriteStream,

  scratch: String,
  tls: TlsConfig,
  config: Config,
}

impl Client {
  /// The [`ClientBuilder`] provides a builder for setting up the client configuration.
  pub fn builder() -> ClientBuilder {
    ClientBuilder {
      config: Default::default(),
    }
  }

  /// Attemps to connect anonymously.
  pub fn anonymous() -> impl Future<Output = Result<Client, ConnectError>> {
    Self::connect(Config::default())
  }

  /// Attempts to connect with the provided `config` and `timeout`.
  async fn connect(config: Config) -> Result<Client, ConnectError> {
    trace!("connecting");
    let tls = TlsConfig::load(ServerName::try_from(conn::HOST)?)?;
    trace!("opening connection to twitch");
    let timeout = config.timeout;
    let stream = conn::open(tls.clone()).timeout(timeout).await??;
    let (reader, writer) = split(stream);
    let mut client = Client {
      reader,
      writer,
      scratch: String::with_capacity(1024),
      tls,
      config,
    };
    client.handshake().timeout(timeout).await??;
    Ok(client)
  }

  /// Attempt to reconnect to Twitch IRC.
  pub async fn reconnect(&mut self) -> Result<(), ReconnectError> {
    trace!("reconnecting");

    let backoff = self.config.backoff;
    let timeout = self.config.timeout;
    let mut tries = backoff.max_tries;
    let mut delay = backoff.initial_delay;
    let mut cause = ConnectError::Timeout;
    while matches!(tries, None | Some(1..)) {
      tokio::time::sleep(delay).await;
      if let Some(tries) = &mut tries {
        *tries -= 1;
      }
      delay = std::cmp::min(backoff.max_delay, delay * backoff.delay_multiplier);

      trace!("opening connection to twitch");
      let stream = match conn::open(self.tls.clone()).timeout(timeout).await? {
        Ok(stream) => stream,
        Err(e @ OpenStreamError::Io(_)) => {
          cause = e.into();
          continue;
        }
      };

      (self.reader, self.writer) = split(stream);

      if let Err(e) = self.handshake().timeout(timeout).await? {
        if e.should_retry() {
          cause = e;
          continue;
        }
        return Err(e.into());
      };

      return Ok(());
    }

    Err(ReconnectError { cause })
  }

  async fn handshake(&mut self) -> Result<(), ConnectError> {
    trace!("performing handshake");

    let credentials = &self.config.credentials;
    const CAP: &str = "twitch.tv/commands twitch.tv/tags twitch.tv/membership";
    trace!("CAP REQ {CAP:?}; NICK {:?}; PASS ***", credentials.login);
    write!(&mut self.scratch, "CAP REQ :{CAP}\r\n").unwrap();

    let login = credentials.login.as_str();
    let token = match credentials.token.as_ref() {
      Some(token) => token.as_str(),
      None => "just_a_lil_guy",
    };
    let oauth = if token.starts_with("oauth:") {
      ""
    } else {
      "oauth:"
    };
    write!(&mut self.scratch, "PASS {oauth}{token}\r\n").unwrap();
    write!(&mut self.scratch, "NICK {login}\r\n").unwrap();

    self.writer.write_all(self.scratch.as_bytes()).await?;
    self.writer.flush().await?;
    self.scratch.clear();

    trace!("waiting for CAP * ACK");
    let message = self.recv().timeout(Duration::from_secs(5)).await??;
    trace!(?message, "received message");

    match message.command() {
      Command::Capability => {
        if message.params().is_some_and(|v| v.starts_with("* ACK")) {
          trace!("received CAP * ACK")
        } else {
          return Err(ConnectError::Auth);
        }
      }
      _ => {
        trace!("unexpected message");
        return Err(ConnectError::Welcome(message));
      }
    }

    trace!("waiting for NOTICE 001");
    let message = self.recv().timeout(Duration::from_secs(5)).await??;
    trace!(?message, "received message");

    match message.command() {
      Command::RplWelcome => {
        trace!("connected");
      }
      Command::Notice => {
        if message
          .params()
          .map(|v| v.contains("authentication failed"))
          .unwrap_or(false)
        {
          trace!("invalid credentials");
          return Err(ConnectError::Auth);
        }

        trace!("unrecognized error");
        return Err(ConnectError::Notice(message));
      }
      _ => {
        trace!("first message not recognized");
        return Err(ConnectError::Welcome(message));
      }
    }

    Ok(())
  }
}

impl Client {
  #[inline]
  pub fn config(&self) -> &Config {
    &self.config
  }

  #[inline]
  pub fn credentials(&self) -> &Credentials {
    &self.config.credentials
  }
}

fn split(stream: conn::Stream) -> (ReadStream, WriteStream) {
  let (reader, writer) = tokio::io::split(stream);

  (
    LinesStream::new(BufReader::new(reader).lines()).fuse(),
    writer,
  )
}

/// An error which occurred while attempting to reconnect to Twitch IRC.
#[derive(Debug)]
pub struct ReconnectError {
  /// The last encountered error.
  pub cause: ConnectError,
}

impl Display for ReconnectError {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    write!(
      f,
      "all reconnect attempts failed. last error was: {}",
      self.cause
    )
  }
}

impl std::error::Error for ReconnectError {
  fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
    Some(&self.cause)
  }

  fn cause(&self) -> Option<&dyn std::error::Error> {
    self.source()
  }
}

impl<T: Into<ConnectError>> From<T> for ReconnectError {
  fn from(cause: T) -> Self {
    Self {
      cause: cause.into(),
    }
  }
}

/// An error which occurred while attempting to connect to Twitch IRC.
#[derive(Debug)]
pub enum ConnectError {
  /// Failed to read from the stream.
  Read(RecvError),

  /// Failed to perform an IO operation on the stream.
  Io(io::Error),

  /// Failed to query DNS.
  Dns(InvalidDnsNameError),

  /// Failed to establish TLS connection.
  Tls(TlsConfigError),

  /// Failed to open a connection.
  Open(OpenStreamError),

  /// Connection timed out.
  Timeout,

  /// Connection received invalid welcome message.
  Welcome(IrcMessage),

  /// Failed to connect because of invalid credentials.
  Auth,

  /// Twitch sent a notice that we didn't expect during the handshake.
  Notice(IrcMessage),
}

impl ConnectError {
  fn should_retry(&self) -> bool {
    matches!(self, Self::Open(OpenStreamError::Io(_)) | Self::Io(_))
  }
}

impl From<RecvError> for ConnectError {
  fn from(value: RecvError) -> Self {
    Self::Read(value)
  }
}

impl From<io::Error> for ConnectError {
  fn from(value: io::Error) -> Self {
    Self::Io(value)
  }
}

impl From<InvalidDnsNameError> for ConnectError {
  fn from(value: InvalidDnsNameError) -> Self {
    Self::Dns(value)
  }
}

impl From<TlsConfigError> for ConnectError {
  fn from(value: TlsConfigError) -> Self {
    Self::Tls(value)
  }
}

impl From<OpenStreamError> for ConnectError {
  fn from(value: OpenStreamError) -> Self {
    Self::Open(value)
  }
}

impl From<tokio::time::error::Elapsed> for ConnectError {
  fn from(_: tokio::time::error::Elapsed) -> Self {
    Self::Timeout
  }
}

impl Display for ConnectError {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    match self {
      ConnectError::Read(e) => write!(f, "failed to connect: {e}"),
      ConnectError::Io(e) => write!(f, "failed to connect: {e}"),
      ConnectError::Dns(e) => write!(f, "failed to connect: {e}"),
      ConnectError::Tls(e) => write!(f, "failed to connect: {e}"),
      ConnectError::Open(e) => write!(f, "failed to connect: {e}"),
      ConnectError::Timeout => write!(f, "failed to connect: connection timed out"),
      ConnectError::Welcome(msg) => write!(
        f,
        "failed to connect: expected `NOTICE` or `001` as first message, instead received: {msg:?}"
      ),
      ConnectError::Auth => write!(f, "failed to connect: invalid credentials"),
      ConnectError::Notice(msg) => write!(
        f,
        "failed to connect: received unrecognized notice: {msg:?}"
      ),
    }
  }
}

impl std::error::Error for ConnectError {}

static_assert_send!(Client);
static_assert_sync!(Client);