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
use super::*;
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct UserNotice {
pub tags: Tags,
pub channel: String,
pub message: Option<String>,
}
impl UserNotice {
pub fn channel(&self) -> &str {
&self.channel
}
pub fn message(&self) -> Option<&str> {
self.message.as_ref().map(|s| s.as_ref())
}
}
impl UserNotice {
pub fn badges(&self) -> Vec<Badge> {
badges(self.get("badges").unwrap_or_default())
}
pub fn color(&self) -> Option<TwitchColor> {
self.get("color").map(RGB::from_hex).map(Into::into)
}
pub fn display_name(&self) -> Option<&str> {
self.get("display-name")
}
pub fn emotes(&self) -> Vec<Emotes> {
emotes(self.get("emotes").unwrap_or_default())
}
pub fn id(&self) -> Option<&str> {
self.get("id")
}
pub fn login(&self) -> Option<&str> {
self.get("login")
}
pub fn moderator(&self) -> bool {
self.get_as_bool("mod")
}
pub fn msg_id(&self) -> NoticeType {
let kind = self.get("msg-id").unwrap();
match kind {
"sub" => NoticeType::Sub,
"resub" => NoticeType::Resub,
"subgift" => NoticeType::SubGift,
"anonsubgift" => NoticeType::AnonSubGift,
"submysterygift" => NoticeType::SubMysteryGift,
"giftpaidupgrade" => NoticeType::GiftPaidUpgrade,
"rewardgift" => NoticeType::RewardGift,
"anongiftpaidupgrade" => NoticeType::AnonGiftPaidUpgrade,
"raid" => NoticeType::Raid,
"unraid" => NoticeType::Unraid,
"ritual" => NoticeType::Ritual,
"bitsbadgetier" => NoticeType::BitsBadgeTier,
e => NoticeType::Unknown(e.to_string()),
}
}
pub fn msg_param_cumulative_months(&self) -> Option<u64> {
self.get_parsed("msg-param-cumulative-months")
}
pub fn msg_param_display_name(&self) -> Option<&str> {
self.get("msg-param-displayName")
}
pub fn msg_param_login(&self) -> Option<&str> {
self.get("msg-param-login")
}
pub fn msg_param_months(&self) -> Option<u64> {
self.get_parsed("msg-param-months")
}
pub fn msg_param_recipient_display_name(&self) -> Option<&str> {
self.get("msg-param-recipient-display-name")
}
pub fn msg_param_recipient_id(&self) -> Option<u64> {
self.get_parsed("msg-param-recipient-id")
}
pub fn msg_param_recipient_user_name(&self) -> Option<&str> {
self.get("msg-param-recipient-user-name")
}
pub fn msg_param_should_share_streak(&self) -> bool {
self.get_as_bool("msg-param-should-share-streak")
}
pub fn msg_param_streak_months(&self) -> Option<u64> {
self.get_parsed("msg-param-streak-months")
}
pub fn msg_param_sub_plan(&self) -> Option<SubPlan> {
self.get("msg-param-sub-plan").and_then(|k| {
let plan = match k {
"Prime" => SubPlan::Prime,
"1000" => SubPlan::Tier1,
"2000" => SubPlan::Tier2,
"3000" => SubPlan::Tier3,
_ => return None,
};
Some(plan)
})
}
pub fn msg_param_sub_plan_name(&self) -> Option<&str> {
self.get("msg-param-sub-plan-name")
}
pub fn msg_param_viewer_count(&self) -> Option<u64> {
self.get_parsed("msg-param-viewerCount")
}
pub fn msg_param_ritual_name(&self) -> Option<&str> {
self.get("msg-param-ritual-name")
}
pub fn room_id(&self) -> Option<u64> {
self.get_parsed("room-id")
}
pub fn system_msg(&self) -> String {
let msg = self.get("system-msg").unwrap();
msg.replace("\\s", " ")
.replace("\\r", "\r")
.replace("\\n", "\n")
.replace("\\\\", "\\")
.replace("\\:", ":")
}
pub fn tmi_sent_ts(&self) -> Option<u64> {
self.get_parsed("tmi-sent-ts")
}
pub fn user_id(&self) -> Option<u64> {
self.get_parsed("user-id")
}
}
impl Tag for UserNotice {
fn get(&self, key: &str) -> Option<&str> {
self.tags.get(key).map(AsRef::as_ref)
}
}
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SubPlan {
Prime,
Tier1,
Tier2,
Tier3,
}
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum NoticeType {
Sub,
Resub,
SubGift,
AnonSubGift,
SubMysteryGift,
GiftPaidUpgrade,
RewardGift,
AnonGiftPaidUpgrade,
Raid,
Unraid,
Ritual,
BitsBadgeTier,
Unknown(String),
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn system_msg_unescape() {
let input = [
r#"@badges=staff/1,broadcaster/1,turbo/1;color=#008000;display-name=ronni;emotes=;id=db25007f-7a18-43eb-9379-80131e44d633;login=ronni;mod=0;msg-id=resub;msg-param-cumulative-months=6;msg-param-streak-months=2;msg-param-should-share-streak=1;msg-param-sub-plan=Prime;msg-param-sub-plan-name=Prime;room-id=1337;subscriber=1;system-msg=ronni\shas\ssubscribed\sfor\s6\smonths!;tmi-sent-ts=1507246572675;turbo=1;user-id=1337;user-type=staff :tmi.twitch.tv USERNOTICE #dallas :Great stream -- keep it up!"#,
r#"@badges=broadcaster/1,subscriber/6;color=;display-name=qa_subs_partner;emotes=;flags=;id=b1818e3c-0005-490f-ad0a-804957ddd760;login=qa_subs_partner;mod=0;msg-id=anonsubgift;msg-param-months=3;msg-param-recipient-display-name=TenureCalculator;msg-param-recipient-id=135054130;msg-param-recipient-user-name=tenurecalculator;msg-param-sub-plan-name=t111;msg-param-sub-plan=1000;room-id=196450059;subscriber=1;system-msg=An\sanonymous\suser\sgifted\sa\sTier\s1\ssub\sto\sTenureCalculator!\s;tmi-sent-ts=1542063432068;turbo=0;user-id=196450059;user-type= :tmi.twitch.tv USERNOTICE #qa_subs_partner"#,
r#"@badges=turbo/1;color=#9ACD32;display-name=TestChannel;emotes=;id=3d830f12-795c-447d-af3c-ea05e40fbddb;login=testchannel;mod=0;msg-id=raid;msg-param-displayName=TestChannel;msg-param-login=testchannel;msg-param-viewerCount=15;room-id=56379257;subscriber=0;system-msg=15\sraiders\sfrom\sTestChannel\shave\sjoined\n!;tmi-sent-ts=1507246572675;tmi-sent-ts=1507246572675;turbo=1;user-id=123456;user-type= :tmi.twitch.tv USERNOTICE #othertestchannel"#,
r#"@badges=;color=;display-name=SevenTest1;emotes=30259:0-6;id=37feed0f-b9c7-4c3a-b475-21c6c6d21c3d;login=seventest1;mod=0;msg-id=ritual;msg-param-ritual-name=new_chatter;room-id=6316121;subscriber=0;system-msg=Seventoes\sis\snew\shere!;tmi-sent-ts=1508363903826;turbo=0;user-id=131260580;user-type= :tmi.twitch.tv USERNOTICE #seventoes :HeyGuys"#,
];
let output = [
"ronni has subscribed for 6 months!",
"An anonymous user gifted a Tier 1 sub to TenureCalculator! ",
"15 raiders from TestChannel have joined\n!",
"Seventoes is new here!",
];
for (input, &output) in input.iter().zip(output.iter()) {
let msg = crate::irc::types::Message::parse(input).unwrap();
if let crate::twitch::Message::UserNotice(msg @ UserNotice { .. }) =
parse(&msg).unwrap()
{
assert_eq!(msg.system_msg(), output);
}
}
}
}