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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
#![doc = include_str!("../README.md")]
use chumsky::IterParser as _;
use chumsky::Parser;
use chumsky::prelude::{any, just, none_of, one_of};
use chumsky::text::whitespace;
pub mod avatar_messages;
pub mod system_messages;
pub mod utils;
/// represents an event commemorated in the Second Life chat log
#[expect(
clippy::large_enum_variant,
reason = "boxing gets in the way of properly pattern matching"
)]
#[derive(Debug, Clone, PartialEq)]
pub enum ChatLogEvent {
/// line about an avatar (or an object doing things indistinguishable from an avatar in the chat log)
AvatarLine {
/// name of the avatar or object
name: String,
/// message
message: crate::avatar_messages::AvatarMessage,
},
/// a message by the Second Life viewer or server itself
SystemMessage {
/// the system message
message: crate::system_messages::SystemMessage,
},
/// a message without a colon, most likely an unnamed object like a translator, spanker, etc.
OtherMessage {
/// the message
message: String,
},
}
/// parse a second life avatar name as it appears in the chat log before a message
///
/// # Errors
///
/// returns an error if the parser fails
#[must_use]
pub fn avatar_name_parser<'src>()
-> impl Parser<'src, &'src str, String, chumsky::extra::Err<chumsky::error::Rich<'src, char>>> {
none_of(":")
.repeated()
.collect::<String>()
.try_map(|s, _span: chumsky::span::SimpleSpan| Ok(s))
}
/// parse a Second Life chat log event
///
/// # Errors
///
/// returns an error if the parser fails
#[must_use]
fn chat_log_event_parser<'src>()
-> impl Parser<'src, &'src str, ChatLogEvent, chumsky::extra::Err<chumsky::error::Rich<'src, char>>>
{
just("Second Life: ")
.ignore_then(
take_until!(
crate::avatar_messages::avatar_came_online_message_parser().or(
crate::avatar_messages::avatar_went_offline_message_parser()
.or(crate::avatar_messages::avatar_entered_area_message_parser())
.or(crate::avatar_messages::avatar_left_area_message_parser()),
)
)
.map(|(name, message)| ChatLogEvent::AvatarLine {
name: name.strip_suffix(" ").unwrap_or(&name).to_owned(),
message,
}),
)
.or(just("Second Life: ").ignore_then(
crate::system_messages::system_message_parser()
.map(|message| ChatLogEvent::SystemMessage { message }),
))
.or(avatar_name_parser()
.then_ignore(just(":").then(whitespace()))
.then(crate::avatar_messages::avatar_message_parser())
.map(|(name, message)| ChatLogEvent::AvatarLine { name, message }))
.or(any()
.repeated()
.collect::<String>()
.map(|s| ChatLogEvent::OtherMessage { message: s }))
}
/// represents a Second Life chat log line
#[derive(Debug, Clone, PartialEq)]
pub struct ChatLogLine {
/// timestamp of the chat log line, some log lines do not have one because of bugs at the time they were written (e.g. some just have the time formatting string)
pub timestamp: Option<time::PrimitiveDateTime>,
/// event that happened at that time
pub event: ChatLogEvent,
}
/// parse a Second Life chat log line
///
/// # Errors
///
/// returns an error if the parser fails
#[must_use]
pub fn chat_log_line_parser<'src>()
-> impl Parser<'src, &'src str, ChatLogLine, chumsky::extra::Err<chumsky::error::Rich<'src, char>>>
{
just("[")
.ignore_then(
one_of("0123456789")
.repeated()
.exactly(4)
.collect::<String>(),
)
.then(
just("/").ignore_then(
one_of("0123456789")
.repeated()
.exactly(2)
.collect::<String>(),
),
)
.then(
just("/").ignore_then(
one_of("0123456789")
.repeated()
.exactly(2)
.collect::<String>(),
),
)
.then(
just(" ").ignore_then(
one_of("0123456789")
.repeated()
.exactly(2)
.collect::<String>(),
),
)
.then(
just(":").ignore_then(
one_of("0123456789")
.repeated()
.exactly(2)
.collect::<String>(),
),
)
.then(
just(":")
.ignore_then(
one_of("0123456789")
.repeated()
.exactly(2)
.collect::<String>(),
)
.or_not(),
)
.then_ignore(just("]"))
.try_map(
|(((((year, month), day), hour), minute), second),
span: chumsky::span::SimpleSpan| {
let second = second.unwrap_or_else(|| "00".to_string());
let format = time::macros::format_description!(
"[year]/[month]/[day] [hour]:[minute]:[second]"
);
Ok(Some(
time::PrimitiveDateTime::parse(
&format!("{year}/{month}/{day} {hour}:{minute}:{second}"),
format,
).map_err(|e| chumsky::error::Rich::custom(span, format!("{e:?}")))?
))
}
)
.or(just("[[year,datetime,slt]/[mthnum,datetime,slt]/[day,datetime,slt] [hour,datetime,slt]:[min,datetime,slt]]").map(|_| None))
.then_ignore(whitespace())
.then(chat_log_event_parser())
.try_map(
|(timestamp, event),
_span: chumsky::span::SimpleSpan| {
Ok(ChatLogLine {
timestamp,
event,
})
},
)
}
/// replacement for take_until combinator in old chumsky versions
macro_rules! take_until {
($p:expr) => {
any()
.and_is($p.not())
.repeated()
.collect::<String>()
.then($p)
};
}
pub(crate) use take_until;
#[expect(
clippy::print_stderr,
reason = "unfortunately our ariadne reports with ANSI are just gibberish in the latest tracing version because someone thought the ability to use ANSI escape codes was a security risk"
)]
#[cfg(test)]
mod test {
use std::io::{BufRead as _, BufReader};
use super::*;
/// used to deserialize the required options from the environment
#[derive(Debug, serde::Deserialize)]
struct EnvOptions {
#[serde(
deserialize_with = "serde_aux::field_attributes::deserialize_vec_from_string_or_vec"
)]
test_avatar_names: Vec<String>,
}
/// Error enum for the application
#[derive(thiserror::Error, Debug)]
pub(crate) enum TestError {
/// error loading environment
#[error("error loading environment: {0}")]
Env(#[from] envy::Error),
/// error loading .env file
#[error("error loading .env file: {0}")]
DotEnv(#[from] dotenvy::Error),
/// error determining current user home directory
#[error("error determining current user home directory")]
HomeDir,
/// error opening chat log file
#[error("error opening chat log file {0}: {1}")]
OpenChatLogFile(std::path::PathBuf, std::io::Error),
/// error reading chat log line from file
#[error("error reading chat log line from file: {0}")]
ChatLogLineRead(std::io::Error),
}
/// determine avatar log dir from avatar name
pub(crate) fn avatar_log_dir(avatar_name: &str) -> Result<std::path::PathBuf, TestError> {
let avatar_dir_name = avatar_name.replace(' ', "_").to_lowercase();
tracing::debug!("Avatar dir name: {}", avatar_dir_name);
let Some(home_dir) = dirs2::home_dir() else {
tracing::error!("Could not determine current user home directory");
return Err(TestError::HomeDir);
};
Ok(home_dir.join(".firestorm/").join(avatar_dir_name))
}
#[tracing_test::traced_test]
#[tokio::test]
async fn test_log_line_parser() -> Result<(), TestError> {
dotenvy::dotenv()?;
let env_options = envy::from_env::<EnvOptions>()?;
for avatar_name in env_options.test_avatar_names {
let avatar_dir = avatar_log_dir(&avatar_name)?;
let local_chat_log_file = avatar_dir.join("chat.txt");
let file = std::fs::File::open(&local_chat_log_file)
.map_err(|e| TestError::OpenChatLogFile(local_chat_log_file.clone(), e))?;
let file = BufReader::new(file);
let mut last_line: Option<String> = None;
let mut failed_to_parse_line = false;
for line in file.lines() {
let line = line.map_err(TestError::ChatLogLineRead)?;
if (line.starts_with(' ') || line.is_empty())
&& let Some(ll) = last_line
{
last_line = Some(format!("{ll}\n{line}"));
continue;
}
if let Some(ref ll) = last_line {
match chat_log_line_parser().parse(ll).into_result() {
Err(e) => {
tracing::error!("failed to parse line\n{}", ll);
eprintln!(
"{}",
utils::ChumskyError {
description: "the above line".to_string(),
source: ll.to_owned(),
errors: e.into_iter().map(|e| e.into_owned()).collect(),
}
);
failed_to_parse_line = true;
}
Ok(parsed_line) => {
if let ChatLogLine {
timestamp: _,
event:
ChatLogEvent::SystemMessage {
message:
system_messages::SystemMessage::OtherSystemMessage {
ref message,
},
},
} = parsed_line
{
let message = message.to_string();
tracing::info!("parsed line\n{}\n{:?}", ll, parsed_line);
if message.starts_with("The message sent to") &&
let Err(e) =
system_messages::chat_message_still_being_processed_message_parser()
.parse(&message).into_result()
{
eprintln!("{}", utils::ChumskyError {
description: "group chat message still being processed".to_string(),
source: message.to_owned(),
errors: e.into_iter().map(|e| e.into_owned()).collect(),
});
}
if message.contains("owned by")
&& message.contains("gave you")
&& let Err(e) =
system_messages::object_gave_object_message_parser()
.parse(&message)
.into_result()
{
eprintln!(
"{}",
utils::ChumskyError {
description: "owned by gave you".to_string(),
source: message.to_owned(),
errors: e.into_iter().map(|e| e.into_owned()).collect(),
}
);
}
if message.contains("An object named")
&& message.contains("gave you this folder")
&& let Err(e) =
system_messages::object_gave_folder_message_parser()
.parse(&message)
.into_result()
{
eprintln!(
"{}",
utils::ChumskyError {
description: "An object named ... gave you this folder"
.to_string(),
source: message.to_owned(),
errors: e.into_iter().map(|e| e.into_owned()).collect(),
}
);
}
if message.starts_with("Can't rez object")
&& message.contains(
"because the owner of this land does not allow it",
)
&& let Err(e) =
system_messages::permission_to_rez_object_denied_message_parser()
.parse(&message).into_result()
{
eprintln!("{}", utils::ChumskyError {
description: "permission to rez object denied".to_string(),
source: message.to_owned(),
errors: e.into_iter().map(|e| e.into_owned()).collect(),
});
}
if message.starts_with("Teleport completed from")
&& let Err(e) =
system_messages::teleport_completed_message_parser()
.parse(&message)
.into_result()
{
eprintln!(
"{}",
utils::ChumskyError {
description: "teleported completed".to_string(),
source: message.to_owned(),
errors: e.into_iter().map(|e| e.into_owned()).collect(),
}
);
}
if message.starts_with('[')
&& message.contains("status.secondlifegrid.net")
&& let Err(e) =
system_messages::grid_status_event_message_parser()
.parse(&message)
.into_result()
{
eprintln!(
"{}",
utils::ChumskyError {
description: "grid status event".to_string(),
source: message.to_owned(),
errors: e.into_iter().map(|e| e.into_owned()).collect(),
}
);
}
if message.starts_with("Object ID:")
&& let Err(e) =
system_messages::extended_script_info_message_parser()
.parse(&message)
.into_result()
{
eprintln!(
"{}",
utils::ChumskyError {
description: "extended script info".to_string(),
source: message.to_owned(),
errors: e.into_iter().map(|e| e.into_owned()).collect(),
}
);
}
if message.starts_with("Bridge")
&& let Err(e) = system_messages::bridge_message_parser()
.parse(&message)
.into_result()
{
eprintln!(
"{}",
utils::ChumskyError {
description: "bridge message".to_string(),
source: message.to_owned(),
errors: e.into_iter().map(|e| e.into_owned()).collect(),
}
);
}
if message.starts_with("You paid")
&& let Err(e) = system_messages::sent_payment_message_parser()
.parse(&message)
.into_result()
{
eprintln!(
"{}",
utils::ChumskyError {
description: "sent payment".to_string(),
source: message.to_owned(),
errors: e.into_iter().map(|e| e.into_owned()).collect(),
}
);
}
if message.contains("Take Linden dollars") &&
let Err(e) = system_messages::object_granted_permission_to_take_money_parser()
.parse(&message).into_result()
{
eprintln!(
"{}",
utils::ChumskyError {
description: "object granted permission to take money".to_string(),
source: message.to_owned(),
errors: e.into_iter().map(|e| e.into_owned()).collect(),
}
);
}
if message.starts_with("You have offered a calling card")
&& let Err(e) =
system_messages::offered_calling_card_message_parser()
.parse(&message)
.into_result()
{
eprintln!(
"{}",
utils::ChumskyError {
description: "offered calling card".to_string(),
source: message.to_owned(),
errors: e.into_iter().map(|e| e.into_owned()).collect(),
}
);
}
if message.starts_with("Draw Distance set")
&& let Err(e) =
system_messages::draw_distance_set_message_parser()
.parse(&message)
.into_result()
{
eprintln!(
"{}",
utils::ChumskyError {
description: "draw distance set".to_string(),
source: message.to_owned(),
errors: e.into_iter().map(|e| e.into_owned()).collect(),
}
);
}
if message.starts_with("Your object") &&
let Err(e) =
system_messages::your_object_has_been_returned_message_parser()
.parse(&message).into_result()
{
eprintln!(
"{}",
utils::ChumskyError {
description: "your object has been returned".to_string(),
source: message.to_owned(),
errors: e.into_iter().map(|e| e.into_owned()).collect(),
}
);
}
}
}
}
}
last_line = Some(line);
}
#[expect(
clippy::panic,
reason = "panics are okay - even intentional - in tests"
)]
if failed_to_parse_line {
panic!("Failed to parse a line");
}
}
// enable to see output during development, both to identity unhandled messages and to see parse errors above
//panic!();
Ok(())
}
}