Various changes to appimage. Removed some old docs.
[citadel.git] / citadel / citmail.c
index 2f4a12cc42058f861a4c3ede894c0ca499d14495..f1c6ae8c780b1e85708f8a378520dae8d1328151 100644 (file)
-/*
- * citmail.c v4.0
- *
- * This program may be used as a local mail delivery agent, which will allow
- * all Citadel users to receive Internet e-mail.  To enable this functionality,
- * you must tell sendmail, smail, or whatever mailer you are using, that this
- * program is your local mail delivery agent.  This program is a direct
- * replacement for lmail, deliver, or whatever.
- *
- * Usage:
- *
- * citmail <recipient>       - Deliver a message
- * citmail -t <recipient>    - Address test mode (will not deliver)
- * citmail -i                - Run as an SMTP daemon (typically from inetd)
- *
- */
+// This program attempts to act like a local MDA if you're using sendmail or
+// some other non-Citadel MTA.  It basically just contacts the Citadel LMTP
+// listener on a unix domain socket and transmits the message.  Really though,
+// if your MTA supports LMTP then you definitely should be using that instead.
+//
+// Copyright (c) 1987-2021 by the citadel.org team
+//
+// This program is open source software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License version 3.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
 
 #include "sysdep.h"
 #include <stdlib.h>
 #include <unistd.h>
-#include <stdio.h>
-#include <fcntl.h>
 #include <ctype.h>
+#include <stdio.h>
+#include <signal.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <sys/un.h>
+#include <netdb.h>
 #include <string.h>
-#include <time.h>
 #include <pwd.h>
 #include <errno.h>
-#include <syslog.h>
+#include <stdarg.h>
+#include <limits.h>
+#include <libcitadel.h>
 #include "citadel.h"
+#include "citadel_dirs.h"
 
-#define LOCAL 0
-#define REMOTE 1
-#define UUCP 2
-#define CCITADEL 3
-
-#undef tolower
-#define tolower(x) isupper(x) ? (x+'a'-'A') : x
-
-char *monthdesc[] = {  "Jan","Feb","Mar","Apr","May","Jun",
-                       "Jul","Aug","Sep","Oct","Nov","Dec" };
-
-
-
-void LoadInternetConfig();
-void get_config();
-int IsHostLocal();
-struct config config;
-
-char ALIASES[128];
-char CIT86NET[128];
-char SENDMAIL[128];
-char FALLBACK[128];
-char GW_DOMAIN[128];
-char TABLEFILE[128];
-char OUTGOING_FQDN[128];
-int RUN_NETPROC = 1;
-
-int struncmp(lstr,rstr,l)
-char lstr[],rstr[]; {
-       int pos = 0;
-       char lc,rc;
-       while (1) {
-               if (pos==l) return(0);
-               lc=tolower(lstr[pos]);
-               rc=tolower(rstr[pos]);
-               if ((lc==0)&&(rc==0)) return(0);
-               if (lc<rc) return(-1);
-               if (lc>rc) return(1);
-               pos=pos+1;
-               }
-       }
-
-long conv_date(sdbuf)
-char sdbuf[]; {
-       int a,b,cpos,tend,tval;
-       long now;
-       struct tm *tmbuf;
-       char dbuf[128];
-
-       strcpy(dbuf,sdbuf);
-       time(&now);
-       tmbuf = (struct tm *)localtime(&now);
-
-       /* get rid of + or - timezone mods */
-       for (a=0; a<strlen(dbuf); ++a) 
-               if ((dbuf[a]=='+')||(dbuf[a]=='-'))
-                       do {
-                               strcpy(&dbuf[a],&dbuf[a+1]);
-                               } while ((dbuf[a]!=32)&&(dbuf[a]!=0));
-
-       /* try and extract the time by looking for colons */
-       cpos = (-1);
-       for (a=strlen(dbuf); a>=0; --a)
-               if ((dbuf[a]==':')&&(atoi(&dbuf[a-1])!=0)) cpos=a;
-       if (cpos>=0) {
-               cpos = cpos - 2;
-               tend = strlen(dbuf);
-               for (a=tend; a>=cpos; --a) if (dbuf[a]==' ') tend=a;
-
-               tmbuf->tm_hour = atoi(&dbuf[cpos]);
-               tmbuf->tm_min = atoi(&dbuf[cpos+3]);
-               tmbuf->tm_sec = atoi(&dbuf[cpos+6]);
-
-               do {
-                       strcpy(&dbuf[cpos],&dbuf[cpos+1]);
-                       } while ((dbuf[cpos]!=32)&&(dbuf[cpos]!=0));
-               }
-
-       /* next try to extract a month */
-       
-       tval = (-1);
-       for (a=0; a<strlen(dbuf); ++a)
-               for (b=0; b<12; ++b)
-                       if (!strncmp(&dbuf[a],monthdesc[b],3)) {
-                               tval = b;
-                               cpos = a;
-                               }
-       if (tval >= 0) {
-               tmbuf->tm_mon = tval;
-               strcpy(&dbuf[cpos],&dbuf[cpos+3]);
-               }
-
-       /* now the year */
+int serv_sock;
+int debug = 0;
 
-       for (a=0; a<strlen(dbuf); ++a)
-               if ((atoi(&dbuf[a])>=1900) && (dbuf[a]!=32)) {
-                       tmbuf->tm_year = atoi(&dbuf[a]) - 1900;
-                       strcpy(&dbuf[a],&dbuf[a+4]);
-                       }
+void strip_trailing_nonprint(char *buf) {
+        while ( (!IsEmptyStr(buf)) && (!isprint(buf[strlen(buf) - 1])) ) {
+                buf[strlen(buf) - 1] = 0;
+       }
+}
 
-       /* whatever's left is the mday (hopefully) */
 
-       for (a=0; a<strlen(dbuf); ++a)
-               if ((dbuf[a]!=32)&&(atoi(&dbuf[a])>=1)&&(atoi(&dbuf[a])<=31)
-                  && ( (a==0)||(dbuf[a-1]==' ') ) ) {
-                       tmbuf->tm_mday = atoi(&dbuf[a]);
-                       strcpy(&dbuf[a],&dbuf[a+2]);
-                       }
+void timeout(int signum) {
+       exit(signum);
+}
 
-       return((long)mktime(tmbuf));
-       }
 
+int uds_connectsock(char *sockpath) {
+       int s;
+       struct sockaddr_un addr;
 
-#ifdef NO_STRERROR
-/*
- * replacement strerror() for systems that don't have it
- */
-char *strerror(e)
-int e; {
-       static char buf[32];
+       memset(&addr, 0, sizeof(addr));
+       addr.sun_family = AF_UNIX;
+       strcpy(addr.sun_path, sockpath);
 
-       sprintf(buf,"errno = %d",e);
-       return(buf);
-       }
-#endif
-
-int haschar(st,ch)
-char st[];
-int ch; {
-       int a,b;
-       b=0;
-       for (a=0; a<strlen(st); ++a) if (st[a]==ch) ++b;
-       return(b);
+       s = socket(AF_UNIX, SOCK_STREAM, 0);
+       if (s < 0) {
+               fprintf(stderr, "citmail: Can't create socket: %s\n",
+                       strerror(errno));
+               exit(3);
        }
 
-void strip_trailing_whitespace(buf)
-char buf[]; {
-       while(isspace(buf[strlen(buf)-1]))
-               buf[strlen(buf)-1]=0;
+       if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
+               fprintf(stderr, "citmail: can't connect: %s\n",
+                       strerror(errno));
+               close(s);
+               exit(3);
        }
 
-/* strip leading and trailing spaces */
-void striplt(buf)
-char buf[]; {
-       while ( (strlen(buf)>0) && (buf[0]==32) ) strcpy(buf,&buf[1]);
-       while (buf[strlen(buf)-1] == 32) buf[strlen(buf)-1] = 0;
-       }
+       return s;
+}
 
 
 /*
- * Check to see if a given FQDN really maps to a Citadel network node
+ * input binary data from socket
  */
-void host_alias(char host[]) {
-
-       int a;
-
-       /* What name is the local host known by? */
-       /* if (!strucmp(host, config.c_fqdn)) { */
-       if (IsHostLocal(host)) {
-               strcpy(host, config.c_nodename);
-               return;
-               }
-
-       /* Other hosts in the gateway domain? */
-       for (a=0; a<strlen(host); ++a) {
-               if ((host[a]=='.') && (!strucmp(&host[a+1], GW_DOMAIN))) {
-                       host[a] = 0;
-                       for (a=0; a<strlen(host); ++a) {
-                               if (host[a]=='.') host[a] = 0;
-                               }
+void serv_read(char *buf, int bytes)
+{
+       int len, rlen;
+
+       len = 0;
+       while (len < bytes) {
+               rlen = read(serv_sock, &buf[len], bytes - len);
+               if (rlen < 1) {
                        return;
-                       }
                }
-
-       /* Otherwise, do nothing... */
+               len = len + rlen;
        }
-
+}
 
 
 /*
- * Split an RFC822-style address into userid, host, and full name
+ * send binary to server
  */
-void process_rfc822_addr(rfc822,user,node,name)
-char rfc822[];
-char user[];
-char node[];
-char name[];  {
-       int a;
-
-       /* extract full name - first, it's From minus <userid> */
-       strcpy(name,rfc822);
-       for (a=0; a<strlen(name); ++a) if (name[a]=='<') {
-               do {
-                       strcpy(&name[a],&name[a+1]);
-                       } while ( (strlen(name) > 0) && (name[a]!='>') );
-               strcpy(&name[a],&name[a+1]);
-               }
-
-       /* strip anything to the left of a bang */
-       while ( (strlen(name)>0) && (haschar(name,'!')>0) ) 
-               strcpy(name,&name[1]);
-
-       /* and anything to the right of a @ or % */
-       for (a=0; a<strlen(name); ++a) {
-               if (name[a]=='@') name[a]=0;
-               if (name[a]=='%') name[a]=0;
-               }
-
-       /* but if there are parentheses, that changes the rules... */
-       if ( (haschar(rfc822,'(') == 1) && (haschar(rfc822,')') == 1) ) {
-               strcpy(name,rfc822);
-               while ( (strlen(name) > 0) && (name[0]!='(') ) {
-                       strcpy(&name[0],&name[1]);
-                       }
-               strcpy(&name[0],&name[1]);
-               for (a=0; a<strlen(name); ++a)
-                        if (name[a]==')') name[a]=0;
-               }
-
-       /* but if there are a set of quotes, that supersedes everything */
-       if (haschar(rfc822,34)==2) {
-               strcpy(name,rfc822);
-               while ( (strlen(name) > 0) && (name[0]!=34) ) {
-                       strcpy(&name[0],&name[1]);
-                       }
-               strcpy(&name[0],&name[1]);
-               for (a=0; a<strlen(name); ++a)
-                       if (name[a]==34) name[a]=0;
-               }
-
-       /* extract user id */
-       strcpy(user,rfc822);
-
-       /* first get rid of anything in parens */
-       for (a=0; a<strlen(user); ++a) if (user[a]=='(') {
-               do {
-                       strcpy(&user[a],&user[a+1]);
-                       } while ( (strlen(user) > 0) && (user[a]!=')') );
-               strcpy(&user[a],&user[a+1]);
-               }
-
-       /* if there's a set of angle brackets, strip it down to that */
-       if ( (haschar(user,'<') == 1) && (haschar(user,'>') == 1) ) {
-               while ( (strlen(user) > 0) && (user[0]!='<') ) {
-                       strcpy(&user[0],&user[1]);
-                       }
-               strcpy(&user[0],&user[1]);
-               for (a=0; a<strlen(user); ++a)
-                        if (user[a]=='>') user[a]=0;
-               }
-
-       /* strip anything to the left of a bang */
-       while ( (strlen(user)>0) && (haschar(user,'!')>0) ) 
-               strcpy(user,&user[1]);
-
-       /* and anything to the right of a @ or % */
-       for (a=0; a<strlen(user); ++a) {
-               if (user[a]=='@') user[a]=0;
-               if (user[a]=='%') user[a]=0;
-               }
-
-
-       
-       /* extract node name */
-       strcpy(node, rfc822);
-
-       /* first get rid of anything in parens */
-       for (a=0; a<strlen(node); ++a) if (node[a]=='(') {
-               do {
-                       strcpy(&node[a],&node[a+1]);
-                       } while ( (strlen(node) > 0) && (node[a]!=')') );
-               strcpy(&node[a],&node[a+1]);
-               }
-
-       /* if there's a set of angle brackets, strip it down to that */
-       if ( (haschar(node,'<') == 1) && (haschar(node,'>') == 1) ) {
-               while ( (strlen(node) > 0) && (node[0]!='<') ) {
-                       strcpy(&node[0],&node[1]);
-                       }
-               strcpy(&node[0],&node[1]);
-               for (a=0; a<strlen(node); ++a)
-                        if (node[a]=='>') node[a]=0;
+void serv_write(char *buf, int nbytes)
+{
+       int bytes_written = 0;
+       int retval;
+       while (bytes_written < nbytes) {
+               retval = write(serv_sock, &buf[bytes_written],
+                              nbytes - bytes_written);
+               if (retval < 1) {
+                       return;
                }
-
-       /* strip anything to the left of a @ */
-       while ( (strlen(node)>0) && (haschar(node,'@')>0) ) 
-               strcpy(node,&node[1]);
-
-       /* strip anything to the left of a % */
-       while ( (strlen(node)>0) && (haschar(node,'%')>0) ) 
-               strcpy(node,&node[1]);
-
-       /* reduce multiple system bang paths to node!user */
-       while ( (strlen(node)>0) && (haschar(node,'!')>1) ) 
-               strcpy(node,&node[1]);
-
-       /* now get rid of the user portion of a node!user string */
-       for (a=0; a<strlen(node); ++a) if (node[a]=='!') node[a]=0;
-
+               bytes_written = bytes_written + retval;
+       }
+}
 
 
-       /* strip leading and trailing spaces in all strings */
-       striplt(user);
-       striplt(node);
-       striplt(name);
-       }
 
 /*
- * Copy line by line, ending at EOF or a "." 
+ * input string from socket - implemented in terms of serv_read()
  */
-void loopcopy(FILE *to, FILE *from) {
-       char buf[1024];
-       char *r;
-       
-       while (1) {
-               r = fgets(buf, sizeof(buf), from);
-               if (r == NULL) return;
-               strip_trailing_whitespace(buf);
-               if (!strcmp(buf, ".")) return;
-               fprintf(to, "%s\n", buf);
-               }
+void serv_gets(char *buf)
+{
+       int i;
+
+       /* Read one character at a time.
+        */
+       for (i = 0;; i++) {
+               serv_read(&buf[i], 1);
+               if (buf[i] == '\n' || i == (SIZ-1))
+                       break;
        }
 
+       /* If we got a long line, discard characters until the newline.
+        */
+       if (i == (SIZ-1))
+               while (buf[i] != '\n')
+                       serv_read(&buf[i], 1);
 
-/*
- * pipe message through netproc
- */
-void do_citmail(char recp[], int dtype) {
-
-       long now;
-       FILE *temp;
-       int a;
-       char buf[128];
-       char from[512];
-       char userbuf[256];
-       char frombuf[256];
-       char nodebuf[256];
-       char destsys[256];
-       char subject[256];
-
-
-       if (dtype==REMOTE) {
-
-               /* get the Citadel node name out of the path */
-               strncpy(destsys, recp, sizeof(destsys) );
-               for (a=0; a<strlen(destsys); ++a) {
-                       if ((destsys[a]=='!')||(destsys[a]=='.')) {
-                               destsys[a]=0;
-                               }
-                       }
+       /* Strip all trailing nonprintables (crlf)
+        */
+       buf[i] = 0;
+       strip_trailing_nonprint(buf);
+       if (debug) fprintf(stderr, "> %s\n", buf);
+}
 
-               /* chop the system name out, so we're left with a user */
-               while (haschar(recp,'!')) strcpy(recp,&recp[1]);
 
-               /* now convert underscores to spaces */
-               for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=' ';
+/*
+ * send line to server - implemented in terms of serv_write()
+ */
+void serv_puts(char *buf)
+{
+       if (debug) fprintf(stderr, "< %s\n", buf);
+       serv_write(buf, strlen(buf));
+       serv_write("\n", 1);
+}
 
-               }
 
-       time(&now);
-       sprintf(from, "postmaster@%s", config.c_nodename);
 
-       sprintf(buf, "./network/spoolin/citmail.%d", getpid());
-       temp = fopen(buf,"w");
+void cleanup(int exitcode) {
+       char buf[1024];
 
-       putc(255,temp); putc(MES_NORMAL,temp); putc(1,temp);
-       strcpy(subject,"");
-       strcpy(nodebuf, config.c_nodename);
-       do {
-               if (fgets(buf,128,stdin) == NULL) strcpy(buf, ".");
-               strip_trailing_whitespace(buf);
-
-               if (!strncmp(buf,"Subject: ",9)) strcpy(subject,&buf[9]);
-               if (!strncmp(buf,"Date: ",6)) now = conv_date(&buf[6]);
-               if (!strncmp(buf,"From: ",6)) strcpy(from, &buf[6]);
-               } while ( (strcmp(buf, ".")) && (strcmp(buf, "")) );
-
-       process_rfc822_addr(from, userbuf, nodebuf, frombuf);
-
-       /* now convert it to Citadel format */
-       fprintf(temp,"P%s@%s%c", userbuf, nodebuf, 0);
-       fprintf(temp,"E%s%c", userbuf, 0);
-       fprintf(temp,"T%ld%c", now, 0);
-       fprintf(temp,"A%s%c", frombuf, 0);
-       fprintf(temp,"OMail%c", 0);
-       fprintf(temp,"N%s%c", nodebuf, 0);
-       if (dtype==REMOTE) {
-               fprintf(temp,"D%s%c", destsys, 0);
-               }
-       fprintf(temp,"R%s%c", recp, 0);
-       if (strlen(subject)>0) {
-               fprintf(temp,"U%s%c", subject, 0);
-               }
-       putc('M',temp);
-       if (strcmp(buf, ".")) loopcopy(temp, stdin);
-       putc(0,temp);
-       fclose(temp);
+       if (exitcode != 0) {
+               fprintf(stderr, "citmail: error #%d occurred while sending mail.\n", exitcode);
+               fprintf(stderr, "Please check your Citadel configuration.\n");
        }
+       serv_puts("QUIT");
+       serv_gets(buf);
+       exit(exitcode);
+}
 
-void do_roommail(recp)  /* pipe public message through netproc */
-char recp[]; {
-       long now;
-       FILE *temp;
-       int a;
-       char buf[128],userbuf[128],frombuf[128],nodebuf[128];
-       char subject[128], from[256];
-
-       strcpy(subject,"");
-       sprintf(from, "postmaster@%s", config.c_nodename);
-       strcpy(recp,&recp[5]);
-       for (a=0; a<strlen(recp); ++a) if (recp[a]=='_') recp[a]=32;
-       time(&now);
 
 
-       sprintf(buf,"./network/spoolin/citmail.%d",getpid());
-       temp = fopen(buf,"w");
-
-       putc(255,temp); putc(MES_NORMAL,temp); putc(1,temp);
-       strcpy(frombuf,"Internet Mail Gateway");
-       strcpy(nodebuf, config.c_nodename);
-       do {
-               if (fgets(buf,128,stdin) == NULL) strcpy(buf, ".");
-               strip_trailing_whitespace(buf);
-
-               if (!strncmp(buf,"Subject: ",9)) strcpy(subject,&buf[9]);
-               if (!strncmp(buf,"Date: ",6)) now = conv_date(&buf[6]);
-               if (!strncmp(buf,"From: ",6)) strcpy(from, &buf[6]);
-               } while ( (strcmp(buf, ".")) && (strcmp(buf, "")) );
-
-       process_rfc822_addr(from, userbuf, nodebuf, frombuf);
-
-       fprintf(temp,"P%s@%s",userbuf,nodebuf); putc(0,temp);
-       fprintf(temp,"T%ld",now); putc(0,temp);
-       fprintf(temp,"A%s",frombuf); putc(0,temp);
-       fprintf(temp,"O%s",recp); putc(0,temp);
-       fprintf(temp,"N%s",nodebuf); putc(0,temp);
-       if (strlen(subject)>0) {
-               fprintf(temp,"U%s",subject); putc(0,temp);
-               }
-       putc('M',temp);
-       if (strcmp(buf, ".")) loopcopy(temp, stdin);
-       putc(0,temp);
-       fclose(temp);
-       }
-
-void do_uudecode(target)
-char *target;  {
-       static char buf[1024];
+int main(int argc, char **argv) {
+       char buf[1024];
+       char fromline[1024];
        FILE *fp;
-       
-       sprintf(buf,"cd %s; uudecode",target);
-
-       fp=popen(buf,"w");
-       if (fp==NULL) return;
-       while (fgets(buf,1024,stdin)!=NULL) {
-               fprintf(fp,"%s",buf);
+       int i;
+       struct passwd *pw;
+       int from_header = 0;
+       int in_body = 0;
+       char ctdldir[PATH_MAX]=CTDLDIR;
+       char *sp, *ep;
+       char hostname[256];
+       char **recipients = NULL;
+       int num_recipients = 0;
+       int to_or_cc = 0;
+       int read_recipients_from_headers = 0;
+       char *add_these_recipients = NULL;
+
+       for (i=1; i<argc; ++i) {
+               if (!strcmp(argv[i], "-d")) {
+                       debug = 1;
+               }
+               else if (!strcmp(argv[i], "-t")) {
+                       read_recipients_from_headers = 1;
+               }
+               else if (!strncmp(argv[i], "-h", 2)) {
+                       safestrncpy(ctdldir, &argv[i][2], sizeof ctdldir);
+               }
+               else if (argv[i][0] != '-') {
+                       ++num_recipients;
+                       recipients = realloc(recipients, (num_recipients * sizeof (char *)));
+                       recipients[num_recipients - 1] = strdup(argv[i]);
                }
-       pclose(fp);
-
        }
 
-void do_fallback(recp)
-char recp[]; {
-       static char buf[1024];
-       FILE *fp;
-       
-       sprintf(buf, FALLBACK, recp);
-       fp=popen(buf,"w");
-       if (fp==NULL) fp = popen("cat >/dev/null", "w");
-       loopcopy(fp, stdin);
-       pclose(fp);
+       if (chdir(ctdldir) != 0) {
+               fprintf(stderr, "sendcommand: %s: %s\n", ctdldir, strerror(errno));
+               exit(errno);
        }
-
-int alias(name)
-char *name; {
-       FILE *fp;
-       int a;
-       char abuf[256];
-       
-       fp=fopen(ALIASES,"r");
-       if (fp==NULL) {
-               syslog(LOG_ERR,"cannot open %s: %s",ALIASES,strerror(errno));
-               return(2);
-               }
-
-       while (fgets(abuf,256,fp)!=NULL) {
-               strip_trailing_whitespace(abuf);
-               for (a=0; a<strlen(abuf); ++a) {
-                       if (abuf[a]==',') {
-                               abuf[a]=0;
-                               if (!strucmp(name,abuf)) {
-                                       strcpy(name,&abuf[a+1]);
-                                       }
-                               }
-                       }
-               }
-       fclose(fp);
-       return(0);
+              
+       pw = getpwuid(getuid());
+
+       fp = tmpfile();
+       if (fp == NULL) return(errno);
+       serv_sock = uds_connectsock(file_lmtp_socket);
+       serv_gets(buf);
+       if (buf[0] != '2') {
+               fprintf(stderr, "%s\n", &buf[4]);
+               if (debug) fprintf(stderr, "citmail: could not connect to LMTP socket.\n");
+               cleanup(1);
        }
 
-
-void deliver(char recp[], int is_test, int deliver_to_ignet) {
-
-       /* various ways we can deliver mail... */
-
-       if (deliver_to_ignet) {
-               syslog(LOG_NOTICE,"to Citadel network user %s",recp);
-               if (is_test == 0) do_citmail(recp, REMOTE);
-               }
-
-       else if (!strcmp(recp,"uudecode")) {
-               syslog(LOG_NOTICE,"uudecoding to bit bucket directory");
-               if (is_test == 0) do_uudecode(config.c_bucket_dir);
-               }
-
-       else if (!strcmp(recp,"cit86net")) {
-               syslog(LOG_NOTICE,"uudecoding to Cit86net spool");
-               if (is_test == 0) {
-                       do_uudecode(CIT86NET);
-                       system("exec ./BatchTranslate86");
-                       }
-               }
-
-       else if (!strcmp(recp,"null")) {
-               syslog(LOG_NOTICE,"zapping nulled message");
-               }
-
-       else if (!struncmp(recp,"room_",5)) {
-               syslog(LOG_NOTICE,"to room %s",recp);
-               if (is_test == 0) do_roommail(recp);
-               }
-
-       else {
-               /* Otherwise, we treat the recipient as the name of a local
-                * Citadel user.  If the username doesn't exist, we still
-                * deliver it to Citadel -- netproc will handle the bounce.
-                */
-               syslog(LOG_NOTICE,"to Citadel user %s",recp);
-               if (is_test == 0) do_citmail(recp, LOCAL);
-               }
-
+       sp = strchr (buf, ' ');
+       if (sp == NULL) {
+               if (debug) fprintf(stderr, "citmail: could not calculate hostname.\n");
+               cleanup(2);
        }
+       sp ++;
+       ep = strchr (sp, ' ');
+       if (ep == NULL) {
+               if (debug) fprintf(stderr, "citmail: error parsing hostname\n");
+               cleanup(3);
+       }
+       else
+               *ep = '\0';
 
+       strncpy(hostname, sp, sizeof hostname);
 
-
-void main(argc,argv)
-int argc;
-char *argv[]; {
-       int is_test = 0;
-       int deliver_to_ignet = 0;
-       int smtp = 0;
-       static char recp[1024], buf[1024];
-       static char user[1024], node[1024], name[1024];
-       int a;
-
-       openlog("citmail", LOG_PID, LOG_USER);
-       get_config();
-       LoadInternetConfig();
-
-       if (!strcmp(argv[1],"-t")) {
-               is_test = 1;
-               syslog(LOG_NOTICE,"test mode - will not deliver");
-               }
-       if (!strcmp(argv[1], "-i")) {
-               smtp = 1;
-               syslog(LOG_NOTICE,"started as an SMTP daemon");
-               }
-
-
-       if (smtp) {
-               strcpy(recp, "");
-               }
-       else if (is_test == 0) {
-               strcpy(recp,argv[1]);
+       snprintf(fromline, sizeof fromline, "From: %s@%s", pw->pw_name, hostname);
+       while (fgets(buf, 1024, stdin) != NULL) {
+               if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) {
+                       in_body = 1;
+                       if (from_header == 0) {
+                               fprintf(fp, "%s%s", fromline, buf);
+                       }
                }
-       else {
-               strcpy(recp,argv[2]);
+               if (in_body == 0 && !strncasecmp(buf, "From:", 5)) {
+                       strcpy(fromline, buf);
+                       from_header = 1;
                }
 
-
-       if (smtp) {
-               /*** SMTP delivery mode ***/
-
-               printf("200 Citadel/UX SMTP gateway ready.\n");
-       
-               do {
-                       fflush(stdout);
-                       fflush(stderr);
-                       fgets(buf, 1024, stdin);
-                       while ( (strlen(buf)>0) && (buf[strlen(buf)-1]>0) && (buf[strlen(buf)-1]<32) ) {
-                               buf[strlen(buf)-1] = 0;
-                               }
-
-                       /* null-pad to allow some lazy compares */
-                       buf[strlen(buf)+1] = 0;
-                       buf[strlen(buf)+2] = 0;
-                       buf[strlen(buf)+3] = 0;
-       
-                       if (!struncmp(buf, "QUIT", 4)) {
-                               printf("221 Later, dude.\n");
-                               }
-                       else if (!struncmp(buf, "HELP", 4)) {
-                               printf("214 You think _you_ need help?\n");
+               if (read_recipients_from_headers) {
+                       add_these_recipients = NULL;
+                       if ((isspace(buf[0])) && (to_or_cc)) {
+                               add_these_recipients = buf;
+                       }
+                       else {
+                               if ((!strncasecmp(buf, "To:", 3)) || (!strncasecmp(buf, "Cc:", 3))) {
+                                       to_or_cc = 1;
                                }
-                       else if (!struncmp(buf, "HELO", 4)) {
-                               printf("250 Howdy ho, Mr. Hankey!\n");
+                               else {
+                                       to_or_cc = 0;
                                }
-                       else if (!struncmp(buf, "MAIL", 4)) {
-                               printf("250 Sure, whatever...\n");
+                               if (to_or_cc) {
+                                       add_these_recipients = &buf[3];
                                }
+                       }
 
-
-                       else if (!struncmp(buf, "RCPT To: ", 9)) {
-                               if (strlen(recp) > 0) {
-                                       printf("571 Multiple recipients not supported.\n");
-                                       }
-                               else {
-                                       strcpy(recp, &buf[9]);
-                                       if (haschar(recp, ',')) {
-                                               printf("571 Multiple recipients not supported.\n");
-                                               strcpy(recp, "");
-                                               }
-                                       else {
-                                               syslog(LOG_NOTICE,"recp: %s",recp);
-                                               for (a=0; a<2; ++a) {
-                                                       alias(recp);
-                                                       }
-
-                                               /* did we alias it back to a remote address? */
-                                               if (    (haschar(recp,'%'))
-                                               ||      (haschar(recp,'@'))
-                                               ||      (haschar(recp,'!')) ) {
-       
-                                                       process_rfc822_addr(recp, user, node, name);
-                                                       host_alias(node);
-               
-                                                       /* If there are dots, it's an Internet host, so feed it
-                                                       * back to an external mail transport agent such as sendmail.
-                                                       */
-                                                       if (haschar(node, '.')) {
-                                                               printf("571 Away with thee, spammer!\n");
-                                                               strcpy(recp, "");
-                                                               }
-               
-                                                       /* Otherwise, we're dealing with Citadel mail. */
-                                                       else {
-                                                               sprintf(recp, "%s!%s", node, user);
-                                                               deliver_to_ignet = 1;
-                                                               printf("250 IGnet recipient.\n");
-                                                               }
-                                                       }
-                                               else {
-                                                       printf("250 Local recipient.\n");
-                                                       }
-       
-                                               }
-       
+                       if (add_these_recipients) {
+                               int num_recp_on_this_line;
+                               char this_recp[256];
+
+                               num_recp_on_this_line = num_tokens(add_these_recipients, ',');
+                               for (i=0; i<num_recp_on_this_line; ++i) {
+                                       extract_token(this_recp, add_these_recipients,
+                                               i, ',', sizeof this_recp);
+                                       striplt(this_recp);
+                                       if (!IsEmptyStr(this_recp)) {
+                                               ++num_recipients;
+                                               recipients = realloc(recipients,
+                                                       (num_recipients * sizeof (char *)));
+                                               recipients[num_recipients - 1] = strdup(this_recp);
                                        }
                                }
+                       }
+               }
 
+               fprintf(fp, "%s", buf);
+       }
+       strip_trailing_nonprint(fromline);
 
+       sprintf(buf, "LHLO %s", hostname);
+       serv_puts(buf);
+       do {
+               serv_gets(buf);
+               strcat(buf, "    ");
+       } while (buf[3] == '-');
+       if (buf[0] != '2') {
+               if (debug) fprintf(stderr, "citmail: LHLO command failed\n");
+               cleanup(4);
+       }
 
-                       else if (!struncmp(buf, "RCPT", 4)) {
-                               printf("501 Only 'To:' commands are supported.\n");
-                               }
-                       else if (!struncmp(buf, "DATA", 4)) {
-                               if (strlen(recp) > 0) {
-                                       printf("354 Sock it to me, baby...\n");
-                                       fflush(stdout);
-                                       deliver(recp, is_test, deliver_to_ignet);
-                                       printf("250 Cool beans!\n");
-                                       }
-                               else {
-                                       printf("503 No recipient has been specified.\n");
-                                       }
-                               }
-                       else {
-                               printf("500 Huh?\n");
-                               }
-       
-                       } while (struncmp(buf,"QUIT",4));
-               }
+       snprintf(buf, sizeof buf, "MAIL %s", fromline);
+       serv_puts(buf);
+       serv_gets(buf);
+       if (buf[0] != '2') {
+               if (debug) fprintf(stderr, "citmail: MAIL command failed\n");
+               cleanup(5);
+       }
+
+       for (i=0; i<num_recipients; ++i) {
+               snprintf(buf, sizeof buf, "RCPT To: %s", recipients[i]);
+               serv_puts(buf);
+               serv_gets(buf);
+               free(recipients[i]);
+       }
+       free(recipients);
+
+       serv_puts("DATA");
+       serv_gets(buf);
+       if (buf[0]!='3') {
+               if (debug) fprintf(stderr, "citmail: DATA command failed\n");
+               cleanup(6);
+       }
 
+       rewind(fp);
+       while (fgets(buf, sizeof buf, fp) != NULL) {
+               strip_trailing_nonprint(buf);
+               serv_puts(buf);
+       }
+       serv_puts(".");
+       serv_gets(buf);
+       if (buf[0] != '2') {
+               fprintf(stderr, "%s\n", &buf[4]);
+               cleanup(7);
+       }
        else {
-               /*** Non-SMTP delivery mode ***/
-               syslog(LOG_NOTICE,"recp: %s",recp);
-               for (a=0; a<2; ++a) {
-                       alias(recp);
-                       }
-       
-               /* did we alias it back to a remote address? */
-               if (    (haschar(recp,'%'))
-               ||      (haschar(recp,'@'))
-               ||      (haschar(recp,'!')) ) {
-       
-                       process_rfc822_addr(recp, user, node, name);
-                       host_alias(node);
-               
-                       /* If there are dots, it's an Internet host, so feed it
-                       * back to an external mail transport agent such as sendmail.
-                       */
-                       if (haschar(node, '.')) {
-                               sprintf(buf, SENDMAIL, recp);
-                               system(buf);
-                               exit(0);
-                               }
-       
-                       /* Otherwise, we're dealing with Citadel mail. */
-                       else {
-                               sprintf(recp, "%s!%s", node, user);
-                               deliver_to_ignet = 1;
-                               }
-       
-                       }
-       
-               deliver(recp, is_test, deliver_to_ignet);
-               }
-       
-       closelog();
-       if (RUN_NETPROC) execlp("./netproc","netproc",NULL);
-       exit(0);
+               cleanup(0);
        }
 
+       /* We won't actually reach this statement but the compiler will
+        * display a spurious warning about an invalid return type if
+        * we don't return an int.
+        */
+       return(0);
+}