diff --git a/src/lib.rs b/src/lib.rs index f40a0c2..419b65a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -26,5 +26,6 @@ pub mod bookmarks; pub mod filetransfer; pub mod fs; pub mod host; +pub mod system; pub mod ui; pub mod utils; diff --git a/src/main.rs b/src/main.rs index f41067d..afd0a06 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,6 +40,7 @@ mod bookmarks; mod filetransfer; mod fs; mod host; +mod system; mod ui; mod utils; diff --git a/src/system/bookmarks_client.rs b/src/system/bookmarks_client.rs new file mode 100644 index 0000000..791cc92 --- /dev/null +++ b/src/system/bookmarks_client.rs @@ -0,0 +1,107 @@ +//! ## BookmarksClient +//! +//! `bookmarks_client` is the module which provides an API between the Bookmarks module and the system + +/* +* +* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com +* +* This file is part of "TermSCP" +* +* TermSCP is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* TermSCP is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with TermSCP. If not, see . +* +*/ + +// Local +use crate::bookmarks::serializer::BookmarkSerializer; +use crate::bookmarks::{Bookmark, SerializerError, SerializerErrorKind, UserHosts}; +use crate::filetransfer::FileTransferProtocol; +// Ext +use std::fs::OpenOptions; +use std::path::{Path, PathBuf}; + +/// ## BookmarksClient +/// +/// BookmarksClient provides a layer between the host system and the bookmarks module +pub struct BookmarksClient { + pub hosts: UserHosts, + bookmarks_file: PathBuf, + key_file: PathBuf, +} + +impl BookmarksClient { + /// ### BookmarksClient + /// + /// Instantiates a new BookmarksClient + /// Bookmarks file path must be provided + /// Key file must be provided + pub fn new(bookmarks_file: &Path, key_file: &Path) -> Result { + // Create default hosts + let default_hosts: UserHosts = Default::default(); + let client: BookmarksClient = BookmarksClient { + hosts: default_hosts, + bookmarks_file: PathBuf::from(bookmarks_file), + key_file: PathBuf::from(key_file), + }; + // If bookmark file doesn't exist, initialize it + if !bookmarks_file.exists() { + if let Err(err) = client.write_bookmarks() { + return Err(err) + } + } + // If key file doesn't exist, create key + if !key_file.exists() { + if let Err(err) = client.generate_key() { + return Err(err) + } + } + Ok(client) + } + + /// ### make_bookmark + /// + /// Make bookmark from credentials + pub fn make_bookmark(&self, addr: String, port: u16, protocol: FileTransferProtocol, username: String, password: Option) -> Bookmark { + // TODO: crypt password + } + + /// ### write_bookmarks + /// + /// Write bookmarks to file + pub fn write_bookmarks(&self) -> Result<(), SerializerError> { + // Open file + match OpenOptions::new() + .create(true) + .write(true) + .truncate(true) + .open(self.bookmarks_file.as_path()) + { + Ok(writer) => { + let serializer: BookmarkSerializer = BookmarkSerializer {}; + serializer.serialize(Box::new(writer), &self.hosts) + } + Err(err) => Err(SerializerError::new_ex( + SerializerErrorKind::IoError, + err.to_string(), + )), + } + } + + /// ### generate_key + /// + /// Generate a new AES key and write it to key file + fn generate_key(&self) -> Result<(), SerializerError> { + // TODO: write + } +} diff --git a/src/system/environment.rs b/src/system/environment.rs new file mode 100644 index 0000000..c4af3fe --- /dev/null +++ b/src/system/environment.rs @@ -0,0 +1,68 @@ +//! ## Environment +//! +//! `environment` is the module which provides Path and values for the system environment + +/* +* +* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com +* +* This file is part of "TermSCP" +* +* TermSCP is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* TermSCP is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with TermSCP. If not, see . +* +*/ + +// Deps +extern crate dirs; + +// Ext +use std::path::PathBuf; + +/// ### get_config_dir +/// +/// Get termscp configuration directory path. +/// Returns None, if it's not possible to get it +pub fn init_config_dir() -> Result, String> { + // Get file + lazy_static! { + static ref CONF_DIR: Option = dirs::config_dir(); + } + if CONF_DIR.is_some() { + // Get path of bookmarks + let mut p: PathBuf = CONF_DIR.as_ref().unwrap().clone(); + // Append termscp dir + p.push("termscp/"); + // If directory doesn't exist, create it + match p.exists() { + true => Ok(Some(p)), + false => match std::fs::create_dir(p.as_path()) { + Ok(_) => Ok(Some(p)), + Err(err) => Err(err.to_string()), + }, + } + } else { + Ok(None) + } +} + +#[cfg(test)] +mod tests { + + use super::*; + + #[test] + fn test_system_environment_get_config_dir() { + assert!(init_config_dir().ok().unwrap().is_some()); + } +} diff --git a/src/system/mod.rs b/src/system/mod.rs new file mode 100644 index 0000000..d0fe5be --- /dev/null +++ b/src/system/mod.rs @@ -0,0 +1,28 @@ +//! ## System +//! +//! `system` is the module which contains functions and data types related to current system + +/* +* +* Copyright (C) 2020 Christian Visintin - christian.visintin1997@gmail.com +* +* This file is part of "TermSCP" +* +* TermSCP is free software: you can redistribute it and/or modify +* it under the terms of the GNU General Public License as published by +* the Free Software Foundation, either version 3 of the License, or +* (at your option) any later version. +* +* TermSCP is distributed in the hope that it will be useful, +* but WITHOUT ANY WARRANTY; without even the implied warranty of +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +* GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License +* along with TermSCP. If not, see . +* +*/ + +// modules +pub mod bookmarks_client; +pub mod environment;