privmsg.rs 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::io;
  19. use darkfi_serial::{Decodable, Encodable, ReadExt, SerialDecodable, SerialEncodable};
  20. #[derive(SerialEncodable, SerialDecodable, Clone)]
  21. pub struct PrivMsgEvent {
  22. pub nick: String,
  23. pub msg: String,
  24. pub target: String,
  25. }
  26. #[derive(Clone)]
  27. pub enum EventAction {
  28. PrivMsg(PrivMsgEvent),
  29. }
  30. impl std::string::ToString for PrivMsgEvent {
  31. fn to_string(&self) -> String {
  32. format!(":{}!anon@dark.fi PRIVMSG {} :{}\r\n", self.nick, self.target, self.msg)
  33. }
  34. }
  35. impl Encodable for EventAction {
  36. fn encode<S: io::Write>(&self, mut s: S) -> core::result::Result<usize, io::Error> {
  37. match self {
  38. Self::PrivMsg(event) => {
  39. let mut len = 0;
  40. len += 0u8.encode(&mut s)?;
  41. len += event.encode(s)?;
  42. Ok(len)
  43. }
  44. }
  45. }
  46. }
  47. impl Decodable for EventAction {
  48. fn decode<D: io::Read>(mut d: D) -> core::result::Result<Self, io::Error> {
  49. let type_id = d.read_u8()?;
  50. match type_id {
  51. 0 => Ok(Self::PrivMsg(PrivMsgEvent::decode(d)?)),
  52. _ => Err(io::Error::new(io::ErrorKind::Other, "Bad type ID byte for Event")),
  53. }
  54. }
  55. }