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