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",
5252 }
5353
5454 fn add_server(args: &ArgMatches, servers: &Servers, config: &ConfigHandle) {
55 let weechat = unsafe { Weechat::weechat() };
56
5755 let server_name = args
5856 .value_of("name")
5957 .expect("Server name not set but was required");
@@ -79,18 +77,16 @@ Use /matrix [command] help to find out more.\n",
7977 .expect("Homeserver option wasn't created");
8078 homeserver_option.set(homeserver.as_str(), true);
8179
82 weechat.print(&format!(
80 Weechat::print(&format!(
8381 "{}: Server {}{}{} has been added.",
8482 PLUGIN_NAME,
85 weechat.color("chat_server"),
83 Weechat::color("chat_server"),
8684 server_name,
87 weechat.color("reset")
85 Weechat::color("reset")
8886 ));
8987 }
9088
9189 fn delete_server(args: &ArgMatches, servers: &Servers) {
92 let weechat = unsafe { Weechat::weechat() };
93
9490 let server_name = args
9591 .value_of("name")
9692 .expect("Server name not set but was required");
@@ -103,24 +99,24 @@ Use /matrix [command] help to find out more.\n",
10399 if let Some(s) = server {
104100 s.connected()
105101 } else {
106 weechat.print(&format!(
102 Weechat::print(&format!(
107103 "{}: No such server {}{}{} found.",
108104 PLUGIN_NAME,
109 weechat.color("chat_server"),
105 Weechat::color("chat_server"),
110106 server_name,
111 weechat.color("reset")
107 Weechat::color("reset")
112108 ));
113109 return;
114110 }
115111 };
116112
117113 if connected {
118 weechat.print(&format!(
114 Weechat::print(&format!(
119115 "{}: Server {}{}{} is still connected.",
120116 PLUGIN_NAME,
121 weechat.color("chat_server"),
117 Weechat::color("chat_server"),
122118 server_name,
123 weechat.color("reset")
119 Weechat::color("reset")
124120 ));
125121 return;
126122 }
@@ -129,29 +125,27 @@ Use /matrix [command] help to find out more.\n",
129125
130126 drop(server);
131127
132 weechat.print(&format!(
128 Weechat::print(&format!(
133129 "{}: Server {}{}{} has been deleted.",
134130 PLUGIN_NAME,
135 weechat.color("chat_server"),
131 Weechat::color("chat_server"),
136132 server_name,
137 weechat.color("reset")
133 Weechat::color("reset")
138134 ));
139135 }
140136
141137 fn list_servers(servers: &Servers) {
142 let weechat = unsafe { Weechat::weechat() };
143
144138 if servers.borrow().is_empty() {
145139 return;
146140 }
147141
148 weechat.print("\nAll Matrix servers:");
142 Weechat::print("\nAll Matrix servers:");
149143
150144 // TODO print out some stats if the server is connected.
151145 for server in servers.borrow().keys() {
152 weechat.print(&format!(
146 Weechat::print(&format!(
153147 " {}{}",
154 weechat.color("chat_server"),
148 Weechat::color("chat_server"),
155149 server
156150 ));
157151 }
@@ -175,21 +169,17 @@ Use /matrix [command] help to find out more.\n",
175169 }
176170
177171 fn server_not_found(server_name: &str) {
178 let weechat = unsafe { Weechat::weechat() };
179
180 weechat.print(&format!(
172 Weechat::print(&format!(
181173 "{}{}: Server \"{}{}{}\" not found.",
182 weechat.prefix("error"),
174 Weechat::prefix("error"),
183175 PLUGIN_NAME,
184 weechat.color("chat_server"),
176 Weechat::color("chat_server"),
185177 server_name,
186 weechat.color("reset")
178 Weechat::color("reset")
187179 ));
188180 }
189181
190182 fn connect_command(args: &ArgMatches, servers: &Servers) {
191 let weechat = unsafe { Weechat::weechat() };
192
193183 let server_names = args
194184 .values_of("name")
195185 .expect("Server names not set but were required");
@@ -201,7 +191,7 @@ Use /matrix [command] help to find out more.\n",
201191 if let Some(s) = server {
202192 match s.connect() {
203193 Ok(_) => (),
204 Err(e) => weechat.print(&format!("{:?}", e)),
194 Err(e) => Weechat::print(&format!("{:?}", e)),
205195 }
206196 } else {
207197 Commands::server_not_found(server_name)
@@ -230,7 +220,6 @@ Use /matrix [command] help to find out more.\n",
230220 _buffer: Buffer,
231221 args: ArgsWeechat,
232222 ) {
233 let weechat = unsafe { Weechat::weechat() };
234223 let server_command = SubCommand::with_name("server")
235224 .about("List, add or delete Matrix servers.")
236225 .subcommand(
@@ -288,7 +277,7 @@ Use /matrix [command] help to find out more.\n",
288277 let matches = match argparse.get_matches_from_safe(args) {
289278 Ok(m) => m,
290279 Err(e) => {
291 weechat.print(&e.to_string());
280 Weechat::print(&e.to_string());
292281 return;
293282 }
294283 };
src/lib.rs +1 −3
@@ -57,13 +57,11 @@ struct Matrix {
5757
5858impl Matrix {
5959 fn autoconnect(servers: &mut HashMap<String, MatrixServer>) {
60 let weechat = unsafe { Weechat::weechat() };
61
6260 for server in servers.values_mut() {
6361 if server.autoconnect() {
6462 match server.connect() {
6563 Ok(_) => (),
66 Err(e) => weechat.print(&format!("{:?}", e)),
64 Err(e) => Weechat::print(&format!("{:?}", e)),
6765 }
6866 }
6967 }
src/server.rs +7 −11
@@ -355,17 +355,15 @@ impl MatrixServer {
355355 }
356356
357357 pub fn disconnect(&self) {
358 let weechat = unsafe { Weechat::weechat() };
359
360358 // TODO these messages should go to the server buffer.
361359 if !self.connected() {
362 weechat.print(&format!(
360 Weechat::print(&format!(
363361 "{}{}: Server {}{}{} is not connected.",
364 weechat.prefix("error"),
362 Weechat::prefix("error"),
365363 PLUGIN_NAME,
366 weechat.color("chat_server"),
364 Weechat::color("chat_server"),
367365 self.name(),
368 weechat.color("reset")
366 Weechat::color("reset")
369367 ));
370368
371369 return;
@@ -379,7 +377,7 @@ impl MatrixServer {
379377 s.response_receiver.cancel();
380378 }
381379
382 weechat.print(&format!("{}: Disconnected from server.", PLUGIN_NAME));
380 Weechat::print(&format!("{}: Disconnected from server.", PLUGIN_NAME));
383381 }
384382
385383 /// Main client sync loop.
@@ -454,13 +452,11 @@ impl MatrixServer {
454452 receiver: Receiver<Result<ThreadMessage, String>>,
455453 server: Weak<RefCell<InnerServer>>,
456454 ) {
457 let weechat = unsafe { Weechat::weechat() };
458
459455 loop {
460456 let ret = match receiver.recv().await {
461457 Some(m) => m,
462458 None => {
463 weechat.print("Error receiving message");
459 Weechat::print("Error receiving message");
464460 return;
465461 }
466462 };
@@ -480,7 +476,7 @@ impl MatrixServer {
480476 server.receive_joined_state_event(&r, e)
481477 }
482478 },
483 Err(e) => weechat.print(&format!("Ruma error {}", e)),
479 Err(e) => Weechat::print(&format!("Ruma error {}", e)),
484480 };
485481 }
486482 }