216 lines
7.0 KiB
Rust
216 lines
7.0 KiB
Rust
#[allow(unused)]
|
|
use env_logger::{Env, Target};
|
|
#[allow(unused)]
|
|
use log::{debug, error, info, log_enabled, warn};
|
|
use reqwest;
|
|
use std::collections::HashMap;
|
|
#[allow(unused)]
|
|
use std::io::{self, BufReader, Write};
|
|
#[allow(unused)]
|
|
use yaserde_derive::{YaDeserialize, YaSerialize};
|
|
use yaserde::de::from_str;
|
|
|
|
#[allow(unused)]
|
|
fn publicise() {}
|
|
|
|
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
|
#[yaserde(
|
|
rename = "multistatus",
|
|
prefix = "d",
|
|
namespace = "d: DAV:",
|
|
namespace = "nc: http://nextcloud.org/ns",
|
|
namespace = "oc: http://owncloud.org/ns",
|
|
namespace = "s: http://sabredav.org/ns"
|
|
)]
|
|
pub struct Multistatus {
|
|
#[yaserde(prefix = "d")]
|
|
response: Vec<NextcloudResponse>,
|
|
}
|
|
|
|
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
|
#[yaserde(rename = "response", prefix = "d", namespace = "d: DAV:")]
|
|
pub struct NextcloudResponse {
|
|
#[yaserde(prefix = "d")]
|
|
// href: Option<String>,
|
|
href: String,
|
|
#[yaserde(prefix = "d")]
|
|
propstat: Vec<Propstat>,
|
|
}
|
|
|
|
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
|
#[yaserde(rename = "propstat", prefix = "d", namespace = "d: DAV:")]
|
|
pub struct Propstat {
|
|
#[yaserde(prefix = "d")]
|
|
prop: Prop,
|
|
#[yaserde(prefix = "d")]
|
|
status: String,
|
|
}
|
|
|
|
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
|
#[yaserde(
|
|
rename = "prop",
|
|
namespace = "d: DAV:",
|
|
namespace = "oc: http://owncloud.org/ns"
|
|
)]
|
|
pub struct Prop {
|
|
#[yaserde(prefix = "d", rename = "getlastmodified")]
|
|
get_last_modified: String,
|
|
#[yaserde(prefix = "oc")]
|
|
permissions: String,
|
|
#[yaserde(prefix = "d", rename = "resourcetype")]
|
|
resource_type: ResourceType,
|
|
#[yaserde(prefix = "d", rename = "getetag")]
|
|
get_etag: String,
|
|
#[yaserde(prefix = "d", rename = "getcontentlength")]
|
|
get_content_length: u32,
|
|
#[yaserde(prefix = "d", rename = "getcontenttype")]
|
|
get_content_type: String,
|
|
#[yaserde(prefix = "oc", rename = "share-types")]
|
|
share_types: Vec<ShareType>,
|
|
}
|
|
|
|
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
|
#[yaserde(rename = "share-types", namespace = "oc: http://owncloud.org/ns")]
|
|
pub struct ShareType {
|
|
#[yaserde(rename = "share-type", prefix = "oc")]
|
|
share_type: u8,
|
|
}
|
|
|
|
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
|
#[yaserde(rename = "resourcetype", namespace = "d: DAV:")]
|
|
pub struct ResourceType {
|
|
#[yaserde(prefix = "d")]
|
|
collection: Vec<Collection>,
|
|
}
|
|
|
|
#[derive(Default, PartialEq, Debug, YaDeserialize)]
|
|
#[yaserde(rename = "collection", namespace = "d: DAV:")]
|
|
pub struct Collection {
|
|
#[yaserde(prefix = "d")]
|
|
collection: String,
|
|
}
|
|
|
|
async fn get_folder_contents(url_tail: &String) -> Result<String, Box<dyn std::error::Error>> {
|
|
debug!("Entering: get_folder_contents()");
|
|
let password = get_password().unwrap().trim().to_string();
|
|
debug!("Received password: {}[END]", password);
|
|
let root_url = "https://theadamcooper.com";
|
|
let method = reqwest::Method::from_bytes(b"PROPFIND").unwrap();
|
|
let client = reqwest::Client::new();
|
|
let url = format!("{}{}", root_url, url_tail);
|
|
debug!("url: {}", url);
|
|
let body = String::from(
|
|
r#"<?xml version="1.0" encoding="UTF-8"?>
|
|
<d:propfind xmlns:d="DAV:">
|
|
<d:prop xmlns:oc="http://owncloud.org/ns">
|
|
<d:getlastmodified/>
|
|
<d:getcontentlength/>
|
|
<d:getcontenttype/>
|
|
<oc:permissions/>
|
|
<d:resourcetype/>
|
|
<d:getetag/>
|
|
<oc:share-types/>
|
|
</d:prop>
|
|
</d:propfind>"#,
|
|
);
|
|
let response_text = client
|
|
.request(method, url)
|
|
.basic_auth("adam", Some(password))
|
|
.body(body)
|
|
.send()
|
|
.await?
|
|
.text()
|
|
.await?;
|
|
debug!("{:?}", response_text);
|
|
Ok(response_text)
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn traverse(mut result: Multistatus) {
|
|
debug!("Entering: traverse()");
|
|
// Initialize the indexed "pointer"
|
|
let mut current_index: usize = 0;
|
|
// Initialize the hashmap of visited items (by etag?)
|
|
let mut visited_items = HashMap::new();
|
|
let mut etag = (&mut result.response[current_index].propstat[0].prop.get_etag).clone();
|
|
visited_items.insert(etag, true);
|
|
current_index += 1;
|
|
|
|
// Depth first traversal
|
|
while current_index < (&mut result.response).len() {
|
|
debug!("current_index: {:?}", current_index);
|
|
// If result.response[current_index] has not been visited
|
|
if !visited_items.contains_key(&mut result.response[current_index].propstat[0].prop.get_etag) {
|
|
debug!("Fresh item...");
|
|
// if it's a collection
|
|
if !(&mut result.response[current_index].propstat[0].prop.resource_type.collection).is_empty() {
|
|
debug!("Collection...");
|
|
// Get the contents XML
|
|
let folder_contents: String = get_folder_contents(
|
|
&result.response[current_index].href, // change to mutable borrow if necessary
|
|
)
|
|
.await
|
|
.unwrap();
|
|
debug!("{:?}", folder_contents);
|
|
// Parse the contents XML into Multistatus
|
|
let mut new_result: Multistatus = from_str(&String::from(folder_contents)).unwrap();
|
|
debug!("\nParsed:\n{:?}", new_result);
|
|
// Append the NextcloudResponse vector to result.response
|
|
result.response.append(&mut new_result.response);
|
|
} else {
|
|
debug!("Node...");
|
|
// else it's a node. if it's not public, publicise it.
|
|
if !(&mut result.response[current_index].propstat[0].prop.share_types).contains(&ShareType{ share_type: 3 }) {
|
|
println!("it's not public");
|
|
}
|
|
}
|
|
// also add debugging statements in this function throughout!
|
|
}
|
|
current_index += 1;
|
|
}
|
|
}
|
|
|
|
fn get_password() -> Result<String, std::io::Error> {
|
|
print!("Nextcloud password: ");
|
|
io::stdout().flush().unwrap();
|
|
let mut buffer = String::new();
|
|
let stdin = io::stdin();
|
|
match stdin.read_line(&mut buffer) {
|
|
Ok(_) => Ok(buffer),
|
|
Err(error) => Err(error),
|
|
}
|
|
}
|
|
|
|
#[allow(unused)]
|
|
fn indent(size: usize) -> String {
|
|
const INDENT: &'static str = " ";
|
|
(0..size)
|
|
.map(|_| INDENT)
|
|
.fold(String::with_capacity(size * INDENT.len()), |r, s| r + s)
|
|
}
|
|
|
|
#[tokio::main]
|
|
async fn main() -> std::io::Result<()> {
|
|
// use yaserde::de::from_str;
|
|
|
|
env_logger::Builder::from_env(Env::default().default_filter_or("trace"))
|
|
.target(Target::Stdout)
|
|
.init();
|
|
println!("Publicise it!");
|
|
|
|
// TODO: Call get_password here. Maybe get_password should then store the password in the
|
|
// environment?
|
|
let folder_contents: String =
|
|
// get_folder_contents("/nextcloud/remote.php/dav/files/adam/test_public/2019_test_public")
|
|
get_folder_contents(&String::from("/nextcloud/remote.php/dav/files/adam/test_public"))
|
|
.await
|
|
.unwrap();
|
|
debug!("{:?}", folder_contents);
|
|
|
|
let mut result: Multistatus = from_str(&folder_contents).unwrap();
|
|
println!("{:?}", result);
|
|
|
|
traverse(result);
|
|
Ok(())
|
|
}
|