2023-09-09 19:23:59 -07:00
|
|
|
use chrono::{DateTime, Datelike, Utc};
|
2023-09-09 18:12:39 -07:00
|
|
|
use chrono_tz::US::Central;
|
|
|
|
use clap::Parser;
|
2023-09-10 21:38:30 -07:00
|
|
|
use lettre::{
|
2023-09-11 17:53:21 -07:00
|
|
|
message::{header::ContentType, Attachment, Message, MultiPart, SinglePart},
|
2023-09-10 21:38:30 -07:00
|
|
|
SendmailTransport, Transport,
|
|
|
|
};
|
2023-09-09 18:12:39 -07:00
|
|
|
use log::{debug, error};
|
|
|
|
use reqwest::blocking::Client;
|
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
|
|
// --- Loading and setup ---
|
|
|
|
let args = CliArgs::parse();
|
|
|
|
|
|
|
|
simplelog::SimpleLogger::init(
|
|
|
|
match args.debug {
|
|
|
|
true => simplelog::LevelFilter::Debug,
|
|
|
|
false => simplelog::LevelFilter::Info,
|
|
|
|
},
|
|
|
|
simplelog::Config::default(),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
debug!("Opening Config file: {}", args.config_file.display());
|
|
|
|
debug!(
|
|
|
|
"Config file exists: {}",
|
|
|
|
std::fs::metadata(&args.config_file).is_ok()
|
|
|
|
);
|
|
|
|
|
|
|
|
let file_contents = match std::fs::read_to_string(&args.config_file) {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => {
|
|
|
|
error!("Could not read config file: {e}");
|
|
|
|
panic!("{e}");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let cfg: Config = match toml::from_str(&file_contents) {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => {
|
|
|
|
error!("Could not parse config file: {e}");
|
|
|
|
panic!("{e}");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// --- END Loading and setup ---
|
|
|
|
|
2023-09-10 21:38:30 -07:00
|
|
|
let mut do_email_summary: bool = false;
|
|
|
|
|
2023-09-09 18:12:39 -07:00
|
|
|
// --- Get checked and unchecked ---
|
|
|
|
let primary_note = Client::new()
|
|
|
|
.get(format!(
|
|
|
|
"https://{}/index.php/apps/notes/api/v1/notes/{}",
|
|
|
|
&cfg.server_url, &cfg.primary_note_id
|
|
|
|
))
|
|
|
|
.header("Accept", "application/json")
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.basic_auth(&cfg.user, Some(&cfg.pswd))
|
|
|
|
.send()?
|
|
|
|
.json::<Note>()?;
|
|
|
|
|
|
|
|
let primary_note_lines: Vec<&str> = primary_note.content.lines().collect();
|
|
|
|
|
|
|
|
let mut unchecked: Vec<String> = vec![];
|
|
|
|
let mut checked: Vec<String> = vec![];
|
|
|
|
|
|
|
|
for line in primary_note_lines.iter() {
|
2023-09-10 21:38:30 -07:00
|
|
|
if line.starts_with("- [x] Send Summary") {
|
|
|
|
do_email_summary = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if line.starts_with("- [ ] Send Summary") {
|
|
|
|
continue;
|
|
|
|
}
|
2023-09-09 18:12:39 -07:00
|
|
|
// if line is a checkbox
|
|
|
|
if line.starts_with("- [") {
|
|
|
|
if line.starts_with("- [ ] ") {
|
|
|
|
unchecked.push(line.replace("- [ ] ", ""));
|
|
|
|
} else if line.starts_with("- [x] ") {
|
|
|
|
checked.push(line.replace("- [x] ", ""));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- END Get checked and unchecked ---
|
|
|
|
|
|
|
|
// --- Get current log ---
|
|
|
|
let logging_note = Client::new()
|
|
|
|
.get(format!(
|
|
|
|
"https://{}/index.php/apps/notes/api/v1/notes/{}",
|
|
|
|
&cfg.server_url, &cfg.logging_note_id
|
|
|
|
))
|
|
|
|
.header("Accept", "application/json")
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.basic_auth(&cfg.user, Some(&cfg.pswd))
|
|
|
|
.send()?
|
|
|
|
.json::<Note>()?;
|
|
|
|
|
|
|
|
let now = Utc::now().with_timezone(&Central);
|
|
|
|
|
|
|
|
let mut body_content: Vec<String> = vec![format!(
|
|
|
|
"*Last Updated:* {} Central Time",
|
|
|
|
now.format("%D - %H:%M:%S")
|
|
|
|
)];
|
|
|
|
|
|
|
|
let logging_note_lines: Vec<&str> = logging_note.content.lines().collect();
|
|
|
|
|
|
|
|
for line in logging_note_lines {
|
|
|
|
if line.starts_with("*Last Updated") {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if line.starts_with("# ") {
|
|
|
|
body_content.push(line.to_string());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if line.starts_with("### ") {
|
|
|
|
body_content.push(line.to_string());
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --- Assume it is a started log ---
|
|
|
|
let split: Vec<&str> = line.split('|').collect();
|
|
|
|
|
|
|
|
let item = match split.first() {
|
|
|
|
Some(val) => val,
|
|
|
|
None => {
|
|
|
|
error!("Couldn't split correctly, item 1");
|
|
|
|
panic!("Couldn't split correctly, item 1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.replace('#', "")
|
|
|
|
.trim()
|
|
|
|
.to_string();
|
|
|
|
|
|
|
|
if unchecked.contains(&item) {
|
|
|
|
let timestamp = match split.get(1) {
|
|
|
|
Some(val) => val,
|
|
|
|
None => {
|
|
|
|
error!("Couldn't split correctly, item 2");
|
|
|
|
panic!("Couldn't split correctly, item 2");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let start_time = match DateTime::parse_from_rfc2822(timestamp) {
|
|
|
|
Ok(val) => val.with_timezone(&Central),
|
|
|
|
Err(e) => {
|
|
|
|
error!("Error parsing time: '{timestamp}' : {e}");
|
|
|
|
panic!("Error parsing time: '{timestamp}' : {e}");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let elapsed_time: chrono::Duration = now - start_time;
|
|
|
|
|
|
|
|
body_content.push(format!(
|
|
|
|
"### {item} | {}h {}m | {}/{}/{}",
|
|
|
|
elapsed_time.num_hours(),
|
2023-09-10 21:38:30 -07:00
|
|
|
elapsed_time.num_minutes() - elapsed_time.num_hours() * 60, // yes, I'm lazy
|
2023-09-09 18:12:39 -07:00
|
|
|
now.month(),
|
|
|
|
now.day(),
|
|
|
|
now.year()
|
|
|
|
));
|
2023-09-09 19:27:22 -07:00
|
|
|
} else if !line.is_empty() {
|
2023-09-10 21:38:30 -07:00
|
|
|
body_content.push(line.to_string());
|
2023-09-09 18:12:39 -07:00
|
|
|
}
|
|
|
|
checked.retain(|val| val != &item);
|
|
|
|
}
|
|
|
|
|
|
|
|
checked = checked
|
|
|
|
.into_iter()
|
|
|
|
.map(|val| {
|
|
|
|
format!(
|
|
|
|
"## {val} | {}",
|
|
|
|
Utc::now().with_timezone(&Central).to_rfc2822()
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect();
|
|
|
|
|
2023-09-10 21:38:30 -07:00
|
|
|
debug!("Creating new logs for: {:#?}", checked);
|
2023-09-09 18:12:39 -07:00
|
|
|
|
|
|
|
body_content.splice(2..2, checked);
|
|
|
|
|
|
|
|
debug!("{:#?}", body_content);
|
|
|
|
|
|
|
|
Client::new()
|
|
|
|
.put(format!(
|
|
|
|
"https://{}/index.php/apps/notes/api/v1/notes/{}",
|
|
|
|
&cfg.server_url, &cfg.logging_note_id
|
|
|
|
))
|
|
|
|
.header("Accept", "application/json")
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.basic_auth(&cfg.user, Some(&cfg.pswd))
|
|
|
|
.body(serde_json::to_string(&NoteUpdate {
|
|
|
|
content: body_content.join("\n"),
|
|
|
|
})?)
|
|
|
|
.send()?;
|
|
|
|
|
2023-09-10 21:38:30 -07:00
|
|
|
if do_email_summary {
|
|
|
|
debug!("Send email selected");
|
2023-09-10 22:12:27 -07:00
|
|
|
reset_email_checkbox(&cfg);
|
2023-09-11 17:53:21 -07:00
|
|
|
send_email_summary(&cfg, body_content, &args);
|
2023-09-10 21:38:30 -07:00
|
|
|
}
|
|
|
|
|
2023-09-09 18:12:39 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
struct Note {
|
|
|
|
id: usize,
|
|
|
|
etag: String,
|
|
|
|
readonly: bool,
|
|
|
|
modified: u64,
|
|
|
|
title: String,
|
|
|
|
category: String,
|
|
|
|
content: String,
|
|
|
|
favorite: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
struct NoteUpdate {
|
|
|
|
content: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Default)]
|
|
|
|
struct Config {
|
|
|
|
user: String,
|
|
|
|
pswd: String,
|
2023-09-10 21:38:30 -07:00
|
|
|
email_addr: String,
|
2023-09-09 18:12:39 -07:00
|
|
|
primary_note_id: String,
|
|
|
|
logging_note_id: String,
|
|
|
|
server_url: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Parser, Debug)]
|
|
|
|
#[command(author, version, about, long_about=None)]
|
|
|
|
struct CliArgs {
|
|
|
|
/// Path to config .toml file
|
|
|
|
#[arg(short, long)]
|
|
|
|
config_file: PathBuf,
|
|
|
|
|
2023-09-11 17:53:21 -07:00
|
|
|
#[arg(short, long)]
|
|
|
|
from_addr: String,
|
|
|
|
|
2023-09-09 18:12:39 -07:00
|
|
|
#[arg(short, long)]
|
|
|
|
debug: bool,
|
|
|
|
}
|
2023-09-10 21:38:30 -07:00
|
|
|
|
|
|
|
// All unwraps are unrecoverable errors
|
|
|
|
// All calls after depend on the success of the one before, and cannot run
|
|
|
|
// without. So it would be too much work to make an error type to handle an error
|
|
|
|
// that would cause a crash anyways
|
|
|
|
fn reset_email_checkbox(config: &Config) {
|
|
|
|
let primary_note = Client::new()
|
|
|
|
.get(format!(
|
|
|
|
"https://{}/index.php/apps/notes/api/v1/notes/{}",
|
|
|
|
&config.server_url, &config.primary_note_id
|
|
|
|
))
|
|
|
|
.header("Accept", "application/json")
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.basic_auth(&config.user, Some(&config.pswd))
|
|
|
|
.send()
|
|
|
|
.unwrap()
|
|
|
|
.json::<Note>()
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
let primary_note_lines: Vec<&str> = primary_note.content.lines().collect();
|
|
|
|
|
|
|
|
let mut body_content: Vec<&str> = vec![];
|
|
|
|
|
|
|
|
for line in primary_note_lines.iter() {
|
|
|
|
if line.starts_with("- [x] Send Summary") {
|
|
|
|
body_content.push("- [ ] Send Summary");
|
|
|
|
} else {
|
|
|
|
body_content.push(line);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Client::new()
|
|
|
|
.put(format!(
|
|
|
|
"https://{}/index.php/apps/notes/api/v1/notes/{}",
|
|
|
|
&config.server_url, &config.primary_note_id
|
|
|
|
))
|
|
|
|
.header("Accept", "application/json")
|
|
|
|
.header("Content-Type", "application/json")
|
|
|
|
.basic_auth(&config.user, Some(&config.pswd))
|
|
|
|
.body(
|
|
|
|
serde_json::to_string(&NoteUpdate {
|
|
|
|
content: body_content.join("\n"),
|
|
|
|
})
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.send()
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Debug)]
|
|
|
|
struct SummaryRow {
|
|
|
|
date: String,
|
|
|
|
total_time: i64,
|
|
|
|
task_name: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
// All unwraps are unrecoverable errors
|
|
|
|
// All calls after depend on the success of the one before, and cannot run
|
|
|
|
// without. So it would be too much work to make an error type to handle an error
|
|
|
|
// that would cause a crash anyways
|
2023-09-11 17:53:21 -07:00
|
|
|
fn send_email_summary(config: &Config, body_content: Vec<String>, cliargs: &CliArgs) {
|
2023-09-10 21:38:30 -07:00
|
|
|
let mut wtr = csv::Writer::from_writer(vec![]);
|
|
|
|
|
|
|
|
for line in body_content {
|
|
|
|
if line.starts_with("### ") {
|
|
|
|
let mut split: Vec<&str> = line.split('|').collect();
|
|
|
|
if split.len() != 3 {
|
2023-09-11 17:53:21 -07:00
|
|
|
error!("There was an issue with this line. 3 splits not found: {line}");
|
2023-09-10 21:38:30 -07:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
let date: String = match split.pop() {
|
|
|
|
Some(val) => val,
|
|
|
|
None => continue,
|
|
|
|
}
|
|
|
|
.to_string();
|
|
|
|
let time: i64 = {
|
|
|
|
// This should never error as the len should always be 3
|
|
|
|
let data = match split.pop() {
|
|
|
|
Some(val) => val,
|
|
|
|
None => continue,
|
|
|
|
}
|
|
|
|
.trim();
|
|
|
|
|
|
|
|
// There should always be an h and an m in the second item
|
|
|
|
let h_index = match data.chars().position(|c| c == 'h') {
|
|
|
|
Some(val) => val,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
let m_index = match data.chars().position(|c| c == 'm') {
|
|
|
|
Some(val) => val,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
// this should always be the "10" in "10h"
|
|
|
|
let hours = match data[0..h_index].parse::<i64>() {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(_) => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
// this should always be the "20" in "10h 20m"
|
|
|
|
let minutes = match data[(h_index + 2)..m_index].parse::<i64>() {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(_) => continue,
|
|
|
|
};
|
|
|
|
|
|
|
|
hours * 60 + minutes
|
|
|
|
};
|
|
|
|
|
|
|
|
let task_name: String = match split.pop() {
|
|
|
|
Some(val) => val.trim(),
|
|
|
|
None => continue,
|
|
|
|
}
|
|
|
|
.to_string()
|
|
|
|
.replace("### ", "");
|
|
|
|
|
2023-09-11 17:53:21 -07:00
|
|
|
match wtr.serialize(SummaryRow {
|
|
|
|
date: date.trim().to_string(),
|
|
|
|
total_time: time,
|
|
|
|
task_name,
|
|
|
|
}) {
|
|
|
|
Ok(_) => continue,
|
|
|
|
Err(e) => {
|
|
|
|
error!("There was an error serializing the csv, aborting: {e}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2023-09-10 21:38:30 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-11 17:53:21 -07:00
|
|
|
let data = match String::from_utf8(wtr.into_inner().unwrap()) {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => {
|
|
|
|
error!("There was an error converting the csv writer to a string, aborting: {e}");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
2023-09-10 21:38:30 -07:00
|
|
|
|
|
|
|
debug!("{:#?}", data);
|
|
|
|
|
2023-09-11 17:53:21 -07:00
|
|
|
let attachment = Attachment::new("TimeSummary.csv".to_string())
|
|
|
|
// The unwrap is on a constant value
|
|
|
|
.body(data, ContentType::parse("text/csv").unwrap());
|
2023-09-10 21:38:30 -07:00
|
|
|
|
2023-09-11 16:56:59 -07:00
|
|
|
const HTML: &str = r#"
|
2023-09-11 18:35:50 -07:00
|
|
|
<!DOCTYPE html>
|
2023-09-11 16:56:59 -07:00
|
|
|
<html lang="en">
|
|
|
|
<head>
|
|
|
|
<meta charset="UTF-8">
|
|
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
2023-09-11 17:53:21 -07:00
|
|
|
<title>ChronoTrack Export</title>
|
2023-09-11 18:35:50 -07:00
|
|
|
<style>
|
|
|
|
#top_banner {
|
|
|
|
background-color: #33ccff;
|
|
|
|
border-radius: 40px;
|
|
|
|
height: 100px
|
|
|
|
}
|
|
|
|
#center_box {
|
|
|
|
display: flex;
|
|
|
|
flex-direction: column;
|
|
|
|
align-items: center;
|
|
|
|
}
|
|
|
|
#image {
|
|
|
|
height:100px;
|
|
|
|
border-radius: 50%;
|
|
|
|
background-color: whitesmoke;
|
|
|
|
position: absolute;
|
|
|
|
top: 40px;
|
|
|
|
}
|
|
|
|
#the_text {
|
|
|
|
font-family: Arial, Helvetica, sans-serif;
|
|
|
|
position: absolute;
|
|
|
|
top: 160px;
|
|
|
|
}
|
|
|
|
</style>
|
2023-09-11 16:56:59 -07:00
|
|
|
</head>
|
|
|
|
<body>
|
2023-09-11 18:35:50 -07:00
|
|
|
<div id="top_banner"></div>
|
|
|
|
<div id="center_box">
|
|
|
|
<img src="https://cdn-icons-png.flaticon.com/512/3938/3938540.png" id="image">
|
|
|
|
<h2 id="the_text">Prepared with care, and sent through the horrors of the interwebs, ChronoTrack presents! (see attached)</h2>
|
2023-09-11 16:56:59 -07:00
|
|
|
</div>
|
|
|
|
</body>
|
2023-09-11 18:35:50 -07:00
|
|
|
</html>
|
|
|
|
"#;
|
2023-09-11 16:56:59 -07:00
|
|
|
|
2023-09-10 21:38:30 -07:00
|
|
|
let email = Message::builder()
|
2023-09-11 17:53:21 -07:00
|
|
|
.from(match cliargs.from_addr.parse() {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => {
|
|
|
|
error!("The provided from_email address was unparsable:\n{e}");
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.to(match config.email_addr.parse() {
|
|
|
|
Ok(val) => val,
|
|
|
|
Err(e) => {
|
|
|
|
error!(
|
|
|
|
"The provided destination email in the configuration file was unparsable:\n{e}"
|
|
|
|
);
|
|
|
|
std::process::exit(1);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.subject("ChronoTrack Export")
|
2023-09-11 16:56:59 -07:00
|
|
|
.multipart(
|
|
|
|
MultiPart::mixed()
|
|
|
|
.multipart(
|
|
|
|
MultiPart::alternative()
|
|
|
|
.singlepart(
|
|
|
|
SinglePart::builder()
|
|
|
|
.header(ContentType::TEXT_PLAIN)
|
2023-09-11 17:53:21 -07:00
|
|
|
.body(String::from("This is an automated email from ChronoTrack")),
|
2023-09-11 16:56:59 -07:00
|
|
|
)
|
|
|
|
.singlepart(
|
|
|
|
SinglePart::builder()
|
|
|
|
.header(ContentType::TEXT_HTML)
|
2023-09-11 17:53:21 -07:00
|
|
|
.body(String::from(HTML)),
|
|
|
|
),
|
|
|
|
)
|
|
|
|
.singlepart(attachment),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2023-09-10 21:38:30 -07:00
|
|
|
|
|
|
|
let mailer = SendmailTransport::new();
|
2023-09-11 17:53:21 -07:00
|
|
|
|
2023-09-10 21:38:30 -07:00
|
|
|
match mailer.send(&email) {
|
|
|
|
Ok(val) => debug!("email sent: {:?}", val),
|
2023-09-11 17:53:21 -07:00
|
|
|
Err(e) => error!("Couldn't send email {}", e),
|
2023-09-10 21:38:30 -07:00
|
|
|
};
|
|
|
|
}
|