* Stuff
[citadel.git] / citadel / serv_smtp.c
1 /* $Id$ */
2
3 #include "sysdep.h"
4 #include <stdlib.h>
5 #include <unistd.h>
6 #include <stdio.h>
7 #include <fcntl.h>
8 #include <signal.h>
9 #include <pwd.h>
10 #include <errno.h>
11 #include <sys/types.h>
12 #include <sys/time.h>
13 #include <sys/wait.h>
14 #include <string.h>
15 #include <limits.h>
16 #include "citadel.h"
17 #include "server.h"
18 #include <time.h>
19 #include "sysdep_decls.h"
20 #include "citserver.h"
21 #include "support.h"
22 #include "config.h"
23 #include "dynloader.h"
24 #include "room_ops.h"
25 #include "user_ops.h"
26 #include "policy.h"
27 #include "database.h"
28 #include "msgbase.h"
29 #include "tools.h"
30 #include "internet_addressing.h"
31 #include "genstamp.h"
32
33
34 struct citsmtp {                /* Information about the current session */
35         int command_state;
36         struct usersupp vrfy_buffer;
37         int vrfy_count;
38         char vrfy_match[256];
39         char from[256];
40         int number_of_recipients;
41         int delivery_mode;
42 };
43
44 enum {                          /* Command states for login authentication */
45         smtp_command,
46         smtp_user,
47         smtp_password
48 };
49
50 enum {                          /* Delivery modes */
51         smtp_deliver_local,
52         smtp_deliver_remote
53 };
54
55 #define SMTP            ((struct citsmtp *)CtdlGetUserData(SYM_SMTP))
56 #define SMTP_RECP       ((char *)CtdlGetUserData(SYM_SMTP_RECP))
57
58 long SYM_SMTP;
59 long SYM_SMTP_RECP;
60
61
62
63 /*****************************************************************************/
64 /*                      SMTP SERVER (INBOUND) STUFF                          */
65 /*****************************************************************************/
66
67
68 /*
69  * Here's where our SMTP session begins its happy day.
70  */
71 void smtp_greeting(void) {
72
73         strcpy(CC->cs_clientname, "SMTP session");
74         CC->internal_pgm = 1;
75         CC->cs_flags |= CS_STEALTH;
76         CtdlAllocUserData(SYM_SMTP, sizeof(struct citsmtp));
77         CtdlAllocUserData(SYM_SMTP_RECP, 256);
78         sprintf(SMTP_RECP, "%s", "");
79
80         cprintf("220 Welcome to the Citadel/UX ESMTP server at %s\r\n",
81                 config.c_fqdn);
82 }
83
84
85 /*
86  * Implement HELO and EHLO commands.
87  */
88 void smtp_hello(char *argbuf, int is_esmtp) {
89
90         if (!is_esmtp) {
91                 cprintf("250 Greetings and joyous salutations.\r\n");
92         }
93         else {
94                 cprintf("250-Greetings and joyous salutations.\r\n");
95                 cprintf("250-HELP\r\n");
96                 cprintf("250-SIZE %ld\r\n", config.c_maxmsglen);
97                 cprintf("250 AUTH=LOGIN\r\n");
98         }
99 }
100
101
102 /*
103  * Implement HELP command.
104  */
105 void smtp_help(void) {
106         cprintf("214-Here's the frequency, Kenneth:\r\n");
107         cprintf("214-    DATA\r\n");
108         cprintf("214-    EHLO\r\n");
109         cprintf("214-    EXPN\r\n");
110         cprintf("214-    HELO\r\n");
111         cprintf("214-    HELP\r\n");
112         cprintf("214-    MAIL\r\n");
113         cprintf("214-    NOOP\r\n");
114         cprintf("214-    QUIT\r\n");
115         cprintf("214-    RCPT\r\n");
116         cprintf("214-    RSET\r\n");
117         cprintf("214-    VRFY\r\n");
118         cprintf("214 I could tell you more, but then I'd have to kill you.\r\n");
119 }
120
121
122 /*
123  *
124  */
125 void smtp_get_user(char *argbuf) {
126         char buf[256];
127         char username[256];
128
129         decode_base64(username, argbuf);
130         lprintf(9, "Trying <%s>\n", username);
131         if (CtdlLoginExistingUser(username) == login_ok) {
132                 encode_base64(buf, "Password:");
133                 cprintf("334 %s\r\n", buf);
134                 SMTP->command_state = smtp_password;
135         }
136         else {
137                 cprintf("500 No such user.\r\n");
138                 SMTP->command_state = smtp_command;
139         }
140 }
141
142
143 /*
144  *
145  */
146 void smtp_get_pass(char *argbuf) {
147         char password[256];
148
149         decode_base64(password, argbuf);
150         lprintf(9, "Trying <%s>\n", password);
151         if (CtdlTryPassword(password) == pass_ok) {
152                 cprintf("235 Authentication successful.\r\n");
153                 lprintf(9, "SMTP authenticated login successful\n");
154                 CC->internal_pgm = 0;
155                 CC->cs_flags &= ~CS_STEALTH;
156         }
157         else {
158                 cprintf("500 Authentication failed.\r\n");
159         }
160         SMTP->command_state = smtp_command;
161 }
162
163
164 /*
165  *
166  */
167 void smtp_auth(char *argbuf) {
168         char buf[256];
169
170         if (strncasecmp(argbuf, "login", 5) ) {
171                 cprintf("550 We only support LOGIN authentication.\r\n");
172                 return;
173         }
174
175         if (strlen(argbuf) >= 7) {
176                 smtp_get_user(&argbuf[6]);
177         }
178
179         else {
180                 encode_base64(buf, "Username:");
181                 cprintf("334 %s\r\n", buf);
182                 SMTP->command_state = smtp_user;
183         }
184 }
185
186
187 /*
188  * Back end for smtp_vrfy() command
189  */
190 void smtp_vrfy_backend(struct usersupp *us, void *data) {
191
192         if (!fuzzy_match(us, SMTP->vrfy_match)) {
193                 ++SMTP->vrfy_count;
194                 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
195         }
196 }
197
198
199 /* 
200  * Implements the VRFY (verify user name) command.
201  * Performs fuzzy match on full user names.
202  */
203 void smtp_vrfy(char *argbuf) {
204         SMTP->vrfy_count = 0;
205         strcpy(SMTP->vrfy_match, argbuf);
206         ForEachUser(smtp_vrfy_backend, NULL);
207
208         if (SMTP->vrfy_count < 1) {
209                 cprintf("550 String does not match anything.\r\n");
210         }
211         else if (SMTP->vrfy_count == 1) {
212                 cprintf("250 %s <cit%ld@%s>\r\n",
213                         SMTP->vrfy_buffer.fullname,
214                         SMTP->vrfy_buffer.usernum,
215                         config.c_fqdn);
216         }
217         else if (SMTP->vrfy_count > 1) {
218                 cprintf("553 Request ambiguous: %d users matched.\r\n",
219                         SMTP->vrfy_count);
220         }
221
222 }
223
224
225
226 /*
227  * Back end for smtp_expn() command
228  */
229 void smtp_expn_backend(struct usersupp *us, void *data) {
230
231         if (!fuzzy_match(us, SMTP->vrfy_match)) {
232
233                 if (SMTP->vrfy_count >= 1) {
234                         cprintf("250-%s <cit%ld@%s>\r\n",
235                                 SMTP->vrfy_buffer.fullname,
236                                 SMTP->vrfy_buffer.usernum,
237                                 config.c_fqdn);
238                 }
239
240                 ++SMTP->vrfy_count;
241                 memcpy(&SMTP->vrfy_buffer, us, sizeof(struct usersupp));
242         }
243 }
244
245
246 /* 
247  * Implements the EXPN (expand user name) command.
248  * Performs fuzzy match on full user names.
249  */
250 void smtp_expn(char *argbuf) {
251         SMTP->vrfy_count = 0;
252         strcpy(SMTP->vrfy_match, argbuf);
253         ForEachUser(smtp_expn_backend, NULL);
254
255         if (SMTP->vrfy_count < 1) {
256                 cprintf("550 String does not match anything.\r\n");
257         }
258         else if (SMTP->vrfy_count >= 1) {
259                 cprintf("250 %s <cit%ld@%s>\r\n",
260                         SMTP->vrfy_buffer.fullname,
261                         SMTP->vrfy_buffer.usernum,
262                         config.c_fqdn);
263         }
264 }
265
266
267 /*
268  * Implements the RSET (reset state) command.
269  * Currently this just zeroes out the state buffer.  If pointers to data
270  * allocated with mallok() are ever placed in the state buffer, we have to
271  * be sure to phree() them first!
272  */
273 void smtp_rset(void) {
274         memset(SMTP, 0, sizeof(struct citsmtp));
275         if (CC->logged_in) logout(CC);
276         cprintf("250 Zap!\r\n");
277 }
278
279
280
281 /*
282  * Implements the "MAIL From:" command
283  */
284 void smtp_mail(char *argbuf) {
285         char user[256];
286         char node[256];
287         int cvt;
288
289         if (strlen(SMTP->from) != 0) {
290                 cprintf("503 Only one sender permitted\r\n");
291                 return;
292         }
293
294         if (strncasecmp(argbuf, "From:", 5)) {
295                 cprintf("501 Syntax error\r\n");
296                 return;
297         }
298
299         strcpy(SMTP->from, &argbuf[5]);
300         striplt(SMTP->from);
301
302         if (strlen(SMTP->from) == 0) {
303                 cprintf("501 Empty sender name is not permitted\r\n");
304                 return;
305         }
306
307
308         /* If this SMTP connection is from a logged-in user, make sure that
309          * the user only sends email from his/her own address.
310          */
311         if (CC->logged_in) {
312                 cvt = convert_internet_address(user, node, SMTP->from);
313                 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
314                 if ( (cvt != 0) || (strcasecmp(user, CC->usersupp.fullname))) {
315                         cprintf("550 <%s> is not your address.\r\n", SMTP->from);
316                         strcpy(SMTP->from, "");
317                         return;
318                 }
319         }
320
321         /* Otherwise, make sure outsiders aren't trying to forge mail from
322          * this system.
323          */
324         else {
325                 cvt = convert_internet_address(user, node, SMTP->from);
326                 lprintf(9, "cvt=%d, citaddr=<%s@%s>\n", cvt, user, node);
327                 if (!strcasecmp(node, config.c_nodename)) { /* FIX use fcn */
328                         cprintf("550 You must log in to send mail from %s\r\n",
329                                 config.c_fqdn);
330                         strcpy(SMTP->from, "");
331                         return;
332                 }
333         }
334
335         cprintf("250 Sender ok.  Groovy.\r\n");
336 }
337
338
339
340 /*
341  * Implements the "RCPT To:" command
342  */
343 void smtp_rcpt(char *argbuf) {
344         int cvt;
345         char user[256];
346         char node[256];
347         char recp[256];
348         int is_spam = 0;        /* FIX implement anti-spamming */
349
350         if (strlen(SMTP->from) == 0) {
351                 cprintf("503 MAIL first, then RCPT.  Duh.\r\n");
352                 return;
353         }
354
355         if (strncasecmp(argbuf, "To:", 3)) {
356                 cprintf("501 Syntax error\r\n");
357                 return;
358         }
359
360         strcpy(recp, &argbuf[3]);
361         striplt(recp);
362         alias(recp);
363
364         cvt = convert_internet_address(user, node, recp);
365         sprintf(recp, "%s@%s", user, node);
366
367
368         switch(cvt) {
369                 case rfc822_address_locally_validated:
370                         cprintf("250 %s is a valid recipient.\r\n", user);
371                         ++SMTP->number_of_recipients;
372                         CtdlReallocUserData(SYM_SMTP_RECP,
373                                 strlen(SMTP_RECP) + 1024 );
374                         strcat(SMTP_RECP, "local|");
375                         strcat(SMTP_RECP, user);
376                         strcat(SMTP_RECP, "|0\n");
377                         return;
378
379                 case rfc822_room_delivery:
380                         cprintf("250 Delivering to room '%s'\r\n", user);
381                         ++SMTP->number_of_recipients;
382                         CtdlReallocUserData(SYM_SMTP_RECP,
383                                 strlen(SMTP_RECP) + 1024 );
384                         strcat(SMTP_RECP, "room|");
385                         strcat(SMTP_RECP, user);
386                         strcat(SMTP_RECP, "|0|\n");
387                         return;
388
389                 case rfc822_no_such_user:
390                         cprintf("550 %s: no such user\r\n", recp);
391                         return;
392
393                 case rfc822_address_invalid:
394                         if (is_spam) {
395                                 cprintf("551 Away with thee, spammer!\r\n");
396                         }
397                         else {
398                                 cprintf("250 Remote recipient %s ok\r\n", recp);
399                                 ++SMTP->number_of_recipients;
400                                 CtdlReallocUserData(SYM_SMTP_RECP,
401                                         strlen(SMTP_RECP) + 1024 );
402                                 strcat(SMTP_RECP, "remote|");
403                                 strcat(SMTP_RECP, recp);
404                                 strcat(SMTP_RECP, "|0|\n");
405                                 return;
406                         }
407                         return;
408         }
409
410         cprintf("599 Unknown error\r\n");
411 }
412
413
414
415
416
417 /*
418  * Back end for smtp_data()  ... this does the actual delivery of the message
419  * Returns 0 on success, nonzero on failure
420  */
421 int smtp_message_delivery(struct CtdlMessage *msg) {
422         char user[1024];
423         char node[1024];
424         char name[1024];
425         char buf[1024];
426         char dtype[1024];
427         char room[1024];
428         int successful_saves = 0;       /* number of successful local saves */
429         int failed_saves = 0;           /* number of failed deliveries */
430         int remote_spools = 0;          /* number of copies to send out */
431         long msgid = (-1L);
432         int i;
433         struct usersupp userbuf;
434         char *instr;                    /* Remote delivery instructions */
435         struct CtdlMessage *imsg;
436
437         lprintf(9, "smtp_message_delivery() called\n");
438
439         /* Fill in 'from' fields with envelope information if missing */
440         process_rfc822_addr(SMTP->from, user, node, name);
441         if (msg->cm_fields['A']==NULL) msg->cm_fields['A'] = strdoop(user);
442         if (msg->cm_fields['N']==NULL) msg->cm_fields['N'] = strdoop(node);
443         if (msg->cm_fields['H']==NULL) msg->cm_fields['H'] = strdoop(name);
444
445         /* Save the message in the queue */
446         msgid = CtdlSaveMsg(msg,
447                 "",
448                 SMTP_SPOOLOUT_ROOM,
449                 MES_LOCAL,
450                 1);
451         ++successful_saves;
452
453         instr = mallok(1024);
454         sprintf(instr, "Content-type: %s\n\nmsgid|%ld\nsubmitted|%ld\n",
455                 SPOOLMIME, msgid, time(NULL) );
456
457         for (i=0; i<SMTP->number_of_recipients; ++i) {
458                 extract_token(buf, SMTP_RECP, i, '\n');
459                 extract(dtype, buf, 0);
460
461                 /* Stuff local mailboxes */
462                 if (!strcasecmp(dtype, "local")) {
463                         extract(user, buf, 1);
464                         if (getuser(&userbuf, user) == 0) {
465                                 MailboxName(room, &userbuf, MAILROOM);
466                                 CtdlSaveMsgPointerInRoom(room, msgid, 0);
467                                 ++successful_saves;
468                         }
469                         else {
470                                 ++failed_saves;
471                         }
472                 }
473
474                 /* Delivery to local non-mailbox rooms */
475                 if (!strcasecmp(dtype, "room")) {
476                         extract(room, buf, 1);
477                         CtdlSaveMsgPointerInRoom(room, msgid, 0);
478                         ++successful_saves;
479                 }
480
481                 /* Remote delivery */
482                 if (!strcasecmp(dtype, "remote")) {
483                         extract(user, buf, 1);
484                         instr = reallok(instr, strlen(instr) + 1024);
485                         sprintf(&instr[strlen(instr)],
486                                 "remote|%s|0\n",
487                                 user);
488                         ++remote_spools;
489                 }
490
491         }
492
493         /* If there are remote spools to be done, save the instructions */
494         if (remote_spools > 0) {
495                 imsg = mallok(sizeof(struct CtdlMessage));
496                 memset(imsg, 0, sizeof(struct CtdlMessage));
497                 imsg->cm_magic = CTDLMESSAGE_MAGIC;
498                 imsg->cm_anon_type = MES_NORMAL;
499                 imsg->cm_format_type = FMT_RFC822;
500                 imsg->cm_fields['M'] = instr;
501                 CtdlSaveMsg(imsg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL, 1);
502                 CtdlFreeMessage(imsg);
503         }
504
505         /* If there are no remote spools, delete the message */ 
506         else {
507                 phree(instr);   /* only needed here, because CtdlSaveMsg()
508                                  * would free this buffer otherwise */
509                 CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, msgid, NULL); 
510         }
511
512         return(failed_saves);
513 }
514
515
516
517 /*
518  * Implements the DATA command
519  */
520 void smtp_data(void) {
521         char *body;
522         struct CtdlMessage *msg;
523         int retval;
524         char nowstamp[256];
525
526         if (strlen(SMTP->from) == 0) {
527                 cprintf("503 Need MAIL command first.\r\n");
528                 return;
529         }
530
531         if (SMTP->number_of_recipients < 1) {
532                 cprintf("503 Need RCPT command first.\r\n");
533                 return;
534         }
535
536         cprintf("354 Transmit message now; terminate with '.' by itself\r\n");
537         
538         generate_rfc822_datestamp(nowstamp, time(NULL));
539         body = mallok(4096);
540         if (body != NULL) sprintf(body,
541                 "Received: from %s\n"
542                 "       by %s;\n"
543                 "       %s\n",
544                         "FIX.FIX.com",
545                         config.c_fqdn,
546                         nowstamp);
547         
548         body = CtdlReadMessageBody(".", config.c_maxmsglen, body);
549         if (body == NULL) {
550                 cprintf("550 Unable to save message text: internal error.\r\n");
551                 return;
552         }
553
554         lprintf(9, "Converting message...\n");
555         msg = convert_internet_message(body);
556
557         /* If the user is locally authenticated, FORCE the From: header to
558          * show up as the real sender
559          */
560         if (CC->logged_in) {
561                 if (msg->cm_fields['A'] != NULL) phree(msg->cm_fields['A']);
562                 if (msg->cm_fields['N'] != NULL) phree(msg->cm_fields['N']);
563                 if (msg->cm_fields['H'] != NULL) phree(msg->cm_fields['H']);
564                 msg->cm_fields['A'] = strdoop(CC->usersupp.fullname);
565                 msg->cm_fields['N'] = strdoop(config.c_nodename);
566                 msg->cm_fields['H'] = strdoop(config.c_humannode);
567         }
568
569         retval = smtp_message_delivery(msg);
570         CtdlFreeMessage(msg);
571
572         if (!retval) {
573                 cprintf("250 Message accepted for delivery.\r\n");
574         }
575         else {
576                 cprintf("550 Internal delivery errors: %d\r\n", retval);
577         }
578 }
579
580
581
582
583 /* 
584  * Main command loop for SMTP sessions.
585  */
586 void smtp_command_loop(void) {
587         char cmdbuf[256];
588
589         time(&CC->lastcmd);
590         memset(cmdbuf, 0, sizeof cmdbuf); /* Clear it, just in case */
591         if (client_gets(cmdbuf) < 1) {
592                 lprintf(3, "SMTP socket is broken.  Ending session.\n");
593                 CC->kill_me = 1;
594                 return;
595         }
596         lprintf(5, "citserver[%3d]: %s\n", CC->cs_pid, cmdbuf);
597         while (strlen(cmdbuf) < 5) strcat(cmdbuf, " ");
598
599         if (SMTP->command_state == smtp_user) {
600                 smtp_get_user(cmdbuf);
601         }
602
603         else if (SMTP->command_state == smtp_password) {
604                 smtp_get_pass(cmdbuf);
605         }
606
607         else if (!strncasecmp(cmdbuf, "AUTH", 4)) {
608                 smtp_auth(&cmdbuf[5]);
609         }
610
611         else if (!strncasecmp(cmdbuf, "DATA", 4)) {
612                 smtp_data();
613         }
614
615         else if (!strncasecmp(cmdbuf, "EHLO", 4)) {
616                 smtp_hello(&cmdbuf[5], 1);
617         }
618
619         else if (!strncasecmp(cmdbuf, "EXPN", 4)) {
620                 smtp_expn(&cmdbuf[5]);
621         }
622
623         else if (!strncasecmp(cmdbuf, "HELO", 4)) {
624                 smtp_hello(&cmdbuf[5], 0);
625         }
626
627         else if (!strncasecmp(cmdbuf, "HELP", 4)) {
628                 smtp_help();
629         }
630
631         else if (!strncasecmp(cmdbuf, "MAIL", 4)) {
632                 smtp_mail(&cmdbuf[5]);
633         }
634
635         else if (!strncasecmp(cmdbuf, "NOOP", 4)) {
636                 cprintf("250 This command successfully did nothing.\r\n");
637         }
638
639         else if (!strncasecmp(cmdbuf, "QUIT", 4)) {
640                 cprintf("221 Goodbye...\r\n");
641                 CC->kill_me = 1;
642                 return;
643                 }
644
645         else if (!strncasecmp(cmdbuf, "RCPT", 4)) {
646                 smtp_rcpt(&cmdbuf[5]);
647         }
648
649         else if (!strncasecmp(cmdbuf, "RSET", 4)) {
650                 smtp_rset();
651         }
652
653         else if (!strncasecmp(cmdbuf, "VRFY", 4)) {
654                 smtp_vrfy(&cmdbuf[5]);
655         }
656
657         else {
658                 cprintf("502 I'm sorry Dave, I'm afraid I can't do that.\r\n");
659         }
660
661 }
662
663
664
665
666 /*****************************************************************************/
667 /*               SMTP CLIENT (OUTBOUND PROCESSING) STUFF                     */
668 /*****************************************************************************/
669
670
671
672 /*
673  * smtp_try()
674  *
675  * Called by smtp_do_procmsg() to attempt delivery to one SMTP host
676  *
677  */
678 void smtp_try(char *key, char *addr, int *status, char *dsn) {
679
680         *status = 3;
681         strcpy(dsn, "smtp_try() is not finished yet");
682 }
683
684
685
686 /*
687  * smtp_do_procmsg()
688  *
689  * Called by smtp_do_queue() to handle an individual message.
690  */
691 void smtp_do_procmsg(long msgnum) {
692         struct CtdlMessage *msg;
693         char *instr = NULL;
694         char *results = NULL;
695         int i;
696         int lines;
697         int status;
698         char buf[1024];
699         char key[1024];
700         char addr[1024];
701         char dsn[1024];
702         long text_msgid = (-1);
703
704         msg = CtdlFetchMessage(msgnum);
705         if (msg == NULL) {
706                 lprintf(3, "SMTP: tried %ld but no such message!\n", msgnum);
707                 return;
708         }
709
710         instr = strdoop(msg->cm_fields['M']);
711         CtdlFreeMessage(msg);
712
713         /* Strip out the headers amd any other non-instruction line */
714         lines = num_tokens(instr, '\n');
715         for (i=0; i<lines; ++i) {
716                 extract_token(buf, instr, i, '\n');
717                 if (num_tokens(buf, '|') < 2) {
718                         lprintf(9, "removing <%s>\n", buf);
719                         remove_token(instr, i, '|');
720                         --lines;
721                         --i;
722                 }
723         }
724
725         /* Learn the message ID */
726         lines = num_tokens(instr, '\n');
727         for (i=0; i<lines; ++i) {
728                 extract_token(buf, instr, i, '\n');
729                 extract(key, buf, 0);
730                 if (!strcasecmp(key, "msgid")) {
731                         text_msgid = extract_long(buf, 1);
732                 }
733         }
734
735         if (text_msgid < 0L) {
736                 lprintf(3, "SMTP: no 'msgid' directive found!\n", msgnum);
737                 phree(instr);
738                 return;
739         }
740
741         /* Plow through the instructions looking for 'remote' directives and
742          * a status of 0 (no delivery yet attempted) or 3 (transient errors
743          * were experienced and it's time to try again)
744          */
745         lines = num_tokens(instr, '\n');
746         for (i=0; i<lines; ++i) {
747                 extract_token(buf, instr, i, '\n');
748                 extract(key, buf, 0);
749                 extract(addr, buf, 1);
750                 status = extract_int(buf, 2);
751                 extract(dsn, buf, 3);
752                 if ( (!strcasecmp(key, "remote"))
753                    && ((status==0)||(status==3)) ) {
754                         remove_token(instr, i, '\n');
755                         --i;
756                         --lines;
757                         lprintf(9, "SMTP: Trying <%s>\n", addr);
758                         smtp_try(key, addr, &status, dsn);
759                         if (status != 2) {
760                                 if (results == NULL) {
761                                         results = mallok(1024);
762                                         memset(results, 0, 1024);
763                                 }
764                                 else {
765                                         results = reallok(results,
766                                                 strlen(results) + 1024);
767                                 }
768                                 sprintf(&results[strlen(results)],
769                                         "%s|%s|%d|%s\n",
770                                         key, addr, status, dsn);
771                         }
772                 }
773         }
774
775         if (results != NULL) {
776                 instr = reallok(instr, strlen(instr) + strlen(results) + 2);
777                 strcat(instr, results);
778                 phree(results);
779         }
780
781         /* Delete the instructions and replace with the updated ones */
782         CtdlDeleteMessages(SMTP_SPOOLOUT_ROOM, msgnum, NULL);    
783         msg = mallok(sizeof(struct CtdlMessage));
784         memset(msg, 0, sizeof(struct CtdlMessage));
785         msg->cm_magic = CTDLMESSAGE_MAGIC;
786         msg->cm_anon_type = MES_NORMAL;
787         msg->cm_format_type = FMT_RFC822;
788         msg->cm_fields['M'] = instr;
789         CtdlSaveMsg(msg, "", SMTP_SPOOLOUT_ROOM, MES_LOCAL, 1);
790         CtdlFreeMessage(msg);
791 }
792
793
794
795 /*
796  * smtp_do_queue()
797  * 
798  * Run through the queue sending out messages.
799  */
800 void smtp_do_queue(void) {
801         lprintf(5, "SMTP: processing outbound queue\n");
802
803         if (getroom(&CC->quickroom, SMTP_SPOOLOUT_ROOM) != 0) {
804                 lprintf(3, "Cannot find room <%s>\n", SMTP_SPOOLOUT_ROOM);
805                 return;
806         }
807         CtdlForEachMessage(MSGS_ALL, 0L, SPOOLMIME, NULL, smtp_do_procmsg);
808
809         lprintf(5, "SMTP: queue run completed\n");
810 }
811
812
813 /**** FIX  temporary hack to run the queue *****/
814 void cmd_qqqq(char *argbuf) {
815         smtp_do_queue();
816         cprintf("%d ok\n", OK);
817 }
818
819
820
821 /*****************************************************************************/
822 /*                      MODULE INITIALIZATION STUFF                          */
823 /*****************************************************************************/
824
825
826 char *Dynamic_Module_Init(void)
827 {
828         SYM_SMTP = CtdlGetDynamicSymbol();
829         SYM_SMTP_RECP = CtdlGetDynamicSymbol();
830         CtdlRegisterServiceHook(SMTP_PORT,
831                                 smtp_greeting,
832                                 smtp_command_loop);
833
834         /****  FIX ... temporary hack to run the queue ******/
835         CtdlRegisterProtoHook(cmd_qqqq, "QQQQ", "run the queue");  
836
837         create_room(SMTP_SPOOLOUT_ROOM, 3, "", 0);
838         return "$Id$";
839 }
840