1 #![recursion_limit = "512"]
3 pub extern crate strum_macros;
5 pub extern crate lazy_static;
7 pub extern crate failure;
9 pub extern crate diesel;
10 pub extern crate actix;
11 pub extern crate actix_web;
12 pub extern crate bcrypt;
13 pub extern crate chrono;
14 pub extern crate dotenv;
15 pub extern crate jsonwebtoken;
16 pub extern crate lettre;
17 pub extern crate lettre_email;
18 pub extern crate rand;
19 pub extern crate regex;
20 pub extern crate serde;
21 pub extern crate serde_json;
22 pub extern crate sha2;
23 pub extern crate strum;
34 use crate::settings::Settings;
35 use chrono::{DateTime, NaiveDateTime, Utc};
36 use lettre::smtp::authentication::{Credentials, Mechanism};
37 use lettre::smtp::extension::ClientId;
38 use lettre::smtp::ConnectionReuseParameters;
39 use lettre::{ClientSecurity, SmtpClient, Transport};
40 use lettre_email::Email;
41 use rand::distributions::Alphanumeric;
42 use rand::{thread_rng, Rng};
43 use regex::{Regex, RegexBuilder};
45 pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> {
46 DateTime::<Utc>::from_utc(ndt, Utc)
49 pub fn naive_now() -> NaiveDateTime {
50 chrono::prelude::Utc::now().naive_utc()
53 pub fn naive_from_unix(time: i64) -> NaiveDateTime {
54 NaiveDateTime::from_timestamp(time, 0)
57 pub fn is_email_regex(test: &str) -> bool {
58 EMAIL_REGEX.is_match(test)
61 pub fn remove_slurs(test: &str) -> String {
62 SLUR_REGEX.replace_all(test, "*removed*").to_string()
65 pub fn slur_check(test: &str) -> Result<(), Vec<&str>> {
66 let mut matches: Vec<&str> = SLUR_REGEX.find_iter(test).map(|mat| mat.as_str()).collect();
69 matches.sort_unstable();
72 if matches.is_empty() {
79 pub fn slurs_vec_to_str(slurs: Vec<&str>) -> String {
80 let start = "No slurs - ";
81 let combined = &slurs.join(", ");
82 [start, combined].concat()
85 pub fn extract_usernames(test: &str) -> Vec<&str> {
86 let mut matches: Vec<&str> = USERNAME_MATCHES_REGEX
88 .map(|mat| mat.as_str())
92 matches.sort_unstable();
96 matches.iter().map(|t| &t[3..]).collect()
99 pub fn generate_random_string() -> String {
100 thread_rng().sample_iter(&Alphanumeric).take(30).collect()
108 ) -> Result<(), String> {
109 let email_config = Settings::get().email.as_ref().ok_or("no_email_setup")?;
111 let email = Email::builder()
112 .to((to_email, to_username))
113 .from(email_config.smtp_from_address.to_owned())
119 let mailer = if email_config.use_tls {
120 SmtpClient::new_simple(&email_config.smtp_server).unwrap()
122 SmtpClient::new(&email_config.smtp_server, ClientSecurity::None).unwrap()
124 .hello_name(ClientId::Domain(Settings::get().hostname.to_owned()))
126 .authentication_mechanism(Mechanism::Plain)
127 .connection_reuse(ConnectionReuseParameters::ReuseUnlimited);
128 let mailer = if let (Some(login), Some(password)) =
129 (&email_config.smtp_login, &email_config.smtp_password)
131 mailer.credentials(Credentials::new(login.to_owned(), password.to_owned()))
136 let mut transport = mailer.transport();
137 let result = transport.send(email.into());
142 Err(e) => Err(e.to_string()),
148 use crate::{extract_usernames, is_email_regex, remove_slurs, slur_check, slurs_vec_to_str};
152 assert!(is_email_regex("gush@gmail.com"));
153 assert!(!is_email_regex("nada_neutho"));
157 fn test_slur_filter() {
159 "coons test dindu ladyboy tranny retardeds. Capitalized Niggerz. This is a bunch of other safe text.";
160 let slur_free = "No slurs here";
163 "*removed* test *removed* *removed* *removed* *removed*. Capitalized *removed*. This is a bunch of other safe text."
167 let has_slurs_vec = vec![
175 let has_slurs_err_str = "No slurs - Niggerz, coons, dindu, ladyboy, retardeds, tranny";
177 assert_eq!(slur_check(test), Err(has_slurs_vec));
178 assert_eq!(slur_check(slur_free), Ok(()));
179 if let Err(slur_vec) = slur_check(test) {
180 assert_eq!(&slurs_vec_to_str(slur_vec), has_slurs_err_str);
185 fn test_extract_usernames() {
186 let usernames = extract_usernames("this is a user mention for [/u/testme](/u/testme) and thats all. Oh [/u/another](/u/another) user. And the first again [/u/testme](/u/testme) okay");
187 let expected = vec!["another", "testme"];
188 assert_eq!(usernames, expected);
192 // fn test_send_email() {
193 // let result = send_email("not a subject", "test_email@gmail.com", "ur user", "<h1>HI there</h1>");
194 // assert!(result.is_ok());
199 static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
200 static ref SLUR_REGEX: Regex = RegexBuilder::new(r"(fag(g|got|tard)?|maricos?|cock\s?sucker(s|ing)?|nig(\b|g?(a|er)?(s|z)?)\b|dindu(s?)|mudslime?s?|kikes?|mongoloids?|towel\s*heads?|\bspi(c|k)s?\b|\bchinks?|niglets?|beaners?|\bnips?\b|\bcoons?\b|jungle\s*bunn(y|ies?)|jigg?aboo?s?|\bpakis?\b|rag\s*heads?|gooks?|cunts?|bitch(es|ing|y)?|puss(y|ies?)|twats?|feminazis?|whor(es?|ing)|\bslut(s|t?y)?|\btrann?(y|ies?)|ladyboy(s?)|\b(b|re|r)tard(ed)?s?)").case_insensitive(true).build().unwrap();
201 static ref USERNAME_MATCHES_REGEX: Regex = Regex::new(r"/u/[a-zA-Z][0-9a-zA-Z_]*").unwrap();