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 contacts the Citadel SMTP
6 * listener on a unix domain socket and transmits the message.
16 #include <sys/types.h>
17 #include <sys/socket.h>
31 /* #define DEBUG */ /* uncomment to get protocol traces */
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 addr;
54 memset(&addr, 0, sizeof(addr));
55 addr.sun_family = AF_UNIX;
56 strncpy(addr.sun_path, sockpath, sizeof addr.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 *) &addr, sizeof(addr)) < 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 == (SIZ-1))
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);
138 printf("> %s\n", buf);
144 * send line to server - implemented in terms of serv_write()
146 void serv_puts(char *buf)
149 printf("< %s\n", buf);
151 serv_write(buf, strlen(buf));
159 void cleanup(int exitcode) {
169 int main(int argc, char **argv) {
177 if (fp == NULL) return(errno);
178 snprintf(fromline, sizeof fromline, "From: someone@somewhere.org");
179 while (fgets(buf, 1024, stdin) != NULL) {
180 fprintf(fp, "%s", buf);
181 if (!strncasecmp(buf, "From:", 5)) strcpy(fromline, buf);
183 strip_trailing_nonprint(fromline);
185 serv_sock = uds_connectsock("smtp.socket");
187 if (buf[0]!='2') cleanup(1);
189 serv_puts("HELO localhost");
191 if (buf[0]!='2') cleanup(1);
193 snprintf(buf, sizeof buf, "MAIL %s", fromline);
196 if (buf[0]!='2') cleanup(1);
198 snprintf(buf, sizeof buf, "RCPT To: %s", argv[1]);
201 if (buf[0]!='2') cleanup(1);
205 if (buf[0]!='3') cleanup(1);
208 while (fgets(buf, sizeof buf, fp) != NULL) {
209 strip_trailing_nonprint(buf);
214 if (buf[0]!='2') cleanup(1);