]> Untitled Git - lemmy.git/blob - server/src/lib.rs
2391449caf818155ebc3c500c6d15537030259d8
[lemmy.git] / server / src / lib.rs
1 #![recursion_limit = "512"]
2 #[macro_use]
3 pub extern crate strum_macros;
4 #[macro_use]
5 pub extern crate lazy_static;
6 #[macro_use]
7 pub extern crate failure;
8 #[macro_use]
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;
19 extern crate log;
20 pub extern crate openssl;
21 pub extern crate rand;
22 pub extern crate regex;
23 pub extern crate rss;
24 pub extern crate serde;
25 pub extern crate serde_json;
26 pub extern crate sha2;
27 pub extern crate strum;
28
29 pub mod api;
30 pub mod apub;
31 pub mod db;
32 pub mod rate_limit;
33 pub mod routes;
34 pub mod schema;
35 pub mod settings;
36 pub mod version;
37 pub mod websocket;
38
39 use crate::settings::Settings;
40 use actix_web::dev::ConnectionInfo;
41 use chrono::{DateTime, FixedOffset, Local, NaiveDateTime, Utc};
42 use itertools::Itertools;
43 use lettre::{
44   smtp::{
45     authentication::{Credentials, Mechanism},
46     extension::ClientId,
47     ConnectionReuseParameters,
48   },
49   ClientSecurity,
50   SmtpClient,
51   Transport,
52 };
53 use lettre_email::Email;
54 use log::error;
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;
59
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;
65
66 pub fn to_datetime_utc(ndt: NaiveDateTime) -> DateTime<Utc> {
67   DateTime::<Utc>::from_utc(ndt, Utc)
68 }
69
70 pub fn naive_now() -> NaiveDateTime {
71   chrono::prelude::Utc::now().naive_utc()
72 }
73
74 pub fn naive_from_unix(time: i64) -> NaiveDateTime {
75   NaiveDateTime::from_timestamp(time, 0)
76 }
77
78 pub fn convert_datetime(datetime: NaiveDateTime) -> DateTime<FixedOffset> {
79   let now = Local::now();
80   DateTime::<FixedOffset>::from_utc(datetime, *now.offset())
81 }
82
83 pub fn is_email_regex(test: &str) -> bool {
84   EMAIL_REGEX.is_match(test)
85 }
86
87 pub fn is_image_content_type(test: &str) -> Result<(), failure::Error> {
88   if attohttpc::get(test)
89     .send()?
90     .headers()
91     .get("Content-Type")
92     .ok_or_else(|| format_err!("No Content-Type header"))?
93     .to_str()?
94     .starts_with("image/")
95   {
96     Ok(())
97   } else {
98     Err(format_err!("Not an image type."))
99   }
100 }
101
102 pub fn remove_slurs(test: &str) -> String {
103   SLUR_REGEX.replace_all(test, "*removed*").to_string()
104 }
105
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();
108
109   // Unique
110   matches.sort_unstable();
111   matches.dedup();
112
113   if matches.is_empty() {
114     Ok(())
115   } else {
116     Err(matches)
117   }
118 }
119
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()
124 }
125
126 pub fn generate_random_string() -> String {
127   thread_rng().sample_iter(&Alphanumeric).take(30).collect()
128 }
129
130 pub fn send_email(
131   subject: &str,
132   to_email: &str,
133   to_username: &str,
134   html: &str,
135 ) -> Result<(), String> {
136   let email_config = Settings::get().email.ok_or("no_email_setup")?;
137
138   let email = Email::builder()
139     .to((to_email, to_username))
140     .from(email_config.smtp_from_address.to_owned())
141     .subject(subject)
142     .html(html)
143     .build()
144     .unwrap();
145
146   let mailer = if email_config.use_tls {
147     SmtpClient::new_simple(&email_config.smtp_server).unwrap()
148   } else {
149     SmtpClient::new(&email_config.smtp_server, ClientSecurity::None).unwrap()
150   }
151   .hello_name(ClientId::Domain(Settings::get().hostname))
152   .smtp_utf8(true)
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)
157   {
158     mailer.credentials(Credentials::new(login.to_owned(), password.to_owned()))
159   } else {
160     mailer
161   };
162
163   let mut transport = mailer.transport();
164   let result = transport.send(email.into());
165   transport.close();
166
167   match result {
168     Ok(_) => Ok(()),
169     Err(e) => Err(e.to_string()),
170   }
171 }
172
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>,
179 }
180
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)?;
185   Ok(res)
186 }
187
188 #[derive(Deserialize, Debug, Clone)]
189 pub struct PictrsResponse {
190   files: Vec<PictrsFile>,
191   msg: String,
192 }
193
194 #[derive(Deserialize, Debug, Clone)]
195 pub struct PictrsFile {
196   file: String,
197   delete_token: String,
198 }
199
200 pub fn fetch_pictrs(image_url: &str) -> Result<PictrsResponse, failure::Error> {
201   is_image_content_type(image_url)?;
202
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
206   );
207   let text = attohttpc::get(&fetch_url).send()?.text()?;
208   let res: PictrsResponse = serde_json::from_str(&text)?;
209   if res.msg == "ok" {
210     Ok(res)
211   } else {
212     Err(format_err!("{}", &res.msg))
213   }
214 }
215
216 fn fetch_iframely_and_pictrs_data(
217   url: Option<String>,
218 ) -> (
219   Option<String>,
220   Option<String>,
221   Option<String>,
222   Option<String>,
223 ) {
224   match &url {
225     Some(url) => {
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),
230           Err(e) => {
231             error!("iframely err: {}", e);
232             (None, None, None, None)
233           }
234         };
235
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()),
240           Err(e) => {
241             error!("pictrs err: {}", e);
242             None
243           }
244         },
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()),
248           Err(e) => {
249             error!("pictrs err: {}", e);
250             None
251           }
252         },
253       };
254
255       (
256         iframely_title,
257         iframely_description,
258         iframely_html,
259         pictrs_thumbnail,
260       )
261     }
262     None => (None, None, None, None),
263   }
264 }
265
266 pub fn markdown_to_html(text: &str) -> String {
267   comrak::markdown_to_html(text, &comrak::ComrakOptions::default())
268 }
269
270 pub fn get_ip(conn_info: &ConnectionInfo) -> String {
271   conn_info
272     .remote()
273     .unwrap_or("127.0.0.1:12345")
274     .split(':')
275     .next()
276     .unwrap_or("127.0.0.1")
277     .to_string()
278 }
279
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 {
283   pub name: String,
284   pub domain: String,
285 }
286
287 impl MentionData {
288   pub fn is_local(&self) -> bool {
289     Settings::get().hostname.eq(&self.domain)
290   }
291   pub fn full_name(&self) -> String {
292     format!("@{}@{}", &self.name, &self.domain)
293   }
294 }
295
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(),
302     });
303   }
304   out.into_iter().unique().collect()
305 }
306
307 pub fn is_valid_username(name: &str) -> bool {
308   VALID_USERNAME_REGEX.is_match(name)
309 }
310
311 pub fn is_valid_community_name(name: &str) -> bool {
312   VALID_COMMUNITY_NAME_REGEX.is_match(name)
313 }
314
315 #[cfg(test)]
316 mod tests {
317   use crate::{
318     is_email_regex,
319     is_image_content_type,
320     is_valid_community_name,
321     is_valid_username,
322     remove_slurs,
323     scrape_text_for_mentions,
324     slur_check,
325     slurs_vec_to_str,
326   };
327
328   #[test]
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);
332
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());
336   }
337
338   #[test]
339   fn test_image() {
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"
343     )
344     .is_err());
345   }
346
347   #[test]
348   fn test_email() {
349     assert!(is_email_regex("gush@gmail.com"));
350     assert!(!is_email_regex("nada_neutho"));
351   }
352
353   #[test]
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(""));
360   }
361
362   #[test]
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(""));
369   }
370
371   #[test]
372   fn test_slur_filter() {
373     let test =
374       "coons test dindu ladyboy tranny retardeds. Capitalized Niggerz. This is a bunch of other safe text.";
375     let slur_free = "No slurs here";
376     assert_eq!(
377       remove_slurs(&test),
378       "*removed* test *removed* *removed* *removed* *removed*. Capitalized *removed*. This is a bunch of other safe text."
379         .to_string()
380     );
381
382     let has_slurs_vec = vec![
383       "Niggerz",
384       "coons",
385       "dindu",
386       "ladyboy",
387       "retardeds",
388       "tranny",
389     ];
390     let has_slurs_err_str = "No slurs - Niggerz, coons, dindu, ladyboy, retardeds, tranny";
391
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);
396     }
397   }
398
399   // These helped with testing
400   // #[test]
401   // fn test_iframely() {
402   //   let res = fetch_iframely("https://www.redspark.nu/?p=15341");
403   //   assert!(res.is_ok());
404   // }
405
406   // #[test]
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());
412   // }
413
414   // #[test]
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());
418   // }
419 }
420
421 lazy_static! {
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();
430 }