2025-11-22 06:20:56 -06:00
|
|
|
//! Internal representations of incoming events.
|
|
|
|
|
|
2025-11-14 05:06:57 -06:00
|
|
|
use irc::proto::{Command, Message};
|
2025-11-09 00:02:38 -06:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
2025-11-22 06:20:56 -06:00
|
|
|
/// Represents an event. Probably from IRC.
|
2025-11-09 00:02:38 -06:00
|
|
|
#[derive(Deserialize, Serialize)]
|
|
|
|
|
pub struct Event {
|
2025-11-22 06:20:56 -06:00
|
|
|
/// Who is the message from?
|
2025-11-14 05:06:57 -06:00
|
|
|
from: String,
|
2025-11-22 06:20:56 -06:00
|
|
|
/// What is the message?
|
2025-11-09 00:02:38 -06:00
|
|
|
message: String,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl Event {
|
2025-11-22 06:20:56 -06:00
|
|
|
/// Creates a new message.
|
2025-11-14 05:06:57 -06:00
|
|
|
pub fn new(from: impl Into<String>, msg: impl Into<String>) -> Self {
|
2025-11-09 00:02:38 -06:00
|
|
|
Self {
|
2025-11-14 05:06:57 -06:00
|
|
|
from: from.into(),
|
2025-11-09 00:02:38 -06:00
|
|
|
message: msg.into(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2025-11-14 05:06:57 -06:00
|
|
|
|
|
|
|
|
impl TryFrom<&Message> for Event {
|
|
|
|
|
type Error = &'static str;
|
|
|
|
|
|
|
|
|
|
fn try_from(value: &Message) -> Result<Self, Self::Error> {
|
|
|
|
|
let from = value.response_target().unwrap_or("unknown").to_string();
|
|
|
|
|
match &value.command {
|
|
|
|
|
Command::PRIVMSG(_channel, message) => Ok(Event {
|
|
|
|
|
from,
|
|
|
|
|
message: message.clone(),
|
|
|
|
|
}),
|
|
|
|
|
_ => Err("Not a PRIVMSG"),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|