Compare commits
10 Commits
cf8ff0289e
...
72caa70606
| Author | SHA1 | Date | |
|---|---|---|---|
| 72caa70606 | |||
| 26d71ee449 | |||
| 76c4c0b86d | |||
| 4df1540bc8 | |||
| a0fcb5d4a8 | |||
| 36c98bd6df | |||
| c055beb4e8 | |||
| 921b10d696 | |||
| f0f64492cc | |||
| cdd4c29464 |
7
Cargo.lock
generated
7
Cargo.lock
generated
@@ -306,6 +306,12 @@ version = "0.3.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024"
|
||||
|
||||
[[package]]
|
||||
name = "humantime"
|
||||
version = "2.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "135b12329e5e3ce057a9f972339ea52bc954fe1e9358ef27f95e89716fbc5424"
|
||||
|
||||
[[package]]
|
||||
name = "iana-time-zone"
|
||||
version = "0.1.60"
|
||||
@@ -651,6 +657,7 @@ version = "0.1.0"
|
||||
dependencies = [
|
||||
"chrono",
|
||||
"futures",
|
||||
"humantime",
|
||||
"irc",
|
||||
"rand",
|
||||
"regex",
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
[package]
|
||||
name = "robbit"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
edition = "2024"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
@@ -13,3 +13,4 @@ futures = {version = "*"}
|
||||
tokio = {version = "*", features = ["full"]}
|
||||
regex = "1"
|
||||
rand = "0.8.5"
|
||||
humantime = "2.3.0"
|
||||
|
||||
@@ -12,4 +12,4 @@ USER robbit
|
||||
|
||||
RUN cargo build -r
|
||||
|
||||
CMD ./target/release/robbit
|
||||
CMD ["./target/release/robbit"]
|
||||
|
||||
17
src/lib.rs
17
src/lib.rs
@@ -6,14 +6,14 @@ use regex::Regex;
|
||||
//is this the best way to do this? probably not
|
||||
mod modules;
|
||||
|
||||
use modules::{bully, lenny, join_rude, grass, noemo, ttb, help, repo,rtfm};
|
||||
use modules::{bully, lenny, join_rude, grass, noemo, ttb, help, repo,rtfm, kick, history, time_to_date, bonk};
|
||||
|
||||
type ModuleFunc = fn(regex::Captures, &Message, &VecDeque<Message>)->String;
|
||||
const NUM_MODS:usize = 8;
|
||||
const NUM_MODS:usize = 12;
|
||||
|
||||
|
||||
const MODULES: [(&str, ModuleFunc);NUM_MODS] = [(lenny::PATTERN, lenny::mod_message), (bully::PATTERN, bully::mod_message), (grass::PATTERN, grass::touch_grass), (noemo::PATTERN, noemo::no_emo), (ttb::PATTERN, ttb::time_to_baby), (help::PATTERN, help::help), (repo::PATTERN, repo::link), (rtfm::PATTERN, rtfm::rtfm)];
|
||||
const MODULE_USAGE: [(&str, &str); NUM_MODS] = [(lenny::NAME, lenny::USAGE), (bully::NAME, bully::USAGE), (grass::NAME, grass::USAGE), (noemo::NAME, noemo::USAGE), (ttb::NAME, ttb::USAGE), (help::NAME, help::USAGE), (repo::NAME, repo::USAGE),(rtfm::NAME, rtfm::USAGE)];
|
||||
const MODULES: [(&str, ModuleFunc);NUM_MODS] = [(lenny::PATTERN, lenny::mod_message), (bully::PATTERN, bully::mod_message), (grass::PATTERN, grass::touch_grass), (noemo::PATTERN, noemo::no_emo), (ttb::PATTERN, ttb::time_to_baby), (help::PATTERN, help::help), (repo::PATTERN, repo::link), (rtfm::PATTERN, rtfm::rtfm), (kick::PATTERN, kick::mod_message), (history::PATTERN, history::mod_message), (time_to_date::PATTERN, time_to_date::time_to_date), (bonk::PATTERN, bonk::bonk)];
|
||||
const MODULE_USAGE: [(&str, &str); NUM_MODS] = [(lenny::NAME, lenny::USAGE), (bully::NAME, bully::USAGE), (grass::NAME, grass::USAGE), (noemo::NAME, noemo::USAGE), (ttb::NAME, ttb::USAGE), (help::NAME, help::USAGE), (repo::NAME, repo::USAGE),(rtfm::NAME, rtfm::USAGE), (kick::NAME, kick::USAGE), (history::NAME, history::USAGE), (time_to_date::NAME, time_to_date::USAGE), (bonk::NAME, bonk::USAGE)];
|
||||
|
||||
pub fn build_modules() -> Result<Vec<(Regex, ModuleFunc)>, regex::Error> {
|
||||
let mut regex_array: Vec<(Regex, ModuleFunc)> = Vec::with_capacity(NUM_MODS);
|
||||
@@ -29,10 +29,17 @@ pub fn handle(modules: &Vec<(Regex, ModuleFunc)>, message: &Message, message_buf
|
||||
match &message.command {
|
||||
PRIVMSG(_,msg) => for (regex, function) in modules{
|
||||
if let Some(captures) = regex.captures(msg.as_str()) {
|
||||
if msg.contains("$kick") {
|
||||
if message.source_nickname().unwrap_or_default() == "L3gion" {
|
||||
return Some((message.response_target().unwrap_or("#lug").to_string(), function(captures, message, message_buf)));
|
||||
}
|
||||
}
|
||||
else {
|
||||
return Some((message.response_target().unwrap_or("#lug").to_string(), function(captures, message, message_buf)));
|
||||
}
|
||||
}
|
||||
},
|
||||
JOIN(ref channel,_,_) => return join_rude::join_rude(message.source_nickname().unwrap_or("unknown user"), channel.as_str()),
|
||||
JOIN(channel,_,_) => return kick::bad_user(message.source_nickname().unwrap_or("unknown user"), channel.as_str()),
|
||||
_ => ()
|
||||
}
|
||||
|
||||
|
||||
10
src/main.rs
10
src/main.rs
@@ -6,7 +6,7 @@ use robbit::{build_modules, handle};
|
||||
|
||||
#[tokio::main]
|
||||
async fn main() -> Result<(), Error>{
|
||||
let max_len = 100;
|
||||
let max_len = 1000;
|
||||
|
||||
let config = Config::load("config.toml")?;
|
||||
let mut client = Client::from_config(config).await?;
|
||||
@@ -23,10 +23,15 @@ async fn main() -> Result<(), Error>{
|
||||
let response = handle(&module_pair, &message, &message_buf);
|
||||
|
||||
if let Some((target,msg))= response {
|
||||
if msg.contains("/kick") {
|
||||
let message: Vec<&str> = msg.split(" ").collect();
|
||||
sender.send_kick(target, message[1], "")?;
|
||||
}
|
||||
else {
|
||||
print!("{}",message);
|
||||
sender.send_privmsg(target,msg)?;
|
||||
}
|
||||
|
||||
} else {
|
||||
if message_buf.len() < max_len {
|
||||
message_buf.push_front(message);
|
||||
}
|
||||
@@ -35,6 +40,7 @@ async fn main() -> Result<(), Error>{
|
||||
message_buf.push_front(message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
18
src/modules/bonk.rs
Normal file
18
src/modules/bonk.rs
Normal file
@@ -0,0 +1,18 @@
|
||||
use std::collections::VecDeque;
|
||||
use irc::proto::Message;
|
||||
|
||||
|
||||
pub const USAGE: &str = "Usage: $bonk <nick>\r\nPut the person identified as nick in horny jail";
|
||||
|
||||
pub const NAME: &str = "bonk";
|
||||
|
||||
pub const PATTERN: &str = "^\\$bonk( (?P<nick>[^\\s]+))?$";
|
||||
|
||||
pub fn bonk(captures: regex::Captures, _message: &Message, _message_buf: &VecDeque<Message>) -> String {
|
||||
if let Some(nick) = captures.get(2) {
|
||||
format!("bonk! {} go to horny jail!", nick.as_str())
|
||||
}
|
||||
else {
|
||||
format!("bonk! go to horny jail!")
|
||||
}
|
||||
}
|
||||
@@ -9,9 +9,9 @@ pub fn touch_grass(captures: regex::Captures, message: &Message, _: &VecDeque<Me
|
||||
|
||||
let grass_toucher = captures.get(1).unwrap().as_str();
|
||||
|
||||
let complete_message = format!("{} thinks you should go outside and touch some grass, {}",
|
||||
message.source_nickname().unwrap_or("unknown_nick").to_string(),
|
||||
grass_toucher);
|
||||
let complete_message = format!("{}: {} thinks you should go outside and touch some grass",
|
||||
grass_toucher,
|
||||
message.source_nickname().unwrap_or("unknown_nick").to_string());
|
||||
|
||||
complete_message
|
||||
}
|
||||
|
||||
39
src/modules/history.rs
Normal file
39
src/modules/history.rs
Normal file
@@ -0,0 +1,39 @@
|
||||
use irc::proto::Message;
|
||||
use std::collections::VecDeque;
|
||||
use irc::proto::Command::*;
|
||||
|
||||
pub const PATTERN: &str = "^\\$history (?P<nick>[^\\s]+) (?P<amount>[0-9]+)";
|
||||
pub const NAME: &str = "history";
|
||||
pub const USAGE: &str = "Usage: $history <nick> <amount\r\nThis replays the last <amount> of messages from a user";
|
||||
|
||||
pub fn mod_message(captures: regex::Captures, _message: &Message, message_buf: &VecDeque<Message>) -> String {
|
||||
let amount: usize = captures.get(2).unwrap().as_str().parse().unwrap();
|
||||
let mut messages: Vec<String> = Vec::with_capacity(amount);
|
||||
let mut total_message: String = format!("No messages found for user: {}", captures.get(1).unwrap().as_str());
|
||||
|
||||
for message in message_buf {
|
||||
if let Some(nick) = message.source_nickname() {
|
||||
if nick == captures.get(1).unwrap().as_str() && messages.len() < amount {
|
||||
match &message.command {
|
||||
PRIVMSG(_,msg) => {
|
||||
messages.push(msg.clone());
|
||||
},
|
||||
_ => ()
|
||||
};
|
||||
}
|
||||
if messages.len() == amount {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if messages.len() > 0 {
|
||||
messages.reverse();
|
||||
total_message = messages.join("\r\n");
|
||||
}
|
||||
total_message
|
||||
}
|
||||
|
||||
pub fn usage(message: &Message) -> (String, String) {
|
||||
(message.response_target().unwrap_or("#lug").to_string(), USAGE.to_string())
|
||||
}
|
||||
35
src/modules/kick.rs
Normal file
35
src/modules/kick.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
use irc::proto::Message;
|
||||
use std::collections::VecDeque;
|
||||
use regex::Regex;
|
||||
|
||||
pub const PATTERN: &str = "^\\$kick (?P<nick>[^\\s]+)";
|
||||
pub const USAGE: &str = "";
|
||||
pub const NAME: &str = "kick";
|
||||
|
||||
|
||||
|
||||
pub fn mod_message(captures: regex::Captures, message: &Message, _message_buf: &VecDeque<Message>) -> String {
|
||||
let to_be_kicked = captures.get(1).unwrap().as_str();
|
||||
|
||||
format!("/kick {}", to_be_kicked)
|
||||
}
|
||||
|
||||
pub fn usage(message: &Message) -> (String, String) {
|
||||
(message.response_target().unwrap_or("#lug").to_string(), USAGE.to_string())
|
||||
}
|
||||
|
||||
pub fn bad_user(user: &str, channel: &str) -> Option<(String, String)> {
|
||||
let regex = match Regex::new(".*[dD]ick[rR]id(er|ing).*") {
|
||||
Ok(r) => r,
|
||||
Err(_) => {
|
||||
return None;
|
||||
}
|
||||
};
|
||||
|
||||
if regex.is_match(user) {
|
||||
Some((channel.to_string(), format!("/kick {}", user)))
|
||||
}
|
||||
else {
|
||||
None
|
||||
}
|
||||
}
|
||||
@@ -7,4 +7,7 @@ pub mod noemo;
|
||||
pub mod help;
|
||||
pub mod repo;
|
||||
pub mod rtfm;
|
||||
|
||||
pub mod kick;
|
||||
pub mod history;
|
||||
pub mod time_to_date;
|
||||
pub mod bonk;
|
||||
|
||||
@@ -1,13 +1,19 @@
|
||||
use irc::proto::Message;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
pub const PATTERN: &str = "^\\$noemo\\s*$";
|
||||
pub const PATTERN: &str = "^\\$noemo( (?P<nick>[^\\s]+))?$";
|
||||
pub const NAME: &str = "noemo";
|
||||
pub const USAGE: &str = "Usage: $noemo <nick>\r\nThis tells the user identified by nick to not be emo";
|
||||
pub const USAGE: &str = "Usage: $noemo <nick>\r\nThis tells the user identified by nick to not be emo, nick is optional";
|
||||
|
||||
pub fn no_emo(_: regex::Captures, message: &Message, _: &VecDeque<Message>) -> String {
|
||||
let complete_message = format!("{} thinks you shouldn't be so emo. Take a deep breath and lighten up",
|
||||
pub fn no_emo(captures : regex::Captures, message: &Message, _: &VecDeque<Message>) -> String {
|
||||
let complete_message;
|
||||
if let Some(person) = captures.get(2) {
|
||||
complete_message = format!("{}: Don't be so emo. Take a deep breath and lighten up", person.as_str());
|
||||
}
|
||||
else {
|
||||
complete_message = format!("{} thinks you shouldn't be so emo. Take a deep breath and lighten up",
|
||||
message.source_nickname().unwrap_or("unknown_nick").to_string());
|
||||
}
|
||||
complete_message
|
||||
}
|
||||
|
||||
|
||||
@@ -1,17 +1,27 @@
|
||||
use irc::proto::Message;
|
||||
use std::collections::VecDeque;
|
||||
|
||||
pub const PATTERN: &str = "^\\$rtf([cm])$";
|
||||
pub const PATTERN: &str = "^\\$rtf([cm])( (?P<nick>[^\\s]+))?$";
|
||||
pub const NAME: &str = "rtfm";
|
||||
pub const USAGE: &str = "Usage: $rtf[cm]\r\nThis tells you read the f-ing manual or chat, whichever is chosen";
|
||||
pub const USAGE: &str = "Usage: $rtf[cm] <nick>\r\nThis tells you read the f-ing manual or chat, whichever is chosen";
|
||||
|
||||
pub fn rtfm(captures: regex::Captures, _: &Message, _: &VecDeque<Message>) -> String {
|
||||
let c_or_m = captures.get(1).unwrap().as_str();
|
||||
|
||||
if let Some(nick) = captures.get(2) {
|
||||
if c_or_m == "c" {
|
||||
format!("{}: Read the f-ing chat", nick.as_str())
|
||||
}
|
||||
else {
|
||||
format!("{}: Read the f-ing manual", nick.as_str())
|
||||
}
|
||||
}
|
||||
else {
|
||||
if c_or_m == "c" {
|
||||
"Read the f-ing chat".to_string()
|
||||
}
|
||||
else {
|
||||
"Read the f-ing manual".to_string()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
110
src/modules/time_to_date.rs
Normal file
110
src/modules/time_to_date.rs
Normal file
@@ -0,0 +1,110 @@
|
||||
use irc::proto::Message;
|
||||
use std::collections::VecDeque;
|
||||
use std::time::Duration;
|
||||
use chrono::prelude::*;
|
||||
use humantime::{self, parse_rfc3339_weak};
|
||||
|
||||
pub const PATTERN: &str = "^\\$ttd\\s*(.*)$";
|
||||
pub const NAME: &str = "ttd";
|
||||
pub const USAGE: &str = "Usage $ttd <datetime>\r\nThis prints the amount of time until the specified datetime";
|
||||
|
||||
pub fn time_to_date(captures: regex::Captures, _: &Message, _: &VecDeque<Message>) -> String {
|
||||
let time_string = captures.get(1).unwrap().as_str();
|
||||
//Dates + Times (numeric)
|
||||
let requested_time = if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%D %R") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%D %I:%M %P") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%D %I:%M %p") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%m/%d/%Y %R") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%m/%d/%Y %I:%M %P") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%m/%d/%Y %I:%M %p") {
|
||||
date_time
|
||||
}
|
||||
//Dates (numeric)
|
||||
else if let Ok(date) = NaiveDate::parse_from_str(time_string, "%D") {
|
||||
NaiveDateTime::from(date)
|
||||
}
|
||||
else if let Ok(date) = NaiveDate::parse_from_str(time_string, "%m/%d/%Y") {
|
||||
NaiveDateTime::from(date)
|
||||
}
|
||||
else if let Ok(date) = NaiveDate::parse_from_str(time_string, "%F") {
|
||||
NaiveDateTime::from(date)
|
||||
}
|
||||
//Time (24hour)
|
||||
else if let Ok(time) = NaiveTime::parse_from_str(time_string, "%R") {
|
||||
Local::now().naive_local().date().and_time(time)
|
||||
}
|
||||
//Time (12hour)
|
||||
else if let Ok(time) = NaiveTime::parse_from_str(time_string, "%I:%M %P") {
|
||||
Local::now().naive_local().date().and_time(time)
|
||||
}
|
||||
else if let Ok(time) = NaiveTime::parse_from_str(time_string, "%I:%M %p") {
|
||||
Local::now().naive_local().date().and_time(time)
|
||||
}
|
||||
//Date + Time (written)
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%v %R") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%v %I:%M %P") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%v %I:%M %p") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%B %d, %y %R") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%B %d, %y %I:%M %p") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%B %d, %y %I:%M %P") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%B %d, %Y %R") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%B %d, %Y %I:%M %p") {
|
||||
date_time
|
||||
}
|
||||
else if let Ok(date_time) = NaiveDateTime::parse_from_str(time_string, "%B %d, %Y %I:%M %P") {
|
||||
date_time
|
||||
}
|
||||
//Date (written)
|
||||
else if let Ok(date) = NaiveDate::parse_from_str(time_string, "%v") {
|
||||
NaiveDateTime::from(date)
|
||||
}
|
||||
else if let Ok(date) = NaiveDate::parse_from_str(time_string, "%B %d, %y") {
|
||||
NaiveDateTime::from(date)
|
||||
}
|
||||
else if let Ok(date) = NaiveDate::parse_from_str(time_string, "%B %d, %Y") {
|
||||
NaiveDateTime::from(date)
|
||||
}
|
||||
//RFC3339 datetime
|
||||
else if let Ok(date_time) = parse_rfc3339_weak(time_string) {
|
||||
let tmp: DateTime<Local> = date_time.into();
|
||||
tmp.naive_local()
|
||||
}
|
||||
else {
|
||||
return "Invalid date given".to_string();
|
||||
};
|
||||
|
||||
let current_time = Local::now().naive_local();
|
||||
|
||||
let difference = requested_time - current_time;
|
||||
let human_difference = humantime::format_duration(Duration::from_secs(difference.num_seconds().abs() as u64));
|
||||
if difference.num_seconds() < 0 {
|
||||
format!("Was {} ago",human_difference.to_string())
|
||||
}
|
||||
else {
|
||||
format!("Is in {}", human_difference.to_string())
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
use irc::proto::Message;
|
||||
use std::collections::VecDeque;
|
||||
use chrono::{prelude::*, TimeDelta};
|
||||
use std::time::Duration;
|
||||
use chrono::prelude::*;
|
||||
use humantime;
|
||||
|
||||
pub const PATTERN: &str = "^\\$ttb\\s*$";
|
||||
pub const NAME: &str = "ttb";
|
||||
@@ -10,17 +12,14 @@ pub const USAGE: &str = "Usage $ttb\r\nThis prints the number of days until pnut
|
||||
pub fn time_to_baby(_: regex::Captures, _: &Message, _: &VecDeque<Message>) -> String {
|
||||
let local_time: DateTime<Local> = Local::now();
|
||||
|
||||
let birth_time: DateTime<Local> = Local.with_ymd_and_hms(2024, 10, 17, 00, 00, 00).unwrap();
|
||||
let birth_time: DateTime<Local> = Local.with_ymd_and_hms(2024, 10, 8, 00, 00, 00).unwrap();
|
||||
|
||||
let difference = birth_time - local_time;
|
||||
let difference = local_time - birth_time;
|
||||
|
||||
let completed_message;
|
||||
if difference > TimeDelta::zero() {
|
||||
completed_message = format!("{} {} until pnutz's baby is due!", difference.num_days(), if difference.num_days() > 1 { "days"} else {"day"} );
|
||||
}
|
||||
else {
|
||||
completed_message = "They're past due!".to_string();
|
||||
}
|
||||
|
||||
let human_difference = humantime::format_duration(Duration::from_secs(difference.num_seconds() as u64));
|
||||
completed_message = format!("He's {} old!", human_difference.to_string());
|
||||
|
||||
|
||||
completed_message
|
||||
|
||||
Reference in New Issue
Block a user