Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hotfix 0.9.1 #142

Merged
merged 5 commits into from
May 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "tuigreet"
version = "0.9.0"
version = "0.9.1"
authors = ["Antoine POPINEAU <[email protected]>"]
edition = "2018"
build = "build.rs"
Expand Down
1 change: 1 addition & 0 deletions contrib/locales/en-US/tuigreet.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ new_command = New command:
shutdown = Shut down
reboot = Reboot

command_missing = No command configured
command_exited = Command exited with
command_failed = Command failed

Expand Down
1 change: 1 addition & 0 deletions contrib/locales/fr-FR/tuigreet.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ command = Nouvelle commande :
shutdown = Éteindre
reboot = Redémarrer

command_missing = Aucune commande configurée
command_exited = La commande a retourné
command_failed = Échec de la commande

Expand Down
54 changes: 35 additions & 19 deletions src/greeter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ use zeroize::Zeroize;

use crate::{
event::Event,
info::{
get_issue, get_last_session, get_last_session_path, get_last_user_name, get_last_user_session, get_last_user_session_path, get_last_user_username, get_min_max_uids, get_sessions, get_users,
},
info::{get_issue, get_last_command, get_last_session_path, get_last_user_command, get_last_user_name, get_last_user_session, get_last_user_username, get_min_max_uids, get_sessions, get_users},
power::PowerOption,
ui::{
common::{masked::MaskedString, menu::Menu, style::Theme},
Expand Down Expand Up @@ -96,7 +94,7 @@ pub enum GreetAlign {
#[default]
Center,
Left,
Right
Right,
}

#[derive(SmartDefault)]
Expand Down Expand Up @@ -204,9 +202,17 @@ impl Greeter {

greeter.parse_options().await;

let sessions = get_sessions(&greeter).unwrap_or_default();

if let SessionSource::None = greeter.session_source {
if !sessions.is_empty() {
greeter.session_source = SessionSource::Session(0);
}
}

greeter.sessions = Menu {
title: fl!("title_session"),
options: get_sessions(&greeter).unwrap_or_default(),
options: sessions,
selected: 0,
};

Expand All @@ -217,29 +223,34 @@ impl Greeter {

// If, on top of that, we should remember their last session.
if greeter.remember_user_session {
if let Ok(ref session_path) = get_last_user_session_path(greeter.username.get()) {
// Set the selected menu option and the session source.
greeter.sessions.selected = greeter.sessions.options.iter().position(|Session { path, .. }| path.as_deref() == Some(session_path)).unwrap_or(0);
greeter.session_source = SessionSource::Session(greeter.sessions.selected);
}

// See if we have the last free-form command from the user.
if let Ok(command) = get_last_user_session(greeter.username.get()) {
if let Ok(command) = get_last_user_command(greeter.username.get()) {
greeter.session_source = SessionSource::Command(command);
}

// If a session was saved, use it and its name.
if let Ok(ref session_path) = get_last_user_session(greeter.username.get()) {
// Set the selected menu option and the session source.
if let Some(index) = greeter.sessions.options.iter().position(|Session { path, .. }| path.as_deref() == Some(session_path)) {
greeter.sessions.selected = index;
greeter.session_source = SessionSource::Session(greeter.sessions.selected);
}
}
}
}
}

// Same thing, but not user specific.
if greeter.remember_session {
if let Ok(ref session_path) = get_last_session_path() {
greeter.sessions.selected = greeter.sessions.options.iter().position(|Session { path, .. }| path.as_deref() == Some(session_path)).unwrap_or(0);
greeter.session_source = SessionSource::Session(greeter.sessions.selected);
if let Ok(command) = get_last_command() {
greeter.session_source = SessionSource::Command(command.trim().to_string());
}

if let Ok(command) = get_last_session() {
greeter.session_source = SessionSource::Command(command.trim().to_string());
if let Ok(ref session_path) = get_last_session_path() {
if let Some(index) = greeter.sessions.options.iter().position(|Session { path, .. }| path.as_deref() == Some(session_path)) {
greeter.sessions.selected = index;
greeter.session_source = SessionSource::Session(greeter.sessions.selected);
}
}
}

Expand Down Expand Up @@ -354,7 +365,7 @@ impl Greeter {
match value.as_str() {
"left" => GreetAlign::Left,
"right" => GreetAlign::Right,
_ => GreetAlign::Center
_ => GreetAlign::Center,
}
} else {
GreetAlign::default()
Expand Down Expand Up @@ -405,7 +416,12 @@ impl Greeter {
opts.optopt("", "window-padding", "padding inside the terminal area (default: 0)", "PADDING");
opts.optopt("", "container-padding", "padding inside the main prompt container (default: 1)", "PADDING");
opts.optopt("", "prompt-padding", "padding between prompt rows (default: 1)", "PADDING");
opts.optopt("", "greet-align", "alignment of the greeting text in the main prompt container (default: 'center')", "[left|center|right]");
opts.optopt(
"",
"greet-align",
"alignment of the greeting text in the main prompt container (default: 'center')",
"[left|center|right]",
);

opts.optopt("", "power-shutdown", "command to run to shut down the system", "'CMD [ARGS]...'");
opts.optopt("", "power-reboot", "command to run to reboot the system", "'CMD [ARGS]...'");
Expand Down
48 changes: 26 additions & 22 deletions src/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ use crate::{

const LAST_USER_USERNAME: &str = "/var/cache/tuigreet/lastuser";
const LAST_USER_NAME: &str = "/var/cache/tuigreet/lastuser-name";
const LAST_SESSION: &str = "/var/cache/tuigreet/lastsession";
const LAST_SESSION_PATH: &str = "/var/cache/tuigreet/lastsession-path";
const LAST_COMMAND: &str = "/var/cache/tuigreet/lastsession";
const LAST_SESSION: &str = "/var/cache/tuigreet/lastsession-path";

const DEFAULT_MIN_UID: u16 = 1000;
const DEFAULT_MAX_UID: u16 = 60000;
Expand Down Expand Up @@ -114,55 +114,59 @@ pub fn write_last_username(username: &MaskedString) {
}

pub fn get_last_session_path() -> Result<PathBuf, io::Error> {
Ok(PathBuf::from(fs::read_to_string(LAST_SESSION_PATH)?.trim()))
Ok(PathBuf::from(fs::read_to_string(LAST_SESSION)?.trim()))
}

pub fn get_last_session() -> Result<String, io::Error> {
Ok(fs::read_to_string(LAST_SESSION)?.trim().to_string())
pub fn get_last_command() -> Result<String, io::Error> {
Ok(fs::read_to_string(LAST_COMMAND)?.trim().to_string())
}

pub fn write_last_session_path<P>(session: &P)
where
P: AsRef<Path>,
{
let _ = fs::write(LAST_SESSION_PATH, session.as_ref().to_string_lossy().as_bytes());
let _ = fs::write(LAST_SESSION, session.as_ref().to_string_lossy().as_bytes());
}

pub fn write_last_session(session: &str) {
let _ = fs::write(LAST_SESSION, session);
pub fn write_last_command(session: &str) {
let _ = fs::write(LAST_COMMAND, session);
}

pub fn get_last_user_session_path(username: &str) -> Result<PathBuf, io::Error> {
Ok(PathBuf::from(fs::read_to_string(format!("{LAST_SESSION_PATH}-{username}"))?.trim()))
pub fn get_last_user_session(username: &str) -> Result<PathBuf, io::Error> {
Ok(PathBuf::from(fs::read_to_string(format!("{LAST_SESSION}-{username}"))?.trim()))
}

pub fn get_last_user_session(username: &str) -> Result<String, io::Error> {
Ok(fs::read_to_string(format!("{LAST_SESSION}-{username}"))?.trim().to_string())
pub fn get_last_user_command(username: &str) -> Result<String, io::Error> {
Ok(fs::read_to_string(format!("{LAST_COMMAND}-{username}"))?.trim().to_string())
}

pub fn write_last_user_session_path<P>(username: &str, session: P)
pub fn write_last_user_session<P>(username: &str, session: P)
where
P: AsRef<Path>,
{
let _ = fs::write(format!("{LAST_SESSION_PATH}-{username}"), session.as_ref().to_string_lossy().as_bytes());
let _ = fs::write(format!("{LAST_SESSION}-{username}"), session.as_ref().to_string_lossy().as_bytes());
}

pub fn delete_last_session_path() {
let _ = fs::remove_file(LAST_SESSION_PATH);
pub fn delete_last_session() {
let _ = fs::remove_file(LAST_SESSION);
}

pub fn write_last_user_session(username: &str, session: &str) {
let _ = fs::write(format!("{LAST_SESSION}-{username}"), session);
}

pub fn delete_last_user_session_path(username: &str) {
let _ = fs::remove_file(format!("{LAST_SESSION_PATH}-{username}"));
pub fn write_last_user_command(username: &str, session: &str) {
let _ = fs::write(format!("{LAST_COMMAND}-{username}"), session);
}

pub fn delete_last_user_session(username: &str) {
let _ = fs::remove_file(format!("{LAST_SESSION}-{username}"));
}

pub fn delete_last_command() {
let _ = fs::remove_file(LAST_COMMAND);
}

pub fn delete_last_user_command(username: &str) {
let _ = fs::remove_file(format!("{LAST_COMMAND}-{username}"));
}

pub fn get_users(min_uid: u16, max_uid: u16) -> Vec<User> {
let users = unsafe { uzers::all_users() };

Expand Down
74 changes: 47 additions & 27 deletions src/ipc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use tokio::sync::{

use crate::{
event::Event,
info::{delete_last_user_session, delete_last_user_session_path, write_last_user_session, write_last_user_session_path, write_last_username},
info::{delete_last_user_command, delete_last_user_session, write_last_user_command, write_last_user_session, write_last_username},
macros::SafeDebug,
ui::sessions::{Session, SessionSource, SessionType},
AuthStatus, Greeter, Mode,
Expand Down Expand Up @@ -69,7 +69,11 @@ impl Ipc {
}

async fn parse_response(&mut self, greeter: &mut Greeter, response: Response) -> Result<(), Box<dyn Error>> {
tracing::info!("received greetd message: {:?}", response);
// Do not display actual message from greetd, which may contain entered information, sometimes passwords.
match response {
Response::Error { ref error_type, .. } => tracing::info!("received greetd error message: {error_type:?}"),
ref response => tracing::info!("received greetd message: {:?}", response),
}

match response {
Response::AuthMessage { auth_message_type, auth_message } => match auth_message_type {
Expand Down Expand Up @@ -124,16 +128,16 @@ impl Ipc {
SessionSource::Command(ref command) => {
tracing::info!("caching last user command: {command}");

write_last_user_session(&greeter.username.value, command);
delete_last_user_session_path(&greeter.username.value);
write_last_user_command(&greeter.username.value, command);
delete_last_user_session(&greeter.username.value);
}

SessionSource::Session(index) => {
if let Some(Session { path: Some(session_path), .. }) = greeter.sessions.options.get(index) {
tracing::info!("caching last user session: {session_path:?}");

write_last_user_session_path(&greeter.username.value, session_path);
delete_last_user_session(&greeter.username.value);
write_last_user_session(&greeter.username.value, session_path);
delete_last_user_command(&greeter.username.value);
}
}

Expand All @@ -148,36 +152,51 @@ impl Ipc {
} else {
tracing::info!("authentication successful, starting session");

let command = greeter.session_source.command(greeter).map(str::to_string);
match greeter.session_source.command(greeter).map(str::to_string) {
None => {
Ipc::cancel(greeter).await;

greeter.message = Some(fl!("command_missing"));
greeter.reset(false).await;
}

Some(command) if command.is_empty() => {
Ipc::cancel(greeter).await;

greeter.message = Some(fl!("command_missing"));
greeter.reset(false).await;
}

if let Some(command) = command {
greeter.done = true;
greeter.mode = Mode::Processing;
Some(command) => {
greeter.done = true;
greeter.mode = Mode::Processing;

let session = Session::get_selected(greeter);
let (command, env) = wrap_session_command(greeter, session, &command);
let session = Session::get_selected(greeter);
let (command, env) = wrap_session_command(greeter, session, &command);

#[cfg(not(debug_assertions))]
self.send(Request::StartSession { cmd: vec![command.to_string()], env }).await;
#[cfg(not(debug_assertions))]
self.send(Request::StartSession { cmd: vec![command.to_string()], env }).await;

#[cfg(debug_assertions)]
{
let _ = command;
let _ = env;
#[cfg(debug_assertions)]
{
let _ = command;
let _ = env;

self
.send(Request::StartSession {
cmd: vec!["true".to_string()],
env: vec![],
})
.await;
self
.send(Request::StartSession {
cmd: vec!["true".to_string()],
env: vec![],
})
.await;
}
}
}
}
}

Response::Error { error_type, description } => {
tracing::info!("received an error from greetd: {error_type:?} - {description}");
Response::Error { error_type, .. } => {
// Do not display actual message from greetd, which may contain entered information, sometimes passwords.
tracing::info!("received an error from greetd: {error_type:?}");

Ipc::cancel(greeter).await;

Expand All @@ -193,7 +212,8 @@ impl Ipc {
}

ErrorType::Error => {
greeter.message = Some(description);
// Do not display actual message from greetd, which may contain entered information, sometimes passwords.
greeter.message = Some("An error was received from greetd".to_string());
greeter.reset(false).await;
}
}
Expand Down
Loading
Loading