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
use clap::{Parser, Subcommand};
mod commands;
mod config;
mod nostr_client;
mod output;
mod state_sync;
mod storage;
use output::Output;
#[derive(Parser)]
#[command(name = "ndr")]
#[command(version)]
#[command(about = "CLI for encrypted Nostr messaging using double ratchet")]
#[command(
long_about = "A command-line tool for end-to-end encrypted messaging over Nostr.\n\nDesigned for humans, AI agents, and automation."
)]
struct Cli {
/// Output in JSON format (for agents/scripts)
#[arg(short, long, global = true)]
json: bool,
/// Data directory (default: platform data dir/ndr)
#[arg(long, global = true, env = "NDR_DATA_DIR")]
data_dir: Option<std::path::PathBuf>,
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand)]
enum Commands {
/// Login with a private key
Login {
/// Private key (nsec or hex)
key: String,
},
/// Logout and clear all data
Logout,
/// Show current identity
Whoami,
/// Invite management
#[command(subcommand)]
Invite(InviteCommands),
/// Link a device using a private invite
#[command(subcommand)]
Link(LinkCommands),
/// Chat management
#[command(subcommand)]
Chat(ChatCommands),
/// Send a message
Send {
/// Recipient npub, hex pubkey, or contact name
target: String,
/// Message content
message: String,
/// Reply to a specific message ID
#[arg(long)]
reply: Option<String>,
/// Disappearing messages: expire N seconds from now (adds inner rumor ["expiration", ...] tag)
#[arg(long)]
ttl: Option<u64>,
/// Disappearing messages: absolute expiration UNIX timestamp (seconds)
#[arg(long, value_name = "UNIX_SECONDS")]
expires_at: Option<u64>,
},
/// React to a message
React {
/// Chat ID, npub, hex pubkey, or contact name
target: String,
/// Message ID to react to
message_id: String,
/// Emoji reaction (e.g., 👍, ❤️, +1)
emoji: String,
},
/// Send a typing indicator
Typing {
/// Chat ID, npub, hex pubkey, or contact name
target: String,
},
/// Send a delivery/read receipt
Receipt {
/// Chat ID, npub, hex pubkey, or contact name
target: String,
/// Receipt type: "delivered" or "seen"
receipt_type: String,
/// Message IDs to acknowledge
message_ids: Vec<String>,
},
/// Read messages from a chat
Read {
/// Chat ID, npub, hex pubkey, or contact name
target: String,
/// Maximum number of messages to show
#[arg(short, long, default_value = "50")]
limit: usize,
},
/// Receive and decrypt a raw encrypted event JSON
#[command(hide = true)]
Receive {
/// Raw encrypted Nostr event JSON
event: String,
},
/// Manage contacts (petnames)
#[command(subcommand)]
Contact(ContactCommands),
/// Listen for new messages
Listen {
/// Specific chat ID (optional, listens to all if not specified)
#[arg(short, long)]
chat: Option<String>,
},
/// Group management
#[command(subcommand)]
Group(GroupCommands),
}
#[derive(Subcommand)]
enum InviteCommands {
/// Create a new invite
Create {
/// Label for the invite
#[arg(short, long)]
label: Option<String>,
},
/// Create and publish an invite event to relays
Publish {
/// Label for the invite
#[arg(short, long)]
label: Option<String>,
/// Device identifier for invite event (default: your identity pubkey hex)
#[arg(long)]
device_id: Option<String>,
},
/// List all invites
List,
/// Delete an invite
Delete {
/// Invite ID
id: String,
},
/// Process an invite acceptance event (creates chat session)
Accept {
/// Invite ID
invite_id: String,
/// The acceptance event JSON
event: String,
},
}
#[derive(Subcommand)]
enum LinkCommands {
/// Create a private link invite for a new device
Create {
/// Publish this device's public invite event to configured relays (optional).
///
/// This can help multi-device AppKeys fanout, but is intentionally not done by default.
#[arg(long)]
publish: bool,
},
/// Accept a link invite URL
Accept {
/// Invite URL
url: String,
},
}
#[derive(Subcommand)]
enum ContactCommands {
/// Add a contact (petname)
Add {
/// npub or hex pubkey
pubkey: String,
/// Petname
name: String,
},
/// List all contacts
List,
/// Remove a contact
Remove {
/// Petname to remove
name: String,
},
}
#[derive(Subcommand)]
enum GroupCommands {
/// Create a new group
Create {
/// Group name
#[arg(short, long)]
name: String,
/// Member pubkeys (hex), comma-separated
#[arg(short, long, value_delimiter = ',')]
members: Vec<String>,
},
/// List all groups
List,
/// Show group details
Show {
/// Group ID
id: String,
},
/// Delete a group
Delete {
/// Group ID
id: String,
},
/// Update group metadata
Update {
/// Group ID
id: String,
/// New name
#[arg(long)]
name: Option<String>,
/// New description
#[arg(long)]
description: Option<String>,
/// New picture URL
#[arg(long)]
picture: Option<String>,
},
/// Add a member to a group
AddMember {
/// Group ID
id: String,
/// Member pubkey (hex)
pubkey: String,
},
/// Remove a member from a group
RemoveMember {
/// Group ID
id: String,
/// Member pubkey (hex)
pubkey: String,
},
/// Promote a member to admin
AddAdmin {
/// Group ID
id: String,
/// Member pubkey (hex)
pubkey: String,
},
/// Demote an admin
RemoveAdmin {
/// Group ID
id: String,
/// Admin pubkey (hex)
pubkey: String,
},
/// Send a message to all group members
Send {
/// Group ID
id: String,
/// Message content
message: String,
/// Reply to a specific message ID
#[arg(long)]
reply: Option<String>,
},
/// React to a group message
React {
/// Group ID
id: String,
/// Message ID to react to
message_id: String,
/// Emoji reaction
emoji: String,
},
/// Rotate your group sender key (Signal-style) and publish a fresh distribution on the shared channel
RotateSenderKey {
/// Group ID
id: String,
},
/// Accept a group invitation (enable shared channel)
Accept {
/// Group ID
id: String,
},
/// Read group messages
Messages {
/// Group ID
id: String,
/// Maximum number of messages to show
#[arg(short, long, default_value = "50")]
limit: usize,
},
}
#[derive(Subcommand)]
enum ChatCommands {
/// List all chats
List,
/// Join a chat via invite URL
Join {
/// Invite URL or hash
url: String,
},
/// Show chat details
Show {
/// Chat ID
id: String,
},
/// Delete a chat
Delete {
/// Chat ID
id: String,
},
/// Set per-chat disappearing-message TTL (seconds) and optionally notify the peer (kind 10448)
Ttl {
/// Chat ID, npub, hex pubkey, or contact name
target: String,
/// TTL in seconds, or "off"
ttl: String,
/// Only update local settings, do not send an encrypted chat-settings event
#[arg(long)]
local_only: bool,
},
}
#[tokio::main]
async fn main() {
let cli = Cli::parse();
let output = Output::new(cli.json);
let result = run(cli, &output).await;
if let Err(e) = result {
output.error(&e.to_string());
std::process::exit(1);
}
}
async fn run(cli: Cli, output: &Output) -> anyhow::Result<()> {
let data_dir = cli.data_dir.unwrap_or_else(|| {
dirs::data_dir()
.expect("Could not find data directory")
.join("ndr")
});
// Ensure data directory exists
std::fs::create_dir_all(&data_dir)?;
let mut config = config::Config::load(&data_dir)?;
let storage = storage::Storage::open(&data_dir)?;
// Commands that need identity - auto-generate if not logged in
let needs_identity = matches!(
&cli.command,
Commands::Invite(_)
| Commands::Chat(ChatCommands::Join { .. })
| Commands::Send { .. }
| Commands::React { .. }
| Commands::Typing { .. }
| Commands::Receipt { .. }
| Commands::Receive { .. }
| Commands::Listen { .. }
| Commands::Group(GroupCommands::Create { .. })
| Commands::Group(GroupCommands::Update { .. })
| Commands::Group(GroupCommands::AddMember { .. })
| Commands::Group(GroupCommands::RemoveMember { .. })
| Commands::Group(GroupCommands::AddAdmin { .. })
| Commands::Group(GroupCommands::RemoveAdmin { .. })
| Commands::Group(GroupCommands::Send { .. })
| Commands::Group(GroupCommands::React { .. })
| Commands::Group(GroupCommands::RotateSenderKey { .. })
| Commands::Group(GroupCommands::Accept { .. })
);
if needs_identity {
let _ = config.ensure_identity()?;
}
match cli.command {
Commands::Login { key } => commands::identity::login(&key, &config, &storage, output).await,
Commands::Logout => commands::identity::logout(&data_dir, output).await,
Commands::Whoami => commands::identity::whoami(&config, output).await,
Commands::Invite(cmd) => match cmd {
InviteCommands::Create { label } => {
commands::invite::create(label, &config, &storage, output).await
}
InviteCommands::Publish { label, device_id } => {
commands::invite::publish(label, device_id, &config, &storage, output).await
}
InviteCommands::List => commands::invite::list(&storage, output).await,
InviteCommands::Delete { id } => commands::invite::delete(&id, &storage, output).await,
InviteCommands::Accept { invite_id, event } => {
commands::invite::accept(&invite_id, &event, &config, &storage, output).await
}
},
Commands::Link(cmd) => match cmd {
LinkCommands::Create { publish } => {
commands::link::create(&config, &storage, output, publish).await
}
LinkCommands::Accept { url } => {
commands::link::accept(&url, &config, &storage, output).await
}
},
Commands::Chat(cmd) => match cmd {
ChatCommands::List => commands::chat::list(&storage, output).await,
ChatCommands::Join { url } => {
commands::chat::join(&url, &config, &storage, output).await
}
ChatCommands::Show { id } => commands::chat::show(&id, &storage, output).await,
ChatCommands::Delete { id } => {
commands::chat::delete(&id, &config, &storage, output).await
}
ChatCommands::Ttl {
target,
ttl,
local_only,
} => commands::chat::ttl(&target, &ttl, local_only, &config, &storage, output).await,
},
Commands::Send {
target,
message,
reply,
ttl,
expires_at,
} => {
commands::message::send(
&target,
&message,
reply.as_deref(),
ttl,
expires_at,
&config,
&storage,
output,
)
.await
}
Commands::React {
target,
message_id,
emoji,
} => {
commands::message::react(&target, &message_id, &emoji, &config, &storage, output).await
}
Commands::Typing { target } => {
commands::message::typing(&target, &config, &storage, output).await
}
Commands::Receipt {
target,
receipt_type,
message_ids,
} => {
let ids: Vec<&str> = message_ids.iter().map(|s| s.as_str()).collect();
commands::message::receipt(&target, &receipt_type, &ids, &config, &storage, output)
.await
}
Commands::Read { target, limit } => {
commands::message::read(&target, limit, &storage, output).await
}
Commands::Receive { event } => commands::message::receive(&event, &storage, output).await,
Commands::Contact(cmd) => match cmd {
ContactCommands::Add { pubkey, name } => {
commands::contact::add(&pubkey, &name, &storage, output).await
}
ContactCommands::List => commands::contact::list(&storage, output).await,
ContactCommands::Remove { name } => {
commands::contact::remove(&name, &storage, output).await
}
},
Commands::Listen { chat } => {
commands::message::listen(chat.as_deref(), &config, &storage, output).await
}
Commands::Group(cmd) => match cmd {
GroupCommands::Create { name, members } => {
commands::group::create(&name, &members, &config, &storage, output).await
}
GroupCommands::List => commands::group::list(&storage, output).await,
GroupCommands::Show { id } => commands::group::show(&id, &storage, output).await,
GroupCommands::Delete { id } => commands::group::delete(&id, &storage, output).await,
GroupCommands::Update {
id,
name,
description,
picture,
} => {
commands::group::update(
&id,
name.as_deref(),
description.as_deref(),
picture.as_deref(),
&config,
&storage,
output,
)
.await
}
GroupCommands::AddMember { id, pubkey } => {
commands::group::add_member(&id, &pubkey, &config, &storage, output).await
}
GroupCommands::RemoveMember { id, pubkey } => {
commands::group::remove_member(&id, &pubkey, &config, &storage, output).await
}
GroupCommands::AddAdmin { id, pubkey } => {
commands::group::add_admin(&id, &pubkey, &config, &storage, output).await
}
GroupCommands::RemoveAdmin { id, pubkey } => {
commands::group::remove_admin(&id, &pubkey, &config, &storage, output).await
}
GroupCommands::Send { id, message, reply } => {
commands::group::send_message(
&id,
&message,
reply.as_deref(),
&config,
&storage,
output,
)
.await
}
GroupCommands::React {
id,
message_id,
emoji,
} => commands::group::react(&id, &message_id, &emoji, &config, &storage, output).await,
GroupCommands::RotateSenderKey { id } => {
commands::group::rotate_sender_key(&id, &config, &storage, output).await
}
GroupCommands::Accept { id } => {
commands::group::accept(&id, &config, &storage, output).await
}
GroupCommands::Messages { id, limit } => {
commands::group::messages(&id, limit, &storage, output).await
}
},
}
}