remove typedef from struct recptypes
[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 // data passed back and forth between listdeliver_do_msg() and listdeliver_sweep_room()
47 struct lddata {
48         long msgnum;            // number of most recent message processed
49         char *netconf;          // netconfig for this room (contains the recipients)
50 };
51
52
53
54 void listdeliver_do_msg(long msgnum, void *userdata) {
55         struct lddata *ld = (struct lddata *) userdata;
56         ld->msgnum = msgnum;
57         char buf[SIZ];
58
59         struct CtdlMessage *TheMessage = CtdlFetchMessage(msgnum, 1);
60
61         int config_lines = num_tokens(ld->netconf, '\n');
62         for (int i=0; i<config_lines; ++i) {
63                 extract_token(buf, ld->netconf, i, '\n', sizeof buf);
64                 if ( (!strncasecmp(buf, "listrecp|", 9)) || (!strncasecmp(buf, "digestrecp|", 11)) ) {
65                         syslog(LOG_DEBUG, "\033[32mDeliver %ld to %s\033[0m", msgnum, buf);
66                         // FIXME
67                 }
68         }
69         CM_Free(TheMessage);
70 }
71
72
73 void listdeliver_sweep_room(struct ctdlroom *qrbuf, void *data) {
74         char *netconfig = NULL;
75         long lastsent = 0;
76         char buf[SIZ];
77         int config_lines;
78         int i;
79         int number_of_messages_processed = 0;
80         int number_of_recipients = 0;
81         struct lddata ld;
82
83         if (CtdlGetRoom(&CC->room, qrbuf->QRname)) {
84                 syslog(LOG_DEBUG, "listdeliver: no room <%s>", qrbuf->QRname);
85                 return;
86         }
87
88         netconfig = LoadRoomNetConfigFile(qrbuf->QRnumber);
89         if (!netconfig) {
90                 return;                         // no netconfig, no processing, no problem
91         }
92
93         config_lines = num_tokens(netconfig, '\n');
94         for (i=0; i<config_lines; ++i) {
95                 extract_token(buf, netconfig, i, '\n', sizeof buf);
96
97                 if (!strncasecmp(buf, "lastsent|", 9)) {
98                         lastsent = atol(&buf[9]);
99                 }
100                 else if ( (!strncasecmp(buf, "listrecp|", 9)) || (!strncasecmp(buf, "digestrecp|", 11)) ) {
101                         ++number_of_recipients;
102                 }
103         }
104
105         if (number_of_recipients > 0) {
106                 syslog(LOG_DEBUG, "listdeliver: processing new messages in <%s> for <%d> recipients", qrbuf->QRname, number_of_recipients);
107                 ld.netconf = netconfig;
108                 number_of_messages_processed = CtdlForEachMessage(MSGS_GT, lastsent, NULL, NULL, NULL, listdeliver_do_msg, &ld);
109                 syslog(LOG_DEBUG, "listdeliver: processed %d messages", number_of_messages_processed);
110         
111                 if (number_of_messages_processed > 0) {
112                         syslog(LOG_DEBUG, "listdeliver: new lastsent is %ld", ld.msgnum);
113
114                         // FIXME write lastsent back to netconfig
115                         syslog(LOG_DEBUG, "\033[31mBEFORE:<%s>\033[0m", netconfig);
116                         syslog(LOG_DEBUG, "\033[32mAFTER:<%s>\033[0m", netconfig);
117
118
119
120
121
122                 }
123         }
124
125         free(netconfig);
126 }
127
128
129
130 void listdeliver_sweep(void) {
131         static time_t last_run = 0L;
132
133         /*
134          * Run mailing list delivery no more frequently than once every 15 minutes (we should make this configurable)
135          */
136         if ( (time(NULL) - last_run) < 900 ) {
137                 return;
138         }
139
140         /*
141          * This is a simple concurrency check to make sure only one listdeliver
142          * run is done at a time.  We could do this with a mutex, but since we
143          * don't really require extremely fine granularity here, we'll do it
144          * with a static variable instead.
145          */
146         if (doing_listdeliver) return;
147         doing_listdeliver = 1;
148
149         /*
150          * Go through each room looking for mailing lists to process
151          */
152         syslog(LOG_DEBUG, "listdeliver: sweep started");
153         CtdlForEachRoom(listdeliver_sweep_room, NULL);
154         syslog(LOG_DEBUG, "listdeliver: ended");
155         last_run = time(NULL);
156         doing_listdeliver = 0;
157
158         exit(0);
159 }
160
161
162
163 /*
164  * Module entry point
165  */
166 CTDL_MODULE_INIT(listdeliver)
167 {
168         if (!threading)
169         {
170                 CtdlRegisterSessionHook(listdeliver_sweep, EVT_TIMER, PRIO_AGGR + 50);
171         }
172         
173         /* return our module name for the log */
174         return "listsub";
175 }