src/commands/upload.rs
58 lines · 1423 bytes
3 symbols in this file
1use std::{borrow::Cow, path::PathBuf};
2
3use weechat::{
4 buffer::Buffer,
5 hooks::{CommandRun, CommandRunCallback},
6 ReturnCode, Weechat,
7};
8
9use crate::{room::thread_root_from_buffer, Servers};
10
11pub struct UploadCommand {
12 servers: Servers,
13}
14
15impl UploadCommand {
16 pub fn create(servers: &Servers) -> Result<CommandRun, ()> {
17 CommandRun::new(
18 "/upload",
19 UploadCommand {
20 servers: servers.clone(),
21 },
22 )
23 }
24}
25
26impl CommandRunCallback for UploadCommand {
27 fn callback(
28 &mut self,
29 _: &Weechat,
30 buffer: &Buffer,
31 cmd: Cow<str>,
32 ) -> ReturnCode {
33 let Some(room) = self.servers.find_room(buffer) else {
34 Weechat::print(
35 "The upload command needs to be executed in a room buffer",
36 );
37 return ReturnCode::Ok;
38 };
39
40 let Some(path) = cmd.strip_prefix("/upload").map(str::trim) else {
41 return ReturnCode::Ok;
42 };
43
44 if path.is_empty() {
45 Weechat::print("Usage: /upload <file>");
46 return ReturnCode::Ok;
47 }
48
49 let path = PathBuf::from(Weechat::expand_home(path));
50 let thread_root = thread_root_from_buffer(buffer);
51 Weechat::spawn(
52 async move { room.send_attachment(path, thread_root).await },
53 )
54 .detach();
55
56 ReturnCode::Ok
57 }
58}