settings.rs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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::{
  19. collections::{HashMap, HashSet},
  20. sync::Arc,
  21. };
  22. use crypto_box::PublicKey;
  23. use darkfi::{Error::ParseFailed, Result};
  24. use tracing::info;
  25. use crate::irc::{IrcChannel, IrcContact};
  26. /// Parse configured autojoin channels from a TOML map.
  27. ///
  28. /// ```toml
  29. /// autojoin = ["#dev", "#memes"]
  30. /// ```
  31. pub fn parse_autojoin_channels(data: &toml::Value) -> Result<Vec<String>> {
  32. let mut ret = vec![];
  33. let Some(autojoin) = data.get("autojoin") else { return Ok(ret) };
  34. let Some(autojoin) = autojoin.as_array() else {
  35. return Err(ParseFailed("autojoin not an array"))
  36. };
  37. for item in autojoin {
  38. let Some(channel) = item.as_str() else {
  39. return Err(ParseFailed("autojoin channel not a string"))
  40. };
  41. if !channel.starts_with('#') {
  42. return Err(ParseFailed("autojoin channel not a valid channel"))
  43. }
  44. if ret.contains(&channel.to_string()) {
  45. return Err(ParseFailed("Duplicate autojoin channel found"))
  46. }
  47. ret.push(channel.to_string());
  48. }
  49. Ok(ret)
  50. }
  51. pub fn list_configured_contacts(
  52. data: &toml::Value,
  53. ) -> Result<HashMap<String, (PublicKey, crypto_box::SecretKey)>> {
  54. let mut ret = HashMap::new();
  55. let Some(table) = data.as_table() else { return Err(ParseFailed("TOML not a map")) };
  56. let Some(contacts) = table.get("contact") else { return Ok(ret) };
  57. let Some(contacts) = contacts.as_table() else {
  58. return Err(ParseFailed("`contact` not a map"))
  59. };
  60. for (name, items) in contacts {
  61. let Some(public_str) = items.get("dm_chacha_public") else {
  62. return Err(ParseFailed("Invalid contact configuration dm_chacha_public missing"))
  63. };
  64. let Some(public_str) = public_str.as_str() else {
  65. return Err(ParseFailed("dm_chacha_public not a string"))
  66. };
  67. let Ok(public_bytes) = bs58::decode(public_str).into_vec() else {
  68. return Err(ParseFailed("Invalid base58 for contact pubkey"))
  69. };
  70. if public_bytes.len() != 32 {
  71. return Err(ParseFailed("Invalid contact pubkey (not 32 bytes)"))
  72. }
  73. let public_bytes: [u8; 32] = public_bytes.try_into().unwrap();
  74. let public = crypto_box::PublicKey::from(public_bytes);
  75. if ret.contains_key(name) {
  76. return Err(ParseFailed("Duplicate contact found"))
  77. }
  78. // Parse the secret key for that specific contact
  79. let Some(my_secret) = items.get("my_dm_chacha_secret") else {
  80. return Err(ParseFailed("Invalid contact configuration my_dm_chacha_secret missing. \
  81. You can generate a keypair with: 'darkirc --gen-chacha-keypair' and then add that keypair to your config toml file."))
  82. };
  83. let Some(my_secret_str) = my_secret.as_str() else {
  84. return Err(ParseFailed("my_dm_chacha_secret not a string"))
  85. };
  86. let Ok(my_secret_bytes) = bs58::decode(my_secret_str).into_vec() else {
  87. return Err(ParseFailed("my_dm_chacha_secret not valid base58"))
  88. };
  89. if my_secret_bytes.len() != 32 {
  90. return Err(ParseFailed("my_dm_chacha_secret not 32 bytes long"))
  91. }
  92. let my_secret_bytes: [u8; 32] = my_secret_bytes.try_into().unwrap();
  93. let my_secret = crypto_box::SecretKey::from(my_secret_bytes);
  94. ret.insert(name.to_string(), (public, my_secret));
  95. }
  96. Ok(ret)
  97. }
  98. /// Parse configured contacts from a TOML map.
  99. ///
  100. /// ```toml
  101. /// [contact."anon"]
  102. /// dm_chacha_public = "7CkVuFgwTUpJn5Sv67Q3fyEDpa28yrSeL5Hg2GqQ4jfM"
  103. /// my_dm_chacha_secret = "A3mLrq4aW9UkFVY4zCfR2aLdEEWVUdH4u8v4o2dgi4kC"
  104. /// ```
  105. #[allow(clippy::type_complexity)]
  106. pub fn parse_configured_contacts(data: &toml::Value) -> Result<HashMap<String, IrcContact>> {
  107. let mut ret = HashMap::new();
  108. let contacts = list_configured_contacts(data)?;
  109. if contacts.is_empty() {
  110. return Ok(ret);
  111. }
  112. for (name, (public, my_secret)) in contacts {
  113. let saltbox: Arc<crypto_box::ChaChaBox> =
  114. Arc::new(crypto_box::ChaChaBox::new(&public, &my_secret));
  115. let self_saltbox: Arc<crypto_box::ChaChaBox> =
  116. Arc::new(crypto_box::ChaChaBox::new(&my_secret.public_key(), &my_secret));
  117. if ret.contains_key(&name) {
  118. return Err(ParseFailed("Duplicate contact found"))
  119. }
  120. info!("Instantiated ChaChaBox for contact \"{name}\"");
  121. ret.insert(name.to_string(), IrcContact { saltbox, self_saltbox });
  122. }
  123. Ok(ret)
  124. }
  125. /// Parse a TOML string for any configured channels and return
  126. /// a map containing said configurations.
  127. ///
  128. /// ```toml
  129. /// [channel."#memes"]
  130. /// secret = "7CkVuFgwTUpJn5Sv67Q3fyEDpa28yrSeL5Hg2GqQ4jfM"
  131. /// topic = "Dank Memes"
  132. /// ```
  133. pub fn parse_configured_channels(data: &toml::Value) -> Result<HashMap<String, IrcChannel>> {
  134. let mut ret = HashMap::new();
  135. let Some(table) = data.as_table() else { return Err(ParseFailed("TOML not a map")) };
  136. let Some(chans) = table.get("channel") else { return Ok(ret) };
  137. let Some(chans) = chans.as_table() else { return Err(ParseFailed("`channel` not a map")) };
  138. for (name, items) in chans {
  139. let mut chan = IrcChannel { topic: String::new(), nicks: HashSet::new(), saltbox: None };
  140. if let Some(topic) = items.get("topic") {
  141. if let Some(topic) = topic.as_str() {
  142. info!("Found configured topic for {name}: {topic}");
  143. chan.topic = topic.to_string();
  144. } else {
  145. return Err(ParseFailed("Channel topic not a string"))
  146. }
  147. }
  148. if let Some(secret) = items.get("secret") {
  149. if let Some(secret) = secret.as_str() {
  150. let Ok(secret_bytes) = bs58::decode(secret).into_vec() else {
  151. return Err(ParseFailed("Channel secret not valid base58"))
  152. };
  153. if secret_bytes.len() != 32 {
  154. return Err(ParseFailed("Channel secret not 32 bytes long"))
  155. }
  156. let secret_bytes: [u8; 32] = secret_bytes.try_into().unwrap();
  157. let secret = crypto_box::SecretKey::from(secret_bytes);
  158. let public = secret.public_key();
  159. chan.saltbox = Some(Arc::new(crypto_box::ChaChaBox::new(&public, &secret)));
  160. info!("Configured NaCl box for channel {name}");
  161. } else {
  162. return Err(ParseFailed("Channel secret not a string"))
  163. }
  164. }
  165. info!("Configured channel {name}");
  166. ret.insert(name.to_string(), chan);
  167. }
  168. Ok(ret)
  169. }