+/*
+ * Deliver list messages to everyone on the list ... efficiently
+ */
+void network_deliver_list(struct CtdlMessage *msg, struct SpoolControl *sc) {
+ long msgnum;
+ char *instr = NULL;
+ size_t instr_len = SIZ;
+ struct CtdlMessage *imsg = NULL;
+ struct namelist *nptr;
+
+ /* Don't do this if there were no recipients! */
+ if (sc->listrecps == NULL) return;
+
+ /* Save the message to disk... */
+ msgnum = CtdlSubmitMsg(msg, NULL, SMTP_SPOOLOUT_ROOM);
+
+ /* Now generate the delivery instructions */
+
+ /*
+ * Figure out how big a buffer we need to allocate
+ */
+ for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
+ instr_len = instr_len + strlen(nptr->name) + 2;
+ }
+
+ /*
+ * allocate...
+ */
+ lprintf(CTDL_DEBUG, "Generating delivery instructions\n");
+ instr = malloc(instr_len);
+ if (instr == NULL) {
+ lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for instr...\n",
+ (long)instr_len);
+ abort();
+ }
+ snprintf(instr, instr_len,
+ "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
+ "bounceto|postmaster@%s\n" ,
+ SPOOLMIME, msgnum, (long)time(NULL), config.c_fqdn );
+
+ /* Generate delivery instructions for each recipient */
+ for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
+ size_t tmp = strlen(instr);
+ snprintf(&instr[tmp], instr_len - tmp, "remote|%s|0||\n", nptr->name);
+ }
+
+ /*
+ * Generate a message from the instructions
+ */
+ imsg = malloc(sizeof(struct CtdlMessage));
+ memset(imsg, 0, sizeof(struct CtdlMessage));
+ imsg->cm_magic = CTDLMESSAGE_MAGIC;
+ imsg->cm_anon_type = MES_NORMAL;
+ imsg->cm_format_type = FMT_RFC822;
+ imsg->cm_fields['A'] = strdup("Citadel");
+ imsg->cm_fields['M'] = instr;
+
+ /* Save delivery instructions in spoolout room */
+ CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM);
+ CtdlFreeMessage(imsg);
+}
+
+
+
+
+/*
+ * Spools out one message from the list.
+ */
+void network_spool_msg(long msgnum, void *userdata) {
+ struct SpoolControl *sc;
+ int i;
+ char *newpath = NULL;
+ size_t instr_len = SIZ;
+ struct CtdlMessage *msg = NULL;
+ struct namelist *nptr;
+ struct maplist *mptr;
+ struct ser_ret sermsg;
+ FILE *fp;
+ char filename[SIZ];
+ char buf[SIZ];
+ int bang = 0;
+ int send = 1;
+ int delete_after_send = 0; /* Set to 1 to delete after spooling */
+ int ok_to_participate = 0;
+ struct recptypes *valid;
+
+ sc = (struct SpoolControl *)userdata;
+
+ /*
+ * Process mailing list recipients
+ */
+ instr_len = SIZ;
+ if (sc->listrecps != NULL) {
+ /* Fetch the message. We're going to need to modify it
+ * in order to insert the [list name] in it, etc.
+ */
+ msg = CtdlFetchMessage(msgnum, 1);
+ if (msg != NULL) {
+
+ /* Prepend "[List name]" to the subject */
+ if (msg->cm_fields['U'] == NULL) {
+ msg->cm_fields['U'] = strdup("(no subject)");
+ }
+ snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
+ free(msg->cm_fields['U']);
+ msg->cm_fields['U'] = strdup(buf);
+
+ /* Set the recipient of the list message to the
+ * email address of the room itself.
+ * FIXME ... I want to be able to pick any address
+ */
+ if (msg->cm_fields['R'] != NULL) {
+ free(msg->cm_fields['R']);
+ }
+ msg->cm_fields['R'] = malloc(256);
+ snprintf(msg->cm_fields['R'], 256,
+ "room_%s@%s", CC->room.QRname,
+ config.c_fqdn);
+ for (i=0; i<strlen(msg->cm_fields['R']); ++i) {
+ if (isspace(msg->cm_fields['R'][i])) {
+ msg->cm_fields['R'][i] = '_';
+ }
+ }
+
+ /* Handle delivery */
+ network_deliver_list(msg, sc);
+ CtdlFreeMessage(msg);
+ }
+ }
+
+ /*
+ * Process digest recipients
+ */
+ if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
+ msg = CtdlFetchMessage(msgnum, 1);
+ if (msg != NULL) {
+ fprintf(sc->digestfp, " -----------------------------------"
+ "------------------------------------"
+ "-------\n");
+ fprintf(sc->digestfp, "From: ");
+ if (msg->cm_fields['A'] != NULL) {
+ fprintf(sc->digestfp, "%s ", msg->cm_fields['A']);
+ }
+ if (msg->cm_fields['F'] != NULL) {
+ fprintf(sc->digestfp, "<%s> ", msg->cm_fields['F']);
+ }
+ else if (msg->cm_fields['N'] != NULL) {
+ fprintf(sc->digestfp, "@%s ", msg->cm_fields['N']);
+ }
+ fprintf(sc->digestfp, "\n");
+ if (msg->cm_fields['U'] != NULL) {
+ fprintf(sc->digestfp, "Subject: %s\n", msg->cm_fields['U']);
+ }
+
+ CC->redirect_buffer = malloc(SIZ);
+ CC->redirect_len = 0;
+ CC->redirect_alloc = SIZ;
+
+ safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
+ CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0);
+
+ striplt(CC->redirect_buffer);
+ fprintf(sc->digestfp, "\n%s\n", CC->redirect_buffer);
+
+ free(CC->redirect_buffer);
+ CC->redirect_buffer = NULL;
+ CC->redirect_len = 0;
+ CC->redirect_alloc = 0;
+
+ sc->num_msgs_spooled += 1;
+ free(msg);
+ }
+ }
+
+ /*
+ * Process client-side list participations for this room
+ */
+ instr_len = SIZ;
+ if (sc->participates != NULL) {
+ msg = CtdlFetchMessage(msgnum, 1);
+ if (msg != NULL) {
+
+ /* Only send messages which originated on our own Citadel
+ * network, otherwise we'll end up sending the remote
+ * mailing list's messages back to it, which is rude...
+ */
+ ok_to_participate = 0;
+ if (msg->cm_fields['N'] != NULL) {
+ if (!strcasecmp(msg->cm_fields['N'], config.c_nodename)) {
+ ok_to_participate = 1;
+ }
+ if (is_valid_node(NULL, NULL, msg->cm_fields['N']) == 0) {
+ ok_to_participate = 1;
+ }
+ }
+ if (ok_to_participate) {
+ if (msg->cm_fields['F'] != NULL) {
+ free(msg->cm_fields['F']);
+ }
+ msg->cm_fields['F'] = malloc(SIZ);
+ /* Replace the Internet email address of the actual
+ * author with the email address of the room itself,
+ * so the remote listserv doesn't reject us.
+ * FIXME ... I want to be able to pick any address
+ */
+ snprintf(msg->cm_fields['F'], SIZ,
+ "room_%s@%s", CC->room.QRname,
+ config.c_fqdn);
+ for (i=0; i<strlen(msg->cm_fields['F']); ++i) {
+ if (isspace(msg->cm_fields['F'][i])) {
+ msg->cm_fields['F'][i] = '_';
+ }
+ }
+
+ /*
+ * Figure out how big a buffer we need to allocate
+ */
+ for (nptr = sc->participates; nptr != NULL; nptr = nptr->next) {
+
+ if (msg->cm_fields['R'] == NULL) {
+ free(msg->cm_fields['R']);
+ }
+ msg->cm_fields['R'] = strdup(nptr->name);
+
+ valid = validate_recipients(nptr->name);
+ CtdlSubmitMsg(msg, valid, "");
+ free_recipients(valid);
+ }
+
+ }
+ CtdlFreeMessage(msg);
+ }
+ }
+
+ /*
+ * Process IGnet push shares
+ */
+ msg = CtdlFetchMessage(msgnum, 1);
+ if (msg != NULL) {
+ size_t newpath_len;
+
+ /* Prepend our node name to the Path field whenever
+ * sending a message to another IGnet node
+ */
+ if (msg->cm_fields['P'] == NULL) {
+ msg->cm_fields['P'] = strdup("username");
+ }
+ newpath_len = strlen(msg->cm_fields['P']) +
+ strlen(config.c_nodename) + 2;
+ newpath = malloc(newpath_len);
+ snprintf(newpath, newpath_len, "%s!%s",
+ config.c_nodename, msg->cm_fields['P']);
+ free(msg->cm_fields['P']);
+ msg->cm_fields['P'] = newpath;
+
+ /*
+ * Determine if this message is set to be deleted
+ * after sending out on the network
+ */
+ if (msg->cm_fields['S'] != NULL) {
+ if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
+ delete_after_send = 1;
+ }
+ }
+
+ /* Now send it to every node */
+ if (sc->ignet_push_shares != NULL)
+ for (mptr = sc->ignet_push_shares; mptr != NULL;
+ mptr = mptr->next) {
+
+ send = 1;
+
+ /* Check for valid node name */
+ if (is_valid_node(NULL, NULL, mptr->remote_nodename) != 0) {
+ lprintf(CTDL_ERR, "Invalid node <%s>\n",
+ mptr->remote_nodename);
+ send = 0;
+ }
+
+ /* Check for split horizon */
+ lprintf(CTDL_DEBUG, "Path is %s\n", msg->cm_fields['P']);
+ bang = num_tokens(msg->cm_fields['P'], '!');
+ if (bang > 1) for (i=0; i<(bang-1); ++i) {
+ extract_token(buf, msg->cm_fields['P'],
+ i, '!', sizeof buf);
+ if (!strcasecmp(buf, mptr->remote_nodename)) {
+ send = 0;
+ }
+ }
+
+ /* Send the message */
+ if (send == 1) {
+
+ /*
+ * Force the message to appear in the correct room
+ * on the far end by setting the C field correctly
+ */
+ if (msg->cm_fields['C'] != NULL) {
+ free(msg->cm_fields['C']);
+ }
+ if (strlen(mptr->remote_roomname) > 0) {
+ msg->cm_fields['C'] = strdup(mptr->remote_roomname);
+ }
+ else {
+ msg->cm_fields['C'] = strdup(CC->room.QRname);
+ }
+
+ /* serialize it for transmission */
+ serialize_message(&sermsg, msg);
+ if (sermsg.len > 0) {
+
+ /* write it to the spool file */
+ snprintf(filename, sizeof filename,"%s/%s",
+ ctdl_netout_dir,
+ mptr->remote_nodename);
+ lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
+ fp = fopen(filename, "ab");
+ if (fp != NULL) {
+ fwrite(sermsg.ser,
+ sermsg.len, 1, fp);
+ fclose(fp);
+ }
+ else {
+ lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
+ }
+
+ /* free the serialized version */
+ free(sermsg.ser);
+ }
+
+ }
+ }
+ CtdlFreeMessage(msg);
+ }
+
+ /* update lastsent */
+ sc->lastsent = msgnum;
+
+ /* Delete this message if delete-after-send is set */
+ if (delete_after_send) {
+ CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
+ }
+
+}
+
+