| @@ -98,11 +98,19 @@ impl Verification { |
| 98 | |
98 | |
| 99 | #[derive(Clone)] |
99 | #[derive(Clone)] |
| 100 | struct InnerVerificationBuffer { |
100 | struct InnerVerificationBuffer { |
| 101 | verification: Rc<RefCell<Verification>>, |
101 | verification: Rc<RefCell<Option<Verification>>>, |
| 102 | connection: Rc<RefCell<Option<Connection>>>, |
102 | connection: Rc<RefCell<Option<Connection>>>, |
| 103 | } |
103 | } |
| 104 | |
104 | |
| 105 | impl InnerVerificationBuffer { |
105 | impl InnerVerificationBuffer { |
| |
106 | fn verification(&self) -> Option<Verification> { |
| |
107 | self.verification.borrow().clone() |
| |
108 | } |
| |
109 | |
| |
110 | fn release_sdk_state(&self) { |
| |
111 | self.verification.borrow_mut().take(); |
| |
112 | } |
| |
113 | |
| 106 | fn print_message(buffer: BufferHandle, message: &str) { |
114 | fn print_message(buffer: BufferHandle, message: &str) { |
| 107 | if let Ok(buffer) = buffer.upgrade() { |
115 | if let Ok(buffer) = buffer.upgrade() { |
| 108 | buffer.print(message); |
116 | buffer.print(message); |
| @@ -115,7 +123,9 @@ impl InnerVerificationBuffer { |
| 115 | |
123 | |
| 116 | pub async fn accept(&self, buffer: BufferHandle) -> Result<(), Error> { |
124 | pub async fn accept(&self, buffer: BufferHandle) -> Result<(), Error> { |
| 117 | if let Some(c) = self.connection.borrow().clone() { |
125 | if let Some(c) = self.connection.borrow().clone() { |
| 118 | let verification = self.verification.borrow().clone(); |
126 | let Some(verification) = self.verification() else { |
| |
127 | return Ok(()); |
| |
128 | }; |
| 119 | let verification_clone = verification.clone(); |
129 | let verification_clone = verification.clone(); |
| 120 | |
130 | |
| 121 | c.spawn(async move { verification_clone.accept().await }) |
131 | c.spawn(async move { verification_clone.accept().await }) |
| @@ -142,7 +152,7 @@ impl InnerVerificationBuffer { |
| 142 | } else if let Some(sas) = |
152 | } else if let Some(sas) = |
| 143 | c.spawn(async move { request.start_sas().await }).await? |
153 | c.spawn(async move { request.start_sas().await }).await? |
| 144 | { |
154 | { |
| 145 | *self.verification.borrow_mut() = sas.into(); |
155 | *self.verification.borrow_mut() = Some(sas.into()); |
| 146 | } |
156 | } |
| 147 | } |
157 | } |
| 148 | } |
158 | } |
| @@ -152,16 +162,14 @@ impl InnerVerificationBuffer { |
| 152 | |
162 | |
| 153 | pub async fn confirm(&self, buffer: BufferHandle) -> Result<(), Error> { |
163 | pub async fn confirm(&self, buffer: BufferHandle) -> Result<(), Error> { |
| 154 | if let Some(c) = self.connection.borrow().clone() { |
164 | if let Some(c) = self.connection.borrow().clone() { |
| 155 | if let Verification::Sas(s) = self.verification.borrow().clone() { |
165 | if let Some(Verification::Sas(s)) = self.verification() { |
| 156 | let sas = s.clone(); |
166 | let sas = s.clone(); |
| 157 | c.spawn(async move { s.confirm().await }).await?; |
167 | c.spawn(async move { s.confirm().await }).await?; |
| 158 | |
168 | |
| 159 | if sas.is_done() { |
169 | if sas.is_done() { |
| 160 | self.print_done(buffer); |
170 | self.print_done(buffer); |
| 161 | } |
171 | } |
| 162 | } else if let Verification::Qr(qr) = |
172 | } else if let Some(Verification::Qr(qr)) = self.verification() { |
| 163 | self.verification.borrow().clone() |
| |
| 164 | { |
| |
| 165 | c.spawn(async move { qr.confirm().await }).await?; |
173 | c.spawn(async move { qr.confirm().await }).await?; |
| 166 | |
174 | |
| 167 | // if qr.is_done() { |
175 | // if qr.is_done() { |
| @@ -177,7 +185,10 @@ impl InnerVerificationBuffer { |
| 177 | |
185 | |
| 178 | pub async fn cancel(&self) -> Result<(), Error> { |
186 | pub async fn cancel(&self) -> Result<(), Error> { |
| 179 | if let Some(c) = self.connection.borrow().clone() { |
187 | if let Some(c) = self.connection.borrow().clone() { |
| 180 | let verification = self.verification.borrow().clone(); |
188 | let Some(verification) = self.verification() else { |
| |
189 | return Ok(()); |
| |
190 | }; |
| |
191 | |
| 181 | c.spawn(async move { verification.cancel().await }).await?; |
192 | c.spawn(async move { verification.cancel().await }).await?; |
| 182 | } |
193 | } |
| 183 | |
194 | |
| @@ -232,7 +243,7 @@ impl VerificationBuffer { |
| 232 | connection: Rc<RefCell<Option<Connection>>>, |
243 | connection: Rc<RefCell<Option<Connection>>>, |
| 233 | ) -> Self { |
244 | ) -> Self { |
| 234 | let inner = InnerVerificationBuffer { |
245 | let inner = InnerVerificationBuffer { |
| 235 | verification: Rc::new(RefCell::new(verification.into())), |
246 | verification: Rc::new(RefCell::new(Some(verification.into()))), |
| 236 | connection, |
247 | connection, |
| 237 | }; |
248 | }; |
| 238 | |
249 | |
| @@ -268,6 +279,10 @@ impl VerificationBuffer { |
| 268 | self.buffer.clone() |
279 | self.buffer.clone() |
| 269 | } |
280 | } |
| 270 | |
281 | |
| |
282 | pub fn release_sdk_state(&self) { |
| |
283 | self.inner.release_sdk_state(); |
| |
284 | } |
| |
285 | |
| 271 | pub fn accept(&self) { |
286 | pub fn accept(&self) { |
| 272 | let buffer = self.buffer(); |
287 | let buffer = self.buffer(); |
| 273 | let inner = self.inner.clone(); |
288 | let inner = self.inner.clone(); |
| @@ -286,12 +301,14 @@ impl VerificationBuffer { |
| 286 | } |
301 | } |
| 287 | |
302 | |
| 288 | pub async fn update_qr(&mut self, qr: QrVerification) { |
303 | pub async fn update_qr(&mut self, qr: QrVerification) { |
| 289 | *self.inner.verification.borrow_mut() = qr.into(); |
304 | *self.inner.verification.borrow_mut() = Some(qr.into()); |
| 290 | } |
305 | } |
| 291 | |
306 | |
| 292 | pub async fn update(&mut self, sas: SasVerification) -> Result<(), Error> { |
307 | pub async fn update(&mut self, sas: SasVerification) -> Result<(), Error> { |
| 293 | *self.inner.verification.borrow_mut() = sas.into(); |
308 | *self.inner.verification.borrow_mut() = Some(sas.into()); |
| 294 | let verification = self.inner.verification.borrow().clone(); |
309 | let Some(verification) = self.inner.verification() else { |
| |
310 | return Ok(()); |
| |
311 | }; |
| 295 | |
312 | |
| 296 | if let Some(c) = self.inner.connection.borrow().clone() { |
313 | if let Some(c) = self.inner.connection.borrow().clone() { |
| 297 | c.spawn(async move { verification.accept().await }).await?; |
314 | c.spawn(async move { verification.accept().await }).await?; |
| @@ -305,8 +322,8 @@ impl VerificationBuffer { |
| 305 | pub async fn handle_event(&self, event: &AnyToDeviceEvent) { |
322 | pub async fn handle_event(&self, event: &AnyToDeviceEvent) { |
| 306 | match event { |
323 | match event { |
| 307 | AnyToDeviceEvent::KeyVerificationRequest(e) => { |
324 | AnyToDeviceEvent::KeyVerificationRequest(e) => { |
| 308 | if let Verification::Request(_) = |
325 | if let Some(Verification::Request(_)) = |
| 309 | self.inner.verification.borrow().clone() |
326 | self.inner.verification() |
| 310 | { |
327 | { |
| 311 | let content = |
328 | let content = |
| 312 | e.content.render(&VerificationContext::ToDevice); |
329 | e.content.render(&VerificationContext::ToDevice); |
| @@ -315,7 +332,9 @@ impl VerificationBuffer { |
| 315 | } |
332 | } |
| 316 | } |
333 | } |
| 317 | AnyToDeviceEvent::KeyVerificationStart(e) => { |
334 | AnyToDeviceEvent::KeyVerificationStart(e) => { |
| 318 | let verification = self.inner.verification.borrow().clone(); |
335 | let Some(verification) = self.inner.verification() else { |
| |
336 | return; |
| |
337 | }; |
| 319 | |
338 | |
| 320 | if let Ok(verification) = verification.try_into() { |
339 | if let Ok(verification) = verification.try_into() { |
| 321 | let content = |
340 | let content = |
| @@ -337,8 +356,7 @@ impl VerificationBuffer { |
| 337 | self.print_sas(&e.content); |
356 | self.print_sas(&e.content); |
| 338 | } |
357 | } |
| 339 | AnyToDeviceEvent::KeyVerificationMac(_) => { |
358 | AnyToDeviceEvent::KeyVerificationMac(_) => { |
| 340 | if let Verification::Sas(sas) = |
359 | if let Some(Verification::Sas(sas)) = self.inner.verification() |
| 341 | self.inner.verification.borrow().clone() |
| |
| 342 | { |
360 | { |
| 343 | if sas.is_done() { |
361 | if sas.is_done() { |
| 344 | self.inner.print_done(self.buffer.clone()); |
362 | self.inner.print_done(self.buffer.clone()); |
| @@ -363,8 +381,7 @@ impl VerificationBuffer { |
| 363 | } |
381 | } |
| 364 | |
382 | |
| 365 | fn print_sas(&self, content: &ToDeviceKeyVerificationKeyEventContent) { |
383 | fn print_sas(&self, content: &ToDeviceKeyVerificationKeyEventContent) { |
| 366 | if let Verification::Sas(sas) = self.inner.verification.borrow().clone() |
384 | if let Some(Verification::Sas(sas)) = self.inner.verification() { |
| 367 | { |
| |
| 368 | let message = content.render(&sas); |
385 | let message = content.render(&sas); |
| 369 | self.print(&message); |
386 | self.print(&message); |
| 370 | } |
387 | } |