Merge branch 'ff/avl-tree'
[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 static 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 } /* static 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 static 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 } /* static 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                                 if (bytes > 0) {
490                                         pthread_mutex_lock (&size_mutex);
491                                         type_list_incr (&size, type, bytes);
492                                         pthread_mutex_unlock (&size_mutex);
493                                 }
494                         }
495                         else if ('s' == line[0]) { /* s:<value> */
496                                 pthread_mutex_lock (&score_mutex);
497                                 score = (score * (double)score_count + atof (line + 2))
498                                                 / (double)(score_count + 1);
499                                 ++score_count;
500                                 pthread_mutex_unlock (&score_mutex);
501                         }
502                         else if ('c' == line[0]) { /* c:<type1>[,<type2>,...] */
503                                 char *ptr  = NULL;
504                                 char *type = strtok_r (line + 2, ",", &ptr);
505
506                                 do {
507                                         pthread_mutex_lock (&check_mutex);
508                                         type_list_incr (&check, type, 1);
509                                         pthread_mutex_unlock (&check_mutex);
510                                 } while (NULL != (type = strtok_r (NULL, ",", &ptr)));
511                         }
512                         else {
513                                 log_err ("unknown type '%c'", line[0]);
514                         }
515                 } /* while (loop) */
516
517                 close (connection->socket);
518
519                 free (connection);
520
521                 pthread_mutex_lock (&available_mutex);
522                 ++available_collectors;
523                 pthread_mutex_unlock (&available_mutex);
524
525                 pthread_cond_signal (&collector_available);
526         } /* while (1) */
527
528         free (buffer);
529         pthread_exit ((void *)0);
530 } /* static void *collect (void *) */
531
532 static void *open_connection (void *arg)
533 {
534         struct sockaddr_un addr;
535
536         /* create UNIX socket */
537         errno = 0;
538         if (-1 == (connector_socket = socket (PF_UNIX, SOCK_STREAM, 0))) {
539                 disabled = 1;
540                 log_err ("socket() failed: %s", strerror (errno));
541                 pthread_exit ((void *)1);
542         }
543
544         addr.sun_family = AF_UNIX;
545
546         strncpy (addr.sun_path, SOCK_PATH, (size_t)(UNIX_PATH_MAX - 1));
547         addr.sun_path[UNIX_PATH_MAX - 1] = '\0';
548         unlink (addr.sun_path);
549
550         errno = 0;
551         if (-1 == bind (connector_socket, (struct sockaddr *)&addr,
552                                 offsetof (struct sockaddr_un, sun_path)
553                                         + strlen(addr.sun_path))) {
554                 disabled = 1;
555                 log_err ("bind() failed: %s", strerror (errno));
556                 pthread_exit ((void *)1);
557         }
558
559         errno = 0;
560         if (-1 == listen (connector_socket, 5)) {
561                 disabled = 1;
562                 log_err ("listen() failed: %s", strerror (errno));
563                 pthread_exit ((void *)1);
564         }
565
566         if ((uid_t)0 == geteuid ()) {
567                 struct group *grp;
568
569                 errno = 0;
570                 if (NULL != (grp = getgrnam (sock_group))) {
571                         errno = 0;
572                         if (0 != chown (SOCK_PATH, (uid_t)-1, grp->gr_gid)) {
573                                 log_warn ("chown() failed: %s", strerror (errno));
574                         }
575                 }
576                 else {
577                         log_warn ("getgrnam() failed: %s", strerror (errno));
578                 }
579         }
580         else {
581                 log_warn ("not running as root");
582         }
583
584         errno = 0;
585         if (0 != chmod (SOCK_PATH, sock_perms)) {
586                 log_warn ("chmod() failed: %s", strerror (errno));
587         }
588
589         { /* initialize collector threads */
590                 int i   = 0;
591                 int err = 0;
592
593                 pthread_attr_t ptattr;
594
595                 conns.head = NULL;
596                 conns.tail = NULL;
597
598                 pthread_attr_init (&ptattr);
599                 pthread_attr_setdetachstate (&ptattr, PTHREAD_CREATE_DETACHED);
600
601                 available_collectors = max_conns;
602
603                 collectors =
604                         (collector_t **)smalloc (max_conns * sizeof (collector_t *));
605
606                 for (i = 0; i < max_conns; ++i) {
607                         collectors[i] = (collector_t *)smalloc (sizeof (collector_t));
608                         collectors[i]->socket = 0;
609
610                         if (0 != (err = pthread_create (&collectors[i]->thread, &ptattr,
611                                                         collect, collectors[i]))) {
612                                 log_err ("pthread_create() failed: %s", strerror (err));
613                         }
614                 }
615
616                 pthread_attr_destroy (&ptattr);
617         }
618
619         while (1) {
620                 int remote = 0;
621
622                 conn_t *connection;
623
624                 pthread_mutex_lock (&available_mutex);
625
626                 while (0 == available_collectors) {
627                         pthread_cond_wait (&collector_available, &available_mutex);
628                 }
629
630                 --available_collectors;
631
632                 pthread_mutex_unlock (&available_mutex);
633
634                 do {
635                         errno = 0;
636                         if (-1 == (remote = accept (connector_socket, NULL, NULL))) {
637                                 if (EINTR != errno) {
638                                         disabled = 1;
639                                         log_err ("accept() failed: %s", strerror (errno));
640                                         pthread_exit ((void *)1);
641                                 }
642                         }
643                 } while (EINTR == errno);
644
645                 connection = (conn_t *)smalloc (sizeof (conn_t));
646
647                 connection->socket = remote;
648                 connection->next   = NULL;
649
650                 pthread_mutex_lock (&conns_mutex);
651
652                 if (NULL == conns.head) {
653                         conns.head = connection;
654                         conns.tail = connection;
655                 }
656                 else {
657                         conns.tail->next = connection;
658                         conns.tail = conns.tail->next;
659                 }
660
661                 pthread_mutex_unlock (&conns_mutex);
662
663                 pthread_cond_signal (&conn_available);
664         }
665         pthread_exit ((void *)0);
666 } /* static void *open_connection (void *) */
667 #endif /* EMAIL_HAVE_READ */
668
669 static void email_init (void)
670 {
671 #if EMAIL_HAVE_READ
672         int err = 0;
673
674         if (0 != (err = pthread_create (&connector, NULL,
675                                 open_connection, NULL))) {
676                 disabled = 1;
677                 log_err ("pthread_create() failed: %s", strerror (err));
678                 return;
679         }
680 #endif /* EMAIL_HAVE_READ */
681         return;
682 } /* static void email_init (void) */
683
684 #if EMAIL_HAVE_READ
685 static void email_shutdown (void)
686 {
687         int i = 0;
688
689         if (disabled)
690                 return;
691
692         pthread_kill (connector, SIGTERM);
693         close (connector_socket);
694
695         /* don't allow any more connections to be processed */
696         pthread_mutex_lock (&conns_mutex);
697
698         for (i = 0; i < max_conns; ++i) {
699                 pthread_kill (collectors[i]->thread, SIGTERM);
700                 close (collectors[i]->socket);
701         }
702
703         pthread_mutex_unlock (&conns_mutex);
704
705         unlink (SOCK_PATH);
706         return;
707 } /* static void email_shutdown (void) */
708 #endif /* EMAIL_HAVE_READ */
709
710 static void count_write (char *host, char *inst, char *val)
711 {
712         char file[BUFSIZE] = "";
713         int  len           = 0;
714
715         len = snprintf (file, BUFSIZE, COUNT_FILE, inst);
716         if ((len < 0) || (len >= BUFSIZE))
717                 return;
718
719         rrd_update_file (host, file, val, count_ds_def, count_ds_num);
720         return;
721 } /* static void email_write (char *host, char *inst, char *val) */
722
723 static void size_write (char *host, char *inst, char *val)
724 {
725         char file[BUFSIZE] = "";
726         int  len           = 0;
727
728         len = snprintf (file, BUFSIZE, SIZE_FILE, inst);
729         if ((len < 0) || (len >= BUFSIZE))
730                 return;
731
732         rrd_update_file (host, file, val, size_ds_def, size_ds_num);
733         return;
734 } /* static void size_write (char *host, char *inst, char *val) */
735
736 static void score_write (char *host, char *inst, char *val)
737 {
738         rrd_update_file (host, SCORE_FILE, val, score_ds_def, score_ds_num);
739         return;
740 } /* static void score_write (char *host, char *inst, char *val) */
741
742 static void check_write (char *host, char *inst, char *val)
743 {
744         char file[BUFSIZE] = "";
745         int  len           = 0;
746
747         len = snprintf (file, BUFSIZE, CHECK_FILE, inst);
748         if ((len < 0) || (len >= BUFSIZE))
749                 return;
750
751         rrd_update_file (host, file, val, check_ds_def, check_ds_num);
752         return;
753 } /* static void check_write (char *host, char *inst, char *val) */
754
755 #if EMAIL_HAVE_READ
756 static void type_submit (char *plugin, char *inst, int value)
757 {
758         char buf[BUFSIZE] = "";
759         int  len          = 0;
760
761         len = snprintf (buf, BUFSIZE, "%u:%i", (unsigned int)curtime, value);
762         if ((len < 0) || (len >= BUFSIZE))
763                 return;
764
765         plugin_submit (plugin, inst, buf);
766         return;
767 } /* static void type_submit (char *, char *, int) */
768
769 static void score_submit (double value)
770 {
771         char buf[BUFSIZE] = "";
772         int  len          = 0;
773
774         len = snprintf (buf, BUFSIZE, "%u:%.2f", (unsigned int)curtime, value);
775         if ((len < 0) || (len >= BUFSIZE))
776                 return;
777
778         plugin_submit ("email_spam_score", "-", buf);
779         return;
780 } /* static void score_submit (double) */
781
782 /* Copy list l1 to list l2. l2 may partly exist already, but it is assumed
783  * that neither the order nor the name of any element of either list is
784  * changed and no elements are deleted. The values of l1 are reset to zero
785  * after they have been copied to l2. */
786 static void copy_type_list (type_list_t *l1, type_list_t *l2)
787 {
788         type_t *ptr1;
789         type_t *ptr2;
790
791         type_t *last = NULL;
792
793         for (ptr1 = l1->head, ptr2 = l2->head; NULL != ptr1;
794                         ptr1 = ptr1->next, last = ptr2, ptr2 = ptr2->next) {
795                 if (NULL == ptr2) {
796                         ptr2 = (type_t *)smalloc (sizeof (type_t));
797                         ptr2->name = NULL;
798                         ptr2->next = NULL;
799
800                         if (NULL == last) {
801                                 l2->head = ptr2;
802                         }
803                         else {
804                                 last->next = ptr2;
805                         }
806
807                         l2->tail = ptr2;
808                 }
809
810                 if (NULL == ptr2->name) {
811                         ptr2->name = sstrdup (ptr1->name);
812                 }
813
814                 ptr2->value = ptr1->value;
815                 ptr1->value = 0;
816         }
817         return;
818 }
819
820 static void email_read (void)
821 {
822         type_t *ptr;
823
824         double sc;
825
826         static type_list_t *cnt;
827         static type_list_t *sz;
828         static type_list_t *chk;
829
830         if (disabled)
831                 return;
832
833         if (NULL == cnt) {
834                 cnt = (type_list_t *)smalloc (sizeof (type_list_t));
835                 cnt->head = NULL;
836         }
837
838         if (NULL == sz) {
839                 sz = (type_list_t *)smalloc (sizeof (type_list_t));
840                 sz->head = NULL;
841         }
842
843         if (NULL == chk) {
844                 chk = (type_list_t *)smalloc (sizeof (type_list_t));
845                 chk->head = NULL;
846         }
847
848         /* email count */
849         pthread_mutex_lock (&count_mutex);
850
851         copy_type_list (&count, cnt);
852
853         pthread_mutex_unlock (&count_mutex);
854
855         for (ptr = cnt->head; NULL != ptr; ptr = ptr->next) {
856                 type_submit ("email_count", ptr->name, ptr->value);
857         }
858
859         /* email size */
860         pthread_mutex_lock (&size_mutex);
861
862         copy_type_list (&size, sz);
863
864         pthread_mutex_unlock (&size_mutex);
865
866         for (ptr = sz->head; NULL != ptr; ptr = ptr->next) {
867                 type_submit ("email_size", ptr->name, ptr->value);
868         }
869
870         /* spam score */
871         pthread_mutex_lock (&score_mutex);
872
873         sc = score;
874         score = 0.0;
875         score_count = 0;
876
877         pthread_mutex_unlock (&score_mutex);
878
879         score_submit (sc);
880
881         /* spam checks */
882         pthread_mutex_lock (&check_mutex);
883
884         copy_type_list (&check, chk);
885
886         pthread_mutex_unlock (&check_mutex);
887
888         for (ptr = chk->head; NULL != ptr; ptr = ptr->next) {
889                 type_submit ("email_spam_check", ptr->name, ptr->value);
890         }
891         return;
892 } /* static void read (void) */
893 #else /* if !EMAIL_HAVE_READ */
894 # define email_read NULL
895 #endif
896
897 void module_register (void)
898 {
899         plugin_register (MODULE_NAME, email_init, email_read, NULL);
900         plugin_register ("email_count", NULL, NULL, count_write);
901         plugin_register ("email_size", NULL, NULL, size_write);
902         plugin_register ("email_spam_score", NULL, NULL, score_write);
903         plugin_register ("email_spam_check", NULL, NULL, check_write);
904 #if EMAIL_HAVE_READ
905         plugin_register_shutdown (MODULE_NAME, email_shutdown);
906         cf_register (MODULE_NAME, email_config, config_keys, config_keys_num);
907 #endif /* EMAIL_HAVE_READ */
908         return;
909 } /* void module_register (void) */
910
911 /* vim: set sw=4 ts=4 tw=78 noexpandtab : */
912