summaryrefslogblamecommitdiffstats
path: root/src/warn.c
blob: 594577c058e1a69ca4a324f95c62218d9ac40981 (plain) (tree)
1
2
3
4
5
6
7
8
9

                 
                    

                   



                      





































                                                                                                                                
                                                                                















                                                                                                 
                                                              

 
                                                                                          
 



                                     








                                                                 
                                                    



                                                                                                      



                                                                              

                                                                       
                                                                                             








                                                                     
                                   
                                                                
                                  




                                                                  
#include "warn.h"
#include "util.h"
#include "x11util.h"
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>

struct action {
	const char *message;
	const char *title;
	int level;
};

static struct action actions[WARN_ENUM_END] = {
	[WARN_REBOOT] = {
		.level = 50,
		.title = "Neustart des Rechners",
		.message = 
		"Dieser PC wird in %02d:%02d neu gestartet. Bitte speichern Sie Ihre Arbeit und beenden Sie die Sitzung.\n\n"
		"This Computer will reboot in %02d:%02d. Please save your work and end the session."
	},
	[WARN_POWEROFF] = {
		.level = 50,
		.title = "Herunterfahren des Rechners",
		.message =
		"Dieser PC wird in %02d:%02d heruntergefahren. Bitte speichern Sie Ihre Arbeit und beenden Sie die Sitzung.\n\n"
		"This Computer will power down in %02d:%02d. Please save your work and end the session."
	},
	[WARN_LOGOUT] = {
		.level = 40,
		.title = "Inaktivität",
		.message =
		"Diese Sitzung scheint inaktiv und wird bei weiterer Inaktivität in %02d:%02d beendet.\n\n"
		"This session seems inactive and will be killed in %02d:%02d if no further activity is detected."
	},
	[WARN_LOGOUT_LOW] = {
		.level = 30,
		.title = "Inaktivität",
		.message =
		"Diese Sitzung scheint inaktiv und wird bei weiterer Inaktivität in %02d:%02d beendet.\n\n"
		"This session seems inactive and will be killed in %02d:%02d if no further activity is detected."
	},
};

void warn_showDefaultWarning( struct user *usr, enum Warning what, int seconds )
{
	const time_t NOW = now();
	if ( what < 0 || what >= WARN_ENUM_END )
		return; // Invalid!
	struct action *warning = &actions[what];
	if ( usr->lastWarn + 30 > NOW && warning->level <= usr->lastWarnLevel )
		return; // Ignore
	usr->lastWarn = NOW;
	usr->lastWarnLevel = warning->level;
	if ( seconds > 60 ) {
		seconds = ( seconds / 10 ) * 10;
	}
	int minutes = seconds / 60;
	seconds %= 60;
	char buffer[1000];
	snprintf( buffer, sizeof(buffer), warning->message, minutes, seconds, minutes, seconds );
	warn_showCustomWarning( usr, warning->title, buffer );
}

void warn_showCustomWarning( const struct user *usr, const char *title, const char *body )
{
	// Text or X11 warning
	if ( *usr->display ) {
		// notify-send
		if ( doublefork() ) {
			char buf[300];
			uid_t uid;
			struct passwd *u = getpwnam( usr->user );
			struct stat tmp;
			if ( u == NULL ) {
				uid = geteuid();
			} else {
				uid = u->pw_uid;
			}
			switchUserSafe( usr->user );
			snprintf( buf, sizeof(buf), "unix:path=/run/user/%u/bus", (unsigned int)uid );
			if ( stat( buf + 10, &tmp ) == 0 ) {
				setenv( "DBUS_SESSION_BUS_ADDRESS", buf, 1 );
			}
			setenv( "DISPLAY", usr->display, 1 );
			if ( usr->xauth[0] != '\0' && usr->xauth[0] != '-' ) {
				setenv( "XAUTHORITY", usr->xauth, 1 );
			}
			// Maybe wake up screen
			setScreenDpms( NULL, usr->display, SCREEN_ON );
			run( false, "notify-send", "-t", "15000", title, body, (char*)NULL );
		}
	} else {
		// write to device
		char dev[100];
		snprintf( dev, sizeof(dev), "/dev/%s", usr->device );
		FILE *fh = fopen( dev, "w" );
		if ( fh == NULL )
			return;
		fputs( "\n\n****************************\n", fh );
		fputs( title, fh );
		fputs( "\n****************************\n", fh );
		fputs( body, fh );
		fputs( "\n****************************\n\n", fh );
		fclose( fh );
	}
}