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>
26 #include "citadel_decls.h"
33 /* #define DEBUG */ /* uncomment to get protocol traces */
38 void strip_trailing_nonprint(char *buf)
40 while ( (strlen(buf)>0) && (!isprint(buf[strlen(buf) - 1])) )
41 buf[strlen(buf) - 1] = 0;
45 void timeout(int signum)
51 int uds_connectsock(char *sockpath)
54 struct sockaddr_un addr;
56 memset(&addr, 0, sizeof(addr));
57 addr.sun_family = AF_UNIX;
58 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
60 s = socket(AF_UNIX, SOCK_STREAM, 0);
62 fprintf(stderr, "Can't create socket: %s\n",
67 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
68 fprintf(stderr, "can't connect: %s\n",
78 * input binary data from socket
80 void serv_read(char *buf, int bytes)
86 rlen = read(serv_sock, &buf[len], bytes - len);
96 * send binary to server
98 void serv_write(char *buf, int nbytes)
100 int bytes_written = 0;
102 while (bytes_written < nbytes) {
103 retval = write(serv_sock, &buf[bytes_written],
104 nbytes - bytes_written);
108 bytes_written = bytes_written + retval;
115 * input string from socket - implemented in terms of serv_read()
117 void serv_gets(char *buf)
121 /* Read one character at a time.
124 serv_read(&buf[i], 1);
125 if (buf[i] == '\n' || i == (SIZ-1))
129 /* If we got a long line, discard characters until the newline.
132 while (buf[i] != '\n')
133 serv_read(&buf[i], 1);
135 /* Strip all trailing nonprintables (crlf)
138 strip_trailing_nonprint(buf);
140 printf("> %s\n", buf);
146 * send line to server - implemented in terms of serv_write()
148 void serv_puts(char *buf)
151 printf("< %s\n", buf);
153 serv_write(buf, strlen(buf));
161 void cleanup(int exitcode) {
171 int main(int argc, char **argv) {
179 if (fp == NULL) return(errno);
180 snprintf(fromline, sizeof fromline, "From: someone@somewhere.org");
181 while (fgets(buf, 1024, stdin) != NULL) {
182 fprintf(fp, "%s", buf);
183 if (!strncasecmp(buf, "From:", 5)) strcpy(fromline, buf);
185 strip_trailing_nonprint(fromline);
187 serv_sock = uds_connectsock("smtp.socket");
189 if (buf[0]!='2') cleanup(1);
191 serv_puts("HELO localhost");
193 if (buf[0]!='2') cleanup(1);
195 snprintf(buf, sizeof buf, "MAIL %s", fromline);
198 if (buf[0]!='2') cleanup(1);
200 snprintf(buf, sizeof buf, "RCPT To: %s", argv[1]);
203 if (buf[0]!='2') cleanup(1);
207 if (buf[0]!='3') cleanup(1);
210 while (fgets(buf, sizeof buf, fp) != NULL) {
211 strip_trailing_nonprint(buf);
216 if (buf[0]!='2') cleanup(1);