4 * This module will eventually replace netproc and some of its utilities. In
5 * the meantime, it serves as a mailing list manager.
7 * Copyright (C) 2000-2001 by Art Cancro and others.
8 * This code is released under the terms of the GNU General Public License.
20 #include <sys/types.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
27 # include <sys/time.h>
38 #include "sysdep_decls.h"
39 #include "citserver.h"
42 #include "dynloader.h"
49 #include "internet_addressing.h"
50 #include "serv_network.h"
54 * When we do network processing, it's accomplished in two passes; one to
55 * gather a list of rooms and one to actually do them. It's ok that rplist
56 * is global; this process *only* runs as part of the housekeeping loop and
57 * therefore only one will run at a time.
60 struct RoomProcList *next;
61 char name[ROOMNAMELEN];
64 struct RoomProcList *rplist = NULL;
69 void cmd_gnet(char *argbuf) {
74 if (CtdlAccessCheck(ac_room_aide)) return;
75 assoc_file_name(filename, &CC->quickroom, "netconfigs");
76 cprintf("%d Network settings for room #%ld <%s>\n",
78 CC->quickroom.QRnumber, CC->quickroom.QRname);
80 fp = fopen(filename, "r");
82 while (fgets(buf, sizeof buf, fp) != NULL) {
83 buf[strlen(buf)-1] = 0;
93 void cmd_snet(char *argbuf) {
94 char tempfilename[SIZ];
99 if (CtdlAccessCheck(ac_room_aide)) return;
100 safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
101 assoc_file_name(filename, &CC->quickroom, "netconfigs");
103 fp = fopen(tempfilename, "w");
105 cprintf("%d Cannot open %s: %s\n",
106 ERROR+INTERNAL_ERROR,
111 cprintf("%d %s\n", SEND_LISTING, tempfilename);
112 while (client_gets(buf), strcmp(buf, "000")) {
113 fprintf(fp, "%s\n", buf);
117 /* Now copy the temp file to its permanent location
118 * (We use /bin/mv instead of link() because they may be on
119 * different filesystems)
122 snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
129 * Spools out one message from the list.
131 void network_spool_msg(long msgnum, void *userdata) {
132 struct SpoolControl *sc;
133 struct namelist *nptr;
136 size_t instr_len = SIZ;
137 struct CtdlMessage *imsg;
139 sc = (struct SpoolControl *)userdata;
141 /* If no recipients, bail out now.
142 * (May need to tweak this when we add other types of targets)
144 if (sc->listrecps == NULL) return;
146 /* First, copy it to the spoolout room */
147 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
148 if (err != 0) return;
151 * Figure out how big a buffer we need to allocate
153 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
154 instr_len = instr_len + strlen(nptr->name);
160 lprintf(9, "Generating delivery instructions\n");
161 instr = mallok(instr_len);
163 lprintf(1, "Cannot allocate %d bytes for instr...\n",
168 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
169 "bounceto|postmaster@%s\n" ,
170 SPOOLMIME, msgnum, time(NULL), config.c_fqdn );
172 /* Generate delivery instructions for each recipient */
173 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
174 sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
179 * Generate a message from the instructions
181 imsg = mallok(sizeof(struct CtdlMessage));
182 memset(imsg, 0, sizeof(struct CtdlMessage));
183 imsg->cm_magic = CTDLMESSAGE_MAGIC;
184 imsg->cm_anon_type = MES_NORMAL;
185 imsg->cm_format_type = FMT_RFC822;
186 imsg->cm_fields['A'] = strdoop("Citadel");
187 imsg->cm_fields['M'] = instr;
189 /* Save delivery instructions in spoolout room */
190 CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
191 CtdlFreeMessage(imsg);
193 /* update lastsent */
194 sc->lastsent = msgnum;
201 * Batch up and send all outbound traffic from the current room
203 void network_spoolout_room(char *room_to_spool) {
208 struct SpoolControl sc;
209 /* struct namelist *digestrecps = NULL; */
210 struct namelist *nptr;
212 lprintf(7, "Spooling <%s>\n", room_to_spool);
213 if (getroom(&CC->quickroom, room_to_spool) != 0) {
214 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
218 memset(&sc, 0, sizeof(struct SpoolControl));
219 assoc_file_name(filename, &CC->quickroom, "netconfigs");
221 fp = fopen(filename, "r");
223 lprintf(7, "Outbound batch processing skipped for <%s>\n",
224 CC->quickroom.QRname);
228 lprintf(5, "Outbound batch processing started for <%s>\n",
229 CC->quickroom.QRname);
231 while (fgets(buf, sizeof buf, fp) != NULL) {
232 buf[strlen(buf)-1] = 0;
234 extract(instr, buf, 0);
235 if (!strcasecmp(instr, "lastsent")) {
236 sc.lastsent = extract_long(buf, 1);
238 else if (!strcasecmp(instr, "listrecp")) {
239 nptr = (struct namelist *)
240 mallok(sizeof(struct namelist));
241 nptr->next = sc.listrecps;
242 extract(nptr->name, buf, 1);
251 /* Do something useful */
252 CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
253 network_spool_msg, &sc);
256 /* Now rewrite the config file */
257 fp = fopen(filename, "w");
259 lprintf(1, "ERROR: cannot open %s: %s\n",
260 filename, strerror(errno));
263 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
265 /* Write out the listrecps while freeing from memory at the
266 * same time. Am I clever or what? :)
268 while (sc.listrecps != NULL) {
269 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
270 nptr = sc.listrecps->next;
278 lprintf(5, "Outbound batch processing finished for <%s>\n",
279 CC->quickroom.QRname);
284 * Batch up and send all outbound traffic from the current room
286 void network_queue_room(struct quickroom *qrbuf, void *data) {
287 struct RoomProcList *ptr;
289 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
290 if (ptr == NULL) return;
292 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
301 * Run through the rooms doing various types of network stuff.
303 void network_do_queue(void) {
304 static int doing_queue = 0;
305 static time_t last_run = 0L;
306 struct RoomProcList *ptr;
308 #define NETWORK_QUEUE_FREQUENCY 3600 /* one hour ... FIXME put in config */
310 * Run no more frequently than once every n seconds
312 if ( (time(NULL) - last_run) < NETWORK_QUEUE_FREQUENCY ) return;
315 * This is a simple concurrency check to make sure only one queue run
316 * is done at a time. We could do this with a mutex, but since we
317 * don't really require extremely fine granularity here, we'll do it
318 * with a static variable instead.
320 if (doing_queue) return;
322 last_run = time(NULL);
325 * Go ahead and run the queue
327 lprintf(7, "network: loading outbound queue\n");
328 ForEachRoom(network_queue_room, NULL);
330 lprintf(7, "network: running outbound queue\n");
331 while (rplist != NULL) {
332 network_spoolout_room(rplist->name);
334 rplist = rplist->next;
338 lprintf(7, "network: queue run completed\n");
343 char *Dynamic_Module_Init(void)
345 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
346 CtdlRegisterProtoHook(cmd_snet, "SNET", "Get network config");
347 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);