about summary refs log tree commit diff stats
path: root/main.c
diff options
context:
space:
mode:
authorAnselm R. Garbe <arg@10kloc.org>2006-09-11 11:28:28 +0200
committerAnselm R. Garbe <arg@10kloc.org>2006-09-11 11:28:28 +0200
commitb597fa46370af6594c0980f6c2ddefbe943ee933 (patch)
tree5000414fe9f80a09e99d199f848e3ff40107f045 /main.c
parentd2d394eccf2d23eb264a321fe187bb2084beb4b9 (diff)
downloaddwm-b597fa46370af6594c0980f6c2ddefbe943ee933.tar.gz
fixed some other comments, now also the code side seems to be at a level to be reviewed by experienced programmers
Diffstat (limited to 'main.c')
-rw-r--r--main.c4
1 files changed, 2 insertions, 2 deletions
diff --git a/main.c b/main.c
index 5e7b4a6..9d583b2 100644
--- a/main.c
+++ b/main.c
@@ -214,7 +214,7 @@ quit(Arg *arg)
 /*
  * There's no way to check accesses to destroyed windows, thus those cases are
  * ignored (especially on UnmapNotify's).  Other types of errors call Xlibs
- * default error handler, which calls exit().
+ * default error handler, which may call exit.
  */
 int
 xerror(Display *dpy, XErrorEvent *ee)
@@ -229,7 +229,7 @@ xerror(Display *dpy, XErrorEvent *ee)
 		return 0;
 	fprintf(stderr, "dwm: fatal error: request code=%d, error code=%d\n",
 		ee->request_code, ee->error_code);
-	return xerrorxlib(dpy, ee); /* may call exit() */
+	return xerrorxlib(dpy, ee); /* may call exit */
 }
 
 int
color: #cc0000; font-weight: bold; background-color: #fff0f0 } /* Comment.Special */ .highlight .gd { color: #000000; background-color: #ffdddd } /* Generic.Deleted */ .highlight .ge { font-style: italic } /* Generic.Emph */ .highlight .ges { font-weight: bold; font-style: italic } /* Generic.EmphStrong */ .highlight .gr { color: #aa0000 } /* Generic.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
use chrono::prelude::*;
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: usize,
    want_users: bool,
    admin_email: String,
    description: String,
    last_generated: String,
    users: Vec<User>,
}

#[derive(Debug, Deserialize, Serialize, Clone)]
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 home_dir = WalkDir::new("/home").follow_links(true).max_depth(1);
    let user_count = fs::read_dir("/home")
        .unwrap()
        .map(|d| d.unwrap())
        .collect::<Vec<fs::DirEntry>>()
        .len();
    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);
            }
        }
    });

    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,
        });
    });

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

    let last_generated = Utc::now().to_rfc2822();

    let mut conf_yaml = Server {
        name: conf_yaml["name"].as_str().unwrap().to_string(),
        url: conf_yaml["url"].as_str().unwrap().to_string(),
        signup_url: conf_yaml["signup_url"].as_str().unwrap().to_string(),
        user_count: 0,
        want_users: conf_yaml["want_users"].as_bool().unwrap(),
        admin_email: conf_yaml["admin_email"].as_str().unwrap().to_string(),
        description: conf_yaml["description"].as_str().unwrap().to_string(),
        last_generated,
        users: Vec::new(),
    };

    users_struct.iter().for_each(|user| {
        conf_yaml.users.push(user.clone());
    });

    conf_yaml.user_count = user_count;

    let json = serde_json::to_string(&conf_yaml).unwrap();
    fs::write(out_path, &json).unwrap();

    println!("File written successfully! Please inspect it: {}", out_path);
}