From 2acc87d6f09548c751f934235768d512b7c68217 Mon Sep 17 00:00:00 2001 From: Drackrath Date: Sat, 7 Nov 2026 16:05:39 +0100 Subject: [PATCH] [Feat] Add invite/other events, bbcode flag, drop self-typing --- Cargo.lock | 2 +- Cargo.toml | 2 +- README.md | 6 +++ examples/chat.rs | 16 +++++-- src/lib.rs | 121 +++++++++++++++++++++++++++++++++++++++++++++-- 5 files changed, 137 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 46ee1b7..fca8c78 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -122,7 +122,7 @@ checksum = "1505bd5d3d116872e7271a6d4e16d81d0c8570876c8de68093a09ac269d8aac0" [[package]] name = "aurelia-steam-vent-chat" -version = "0.5.0" +version = "0.6.0" dependencies = [ "clap", "steam-vent", diff --git a/Cargo.toml b/Cargo.toml index 66b6223..90b4b4c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "aurelia-steam-vent-chat" -version = "0.5.0" +version = "0.6.0" authors = ["Robin Appelman "] edition = "2024" description = "Steam chat client library (Aurelia fork of steam-vent-chat, ported to steam-vent 0.5)" diff --git a/README.md b/README.md index 2cb2088..bcc2c2b 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,12 @@ upstream in these ways: 3 has no default `SteamID` to fall back to. - Incoming messages are delivered. Upstream checks `local_echo()` on both arms, so it drops every message from a friend. +- Typing notifications from your own other devices are dropped, so they no + longer show up as the friend typing. +- `send_message` sets `contains_bbcode`, as the official clients do. +- Game invites arrive as `ChatEvent::GameInvite`. Every other entry type + arrives as `ChatEvent::Other` with its raw value. `ChatEvent` is + `#[non_exhaustive]`, so a `match` on it needs a wildcard arm. ## Usage diff --git a/examples/chat.rs b/examples/chat.rs index cef75cf..61ae89d 100644 --- a/examples/chat.rs +++ b/examples/chat.rs @@ -1,3 +1,4 @@ +use aurelia_steam_vent_chat::{ChatClient, ChatEvent}; use clap::Parser; use std::error::Error; use std::io::stdin; @@ -7,7 +8,6 @@ use steam_vent::auth::{ FileGuardDataStore, SharedSecretAuthConfirmationHandler, }; use steam_vent::{Connection, ServerList}; -use aurelia_steam_vent_chat::{ChatClient, ChatEvent}; use steamid_ng::SteamID; use tokio::spawn; use tokio_stream::StreamExt; @@ -64,8 +64,18 @@ async fn main() -> Result<(), Box> { while let Some(Ok(event)) = events.next().await { match event { ChatEvent::Typing(event) => println!("{} is tying...", event.source.steam64()), - ChatEvent::Message(event) => println!("{}: {}", event.source.steam64(), event.message_no_bbcode.unwrap_or(event.message)), - ChatEvent::EchoMessage(event) => println!("me: {}", event.message_no_bbcode.unwrap_or(event.message)), + ChatEvent::Message(event) => println!( + "{}: {}", + event.source.steam64(), + event.message_no_bbcode.unwrap_or(event.message) + ), + ChatEvent::EchoMessage(event) => { + println!("me: {}", event.message_no_bbcode.unwrap_or(event.message)) + } + ChatEvent::GameInvite(event) => { + println!("{} sent a game invite", event.source.steam64()) + } + _ => {} } } }); diff --git a/src/lib.rs b/src/lib.rs index 0a05952..c97c876 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,7 +1,7 @@ +use steam_vent::{Connection, ConnectionTrait, NetworkError}; use steam_vent_proto::steammessages_friendmessages_steamclient::{ CFriendMessages_IncomingMessage_Notification, CFriendMessages_SendMessage_Request, }; -use steam_vent::{Connection, ConnectionTrait, NetworkError}; use steamid_ng::SteamID; use tokio_stream::{Stream, StreamExt}; @@ -27,12 +27,19 @@ impl ChatClient { }) } - /// Send a chat message to a user - pub async fn send_message(&self, target: SteamID, message: String) -> Result { + /// Send a chat message to a user. + /// + /// Flags text as BBCode, like Steam. + pub async fn send_message( + &self, + target: SteamID, + message: String, + ) -> Result { let req = CFriendMessages_SendMessage_Request { steamid: Some(target.into()), message: Some(message), chat_entry_type: Some(MessageType::Chat as i32), + contains_bbcode: Some(true), ..CFriendMessages_SendMessage_Request::default() }; let result = self.connection.service_method(req).await?; @@ -46,6 +53,7 @@ impl ChatClient { /// Incoming chat event #[derive(Debug)] +#[non_exhaustive] pub enum ChatEvent { /// Another user sent a message Message(MessageEvent), @@ -53,6 +61,10 @@ pub enum ChatEvent { EchoMessage(MessageEvent), /// Another user is typing Typing(TypingEvent), + /// Another user sent a game invite + GameInvite(MessageEvent), + /// Any other entry type + Other(OtherEvent), } impl TryFrom for ChatEvent { @@ -83,11 +95,27 @@ impl TryFrom for ChatEvent { message: notification.message.unwrap_or_default(), message_no_bbcode: notification.message_no_bbcode, }), + // Own typing elsewhere is noise. + MessageType::Typing if notification.local_echo() => return Err(()), MessageType::Typing => ChatEvent::Typing(TypingEvent { source, server_timestamp: notification.rtime32_server_timestamp() as u64, }), - _ => return Err(()), + MessageType::GameInvite if !notification.local_echo() => { + ChatEvent::GameInvite(MessageEvent { + source, + server_timestamp: notification.rtime32_server_timestamp() as u64, + message: notification.message.unwrap_or_default(), + message_no_bbcode: notification.message_no_bbcode, + }) + } + _ => ChatEvent::Other(OtherEvent { + source, + entry_type: notification.chat_entry_type(), + local_echo: notification.local_echo(), + server_timestamp: notification.rtime32_server_timestamp() as u64, + message: notification.message, + }), }) } } @@ -105,6 +133,21 @@ pub struct MessageEvent { pub server_timestamp: u64, } +/// Entry type without its own variant +#[derive(Debug)] +pub struct OtherEvent { + /// SteamID of the conversation partner + pub source: SteamID, + /// Raw `EChatEntryType` value + pub entry_type: i32, + /// Sent by the local user elsewhere + pub local_echo: bool, + /// Service side time of the event + pub server_timestamp: u64, + /// Raw message contents, if any + pub message: Option, +} + /// Incoming typing event #[derive(Debug)] pub struct TypingEvent { @@ -157,4 +200,72 @@ pub struct MessageResult { /// Chat message with bbcode added pub modified_message: Option, pub server_timestamp: u64, -} \ No newline at end of file +} +#[cfg(test)] +mod tests { + use super::*; + + const FRIEND: u64 = 76_561_198_000_000_001; + + fn note(entry_type: i32, local_echo: bool) -> CFriendMessages_IncomingMessage_Notification { + CFriendMessages_IncomingMessage_Notification { + steamid_friend: Some(FRIEND), + chat_entry_type: Some(entry_type), + local_echo: Some(local_echo), + message: Some("hi".into()), + rtime32_server_timestamp: Some(42), + ..Default::default() + } + } + + #[test] + fn friend_message_is_message() { + assert!(matches!( + ChatEvent::try_from(note(1, false)), + Ok(ChatEvent::Message(_)) + )); + } + + #[test] + fn own_message_is_echo() { + assert!(matches!( + ChatEvent::try_from(note(1, true)), + Ok(ChatEvent::EchoMessage(_)) + )); + } + + #[test] + fn friend_typing_is_typing() { + assert!(matches!( + ChatEvent::try_from(note(2, false)), + Ok(ChatEvent::Typing(_)) + )); + } + + #[test] + fn own_typing_is_dropped() { + assert!(ChatEvent::try_from(note(2, true)).is_err()); + } + + #[test] + fn game_invite_is_delivered() { + match ChatEvent::try_from(note(3, false)) { + Ok(ChatEvent::GameInvite(e)) => { + assert_eq!(u64::from(e.source), FRIEND); + assert_eq!(e.message, "hi"); + } + other => panic!("expected GameInvite, got {other:?}"), + } + } + + #[test] + fn unknown_type_is_other_with_raw_value() { + match ChatEvent::try_from(note(99, false)) { + Ok(ChatEvent::Other(e)) => { + assert_eq!(e.entry_type, 99); + assert_eq!(e.server_timestamp, 42); + } + other => panic!("expected Other, got {other:?}"), + } + } +}