chat-system 0.1.2

A multi-protocol async chat crate — single interface for IRC, Matrix, Discord, Telegram, Slack, Signal, WhatsApp, and more
Documentation
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
# chat-system

A multi-protocol async chat crate for Rust. Provides a **single unified `Messenger` trait** for IRC, Matrix, Discord, Telegram, Slack, Signal, WhatsApp, Microsoft Teams, Google Chat, iMessage, Webhook, and Console — with full rich-text support for every platform's native format.

The primary way to use this crate is through the **generic interface**: `MessengerConfig` is a serde-tagged enum whose `protocol` field selects the backend at runtime, so the protocol is just a field in your config file rather than a compile-time choice.

---

## Features

| Feature flag | Protocols added | Extra dependencies |
|---|---|---|
| *(default)* | IRC, Discord, Telegram, Slack, Teams, Google Chat, iMessage, Webhook, Console | none |
| `matrix` | Matrix (via `matrix-sdk`) | `matrix-sdk` |
| `whatsapp` | WhatsApp (via `wa-rs`) | `wa-rs` family |
| `signal-cli` | Signal (via `signal-cli` subprocess) | *(external binary only)* |
| `full` | All of the above | all optional deps |

---

## Quick Start

```toml
[dependencies]
chat-system = "0.1"
tokio = { version = "1", features = ["full"] }
```

### Generic interface (recommended)

`MessengerConfig` deserializes from any serde-compatible source.  The `protocol` field picks the backend; everything else is the same [`Messenger`] trait regardless of platform.

```rust
use chat_system::{GenericMessenger, Messenger, MessengerConfig, PresenceStatus};
use chat_system::config::IrcConfig;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    // Build the config programmatically or load it from a file (see below).
    let config = MessengerConfig::Irc(IrcConfig {
        name: "my-bot".into(),
        server: "irc.libera.chat".into(),
        port: 6697,
        nick: "my-bot".into(),
        channels: vec!["#rust".into()],
        tls: true,
    });

    // GenericMessenger is a drop-in Messenger — swap the config to change protocol.
    let mut client = GenericMessenger::new(config);
    client.initialize().await?;

    // Presence + text status (no-op on platforms that don't support them)
    client.set_status(PresenceStatus::Online).await?;
    client.set_text_status("Building something in Rust 🦀").await?;

    client.send_message("#rust", "Hello from chat-system!").await?;

    // Receive messages; reactions are available where the platform supports them
    for msg in client.receive_messages().await? {
        println!("[{}] {}: {}", msg.channel.as_deref().unwrap_or("?"), msg.sender, msg.content);
        if let Some(reactions) = &msg.reactions {
            for r in reactions { println!("  {} × {}", r.emoji, r.count); }
        }
    }

    client.disconnect().await?;
    Ok(())
}
```

### Loading the config from a file

Because `MessengerConfig` derives `serde::Deserialize`, any serde-compatible source works.

**TOML** (`config.toml`):

```toml
protocol = "discord"
name     = "my-bot"
token    = "Bot TOKEN_HERE"
```

```rust
# use chat_system::{GenericMessenger, Messenger, MessengerConfig};
let toml_str = std::fs::read_to_string("config.toml")?;
let config: MessengerConfig = toml::from_str(&toml_str)?;
let mut client = GenericMessenger::new(config);
client.initialize().await?;
```

**JSON** (`config.json`):

```json
{"protocol":"telegram","name":"my-bot","token":"BOT_TOKEN"}
```

```rust
# use chat_system::{GenericMessenger, Messenger, MessengerConfig};
let json_str = std::fs::read_to_string("config.json")?;
let config: MessengerConfig = serde_json::from_str(&json_str)?;
let mut client = GenericMessenger::new(config);
client.initialize().await?;
```

---

## Multi-platform with `MessengerManager`

`MessengerManager` holds multiple `GenericMessenger` instances and broadcasts / receives across all of them at once.

```rust
use chat_system::{GenericMessenger, Messenger, MessengerConfig, MessengerManager};
use chat_system::config::{DiscordConfig, TelegramConfig};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let mut mgr = MessengerManager::new()
        .add(GenericMessenger::new(MessengerConfig::Discord(DiscordConfig {
            name: "discord".into(),
            token: std::env::var("DISCORD_TOKEN")?,
        })))
        .add(GenericMessenger::new(MessengerConfig::Telegram(TelegramConfig {
            name: "telegram".into(),
            token: std::env::var("TELEGRAM_TOKEN")?,
        })));
    mgr.initialize_all().await?;

    // Broadcast to every connected platform in one call
    mgr.broadcast("#general", "Hello from all platforms!").await;

    // Receive from every platform in one call
    for msg in mgr.receive_all().await? {
        println!("[{}] {}: {}", msg.channel.as_deref().unwrap_or("?"), msg.sender, msg.content);
    }

    mgr.disconnect_all().await?;
    Ok(())
}
```

---

## Reactions

```rust
// Add / remove a reaction (no-op on platforms that don't support it)
client.add_reaction("msg-id-123", "#general", "👍").await?;
client.remove_reaction("msg-id-123", "#general", "👍").await?;

// Reactions arrive on received messages where the platform populates them
for msg in client.receive_messages().await? {
    if let Some(reactions) = &msg.reactions {
        for r in reactions {
            println!("{}: {} ({})", msg.id, r.emoji, r.count);
        }
    }
}
```

---

## Profile pictures

```rust
// Retrieve a user's profile picture URL (None if not supported)
if let Some(url) = client.get_profile_picture("user-id-123").await? {
    println!("Avatar: {url}");
}

// Update the bot's own profile picture
client.set_profile_picture("https://example.com/avatar.png").await?;
```

---

## Replies

```rust
use chat_system::SendOptions;

client.send_message_with_options(SendOptions {
    recipient: "#general",
    content: "Thanks for the message!",
    reply_to: Some("original-message-id"),
    ..Default::default()
}).await?;
```

Incoming reply messages expose the parent ID via `msg.reply_to`.

---

## Search

```rust
use chat_system::SearchQuery;

let results = client.search_messages(SearchQuery {
    text: "deploy".into(),
    channel: Some("#ops".into()),
    limit: Some(20),
    ..Default::default()
}).await?;
for msg in results {
    println!("{}: {}", msg.sender, msg.content);
}
```

---

## Rich Text

```rust
use chat_system::{RichText, RichTextNode};

let msg = RichText(vec![
    RichTextNode::Bold(vec![RichTextNode::Plain("Hello".into())]),
    RichTextNode::Plain(", world! ".into()),
    RichTextNode::Link {
        url: "https://example.com".into(),
        text: vec![RichTextNode::Plain("click".into())],
    },
]);

println!("{}", msg.to_discord_markdown());   // **Hello**, world! [click](https://example.com)
println!("{}", msg.to_telegram_html());      // <b>Hello</b>, world! <a href="…">click</a>
println!("{}", msg.to_slack_mrkdwn());       // *Hello*, world! <https://example.com|click>
println!("{}", msg.to_irc_formatted());      // \x02Hello\x02, world! click (https://example.com)
println!("{}", msg.to_whatsapp_formatted()); // *Hello*, world! click (https://example.com)
println!("{}", msg.to_matrix_html());        // <b>Hello</b>, world! <a href="">click</a>

// Parse from Markdown
let rt = RichText::from_markdown("**bold** _italic_ `code`");
```

---

## Channel Capabilities

Each `ChannelType` exposes its feature set so you can decide at runtime what to offer:

```rust
use chat_system::ChannelType;

let caps = ChannelType::Slack.descriptor().capabilities;
println!("Slack supports reactions: {}", caps.supports_reactions);  // true
println!("Slack supports threads: {}",   caps.supports_threads);    // true

for ct in ChannelType::ALL {
    println!("{:14} reactions={} threads={} inbound={:?}",
        ct.display_name(),
        ct.descriptor().capabilities.supports_reactions,
        ct.descriptor().capabilities.supports_threads,
        ct.descriptor().capabilities.inbound_mode);
}
```

---

## Markdown Converters

```rust
use chat_system::markdown::{markdown_to_telegram_html, markdown_to_slack, chunk_markdown_html};

let html  = markdown_to_telegram_html("**bold** and `code`");
// → "<b>bold</b> and <code>code</code>"

let slack = markdown_to_slack("**bold** [link](https://x.com)");
// → "*bold* <https://x.com|link>"

// Split long messages respecting Telegram's 4096-char limit:
let chunks = chunk_markdown_html(&very_long_markdown, 4096);
```

---

## Server

A **server** is a named container of listeners.  It owns no address, port, or protocol — those belong to the listeners.  Different listeners can speak different protocols while feeding into the same handler.

### Programmatic (recommended)

```rust
use chat_system::server::Server;
use chat_system::servers::IrcListener;
use chat_system::ChatServer;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let mut server = Server::new("my-server")
        .add_listener(IrcListener::new("0.0.0.0:6667"))
        .add_listener(IrcListener::new("0.0.0.0:6697"));

    server.run(|msg| async move {
        println!("{}: {}", msg.sender, msg.content);
        Ok(Some(format!("echo: {}", msg.content)))
    }).await?;

    Ok(())
}
```

### With TLS (feature `tls`)

Enable the `tls` feature to use `TlsIrcListener` for encrypted connections:

```rust
use chat_system::server::Server;
use chat_system::servers::{IrcListener, TlsIrcListener};
use chat_system::ChatServer;
use std::sync::Arc;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let tls_config = rustls::ServerConfig::builder()
        .with_no_client_auth()
        .with_single_cert(certs, key)?;          // load from PEM files

    // Plaintext on 6667, TLS on 6697 — same handler for both.
    let mut server = Server::new("my-server")
        .add_listener(IrcListener::new("0.0.0.0:6667"))
        .add_listener(TlsIrcListener::new("0.0.0.0:6697", Arc::new(tls_config)));

    server.run(|msg| async move {
        Ok(Some(format!("echo: {}", msg.content)))
    }).await?;

    Ok(())
}
```

### Config-driven with `GenericServer`

`ServerConfig` uses the `ListenerConfig` trait (via `typetag`), so listener configs are extensible and can be deserialized from any serde format.

**JSON** (`server.json`):

```json
{
  "name": "my-server",
  "listeners": [
    { "protocol": "irc", "address": "0.0.0.0:6667" },
    { "protocol": "irc", "address": "0.0.0.0:6697" }
  ]
}
```

```rust
use chat_system::{GenericServer, ChatServer, ServerConfig};

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let json = std::fs::read_to_string("server.json")?;
    let config: ServerConfig = serde_json::from_str(&json)?;
    let mut server = GenericServer::new(config);

    server.run(|msg| async move {
        println!("{}: {}", msg.sender, msg.content);
        Ok(Some(format!("echo: {}", msg.content)))
    }).await?;

    Ok(())
}
```

---

## Examples

```sh
# Generic interface (recommended starting point — no credentials needed)
cargo run --example generic_config_client     # Full API showcase (console backend)
cargo run --example generic_multi_platform    # MessengerManager multi-bot demo
cargo run --example generic_config_server     # Server loaded from a JSON config file

# IRC client + server
cargo run --example irc_echo_server           # Server using Server + IrcListener API
cargo run --example irc_client                # Plaintext client (connects to libera.chat)
cargo run --example irc_encrypted_echo_server --features tls  # TLS server (needs cert.pem/key.pem)
cargo run --example irc_encrypted_client      # TLS client (connects to libera.chat:6697)

# Other protocols
cargo run --example discord_bot               # Discord bot (needs DISCORD_BOT_TOKEN)
cargo run --example matrix_client --features matrix  # Matrix client (needs credentials)
```

---

## Protocol-specific clients

When you need direct access to protocol-specific features, you can construct the concrete type directly.  The `Messenger` trait is still the primary interface:

```rust
use chat_system::messengers::IrcMessenger;
use chat_system::Messenger;

#[tokio::main]
async fn main() -> anyhow::Result<()> {
    let mut bot = IrcMessenger::new("bot", "irc.libera.chat", 6697, "mybot")
        .with_tls(true)
        .with_channels(vec!["#rust"]);
    bot.initialize().await?;
    bot.send_message("#rust", "Hello from chat-system!").await?;
    bot.disconnect().await?;
    Ok(())
}
```

### IRC TLS

The IRC messenger supports both plaintext and encrypted connections:

| Mode | Port | Setting |
|---|---|---|
| Plaintext | 6667 | `.with_tls(false)` |
| Encrypted (RFC 7194) | 6697 | `.with_tls(true)` |

```rust
// Plaintext
let mut bot = IrcMessenger::new("name", "irc.server.com", 6667, "nick").with_tls(false);

// TLS
let mut bot = IrcMessenger::new("name", "irc.server.com", 6697, "nick").with_tls(true);
```

| Network | Host | Plaintext port | TLS port |
|---|---|---|---|
| Libera.Chat | `irc.libera.chat` | 6667 | 6697 |
| Freenode | `irc.freenode.net` | 6667 | 6697 |
| Undernet | `irc.undernet.org` | 6667 | 6697 |

---

## License

MIT. Code adapted from [RustyClaw](https://github.com/rexlunae/RustyClaw) and [Moltis](https://github.com/moltis-org/moltis), both MIT.