about summary refs log tree commit diff stats
path: root/src/main.rs
blob: eedf304236f9b33946f4864597ba0b8fb082ebba (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
use std::env;
use std::fs;
use std::path::Path;
use std::process;

use serde::{Deserialize, Serialize};
use serde_yaml;
use walkdir::WalkDir;

const VERS: &str = "v0.1";
const CONF_PATH: &str = "instistats.yml";

#[derive(Debug, Deserialize, Serialize)]
struct Server {
    name: String,
    url: String,
    signup_url: String,
    user_count: Option<u32>,
    want_users: bool,
    admin_email: String,
    description: String,
    users: Option<Vec<User>>,
}

#[derive(Debug, Deserialize, Serialize)]
struct User {
    name: String,
    title: String,
    mtime: String,
}

fn main() {
    println!("instistats {}", VERS);
    println!("(c) 2019 Ben Morrison - ISC License");
    println!();
    let args = env::args().collect::<Vec<String>>();
    let out_path = match args[1].trim() {
        "-h" | "--help" => {
            println!("The only argument should be the path to save the tilde.json file.\nEx: /var/www/htdocs/tilde.json");
            process::exit(0);
        }
        out_path => {
            println!("Output Location: {}", out_path);
            out_path
        }
    };
    println!();

    let conf = fs::read_to_string(CONF_PATH).expect("Could not read config file");
    let conf_yaml: Server =
        serde_yaml::from_str(&conf).expect("Could not parse config data as YAML");

    eprintln!("{:#?}", conf_yaml);

    let home_dir = WalkDir::new("/home").follow_links(true).max_depth(1);
    let mut users_list = Vec::new();
    home_dir.into_iter().for_each(|d| {
        if let Ok(p) = d {
            let p = p.path().strip_prefix("/home").unwrap();
            let p = p.to_str().unwrap();
            if p.len() > 1 {
                let user = p
                    .chars()
                    .map(|c| {
                        if c != '"' {
                            c.to_string()
                        } else {
                            "".to_string()
                        }
                    })
                    .collect::<String>();
                users_list.push(user);
            }
        }
    });

    eprintln!("{:?}", users_list);

    let mut users_struct = Vec::new();
    users_list.iter().for_each(|user| {
        let path = format!("/home/{}/public_html/index.html", user);
        let path = Path::new(&path);
        let index_file = if let Ok(file) = fs::read_to_string(path) {
            file
        } else {
            return;
        };
        let mut title = String::new();
        index_file.split("\n").for_each(|line| {
            if line.contains("<title>") {
                let title_line = line
                    .split("<title>")
                    .map(|s| s.to_string())
                    .collect::<Vec<String>>();
                let title_line = title_line
                    .iter()
                    .map(|s| {
                        s.split("</title>")
                            .map(|s| s.to_string())
                            .collect::<Vec<String>>()
                    })
                    .flatten()
                    .collect::<Vec<String>>();
                title_line.iter().for_each(|e| {
                    if !e.contains("<title>") && !e.contains("</title>") {
                        title.push_str(e);
                    }
                })
            }
        });

        let meta = fs::metadata(path);
        let mtime = format!(
            "{}",
            meta.unwrap()
                .modified()
                .unwrap()
                .duration_since(std::time::SystemTime::UNIX_EPOCH)
                .unwrap()
                .as_secs()
        );
        users_struct.push(User {
            name: user.to_string(),
            title,
            mtime,
        });
    });

    eprintln!("{:#?}", users_struct);
}