1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
use lettre::smtp::authentication::{Credentials, Mechanism};
use lettre::{EmailTransport, SmtpTransport};
use lettre_email::EmailBuilder;
use reqwest;
use std::collections::HashMap;
use config::{Notifications, Telegram};
use data::FormInput;
use errors::*;
use regex::Regex;
fn get_domain(host: &str) -> &str {
lazy_static! {
static ref URLPARSE: Regex = Regex::new(r"(?i)(https?)://([^\s/?#_:]+\.?)+:?(\d+)?(/[^\s]*)?$").unwrap();
}
let caps = URLPARSE.captures(host).unwrap();
caps.get(2).map_or("noreply", |m| m.as_str())
}
pub fn send_notification(
form: &FormInput,
notify: &Notifications,
host: &str,
blog_name: &str,
ip_addr: &str,
) -> Result<()> {
let post_url = format!("{}{}", host.trim_right_matches('/'), form.path);
let oration_addr = format!("oration@{}", get_domain(host));
let recipient_name = if notify.recipient.name == "~" {
"Oration Admin".to_string()
} else {
notify.recipient.name.to_owned()
};
let email = EmailBuilder::new()
.to((notify.recipient.email.to_owned(), recipient_name))
.from((oration_addr, "Oration Watchdog"))
.reply_to((form.sender_email(), form.sender_name()))
.subject(format!("A new comment has been posted on {}", blog_name))
.text(format!(
"A comment has been posted by {} on a post titled: {}.
The comment reads:
{}
You may reply on your blog post ({}), or if the user has left an email address, responding to this message will deliver them an email.
Debug information:
{:?}
Commenter's IP: {}",
form.sender_name(), form.title, form.comment, post_url, form, ip_addr))
.build()
.chain_err(|| ErrorKind::BuildEmail)?;
let mut mailer = SmtpTransport::simple_builder(¬ify.smtp_server.host)
.chain_err(|| ErrorKind::BuildSmtpTransport)?
.credentials(Credentials::new(notify.smtp_server.user_name.to_owned(), notify.smtp_server.password.to_owned()))
.smtp_utf8(true)
.authentication_mechanism(Mechanism::Plain)
.build();
mailer.send(&email).chain_err(|| ErrorKind::SendEmail)?;
Ok(())
}
pub fn push_telegram(
form: &FormInput,
telegram: &Telegram,
host: &str,
ip_addr: &str,
) -> Result<()> {
let post_url = format!("{}{}", host.trim_right_matches('/'), form.path);
let message = format!(
"A comment has been posted by *{}* on a post titled:
_{}_.
The comment reads:
{}
You may reply on your blog post [here]({}). In the future, this bot may also provide a means of responding.
Debug information:
`{:?}`
Commenter's IP: {}",
form.sender_name(), form.title, form.comment, post_url, form, ip_addr);
println!("{}", message);
let preview = String::from("1");
let md = String::from("Markdown");
let mut params = HashMap::new();
params.insert("chat_id", &telegram.chat_id);
params.insert("parse_mode", &md);
params.insert("disable_web_page_preview", &preview);
params.insert("text", &message);
let res = reqwest::Client::new()
.post(&format!(
"https://api.telegram.org/bot{}/sendMessage",
telegram.bot_id
))
.form(¶ms)
.send()
.chain_err(|| ErrorKind::Request)?;
if res.status() == reqwest::StatusCode::Ok {
Ok(())
} else {
Err(ErrorKind::TelegramNotify.into())
}
}