mod.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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::{collections::HashSet, sync::Arc};
  19. use crypto_box::ChaChaBox;
  20. use darkfi::{Error, Result};
  21. use darkfi_serial::{async_trait, deserialize_async_partial, SerialDecodable, SerialEncodable};
  22. /// IRC client state
  23. pub(crate) mod client;
  24. /// IRC server implementation
  25. pub(crate) mod server;
  26. /// IRC command handler
  27. pub(crate) mod command;
  28. /// Services implementations
  29. pub(crate) mod services;
  30. pub(crate) use services::nickserv::NickServ;
  31. /// IRC numerics and server replies
  32. pub(crate) mod rpl;
  33. /// Hardcoded server name
  34. const SERVER_NAME: &str = "irc.dark.fi";
  35. pub trait Priv {
  36. fn channel(&mut self) -> &mut String;
  37. fn nick(&mut self) -> &mut String;
  38. fn msg(&mut self) -> &mut String;
  39. }
  40. /// IRC PRIVMSG (old version)
  41. #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
  42. pub struct OldPrivmsg {
  43. pub channel: String,
  44. pub nick: String,
  45. pub msg: String,
  46. }
  47. impl OldPrivmsg {
  48. pub fn into_new(self) -> Privmsg {
  49. Privmsg {
  50. version: 0,
  51. msg_type: 0,
  52. channel: self.channel.clone(),
  53. nick: self.nick.clone(),
  54. msg: self.msg.clone(),
  55. }
  56. }
  57. }
  58. /// IRC PRIVMSG (new version)
  59. #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
  60. pub struct Privmsg {
  61. pub version: u8,
  62. pub msg_type: u8,
  63. pub channel: String,
  64. pub nick: String,
  65. pub msg: String,
  66. }
  67. impl Priv for OldPrivmsg {
  68. fn channel(&mut self) -> &mut String {
  69. &mut self.channel
  70. }
  71. fn nick(&mut self) -> &mut String {
  72. &mut self.nick
  73. }
  74. fn msg(&mut self) -> &mut String {
  75. &mut self.msg
  76. }
  77. }
  78. impl Priv for Privmsg {
  79. fn channel(&mut self) -> &mut String {
  80. &mut self.channel
  81. }
  82. fn nick(&mut self) -> &mut String {
  83. &mut self.nick
  84. }
  85. fn msg(&mut self) -> &mut String {
  86. &mut self.msg
  87. }
  88. }
  89. pub enum Msg {
  90. V1(OldPrivmsg),
  91. V2(Privmsg),
  92. }
  93. impl Msg {
  94. pub async fn deserialize(bytes: &[u8]) -> Result<Self> {
  95. let old_privmsg = deserialize_async_partial(bytes).await;
  96. if let Ok((old_msg, _)) = old_privmsg {
  97. return Ok(Msg::V1(old_msg))
  98. }
  99. let new_privmsg = deserialize_async_partial(bytes).await;
  100. if let Ok((new_msg, _)) = new_privmsg {
  101. return Ok(Msg::V2(new_msg))
  102. }
  103. Err(Error::Custom("Unknown message format".into()))
  104. }
  105. }
  106. /// IRC channel definition
  107. #[derive(Clone)]
  108. pub struct IrcChannel {
  109. pub topic: String,
  110. pub nicks: HashSet<String>,
  111. pub saltbox: Option<Arc<ChaChaBox>>,
  112. }
  113. /// IRC contact definition
  114. #[derive(Clone)]
  115. pub struct IrcContact {
  116. pub saltbox: Option<Arc<ChaChaBox>>,
  117. }