//! Code for converting composed messages into content to send to the homeserver. use std::sync::Arc; use comrak::options::{BrokenLinkReference, Options}; use comrak::{ResolvedReference, markdown_to_html}; use matrix_sdk::ruma::events::room::message::{EmoteMessageEventContent, TextMessageEventContent}; use nom::branch::alt; use nom::bytes::complete::tag; use nom::character::complete::space0; use nom::combinator::value; use nom::{IResult, Parser as _}; use crate::config::MarkupFormat; use crate::prelude::*; fn broken_link_handler(link: BrokenLinkReference<'_>) -> Option { let uri = if let Ok(user_id) = OwnedUserId::from_str(link.normalized) { user_id.matrix_to_uri() } else if let Ok(room_id) = OwnedRoomAliasId::from_str(link.normalized) { room_id.matrix_to_uri() } else if let Ok(room_id) = OwnedRoomId::from_str(link.normalized) { room_id.matrix_to_uri() } else { return None; }; Some(ResolvedReference { url: uri.to_string(), title: link.normalized.to_string(), }) } #[derive(Clone, Debug, Default)] enum SlashCommand { /// Send an emote message. Emote, /// Send a message as literal HTML. Html, /// Send a message without parsing any markup. Plaintext, /// Send a Markdown message (the default message markup). #[default] Markdown, /// Send a message with confetti effects in clients that show them. Confetti, /// Send a message with fireworks effects in clients that show them. Fireworks, /// Send a message with heart effects in clients that show them. Hearts, /// Send a message with rainfall effects in clients that show them. Rainfall, /// Send a message with snowfall effects in clients that show them. Snowfall, /// Send a message with heart effects in clients that show them. SpaceInvaders, } impl SlashCommand { fn to_message(&self, input: &str) -> anyhow::Result { let msgtype = match self { SlashCommand::Emote => { let msg = if let Some(html) = text_to_html(input) { EmoteMessageEventContent::html(input, html) } else { EmoteMessageEventContent::plain(input) }; MessageType::Emote(msg) }, SlashCommand::Html => { let msg = TextMessageEventContent::html(input, input); MessageType::Text(msg) }, SlashCommand::Plaintext => { let msg = TextMessageEventContent::plain(input); MessageType::Text(msg) }, SlashCommand::Markdown => { let msg = text_to_message_content(input.into()); MessageType::Text(msg) }, SlashCommand::Confetti => { MessageType::new("nic.custom.confetti", input.into(), Default::default())? }, SlashCommand::Fireworks => { MessageType::new("nic.custom.fireworks", input.into(), Default::default())? }, SlashCommand::Hearts => { MessageType::new("io.element.effect.hearts", input.into(), Default::default())? }, SlashCommand::Rainfall => { MessageType::new("io.element.effect.rainfall", input.into(), Default::default())? }, SlashCommand::Snowfall => { MessageType::new("io.element.effect.snowfall", input.into(), Default::default())? }, SlashCommand::SpaceInvaders => { MessageType::new( "io.element.effects.space_invaders", input.into(), Default::default(), )? }, }; Ok(msgtype) } } impl From for SlashCommand { fn from(markup: MarkupFormat) -> Self { match markup { MarkupFormat::Html => Self::Html, MarkupFormat::Markdown => Self::Markdown, MarkupFormat::Plaintext => Self::Plaintext, } } } fn parse_slash_command_inner(input: &str) -> IResult<&str, SlashCommand> { let (input, _) = space0(input)?; let (input, slash) = alt(( value(SlashCommand::Emote, tag("/me ")), value(SlashCommand::Html, tag("/h ")), value(SlashCommand::Html, tag("/html ")), value(SlashCommand::Plaintext, tag("/p ")), value(SlashCommand::Plaintext, tag("/plain ")), value(SlashCommand::Plaintext, tag("/plaintext ")), value(SlashCommand::Markdown, tag("/md ")), value(SlashCommand::Markdown, tag("/markdown ")), value(SlashCommand::Confetti, tag("/confetti ")), value(SlashCommand::Fireworks, tag("/fireworks ")), value(SlashCommand::Hearts, tag("/hearts ")), value(SlashCommand::Rainfall, tag("/rainfall ")), value(SlashCommand::Snowfall, tag("/snowfall ")), value(SlashCommand::SpaceInvaders, tag("/spaceinvaders ")), )) .parse(input)?; let (input, _) = space0(input)?; Ok((input, slash)) } fn parse_slash_command(input: &str) -> anyhow::Result<(&str, SlashCommand)> { match parse_slash_command_inner(input) { Ok(input) => Ok(input), Err(e) => Err(anyhow::anyhow!("Failed to parse slash command: {e}")), } } /// Check whether this character is not used for markup in Markdown. /// /// Markdown uses just about every ASCII punctuation symbol in some way, especially /// once autolinking is involved, so we really just check whether it's non-punctuation or /// single/double quotations. fn not_markdown_char(c: char) -> bool { if !c.is_ascii_punctuation() { return true; } matches!(c, '"' | '\'') } /// Check whether the input actually needs to be processed as Markdown. fn not_markdown(input: &str) -> bool { input.chars().all(not_markdown_char) } fn text_to_html(input: &str) -> Option { if not_markdown(input) { return None; } let mut options = Options::default(); options.extension.autolink = true; options.extension.shortcodes = true; options.extension.strikethrough = true; options.extension.table = true; options.parse.broken_link_callback = Some(Arc::new(broken_link_handler)); options.render.hardbreaks = true; markdown_to_html(input, &options).into() } fn text_to_message_content(input: Cow<'_, str>) -> TextMessageEventContent { if let Some(html) = text_to_html(&input) { TextMessageEventContent::html(input, html) } else { TextMessageEventContent::plain(input) } } pub fn text_to_message( input: Cow<'_, str>, default_markup: MarkupFormat, ) -> RoomMessageEventContent { let (rest, slash) = parse_slash_command(&input) .unwrap_or_else(|_| (&input, SlashCommand::from(default_markup))); let msg = slash .to_message(rest) .unwrap_or_else(|_| MessageType::Text(text_to_message_content(input))); RoomMessageEventContent::new(msg) } /// Returns `None` if `input` contains a non-text slash command. pub fn text_to_text_message_event_content( input: String, default_markup: MarkupFormat, ) -> Option { let (rest, slash) = parse_slash_command(&input) .unwrap_or_else(|_| (&input, SlashCommand::from(default_markup))); let content = match slash { SlashCommand::Html => TextMessageEventContent::html(rest, rest), SlashCommand::Plaintext => TextMessageEventContent::plain(rest), SlashCommand::Markdown => text_to_message_content(rest.into()), _ => return None, }; Some(content) } #[cfg(test)] pub mod tests { use super::*; #[test] fn test_markdown_autolink() { let input = "http://example.com\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!( content.formatted.unwrap().body, "

http://example.com

\n" ); let input = "www.example.com\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!( content.formatted.unwrap().body, "

www.example.com

\n" ); let input = "See docs (they're at https://iamb.chat)\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!( content.formatted.unwrap().body, "

See docs (they're at https://iamb.chat)

\n" ); } #[test] fn test_markdown_link_alias() { let input = "[#room1:example.com]\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!( content.formatted.unwrap().body, "

#room1:example.com

\n" ); let input = "See [other room][#room1:example.com]\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!( content.formatted.unwrap().body, "

See other room

\n" ); } #[test] fn test_markdown_link_room() { let input = "[!room1:example.com]\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!( content.formatted.unwrap().body, "

!room1:example.com

\n" ); let input = "See [other room][!room1:example.com]\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!( content.formatted.unwrap().body, "

See other room

\n" ); } #[test] fn test_markdown_link_user() { let input = "[@user1:example.com]\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!( content.formatted.unwrap().body, "

@user1:example.com

\n" ); let input = "Talk to [Jane][@user1:example.com]\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!( content.formatted.unwrap().body, "

Talk to Jane

\n" ); } #[test] fn test_markdown_message() { let input = "**bold**\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!(content.formatted.unwrap().body, "

bold

\n"); let input = "*emphasis*\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!(content.formatted.unwrap().body, "

emphasis

\n"); let input = "`code`\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!(content.formatted.unwrap().body, "

code

\n"); let input = "```rust\nconst A: usize = 1;\n```\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!( content.formatted.unwrap().body, "
const A: usize = 1;\n
\n" ); let input = ":heart:\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!(content.formatted.unwrap().body, "

\u{2764}\u{FE0F}

\n"); let input = "para *1*\n\npara _2_\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!( content.formatted.unwrap().body, "

para 1

\n

para 2

\n" ); let input = "line 1\nline ~~2~~\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!(content.formatted.unwrap().body, "

line 1
\nline 2

\n"); let input = "# Heading\n## Subheading\n\ntext\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!( content.formatted.unwrap().body, "

Heading

\n

Subheading

\n

text

\n" ); } #[test] fn test_markdown_headers() { let input = "hello\n=====\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!(content.formatted.unwrap().body, "

hello

\n"); let input = "hello\n-----\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!(content.formatted.unwrap().body, "

hello

\n"); } #[test] fn test_markdown_lists() { let input = "- A\n- B\n- C\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!( content.formatted.unwrap().body, "
    \n
  • A
  • \n
  • B
  • \n
  • C
  • \n
\n" ); let input = "1) A\n2) B\n3) C\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert_eq!( content.formatted.unwrap().body, "
    \n
  1. A
  2. \n
  3. B
  4. \n
  5. C
  6. \n
\n" ); } #[test] fn test_no_markdown_conversion_on_simple_text() { let input = "para 1\n\npara 2\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert!(content.formatted.is_none()); let input = "line 1\nline 2\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert!(content.formatted.is_none()); let input = "isn't markdown\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert!(content.formatted.is_none()); let input = "\"scare quotes\"\n"; let content = text_to_message_content(input.into()); assert_eq!(content.body, input); assert!(content.formatted.is_none()); } #[test] fn text_to_message_slash_commands() { let MessageType::Text(content) = text_to_message("/html bold".into(), Default::default()).msgtype else { panic!("Expected MessageType::Text"); }; assert_eq!(content.body, "bold"); assert_eq!(content.formatted.unwrap().body, "bold"); let MessageType::Text(content) = text_to_message("/h bold".into(), Default::default()).msgtype else { panic!("Expected MessageType::Text"); }; assert_eq!(content.body, "bold"); assert_eq!(content.formatted.unwrap().body, "bold"); let MessageType::Text(content) = text_to_message("/plain bold".into(), Default::default()).msgtype else { panic!("Expected MessageType::Text"); }; assert_eq!(content.body, "bold"); assert!(content.formatted.is_none(), "{:?}", content.formatted); let MessageType::Text(content) = text_to_message("/p bold".into(), Default::default()).msgtype else { panic!("Expected MessageType::Text"); }; assert_eq!(content.body, "bold"); assert!(content.formatted.is_none(), "{:?}", content.formatted); let MessageType::Emote(content) = text_to_message("/me *bold*".into(), Default::default()).msgtype else { panic!("Expected MessageType::Emote"); }; assert_eq!(content.body, "*bold*"); assert_eq!(content.formatted.unwrap().body, "

bold

\n"); let content = text_to_message("/confetti hello".into(), Default::default()).msgtype; assert_eq!(content.msgtype(), "nic.custom.confetti"); assert_eq!(content.body(), "hello"); let content = text_to_message("/fireworks hello".into(), Default::default()).msgtype; assert_eq!(content.msgtype(), "nic.custom.fireworks"); assert_eq!(content.body(), "hello"); let content = text_to_message("/hearts hello".into(), Default::default()).msgtype; assert_eq!(content.msgtype(), "io.element.effect.hearts"); assert_eq!(content.body(), "hello"); let content = text_to_message("/rainfall hello".into(), Default::default()).msgtype; assert_eq!(content.msgtype(), "io.element.effect.rainfall"); assert_eq!(content.body(), "hello"); let content = text_to_message("/snowfall hello".into(), Default::default()).msgtype; assert_eq!(content.msgtype(), "io.element.effect.snowfall"); assert_eq!(content.body(), "hello"); let content = text_to_message("/spaceinvaders hello".into(), Default::default()).msgtype; assert_eq!(content.msgtype(), "io.element.effects.space_invaders"); assert_eq!(content.body(), "hello"); } #[test] fn text_to_message_slash_default() { let MessageType::Text(content) = text_to_message("*hello*".into(), MarkupFormat::Html).msgtype else { panic!("Expected MessageType::Text"); }; assert_eq!(content.body, "*hello*"); assert_eq!(content.formatted.unwrap().body, "*hello*"); let MessageType::Text(content) = text_to_message("/markdown *hello*".into(), MarkupFormat::Html).msgtype else { panic!("Expected MessageType::Text"); }; assert_eq!(content.body, "*hello*"); assert_eq!(content.formatted.unwrap().body, "

hello

\n"); let MessageType::Text(content) = text_to_message("hello".into(), MarkupFormat::Plaintext).msgtype else { panic!("Expected MessageType::Text"); }; assert_eq!(content.body, "hello"); assert!(content.formatted.is_none(), "{:?}", content.formatted); let MessageType::Text(content) = text_to_message("/html hello".into(), MarkupFormat::Plaintext).msgtype else { panic!("Expected MessageType::Text"); }; assert_eq!(content.body, "hello"); assert_eq!(content.formatted.unwrap().body, "hello"); } }