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;
33 use chrono::{DateTime, NaiveDateTime, Utc};
35 use lettre::smtp::authentication::{Credentials, Mechanism};
36 use lettre::smtp::extension::ClientId;
37 use lettre::smtp::ConnectionReuseParameters;
38 use lettre::{SmtpClient, Transport};
39 use lettre_email::Email;
40 use rand::distributions::Alphanumeric;
41 use rand::{thread_rng, Rng};
49 rate_limit_message: i32,
50 rate_limit_message_per_second: i32,
52 rate_limit_post_per_second: i32,
53 rate_limit_register: i32,
54 rate_limit_register_per_second: i32,
55 email_config: Option<EmailConfig>,
58 pub struct EmailConfig {
61 smtp_password: String,
62 smtp_from_address: String,
70 if env::var("SMTP_SERVER").is_ok() && !env::var("SMTP_SERVER").unwrap().eq("") {
72 smtp_server: env::var("SMTP_SERVER").expect("SMTP_SERVER must be set"),
73 smtp_login: env::var("SMTP_LOGIN").expect("SMTP_LOGIN must be set"),
74 smtp_password: env::var("SMTP_PASSWORD").expect("SMTP_PASSWORD must be set"),
75 smtp_from_address: env::var("SMTP_FROM_ADDRESS").expect("SMTP_FROM_ADDRESS must be set"),
82 db_url: env::var("DATABASE_URL").expect("DATABASE_URL must be set"),
83 hostname: env::var("HOSTNAME").unwrap_or("rrr".to_string()),
84 jwt_secret: env::var("JWT_SECRET").unwrap_or("changeme".to_string()),
85 rate_limit_message: env::var("RATE_LIMIT_MESSAGE")
86 .unwrap_or("30".to_string())
89 rate_limit_message_per_second: env::var("RATE_LIMIT_MESSAGE_PER_SECOND")
90 .unwrap_or("60".to_string())
93 rate_limit_post: env::var("RATE_LIMIT_POST")
94 .unwrap_or("3".to_string())
97 rate_limit_post_per_second: env::var("RATE_LIMIT_POST_PER_SECOND")
98 .unwrap_or("600".to_string())
101 rate_limit_register: env::var("RATE_LIMIT_REGISTER")
102 .unwrap_or("1".to_string())
105 rate_limit_register_per_second: env::var("RATE_LIMIT_REGISTER_PER_SECOND")
106 .unwrap_or("3600".to_string())
109 email_config: email_config,
112 fn api_endpoint(&self) -> String {
113 format!("{}/api/v1", self.hostname)
117 pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> {
118 DateTime::<Utc>::from_utc(ndt, Utc)
121 pub fn naive_now() -> NaiveDateTime {
122 chrono::prelude::Utc::now().naive_utc()
125 pub fn naive_from_unix(time: i64) -> NaiveDateTime {
126 NaiveDateTime::from_timestamp(time, 0)
129 pub fn is_email_regex(test: &str) -> bool {
130 EMAIL_REGEX.is_match(test)
133 pub fn remove_slurs(test: &str) -> String {
134 SLUR_REGEX.replace_all(test, "*removed*").to_string()
137 pub fn has_slurs(test: &str) -> bool {
138 SLUR_REGEX.is_match(test)
141 pub fn extract_usernames(test: &str) -> Vec<&str> {
142 let mut matches: Vec<&str> = USERNAME_MATCHES_REGEX
144 .map(|mat| mat.as_str())
148 matches.sort_unstable();
152 matches.iter().map(|t| &t[3..]).collect()
155 pub fn generate_random_string() -> String {
156 thread_rng().sample_iter(&Alphanumeric).take(30).collect()
164 ) -> Result<(), String> {
165 let email_config = Settings::get().email_config.ok_or("no_email_setup")?;
167 let email = Email::builder()
168 .to((to_email, to_username))
170 email_config.smtp_login.to_owned(),
171 email_config.smtp_from_address,
178 let mut mailer = SmtpClient::new_simple(&email_config.smtp_server)
180 .hello_name(ClientId::Domain("localhost".to_string()))
181 .credentials(Credentials::new(
182 email_config.smtp_login.to_owned(),
183 email_config.smtp_password.to_owned(),
186 .authentication_mechanism(Mechanism::Plain)
187 .connection_reuse(ConnectionReuseParameters::ReuseUnlimited)
190 let result = mailer.send(email.into());
194 Err(_) => Err("no_email_setup".to_string()),
200 use crate::{extract_usernames, has_slurs, is_email_regex, remove_slurs, Settings};
203 assert_eq!(Settings::get().api_endpoint(), "rrr/api/v1");
208 assert!(is_email_regex("gush@gmail.com"));
209 assert!(!is_email_regex("nada_neutho"));
213 fn test_slur_filter() {
215 "coons test dindu ladyboy tranny retardeds. This is a bunch of other safe text.".to_string();
216 let slur_free = "No slurs here";
219 "*removed* test *removed* *removed* *removed* *removed*. This is a bunch of other safe text."
222 assert!(has_slurs(&test));
223 assert!(!has_slurs(slur_free));
227 fn test_extract_usernames() {
228 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");
229 let expected = vec!["another", "testme"];
230 assert_eq!(usernames, expected);
234 // fn test_send_email() {
235 // let result = send_email("not a subject", "test_email@gmail.com", "ur user", "<h1>HI there</h1>");
236 // assert!(result.is_ok());
241 static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
242 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();
243 static ref USERNAME_MATCHES_REGEX: Regex = Regex::new(r"/u/[a-zA-Z][0-9a-zA-Z_]*").unwrap();