3 * Completely reworked version of "citmail"
4 * This program attempts to act like a local MDA if you're using sendmail or
5 * some other non-Citadel MTA. It basically just forwards the message to
6 * the Citadel SMTP listener on some non-standard port.
18 #include <sys/types.h>
19 #include <sys/socket.h>
28 #include "citadel_decls.h"
35 /* #define DEBUG */ /* uncomment to get protocol traces */
40 void strip_trailing_nonprint(char *buf)
42 while ( (strlen(buf)>0) && (!isprint(buf[strlen(buf) - 1])) )
43 buf[strlen(buf) - 1] = 0;
47 void timeout(int signum)
53 int uds_connectsock(char *sockpath)
56 struct sockaddr_un addr;
58 memset(&addr, 0, sizeof(addr));
59 addr.sun_family = AF_UNIX;
60 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
62 s = socket(AF_UNIX, SOCK_STREAM, 0);
64 fprintf(stderr, "Can't create socket: %s\n",
69 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
70 fprintf(stderr, "can't connect: %s\n",
80 * input binary data from socket
82 void serv_read(char *buf, int bytes)
88 rlen = read(serv_sock, &buf[len], bytes - len);
98 * send binary to server
100 void serv_write(char *buf, int nbytes)
102 int bytes_written = 0;
104 while (bytes_written < nbytes) {
105 retval = write(serv_sock, &buf[bytes_written],
106 nbytes - bytes_written);
110 bytes_written = bytes_written + retval;
117 * input string from socket - implemented in terms of serv_read()
119 void serv_gets(char *buf)
123 /* Read one character at a time.
126 serv_read(&buf[i], 1);
127 if (buf[i] == '\n' || i == 255)
131 /* If we got a long line, discard characters until the newline.
134 while (buf[i] != '\n')
135 serv_read(&buf[i], 1);
137 /* Strip all trailing nonprintables (crlf)
140 strip_trailing_nonprint(buf);
142 printf("> %s\n", buf);
148 * send line to server - implemented in terms of serv_write()
150 void serv_puts(char *buf)
153 printf("< %s\n", buf);
155 serv_write(buf, strlen(buf));
163 void cleanup(int exitcode) {
173 int main(int argc, char **argv) {
181 if (fp == NULL) return(errno);
182 sprintf(fromline, "From: someone@somewhere.org");
183 while (fgets(buf, 1024, stdin) != NULL) {
184 fprintf(fp, "%s", buf);
185 if (!strncasecmp(buf, "From:", 5)) strcpy(fromline, buf);
187 strip_trailing_nonprint(fromline);
189 serv_sock = uds_connectsock("smtp.socket");
191 if (buf[0]!='2') cleanup(1);
193 serv_puts("HELO localhost");
195 if (buf[0]!='2') cleanup(1);
197 sprintf(buf, "MAIL %s", fromline);
200 if (buf[0]!='2') cleanup(1);
202 sprintf(buf, "RCPT To: %s", argv[1]);
205 if (buf[0]!='2') cleanup(1);
209 if (buf[0]!='3') cleanup(1);
212 while (fgets(buf, sizeof buf, fp) != NULL) {
213 strip_trailing_nonprint(buf);
218 if (buf[0]!='2') cleanup(1);