[verified] feat(tui): fail-closed /file transfer (1 MiB)
All checks were successful
ci / test (pull_request) Successful in 3m9s
All checks were successful
ci / test (pull_request) Successful in 3m9s
Typed fil frames chunk a local file to the selected friend over the existing one-shot pipe. Receive-side 1 MiB cap, basename-only names, hash check before rename, never overwrite. Chat shows [file] name only.
This commit is contained in:
parent
c4ccca7e8c
commit
0ea6fd68c4
10 changed files with 641 additions and 8 deletions
|
|
@ -8,6 +8,7 @@ pub enum Kind {
|
|||
Invoice,
|
||||
Receipt,
|
||||
Ping,
|
||||
File,
|
||||
Drop,
|
||||
}
|
||||
|
||||
|
|
@ -27,6 +28,9 @@ pub fn classify(pt: &[u8]) -> Kind {
|
|||
if pt.starts_with(b"png ") {
|
||||
return Kind::Ping;
|
||||
}
|
||||
if pt.starts_with(b"fil ") {
|
||||
return Kind::File;
|
||||
}
|
||||
if pt.len() >= 4
|
||||
&& pt[0].is_ascii_lowercase()
|
||||
&& pt[1].is_ascii_lowercase()
|
||||
|
|
|
|||
387
src/file.rs
Normal file
387
src/file.rs
Normal file
|
|
@ -0,0 +1,387 @@
|
|||
//! Fail-closed file frames. One file = N one-shot `fil ` payloads.
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::fs::{self, OpenOptions};
|
||||
use std::io::{Read, Write};
|
||||
use std::os::unix::fs::{OpenOptionsExt, PermissionsExt};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use rand::RngCore;
|
||||
use sha2::{Digest, Sha256};
|
||||
|
||||
use crate::frame;
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Error(String);
|
||||
|
||||
impl std::fmt::Display for Error {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
self.0.fmt(f)
|
||||
}
|
||||
}
|
||||
|
||||
impl std::error::Error for Error {}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(e: std::io::Error) -> Self {
|
||||
Self(e.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub const MAX_BYTES: usize = 1024 * 1024;
|
||||
const PREFIX: &[u8] = b"fil ";
|
||||
const NOISE_TAG: usize = 16;
|
||||
const NAME_MAX: usize = 128;
|
||||
const XFER_LEN: usize = 16;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct Chunk {
|
||||
pub xfer_id: [u8; XFER_LEN],
|
||||
pub filename: String,
|
||||
pub sha256: [u8; 32],
|
||||
pub idx: u32,
|
||||
pub total: u32,
|
||||
pub data: Vec<u8>,
|
||||
}
|
||||
|
||||
struct Inflight {
|
||||
filename: String,
|
||||
sha256: [u8; 32],
|
||||
total: u32,
|
||||
next: u32,
|
||||
written: usize,
|
||||
}
|
||||
|
||||
pub struct Inbox {
|
||||
root: PathBuf,
|
||||
// ponytail: no timeout janitor. A vanished peer leaves .partial-* until
|
||||
// a later bad chunk for that xfer_id or process exit. Size is still capped.
|
||||
inflight: HashMap<[u8; XFER_LEN], Inflight>,
|
||||
}
|
||||
|
||||
pub fn safe_name(name: &str) -> Result<&str> {
|
||||
if name.is_empty() || name.len() > NAME_MAX {
|
||||
return Err(Error("bad file name".into()));
|
||||
}
|
||||
if name.contains('\0')
|
||||
|| name.contains('/')
|
||||
|| name.contains('\n')
|
||||
|| name == ".."
|
||||
|| name == "."
|
||||
{
|
||||
return Err(Error("bad file name".into()));
|
||||
}
|
||||
if Path::new(name).file_name().and_then(|s| s.to_str()) != Some(name) {
|
||||
return Err(Error("bad file name".into()));
|
||||
}
|
||||
Ok(name)
|
||||
}
|
||||
|
||||
fn safe_fp(fp: &str) -> bool {
|
||||
!fp.is_empty() && fp.len() <= 64 && fp.bytes().all(|b| b.is_ascii_hexdigit())
|
||||
}
|
||||
|
||||
pub fn read_limited(path: &Path) -> Result<(String, Vec<u8>)> {
|
||||
let meta = fs::metadata(path)?;
|
||||
if meta.len() > MAX_BYTES as u64 {
|
||||
return Err(Error("file larger than 1 MiB".into()));
|
||||
}
|
||||
let name = path
|
||||
.file_name()
|
||||
.and_then(|s| s.to_str())
|
||||
.ok_or_else(|| Error("bad file name".into()))?
|
||||
.to_string();
|
||||
safe_name(&name)?;
|
||||
let bytes = fs::read(path)?;
|
||||
if bytes.len() > MAX_BYTES {
|
||||
return Err(Error("file larger than 1 MiB".into()));
|
||||
}
|
||||
Ok((name, bytes))
|
||||
}
|
||||
|
||||
pub fn chunks(filename: &str, bytes: &[u8]) -> Result<Vec<Chunk>> {
|
||||
safe_name(filename)?;
|
||||
if bytes.len() > MAX_BYTES {
|
||||
return Err(Error("file larger than 1 MiB".into()));
|
||||
}
|
||||
let mut hasher = Sha256::new();
|
||||
hasher.update(bytes);
|
||||
let sha256: [u8; 32] = hasher.finalize().into();
|
||||
let mut xfer_id = [0u8; XFER_LEN];
|
||||
rand::rngs::OsRng.fill_bytes(&mut xfer_id);
|
||||
|
||||
let total = total_chunks(filename, bytes.len())?;
|
||||
let cap = data_cap(filename, total);
|
||||
let mut out = Vec::with_capacity(total as usize);
|
||||
for idx in 0..total {
|
||||
let start = (idx as usize).saturating_mul(cap);
|
||||
let end = (start + cap).min(bytes.len());
|
||||
out.push(Chunk {
|
||||
xfer_id,
|
||||
filename: filename.to_string(),
|
||||
sha256,
|
||||
idx,
|
||||
total,
|
||||
data: bytes[start..end].to_vec(),
|
||||
});
|
||||
}
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
pub fn encode(chunk: &Chunk) -> Vec<u8> {
|
||||
let mut out = Vec::from(PREFIX);
|
||||
out.extend_from_slice(to_hex(&chunk.xfer_id).as_bytes());
|
||||
out.push(b'\n');
|
||||
out.extend_from_slice(chunk.filename.as_bytes());
|
||||
out.push(b'\n');
|
||||
out.extend_from_slice(to_hex(&chunk.sha256).as_bytes());
|
||||
out.push(b'\n');
|
||||
out.extend_from_slice(chunk.idx.to_string().as_bytes());
|
||||
out.push(b'/');
|
||||
out.extend_from_slice(chunk.total.to_string().as_bytes());
|
||||
out.push(b'\n');
|
||||
out.extend_from_slice(&chunk.data);
|
||||
out
|
||||
}
|
||||
|
||||
pub fn decode(pt: &[u8]) -> Option<Chunk> {
|
||||
let rest = pt.strip_prefix(PREFIX)?;
|
||||
let mut parts = rest.splitn(5, |&b| b == b'\n');
|
||||
let xfer_hex = std::str::from_utf8(parts.next()?).ok()?;
|
||||
let filename = std::str::from_utf8(parts.next()?).ok()?;
|
||||
let sha_hex = std::str::from_utf8(parts.next()?).ok()?;
|
||||
let idx_total = std::str::from_utf8(parts.next()?).ok()?;
|
||||
let data = parts.next()?.to_vec();
|
||||
safe_name(filename).ok()?;
|
||||
let xfer_id: [u8; XFER_LEN] = from_hex(xfer_hex)?.try_into().ok()?;
|
||||
let sha256: [u8; 32] = from_hex(sha_hex)?.try_into().ok()?;
|
||||
let (idx_s, total_s) = idx_total.split_once('/')?;
|
||||
let idx: u32 = idx_s.parse().ok()?;
|
||||
let total: u32 = total_s.parse().ok()?;
|
||||
if total == 0 || idx >= total {
|
||||
return None;
|
||||
}
|
||||
Some(Chunk {
|
||||
xfer_id,
|
||||
filename: filename.to_string(),
|
||||
sha256,
|
||||
idx,
|
||||
total,
|
||||
data,
|
||||
})
|
||||
}
|
||||
|
||||
pub fn chat_line(name: &str, nbytes: usize) -> String {
|
||||
format!("[file] {name} ({nbytes} bytes)")
|
||||
}
|
||||
|
||||
impl Inbox {
|
||||
pub fn new(root: impl Into<PathBuf>) -> Self {
|
||||
Self {
|
||||
root: root.into(),
|
||||
inflight: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn ingest(&mut self, peer_fp: &str, chunk: &Chunk) -> Result<Option<PathBuf>> {
|
||||
if safe_name(&chunk.filename).is_err() {
|
||||
return Err(Error("bad file name".into()));
|
||||
}
|
||||
if chunk.total == 0 || chunk.idx >= chunk.total {
|
||||
return Err(Error("bad chunk index".into()));
|
||||
}
|
||||
if !safe_fp(peer_fp) {
|
||||
return Err(Error("bad fingerprint".into()));
|
||||
}
|
||||
if chunk.data.len() > MAX_BYTES {
|
||||
return Err(Error("file larger than 1 MiB".into()));
|
||||
}
|
||||
let partial = self.partial_path(&chunk.xfer_id);
|
||||
|
||||
if chunk.idx == 0 {
|
||||
self.drop_partial(&chunk.xfer_id);
|
||||
if let Err(e) = (|| {
|
||||
ensure_dir(&self.root)?;
|
||||
write_partial(&partial, &chunk.data, false)
|
||||
})() {
|
||||
self.drop_partial(&chunk.xfer_id);
|
||||
return Err(e);
|
||||
}
|
||||
self.inflight.insert(
|
||||
chunk.xfer_id,
|
||||
Inflight {
|
||||
filename: chunk.filename.clone(),
|
||||
sha256: chunk.sha256,
|
||||
total: chunk.total,
|
||||
next: 1,
|
||||
written: chunk.data.len(),
|
||||
},
|
||||
);
|
||||
} else {
|
||||
let ok = self.inflight.get(&chunk.xfer_id).is_some_and(|st| {
|
||||
st.filename == chunk.filename
|
||||
&& st.sha256 == chunk.sha256
|
||||
&& st.total == chunk.total
|
||||
&& st.next == chunk.idx
|
||||
});
|
||||
if !ok {
|
||||
self.drop_partial(&chunk.xfer_id);
|
||||
return Err(Error("chunk mismatch".into()));
|
||||
}
|
||||
let next_len = self
|
||||
.inflight
|
||||
.get(&chunk.xfer_id)
|
||||
.map(|st| st.written.saturating_add(chunk.data.len()))
|
||||
.unwrap_or(usize::MAX);
|
||||
if next_len > MAX_BYTES {
|
||||
self.drop_partial(&chunk.xfer_id);
|
||||
return Err(Error("file larger than 1 MiB".into()));
|
||||
}
|
||||
if let Err(e) = write_partial(&partial, &chunk.data, true) {
|
||||
self.drop_partial(&chunk.xfer_id);
|
||||
return Err(e);
|
||||
}
|
||||
if let Some(st) = self.inflight.get_mut(&chunk.xfer_id) {
|
||||
st.next = chunk.idx + 1;
|
||||
st.written = next_len;
|
||||
}
|
||||
}
|
||||
|
||||
if chunk.idx + 1 != chunk.total {
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
let finish = (|| {
|
||||
let hashed = hash_file(&partial)?;
|
||||
if hashed != chunk.sha256 {
|
||||
return Err(Error("hash mismatch".into()));
|
||||
}
|
||||
let dest_dir = self.root.join(peer_fp);
|
||||
ensure_dir(&dest_dir)?;
|
||||
let dest = unique_path(&dest_dir, &chunk.filename)?;
|
||||
fs::rename(&partial, &dest)?;
|
||||
chmod(&dest, 0o600);
|
||||
Ok(dest)
|
||||
})();
|
||||
match finish {
|
||||
Ok(dest) => {
|
||||
self.inflight.remove(&chunk.xfer_id);
|
||||
Ok(Some(dest))
|
||||
}
|
||||
Err(e) => {
|
||||
self.drop_partial(&chunk.xfer_id);
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn partial_path(&self, xfer_id: &[u8; XFER_LEN]) -> PathBuf {
|
||||
self.root.join(format!(".partial-{}", to_hex(xfer_id)))
|
||||
}
|
||||
|
||||
fn drop_partial(&mut self, xfer_id: &[u8; XFER_LEN]) {
|
||||
self.inflight.remove(xfer_id);
|
||||
let _ = fs::remove_file(self.partial_path(xfer_id));
|
||||
}
|
||||
}
|
||||
|
||||
fn total_chunks(filename: &str, len: usize) -> Result<u32> {
|
||||
if len == 0 {
|
||||
return Ok(1);
|
||||
}
|
||||
let mut total = 1u32;
|
||||
loop {
|
||||
let cap = data_cap(filename, total);
|
||||
if cap == 0 {
|
||||
return Err(Error("file name too long for a frame".into()));
|
||||
}
|
||||
let need = u32::try_from(len.div_ceil(cap)).map_err(|_| Error("too many chunks".into()))?;
|
||||
if need <= total {
|
||||
return Ok(need.max(1));
|
||||
}
|
||||
total = need;
|
||||
}
|
||||
}
|
||||
|
||||
fn data_cap(filename: &str, total: u32) -> usize {
|
||||
let digits = total.to_string().len().max(1);
|
||||
let header =
|
||||
PREFIX.len() + XFER_LEN * 2 + 1 + filename.len() + 1 + 64 + 1 + digits + 1 + digits + 1;
|
||||
frame::MAX_FRAME
|
||||
.saturating_sub(NOISE_TAG)
|
||||
.saturating_sub(header)
|
||||
}
|
||||
|
||||
fn write_partial(path: &Path, data: &[u8], append: bool) -> Result<()> {
|
||||
let mut opts = OpenOptions::new();
|
||||
opts.write(true).mode(0o600);
|
||||
if append {
|
||||
opts.append(true);
|
||||
} else {
|
||||
opts.create(true).truncate(true);
|
||||
}
|
||||
let mut f = opts.open(path)?;
|
||||
f.write_all(data)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn hash_file(path: &Path) -> Result<[u8; 32]> {
|
||||
let mut f = fs::File::open(path)?;
|
||||
let mut hasher = Sha256::new();
|
||||
let mut buf = [0u8; 8192];
|
||||
loop {
|
||||
let n = f.read(&mut buf)?;
|
||||
if n == 0 {
|
||||
break;
|
||||
}
|
||||
hasher.update(&buf[..n]);
|
||||
}
|
||||
Ok(hasher.finalize().into())
|
||||
}
|
||||
|
||||
fn unique_path(dir: &Path, name: &str) -> Result<PathBuf> {
|
||||
let first = dir.join(name);
|
||||
if !first.exists() {
|
||||
return Ok(first);
|
||||
}
|
||||
for n in 2..1000 {
|
||||
let p = dir.join(format!("{name}-{n}"));
|
||||
if !p.exists() {
|
||||
return Ok(p);
|
||||
}
|
||||
}
|
||||
Err(Error("name collision".into()))
|
||||
}
|
||||
|
||||
fn ensure_dir(path: &Path) -> Result<()> {
|
||||
fs::create_dir_all(path)?;
|
||||
chmod(path, 0o700);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn chmod(path: &Path, mode: u32) {
|
||||
if let Ok(meta) = fs::metadata(path) {
|
||||
let mut p = meta.permissions();
|
||||
p.set_mode(mode);
|
||||
let _ = fs::set_permissions(path, p);
|
||||
}
|
||||
}
|
||||
|
||||
fn to_hex(bytes: &[u8]) -> String {
|
||||
bytes.iter().map(|b| format!("{b:02x}")).collect()
|
||||
}
|
||||
|
||||
fn from_hex(s: &str) -> Option<Vec<u8>> {
|
||||
if s.is_empty() || !s.len().is_multiple_of(2) {
|
||||
return None;
|
||||
}
|
||||
if !s.bytes().all(|c| c.is_ascii_hexdigit()) {
|
||||
return None;
|
||||
}
|
||||
(0..s.len())
|
||||
.step_by(2)
|
||||
.map(|i| u8::from_str_radix(&s[i..i + 2], 16).ok())
|
||||
.collect()
|
||||
}
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
pub mod backup;
|
||||
pub mod dispatch;
|
||||
pub mod file;
|
||||
pub mod frame;
|
||||
pub mod hs;
|
||||
pub mod loc;
|
||||
|
|
|
|||
54
src/node.rs
54
src/node.rs
|
|
@ -10,6 +10,7 @@ use tor_cell::relaycell::msg::Connected;
|
|||
use tor_hsservice::{RunningOnionService, handle_rend_requests};
|
||||
|
||||
use crate::dispatch::{self, Kind};
|
||||
use crate::file;
|
||||
use crate::frame;
|
||||
use crate::hs::{self, Client, HS_PORT};
|
||||
use crate::loc;
|
||||
|
|
@ -46,6 +47,7 @@ pub struct Node {
|
|||
keys: Mutex<Keys>,
|
||||
wallet: Wallet,
|
||||
incoming_limit: Mutex<TokenBucket>,
|
||||
inbox: Mutex<file::Inbox>,
|
||||
}
|
||||
|
||||
impl Node {
|
||||
|
|
@ -75,6 +77,7 @@ impl Node {
|
|||
.ok_or_else(|| "onion service disabled in config — fail closed".to_string())?;
|
||||
let (svc, rend) = launched;
|
||||
let onion = hs::onion_string(&svc)?;
|
||||
let inbox = file::Inbox::new(home.join("inbox"));
|
||||
let node = Arc::new(Self {
|
||||
home,
|
||||
store: Mutex::new(store),
|
||||
|
|
@ -84,6 +87,7 @@ impl Node {
|
|||
keys: Mutex::new(keys),
|
||||
wallet: Wallet::from_env(),
|
||||
incoming_limit: Mutex::new(TokenBucket::default()),
|
||||
inbox: Mutex::new(inbox),
|
||||
});
|
||||
// Accept rens before waiting so a reachability probe can succeed
|
||||
// while combined status is still Bootstrapping.
|
||||
|
|
@ -481,6 +485,25 @@ impl Node {
|
|||
})
|
||||
}
|
||||
|
||||
/// One-shot file to a friend. Fail closed; no outbox, no resume.
|
||||
pub async fn send_file(&self, friend_pk: &[u8], path: &Path) -> Result<String, String> {
|
||||
let (name, bytes) = file::read_limited(path).map_err(|e| e.to_string())?;
|
||||
let chunks = file::chunks(&name, &bytes).map_err(|e| e.to_string())?;
|
||||
for chunk in &chunks {
|
||||
self.send_once(friend_pk, &file::encode(chunk)).await?;
|
||||
}
|
||||
self.store
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?
|
||||
.append_message(
|
||||
friend_pk,
|
||||
"out",
|
||||
file::chat_line(&name, bytes.len()).as_bytes(),
|
||||
)
|
||||
.map_err(|e| e.to_string())?;
|
||||
Ok(name)
|
||||
}
|
||||
|
||||
pub async fn send(&self, friend_pk: &[u8], plaintext: &[u8]) -> Result<(), String> {
|
||||
let (onion, prekey) = {
|
||||
let store = self.store.lock().map_err(|e| e.to_string())?;
|
||||
|
|
@ -620,10 +643,41 @@ impl Node {
|
|||
}
|
||||
Kind::Invoice => self.ingest_invoice(&sess.peer_identity, &pt).await,
|
||||
Kind::Receipt => self.ingest_receipt(&sess.peer_identity, &pt).await,
|
||||
Kind::File => self.ingest_file(&sess.peer_identity, &pt),
|
||||
Kind::Ping | Kind::Drop => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
fn ingest_file(&self, peer: &[u8], pt: &[u8]) -> Result<(), String> {
|
||||
let Some(chunk) = file::decode(pt) else {
|
||||
return Ok(());
|
||||
};
|
||||
let fp: String = peer.iter().map(|b| format!("{b:02x}")).collect();
|
||||
let done = {
|
||||
let mut inbox = self.inbox.lock().map_err(|e| e.to_string())?;
|
||||
match inbox.ingest(&fp, &chunk) {
|
||||
Ok(p) => p,
|
||||
Err(e) => {
|
||||
eprintln!("fil dropped ({e})");
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
};
|
||||
if let Some(path) = done {
|
||||
let nbytes = std::fs::metadata(&path).map(|m| m.len()).unwrap_or(0) as usize;
|
||||
let name = path
|
||||
.file_name()
|
||||
.and_then(|s| s.to_str())
|
||||
.unwrap_or(&chunk.filename);
|
||||
self.store
|
||||
.lock()
|
||||
.map_err(|e| e.to_string())?
|
||||
.append_message(peer, "in", file::chat_line(name, nbytes).as_bytes())
|
||||
.map_err(|e| e.to_string())?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn ingest_invoice(&self, peer: &[u8], pt: &[u8]) -> Result<(), String> {
|
||||
let Some(inv) = pay::decode_invoice(pt) else {
|
||||
return Ok(());
|
||||
|
|
|
|||
34
src/tui.rs
34
src/tui.rs
|
|
@ -77,6 +77,7 @@ OnionWire keys\n\
|
|||
/profile edit name, bio, Monero address\n\
|
||||
/pay <xmr> [memo] invoice to receive\n\
|
||||
/tip <xmr> [memo] pay selected friend\n\
|
||||
/file /path send file to selected friend (1 MiB)\n\
|
||||
/backup /path encrypted identity export\n\
|
||||
/restore /path overwrite self keys\n\
|
||||
? this help\n\
|
||||
|
|
@ -163,6 +164,7 @@ pub enum SlashCmd {
|
|||
Tip { atomic: String, memo: String },
|
||||
Backup { path: String },
|
||||
Restore { path: String },
|
||||
File { path: String },
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
|
|
@ -306,6 +308,11 @@ pub fn parse_cmd(raw: &str) -> Option<SlashCmd> {
|
|||
{
|
||||
return parse_path_cmd(rest).map(|path| SlashCmd::Restore { path });
|
||||
}
|
||||
if let Some(rest) = s.strip_prefix("/file")
|
||||
&& (rest.is_empty() || rest.starts_with(char::is_whitespace))
|
||||
{
|
||||
return parse_path_cmd(rest).map(|path| SlashCmd::File { path });
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
|
|
@ -715,6 +722,7 @@ impl App {
|
|||
let mut open_profile = false;
|
||||
let mut pay_cmd = None;
|
||||
let mut tip_cmd = None;
|
||||
let mut file_cmd = None;
|
||||
let mut chat_send = None;
|
||||
match &mut self.screen {
|
||||
Screen::Main => match key.code {
|
||||
|
|
@ -782,6 +790,10 @@ impl App {
|
|||
prompt: BackupPrompt::restore(),
|
||||
};
|
||||
}
|
||||
Some(ComposerAction::Cmd(SlashCmd::File { path })) => {
|
||||
self.composer.clear();
|
||||
file_cmd = Some(path);
|
||||
}
|
||||
Some(ComposerAction::Send(text)) => {
|
||||
self.composer.clear();
|
||||
chat_send = Some(text);
|
||||
|
|
@ -943,6 +955,9 @@ impl App {
|
|||
if let Some((atomic, memo)) = tip_cmd {
|
||||
self.send_tip(&atomic, &memo)?;
|
||||
}
|
||||
if let Some(path) = file_cmd {
|
||||
self.send_file(&path)?;
|
||||
}
|
||||
if let Some(text) = chat_send {
|
||||
self.send_chat(&text)?;
|
||||
}
|
||||
|
|
@ -1197,6 +1212,23 @@ impl App {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn send_file(&mut self, path: &str) -> Result<(), String> {
|
||||
let Some(friend) = self.friends.get(self.selected) else {
|
||||
self.status_note = Some("no friend selected".into());
|
||||
return Ok(());
|
||||
};
|
||||
let pk = friend.pubkey.clone();
|
||||
self.status_note = Some("sending".into());
|
||||
match self
|
||||
.rt
|
||||
.block_on(self.node.send_file(&pk, std::path::Path::new(path)))
|
||||
{
|
||||
Ok(name) => self.status_note = Some(format!("sent file {name}")),
|
||||
Err(e) => self.status_note = Some(format!("send failed: {e}")),
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn send_chat(&mut self, text: &str) -> Result<(), String> {
|
||||
let Some(friend) = self.friends.get(self.selected) else {
|
||||
self.status_note = Some("no friend selected".into());
|
||||
|
|
@ -1341,7 +1373,7 @@ impl App {
|
|||
f.render_widget(chat, panes[1]);
|
||||
|
||||
let cmd = if self.composer.is_empty() {
|
||||
"/wipe /wipe-all /profile /who /pay /tip".to_string()
|
||||
"/wipe /wipe-all /profile /who /pay /tip /file".to_string()
|
||||
} else {
|
||||
self.composer.clone()
|
||||
};
|
||||
|
|
|
|||
Loading…
Reference in a new issue