| @@ -1,3 +1,7 @@ |
| 1 | mod server; |
| 2 | mod room_buffer; |
| 3 | |
| 4 | use url::Url; |
| 1 | 5 | use std::time::Duration; |
| 2 | 6 | use std::sync::{Arc, Mutex}; |
| 3 | 7 | |
| @@ -8,28 +12,40 @@ use async_task; |
| 8 | 12 | use async_std; |
| 9 | 13 | use async_std::sync::channel as async_channel; |
| 10 | 14 | use async_std::sync::Sender as AsyncSender; |
| 15 | use async_std::sync::Receiver as AsyncReceiver; |
| 11 | 16 | use std::future::Future; |
| 12 | 17 | use std::collections::VecDeque; |
| 18 | use std::collections::HashMap; |
| 19 | |
| 20 | use server::{MatrixServer, ServerMessage}; |
| 13 | 21 | |
| 14 | 22 | use weechat::{weechat_plugin, ArgsWeechat, Weechat, WeechatPlugin, WeechatResult, |
| 15 | | FdHookMode, FdHook}; |
| 23 | FdHookMode, FdHook, WeechatBuffer}; |
| 16 | 24 | |
| 17 | 25 | use matrix_nio::{ |
| 18 | 26 | self, |
| 19 | 27 | events::{ |
| 20 | | collections::all::RoomEvent, |
| 28 | collections::all::{RoomEvent, StateEvent}, |
| 21 | 29 | room::message::{MessageEvent, MessageEventContent, TextMessageEventContent}, |
| 22 | 30 | }, |
| 23 | 31 | AsyncClient, |
| 24 | 32 | SyncSettings, |
| 25 | 33 | AsyncClientConfig, |
| 26 | 34 | }; |
| 35 | use matrix_nio::api::r0::session::login::Response as LoginResponse; |
| 36 | |
| 37 | pub enum ThreadMessage { |
| 38 | LoginMessage(LoginResponse), |
| 39 | SyncState(String, StateEvent), |
| 40 | SyncEvent(String, RoomEvent), |
| 41 | } |
| 42 | |
| 27 | 43 | |
| 28 | | static mut _WEECHAT: Option<Weechat> = None; |
| 44 | const PLUGIN_NAME: &str = "matrix"; |
| 29 | 45 | |
| 30 | | struct SamplePlugin { |
| 31 | | weechat: Weechat, |
| 46 | struct Matrix { |
| 32 | 47 | tokio: Option<Runtime>, |
| 48 | servers: HashMap<String, MatrixServer>, |
| 33 | 49 | } |
| 34 | 50 | |
| 35 | 51 | fn spawn_cb(future_queue: &FutureQueue, receiver: &mut Receiver<()>) { |
| @@ -57,7 +73,7 @@ where |
| 57 | 73 | F: Future<Output = T> + 'static, |
| 58 | 74 | T: 'static |
| 59 | 75 | { |
| 60 | | let weechat = get_weechat(); |
| 76 | let weechat = unsafe { Weechat::weechat() }; |
| 61 | 77 | |
| 62 | 78 | unsafe { |
| 63 | 79 | if _FUTURE_HOOK.is_none() { |
| @@ -106,66 +122,40 @@ where |
| 106 | 122 | } |
| 107 | 123 | |
| 108 | 124 | |
| 109 | | fn get_weechat() -> &'static mut Weechat { |
| 110 | | unsafe { |
| 111 | | match &mut _WEECHAT { |
| 112 | | Some(x) => x, |
| 113 | | None => panic!(), |
| 114 | | } |
| 115 | | } |
| 116 | | } |
| 117 | | |
| 118 | | fn get_plugin() -> &'static mut SamplePlugin { |
| 119 | | unsafe { |
| 120 | | match &mut __PLUGIN { |
| 121 | | Some(x) => x, |
| 122 | | None => panic!(), |
| 123 | | } |
| 124 | | } |
| 125 | | } |
| 126 | | |
| 127 | | async fn sync_loop(channel: AsyncSender<Result<(String, String), String>>) { |
| 128 | | let server = "http://localhost:8008"; |
| 129 | | let config = AsyncClientConfig::new().proxy("http://localhost:8080").unwrap().disable_ssl_verification(); |
| 130 | | let mut client = AsyncClient::new_with_config(server, None, config).unwrap(); |
| 125 | async fn sync_loop(mut client: AsyncClient, channel: AsyncSender<Result<ThreadMessage, String>>) { |
| 131 | 126 | |
| 132 | | channel.send(Err("HELLO WORLD".to_string())).await; |
| 133 | | channel.send(Err("HELLO WORLD".to_string())).await; |
| 127 | let sender_client = client.clone(); |
| 134 | 128 | |
| 135 | 129 | let ret = client.login("example", "wordpass", None).await; |
| 136 | 130 | |
| 137 | 131 | match ret { |
| 138 | | Ok(_) => (), |
| 132 | Ok(response) => channel.send(Ok(ThreadMessage::LoginMessage(response))).await, |
| 139 | 133 | Err(e) => { |
| 140 | 134 | channel.send(Err("No logging in".to_string())).await; |
| 141 | 135 | return; |
| 142 | 136 | }, |
| 143 | 137 | } |
| 144 | 138 | |
| 145 | | channel.send(Err("Syncing now".to_string())).await; |
| 146 | | |
| 147 | 139 | let sync_settings = SyncSettings::new().timeout(30000).unwrap(); |
| 148 | 140 | let response = client.sync(sync_settings).await; |
| 149 | 141 | |
| 150 | | channel.send(Err("Synced now".to_string())).await; |
| 151 | | |
| 152 | 142 | match response { |
| 153 | 143 | Ok(r) => { |
| 154 | 144 | for (room_id, room) in r.rooms.join { |
| 145 | for event in room.state.events { |
| 146 | let event = match event.into_result() { |
| 147 | Ok(e) => e, |
| 148 | Err(e) => continue, |
| 149 | }; |
| 150 | channel.send(Ok((ThreadMessage::SyncState(room_id.to_string(), event)))).await; |
| 151 | } |
| 152 | |
| 155 | 153 | for event in room.timeline.events { |
| 156 | 154 | let event = match event.into_result() { |
| 157 | 155 | Ok(e) => e, |
| 158 | 156 | Err(e) => continue, |
| 159 | 157 | }; |
| 160 | | if let RoomEvent::RoomMessage(MessageEvent { |
| 161 | | content: |
| 162 | | MessageEventContent::Text(TextMessageEventContent { body: msg_body, .. }), |
| 163 | | sender, |
| 164 | | .. |
| 165 | | }) = event |
| 166 | | { |
| 167 | | channel.send(Ok((sender.to_string(), msg_body.to_string()))).await; |
| 168 | | } |
| 158 | channel.send(Ok((ThreadMessage::SyncEvent(room_id.to_string(), event)))).await; |
| 169 | 159 | } |
| 170 | 160 | } |
| 171 | 161 | }, |
| @@ -177,25 +167,30 @@ async fn sync_loop(channel: AsyncSender<Result<(String, String), String>>) { |
| 177 | 167 | } |
| 178 | 168 | |
| 179 | 169 | loop { |
| 180 | | channel.send(Ok(("Hello".to_string(), "world".to_string()))).await; |
| 181 | 170 | async_std::task::sleep(Duration::from_secs(3)).await; |
| 182 | 171 | } |
| 183 | 172 | |
| 184 | 173 | } |
| 185 | 174 | |
| 186 | | impl WeechatPlugin for SamplePlugin { |
| 187 | | fn init(weechat: Weechat, _args: ArgsWeechat) -> WeechatResult<Self> { |
| 188 | | unsafe { |
| 189 | | _WEECHAT = Some(weechat.clone()); |
| 175 | async fn send_loop (mut client: AsyncClient, channel: AsyncReceiver<ServerMessage>) { |
| 176 | while let Some(message) = channel.recv().await { |
| 177 | match message { |
| 178 | ServerMessage::ShutDown => return, |
| 179 | _ => async_std::task::sleep(Duration::from_secs(3)).await, |
| 190 | 180 | } |
| 181 | } |
| 182 | } |
| 191 | 183 | |
| 184 | impl WeechatPlugin for Matrix { |
| 185 | fn init(weechat: &Weechat, _args: ArgsWeechat) -> WeechatResult<Self> { |
| 192 | 186 | let runtime = Runtime::new().unwrap(); |
| 193 | | let (tx, rx) = async_channel(100); |
| 187 | let (tx, rx) = async_channel(1000); |
| 194 | 188 | |
| 195 | 189 | let weechat_task = async move { |
| 196 | | let weechat = get_weechat(); |
| 197 | | weechat.print("Hello async/await"); |
| 198 | | let plugin = get_plugin(); |
| 190 | let weechat = unsafe { Weechat::weechat() }; |
| 191 | let plugin = plugin(); |
| 192 | |
| 193 | let mut server = plugin.servers.get_mut("localhost").unwrap(); |
| 199 | 194 | |
| 200 | 195 | loop { |
| 201 | 196 | let ret = match rx.recv().await { |
| @@ -207,25 +202,44 @@ impl WeechatPlugin for SamplePlugin { |
| 207 | 202 | }; |
| 208 | 203 | |
| 209 | 204 | match ret { |
| 210 | | Ok((sender, msg)) => { |
| 211 | | weechat.print(&format!("Got message from {}: {}", sender, msg)); |
| 205 | Ok(message) => match message { |
| 206 | ThreadMessage::LoginMessage(r) => server.receive_login(r), |
| 207 | ThreadMessage::SyncEvent(r, e) => server.receive_joined_timeline_event(&r, e), |
| 208 | ThreadMessage::SyncState(r, e) => server.receive_joined_state_event(&r, e), |
| 209 | _ => (), |
| 212 | 210 | }, |
| 213 | 211 | Err(e) => weechat.print(&format!("Ruma error {}", e)), |
| 214 | 212 | }; |
| 215 | 213 | } |
| 216 | 214 | }; |
| 215 | let homeserver = Url::parse("http://localhost:8008").unwrap(); |
| 216 | |
| 217 | let config = AsyncClientConfig::new().proxy("http://localhost:8080").unwrap().disable_ssl_verification(); |
| 218 | let client = AsyncClient::new_with_config(homeserver.clone(), None, config).unwrap(); |
| 219 | let send_client = client.clone(); |
| 220 | |
| 221 | runtime.spawn(async move { |
| 222 | sync_loop(client, tx).await; |
| 223 | }); |
| 224 | |
| 225 | let (tx, rx) = async_channel(10); |
| 226 | |
| 227 | let server = MatrixServer::new(&homeserver, tx); |
| 228 | let mut servers = HashMap::new(); |
| 229 | servers.insert("localhost".to_owned(), server); |
| 230 | |
| 217 | 231 | |
| 218 | 232 | runtime.spawn(async move { |
| 219 | | sync_loop(tx).await; |
| 233 | send_loop(send_client, rx).await; |
| 220 | 234 | }); |
| 221 | 235 | |
| 222 | 236 | spawn_weechat(weechat_task); |
| 223 | 237 | |
| 224 | | Ok(SamplePlugin { weechat, tokio: Some(runtime) }) |
| 238 | Ok(Matrix { tokio: Some(runtime), servers }) |
| 225 | 239 | } |
| 226 | 240 | } |
| 227 | 241 | |
| 228 | | impl Drop for SamplePlugin { |
| 242 | impl Drop for Matrix { |
| 229 | 243 | fn drop(&mut self) { |
| 230 | 244 | let runtime = self.tokio.take(); |
| 231 | 245 | |
| @@ -233,7 +247,6 @@ impl Drop for SamplePlugin { |
| 233 | 247 | r.shutdown_now(); |
| 234 | 248 | } |
| 235 | 249 | |
| 236 | | self.weechat.print("Bye rust!"); |
| 237 | 250 | unsafe { |
| 238 | 251 | _FUTURE_HOOK.take(); |
| 239 | 252 | _SENDER.take(); |
| @@ -243,10 +256,10 @@ impl Drop for SamplePlugin { |
| 243 | 256 | } |
| 244 | 257 | |
| 245 | 258 | weechat_plugin!( |
| 246 | | SamplePlugin, |
| 247 | | name: "weechat-matrix", |
| 259 | Matrix, |
| 260 | name: "matrix", |
| 248 | 261 | author: "poljar", |
| 249 | 262 | description: "", |
| 250 | 263 | version: "0.1.0", |
| 251 | | license: "MIT" |
| 264 | license: "ISC" |
| 252 | 265 | ); |