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
use std::io::Write;
use std::sync::Arc;

use super::{Channel, Error, MutexWrapper as Mutex, TwitchColor};

/// A thread-safe, clonable writer for the Twitch client
pub struct Writer<W>(pub Arc<Mutex<W>>);

impl<W: Write> Clone for Writer<W> {
    fn clone(&self) -> Self {
        Self(Arc::clone(&self.0))
    }
}

impl<W: Write> Writer<W> {
    pub(crate) fn write_line<S: AsRef<[u8]>>(&self, data: S) -> Result<(), Error> {
        let mut write = self.0.lock();
        write
            .write_all(data.as_ref())
            .and_then(|_| write.write_all(b"\r\n"))
            .and_then(|_| write.flush())
            .map_err(Error::Write)
    }

    // TODO: https://dev.twitch.tv/docs/irc/guide/#scopes-for-irc-commands
    // /host,        /unhost 	       channel_editor
    // /marker	                       channel_editor
    // /raid,        /unraid 	       channel_editor
    // /color	                       chat:edit
    // /disconnect	                   chat:edit
    // /help	                       chat:edit
    // /me	                           chat:edit
    // /mods	                       chat:edit
    // /vips	                       chat:edit
    // /commercial	                   channel_commercial
    // /ban,         /unban            channel:moderate
    // /clear	                       channel:moderate
    // /delete	                       channel:moderate
    // /emoteonly,   /emoteonlyoff     channel:moderate
    // /followers,   /followersoff 	   channel:moderate
    // /mod,         /unmod 	       channel:moderate
    // /r9kbeta,     /r9kbetaoff 	   channel:moderate
    // /slow,        /slowoff 	       channel:moderate
    // /subscribers, /subscribersoff   channel:moderate
    // /timeout,     /untimeout 	   channel:moderate
    // /vip,         /unvip	           channel:moderate
    // /w	                           whispers:edit

    /// Host another channel.
    ///
    /// Use [`Writer::unhost`](./struct.Writer.html#method.unhost) to unset host mode.
    pub fn host<C>(&self, channel: C) -> Result<(), Error>
    where
        C: Into<Channel>,
    {
        let channel = Channel::validate(channel)?;
        self.command(format!("/host {}", *channel))
    }

    /// Stop hosting another channel.
    pub fn unhost(&self) -> Result<(), Error> {
        self.command("/unhost")
    }

    /// Adds a stream marker (with an optional comment, max 140 characters) at the current timestamp.
    ///
    /// You can use markers in the Highlighter for easier editing.
    pub fn marker(&self, comment: Option<&str>) -> Result<(), Error> {
        match comment {
            Some(comment) => {
                // TODO use https://github.com/unicode-rs/unicode-width
                let cmd = if comment.len() <= 140 {
                    format!("/marker {}", comment)
                } else {
                    let comment = comment.chars().take(140).collect::<String>();
                    format!("/marker {}", comment)
                };
                self.command(cmd)
            }
            _ => self.command("/marker"),
        }
    }

    /// Raid another channel.
    ///
    /// Use [`Writer::unraid`](./struct.Writer.html#method.unraid) to cancel the Raid.
    pub fn raid<C>(&self, channel: C) -> Result<(), Error>
    where
        C: Into<Channel>,
    {
        let channel = Channel::validate(channel)?;
        self.command(format!("/raid {}", *channel))
    }

    /// Cancel the Raid.
    pub fn unraid(&self) -> Result<(), Error> {
        self.command("/unraid")
    }

    /// Change your username color.
    pub fn color<C>(&self, color: C) -> Result<(), Error>
    where
        C: Into<TwitchColor>,
    {
        self.command(format!("/color {}", color.into()))
    }

    /// Reconnects to chat.
    pub fn disconnect(&self) -> Result<(), Error> {
        self.command("/disconnect")
    }

    /// Lists the commands available to you in this room.
    pub fn help(&self) -> Result<(), Error> {
        self.command("/help")
    }

    /// Lists the moderators of this channel.
    pub fn mods(&self) -> Result<(), Error> {
        self.command("/mods")
    }

    /// Lists the VIPs of this channel.
    pub fn vips(&self) -> Result<(), Error> {
        self.command("/vips")
    }

    /// Triggers a commercial.
    ///
    /// Length (optional) must be a positive number of seconds.
    pub fn commercial(&self, length: Option<usize>) -> Result<(), Error> {
        match length {
            Some(n) => self.command(format!("/commercial {}", n)),
            None => self.command("/commercial"),
        }
    }

    /// Permanently prevent a user from chatting.
    /// Reason is optional and will be shown to the target user and other moderators.
    ///
    /// Use [`Writer::unban`](./struct.Writer.html#method.unban) to remove a ban.
    pub fn ban(&self, username: &str, reason: Option<&str>) -> Result<(), Error> {
        match reason {
            Some(reason) => self.command(format!("/ban {} {}", username, reason)),
            None => self.command(format!("/ban {}", username)),
        }
    }

    /// Removes a ban on a user.
    pub fn unban(&self, username: &str) -> Result<(), Error> {
        self.command(format!("/unban {}", username))
    }

    /// Clear chat history for all users in this room.
    pub fn clear(&self) -> Result<(), Error> {
        self.command("/clear")
    }

    // ???
    // pub fn delete(&self) -> Result<(), Error> {
    //     unimplemented!()
    // }

    /// Enables emote-only mode (only emoticons may be used in chat).
    ///
    /// Use [`Writer::emoteonlyoff`](./struct.Writer.html#method.emoteonlyoff) to disable.
    pub fn emoteonly(&self) -> Result<(), Error> {
        self.command("/emoteonly")
    }

    /// Disables emote-only mode.
    pub fn emoteonlyoff(&self) -> Result<(), Error> {
        self.command("/emoteonlyoff")
    }

    /// Enables followers-only mode (only users who have followed for 'duration' may chat).
    ///
    /// Examples: "30m", "1 week", "5 days 12 hours".
    ///
    /// Must be less than 3 months.
    pub fn followers(&self, duration: &str) -> Result<(), Error> {
        // TODO use https://docs.rs/chrono/0.4.6/chrono/#duration
        // and verify its < 3 months
        self.command(&format!("/followers {}", duration))
    }

    /// Disables followers-only mode.
    pub fn followersoff(&self) -> Result<(), Error> {
        self.command("/followersoff")
    }

    /// Grant moderator status to a user.
    ///
    /// Use [`Writer::mods`](./struct.Writer.html#method.mods) to list the moderators of this channel.
    ///
    /// (**NOTE**: renamed to `op` because r#mod is annoying to type)
    pub fn op(&self, username: &str) -> Result<(), Error> {
        self.command(&format!("/mod {}", username))
    }

    /// Revoke moderator status from a user.
    ///
    /// Use [`Writer::mods`](./struct.Writer.html#method.mods) to list the moderators of this channel.
    pub fn unmod(&self, username: &str) -> Result<(), Error> {
        self.command(&format!("/unmod {}", username))
    }

    /// Enables r9k mode.
    ///
    /// Use [`Writer::r9kbetaoff`](./struct.Writer.html#method.r9kbetaoff) to disable.
    pub fn r9kbeta(&self) -> Result<(), Error> {
        self.command("/r9kbeta")
    }

    /// Disables r9k mode.
    pub fn r9kbetaoff(&self) -> Result<(), Error> {
        self.command("/r9kbetaoff")
    }

    /// Enables slow mode (limit how often users may send messages).
    ///
    /// Duration (optional, default=120) must be a positive number of seconds.
    ///
    /// Use [`Writer::slowoff`](./struct.Writer.html#method.slowoff) to disable.
    pub fn slow(&self, duration: Option<usize>) -> Result<(), Error> {
        // TODO use https://docs.rs/chrono/0.4.6/chrono/#duration
        match duration {
            Some(dur) => self.command(format!("/slow {}", dur)),
            None => self.command("/slow"),
        }
    }

    /// Disables slow mode.
    pub fn slowoff(&self) -> Result<(), Error> {
        self.command("/slowoff")
    }

    /// Enables subscribers-only mode (only subscribers may chat in this channel).
    ///
    /// Use [`Writer::subscribersoff`](./struct.Writer.html#method.subscribersoff) to disable.
    pub fn subscribers(&self) -> Result<(), Error> {
        self.command("/subscribers")
    }

    /// Disables subscribers-only mode.
    pub fn subscribersoff(&self) -> Result<(), Error> {
        self.command("/subscribersoff")
    }

    /// Temporarily prevent a user from chatting.
    ///
    /// * duration (*optional*, default=`10 minutes`) must be a positive integer.
    /// * time unit (*optional*, default=`s`) must be one of
    ///   * s
    ///   * m
    ///   * h
    ///   * d
    ///   * w
    /// * maximum duration is `2 weeks`.
    ///
    /// Combinations like `1d2h` are also allowed.
    ///
    /// Reason is optional and will be shown to the target user and other moderators.
    ///
    /// Use [`Writer::untimeout`](./struct.Writer.html#method.untimeout) to remove a timeout.
    pub fn timeout(
        &self,
        username: &str,
        duration: Option<&str>,
        reason: Option<&str>,
    ) -> Result<(), Error> {
        // TODO use https://docs.rs/chrono/0.4.6/chrono/#duration
        // and verify the duration stuff
        let timeout = match (duration, reason) {
            (Some(dur), Some(reason)) => format!("/timeout {} {} {}", username, dur, reason),
            (None, Some(reason)) => format!("/timeout {} {}", username, reason),
            (Some(dur), None) => format!("/timeout {} {}", username, dur),
            (None, None) => format!("/timeout {}", username),
        };
        self.command(timeout)
    }

    /// Removes a timeout on a user.
    pub fn untimeout(&self, username: &str) -> Result<(), Error> {
        self.command(format!("/untimeout {}", username))
    }

    /// Grant VIP status to a user.
    ///
    /// Use [`Writer::vips`](./struct.Writer.html#method.vips) to list the VIPs of this channel.
    pub fn vip(&self, username: &str) -> Result<(), Error> {
        self.command(format!("/vip {}", username))
    }

    /// Revoke VIP status from a user.
    ///
    /// Use [`Writer::vips`](./struct.Writer.html#method.vips) to list the VIPs of this channel.
    pub fn unvip(&self, username: &str) -> Result<(), Error> {
        self.command(format!("/unvip {}", username))
    }

    /// Whispers the message to the username.
    pub fn whisper(&self, username: &str, message: &str) -> Result<(), Error> {
        self.command(format!("/w {} {}", username, message))
    }

    /// Joins a `channel`
    ///
    /// This ensures the channel name is lowercased and begins with a '#'.
    ///
    /// The following are equivilant
    /// ```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);
    /// let w = client.writer();
    /// w.join("museun").unwrap();
    /// w.join("#museun").unwrap();
    /// w.join("Museun").unwrap();
    /// w.join("#MUSEUN").unwrap();
    /// ```    
    pub fn join<C>(&self, channel: C) -> Result<(), Error>
    where
        C: Into<Channel>,
    {
        let channel = Channel::validate(channel)?;
        self.raw(format!("JOIN {}", *channel))
    }

    /// Parts a `channel`
    ///
    /// This ensures the channel name is lowercased and begins with a '#'.
    ///
    /// The following are equivilant
    /// ```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);
    /// let w = client.writer();
    /// w.part("museun").unwrap();
    /// w.part("#museun").unwrap();
    /// w.part("Museun").unwrap();
    /// w.part("#MUSEUN").unwrap();
    /// ```    
    pub fn part<C>(&self, channel: C) -> Result<(), Error>
    where
        C: Into<Channel>,
    {
        let channel = Channel::validate(channel)?;
        self.raw(format!("PART {}", *channel))
    }

    /// Sends an "emote" `message` in the third person to the `channel`
    ///
    /// This ensures the channel name is lowercased and begins with a '#'.
    pub fn me<C, S>(&self, channel: C, message: S) -> Result<(), Error>
    where
        C: Into<Channel>,
        S: AsRef<str>,
    {
        let channel = Channel::validate(channel)?;
        self.send(channel, format!("/me {}", message.as_ref()))
    }

    /// Sends the `message` to the `channel`
    ///
    /// This ensures the channel name is lowercased and begins with a '#'.
    ///
    /// Same as [`send`](./struct.Client.html#method.send)
    pub fn privmsg<C, S>(&self, channel: C, message: S) -> Result<(), Error>
    where
        C: Into<Channel>,
        S: AsRef<str>,
    {
        let channel = Channel::validate(channel)?;
        self.raw(format!("PRIVMSG {} :{}", *channel, message.as_ref()))
    }

    /// Sends the `message` to the `channel`
    ///
    /// This ensures the channel name is lowercased and begins with a '#'.
    ///
    /// Same as [`privmsg`](./struct.Client.html#method.privmsg)
    pub fn send<C, S>(&self, channel: C, message: S) -> Result<(), Error>
    where
        C: Into<Channel>,
        S: AsRef<str>,
    {
        self.privmsg(channel, message)
    }

    /// Sends the command: `data` (e.g. `/color #FFFFFF`)
    pub fn command<S>(&self, data: S) -> Result<(), Error>
    where
        S: AsRef<str>,
    {
        self.raw(format!("PRIVMSG jtv :{}", data.as_ref()))
    }

    /// Sends a raw line (appends the required `\r\n`)
    pub fn raw<S>(&self, data: S) -> Result<(), Error>
    where
        S: AsRef<str>,
    {
        self.write_line(data.as_ref())
    }
}