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
use super::adapter::{ReadAdapter, WriteAdapter}; use super::filter::{FilterMap, MessageFilter}; use super::handler::Handlers; use super::{Capability, Error, LocalUser, Message, Token, UserConfig, Writer}; /// 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, sync_adapters}; /// let stream = TestStream::new(); /// // create a synchronous read and write adapter (shorthand for SyncReadAdapter::new() and SyncWriteAdapter::new()) /// let (r, w) = sync_adapters(stream.clone(), stream.clone()); /// let mut client = Client::new(r,w); // moves the r,w /// // register, join, on, etc /// client.run().unwrap(); /// ``` pub struct Client<R> { reader: R, filters: FilterMap, handlers: Handlers, writer: Writer, } impl<R: ReadAdapter> Client<R> { /// 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<W>(reader: R, writer: W) -> Self where W: WriteAdapter + Send + 'static, { let (writer_, rx) = Writer::new(); let _ = std::thread::spawn(move || { log::trace!("starting write loop"); let mut w = writer; for msg in rx { if w.write_line(msg.as_bytes()).is_err() { break; } } log::trace!("ending write loop"); }); Self { reader, filters: FilterMap::default(), handlers: Handlers::default(), writer: writer_, } } /// Consumes the client, returning the reader pub fn into_reader(self) -> R::Reader { self.reader.into_inner() } /// 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)) => { log::warn!("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, *}; /// # let mut stream = TestStream::new(); /// # let (r, w) = sync_adapters(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, *}; /// # let mut stream = TestStream::new(); /// # let (r, w) = sync_adapters(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, *}; /// # let mut stream = TestStream::new(); /// # let (r, w) = sync_adapters(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 'pumps' the messages through the filter system /// /// Using this will drive the client (blocking for a read, then producing messages). /// Usage: /// ```no_run /// # use twitchchat::{helpers::TestStream, *}; /// # let mut stream = TestStream::new(); /// # let (r, w) = sync_adapters(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> { let msg = self.reader.read_message()?; log::trace!("<- {:?}", msg); { let w = self.writer(); if let Message::Irc(crate::irc::types::Message::Ping { token }) = &msg { return w .write_line(format!("PONG :{}", token)) .and_then(|_| Ok(msg)); } let key = msg.what_filter(); if let Some(filters) = self.filters.get_mut(key) { for filter in filters.iter_mut() { log::trace!("sending msg to filter (id: {}): {:?}", (filter.1).0, key); (filter.0)(msg.clone(), w.clone()) // when in doubt } } } log::trace!("begin dispatch"); self.handlers.handle(msg.clone()); log::trace!("end dispatch"); Ok(msg) } } impl<R> Client<R> { /** 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, *}; # let mut stream = TestStream::new(); # let (r, w) = sync_adapters(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) + 'static + Send + Sync, 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); log::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(&mut self, tok: Token) -> bool { let ok = self.handlers.remove(tok); log::trace!("tried to remove handler, id: {}, status: {}", tok, ok); ok } /// Get a clone of the writer pub fn writer(&self) -> Writer { self.writer.clone() } } // TODO rate limit: // 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