2 * This module handles network mail and mailing list processing.
4 * Copyright (c) 2000-2019 by the citadel.org team
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.
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.
14 * ** NOTE ** A word on the S_NETCONFIGS semaphore:
15 * This is a fairly high-level type of critical section. It ensures that no
16 * two threads work on the netconfigs files at the same time. Since we do
17 * so many things inside these, here are the rules:
18 * 1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
19 * 2. Do *not* perform any I/O with the client during these sections.
23 * Duration of time (in seconds) after which pending list subscribe/unsubscribe
24 * requests that have not been confirmed will be deleted.
26 #define EXP 259200 /* three days */
38 #include <sys/types.h>
40 #if TIME_WITH_SYS_TIME
41 # include <sys/time.h>
45 # include <sys/time.h>
53 # if HAVE_SYS_SYSCALL_H
54 # include <sys/syscall.h>
61 #include <libcitadel.h>
64 #include "citserver.h"
70 #include "internet_addressing.h"
71 #include "serv_network.h"
72 #include "clientsocket.h"
73 #include "citadel_dirs.h"
76 #include "ctdl_module.h"
80 /* comes from lookup3.c from libcitadel... */
81 extern uint32_t hashlittle( const void *key, size_t length, uint32_t initval);
83 typedef struct __roomlists {
89 * When we do network processing, it's accomplished in two passes; one to
90 * gather a list of rooms and one to actually do them. It's ok that rplist
91 * is global; we have a mutex that keeps it safe.
93 struct RoomProcList *rplist = NULL;
95 RoomProcList *CreateRoomProcListEntry(struct ctdlroom *qrbuf, OneRoomNetCfg *OneRNCFG)
98 struct RoomProcList *ptr;
100 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
105 ptr->namelen = strlen(qrbuf->QRname);
106 if (ptr->namelen > ROOMNAMELEN) {
107 ptr->namelen = ROOMNAMELEN - 1;
110 memcpy (ptr->name, qrbuf->QRname, ptr->namelen);
111 ptr->name[ptr->namelen] = '\0';
112 ptr->QRNum = qrbuf->QRnumber;
114 for (i = 0; i < ptr->namelen; i++)
116 ptr->lcname[i] = tolower(ptr->name[i]);
119 ptr->lcname[ptr->namelen] = '\0';
120 ptr->key = hashlittle(ptr->lcname, ptr->namelen, 9872345);
125 * Batch up and send all outbound traffic from the current room
127 void network_queue_interesting_rooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
129 struct RoomProcList *ptr;
130 roomlists *RP = (roomlists*) data;
132 if (!HaveSpoolConfig(OneRNCfg)) {
136 ptr = CreateRoomProcListEntry(qrbuf, OneRNCfg);
140 ptr->next = RP->rplist;
146 * Batch up and send all outbound traffic from the current room
148 int network_room_handler(struct ctdlroom *qrbuf)
150 struct RoomProcList *ptr;
151 OneRoomNetCfg *RNCfg;
153 if (qrbuf->QRdefaultview == VIEW_QUEUE) {
157 RNCfg = CtdlGetNetCfgForRoom(qrbuf->QRnumber);
162 if (!HaveSpoolConfig(RNCfg)) {
163 FreeRoomNetworkStruct(&RNCfg);
167 ptr = CreateRoomProcListEntry(qrbuf, RNCfg);
169 FreeRoomNetworkStruct(&RNCfg);
173 begin_critical_section(S_RPLIST);
176 end_critical_section(S_RPLIST);
177 FreeRoomNetworkStruct(&RNCfg);
181 void destroy_network_queue_room(RoomProcList *rplist)
183 struct RoomProcList *cur, *p;
194 void destroy_network_queue_room_locked (void)
196 begin_critical_section(S_RPLIST);
197 destroy_network_queue_room(rplist);
198 end_critical_section(S_RPLIST);
205 * Run through the rooms doing various types of network stuff.
207 void network_do_queue(void)
209 static time_t last_run = 0L;
210 int full_processing = 1;
211 HashList *working_ignetcfg;
212 HashList *the_netmap = NULL;
213 int netmap_changed = 0;
215 SpoolControl *sc = NULL;
219 * Run the full set of processing tasks no more frequently
220 * than once every n seconds
222 if ( (time(NULL) - last_run) < CtdlGetConfigLong("c_net_freq") )
225 syslog(LOG_DEBUG, "network: full processing in %ld seconds.", CtdlGetConfigLong("c_net_freq") - (time(NULL)- last_run));
228 begin_critical_section(S_RPLIST);
231 end_critical_section(S_RPLIST);
233 // TODO hm, check whether we have a config at all here?
234 /* Load the IGnet Configuration into memory */
235 working_ignetcfg = CtdlLoadIgNetCfg();
238 * Load the network map and filter list into memory.
240 if (!server_shutting_down) {
241 the_netmap = CtdlReadNetworkMap();
245 * Go ahead and run the queue
247 if (full_processing && !server_shutting_down) {
248 syslog(LOG_DEBUG, "network: loading outbound queue");
249 CtdlForEachNetCfgRoom(network_queue_interesting_rooms, &RL);
252 if ((RL.rplist != NULL) && (!server_shutting_down)) {
253 RoomProcList *ptr, *cmp;
255 syslog(LOG_DEBUG, "network: running outbound queue");
256 while (ptr != NULL && !server_shutting_down) {
259 /* filter duplicates from the list... */
260 while (cmp != NULL) {
261 if ((cmp->namelen > 0) &&
262 (cmp->key == ptr->key) &&
263 (cmp->namelen == ptr->namelen) &&
264 (strcmp(cmp->lcname, ptr->lcname) == 0))
271 if (ptr->namelen > 0) {
272 InspectQueuedRoom(&sc, ptr, working_ignetcfg, the_netmap);
282 network_spoolout_room(pSC);
290 free_spoolcontrol_struct(&pSC);
294 /* Save the network map back to disk */
295 if (netmap_changed) {
296 StrBuf *MapStr = CtdlSerializeNetworkMap(the_netmap);
297 char *pMapStr = SmashStrBuf(&MapStr);
298 CtdlPutSysConfig(IGNETMAP, pMapStr);
304 DeleteHash(&the_netmap);
306 DeleteHash(&working_ignetcfg);
308 syslog(LOG_DEBUG, "network: queue run completed");
310 if (full_processing) {
311 last_run = time(NULL);
313 destroy_network_queue_room(RL.rplist);
320 CTDL_MODULE_INIT(network)
324 CtdlRegisterRoomHook(network_room_handler);
325 CtdlRegisterCleanupHook(destroy_network_queue_room_locked);
326 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER, PRIO_QUEUE + 10);