From 41bcf2dce794019c3e50b447eb7f172ebc150f9b Mon Sep 17 00:00:00 2001 From: Emmanuel Gil Peyrot Date: Sun, 3 Mar 2019 20:13:51 +0100 Subject: [PATCH] examples: Add a caps/ecaps2 generator. --- examples/generate-caps.rs | 62 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 examples/generate-caps.rs diff --git a/examples/generate-caps.rs b/examples/generate-caps.rs new file mode 100644 index 00000000..f47715c1 --- /dev/null +++ b/examples/generate-caps.rs @@ -0,0 +1,62 @@ +// Copyright (c) 2019 Emmanuel Gil Peyrot +// +// This Source Code Form is subject to the terms of the Mozilla Public +// License, v. 2.0. If a copy of the MPL was not distributed with this +// file, You can obtain one at http://mozilla.org/MPL/2.0/. + +use std::io::{self, Read}; +use std::env; +use xmpp_parsers::{ + TryFrom, + Element, + disco::DiscoInfoResult, + caps::{Caps, compute_disco as compute_disco_caps, hash_caps}, + ecaps2::{ECaps2, compute_disco as compute_disco_ecaps2, hash_ecaps2}, + hashes::Algo, +}; + +fn get_caps(disco: &DiscoInfoResult, node: String) -> Result { + let caps_data = compute_disco_caps(&disco); + let caps_hash = hash_caps(&caps_data, Algo::Sha_1)?; + Ok(Caps::new(node, caps_hash)) +} + +fn get_ecaps2(disco: &DiscoInfoResult) -> Result { + let ecaps2_data = compute_disco_ecaps2(&disco).unwrap(); + let ecaps2_sha256 = hash_ecaps2(&ecaps2_data, Algo::Sha_256)?; + let ecaps2_sha3_256 = hash_ecaps2(&ecaps2_data, Algo::Sha3_256)?; + Ok(ECaps2::new(vec![ecaps2_sha256, ecaps2_sha3_256])) +} + +fn main() -> Result<(), ()> { + let args: Vec<_> = env::args().collect(); + if args.len() != 2 { + println!("Usage: {} ", args[0]); + return Err(()); + } + let node = args[1].clone(); + + eprintln!("Reading a disco#info payload from stdin..."); + + // Read from stdin. + let stdin = io::stdin(); + let mut data = String::new(); + let mut handle = stdin.lock(); + handle.read_to_string(&mut data).unwrap(); + + // Parse the payload into a DiscoInfoResult. + let elem: Element = data.parse().unwrap(); + let disco = DiscoInfoResult::try_from(elem).unwrap(); + + // Compute both kinds of caps. + let caps = get_caps(&disco, node).unwrap(); + let ecaps2 = get_ecaps2(&disco).unwrap(); + + // Print them. + let caps_elem = Element::from(caps); + let ecaps2_elem = Element::from(ecaps2); + println!("{}", String::from(&caps_elem)); + println!("{}", String::from(&ecaps2_elem)); + + Ok(()) +}