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",
216216 .subcommand(server_command)
217217 .subcommand(
218218 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 ),
220226 )
221227 .subcommand(
222228 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 ),
224235 );
225236
226237 let matches = match argparse.get_matches_from_safe(args) {
@@ -246,10 +257,25 @@ Use /matrix [command] help to find out more.\n",
246257 }
247258 }
248259 ("disconnect", Some(subargs)) => {
249 weechat.print("Disconnecting");
250260 let mut servers = servers.borrow_mut();
251 for server in servers.values_mut() {
252 server.disconnect();
261
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 ));
253279 }
254280 }
255281 ("server", Some(subargs)) => {
src/server.rs +9
@@ -27,6 +27,7 @@ use weechat::Weechat;
2727use crate::executor::spawn_weechat;
2828use crate::room_buffer::RoomBuffer;
2929use crate::Config;
30use crate::PLUGIN_NAME;
3031
3132const DEFAULT_SYNC_TIMEOUT: i32 = 30000;
3233
@@ -280,6 +281,10 @@ impl InnerServer {
280281
281282impl MatrixServer {
282283 pub fn connect(&self) -> Result<(), ServerError> {
284 if self.connected() {
285 return Ok(());
286 }
287
283288 let runtime = Runtime::new().unwrap();
284289 let mut server = self.inner.borrow_mut();
285290
@@ -323,6 +328,8 @@ impl MatrixServer {
323328 }
324329
325330 pub fn disconnect(&self) {
331 let weechat = unsafe { Weechat::weechat() };
332
326333 let server = self.inner.borrow_mut();
327334 let mut connected_state = server.connected_state.borrow_mut();
328335 let state = connected_state.take();
@@ -330,6 +337,8 @@ impl MatrixServer {
330337 if let Some(s) = state {
331338 s.response_receiver.cancel();
332339 }
340
341 weechat.print(&format!("{}: Disconnected from server.", PLUGIN_NAME));
333342 }
334343
335344 /// Main client sync loop.