netget-tool 0.3.4

Network protocol stubbing toolkit: ~280 protocol servers and clients whose responses come from an LLM, a script, a static rule or a person.
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
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
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
//! Matrix homeserver, client-server API v3. Rust owns HTTP, the API's JSON shapes, access
//! tokens, room membership and per-user `/sync` delivery (`hub.rs`); the handler decides
//! logins (unless `user_passwords` is given), room creation, joins and every message, and
//! speaks in rooms as its own user.
pub mod actions;
pub mod hub;

use crate::llm::action_helper::call_llm;
use crate::llm::actions::protocol_trait::ActionResult;
use crate::logging::emit::Log;
use crate::protocol::{Event, EventType, SpawnContext};
use crate::server::{
    accept_bounded::{accept_bounded, ConnectionLimiter, DEFAULT_MAX_CONNECTIONS},
    connection::ConnectionId,
};
use crate::state::server::{ConnectionState, ConnectionStatus, ProtocolConnectionInfo};
use anyhow::Result;
use bytes::Bytes;
use http_body_util::{BodyExt, Full, Limited};
use hub::Hub;
use hyper::{
    body::Incoming,
    header::{HeaderValue, AUTHORIZATION, CONTENT_TYPE},
    server::conn::http1,
    service::service_fn,
    Method, Request, Response, StatusCode,
};
use hyper_util::rt::{TokioIo, TokioTimer};
use serde_json::{json, Value};
use std::{
    collections::{HashMap, VecDeque},
    convert::Infallible,
    net::SocketAddr,
    sync::{Arc, Mutex},
    time::Duration,
};
use tokio::sync::Notify;

pub const HEADER_TIMEOUT: Duration = Duration::from_secs(30);
pub const BODY_TIMEOUT: Duration = Duration::from_secs(30);
pub const MAX_HEADER_BYTES: usize = 32 * 1024;
pub const MAX_HEADERS: usize = 64;
/// The longest a `/sync` waits for something new, whatever `timeout` the client asks for.
pub const MAX_SYNC_WAIT: Duration = Duration::from_secs(30);
/// Transaction ids remembered for idempotent sends.
pub const MAX_TXNS: usize = 10_000;
/// `/messages` returns at most this many events per page.
pub const MAX_PAGE: usize = 100;
const CAP_REFUSAL: &[u8] = b"HTTP/1.1 503 Service Unavailable\r\nContent-Type: application/json\r\nContent-Length: 61\r\nConnection: close\r\nRetry-After: 5\r\n\r\n{\"errcode\":\"M_LIMIT_EXCEEDED\",\"error\":\"Too many connections\"}";

type Reply = Response<Full<Bytes>>;

/// Sent transactions: (user, txn id) → event id, and their order for eviction.
type TxnLog = (
    HashMap<(String, String), String>,
    VecDeque<(String, String)>,
);

struct Shared {
    hub: Mutex<Hub>,
    wake: Notify,
    passwords: Option<HashMap<String, String>>,
    bot: String,
    txns: Mutex<TxnLog>,
}

pub async fn spawn(ctx: SpawnContext) -> Result<SocketAddr> {
    let params = ctx.startup_params.as_ref();
    let server_name = params
        .map(|p| p.get_optional_string("server_name"))
        .transpose()?
        .flatten()
        .unwrap_or_else(|| actions::DEFAULT_SERVER_NAME.to_string());
    let bot = params
        .map(|p| p.get_optional_string("bot_user"))
        .transpose()?
        .flatten()
        .unwrap_or_else(|| actions::DEFAULT_BOT_USER.to_string());
    let passwords = params
        .map(|p| p.get_optional_object("user_passwords"))
        .transpose()?
        .flatten()
        .map(|m| {
            m.iter()
                .map(|(k, v)| {
                    let pw = v.as_str().map(str::to_string).ok_or_else(|| {
                        anyhow::anyhow!("user_passwords: the password for {k:?} must be a string")
                    })?;
                    Ok((
                        k.trim_start_matches('@')
                            .split(':')
                            .next()
                            .unwrap_or(k)
                            .to_lowercase(),
                        pw,
                    ))
                })
                .collect::<Result<HashMap<_, _>>>()
        })
        .transpose()?;
    let hub = Hub::new(&server_name);
    let bot = hub.user_id(&bot);
    let shared = Arc::new(Shared {
        hub: Mutex::new(hub),
        wake: Notify::new(),
        passwords,
        bot,
        txns: Mutex::new(Default::default()),
    });

    let listener = tokio::net::TcpListener::bind(ctx.legacy_listen_addr()).await?;
    let local = listener.local_addr()?;
    Log::new(Some(&ctx.status_tx)).info(format!(
        "Matrix homeserver {server_name} listening on {local}"
    ));
    let server_id = ctx.server_id;
    let state = ctx.state.clone();
    let accept = tokio::spawn(async move {
        let limiter = ConnectionLimiter::new(DEFAULT_MAX_CONNECTIONS);
        loop {
            let (stream, peer, permit) = match accept_bounded(
                &listener,
                &limiter,
                CAP_REFUSAL,
                "Matrix",
                Some(&ctx.status_tx),
            )
            .await
            {
                Ok(v) => v,
                Err(_) => break,
            };
            let id = ConnectionId::new(ctx.state.get_next_unified_id().await);
            let now = crate::utils::clock::Instant::now();
            ctx.state
                .add_connection_to_server(
                    server_id,
                    ConnectionState {
                        id,
                        remote_addr: peer,
                        local_addr: local,
                        bytes_sent: 0,
                        bytes_received: 0,
                        packets_sent: 0,
                        packets_received: 0,
                        last_activity: now,
                        status: ConnectionStatus::Active,
                        status_changed_at: now,
                        protocol_info: ProtocolConnectionInfo::empty(),
                    },
                )
                .await;
            let child = ctx.clone();
            let shared = shared.clone();
            ctx.state
                .spawn_server_task(server_id, async move {
                    let _permit = permit;
                    let request_ctx = child.clone();
                    let service = service_fn(move |request| {
                        let ctx = request_ctx.clone();
                        let shared = shared.clone();
                        async move { Ok::<_, Infallible>(handle(request, id, &ctx, &shared).await) }
                    });
                    let mut builder = http1::Builder::new();
                    builder
                        .timer(TokioTimer::new())
                        .header_read_timeout(HEADER_TIMEOUT)
                        .max_headers(MAX_HEADERS)
                        .max_buf_size(MAX_HEADER_BYTES);
                    if let Err(e) = builder
                        .serve_connection(TokioIo::new(stream), service)
                        .await
                    {
                        Log::new(Some(&child.status_tx))
                            .debug(format!("Matrix connection {id} HTTP error: {e}"));
                    }
                    child
                        .state
                        .update_connection_status(server_id, id, ConnectionStatus::Closed)
                        .await;
                    let _ = child.status_tx.send("__UPDATE_UI__".into());
                })
                .await;
        }
    });
    state.register_server_task(server_id, accept).await;
    Ok(local)
}

fn reply(status: u16, body: &Value) -> Reply {
    let mut r = Response::new(Full::new(Bytes::from(
        serde_json::to_vec(body).unwrap_or_default(),
    )));
    *r.status_mut() = StatusCode::from_u16(status).unwrap_or(StatusCode::INTERNAL_SERVER_ERROR);
    r.headers_mut()
        .insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
    r.headers_mut()
        .insert("Access-Control-Allow-Origin", HeaderValue::from_static("*"));
    r
}

fn error(status: u16, errcode: &str, message: &str) -> Reply {
    reply(status, &json!({"errcode": errcode, "error": message}))
}

fn failure(e: Option<&anyhow::Error>) -> Reply {
    let kind = e
        .map(crate::utils::WireFailure::classify)
        .unwrap_or(crate::utils::WireFailure::Unavailable);
    if kind.is_overloaded() {
        let mut r = error(503, "M_LIMIT_EXCEEDED", kind.prefixed_text());
        r.headers_mut()
            .insert("Retry-After", HeaderValue::from_static("5"));
        r
    } else {
        error(500, "M_UNKNOWN", kind.prefixed_text())
    }
}

fn outcome(ctx: &SpawnContext, id: ConnectionId, op: &str, decision: &str) {
    let line = format!("Matrix connection {id} operation={op} decision={decision}");
    let log = Log::new(Some(&ctx.status_tx));
    if decision.starts_with("fail_closed") || decision == "model_silent" {
        log.error(line)
    } else {
        log.info(line)
    }
}

/// What the handler said about a request: allowed (with any messages to post), or refused.
struct Decision {
    refusal: Option<(String, String)>,
    sends: Vec<Value>,
}

async fn ask(
    ctx: &SpawnContext,
    id: ConnectionId,
    event_type: &'static EventType,
    data: Value,
    op: &str,
) -> std::result::Result<Decision, Reply> {
    let event = Event::new(event_type, data);
    let result = match call_llm(
        &ctx.llm_client,
        &ctx.state,
        ctx.server_id,
        Some(id),
        &event,
        &actions::MatrixProtocol,
    )
    .await
    {
        Ok(r) => r,
        Err(e) => {
            outcome(ctx, id, op, "fail_closed_llm_error");
            return Err(failure(Some(&e)));
        }
    };
    if !result.failures.is_empty() {
        outcome(ctx, id, op, "fail_closed_invalid_reply");
        return Err(failure(None));
    }
    let mut accepted = false;
    let mut refusal = None;
    let mut sends = Vec::new();
    let mut stack = result.protocol_results;
    while let Some(r) = stack.pop() {
        match r {
            ActionResult::Custom { name, data } => match name.as_str() {
                actions::ACCEPT => accepted = true,
                actions::SEND => sends.push(data),
                actions::REJECT => {
                    refusal = Some((
                        data["errcode"]
                            .as_str()
                            .unwrap_or("M_FORBIDDEN")
                            .to_string(),
                        data["error"]
                            .as_str()
                            .unwrap_or("Refused by the server")
                            .to_string(),
                    ))
                }
                _ => {}
            },
            ActionResult::Multiple(items) => stack.extend(items),
            _ => {}
        }
    }
    sends.reverse();
    match (refusal, accepted || !sends.is_empty()) {
        (Some(_), true) => {
            outcome(ctx, id, op, "fail_closed_invalid_reply");
            Err(failure(None))
        }
        (Some(r), false) => {
            outcome(ctx, id, op, "model_reject");
            Ok(Decision {
                refusal: Some(r),
                sends,
            })
        }
        (None, true) => {
            outcome(ctx, id, op, "model_answer");
            Ok(Decision {
                refusal: None,
                sends,
            })
        }
        (None, false) => {
            outcome(ctx, id, op, "model_silent");
            Err(failure(None))
        }
    }
}

/// Post the handler's messages as its own user, joining it to each room first.
fn apply_sends(ctx: &SpawnContext, shared: &Shared, default_room: &str, sends: &[Value]) {
    if sends.is_empty() {
        return;
    }
    {
        let mut hub = shared.hub.lock().unwrap_or_else(|e| e.into_inner());
        for s in sends {
            let room = s["room_id"].as_str().unwrap_or(default_room).to_string();
            if !hub.rooms.contains_key(&room) {
                Log::new(Some(&ctx.status_tx)).warn(format!(
                    "Matrix: matrix_send to unknown room {room}; dropped"
                ));
                continue;
            }
            if !hub.is_member(&room, &shared.bot) {
                hub.join(&room, &shared.bot);
            }
            let kind = s["event_type"].as_str().unwrap_or("m.room.message");
            hub.post(&room, &shared.bot, kind, actions::send_content(s), None);
        }
    }
    shared.wake.notify_waiters();
}

fn query(q: Option<&str>) -> HashMap<String, String> {
    q.unwrap_or_default()
        .split('&')
        .filter(|p| !p.is_empty())
        .map(|p| {
            let (k, v) = p.split_once('=').unwrap_or((p, ""));
            let d = |s: &str| {
                urlencoding::decode(&s.replace('+', " "))
                    .map(|c| c.into_owned())
                    .unwrap_or_default()
            };
            (d(k), d(v))
        })
        .collect()
}

fn segment(s: &str) -> String {
    urlencoding::decode(s)
        .map(|c| c.into_owned())
        .unwrap_or_default()
}

async fn handle(
    request: Request<Incoming>,
    id: ConnectionId,
    ctx: &SpawnContext,
    shared: &Shared,
) -> Reply {
    let (parts, body) = request.into_parts();
    let bytes = match tokio::time::timeout(
        BODY_TIMEOUT,
        Limited::new(body, actions::MAX_BODY_BYTES).collect(),
    )
    .await
    {
        Err(_) => return error(408, "M_UNKNOWN", "request body deadline exceeded"),
        Ok(Err(e)) => {
            return if e
                .downcast_ref::<http_body_util::LengthLimitError>()
                .is_some()
            {
                error(413, "M_TOO_LARGE", "Request body too large")
            } else {
                error(400, "M_UNKNOWN", "incomplete HTTP body")
            }
        }
        Ok(Ok(b)) => b.to_bytes(),
    };
    ctx.state
        .update_connection_stats(
            ctx.server_id,
            id,
            Some(bytes.len() as u64),
            None,
            Some(1),
            None,
        )
        .await;
    let q = query(parts.uri.query());
    let path = parts.uri.path().to_string();
    let method = parts.method.clone();
    if method == Method::OPTIONS {
        let mut r = reply(200, &json!({}));
        let h = r.headers_mut();
        h.insert(
            "Access-Control-Allow-Methods",
            HeaderValue::from_static("GET, POST, PUT, DELETE, OPTIONS"),
        );
        h.insert(
            "Access-Control-Allow-Headers",
            HeaderValue::from_static("Authorization, Content-Type"),
        );
        return r;
    }
    if path == "/_matrix/client/versions" {
        return reply(
            200,
            &json!({"versions": ["r0.6.1", "v1.1", "v1.2", "v1.3", "v1.4", "v1.5", "v1.6", "v1.7", "v1.8", "v1.9", "v1.10", "v1.11"],
                    "unstable_features": {}}),
        );
    }
    let Some(rest) = path
        .strip_prefix("/_matrix/client/v3/")
        .or_else(|| path.strip_prefix("/_matrix/client/r0/"))
    else {
        return error(404, "M_UNRECOGNIZED", "Unrecognized request");
    };
    let body: Value = if bytes.is_empty() {
        json!({})
    } else {
        match serde_json::from_slice(&bytes) {
            Ok(v @ Value::Object(_)) => v,
            _ => return error(400, "M_NOT_JSON", "Content not JSON."),
        }
    };
    match (&method, rest) {
        (&Method::GET, "login") => {
            return reply(200, &json!({"flows": [{"type": "m.login.password"}]}))
        }
        (&Method::POST, "login") => return login(ctx, id, shared, &body).await,
        _ => {}
    }

    // Everything else needs an access token.
    let token = parts
        .headers
        .get(AUTHORIZATION)
        .and_then(|v| v.to_str().ok())
        .and_then(|v| v.strip_prefix("Bearer "))
        .map(str::to_string)
        .or_else(|| q.get("access_token").cloned());
    let Some(token) = token else {
        return error(401, "M_MISSING_TOKEN", "Missing access token");
    };
    let Some((user, device)) = shared
        .hub
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .whoami(&token)
    else {
        return error(401, "M_UNKNOWN_TOKEN", "Unrecognised access token");
    };
    let segs: Vec<&str> = rest.split('/').collect();
    match (&method, segs.as_slice()) {
        (&Method::POST, ["logout"]) => {
            shared
                .hub
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .logout(&token);
            reply(200, &json!({}))
        }
        (&Method::GET, ["account", "whoami"]) => {
            reply(200, &json!({"user_id": user, "device_id": device}))
        }
        (&Method::GET, ["sync"]) => sync(shared, &user, &q).await,
        (&Method::GET, ["joined_rooms"]) => {
            let rooms = shared
                .hub
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .joined(&user);
            reply(200, &json!({"joined_rooms": rooms}))
        }
        (&Method::POST, ["user", _, "filter"]) => reply(200, &json!({"filter_id": "0"})),
        (&Method::GET, ["user", _, "filter", _]) => reply(200, &json!({})),
        (&Method::POST, ["createRoom"]) => create_room(ctx, id, shared, &user, &body).await,
        (&Method::POST, ["join", room]) | (&Method::POST, ["rooms", room, "join"]) => {
            join(ctx, id, shared, &user, &segment(room)).await
        }
        (&Method::POST, ["rooms", room, "leave"]) => {
            let room = segment(room);
            if !shared
                .hub
                .lock()
                .unwrap_or_else(|e| e.into_inner())
                .leave(&room, &user)
            {
                return error(403, "M_FORBIDDEN", "You are not in this room");
            }
            shared.wake.notify_waiters();
            reply(200, &json!({}))
        }
        (&Method::POST, ["rooms", room, "invite"]) => {
            let room = segment(room);
            let Some(invitee) = body["user_id"].as_str() else {
                return error(400, "M_MISSING_PARAM", "user_id is required");
            };
            let mut hub = shared.hub.lock().unwrap_or_else(|e| e.into_inner());
            if !hub.is_member(&room, &user) {
                return error(403, "M_FORBIDDEN", "You are not in this room");
            }
            hub.invite(&room, &user, invitee);
            drop(hub);
            shared.wake.notify_waiters();
            reply(200, &json!({}))
        }
        (&Method::PUT, ["rooms", room, "send", kind, txn]) => {
            send(
                ctx,
                id,
                shared,
                &user,
                &segment(room),
                &segment(kind),
                &segment(txn),
                body,
            )
            .await
        }
        (&Method::GET, ["rooms", room, "messages"]) => {
            let room = segment(room);
            let hub = shared.hub.lock().unwrap_or_else(|e| e.into_inner());
            if !hub.is_member(&room, &user) {
                return error(403, "M_FORBIDDEN", "You are not in this room");
            }
            let limit = q
                .get("limit")
                .and_then(|l| l.parse::<usize>().ok())
                .unwrap_or(10)
                .min(MAX_PAGE);
            let history: Vec<Value> = hub.rooms[&room].history.iter().cloned().collect();
            let chunk: Vec<Value> = if q.get("dir").map(String::as_str) == Some("f") {
                history.into_iter().take(limit).collect()
            } else {
                history.into_iter().rev().take(limit).collect()
            };
            reply(200, &json!({"chunk": chunk, "start": "t0", "end": "t0"}))
        }
        (&Method::GET, ["rooms", room, "joined_members"]) => {
            let room = segment(room);
            let hub = shared.hub.lock().unwrap_or_else(|e| e.into_inner());
            if !hub.is_member(&room, &user) {
                return error(403, "M_FORBIDDEN", "You are not in this room");
            }
            let joined: serde_json::Map<String, Value> = hub
                .members(&room)
                .into_iter()
                .map(|m| (m, json!({})))
                .collect();
            reply(200, &json!({"joined": joined}))
        }
        _ => error(404, "M_UNRECOGNIZED", "Unrecognized request"),
    }
}

async fn login(ctx: &SpawnContext, id: ConnectionId, shared: &Shared, body: &Value) -> Reply {
    if body["type"] != "m.login.password" {
        return error(400, "M_UNKNOWN", "Only m.login.password is supported");
    }
    let user = body["identifier"]["user"]
        .as_str()
        .or_else(|| body["user"].as_str());
    let (Some(user), Some(password)) = (user, body["password"].as_str()) else {
        return error(
            400,
            "M_MISSING_PARAM",
            "identifier.user and password are required",
        );
    };
    let user_id = shared
        .hub
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .user_id(user);
    let device = body["device_id"]
        .as_str()
        .filter(|d| !d.is_empty() && d.len() <= 64)
        .map(str::to_string)
        .unwrap_or_else(|| hub::random_id(10).to_uppercase());
    match &shared.passwords {
        Some(table) => {
            let local = user_id
                .trim_start_matches('@')
                .split(':')
                .next()
                .unwrap_or_default();
            if table.get(local).map(String::as_str) != Some(password) {
                outcome(ctx, id, "login", "password_mismatch");
                return error(403, "M_FORBIDDEN", "Invalid username or password");
            }
            outcome(ctx, id, "login", "password_match");
        }
        None => {
            let decision = match ask(
                ctx,
                id,
                &actions::LOGIN_EVENT,
                json!({"user_id": user_id, "device_id": device}),
                "login",
            )
            .await
            {
                Ok(d) => d,
                Err(r) => return r,
            };
            if let Some((code, msg)) = decision.refusal {
                return error(403, &code, &msg);
            }
        }
    }
    let token = shared
        .hub
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .issue_token(&user_id, &device);
    let Some(token) = token else {
        return error(429, "M_LIMIT_EXCEEDED", "Too many sessions");
    };
    let server_name = shared
        .hub
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .server_name
        .clone();
    reply(
        200,
        &json!({"user_id": user_id, "access_token": token, "device_id": device, "home_server": server_name}),
    )
}

async fn create_room(
    ctx: &SpawnContext,
    id: ConnectionId,
    shared: &Shared,
    user: &str,
    body: &Value,
) -> Reply {
    let name = body["name"].as_str().map(str::to_string);
    let invite: Vec<String> = body["invite"]
        .as_array()
        .map(|a| {
            a.iter()
                .filter_map(Value::as_str)
                .filter(|u| u.starts_with('@') && u.contains(':'))
                .map(str::to_string)
                .collect()
        })
        .unwrap_or_default();
    let decision = match ask(
        ctx,
        id,
        &actions::CREATE_ROOM_EVENT,
        json!({"user_id": user, "name": name, "invite": invite}),
        "create_room",
    )
    .await
    {
        Ok(d) => d,
        Err(r) => return r,
    };
    if let Some((code, msg)) = decision.refusal {
        return error(403, &code, &msg);
    }
    let room = shared
        .hub
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .create_room(user, name, &invite);
    let Some(room) = room else {
        return error(429, "M_LIMIT_EXCEEDED", "Too many rooms");
    };
    shared.wake.notify_waiters();
    apply_sends(ctx, shared, &room, &decision.sends);
    reply(200, &json!({"room_id": room}))
}

async fn join(
    ctx: &SpawnContext,
    id: ConnectionId,
    shared: &Shared,
    user: &str,
    room: &str,
) -> Reply {
    let (exists, member, invited, name) = {
        let hub = shared.hub.lock().unwrap_or_else(|e| e.into_inner());
        match hub.rooms.get(room) {
            Some(r) => (
                true,
                r.members.contains(user),
                r.invited.contains(user),
                r.name.clone(),
            ),
            None => (false, false, false, None),
        }
    };
    if !exists {
        return error(404, "M_NOT_FOUND", "No such room");
    }
    if member {
        return reply(200, &json!({"room_id": room}));
    }
    let decision = match ask(
        ctx,
        id,
        &actions::JOIN_EVENT,
        json!({"user_id": user, "room_id": room, "room_name": name, "invited": invited}),
        "join",
    )
    .await
    {
        Ok(d) => d,
        Err(r) => return r,
    };
    if let Some((code, msg)) = decision.refusal {
        return error(403, &code, &msg);
    }
    shared
        .hub
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .join(room, user);
    shared.wake.notify_waiters();
    apply_sends(ctx, shared, room, &decision.sends);
    reply(200, &json!({"room_id": room}))
}

#[allow(clippy::too_many_arguments)]
async fn send(
    ctx: &SpawnContext,
    id: ConnectionId,
    shared: &Shared,
    user: &str,
    room: &str,
    kind: &str,
    txn: &str,
    content: Value,
) -> Reply {
    let key = (user.to_string(), txn.to_string());
    if let Some(event_id) = shared
        .txns
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .0
        .get(&key)
    {
        return reply(200, &json!({"event_id": event_id}));
    }
    let (member, name, members) = {
        let hub = shared.hub.lock().unwrap_or_else(|e| e.into_inner());
        (
            hub.is_member(room, user),
            hub.room_name(room),
            hub.members(room),
        )
    };
    if !member {
        return error(403, "M_FORBIDDEN", "You are not in this room");
    }
    let decision = match ask(
        ctx,
        id,
        &actions::MESSAGE_EVENT,
        json!({"room_id": room, "room_name": name, "sender": user, "event_type": kind,
               "content": content, "members": members}),
        "send",
    )
    .await
    {
        Ok(d) => d,
        Err(r) => return r,
    };
    if let Some((code, msg)) = decision.refusal {
        return error(403, &code, &msg);
    }
    let event_id = shared
        .hub
        .lock()
        .unwrap_or_else(|e| e.into_inner())
        .post(room, user, kind, content, None);
    let Some(event_id) = event_id else {
        return error(404, "M_NOT_FOUND", "No such room");
    };
    {
        let mut t = shared.txns.lock().unwrap_or_else(|e| e.into_inner());
        t.0.insert(key.clone(), event_id.clone());
        t.1.push_back(key);
        while t.1.len() > MAX_TXNS {
            if let Some(old) = t.1.pop_front() {
                t.0.remove(&old);
            }
        }
    }
    shared.wake.notify_waiters();
    apply_sends(ctx, shared, room, &decision.sends);
    reply(200, &json!({"event_id": event_id}))
}

async fn sync(shared: &Shared, user: &str, q: &HashMap<String, String>) -> Reply {
    let since = hub::parse_batch(q.get("since").map(String::as_str));
    let wait = q
        .get("timeout")
        .and_then(|t| t.parse::<u64>().ok())
        .map(Duration::from_millis)
        .unwrap_or_default()
        .min(MAX_SYNC_WAIT);
    let deadline = tokio::time::Instant::now() + wait;
    loop {
        let notified = shared.wake.notified();
        tokio::pin!(notified);
        notified.as_mut().enable();
        let body = shared
            .hub
            .lock()
            .unwrap_or_else(|e| e.into_inner())
            .sync(user, since);
        if let Some(body) = body {
            return reply(200, &body);
        }
        if tokio::time::timeout_at(deadline, notified).await.is_err() {
            return reply(
                200,
                &json!({"next_batch": format!("s{}", since.unwrap_or(0)),
                        "rooms": {"join": {}, "invite": {}, "leave": {}},
                        "presence": {"events": []}, "account_data": {"events": []},
                        "to_device": {"events": []}, "device_lists": {"changed": [], "left": []},
                        "device_one_time_keys_count": {}}),
            );
        }
    }
}