* mailing list stuff ... still needs work
[citadel.git] / citadel / serv_network.c
1 /*
2  * $Id$ 
3  *
4  * This module will eventually replace netproc and some of its utilities.
5  * Copyright (C) 2000 by Art Cancro and others.
6  * This code is released under the terms of the GNU General Public License.
7  *
8  */
9
10
11 /* FIXME
12
13 there's stuff in here that makes the assumption that /tmp is on the same
14 filesystem as Citadel, and makes calls to link() on that basis.  fix this.
15
16 */
17
18 #include "sysdep.h"
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <signal.h>
24 #include <pwd.h>
25 #include <errno.h>
26 #include <sys/types.h>
27 #include <sys/time.h>
28 #include <sys/wait.h>
29 #include <string.h>
30 #include <limits.h>
31 #include "citadel.h"
32 #include "server.h"
33 #include <time.h>
34 #include "sysdep_decls.h"
35 #include "citserver.h"
36 #include "support.h"
37 #include "config.h"
38 #include "dynloader.h"
39 #include "room_ops.h"
40 #include "user_ops.h"
41 #include "policy.h"
42 #include "database.h"
43 #include "msgbase.h"
44 #include "tools.h"
45 #include "internet_addressing.h"
46 #include "serv_network.h"
47
48
49 void cmd_gnet(char *argbuf) {
50         char filename[256];
51         char buf[256];
52         FILE *fp;
53
54         if (CtdlAccessCheck(ac_room_aide)) return;
55         assoc_file_name(filename, &CC->quickroom, "netconfigs");
56         cprintf("%d Network settings for room #%ld <%s>\n",
57                 LISTING_FOLLOWS,
58                 CC->quickroom.QRnumber, CC->quickroom.QRname);
59
60         fp = fopen(filename, "r");
61         if (fp != NULL) {
62                 while (fgets(buf, sizeof buf, fp) != NULL) {
63                         buf[strlen(buf)-1] = 0;
64                         cprintf("%s\n", buf);
65                 }
66                 fclose(fp);
67         }
68
69         cprintf("000\n");
70 }
71
72
73 void cmd_snet(char *argbuf) {
74         char tempfilename[256];
75         char filename[256];
76         char buf[256];
77         FILE *fp;
78
79         if (CtdlAccessCheck(ac_room_aide)) return;
80         safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
81         assoc_file_name(filename, &CC->quickroom, "netconfigs");
82
83         fp = fopen(tempfilename, "w");
84         if (fp == NULL) {
85                 cprintf("%d Cannot open %s: %s\n",
86                         ERROR+INTERNAL_ERROR,
87                         tempfilename,
88                         strerror(errno));
89         }
90
91         cprintf("%d %s\n", SEND_LISTING, tempfilename);
92         while (client_gets(buf), strcmp(buf, "000")) {
93                 fprintf(fp, "%s\n", buf);
94         }
95         fclose(fp);
96
97         /* Now that we've got the whole file, put it in place */
98         unlink(filename);
99         link(tempfilename, filename);
100         unlink(tempfilename);
101 }
102
103
104
105 /*
106  * Spools out one message from the list.
107  */
108 void network_spool_msg(long msgnum, void *userdata) {
109         struct SpoolControl *sc;
110         struct namelist *nptr;
111         int err;
112         char *instr = NULL;
113         struct CtdlMessage *imsg;
114
115         sc = (struct SpoolControl *)userdata;
116
117         /* If no recipients, bail out now.
118          * (May need to tweak this when we add other types of targets)
119          */
120         if (sc->listrecps == NULL) return;
121         
122         /* First, copy it to the spoolout room */
123         err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
124         if (err != 0) return;
125
126         lprintf(9, "Generating delivery instructions\n");
127         instr = mallok(2048);   /* FIXME this won't be enough */
128         sprintf(instr,
129                 "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
130                 "bounceto|postmaster@%s\n" ,
131                 SPOOLMIME, msgnum, time(NULL), config.c_fqdn );
132
133         imsg = mallok(sizeof(struct CtdlMessage));
134         memset(imsg, 0, sizeof(struct CtdlMessage));
135         imsg->cm_magic = CTDLMESSAGE_MAGIC;
136         imsg->cm_anon_type = MES_NORMAL;
137         imsg->cm_format_type = FMT_RFC822;
138         imsg->cm_fields['A'] = strdoop("Citadel");
139         imsg->cm_fields['M'] = instr;
140
141
142         /* FIXME generate delivery instructions for each recipient */
143         for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
144                 sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
145                         nptr->name);
146         }
147
148         /* FIXME save delivery instructions in spoolout room */
149         CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
150         CtdlFreeMessage(imsg);
151
152         /* FIXME update lastseen */
153
154 }
155
156
157
158
159 /*
160  * Batch up and send all outbound traffic from the current room
161  */
162 void network_spoolout_current_room(void) {
163         char filename[256];
164         char buf[256];
165         char instr[256];
166         FILE *fp;
167         struct SpoolControl sc;
168         /* struct namelist *digestrecps = NULL; */
169         struct namelist *nptr;
170
171         memset(&sc, 0, sizeof(struct SpoolControl));
172         assoc_file_name(filename, &CC->quickroom, "netconfigs");
173
174         fp = fopen(filename, "r");
175         if (fp == NULL) {
176                 lprintf(7, "Outbound batch processing skipped for <%s>\n",
177                         CC->quickroom.QRname);
178                 return;
179         }
180
181         lprintf(5, "Outbound batch processing started for <%s>\n",
182                 CC->quickroom.QRname);
183
184         while (fgets(buf, sizeof buf, fp) != NULL) {
185                 buf[strlen(buf)-1] = 0;
186
187                 extract(instr, buf, 0);
188                 if (!strcasecmp(instr, "lastsent")) {
189                         sc.lastsent = extract_long(buf, 1);
190                 }
191                 else if (!strcasecmp(instr, "listrecp")) {
192                         nptr = (struct namelist *)
193                                 mallok(sizeof(struct namelist));
194                         nptr->next = sc.listrecps;
195                         extract(nptr->name, buf, 1);
196                         sc.listrecps = nptr;
197                 }
198
199
200         }
201         fclose(fp);
202
203
204
205         /* Do something useful */
206         CtdlForEachMessage(MSGS_ALL, 0L, (-63), NULL, NULL, network_spool_msg, &sc);
207
208
209
210         /* Now rewrite the config file */
211         fp = fopen(filename, "w");
212         if (fp == NULL) {
213                 lprintf(1, "ERROR: cannot open %s: %s\n", filename, strerror(errno));
214         }
215         else {
216                 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
217
218                 /* Write out the listrecps while freeing from memory at the
219                  * same time.  Am I clever or what?  :)
220                  */
221                 while (sc.listrecps != NULL) {
222                         fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
223                         nptr = sc.listrecps->next;
224                         phree(sc.listrecps);
225                         sc.listrecps = nptr;
226                 }
227
228                 fclose(fp);
229         }
230
231         lprintf(5, "Outbound batch processing finished for <%s>\n",
232                 CC->quickroom.QRname);
233 }
234
235
236
237 /* FIXME temporary server command for batch send */
238 void cmd_batc(char *argbuf) {
239         if (CtdlAccessCheck(ac_aide)) return;
240
241         network_spoolout_current_room();
242
243         cprintf("%d FIXME cmd_batc() ok\n", OK);
244 }
245
246
247
248 char *Dynamic_Module_Init(void)
249 {
250         CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
251         CtdlRegisterProtoHook(cmd_snet, "SNET", "Get network config");
252
253         /* FIXME
254            temporary server command for batch send
255          */
256         CtdlRegisterProtoHook(cmd_batc, "BATC", "send out batch (temp)");
257
258         return "$Id$";
259 }