misc

Misc scripts and tools
git clone git://git.alexwennerberg.com/misc
Log | Files | Refs | README | LICENSE

commit ce52cb4daefcf5c4a95c9540e1b7684f6d271512
parent 9ab4dfe725286605eed23197c706bf5882ca97ea
Author: alex wennerberg <alex@alexwennerberg.com>
Date:   Thu, 18 Nov 2021 21:26:32 -0800

Add support for finger:// URIs

Diffstat:
Mfinger.rs | 48+++++++++++++++++++++++++++++++++---------------
1 file changed, 33 insertions(+), 15 deletions(-)

diff --git a/finger.rs b/finger.rs @@ -19,26 +19,44 @@ use std::io::prelude::*; use std::net::TcpStream; use std::process; +// supports standard finger queries and URIs +// From the drat spec: +// https://datatracker.ietf.org/doc/html/draft-ietf-uri-url-finger-02 +// returns host, user +fn get_params(query: &str) -> (&str, &str) { + // is URI + if query.starts_with("finger://") { + // kinda ugly + let i = query[9..].find("/").unwrap_or(query.len() - 9); + return (&query[9..9 + i], &query[9 + i..].trim_matches('/')); + } else { + let user: &str; + let host: &str; + let params: Vec<&str> = query.split("@").collect(); + if params.len() == 1 { + host = params[0]; + user = ""; + } else { + user = params[0]; + if params.len() > 2 || !user.chars().all(|c| c.is_ascii()) { + println!("Username must be ascii and not include @"); + process::exit(1); + } + host = params[1]; + } + return (host, user); + } +} + fn main() -> std::io::Result<()> { let args: Vec<String> = env::args().collect(); if args.len() < 2 { - println!("Usage: finger [user]@host"); + println!("Usage: finger [user]@host or finger://host/user"); process::exit(1); } - let params: Vec<&str> = args[1].split("@").collect(); - let user: &str; - let host: &str; - if params.len() == 1 { - host = params[0]; - user = ""; - } else { - user = params[0]; - if params.len() > 2 || !user.chars().all(|c| c.is_ascii()) { - println!("Username must be ascii and not include @"); - process::exit(1); - } - host = params[1]; - } + + let (host, user) = get_params(&args[1]); + let dest: String; if !host.contains(":") { dest = format!("{}:79", host);