src/commands/mod.rs

157 lines · 4297 bytes

31 symbols in this file
  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 access;
 11mod buffer_clear;
 12mod buffer_switch;
 13mod devices;
 14mod dm;
 15mod edit;
 16mod ignore;
 17mod invite;
 18mod join;
 19mod keys;
 20mod matrix;
 21mod matrix_upload;
 22mod me;
 23mod media;
 24mod mention_send;
 25mod moderation;
 26mod names;
 27mod nick;
 28mod page_up;
 29mod part;
 30mod redact;
 31mod reply;
 32mod say;
 33mod topic;
 34mod upload;
 35mod verification;
 36mod verify;
 37pub(crate) mod whois;
 38
 39use access::RoomAccessCommand;
 40use buffer_clear::BufferClearCommand;
 41use buffer_switch::BufferSwitchCommand;
 42
 43pub(crate) use buffer_switch::is_buffer_target;
 44use devices::DevicesCommand;
 45use dm::DirectMessageCommand;
 46use edit::EditCommand;
 47use ignore::IgnoreCommand;
 48use invite::InviteCommand;
 49use join::JoinCommand;
 50use keys::KeysCommand;
 51use matrix::MatrixCommand;
 52use matrix_upload::MatrixUploadCommand;
 53use me::MeCommand;
 54use media::MediaCommand;
 55use mention_send::MentionSendCommand;
 56use moderation::ModerationCommand;
 57use names::NamesCommand;
 58use nick::NickCommand;
 59use page_up::PageUpCommand;
 60use part::PartCommand;
 61use redact::RedactCommand;
 62use reply::ReplyCommand;
 63use say::SayCommand;
 64use topic::TopicCommand;
 65use upload::UploadCommand;
 66use verify::VerifyCommand;
 67use whois::WhoisCommand;
 68
 69pub struct Commands {
 70    _room_access: Command,
 71    _matrix: Command,
 72    _mention_send: CommandRun,
 73    _matrix_upload: CommandRun,
 74    _keys: Command,
 75    _devices: Command,
 76    _invite: Command,
 77    _ignore: [CommandRun; 2],
 78    _ban: Command,
 79    _kick: Command,
 80    _page_up: CommandRun,
 81    _redact: Command,
 82    _edit: Command,
 83    _reply: Command,
 84    _say: CommandRun,
 85    _topic: Command,
 86    _verification: Command,
 87    _buffer_clear: CommandRun,
 88    _buffer_switch: CommandRun,
 89    _join: CommandRun,
 90    _me: CommandRun,
 91    _upload: CommandRun,
 92    _part: CommandRun,
 93    _query: CommandRun,
 94    _msg: CommandRun,
 95    _names: CommandRun,
 96    _unban: Command,
 97    _nick: CommandRun,
 98    _whois: Command,
 99}
100
101impl Commands {
102    pub fn hook_all(
103        servers: &Servers,
104        config: &ConfigHandle,
105    ) -> Result<Commands, ()> {
106        Ok(Commands {
107            _room_access: RoomAccessCommand::create(servers)?,
108            _matrix: MatrixCommand::create(servers, config)?,
109            _mention_send: MentionSendCommand::create(servers)?,
110            _matrix_upload: MatrixUploadCommand::create(servers)?,
111            _devices: DevicesCommand::create(servers)?,
112            _invite: InviteCommand::create(servers)?,
113            _ignore: IgnoreCommand::create()?,
114            _ban: ModerationCommand::ban(servers)?,
115            _kick: ModerationCommand::kick(servers)?,
116            _keys: KeysCommand::create(servers)?,
117            _page_up: PageUpCommand::create(servers)?,
118            _redact: RedactCommand::create(servers)?,
119            _edit: EditCommand::create(servers)?,
120            _reply: ReplyCommand::create(servers)?,
121            _say: SayCommand::create(servers)?,
122            _topic: TopicCommand::create(servers)?,
123            _verification: VerificationCommand::create(servers)?,
124            _buffer_clear: BufferClearCommand::create(servers)?,
125            _buffer_switch: BufferSwitchCommand::create(servers)?,
126            _join: JoinCommand::create(servers)?,
127            _me: MeCommand::create(servers)?,
128            _upload: UploadCommand::create(servers)?,
129            _part: PartCommand::create(servers)?,
130            _query: DirectMessageCommand::query(servers)?,
131            _msg: DirectMessageCommand::msg(servers)?,
132            _names: NamesCommand::create(servers)?,
133            _unban: ModerationCommand::unban(servers)?,
134            _nick: NickCommand::create(servers)?,
135            _whois: WhoisCommand::create()?,
136        })
137    }
138}
139
140fn parse_and_run(
141    parser: App,
142    arguments: Args,
143    command: impl FnOnce(&ArgMatches),
144) {
145    match parser.get_matches_from_safe(arguments) {
146        Ok(m) => command(&m),
147        Err(e) => {
148            let error = Weechat::execute_modifier(
149                "color_decode_ansi",
150                "1",
151                &e.to_string(),
152            )
153            .expect("Can't color decode ansi string");
154            Weechat::print(&error);
155        }
156    }
157}