pass_manager/src/main.rs

88 lines
3.3 KiB
Rust
Raw Normal View History

2023-07-25 14:44:12 +00:00
#![allow(unused)]
2023-06-23 08:38:54 +00:00
mod callbacks;
mod commands;
mod default;
mod errors;
2023-07-16 19:34:11 +00:00
mod macros;
2023-06-23 08:38:54 +00:00
mod markups;
mod master_password_check;
mod models;
mod prelude;
2023-06-23 08:38:54 +00:00
mod state;
mod utils;
use anyhow::{Error, Result};
2023-04-24 17:48:34 +00:00
use dotenv::dotenv;
2023-04-27 13:25:23 +00:00
use migration::{Migrator, MigratorTrait};
use prelude::*;
use sea_orm::Database;
2023-05-15 16:09:15 +00:00
use std::env;
use teloxide::{adaptors::throttle::Limits, dispatching::dialogue::InMemStorage, filter_command};
2023-06-23 08:38:54 +00:00
2023-07-25 14:44:12 +00:00
use crate::callbacks::CallbackCommand;
2023-06-23 08:38:54 +00:00
fn get_dispatcher(
token: String,
db: DatabaseConnection,
) -> Dispatcher<Throttle<Bot>, crate::Error, teloxide::dispatching::DefaultKey> {
use dptree::{case, deps};
let bot = Bot::new(token).throttle(Limits::default());
let command_handler = filter_command::<Command, _>()
.branch(case![Command::Start].endpoint(commands::start))
.branch(case![Command::Help].endpoint(commands::help))
.branch(case![Command::SetMasterPass].endpoint(commands::set_master_pass))
.branch(case![Command::GenPassword].endpoint(commands::gen_password))
.branch(case![Command::Cancel].endpoint(commands::cancel))
// This branch filters out the users that don't have a master password set
.branch(master_password_check::get_handler())
.branch(case![Command::AddAccount].endpoint(commands::add_account))
.branch(case![Command::GetAccount].endpoint(commands::get_account))
.branch(case![Command::GetAccounts].endpoint(commands::get_accounts))
.branch(case![Command::Delete].endpoint(commands::delete))
.branch(case![Command::DeleteAll].endpoint(commands::delete_all))
.branch(case![Command::Export].endpoint(commands::export))
.branch(case![Command::Import].endpoint(commands::import));
let message_handler = Update::filter_message()
.map_async(utils::delete_message)
.enter_dialogue::<Update, InMemStorage<State>, State>()
.branch(case![State::GetExistingName(next)].endpoint(state::get_existing_name))
.branch(case![State::GetNewName(next)].endpoint(state::get_new_name))
.branch(case![State::GetMasterPass(next)].endpoint(state::get_master_pass))
.branch(case![State::GetNewMasterPass(next)].endpoint(state::get_new_master_pass))
.branch(case![State::GetLogin(next)].endpoint(state::get_login))
.branch(case![State::GetPassword(next)].endpoint(state::get_password))
.branch(case![State::GetUser(next)].endpoint(state::get_user))
.branch(command_handler)
.endpoint(default::default);
2023-07-25 14:44:12 +00:00
let callback_handler = Update::filter_callback_query()
.filter_map(CallbackCommand::from_query)
.branch(case![CallbackCommand::DeleteMessage].endpoint(callbacks::delete_message));
2023-06-23 08:38:54 +00:00
let handler = dptree::entry()
.branch(message_handler)
.branch(callback_handler);
Dispatcher::builder(bot, handler)
.dependencies(deps![db, InMemStorage::<State>::new()])
.enable_ctrlc_handler()
.build()
}
2023-04-24 17:48:34 +00:00
#[tokio::main]
2023-04-27 13:25:23 +00:00
async fn main() -> Result<()> {
2023-04-24 17:48:34 +00:00
let _ = dotenv();
pretty_env_logger::init();
let token = env::var("TOKEN").expect("expected TOKEN in the enviroment");
let database_url = env::var("DATABASE_URL").expect("expected DATABASE_URL in the enviroment");
2023-04-27 13:25:23 +00:00
let db = Database::connect(database_url).await?;
Migrator::up(&db, None).await?;
get_dispatcher(token, db).dispatch().await;
Ok(())
2023-04-23 17:54:16 +00:00
}