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"
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 sun;
56 memset(&sun, 0, sizeof(sun));
57 sun.sun_family = AF_UNIX;
58 strncpy(sun.sun_path, sockpath, sizeof sun.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 *) &sun, sizeof(sun)) < 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 == 255)
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) {
177 if (fp == NULL) return(errno);
178 sprintf(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 sprintf(buf, "MAIL %s", fromline);
196 if (buf[0]!='2') cleanup(1);
198 sprintf(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);