<commit_before>use iron::prelude::*;
use iron_sessionstorage::traits::SessionRequestExt;
use lettre::email::EmailBuilder;
use lettre::transport::smtp::{SecurityLevel, SmtpTransport, SmtpTransportBuilder};
use lettre::transport::smtp::authentication::Mechanism;
use lettre::transport::EmailTransport;
use uuid::Uuid;

use common::http::*;
use common::utils::*;
use common::lazy_static::CONFIG_TABLE;
use services::user::{get_username_by_email, update_retrieve, get_retrieve_time, update_password};

pub fn render_reset_password(req: &mut Request) -> IronResult<Response> {

    let mut data = ViewData::new(req);

    respond_view("reset-password", &data)
}

pub fn send_reset_password_email(req: &mut Request) -> IronResult<Response> {

    let params = get_request_body(req);
    let email_str = &*params.get("email").unwrap()[0];
    let smtp_config = CONFIG_TABLE.get("smtp").unwrap().as_table().unwrap();
    let smtp_host = smtp_config.get("host").unwrap().as_str().unwrap();
    let smtp_port = smtp_config.get("port").unwrap().as_integer().unwrap() as u16;
    let smtp_username = smtp_config.get("username").unwrap().as_str().unwrap();
    let smtp_password = smtp_config.get("password").unwrap().as_str().unwrap();

    let mut data = JsonData::new();

    let username_wrapper = get_username_by_email(email_str);

    if username_wrapper.is_none() {
        data.success = false;
        data.message = "该邮箱并未在本站注册账号，请检查邮箱地址！".to_string();

        return respond_json(&data);
    }

    let username = username_wrapper.unwrap();
    let retrieve_token = Uuid::new_v4().to_string();
    let path = CONFIG_TABLE.get("path").unwrap().as_str().unwrap();

    let email = EmailBuilder::new()
        .to(email_str)
        .from(smtp_username)
        .subject("重置密码")
        .html(&*format!(r#"
            <p style="padding:10px;">
                请点击
                <a style="text-decoration:underline;color:#1e90ff;" href="{0}/set-new-password?username={1}&token={2}" target="_blank">{0}/set-new-password?username={1}&token={2}</a>
                ，进行密码重置！
            </p>
            "#, path, username, retrieve_token))
        .build()
        .unwrap();

    let mut mailer = SmtpTransportBuilder::new((smtp_host, smtp_port)).unwrap()
        .credentials(smtp_username, smtp_password)
        .security_level(SecurityLevel::AlwaysEncrypt)
        .smtp_utf8(true)
        .authentication_mechanism(Mechanism::Plain)
        .connection_reuse(true)
        .build();

    let result = mailer.send(email);

    if result.is_ok() {

        update_retrieve(&*username, &*retrieve_token);

        data.message = "验证邮件已发送，该邮件的有效时间为24小时，请注意查收！".to_string();<commit_msg>修改邮件提示文字<commit_after>use iron::prelude::*;
use iron_sessionstorage::traits::SessionRequestExt;
use lettre::email::EmailBuilder;
use lettre::transport::smtp::{SecurityLevel, SmtpTransport, SmtpTransportBuilder};
use lettre::transport::smtp::authentication::Mechanism;
use lettre::transport::EmailTransport;
use uuid::Uuid;

use common::http::*;
use common::utils::*;
use common::lazy_static::CONFIG_TABLE;
use services::user::{get_username_by_email, update_retrieve, get_retrieve_time, update_password};

pub fn render_reset_password(req: &mut Request) -> IronResult<Response> {

    let mut data = ViewData::new(req);

    respond_view("reset-password", &data)
}

pub fn send_reset_password_email(req: &mut Request) -> IronResult<Response> {

    let params = get_request_body(req);
    let email_str = &*params.get("email").unwrap()[0];
    let smtp_config = CONFIG_TABLE.get("smtp").unwrap().as_table().unwrap();
    let smtp_host = smtp_config.get("host").unwrap().as_str().unwrap();
    let smtp_port = smtp_config.get("port").unwrap().as_integer().unwrap() as u16;
    let smtp_username = smtp_config.get("username").unwrap().as_str().unwrap();
    let smtp_password = smtp_config.get("password").unwrap().as_str().unwrap();

    let mut data = JsonData::new();

    let username_wrapper = get_username_by_email(email_str);

    if username_wrapper.is_none() {
        data.success = false;
        data.message = "该邮箱并未在本站注册账号，请检查邮箱地址！".to_string();

        return respond_json(&data);
    }

    let username = username_wrapper.unwrap();
    let retrieve_token = Uuid::new_v4().to_string();
    let path = CONFIG_TABLE.get("path").unwrap().as_str().unwrap();

    let email = EmailBuilder::new()
        .to(email_str)
        .from(smtp_username)
        .subject("重置密码")
        .html(&*format!(r#"
            <p style="padding:10px;">
                请点击
                <a style="text-decoration:underline;color:#1e90ff;" href="{0}/set-new-password?username={1}&token={2}" target="_blank">{0}/set-new-password?username={1}&token={2}</a>
                ，进行密码重置！该链接的有效时间为 <span style="color:red;">24</span> 小时！
            </p>
            "#, path, username, retrieve_token))
        .build()
        .unwrap();

    let mut mailer = SmtpTransportBuilder::new((smtp_host, smtp_port)).unwrap()
        .credentials(smtp_username, smtp_password)
        .security_level(SecurityLevel::AlwaysEncrypt)
        .smtp_utf8(true)
        .authentication_mechanism(Mechanism::Plain)
        .connection_reuse(true)
        .build();

    let result = mailer.send(email);

    if result.is_ok() {

        update_retrieve(&*username, &*retrieve_token);

        data.message = "验证邮件已发送，该邮件的有效时间为24小时，请注意查收！".to_string();
{'fraction_non_alphanumeric': 0.1592380247826891, 'fraction_numerical': 0.007027926761605327, 'mean_word_length': 3.9935364727608493, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6867515', 'n_tokens_mistral': 1857, 'n_tokens_neox': 1872, 'n_words': 270}