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 comrak;
15 pub extern crate dotenv;
16 pub extern crate jsonwebtoken;
17 pub extern crate lettre;
18 pub extern crate lettre_email;
20 pub extern crate openssl;
21 pub extern crate rand;
22 pub extern crate regex;
24 pub extern crate serde;
25 pub extern crate serde_json;
26 pub extern crate sha2;
27 pub extern crate strum;
39 use crate::settings::Settings;
40 use actix_web::dev::ConnectionInfo;
41 use chrono::{DateTime, FixedOffset, Local, NaiveDateTime, Utc};
42 use itertools::Itertools;
45 authentication::{Credentials, Mechanism},
47 ConnectionReuseParameters,
53 use lettre_email::Email;
55 use percent_encoding::{utf8_percent_encode, NON_ALPHANUMERIC};
56 use rand::{distributions::Alphanumeric, thread_rng, Rng};
57 use regex::{Regex, RegexBuilder};
58 use serde::Deserialize;
60 pub type ConnectionId = usize;
61 pub type PostId = i32;
62 pub type CommunityId = i32;
63 pub type UserId = i32;
64 pub type IPAddr = String;
66 pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> {
67 DateTime::<Utc>::from_utc(ndt, Utc)
70 pub fn naive_now() -> NaiveDateTime {
71 chrono::prelude::Utc::now().naive_utc()
74 pub fn naive_from_unix(time: i64) -> NaiveDateTime {
75 NaiveDateTime::from_timestamp(time, 0)
78 pub fn convert_datetime(datetime: NaiveDateTime) -> DateTime<FixedOffset> {
79 let now = Local::now();
80 DateTime::<FixedOffset>::from_utc(datetime, *now.offset())
83 pub fn is_email_regex(test: &str) -> bool {
84 EMAIL_REGEX.is_match(test)
87 pub fn is_image_content_type(test: &str) -> Result<(), failure::Error> {
88 if attohttpc::get(test)
92 .ok_or_else(|| format_err!("No Content-Type header"))?
94 .starts_with("image/")
98 Err(format_err!("Not an image type."))
102 pub fn remove_slurs(test: &str) -> String {
103 SLUR_REGEX.replace_all(test, "*removed*").to_string()
106 pub fn slur_check(test: &str) -> Result<(), Vec<&str>> {
107 let mut matches: Vec<&str> = SLUR_REGEX.find_iter(test).map(|mat| mat.as_str()).collect();
110 matches.sort_unstable();
113 if matches.is_empty() {
120 pub fn slurs_vec_to_str(slurs: Vec<&str>) -> String {
121 let start = "No slurs - ";
122 let combined = &slurs.join(", ");
123 [start, combined].concat()
126 pub fn generate_random_string() -> String {
127 thread_rng().sample_iter(&Alphanumeric).take(30).collect()
135 ) -> Result<(), String> {
136 let email_config = Settings::get().email.ok_or("no_email_setup")?;
138 let email = Email::builder()
139 .to((to_email, to_username))
140 .from(email_config.smtp_from_address.to_owned())
146 let mailer = if email_config.use_tls {
147 SmtpClient::new_simple(&email_config.smtp_server).unwrap()
149 SmtpClient::new(&email_config.smtp_server, ClientSecurity::None).unwrap()
151 .hello_name(ClientId::Domain(Settings::get().hostname))
153 .authentication_mechanism(Mechanism::Plain)
154 .connection_reuse(ConnectionReuseParameters::ReuseUnlimited);
155 let mailer = if let (Some(login), Some(password)) =
156 (&email_config.smtp_login, &email_config.smtp_password)
158 mailer.credentials(Credentials::new(login.to_owned(), password.to_owned()))
163 let mut transport = mailer.transport();
164 let result = transport.send(email.into());
169 Err(e) => Err(e.to_string()),
173 #[derive(Deserialize, Debug)]
174 pub struct IframelyResponse {
175 title: Option<String>,
176 description: Option<String>,
177 thumbnail_url: Option<String>,
178 html: Option<String>,
181 pub fn fetch_iframely(url: &str) -> Result<IframelyResponse, failure::Error> {
182 let fetch_url = format!("http://iframely/oembed?url={}", url);
183 let text: String = attohttpc::get(&fetch_url).send()?.text()?;
184 let res: IframelyResponse = serde_json::from_str(&text)?;
188 #[derive(Deserialize, Debug, Clone)]
189 pub struct PictrsResponse {
190 files: Vec<PictrsFile>,
194 #[derive(Deserialize, Debug, Clone)]
195 pub struct PictrsFile {
197 delete_token: String,
200 pub fn fetch_pictrs(image_url: &str) -> Result<PictrsResponse, failure::Error> {
201 is_image_content_type(image_url)?;
203 let fetch_url = format!(
204 "http://pictrs:8080/image/download?url={}",
205 utf8_percent_encode(image_url, NON_ALPHANUMERIC) // TODO this might not be needed
207 let text = attohttpc::get(&fetch_url).send()?.text()?;
208 let res: PictrsResponse = serde_json::from_str(&text)?;
212 Err(format_err!("{}", &res.msg))
216 fn fetch_iframely_and_pictrs_data(
226 // Fetch iframely data
227 let (iframely_title, iframely_description, iframely_thumbnail_url, iframely_html) =
228 match fetch_iframely(url) {
229 Ok(res) => (res.title, res.description, res.thumbnail_url, res.html),
231 error!("iframely err: {}", e);
232 (None, None, None, None)
236 // Fetch pictrs thumbnail
237 let pictrs_thumbnail = match iframely_thumbnail_url {
238 Some(iframely_thumbnail_url) => match fetch_pictrs(&iframely_thumbnail_url) {
239 Ok(res) => Some(res.files[0].file.to_owned()),
241 error!("pictrs err: {}", e);
245 // Try to generate a small thumbnail if iframely is not supported
246 None => match fetch_pictrs(&url) {
247 Ok(res) => Some(res.files[0].file.to_owned()),
249 error!("pictrs err: {}", e);
257 iframely_description,
262 None => (None, None, None, None),
266 pub fn markdown_to_html(text: &str) -> String {
267 comrak::markdown_to_html(text, &comrak::ComrakOptions::default())
270 pub fn get_ip(conn_info: &ConnectionInfo) -> String {
273 .unwrap_or("127.0.0.1:12345")
276 .unwrap_or("127.0.0.1")
280 // TODO nothing is done with community / group webfingers yet, so just ignore those for now
281 #[derive(Clone, PartialEq, Eq, Hash)]
282 pub struct MentionData {
288 pub fn is_local(&self) -> bool {
289 Settings::get().hostname.eq(&self.domain)
291 pub fn full_name(&self) -> String {
292 format!("@{}@{}", &self.name, &self.domain)
296 pub fn scrape_text_for_mentions(text: &str) -> Vec<MentionData> {
297 let mut out: Vec<MentionData> = Vec::new();
298 for caps in WEBFINGER_USER_REGEX.captures_iter(text) {
299 out.push(MentionData {
300 name: caps["name"].to_string(),
301 domain: caps["domain"].to_string(),
304 out.into_iter().unique().collect()
307 pub fn is_valid_username(name: &str) -> bool {
308 VALID_USERNAME_REGEX.is_match(name)
311 pub fn is_valid_community_name(name: &str) -> bool {
312 VALID_COMMUNITY_NAME_REGEX.is_match(name)
319 is_image_content_type,
320 is_valid_community_name,
323 scrape_text_for_mentions,
329 fn test_mentions_regex() {
330 let text = "Just read a great blog post by [@tedu@honk.teduangst.com](/u/test). And another by !test_community@fish.teduangst.com . Another [@lemmy@lemmy_alpha:8540](/u/fish)";
331 let mentions = scrape_text_for_mentions(text);
333 assert_eq!(mentions[0].name, "tedu".to_string());
334 assert_eq!(mentions[0].domain, "honk.teduangst.com".to_string());
335 assert_eq!(mentions[1].domain, "lemmy_alpha:8540".to_string());
340 assert!(is_image_content_type("https://1734811051.rsc.cdn77.org/data/images/full/365645/as-virus-kills-navajos-in-their-homes-tribal-women-provide-lifeline.jpg?w=600?w=650").is_ok());
341 assert!(is_image_content_type(
342 "https://twitter.com/BenjaminNorton/status/1259922424272957440?s=20"
349 assert!(is_email_regex("gush@gmail.com"));
350 assert!(!is_email_regex("nada_neutho"));
354 fn test_valid_register_username() {
355 assert!(is_valid_username("Hello_98"));
356 assert!(is_valid_username("ten"));
357 assert!(!is_valid_username("Hello-98"));
358 assert!(!is_valid_username("a"));
359 assert!(!is_valid_username(""));
363 fn test_valid_community_name() {
364 assert!(is_valid_community_name("example"));
365 assert!(is_valid_community_name("example_community"));
366 assert!(!is_valid_community_name("Example"));
367 assert!(!is_valid_community_name("Ex"));
368 assert!(!is_valid_community_name(""));
372 fn test_slur_filter() {
374 "coons test dindu ladyboy tranny retardeds. Capitalized Niggerz. This is a bunch of other safe text.";
375 let slur_free = "No slurs here";
378 "*removed* test *removed* *removed* *removed* *removed*. Capitalized *removed*. This is a bunch of other safe text."
382 let has_slurs_vec = vec![
390 let has_slurs_err_str = "No slurs - Niggerz, coons, dindu, ladyboy, retardeds, tranny";
392 assert_eq!(slur_check(test), Err(has_slurs_vec));
393 assert_eq!(slur_check(slur_free), Ok(()));
394 if let Err(slur_vec) = slur_check(test) {
395 assert_eq!(&slurs_vec_to_str(slur_vec), has_slurs_err_str);
399 // These helped with testing
401 // fn test_iframely() {
402 // let res = fetch_iframely("https://www.redspark.nu/?p=15341");
403 // assert!(res.is_ok());
407 // fn test_pictshare() {
408 // let res = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpg");
409 // assert!(res.is_ok());
410 // let res_other = fetch_pictshare("https://upload.wikimedia.org/wikipedia/en/2/27/The_Mandalorian_logo.jpgaoeu");
411 // assert!(res_other.is_err());
415 // fn test_send_email() {
416 // let result = send_email("not a subject", "test_email@gmail.com", "ur user", "<h1>HI there</h1>");
417 // assert!(result.is_ok());
422 static ref EMAIL_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
423 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();
424 static ref USERNAME_MATCHES_REGEX: Regex = Regex::new(r"/u/[a-zA-Z][0-9a-zA-Z_]*").unwrap();
425 // TODO keep this old one, it didn't work with port well tho
426 // static ref WEBFINGER_USER_REGEX: Regex = Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._-]+\.[a-zA-Z0-9_-]+)").unwrap();
427 static ref WEBFINGER_USER_REGEX: Regex = Regex::new(r"@(?P<name>[\w.]+)@(?P<domain>[a-zA-Z0-9._:-]+)").unwrap();
428 static ref VALID_USERNAME_REGEX: Regex = Regex::new(r"^[a-zA-Z0-9_]{3,20}$").unwrap();
429 static ref VALID_COMMUNITY_NAME_REGEX: Regex = Regex::new(r"^[a-z0-9_]{3,20}$").unwrap();