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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
#![recursion_limit = "256"]
mod app;
mod cli;
mod config;
mod event;
mod fs_utils;
mod global_ptt;
mod input;
mod matrix;
mod state;
mod terminal;
mod ui;
mod voip;
use std::sync::Arc;
use std::time::{Duration, Instant};
use anyhow::Result;
use crossterm::event::{Event, EventStream, KeyEventKind};
use tokio::sync::Mutex;
use tokio_stream::StreamExt;
use tracing::{error, info};
use tracing_subscriber::{EnvFilter, fmt, prelude::*};
use app::App;
use event::AppEvent;
use event::WarnClosed;
macro_rules! spawn_with_client {
($holder:expr, |$client:ident| $body:expr) => {{
let holder = $holder.clone();
tokio::spawn(async move {
if let Some($client) = holder.lock().await.clone() {
$body.await;
}
})
}};
}
const NOISY_DEPS: &[&str] = &[
"matrix_sdk",
"hyper",
"livekit",
"tokio",
"rustls",
"reqwest",
"cpal",
];
/// Build the tracing `EnvFilter` from the `GOSUTO_LOG` environment variable.
///
/// - **Unset**: `warn,gosuto=info,<noisy deps>=warn`
/// - **Simple level** (e.g. `debug`): `warn,gosuto=debug,<noisy deps>=warn`
/// - **Full filter** (contains `,` or `=`): used as-is
fn build_env_filter() -> EnvFilter {
let filter = match std::env::var("GOSUTO_LOG") {
Ok(val) if !val.is_empty() => {
if val.contains(',') || val.contains('=') {
// Advanced filter string — use as-is
return EnvFilter::new(val);
}
// Simple level — scope to gosuto only
let mut parts = vec![format!("warn,gosuto={val}")];
for dep in NOISY_DEPS {
parts.push(format!("{dep}=warn"));
}
parts.join(",")
}
_ => {
// Default filter
let mut parts = vec!["warn,gosuto=info".to_owned()];
for dep in NOISY_DEPS {
parts.push(format!("{dep}=warn"));
}
parts.join(",")
}
};
EnvFilter::new(filter)
}
#[tokio::main]
async fn main() -> Result<()> {
let cli_args = cli::parse_args();
config::init_profile(cli_args.profile);
// Set up file-based logging (controlled by GOSUTO_LOG env var)
let log_path = config::log_path()?;
let file_appender = tracing_appender::rolling::daily(&log_path, "gosuto.log");
let (non_blocking, _guard) = tracing_appender::non_blocking(file_appender);
tracing_subscriber::registry()
.with(fmt::layer().with_writer(non_blocking).with_ansi(false))
.with(build_env_filter())
.init();
config::cleanup_old_logs(&log_path, 7);
info!("Starting gōsuto");
// Create event channel
let (event_tx, mut event_rx) = event::event_channel();
// Initialize terminal
let mut tui = terminal::init()?;
let picker = terminal::init_picker();
info!(
"Image protocol: {:?}, font size: {:?}",
picker.protocol_type(),
picker.font_size()
);
terminal::init_keyboard_enhancement();
// Suppress native-library stdout/stderr writes (e.g. "VAAPI is supported")
// that would bleed through the TUI as visual artifacts. Must happen AFTER
// init_keyboard_enhancement which still writes to fd 1.
#[cfg(unix)]
let saved_stdout = terminal::suppress_stdout();
#[cfg(unix)]
let saved_stderr = terminal::suppress_stderr();
// Restore terminal on panic so spawned-task panics produce readable output
// instead of corrupting the raw-mode terminal.
let default_hook = std::panic::take_hook();
std::panic::set_hook(Box::new(move |info| {
#[cfg(unix)]
terminal::restore_stdout_from_global();
let _ = terminal::restore();
default_hook(info);
}));
// Load config
let gosuto_config = config::load_config();
info!("Config: {:?}", gosuto_config);
// Create app
let accept_invalid_certs = gosuto_config.network.accept_invalid_certs;
let (image_decode_tx, image_decode_rx) = std::sync::mpsc::channel();
let mut app = App::new(event_tx.clone(), gosuto_config, picker, image_decode_tx);
// Shared Matrix client
let matrix_client: Arc<Mutex<Option<matrix_sdk::Client>>> = Arc::new(Mutex::new(None));
// Shared state for incoming verification requests
let incoming_verification: matrix::sync::IncomingVerification = Arc::new(Mutex::new(None));
// Create CallManager command channel early so sync handlers can forward encryption keys
let (call_cmd_tx, call_cmd_rx) = voip::manager::command_channel();
app.call_cmd_tx = Some(call_cmd_tx.clone());
// Try to restore session
match matrix::client::try_restore_session(&event_tx, accept_invalid_certs).await {
Ok(Some(client)) => {
*matrix_client.lock().await = Some(client.clone());
let tx = event_tx.clone();
let iv = incoming_verification.clone();
let cmd_tx = call_cmd_tx.clone();
tokio::spawn(async move {
if let Err(e) = matrix::sync::start_sync(client, tx.clone(), iv, Some(cmd_tx)).await
{
error!("Sync error: {}", e);
tx.send(AppEvent::SyncError(e.to_string()))
.warn_closed("SyncError");
}
});
}
Ok(None) => {
info!("No stored session found");
if let Some(creds) = matrix::credentials::load_credentials() {
event_tx
.send(AppEvent::AutoLogin {
homeserver: creds.homeserver,
username: creds.username,
password: creds.password,
})
.warn_closed("AutoLogin");
}
}
Err(e) => {
info!("Failed to restore session: {}", e);
// Clean up stale session and store to avoid repeated failures
if let Ok(session_path) = config::session_path() {
if let Ok(stored) = matrix::session::load_session(&session_path)
&& let Ok(store_path) =
config::store_path_for_homeserver_unchecked(&stored.homeserver)
{
info!("Removing stale store at {}", store_path.display());
if let Err(e) = std::fs::remove_dir_all(&store_path) {
info!("Could not remove store: {}", e);
}
}
if let Err(e) = matrix::session::delete_session(&session_path) {
tracing::warn!("Failed to delete session: {e}");
}
}
if let Some(creds) = matrix::credentials::load_credentials() {
event_tx
.send(AppEvent::AutoLogin {
homeserver: creds.homeserver,
username: creds.username,
password: creds.password,
})
.warn_closed("AutoLogin");
}
}
}
// Spawn crossterm event reader
let input_tx = event_tx.clone();
tokio::spawn(async move {
let mut reader = EventStream::new();
while let Some(Ok(event)) = reader.next().await {
match event {
Event::Key(key) => match key.kind {
KeyEventKind::Press | KeyEventKind::Repeat => {
input_tx.send(AppEvent::Key(key)).warn_closed("Key");
}
KeyEventKind::Release => {
input_tx
.send(AppEvent::KeyRelease)
.warn_closed("KeyRelease");
}
},
Event::Resize(_, _) => {
input_tx.send(AppEvent::Resize).warn_closed("Resize");
}
_ => {}
}
}
});
// Tick timer
let tick_tx = event_tx.clone();
tokio::spawn(async move {
let mut interval = tokio::time::interval(Duration::from_millis(config::TICK_RATE_MS));
loop {
interval.tick().await;
if tick_tx.send(AppEvent::Tick).is_err() {
break;
}
}
});
// Shared audio config for CallManager
let shared_audio_config = Arc::new(parking_lot::RwLock::new(app.config.audio.clone()));
app.shared_audio_config = Some(shared_audio_config.clone());
let ptt_transmitting = app.ptt_transmitting.clone();
let mic_active = app.mic_active.clone();
// Spawn CallManager
let call_manager = voip::manager::CallManager::new(
call_cmd_rx,
event_tx.clone(),
matrix_client.clone(),
shared_audio_config.clone(),
ptt_transmitting,
mic_active,
);
tokio::spawn(call_manager.run());
// Spawn global PTT listener when push-to-talk is enabled
if app.config.audio.push_to_talk {
let ptt_key = app
.config
.audio
.push_to_talk_key
.clone()
.unwrap_or_default();
let handle =
global_ptt::spawn_listener(app.ptt_transmitting.clone(), ptt_key, event_tx.clone());
app.global_ptt = Some(handle);
}
// Track login/registration state to avoid re-triggering
let mut login_in_progress = false;
let mut registration_in_progress = false;
// Track popup state to clear terminal on close (restores Kitty images)
// Main loop
let render_interval = Duration::from_millis(config::RENDER_RATE_MS);
let mut last_render = Instant::now();
loop {
// Calculate time until next render is due
let until_render = render_interval.saturating_sub(last_render.elapsed());
// Wait for at least one event, or wake when it's time to render
tokio::select! {
event = event_rx.recv() => {
match event {
Some(ev) => {
// Track room change for message fetching
let prev_room = app.messages.current_room_id.clone();
app.handle_event(ev);
// Drain ALL remaining pending events so key events
// are never buried behind MicLevel floods
while let Ok(ev) = event_rx.try_recv() {
app.handle_event(ev);
}
let new_room = app.messages.current_room_id.clone();
// Fetch messages and members when room changes
if prev_room != new_room {
// Clear stale members and image cache immediately
app.members_list.clear();
app.image_cache.clear();
while image_decode_rx.try_recv().is_ok() {}
if let Some(ref room_id) = new_room {
// Fetch messages
let client_holder = matrix_client.clone();
let tx = event_tx.clone();
let rid = room_id.clone();
let sync_token = app.sync_token.clone();
tokio::spawn(async move {
let client = { client_holder.lock().await.clone() };
if let Some(client) = client {
if let Err(e) = matrix::messages::fetch_messages(&client, &rid, &tx, sync_token).await {
error!("Failed to fetch messages for {}: {:?}", rid, e);
tx.send(AppEvent::FetchError {
room_id: rid,
error: e.to_string(),
}).warn_closed("FetchError");
}
} else {
tx.send(AppEvent::FetchError {
room_id: rid,
error: "Not connected".to_string(),
}).warn_closed("FetchError");
}
});
// Fetch members
let tx2 = event_tx.clone();
let rid2 = room_id.clone();
spawn_with_client!(matrix_client, |client| async move {
matrix::rooms::fetch_room_members(&client, &rid2, &tx2).await;
matrix::rooms::check_member_verification(&client, &rid2, &tx2).await;
});
// Send read receipt
let rid3 = room_id.clone();
spawn_with_client!(matrix_client, |client| async move {
matrix::rooms::mark_room_as_read(&client, &rid3, None).await;
});
}
}
// Re-fetch messages after verification
if app.pending_refetch {
app.pending_refetch = false;
if let Some(ref room_id) = app.messages.current_room_id.clone() {
app.messages.messages.clear();
app.messages.loading = true;
let tx = event_tx.clone();
let rid = room_id.clone();
let sync_token = app.sync_token.clone();
spawn_with_client!(matrix_client, |client| async move {
if let Err(e) = matrix::messages::fetch_messages(&client, &rid, &tx, sync_token).await {
error!("Failed to re-fetch messages for {}: {:?}", rid, e);
tx.send(AppEvent::FetchError {
room_id: rid,
error: e.to_string(),
}).warn_closed("FetchError");
}
});
}
}
// Lazy-load older messages when user scrolls to top
if app.pending_load_more {
app.pending_load_more = false;
if let Some(ref room_id) = app.messages.current_room_id.clone() {
let tx = event_tx.clone();
let rid = room_id.clone();
let token = app.messages.pagination_token.clone();
spawn_with_client!(matrix_client, |client| async move {
if let Err(e) = matrix::messages::fetch_messages(&client, &rid, &tx, token).await {
error!("Failed to load more messages for {}: {:?}", rid, e);
tx.send(AppEvent::FetchError {
room_id: rid,
error: e.to_string(),
}).warn_closed("FetchError");
}
});
}
}
// Handle message sending
if let Some(pending) = app.take_pending_send() {
let tx = event_tx.clone();
if let Some(edit_ctx) = pending.edit {
spawn_with_client!(matrix_client, |client| async move {
if let Err(e) = matrix::messages::send_edit(&client, &pending.room_id, &edit_ctx.event_id, &pending.body, &tx).await {
error!("Failed to send edit: {}", e);
}
});
} else {
spawn_with_client!(matrix_client, |client| async move {
let reply_to = pending.reply_to.as_ref().map(|r| (r.event_id.as_str(), r.sender.as_str()));
if let Err(e) = matrix::messages::send_message(&client, &pending.room_id, &pending.body, reply_to, &tx).await {
error!("Failed to send message: {}", e);
}
});
}
}
// Handle reaction sending
if let Some(pending) = app.take_pending_reaction() {
let tx = event_tx.clone();
if let Some(reaction_eid) = pending.toggle_off_reaction_event_id {
let rid = pending.room_id;
spawn_with_client!(matrix_client, |client| async move {
let _ = matrix::messages::redact_reaction(
&client, &rid, &reaction_eid, &tx,
)
.await;
});
} else {
let rid = pending.room_id;
let target_eid = pending.target_event_id;
let emoji = pending.emoji_key;
let sender = match &app.auth {
crate::state::AuthState::LoggedIn { user_id, .. } => {
user_id.clone()
}
_ => String::new(),
};
spawn_with_client!(matrix_client, |client| async move {
let _ = matrix::messages::send_reaction(
&client, &rid, &target_eid, &emoji, &sender, &tx,
)
.await;
});
}
}
// Handle message redaction
if let Some(pending) = app.take_pending_redact() {
let tx = event_tx.clone();
let rid = pending.room_id;
let eid = pending.event_id;
spawn_with_client!(matrix_client, |client| async move {
let _ = matrix::messages::redact_message(
&client, &rid, &eid, &tx,
)
.await;
});
}
// Handle outgoing typing notice
if let Some((room_id, typing)) = app.take_pending_typing_notice() {
spawn_with_client!(matrix_client, |client| async move {
let room_id_parsed: Result<matrix_sdk::ruma::OwnedRoomId, _> = room_id.as_str().try_into();
if let Ok(id) = room_id_parsed
&& let Some(room) = client.get_room(&id)
&& let Err(e) = room.typing_notice(typing).await
{
tracing::warn!("typing_notice failed: {e}");
}
});
}
// Handle read receipt for new messages in open room
if let Some((room_id, event_id)) = app.pending_read_receipt.take() {
spawn_with_client!(matrix_client, |client| async move {
matrix::rooms::mark_room_as_read(&client, &room_id, event_id.as_deref()).await;
});
}
// Handle logout
if app.pending_logout {
app.pending_logout = false;
let client_holder = matrix_client.clone();
let tx = event_tx.clone();
tokio::spawn(async move {
let client = { client_holder.lock().await.take() };
if let Some(client) = client
&& let Err(e) = matrix::client::logout(&client).await
{
error!("Logout error: {}", e);
}
tx.send(AppEvent::LoggedOut).warn_closed("LoggedOut");
});
}
// Handle room join
if let Some(room_id) = app.take_pending_join() {
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
let room_id_parsed: Result<matrix_sdk::ruma::OwnedRoomOrAliasId, _> = room_id.as_str().try_into();
match room_id_parsed {
Ok(id) => {
if let Err(e) = client.join_room_by_id_or_alias(&id, &[]).await {
tx.send(AppEvent::SyncError(format!("Join failed: {}", e))).warn_closed("SyncError");
}
}
Err(e) => {
tx.send(AppEvent::SyncError(format!("Invalid room: {}", e))).warn_closed("SyncError");
}
}
});
}
// Handle pending DM
if let Some(user_id_str) = app.take_pending_dm() {
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
let user_id: Result<matrix_sdk::ruma::OwnedUserId, _> = user_id_str.as_str().try_into();
match user_id {
Ok(uid) => {
// Check for existing DM room
if let Some(room) = client.get_dm_room(&uid) {
tx.send(AppEvent::DmRoomReady {
room_id: room.room_id().to_string(),
}).warn_closed("DmRoomReady");
} else {
// Create new DM room
use matrix_sdk::ruma::api::client::room::create_room::v3::Request as CreateRoomRequest;
let mut request = CreateRoomRequest::new();
request.invite = vec![uid.clone()];
request.is_direct = true;
use matrix_sdk::ruma::events::InitialStateEvent;
use matrix_sdk::ruma::events::room::encryption::RoomEncryptionEventContent;
let enc = RoomEncryptionEventContent::with_recommended_defaults();
let enc_event = InitialStateEvent::with_empty_state_key(enc);
request.initial_state.push(enc_event.to_raw_any());
// Set call member event PL to 0 so both DM participants can use calls
request.power_level_content_override = matrix::rooms::call_power_level_override();
match client.create_room(request).await {
Ok(response) => {
tx.send(AppEvent::DmRoomReady {
room_id: response.room_id().to_string(),
}).warn_closed("DmRoomReady");
}
Err(e) => {
tx.send(AppEvent::SyncError(format!("Failed to create DM: {}", e))).warn_closed("SyncError");
}
}
}
}
Err(e) => {
tx.send(AppEvent::SyncError(format!("Invalid user ID: {}", e))).warn_closed("SyncError");
}
}
});
}
// Handle room creation
if let Some(params) = app.take_pending_create_room() {
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
use matrix_sdk::ruma::api::client::room::create_room::v3::Request as CreateRoomRequest;
use matrix_sdk::ruma::events::InitialStateEvent;
use matrix_sdk::ruma::events::room::history_visibility::{
HistoryVisibility, RoomHistoryVisibilityEventContent,
};
let mut request = CreateRoomRequest::new();
request.name = Some(params.name);
// Set topic as initial state if provided
if let Some(topic) = params.topic {
use matrix_sdk::ruma::events::room::topic::RoomTopicEventContent;
let topic_content = RoomTopicEventContent::new(topic);
let topic_event = InitialStateEvent::with_empty_state_key(topic_content);
request.initial_state.push(topic_event.to_raw_any());
}
// Set history visibility as initial state
let vis = match params.history_visibility.as_str() {
"invited" => HistoryVisibility::Invited,
"joined" => HistoryVisibility::Joined,
"world_readable" => HistoryVisibility::WorldReadable,
_ => HistoryVisibility::Shared,
};
let vis_content = RoomHistoryVisibilityEventContent::new(vis);
let initial_event = InitialStateEvent::with_empty_state_key(vis_content);
request.initial_state.push(initial_event.to_raw_any());
// Enable encryption if requested
if params.encrypted {
use matrix_sdk::ruma::events::room::encryption::RoomEncryptionEventContent;
let enc = RoomEncryptionEventContent::with_recommended_defaults();
let enc_event = InitialStateEvent::with_empty_state_key(enc);
request.initial_state.push(enc_event.to_raw_any());
}
// Set call member event PL to 0 so all participants can use calls
request.power_level_content_override = matrix::rooms::call_power_level_override();
match client.create_room(request).await {
Ok(response) => {
tx.send(AppEvent::RoomCreated {
room_id: response.room_id().to_string(),
}).warn_closed("RoomCreated");
}
Err(e) => {
tx.send(AppEvent::SyncError(format!("Failed to create room: {}", e))).warn_closed("SyncError");
}
}
});
}
// Handle room leave
if let Some(room_id) = app.take_pending_leave() {
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
let room_id_parsed: Result<matrix_sdk::ruma::OwnedRoomId, _> = room_id.as_str().try_into();
if let Ok(id) = room_id_parsed
&& let Some(room) = client.get_room(&id)
&& let Err(e) = room.leave().await
{
tx.send(AppEvent::SyncError(format!("Leave failed: {}", e))).warn_closed("SyncError");
}
});
}
// Handle accept invite
if let Some(room_id) = app.take_pending_accept_invite() {
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
matrix::rooms::accept_invite(&client, &room_id, &tx).await;
});
}
// Handle decline invite
if let Some(room_id) = app.take_pending_decline_invite() {
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
matrix::rooms::decline_invite(&client, &room_id, &tx).await;
});
}
// Handle invite user
if let Some((room_id, user_id)) = app.take_pending_invite_user() {
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
matrix::rooms::invite_user(&client, &room_id, &user_id, &tx).await;
});
}
// Handle user config fetch
if app.pending_user_config {
app.pending_user_config = false;
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
matrix::profile::fetch_user_config(&client, &tx).await;
});
}
// Handle display name update
if let Some(name) = app.pending_set_display_name.take() {
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
matrix::profile::set_user_display_name(&client, &name, &tx).await;
});
}
// Handle password change
if let Some((current, new)) = app.pending_change_password.take() {
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
matrix::profile::change_user_password(&client, ¤t, &new, &tx).await;
});
}
// Handle room info fetch
if app.pending_room_info {
app.pending_room_info = false;
let tx = event_tx.clone();
let rid = app.room_info.room_id.clone();
spawn_with_client!(matrix_client, |client| async move {
matrix::rooms::fetch_room_info(&client, &rid, &tx).await;
});
}
// Handle visibility update
if let Some((room_id, visibility)) = app.pending_set_visibility.take() {
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
matrix::rooms::set_history_visibility(&client, &room_id, &visibility, &tx).await;
});
}
// Handle room topic update
if let Some((room_id, topic)) = app.pending_set_room_topic.take() {
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
matrix::rooms::set_room_topic(&client, &room_id, &topic, &tx).await;
});
}
// Handle room name update
if let Some((room_id, name)) = app.pending_set_room_name.take() {
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
matrix::rooms::set_room_name(&client, &room_id, &name, &tx).await;
});
}
// Handle encryption enable
if let Some(room_id) = app.pending_enable_encryption.take() {
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
matrix::rooms::enable_encryption(&client, &room_id, &tx).await;
});
}
// Handle recovery actions
if let Some(action) = app.pending_recovery.take() {
use state::RecoveryAction;
match action {
RecoveryAction::SubmitPassword(password) => {
if let Some(ref mut modal) = app.recovery
&& let Some(tx) = modal.password_tx.take()
&& tx.send(password).is_err()
{
tracing::warn!("password oneshot: receiver dropped");
}
}
other => {
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
match other {
RecoveryAction::Check => {
client.encryption().wait_for_e2ee_initialization_tasks().await;
let state = client.encryption().recovery().state();
use matrix_sdk::encryption::recovery::RecoveryState;
let stage = match state {
RecoveryState::Enabled => state::RecoveryStage::Enabled,
RecoveryState::Disabled => state::RecoveryStage::Disabled,
RecoveryState::Incomplete => state::RecoveryStage::Incomplete,
_ => state::RecoveryStage::Disabled,
};
tx.send(AppEvent::RecoveryStateChecked(stage)).warn_closed("RecoveryStateChecked");
}
RecoveryAction::Create => {
match client.encryption().recovery()
.enable().wait_for_backups_to_upload().await {
Ok(key) => { tx.send(AppEvent::RecoveryKeyReady(key)).warn_closed("RecoveryKeyReady"); }
Err(e) => { tx.send(AppEvent::RecoveryError(e.to_string())).warn_closed("RecoveryError"); }
}
}
RecoveryAction::Recover(phrase) => {
match client.encryption().recovery().recover(&phrase).await {
Ok(()) => {
let state = client.encryption().recovery().state();
if matches!(state, matrix_sdk::encryption::recovery::RecoveryState::Incomplete) {
match matrix::client::heal_recovery(&client, &tx).await {
Ok(new_key) => {
tx.send(AppEvent::RecoveryKeyReady(new_key)).warn_closed("RecoveryKeyReady");
}
Err(e) => {
tx.send(AppEvent::RecoveryError(
format!("Recovery succeeded but healing failed: {}", e),
)).warn_closed("RecoveryError");
}
}
} else {
// Download room keys from backup so messages can be decrypted
let rooms: Vec<_> = client.joined_rooms().iter()
.map(|r| r.room_id().to_owned())
.collect();
for room_id in &rooms {
if let Err(e) = client.encryption().backups().download_room_keys_for_room(room_id).await { tracing::warn!("download_room_keys failed for {room_id}: {e}"); }
}
tx.send(AppEvent::RecoveryRecovered).warn_closed("RecoveryRecovered");
}
}
Err(e) => { tx.send(AppEvent::RecoveryError(e.to_string())).warn_closed("RecoveryError"); }
}
}
RecoveryAction::Reset => {
use matrix_sdk::encryption::recovery::RecoveryState;
let is_incomplete = matches!(
client.encryption().recovery().state(),
RecoveryState::Incomplete
);
if is_incomplete {
match matrix::client::heal_recovery(&client, &tx).await {
Ok(key) => { tx.send(AppEvent::RecoveryKeyReady(key)).warn_closed("RecoveryKeyReady"); }
Err(e) => { tx.send(AppEvent::RecoveryError(e.to_string())).warn_closed("RecoveryError"); }
}
} else {
match client.encryption().recovery().reset_key().await {
Ok(key) => { tx.send(AppEvent::RecoveryKeyReady(key)).warn_closed("RecoveryKeyReady"); }
Err(e) => { tx.send(AppEvent::RecoveryError(e.to_string())).warn_closed("RecoveryError"); }
}
}
}
RecoveryAction::SubmitPassword(_) => unreachable!(),
}
});
}
}
}
// Handle outgoing verification (:verify command)
if let Some(target) = app.take_pending_verify() {
if let Some(handle) = app.verify_task_handle.take() {
handle.abort();
}
let tx = event_tx.clone();
let (confirm_tx, confirm_rx) = tokio::sync::oneshot::channel();
app.verify_confirm_tx = Some(confirm_tx);
let handle = spawn_with_client!(matrix_client, |client| async move {
match target {
None => {
matrix::verification::start_self_verification(
client, tx, confirm_rx,
)
.await;
}
Some(user_id) => {
matrix::verification::start_user_verification(
client, &user_id, tx, confirm_rx,
)
.await;
}
}
});
app.verify_task_handle = Some(handle);
}
// Handle cross-signing reset
if app.pending_reset_cross_signing {
app.pending_reset_cross_signing = false;
let tx = event_tx.clone();
spawn_with_client!(matrix_client, |client| async move {
match matrix::client::reset_cross_signing_with_uia(&client, &tx)
.await
{
Ok(()) => {
tx.send(AppEvent::CrossSigningResetCompleted)
.warn_closed("CrossSigningResetCompleted");
}
Err(e) => {
tx.send(AppEvent::CrossSigningResetError(e.to_string()))
.warn_closed("CrossSigningResetError");
}
}
});
}
// Handle incoming verification requests
{
let mut iv_guard = incoming_verification.lock().await;
if let Some(request) = iv_guard.take() {
if let Some(handle) = app.verify_task_handle.take() {
handle.abort();
}
let tx = event_tx.clone();
let (confirm_tx, confirm_rx) = tokio::sync::oneshot::channel();
app.verify_confirm_tx = Some(confirm_tx);
let handle = tokio::spawn(async move {
matrix::verification::handle_incoming_verification(
request, tx, confirm_rx,
)
.await;
});
app.verify_task_handle = Some(handle);
}
}
}
None => break,
}
}
_ = tokio::time::sleep(until_render) => {}
}
// Check for login trigger
if app.is_logging_in() && !login_in_progress {
login_in_progress = true;
let (homeserver, username, password) = app.login_credentials();
let tx = event_tx.clone();
let client_holder = matrix_client.clone();
let iv = incoming_verification.clone();
let cmd_tx = call_cmd_tx.clone();
tokio::spawn(async move {
match matrix::client::login(
&homeserver,
&username,
&password,
&tx,
accept_invalid_certs,
)
.await
{
Ok(client) => {
*client_holder.lock().await = Some(client.clone());
let sync_tx = tx.clone();
tokio::spawn(async move {
if let Err(e) =
matrix::sync::start_sync(client, sync_tx.clone(), iv, Some(cmd_tx))
.await
{
error!("Sync error: {}", e);
sync_tx
.send(AppEvent::SyncError(e.to_string()))
.warn_closed("SyncError");
}
});
}
Err(e) => {
error!("Login failed: {:#}", e);
tx.send(AppEvent::LoginFailure(e.to_string()))
.warn_closed("LoginFailure");
}
}
});
}
// Reset login tracking when auth state changes away from LoggingIn
if !app.is_logging_in() {
login_in_progress = false;
}
// Check for registration trigger
if app.is_registering() && !registration_in_progress {
registration_in_progress = true;
let (homeserver, username, password, token) = app.registration_credentials();
let tx = event_tx.clone();
let client_holder = matrix_client.clone();
let iv = incoming_verification.clone();
let cmd_tx = call_cmd_tx.clone();
tokio::spawn(async move {
match matrix::client::register(
&homeserver,
&username,
&password,
&token,
&tx,
accept_invalid_certs,
)
.await
{
Ok(client) => {
*client_holder.lock().await = Some(client.clone());
let sync_tx = tx.clone();
tokio::spawn(async move {
if let Err(e) =
matrix::sync::start_sync(client, sync_tx.clone(), iv, Some(cmd_tx))
.await
{
error!("Sync error: {}", e);
sync_tx
.send(AppEvent::SyncError(e.to_string()))
.warn_closed("SyncError");
}
});
}
Err(e) => {
error!("Registration failed: {:#}", e);
tx.send(AppEvent::RegisterFailure(e.to_string()))
.warn_closed("RegisterFailure");
}
}
});
}
// Reset registration tracking
if !app.is_registering() {
registration_in_progress = false;
}
// Tick + render only when render_interval has elapsed
let now = Instant::now();
let elapsed = now.duration_since(last_render);
if elapsed >= render_interval {
let dt = elapsed.as_millis() as u64;
last_render = now;
let term_size = tui.size()?;
let term_area = ratatui::layout::Rect::new(0, 0, term_size.width, term_size.height);
let logged_in = app.auth.is_logged_in();
app.effects.tick(dt, term_area, logged_in);
// Tick EMP effect with approximate room pane area
let room_focused = app.vim.focus == crate::input::FocusPanel::RoomList;
let room_area = ratatui::layout::Rect::new(
term_area.x,
term_area.y,
24, // matches layout::compute_layout Constraint::Length(24)
term_area.height.saturating_sub(1),
);
app.effects.tick_emp(dt, room_area, room_focused);
// Tick EMP effect for members pane
let members_focused = app.vim.focus == crate::input::FocusPanel::Members;
let members_area = ratatui::layout::Rect::new(
term_area.width.saturating_sub(20),
term_area.y,
20, // matches layout::compute_layout Constraint::Length(20)
term_area.height.saturating_sub(1),
);
app.effects
.tick_members_emp(dt, members_area, members_focused);
app.anim_clock.tick(dt);
app.room_list_anim.tick(dt);
app.chat_title_reveal.tick(dt);
app.members_title_reveal.tick(dt);
if let Some(ref info) = app.call_info {
let ds = match info.state {
voip::CallState::Connecting(_) => {
ui::call_overlay::CallDisplayState::Connecting
}
voip::CallState::Active => ui::call_overlay::CallDisplayState::Active,
};
app.call_popup.tick(dt, &ds);
} else if app.incoming_call_room.is_some() {
app.call_popup
.tick(dt, &ui::call_overlay::CallDisplayState::Ringing);
}
// Process at most one decoded image per frame to avoid batch freeze
if let Ok((event_id, result)) = image_decode_rx.try_recv() {
match result {
Ok((protocol, width, height)) => {
app.image_cache.insert(
event_id,
state::image_cache::CachedImage {
protocol: Some(protocol),
width: Some(width),
height: Some(height),
last_encoded_rect: None,
},
);
}
Err(e) => {
error!("Failed to decode image {}: {}", event_id, e);
app.image_cache.mark_failed(&event_id);
}
}
}
tui.draw(|frame| ui::render(&mut app, frame))?;
}
if !app.running {
break;
}
}
// Cleanup
if let Some(ref handle) = app.global_ptt {
handle
.active
.store(false, std::sync::atomic::Ordering::Relaxed);
}
let _ = call_cmd_tx.send(voip::manager::CallCommand::Shutdown);
#[cfg(unix)]
if let Some(ref fd) = saved_stdout {
terminal::restore_stdout(fd);
}
#[cfg(unix)]
if let Some(ref fd) = saved_stderr {
terminal::restore_stderr(fd);
}
terminal::restore()?;
info!("gōsuto shut down cleanly");
// Flush logs before exit (process::exit skips destructors)
drop(_guard);
// Force-exit to terminate the blocking rdev listener thread and any
// in-flight async tasks that would otherwise delay shutdown.
std::process::exit(0);
}