Rust implementation

Rust implementation of fucking_coffee, hangover, and smack_my_bitch_up
This commit is contained in:
Ethan Water 2023-04-10 15:15:54 -04:00
parent b14a0a89bd
commit 2ba8e46abe
4 changed files with 154 additions and 0 deletions

9
rust/dependencies.txt Normal file
View File

@ -0,0 +1,9 @@
smack_my_bitch_up + hangover
-rand = "0.8.5"
-dotenv = "0.15.0"
-openapi = { path = "./twilio-rust" }
-tokio = { version = "1.21.1", features = ["rt", "rt-multi-thread", "macros"] }
fucking_coffee
-pure rust

37
rust/fucking_coffee.rs Normal file
View File

@ -0,0 +1,37 @@
use std::{
io::{Read, Write},
net::TcpStream,
process::Command,
thread,
time::Duration,
};
const COFFEE_MACHINE_IP: &str = "10.10.42.42:23";
const COFFEE_MACHINE_PASSWORD: &str = "1234";
fn main() -> std::io::Result<()> {
//Exit early if no sessions with my username are found
let users = Command::new("who").output().unwrap();
let is_active = String::from_utf8_lossy(&users.stdout)
.split('\n')
.any(|line| line.starts_with("username"));
if !is_active {
std::process::exit(0);
}
//Brew fucking coffee
let delay_before_brew = Duration::from_secs(17);
let delay = Duration::from_secs(24);
thread::sleep(delay_before_brew);
let mut conn = TcpStream::connect(COFFEE_MACHINE_IP)?;
conn.write_all(COFFEE_MACHINE_PASSWORD.as_bytes())?;
conn.read_exact(&mut [0u8; 1024])?;
conn.write_all(b"sys brew")?;
thread::sleep(delay);
conn.write_all(b"sys pour")?;
Ok(())
}

55
rust/hangover.rs Normal file
View File

@ -0,0 +1,55 @@
use std::{env, process::Command};
use dotenv::dotenv;
use rand::seq::SliceRandom;
use openapi::apis::{
configuration::Configuration,
default_api::{self as twilio_api, CreateMessageParams},
};
#[tokio::main]
async fn main() {
dotenv().expect("Error reading .env file");
let my_number = env::var("MY_NUMBER").expect("oof");
let boss_number = env::var("BOSSES_NUMBER").unwrap();
let api_key_sid = env::var("API_KEY_SID").unwrap();
let api_key_secret = env::var("API_KEY_SECRET").unwrap();
let account_sid = env::var("ACCOUNT_SID").unwrap();
//Exit early if no sessions with my username are found
let users = Command::new("who").output().unwrap();
let is_active = String::from_utf8_lossy(&users.stdout)
.split('\n')
.any(|line| line.starts_with("username"));
if !is_active {
std::process::exit(0);
}
//Create message
let reasons: Vec<&str> = vec!["Locked out",
"Pipes broke",
"Food poisoning",
"Not feeling well"];
let random_reason = reasons.choose(&mut rand::thread_rng()).unwrap();
let message = format!("Gonna work from home. {}", random_reason);
//Send a text message
let twilio_config = Configuration {
basic_auth: Some((api_key_sid, Some(api_key_secret))),
..Default::default()
};
let msg_params = CreateMessageParams {
account_sid,
to: my_number,
from: Some(boss_number),
body: Some(msg.into()),
..Default::default()
};
let send_msg = twilio_api::create_message(&twilio_config, msg_params).await;
match send_msg {
Ok(result) => result,
Err(error) => panic!("Error sending message: {}", error),
};
}

53
rust/smack_my_bitch_up.rs Normal file
View File

@ -0,0 +1,53 @@
use std::{env, process::Command};
use dotenv::dotenv;
use rand::seq::SliceRandom;
use openapi::apis::{
configuration::Configuration,
default_api::{self as twilio_api, CreateMessageParams},
};
#[tokio::main]
async fn main() {
dotenv().expect("Error reading .env file");
let my_number = env::var("MY_NUMBER").expect("oof");
let her_number = env::var("HER_NUMBER").unwrap();
let api_key_sid = env::var("API_KEY_SID").unwrap();
let api_key_secret = env::var("API_KEY_SECRET").unwrap();
let account_sid = env::var("ACCOUNT_SID").unwrap();
//Exit early if no sessions with my username are found
let users = Command::new("who").output().unwrap();
let is_active = String::from_utf8_lossy(&users.stdout)
.split('\n')
.any(|line| line.starts_with("username"));
if !is_active {
std::process::exit(0);
}
//Create message
let reasons: Vec<&str> = vec!["Working hard",
"Gotta ship this feature",
"Someone fucked the system again"];
let random_reason: &str = reasons.choose(&mut rand::thread_rng()).unwrap();
let msg = format!("Late at work. {}", random_reason);
//Send a text message
let twilio_config = Configuration {
basic_auth: Some((api_key_sid, Some(api_key_secret))),
..Default::default()
};
let msg_params = CreateMessageParams {
account_sid,
to: my_number,
from: Some(her_number),
body: Some(msg.into()),
..Default::default()
};
let send_msg = twilio_api::create_message(&twilio_config, msg_params).await;
match send_msg {
Ok(result) => result,
Err(error) => panic!("Error sending message: {}", error),
};
}