2024-07-13 13:00:42 +00:00
|
|
|
use std::borrow::Borrow;
|
2024-07-11 07:53:58 +00:00
|
|
|
use std::env::args;
|
2024-07-13 13:00:42 +00:00
|
|
|
use std::io::{stdin, stdout, Write};
|
2024-07-09 14:56:08 +00:00
|
|
|
use std::net::{Ipv4Addr, SocketAddr, SocketAddrV4};
|
2024-07-13 13:00:42 +00:00
|
|
|
use std::ops::Deref;
|
2024-07-11 07:53:58 +00:00
|
|
|
use std::time::Duration;
|
2024-07-13 13:00:42 +00:00
|
|
|
use once_cell::sync::Lazy;
|
2024-07-11 07:53:58 +00:00
|
|
|
use nostr_sdk::async_utility::futures_util::TryFutureExt;
|
2024-07-09 14:56:08 +00:00
|
|
|
use nostr_sdk::prelude::*;
|
|
|
|
|
2024-07-13 13:00:42 +00:00
|
|
|
|
|
|
|
static TASK_KIND: Lazy<Kind> = Lazy::new(||Kind::from(90002));
|
|
|
|
|
|
|
|
static MY_KEYS: Lazy<Keys> = Lazy::new(||Keys::generate());
|
|
|
|
static CLIENT: Lazy<Client> = Lazy::new(||Client::new(MY_KEYS.borrow().deref()));
|
|
|
|
|
2024-07-09 14:56:08 +00:00
|
|
|
#[tokio::main]
|
|
|
|
async fn main() {
|
|
|
|
let proxy = Some(SocketAddr::V4(SocketAddrV4::new(Ipv4Addr::LOCALHOST, 9050)));
|
|
|
|
|
2024-07-13 13:00:42 +00:00
|
|
|
CLIENT.add_relay("ws://localhost:4736").await;
|
|
|
|
//CLIENT.add_relay("wss://relay.damus.io").await;
|
|
|
|
//CLIENT
|
2024-07-09 14:56:08 +00:00
|
|
|
// .add_relay_with_opts(
|
|
|
|
// "wss://relay.nostr.info",
|
|
|
|
// RelayOptions::new().proxy(proxy).flags(RelayServiceFlags::default().remove(RelayServiceFlags::WRITE)),
|
|
|
|
// )
|
|
|
|
// .await?;
|
2024-07-13 13:00:42 +00:00
|
|
|
//CLIENT
|
2024-07-09 14:56:08 +00:00
|
|
|
// .add_relay_with_opts(
|
|
|
|
// "ws://jgqaglhautb4k6e6i2g34jakxiemqp6z4wynlirltuukgkft2xuglmqd.onion",
|
|
|
|
// RelayOptions::new().proxy(proxy),
|
|
|
|
// )
|
|
|
|
// .await?;
|
|
|
|
|
|
|
|
//let metadata = Metadata::new()
|
|
|
|
// .name("username")
|
|
|
|
// .display_name("My Username")
|
|
|
|
// .about("Description")
|
|
|
|
// .picture(Url::parse("https://example.com/avatar.png")?)
|
|
|
|
// .banner(Url::parse("https://example.com/banner.png")?)
|
|
|
|
// .nip05("username@example.com")
|
|
|
|
// .lud16("yuki@getalby.com")
|
|
|
|
// .custom_field("custom_field", "my value");
|
|
|
|
|
2024-07-13 13:00:42 +00:00
|
|
|
//CLIENT.set_metadata(&metadata).await?;
|
2024-07-09 14:56:08 +00:00
|
|
|
|
2024-07-13 13:00:42 +00:00
|
|
|
CLIENT.connect().await;
|
2024-07-09 14:56:08 +00:00
|
|
|
|
2024-07-11 07:53:58 +00:00
|
|
|
let timeout = Duration::from_secs(3);
|
|
|
|
|
2024-07-13 13:00:42 +00:00
|
|
|
let filter = Filter::new().kind(*TASK_KIND);
|
|
|
|
let sub_id: SubscriptionId = CLIENT.subscribe(vec![filter.clone()], None).await;
|
2024-07-11 07:53:58 +00:00
|
|
|
|
|
|
|
for argument in args().skip(1) {
|
2024-07-13 13:00:42 +00:00
|
|
|
let _ = send(argument, &[]).await;
|
2024-07-11 07:53:58 +00:00
|
|
|
}
|
|
|
|
|
2024-07-13 13:00:42 +00:00
|
|
|
repl().await;
|
|
|
|
|
2024-07-11 07:53:58 +00:00
|
|
|
println!("Finding existing events");
|
2024-07-13 13:00:42 +00:00
|
|
|
let res = CLIENT.get_events_of(vec![filter], Option::from(timeout)).map_ok(|res|
|
2024-07-11 07:53:58 +00:00
|
|
|
for event in res {
|
|
|
|
println!("Found {} {:?}", event.content, event.tags)
|
|
|
|
}).await;
|
2024-07-09 14:56:08 +00:00
|
|
|
|
2024-07-13 13:00:42 +00:00
|
|
|
let mut notifications = CLIENT.notifications();
|
2024-07-11 07:53:58 +00:00
|
|
|
println!("Listening for events...");
|
2024-07-09 14:56:08 +00:00
|
|
|
while let Ok(notification) = notifications.recv().await {
|
|
|
|
if let RelayPoolNotification::Event { subscription_id, event, .. } = notification {
|
|
|
|
let kind = event.kind;
|
|
|
|
let content = &event.content;
|
|
|
|
println!("{kind}: {content}");
|
|
|
|
//break; // Exit
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-07-13 13:00:42 +00:00
|
|
|
|
|
|
|
async fn send(text: String, tags: &[Tag]) -> Result<EventId, Error> {
|
|
|
|
println!("Sending {}", text);
|
|
|
|
let event = EventBuilder::new(*TASK_KIND, text, tags.to_vec()).to_event(&MY_KEYS).unwrap();
|
|
|
|
return CLIENT.send_event(event).await;
|
|
|
|
}
|
|
|
|
|
|
|
|
async fn repl() {
|
|
|
|
loop {
|
|
|
|
print!("> ");
|
|
|
|
stdout().flush().unwrap();
|
|
|
|
match stdin().lines().next() {
|
|
|
|
Some(Ok(input)) => {
|
|
|
|
if input.trim() == "exit" {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if input.trim().is_empty() {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let fut = match input.split_once(": ") {
|
|
|
|
None => {
|
|
|
|
send(input, &[Tag::Name("default".to_string())]).await;
|
|
|
|
}
|
|
|
|
Some(s) => {
|
|
|
|
let tags: Vec<Tag> = s.1.split(" ").map(|t|Tag::Hashtag(t.to_string())).collect();
|
|
|
|
send(s.0.to_string(), &tags).await;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|