more work on the new listdeliver module
[citadel.git] / citadel / modules / listdeliver / serv_listdeliver.c
1 /*
2  * This module delivers messages to mailing lists.
3  *
4  * Copyright (c) 2002-2021 by the citadel.org team
5  *
6  * This program is open source software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 3.
8  *  
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  */
14
15 #include "sysdep.h"
16 #include <stdlib.h>
17 #include <unistd.h>
18 #include <stdio.h>
19 #include <fcntl.h>
20 #include <ctype.h>
21 #include <signal.h>
22 #include <pwd.h>
23 #include <errno.h>
24 #include <sys/types.h>
25 #include <dirent.h>
26 #include <time.h>
27 #include <sys/wait.h>
28 #include <string.h>
29 #include <limits.h>
30 #include <libcitadel.h>
31 #include "citadel.h"
32 #include "server.h"
33 #include "citserver.h"
34 #include "support.h"
35 #include "config.h"
36 #include "user_ops.h"
37 #include "database.h"
38 #include "msgbase.h"
39 #include "internet_addressing.h"
40 #include "clientsocket.h"
41 #include "ctdl_module.h"
42
43 int doing_listdeliver = 0;
44
45
46 void listdeliver_do_msg(long msgnum, void *userdata) {
47 }
48
49
50 void listdeliver_sweep_room(struct ctdlroom *qrbuf, void *data) {
51         char *serialized_config = NULL;
52         long lastsent = 0;
53         char buf[SIZ];
54         int config_lines;
55         int i;
56         int number_of_messages_processed = 0;
57         int number_of_recipients = 0;
58
59         if (CtdlGetRoom(&CC->room, qrbuf->QRname)) {
60                 syslog(LOG_DEBUG, "listdeliver: no room <%s>", qrbuf->QRname);
61                 return;
62         }
63
64         serialized_config = LoadRoomNetConfigFile(qrbuf->QRnumber);
65         if (!serialized_config) {
66                 return;                         // no netconfig, no processing, no problem
67         }
68
69         config_lines = num_tokens(serialized_config, '\n');
70         for (i=0; i<config_lines; ++i) {
71                 extract_token(buf, serialized_config, i, '\n', sizeof buf);
72
73                 if (!strncasecmp(buf, "lastsent|", 9)) {
74                         lastsent = atol(&buf[9]);
75                 }
76                 else if ( (!strncasecmp(buf, "listrecp|", 9)) || (!strncasecmp(buf, "digestrecp|", 11)) ) {
77                         ++number_of_recipients;
78                 }
79         }
80
81         if (number_of_recipients > 0) {
82                 syslog(LOG_DEBUG, "listdeliver: processing new messages in <%s> for <%d> recipients", qrbuf->QRname, number_of_recipients);
83                 number_of_messages_processed = CtdlForEachMessage(MSGS_GT, lastsent, NULL, NULL, NULL, listdeliver_do_msg, NULL);
84                 syslog(LOG_DEBUG, "listdeliver: processed %d messages", number_of_messages_processed);
85         
86                 if (number_of_messages_processed > 0) {
87                         // FIXME write lastsent back to netconfig
88                 }
89         }
90
91         free(serialized_config);
92 }
93
94
95
96 void listdeliver_sweep(void) {
97         static time_t last_run = 0L;
98
99         /*
100          * Run mailing list delivery no more frequently than once every 15 minutes (we should make this configurable)
101          */
102         if ( (time(NULL) - last_run) < 900 ) {
103                 return;
104         }
105
106         /*
107          * This is a simple concurrency check to make sure only one listdeliver
108          * run is done at a time.  We could do this with a mutex, but since we
109          * don't really require extremely fine granularity here, we'll do it
110          * with a static variable instead.
111          */
112         if (doing_listdeliver) return;
113         doing_listdeliver = 1;
114
115         /*
116          * Go through each room looking for mailing lists to process
117          */
118         syslog(LOG_DEBUG, "listdeliver: sweep started");
119         CtdlForEachRoom(listdeliver_sweep_room, NULL);
120         syslog(LOG_DEBUG, "listdeliver: ended");
121         last_run = time(NULL);
122         doing_listdeliver = 0;
123 }
124
125
126
127 /*
128  * Module entry point
129  */
130 CTDL_MODULE_INIT(listdeliver)
131 {
132         if (!threading)
133         {
134                 CtdlRegisterSessionHook(listdeliver_sweep, EVT_TIMER, PRIO_AGGR + 50);
135         }
136         
137         /* return our module name for the log */
138         return "listsub";
139 }