Skyld AV  0.6
On access virus scanning for Linux
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
notify.cc
Go to the documentation of this file.
1 /*
2  * File: notify.c
3  *
4  * Copyright 2013 Heinrich Schuchardt <xypron.glpk@gmx.de>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19 
27 #include <gtk/gtk.h>
28 #include <libnotify/notify.h>
29 #include <glib.h>
30 #include <canberra.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <signal.h>
34 #include "config.h"
35 #include "notify.h"
36 
37 #define RUNNING 1
38 #define STOPPING 2
39 
40 volatile sig_atomic_t status;
41 
42 static void sigint_handler(int sig) {
43  status = STOPPING;
44 }
45 
49 static void help() {
50  printf("%s", HELP_TEXT);
51  exit(EXIT_FAILURE);
52 }
53 
57 static void version() {
58  printf("Skyld AV, version %s\n", VERSION);
59  printf("%s", VERSION_TEXT);
60  exit(EXIT_SUCCESS);
61 }
62 
63 int main(int argc, char **argv) {
64  int i;
65  NotifyNotification *n;
66  char filename[] = "/run/skyldav/log";
67  char application[] = "Skyld AV";
68  char title[] = "Skyld AV";
69  char body[2048];
70  FILE *file;
71  struct sigaction sa;
72 
73  // Analyze command line options.
74  for (i = 1; i < argc; i++) {
75  char *opt;
76 
77  opt = argv[i];
78  if (*opt == '-') {
79  opt++;
80  } else {
81  help();
82  }
83  if (*opt == '-') {
84  opt++;
85  }
86  switch (*opt) {
87  case 'v':
88  version();
89  break;
90  default:
91  help();
92  }
93  }
94 
95  printf("Skyld AV notifier %s\n", VERSION);
96  printf("Exit with CTRL+C\n");
97 
98  file = fopen(filename, "r");
99  if (file == NULL) {
100  fprintf(stderr, "File '%s' not found\n", filename);
101  return EXIT_FAILURE;
102  }
103  // position to end of file
104  fseek(file, 0, SEEK_END);
105 
106  sa.sa_handler = sigint_handler;
107  sa.sa_flags = 0; // or SA_RESTART
108  sigemptyset(&sa.sa_mask);
109  if (sigaction(SIGINT, &sa, NULL) == -1) {
110  fclose(file);
111  perror("sigaction");
112  return EXIT_FAILURE;
113  }
114 
115  ca_context *c;
116 
117  // initialize gtk
118  gtk_init(&argc, &argv);
119 
120  // initialize notify
121  notify_init(application);
122 
123  status = RUNNING;
124 
125  for (;;) {
126  char *msg;
127 
128  msg = fgets(body, 2047, file);
129  if (msg == NULL) {
130  if (status != RUNNING) {
131  break;
132  }
133  if (feof(file)) {
134  usleep(500000);
135  }
136  if (ferror(file)) {
137  perror("fgets");
138  }
139  continue;
140  }
141 
142  // create a new notification
143  switch (body[0]) {
144  case '\0':
145  case '\n':
146  n = notify_notification_new(title, "<Empty message>",
147  "dialog-information");
148  break;
149  case 'E':
150  n = notify_notification_new(title, body + 1, "dialog-error");
151  break;
152  case 'W':
153  n = notify_notification_new(title, body + 1, "dialog-warning");
154  break;
155  case 'I':
156  n = notify_notification_new(title, body + 1, "dialog-information");
157  break;
158  default:
159  n = notify_notification_new(title, body + 1, "dialog-information");
160  }
161 
162  // set the timeout to 9000 ms
163  notify_notification_set_timeout(n, 9000);
164 
165  // set the urgency level to critical
166  notify_notification_set_urgency(n, NOTIFY_URGENCY_CRITICAL);
167 
168  // show the notification
169  GError *error = NULL;
170  notify_notification_show(n, &error);
171 
172  ca_context_create(&c);
173  ca_context_play(c, 0,
174  CA_PROP_EVENT_ID, "dialog-warning",
175  CA_PROP_EVENT_DESCRIPTION, title,
176  NULL);
177  usleep(500000);
178  }
179 
180  fclose(file);
181 
182  return EXIT_SUCCESS;
183 }
184 
Notify Skyld AV events.
static void version()
Shows version information and exits.
Definition: notify.cc:57
const char * VERSION_TEXT
Definition: notify.h:41
volatile sig_atomic_t status
Definition: notify.cc:40
#define RUNNING
Definition: notify.cc:37
static void sigint_handler(int sig)
Definition: notify.cc:42
#define STOPPING
Definition: notify.cc:38
int main(int argc, char **argv)
Definition: notify.cc:63
static void help()
Prints help message and exits.
Definition: notify.cc:49
const char * HELP_TEXT
Definition: notify.h:32