summaryrefslogtreecommitdiffstats
path: root/src/warn.c
blob: 7b1ec8ee101ef834aede6943f6f1ae1e28a23964 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include "warn.h"
#include "util.h"
#include <stdio.h>
#include <stdlib.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 warnUser( 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 );
	showWarning( usr, warning->title, buffer );
}

void showWarning( const struct user *usr, const char *title, const char *body )
{
	// Text or X11 warning
	if ( *usr->display ) {
		// notify-send
		if ( doublefork() ) {
			switchUserSafe( usr->user );
			setenv( "DISPLAY", usr->display, 1 );
			if ( usr->xauth[0] != '\0' && usr->xauth[0] != '-' ) {
				setenv( "XAUTHORITY", usr->xauth, 1 );
			}
			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 );
	}
}