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
use super::AsRawIRC;
use std::fmt;

/// A "prefix" part of an IRC message, as defined by RFC 2812:
/// ```none
/// <prefix>     ::= <servername> | <nick> [ '!' <user> ] [ '@' <host> ]
/// <servername> ::= <host>
/// <nick>       ::= <letter> { <letter> | <number> | <special> }
/// <user>       ::= <nonwhite> { <nonwhite> }
/// <host>       ::= see RFC 952 [DNS:4] for details on allowed hostnames
/// <letter>     ::= 'a' ... 'z' | 'A' ... 'Z'
/// <number>     ::= '0' ... '9'
/// <special>    ::= '-' | '[' | ']' | '\' | '`' | '^' | '{' | '}'
/// <nonwhite>   ::= <any 8bit code except SPACE (0x20), NUL (0x0), CR
///                   (0xd), and LF (0xa)>
/// ```
///
/// # Examples
///
/// ```
/// use twitch_irc::message::IRCPrefix;
/// use twitch_irc::message::AsRawIRC;
///
/// let prefix = IRCPrefix::Full {
///     nick: "a_nick".to_owned(),
///     user: Some("a_user".to_owned()),
///     host: Some("a_host.com".to_owned())
/// };
///
/// assert_eq!(prefix.as_raw_irc(), "a_nick!a_user@a_host.com");
/// ```
///
/// ```
/// use twitch_irc::message::IRCPrefix;
/// use twitch_irc::message::AsRawIRC;
///
/// let prefix = IRCPrefix::Full {
///     nick: "a_nick".to_owned(),
///     user: None,
///     host: Some("a_host.com".to_owned())
/// };
///
/// assert_eq!(prefix.as_raw_irc(), "a_nick@a_host.com");
/// ```
///
/// ```
/// use twitch_irc::message::IRCPrefix;
/// use twitch_irc::message::AsRawIRC;
///
/// let prefix = IRCPrefix::HostOnly {
///     host: "a_host.com".to_owned()
/// };
///
/// assert_eq!(prefix.as_raw_irc(), "a_host.com");
/// ```
#[derive(Debug, PartialEq, Clone, Hash)]
pub enum IRCPrefix {
    /// The prefix specifies only a sending server/hostname.
    ///
    /// Note that the spec also allows a very similar format where only a sending nickname is
    /// specified. However that type of format plays no role on Twitch, and is practically impossible
    /// to reliably tell apart from host-only prefix messages. For this reason, a prefix without
    /// a `@` character is always assumed to be purely a host-only prefix, and not a nickname-only prefix.
    HostOnly {
        /// `host` part of the prefix
        host: String,
    },
    /// The prefix variant specifies a nickname, and optionally also a username and optionally a
    /// hostname. See above for the RFC definition.
    Full {
        /// `nick` part of the prefix
        nick: String,
        /// `user` part of the prefix
        user: Option<String>,
        /// `host` part of the prefix
        host: Option<String>,
    },
}

impl IRCPrefix {
    /// Parse the `IRCPrefix` from the given string slice. `source` should be specified without
    /// the leading `:` that precedes in full IRC messages.
    ///
    /// # Examples
    ///
    /// ```
    /// use twitch_irc::message::IRCPrefix;
    ///
    /// let prefix = IRCPrefix::parse("a_nick!a_user@a_host.com");
    /// assert_eq!(prefix, IRCPrefix::Full {
    ///     nick: "a_nick".to_owned(),
    ///     user: Some("a_user".to_owned()),
    ///     host: Some("a_host.com".to_owned())
    /// })
    /// ```
    ///
    /// ```
    /// use twitch_irc::message::IRCPrefix;
    ///
    /// let prefix = IRCPrefix::parse("a_host.com");
    /// assert_eq!(prefix, IRCPrefix::HostOnly {
    ///     host: "a_host.com".to_owned()
    /// })
    /// ```
    pub fn parse(source: &str) -> IRCPrefix {
        if !source.contains('@') {
            // just a hostname
            IRCPrefix::HostOnly {
                host: source.to_owned(),
            }
        } else {
            // full prefix (nick[[!user]@host])
            // valid forms:
            // nick
            // nick@host
            // nick!user@host

            // split on @ first, then on !
            let mut at_split = source.splitn(2, '@');
            let nick_and_user = at_split.next().unwrap();
            let host = at_split.next();

            // now nick_and_user is either "nick" or "nick!user"
            let mut exc_split = nick_and_user.splitn(2, '!');
            let nick = exc_split.next();
            let user = exc_split.next();

            IRCPrefix::Full {
                nick: nick.unwrap().to_owned(),
                user: user.map(|s| s.to_owned()),
                host: host.map(|s| s.to_owned()),
            }
        }
    }
}

impl AsRawIRC for IRCPrefix {
    fn format_as_raw_irc(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
        match self {
            Self::HostOnly { host } => write!(f, "{}", host)?,
            Self::Full { nick, user, host } => {
                write!(f, "{}", nick)?;
                if let Some(host) = host {
                    if let Some(user) = user {
                        write!(f, "!{}", user)?
                    }
                    write!(f, "@{}", host)?;
                }
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_display_host_only() {
        let prefix = IRCPrefix::HostOnly {
            host: "tmi.twitch.tv".to_owned(),
        };
        assert_eq!(prefix.as_raw_irc(), "tmi.twitch.tv");
    }

    #[test]
    fn test_display_full_1() {
        let prefix = IRCPrefix::Full {
            nick: "justin".to_owned(),
            user: Some("justin".to_owned()),
            host: Some("justin.tmi.twitch.tv".to_owned()),
        };
        assert_eq!(prefix.as_raw_irc(), "justin!justin@justin.tmi.twitch.tv");
    }

    #[test]
    fn test_display_full_2() {
        let prefix = IRCPrefix::Full {
            nick: "justin".to_owned(),
            user: None,
            host: Some("justin.tmi.twitch.tv".to_owned()),
        };
        assert_eq!(prefix.as_raw_irc(), "justin@justin.tmi.twitch.tv");
    }

    #[test]
    fn test_display_full_3() {
        let prefix = IRCPrefix::Full {
            nick: "justin".to_owned(),
            user: None,
            host: None,
        };
        assert_eq!(prefix.as_raw_irc(), "justin");
    }

    #[test]
    fn test_display_full_4_user_without_host_invalid_edge_case() {
        let prefix = IRCPrefix::Full {
            nick: "justin".to_owned(),
            user: Some("justin".to_owned()),
            host: None,
        };
        assert_eq!(prefix.as_raw_irc(), "justin");
    }
}