2023-10-06 22:35:27 -07:00
|
|
|
//! # Default Commands
|
|
|
|
//!
|
|
|
|
//! The command-bar commands are set up here, and iamb-specific commands are defined here. See
|
|
|
|
//! [modalkit::env::vim::command] for additional Vim commands we pull in.
|
2023-01-11 17:54:49 -08:00
|
|
|
use std::convert::TryFrom;
|
|
|
|
|
2023-01-25 17:54:16 -08:00
|
|
|
use matrix_sdk::ruma::{events::tag::TagName, OwnedUserId};
|
2023-01-11 17:54:49 -08:00
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
use modalkit::{
|
2024-02-27 21:21:05 -08:00
|
|
|
commands::{CommandError, CommandResult, CommandStep},
|
2023-03-04 12:23:17 -08:00
|
|
|
env::vim::command::{CommandContext, CommandDescription, OptionType},
|
2024-02-27 21:21:05 -08:00
|
|
|
prelude::OpenTarget,
|
2022-12-29 18:00:59 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
use crate::base::{
|
2023-03-04 12:23:17 -08:00
|
|
|
CreateRoomFlags,
|
|
|
|
CreateRoomType,
|
2023-01-28 12:29:06 +00:00
|
|
|
DownloadFlags,
|
2023-03-04 12:23:17 -08:00
|
|
|
HomeserverAction,
|
2022-12-29 18:00:59 -08:00
|
|
|
IambAction,
|
|
|
|
IambId,
|
2024-03-28 20:58:34 -07:00
|
|
|
KeysAction,
|
2023-01-10 19:59:30 -08:00
|
|
|
MessageAction,
|
2022-12-29 18:00:59 -08:00
|
|
|
ProgramCommand,
|
|
|
|
ProgramCommands,
|
2023-01-04 12:51:33 -08:00
|
|
|
RoomAction,
|
2023-01-25 17:54:16 -08:00
|
|
|
RoomField,
|
2023-01-10 19:59:30 -08:00
|
|
|
SendAction,
|
2022-12-29 18:00:59 -08:00
|
|
|
VerifyAction,
|
|
|
|
};
|
|
|
|
|
2024-02-27 21:21:05 -08:00
|
|
|
type ProgContext = CommandContext;
|
2022-12-29 18:00:59 -08:00
|
|
|
type ProgResult = CommandResult<ProgramCommand>;
|
|
|
|
|
2023-01-25 17:54:16 -08:00
|
|
|
/// Convert strings the user types into a tag name.
|
|
|
|
fn tag_name(name: String) -> Result<TagName, CommandError> {
|
|
|
|
let tag = match name.as_str() {
|
|
|
|
"fav" | "favorite" | "favourite" | "m.favourite" => TagName::Favorite,
|
|
|
|
"low" | "lowpriority" | "low_priority" | "low-priority" | "m.lowpriority" => {
|
|
|
|
TagName::LowPriority
|
|
|
|
},
|
|
|
|
"servernotice" | "server_notice" | "server-notice" | "m.server_notice" => {
|
|
|
|
TagName::ServerNotice
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
if let Ok(tag) = name.parse() {
|
|
|
|
TagName::User(tag)
|
|
|
|
} else {
|
2023-01-30 13:51:32 -08:00
|
|
|
let msg = format!("Invalid user tag name: {name}");
|
2023-01-25 17:54:16 -08:00
|
|
|
|
|
|
|
return Err(CommandError::Error(msg));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
Ok(tag)
|
|
|
|
}
|
|
|
|
|
2023-01-11 17:54:49 -08:00
|
|
|
fn iamb_invite(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
let args = desc.arg.strings()?;
|
|
|
|
|
|
|
|
if args.is_empty() {
|
|
|
|
return Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
let ract = match args[0].as_str() {
|
|
|
|
"accept" => {
|
|
|
|
if args.len() != 1 {
|
|
|
|
return Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
RoomAction::InviteAccept
|
|
|
|
},
|
|
|
|
"reject" => {
|
|
|
|
if args.len() != 1 {
|
|
|
|
return Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
RoomAction::InviteReject
|
|
|
|
},
|
|
|
|
"send" => {
|
|
|
|
if args.len() != 2 {
|
|
|
|
return Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Ok(user) = OwnedUserId::try_from(args[1].as_str()) {
|
|
|
|
RoomAction::InviteSend(user)
|
|
|
|
} else {
|
|
|
|
let msg = format!("Invalid user identifier: {}", args[1]);
|
|
|
|
let err = CommandError::Error(msg);
|
|
|
|
|
|
|
|
return Err(err);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
return Err(CommandError::InvalidArgument);
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
let iact = IambAction::from(ract);
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(iact.into(), ctx.context.clone());
|
2023-01-11 17:54:49 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2024-03-28 20:58:34 -07:00
|
|
|
fn iamb_keys(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
let mut args = desc.arg.strings()?;
|
|
|
|
|
|
|
|
if args.len() != 3 {
|
|
|
|
return Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
let act = args.remove(0);
|
|
|
|
let path = args.remove(0);
|
|
|
|
let passphrase = args.remove(0);
|
|
|
|
|
|
|
|
let act = match act.as_str() {
|
|
|
|
"export" => KeysAction::Export(path, passphrase),
|
|
|
|
"import" => KeysAction::Import(path, passphrase),
|
|
|
|
_ => return Err(CommandError::InvalidArgument),
|
|
|
|
};
|
|
|
|
|
|
|
|
let vact = IambAction::Keys(act);
|
|
|
|
let step = CommandStep::Continue(vact.into(), ctx.context.clone());
|
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
fn iamb_verify(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
let mut args = desc.arg.strings()?;
|
|
|
|
|
|
|
|
match args.len() {
|
|
|
|
0 => {
|
2023-01-04 12:51:33 -08:00
|
|
|
let open = ctx.switch(OpenTarget::Application(IambId::VerifyList));
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(open, ctx.context.clone());
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
},
|
|
|
|
1 => {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
},
|
|
|
|
2 => {
|
|
|
|
let act = match args[0].as_str() {
|
|
|
|
"accept" => VerifyAction::Accept,
|
|
|
|
"cancel" => VerifyAction::Cancel,
|
|
|
|
"confirm" => VerifyAction::Confirm,
|
|
|
|
"mismatch" => VerifyAction::Mismatch,
|
|
|
|
"request" => {
|
|
|
|
let iact = IambAction::VerifyRequest(args.remove(1));
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(iact.into(), ctx.context.clone());
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
},
|
|
|
|
_ => return Result::Err(CommandError::InvalidArgument),
|
|
|
|
};
|
|
|
|
|
|
|
|
let vact = IambAction::Verify(act, args.remove(1));
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(vact.into(), ctx.context.clone());
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
},
|
|
|
|
_ => {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn iamb_dms(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
if !desc.arg.text.is_empty() {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:51:33 -08:00
|
|
|
let open = ctx.switch(OpenTarget::Application(IambId::DirectList));
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(open, ctx.context.clone());
|
2023-01-04 12:51:33 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn iamb_members(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
if !desc.arg.text.is_empty() {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
let open = IambAction::Room(RoomAction::Members(ctx.clone().into()));
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(open.into(), ctx.context.clone());
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2023-04-28 16:52:33 -07:00
|
|
|
fn iamb_leave(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
if !desc.arg.text.is_empty() {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
let leave = IambAction::Room(RoomAction::Leave(desc.bang));
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(leave.into(), ctx.context.clone());
|
2023-04-28 16:52:33 -07:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:20:32 -08:00
|
|
|
fn iamb_cancel(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
if !desc.arg.text.is_empty() {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
2023-05-01 22:14:08 -07:00
|
|
|
let mact = IambAction::from(MessageAction::Cancel(desc.bang));
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(mact.into(), ctx.context.clone());
|
2023-01-12 21:20:32 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2023-01-19 16:05:02 -08:00
|
|
|
fn iamb_edit(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
if !desc.arg.text.is_empty() {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
2023-02-09 17:53:33 -08:00
|
|
|
let mact = IambAction::from(MessageAction::Edit);
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(mact.into(), ctx.context.clone());
|
2023-02-09 17:53:33 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn iamb_react(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
let args = desc.arg.strings()?;
|
|
|
|
|
|
|
|
if args.len() != 1 {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
let k = args[0].as_str();
|
|
|
|
|
|
|
|
if let Some(emoji) = emojis::get(k).or_else(|| emojis::get_by_shortcode(k)) {
|
|
|
|
let mact = IambAction::from(MessageAction::React(emoji.to_string()));
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(mact.into(), ctx.context.clone());
|
2023-02-09 17:53:33 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
} else {
|
|
|
|
let msg = format!("Invalid Emoji or shortcode: {k}");
|
|
|
|
|
|
|
|
return Result::Err(CommandError::Error(msg));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn iamb_unreact(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
let mut args = desc.arg.strings()?;
|
|
|
|
|
|
|
|
if args.len() > 1 {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mact = if let Some(k) = args.pop() {
|
|
|
|
let k = k.as_str();
|
|
|
|
|
|
|
|
if let Some(emoji) = emojis::get(k).or_else(|| emojis::get_by_shortcode(k)) {
|
|
|
|
IambAction::from(MessageAction::Unreact(Some(emoji.to_string())))
|
|
|
|
} else {
|
|
|
|
let msg = format!("Invalid Emoji or shortcode: {k}");
|
|
|
|
|
|
|
|
return Result::Err(CommandError::Error(msg));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
IambAction::from(MessageAction::Unreact(None))
|
|
|
|
};
|
|
|
|
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(mact.into(), ctx.context.clone());
|
2023-01-19 16:05:02 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2023-01-13 17:53:54 -08:00
|
|
|
fn iamb_redact(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
let args = desc.arg.strings()?;
|
|
|
|
|
|
|
|
if args.len() > 1 {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
2023-04-28 16:52:33 -07:00
|
|
|
let reason = args.into_iter().next();
|
|
|
|
let ract = IambAction::from(MessageAction::Redact(reason, desc.bang));
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(ract.into(), ctx.context.clone());
|
2023-01-13 17:53:54 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2023-01-12 21:20:32 -08:00
|
|
|
fn iamb_reply(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
if !desc.arg.text.is_empty() {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
let ract = IambAction::from(MessageAction::Reply);
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(ract.into(), ctx.context.clone());
|
2023-01-12 21:20:32 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2023-09-10 16:45:27 +03:00
|
|
|
fn iamb_editor(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
if !desc.arg.text.is_empty() {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
let sact = IambAction::from(SendAction::SubmitFromEditor);
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(sact.into(), ctx.context.clone());
|
2023-09-10 16:45:27 +03:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
fn iamb_rooms(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
if !desc.arg.text.is_empty() {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:51:33 -08:00
|
|
|
let open = ctx.switch(OpenTarget::Application(IambId::RoomList));
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(open, ctx.context.clone());
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2024-02-27 18:36:09 -08:00
|
|
|
fn iamb_chats(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
if !desc.arg.text.is_empty() {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
let open = ctx.switch(OpenTarget::Application(IambId::ChatList));
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(open, ctx.context.clone());
|
2024-02-27 18:36:09 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
fn iamb_spaces(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
if !desc.arg.text.is_empty() {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:51:33 -08:00
|
|
|
let open = ctx.switch(OpenTarget::Application(IambId::SpaceList));
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(open, ctx.context.clone());
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn iamb_welcome(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
if !desc.arg.text.is_empty() {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:51:33 -08:00
|
|
|
let open = ctx.switch(OpenTarget::Application(IambId::Welcome));
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(open, ctx.context.clone());
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn iamb_join(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
let mut args = desc.arg.filenames()?;
|
|
|
|
|
|
|
|
if args.len() != 1 {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:51:33 -08:00
|
|
|
let open = ctx.switch(args.remove(0));
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(open, ctx.context.clone());
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2023-03-04 12:23:17 -08:00
|
|
|
fn iamb_create(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
let args = desc.arg.options()?;
|
|
|
|
let mut flags = CreateRoomFlags::NONE;
|
|
|
|
let mut alias = None;
|
|
|
|
let mut ct = CreateRoomType::Room;
|
|
|
|
|
|
|
|
for arg in args {
|
|
|
|
match arg {
|
|
|
|
OptionType::Flag(name, Some(arg)) => {
|
|
|
|
match name.as_str() {
|
|
|
|
"alias" => {
|
|
|
|
if alias.is_some() {
|
|
|
|
let msg = "Multiple ++alias arguments are not allowed";
|
|
|
|
let err = CommandError::Error(msg.into());
|
|
|
|
|
|
|
|
return Err(err);
|
|
|
|
} else {
|
|
|
|
alias = Some(arg);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_ => return Err(CommandError::InvalidArgument),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
OptionType::Flag(name, None) => {
|
|
|
|
match name.as_str() {
|
|
|
|
"public" => flags |= CreateRoomFlags::PUBLIC,
|
|
|
|
"space" => ct = CreateRoomType::Space,
|
|
|
|
"enc" | "encrypted" => flags |= CreateRoomFlags::ENCRYPTED,
|
|
|
|
_ => return Err(CommandError::InvalidArgument),
|
|
|
|
}
|
|
|
|
},
|
|
|
|
OptionType::Positional(_) => {
|
|
|
|
let msg = ":create doesn't take any positional arguments";
|
|
|
|
let err = CommandError::Error(msg.into());
|
|
|
|
|
|
|
|
return Err(err);
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let hact = HomeserverAction::CreateRoom(alias, ct, flags);
|
|
|
|
let iact = IambAction::from(hact);
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(iact.into(), ctx.context.clone());
|
2023-03-04 12:23:17 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2023-01-25 17:54:16 -08:00
|
|
|
fn iamb_room(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
2023-01-05 18:12:25 -08:00
|
|
|
let mut args = desc.arg.strings()?;
|
|
|
|
|
2023-01-25 17:54:16 -08:00
|
|
|
if args.len() < 2 {
|
2023-01-05 18:12:25 -08:00
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
let field = args.remove(0);
|
2023-01-25 17:54:16 -08:00
|
|
|
let action = args.remove(0);
|
2023-01-05 18:12:25 -08:00
|
|
|
|
2023-01-25 17:54:16 -08:00
|
|
|
if args.len() > 1 {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
let act: IambAction = match (field.as_str(), action.as_str(), args.pop()) {
|
|
|
|
// :room name set <room-name>
|
|
|
|
("name", "set", Some(s)) => RoomAction::Set(RoomField::Name, s).into(),
|
|
|
|
("name", "set", None) => return Result::Err(CommandError::InvalidArgument),
|
|
|
|
|
|
|
|
// :room name unset
|
|
|
|
("name", "unset", None) => RoomAction::Unset(RoomField::Name).into(),
|
|
|
|
("name", "unset", Some(_)) => return Result::Err(CommandError::InvalidArgument),
|
|
|
|
|
|
|
|
// :room topic set <topic>
|
|
|
|
("topic", "set", Some(s)) => RoomAction::Set(RoomField::Topic, s).into(),
|
|
|
|
("topic", "set", None) => return Result::Err(CommandError::InvalidArgument),
|
|
|
|
|
|
|
|
// :room topic unset
|
|
|
|
("topic", "unset", None) => RoomAction::Unset(RoomField::Topic).into(),
|
|
|
|
("topic", "unset", Some(_)) => return Result::Err(CommandError::InvalidArgument),
|
|
|
|
|
|
|
|
// :room tag set <tag-name>
|
|
|
|
("tag", "set", Some(s)) => RoomAction::Set(RoomField::Tag(tag_name(s)?), "".into()).into(),
|
|
|
|
("tag", "set", None) => return Result::Err(CommandError::InvalidArgument),
|
|
|
|
|
|
|
|
// :room tag unset <tag-name>
|
|
|
|
("tag", "unset", Some(s)) => RoomAction::Unset(RoomField::Tag(tag_name(s)?)).into(),
|
|
|
|
("tag", "unset", None) => return Result::Err(CommandError::InvalidArgument),
|
|
|
|
|
|
|
|
_ => return Result::Err(CommandError::InvalidArgument),
|
2023-01-05 18:12:25 -08:00
|
|
|
};
|
|
|
|
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(act.into(), ctx.context.clone());
|
2023-01-05 18:12:25 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2023-01-10 19:59:30 -08:00
|
|
|
fn iamb_upload(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
let mut args = desc.arg.strings()?;
|
|
|
|
|
|
|
|
if args.len() != 1 {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
let sact = SendAction::Upload(args.remove(0));
|
|
|
|
let iact = IambAction::from(sact);
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(iact.into(), ctx.context.clone());
|
2023-01-10 19:59:30 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn iamb_download(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
let mut args = desc.arg.strings()?;
|
|
|
|
|
|
|
|
if args.len() > 1 {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
2023-01-28 12:29:06 +00:00
|
|
|
let mut flags = DownloadFlags::NONE;
|
|
|
|
if desc.bang {
|
|
|
|
flags |= DownloadFlags::FORCE;
|
|
|
|
};
|
|
|
|
let mact = MessageAction::Download(args.pop(), flags);
|
|
|
|
let iact = IambAction::from(mact);
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(iact.into(), ctx.context.clone());
|
2023-01-28 12:29:06 +00:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn iamb_open(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
let mut args = desc.arg.strings()?;
|
|
|
|
|
|
|
|
if args.len() > 1 {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut flags = DownloadFlags::OPEN;
|
|
|
|
if desc.bang {
|
|
|
|
flags |= DownloadFlags::FORCE;
|
|
|
|
};
|
|
|
|
let mact = MessageAction::Download(args.pop(), flags);
|
2023-01-10 19:59:30 -08:00
|
|
|
let iact = IambAction::from(mact);
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(iact.into(), ctx.context.clone());
|
2023-01-10 19:59:30 -08:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2023-10-13 00:58:59 -05:00
|
|
|
fn iamb_logout(desc: CommandDescription, ctx: &mut ProgContext) -> ProgResult {
|
|
|
|
let args = desc.arg.strings()?;
|
|
|
|
|
|
|
|
if args.len() != 1 {
|
|
|
|
return Result::Err(CommandError::InvalidArgument);
|
|
|
|
}
|
|
|
|
|
|
|
|
let iact = IambAction::from(HomeserverAction::Logout(args[0].clone(), desc.bang));
|
2024-02-27 21:21:05 -08:00
|
|
|
let step = CommandStep::Continue(iact.into(), ctx.context.clone());
|
2023-10-13 00:58:59 -05:00
|
|
|
|
|
|
|
return Ok(step);
|
|
|
|
}
|
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
fn add_iamb_commands(cmds: &mut ProgramCommands) {
|
2023-03-01 18:46:33 -08:00
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "cancel".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_cancel,
|
|
|
|
});
|
2023-03-04 12:23:17 -08:00
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "create".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_create,
|
|
|
|
});
|
2024-02-27 18:36:09 -08:00
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "chats".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_chats,
|
|
|
|
});
|
2023-03-01 18:46:33 -08:00
|
|
|
cmds.add_command(ProgramCommand { name: "dms".into(), aliases: vec![], f: iamb_dms });
|
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "download".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_download,
|
|
|
|
});
|
|
|
|
cmds.add_command(ProgramCommand { name: "open".into(), aliases: vec![], f: iamb_open });
|
|
|
|
cmds.add_command(ProgramCommand { name: "edit".into(), aliases: vec![], f: iamb_edit });
|
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "invite".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_invite,
|
|
|
|
});
|
|
|
|
cmds.add_command(ProgramCommand { name: "join".into(), aliases: vec![], f: iamb_join });
|
2024-03-28 20:58:34 -07:00
|
|
|
cmds.add_command(ProgramCommand { name: "keys".into(), aliases: vec![], f: iamb_keys });
|
2023-04-28 16:52:33 -07:00
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "leave".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_leave,
|
|
|
|
});
|
2023-03-01 18:46:33 -08:00
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "members".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_members,
|
|
|
|
});
|
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "react".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_react,
|
|
|
|
});
|
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "redact".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_redact,
|
|
|
|
});
|
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "reply".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_reply,
|
|
|
|
});
|
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "rooms".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_rooms,
|
|
|
|
});
|
|
|
|
cmds.add_command(ProgramCommand { name: "room".into(), aliases: vec![], f: iamb_room });
|
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "spaces".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_spaces,
|
|
|
|
});
|
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "unreact".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_unreact,
|
|
|
|
});
|
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "upload".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_upload,
|
|
|
|
});
|
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "verify".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_verify,
|
|
|
|
});
|
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "welcome".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_welcome,
|
|
|
|
});
|
2023-09-10 16:45:27 +03:00
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "editor".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_editor,
|
|
|
|
});
|
2023-10-13 00:58:59 -05:00
|
|
|
cmds.add_command(ProgramCommand {
|
|
|
|
name: "logout".into(),
|
|
|
|
aliases: vec![],
|
|
|
|
f: iamb_logout,
|
|
|
|
});
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|
|
|
|
|
2023-10-06 22:35:27 -07:00
|
|
|
/// Initialize the default command state.
|
2022-12-29 18:00:59 -08:00
|
|
|
pub fn setup_commands() -> ProgramCommands {
|
|
|
|
let mut cmds = ProgramCommands::default();
|
|
|
|
|
|
|
|
add_iamb_commands(&mut cmds);
|
|
|
|
|
|
|
|
return cmds;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2023-01-11 17:54:49 -08:00
|
|
|
use matrix_sdk::ruma::user_id;
|
2024-02-28 23:00:25 -08:00
|
|
|
use modalkit::actions::WindowAction;
|
2024-02-27 21:21:05 -08:00
|
|
|
use modalkit::editing::context::EditContext;
|
2023-01-04 12:51:33 -08:00
|
|
|
|
2022-12-29 18:00:59 -08:00
|
|
|
#[test]
|
|
|
|
fn test_cmd_verify() {
|
|
|
|
let mut cmds = setup_commands();
|
2024-02-27 21:21:05 -08:00
|
|
|
let ctx = EditContext::default();
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
let res = cmds.input_cmd(":verify", ctx.clone()).unwrap();
|
|
|
|
let act = WindowAction::Switch(OpenTarget::Application(IambId::VerifyList));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd(":verify request @user1:example.com", ctx.clone()).unwrap();
|
|
|
|
let act = IambAction::VerifyRequest("@user1:example.com".into());
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds
|
|
|
|
.input_cmd(":verify accept @user1:example.com/FOOBAR", ctx.clone())
|
|
|
|
.unwrap();
|
|
|
|
let act = IambAction::Verify(VerifyAction::Accept, "@user1:example.com/FOOBAR".into());
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds
|
|
|
|
.input_cmd(":verify mismatch @user2:example.com/QUUXBAZ", ctx.clone())
|
|
|
|
.unwrap();
|
|
|
|
let act = IambAction::Verify(VerifyAction::Mismatch, "@user2:example.com/QUUXBAZ".into());
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds
|
|
|
|
.input_cmd(":verify cancel @user3:example.com/MYDEVICE", ctx.clone())
|
|
|
|
.unwrap();
|
|
|
|
let act = IambAction::Verify(VerifyAction::Cancel, "@user3:example.com/MYDEVICE".into());
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds
|
|
|
|
.input_cmd(":verify confirm @user4:example.com/GOODDEV", ctx.clone())
|
|
|
|
.unwrap();
|
|
|
|
let act = IambAction::Verify(VerifyAction::Confirm, "@user4:example.com/GOODDEV".into());
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd(":verify confirm", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd(":verify cancel @user4:example.com MYDEVICE", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd(":verify mismatch a b c d e f", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cmd_join() {
|
|
|
|
let mut cmds = setup_commands();
|
2024-02-27 21:21:05 -08:00
|
|
|
let ctx = EditContext::default();
|
2022-12-29 18:00:59 -08:00
|
|
|
|
|
|
|
let res = cmds.input_cmd("join #foobar:example.com", ctx.clone()).unwrap();
|
|
|
|
let act = WindowAction::Switch(OpenTarget::Name("#foobar:example.com".into()));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("join #", ctx.clone()).unwrap();
|
|
|
|
let act = WindowAction::Switch(OpenTarget::Alternate);
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("join", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("join foo bar", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
}
|
2023-01-05 18:12:25 -08:00
|
|
|
|
|
|
|
#[test]
|
2023-01-25 17:54:16 -08:00
|
|
|
fn test_cmd_room_invalid() {
|
|
|
|
let mut cmds = setup_commands();
|
2024-02-27 21:21:05 -08:00
|
|
|
let ctx = EditContext::default();
|
2023-01-25 17:54:16 -08:00
|
|
|
|
|
|
|
let res = cmds.input_cmd("room", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room foo", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room set topic", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cmd_room_topic_set() {
|
2023-01-05 18:12:25 -08:00
|
|
|
let mut cmds = setup_commands();
|
2024-02-27 21:21:05 -08:00
|
|
|
let ctx = EditContext::default();
|
2023-01-05 18:12:25 -08:00
|
|
|
|
|
|
|
let res = cmds
|
2023-01-25 17:54:16 -08:00
|
|
|
.input_cmd("room topic set \"Lots of fun discussion!\"", ctx.clone())
|
2023-01-05 18:12:25 -08:00
|
|
|
.unwrap();
|
2023-01-25 17:54:16 -08:00
|
|
|
let act = RoomAction::Set(RoomField::Topic, "Lots of fun discussion!".into());
|
2023-01-05 18:12:25 -08:00
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds
|
2023-01-25 17:54:16 -08:00
|
|
|
.input_cmd("room topic set The\\ Discussion\\ Room", ctx.clone())
|
2023-01-05 18:12:25 -08:00
|
|
|
.unwrap();
|
2023-01-25 17:54:16 -08:00
|
|
|
let act = RoomAction::Set(RoomField::Topic, "The Discussion Room".into());
|
2023-01-05 18:12:25 -08:00
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
2023-01-25 17:54:16 -08:00
|
|
|
let res = cmds.input_cmd("room topic set Development", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Set(RoomField::Topic, "Development".into());
|
2023-01-05 18:12:25 -08:00
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
2023-01-25 17:54:16 -08:00
|
|
|
let res = cmds.input_cmd("room topic", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room topic set", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room topic set A B C", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cmd_room_name_invalid() {
|
|
|
|
let mut cmds = setup_commands();
|
2024-02-27 21:21:05 -08:00
|
|
|
let ctx = EditContext::default();
|
2023-01-25 17:54:16 -08:00
|
|
|
|
|
|
|
let res = cmds.input_cmd("room name", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room name foo", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cmd_room_name_set() {
|
|
|
|
let mut cmds = setup_commands();
|
2024-02-27 21:21:05 -08:00
|
|
|
let ctx = EditContext::default();
|
2023-01-25 17:54:16 -08:00
|
|
|
|
|
|
|
let res = cmds.input_cmd("room name set Development", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Set(RoomField::Name, "Development".into());
|
2023-01-05 18:12:25 -08:00
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds
|
2023-01-25 17:54:16 -08:00
|
|
|
.input_cmd("room name set \"Application Development\"", ctx.clone())
|
2023-01-05 18:12:25 -08:00
|
|
|
.unwrap();
|
2023-01-25 17:54:16 -08:00
|
|
|
let act = RoomAction::Set(RoomField::Name, "Application Development".into());
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room name set", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cmd_room_name_unset() {
|
|
|
|
let mut cmds = setup_commands();
|
2024-02-27 21:21:05 -08:00
|
|
|
let ctx = EditContext::default();
|
2023-01-25 17:54:16 -08:00
|
|
|
|
|
|
|
let res = cmds.input_cmd("room name unset", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Unset(RoomField::Name);
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room name unset foo", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cmd_room_tag_set() {
|
|
|
|
let mut cmds = setup_commands();
|
2024-02-27 21:21:05 -08:00
|
|
|
let ctx = EditContext::default();
|
2023-01-25 17:54:16 -08:00
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag set favourite", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Set(RoomField::Tag(TagName::Favorite), "".into());
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag set favorite", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Set(RoomField::Tag(TagName::Favorite), "".into());
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag set fav", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Set(RoomField::Tag(TagName::Favorite), "".into());
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag set low_priority", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Set(RoomField::Tag(TagName::LowPriority), "".into());
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag set low-priority", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Set(RoomField::Tag(TagName::LowPriority), "".into());
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag set low", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Set(RoomField::Tag(TagName::LowPriority), "".into());
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag set servernotice", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Set(RoomField::Tag(TagName::ServerNotice), "".into());
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag set server_notice", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Set(RoomField::Tag(TagName::ServerNotice), "".into());
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag set server_notice", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Set(RoomField::Tag(TagName::ServerNotice), "".into());
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag set u.custom-tag", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Set(
|
|
|
|
RoomField::Tag(TagName::User("u.custom-tag".parse().unwrap())),
|
|
|
|
"".into(),
|
|
|
|
);
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag set u.irc", ctx.clone()).unwrap();
|
|
|
|
let act =
|
|
|
|
RoomAction::Set(RoomField::Tag(TagName::User("u.irc".parse().unwrap())), "".into());
|
2023-01-05 18:12:25 -08:00
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
2023-01-25 17:54:16 -08:00
|
|
|
let res = cmds.input_cmd("room tag", ctx.clone());
|
2023-01-05 18:12:25 -08:00
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
2023-01-25 17:54:16 -08:00
|
|
|
let res = cmds.input_cmd("room tag set", ctx.clone());
|
2023-01-05 18:12:25 -08:00
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
2023-01-25 17:54:16 -08:00
|
|
|
let res = cmds.input_cmd("room tag set unknown", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::Error("Invalid user tag name: unknown".into())));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag set needs-leading-u-dot", ctx.clone());
|
|
|
|
assert_eq!(
|
|
|
|
res,
|
|
|
|
Err(CommandError::Error("Invalid user tag name: needs-leading-u-dot".into()))
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cmd_room_tag_unset() {
|
|
|
|
let mut cmds = setup_commands();
|
2024-02-27 21:21:05 -08:00
|
|
|
let ctx = EditContext::default();
|
2023-01-25 17:54:16 -08:00
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag unset favourite", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Unset(RoomField::Tag(TagName::Favorite));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag unset favorite", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Unset(RoomField::Tag(TagName::Favorite));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag unset fav", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Unset(RoomField::Tag(TagName::Favorite));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag unset low_priority", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Unset(RoomField::Tag(TagName::LowPriority));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag unset low-priority", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Unset(RoomField::Tag(TagName::LowPriority));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag unset low", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Unset(RoomField::Tag(TagName::LowPriority));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag unset servernotice", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Unset(RoomField::Tag(TagName::ServerNotice));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag unset server_notice", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Unset(RoomField::Tag(TagName::ServerNotice));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag unset server_notice", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Unset(RoomField::Tag(TagName::ServerNotice));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag unset u.custom-tag", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Unset(RoomField::Tag(TagName::User("u.custom-tag".parse().unwrap())));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag unset u.irc", ctx.clone()).unwrap();
|
|
|
|
let act = RoomAction::Unset(RoomField::Tag(TagName::User("u.irc".parse().unwrap())));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag", ctx.clone());
|
2023-01-05 18:12:25 -08:00
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
2023-01-25 17:54:16 -08:00
|
|
|
let res = cmds.input_cmd("room tag set", ctx.clone());
|
2023-01-05 18:12:25 -08:00
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
2023-01-25 17:54:16 -08:00
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag unset unknown", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::Error("Invalid user tag name: unknown".into())));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("room tag unset needs-leading-u-dot", ctx.clone());
|
|
|
|
assert_eq!(
|
|
|
|
res,
|
|
|
|
Err(CommandError::Error("Invalid user tag name: needs-leading-u-dot".into()))
|
|
|
|
);
|
2023-01-05 18:12:25 -08:00
|
|
|
}
|
2023-01-11 17:54:49 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cmd_invite() {
|
|
|
|
let mut cmds = setup_commands();
|
2024-02-27 21:21:05 -08:00
|
|
|
let ctx = EditContext::default();
|
2023-01-11 17:54:49 -08:00
|
|
|
|
|
|
|
let res = cmds.input_cmd("invite accept", ctx.clone()).unwrap();
|
|
|
|
let act = IambAction::Room(RoomAction::InviteAccept);
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("invite reject", ctx.clone()).unwrap();
|
|
|
|
let act = IambAction::Room(RoomAction::InviteReject);
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("invite send @user:example.com", ctx.clone()).unwrap();
|
|
|
|
let act =
|
|
|
|
IambAction::Room(RoomAction::InviteSend(user_id!("@user:example.com").to_owned()));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("invite", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("invite foo", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("invite accept @user:example.com", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("invite reject @user:example.com", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("invite send", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("invite @user:example.com", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
}
|
2023-01-13 17:53:54 -08:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cmd_redact() {
|
|
|
|
let mut cmds = setup_commands();
|
2024-02-27 21:21:05 -08:00
|
|
|
let ctx = EditContext::default();
|
2023-01-13 17:53:54 -08:00
|
|
|
|
|
|
|
let res = cmds.input_cmd("redact", ctx.clone()).unwrap();
|
2023-04-28 16:52:33 -07:00
|
|
|
let act = IambAction::Message(MessageAction::Redact(None, false));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("redact!", ctx.clone()).unwrap();
|
|
|
|
let act = IambAction::Message(MessageAction::Redact(None, true));
|
2023-01-13 17:53:54 -08:00
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("redact Removed", ctx.clone()).unwrap();
|
2023-04-28 16:52:33 -07:00
|
|
|
let act = IambAction::Message(MessageAction::Redact(Some("Removed".into()), false));
|
2023-01-13 17:53:54 -08:00
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("redact \"Removed\"", ctx.clone()).unwrap();
|
2023-04-28 16:52:33 -07:00
|
|
|
let act = IambAction::Message(MessageAction::Redact(Some("Removed".into()), false));
|
2023-01-13 17:53:54 -08:00
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("redact Removed Removed", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
}
|
2024-03-28 20:58:34 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_cmd_keys() {
|
|
|
|
let mut cmds = setup_commands();
|
|
|
|
let ctx = EditContext::default();
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("keys import /a/b/c pword", ctx.clone()).unwrap();
|
|
|
|
let act = IambAction::Keys(KeysAction::Import("/a/b/c".into(), "pword".into()));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("keys export /a/b/c pword", ctx.clone()).unwrap();
|
|
|
|
let act = IambAction::Keys(KeysAction::Export("/a/b/c".into(), "pword".into()));
|
|
|
|
assert_eq!(res, vec![(act.into(), ctx.clone())]);
|
|
|
|
|
|
|
|
// Invalid invocations.
|
|
|
|
let res = cmds.input_cmd("keys", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("keys import", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("keys import foo", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
|
|
|
|
let res = cmds.input_cmd("keys import foo bar baz", ctx.clone());
|
|
|
|
assert_eq!(res, Err(CommandError::InvalidArgument));
|
|
|
|
}
|
2022-12-29 18:00:59 -08:00
|
|
|
}
|