use std::env;
use std::fs;
use robotstxt_with_cache::{DefaultCachingMatcher, DefaultMatcher};
fn show_help(name: &str) {
eprintln!(
"Shows whether the given user_agent and URI combination \
is allowed or disallowed by the given robots.txt file. \n"
);
eprintln!(
"Usage:\n {} <robots.txt filename> <user_agent> <URI> \n",
name
);
eprintln!("The URI must be %-encoded according to RFC3986.\n");
eprintln!(
"Example:\n {} robots.txt FooBot http://example.com/foo\n",
name
);
}
fn main() {
let mut args = env::args();
match (args.next(), args.next(), args.next(), args.next()) {
(Some(execute), Some(filename), ..)
if &filename == "-h" || &filename == "-help" || &filename == "--help" =>
{
show_help(&execute);
}
(_, Some(filename), Some(user_agent), Some(url)) => {
if let Ok(robots_content) = fs::read_to_string(filename.clone()) {
let user_agents = vec![user_agent.as_str()];
let mut matcher = DefaultCachingMatcher::new(DefaultMatcher::default());
matcher.parse(&robots_content);
let allowed = matcher.allowed_by_robots(user_agents, &url);
println!(
"user-agent '{}' with URI '{}': {}",
user_agent,
url,
if allowed { "ALLOWED" } else { "DISALLOWED" }
);
if robots_content.is_empty() {
println!("notice: robots file is empty so all user-agents are allowed");
}
} else {
eprintln!("failed to read file \"{}\"", filename);
}
}
(Some(execute), ..) => {
eprintln!("Invalid amount of arguments. Showing help.\n");
show_help(&execute);
}
_ => {}
}
}