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
use log::*;
use std::io::{BufRead, BufReader, Read, Write};
use std::sync::Arc;

use super::filter::{FilterMap, MessageFilter};
use super::{commands, Capability, Error, LocalUser, Message, MutexWrapper as Mutex, Writer};

use crate::irc::types::Message as IrcMessage;
use crate::UserConfig;

use super::handler::Handlers;
use super::Token;

// 20 per 30 seconds	Users sending commands or messages to channels in which they do not have Moderator or Operator status
// 100 per 30 seconds	Users sending commands or messages to channels in which they have Moderator or Operator status

/// Client for interacting with Twitch's chat.
///
/// It wraps a [Read](https://doc.rust-lang.org/std/io/trait.Read.html),
/// [Write](https://doc.rust-lang.org/std/io/trait.Write.html) pair
///
/// ```no_run
/// use twitchchat::{helpers::TestStream, Client};
/// let stream = TestStream::new();
/// let (r,w) = (stream.clone(), stream.clone());
/// let mut client = Client::new(r,w); // moves the r,w
/// // register, join, on, etc
/// client.run().unwrap();
/// ```
// TODO write usage
pub struct Client<R, W> {
    reader: BufReader<R>,
    filters: FilterMap<W>,
    handlers: Handlers,
    writer: Writer<W>,
}

impl<R: Read, W: Write> Client<R, W> {
    /// Create a new Client from a
    /// [Read](https://doc.rust-lang.org/std/io/trait.Read.html),
    /// [Write](https://doc.rust-lang.org/std/io/trait.Write.html) pair
    ///
    /// This client is clonable, and thread safe.
    pub fn new(read: R, write: W) -> Self {
        Self {
            reader: BufReader::new(read),
            filters: FilterMap::default(),
            handlers: Handlers::default(),
            writer: Writer(Arc::new(Mutex::new(write))),
        }
    }

    /// Runs, consuming all messages.
    ///
    /// This also pumping them through
    /// [`Client::on`](./struct.Client.html#method.on) filters
    pub fn run(mut self) -> Result<(), Error> {
        loop {
            match self.read_message() {
                Ok(..) => (),
                Err(Error::InvalidMessage(msg)) => {
                    warn!("read an invalid message: {}", msg);
                    continue;
                }
                Err(err) => return Err(err),
            }
        }
    }

    /// Registers with the server uses the provided [`UserConfig`](./struct.UserConfig.html)
    ///
    /// This is a **very** useful step, after you make the client and set up your initial filters
    ///
    /// You should call this to send your `OAuth token` and `Nickname`
    ///
    /// This also sends the [`Capabilities`](./enum.Capability.html) in the correct order
    ///
    /// Usage
    /// ```no_run
    /// # use twitchchat::{helpers::TestStream, Client, UserConfig};
    /// # let mut stream = TestStream::new();
    /// # let (r, w) = (stream.clone(), stream.clone());
    /// # let mut client = Client::new(r, w);
    /// let config = UserConfig::builder()
    ///                 .token(std::env::var("MY_PASSWORD").unwrap())
    ///                 .nick("museun")
    ///                 .build()
    ///                 .unwrap();
    /// client.register(config).unwrap();
    /// // we should be connected now
    /// // this'll block until everything is read
    /// let _ = client.wait_for_ready().unwrap();
    /// ```
    pub fn register<U>(&mut self, config: U) -> Result<(), Error>
    where
        U: std::borrow::Borrow<UserConfig>,
    {
        let config = config.borrow();
        for cap in config.caps.iter().filter_map(|c| c.get_command()) {
            self.writer.write_line(cap)?;
        }

        self.writer.write_line(&format!("PASS {}", config.token))?;
        self.writer.write_line(&format!("NICK {}", config.nick))
    }

    /// Waits for the
    /// [`GLOBALUSERSTATE`](./commands/struct.GlobalUserState.html) before
    /// continuing, discarding any messages received
    ///
    /// Returns some [`useful information`](./struct.LocalUser.html) about your user
    ///
    /// This blocks until the twitch registration is completed, this relies on
    /// the [`Tags Capability`](./enum.Capability.html#variant.Tags) being sent.
    ///
    /// Usage:
    /// ```no_run
    /// # use twitchchat::{helpers::TestStream, Client};
    /// # let mut stream = TestStream::new();
    /// # let (r, w) = (stream.clone(), stream.clone());
    /// # let mut client = Client::new(r, w);
    /// match client.wait_for_ready() {
    ///     Ok(user) => println!("user id: {}", user.user_id),
    ///     Err(err) => panic!("failed to finish registration: {}", err)
    /// };
    /// // we can be sure that we're ready to join
    /// client.writer().join("some_channel").unwrap();
    /// ```
    pub fn wait_for_ready(&mut self) -> Result<LocalUser, Error> {
        use crate::irc::types::Message as IRCMessage;
        let mut caps = vec![];

        loop {
            match self.read_message()? {
                Message::Irc(IRCMessage::Cap {
                    acknowledge: true,
                    cap,
                }) => match cap.as_str() {
                    "twitch.tv/tags" => caps.push(Capability::Tags),
                    "twitch.tv/membership" => caps.push(Capability::Membership),
                    "twitch.tv/commands" => caps.push(Capability::Commands),
                    _ => {}
                },

                Message::Irc(IRCMessage::Ready { .. }) => {
                    let mut bad = vec![];
                    match (
                        caps.contains(&Capability::Tags),
                        caps.contains(&Capability::Commands),
                    ) {
                        (true, true) => continue,

                        (false, true) => bad.push(Capability::Tags),
                        (true, false) => bad.push(Capability::Commands),
                        _ => {
                            bad.push(Capability::Tags);
                            bad.push(Capability::Commands);
                        }
                    };

                    if !bad.is_empty() {
                        return Err(Error::CapabilityRequired(bad));
                    }
                }

                Message::GlobalUserState(state) => {
                    return Ok(LocalUser {
                        user_id: state.user_id(),
                        display_name: state.display_name().map(ToString::to_string),
                        color: state.color(),
                        badges: state.badges(),
                        emote_sets: state.emote_sets(),
                        caps,
                    });
                }
                _ => continue,
            }
        }
    }

    /// Like [`wait_for_ready`](./struct.Client.html#method.wait_for_ready) but waits for the end of the IRC MOTD
    ///
    /// This will generally happen before `GLOBALUSERSTATE` but don't rely on that
    ///
    /// Returns the username assigned to you by the server
    ///
    /// Usage:
    /// ```no_run
    /// # use twitchchat::{helpers::TestStream, Client};
    /// # let mut stream = TestStream::new();
    /// # let (r, w) = (stream.clone(), stream.clone());
    /// # let mut client = Client::new(r, w);
    /// match client.wait_for_irc_ready() {
    ///     Ok(name) => println!("end of motd, our name is: {}", name),
    ///     Err(err) => panic!("failed to finish registration: {}", err),
    /// };
    /// // we can be sure that we're ready to join
    /// client.writer().join("some_channel").unwrap();
    /// ```
    pub fn wait_for_irc_ready(&mut self) -> Result<String, Error> {
        use crate::irc::types::Message as IrcMessage;
        loop {
            match self.read_message()? {
                Message::Irc(IrcMessage::Ready { name }) => return Ok(name),
                _ => continue,
            }
        }
    }

    /// Reads a [`Message`](./enum.Message.html#variants)
    ///
    /// This will automatically handle some *tedious* messages, like the _heartbeat_ (PING)
    ///
    /// This also 'pumps' the messages through the filter
    ///
    /// Using this will drive the client (blocking for a read, then producing messages).
    /// Usage:
    /// ```no_run
    /// # use twitchchat::{helpers::TestStream, Client};
    /// # let mut stream = TestStream::new();
    /// # let (r, w) = (stream.clone(), stream.clone());
    /// # let mut client = Client::new(r, w);
    /// // block the thread (i.e. wait for the client to close down)    
    /// while let Ok(msg) = client.read_message() {
    ///     // match msg {
    ///     // .. stuff
    ///     // }
    /// }
    ///
    /// // or incrementally calling `client.read_message()`
    /// // when you want the next message
    /// ```
    pub fn read_message(&mut self) -> Result<Message, Error> {
        // TODO provide an internal buffer to prevent this dumb allocation
        // using https://docs.rs/bytes/0.4.11/bytes/

        let mut buf = String::new();
        let len = self.reader.read_line(&mut buf).map_err(Error::Read)?;
        // 0 == EOF
        if len == 0 {
            return Err(Error::CannotRead);
        }

        let buf = buf.trim_end();
        if buf.is_empty() {
            return Err(Error::CannotRead);
        }

        trace!("trying to parse message");
        let msg = IrcMessage::parse(&buf) //
            .ok_or_else(|| Error::InvalidMessage(buf.to_string()))?;
        trace!("parsed message");

        // handle PINGs automatically
        if let IrcMessage::Ping { token } = &msg {
            self.writer.write_line(&format!("PONG :{}", token))?;
        }

        // sanity check, doing it here instead of after its been re-parsed to fail early
        if let IrcMessage::Unknown {
            prefix,
            head,
            args,
            tail,
            ..
        } = &msg
        {
            if let (Some(crate::irc::types::Prefix::Server { host }), Some(data)) = (prefix, tail) {
                if head == "NOTICE"
                    && host == "tmi.twitch.tv"
                    && data == "Improperly formatted auth"
                    // excellent
                    && args.get(0) == Some(&"*".into())
                {
                    trace!("got a registartion error");
                    return Err(Error::InvalidRegistration);
                }
            }
        }

        let msg = commands::parse(&msg).unwrap_or_else(|| Message::Irc(msg));
        trace!("<- {:?}", msg);
        {
            let w = self.writer();
            let key = msg.what_filter();
            if let Some(filters) = self.filters.get_mut(key) {
                for filter in filters.iter_mut() {
                    trace!("sending msg to filter (id: {}): {:?}", (filter.1).0, key);
                    (filter.0)(msg.clone(), w.clone()) // when in doubt
                }
            }
        }

        trace!("begin dispatch");
        self.handlers.handle(msg.clone());
        trace!("end dispatch");

        Ok(msg)
    }
}

impl<R, W: Write> Client<R, W> {
    /**
    When a message is received run this function with it and a clone of the Writer.

    The type of the closure determines what is filtered

    Usage:
    ```no_run
    # use twitchchat::{helpers::TestStream, Client, Writer};
    # let mut stream = TestStream::new();
    # let (r, w) = (stream.clone(), stream.clone());
    # let mut client = Client::new(r, w);
    use twitchchat::commands::*;
    let pm_tok = client.on(|msg: PrivMsg, w: Writer<_>| {
        // msg is now a `twitchchat::commands::PrivMsg`
    });
    let join_tok = client.on(|msg: Join, w: Writer<_>| {
        // msg is now a `twitchchat::commands::Join`
    });

    // if a PRIVMSG or JOIN is parsed here
    // the corresponding closure, above, will run
    client.read_message();
    ```

    The available filters are the same names as the structs in
    [commands](./commands/index.html#structs)

    When [`Client::read_message`](./struct.Client.html#method.read_message)
    is called, it'll send a copy of the matching message to these filters.

    Multiple filters can be 'registered' for the same type

    Use the returned token to remove the filter, by passing it to the
    [`Client::off`](./struct.Client.html#method.off) method
    */
    pub fn on<F, T>(&mut self, mut f: F) -> Token
    where
        F: FnMut(T, Writer<W>) + 'static + Send + Sync, // hmm
        T: From<Message>,
        T: MessageFilter,
    {
        let filter = T::to_filter();
        self.filters
            .insert(filter, Box::new(move |msg, w| f(msg.into(), w)))
    }

    /// Remove a previously registered message filter, using the token returned by `on`
    ///
    /// Returns true if this filter existed
    pub fn off(&mut self, tok: Token) -> bool {
        self.filters.try_remove(tok)
    }

    /**
    Add a [`Handler`](./trait.Handler.html) to the internal filtering

    When [`Client::read_message`](./struct.Client.html#method.read_message)
    is called, it'll send a RC message to the appropriate function.

    Use the returned token to remove the filter, by passing it to the
    [`Client::remove_handler`](./struct.Client.html#method.remove_handler) method

    */
    pub fn handler<H>(&mut self, handler: H) -> Token
    where
        H: super::Handler + Send + Sync + 'static,
    {
        let tok = self.handlers.add(handler);
        trace!("add handler, id: {}", tok);
        tok
    }

    /// Remove a previously added handler, using the returned token
    ///
    /// Returns true if this handler existed
    pub fn remove_handler<H>(&mut self, tok: Token) -> bool {
        let ok = self.handlers.remove(tok);
        trace!("tried to remove handler, id: {}, status: {}", tok, ok);
        ok
    }

    /// Get a clone of the internal writer
    pub fn writer(&self) -> Writer<W> {
        self.writer.clone()
    }
}