2 * This module handles network mail and mailing list processing.
4 * Copyright (c) 2000-2018 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 struct CitContext networker_spool_CC;
82 /* comes from lookup3.c from libcitadel... */
83 extern uint32_t hashlittle( const void *key, size_t length, uint32_t initval);
85 typedef struct __roomlists {
91 * When we do network processing, it's accomplished in two passes; one to
92 * gather a list of rooms and one to actually do them. It's ok that rplist
93 * is global; we have a mutex that keeps it safe.
95 struct RoomProcList *rplist = NULL;
97 RoomProcList *CreateRoomProcListEntry(struct ctdlroom *qrbuf, OneRoomNetCfg *OneRNCFG)
100 struct RoomProcList *ptr;
102 ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
107 ptr->namelen = strlen(qrbuf->QRname);
108 if (ptr->namelen > ROOMNAMELEN) {
109 ptr->namelen = ROOMNAMELEN - 1;
112 memcpy (ptr->name, qrbuf->QRname, ptr->namelen);
113 ptr->name[ptr->namelen] = '\0';
114 ptr->QRNum = qrbuf->QRnumber;
116 for (i = 0; i < ptr->namelen; i++)
118 ptr->lcname[i] = tolower(ptr->name[i]);
121 ptr->lcname[ptr->namelen] = '\0';
122 ptr->key = hashlittle(ptr->lcname, ptr->namelen, 9872345);
127 * Batch up and send all outbound traffic from the current room
129 void network_queue_interesting_rooms(struct ctdlroom *qrbuf, void *data, OneRoomNetCfg *OneRNCfg)
131 struct RoomProcList *ptr;
132 roomlists *RP = (roomlists*) data;
134 if (!HaveSpoolConfig(OneRNCfg)) {
138 ptr = CreateRoomProcListEntry(qrbuf, OneRNCfg);
142 ptr->next = RP->rplist;
148 * Batch up and send all outbound traffic from the current room
150 int network_room_handler(struct ctdlroom *qrbuf)
152 struct RoomProcList *ptr;
153 OneRoomNetCfg *RNCfg;
155 if (qrbuf->QRdefaultview == VIEW_QUEUE) {
159 RNCfg = CtdlGetNetCfgForRoom(qrbuf->QRnumber);
164 if (!HaveSpoolConfig(RNCfg)) {
165 FreeRoomNetworkStruct(&RNCfg);
169 ptr = CreateRoomProcListEntry(qrbuf, RNCfg);
171 FreeRoomNetworkStruct(&RNCfg);
175 begin_critical_section(S_RPLIST);
178 end_critical_section(S_RPLIST);
179 FreeRoomNetworkStruct(&RNCfg);
183 void destroy_network_queue_room(RoomProcList *rplist)
185 struct RoomProcList *cur, *p;
196 void destroy_network_queue_room_locked (void)
198 begin_critical_section(S_RPLIST);
199 destroy_network_queue_room(rplist);
200 end_critical_section(S_RPLIST);
207 * Run through the rooms doing various types of network stuff.
209 void network_do_queue(void)
211 static time_t last_run = 0L;
212 int full_processing = 1;
213 HashList *working_ignetcfg;
214 HashList *the_netmap = NULL;
215 int netmap_changed = 0;
217 SpoolControl *sc = NULL;
221 * Run the full set of processing tasks no more frequently
222 * than once every n seconds
224 if ( (time(NULL) - last_run) < CtdlGetConfigLong("c_net_freq") )
227 syslog(LOG_DEBUG, "network: full processing in %ld seconds.", CtdlGetConfigLong("c_net_freq") - (time(NULL)- last_run));
230 become_session(&networker_spool_CC);
231 begin_critical_section(S_RPLIST);
234 end_critical_section(S_RPLIST);
236 // TODO hm, check whether we have a config at all here?
237 /* Load the IGnet Configuration into memory */
238 working_ignetcfg = CtdlLoadIgNetCfg();
241 * Load the network map and filter list into memory.
243 if (!server_shutting_down) {
244 the_netmap = CtdlReadNetworkMap();
248 * Go ahead and run the queue
250 if (full_processing && !server_shutting_down) {
251 syslog(LOG_DEBUG, "network: loading outbound queue");
252 CtdlForEachNetCfgRoom(network_queue_interesting_rooms, &RL);
255 if ((RL.rplist != NULL) && (!server_shutting_down)) {
256 RoomProcList *ptr, *cmp;
258 syslog(LOG_DEBUG, "network: running outbound queue");
259 while (ptr != NULL && !server_shutting_down) {
262 /* filter duplicates from the list... */
263 while (cmp != NULL) {
264 if ((cmp->namelen > 0) &&
265 (cmp->key == ptr->key) &&
266 (cmp->namelen == ptr->namelen) &&
267 (strcmp(cmp->lcname, ptr->lcname) == 0))
274 if (ptr->namelen > 0) {
275 InspectQueuedRoom(&sc, ptr, working_ignetcfg, the_netmap);
285 network_spoolout_room(pSC);
293 free_spoolcontrol_struct(&pSC);
297 /* Save the network map back to disk */
298 if (netmap_changed) {
299 StrBuf *MapStr = CtdlSerializeNetworkMap(the_netmap);
300 char *pMapStr = SmashStrBuf(&MapStr);
301 CtdlPutSysConfig(IGNETMAP, pMapStr);
307 DeleteHash(&the_netmap);
309 DeleteHash(&working_ignetcfg);
311 syslog(LOG_DEBUG, "network: queue run completed");
313 if (full_processing) {
314 last_run = time(NULL);
316 destroy_network_queue_room(RL.rplist);
325 CTDL_MODULE_INIT(network)
329 CtdlFillSystemContext(&networker_spool_CC, "CitNetSpool");
330 CtdlRegisterRoomHook(network_room_handler);
331 CtdlRegisterCleanupHook(destroy_network_queue_room_locked);
332 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER, PRIO_QUEUE + 10);