Amended the 'Corrupt message' error with more detail
[citadel.git] / citadel / modules / network / serv_network.c
1 /*
2  * $Id$ 
3  *
4  * This module handles shared rooms, inter-Citadel mail, and outbound
5  * mailing list processing.
6  *
7  * Copyright (C) 2000-2005 by Art Cancro and others.
8  * This code is released under the terms of the GNU General Public License.
9  *
10  * ** NOTE **   A word on the S_NETCONFIGS semaphore:
11  * This is a fairly high-level type of critical section.  It ensures that no
12  * two threads work on the netconfigs files at the same time.  Since we do
13  * so many things inside these, here are the rules:
14  *  1. begin_critical_section(S_NETCONFIGS) *before* begin_ any others.
15  *  2. Do *not* perform any I/O with the client during these sections.
16  *
17  */
18
19 /*
20  * Duration of time (in seconds) after which pending list subscribe/unsubscribe
21  * requests that have not been confirmed will be deleted.
22  */
23 #define EXP     259200  /* three days */
24
25 #include "sysdep.h"
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <stdio.h>
29 #include <fcntl.h>
30 #include <ctype.h>
31 #include <signal.h>
32 #include <pwd.h>
33 #include <errno.h>
34 #include <sys/stat.h>
35 #include <sys/types.h>
36 #include <dirent.h>
37 #if TIME_WITH_SYS_TIME
38 # include <sys/time.h>
39 # include <time.h>
40 #else
41 # if HAVE_SYS_TIME_H
42 #  include <sys/time.h>
43 # else
44 #  include <time.h>
45 # endif
46 #endif
47
48 #include <sys/wait.h>
49 #include <string.h>
50 #include <limits.h>
51 #include "citadel.h"
52 #include "server.h"
53 #include "citserver.h"
54 #include "support.h"
55 #include "config.h"
56 #include "room_ops.h"
57 #include "user_ops.h"
58 #include "policy.h"
59 #include "database.h"
60 #include "msgbase.h"
61 #include "tools.h"
62 #include "internet_addressing.h"
63 #include "serv_network.h"
64 #include "clientsocket.h"
65 #include "file_ops.h"
66 #include "citadel_dirs.h"
67
68 #ifndef HAVE_SNPRINTF
69 #include "snprintf.h"
70 #endif
71
72
73 #include "ctdl_module.h"
74
75
76
77 /* Nonzero while we are doing network processing */
78 static int doing_queue = 0;
79
80 /*
81  * When we do network processing, it's accomplished in two passes; one to
82  * gather a list of rooms and one to actually do them.  It's ok that rplist
83  * is global; we have a mutex that keeps it safe.
84  */
85 struct RoomProcList *rplist = NULL;
86
87 /*
88  * We build a map of network nodes during processing.
89  */
90 struct NetMap *the_netmap = NULL;
91 int netmap_changed = 0;
92 char *working_ignetcfg = NULL;
93
94 /*
95  * Load or refresh the Citadel network (IGnet) configuration for this node.
96  */
97 void load_working_ignetcfg(void) {
98         char *cfg;
99         char *oldcfg;
100
101         cfg = CtdlGetSysConfig(IGNETCFG);
102         if (cfg == NULL) {
103                 cfg = strdup("");
104         }
105
106         oldcfg = working_ignetcfg;
107         working_ignetcfg = cfg;
108         if (oldcfg != NULL) {
109                 free(oldcfg);
110         }
111 }
112
113
114
115
116
117 /*
118  * Keep track of what messages to reject
119  */
120 struct FilterList *load_filter_list(void) {
121         char *serialized_list = NULL;
122         int i;
123         char buf[SIZ];
124         struct FilterList *newlist = NULL;
125         struct FilterList *nptr;
126
127         serialized_list = CtdlGetSysConfig(FILTERLIST);
128         if (serialized_list == NULL) return(NULL); /* if null, no entries */
129
130         /* Use the string tokenizer to grab one line at a time */
131         for (i=0; i<num_tokens(serialized_list, '\n'); ++i) {
132                 extract_token(buf, serialized_list, i, '\n', sizeof buf);
133                 nptr = (struct FilterList *) malloc(sizeof(struct FilterList));
134                 extract_token(nptr->fl_user, buf, 0, '|', sizeof nptr->fl_user);
135                 striplt(nptr->fl_user);
136                 extract_token(nptr->fl_room, buf, 1, '|', sizeof nptr->fl_room);
137                 striplt(nptr->fl_room);
138                 extract_token(nptr->fl_node, buf, 2, '|', sizeof nptr->fl_node);
139                 striplt(nptr->fl_node);
140
141                 /* Cowardly refuse to add an any/any/any entry that would
142                  * end up filtering every single message.
143                  */
144                 if (IsEmptyStr(nptr->fl_user) && 
145                     IsEmptyStr(nptr->fl_room) &&
146                     IsEmptyStr(nptr->fl_node)) {
147                         free(nptr);
148                 }
149                 else {
150                         nptr->next = newlist;
151                         newlist = nptr;
152                 }
153         }
154
155         free(serialized_list);
156         return newlist;
157 }
158
159
160 void free_filter_list(struct FilterList *fl) {
161         if (fl == NULL) return;
162         free_filter_list(fl->next);
163         free(fl);
164 }
165
166
167
168 /*
169  * Check the use table.  This is a list of messages which have recently
170  * arrived on the system.  It is maintained and queried to prevent the same
171  * message from being entered into the database multiple times if it happens
172  * to arrive multiple times by accident.
173  */
174 int network_usetable(struct CtdlMessage *msg) {
175
176         char msgid[SIZ];
177         struct cdbdata *cdbut;
178         struct UseTable ut;
179
180         /* Bail out if we can't generate a message ID */
181         if (msg == NULL) {
182                 return(0);
183         }
184         if (msg->cm_fields['I'] == NULL) {
185                 return(0);
186         }
187         if (IsEmptyStr(msg->cm_fields['I'])) {
188                 return(0);
189         }
190
191         /* Generate the message ID */
192         strcpy(msgid, msg->cm_fields['I']);
193         if (haschar(msgid, '@') == 0) {
194                 strcat(msgid, "@");
195                 if (msg->cm_fields['N'] != NULL) {
196                         strcat(msgid, msg->cm_fields['N']);
197                 }
198                 else {
199                         return(0);
200                 }
201         }
202
203         cdbut = cdb_fetch(CDB_USETABLE, msgid, strlen(msgid));
204         if (cdbut != NULL) {
205                 cdb_free(cdbut);
206                 return(1);
207         }
208
209         /* If we got to this point, it's unique: add it. */
210         strcpy(ut.ut_msgid, msgid);
211         ut.ut_timestamp = time(NULL);
212         cdb_store(CDB_USETABLE, msgid, strlen(msgid),
213                 &ut, sizeof(struct UseTable) );
214         return(0);
215 }
216
217
218 /* 
219  * Read the network map from its configuration file into memory.
220  */
221 void read_network_map(void) {
222         char *serialized_map = NULL;
223         int i;
224         char buf[SIZ];
225         struct NetMap *nmptr;
226
227         serialized_map = CtdlGetSysConfig(IGNETMAP);
228         if (serialized_map == NULL) return;     /* if null, no entries */
229
230         /* Use the string tokenizer to grab one line at a time */
231         for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
232                 extract_token(buf, serialized_map, i, '\n', sizeof buf);
233                 nmptr = (struct NetMap *) malloc(sizeof(struct NetMap));
234                 extract_token(nmptr->nodename, buf, 0, '|', sizeof nmptr->nodename);
235                 nmptr->lastcontact = extract_long(buf, 1);
236                 extract_token(nmptr->nexthop, buf, 2, '|', sizeof nmptr->nexthop);
237                 nmptr->next = the_netmap;
238                 the_netmap = nmptr;
239         }
240
241         free(serialized_map);
242         netmap_changed = 0;
243 }
244
245
246 /*
247  * Write the network map from memory back to the configuration file.
248  */
249 void write_network_map(void) {
250         char *serialized_map = NULL;
251         struct NetMap *nmptr;
252
253
254         if (netmap_changed) {
255                 serialized_map = strdup("");
256         
257                 if (the_netmap != NULL) {
258                         for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
259                                 serialized_map = realloc(serialized_map,
260                                                         (strlen(serialized_map)+SIZ) );
261                                 if (!IsEmptyStr(nmptr->nodename)) {
262                                         snprintf(&serialized_map[strlen(serialized_map)],
263                                                 SIZ,
264                                                 "%s|%ld|%s\n",
265                                                 nmptr->nodename,
266                                                 (long)nmptr->lastcontact,
267                                                 nmptr->nexthop);
268                                 }
269                         }
270                 }
271
272                 CtdlPutSysConfig(IGNETMAP, serialized_map);
273                 free(serialized_map);
274         }
275
276         /* Now free the list */
277         while (the_netmap != NULL) {
278                 nmptr = the_netmap->next;
279                 free(the_netmap);
280                 the_netmap = nmptr;
281         }
282         netmap_changed = 0;
283 }
284
285
286
287 /* 
288  * Check the network map and determine whether the supplied node name is
289  * valid.  If it is not a neighbor node, supply the name of a neighbor node
290  * which is the next hop.  If it *is* a neighbor node, we also fill in the
291  * shared secret.
292  */
293 int is_valid_node(char *nexthop, char *secret, char *node) {
294         int i;
295         char linebuf[SIZ];
296         char buf[SIZ];
297         int retval;
298         struct NetMap *nmptr;
299
300         if (node == NULL) {
301                 return(-1);
302         }
303
304         /*
305          * First try the neighbor nodes
306          */
307         if (working_ignetcfg == NULL) {
308                 lprintf(CTDL_ERR, "working_ignetcfg is NULL!\n");
309                 if (nexthop != NULL) {
310                         strcpy(nexthop, "");
311                 }
312                 return(-1);
313         }
314
315         retval = (-1);
316         if (nexthop != NULL) {
317                 strcpy(nexthop, "");
318         }
319
320         /* Use the string tokenizer to grab one line at a time */
321         for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
322                 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
323                 extract_token(buf, linebuf, 0, '|', sizeof buf);
324                 if (!strcasecmp(buf, node)) {
325                         if (nexthop != NULL) {
326                                 strcpy(nexthop, "");
327                         }
328                         if (secret != NULL) {
329                                 extract_token(secret, linebuf, 1, '|', 256);
330                         }
331                         retval = 0;
332                 }
333         }
334
335         if (retval == 0) {
336                 return(retval);         /* yup, it's a direct neighbor */
337         }
338
339         /*      
340          * If we get to this point we have to see if we know the next hop
341          */
342         if (the_netmap != NULL) {
343                 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
344                         if (!strcasecmp(nmptr->nodename, node)) {
345                                 if (nexthop != NULL) {
346                                         strcpy(nexthop, nmptr->nexthop);
347                                 }
348                                 return(0);
349                         }
350                 }
351         }
352
353         /*
354          * If we get to this point, the supplied node name is bogus.
355          */
356         lprintf(CTDL_ERR, "Invalid node name <%s>\n", node);
357         return(-1);
358 }
359
360
361
362
363
364 void cmd_gnet(char *argbuf) {
365         char filename[SIZ];
366         char buf[SIZ];
367         FILE *fp;
368
369         if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
370                 /* users can edit the netconfigs for their own mailbox rooms */
371         }
372         else if (CtdlAccessCheck(ac_room_aide)) return;
373
374         assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
375         cprintf("%d Network settings for room #%ld <%s>\n",
376                 LISTING_FOLLOWS,
377                 CC->room.QRnumber, CC->room.QRname);
378
379         fp = fopen(filename, "r");
380         if (fp != NULL) {
381                 while (fgets(buf, sizeof buf, fp) != NULL) {
382                         buf[strlen(buf)-1] = 0;
383                         cprintf("%s\n", buf);
384                 }
385                 fclose(fp);
386         }
387
388         cprintf("000\n");
389 }
390
391
392 void cmd_snet(char *argbuf) {
393         char tempfilename[SIZ];
394         char filename[SIZ];
395         char buf[SIZ];
396         FILE *fp, *newfp;
397
398         unbuffer_output();
399
400         if ( (CC->room.QRflags & QR_MAILBOX) && (CC->user.usernum == atol(CC->room.QRname)) ) {
401                 /* users can edit the netconfigs for their own mailbox rooms */
402         }
403         else if (CtdlAccessCheck(ac_room_aide)) return;
404
405         CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
406         assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
407
408         fp = fopen(tempfilename, "w");
409         if (fp == NULL) {
410                 cprintf("%d Cannot open %s: %s\n",
411                         ERROR + INTERNAL_ERROR,
412                         tempfilename,
413                         strerror(errno));
414         }
415
416         cprintf("%d %s\n", SEND_LISTING, tempfilename);
417         while (client_getln(buf, sizeof buf), strcmp(buf, "000")) {
418                 fprintf(fp, "%s\n", buf);
419         }
420         fclose(fp);
421
422         /* Now copy the temp file to its permanent location.
423          * (We copy instead of link because they may be on different filesystems)
424          */
425         begin_critical_section(S_NETCONFIGS);
426         fp = fopen(tempfilename, "r");
427         if (fp != NULL) {
428                 newfp = fopen(filename, "w");
429                 if (newfp != NULL) {
430                         while (fgets(buf, sizeof buf, fp) != NULL) {
431                                 fprintf(newfp, "%s", buf);
432                         }
433                         fclose(newfp);
434                 }
435                 fclose(fp);
436         }
437         end_critical_section(S_NETCONFIGS);
438         unlink(tempfilename);
439 }
440
441
442 /*
443  * Deliver digest messages
444  */
445 void network_deliver_digest(struct SpoolControl *sc) {
446         char buf[SIZ];
447         int i;
448         struct CtdlMessage *msg = NULL;
449         long msglen;
450         char *recps = NULL;
451         size_t recps_len = SIZ;
452         struct recptypes *valid;
453         struct namelist *nptr;
454
455         if (sc->num_msgs_spooled < 1) {
456                 fclose(sc->digestfp);
457                 sc->digestfp = NULL;
458                 return;
459         }
460
461         msg = malloc(sizeof(struct CtdlMessage));
462         memset(msg, 0, sizeof(struct CtdlMessage));
463         msg->cm_magic = CTDLMESSAGE_MAGIC;
464         msg->cm_format_type = FMT_RFC822;
465         msg->cm_anon_type = MES_NORMAL;
466
467         sprintf(buf, "%ld", time(NULL));
468         msg->cm_fields['T'] = strdup(buf);
469         msg->cm_fields['A'] = strdup(CC->room.QRname);
470         snprintf(buf, sizeof buf, "[%s]", CC->room.QRname);
471         msg->cm_fields['U'] = strdup(buf);
472         sprintf(buf, "room_%s@%s", CC->room.QRname, config.c_fqdn);
473         for (i=0; buf[i]; ++i) {
474                 if (isspace(buf[i])) buf[i]='_';
475                 buf[i] = tolower(buf[i]);
476         }
477         msg->cm_fields['F'] = strdup(buf);
478         msg->cm_fields['R'] = strdup(buf);
479
480         /*
481          * Go fetch the contents of the digest
482          */
483         fseek(sc->digestfp, 0L, SEEK_END);
484         msglen = ftell(sc->digestfp);
485
486         msg->cm_fields['M'] = malloc(msglen + 1);
487         fseek(sc->digestfp, 0L, SEEK_SET);
488         fread(msg->cm_fields['M'], (size_t)msglen, 1, sc->digestfp);
489         msg->cm_fields['M'][msglen] = 0;
490
491         fclose(sc->digestfp);
492         sc->digestfp = NULL;
493
494         /* Now generate the delivery instructions */
495
496         /* 
497          * Figure out how big a buffer we need to allocate
498          */
499         for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
500                 recps_len = recps_len + strlen(nptr->name) + 2;
501         }
502         
503         recps = malloc(recps_len);
504
505         if (recps == NULL) {
506                 lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for recps...\n", (long)recps_len);
507                 abort();
508         }
509
510         strcpy(recps, "");
511
512         /* Each recipient */
513         for (nptr = sc->digestrecps; nptr != NULL; nptr = nptr->next) {
514                 if (nptr != sc->digestrecps) {
515                         strcat(recps, ",");
516                 }
517                 strcat(recps, nptr->name);
518         }
519
520         /* Now submit the message */
521         valid = validate_recipients(recps);
522         free(recps);
523         CtdlSubmitMsg(msg, valid, NULL);
524         CtdlFreeMessage(msg);
525         free_recipients(valid);
526 }
527
528
529 /*
530  * Deliver list messages to everyone on the list ... efficiently
531  */
532 void network_deliver_list(struct CtdlMessage *msg, struct SpoolControl *sc) {
533         char *recps = NULL;
534         size_t recps_len = SIZ;
535         struct recptypes *valid;
536         struct namelist *nptr;
537
538         /* Don't do this if there were no recipients! */
539         if (sc->listrecps == NULL) return;
540
541         /* Now generate the delivery instructions */
542
543         /* 
544          * Figure out how big a buffer we need to allocate
545          */
546         for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
547                 recps_len = recps_len + strlen(nptr->name) + 2;
548         }
549         
550         recps = malloc(recps_len);
551
552         if (recps == NULL) {
553                 lprintf(CTDL_EMERG, "Cannot allocate %ld bytes for recps...\n", (long)recps_len);
554                 abort();
555         }
556
557         strcpy(recps, "");
558
559         /* Each recipient */
560         for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
561                 if (nptr != sc->listrecps) {
562                         strcat(recps, ",");
563                 }
564                 strcat(recps, nptr->name);
565         }
566
567         /* Now submit the message */
568         valid = validate_recipients(recps);
569         free(recps);
570         CtdlSubmitMsg(msg, valid, NULL);
571         free_recipients(valid);
572         /* Do not call CtdlFreeMessage(msg) here; the caller will free it. */
573 }
574
575
576
577
578 /*
579  * Spools out one message from the list.
580  */
581 void network_spool_msg(long msgnum, void *userdata) {
582         struct SpoolControl *sc;
583         int i;
584         char *newpath = NULL;
585         size_t instr_len = SIZ;
586         struct CtdlMessage *msg = NULL;
587         struct namelist *nptr;
588         struct maplist *mptr;
589         struct ser_ret sermsg;
590         FILE *fp;
591         char filename[SIZ];
592         char buf[SIZ];
593         int bang = 0;
594         int send = 1;
595         int delete_after_send = 0;      /* Set to 1 to delete after spooling */
596         int ok_to_participate = 0;
597         struct recptypes *valid;
598
599         sc = (struct SpoolControl *)userdata;
600
601         /*
602          * Process mailing list recipients
603          */
604         instr_len = SIZ;
605         if (sc->listrecps != NULL) {
606                 /* Fetch the message.  We're going to need to modify it
607                  * in order to insert the [list name] in it, etc.
608                  */
609                 msg = CtdlFetchMessage(msgnum, 1);
610                 if (msg != NULL) {
611
612                         /* Prepend "[List name]" to the subject */
613                         if (msg->cm_fields['U'] == NULL) {
614                                 msg->cm_fields['U'] = strdup("(no subject)");
615                         }
616                         snprintf(buf, sizeof buf, "[%s] %s", CC->room.QRname, msg->cm_fields['U']);
617                         free(msg->cm_fields['U']);
618                         msg->cm_fields['U'] = strdup(buf);
619
620                         /* Set the recipient of the list message to the
621                          * email address of the room itself.
622                          * FIXME ... I want to be able to pick any address
623                          */
624                         if (msg->cm_fields['R'] != NULL) {
625                                 free(msg->cm_fields['R']);
626                         }
627                         msg->cm_fields['R'] = malloc(256);
628                         snprintf(msg->cm_fields['R'], 256,
629                                 "room_%s@%s", CC->room.QRname,
630                                 config.c_fqdn);
631                         for (i=0; msg->cm_fields['R'][i]; ++i) {
632                                 if (isspace(msg->cm_fields['R'][i])) {
633                                         msg->cm_fields['R'][i] = '_';
634                                 }
635                         }
636
637                         /* Handle delivery */
638                         network_deliver_list(msg, sc);
639                         CtdlFreeMessage(msg);
640                 }
641         }
642
643         /*
644          * Process digest recipients
645          */
646         if ((sc->digestrecps != NULL) && (sc->digestfp != NULL)) {
647                 msg = CtdlFetchMessage(msgnum, 1);
648                 if (msg != NULL) {
649                         fprintf(sc->digestfp,   " -----------------------------------"
650                                                 "------------------------------------"
651                                                 "-------\n");
652                         fprintf(sc->digestfp, "From: ");
653                         if (msg->cm_fields['A'] != NULL) {
654                                 fprintf(sc->digestfp, "%s ", msg->cm_fields['A']);
655                         }
656                         if (msg->cm_fields['F'] != NULL) {
657                                 fprintf(sc->digestfp, "<%s> ", msg->cm_fields['F']);
658                         }
659                         else if (msg->cm_fields['N'] != NULL) {
660                                 fprintf(sc->digestfp, "@%s ", msg->cm_fields['N']);
661                         }
662                         fprintf(sc->digestfp, "\n");
663                         if (msg->cm_fields['U'] != NULL) {
664                                 fprintf(sc->digestfp, "Subject: %s\n", msg->cm_fields['U']);
665                         }
666
667                         CC->redirect_buffer = malloc(SIZ);
668                         CC->redirect_len = 0;
669                         CC->redirect_alloc = SIZ;
670
671                         safestrncpy(CC->preferred_formats, "text/plain", sizeof CC->preferred_formats);
672                         CtdlOutputPreLoadedMsg(msg, MT_CITADEL, HEADERS_NONE, 0, 0);
673
674                         striplt(CC->redirect_buffer);
675                         fprintf(sc->digestfp, "\n%s\n", CC->redirect_buffer);
676
677                         free(CC->redirect_buffer);
678                         CC->redirect_buffer = NULL;
679                         CC->redirect_len = 0;
680                         CC->redirect_alloc = 0;
681
682                         sc->num_msgs_spooled += 1;
683                         free(msg);
684                 }
685         }
686
687         /*
688          * Process client-side list participations for this room
689          */
690         instr_len = SIZ;
691         if (sc->participates != NULL) {
692                 msg = CtdlFetchMessage(msgnum, 1);
693                 if (msg != NULL) {
694
695                         /* Only send messages which originated on our own Citadel
696                          * network, otherwise we'll end up sending the remote
697                          * mailing list's messages back to it, which is rude...
698                          */
699                         ok_to_participate = 0;
700                         if (msg->cm_fields['N'] != NULL) {
701                                 if (!strcasecmp(msg->cm_fields['N'], config.c_nodename)) {
702                                         ok_to_participate = 1;
703                                 }
704                                 if (is_valid_node(NULL, NULL, msg->cm_fields['N']) == 0) {
705                                         ok_to_participate = 1;
706                                 }
707                         }
708                         if (ok_to_participate) {
709                                 if (msg->cm_fields['F'] != NULL) {
710                                         free(msg->cm_fields['F']);
711                                 }
712                                 msg->cm_fields['F'] = malloc(SIZ);
713                                 /* Replace the Internet email address of the actual
714                                 * author with the email address of the room itself,
715                                 * so the remote listserv doesn't reject us.
716                                 * FIXME ... I want to be able to pick any address
717                                 */
718                                 snprintf(msg->cm_fields['F'], SIZ,
719                                         "room_%s@%s", CC->room.QRname,
720                                         config.c_fqdn);
721                                 for (i=0; msg->cm_fields['F'][i]; ++i) {
722                                         if (isspace(msg->cm_fields['F'][i])) {
723                                                 msg->cm_fields['F'][i] = '_';
724                                         }
725                                 }
726
727                                 /* 
728                                  * Figure out how big a buffer we need to allocate
729                                  */
730                                 for (nptr = sc->participates; nptr != NULL; nptr = nptr->next) {
731
732                                         if (msg->cm_fields['R'] == NULL) {
733                                                 free(msg->cm_fields['R']);
734                                         }
735                                         msg->cm_fields['R'] = strdup(nptr->name);
736         
737                                         valid = validate_recipients(nptr->name);
738                                         CtdlSubmitMsg(msg, valid, "");
739                                         free_recipients(valid);
740                                 }
741                         
742                         }
743                         CtdlFreeMessage(msg);
744                 }
745         }
746         
747         /*
748          * Process IGnet push shares
749          */
750         msg = CtdlFetchMessage(msgnum, 1);
751         if (msg != NULL) {
752                 size_t newpath_len;
753
754                 /* Prepend our node name to the Path field whenever
755                  * sending a message to another IGnet node
756                  */
757                 if (msg->cm_fields['P'] == NULL) {
758                         msg->cm_fields['P'] = strdup("username");
759                 }
760                 newpath_len = strlen(msg->cm_fields['P']) +
761                          strlen(config.c_nodename) + 2;
762                 newpath = malloc(newpath_len);
763                 snprintf(newpath, newpath_len, "%s!%s",
764                          config.c_nodename, msg->cm_fields['P']);
765                 free(msg->cm_fields['P']);
766                 msg->cm_fields['P'] = newpath;
767
768                 /*
769                  * Determine if this message is set to be deleted
770                  * after sending out on the network
771                  */
772                 if (msg->cm_fields['S'] != NULL) {
773                         if (!strcasecmp(msg->cm_fields['S'], "CANCEL")) {
774                                 delete_after_send = 1;
775                         }
776                 }
777
778                 /* Now send it to every node */
779                 if (sc->ignet_push_shares != NULL)
780                   for (mptr = sc->ignet_push_shares; mptr != NULL;
781                     mptr = mptr->next) {
782
783                         send = 1;
784
785                         /* Check for valid node name */
786                         if (is_valid_node(NULL, NULL, mptr->remote_nodename) != 0) {
787                                 lprintf(CTDL_ERR, "Invalid node <%s>\n",
788                                         mptr->remote_nodename);
789                                 send = 0;
790                         }
791
792                         /* Check for split horizon */
793                         lprintf(CTDL_DEBUG, "Path is %s\n", msg->cm_fields['P']);
794                         bang = num_tokens(msg->cm_fields['P'], '!');
795                         if (bang > 1) for (i=0; i<(bang-1); ++i) {
796                                 extract_token(buf, msg->cm_fields['P'],
797                                         i, '!', sizeof buf);
798                                 if (!strcasecmp(buf, mptr->remote_nodename)) {
799                                         send = 0;
800                                 }
801                         }
802
803                         /* Send the message */
804                         if (send == 1) {
805
806                                 /*
807                                  * Force the message to appear in the correct room
808                                  * on the far end by setting the C field correctly
809                                  */
810                                 if (msg->cm_fields['C'] != NULL) {
811                                         free(msg->cm_fields['C']);
812                                 }
813                                 if (!IsEmptyStr(mptr->remote_roomname)) {
814                                         msg->cm_fields['C'] = strdup(mptr->remote_roomname);
815                                 }
816                                 else {
817                                         msg->cm_fields['C'] = strdup(CC->room.QRname);
818                                 }
819
820                                 /* serialize it for transmission */
821                                 serialize_message(&sermsg, msg);
822                                 if (sermsg.len > 0) {
823
824                                         /* write it to the spool file */
825                                         snprintf(filename, sizeof filename,"%s/%s",
826                                                         ctdl_netout_dir,
827                                                         mptr->remote_nodename);
828                                         lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
829                                         fp = fopen(filename, "ab");
830                                         if (fp != NULL) {
831                                                 fwrite(sermsg.ser,
832                                                         sermsg.len, 1, fp);
833                                                 fclose(fp);
834                                         }
835                                         else {
836                                                 lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
837                                         }
838         
839                                         /* free the serialized version */
840                                         free(sermsg.ser);
841                                 }
842
843                         }
844                 }
845                 CtdlFreeMessage(msg);
846         }
847
848         /* update lastsent */
849         sc->lastsent = msgnum;
850
851         /* Delete this message if delete-after-send is set */
852         if (delete_after_send) {
853                 CtdlDeleteMessages(CC->room.QRname, &msgnum, 1, "");
854         }
855
856 }
857         
858
859 /*
860  * Batch up and send all outbound traffic from the current room
861  */
862 void network_spoolout_room(char *room_to_spool) {
863         char filename[SIZ];
864         char buf[SIZ];
865         char instr[SIZ];
866         char nodename[256];
867         char roomname[ROOMNAMELEN];
868         char nexthop[256];
869         FILE *fp;
870         struct SpoolControl sc;
871         struct namelist *nptr = NULL;
872         struct maplist *mptr = NULL;
873         size_t miscsize = 0;
874         size_t linesize = 0;
875         int skipthisline = 0;
876         int i;
877
878         /*
879          * If the room doesn't exist, don't try to perform its networking tasks.
880          * Normally this should never happen, but once in a while maybe a room gets
881          * queued for networking and then deleted before it can happen.
882          */
883         if (getroom(&CC->room, room_to_spool) != 0) {
884                 lprintf(CTDL_CRIT, "ERROR: cannot load <%s>\n", room_to_spool);
885                 return;
886         }
887
888         memset(&sc, 0, sizeof(struct SpoolControl));
889         assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
890
891         begin_critical_section(S_NETCONFIGS);
892
893         /* Only do net processing for rooms that have netconfigs */
894         fp = fopen(filename, "r");
895         if (fp == NULL) {
896                 end_critical_section(S_NETCONFIGS);
897                 return;
898         }
899
900         lprintf(CTDL_INFO, "Networking started for <%s>\n", CC->room.QRname);
901
902         while (fgets(buf, sizeof buf, fp) != NULL) {
903                 buf[strlen(buf)-1] = 0;
904
905                 extract_token(instr, buf, 0, '|', sizeof instr);
906                 if (!strcasecmp(instr, "lastsent")) {
907                         sc.lastsent = extract_long(buf, 1);
908                 }
909                 else if (!strcasecmp(instr, "listrecp")) {
910                         nptr = (struct namelist *)
911                                 malloc(sizeof(struct namelist));
912                         nptr->next = sc.listrecps;
913                         extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
914                         sc.listrecps = nptr;
915                 }
916                 else if (!strcasecmp(instr, "participate")) {
917                         nptr = (struct namelist *)
918                                 malloc(sizeof(struct namelist));
919                         nptr->next = sc.participates;
920                         extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
921                         sc.participates = nptr;
922                 }
923                 else if (!strcasecmp(instr, "digestrecp")) {
924                         nptr = (struct namelist *)
925                                 malloc(sizeof(struct namelist));
926                         nptr->next = sc.digestrecps;
927                         extract_token(nptr->name, buf, 1, '|', sizeof nptr->name);
928                         sc.digestrecps = nptr;
929                 }
930                 else if (!strcasecmp(instr, "ignet_push_share")) {
931                         /* by checking each node's validity, we automatically
932                          * purge nodes which do not exist from room network
933                          * configurations at this time.
934                          */
935                         extract_token(nodename, buf, 1, '|', sizeof nodename);
936                         extract_token(roomname, buf, 2, '|', sizeof roomname);
937                         strcpy(nexthop, "xxx");
938                         if (is_valid_node(nexthop, NULL, nodename) == 0) {
939                                 if (IsEmptyStr(nexthop)) {
940                                         mptr = (struct maplist *)
941                                                 malloc(sizeof(struct maplist));
942                                         mptr->next = sc.ignet_push_shares;
943                                         strcpy(mptr->remote_nodename, nodename);
944                                         strcpy(mptr->remote_roomname, roomname);
945                                         sc.ignet_push_shares = mptr;
946                                 }
947                         }
948                 }
949                 else {
950                         /* Preserve 'other' lines ... *unless* they happen to
951                          * be subscribe/unsubscribe pendings with expired
952                          * timestamps.
953                          */
954                         skipthisline = 0;
955                         if (!strncasecmp(buf, "subpending|", 11)) {
956                                 if (time(NULL) - extract_long(buf, 4) > EXP) {
957                                         skipthisline = 1;
958                                 }
959                         }
960                         if (!strncasecmp(buf, "unsubpending|", 13)) {
961                                 if (time(NULL) - extract_long(buf, 3) > EXP) {
962                                         skipthisline = 1;
963                                 }
964                         }
965
966                         if (skipthisline == 0) {
967                                 linesize = strlen(buf);
968                                 sc.misc = realloc(sc.misc,
969                                         (miscsize + linesize + 2) );
970                                 sprintf(&sc.misc[miscsize], "%s\n", buf);
971                                 miscsize = miscsize + linesize + 1;
972                         }
973                 }
974
975
976         }
977         fclose(fp);
978
979         /* If there are digest recipients, we have to build a digest */
980         if (sc.digestrecps != NULL) {
981                 sc.digestfp = tmpfile();
982                 fprintf(sc.digestfp, "Content-type: text/plain\n\n");
983         }
984
985         /* Do something useful */
986         CtdlForEachMessage(MSGS_GT, sc.lastsent, NULL, NULL, NULL,
987                 network_spool_msg, &sc);
988
989         /* If we wrote a digest, deliver it and then close it */
990         snprintf(buf, sizeof buf, "room_%s@%s",
991                 CC->room.QRname, config.c_fqdn);
992         for (i=0; buf[i]; ++i) {
993                 buf[i] = tolower(buf[i]);
994                 if (isspace(buf[i])) buf[i] = '_';
995         }
996         if (sc.digestfp != NULL) {
997                 fprintf(sc.digestfp,    " -----------------------------------"
998                                         "------------------------------------"
999                                         "-------\n"
1000                                         "You are subscribed to the '%s' "
1001                                         "list.\n"
1002                                         "To post to the list: %s\n",
1003                                         CC->room.QRname, buf
1004                 );
1005                 network_deliver_digest(&sc);    /* deliver and close */
1006         }
1007
1008         /* Now rewrite the config file */
1009         fp = fopen(filename, "w");
1010         if (fp == NULL) {
1011                 lprintf(CTDL_CRIT, "ERROR: cannot open %s: %s\n",
1012                         filename, strerror(errno));
1013         }
1014         else {
1015                 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
1016
1017                 /* Write out the listrecps while freeing from memory at the
1018                  * same time.  Am I clever or what?  :)
1019                  */
1020                 while (sc.listrecps != NULL) {
1021                         fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
1022                         nptr = sc.listrecps->next;
1023                         free(sc.listrecps);
1024                         sc.listrecps = nptr;
1025                 }
1026                 /* Do the same for digestrecps */
1027                 while (sc.digestrecps != NULL) {
1028                         fprintf(fp, "digestrecp|%s\n", sc.digestrecps->name);
1029                         nptr = sc.digestrecps->next;
1030                         free(sc.digestrecps);
1031                         sc.digestrecps = nptr;
1032                 }
1033                 /* Do the same for participates */
1034                 while (sc.participates != NULL) {
1035                         fprintf(fp, "participate|%s\n", sc.participates->name);
1036                         nptr = sc.participates->next;
1037                         free(sc.participates);
1038                         sc.participates = nptr;
1039                 }
1040                 while (sc.ignet_push_shares != NULL) {
1041                         /* by checking each node's validity, we automatically
1042                          * purge nodes which do not exist from room network
1043                          * configurations at this time.
1044                          */
1045                         if (is_valid_node(NULL, NULL, sc.ignet_push_shares->remote_nodename) == 0) {
1046                         }
1047                         fprintf(fp, "ignet_push_share|%s",
1048                                 sc.ignet_push_shares->remote_nodename);
1049                         if (!IsEmptyStr(sc.ignet_push_shares->remote_roomname)) {
1050                                 fprintf(fp, "|%s", sc.ignet_push_shares->remote_roomname);
1051                         }
1052                         fprintf(fp, "\n");
1053                         mptr = sc.ignet_push_shares->next;
1054                         free(sc.ignet_push_shares);
1055                         sc.ignet_push_shares = mptr;
1056                 }
1057                 if (sc.misc != NULL) {
1058                         fwrite(sc.misc, strlen(sc.misc), 1, fp);
1059                 }
1060                 free(sc.misc);
1061
1062                 fclose(fp);
1063         }
1064         end_critical_section(S_NETCONFIGS);
1065 }
1066
1067
1068
1069 /*
1070  * Send the *entire* contents of the current room to one specific network node,
1071  * ignoring anything we know about which messages have already undergone
1072  * network processing.  This can be used to bring a new node into sync.
1073  */
1074 int network_sync_to(char *target_node) {
1075         struct SpoolControl sc;
1076         int num_spooled = 0;
1077         int found_node = 0;
1078         char buf[256];
1079         char sc_type[256];
1080         char sc_node[256];
1081         char sc_room[256];
1082         char filename[256];
1083         FILE *fp;
1084
1085         /* Grab the configuration line we're looking for */
1086         assoc_file_name(filename, sizeof filename, &CC->room, ctdl_netcfg_dir);
1087         begin_critical_section(S_NETCONFIGS);
1088         fp = fopen(filename, "r");
1089         if (fp == NULL) {
1090                 end_critical_section(S_NETCONFIGS);
1091                 return(-1);
1092         }
1093         while (fgets(buf, sizeof buf, fp) != NULL) {
1094                 buf[strlen(buf)-1] = 0;
1095                 extract_token(sc_type, buf, 0, '|', sizeof sc_type);
1096                 extract_token(sc_node, buf, 1, '|', sizeof sc_node);
1097                 extract_token(sc_room, buf, 2, '|', sizeof sc_room);
1098                 if ( (!strcasecmp(sc_type, "ignet_push_share"))
1099                    && (!strcasecmp(sc_node, target_node)) ) {
1100                         found_node = 1;
1101                         
1102                         /* Concise syntax because we don't need a full linked-list */
1103                         memset(&sc, 0, sizeof(struct SpoolControl));
1104                         sc.ignet_push_shares = (struct maplist *)
1105                                 malloc(sizeof(struct maplist));
1106                         sc.ignet_push_shares->next = NULL;
1107                         safestrncpy(sc.ignet_push_shares->remote_nodename,
1108                                 sc_node,
1109                                 sizeof sc.ignet_push_shares->remote_nodename);
1110                         safestrncpy(sc.ignet_push_shares->remote_roomname,
1111                                 sc_room,
1112                                 sizeof sc.ignet_push_shares->remote_roomname);
1113                 }
1114         }
1115         fclose(fp);
1116         end_critical_section(S_NETCONFIGS);
1117
1118         if (!found_node) return(-1);
1119
1120         /* Send ALL messages */
1121         num_spooled = CtdlForEachMessage(MSGS_ALL, 0L, NULL, NULL, NULL,
1122                 network_spool_msg, &sc);
1123
1124         /* Concise cleanup because we know there's only one node in the sc */
1125         free(sc.ignet_push_shares);
1126
1127         lprintf(CTDL_NOTICE, "Synchronized %d messages to <%s>\n",
1128                 num_spooled, target_node);
1129         return(num_spooled);
1130 }
1131
1132
1133 /*
1134  * Implements the NSYN command
1135  */
1136 void cmd_nsyn(char *argbuf) {
1137         int num_spooled;
1138         char target_node[256];
1139
1140         if (CtdlAccessCheck(ac_aide)) return;
1141
1142         extract_token(target_node, argbuf, 0, '|', sizeof target_node);
1143         num_spooled = network_sync_to(target_node);
1144         if (num_spooled >= 0) {
1145                 cprintf("%d Spooled %d messages.\n", CIT_OK, num_spooled);
1146         }
1147         else {
1148                 cprintf("%d No such room/node share exists.\n",
1149                         ERROR + ROOM_NOT_FOUND);
1150         }
1151 }
1152
1153
1154
1155 /*
1156  * Batch up and send all outbound traffic from the current room
1157  */
1158 void network_queue_room(struct ctdlroom *qrbuf, void *data) {
1159         struct RoomProcList *ptr;
1160
1161         ptr = (struct RoomProcList *) malloc(sizeof (struct RoomProcList));
1162         if (ptr == NULL) return;
1163
1164         safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
1165         begin_critical_section(S_RPLIST);
1166         ptr->next = rplist;
1167         rplist = ptr;
1168         end_critical_section(S_RPLIST);
1169 }
1170
1171 void destroy_network_queue_room(void)
1172 {
1173         struct RoomProcList *cur, *p;
1174         struct NetMap *nmcur, *nmp;
1175
1176         cur = rplist;
1177         begin_critical_section(S_RPLIST);
1178         while (cur != NULL)
1179         {
1180                 p = cur->next;
1181                 free (cur);
1182                 cur = p;                
1183         }
1184         rplist = NULL;
1185         end_critical_section(S_RPLIST);
1186
1187         nmcur = the_netmap;
1188         while (nmcur != NULL)
1189         {
1190                 nmp = nmcur->next;
1191                 free (nmcur);
1192                 nmcur = nmp;            
1193         }
1194         the_netmap = NULL;
1195         if (working_ignetcfg != NULL)
1196                 free (working_ignetcfg);
1197         working_ignetcfg = NULL;
1198 }
1199
1200
1201 /*
1202  * Learn topology from path fields
1203  */
1204 void network_learn_topology(char *node, char *path) {
1205         char nexthop[256];
1206         struct NetMap *nmptr;
1207
1208         strcpy(nexthop, "");
1209
1210         if (num_tokens(path, '!') < 3) return;
1211         for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
1212                 if (!strcasecmp(nmptr->nodename, node)) {
1213                         extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1214                         nmptr->lastcontact = time(NULL);
1215                         ++netmap_changed;
1216                         return;
1217                 }
1218         }
1219
1220         /* If we got here then it's not in the map, so add it. */
1221         nmptr = (struct NetMap *) malloc(sizeof (struct NetMap));
1222         strcpy(nmptr->nodename, node);
1223         nmptr->lastcontact = time(NULL);
1224         extract_token(nmptr->nexthop, path, 0, '!', sizeof nmptr->nexthop);
1225         nmptr->next = the_netmap;
1226         the_netmap = nmptr;
1227         ++netmap_changed;
1228 }
1229
1230
1231
1232
1233 /*
1234  * Bounce a message back to the sender
1235  */
1236 void network_bounce(struct CtdlMessage *msg, char *reason) {
1237         char *oldpath = NULL;
1238         char buf[SIZ];
1239         char bouncesource[SIZ];
1240         char recipient[SIZ];
1241         struct recptypes *valid = NULL;
1242         char force_room[ROOMNAMELEN];
1243         static int serialnum = 0;
1244         size_t size;
1245
1246         lprintf(CTDL_DEBUG, "entering network_bounce()\n");
1247
1248         if (msg == NULL) return;
1249
1250         snprintf(bouncesource, sizeof bouncesource, "%s@%s", BOUNCESOURCE, config.c_nodename);
1251
1252         /* 
1253          * Give it a fresh message ID
1254          */
1255         if (msg->cm_fields['I'] != NULL) {
1256                 free(msg->cm_fields['I']);
1257         }
1258         snprintf(buf, sizeof buf, "%ld.%04lx.%04x@%s",
1259                 (long)time(NULL), (long)getpid(), ++serialnum, config.c_fqdn);
1260         msg->cm_fields['I'] = strdup(buf);
1261
1262         /*
1263          * FIXME ... right now we're just sending a bounce; we really want to
1264          * include the text of the bounced message.
1265          */
1266         if (msg->cm_fields['M'] != NULL) {
1267                 free(msg->cm_fields['M']);
1268         }
1269         msg->cm_fields['M'] = strdup(reason);
1270         msg->cm_format_type = 0;
1271
1272         /*
1273          * Turn the message around
1274          */
1275         if (msg->cm_fields['R'] == NULL) {
1276                 free(msg->cm_fields['R']);
1277         }
1278
1279         if (msg->cm_fields['D'] == NULL) {
1280                 free(msg->cm_fields['D']);
1281         }
1282
1283         snprintf(recipient, sizeof recipient, "%s@%s",
1284                 msg->cm_fields['A'], msg->cm_fields['N']);
1285
1286         if (msg->cm_fields['A'] == NULL) {
1287                 free(msg->cm_fields['A']);
1288         }
1289
1290         if (msg->cm_fields['N'] == NULL) {
1291                 free(msg->cm_fields['N']);
1292         }
1293
1294         if (msg->cm_fields['U'] == NULL) {
1295                 free(msg->cm_fields['U']);
1296         }
1297
1298         msg->cm_fields['A'] = strdup(BOUNCESOURCE);
1299         msg->cm_fields['N'] = strdup(config.c_nodename);
1300         msg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
1301
1302         /* prepend our node to the path */
1303         if (msg->cm_fields['P'] != NULL) {
1304                 oldpath = msg->cm_fields['P'];
1305                 msg->cm_fields['P'] = NULL;
1306         }
1307         else {
1308                 oldpath = strdup("unknown_user");
1309         }
1310         size = strlen(oldpath) + SIZ;
1311         msg->cm_fields['P'] = malloc(size);
1312         snprintf(msg->cm_fields['P'], size, "%s!%s", config.c_nodename, oldpath);
1313         free(oldpath);
1314
1315         /* Now submit the message */
1316         valid = validate_recipients(recipient);
1317         if (valid != NULL) if (valid->num_error != 0) {
1318                 free_recipients(valid);
1319                 valid = NULL;
1320         }
1321         if ( (valid == NULL) || (!strcasecmp(recipient, bouncesource)) ) {
1322                 strcpy(force_room, config.c_aideroom);
1323         }
1324         else {
1325                 strcpy(force_room, "");
1326         }
1327         if ( (valid == NULL) && IsEmptyStr(force_room) ) {
1328                 strcpy(force_room, config.c_aideroom);
1329         }
1330         CtdlSubmitMsg(msg, valid, force_room);
1331
1332         /* Clean up */
1333         if (valid != NULL) free_recipients(valid);
1334         CtdlFreeMessage(msg);
1335         lprintf(CTDL_DEBUG, "leaving network_bounce()\n");
1336 }
1337
1338
1339
1340
1341 /*
1342  * Process a buffer containing a single message from a single file
1343  * from the inbound queue 
1344  */
1345 void network_process_buffer(char *buffer, long size) {
1346         struct CtdlMessage *msg = NULL;
1347         long pos;
1348         int field;
1349         struct recptypes *recp = NULL;
1350         char target_room[ROOMNAMELEN];
1351         struct ser_ret sermsg;
1352         char *oldpath = NULL;
1353         char filename[SIZ];
1354         FILE *fp;
1355         char nexthop[SIZ];
1356         unsigned char firstbyte;
1357         unsigned char lastbyte;
1358
1359         /* Validate just a little bit.  First byte should be FF and
1360          * last byte should be 00.
1361          */
1362         firstbyte = buffer[0];
1363         lastbyte = buffer[size-1];
1364         if ( (firstbyte != 255) || (lastbyte != 0) ) {
1365                 lprintf(CTDL_ERR, "Corrupt message ignored.  Length=%ld, firstbyte = %d, lastbyte = %d\n",
1366                         size, firstbyte, lastbyte);
1367                 return;
1368         }
1369
1370         /* Set default target room to trash */
1371         strcpy(target_room, TWITROOM);
1372
1373         /* Load the message into memory */
1374         msg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
1375         memset(msg, 0, sizeof(struct CtdlMessage));
1376         msg->cm_magic = CTDLMESSAGE_MAGIC;
1377         msg->cm_anon_type = buffer[1];
1378         msg->cm_format_type = buffer[2];
1379
1380         for (pos = 3; pos < size; ++pos) {
1381                 field = buffer[pos];
1382                 msg->cm_fields[field] = strdup(&buffer[pos+1]);
1383                 pos = pos + strlen(&buffer[(int)pos]);
1384         }
1385
1386         /* Check for message routing */
1387         if (msg->cm_fields['D'] != NULL) {
1388                 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
1389
1390                         /* route the message */
1391                         strcpy(nexthop, "");
1392                         if (is_valid_node(nexthop, NULL,
1393                            msg->cm_fields['D']) == 0) {
1394
1395                                 /* prepend our node to the path */
1396                                 if (msg->cm_fields['P'] != NULL) {
1397                                         oldpath = msg->cm_fields['P'];
1398                                         msg->cm_fields['P'] = NULL;
1399                                 }
1400                                 else {
1401                                         oldpath = strdup("unknown_user");
1402                                 }
1403                                 size = strlen(oldpath) + SIZ;
1404                                 msg->cm_fields['P'] = malloc(size);
1405                                 snprintf(msg->cm_fields['P'], size, "%s!%s",
1406                                         config.c_nodename, oldpath);
1407                                 free(oldpath);
1408
1409                                 /* serialize the message */
1410                                 serialize_message(&sermsg, msg);
1411
1412                                 /* now send it */
1413                                 if (IsEmptyStr(nexthop)) {
1414                                         strcpy(nexthop, msg->cm_fields['D']);
1415                                 }
1416                                 snprintf(filename, 
1417                                                  sizeof filename,
1418                                                  "%s/%s",
1419                                                  ctdl_netout_dir,
1420                                                  nexthop);
1421                                 lprintf(CTDL_DEBUG, "Appending to %s\n", filename);
1422                                 fp = fopen(filename, "ab");
1423                                 if (fp != NULL) {
1424                                         fwrite(sermsg.ser,
1425                                                 sermsg.len, 1, fp);
1426                                         fclose(fp);
1427                                 }
1428                                 else {
1429                                         lprintf(CTDL_ERR, "%s: %s\n", filename, strerror(errno));
1430                                 }
1431                                 free(sermsg.ser);
1432                                 CtdlFreeMessage(msg);
1433                                 return;
1434                         }
1435                         
1436                         else {  /* invalid destination node name */
1437
1438                                 network_bounce(msg,
1439 "A message you sent could not be delivered due to an invalid destination node"
1440 " name.  Please check the address and try sending the message again.\n");
1441                                 msg = NULL;
1442                                 return;
1443
1444                         }
1445                 }
1446         }
1447
1448         /*
1449          * Check to see if we already have a copy of this message, and
1450          * abort its processing if so.  (We used to post a warning to Aide>
1451          * every time this happened, but the network is now so densely
1452          * connected that it's inevitable.)
1453          */
1454         if (network_usetable(msg) != 0) {
1455                 CtdlFreeMessage(msg);
1456                 return;
1457         }
1458
1459         /* Learn network topology from the path */
1460         if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
1461                 network_learn_topology(msg->cm_fields['N'], 
1462                                         msg->cm_fields['P']);
1463         }
1464
1465         /* Is the sending node giving us a very persuasive suggestion about
1466          * which room this message should be saved in?  If so, go with that.
1467          */
1468         if (msg->cm_fields['C'] != NULL) {
1469                 safestrncpy(target_room,
1470                         msg->cm_fields['C'],
1471                         sizeof target_room);
1472         }
1473
1474         /* Otherwise, does it have a recipient?  If so, validate it... */
1475         else if (msg->cm_fields['R'] != NULL) {
1476                 recp = validate_recipients(msg->cm_fields['R']);
1477                 if (recp != NULL) if (recp->num_error != 0) {
1478                         network_bounce(msg,
1479                                 "A message you sent could not be delivered due to an invalid address.\n"
1480                                 "Please check the address and try sending the message again.\n");
1481                         msg = NULL;
1482                         free_recipients(recp);
1483                         return;
1484                 }
1485                 strcpy(target_room, "");        /* no target room if mail */
1486         }
1487
1488         /* Our last shot at finding a home for this message is to see if
1489          * it has the O field (Originating room) set.
1490          */
1491         else if (msg->cm_fields['O'] != NULL) {
1492                 safestrncpy(target_room,
1493                         msg->cm_fields['O'],
1494                         sizeof target_room);
1495         }
1496
1497         /* Strip out fields that are only relevant during transit */
1498         if (msg->cm_fields['D'] != NULL) {
1499                 free(msg->cm_fields['D']);
1500                 msg->cm_fields['D'] = NULL;
1501         }
1502         if (msg->cm_fields['C'] != NULL) {
1503                 free(msg->cm_fields['C']);
1504                 msg->cm_fields['C'] = NULL;
1505         }
1506
1507         /* save the message into a room */
1508         if (PerformNetprocHooks(msg, target_room) == 0) {
1509                 msg->cm_flags = CM_SKIP_HOOKS;
1510                 CtdlSubmitMsg(msg, recp, target_room);
1511         }
1512         CtdlFreeMessage(msg);
1513         free_recipients(recp);
1514 }
1515
1516
1517 /*
1518  * Process a single message from a single file from the inbound queue 
1519  */
1520 void network_process_message(FILE *fp, long msgstart, long msgend) {
1521         long hold_pos;
1522         long size;
1523         char *buffer;
1524
1525         hold_pos = ftell(fp);
1526         size = msgend - msgstart + 1;
1527         buffer = malloc(size);
1528         if (buffer != NULL) {
1529                 fseek(fp, msgstart, SEEK_SET);
1530                 fread(buffer, size, 1, fp);
1531                 network_process_buffer(buffer, size);
1532                 free(buffer);
1533         }
1534
1535         fseek(fp, hold_pos, SEEK_SET);
1536 }
1537
1538
1539 /*
1540  * Process a single file from the inbound queue 
1541  */
1542 void network_process_file(char *filename) {
1543         FILE *fp;
1544         long msgstart = (-1L);
1545         long msgend = (-1L);
1546         long msgcur = 0L;
1547         int ch;
1548
1549
1550         fp = fopen(filename, "rb");
1551         if (fp == NULL) {
1552                 lprintf(CTDL_CRIT, "Error opening %s: %s\n",
1553                         filename, strerror(errno));
1554                 return;
1555         }
1556
1557         lprintf(CTDL_INFO, "network: processing <%s>\n", filename);
1558
1559         /* Look for messages in the data stream and break them out */
1560         while (ch = getc(fp), ch >= 0) {
1561         
1562                 if (ch == 255) {
1563                         if (msgstart >= 0L) {
1564                                 msgend = msgcur - 1;
1565                                 network_process_message(fp, msgstart, msgend);
1566                         }
1567                         msgstart = msgcur;
1568                 }
1569
1570                 ++msgcur;
1571         }
1572
1573         msgend = msgcur - 1;
1574         if (msgstart >= 0L) {
1575                 network_process_message(fp, msgstart, msgend);
1576         }
1577
1578         fclose(fp);
1579         unlink(filename);
1580 }
1581
1582
1583 /*
1584  * Process anything in the inbound queue
1585  */
1586 void network_do_spoolin(void) {
1587         DIR *dp;
1588         struct dirent *d;
1589         struct stat statbuf;
1590         char filename[256];
1591         static time_t last_spoolin_mtime = 0L;
1592
1593         /*
1594          * Check the spoolin directory's modification time.  If it hasn't
1595          * been touched, we don't need to scan it.
1596          */
1597         if (stat(ctdl_netin_dir, &statbuf)) return;
1598         if (statbuf.st_mtime == last_spoolin_mtime) {
1599                 lprintf(CTDL_DEBUG, "network: nothing in inbound queue\n");
1600                 return;
1601         }
1602         last_spoolin_mtime = statbuf.st_mtime;
1603         lprintf(CTDL_DEBUG, "network: processing inbound queue\n");
1604
1605         /*
1606          * Ok, there's something interesting in there, so scan it.
1607          */
1608         dp = opendir(ctdl_netin_dir);
1609         if (dp == NULL) return;
1610
1611         while (d = readdir(dp), d != NULL) {
1612                 if ((strcmp(d->d_name, ".")) && (strcmp(d->d_name, ".."))) {
1613                         snprintf(filename, 
1614                                          sizeof filename,
1615                                          "%s/%s",
1616                                          ctdl_netin_dir,
1617                                          d->d_name);
1618                         network_process_file(filename);
1619                 }
1620         }
1621
1622         closedir(dp);
1623 }
1624
1625 /*
1626  * Delete any files in the outbound queue that were intended
1627  * to be sent to nodes which no longer exist.
1628  */
1629 void network_purge_spoolout(void) {
1630         DIR *dp;
1631         struct dirent *d;
1632         char filename[256];
1633         char nexthop[256];
1634         int i;
1635
1636         dp = opendir(ctdl_netout_dir);
1637         if (dp == NULL) return;
1638
1639         while (d = readdir(dp), d != NULL) {
1640                 if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
1641                         continue;
1642                 snprintf(filename, 
1643                                  sizeof filename,
1644                                  "%s/%s",
1645                                  ctdl_netout_dir,
1646                                  d->d_name);
1647
1648                 strcpy(nexthop, "");
1649                 i = is_valid_node(nexthop, NULL, d->d_name);
1650         
1651                 if ( (i != 0) || !IsEmptyStr(nexthop) ) {
1652                         unlink(filename);
1653                 }
1654         }
1655
1656
1657         closedir(dp);
1658 }
1659
1660
1661 /*
1662  * receive network spool from the remote system
1663  */
1664 void receive_spool(int sock, char *remote_nodename) {
1665         long download_len;
1666         long bytes_received;
1667         char buf[SIZ];
1668         static char pbuf[IGNET_PACKET_SIZE];
1669         char tempfilename[PATH_MAX];
1670         char filename[PATH_MAX];
1671         long plen;
1672         FILE *fp, *newfp;
1673
1674         CtdlMakeTempFileName(tempfilename, sizeof tempfilename);
1675         if (sock_puts(sock, "NDOP") < 0) return;
1676         if (sock_getln(sock, buf, sizeof buf) < 0) return;
1677         lprintf(CTDL_DEBUG, "<%s\n", buf);
1678         if (buf[0] != '2') {
1679                 return;
1680         }
1681         download_len = extract_long(&buf[4], 0);
1682
1683         bytes_received = 0L;
1684         fp = fopen(tempfilename, "w");
1685         if (fp == NULL) {
1686                 lprintf(CTDL_CRIT, "cannot open download file locally: %s\n",
1687                         strerror(errno));
1688                 return;
1689         }
1690
1691         while (bytes_received < download_len) {
1692                 snprintf(buf, sizeof buf, "READ %ld|%ld",
1693                         bytes_received,
1694                      ((download_len - bytes_received > IGNET_PACKET_SIZE)
1695                  ? IGNET_PACKET_SIZE : (download_len - bytes_received)));
1696                 if (sock_puts(sock, buf) < 0) {
1697                         fclose(fp);
1698                         unlink(tempfilename);
1699                         return;
1700                 }
1701                 if (sock_getln(sock, buf, sizeof buf) < 0) {
1702                         fclose(fp);
1703                         unlink(tempfilename);
1704                         return;
1705                 }
1706                 if (buf[0] == '6') {
1707                         plen = extract_long(&buf[4], 0);
1708                         if (sock_read(sock, pbuf, plen, 1) < 0) {
1709                                 fclose(fp);
1710                                 unlink(tempfilename);
1711                                 return;
1712                         }
1713                         fwrite((char *) pbuf, plen, 1, fp);
1714                         bytes_received = bytes_received + plen;
1715                 }
1716         }
1717
1718         fclose(fp);
1719         if (sock_puts(sock, "CLOS") < 0) {
1720                 unlink(tempfilename);
1721                 return;
1722         }
1723         if (sock_getln(sock, buf, sizeof buf) < 0) {
1724                 unlink(tempfilename);
1725                 return;
1726         }
1727         if (download_len > 0)
1728                 lprintf(CTDL_NOTICE, "Received %ld octets from <%s>\n",
1729                                 download_len, remote_nodename);
1730         lprintf(CTDL_DEBUG, "%s\n", buf);
1731         
1732         /* Now copy the temp file to its permanent location.
1733          * (We copy instead of link because they may be on different filesystems)
1734          */
1735         begin_critical_section(S_NETSPOOL);
1736         snprintf(filename, 
1737                          sizeof filename, 
1738                          "%s/%s.%ld",
1739                          ctdl_netin_dir,
1740                          remote_nodename, 
1741                          (long) getpid());
1742         fp = fopen(tempfilename, "r");
1743         if (fp != NULL) {
1744                 newfp = fopen(filename, "w");
1745                 if (newfp != NULL) {
1746                         while (fgets(buf, sizeof buf, fp) != NULL) {
1747                                 fprintf(newfp, "%s", buf);
1748                         }
1749                         fclose(newfp);
1750                 }
1751                 fclose(fp);
1752         }
1753         end_critical_section(S_NETSPOOL);
1754         unlink(tempfilename);
1755 }
1756
1757
1758
1759 /*
1760  * transmit network spool to the remote system
1761  */
1762 void transmit_spool(int sock, char *remote_nodename)
1763 {
1764         char buf[SIZ];
1765         char pbuf[4096];
1766         long plen;
1767         long bytes_to_write, thisblock, bytes_written;
1768         int fd;
1769         char sfname[128];
1770
1771         if (sock_puts(sock, "NUOP") < 0) return;
1772         if (sock_getln(sock, buf, sizeof buf) < 0) return;
1773         lprintf(CTDL_DEBUG, "<%s\n", buf);
1774         if (buf[0] != '2') {
1775                 return;
1776         }
1777
1778         snprintf(sfname, sizeof sfname, 
1779                          "%s/%s",
1780                          ctdl_netout_dir,
1781                          remote_nodename);
1782         fd = open(sfname, O_RDONLY);
1783         if (fd < 0) {
1784                 if (errno != ENOENT) {
1785                         lprintf(CTDL_CRIT, "cannot open upload file locally: %s\n",
1786                                 strerror(errno));
1787                 }
1788                 return;
1789         }
1790         bytes_written = 0;
1791         while (plen = (long) read(fd, pbuf, IGNET_PACKET_SIZE), plen > 0L) {
1792                 bytes_to_write = plen;
1793                 while (bytes_to_write > 0L) {
1794                         snprintf(buf, sizeof buf, "WRIT %ld", bytes_to_write);
1795                         if (sock_puts(sock, buf) < 0) {
1796                                 close(fd);
1797                                 return;
1798                         }
1799                         if (sock_getln(sock, buf, sizeof buf) < 0) {
1800                                 close(fd);
1801                                 return;
1802                         }
1803                         thisblock = atol(&buf[4]);
1804                         if (buf[0] == '7') {
1805                                 if (sock_write(sock, pbuf,
1806                                    (int) thisblock) < 0) {
1807                                         close(fd);
1808                                         return;
1809                                 }
1810                                 bytes_to_write -= thisblock;
1811                                 bytes_written += thisblock;
1812                         } else {
1813                                 goto ABORTUPL;
1814                         }
1815                 }
1816         }
1817
1818 ABORTUPL:
1819         close(fd);
1820         if (sock_puts(sock, "UCLS 1") < 0) return;
1821         if (sock_getln(sock, buf, sizeof buf) < 0) return;
1822         lprintf(CTDL_NOTICE, "Sent %ld octets to <%s>\n",
1823                         bytes_written, remote_nodename);
1824         lprintf(CTDL_DEBUG, "<%s\n", buf);
1825         if (buf[0] == '2') {
1826                 lprintf(CTDL_DEBUG, "Removing <%s>\n", sfname);
1827                 unlink(sfname);
1828         }
1829 }
1830
1831
1832
1833 /*
1834  * Poll one Citadel node (called by network_poll_other_citadel_nodes() below)
1835  */
1836 void network_poll_node(char *node, char *secret, char *host, char *port) {
1837         int sock;
1838         char buf[SIZ];
1839
1840         if (network_talking_to(node, NTT_CHECK)) return;
1841         network_talking_to(node, NTT_ADD);
1842         lprintf(CTDL_NOTICE, "Connecting to <%s> at %s:%s\n", node, host, port);
1843
1844         sock = sock_connect(host, port, "tcp");
1845         if (sock < 0) {
1846                 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
1847                 network_talking_to(node, NTT_REMOVE);
1848                 return;
1849         }
1850         
1851         lprintf(CTDL_DEBUG, "Connected!\n");
1852
1853         /* Read the server greeting */
1854         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
1855         lprintf(CTDL_DEBUG, ">%s\n", buf);
1856
1857         /* Identify ourselves */
1858         snprintf(buf, sizeof buf, "NETP %s|%s", config.c_nodename, secret);
1859         lprintf(CTDL_DEBUG, "<%s\n", buf);
1860         if (sock_puts(sock, buf) <0) goto bail;
1861         if (sock_getln(sock, buf, sizeof buf) < 0) goto bail;
1862         lprintf(CTDL_DEBUG, ">%s\n", buf);
1863         if (buf[0] != '2') goto bail;
1864
1865         /* At this point we are authenticated. */
1866         receive_spool(sock, node);
1867         transmit_spool(sock, node);
1868
1869         sock_puts(sock, "QUIT");
1870 bail:   sock_close(sock);
1871         network_talking_to(node, NTT_REMOVE);
1872 }
1873
1874
1875
1876 /*
1877  * Poll other Citadel nodes and transfer inbound/outbound network data.
1878  * Set "full" to nonzero to force a poll of every node, or to zero to poll
1879  * only nodes to which we have data to send.
1880  */
1881 void network_poll_other_citadel_nodes(int full_poll) {
1882         int i;
1883         char linebuf[256];
1884         char node[SIZ];
1885         char host[256];
1886         char port[256];
1887         char secret[256];
1888         int poll = 0;
1889         char spoolfile[256];
1890
1891         if (working_ignetcfg == NULL) {
1892                 lprintf(CTDL_DEBUG, "No nodes defined - not polling\n");
1893                 return;
1894         }
1895
1896         /* Use the string tokenizer to grab one line at a time */
1897         for (i=0; i<num_tokens(working_ignetcfg, '\n'); ++i) {
1898                 extract_token(linebuf, working_ignetcfg, i, '\n', sizeof linebuf);
1899                 extract_token(node, linebuf, 0, '|', sizeof node);
1900                 extract_token(secret, linebuf, 1, '|', sizeof secret);
1901                 extract_token(host, linebuf, 2, '|', sizeof host);
1902                 extract_token(port, linebuf, 3, '|', sizeof port);
1903                 if ( !IsEmptyStr(node) && !IsEmptyStr(secret) 
1904                    && !IsEmptyStr(host) && !IsEmptyStr(port)) {
1905                         poll = full_poll;
1906                         if (poll == 0) {
1907                                 snprintf(spoolfile, 
1908                                                  sizeof spoolfile,
1909                                                  "%s/%s",
1910                                                  ctdl_netout_dir, 
1911                                                  node);
1912                                 if (access(spoolfile, R_OK) == 0) {
1913                                         poll = 1;
1914                                 }
1915                         }
1916                         if (poll) {
1917                                 network_poll_node(node, secret, host, port);
1918                         }
1919                 }
1920         }
1921
1922 }
1923
1924
1925
1926
1927 /*
1928  * It's ok if these directories already exist.  Just fail silently.
1929  */
1930 void create_spool_dirs(void) {
1931         mkdir(ctdl_spool_dir, 0700);
1932         chown(ctdl_spool_dir, CTDLUID, (-1));
1933         mkdir(ctdl_netin_dir, 0700);
1934         chown(ctdl_netin_dir, CTDLUID, (-1));
1935         mkdir(ctdl_netout_dir, 0700);
1936         chown(ctdl_netout_dir, CTDLUID, (-1));
1937 }
1938
1939
1940
1941
1942
1943 /*
1944  * network_do_queue()
1945  * 
1946  * Run through the rooms doing various types of network stuff.
1947  */
1948 void network_do_queue(void) {
1949         static time_t last_run = 0L;
1950         struct RoomProcList *ptr;
1951         int full_processing = 1;
1952
1953         /*
1954          * Run the full set of processing tasks no more frequently
1955          * than once every n seconds
1956          */
1957         if ( (time(NULL) - last_run) < config.c_net_freq ) {
1958                 full_processing = 0;
1959         }
1960
1961         /*
1962          * This is a simple concurrency check to make sure only one queue run
1963          * is done at a time.  We could do this with a mutex, but since we
1964          * don't really require extremely fine granularity here, we'll do it
1965          * with a static variable instead.
1966          */
1967         if (doing_queue) return;
1968         doing_queue = 1;
1969
1970         /* Load the IGnet Configuration into memory */
1971         load_working_ignetcfg();
1972
1973         /*
1974          * Poll other Citadel nodes.  Maybe.  If "full_processing" is set
1975          * then we poll everyone.  Otherwise we only poll nodes we have stuff
1976          * to send to.
1977          */
1978         network_poll_other_citadel_nodes(full_processing);
1979
1980         /*
1981          * Load the network map and filter list into memory.
1982          */
1983         read_network_map();
1984         filterlist = load_filter_list();
1985
1986         /* 
1987          * Go ahead and run the queue
1988          */
1989         if (full_processing) {
1990                 lprintf(CTDL_DEBUG, "network: loading outbound queue\n");
1991                 ForEachRoom(network_queue_room, NULL);
1992         }
1993
1994         if (rplist != NULL) {
1995                 lprintf(CTDL_DEBUG, "network: running outbound queue\n");
1996                 while (rplist != NULL) {
1997                         char spoolroomname[ROOMNAMELEN];
1998                         safestrncpy(spoolroomname, rplist->name, sizeof spoolroomname);
1999                         begin_critical_section(S_RPLIST);
2000
2001                         /* pop this record off the list */
2002                         ptr = rplist;
2003                         rplist = rplist->next;
2004                         free(ptr);
2005
2006                         /* invalidate any duplicate entries to prevent double processing */
2007                         for (ptr=rplist; ptr!=NULL; ptr=ptr->next) {
2008                                 if (!strcasecmp(ptr->name, spoolroomname)) {
2009                                         ptr->name[0] = 0;
2010                                 }
2011                         }
2012
2013                         end_critical_section(S_RPLIST);
2014                         if (spoolroomname[0] != 0) {
2015                                 network_spoolout_room(spoolroomname);
2016                         }
2017                 }
2018         }
2019
2020         /* If there is anything in the inbound queue, process it */
2021         network_do_spoolin();
2022
2023         /* Save the network map back to disk */
2024         write_network_map();
2025
2026         /* Free the filter list in memory */
2027         free_filter_list(filterlist);
2028         filterlist = NULL;
2029
2030         network_purge_spoolout();
2031
2032         lprintf(CTDL_DEBUG, "network: queue run completed\n");
2033
2034         if (full_processing) {
2035                 last_run = time(NULL);
2036         }
2037
2038         doing_queue = 0;
2039 }
2040
2041
2042 /*
2043  * cmd_netp() - authenticate to the server as another Citadel node polling
2044  *            for network traffic
2045  */
2046 void cmd_netp(char *cmdbuf)
2047 {
2048         char node[256];
2049         char pass[256];
2050         int v;
2051
2052         char secret[256];
2053         char nexthop[256];
2054
2055         /* Authenticate */
2056         extract_token(node, cmdbuf, 0, '|', sizeof node);
2057         extract_token(pass, cmdbuf, 1, '|', sizeof pass);
2058
2059         if (doing_queue) {
2060                 lprintf(CTDL_WARNING, "Network node <%s> refused - spooling", node);
2061                 cprintf("%d spooling - try again in a few minutes\n",
2062                         ERROR + RESOURCE_BUSY);
2063                 return;
2064         }
2065
2066         /* load the IGnet Configuration to check node validity */
2067         load_working_ignetcfg();
2068         v = is_valid_node(nexthop, secret, node);
2069
2070         if (v != 0) {
2071                 lprintf(CTDL_WARNING, "Unknown node <%s>\n", node);
2072                 cprintf("%d authentication failed\n",
2073                         ERROR + PASSWORD_REQUIRED);
2074                 return;
2075         }
2076
2077         if (strcasecmp(pass, secret)) {
2078                 lprintf(CTDL_WARNING, "Bad password for network node <%s>", node);
2079                 cprintf("%d authentication failed\n", ERROR + PASSWORD_REQUIRED);
2080                 return;
2081         }
2082
2083         if (network_talking_to(node, NTT_CHECK)) {
2084                 lprintf(CTDL_WARNING, "Duplicate session for network node <%s>", node);
2085                 cprintf("%d Already talking to %s right now\n", ERROR + RESOURCE_BUSY, node);
2086                 return;
2087         }
2088
2089         safestrncpy(CC->net_node, node, sizeof CC->net_node);
2090         network_talking_to(node, NTT_ADD);
2091         lprintf(CTDL_NOTICE, "Network node <%s> logged in\n", CC->net_node);
2092         cprintf("%d authenticated as network node '%s'\n", CIT_OK,
2093                 CC->net_node);
2094 }
2095
2096 int network_room_handler (struct ctdlroom *room)
2097 {
2098         network_queue_room(room, NULL);
2099         return 0;
2100 }
2101
2102 /*
2103  * Module entry point
2104  */
2105 CTDL_MODULE_INIT(network)
2106 {
2107         create_spool_dirs();
2108         CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
2109         CtdlRegisterProtoHook(cmd_snet, "SNET", "Set network config");
2110         CtdlRegisterProtoHook(cmd_netp, "NETP", "Identify as network poller");
2111         CtdlRegisterProtoHook(cmd_nsyn, "NSYN", "Synchronize room to node");
2112         CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);
2113         CtdlRegisterRoomHook(network_room_handler);
2114         CtdlRegisterCleanupHook(destroy_network_queue_room);
2115         
2116
2117         /* return our Subversion id for the Log */
2118         return "$Id$";
2119 }