Tue Aug 18 00:42:33 EDT 1998 Nathan Bryant <bryant@cs.usm.maine.edu>
[citadel.git] / citadel / mailinglist.c
1 /*
2  * mailinglist.c
3  *
4  * This program is designed to be a filter which allows two-way interaction
5  * between a traditional e-mail mailing list and a Citadel room.
6  *
7  * It only handles the outbound side.  The inbound side is handled by the
8  * Citadel e-mail gateway.  You should subscribe rooms to lists using the
9  * "room_roomname@node.dom" type address.
10  * 
11  * Since some listprocs only accept postings from subscribed addresses, the
12  * messages which this program converts will all appear to be from the room
13  * address; however, the full name of the sender is set to the Citadel user
14  * name of the real message author.
15  * 
16  * To prevent loops, this program only sends messages originating on the local
17  * system.  Therefore it is not possible to carry a two-way mailing list room
18  * on a Citadel network.
19  */
20
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <stdio.h>
24 #include <syslog.h>
25 #include <string.h>
26 #include "citadel.h"
27
28 void LoadInternetConfig(void);
29 void get_config(void);
30 struct config config;
31
32 char ALIASES[128];
33 char CIT86NET[128];
34 char SENDMAIL[128];
35 char FALLBACK[128];
36 char GW_DOMAIN[128];
37 char TABLEFILE[128];
38 char OUTGOING_FQDN[128];
39 int RUN_NETPROC = 1;
40
41 /* 
42  * Consult the mailinglists table to find out where we should send the
43  * mailing list postings to.
44  */
45 void xref(char *room, char *listaddr)           /* xref table */
46              
47                  {
48         FILE *fp;
49         int a;
50         char buf[128];
51         
52         strcpy(listaddr, "");
53         fp=fopen(TABLEFILE, "r");
54         while (fgets(buf,128,fp)!=NULL) {
55                 buf[strlen(buf)-1]=0;
56                 for (a=0; a<strlen(buf); ++a) {
57                         if ( (buf[a]==',') && (!strcasecmp(&buf[a+1],room)) ) {
58                                 strcpy(listaddr,buf);
59                                 listaddr[a] = 0;
60                                 }
61                         }
62                 }
63         fclose(fp);
64         return;
65         }
66
67
68 /*
69  * The main loop.  We don't need any command-line parameters to this program.
70  */
71 void main(void) {
72
73         char header[3];
74         char fields[32][1024];
75         int num_fields;
76         int ch, p, a;
77         int in_header;
78         int is_good;
79         char listaddr[512];
80         char mailcmd[256];
81         FILE *nm;
82         char tempfile[64];
83
84         get_config();
85         LoadInternetConfig();
86         sprintf(tempfile, "/tmp/mlist.%d", getpid() );
87
88         while(1) {
89
90                 /* seek to the next message */
91                 is_good = 0;
92                 do {
93                         if (feof(stdin)) {
94                                 unlink(tempfile);
95                                 exit(0);
96                                 }
97                         } while (getc(stdin) != 255);
98
99                 header[0] = 255;
100                 header[1] = getc(stdin);
101                 header[2] = getc(stdin);
102                 in_header = 1;
103                 num_fields = 0;
104
105                 do {
106                         fields[num_fields][0] = getc(stdin);
107                         if (fields[num_fields][0] != 'M') {
108                                 p = 1;
109                                 do {
110                                         ch = getc(stdin);
111                                         fields[num_fields][p++] = ch;
112                                         } while ((ch!=0) && (!feof(stdin)));
113                                 if (fields[num_fields][0] == 'N') {
114                                         if (!strcmp(&fields[num_fields][1],
115                                                 NODENAME)) {
116                                                         is_good = 1;
117                                                         }
118                                         }
119                                 if (fields[num_fields][0] == 'O') {
120                                         xref(&fields[num_fields][1], listaddr);
121                                         }
122                                 if (fields[num_fields][0]!='R') ++num_fields;
123                                 }
124                         else {
125                                 /* flush the message out to the next program */
126
127                                 nm =  fopen(tempfile, "wb");
128                                 fprintf(nm, "%c%c%c",
129                                         header[0], header[1], header[2]);
130                                 for (a=0; a<num_fields; ++a) {
131                                         fprintf(nm, "%s%c", &fields[a][0], 0);
132                                         }
133                                 fprintf(nm, "R%s%c", listaddr, 0);
134                         
135                                 putc('M', nm);
136                                 do {
137                                         ch = getc(stdin);
138                                         putc(ch, nm);
139                                         } while ((ch!=0) && (!feof(stdin)));
140                                 in_header = 0;
141                                 fclose(nm);
142                                 if (is_good) {
143                                         sprintf(mailcmd, "exec netmailer %s mlist", tempfile);
144                                         system(mailcmd);
145                                         is_good = 0;
146                                         }
147                                 }
148                         } while (in_header);
149                 }
150         }