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>
29 #include "citadel_dirs.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",
77 * input binary data from socket
79 void serv_read(char *buf, int bytes)
85 rlen = read(serv_sock, &buf[len], bytes - len);
95 * send binary to server
97 void serv_write(char *buf, int nbytes)
99 int bytes_written = 0;
101 while (bytes_written < nbytes) {
102 retval = write(serv_sock, &buf[bytes_written],
103 nbytes - bytes_written);
107 bytes_written = bytes_written + retval;
114 * input string from socket - implemented in terms of serv_read()
116 void serv_gets(char *buf)
120 /* Read one character at a time.
123 serv_read(&buf[i], 1);
124 if (buf[i] == '\n' || i == (SIZ-1))
128 /* If we got a long line, discard characters until the newline.
131 while (buf[i] != '\n')
132 serv_read(&buf[i], 1);
134 /* Strip all trailing nonprintables (crlf)
137 strip_trailing_nonprint(buf);
139 printf("> %s\n", buf);
145 * send line to server - implemented in terms of serv_write()
147 void serv_puts(char *buf)
150 printf("< %s\n", buf);
152 serv_write(buf, strlen(buf));
160 void cleanup(int exitcode) {
164 printf ("Error while sending mail."
165 "Check your maildata and make shure "
166 "citadel is configured properly!");
174 int main(int argc, char **argv) {
184 char relhome[PATH_MAX]="";
185 char ctdldir[PATH_MAX]=CTDLDIR;
189 /* TODO: should we be able to calculate relative dirs? */
190 calc_dirs_n_files(relh, home, relhome, ctdldir);
192 pw = getpwuid(getuid());
195 if (fp == NULL) return(errno);
196 serv_sock = uds_connectsock(file_lmtp_socket);
198 if (buf[0]!='2') cleanup(1);
200 sp = strchr (buf, ' ');
201 if (sp == NULL) cleanup(1);
203 ep = strchr (sp, ' ');
204 if (ep == NULL) cleanup(1);
207 snprintf(fromline, sizeof fromline, "From: %s@%s",
211 while (fgets(buf, 1024, stdin) != NULL) {
212 if ( ( (buf[0] == 13) || (buf[0] == 10)) && (in_body == 0) ) {
214 if (from_header == 0) {
215 fprintf(fp, "%s%s", fromline, buf);
218 if (!strncasecmp(buf, "From:", 5)) {
219 strcpy(fromline, buf);
224 fprintf(fp, "%s", buf);
226 strip_trailing_nonprint(fromline);
233 } while (buf[3] == '-');
234 if (buf[0] != '2') cleanup(1);
236 snprintf(buf, sizeof buf, "MAIL %s", fromline);
239 if (buf[0]!='2') cleanup(1);
241 for (i=1; i<argc; ++i) {
242 if (argv[i][0] != '-') {
243 snprintf(buf, sizeof buf, "RCPT To: %s", argv[i]);
246 /* if (buf[0]!='2') cleanup(1); */
252 if (buf[0]!='3') cleanup(1);
255 while (fgets(buf, sizeof buf, fp) != NULL) {
256 strip_trailing_nonprint(buf);
261 if (buf[0]!='2') cleanup(1);