privmsg.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. use async_std::sync::Mutex;
  2. use std::{
  3. collections::HashSet,
  4. io,
  5. sync::Arc,
  6. };
  7. use drk::{
  8. net,
  9. serial::{Decodable, Encodable}, Result,
  10. };
  11. pub type PrivMsgId = u32;
  12. #[derive(Debug, Clone)]
  13. pub struct PrivMsg {
  14. pub id: PrivMsgId,
  15. pub nickname: String,
  16. pub channel: String,
  17. pub message: String,
  18. }
  19. impl net::Message for PrivMsg {
  20. fn name() -> &'static str {
  21. "privmsg"
  22. }
  23. }
  24. impl Encodable for PrivMsg {
  25. fn encode<S: io::Write>(&self, mut s: S) -> Result<usize> {
  26. let mut len = 0;
  27. len += self.id.encode(&mut s)?;
  28. len += self.nickname.encode(&mut s)?;
  29. len += self.channel.encode(&mut s)?;
  30. len += self.message.encode(&mut s)?;
  31. Ok(len)
  32. }
  33. }
  34. impl Decodable for PrivMsg {
  35. fn decode<D: io::Read>(mut d: D) -> Result<Self> {
  36. Ok(Self {
  37. id: Decodable::decode(&mut d)?,
  38. nickname: Decodable::decode(&mut d)?,
  39. channel: Decodable::decode(&mut d)?,
  40. message: Decodable::decode(&mut d)?,
  41. })
  42. }
  43. }
  44. pub struct SeenPrivMsgIds {
  45. privmsg_ids: Mutex<HashSet<PrivMsgId>>,
  46. }
  47. pub type SeenPrivMsgIdsPtr = Arc<SeenPrivMsgIds>;
  48. impl SeenPrivMsgIds {
  49. pub fn new() -> Arc<Self> {
  50. Arc::new(Self { privmsg_ids: Mutex::new(HashSet::new()) })
  51. }
  52. pub async fn add_seen(&self, id: u32) {
  53. self.privmsg_ids.lock().await.insert(id);
  54. }
  55. pub async fn is_seen(&self, id: u32) -> bool {
  56. self.privmsg_ids.lock().await.contains(&id)
  57. }
  58. }