Skip to main content

simploxide_client/
lib.rs

1#![cfg_attr(docsrs, feature(doc_cfg))]
2//! For first-time users, it's recommended to get hands-on experience by running some example bots
3//! on [GitHub](https://github.com/a1akris/simploxide/tree/main/simploxide-client) before writing
4//! their own.
5//!
6//! This SDK is intended to be used with the `tokio` runtime. Here are the steps to implement any bot:
7//!
8//! ### 1. Choose a backend
9//!
10//! `simploxide` supports both **WebSocket** and **FFI** SimpleX-Chat backends.
11//! All FFI-exclusive methods are reimplemented in native Rust, so in practice the backends differ
12//! only in their runtime characteristics: a single-process app via **FFI** vs. an app that
13//! connects to a running SimpleX-Chat **WebSocket** server.
14//!
15//! Since both backends are equally capable, always start development with the **WebSocket** backend
16//! (enabled by default). Switching to **FFI** later is as simple as replacing `ws` imports with
17//! `ffi` imports, but **FFI** requires configuring the crate build and obliges you to use the
18//! AGPL-3.0 license. You can read more about switching to **FFI** in the `simploxide-sxcrt-sys` crate docs.
19//!
20//! ### 2. Initialise the bot
21//!
22//! `simploxide` provides convenient bot builders to launch and configure your bot.
23//!
24//! ```ignore
25//! let (bot, events, mut cli) = ws::BotBuilder::new("YesMan", 5225)
26//!     .db_prefix("db/bot")
27//!     // Create a public bot address that auto-accepts new users with a welcome message.
28//!     .auto_accept_with(
29//!         "Hello, I'm a bot that always agrees with my users",
30//!     )
31//!     // Launch the CLI, connect the client, and initialise the bot.
32//!     .launch()
33//!     .await?;
34//!
35//! let address = bot.address().await?;
36//! println!("My address: {address}");
37//! ```
38//!
39//! See all available options in [ws::BotBuilder] and [ffi::BotBuilder].
40//!
41//! ### 3. Set up an event dispatcher
42//!
43//! Dispatchers are zero-cost and provide a convenient API for handling events.
44//!
45//! ```ignore
46//! // into_dispatcher accepts any type and creates a dispatcher from the event stream.
47//! // The value provided here is passed into all event handlers as a second argument.
48//! events.into_dispatcher(bot)
49//!     .on(new_messages)
50//!     .dispatch()
51//!     .await?;
52//! ```
53//!
54//! Learn more about dispatchers in the [dispatcher] and [EventStream] docs.
55//!
56//! ### 4. Implement event handlers
57//!
58//! The first handler argument determines which event the handler processes. The [StreamEvents]
59//! type allows interrupting event dispatching via [`StreamEvents::Break`].
60//!
61//! ```ignore
62//! async fn new_msgs(ev: Arc<NewChatItems>, bot: Bot) -> ws::ClientResult<StreamEvents> {
63//!     for (chat, msg, content) in ev.filter_messages() {
64//!         bot.update_msg_reaction(chat, msg, Reaction::Set("👍")).await?;
65//!
66//!         bot.send_msg(chat, "I absolutely agree with this!".bold())
67//!            .reply_to(msg)
68//!            .await?;
69//!     }
70//!
71//!     Ok(StreamEvents::Continue)
72//! }
73//! ```
74//!
75//! Message builders are quite powerful, see [`messages`] for details. In most places where an
76//! ID is expected you can pass a struct directly; see the type-safe conversions available in [id].
77//!
78//! ### 5. Execute cleanup before exiting
79//!
80//! ```ignore
81//! bot.shutdown().await;
82//! cli.kill().await?;
83//! ```
84//!
85//! ## Features
86//!
87//! `simploxide` strives to be a minimal library for simple bots while also coming with batteries
88//! included for all sorts of the advanced use cases. The balance is maintained through feature
89//! gates documented below:
90//!
91//! - **`cli`** *(default)*: WebSocket backend ([`ws`]) with a built-in runner that spawns and
92//!   manages a local `simplex-chat` process. Use [`ws::BotBuilder::launch`] to start everything
93//!   in one call.
94//!
95//! - **`websocket`**: WebSocket backend ([`ws`]) without the CLI runner. Use
96//!   [`ws::BotBuilder::connect`] to attach to an already-running `simplex-chat` server.
97//!
98//! - **`ffi`**: FFI backend ([`ffi`]) that embeds the SimpleX-Chat library in-process.
99//!   Requires AGPL-3.0 and additional build configuration; see `simploxide-sxcrt-sys`.
100//!
101//! - **`native_crypto`**: Native Rust implementation of client-side encryption(XSalsa20 + Poly1305). Enables
102//!   [`ImagePreview::from_crypto_file`](preview::ImagePreview::from_crypto_file) and [crypto::fs]
103//!   module allowing to encrypt decrypt files directly in the Rust code
104//!
105//! - **`multimedia`**: Image transcoding via the `image` crate. Enables
106//!   [`preview::transcoder::Transcoder`] and automatic thumbnail generation for [`messages::Image`].
107//!   [`preview::ImagePreview`] automatically tries to transcode its sources to JPEGs with this
108//!   feature on
109//!
110//! - **`xftp`**: XFTP file transfer support. Enables [`xftp::XftpClient`], which intercepts
111//!   streamlines file downlaods with a `download_file` method.
112//!
113//! - **`cancellation`**: Re-exports [`tokio_util::sync::CancellationToken`] and enables helper
114//!   methods for cooperative shutdown.
115//!
116//! - **`crypto`**: Low-level cryptographic primitives (zeroize, rand). Pulled in automatically by
117//!   `native_crypto`. Useful on its own if you wish to use your own crypto implementation.
118//!
119//! - **`fullcli`**: Convenience bundle: `cli` + `native_crypto` + `multimedia` + `xftp` +
120//!   `cancellation`.
121//!
122//! - **`fullffi`**: Convenience bundle: `ffi` + `native_crypto` + `multimedia` + `xftp` +
123//!   `cancellation`.
124//!
125//! ### How to work with this documentation?
126//!
127//! The [bot] page should be your primary reference and the [events] page your secondary one.
128//! From these two pages you should be able to find everything in a structured manner.
129
130#[cfg(feature = "crypto")]
131pub mod crypto;
132#[cfg(feature = "ffi")]
133pub mod ffi;
134#[cfg(feature = "websocket")]
135pub mod ws;
136#[cfg(feature = "xftp")]
137pub mod xftp;
138
139pub mod bot;
140pub mod dispatcher;
141pub mod ext;
142pub mod id;
143pub mod messages;
144pub mod prelude;
145pub mod preview;
146
147mod util;
148
149pub use simploxide_api_types::{
150    self as types,
151    client_api::{self, BadResponseError, ClientApi, ClientApiError},
152    commands, events,
153    events::{Event, EventKind},
154    responses,
155    utils::CommandSyntax,
156};
157
158#[cfg(feature = "cancellation")]
159pub use tokio_util::{self, sync::CancellationToken};
160
161pub use dispatcher::DispatchChain;
162
163use futures::{Stream, TryStreamExt as _};
164
165use std::{
166    pin::Pin,
167    sync::Arc,
168    task::{Context, Poll},
169};
170
171/// The high level event stream that embeds event filtering.
172///
173/// Parsing SimpleX events may be costly, they are quite large deeply nested structs with a lot of
174/// [`String`] and [`std::collections::BTreeMap`] types. This stream provides filtering APIs
175/// allowing to parse and propagate events the application handles and drop all other events early
176/// without allocating any extra memory.
177///
178/// By default filters are disabled and no events are dropped. Use [`Self::set_filter`] to only
179/// receive events you're interested in.
180///
181/// Use [`Self::into_dispatcher`] to handle events conveniently. Dispatchers are completely
182/// zerocost, manage filters internally, and provide a high-level easy to use API covering the
183/// absolute majority of use cases.
184pub struct EventStream<P> {
185    filter: [bool; EventKind::COUNT],
186    receiver: tokio::sync::mpsc::UnboundedReceiver<P>,
187    hooks: Vec<Box<dyn Hook>>,
188}
189
190impl<P> From<tokio::sync::mpsc::UnboundedReceiver<P>> for EventStream<P> {
191    fn from(receiver: tokio::sync::mpsc::UnboundedReceiver<P>) -> Self {
192        Self {
193            filter: [true; EventKind::COUNT],
194            receiver,
195            hooks: Vec::new(),
196        }
197    }
198}
199
200impl<P> EventStream<P> {
201    pub fn add_hook(&mut self, hook: Box<dyn Hook>) {
202        self.hooks.push(hook);
203    }
204
205    #[cfg(feature = "xftp")]
206    pub fn hook_xftp<C: 'static + Clone + ClientApi>(&mut self, client: C) -> xftp::XftpClient<C> {
207        let xftp_client = xftp::XftpClient::from(client);
208        let hook = xftp_client.clone();
209        self.add_hook(Box::new(hook));
210
211        xftp_client
212    }
213
214    pub fn set_filter<I: IntoIterator<Item = EventKind>>(&mut self, f: Filter<I>) -> &mut Self {
215        match f {
216            Filter::Accept(kinds) => {
217                self.reject_all();
218                for kind in kinds {
219                    self.filter[kind.as_usize()] = true;
220                }
221            }
222            Filter::AcceptAllExcept(kinds) => {
223                self.accept_all();
224                for kind in kinds {
225                    self.filter[kind.as_usize()] = false;
226                }
227            }
228            Filter::AcceptAll => self.accept_all(),
229        }
230
231        self
232    }
233
234    pub fn accept(&mut self, kind: EventKind) {
235        self.filter[kind.as_usize()] = true;
236    }
237
238    pub fn reject(&mut self, kind: EventKind) {
239        self.filter[kind.as_usize()] = false;
240    }
241
242    pub fn accept_all(&mut self) {
243        self.set_all(true);
244    }
245
246    pub fn reject_all(&mut self) {
247        self.set_all(false)
248    }
249
250    fn set_all(&mut self, new: bool) {
251        for old in &mut self.filter {
252            *old = new;
253        }
254    }
255}
256
257impl<P: EventParser> EventStream<P> {
258    /// Turns stream into a [`DispatchChain`] builder with the provided `ctx`. The `ctx` is an
259    /// arbitrary type that can be used within event handlers. Use [`dispatcher::Dispatcher::seq`] to add
260    /// sequential handlers: `AsyncFnMut(Arc<Ev>, &mut Ctx)`; or [`dispatcher::Dispatcher::on`] for concurrent
261    /// ones: `AsyncFn(Arc<Ev>, Ctx) where Ctx: 'static + Clone + Send`.
262    pub fn into_dispatcher<C>(self, ctx: C) -> DispatchChain<P, C> {
263        DispatchChain::with_ctx(self, ctx)
264    }
265
266    /// Waits for a particular event `Ev` **dropping** other events in the process. This method is
267    /// mostly useful in bot initialisation scenarios when the bot doesn't have any active users.
268    /// Misusing this method may result in not receiving user messages and other important events.
269    pub async fn wait_for<Ev: events::EventData>(&mut self) -> Result<Option<Arc<Ev>>, P::Error> {
270        self.reject_all();
271        self.accept(Ev::KIND);
272        let result = self.try_next().await;
273        self.accept_all();
274
275        let ev = result?;
276        Ok(ev.map(|ev| Ev::from_event(ev).unwrap()))
277    }
278
279    /// Waits for one one of the events in the `kinds` list **dropping** other events in the
280    /// process. Returns the first encountered event of the specified kind. This method is mostly
281    /// useful in bot initialisation scenarios when the bot doesn't have any active users. Misusing
282    /// this method may result in not receiving user messages and other important events.
283    pub async fn wait_for_any(
284        &mut self,
285        kinds: impl IntoIterator<Item = EventKind>,
286    ) -> Result<Option<Event>, P::Error> {
287        self.set_filter(Filter::Accept(kinds));
288        let result = self.try_next().await;
289        self.accept_all();
290        result
291    }
292
293    pub async fn stream_events<E, F>(mut self, mut f: F) -> Result<Self, E>
294    where
295        F: AsyncFnMut(Event) -> Result<StreamEvents, E>,
296        E: From<P::Error>,
297    {
298        while let Some(event) = self.try_next().await? {
299            if let StreamEvents::Break = f(event).await? {
300                break;
301            }
302        }
303
304        Ok(self)
305    }
306
307    pub async fn stream_events_with_ctx_mut<E, Ctx, F>(
308        mut self,
309        mut f: F,
310        mut ctx: Ctx,
311    ) -> Result<(Self, Ctx), E>
312    where
313        F: AsyncFnMut(Event, &mut Ctx) -> Result<StreamEvents, E>,
314        E: From<P::Error>,
315    {
316        while let Some(event) = self.try_next().await? {
317            if let StreamEvents::Break = f(event, &mut ctx).await? {
318                break;
319            }
320        }
321
322        Ok((self, ctx))
323    }
324
325    pub async fn stream_events_with_ctx_cloned<E, Ctx, F>(
326        mut self,
327        f: F,
328        ctx: Ctx,
329    ) -> Result<(Self, Ctx), E>
330    where
331        Ctx: Clone,
332        F: AsyncFn(Event, Ctx) -> Result<StreamEvents, E>,
333        E: From<P::Error>,
334    {
335        while let Some(event) = self.try_next().await? {
336            if let StreamEvents::Break = f(event, ctx.clone()).await? {
337                break;
338            }
339        }
340
341        Ok((self, ctx))
342    }
343}
344
345pub enum Filter<I: IntoIterator<Item = EventKind>> {
346    Accept(I),
347    AcceptAll,
348    AcceptAllExcept(I),
349}
350
351#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
352pub enum StreamEvents {
353    Break,
354    Continue,
355}
356
357impl<P: EventParser> Stream for EventStream<P> {
358    type Item = Result<Event, P::Error>;
359
360    fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
361        loop {
362            match self.receiver.poll_recv(cx) {
363                Poll::Ready(Some(raw_event)) => {
364                    let kind = match raw_event.parse_kind() {
365                        Ok(kind) => kind,
366                        Err(e) => break Poll::Ready(Some(Err(e))),
367                    };
368
369                    if !self.hooks.iter().any(|h| h.should_intercept(kind))
370                        && !self.filter[kind.as_usize()]
371                    {
372                        continue;
373                    }
374
375                    match raw_event.parse_event() {
376                        Ok(event) => {
377                            for hook in self.hooks.iter_mut() {
378                                if hook.should_intercept(kind) {
379                                    hook.intercept_event(event.clone());
380                                }
381                            }
382
383                            if self.filter[kind.as_usize()] {
384                                break Poll::Ready(Some(Ok(event)));
385                            }
386                        }
387                        Err(e) => break Poll::Ready(Some(Err(e))),
388                    }
389                }
390                Poll::Ready(None) => break Poll::Ready(None),
391                Poll::Pending => break Poll::Pending,
392            }
393        }
394    }
395}
396
397/// A helper trait meant to be implemented by raw event types
398pub trait EventParser {
399    type Error;
400
401    /// Should parse kind cheaply without allocations
402    fn parse_kind(&self) -> Result<EventKind, Self::Error>;
403
404    /// Parse the whole events
405    fn parse_event(&self) -> Result<Event, Self::Error>;
406}
407
408impl EventParser for Event {
409    type Error = std::convert::Infallible;
410
411    fn parse_kind(&self) -> Result<EventKind, Self::Error> {
412        Ok(self.kind())
413    }
414
415    fn parse_event(&self) -> Result<Event, Self::Error> {
416        // Cheap Arc Clone
417        Ok(self.clone())
418    }
419}
420
421pub trait Hook {
422    fn should_intercept(&self, kind: EventKind) -> bool;
423
424    /// Hooks must not block the event stream; this method should be a cheap synchronous call.
425    /// Delegate heavy work to another thread or spawn async tasks internally.
426    fn intercept_event(&mut self, event: Event);
427}
428
429/// Syntactic sugar for constructing [`Preferences`](simploxide_api_types::Preferences) values.
430///
431/// ```ignore
432/// Preferences {
433///     timed_messages: preferences::timed_messages::yes(Duration::from_hours(4)),
434///     full_delete: preferences::YES,
435///     reactions: preferences::ALWAYS,
436///     voice: preferences::NO,
437///     files: preferences::ALWAYS,
438///     calls: preferences::YES,
439///     sessions: preferences::NO,
440///     commands: None,
441///     undocumented: Default::default(),
442/// }
443/// ```
444pub mod preferences {
445    use simploxide_api_types::{FeatureAllowed, SimplePreference};
446
447    pub mod timed_messages {
448        use super::*;
449        use simploxide_api_types::TimedMessagesPreference;
450
451        pub const TTL_MAX: std::time::Duration = std::time::Duration::from_hours(8784);
452
453        pub fn ttl_to_secs(ttl: std::time::Duration) -> i32 {
454            let clamped = std::cmp::min(ttl, TTL_MAX);
455            clamped.as_secs() as i32
456        }
457
458        pub fn always(ttl: std::time::Duration) -> Option<TimedMessagesPreference> {
459            Some(TimedMessagesPreference {
460                allow: FeatureAllowed::Always,
461                ttl: Some(ttl_to_secs(ttl)),
462                undocumented: serde_json::Value::Null,
463            })
464        }
465
466        pub fn yes(ttl: std::time::Duration) -> Option<TimedMessagesPreference> {
467            Some(TimedMessagesPreference {
468                allow: FeatureAllowed::Yes,
469                ttl: Some(ttl_to_secs(ttl)),
470                undocumented: serde_json::Value::Null,
471            })
472        }
473
474        pub const NO: Option<TimedMessagesPreference> = Some(TimedMessagesPreference {
475            allow: FeatureAllowed::No,
476            ttl: None,
477            undocumented: serde_json::Value::Null,
478        });
479    }
480
481    pub const ALWAYS: Option<SimplePreference> = Some(SimplePreference {
482        allow: FeatureAllowed::Always,
483        undocumented: serde_json::Value::Null,
484    });
485
486    pub const YES: Option<SimplePreference> = Some(SimplePreference {
487        allow: FeatureAllowed::Yes,
488        undocumented: serde_json::Value::Null,
489    });
490
491    pub const NO: Option<SimplePreference> = Some(SimplePreference {
492        allow: FeatureAllowed::No,
493        undocumented: serde_json::Value::Null,
494    });
495}