Prepare rewrite
This commit is contained in:
parent
fe68694932
commit
904dda1af0
21 changed files with 6 additions and 2265 deletions
|
|
@ -1,7 +0,0 @@
|
|||
//! Building blocks for bots.
|
||||
|
||||
pub mod botrulez;
|
||||
pub mod command;
|
||||
pub mod commands;
|
||||
pub mod instance;
|
||||
pub mod instances;
|
||||
|
|
@ -1,10 +0,0 @@
|
|||
//! The main [botrulez](https://github.com/jedevc/botrulez) commands.
|
||||
pub mod full_help;
|
||||
pub mod ping;
|
||||
pub mod short_help;
|
||||
pub mod uptime;
|
||||
|
||||
pub use self::full_help::{FullHelp, HasDescriptions};
|
||||
pub use self::ping::Ping;
|
||||
pub use self::short_help::ShortHelp;
|
||||
pub use self::uptime::{format_duration, format_relative_time, format_time, HasStartTime, Uptime};
|
||||
|
|
@ -1,93 +0,0 @@
|
|||
use async_trait::async_trait;
|
||||
use clap::Parser;
|
||||
|
||||
use crate::api::Message;
|
||||
use crate::bot::command::{ClapCommand, Command, Context};
|
||||
use crate::conn;
|
||||
|
||||
pub struct FullHelp {
|
||||
pub before: String,
|
||||
pub after: String,
|
||||
}
|
||||
|
||||
pub trait HasDescriptions {
|
||||
fn descriptions(&self, ctx: &Context) -> Vec<String>;
|
||||
}
|
||||
|
||||
impl FullHelp {
|
||||
pub fn new<S1: ToString, S2: ToString>(before: S1, after: S2) -> Self {
|
||||
Self {
|
||||
before: before.to_string(),
|
||||
after: after.to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
fn formulate_reply<B: HasDescriptions>(&self, ctx: &Context, bot: &B) -> String {
|
||||
let mut result = String::new();
|
||||
|
||||
if !self.before.is_empty() {
|
||||
result.push_str(&self.before);
|
||||
result.push('\n');
|
||||
}
|
||||
|
||||
for description in bot.descriptions(ctx) {
|
||||
result.push_str(&description);
|
||||
result.push('\n');
|
||||
}
|
||||
|
||||
if !self.after.is_empty() {
|
||||
result.push_str(&self.after);
|
||||
result.push('\n');
|
||||
}
|
||||
|
||||
result
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<B, E> Command<B, E> for FullHelp
|
||||
where
|
||||
B: HasDescriptions + Send,
|
||||
E: From<conn::Error>,
|
||||
{
|
||||
async fn execute(
|
||||
&self,
|
||||
arg: &str,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
bot: &mut B,
|
||||
) -> Result<bool, E> {
|
||||
if arg.trim().is_empty() {
|
||||
let reply = self.formulate_reply(ctx, bot);
|
||||
ctx.reply(msg.id, reply).await?;
|
||||
Ok(true)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Show full bot help.
|
||||
#[derive(Parser)]
|
||||
pub struct Args {}
|
||||
|
||||
#[async_trait]
|
||||
impl<B, E> ClapCommand<B, E> for FullHelp
|
||||
where
|
||||
B: HasDescriptions + Send,
|
||||
E: From<conn::Error>,
|
||||
{
|
||||
type Args = Args;
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
_args: Self::Args,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
bot: &mut B,
|
||||
) -> Result<bool, E> {
|
||||
let reply = self.formulate_reply(ctx, bot);
|
||||
ctx.reply(msg.id, reply).await?;
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
use async_trait::async_trait;
|
||||
use clap::Parser;
|
||||
|
||||
use crate::api::Message;
|
||||
use crate::bot::command::{ClapCommand, Command, Context};
|
||||
use crate::conn;
|
||||
|
||||
pub struct Ping(pub String);
|
||||
|
||||
impl Ping {
|
||||
pub fn new<S: ToString>(reply: S) -> Self {
|
||||
Self(reply.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Ping {
|
||||
fn default() -> Self {
|
||||
Self::new("Pong!")
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<B, E> Command<B, E> for Ping
|
||||
where
|
||||
E: From<conn::Error>,
|
||||
{
|
||||
async fn execute(
|
||||
&self,
|
||||
arg: &str,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
_bot: &mut B,
|
||||
) -> Result<bool, E> {
|
||||
if arg.trim().is_empty() {
|
||||
ctx.reply(msg.id, &self.0).await?;
|
||||
Ok(true)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Trigger a short reply.
|
||||
#[derive(Parser)]
|
||||
pub struct Args {}
|
||||
|
||||
#[async_trait]
|
||||
impl<B, E> ClapCommand<B, E> for Ping
|
||||
where
|
||||
E: From<conn::Error>,
|
||||
{
|
||||
type Args = Args;
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
_args: Self::Args,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
_bot: &mut B,
|
||||
) -> Result<bool, E> {
|
||||
ctx.reply(msg.id, &self.0).await?;
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,58 +0,0 @@
|
|||
use async_trait::async_trait;
|
||||
use clap::Parser;
|
||||
|
||||
use crate::api::Message;
|
||||
use crate::bot::command::{ClapCommand, Command, Context};
|
||||
use crate::conn;
|
||||
|
||||
pub struct ShortHelp(pub String);
|
||||
|
||||
impl ShortHelp {
|
||||
pub fn new<S: ToString>(text: S) -> Self {
|
||||
Self(text.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<B, E> Command<B, E> for ShortHelp
|
||||
where
|
||||
E: From<conn::Error>,
|
||||
{
|
||||
async fn execute(
|
||||
&self,
|
||||
arg: &str,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
_bot: &mut B,
|
||||
) -> Result<bool, E> {
|
||||
if arg.trim().is_empty() {
|
||||
ctx.reply(msg.id, &self.0).await?;
|
||||
Ok(true)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Show short bot help.
|
||||
#[derive(Parser)]
|
||||
pub struct Args {}
|
||||
|
||||
#[async_trait]
|
||||
impl<B, E> ClapCommand<B, E> for ShortHelp
|
||||
where
|
||||
E: From<conn::Error>,
|
||||
{
|
||||
type Args = Args;
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
_args: Self::Args,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
_bot: &mut B,
|
||||
) -> Result<bool, E> {
|
||||
ctx.reply(msg.id, &self.0).await?;
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,133 +0,0 @@
|
|||
use async_trait::async_trait;
|
||||
use clap::Parser;
|
||||
use jiff::{Span, Timestamp, Unit};
|
||||
|
||||
use crate::api::Message;
|
||||
use crate::bot::command::{ClapCommand, Command, Context};
|
||||
use crate::conn;
|
||||
|
||||
pub fn format_time(t: Timestamp) -> String {
|
||||
t.strftime("%Y-%m-%d %H:%M:%S UTC").to_string()
|
||||
}
|
||||
|
||||
pub fn format_relative_time(d: Span) -> String {
|
||||
if d.is_positive() {
|
||||
format!("in {}", format_duration(d.abs()))
|
||||
} else {
|
||||
format!("{} ago", format_duration(d.abs()))
|
||||
}
|
||||
}
|
||||
|
||||
pub fn format_duration(d: Span) -> String {
|
||||
let total = d.abs().total(Unit::Second).unwrap() as i64;
|
||||
let secs = total % 60;
|
||||
let mins = (total / 60) % 60;
|
||||
let hours = (total / 60 / 60) % 24;
|
||||
let days = total / 60 / 60 / 24;
|
||||
|
||||
let mut segments = vec![];
|
||||
if days > 0 {
|
||||
segments.push(format!("{days}d"));
|
||||
}
|
||||
if hours > 0 {
|
||||
segments.push(format!("{hours}h"));
|
||||
}
|
||||
if mins > 0 {
|
||||
segments.push(format!("{mins}m"));
|
||||
}
|
||||
if secs > 0 {
|
||||
segments.push(format!("{secs}s"));
|
||||
}
|
||||
if segments.is_empty() {
|
||||
segments.push("0s".to_string());
|
||||
}
|
||||
|
||||
let segments = segments.join(" ");
|
||||
if d.is_positive() {
|
||||
segments
|
||||
} else {
|
||||
format!("-{segments}")
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Uptime;
|
||||
|
||||
pub trait HasStartTime {
|
||||
fn start_time(&self) -> Timestamp;
|
||||
}
|
||||
|
||||
impl Uptime {
|
||||
fn formulate_reply<B: HasStartTime>(&self, ctx: &Context, bot: &B, connected: bool) -> String {
|
||||
let start = bot.start_time();
|
||||
let now = Timestamp::now();
|
||||
|
||||
let mut reply = format!(
|
||||
"/me has been up since {} ({})",
|
||||
format_time(start),
|
||||
format_relative_time(start - now),
|
||||
);
|
||||
|
||||
if connected {
|
||||
let since = ctx.joined.since;
|
||||
reply.push_str(&format!(
|
||||
", connected since {} ({})",
|
||||
format_time(since),
|
||||
format_relative_time(since - now),
|
||||
));
|
||||
}
|
||||
|
||||
reply
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<B, E> Command<B, E> for Uptime
|
||||
where
|
||||
B: HasStartTime + Send,
|
||||
E: From<conn::Error>,
|
||||
{
|
||||
async fn execute(
|
||||
&self,
|
||||
arg: &str,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
bot: &mut B,
|
||||
) -> Result<bool, E> {
|
||||
if arg.trim().is_empty() {
|
||||
let reply = self.formulate_reply(ctx, bot, false);
|
||||
ctx.reply(msg.id, reply).await?;
|
||||
Ok(true)
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Show how long the bot has been online.
|
||||
#[derive(Parser)]
|
||||
pub struct Args {
|
||||
/// Show how long the bot has been connected without interruption.
|
||||
#[arg(long, short)]
|
||||
pub connected: bool,
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<B, E> ClapCommand<B, E> for Uptime
|
||||
where
|
||||
B: HasStartTime + Send,
|
||||
E: From<conn::Error>,
|
||||
{
|
||||
type Args = Args;
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
args: Self::Args,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
bot: &mut B,
|
||||
) -> Result<bool, E> {
|
||||
let reply = self.formulate_reply(ctx, bot, args.connected);
|
||||
ctx.reply(msg.id, reply).await?;
|
||||
Ok(true)
|
||||
}
|
||||
}
|
||||
|
|
@ -1,64 +0,0 @@
|
|||
mod bang;
|
||||
mod clap;
|
||||
mod hidden;
|
||||
mod prefixed;
|
||||
|
||||
use std::future::Future;
|
||||
|
||||
use async_trait::async_trait;
|
||||
|
||||
use crate::api::{self, Message, MessageId};
|
||||
use crate::conn::{self, ConnTx, Joined};
|
||||
|
||||
pub use self::bang::*;
|
||||
pub use self::clap::*;
|
||||
pub use self::hidden::*;
|
||||
pub use self::prefixed::*;
|
||||
|
||||
use super::instance::InstanceConfig;
|
||||
|
||||
pub struct Context {
|
||||
pub config: InstanceConfig,
|
||||
pub conn_tx: ConnTx,
|
||||
pub joined: Joined,
|
||||
}
|
||||
|
||||
impl Context {
|
||||
pub fn send<S: ToString>(&self, content: S) -> impl Future<Output = conn::Result<Message>> {
|
||||
let cmd = api::Send {
|
||||
content: content.to_string(),
|
||||
parent: None,
|
||||
};
|
||||
let reply = self.conn_tx.send(cmd);
|
||||
async move { reply.await.map(|r| r.0) }
|
||||
}
|
||||
|
||||
pub fn reply<S: ToString>(
|
||||
&self,
|
||||
parent: MessageId,
|
||||
content: S,
|
||||
) -> impl Future<Output = conn::Result<Message>> {
|
||||
let cmd = api::Send {
|
||||
content: content.to_string(),
|
||||
parent: Some(parent),
|
||||
};
|
||||
let reply = self.conn_tx.send(cmd);
|
||||
async move { reply.await.map(|r| r.0) }
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(unused_variables)]
|
||||
#[async_trait]
|
||||
pub trait Command<B, E> {
|
||||
fn description(&self, ctx: &Context) -> Option<String> {
|
||||
None
|
||||
}
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
arg: &str,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
bot: &mut B,
|
||||
) -> Result<bool, E>;
|
||||
}
|
||||
|
|
@ -1,235 +0,0 @@
|
|||
use async_trait::async_trait;
|
||||
|
||||
use crate::api::Message;
|
||||
use crate::nick;
|
||||
|
||||
use super::{Command, Context};
|
||||
|
||||
// TODO Don't ignore leading whitespace?
|
||||
// I'm not entirely happy with how commands handle whitespace, and on euphoria,
|
||||
// prefixing commands with whitespace is traditionally used to not trigger them.
|
||||
|
||||
/// Parse leading whitespace followed by an prefix-initiated command.
|
||||
///
|
||||
/// Returns the command name and the remaining text with one leading whitespace
|
||||
/// removed. The remaining text may be the empty string.
|
||||
pub fn parse_prefix_initiated<'a>(text: &'a str, prefix: &str) -> Option<(&'a str, &'a str)> {
|
||||
let text = text.trim_start();
|
||||
let text = text.strip_prefix(prefix)?;
|
||||
let (name, rest) = text.split_once(char::is_whitespace).unwrap_or((text, ""));
|
||||
if name.is_empty() {
|
||||
return None;
|
||||
}
|
||||
Some((name, rest))
|
||||
}
|
||||
|
||||
pub struct Global<C> {
|
||||
prefix: String,
|
||||
name: String,
|
||||
inner: C,
|
||||
}
|
||||
|
||||
impl<C> Global<C> {
|
||||
pub fn new<S: ToString>(name: S, inner: C) -> Self {
|
||||
Self {
|
||||
prefix: "!".to_string(),
|
||||
name: name.to_string(),
|
||||
inner,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prefix<S: ToString>(mut self, prefix: S) -> Self {
|
||||
self.prefix = prefix.to_string();
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<B, E, C> Command<B, E> for Global<C>
|
||||
where
|
||||
B: Send,
|
||||
C: Command<B, E> + Send + Sync,
|
||||
{
|
||||
fn description(&self, ctx: &Context) -> Option<String> {
|
||||
let inner = self.inner.description(ctx)?;
|
||||
Some(format!("{}{} - {inner}", self.prefix, self.name))
|
||||
}
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
arg: &str,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
bot: &mut B,
|
||||
) -> Result<bool, E> {
|
||||
// TODO Replace with let-else
|
||||
let (name, rest) = match parse_prefix_initiated(arg, &self.prefix) {
|
||||
Some(parsed) => parsed,
|
||||
None => return Ok(false),
|
||||
};
|
||||
|
||||
if name != self.name {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
self.inner.execute(rest, msg, ctx, bot).await
|
||||
}
|
||||
}
|
||||
|
||||
pub struct General<C> {
|
||||
prefix: String,
|
||||
name: String,
|
||||
inner: C,
|
||||
}
|
||||
|
||||
impl<C> General<C> {
|
||||
pub fn new<S: ToString>(name: S, inner: C) -> Self {
|
||||
Self {
|
||||
prefix: "!".to_string(),
|
||||
name: name.to_string(),
|
||||
inner,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prefix<S: ToString>(mut self, prefix: S) -> Self {
|
||||
self.prefix = prefix.to_string();
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<B, E, C> Command<B, E> for General<C>
|
||||
where
|
||||
B: Send,
|
||||
C: Command<B, E> + Send + Sync,
|
||||
{
|
||||
fn description(&self, ctx: &Context) -> Option<String> {
|
||||
let inner = self.inner.description(ctx)?;
|
||||
Some(format!("{}{} - {inner}", self.prefix, self.name))
|
||||
}
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
arg: &str,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
bot: &mut B,
|
||||
) -> Result<bool, E> {
|
||||
// TODO Replace with let-else
|
||||
let (name, rest) = match parse_prefix_initiated(arg, &self.prefix) {
|
||||
Some(parsed) => parsed,
|
||||
None => return Ok(false),
|
||||
};
|
||||
|
||||
if name != self.name {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
if parse_prefix_initiated(rest, "@").is_some() {
|
||||
// The command looks like a specific command. If we treated it like
|
||||
// a general command match, we would interpret other bots' specific
|
||||
// commands as general commands.
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
self.inner.execute(rest, msg, ctx, bot).await
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Specific<C> {
|
||||
prefix: String,
|
||||
name: String,
|
||||
inner: C,
|
||||
}
|
||||
|
||||
impl<C> Specific<C> {
|
||||
pub fn new<S: ToString>(name: S, inner: C) -> Self {
|
||||
Self {
|
||||
prefix: "!".to_string(),
|
||||
name: name.to_string(),
|
||||
inner,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prefix<S: ToString>(mut self, prefix: S) -> Self {
|
||||
self.prefix = prefix.to_string();
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<B, E, C> Command<B, E> for Specific<C>
|
||||
where
|
||||
B: Send,
|
||||
C: Command<B, E> + Send + Sync,
|
||||
{
|
||||
fn description(&self, ctx: &Context) -> Option<String> {
|
||||
let inner = self.inner.description(ctx)?;
|
||||
let nick = nick::mention(&ctx.joined.session.name);
|
||||
Some(format!("{}{} @{nick} - {inner}", self.prefix, self.name))
|
||||
}
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
arg: &str,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
bot: &mut B,
|
||||
) -> Result<bool, E> {
|
||||
// TODO Replace with let-else
|
||||
let (name, rest) = match parse_prefix_initiated(arg, &self.prefix) {
|
||||
Some(parsed) => parsed,
|
||||
None => return Ok(false),
|
||||
};
|
||||
|
||||
if name != self.name {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
// TODO Replace with let-else
|
||||
let (nick, rest) = match parse_prefix_initiated(rest, "@") {
|
||||
Some(parsed) => parsed,
|
||||
None => return Ok(false),
|
||||
};
|
||||
|
||||
if nick::normalize(nick) != nick::normalize(&ctx.joined.session.name) {
|
||||
return Ok(false);
|
||||
}
|
||||
|
||||
self.inner.execute(rest, msg, ctx, bot).await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::parse_prefix_initiated;
|
||||
|
||||
#[test]
|
||||
fn test_parse_prefixed() {
|
||||
assert_eq!(parse_prefix_initiated("!foo", "!"), Some(("foo", "")));
|
||||
assert_eq!(parse_prefix_initiated(" !foo", "!"), Some(("foo", "")));
|
||||
assert_eq!(
|
||||
parse_prefix_initiated("!foo ", "!"),
|
||||
Some(("foo", " "))
|
||||
);
|
||||
assert_eq!(
|
||||
parse_prefix_initiated(" !foo ", "!"),
|
||||
Some(("foo", " "))
|
||||
);
|
||||
assert_eq!(
|
||||
parse_prefix_initiated("!foo @bar", "!"),
|
||||
Some(("foo", "@bar"))
|
||||
);
|
||||
assert_eq!(
|
||||
parse_prefix_initiated("!foo @bar", "!"),
|
||||
Some(("foo", " @bar"))
|
||||
);
|
||||
assert_eq!(
|
||||
parse_prefix_initiated("!foo @bar ", "!"),
|
||||
Some(("foo", "@bar "))
|
||||
);
|
||||
assert_eq!(parse_prefix_initiated("! foo @bar", "!"), None);
|
||||
assert_eq!(parse_prefix_initiated("!", "!"), None);
|
||||
assert_eq!(parse_prefix_initiated("?foo", "!"), None);
|
||||
}
|
||||
}
|
||||
|
|
@ -1,182 +0,0 @@
|
|||
use async_trait::async_trait;
|
||||
use clap::{CommandFactory, Parser};
|
||||
|
||||
use crate::api::Message;
|
||||
use crate::conn;
|
||||
|
||||
use super::{Command, Context};
|
||||
|
||||
#[async_trait]
|
||||
pub trait ClapCommand<B, E> {
|
||||
type Args;
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
args: Self::Args,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
bot: &mut B,
|
||||
) -> Result<bool, E>;
|
||||
}
|
||||
|
||||
/// Parse bash-like quoted arguments separated by whitespace.
|
||||
///
|
||||
/// Outside of quotes, the backslash either escapes the next character or forms
|
||||
/// an escape sequence. \n is a newline, \r a carriage return and \t a tab.
|
||||
/// TODO Escape sequences
|
||||
///
|
||||
/// Special characters like the backslash and whitespace can also be quoted
|
||||
/// using double quotes. Within double quotes, \" escapes a double quote and \\
|
||||
/// escapes a backslash. Other occurrences of \ have no special meaning.
|
||||
fn parse_quoted_args(text: &str) -> Result<Vec<String>, &'static str> {
|
||||
let mut args = vec![];
|
||||
let mut arg = String::new();
|
||||
let mut arg_exists = false;
|
||||
|
||||
let mut quoted = false;
|
||||
let mut escaped = false;
|
||||
for c in text.chars() {
|
||||
if quoted {
|
||||
match c {
|
||||
'\\' if escaped => {
|
||||
arg.push('\\');
|
||||
escaped = false;
|
||||
}
|
||||
'"' if escaped => {
|
||||
arg.push('"');
|
||||
escaped = false;
|
||||
}
|
||||
c if escaped => {
|
||||
arg.push('\\');
|
||||
arg.push(c);
|
||||
escaped = false;
|
||||
}
|
||||
'\\' => escaped = true,
|
||||
'"' => quoted = false,
|
||||
c => arg.push(c),
|
||||
}
|
||||
} else {
|
||||
match c {
|
||||
c if escaped => {
|
||||
arg.push(c);
|
||||
arg_exists = true;
|
||||
escaped = false;
|
||||
}
|
||||
c if c.is_whitespace() => {
|
||||
if arg_exists {
|
||||
args.push(arg);
|
||||
arg = String::new();
|
||||
arg_exists = false;
|
||||
}
|
||||
}
|
||||
'\\' => escaped = true,
|
||||
'"' => {
|
||||
quoted = true;
|
||||
arg_exists = true;
|
||||
}
|
||||
c => {
|
||||
arg.push(c);
|
||||
arg_exists = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if quoted {
|
||||
return Err("Unclosed trailing quote");
|
||||
}
|
||||
if escaped {
|
||||
return Err("Unfinished trailing escape");
|
||||
}
|
||||
|
||||
if arg_exists {
|
||||
args.push(arg);
|
||||
}
|
||||
|
||||
Ok(args)
|
||||
}
|
||||
|
||||
pub struct Clap<C>(pub C);
|
||||
|
||||
#[async_trait]
|
||||
impl<B, E, C> Command<B, E> for Clap<C>
|
||||
where
|
||||
B: Send,
|
||||
E: From<conn::Error>,
|
||||
C: ClapCommand<B, E> + Send + Sync,
|
||||
C::Args: Parser + Send,
|
||||
{
|
||||
fn description(&self, _ctx: &Context) -> Option<String> {
|
||||
C::Args::command().get_about().map(|s| format!("{s}"))
|
||||
}
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
arg: &str,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
bot: &mut B,
|
||||
) -> Result<bool, E> {
|
||||
let mut args = match parse_quoted_args(arg) {
|
||||
Ok(args) => args,
|
||||
Err(err) => {
|
||||
ctx.reply(msg.id, err).await?;
|
||||
return Ok(true);
|
||||
}
|
||||
};
|
||||
|
||||
// Hacky, but it should work fine in most cases
|
||||
let usage = msg.content.strip_suffix(arg).unwrap_or("<command>").trim();
|
||||
args.insert(0, usage.to_string());
|
||||
|
||||
let args = match C::Args::try_parse_from(args) {
|
||||
Ok(args) => args,
|
||||
Err(err) => {
|
||||
ctx.reply(msg.id, format!("{}", err.render())).await?;
|
||||
return Ok(true);
|
||||
}
|
||||
};
|
||||
|
||||
self.0.execute(args, msg, ctx, bot).await
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::parse_quoted_args;
|
||||
|
||||
fn assert_quoted(raw: &str, parsed: &[&str]) {
|
||||
let parsed = parsed.iter().map(|s| s.to_string()).collect();
|
||||
assert_eq!(parse_quoted_args(raw), Ok(parsed))
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_parse_quoted_args() {
|
||||
assert_quoted("foo bar baz", &["foo", "bar", "baz"]);
|
||||
assert_quoted(" foo bar baz ", &["foo", "bar", "baz"]);
|
||||
assert_quoted("foo\\ ba\"r ba\"z", &["foo bar baz"]);
|
||||
assert_quoted(
|
||||
"It's a nice day, isn't it?",
|
||||
&["It's", "a", "nice", "day,", "isn't", "it?"],
|
||||
);
|
||||
|
||||
// Trailing whitespace
|
||||
assert_quoted("a ", &["a"]);
|
||||
assert_quoted("a\\ ", &["a "]);
|
||||
assert_quoted("a\\ ", &["a "]);
|
||||
|
||||
// Zero-length arguments
|
||||
assert_quoted("a \"\" b \"\"", &["a", "", "b", ""]);
|
||||
assert_quoted("a \"\" b \"\" ", &["a", "", "b", ""]);
|
||||
|
||||
// Backslashes in quotes
|
||||
assert_quoted("\"a \\b \\\" \\\\\"", &["a \\b \" \\"]);
|
||||
|
||||
// Unclosed quotes and unfinished escapes
|
||||
assert!(parse_quoted_args("foo 'bar \"baz").is_err());
|
||||
assert!(parse_quoted_args("foo \"bar baz").is_err());
|
||||
assert!(parse_quoted_args("foo \"bar 'baz").is_err());
|
||||
assert!(parse_quoted_args("foo \\").is_err());
|
||||
assert!(parse_quoted_args("foo 'bar\\").is_err());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,29 +0,0 @@
|
|||
use async_trait::async_trait;
|
||||
|
||||
use crate::api::Message;
|
||||
|
||||
use super::{Command, Context};
|
||||
|
||||
pub struct Hidden<C>(pub C);
|
||||
|
||||
#[async_trait]
|
||||
impl<B, E, C> Command<B, E> for Hidden<C>
|
||||
where
|
||||
B: Send,
|
||||
C: Command<B, E> + Send + Sync,
|
||||
{
|
||||
fn description(&self, _ctx: &Context) -> Option<String> {
|
||||
// Default implementation, repeated here for emphasis.
|
||||
None
|
||||
}
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
arg: &str,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
bot: &mut B,
|
||||
) -> Result<bool, E> {
|
||||
self.0.execute(arg, msg, ctx, bot).await
|
||||
}
|
||||
}
|
||||
|
|
@ -1,45 +0,0 @@
|
|||
use async_trait::async_trait;
|
||||
|
||||
use crate::api::Message;
|
||||
|
||||
use super::{Command, Context};
|
||||
|
||||
pub struct Prefixed<C> {
|
||||
prefix: String,
|
||||
inner: C,
|
||||
}
|
||||
|
||||
impl<C> Prefixed<C> {
|
||||
pub fn new<S: ToString>(prefix: S, inner: C) -> Self {
|
||||
Self {
|
||||
prefix: prefix.to_string(),
|
||||
inner,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[async_trait]
|
||||
impl<B, E, C> Command<B, E> for Prefixed<C>
|
||||
where
|
||||
B: Send,
|
||||
C: Command<B, E> + Send + Sync,
|
||||
{
|
||||
fn description(&self, ctx: &Context) -> Option<String> {
|
||||
let inner = self.inner.description(ctx)?;
|
||||
Some(format!("{} - {inner}", self.prefix))
|
||||
}
|
||||
|
||||
async fn execute(
|
||||
&self,
|
||||
arg: &str,
|
||||
msg: &Message,
|
||||
ctx: &Context,
|
||||
bot: &mut B,
|
||||
) -> Result<bool, E> {
|
||||
if let Some(rest) = arg.trim_start().strip_prefix(&self.prefix) {
|
||||
self.inner.execute(rest, msg, ctx, bot).await
|
||||
} else {
|
||||
Ok(false)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,93 +0,0 @@
|
|||
use crate::api::packet::ParsedPacket;
|
||||
use crate::api::{Data, SendEvent};
|
||||
use crate::conn;
|
||||
|
||||
use super::command::{Command, Context};
|
||||
use super::instance::{ConnSnapshot, InstanceConfig};
|
||||
|
||||
pub struct Commands<B, E> {
|
||||
commands: Vec<Box<dyn Command<B, E> + Send + Sync>>,
|
||||
fallthrough: bool,
|
||||
}
|
||||
|
||||
impl<B, E> Commands<B, E> {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
commands: vec![],
|
||||
fallthrough: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether further commands should be executed after a command returns
|
||||
/// `true`.
|
||||
///
|
||||
/// If disabled, commands are run until the first command that returns
|
||||
/// `true`. If enabled, all commands are run irrespective of their return
|
||||
/// values.
|
||||
pub fn fallthrough(&self) -> bool {
|
||||
self.fallthrough
|
||||
}
|
||||
|
||||
/// Set whether fallthrough is active.
|
||||
///
|
||||
/// See [`Self::fallthrough`] for more details.
|
||||
pub fn set_fallthrough(&mut self, active: bool) {
|
||||
self.fallthrough = active;
|
||||
}
|
||||
|
||||
pub fn add<C>(&mut self, command: C)
|
||||
where
|
||||
C: Command<B, E> + Send + Sync + 'static,
|
||||
{
|
||||
self.commands.push(Box::new(command));
|
||||
}
|
||||
|
||||
pub fn descriptions(&self, ctx: &Context) -> Vec<String> {
|
||||
self.commands
|
||||
.iter()
|
||||
.filter_map(|c| c.description(ctx))
|
||||
.collect::<Vec<_>>()
|
||||
}
|
||||
|
||||
/// Returns `true` if one or more commands returned `true`, `false`
|
||||
/// otherwise.
|
||||
pub async fn handle_packet(
|
||||
&self,
|
||||
config: &InstanceConfig,
|
||||
packet: &ParsedPacket,
|
||||
snapshot: &ConnSnapshot,
|
||||
bot: &mut B,
|
||||
) -> Result<bool, E> {
|
||||
let msg = match &packet.content {
|
||||
Ok(Data::SendEvent(SendEvent(msg))) => msg,
|
||||
_ => return Ok(false),
|
||||
};
|
||||
|
||||
let joined = match &snapshot.state {
|
||||
conn::State::Joining(_) => return Ok(false),
|
||||
conn::State::Joined(joined) => joined.clone(),
|
||||
};
|
||||
|
||||
let ctx = Context {
|
||||
config: config.clone(),
|
||||
conn_tx: snapshot.conn_tx.clone(),
|
||||
joined,
|
||||
};
|
||||
|
||||
let mut handled = false;
|
||||
for command in &self.commands {
|
||||
handled = handled || command.execute(&msg.content, msg, &ctx, bot).await?;
|
||||
if !self.fallthrough && handled {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(handled)
|
||||
}
|
||||
}
|
||||
|
||||
impl<B, E> Default for Commands<B, E> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
|
@ -1,534 +0,0 @@
|
|||
//! A single instance of a bot in a single room.
|
||||
//!
|
||||
//! See [`Instance`] for more details.
|
||||
|
||||
use std::convert::Infallible;
|
||||
use std::fmt;
|
||||
use std::str::FromStr;
|
||||
use std::sync::{Arc, Mutex};
|
||||
use std::time::Duration;
|
||||
|
||||
use cookie::{Cookie, CookieJar};
|
||||
use tokio::select;
|
||||
use tokio::sync::{mpsc, oneshot};
|
||||
use tokio_tungstenite::tungstenite;
|
||||
use tokio_tungstenite::tungstenite::http::{HeaderValue, StatusCode};
|
||||
|
||||
use crate::api::packet::ParsedPacket;
|
||||
use crate::api::{Auth, AuthOption, Data, Nick};
|
||||
use crate::conn::{self, Conn, ConnTx, State};
|
||||
|
||||
macro_rules! ilog {
|
||||
( $conf:expr, $target:expr, $($arg:tt)+ ) => {
|
||||
::log::log!(
|
||||
target: &format!("euphoxide::live::{}", $conf.name),
|
||||
$target,
|
||||
$($arg)+
|
||||
);
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! idebug {
|
||||
( $conf:expr, $($arg:tt)+ ) => {
|
||||
ilog!($conf, ::log::Level::Debug, $($arg)+);
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! iinfo {
|
||||
( $conf:expr, $($arg:tt)+ ) => {
|
||||
ilog!($conf, ::log::Level::Info, $($arg)+);
|
||||
};
|
||||
}
|
||||
|
||||
macro_rules! iwarn {
|
||||
( $conf:expr, $($arg:tt)+ ) => {
|
||||
ilog!($conf, ::log::Level::Warn, $($arg)+);
|
||||
};
|
||||
}
|
||||
|
||||
/// Settings that are usually shared between all instances connecting to a
|
||||
/// specific server.
|
||||
#[derive(Clone)]
|
||||
pub struct ServerConfig {
|
||||
/// How long to wait for the server until an operation is considered timed
|
||||
/// out.
|
||||
///
|
||||
/// This timeout applies to waiting for reply packets to command packets
|
||||
/// sent by the client, as well as operations like connecting or closing a
|
||||
/// connection.
|
||||
pub timeout: Duration,
|
||||
/// How long to wait until reconnecting after an unsuccessful attempt to
|
||||
/// connect.
|
||||
pub reconnect_delay: Duration,
|
||||
/// Domain name, to be used with [`Conn::connect`].
|
||||
pub domain: String,
|
||||
/// Cookies to use when connecting. They are updated with the server's reply
|
||||
/// after successful connection attempts.
|
||||
pub cookies: Arc<Mutex<CookieJar>>,
|
||||
}
|
||||
|
||||
impl ServerConfig {
|
||||
pub fn timeout(mut self, timeout: Duration) -> Self {
|
||||
self.timeout = timeout;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn reconnect_delay(mut self, reconnect_delay: Duration) -> Self {
|
||||
self.reconnect_delay = reconnect_delay;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn domain<S: ToString>(mut self, domain: S) -> Self {
|
||||
self.domain = domain.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn cookies(mut self, cookies: Arc<Mutex<CookieJar>>) -> Self {
|
||||
self.cookies = cookies;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn room<S: ToString>(self, room: S) -> InstanceConfig {
|
||||
InstanceConfig::new(self, room)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for ServerConfig {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
timeout: Duration::from_secs(30),
|
||||
reconnect_delay: Duration::from_secs(30),
|
||||
domain: "euphoria.leet.nu".to_string(),
|
||||
cookies: Arc::new(Mutex::new(CookieJar::new())),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct Hidden;
|
||||
|
||||
impl fmt::Debug for Hidden {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "<hidden>")
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Debug for ServerConfig {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("ServerConfig")
|
||||
.field("timeout", &self.timeout)
|
||||
.field("reconnect_delay", &self.reconnect_delay)
|
||||
.field("domain", &self.domain)
|
||||
.field("cookies", &Hidden)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
/// Settings that are usually specific to a single instance.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct InstanceConfig {
|
||||
pub server: ServerConfig,
|
||||
/// Unique name of this instance.
|
||||
pub name: String,
|
||||
/// Room name, to be used with [`Conn::connect`].
|
||||
pub room: String,
|
||||
/// Whether the instance should connect as human or bot.
|
||||
pub human: bool,
|
||||
/// Username to set upon connecting.
|
||||
pub username: Option<String>,
|
||||
/// Whether to set the username even if the server reports that the session
|
||||
/// already has a username set.
|
||||
pub force_username: bool,
|
||||
/// Password to use if room requires authentication.
|
||||
pub password: Option<String>,
|
||||
}
|
||||
|
||||
impl InstanceConfig {
|
||||
pub fn new<S: ToString>(server: ServerConfig, room: S) -> Self {
|
||||
Self {
|
||||
server,
|
||||
name: room.to_string(),
|
||||
room: room.to_string(),
|
||||
human: false,
|
||||
username: None,
|
||||
force_username: false,
|
||||
password: None,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn name<S: ToString>(mut self, name: S) -> Self {
|
||||
self.name = name.to_string();
|
||||
self
|
||||
}
|
||||
|
||||
pub fn human(mut self, human: bool) -> Self {
|
||||
self.human = human;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn username<S: ToString>(mut self, username: Option<S>) -> Self {
|
||||
self.username = username.map(|s| s.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
pub fn force_username(mut self, force_username: bool) -> Self {
|
||||
self.force_username = force_username;
|
||||
self
|
||||
}
|
||||
|
||||
pub fn password<S: ToString>(mut self, password: Option<S>) -> Self {
|
||||
self.password = password.map(|s| s.to_string());
|
||||
self
|
||||
}
|
||||
|
||||
/// Create a new instance using this config.
|
||||
///
|
||||
/// See [`Instance::new`] for more details.
|
||||
pub fn build<F>(self, on_event: F) -> Instance
|
||||
where
|
||||
F: Fn(Event) + Send + Sync + 'static,
|
||||
{
|
||||
Instance::new(self, on_event)
|
||||
}
|
||||
}
|
||||
|
||||
/// Snapshot of a [`Conn`]'s state immediately after receiving a packet.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct ConnSnapshot {
|
||||
pub conn_tx: ConnTx,
|
||||
pub state: State,
|
||||
}
|
||||
|
||||
impl ConnSnapshot {
|
||||
fn from_conn(conn: &Conn) -> Self {
|
||||
Self {
|
||||
conn_tx: conn.tx().clone(),
|
||||
state: conn.state().clone(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Most of the time, the largest variant (`Packet`) is sent. The size of this
|
||||
// enum is not critical anyways since it's not constructed that often.
|
||||
#[allow(clippy::large_enum_variant)]
|
||||
/// An event emitted by an [`Instance`].
|
||||
///
|
||||
/// Events are emitted by a single instance following this schema, written in
|
||||
/// pseudo-regex syntax:
|
||||
/// ```text
|
||||
/// (Connecting (Connected Packet*)? Disconnected)* Stopped
|
||||
/// ```
|
||||
///
|
||||
/// In particular, this means that every [`Self::Connecting`] is always followed
|
||||
/// by exactly one [`Self::Disconnected`], and that [`Self::Stopped`] is always
|
||||
/// the last event and is always sent exactly once per instance.
|
||||
#[derive(Debug)]
|
||||
pub enum Event {
|
||||
Connecting(InstanceConfig),
|
||||
Connected(InstanceConfig, ConnSnapshot),
|
||||
Packet(InstanceConfig, ParsedPacket, ConnSnapshot),
|
||||
Disconnected(InstanceConfig),
|
||||
Stopped(InstanceConfig),
|
||||
}
|
||||
|
||||
impl Event {
|
||||
pub fn config(&self) -> &InstanceConfig {
|
||||
match self {
|
||||
Self::Connecting(config) => config,
|
||||
Self::Connected(config, _) => config,
|
||||
Self::Packet(config, _, _) => config,
|
||||
Self::Disconnected(config) => config,
|
||||
Self::Stopped(config) => config,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
enum Request {
|
||||
GetConnTx(oneshot::Sender<ConnTx>),
|
||||
Stop,
|
||||
}
|
||||
|
||||
/// An error that occurred inside an [`Instance`] while it was running.
|
||||
enum RunError {
|
||||
StoppedManually,
|
||||
InstanceDropped,
|
||||
CouldNotConnect(conn::Error),
|
||||
Conn(conn::Error),
|
||||
}
|
||||
|
||||
/// A single instance of a bot in a single room.
|
||||
///
|
||||
/// The instance automatically connects to its room once it is created, and it
|
||||
/// reconnects when it loses connection. If the room requires authentication and
|
||||
/// a password is given, the instance automatically authenticates. If a nick is
|
||||
/// given, the instance sets its nick upon joining the room.
|
||||
///
|
||||
/// An instance has a unique name used for logging and identifying the instance.
|
||||
/// The room name can be used as the instance name if there is never more than
|
||||
/// one instance per room.
|
||||
///
|
||||
/// An instance can be created using [`Instance::new`] or using
|
||||
/// [`InstanceConfig::build`].
|
||||
///
|
||||
/// An instance can be stopped using [`Instance::stop`] or by dropping it. In
|
||||
/// either case, the last event the instance sends will be an
|
||||
/// [`Event::Stopped`]. If it is not stopped using one of these two ways, it
|
||||
/// will continue to run and reconnect indefinitely.
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Instance {
|
||||
config: InstanceConfig,
|
||||
request_tx: mpsc::UnboundedSender<Request>,
|
||||
// In theory, request_tx should be sufficient as canary, but I'm not sure
|
||||
// exactly how to check it during the reconnect timeout.
|
||||
_canary_tx: mpsc::UnboundedSender<Infallible>,
|
||||
}
|
||||
|
||||
impl Instance {
|
||||
// Previously, the event callback was asynchronous and would return a result. It
|
||||
// was called in-line to calling Conn::recv. The idea was that the instance
|
||||
// would stop if the event handler returned Err. This was, however, not even
|
||||
// implemented correctly and the instance would just reconnect.
|
||||
//
|
||||
// The new event handler is synchronous. This way, it becomes harder to
|
||||
// accidentally block Conn::recv, for example by waiting for a channel with
|
||||
// limited capacity. If async code must be executed upon receiving a command,
|
||||
// the user can start a task from inside the handler.
|
||||
//
|
||||
// The new event handler does not return anything. This makes the code nicer. In
|
||||
// the use cases I'm thinking of, it should not be a problem: If the event
|
||||
// handler encounters errors, there's usually other ways to tell the same. Make
|
||||
// the event handler ignore the errors and stop the instance in that other way.
|
||||
|
||||
/// Create a new instance based on an [`InstanceConfig`].
|
||||
///
|
||||
/// The `on_event` parameter is called whenever the instance wants to emit
|
||||
/// an [`Event`]. It must not block for long. See [`Event`] for more details
|
||||
/// on the events and the order in which they are emitted.
|
||||
///
|
||||
/// [`InstanceConfig::build`] can be used in place of this function.
|
||||
pub fn new<F>(config: InstanceConfig, on_event: F) -> Self
|
||||
where
|
||||
F: Fn(Event) + Send + Sync + 'static,
|
||||
{
|
||||
idebug!(config, "Created with config {config:?}");
|
||||
|
||||
let (request_tx, request_rx) = mpsc::unbounded_channel();
|
||||
let (canary_tx, canary_rx) = mpsc::unbounded_channel();
|
||||
|
||||
tokio::spawn(Self::run::<F>(
|
||||
config.clone(),
|
||||
on_event,
|
||||
request_rx,
|
||||
canary_rx,
|
||||
));
|
||||
|
||||
Self {
|
||||
config,
|
||||
request_tx,
|
||||
_canary_tx: canary_tx,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn config(&self) -> &InstanceConfig {
|
||||
&self.config
|
||||
}
|
||||
|
||||
/// Retrieve the instance's current connection.
|
||||
///
|
||||
/// Returns `None` if the instance is currently not connected, or has
|
||||
/// stopped running.
|
||||
pub async fn conn_tx(&self) -> Option<ConnTx> {
|
||||
let (tx, rx) = oneshot::channel();
|
||||
let _ = self.request_tx.send(Request::GetConnTx(tx));
|
||||
rx.await.ok()
|
||||
}
|
||||
|
||||
/// Stop the instance.
|
||||
///
|
||||
/// For more info on stopping instances, see [`Instance`].
|
||||
pub fn stop(&self) {
|
||||
let _ = self.request_tx.send(Request::Stop);
|
||||
}
|
||||
|
||||
/// Whether this instance is stopped.
|
||||
///
|
||||
/// For more info on stopping instances, see [`Instance`].
|
||||
pub fn stopped(&self) -> bool {
|
||||
self.request_tx.is_closed()
|
||||
}
|
||||
|
||||
async fn run<F: Fn(Event)>(
|
||||
config: InstanceConfig,
|
||||
on_event: F,
|
||||
request_rx: mpsc::UnboundedReceiver<Request>,
|
||||
mut canary_rx: mpsc::UnboundedReceiver<Infallible>,
|
||||
) {
|
||||
select! {
|
||||
_ = Self::stay_connected(&config, &on_event, request_rx) => (),
|
||||
_ = canary_rx.recv() => { idebug!(config, "Instance dropped"); },
|
||||
}
|
||||
on_event(Event::Stopped(config))
|
||||
}
|
||||
|
||||
async fn stay_connected<F: Fn(Event)>(
|
||||
config: &InstanceConfig,
|
||||
on_event: &F,
|
||||
mut request_rx: mpsc::UnboundedReceiver<Request>,
|
||||
) {
|
||||
loop {
|
||||
idebug!(config, "Connecting...");
|
||||
|
||||
on_event(Event::Connecting(config.clone()));
|
||||
let result = Self::run_once::<F>(config, on_event, &mut request_rx).await;
|
||||
on_event(Event::Disconnected(config.clone()));
|
||||
|
||||
let connected = match result {
|
||||
Ok(()) => {
|
||||
idebug!(config, "Connection closed normally");
|
||||
true
|
||||
}
|
||||
Err(RunError::StoppedManually) => {
|
||||
idebug!(config, "Instance stopped manually");
|
||||
break;
|
||||
}
|
||||
Err(RunError::InstanceDropped) => {
|
||||
idebug!(config, "Instance dropped");
|
||||
break;
|
||||
}
|
||||
Err(RunError::CouldNotConnect(conn::Error::Tungstenite(
|
||||
tungstenite::Error::Http(response),
|
||||
))) if response.status() == StatusCode::NOT_FOUND => {
|
||||
iwarn!(config, "Failed to connect: room does not exist");
|
||||
break;
|
||||
}
|
||||
Err(RunError::CouldNotConnect(err)) => {
|
||||
iwarn!(config, "Failed to connect: {err}");
|
||||
false
|
||||
}
|
||||
Err(RunError::Conn(err)) => {
|
||||
iwarn!(config, "An error occurred: {err}");
|
||||
true
|
||||
}
|
||||
};
|
||||
|
||||
if !connected {
|
||||
let s = config.server.reconnect_delay.as_secs();
|
||||
idebug!(config, "Waiting {s} seconds before reconnecting");
|
||||
tokio::time::sleep(config.server.reconnect_delay).await;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_cookies(config: &InstanceConfig) -> HeaderValue {
|
||||
let guard = config.server.cookies.lock().unwrap();
|
||||
let cookies = guard
|
||||
.iter()
|
||||
.map(|c| format!("{}", c.stripped()))
|
||||
.collect::<Vec<_>>()
|
||||
.join("; ");
|
||||
drop(guard);
|
||||
cookies.try_into().unwrap()
|
||||
}
|
||||
|
||||
fn set_cookies(config: &InstanceConfig, cookies: Vec<HeaderValue>) {
|
||||
idebug!(config, "Updating cookies");
|
||||
let mut guard = config.server.cookies.lock().unwrap();
|
||||
|
||||
for cookie in cookies {
|
||||
if let Ok(cookie) = cookie.to_str() {
|
||||
if let Ok(cookie) = Cookie::from_str(cookie) {
|
||||
guard.add(cookie);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async fn run_once<F: Fn(Event)>(
|
||||
config: &InstanceConfig,
|
||||
on_event: &F,
|
||||
request_rx: &mut mpsc::UnboundedReceiver<Request>,
|
||||
) -> Result<(), RunError> {
|
||||
let (mut conn, cookies) = Conn::connect(
|
||||
&config.server.domain,
|
||||
&config.room,
|
||||
config.human,
|
||||
Some(Self::get_cookies(config)),
|
||||
config.server.timeout,
|
||||
)
|
||||
.await
|
||||
.map_err(RunError::CouldNotConnect)?;
|
||||
|
||||
Self::set_cookies(config, cookies);
|
||||
on_event(Event::Connected(
|
||||
config.clone(),
|
||||
ConnSnapshot::from_conn(&conn),
|
||||
));
|
||||
|
||||
let conn_tx = conn.tx().clone();
|
||||
select! {
|
||||
r = Self::receive::<F>(config, &mut conn, on_event) => r,
|
||||
r = Self::handle_requests(request_rx, &conn_tx) => Err(r),
|
||||
}
|
||||
}
|
||||
|
||||
async fn receive<F: Fn(Event)>(
|
||||
config: &InstanceConfig,
|
||||
conn: &mut Conn,
|
||||
on_event: &F,
|
||||
) -> Result<(), RunError> {
|
||||
loop {
|
||||
let packet = conn.recv().await.map_err(RunError::Conn)?;
|
||||
let snapshot = ConnSnapshot::from_conn(conn);
|
||||
|
||||
match &packet.content {
|
||||
Ok(Data::SnapshotEvent(snapshot)) => {
|
||||
if let Some(username) = &config.username {
|
||||
if config.force_username || snapshot.nick.is_none() {
|
||||
idebug!(config, "Setting nick to username {username}");
|
||||
let name = username.to_string();
|
||||
conn.tx().send_only(Nick { name });
|
||||
} else if let Some(nick) = &snapshot.nick {
|
||||
idebug!(config, "Not setting nick, already set to {nick}");
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(Data::BounceEvent(_)) => {
|
||||
if let Some(password) = &config.password {
|
||||
idebug!(config, "Authenticating with password");
|
||||
let cmd = Auth {
|
||||
r#type: AuthOption::Passcode,
|
||||
passcode: Some(password.to_string()),
|
||||
};
|
||||
conn.tx().send_only(cmd);
|
||||
} else {
|
||||
iwarn!(config, "Auth required but no password configured");
|
||||
}
|
||||
}
|
||||
Ok(Data::DisconnectEvent(ev)) => {
|
||||
if ev.reason == "authentication changed" {
|
||||
iinfo!(config, "Disconnected because {}", ev.reason);
|
||||
} else {
|
||||
iwarn!(config, "Disconnected because {}", ev.reason);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
on_event(Event::Packet(config.clone(), packet, snapshot));
|
||||
}
|
||||
}
|
||||
|
||||
async fn handle_requests(
|
||||
request_rx: &mut mpsc::UnboundedReceiver<Request>,
|
||||
conn_tx: &ConnTx,
|
||||
) -> RunError {
|
||||
while let Some(request) = request_rx.recv().await {
|
||||
match request {
|
||||
Request::GetConnTx(tx) => {
|
||||
let _ = tx.send(conn_tx.clone());
|
||||
}
|
||||
Request::Stop => return RunError::StoppedManually,
|
||||
}
|
||||
}
|
||||
RunError::InstanceDropped
|
||||
}
|
||||
}
|
||||
|
|
@ -1,70 +0,0 @@
|
|||
//! A convenient way to keep a [`ServerConfig`] and some [`Instance`]s.
|
||||
|
||||
use std::collections::HashMap;
|
||||
|
||||
use super::instance::{self, Instance, ServerConfig};
|
||||
|
||||
/// A convenient way to keep a [`ServerConfig`] and some [`Instance`]s.
|
||||
pub struct Instances {
|
||||
server_config: ServerConfig,
|
||||
instances: HashMap<String, Instance>,
|
||||
}
|
||||
|
||||
impl Instances {
|
||||
pub fn new(server_config: ServerConfig) -> Self {
|
||||
Self {
|
||||
server_config,
|
||||
instances: HashMap::new(),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn server_config(&self) -> &ServerConfig {
|
||||
&self.server_config
|
||||
}
|
||||
|
||||
pub fn instances(&self) -> impl Iterator<Item = &Instance> {
|
||||
self.instances.values()
|
||||
}
|
||||
|
||||
/// Check if an event comes from an instance whose name is known.
|
||||
///
|
||||
/// Assuming every instance has a unique name, events from unknown instances
|
||||
/// should be discarded. This helps prevent "ghost instances" that were
|
||||
/// stopped but haven't yet disconnected properly from influencing your
|
||||
/// bot's state.
|
||||
///
|
||||
/// The user is responsible for ensuring that instances' names are unique.
|
||||
pub fn is_from_known_instance(&self, event: &instance::Event) -> bool {
|
||||
self.instances.contains_key(&event.config().name)
|
||||
}
|
||||
|
||||
pub fn is_empty(&self) -> bool {
|
||||
self.instances.is_empty()
|
||||
}
|
||||
|
||||
/// Get an instance by its name.
|
||||
pub fn get(&self, name: &str) -> Option<&Instance> {
|
||||
self.instances.get(name)
|
||||
}
|
||||
|
||||
/// Add a new instance.
|
||||
///
|
||||
/// If an instance with the same name exists already, it will be replaced by
|
||||
/// the new instance.
|
||||
pub fn add(&mut self, instance: Instance) {
|
||||
self.instances
|
||||
.insert(instance.config().name.clone(), instance);
|
||||
}
|
||||
|
||||
/// Remove an instance by its name.
|
||||
pub fn remove(&mut self, name: &str) -> Option<Instance> {
|
||||
self.instances.remove(name)
|
||||
}
|
||||
|
||||
/// Remove all stopped instances.
|
||||
///
|
||||
/// This function should be called regularly.
|
||||
pub fn purge(&mut self) {
|
||||
self.instances.retain(|_, i| !i.stopped());
|
||||
}
|
||||
}
|
||||
|
|
@ -1,9 +1 @@
|
|||
pub mod api;
|
||||
#[cfg(feature = "bot")]
|
||||
pub mod bot;
|
||||
pub mod conn;
|
||||
mod emoji;
|
||||
pub mod nick;
|
||||
mod replies;
|
||||
|
||||
pub use emoji::Emoji;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue