Commit cf277acd77

cf277acd775f168cf3ede332d505a77ac8b608f2

parent: c1cd294aab

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-01-12 13:38 UTC

commands: Implement the disconnect command.

Layout: unified · split

src/commands.rs +31 −5
@@ -216,11 +216,22 @@ Use /matrix [command] help to find out more.\n",
216 .subcommand(server_command) 216 .subcommand(server_command)
217 .subcommand( 217 .subcommand(
218 SubCommand::with_name("connect") 218 SubCommand::with_name("connect")
219 .about("Connect to Matrix servers."), 219 .about("Connect to Matrix servers.")
220 .arg(
221 Arg::with_name("name")
222 .value_name("server-name")
223 .required(true)
224 .multiple(true),
225 ),
220 ) 226 )
221 .subcommand( 227 .subcommand(
222 SubCommand::with_name("disconnect") 228 SubCommand::with_name("disconnect")
223 .about("Disconnect from one or all Matrix servers"), 229 .about("Disconnect from one or all Matrix servers")
230 .arg(
231 Arg::with_name("name")
232 .value_name("server-name")
233 .required(true),
234 ),
224 ); 235 );
225 236
226 let matches = match argparse.get_matches_from_safe(args) { 237 let matches = match argparse.get_matches_from_safe(args) {
@@ -246,10 +257,25 @@ Use /matrix [command] help to find out more.\n",
246 } 257 }
247 } 258 }
248 ("disconnect", Some(subargs)) => { 259 ("disconnect", Some(subargs)) => {
249 weechat.print("Disconnecting");
250 let mut servers = servers.borrow_mut(); 260 let mut servers = servers.borrow_mut();
251 for server in servers.values_mut() { 261
252 server.disconnect(); 262 let server_name = subargs
263 .value_of("name")
264 .expect("Server name not set but was required");
265
266 let server = servers.get_mut(server_name);
267
268 if let Some(s) = server {
269 s.disconnect();
270 } else {
271 weechat.print(&format!(
272 "{}{}: Server \"{}{}{}\" not found.",
273 weechat.prefix("error"),
274 PLUGIN_NAME,
275 weechat.color("chat_server"),
276 server_name,
277 weechat.color("reset")
278 ));
253 } 279 }
254 } 280 }
255 ("server", Some(subargs)) => { 281 ("server", Some(subargs)) => {
src/server.rs +9
@@ -27,6 +27,7 @@ use weechat::Weechat;
27use crate::executor::spawn_weechat; 27use crate::executor::spawn_weechat;
28use crate::room_buffer::RoomBuffer; 28use crate::room_buffer::RoomBuffer;
29use crate::Config; 29use crate::Config;
30use crate::PLUGIN_NAME;
30 31
31const DEFAULT_SYNC_TIMEOUT: i32 = 30000; 32const DEFAULT_SYNC_TIMEOUT: i32 = 30000;
32 33
@@ -280,6 +281,10 @@ impl InnerServer {
280 281
281impl MatrixServer { 282impl MatrixServer {
282 pub fn connect(&self) -> Result<(), ServerError> { 283 pub fn connect(&self) -> Result<(), ServerError> {
284 if self.connected() {
285 return Ok(());
286 }
287
283 let runtime = Runtime::new().unwrap(); 288 let runtime = Runtime::new().unwrap();
284 let mut server = self.inner.borrow_mut(); 289 let mut server = self.inner.borrow_mut();
285 290
@@ -323,6 +328,8 @@ impl MatrixServer {
323 } 328 }
324 329
325 pub fn disconnect(&self) { 330 pub fn disconnect(&self) {
331 let weechat = unsafe { Weechat::weechat() };
332
326 let server = self.inner.borrow_mut(); 333 let server = self.inner.borrow_mut();
327 let mut connected_state = server.connected_state.borrow_mut(); 334 let mut connected_state = server.connected_state.borrow_mut();
328 let state = connected_state.take(); 335 let state = connected_state.take();
@@ -330,6 +337,8 @@ impl MatrixServer {
330 if let Some(s) = state { 337 if let Some(s) = state {
331 s.response_receiver.cancel(); 338 s.response_receiver.cancel();
332 } 339 }
340
341 weechat.print(&format!("{}: Disconnected from server.", PLUGIN_NAME));
333 } 342 }
334 343
335 /// Main client sync loop. 344 /// Main client sync loop.