settings.rs 6.8 KB

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