Altered the translation of incoming Internet e-mail addresses to Citadel
[citadel.git] / citadel / citmail.c
1 /*
2  * citmail.c v4.0
3  *
4  * This program may be used as a local mail delivery agent, which will allow
5  * all Citadel users to receive Internet e-mail.  To enable this functionality,
6  * you must tell sendmail, smail, or whatever mailer you are using, that this
7  * program is your local mail delivery agent.  This program is a direct
8  * replacement for lmail, deliver, or whatever.
9  *
10  * Usage:
11  *
12  * citmail <recipient>       - Deliver a message
13  * citmail -t <recipient>    - Address test mode (will not deliver)
14  * citmail -i                - Run as an SMTP daemon (typically from inetd)
15  *
16  */
17
18 #include "sysdep.h"
19 #include <stdlib.h>
20 #include <unistd.h>
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <ctype.h>
24 #include <string.h>
25 #include <time.h>
26 #include <pwd.h>
27 #include <errno.h>
28 #include <syslog.h>
29 #include "citadel.h"
30
31 #define LOCAL 0
32 #define REMOTE 1
33 #define UUCP 2
34 #define CCITADEL 3
35
36 #undef tolower
37 #define tolower(x) isupper(x) ? (x+'a'-'A') : x
38
39 char *monthdesc[] = {   "Jan","Feb","Mar","Apr","May","Jun",
40                         "Jul","Aug","Sep","Oct","Nov","Dec" };
41
42
43
44 void LoadInternetConfig();
45 void get_config();
46 int IsHostLocal();
47 struct config config;
48
49 char ALIASES[128];
50 char CIT86NET[128];
51 char SENDMAIL[128];
52 char FALLBACK[128];
53 char GW_DOMAIN[128];
54 char TABLEFILE[128];
55 char OUTGOING_FQDN[128];
56 int RUN_NETPROC = 1;
57
58 int struncmp(lstr,rstr,l)
59 char lstr[],rstr[]; {
60         int pos = 0;
61         char lc,rc;
62         while (1) {
63                 if (pos==l) return(0);
64                 lc=tolower(lstr[pos]);
65                 rc=tolower(rstr[pos]);
66                 if ((lc==0)&&(rc==0)) return(0);
67                 if (lc<rc) return(-1);
68                 if (lc>rc) return(1);
69                 pos=pos+1;
70                 }
71         }
72
73 long conv_date(sdbuf)
74 char sdbuf[]; {
75         int a,b,cpos,tend,tval;
76         long now;
77         struct tm *tmbuf;
78         char dbuf[128];
79
80         strcpy(dbuf,sdbuf);
81         time(&now);
82         tmbuf = (struct tm *)localtime(&now);
83
84         /* get rid of + or - timezone mods */
85         for (a=0; a<strlen(dbuf); ++a) 
86                 if ((dbuf[a]=='+')||(dbuf[a]=='-'))
87                         do {
88                                 strcpy(&dbuf[a],&dbuf[a+1]);
89                                 } while ((dbuf[a]!=32)&&(dbuf[a]!=0));
90
91         /* try and extract the time by looking for colons */
92         cpos = (-1);
93         for (a=strlen(dbuf); a>=0; --a)
94                 if ((dbuf[a]==':')&&(atoi(&dbuf[a-1])!=0)) cpos=a;
95         if (cpos>=0) {
96                 cpos = cpos - 2;
97                 tend = strlen(dbuf);
98                 for (a=tend; a>=cpos; --a) if (dbuf[a]==' ') tend=a;
99
100                 tmbuf->tm_hour = atoi(&dbuf[cpos]);
101                 tmbuf->tm_min = atoi(&dbuf[cpos+3]);
102                 tmbuf->tm_sec = atoi(&dbuf[cpos+6]);
103
104                 do {
105                         strcpy(&dbuf[cpos],&dbuf[cpos+1]);
106                         } while ((dbuf[cpos]!=32)&&(dbuf[cpos]!=0));
107                 }
108
109         /* next try to extract a month */
110         
111         tval = (-1);
112         for (a=0; a<strlen(dbuf); ++a)
113                 for (b=0; b<12; ++b)
114                         if (!strncmp(&dbuf[a],monthdesc[b],3)) {
115                                 tval = b;
116                                 cpos = a;
117                                 }
118         if (tval >= 0) {
119                 tmbuf->tm_mon = tval;
120                 strcpy(&dbuf[cpos],&dbuf[cpos+3]);
121                 }
122
123         /* now the year */
124
125         for (a=0; a<strlen(dbuf); ++a)
126                 if ((atoi(&dbuf[a])>=1900) && (dbuf[a]!=32)) {
127                         tmbuf->tm_year = atoi(&dbuf[a]) - 1900;
128                         strcpy(&dbuf[a],&dbuf[a+4]);
129                         }
130
131         /* whatever's left is the mday (hopefully) */
132
133         for (a=0; a<strlen(dbuf); ++a)
134                 if ((dbuf[a]!=32)&&(atoi(&dbuf[a])>=1)&&(atoi(&dbuf[a])<=31)
135                    && ( (a==0)||(dbuf[a-1]==' ') ) ) {
136                         tmbuf->tm_mday = atoi(&dbuf[a]);
137                         strcpy(&dbuf[a],&dbuf[a+2]);
138                         }
139
140         return((long)mktime(tmbuf));
141         }
142
143
144 #ifdef NO_STRERROR
145 /*
146  * replacement strerror() for systems that don't have it
147  */
148 char *strerror(e)
149 int e; {
150         static char buf[32];
151
152         sprintf(buf,"errno = %d",e);
153         return(buf);
154         }
155 #endif
156
157 int haschar(st,ch)
158 char st[];
159 int ch; {
160         int a,b;
161         b=0;
162         for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
163         return(b);
164         }
165
166 void strip_trailing_whitespace(buf)
167 char buf[]; {
168         while(isspace(buf[strlen(buf)-1]))
169                 buf[strlen(buf)-1]=0;
170         }
171
172 int islocalok(char recp[]) {
173
174         struct usersupp ust;
175         long lookfor;
176         char a_recp[128];
177         int found_closest_match = 0;
178         int a,us;
179         strcpy(a_recp,recp);
180         for (a=0; a<strlen(a_recp); ++a)
181                 if (a_recp[a]=='_') a_recp[a]=32;
182         lookfor = (-1L); if (!struncmp(recp,"cit",3)) lookfor=atol(&recp[3]);
183         us=open("/appl/citadel/usersupp",O_RDONLY);
184         if (us>=0) {
185                 while(read(us,&ust,sizeof(struct usersupp))>0) {
186                         if (lookfor == ust.eternal) {
187                                 strcpy(recp,ust.fullname);
188                                 close(us);
189                                 return(2);
190                                 }
191                         if (!strucmp(ust.fullname,a_recp)) {
192                                 strcpy(recp,ust.fullname);
193                                 close(us);
194                                 return(3);
195                                 }
196                         if (!struncmp(ust.fullname,a_recp,strlen(a_recp))) {
197                                 strcpy(recp,ust.fullname);
198                                 found_closest_match = 1;
199                                 }
200                         }
201                 close(us);
202                 }
203         if (getpwnam(recp)!=NULL) return(1);
204         if (found_closest_match) return(3);
205         return(0);
206         }
207
208 /* strip leading and trailing spaces */
209 void striplt(buf)
210 char buf[]; {
211         while ( (strlen(buf)>0) && (buf[0]==32) ) strcpy(buf,&buf[1]);
212         while (buf[strlen(buf)-1] == 32) buf[strlen(buf)-1] = 0;
213         }
214
215
216 /*
217  * Check to see if a given FQDN really maps to a Citadel network node
218  */
219 void host_alias(char host[]) {
220
221         int a;
222
223         /* What name is the local host known by? */
224         /* if (!strucmp(host, config.c_fqdn)) { */
225         if (IsHostLocal(host)) {
226                 strcpy(host, config.c_nodename);
227                 return;
228                 }
229
230         /* Other hosts in the gateway domain? */
231         for (a=0; a<strlen(host); ++a) {
232                 if ((host[a]=='.') && (!strucmp(&host[a+1], GW_DOMAIN))) {
233                         host[a] = 0;
234                         for (a=0; a<strlen(host); ++a) {
235                                 if (host[a]=='.') host[a] = 0;
236                                 }
237                         return;
238                         }
239                 }
240
241         /* Otherwise, do nothing... */
242         }
243
244
245
246 /*
247  * Split an RFC822-style address into userid, host, and full name
248  */
249 void process_rfc822_addr(rfc822,user,node,name)
250 char rfc822[];
251 char user[];
252 char node[];
253 char name[];  {
254         int a;
255
256         /* extract full name - first, it's From minus <userid> */
257         strcpy(name,rfc822);
258         for (a=0; a<strlen(name); ++a) if (name[a]=='<') {
259                 do {
260                         strcpy(&name[a],&name[a+1]);
261                         } while ( (strlen(name) > 0) && (name[a]!='>') );
262                 strcpy(&name[a],&name[a+1]);
263                 }
264
265         /* strip anything to the left of a bang */
266         while ( (strlen(name)>0) && (haschar(name,'!')>0) ) 
267                 strcpy(name,&name[1]);
268
269         /* and anything to the right of a @ or % */
270         for (a=0; a<strlen(name); ++a) {
271                 if (name[a]=='@') name[a]=0;
272                 if (name[a]=='%') name[a]=0;
273                 }
274
275         /* but if there are parentheses, that changes the rules... */
276         if ( (haschar(rfc822,'(') == 1) && (haschar(rfc822,')') == 1) ) {
277                 strcpy(name,rfc822);
278                 while ( (strlen(name) > 0) && (name[0]!='(') ) {
279                         strcpy(&name[0],&name[1]);
280                         }
281                 strcpy(&name[0],&name[1]);
282                 for (a=0; a<strlen(name); ++a)
283                          if (name[a]==')') name[a]=0;
284                 }
285
286         /* but if there are a set of quotes, that supersedes everything */
287         if (haschar(rfc822,34)==2) {
288                 strcpy(name,rfc822);
289                 while ( (strlen(name) > 0) && (name[0]!=34) ) {
290                         strcpy(&name[0],&name[1]);
291                         }
292                 strcpy(&name[0],&name[1]);
293                 for (a=0; a<strlen(name); ++a)
294                         if (name[a]==34) name[a]=0;
295                 }
296
297         /* extract user id */
298         strcpy(user,rfc822);
299
300         /* first get rid of anything in parens */
301         for (a=0; a<strlen(user); ++a) if (user[a]=='(') {
302                 do {
303                         strcpy(&user[a],&user[a+1]);
304                         } while ( (strlen(user) > 0) && (user[a]!=')') );
305                 strcpy(&user[a],&user[a+1]);
306                 }
307
308         /* if there's a set of angle brackets, strip it down to that */
309         if ( (haschar(user,'<') == 1) && (haschar(user,'>') == 1) ) {
310                 while ( (strlen(user) > 0) && (user[0]!='<') ) {
311                         strcpy(&user[0],&user[1]);
312                         }
313                 strcpy(&user[0],&user[1]);
314                 for (a=0; a<strlen(user); ++a)
315                          if (user[a]=='>') user[a]=0;
316                 }
317
318         /* strip anything to the left of a bang */
319         while ( (strlen(user)>0) && (haschar(user,'!')>0) ) 
320                 strcpy(user,&user[1]);
321
322         /* and anything to the right of a @ or % */
323         for (a=0; a<strlen(user); ++a) {
324                 if (user[a]=='@') user[a]=0;
325                 if (user[a]=='%') user[a]=0;
326                 }
327
328
329         
330         /* extract node name */
331         strcpy(node, rfc822);
332
333         /* first get rid of anything in parens */
334         for (a=0; a<strlen(node); ++a) if (node[a]=='(') {
335                 do {
336                         strcpy(&node[a],&node[a+1]);
337                         } while ( (strlen(node) > 0) && (node[a]!=')') );
338                 strcpy(&node[a],&node[a+1]);
339                 }
340
341         /* if there's a set of angle brackets, strip it down to that */
342         if ( (haschar(node,'<') == 1) && (haschar(node,'>') == 1) ) {
343                 while ( (strlen(node) > 0) && (node[0]!='<') ) {
344                         strcpy(&node[0],&node[1]);
345                         }
346                 strcpy(&node[0],&node[1]);
347                 for (a=0; a<strlen(node); ++a)
348                          if (node[a]=='>') node[a]=0;
349                 }
350
351         /* strip anything to the left of a @ */
352         while ( (strlen(node)>0) && (haschar(node,'@')>0) ) 
353                 strcpy(node,&node[1]);
354
355         /* strip anything to the left of a % */
356         while ( (strlen(node)>0) && (haschar(node,'%')>0) ) 
357                 strcpy(node,&node[1]);
358
359         /* reduce multiple system bang paths to node!user */
360         while ( (strlen(node)>0) && (haschar(node,'!')>1) ) 
361                 strcpy(node,&node[1]);
362
363         /* now get rid of the user portion of a node!user string */
364         for (a=0; a<strlen(node); ++a) if (node[a]=='!') node[a]=0;
365
366
367
368         /* strip leading and trailing spaces in all strings */
369         striplt(user);
370         striplt(node);
371         striplt(name);
372         }
373
374 /*
375  * Copy line by line, ending at EOF or a "." 
376  */
377 void loopcopy(FILE *to, FILE *from) {
378         char buf[1024];
379         char *r;
380         
381         while (1) {
382                 r = fgets(buf, sizeof(buf), from);
383                 if (r == NULL) return;
384                 strip_trailing_whitespace(buf);
385                 if (!strcmp(buf, ".")) return;
386                 fprintf(to, "%s\n", buf);
387                 }
388         }
389
390
391 /*
392  * pipe message through netproc
393  */
394 void do_citmail(char recp[], int dtype) {
395
396         long now;
397         FILE *temp;
398         int a;
399         char buf[128];
400         char from[512];
401         char userbuf[256];
402         char frombuf[256];
403         char nodebuf[256];
404         char destsys[256];
405         char subject[256];
406
407
408         if (dtype==REMOTE) {
409
410                 /* get the Citadel node name out of the path */
411                 strncpy(destsys, recp, sizeof(destsys) );
412                 for (a=0; a<strlen(destsys); ++a) {
413                         if ((destsys[a]=='!')||(destsys[a]=='.')) {
414                                 destsys[a]=0;
415                                 }
416                         }
417
418                 /* chop the system name out, so we're left with a user */
419                 while (haschar(recp,'!')) strcpy(recp,&recp[1]);
420
421                 /* now convert underscores to spaces */
422                 for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=' ';
423
424                 }
425
426         time(&now);
427         sprintf(from, "postmaster@%s", config.c_nodename);
428
429         sprintf(buf, "./network/spoolin/citmail.%d", getpid());
430         temp = fopen(buf,"w");
431
432         putc(255,temp); putc(MES_NORMAL,temp); putc(1,temp);
433         strcpy(subject,"");
434         strcpy(nodebuf, config.c_nodename);
435         do {
436                 if (fgets(buf,128,stdin) == NULL) strcpy(buf, ".");
437                 strip_trailing_whitespace(buf);
438
439                 if (!strncmp(buf,"Subject: ",9)) strcpy(subject,&buf[9]);
440                 if (!strncmp(buf,"Date: ",6)) now = conv_date(&buf[6]);
441                 if (!strncmp(buf,"From: ",6)) strcpy(from, &buf[6]);
442                 } while ( (strcmp(buf, ".")) && (strcmp(buf, "")) );
443
444         process_rfc822_addr(from, userbuf, nodebuf, frombuf);
445
446         /* now convert it to Citadel format */
447         fprintf(temp,"P%s@%s%c", userbuf, nodebuf, 0);
448         fprintf(temp,"T%ld%c", now, 0);
449         fprintf(temp,"A%s%c", userbuf, 0);
450         fprintf(temp,"OMail%c", 0);
451         fprintf(temp,"N%s%c", nodebuf, 0);
452         fprintf(temp,"H%s%c", frombuf, 0);
453         if (dtype==REMOTE) {
454                 fprintf(temp,"D%s%c", destsys, 0);
455                 }
456         fprintf(temp,"R%s%c", recp, 0);
457         if (strlen(subject)>0) {
458                 fprintf(temp,"U%s%c", subject, 0);
459                 }
460         putc('M',temp);
461         if (strcmp(buf, ".")) loopcopy(temp, stdin);
462         putc(0,temp);
463         fclose(temp);
464         }
465
466 void do_roommail(recp)  /* pipe public message through netproc */
467 char recp[]; {
468         long now;
469         FILE *temp;
470         int a;
471         char buf[128],userbuf[128],frombuf[128],nodebuf[128];
472         char subject[128], from[256];
473
474         strcpy(subject,"");
475         sprintf(from, "postmaster@%s", config.c_nodename);
476         strcpy(recp,&recp[5]);
477         for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=32;
478         time(&now);
479
480
481
482         strcpy(frombuf,"Internet Mail Gateway");
483         strcpy(nodebuf, config.c_nodename);
484         do {
485                 if (fgets(buf,128,stdin) == NULL) strcpy(buf, ".");
486                 strip_trailing_whitespace(buf);
487
488                 if (!strncmp(buf,"Subject: ",9)) strcpy(subject,&buf[9]);
489                 if (!strncmp(buf,"Date: ",6)) now = conv_date(&buf[6]);
490                 if (!strncmp(buf,"From: ",6)) strcpy(from, &buf[6]);
491                 } while ( (strcmp(buf, ".")) && (strcmp(buf, "")) );
492
493         process_rfc822_addr(from, userbuf, nodebuf, frombuf);
494
495         sprintf(buf,"./network/spoolin/citmail.%d",getpid());
496         temp = fopen(buf,"ab");
497         putc(255,temp); putc(MES_NORMAL,temp); putc(1,temp);
498         fprintf(temp,"P%s@%s",userbuf,nodebuf); putc(0,temp);
499         fprintf(temp,"T%ld",now); putc(0,temp);
500         fprintf(temp,"A%s",userbuf); putc(0,temp);
501         fprintf(temp,"O%s",recp); putc(0,temp);
502         fprintf(temp,"N%s",nodebuf); putc(0,temp);
503         fprintf(temp,"H%s",frombuf); putc(0,temp);
504         if (strlen(subject)>0) {
505                 fprintf(temp,"U%s",subject); putc(0,temp);
506                 }
507
508         /* FIX we have to figure out how to handle metoo list loops */
509         if (IsHostLocal(nodebuf)) {
510                 fprintf(temp, "C%s%c", "spoo", 0);
511                 }
512
513         putc('M',temp);
514         if (strcmp(buf, ".")) loopcopy(temp, stdin);
515         putc(0,temp);
516         fclose(temp);
517         }
518
519 void do_uudecode(target)
520 char *target;  {
521         static char buf[1024];
522         FILE *fp;
523         
524         sprintf(buf,"cd %s; uudecode",target);
525
526         fp=popen(buf,"w");
527         if (fp==NULL) return;
528         while (fgets(buf,1024,stdin)!=NULL) {
529                 fprintf(fp,"%s",buf);
530                 }
531         pclose(fp);
532
533         }
534
535 void do_fallback(recp)
536 char recp[]; {
537         static char buf[1024];
538         FILE *fp;
539         
540         sprintf(buf, FALLBACK, recp);
541         fp=popen(buf,"w");
542         if (fp==NULL) fp = popen("cat >/dev/null", "w");
543         loopcopy(fp, stdin);
544         pclose(fp);
545         }
546
547 int alias(name)
548 char *name; {
549         FILE *fp;
550         int a;
551         char abuf[256];
552         
553         fp=fopen(ALIASES,"r");
554         if (fp==NULL) {
555                 syslog(LOG_ERR,"cannot open %s: %s",ALIASES,strerror(errno));
556                 return(2);
557                 }
558
559         while (fgets(abuf,256,fp)!=NULL) {
560                 strip_trailing_whitespace(abuf);
561                 for (a=0; a<strlen(abuf); ++a) {
562                         if (abuf[a]==',') {
563                                 abuf[a]=0;
564                                 if (!strucmp(name,abuf)) {
565                                         strcpy(name,&abuf[a+1]);
566                                         }
567                                 }
568                         }
569                 }
570         fclose(fp);
571         return(0);
572         }
573
574
575 void deliver(char recp[], int is_test, int deliver_to_ignet) {
576
577         int b;
578         b=islocalok(recp);
579
580         /* various ways we can deliver mail... */
581
582         if (deliver_to_ignet) {
583                 syslog(LOG_NOTICE,"to Citadel network user %s",recp);
584                 if (is_test == 0) do_citmail(recp, REMOTE);
585                 }
586
587         else if (!strcmp(recp,"uudecode")) {
588                 syslog(LOG_NOTICE,"uudecoding to bit bucket directory");
589                 if (is_test == 0) do_uudecode(config.c_bucket_dir);
590                 }
591
592         else if (!strcmp(recp,"cit86net")) {
593                 syslog(LOG_NOTICE,"uudecoding to Cit86net spool");
594                 if (is_test == 0) {
595                         do_uudecode(CIT86NET);
596                         system("exec ./BatchTranslate86");
597                         }
598                 }
599
600         else if (!strcmp(recp,"null")) {
601                 syslog(LOG_NOTICE,"zapping nulled message");
602                 }
603
604         else if (!struncmp(recp,"room_",5)) {
605                 syslog(LOG_NOTICE,"to room %s",recp);
606                 if (is_test == 0) do_roommail(recp);
607                 }
608
609         else if (b==1) {
610                 syslog(LOG_NOTICE,"fallback mailer to user %s",recp);
611                 if (is_test == 0) do_fallback(recp);
612                 }
613
614         else {
615                 /* Otherwise, the user is local (or an unknown name was specified, in
616                  * which case we let netproc handle the bounce)
617                  */
618                 syslog(LOG_NOTICE,"to Citadel user %s",recp);
619                 if (is_test == 0) do_citmail(recp, LOCAL);
620                 }
621
622         }
623
624
625
626 void main(argc,argv)
627 int argc;
628 char *argv[]; {
629         int is_test = 0;
630         int deliver_to_ignet = 0;
631         int smtp = 0;
632         static char recp[1024], buf[1024];
633         static char user[1024], node[1024], name[1024];
634         int a;
635
636         openlog("citmail", LOG_PID, LOG_USER);
637         get_config();
638         LoadInternetConfig();
639
640         if (!strcmp(argv[1],"-t")) {
641                 is_test = 1;
642                 syslog(LOG_NOTICE,"test mode - will not deliver");
643                 }
644         if (!strcmp(argv[1], "-i")) {
645                 smtp = 1;
646                 syslog(LOG_NOTICE,"started as an SMTP daemon");
647                 }
648
649
650         if (smtp) {
651                 strcpy(recp, "");
652                 }
653         else if (is_test == 0) {
654                 strcpy(recp,argv[1]);
655                 }
656         else {
657                 strcpy(recp,argv[2]);
658                 }
659
660
661         if (smtp) {
662                 /*** SMTP delivery mode ***/
663
664                 printf("200 Citadel/UX SMTP gateway ready.\n");
665         
666                 do {
667                         fflush(stdout);
668                         fflush(stderr);
669                         fgets(buf, 1024, stdin);
670                         while ( (strlen(buf)>0) && (buf[strlen(buf)-1]>0) && (buf[strlen(buf)-1]<32) ) {
671                                 buf[strlen(buf)-1] = 0;
672                                 }
673
674                         /* null-pad to allow some lazy compares */
675                         buf[strlen(buf)+1] = 0;
676                         buf[strlen(buf)+2] = 0;
677                         buf[strlen(buf)+3] = 0;
678         
679                         if (!struncmp(buf, "QUIT", 4)) {
680                                 printf("221 Later, dude.\n");
681                                 }
682                         else if (!struncmp(buf, "HELP", 4)) {
683                                 printf("214 You think _you_ need help?\n");
684                                 }
685                         else if (!struncmp(buf, "HELO", 4)) {
686                                 printf("250 Howdy ho, Mr. Hankey!\n");
687                                 }
688                         else if (!struncmp(buf, "MAIL", 4)) {
689                                 printf("250 Sure, whatever...\n");
690                                 }
691
692
693                         else if (!struncmp(buf, "RCPT To: ", 9)) {
694                                 if (strlen(recp) > 0) {
695                                         printf("571 Multiple recipients not supported.\n");
696                                         }
697                                 else {
698                                         strcpy(recp, &buf[9]);
699                                         if (haschar(recp, ',')) {
700                                                 printf("571 Multiple recipients not supported.\n");
701                                                 strcpy(recp, "");
702                                                 }
703                                         else {
704                                                 syslog(LOG_NOTICE,"recp: %s",recp);
705                                                 for (a=0; a<2; ++a) {
706                                                         alias(recp);
707                                                         }
708
709                                                 /* did we alias it back to a remote address? */
710                                                 if (    (haschar(recp,'%'))
711                                                 ||      (haschar(recp,'@'))
712                                                 ||      (haschar(recp,'!')) ) {
713         
714                                                         process_rfc822_addr(recp, user, node, name);
715                                                         host_alias(node);
716                 
717                                                         /* If there are dots, it's an Internet host, so feed it
718                                                         * back to an external mail transport agent such as sendmail.
719                                                         */
720                                                         if (haschar(node, '.')) {
721                                                                 printf("571 Away with thee, spammer!\n");
722                                                                 strcpy(recp, "");
723                                                                 }
724                 
725                                                         /* Otherwise, we're dealing with Citadel mail. */
726                                                         else {
727                                                                 sprintf(recp, "%s!%s", node, user);
728                                                                 deliver_to_ignet = 1;
729                                                                 printf("250 IGnet recipient.\n");
730                                                                 }
731                                                         }
732                                                 else {
733                                                         printf("250 Local recipient.\n");
734                                                         }
735         
736                                                 }
737         
738                                         }
739                                 }
740
741
742
743                         else if (!struncmp(buf, "RCPT", 4)) {
744                                 printf("501 Only 'To:' commands are supported.\n");
745                                 }
746                         else if (!struncmp(buf, "DATA", 4)) {
747                                 if (strlen(recp) > 0) {
748                                         printf("354 Sock it to me, baby...\n");
749                                         fflush(stdout);
750                                         deliver(recp, is_test, deliver_to_ignet);
751                                         printf("250 Cool beans!\n");
752                                         }
753                                 else {
754                                         printf("503 No recipient has been specified.\n");
755                                         }
756                                 }
757                         else {
758                                 printf("500 Huh?\n");
759                                 }
760         
761                         } while (struncmp(buf,"QUIT",4));
762                 }
763
764         else {
765                 /*** Non-SMTP delivery mode ***/
766                 syslog(LOG_NOTICE,"recp: %s",recp);
767                 for (a=0; a<2; ++a) {
768                         alias(recp);
769                         }
770         
771                 /* did we alias it back to a remote address? */
772                 if (    (haschar(recp,'%'))
773                 ||      (haschar(recp,'@'))
774                 ||      (haschar(recp,'!')) ) {
775         
776                         process_rfc822_addr(recp, user, node, name);
777                         host_alias(node);
778                 
779                         /* If there are dots, it's an Internet host, so feed it
780                         * back to an external mail transport agent such as sendmail.
781                         */
782                         if (haschar(node, '.')) {
783                                 sprintf(buf, SENDMAIL, recp);
784                                 system(buf);
785                                 exit(0);
786                                 }
787         
788                         /* Otherwise, we're dealing with Citadel mail. */
789                         else {
790                                 sprintf(recp, "%s!%s", node, user);
791                                 deliver_to_ignet = 1;
792                                 }
793         
794                         }
795         
796                 deliver(recp, is_test, deliver_to_ignet);
797                 }
798         
799         closelog();
800         if (RUN_NETPROC) execlp("./netproc","netproc",NULL);
801         exit(0);
802         }
803