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 LMTP
6 * listener on a unix domain socket and transmits the message.
16 #include <sys/types.h>
17 #include <sys/socket.h>
30 #include "citadel_dirs.h"
32 /* #define DEBUG */ /* uncomment to get protocol traces */
37 void strip_trailing_nonprint(char *buf)
39 while ( (strlen(buf)>0) && (!isprint(buf[strlen(buf) - 1])) )
40 buf[strlen(buf) - 1] = 0;
44 void timeout(int signum)
50 int uds_connectsock(char *sockpath)
53 struct sockaddr_un addr;
55 memset(&addr, 0, sizeof(addr));
56 addr.sun_family = AF_UNIX;
57 strncpy(addr.sun_path, sockpath, sizeof addr.sun_path);
59 s = socket(AF_UNIX, SOCK_STREAM, 0);
61 fprintf(stderr, "Can't create socket: %s\n",
66 if (connect(s, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
67 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) {
181 char relhome[PATH_MAX]="";
182 char ctdldir[PATH_MAX]=CTDLDIR;
184 /* TODO: should we be able to calculate relative dirs? */
185 calc_dirs_n_files(relh, home, relhome, ctdldir);
189 pw = getpwuid(getuid());
192 if (fp == NULL) return(errno);
193 snprintf(fromline, sizeof fromline, "From: %s@%s",
197 while (fgets(buf, 1024, stdin) != NULL) {
198 if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) {
200 if (from_header == 0) {
201 fprintf(fp, "%s%s", fromline, buf);
204 if (!strncasecmp(buf, "From:", 5)) {
205 strcpy(fromline, buf);
210 fprintf(fp, "%s", buf);
212 strip_trailing_nonprint(fromline);
214 serv_sock = uds_connectsock(file_lmtp_socket);
216 if (buf[0]!='2') cleanup(1);
222 } while (buf[3] == '-');
223 if (buf[0] != '2') cleanup(1);
225 snprintf(buf, sizeof buf, "MAIL %s", fromline);
228 if (buf[0]!='2') cleanup(1);
230 for (i=1; i<argc; ++i) {
231 if (argv[i][0] != '-') {
232 snprintf(buf, sizeof buf, "RCPT To: %s", argv[i]);
235 /* if (buf[0]!='2') cleanup(1); */
241 if (buf[0]!='3') cleanup(1);
244 while (fgets(buf, sizeof buf, fp) != NULL) {
245 strip_trailing_nonprint(buf);
250 if (buf[0]!='2') cleanup(1);