email plugin: "Calculate" UNIX_PATH_MAX if it is not defined.
[collectd.git] / src / email.c
1 /**
2  * collectd - src/email.c
3  * Copyright (C) 2006  Sebastian Harl
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Author:
20  *   Sebastian Harl <sh at tokkee.org>
21  **/
22
23 /*
24  * This plugin communicates with a spam filter, a virus scanner or similar
25  * software using a UNIX socket and a very simple protocol:
26  *
27  * e-mail type (e.g. ham, spam, virus, ...) and size
28  * e:<type>:<bytes>
29  *
30  * spam score
31  * s:<value>
32  *
33  * successful spam checks
34  * c:<type1>[,<type2>,...]
35  */
36
37 #include "collectd.h"
38 #include "common.h"
39 #include "plugin.h"
40
41 #include "configfile.h"
42
43 #if HAVE_LIBPTHREAD
44 # include <pthread.h>
45 # define EMAIL_HAVE_READ 1
46 #else
47 # define EMAIL_HAVE_READ 0
48 #endif
49
50 #if HAVE_SYS_SELECT_H
51 #       include <sys/select.h>
52 #endif /* HAVE_SYS_SELECT_H */
53
54 #if HAVE_SYS_SOCKET_H
55 #       include <sys/socket.h>
56 #endif /* HAVE_SYS_SOCKET_H */
57
58 /* *sigh* glibc does not define UNIX_PATH_MAX in sys/un.h ... */
59 #if HAVE_LINUX_UN_H
60 #       include <linux/un.h>
61 #elif HAVE_SYS_UN_H
62 #       include <sys/un.h>
63 #endif /* HAVE_LINUX_UN_H | HAVE_SYS_UN_H */
64
65 /* some systems (e.g. Darwin) seem to not define UNIX_PATH_MAX at all */
66 #ifndef UNIX_PATH_MAX
67 # define UNIX_PATH_MAX sizeof (((struct sockaddr_un *)0)->sun_path)
68 #endif /* UNIX_PATH_MAX */
69
70 #if HAVE_GRP_H
71 #       include <grp.h>
72 #endif /* HAVE_GRP_H */
73
74 #define MODULE_NAME "email"
75
76 /* 256 bytes ought to be enough for anybody ;-) */
77 #define BUFSIZE 256
78
79 #ifndef COLLECTD_SOCKET_PREFIX
80 # define COLLECTD_SOCKET_PREFIX "/tmp/.collectd-"
81 #endif /* COLLECTD_SOCKET_PREFIX */
82
83 #define SOCK_PATH COLLECTD_SOCKET_PREFIX"email"
84 #define MAX_CONNS 5
85 #define MAX_CONNS_LIMIT 16384
86
87 /*
88  * Private data structures
89  */
90 #if EMAIL_HAVE_READ
91 /* linked list of email and check types */
92 typedef struct type {
93         char        *name;
94         int         value;
95         struct type *next;
96 } type_t;
97
98 typedef struct {
99         type_t *head;
100         type_t *tail;
101 } type_list_t;
102
103 /* linked list of collector thread control information */
104 typedef struct collector {
105         pthread_t thread;
106
107         /* socket to read data from */
108         int socket;
109
110         /* buffer to read data to */
111         char buffer[BUFSIZE];
112         int  idx; /* current position in buffer */
113
114         struct collector *next;
115 } collector_t;
116
117 typedef struct {
118         collector_t *head;
119         collector_t *tail;
120 } collector_list_t;
121 #endif /* EMAIL_HAVE_READ */
122
123 /*
124  * Private variables
125  */
126 #if EMAIL_HAVE_READ
127 /* valid configuration file keys */
128 static char *config_keys[] =
129 {
130         "SocketGroup",
131         "SocketPerms",
132         "MaxConns",
133         NULL
134 };
135 static int config_keys_num = 3;
136
137 /* socket configuration */
138 static char *sock_group = COLLECTD_GRP_NAME;
139 static int  sock_perms  = S_IRWXU | S_IRWXG;
140 static int  max_conns   = MAX_CONNS;
141
142 /* state of the plugin */
143 static int disabled = 0;
144
145 /* thread managing "client" connections */
146 static pthread_t connector;
147
148 /* tell the connector thread that a collector is available */
149 static pthread_cond_t collector_available = PTHREAD_COND_INITIALIZER;
150
151 /* collector threads that are in use */
152 static pthread_mutex_t active_mutex = PTHREAD_MUTEX_INITIALIZER;
153 static collector_list_t active;
154
155 /* collector threads that are available for use */
156 static pthread_mutex_t available_mutex = PTHREAD_MUTEX_INITIALIZER;
157 static collector_list_t available;
158
159 static pthread_mutex_t count_mutex = PTHREAD_MUTEX_INITIALIZER;
160 static type_list_t count;
161
162 static pthread_mutex_t size_mutex = PTHREAD_MUTEX_INITIALIZER;
163 static type_list_t size;
164
165 static pthread_mutex_t score_mutex = PTHREAD_MUTEX_INITIALIZER;
166 static double score;
167 static int score_count;
168
169 static pthread_mutex_t check_mutex = PTHREAD_MUTEX_INITIALIZER;
170 static type_list_t check;
171 #endif /* EMAIL_HAVE_READ */
172
173 #define COUNT_FILE "email/email-%s.rrd"
174 static char *count_ds_def[] =
175 {
176         "DS:count:GAUGE:"COLLECTD_HEARTBEAT":0:U",
177         NULL
178 };
179 static int count_ds_num = 1;
180
181 #define SIZE_FILE  "email/email_size-%s.rrd"
182 static char *size_ds_def[] =
183 {
184         "DS:size:GAUGE:"COLLECTD_HEARTBEAT":0:U",
185         NULL
186 };
187 static int size_ds_num = 1;
188
189 #define SCORE_FILE "email/spam_score.rrd"
190 static char *score_ds_def[] =
191 {
192         "DS:score:GAUGE:"COLLECTD_HEARTBEAT":U:U",
193         NULL
194 };
195 static int score_ds_num = 1;
196
197 #define CHECK_FILE "email/spam_check-%s.rrd"
198 static char *check_ds_def[] =
199 {
200         "DS:hits:GAUGE:"COLLECTD_HEARTBEAT":0:U",
201         NULL
202 };
203 static int check_ds_num = 1;
204
205 #if EMAIL_HAVE_READ
206 static int email_config (char *key, char *value)
207 {
208         if (0 == strcasecmp (key, "SocketGroup")) {
209                 sock_group = sstrdup (value);
210         }
211         else if (0 == strcasecmp (key, "SocketPerms")) {
212                 /* the user is responsible for providing reasonable values */
213                 sock_perms = (int)strtol (value, NULL, 0);
214         }
215         else if (0 == strcasecmp (key, "MaxConns")) {
216                 long int tmp = strtol (value, NULL, 0);
217
218                 if (tmp < 1) {
219                         fprintf (stderr, "email plugin: `MaxConns' was set to invalid "
220                                         "value %li, will use default %i.\n",
221                                         tmp, MAX_CONNS);
222                         max_conns = MAX_CONNS;
223                 }
224                 else if (tmp > MAX_CONNS_LIMIT) {
225                         fprintf (stderr, "email plugin: `MaxConns' was set to invalid "
226                                         "value %li, will use hardcoded limit %i.\n",
227                                         tmp, MAX_CONNS_LIMIT);
228                         max_conns = MAX_CONNS_LIMIT;
229                 }
230                 else {
231                         max_conns = (int)tmp;
232                 }
233         }
234         else {
235                 return -1;
236         }
237         return 0;
238 } /* static int email_config (char *, char *) */
239
240 /* Increment the value of the given name in the given list by incr. */
241 static void type_list_incr (type_list_t *list, char *name, int incr)
242 {
243         if (NULL == list->head) {
244                 list->head = (type_t *)smalloc (sizeof (type_t));
245
246                 list->head->name  = sstrdup (name);
247                 list->head->value = incr;
248                 list->head->next  = NULL;
249
250                 list->tail = list->head;
251         }
252         else {
253                 type_t *ptr;
254
255                 for (ptr = list->head; NULL != ptr; ptr = ptr->next) {
256                         if (0 == strcmp (name, ptr->name))
257                                 break;
258                 }
259
260                 if (NULL == ptr) {
261                         list->tail->next = (type_t *)smalloc (sizeof (type_t));
262                         list->tail = list->tail->next;
263
264                         list->tail->name  = sstrdup (name);
265                         list->tail->value = incr;
266                         list->tail->next  = NULL;
267                 }
268                 else {
269                         ptr->value += incr;
270                 }
271         }
272         return;
273 } /* static void type_list_incr (type_list_t *, char *) */
274
275 /* Read a single character from the socket. If an error occurs or end-of-file
276  * is reached return '\0'. */
277 char read_char (collector_t *src)
278 {
279         char ret = '\0';
280
281         fd_set fdset;
282
283         FD_ZERO (&fdset);
284         FD_SET (src->socket, &fdset);
285
286         if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
287                 syslog (LOG_ERR, "select() failed: %s", strerror (errno));
288                 return '\0';
289         }
290
291         assert (FD_ISSET (src->socket, &fdset));
292
293         do {
294                 ssize_t len = 0;
295
296                 errno = 0;
297                 if (0 > (len = read (src->socket, (void *)&ret, 1))) {
298                         if (EINTR != errno) {
299                                 syslog (LOG_ERR, "read() failed: %s", strerror (errno));
300                                 return '\0';
301                         }
302                 }
303
304                 if (0 == len)
305                         return '\0';
306         } while (EINTR == errno);
307         return ret;
308 } /* char read_char (collector_t *) */
309
310 /* Read a single line (terminated by '\n') from the the socket.
311  *
312  * The return value is zero terminated and does not contain any newline
313  * characters. In case that no complete line is available (non-blocking mode
314  * should be enabled) an empty string is returned.
315  *
316  * If an error occurs or end-of-file is reached return NULL.
317  *
318  * IMPORTANT NOTE: If there is no newline character found in BUFSIZE
319  * characters of the input stream, the line will will be ignored! By
320  * definition we should not get any longer input lines, thus this is
321  * acceptable in this case ;-) */
322 char *read_line (collector_t *src)
323 {
324         int  i = 0;
325         char *ret;
326
327         assert (BUFSIZE > src->idx);
328
329         for (i = 0; i < src->idx; ++i) {
330                 if ('\n' == src->buffer[i])
331                         break;
332         }
333
334         if ('\n' != src->buffer[i]) {
335                 fd_set fdset;
336         
337                 ssize_t len = 0;
338
339                 FD_ZERO (&fdset);
340                 FD_SET (src->socket, &fdset);
341
342                 if (-1 == select (src->socket + 1, &fdset, NULL, NULL, NULL)) {
343                         syslog (LOG_ERR, "select() failed: %s", strerror (errno));
344                         return NULL;
345                 }
346
347                 assert (FD_ISSET (src->socket, &fdset));
348
349                 do {
350                         errno = 0;
351                         if (0 > (len = read (src->socket,
352                                                         (void *)(&(src->buffer[0]) + src->idx),
353                                                         BUFSIZE - src->idx))) {
354                                 if (EINTR != errno) {
355                                         syslog (LOG_ERR, "read() failed: %s", strerror (errno));
356                                         return NULL;
357                                 }
358                         }
359
360                         if (0 == len)
361                                 return NULL;
362                 } while (EINTR == errno);
363
364                 src->idx += len;
365
366                 for (i = src->idx - len; i < src->idx; ++i) {
367                         if ('\n' == src->buffer[i])
368                                 break;
369                 }
370
371                 if ('\n' != src->buffer[i]) {
372                         ret = (char *)smalloc (1);
373
374                         ret[0] = '\0';
375
376                         if (BUFSIZE == src->idx) { /* no space left in buffer */
377                                 while ('\n' != read_char (src))
378                                         /* ignore complete line */;
379
380                                 src->idx = 0;
381                         }
382                         return ret;
383                 }
384         }
385
386         ret = (char *)smalloc (i + 1);
387         memcpy (ret, &(src->buffer[0]), i + 1);
388         ret[i] = '\0';
389
390         src->idx -= (i + 1);
391
392         if (0 == src->idx)
393                 src->buffer[0] = '\0';
394         else
395                 memmove (&(src->buffer[0]), &(src->buffer[i + 1]), src->idx);
396         return ret;
397 } /* char *read_line (collector_t *) */
398
399 static void *collect (void *arg)
400 {
401         collector_t *this = (collector_t *)arg;
402         
403         int loop = 1;
404
405         { /* put the socket in non-blocking mode */
406                 int flags = 0;
407
408                 errno = 0;
409                 if (-1 == fcntl (this->socket, F_GETFL, &flags)) {
410                         syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
411                         loop = 0;
412                 }
413
414                 errno = 0;
415                 if (-1 == fcntl (this->socket, F_SETFL, flags | O_NONBLOCK)) {
416                         syslog (LOG_ERR, "fcntl() failed: %s", strerror (errno));
417                         loop = 0;
418                 }
419         }
420
421         while (loop) {
422                 char *line = read_line (this);
423
424                 if (NULL == line) {
425                         loop = 0;
426                         break;
427                 }
428
429                 if ('\0' == line[0]) {
430                         free (line);
431                         continue;
432                 }
433
434                 if (':' != line[1]) {
435                         syslog (LOG_ERR, "email: syntax error in line '%s'", line);
436                         free (line);
437                         continue;
438                 }
439
440                 if ('e' == line[0]) { /* e:<type>:<bytes> */
441                         char *ptr  = NULL;
442                         char *type = strtok_r (line + 2, ":", &ptr);
443                         char *tmp  = strtok_r (NULL, ":", &ptr);
444                         int  bytes = 0;
445
446                         if (NULL == tmp) {
447                                 syslog (LOG_ERR, "email: syntax error in line '%s'", line);
448                                 free (line);
449                                 continue;
450                         }
451
452                         bytes = atoi (tmp);
453
454                         pthread_mutex_lock (&count_mutex);
455                         type_list_incr (&count, type, 1);
456                         pthread_mutex_unlock (&count_mutex);
457
458                         pthread_mutex_lock (&size_mutex);
459                         type_list_incr (&size, type, bytes);
460                         pthread_mutex_unlock (&size_mutex);
461                 }
462                 else if ('s' == line[0]) { /* s:<value> */
463                         pthread_mutex_lock (&score_mutex);
464                         score = (score * (double)score_count + atof (line + 2))
465                                         / (double)(score_count + 1);
466                         ++score_count;
467                         pthread_mutex_unlock (&score_mutex);
468                 }
469                 else if ('c' == line[0]) { /* c:<type1>[,<type2>,...] */
470                         char *ptr  = NULL;
471                         char *type = strtok_r (line + 2, ",", &ptr);
472
473                         do {
474                                 pthread_mutex_lock (&check_mutex);
475                                 type_list_incr (&check, type, 1);
476                                 pthread_mutex_unlock (&check_mutex);
477                         } while (NULL != (type = strtok_r (NULL, ",", &ptr)));
478                 }
479                 else {
480                         syslog (LOG_ERR, "email: unknown type '%c'", line[0]);
481                 }
482
483                 free (line);
484         }
485
486         /* put this thread back into the available list */
487         pthread_mutex_lock (&active_mutex);
488         {
489                 collector_t *last;
490                 collector_t *ptr;
491
492                 last = NULL;
493
494                 for (ptr = active.head; NULL != ptr; last = ptr, ptr = ptr->next) {
495                         if (0 != pthread_equal (ptr->thread, this->thread))
496                                 break;
497                 }
498
499                 /* the current thread _has_ to be in the active list */
500                 assert (NULL != ptr);
501
502                 if (NULL == last) {
503                         active.head = ptr->next;
504                 }
505                 else {
506                         last->next = ptr->next;
507
508                         if (NULL == last->next) {
509                                 active.tail = last;
510                         }
511                 }
512         }
513         pthread_mutex_unlock (&active_mutex);
514
515         this->next = NULL;
516
517         pthread_mutex_lock (&available_mutex);
518
519         if (NULL == available.head) {
520                 available.head = this;
521                 available.tail = this;
522         }
523         else {
524                 available.tail->next = this;
525                 available.tail = this;
526         }
527
528         pthread_mutex_unlock (&available_mutex);
529
530         pthread_cond_signal (&collector_available);
531         pthread_exit ((void *)0);
532 } /* void *collect (void *) */
533
534 static void *open_connection (void *arg)
535 {
536         int local = 0;
537
538         struct sockaddr_un addr;
539
540         /* create UNIX socket */
541         errno = 0;
542         if (-1 == (local = socket (PF_UNIX, SOCK_STREAM, 0))) {
543                 disabled = 1;
544                 syslog (LOG_ERR, "socket() failed: %s", strerror (errno));
545                 pthread_exit ((void *)1);
546         }
547
548         addr.sun_family = AF_UNIX;
549
550         strncpy (addr.sun_path, SOCK_PATH, (size_t)(UNIX_PATH_MAX - 1));
551         addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
552         unlink (addr.sun_path);
553
554         errno = 0;
555         if (-1 == bind (local, (struct sockaddr *)&addr,
556                                 offsetof (struct sockaddr_un, sun_path)
557                                         + strlen(addr.sun_path))) {
558                 disabled = 1;
559                 syslog (LOG_ERR, "bind() failed: %s", strerror (errno));
560                 pthread_exit ((void *)1);
561         }
562
563         errno = 0;
564         if (-1 == listen (local, 5)) {
565                 disabled = 1;
566                 syslog (LOG_ERR, "listen() failed: %s", strerror (errno));
567                 pthread_exit ((void *)1);
568         }
569
570         if ((uid_t)0 == geteuid ()) {
571                 struct group *grp;
572
573                 errno = 0;
574                 if (NULL != (grp = getgrnam (sock_group))) {
575                         errno = 0;
576                         if (0 != chown (SOCK_PATH, (uid_t)-1, grp->gr_gid)) {
577                                 syslog (LOG_WARNING, "chown() failed: %s", strerror (errno));
578                         }
579                 }
580                 else {
581                         syslog (LOG_WARNING, "getgrnam() failed: %s", strerror (errno));
582                 }
583         }
584         else {
585                 syslog (LOG_WARNING, "not running as root");
586         }
587
588         errno = 0;
589         if (0 != chmod (SOCK_PATH, sock_perms)) {
590                 syslog (LOG_WARNING, "chmod() failed: %s", strerror (errno));
591         }
592
593         { /* initialize queue of available threads */
594                 int i = 0;
595
596                 collector_t *last;
597
598                 active.head = NULL;
599                 active.tail = NULL;
600
601                 available.head = (collector_t *)smalloc (sizeof (collector_t));
602                 available.tail = available.head;
603                 available.tail->next = NULL;
604
605                 last = available.head;
606
607                 for (i = 1; i < max_conns; ++i) {
608                         last->next = (collector_t *)smalloc (sizeof (collector_t));
609                         last = last->next;
610                         available.tail = last;
611                         available.tail->next = NULL;
612                 }
613         }
614
615         while (1) {
616                 int remote = 0;
617                 int err    = 0;
618
619                 collector_t *collector;
620
621                 pthread_attr_t ptattr;
622
623                 pthread_mutex_lock (&available_mutex);
624                 while (NULL == available.head) {
625                         pthread_cond_wait (&collector_available, &available_mutex);
626                 }
627                 pthread_mutex_unlock (&available_mutex);
628
629                 do {
630                         errno = 0;
631                         if (-1 == (remote = accept (local, NULL, NULL))) {
632                                 if (EINTR != errno) {
633                                         disabled = 1;
634                                         syslog (LOG_ERR, "accept() failed: %s", strerror (errno));
635                                         pthread_exit ((void *)1);
636                                 }
637                         }
638                 } while (EINTR == errno);
639
640                 /* assign connection to next available thread */
641                 pthread_mutex_lock (&available_mutex);
642
643                 collector = available.head;
644                 collector->socket = remote;
645
646                 if (available.head == available.tail) {
647                         available.head = NULL;
648                         available.tail = NULL;
649                 }
650                 else {
651                         available.head = available.head->next;
652                 }
653
654                 pthread_mutex_unlock (&available_mutex);
655
656                 collector->idx  = 0;
657                 collector->next = NULL;
658
659                 pthread_attr_init (&ptattr);
660                 pthread_attr_setdetachstate (&ptattr, PTHREAD_CREATE_DETACHED);
661
662                 if (0 == (err = pthread_create (&collector->thread, &ptattr, collect,
663                                 (void *)collector))) {
664                         pthread_mutex_lock (&active_mutex);
665
666                         if (NULL == active.head) {
667                                 active.head = collector;
668                                 active.tail = collector;
669                         }
670                         else {
671                                 active.tail->next = collector;
672                                 active.tail = collector;
673                         }
674
675                         pthread_mutex_unlock (&active_mutex);
676                 }
677                 else {
678                         pthread_mutex_lock (&available_mutex);
679
680                         if (NULL == available.head) {
681                                 available.head = collector;
682                                 available.tail = collector;
683                         }
684                         else {
685                                 available.tail->next = collector;
686                                 available.tail = collector;
687                         }
688
689                         pthread_mutex_unlock (&available_mutex);
690
691                         close (remote);
692                         syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
693                 }
694
695                 pthread_attr_destroy (&ptattr);
696         }
697         pthread_exit ((void *)0);
698 } /* void *open_connection (void *) */
699 #endif /* EMAIL_HAVE_READ */
700
701 static void email_init (void)
702 {
703 #if EMAIL_HAVE_READ
704         int err = 0;
705
706         if (0 != (err = pthread_create (&connector, NULL,
707                                 open_connection, NULL))) {
708                 disabled = 1;
709                 syslog (LOG_ERR, "pthread_create() failed: %s", strerror (err));
710                 return;
711         }
712 #endif /* EMAIL_HAVE_READ */
713         return;
714 } /* static void email_init (void) */
715
716 static void count_write (char *host, char *inst, char *val)
717 {
718         char file[BUFSIZE] = "";
719         int  len           = 0;
720
721         len = snprintf (file, BUFSIZE, COUNT_FILE, inst);
722         if ((len < 0) || (len >= BUFSIZE))
723                 return;
724
725         rrd_update_file (host, file, val, count_ds_def, count_ds_num);
726         return;
727 } /* static void email_write (char *host, char *inst, char *val) */
728
729 static void size_write (char *host, char *inst, char *val)
730 {
731         char file[BUFSIZE] = "";
732         int  len           = 0;
733
734         len = snprintf (file, BUFSIZE, SIZE_FILE, inst);
735         if ((len < 0) || (len >= BUFSIZE))
736                 return;
737
738         rrd_update_file (host, file, val, size_ds_def, size_ds_num);
739         return;
740 } /* static void size_write (char *host, char *inst, char *val) */
741
742 static void score_write (char *host, char *inst, char *val)
743 {
744         rrd_update_file (host, SCORE_FILE, val, score_ds_def, score_ds_num);
745         return;
746 } /* static void score_write (char *host, char *inst, char *val) */
747
748 static void check_write (char *host, char *inst, char *val)
749 {
750         char file[BUFSIZE] = "";
751         int  len           = 0;
752
753         len = snprintf (file, BUFSIZE, CHECK_FILE, inst);
754         if ((len < 0) || (len >= BUFSIZE))
755                 return;
756
757         rrd_update_file (host, file, val, check_ds_def, check_ds_num);
758         return;
759 } /* static void check_write (char *host, char *inst, char *val) */
760
761 #if EMAIL_HAVE_READ
762 static void type_submit (char *plugin, char *inst, int value)
763 {
764         char buf[BUFSIZE] = "";
765         int  len          = 0;
766
767         if (0 == value)
768                 return;
769
770         len = snprintf (buf, BUFSIZE, "%u:%i", (unsigned int)curtime, value);
771         if ((len < 0) || (len >= BUFSIZE))
772                 return;
773
774         plugin_submit (plugin, inst, buf);
775         return;
776 } /* static void type_submit (char *, char *, int) */
777
778 static void score_submit (double value)
779 {
780         char buf[BUFSIZE] = "";
781         int  len          = 0;
782
783         if (0.0 == value)
784                 return;
785
786         len = snprintf (buf, BUFSIZE, "%u:%.2f", (unsigned int)curtime, value);
787         if ((len < 0) || (len >= BUFSIZE))
788                 return;
789
790         plugin_submit ("email_spam_score", NULL, buf);
791         return;
792 }
793
794 static void email_read (void)
795 {
796         type_t *ptr;
797
798         if (disabled)
799                 return;
800
801         pthread_mutex_lock (&count_mutex);
802
803         for (ptr = count.head; NULL != ptr; ptr = ptr->next) {
804                 type_submit ("email_count", ptr->name, ptr->value);
805                 ptr->value = 0;
806         }
807
808         pthread_mutex_unlock (&count_mutex);
809
810         pthread_mutex_lock (&size_mutex);
811
812         for (ptr = size.head; NULL != ptr; ptr = ptr->next) {
813                 type_submit ("email_size", ptr->name, ptr->value);
814                 ptr->value = 0;
815         }
816
817         pthread_mutex_unlock (&size_mutex);
818
819         pthread_mutex_lock (&score_mutex);
820
821         score_submit (score);
822         score = 0.0;
823         score_count = 0;
824
825         pthread_mutex_unlock (&score_mutex);
826
827         pthread_mutex_lock (&check_mutex);
828
829         for (ptr = check.head; NULL != ptr; ptr = ptr->next) {
830                 type_submit ("email_spam_check", ptr->name, ptr->value);
831                 ptr->value = 0;
832         }
833
834         pthread_mutex_unlock (&check_mutex);
835         return;
836 } /* static void read (void) */
837 #else /* if !EMAIL_HAVE_READ */
838 # define email_read NULL
839 #endif
840
841 void module_register (void)
842 {
843         plugin_register (MODULE_NAME, email_init, email_read, NULL);
844         plugin_register ("email_count", NULL, NULL, count_write);
845         plugin_register ("email_size", NULL, NULL, size_write);
846         plugin_register ("email_spam_score", NULL, NULL, score_write);
847         plugin_register ("email_spam_check", NULL, NULL, check_write);
848 #if EMAIL_HAVE_READ
849         cf_register (MODULE_NAME, email_config, config_keys, config_keys_num);
850 #endif /* EMAIL_HAVE_READ */
851         return;
852 } /* void module_register (void) */
853
854 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
855