summaryrefslogtreecommitdiffstats
path: root/src/warn.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/warn.c')
-rw-r--r--src/warn.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/src/warn.c b/src/warn.c
new file mode 100644
index 0000000..6dfd427
--- /dev/null
+++ b/src/warn.c
@@ -0,0 +1,86 @@
+#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 );
+ // 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", warning->title, buffer, (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( warning->title, fh );
+ fputs( "\n****************************\n", fh );
+ fputs( buffer, fh );
+ fputs( "\n****************************\n\n", fh );
+ fclose( fh );
+ }
+}
+