* You guessed it: still more code for the new networker.
[citadel.git] / citadel / serv_network.c
1 /*
2  * $Id$ 
3  *
4  * This module will eventually replace netproc and some of its utilities.  In
5  * the meantime, it serves as a mailing list manager.
6  *
7  * Copyright (C) 2000-2001 by Art Cancro and others.
8  * This code is released under the terms of the GNU General Public License.
9  *
10  */
11
12 #include "sysdep.h"
13 #include <stdlib.h>
14 #include <unistd.h>
15 #include <stdio.h>
16 #include <fcntl.h>
17 #include <signal.h>
18 #include <pwd.h>
19 #include <errno.h>
20 #include <sys/types.h>
21 #include <dirent.h>
22 #if TIME_WITH_SYS_TIME
23 # include <sys/time.h>
24 # include <time.h>
25 #else
26 # if HAVE_SYS_TIME_H
27 #  include <sys/time.h>
28 # else
29 #  include <time.h>
30 # endif
31 #endif
32
33 #include <sys/wait.h>
34 #include <string.h>
35 #include <limits.h>
36 #include "citadel.h"
37 #include "server.h"
38 #include "sysdep_decls.h"
39 #include "citserver.h"
40 #include "support.h"
41 #include "config.h"
42 #include "dynloader.h"
43 #include "room_ops.h"
44 #include "user_ops.h"
45 #include "policy.h"
46 #include "database.h"
47 #include "msgbase.h"
48 #include "tools.h"
49 #include "internet_addressing.h"
50 #include "serv_network.h"
51
52
53 /*
54  * When we do network processing, it's accomplished in two passes; one to
55  * gather a list of rooms and one to actually do them.  It's ok that rplist
56  * is global; this process *only* runs as part of the housekeeping loop and
57  * therefore only one will run at a time.
58  */
59 struct RoomProcList {
60         struct RoomProcList *next;
61         char name[ROOMNAMELEN];
62 };
63
64 struct RoomProcList *rplist = NULL;
65
66
67 /*
68  * We build a map of the Citadel network during network runs.
69  */
70 struct NetMap {
71         struct NetMap *next;
72         char nodename[SIZ];
73         time_t lastcontact;
74         char nexthop[SIZ];
75 };
76
77 struct NetMap *the_netmap = NULL;
78
79
80
81 /* 
82  * Read the network map from its configuration file into memory.
83  */
84 void read_network_map(void) {
85         char *serialized_map = NULL;
86         int i;
87         char buf[SIZ];
88         struct NetMap *nmptr;
89
90         serialized_map = CtdlGetSysConfig(IGNETMAP);
91         if (serialized_map == NULL) return;     /* if null, no entries */
92
93         /* Use the string tokenizer to grab one line at a time */
94         for (i=0; i<num_tokens(serialized_map, '\n'); ++i) {
95                 extract_token(buf, serialized_map, i, '\n');
96                 nmptr = (struct NetMap *) mallok(sizeof(struct NetMap));
97                 extract(nmptr->nodename, buf, 0);
98                 nmptr->lastcontact = extract_long(buf, 1);
99                 extract(nmptr->nexthop, buf, 2);
100                 nmptr->next = the_netmap;
101                 the_netmap = nmptr;
102         }
103
104         phree(serialized_map);
105 }
106
107
108 /*
109  * Write the network map from memory back to the configuration file.
110  */
111 void write_network_map(void) {
112         char *serialized_map = NULL;
113         struct NetMap *nmptr;
114
115         serialized_map = strdoop("");
116
117         if (the_netmap != NULL) {
118                 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
119                         serialized_map = reallok(serialized_map,
120                                                 (strlen(serialized_map)+SIZ) );
121                         if (strlen(nmptr->nodename) > 0) {
122                                 sprintf(&serialized_map[strlen(serialized_map)],
123                                         "%s|%ld|%s\n",
124                                         nmptr->nodename,
125                                         nmptr->lastcontact,
126                                         nmptr->nexthop);
127                         }
128                 }
129         }
130
131         CtdlPutSysConfig(IGNETMAP, serialized_map);
132         phree(serialized_map);
133
134         /* Now free the list */
135         while (the_netmap != NULL) {
136                 nmptr = the_netmap->next;
137                 phree(the_netmap);
138                 the_netmap = nmptr;
139         }
140 }
141
142
143
144 /* 
145  * Check the network map and determine whether the supplied node name is
146  * valid.  If it is not a neighbor node, supply the name of a neighbor node
147  * which is the next hop.
148  */
149 int is_valid_node(char *nexthop, char *node) {
150         char *ignetcfg = NULL;
151         int i;
152         char linebuf[SIZ];
153         char buf[SIZ];
154         int retval;
155         struct NetMap *nmptr;
156
157         if (node == NULL) {
158                 return(-1);
159         }
160
161         /*
162          * First try the neighbor nodes
163          */
164         ignetcfg = CtdlGetSysConfig(IGNETCFG);
165         if (ignetcfg == NULL) {
166                 if (nexthop != NULL) {
167                         strcpy(nexthop, "");
168                 }
169                 return(-1);
170         }
171
172         retval = (-1);
173         if (nexthop != NULL) {
174                 strcpy(nexthop, "");
175         }
176
177         /* Use the string tokenizer to grab one line at a time */
178         for (i=0; i<num_tokens(ignetcfg, '\n'); ++i) {
179                 extract_token(linebuf, ignetcfg, i, '\n');
180                 extract(buf, linebuf, 0);
181                 if (!strcasecmp(buf, node)) {
182                         if (nexthop != NULL) {
183                                 strcpy(nexthop, "");
184                         }
185                         retval = 0;
186                 }
187         }
188
189         phree(ignetcfg);
190         if (retval == 0) {
191                 return(retval);         /* yup, it's a direct neighbor */
192         }
193
194         /*      
195          * If we get to this point we have to see if we know the next hop
196          */
197         if (the_netmap != NULL) {
198                 for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
199                         if (!strcasecmp(nmptr->nodename, node)) {
200                                 if (nexthop != NULL) {
201                                         strcpy(nexthop, nmptr->nexthop);
202                                 }
203                                 return(0);
204                         }
205                 }
206         }
207
208         /*
209          * If we get to this point, the supplied node name is bogus.
210          */
211         lprintf(5, "Invalid node name <%s>\n", node);
212         return(-1);
213 }
214
215
216
217
218
219 void cmd_gnet(char *argbuf) {
220         char filename[SIZ];
221         char buf[SIZ];
222         FILE *fp;
223
224         if (CtdlAccessCheck(ac_room_aide)) return;
225         assoc_file_name(filename, &CC->quickroom, "netconfigs");
226         cprintf("%d Network settings for room #%ld <%s>\n",
227                 LISTING_FOLLOWS,
228                 CC->quickroom.QRnumber, CC->quickroom.QRname);
229
230         fp = fopen(filename, "r");
231         if (fp != NULL) {
232                 while (fgets(buf, sizeof buf, fp) != NULL) {
233                         buf[strlen(buf)-1] = 0;
234                         cprintf("%s\n", buf);
235                 }
236                 fclose(fp);
237         }
238
239         cprintf("000\n");
240 }
241
242
243 void cmd_snet(char *argbuf) {
244         char tempfilename[SIZ];
245         char filename[SIZ];
246         char buf[SIZ];
247         FILE *fp;
248
249         if (CtdlAccessCheck(ac_room_aide)) return;
250         safestrncpy(tempfilename, tmpnam(NULL), sizeof tempfilename);
251         assoc_file_name(filename, &CC->quickroom, "netconfigs");
252
253         fp = fopen(tempfilename, "w");
254         if (fp == NULL) {
255                 cprintf("%d Cannot open %s: %s\n",
256                         ERROR+INTERNAL_ERROR,
257                         tempfilename,
258                         strerror(errno));
259         }
260
261         cprintf("%d %s\n", SEND_LISTING, tempfilename);
262         while (client_gets(buf), strcmp(buf, "000")) {
263                 fprintf(fp, "%s\n", buf);
264         }
265         fclose(fp);
266
267         /* Now copy the temp file to its permanent location
268          * (We use /bin/mv instead of link() because they may be on
269          * different filesystems)
270          */
271         unlink(filename);
272         snprintf(buf, sizeof buf, "/bin/mv %s %s", tempfilename, filename);
273         system(buf);
274 }
275
276
277
278 /*
279  * Spools out one message from the list.
280  */
281 void network_spool_msg(long msgnum, void *userdata) {
282         struct SpoolControl *sc;
283         struct namelist *nptr;
284         int err;
285         int i;
286         char *instr = NULL;
287         char *newpath = NULL;
288         size_t instr_len = SIZ;
289         struct CtdlMessage *msg;
290         struct CtdlMessage *imsg;
291         struct ser_ret sermsg;
292         FILE *fp;
293         char filename[SIZ];
294         char buf[SIZ];
295         int bang = 0;
296         int send = 1;
297         int delete_after_send = 0;      /* Set to 1 to delete after spooling */
298
299         sc = (struct SpoolControl *)userdata;
300
301         /*
302          * Process mailing list recipients
303          */
304         if (sc->listrecps != NULL) {
305         
306                 /* First, copy it to the spoolout room */
307                 err = CtdlSaveMsgPointerInRoom(SMTP_SPOOLOUT_ROOM, msgnum, 0);
308                 if (err != 0) return;
309
310                 /* 
311                  * Figure out how big a buffer we need to allocate
312                  */
313                 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
314                         instr_len = instr_len + strlen(nptr->name);
315                 }
316         
317                 /*
318                  * allocate...
319                  */
320                 lprintf(9, "Generating delivery instructions\n");
321                 instr = mallok(instr_len);
322                 if (instr == NULL) {
323                         lprintf(1, "Cannot allocate %d bytes for instr...\n",
324                                 instr_len);
325                         abort();
326                 }
327                 sprintf(instr,
328                         "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n"
329                         "bounceto|postmaster@%s\n" ,
330                         SPOOLMIME, msgnum, time(NULL), config.c_fqdn );
331         
332                 /* Generate delivery instructions for each recipient */
333                 for (nptr = sc->listrecps; nptr != NULL; nptr = nptr->next) {
334                         sprintf(&instr[strlen(instr)], "remote|%s|0||\n",
335                                 nptr->name);
336                 }
337         
338                 /*
339                  * Generate a message from the instructions
340                  */
341                 imsg = mallok(sizeof(struct CtdlMessage));
342                 memset(imsg, 0, sizeof(struct CtdlMessage));
343                 imsg->cm_magic = CTDLMESSAGE_MAGIC;
344                 imsg->cm_anon_type = MES_NORMAL;
345                 imsg->cm_format_type = FMT_RFC822;
346                 imsg->cm_fields['A'] = strdoop("Citadel");
347                 imsg->cm_fields['M'] = instr;
348         
349                 /* Save delivery instructions in spoolout room */
350                 CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL);
351                 CtdlFreeMessage(imsg);
352         }
353         
354         /*
355          * Process IGnet push shares
356          */
357         if (sc->ignet_push_shares != NULL) {
358         
359                 msg = CtdlFetchMessage(msgnum);
360                 if (msg != NULL) {
361
362                         /* Prepend our node name to the Path field whenever
363                          * sending a message to another IGnet node
364                          */
365                         if (msg->cm_fields['P'] == NULL) {
366                                 msg->cm_fields['P'] = strdoop("username");
367                         }
368                         newpath = mallok(strlen(msg->cm_fields['P']) + 
369                                         strlen(config.c_nodename) + 2);
370                         sprintf(newpath, "%s!%s", config.c_nodename,
371                                         msg->cm_fields['P']);
372                         phree(msg->cm_fields['P']);
373                         msg->cm_fields['P'] = newpath;
374
375                         /*
376                          * Force the message to appear in the correct room
377                          * on the far end by setting the C field correctly
378                          */
379                         if (msg->cm_fields['C'] != NULL) {
380                                 phree(msg->cm_fields['C']);
381                         }
382                         msg->cm_fields['C'] = strdoop(CC->quickroom.QRname);
383
384                         /*
385                          * Determine if this message is set to be deleted
386                          * after sending out on the network
387                          */
388                         if (msg->cm_fields['S'] != NULL) {
389                                 if (!strcasecmp(msg->cm_fields['S'],
390                                    "CANCEL")) {
391                                         delete_after_send = 1;
392                                 }
393                         }
394
395                         /* 
396                          * Now serialize it for transmission
397                          */
398                         serialize_message(&sermsg, msg);
399                         CtdlFreeMessage(msg);
400
401                         /* Now send it to every node */
402                         for (nptr = sc->ignet_push_shares; nptr != NULL;
403                             nptr = nptr->next) {
404
405                                 send = 1;
406
407                                 /* Check for valid node name */
408                                 if (is_valid_node(NULL, nptr->name) != 0) {
409                                         lprintf(3, "Invalid node <%s>\n",
410                                                 nptr->name);
411                                         send = 0;
412                                 }
413
414                                 /* Check for split horizon */
415                                 bang = num_tokens(msg->cm_fields['P'], '!');
416                                 if (bang > 1) for (i=0; i<(bang-1); ++i) {
417                                         extract_token(buf, msg->cm_fields['P'],
418                                                 i, '!');
419                                         if (!strcasecmp(buf, nptr->name)) {
420                                                 send = 0;
421                                         }
422                                 }
423
424                                 /* Send the message */
425                                 if (send == 1) {
426                                         sprintf(filename,
427                                                 "./network/spoolout/%s",
428                                                 nptr->name);
429                                         fp = fopen(filename, "ab");
430                                         if (fp != NULL) {
431                                                 fwrite(sermsg.ser,
432                                                         sermsg.len, 1, fp);
433                                                 fclose(fp);
434                                         }
435                                 }
436                         }
437                 }
438         }
439
440         /* update lastsent */
441         sc->lastsent = msgnum;
442
443         /* Delete this message if delete-after-send is set */
444         if (delete_after_send) {
445                 CtdlDeleteMessages(CC->quickroom.QRname, msgnum, "");
446         }
447
448 }
449         
450
451
452
453 /*
454  * Batch up and send all outbound traffic from the current room
455  */
456 void network_spoolout_room(char *room_to_spool) {
457         char filename[SIZ];
458         char buf[SIZ];
459         char instr[SIZ];
460         FILE *fp;
461         struct SpoolControl sc;
462         /* struct namelist *digestrecps = NULL; */
463         struct namelist *nptr;
464
465         lprintf(7, "Spooling <%s>\n", room_to_spool);
466         if (getroom(&CC->quickroom, room_to_spool) != 0) {
467                 lprintf(1, "ERROR: cannot load <%s>\n", room_to_spool);
468                 return;
469         }
470
471         memset(&sc, 0, sizeof(struct SpoolControl));
472         assoc_file_name(filename, &CC->quickroom, "netconfigs");
473
474         fp = fopen(filename, "r");
475         if (fp == NULL) {
476                 lprintf(7, "Outbound batch processing skipped for <%s>\n",
477                         CC->quickroom.QRname);
478                 return;
479         }
480
481         lprintf(5, "Outbound batch processing started for <%s>\n",
482                 CC->quickroom.QRname);
483
484         while (fgets(buf, sizeof buf, fp) != NULL) {
485                 buf[strlen(buf)-1] = 0;
486
487                 extract(instr, buf, 0);
488                 if (!strcasecmp(instr, "lastsent")) {
489                         sc.lastsent = extract_long(buf, 1);
490                 }
491                 else if (!strcasecmp(instr, "listrecp")) {
492                         nptr = (struct namelist *)
493                                 mallok(sizeof(struct namelist));
494                         nptr->next = sc.listrecps;
495                         extract(nptr->name, buf, 1);
496                         sc.listrecps = nptr;
497                 }
498                 else if (!strcasecmp(instr, "ignet_push_share")) {
499                         nptr = (struct namelist *)
500                                 mallok(sizeof(struct namelist));
501                         nptr->next = sc.ignet_push_shares;
502                         extract(nptr->name, buf, 1);
503                         sc.ignet_push_shares = nptr;
504                 }
505
506
507         }
508         fclose(fp);
509
510
511         /* Do something useful */
512         CtdlForEachMessage(MSGS_GT, sc.lastsent, (-63), NULL, NULL,
513                 network_spool_msg, &sc);
514
515
516         /* Now rewrite the config file */
517         fp = fopen(filename, "w");
518         if (fp == NULL) {
519                 lprintf(1, "ERROR: cannot open %s: %s\n",
520                         filename, strerror(errno));
521         }
522         else {
523                 fprintf(fp, "lastsent|%ld\n", sc.lastsent);
524
525                 /* Write out the listrecps while freeing from memory at the
526                  * same time.  Am I clever or what?  :)
527                  */
528                 while (sc.listrecps != NULL) {
529                         fprintf(fp, "listrecp|%s\n", sc.listrecps->name);
530                         nptr = sc.listrecps->next;
531                         phree(sc.listrecps);
532                         sc.listrecps = nptr;
533                 }
534                 while (sc.ignet_push_shares != NULL) {
535                         fprintf(fp, "ignet_push_share|%s\n",
536                                 sc.ignet_push_shares->name);
537                         nptr = sc.ignet_push_shares->next;
538                         phree(sc.ignet_push_shares);
539                         sc.ignet_push_shares = nptr;
540                 }
541
542                 fclose(fp);
543         }
544
545         lprintf(5, "Outbound batch processing finished for <%s>\n",
546                 CC->quickroom.QRname);
547 }
548
549
550 /*
551  * Batch up and send all outbound traffic from the current room
552  */
553 void network_queue_room(struct quickroom *qrbuf, void *data) {
554         struct RoomProcList *ptr;
555
556         ptr = (struct RoomProcList *) mallok(sizeof (struct RoomProcList));
557         if (ptr == NULL) return;
558
559         safestrncpy(ptr->name, qrbuf->QRname, sizeof ptr->name);
560         ptr->next = rplist;
561         rplist = ptr;
562 }
563
564
565 /*
566  * Learn topology from path fields
567  */
568 void network_learn_topology(char *node, char *path) {
569         char nexthop[SIZ];
570         struct NetMap *nmptr;
571
572         strcpy(nexthop, "");
573
574         if (num_tokens(path, '!') < 3) return;
575         for (nmptr = the_netmap; nmptr != NULL; nmptr = nmptr->next) {
576                 if (!strcasecmp(nmptr->nodename, node)) {
577                         extract_token(nmptr->nexthop, path, 0, '!');
578                         nmptr->lastcontact = time(NULL);
579                         return;
580                 }
581         }
582
583         /* If we got here then it's not in the map, so add it. */
584         nmptr = (struct NetMap *) mallok(sizeof (struct NetMap));
585         strcpy(nmptr->nodename, node);
586         nmptr->lastcontact = time(NULL);
587         extract_token(nmptr->nexthop, path, 0, '!');
588         nmptr->next = the_netmap;
589         the_netmap = nmptr;
590 }
591
592
593
594 /*
595  * Process a buffer containing a single message from a single file
596  * from the inbound queue 
597  */
598 void network_process_buffer(char *buffer, long size) {
599         struct CtdlMessage *msg;
600         long pos;
601         int field;
602         int a;
603         int e = MES_LOCAL;
604         struct usersupp tempUS;
605         char recp[SIZ];
606         char target_room[ROOMNAMELEN];
607
608         /* Set default target room to trash */
609         strcpy(target_room, TWITROOM);
610
611         /* Load the message into memory */
612         msg = (struct CtdlMessage *) mallok(sizeof(struct CtdlMessage));
613         memset(msg, 0, sizeof(struct CtdlMessage));
614         msg->cm_magic = CTDLMESSAGE_MAGIC;
615         msg->cm_anon_type = buffer[1];
616         msg->cm_format_type = buffer[2];
617
618         for (pos = 3; pos < size; ++pos) {
619                 field = buffer[pos];
620                 msg->cm_fields[field] = strdoop(&buffer[pos+1]);
621                 pos = pos + strlen(&buffer[(int)pos]);
622         }
623
624         /* Check for message routing */
625         if (msg->cm_fields['D'] != NULL) {
626                 if (strcasecmp(msg->cm_fields['D'], config.c_nodename)) {
627
628                         /* FIXME route the message, stupid */
629
630                 }
631         }
632
633         /* FIXME check to see if we already have this message */
634
635         /* Learn network topology from the path */
636         if ((msg->cm_fields['N'] != NULL) && (msg->cm_fields['P'] != NULL)) {
637                 network_learn_topology(msg->cm_fields['N'], 
638                                         msg->cm_fields['P']);
639         }
640
641         /* Does it have a recipient?  If so, validate it... */
642         if (msg->cm_fields['R'] != NULL) {
643
644                 safestrncpy(recp, msg->cm_fields['R'], sizeof(recp));
645
646                 e = alias(recp);        /* alias and mail type */
647                 if ((recp[0] == 0) || (e == MES_ERROR)) {
648
649                         /* FIXME bounce the msg */
650
651                 }
652                 else if (e == MES_LOCAL) {
653                         a = getuser(&tempUS, recp);
654                         if (a != 0) {
655                                 /* FIXME bounce the msg */
656                         }
657                         else {
658                                 MailboxName(target_room, &tempUS, MAILROOM);
659                         }
660                 }
661         }
662
663         else if (msg->cm_fields['C'] != NULL) {
664                 safestrncpy(target_room,
665                         msg->cm_fields['C'],
666                         sizeof target_room);
667         }
668
669         else if (msg->cm_fields['O'] != NULL) {
670                 safestrncpy(target_room,
671                         msg->cm_fields['O'],
672                         sizeof target_room);
673         }
674
675         /* save the message into a room */
676         msg->cm_flags = CM_SKIP_HOOKS;
677         CtdlSaveMsg(msg, "", target_room, 0);
678         CtdlFreeMessage(msg);
679 }
680
681
682 /*
683  * Process a single message from a single file from the inbound queue 
684  */
685 void network_process_message(FILE *fp, long msgstart, long msgend) {
686         long hold_pos;
687         long size;
688         char *buffer;
689
690         hold_pos = ftell(fp);
691         size = msgend - msgstart + 1;
692         buffer = mallok(size);
693         if (buffer != NULL) {
694                 fseek(fp, msgstart, SEEK_SET);
695                 fread(buffer, size, 1, fp);
696                 network_process_buffer(buffer, size);
697                 phree(buffer);
698         }
699
700         fseek(fp, hold_pos, SEEK_SET);
701 }
702
703
704 /*
705  * Process a single file from the inbound queue 
706  */
707 void network_process_file(char *filename) {
708         FILE *fp;
709         long msgstart = (-1L);
710         long msgend = (-1L);
711         long msgcur = 0L;
712         int ch;
713
714         lprintf(7, "network: processing <%s>\n", filename);
715
716         fp = fopen(filename, "rb");
717         if (fp == NULL) {
718                 lprintf(5, "Error opening %s: %s\n",
719                         filename, strerror(errno));
720                 return;
721         }
722
723         /* Look for messages in the data stream and break them out */
724         while (ch = getc(fp), ch >= 0) {
725         
726                 if (ch == 255) {
727                         if (msgstart >= 0L) {
728                                 msgend = msgcur - 1;
729                                 network_process_message(fp, msgstart, msgend);
730                         }
731                         msgstart = msgcur;
732                 }
733
734                 ++msgcur;
735         }
736
737         msgend = msgcur - 1;
738         if (msgstart >= 0L) {
739                 network_process_message(fp, msgstart, msgend);
740         }
741
742         fclose(fp);
743         unlink(filename);
744 }
745
746
747 /*
748  * Process anything in the inbound queue
749  */
750 void network_do_spoolin(void) {
751         DIR *dp;
752         struct dirent *d;
753         char filename[SIZ];
754
755         dp = opendir("./network/spoolin");
756         if (dp == NULL) return;
757
758         while (d = readdir(dp), d != NULL) {
759                 sprintf(filename, "./network/spoolin/%s", d->d_name);
760                 network_process_file(filename);
761         }
762
763
764         closedir(dp);
765 }
766
767
768 /*
769  * network_do_queue()
770  * 
771  * Run through the rooms doing various types of network stuff.
772  */
773 void network_do_queue(void) {
774         static int doing_queue = 0;
775         static time_t last_run = 0L;
776         struct RoomProcList *ptr;
777
778         /*
779          * Run no more frequently than once every n seconds
780          */
781         if ( (time(NULL) - last_run) < NETWORK_QUEUE_FREQUENCY ) return;
782
783         /*
784          * This is a simple concurrency check to make sure only one queue run
785          * is done at a time.  We could do this with a mutex, but since we
786          * don't really require extremely fine granularity here, we'll do it
787          * with a static variable instead.
788          */
789         if (doing_queue) return;
790         doing_queue = 1;
791         last_run = time(NULL);
792
793         read_network_map();
794
795         /* 
796          * Go ahead and run the queue
797          */
798         lprintf(7, "network: loading outbound queue\n");
799         ForEachRoom(network_queue_room, NULL);
800
801         lprintf(7, "network: running outbound queue\n");
802         while (rplist != NULL) {
803                 network_spoolout_room(rplist->name);
804                 ptr = rplist;
805                 rplist = rplist->next;
806                 phree(ptr);
807         }
808
809         lprintf(7, "network: processing inbound queue\n");
810         network_do_spoolin();
811
812         write_network_map();
813
814         lprintf(7, "network: queue run completed\n");
815         doing_queue = 0;
816 }
817
818
819 /*
820  * Module entry point
821  */
822 char *Dynamic_Module_Init(void)
823 {
824         CtdlRegisterProtoHook(cmd_gnet, "GNET", "Get network config");
825         CtdlRegisterProtoHook(cmd_snet, "SNET", "Get network config");
826         CtdlRegisterSessionHook(network_do_queue, EVT_TIMER);
827         return "$Id$";
828 }