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 crypto;
15 pub extern crate dotenv;
16 pub extern crate jsonwebtoken;
17 pub extern crate lettre;
18 pub extern crate lettre_email;
19 pub extern crate rand;
20 pub extern crate regex;
21 pub extern crate serde;
22 pub extern crate serde_json;
23 pub extern crate strum;
36 use crate::settings::Settings;
37 use chrono::{DateTime, NaiveDateTime, Utc};
38 use lettre::smtp::authentication::{Credentials, Mechanism};
39 use lettre::smtp::extension::ClientId;
40 use lettre::smtp::ConnectionReuseParameters;
41 use lettre::{SmtpClient, Transport};
42 use lettre_email::Email;
43 use rand::distributions::Alphanumeric;
44 use rand::{thread_rng, Rng};
47 pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> {
48 DateTime::<Utc>::from_utc(ndt, Utc)
51 pub fn naive_now() -> NaiveDateTime {
52 chrono::prelude::Utc::now().naive_utc()
55 pub fn naive_from_unix(time: i64) -> NaiveDateTime {
56 NaiveDateTime::from_timestamp(time, 0)
59 pub fn is_email_regex(test: &str) -> bool {
60 EMAIL_REGEX.is_match(test)
63 pub fn remove_slurs(test: &str) -> String {
64 SLUR_REGEX.replace_all(test, "*removed*").to_string()
67 pub fn has_slurs(test: &str) -> bool {
68 SLUR_REGEX.is_match(test)
71 pub fn extract_usernames(test: &str) -> Vec<&str> {
72 let mut matches: Vec<&str> = USERNAME_MATCHES_REGEX
74 .map(|mat| mat.as_str())
78 matches.sort_unstable();
82 matches.iter().map(|t| &t[3..]).collect()
85 pub fn generate_random_string() -> String {
86 thread_rng().sample_iter(&Alphanumeric).take(30).collect()
94 ) -> Result<(), String> {
95 let email_config = Settings::get().email.as_ref().ok_or("no_email_setup")?;
97 let email = Email::builder()
98 .to((to_email, to_username))
100 email_config.smtp_login.to_owned(),
101 email_config.smtp_from_address.to_owned(),
108 let mut mailer = SmtpClient::new_simple(&email_config.smtp_server)
110 .hello_name(ClientId::Domain("localhost".to_string()))
111 .credentials(Credentials::new(
112 email_config.smtp_login.to_owned(),
113 email_config.smtp_password.to_owned(),
116 .authentication_mechanism(Mechanism::Plain)
117 .connection_reuse(ConnectionReuseParameters::ReuseUnlimited)
120 let result = mailer.send(email.into());
124 Err(_) => Err("no_email_setup".to_string()),
130 use crate::{extract_usernames, has_slurs, is_email_regex, remove_slurs, Settings};
134 assert!(is_email_regex("gush@gmail.com"));
135 assert!(!is_email_regex("nada_neutho"));
139 fn test_slur_filter() {
141 "coons test dindu ladyboy tranny retardeds. This is a bunch of other safe text.".to_string();
142 let slur_free = "No slurs here";
145 "*removed* test *removed* *removed* *removed* *removed*. This is a bunch of other safe text."
148 assert!(has_slurs(&test));
149 assert!(!has_slurs(slur_free));
153 fn test_extract_usernames() {
154 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");
155 let expected = vec!["another", "testme"];
156 assert_eq!(usernames, expected);
160 // fn test_send_email() {
161 // let result = send_email("not a subject", "test_email@gmail.com", "ur user", "<h1>HI there</h1>");
162 // assert!(result.is_ok());
167 static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
168 static ref SLUR_REGEX: Regex = Regex::new(r"(fag(g|got|tard)?|maricos?|cock\s?sucker(s|ing)?|nig(\b|g?(a|er)?s?)\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?)").unwrap();
169 static ref USERNAME_MATCHES_REGEX: Regex = Regex::new(r"/u/[a-zA-Z][0-9a-zA-Z_]*").unwrap();