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