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>
27 #include "citadel_decls.h"
36 void strip_trailing_nonprint(char *buf)
38 while ( (strlen(buf)>0) && (!isprint(buf[strlen(buf) - 1])) )
39 buf[strlen(buf) - 1] = 0;
43 void timeout(int signum)
49 int uds_connectsock(char *sockpath)
52 struct sockaddr_un sun;
54 memset(&sun, 0, sizeof(sun));
55 sun.sun_family = AF_UNIX;
56 strncpy(sun.sun_path, sockpath, sizeof sun.sun_path);
58 s = socket(AF_UNIX, SOCK_STREAM, 0);
60 fprintf(stderr, "Can't create socket: %s\n",
65 if (connect(s, (struct sockaddr *) &sun, sizeof(sun)) < 0) {
66 fprintf(stderr, "can't connect: %s\n",
76 * input binary data from socket
78 void serv_read(char *buf, int bytes)
84 rlen = read(serv_sock, &buf[len], bytes - len);
94 * send binary to server
96 void serv_write(char *buf, int nbytes)
98 int bytes_written = 0;
100 while (bytes_written < nbytes) {
101 retval = write(serv_sock, &buf[bytes_written],
102 nbytes - bytes_written);
106 bytes_written = bytes_written + retval;
113 * input string from socket - implemented in terms of serv_read()
115 void serv_gets(char *buf)
119 /* Read one character at a time.
122 serv_read(&buf[i], 1);
123 if (buf[i] == '\n' || i == 255)
127 /* If we got a long line, discard characters until the newline.
130 while (buf[i] != '\n')
131 serv_read(&buf[i], 1);
133 /* Strip all trailing nonprintables (crlf)
136 strip_trailing_nonprint(buf);
141 * send line to server - implemented in terms of serv_write()
143 void serv_puts(char *buf)
145 /* printf("< %s\n", buf); */
146 serv_write(buf, strlen(buf));
154 void cleanup(int exitcode) {
159 fprintf(stderr, "%s\n", buf);
165 int main(int argc, char **argv) {
171 if (fp == NULL) return(errno);
172 sprintf(fromline, "From: someone@somewhere.org");
173 while (fgets(buf, 1024, stdin) != NULL) {
174 fprintf(fp, "%s", buf);
175 if (!strncasecmp(buf, "From:", 5)) strcpy(fromline, buf);
177 strip_trailing_nonprint(fromline);
179 sprintf(buf, "%d", SMTP_PORT);
180 serv_sock = uds_connectsock("smtp.socket");
182 fprintf(stderr, "%s\n", buf);
183 if (buf[0]!='2') cleanup(1);
185 serv_puts("HELO localhost");
187 fprintf(stderr, "%s\n", buf);
188 if (buf[0]!='2') cleanup(1);
190 sprintf(buf, "MAIL %s", fromline);
193 fprintf(stderr, "%s\n", buf);
194 if (buf[0]!='2') cleanup(1);
196 sprintf(buf, "RCPT To: %s", argv[1]);
199 fprintf(stderr, "%s\n", buf);
200 if (buf[0]!='2') cleanup(1);
204 fprintf(stderr, "%s\n", buf);
205 if (buf[0]!='3') cleanup(1);
208 while (fgets(buf, sizeof buf, fp) != NULL) {
209 strip_trailing_nonprint(buf);
214 fprintf(stderr, "%s\n", buf);
215 if (buf[0]!='2') cleanup(1);