| @@ -1,19 +1,23 @@ |
| 1 | use std::collections::HashMap; |
| |
| 2 | |
| |
| 3 | use clap::App as Argparse; |
1 | use clap::App as Argparse; |
| 4 | use clap::AppSettings as ArgParseSettings; |
2 | use clap::AppSettings as ArgParseSettings; |
| 5 | use clap::{Arg, ArgMatches, SubCommand}; |
3 | use clap::{Arg, ArgMatches, SubCommand}; |
| |
4 | use url::Url; |
| 6 | |
5 | |
| 7 | use crate::{Servers, ServersHandle}; |
6 | use crate::config::{Config, ConfigHandle}; |
| |
7 | use crate::{MatrixServer, Servers, ServersHandle}; |
| 8 | use weechat::Weechat; |
8 | use weechat::Weechat; |
| 9 | use weechat::{ArgsWeechat, Buffer, CommandDescription, CommandHook}; |
9 | use weechat::{ArgsWeechat, Buffer, CommandDescription, CommandHook}; |
| 10 | |
10 | |
| 11 | pub struct Commands { |
11 | pub struct Commands { |
| 12 | _matrix: CommandHook<ServersHandle>, |
12 | _matrix: CommandHook<(ServersHandle, ConfigHandle)>, |
| 13 | } |
13 | } |
| 14 | |
14 | |
| 15 | impl Commands { |
15 | impl Commands { |
| 16 | pub fn hook_all(weechat: &Weechat, servers: &Servers) -> Commands { |
16 | pub fn hook_all( |
| |
17 | weechat: &Weechat, |
| |
18 | servers: &Servers, |
| |
19 | config: &Config, |
| |
20 | ) -> Commands { |
| 17 | let matrix_desc = CommandDescription { |
21 | let matrix_desc = CommandDescription { |
| 18 | name: "matrix", |
22 | name: "matrix", |
| 19 | description: "Matrix chat protocol command", |
23 | description: "Matrix chat protocol command", |
| @@ -39,16 +43,45 @@ Use /matrix help [command] to find out more.\n", |
| 39 | let matrix = weechat.hook_command( |
43 | let matrix = weechat.hook_command( |
| 40 | matrix_desc, |
44 | matrix_desc, |
| 41 | Commands::matrix_command_cb, |
45 | Commands::matrix_command_cb, |
| 42 | Some(servers.clone_weak()), |
46 | Some((servers.clone_weak(), config.clone_weak())), |
| 43 | ); |
47 | ); |
| 44 | |
48 | |
| 45 | Commands { _matrix: matrix } |
49 | Commands { _matrix: matrix } |
| 46 | } |
50 | } |
| 47 | |
51 | |
| 48 | fn server_command(buffer: &Buffer, args: &ArgMatches, server: &Servers) { |
52 | fn server_command( |
| |
53 | buffer: &Buffer, |
| |
54 | args: &ArgMatches, |
| |
55 | servers: &Servers, |
| |
56 | config: &ConfigHandle, |
| |
57 | ) { |
| 49 | match args.subcommand() { |
58 | match args.subcommand() { |
| 50 | ("add", Some(subargs)) => { |
59 | ("add", Some(subargs)) => { |
| 51 | buffer.print("Adding server"); |
60 | let server_name = subargs |
| |
61 | .value_of("name") |
| |
62 | .expect("Server name not set but was required"); |
| |
63 | let homeserver = subargs |
| |
64 | .value_of("homeserver") |
| |
65 | .expect("Homeserver not set but was required"); |
| |
66 | let homeserver = Url::parse(homeserver) |
| |
67 | .expect("Can't parse Homeserver even if validation passed"); |
| |
68 | |
| |
69 | let config = config.upgrade(); |
| |
70 | let mut config_borrow = config.borrow_mut(); |
| |
71 | let mut section = config_borrow |
| |
72 | .search_section_mut("server") |
| |
73 | .expect("Can't get server section"); |
| |
74 | |
| |
75 | let server = |
| |
76 | MatrixServer::new(server_name, &config, &mut section); |
| |
77 | |
| |
78 | let mut servers = servers.borrow_mut(); |
| |
79 | servers.insert(server_name.to_owned(), server); |
| |
80 | |
| |
81 | let homeserver_option = section |
| |
82 | .search_option(&format!("{}.homeserver", server_name)) |
| |
83 | .expect("Homeserver option wasn't created"); |
| |
84 | homeserver_option.set(homeserver.as_str(), true); |
| 52 | } |
85 | } |
| 53 | ("delete", Some(subargs)) => { |
86 | ("delete", Some(subargs)) => { |
| 54 | buffer.print("Deleting server"); |
87 | buffer.print("Deleting server"); |
| @@ -58,7 +91,7 @@ Use /matrix help [command] to find out more.\n", |
| 58 | } |
91 | } |
| 59 | |
92 | |
| 60 | fn matrix_command_cb( |
93 | fn matrix_command_cb( |
| 61 | servers: &ServersHandle, |
94 | data: &(ServersHandle, ConfigHandle), |
| 62 | buffer: Buffer, |
95 | buffer: Buffer, |
| 63 | args: ArgsWeechat, |
96 | args: ArgsWeechat, |
| 64 | ) { |
97 | ) { |
| @@ -73,8 +106,8 @@ Use /matrix help [command] to find out more.\n", |
| 73 | ) |
106 | ) |
| 74 | .arg( |
107 | .arg( |
| 75 | Arg::with_name("homeserver") |
108 | Arg::with_name("homeserver") |
| 76 | .value_name("homeserver-address") |
109 | .required(true) |
| 77 | .required(true), |
110 | .validator(MatrixServer::parse_homeserver_url), |
| 78 | ), |
111 | ), |
| 79 | ) |
112 | ) |
| 80 | .subcommand(SubCommand::with_name("delete")); |
113 | .subcommand(SubCommand::with_name("delete")); |
| @@ -96,6 +129,7 @@ Use /matrix help [command] to find out more.\n", |
| 96 | return; |
129 | return; |
| 97 | } |
130 | } |
| 98 | }; |
131 | }; |
| |
132 | let (servers, config) = data; |
| 99 | let servers_ref = servers.upgrade(); |
133 | let servers_ref = servers.upgrade(); |
| 100 | let servers = servers_ref; |
134 | let servers = servers_ref; |
| 101 | |
135 | |
| @@ -118,7 +152,7 @@ Use /matrix help [command] to find out more.\n", |
| 118 | } |
152 | } |
| 119 | } |
153 | } |
| 120 | ("server", Some(subargs)) => { |
154 | ("server", Some(subargs)) => { |
| 121 | Commands::server_command(&buffer, subargs, &servers); |
155 | Commands::server_command(&buffer, subargs, &servers, config); |
| 122 | } |
156 | } |
| 123 | _ => unreachable!(), |
157 | _ => unreachable!(), |
| 124 | } |
158 | } |