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