Commit 3f386bd25f

3f386bd25f3f098cc2b39c082423645eb864b3a1

parent: 2e42388817

Unsigned

Damir Jelić <poljar@termina.org.uk> · 2020-01-26 14:58 UTC

weechat-matrix: print() and friends are now static methods.

Layout: unified · split

src/commands.rs +21 −32
@@ -52,8 +52,6 @@ Use /matrix [command] help to find out more.\n",
52 } 52 }
53 53
54 fn add_server(args: &ArgMatches, servers: &Servers, config: &ConfigHandle) { 54 fn add_server(args: &ArgMatches, servers: &Servers, config: &ConfigHandle) {
55 let weechat = unsafe { Weechat::weechat() };
56
57 let server_name = args 55 let server_name = args
58 .value_of("name") 56 .value_of("name")
59 .expect("Server name not set but was required"); 57 .expect("Server name not set but was required");
@@ -79,18 +77,16 @@ Use /matrix [command] help to find out more.\n",
79 .expect("Homeserver option wasn't created"); 77 .expect("Homeserver option wasn't created");
80 homeserver_option.set(homeserver.as_str(), true); 78 homeserver_option.set(homeserver.as_str(), true);
81 79
82 weechat.print(&format!( 80 Weechat::print(&format!(
83 "{}: Server {}{}{} has been added.", 81 "{}: Server {}{}{} has been added.",
84 PLUGIN_NAME, 82 PLUGIN_NAME,
85 weechat.color("chat_server"), 83 Weechat::color("chat_server"),
86 server_name, 84 server_name,
87 weechat.color("reset") 85 Weechat::color("reset")
88 )); 86 ));
89 } 87 }
90 88
91 fn delete_server(args: &ArgMatches, servers: &Servers) { 89 fn delete_server(args: &ArgMatches, servers: &Servers) {
92 let weechat = unsafe { Weechat::weechat() };
93
94 let server_name = args 90 let server_name = args
95 .value_of("name") 91 .value_of("name")
96 .expect("Server name not set but was required"); 92 .expect("Server name not set but was required");
@@ -103,24 +99,24 @@ Use /matrix [command] help to find out more.\n",
103 if let Some(s) = server { 99 if let Some(s) = server {
104 s.connected() 100 s.connected()
105 } else { 101 } else {
106 weechat.print(&format!( 102 Weechat::print(&format!(
107 "{}: No such server {}{}{} found.", 103 "{}: No such server {}{}{} found.",
108 PLUGIN_NAME, 104 PLUGIN_NAME,
109 weechat.color("chat_server"), 105 Weechat::color("chat_server"),
110 server_name, 106 server_name,
111 weechat.color("reset") 107 Weechat::color("reset")
112 )); 108 ));
113 return; 109 return;
114 } 110 }
115 }; 111 };
116 112
117 if connected { 113 if connected {
118 weechat.print(&format!( 114 Weechat::print(&format!(
119 "{}: Server {}{}{} is still connected.", 115 "{}: Server {}{}{} is still connected.",
120 PLUGIN_NAME, 116 PLUGIN_NAME,
121 weechat.color("chat_server"), 117 Weechat::color("chat_server"),
122 server_name, 118 server_name,
123 weechat.color("reset") 119 Weechat::color("reset")
124 )); 120 ));
125 return; 121 return;
126 } 122 }
@@ -129,29 +125,27 @@ Use /matrix [command] help to find out more.\n",
129 125
130 drop(server); 126 drop(server);
131 127
132 weechat.print(&format!( 128 Weechat::print(&format!(
133 "{}: Server {}{}{} has been deleted.", 129 "{}: Server {}{}{} has been deleted.",
134 PLUGIN_NAME, 130 PLUGIN_NAME,
135 weechat.color("chat_server"), 131 Weechat::color("chat_server"),
136 server_name, 132 server_name,
137 weechat.color("reset") 133 Weechat::color("reset")
138 )); 134 ));
139 } 135 }
140 136
141 fn list_servers(servers: &Servers) { 137 fn list_servers(servers: &Servers) {
142 let weechat = unsafe { Weechat::weechat() };
143
144 if servers.borrow().is_empty() { 138 if servers.borrow().is_empty() {
145 return; 139 return;
146 } 140 }
147 141
148 weechat.print("\nAll Matrix servers:"); 142 Weechat::print("\nAll Matrix servers:");
149 143
150 // TODO print out some stats if the server is connected. 144 // TODO print out some stats if the server is connected.
151 for server in servers.borrow().keys() { 145 for server in servers.borrow().keys() {
152 weechat.print(&format!( 146 Weechat::print(&format!(
153 " {}{}", 147 " {}{}",
154 weechat.color("chat_server"), 148 Weechat::color("chat_server"),
155 server 149 server
156 )); 150 ));
157 } 151 }
@@ -175,21 +169,17 @@ Use /matrix [command] help to find out more.\n",
175 } 169 }
176 170
177 fn server_not_found(server_name: &str) { 171 fn server_not_found(server_name: &str) {
178 let weechat = unsafe { Weechat::weechat() }; 172 Weechat::print(&format!(
179
180 weechat.print(&format!(
181 "{}{}: Server \"{}{}{}\" not found.", 173 "{}{}: Server \"{}{}{}\" not found.",
182 weechat.prefix("error"), 174 Weechat::prefix("error"),
183 PLUGIN_NAME, 175 PLUGIN_NAME,
184 weechat.color("chat_server"), 176 Weechat::color("chat_server"),
185 server_name, 177 server_name,
186 weechat.color("reset") 178 Weechat::color("reset")
187 )); 179 ));
188 } 180 }
189 181
190 fn connect_command(args: &ArgMatches, servers: &Servers) { 182 fn connect_command(args: &ArgMatches, servers: &Servers) {
191 let weechat = unsafe { Weechat::weechat() };
192
193 let server_names = args 183 let server_names = args
194 .values_of("name") 184 .values_of("name")
195 .expect("Server names not set but were required"); 185 .expect("Server names not set but were required");
@@ -201,7 +191,7 @@ Use /matrix [command] help to find out more.\n",
201 if let Some(s) = server { 191 if let Some(s) = server {
202 match s.connect() { 192 match s.connect() {
203 Ok(_) => (), 193 Ok(_) => (),
204 Err(e) => weechat.print(&format!("{:?}", e)), 194 Err(e) => Weechat::print(&format!("{:?}", e)),
205 } 195 }
206 } else { 196 } else {
207 Commands::server_not_found(server_name) 197 Commands::server_not_found(server_name)
@@ -230,7 +220,6 @@ Use /matrix [command] help to find out more.\n",
230 _buffer: Buffer, 220 _buffer: Buffer,
231 args: ArgsWeechat, 221 args: ArgsWeechat,
232 ) { 222 ) {
233 let weechat = unsafe { Weechat::weechat() };
234 let server_command = SubCommand::with_name("server") 223 let server_command = SubCommand::with_name("server")
235 .about("List, add or delete Matrix servers.") 224 .about("List, add or delete Matrix servers.")
236 .subcommand( 225 .subcommand(
@@ -288,7 +277,7 @@ Use /matrix [command] help to find out more.\n",
288 let matches = match argparse.get_matches_from_safe(args) { 277 let matches = match argparse.get_matches_from_safe(args) {
289 Ok(m) => m, 278 Ok(m) => m,
290 Err(e) => { 279 Err(e) => {
291 weechat.print(&e.to_string()); 280 Weechat::print(&e.to_string());
292 return; 281 return;
293 } 282 }
294 }; 283 };
src/lib.rs +1 −3
@@ -57,13 +57,11 @@ struct Matrix {
57 57
58impl Matrix { 58impl Matrix {
59 fn autoconnect(servers: &mut HashMap<String, MatrixServer>) { 59 fn autoconnect(servers: &mut HashMap<String, MatrixServer>) {
60 let weechat = unsafe { Weechat::weechat() };
61
62 for server in servers.values_mut() { 60 for server in servers.values_mut() {
63 if server.autoconnect() { 61 if server.autoconnect() {
64 match server.connect() { 62 match server.connect() {
65 Ok(_) => (), 63 Ok(_) => (),
66 Err(e) => weechat.print(&format!("{:?}", e)), 64 Err(e) => Weechat::print(&format!("{:?}", e)),
67 } 65 }
68 } 66 }
69 } 67 }
src/server.rs +7 −11
@@ -355,17 +355,15 @@ impl MatrixServer {
355 } 355 }
356 356
357 pub fn disconnect(&self) { 357 pub fn disconnect(&self) {
358 let weechat = unsafe { Weechat::weechat() };
359
360 // TODO these messages should go to the server buffer. 358 // TODO these messages should go to the server buffer.
361 if !self.connected() { 359 if !self.connected() {
362 weechat.print(&format!( 360 Weechat::print(&format!(
363 "{}{}: Server {}{}{} is not connected.", 361 "{}{}: Server {}{}{} is not connected.",
364 weechat.prefix("error"), 362 Weechat::prefix("error"),
365 PLUGIN_NAME, 363 PLUGIN_NAME,
366 weechat.color("chat_server"), 364 Weechat::color("chat_server"),
367 self.name(), 365 self.name(),
368 weechat.color("reset") 366 Weechat::color("reset")
369 )); 367 ));
370 368
371 return; 369 return;
@@ -379,7 +377,7 @@ impl MatrixServer {
379 s.response_receiver.cancel(); 377 s.response_receiver.cancel();
380 } 378 }
381 379
382 weechat.print(&format!("{}: Disconnected from server.", PLUGIN_NAME)); 380 Weechat::print(&format!("{}: Disconnected from server.", PLUGIN_NAME));
383 } 381 }
384 382
385 /// Main client sync loop. 383 /// Main client sync loop.
@@ -454,13 +452,11 @@ impl MatrixServer {
454 receiver: Receiver<Result<ThreadMessage, String>>, 452 receiver: Receiver<Result<ThreadMessage, String>>,
455 server: Weak<RefCell<InnerServer>>, 453 server: Weak<RefCell<InnerServer>>,
456 ) { 454 ) {
457 let weechat = unsafe { Weechat::weechat() };
458
459 loop { 455 loop {
460 let ret = match receiver.recv().await { 456 let ret = match receiver.recv().await {
461 Some(m) => m, 457 Some(m) => m,
462 None => { 458 None => {
463 weechat.print("Error receiving message"); 459 Weechat::print("Error receiving message");
464 return; 460 return;
465 } 461 }
466 }; 462 };
@@ -480,7 +476,7 @@ impl MatrixServer {
480 server.receive_joined_state_event(&r, e) 476 server.receive_joined_state_event(&r, e)
481 } 477 }
482 }, 478 },
483 Err(e) => weechat.print(&format!("Ruma error {}", e)), 479 Err(e) => Weechat::print(&format!("Ruma error {}", e)),
484 }; 480 };
485 } 481 }
486 } 482 }