src/commands/mod.rs

8454dcd1674dfb48ab4ee615134e2d4d5fc04fa7
weechat-matrix-rs/src/commands/mod.rs history · blame · raw

115 lines · 2985 bytes

  1use clap::{App, ArgMatches};
  2use verification::VerificationCommand;
  3use weechat::{
  4    hooks::{Command, CommandRun},
  5    Args, Weechat,
  6};
  7
  8use crate::{config::ConfigHandle, Servers};
  9
 10mod buffer_clear;
 11mod devices;
 12mod ignore;
 13mod invite;
 14mod join;
 15mod keys;
 16mod matrix;
 17mod me;
 18mod media;
 19mod moderation;
 20mod names;
 21mod nick;
 22mod page_up;
 23mod part;
 24mod redact;
 25mod topic;
 26mod upload;
 27mod verification;
 28
 29use buffer_clear::BufferClearCommand;
 30use devices::DevicesCommand;
 31use ignore::IgnoreCommand;
 32use invite::InviteCommand;
 33use join::JoinCommand;
 34use keys::KeysCommand;
 35use matrix::MatrixCommand;
 36use me::MeCommand;
 37use media::MediaCommand;
 38use moderation::ModerationCommand;
 39use names::NamesCommand;
 40use nick::NickCommand;
 41use page_up::PageUpCommand;
 42use part::PartCommand;
 43use redact::RedactCommand;
 44use topic::TopicCommand;
 45use upload::UploadCommand;
 46
 47pub struct Commands {
 48    _matrix: Command,
 49    _keys: Command,
 50    _devices: Command,
 51    _invite: Command,
 52    _ignore: [CommandRun; 2],
 53    _ban: Command,
 54    _kick: Command,
 55    _page_up: CommandRun,
 56    _redact: Command,
 57    _topic: Command,
 58    _verification: Command,
 59    _buffer_clear: CommandRun,
 60    _join: CommandRun,
 61    _me: CommandRun,
 62    _upload: CommandRun,
 63    _part: CommandRun,
 64    _names: CommandRun,
 65    _unban: Command,
 66    _nick: CommandRun,
 67}
 68
 69impl Commands {
 70    pub fn hook_all(
 71        servers: &Servers,
 72        config: &ConfigHandle,
 73    ) -> Result<Commands, ()> {
 74        Ok(Commands {
 75            _matrix: MatrixCommand::create(servers, config)?,
 76            _devices: DevicesCommand::create(servers)?,
 77            _invite: InviteCommand::create(servers)?,
 78            _ignore: IgnoreCommand::create()?,
 79            _ban: ModerationCommand::ban(servers)?,
 80            _kick: ModerationCommand::kick(servers)?,
 81            _keys: KeysCommand::create(servers)?,
 82            _page_up: PageUpCommand::create(servers)?,
 83            _redact: RedactCommand::create(servers)?,
 84            _topic: TopicCommand::create(servers)?,
 85            _verification: VerificationCommand::create(servers)?,
 86            _buffer_clear: BufferClearCommand::create(servers)?,
 87            _join: JoinCommand::create(servers)?,
 88            _me: MeCommand::create(servers)?,
 89            _upload: UploadCommand::create(servers)?,
 90            _part: PartCommand::create(servers)?,
 91            _names: NamesCommand::create(servers)?,
 92            _unban: ModerationCommand::unban(servers)?,
 93            _nick: NickCommand::create(servers)?,
 94        })
 95    }
 96}
 97
 98fn parse_and_run(
 99    parser: App,
100    arguments: Args,
101    command: impl FnOnce(&ArgMatches),
102) {
103    match parser.get_matches_from_safe(arguments) {
104        Ok(m) => command(&m),
105        Err(e) => {
106            let error = Weechat::execute_modifier(
107                "color_decode_ansi",
108                "1",
109                &e.to_string(),
110            )
111            .expect("Can't color decode ansi string");
112            Weechat::print(&error);
113        }
114    }
115}