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 char *newpath = NULL;
137 size_t instr_len = SIZ;
138 struct CtdlMessage *msg;
139 struct CtdlMessage *imsg;
140 struct ser_ret sermsg;
144 sc = (struct SpoolControl *)userdata;
147 * Process mailing list recipients
149 if (sc->listrecps != NULL) {
151 /* First, copy it to the spoolout room */
152 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
153 if (err != 0) return;
156 * Figure out how big a buffer we need to allocate
158 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
159 instr_len = instr_len + strlen(nptr->name);
165 lprintf(9, "Generating delivery instructions\n");
166 instr = mallok(instr_len);
168 lprintf(1, "Cannot allocate %d bytes for instr...\n",
173 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
174 "bounceto|postmaster@%s\n" ,
175 SPOOLMIME, msgnum, time(NULL), config.c_fqdn );
177 /* Generate delivery instructions for each recipient */
178 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
179 sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
184 * Generate a message from the instructions
186 imsg = mallok(sizeof(struct CtdlMessage));
187 memset(imsg, 0, sizeof(struct CtdlMessage));
188 imsg->cm_magic = CTDLMESSAGE_MAGIC;
189 imsg->cm_anon_type = MES_NORMAL;
190 imsg->cm_format_type = FMT_RFC822;
191 imsg->cm_fields['A'] = strdoop("Citadel");
192 imsg->cm_fields['M'] = instr;
194 /* Save delivery instructions in spoolout room */
195 CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
196 CtdlFreeMessage(imsg);
200 * Process IGnet push shares
202 if (sc->ignet_push_shares != NULL) {
204 msg = CtdlFetchMessage(msgnum);
207 /* Prepend our node name to the Path field whenever
208 * sending a message to another IGnet node
210 if (msg->cm_fields['P'] == NULL) {
211 msg->cm_fields['P'] = strdoop("username");
213 newpath = mallok(strlen(msg->cm_fields['P']) +
214 strlen(config.c_nodename) + 2);
215 sprintf(newpath, "%s!%s", config.c_nodename,
216 msg->cm_fields['P']);
217 phree(msg->cm_fields['P']);
218 msg->cm_fields['P'] = newpath;
221 * Force the message to appear in the correct room
222 * on the far end by setting the C field correctly
224 if (msg->cm_fields['C'] != NULL) {
225 phree(msg->cm_fields['C']);
227 msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
230 * Now serialize it for transmission
232 serialize_message(&sermsg, msg);
233 CtdlFreeMessage(msg);
235 /* Now send it to every node */
236 for (nptr = sc->ignet_push_shares; nptr != NULL;
239 /* FIXME check for valid node name */
240 /* FIXME check for split horizon */
242 /* Send the message */
243 sprintf(filename, "./network/spoolout/%s",
245 fp = fopen(filename, "ab");
247 fwrite(sermsg.ser, sermsg.len, 1, fp);
257 /* update lastsent */
258 sc->lastsent = msgnum;
265 * Batch up and send all outbound traffic from the current room
267 void network_spoolout_room(char *room_to_spool) {
272 struct SpoolControl sc;
273 /* struct namelist *digestrecps = NULL; */
274 struct namelist *nptr;
276 lprintf(7, "Spooling <%s>\n", room_to_spool);
277 if (getroom(&CC->quickroom, room_to_spool) != 0) {
278 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
282 memset(&sc, 0, sizeof(struct SpoolControl));
283 assoc_file_name(filename, &CC->quickroom, "netconfigs");
285 fp = fopen(filename, "r");
287 lprintf(7, "Outbound batch processing skipped for <%s>\n",
288 CC->quickroom.QRname);
292 lprintf(5, "Outbound batch processing started for <%s>\n",
293 CC->quickroom.QRname);
295 while (fgets(buf, sizeof buf, fp) != NULL) {
296 buf[strlen(buf)-1] = 0;
298 extract(instr, buf, 0);
299 if (!strcasecmp(instr, "lastsent")) {
300 sc.lastsent = extract_long(buf, 1);
302 else if (!strcasecmp(instr, "listrecp")) {
303 nptr = (struct namelist *)
304 mallok(sizeof(struct namelist));
305 nptr->next = sc.listrecps;
306 extract(nptr->name, buf, 1);
309 else if (!strcasecmp(instr, "ignet_push_share")) {
310 nptr = (struct namelist *)
311 mallok(sizeof(struct namelist));
312 nptr->next = sc.ignet_push_shares;
313 extract(nptr->name, buf, 1);
314 sc.ignet_push_shares = nptr;
322 /* Do something useful */
323 CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
324 network_spool_msg, &sc);
327 /* Now rewrite the config file */
328 fp = fopen(filename, "w");
330 lprintf(1, "ERROR: cannot open %s: %s\n",
331 filename, strerror(errno));
334 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
336 /* Write out the listrecps while freeing from memory at the
337 * same time. Am I clever or what? :)
339 while (sc.listrecps != NULL) {
340 fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
341 nptr = sc.listrecps->next;
345 while (sc.ignet_push_shares != NULL) {
346 fprintf(fp, "ignet_push_share|%s\n",
347 sc.ignet_push_shares->name);
348 nptr = sc.ignet_push_shares->next;
349 phree(sc.ignet_push_shares);
350 sc.ignet_push_shares = nptr;
356 lprintf(5, "Outbound batch processing finished for <%s>\n",
357 CC->quickroom.QRname);
362 * Batch up and send all outbound traffic from the current room
364 void network_queue_room(struct quickroom *qrbuf, void *data) {
365 struct RoomProcList *ptr;
367 ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
368 if (ptr == NULL) return;
370 safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
379 * Run through the rooms doing various types of network stuff.
381 void network_do_queue(void) {
382 static int doing_queue = 0;
383 static time_t last_run = 0L;
384 struct RoomProcList *ptr;
386 #define NETWORK_QUEUE_FREQUENCY 3600 /* one hour ... FIXME put in config */
388 * Run no more frequently than once every n seconds
390 if ( (time(NULL) - last_run) < NETWORK_QUEUE_FREQUENCY ) return;
393 * This is a simple concurrency check to make sure only one queue run
394 * is done at a time. We could do this with a mutex, but since we
395 * don't really require extremely fine granularity here, we'll do it
396 * with a static variable instead.
398 if (doing_queue) return;
400 last_run = time(NULL);
403 * Go ahead and run the queue
405 lprintf(7, "network: loading outbound queue\n");
406 ForEachRoom(network_queue_room, NULL);
408 lprintf(7, "network: running outbound queue\n");
409 while (rplist != NULL) {
410 network_spoolout_room(rplist->name);
412 rplist = rplist->next;
416 lprintf(7, "network: queue run completed\n");
424 char *Dynamic_Module_Init(void)
426 CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
427 CtdlRegisterProtoHook(cmd_snet, "SNET", "Get network config");
428 CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);