df04bdcc5f713f66ebdb138c9d2bc73ab1067458
[citadel.git] / citadel / modules / smtp / serv_smtpqueue.c
1 /*
2  * This module is an SMTP and ESMTP implementation for the Citadel system.
3  * It is compliant with all of the following:
4  *
5  * RFC  821 - Simple Mail Transfer Protocol
6  * RFC  876 - Survey of SMTP Implementations
7  * RFC 1047 - Duplicate messages and SMTP
8  * RFC 1652 - 8 bit MIME
9  * RFC 1869 - Extended Simple Mail Transfer Protocol
10  * RFC 1870 - SMTP Service Extension for Message Size Declaration
11  * RFC 2033 - Local Mail Transfer Protocol
12  * RFC 2197 - SMTP Service Extension for Command Pipelining
13  * RFC 2476 - Message Submission
14  * RFC 2487 - SMTP Service Extension for Secure SMTP over TLS
15  * RFC 2554 - SMTP Service Extension for Authentication
16  * RFC 2821 - Simple Mail Transfer Protocol
17  * RFC 2822 - Internet Message Format
18  * RFC 2920 - SMTP Service Extension for Command Pipelining
19  *  
20  * The VRFY and EXPN commands have been removed from this implementation
21  * because nobody uses these commands anymore, except for spammers.
22  *
23  * Copyright (c) 1998-2012 by the citadel.org team
24  *
25  *  This program is open source software; you can redistribute it and/or modify
26  *  it under the terms of the GNU General Public License version 3.
27  *  
28  *  
29  *
30  *  This program is distributed in the hope that it will be useful,
31  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
32  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
33  *  GNU General Public License for more details.
34  *
35  *  
36  *  
37  *  
38  */
39
40 #include "sysdep.h"
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <stdio.h>
44 #include <termios.h>
45 #include <fcntl.h>
46 #include <signal.h>
47 #include <pwd.h>
48 #include <errno.h>
49 #include <sys/types.h>
50 #include <syslog.h>
51
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
54 # include <time.h>
55 #else
56 # if HAVE_SYS_TIME_H
57 #  include <sys/time.h>
58 # else
59 #  include <time.h>
60 # endif
61 #endif
62 #include <sys/wait.h>
63 #include <ctype.h>
64 #include <string.h>
65 #include <limits.h>
66 #include <sys/socket.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <libcitadel.h>
70 #include "citadel.h"
71 #include "server.h"
72 #include "citserver.h"
73 #include "support.h"
74 #include "config.h"
75 #include "control.h"
76 #include "user_ops.h"
77 #include "database.h"
78 #include "msgbase.h"
79 #include "internet_addressing.h"
80 #include "genstamp.h"
81 #include "domain.h"
82 #include "clientsocket.h"
83 #include "locate_host.h"
84 #include "citadel_dirs.h"
85
86 #include "ctdl_module.h"
87
88 #include "smtpqueue.h"
89 #include "smtp_clienthandlers.h"
90 #include "event_client.h"
91
92
93 struct CitContext smtp_queue_CC;
94 pthread_mutex_t ActiveQItemsLock;
95 HashList *ActiveQItems  = NULL;
96 HashList *QItemHandlers = NULL;
97 const unsigned short DefaultMXPort = 25;
98 int max_sessions_for_outbound_smtp = 500; /* how many sessions might be active till we stop adding more smtp jobs */
99 int ndelay_count = 50; /* every n queued messages we will sleep... */
100 int delay_msec = 5000; /* this many seconds. */
101
102 static const long MaxRetry = SMTP_RETRY_INTERVAL * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2;
103 int MsgCount            = 0;
104 int run_queue_now       = 0;    /* Set to 1 to ignore SMTP send retry times */
105
106 void RegisterQItemHandler(const char *Key, long Len, QItemHandler H)
107 {
108         QItemHandlerStruct *HS = (QItemHandlerStruct*)malloc(sizeof(QItemHandlerStruct));
109         HS->H = H;
110         Put(QItemHandlers, Key, Len, HS, NULL);
111 }
112
113
114
115 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
116                               MailQEntry *MyQEntry,
117                               StrBuf *MsgText,
118 /* KeepMsgText allows us to use MsgText as ours. */
119                               int KeepMsgText,
120                               int MsgCount,
121                               ParsedURL *RelayUrls);
122
123
124 void smtp_evq_cleanup(void)
125 {
126
127         pthread_mutex_lock(&ActiveQItemsLock);
128         DeleteHash(&QItemHandlers);
129         DeleteHash(&ActiveQItems);
130         pthread_mutex_unlock(&ActiveQItemsLock);
131         pthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
132 /*      citthread_mutex_destroy(&ActiveQItemsLock); TODO */
133 }
134
135 int DecreaseQReference(OneQueItem *MyQItem)
136 {
137         int IDestructQueItem;
138
139         pthread_mutex_lock(&ActiveQItemsLock);
140         MyQItem->ActiveDeliveries--;
141         IDestructQueItem = MyQItem->ActiveDeliveries == 0;
142         pthread_mutex_unlock(&ActiveQItemsLock);
143         return IDestructQueItem;
144 }
145
146 void DecreaseShutdownDeliveries(OneQueItem *MyQItem)
147 {
148         pthread_mutex_lock(&ActiveQItemsLock);
149         MyQItem->NotYetShutdownDeliveries--;
150         pthread_mutex_unlock(&ActiveQItemsLock);
151 }
152
153 int GetShutdownDeliveries(OneQueItem *MyQItem)
154 {
155         int DestructNow;
156
157         pthread_mutex_lock(&ActiveQItemsLock);
158         DestructNow = MyQItem->ActiveDeliveries == 0;
159         pthread_mutex_unlock(&ActiveQItemsLock);
160         return DestructNow;
161 }
162 void RemoveQItem(OneQueItem *MyQItem)
163 {
164         long len;
165         const char* Key;
166         void *VData;
167         HashPos  *It;
168
169         pthread_mutex_lock(&ActiveQItemsLock);
170         It = GetNewHashPos(ActiveQItems, 0);
171         if (GetHashPosFromKey(ActiveQItems, LKEY(MyQItem->MessageID), It))
172                 DeleteEntryFromHash(ActiveQItems, It);
173         else
174         {
175                 SMTPC_syslog(LOG_WARNING,
176                              "unable to find QItem with ID[%ld]",
177                              MyQItem->MessageID);
178                 while (GetNextHashPos(ActiveQItems, It, &len, &Key, &VData))
179                         SMTPC_syslog(LOG_WARNING,
180                                      "have_: ID[%ld]",
181                                      ((OneQueItem *)VData)->MessageID);
182         }
183         pthread_mutex_unlock(&ActiveQItemsLock);
184         DeleteHashPos(&It);
185 }
186
187
188 void FreeMailQEntry(void *qv)
189 {
190         MailQEntry *Q = qv;
191 /*
192         SMTPC_syslog(LOG_DEBUG, "---------------%s--------------", __FUNCTION__);
193         cit_backtrace();
194 */
195         FreeStrBuf(&Q->Recipient);
196         FreeStrBuf(&Q->StatusMessage);
197         FreeStrBuf(&Q->AllStatusMessages);
198         memset(Q, 0, sizeof(MailQEntry));
199         free(Q);
200 }
201 void FreeQueItem(OneQueItem **Item)
202 {
203 /*
204         SMTPC_syslog(LOG_DEBUG, "---------------%s--------------", __FUNCTION__);
205         cit_backtrace();
206 */
207         DeleteHash(&(*Item)->MailQEntries);
208         FreeStrBuf(&(*Item)->EnvelopeFrom);
209         FreeStrBuf(&(*Item)->BounceTo);
210         FreeStrBuf(&(*Item)->SenderRoom);
211         FreeURL(&(*Item)->URL);
212         memset(*Item, 0, sizeof(OneQueItem));
213         free(*Item);
214         Item = NULL;
215 }
216 void HFreeQueItem(void *Item)
217 {
218         FreeQueItem((OneQueItem**)&Item);
219 }
220
221 /* inspect recipients with a status of:
222  * - 0 (no delivery yet attempted)
223  * - 3/4 (transient errors
224  *        were experienced and it's time to try again)
225  */
226 int CheckQEntryActive(MailQEntry *ThisItem)
227 {
228         if ((ThisItem->Status == 0) ||
229             (ThisItem->Status == 3) ||
230             (ThisItem->Status == 4))
231         {
232                 return 1;
233         }
234         else
235                 return 0;
236 }
237 int CheckQEntryIsBounce(MailQEntry *ThisItem)
238 {
239         if ((ThisItem->Status == 3) ||
240             (ThisItem->Status == 4) ||
241             (ThisItem->Status == 5))
242         {
243                 return 1;
244         }
245         else
246                 return 0;
247 }       
248
249 int CountActiveQueueEntries(OneQueItem *MyQItem, int before)
250 {
251         HashPos  *It;
252         long len;
253         long ActiveDeliveries;
254         const char *Key;
255         void *vQE;
256
257         ActiveDeliveries = 0;
258         It = GetNewHashPos(MyQItem->MailQEntries, 0);
259         while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
260         {
261                 int Active;
262                 MailQEntry *ThisItem = vQE;
263
264                 if (CheckQEntryActive(ThisItem))
265                 {
266                         ActiveDeliveries++;
267                         Active = 1;
268                 }
269                 else
270                         Active = 0;
271                 if (before)
272                         ThisItem->Active = Active;
273                 else
274                         ThisItem->StillActive = Active;
275         }
276         DeleteHashPos(&It);
277         return ActiveDeliveries;
278 }
279
280 OneQueItem *DeserializeQueueItem(StrBuf *RawQItem, long QueMsgID)
281 {
282         OneQueItem *Item;
283         const char *pLine = NULL;
284         StrBuf *Line;
285         StrBuf *Token;
286         void *v;
287
288         Item = (OneQueItem*)malloc(sizeof(OneQueItem));
289         memset(Item, 0, sizeof(OneQueItem));
290         Item->Retry = SMTP_RETRY_INTERVAL;
291         Item->MessageID = -1;
292         Item->QueMsgID = QueMsgID;
293
294         Token = NewStrBuf();
295         Line = NewStrBufPlain(NULL, 128);
296         while (pLine != StrBufNOTNULL) {
297                 const char *pItemPart = NULL;
298                 void *vHandler;
299
300                 StrBufExtract_NextToken(Line, RawQItem, &pLine, '\n');
301                 if (StrLength(Line) == 0) continue;
302                 StrBufExtract_NextToken(Token, Line, &pItemPart, '|');
303                 if (GetHash(QItemHandlers, SKEY(Token), &vHandler))
304                 {
305                         QItemHandlerStruct *HS;
306                         HS = (QItemHandlerStruct*) vHandler;
307                         HS->H(Item, Line, &pItemPart);
308                 }
309         }
310         FreeStrBuf(&Line);
311         FreeStrBuf(&Token);
312
313         if (Item->Retry >= MaxRetry)
314                 Item->FailNow = 1;
315
316         pthread_mutex_lock(&ActiveQItemsLock);
317         if (GetHash(ActiveQItems,
318                     LKEY(Item->MessageID),
319                     &v))
320         {
321                 /* WHOOPS. somebody else is already working on this. */
322                 pthread_mutex_unlock(&ActiveQItemsLock);
323                 FreeQueItem(&Item);
324                 return NULL;
325         }
326         else {
327                 /* mark our claim on this. */
328                 Put(ActiveQItems,
329                     LKEY(Item->MessageID),
330                     Item,
331                     HFreeQueItem);
332                 pthread_mutex_unlock(&ActiveQItemsLock);
333         }
334
335         return Item;
336 }
337
338 StrBuf *SerializeQueueItem(OneQueItem *MyQItem)
339 {
340         StrBuf *QMessage;
341         HashPos  *It;
342         const char *Key;
343         long len;
344         void *vQE;
345
346         QMessage = NewStrBufPlain(NULL, SIZ);
347         StrBufPrintf(QMessage, "Content-type: %s\n", SPOOLMIME);
348 //      "attempted|%ld\n"  "retry|%ld\n",, (long)time(NULL), (long)retry );
349         StrBufAppendBufPlain(QMessage, HKEY("\nmsgid|"), 0);
350         StrBufAppendPrintf(QMessage, "%ld", MyQItem->MessageID);
351
352         StrBufAppendBufPlain(QMessage, HKEY("\nsubmitted|"), 0);
353         StrBufAppendPrintf(QMessage, "%ld", MyQItem->Submitted);
354
355         if (StrLength(MyQItem->BounceTo) > 0) {
356                 StrBufAppendBufPlain(QMessage, HKEY("\nbounceto|"), 0);
357                 StrBufAppendBuf(QMessage, MyQItem->BounceTo, 0);
358         }
359
360         if (StrLength(MyQItem->EnvelopeFrom) > 0) {
361                 StrBufAppendBufPlain(QMessage, HKEY("\nenvelope_from|"), 0);
362                 StrBufAppendBuf(QMessage, MyQItem->EnvelopeFrom, 0);
363         }
364
365         if (StrLength(MyQItem->SenderRoom) > 0) {
366                 StrBufAppendBufPlain(QMessage, HKEY("\nsource_room|"), 0);
367                 StrBufAppendBuf(QMessage, MyQItem->SenderRoom, 0);
368         }
369
370         StrBufAppendBufPlain(QMessage, HKEY("\nretry|"), 0);
371         StrBufAppendPrintf(QMessage, "%ld",
372                            MyQItem->Retry);
373
374         StrBufAppendBufPlain(QMessage, HKEY("\nattempted|"), 0);
375         StrBufAppendPrintf(QMessage, "%ld",
376                            time(NULL) /*ctdl_ev_now()*/ + MyQItem->Retry);
377
378         It = GetNewHashPos(MyQItem->MailQEntries, 0);
379         while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
380         {
381                 MailQEntry *ThisItem = vQE;
382
383                 StrBufAppendBufPlain(QMessage, HKEY("\nremote|"), 0);
384                 StrBufAppendBuf(QMessage, ThisItem->Recipient, 0);
385                 StrBufAppendBufPlain(QMessage, HKEY("|"), 0);
386                 StrBufAppendPrintf(QMessage, "%d", ThisItem->Status);
387                 StrBufAppendBufPlain(QMessage, HKEY("|"), 0);
388                 if (ThisItem->AllStatusMessages != NULL)
389                         StrBufAppendBuf(QMessage, ThisItem->AllStatusMessages, 0);
390                 else
391                         StrBufAppendBuf(QMessage, ThisItem->StatusMessage, 0);
392         }
393         DeleteHashPos(&It);
394         StrBufAppendBufPlain(QMessage, HKEY("\n"), 0);
395         return QMessage;
396 }
397
398
399
400
401
402 void NewMailQEntry(OneQueItem *Item)
403 {
404         Item->Current = (MailQEntry*) malloc(sizeof(MailQEntry));
405         memset(Item->Current, 0, sizeof(MailQEntry));
406
407         if (Item->MailQEntries == NULL)
408                 Item->MailQEntries = NewHash(1, Flathash);
409         /* alocate big buffer so we won't get problems reallocating later. */
410         Item->Current->StatusMessage = NewStrBufPlain(NULL, SIZ);
411         Item->Current->n = GetCount(Item->MailQEntries);
412         Put(Item->MailQEntries,
413             IKEY(Item->Current->n),
414             Item->Current,
415             FreeMailQEntry);
416 }
417
418 void QItem_Handle_MsgID(OneQueItem *Item, StrBuf *Line, const char **Pos)
419 {
420         Item->MessageID = StrBufExtractNext_long(Line, Pos, '|');
421 }
422
423 void QItem_Handle_EnvelopeFrom(OneQueItem *Item, StrBuf *Line, const char **Pos)
424 {
425         if (Item->EnvelopeFrom == NULL)
426                 Item->EnvelopeFrom = NewStrBufPlain(NULL, StrLength(Line));
427         StrBufExtract_NextToken(Item->EnvelopeFrom, Line, Pos, '|');
428 }
429
430 void QItem_Handle_BounceTo(OneQueItem *Item, StrBuf *Line, const char **Pos)
431 {
432         if (Item->BounceTo == NULL)
433                 Item->BounceTo = NewStrBufPlain(NULL, StrLength(Line));
434         StrBufExtract_NextToken(Item->BounceTo, Line, Pos, '|');
435 }
436
437 void QItem_Handle_SenderRoom(OneQueItem *Item, StrBuf *Line, const char **Pos)
438 {
439         if (Item->SenderRoom == NULL)
440                 Item->SenderRoom = NewStrBufPlain(NULL, StrLength(Line));
441         StrBufExtract_NextToken(Item->SenderRoom, Line, Pos, '|');
442 }
443
444 void QItem_Handle_Recipient(OneQueItem *Item, StrBuf *Line, const char **Pos)
445 {
446         if (Item->Current == NULL)
447                 NewMailQEntry(Item);
448         if (Item->Current->Recipient == NULL)
449                 Item->Current->Recipient=NewStrBufPlain(NULL, StrLength(Line));
450         StrBufExtract_NextToken(Item->Current->Recipient, Line, Pos, '|');
451         Item->Current->Status = StrBufExtractNext_int(Line, Pos, '|');
452         StrBufExtract_NextToken(Item->Current->StatusMessage, Line, Pos, '|');
453         Item->Current = NULL; // TODO: is this always right?
454 }
455
456
457 void QItem_Handle_retry(OneQueItem *Item, StrBuf *Line, const char **Pos)
458 {
459         Item->Retry =
460                 StrBufExtractNext_int(Line, Pos, '|');
461         if (Item->Retry == 0)
462                 Item->Retry = SMTP_RETRY_INTERVAL;
463         else
464                 Item->Retry *= 2;
465 }
466
467
468 void QItem_Handle_Submitted(OneQueItem *Item, StrBuf *Line, const char **Pos)
469 {
470         Item->Submitted = atol(*Pos);
471
472 }
473
474 void QItem_Handle_Attempted(OneQueItem *Item, StrBuf *Line, const char **Pos)
475 {
476         Item->ReattemptWhen = StrBufExtractNext_int(Line, Pos, '|');
477 }
478
479
480
481 /**
482  * this one has to have the context for loading the message via the redirect buffer...
483  */
484 StrBuf *smtp_load_msg(OneQueItem *MyQItem, int n, char **Author, char **Address)
485 {
486         CitContext *CCC=CC;
487         StrBuf *SendMsg;
488
489         CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
490         CtdlOutputMsg(MyQItem->MessageID,
491                       MT_RFC822, HEADERS_ALL,
492                       0, 1, NULL,
493                       (ESC_DOT|SUPPRESS_ENV_TO),
494                       Author,
495                       Address,
496                         NULL);
497
498         SendMsg = CCC->redirect_buffer;
499         CCC->redirect_buffer = NULL;
500         if ((StrLength(SendMsg) > 0) &&
501             ChrPtr(SendMsg)[StrLength(SendMsg) - 1] != '\n') {
502                 SMTPC_syslog(LOG_WARNING,
503                              "[%d] Possible problem: message did not "
504                              "correctly terminate. (expecting 0x10, got 0x%02x)\n",
505                              MsgCount, //yes uncool, but best choice here...
506                              ChrPtr(SendMsg)[StrLength(SendMsg) - 1] );
507                 StrBufAppendBufPlain(SendMsg, HKEY("\r\n"), 0);
508         }
509         return SendMsg;
510 }
511
512
513
514 /*
515  * smtp_do_bounce() is caled by smtp_do_procmsg() to scan a set of delivery
516  * instructions for "5" codes (permanent fatal errors) and produce/deliver
517  * a "bounce" message (delivery status notification).
518  */
519 void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt, ParsedURL *Relay)
520 {
521         static int seq = 0;
522         
523         struct CtdlMessage *bmsg = NULL;
524         StrBuf *boundary;
525         StrBuf *Msg = NULL;
526         StrBuf *BounceMB;
527         recptypes *valid;
528         time_t now;
529
530         HashPos *It;
531         void *vQE;
532         long len;
533         const char *Key;
534
535         int first_attempt = 0;
536         int successful_bounce = 0;
537         int num_bounces = 0;
538         int give_up = 0;
539
540         SMTPCM_syslog(LOG_DEBUG, "smtp_do_bounce() called\n");
541
542         if (MyQItem->SendBounceMail == 0)
543                 return;
544
545         now = time (NULL); //ev_time();
546
547         if ( (now - MyQItem->Submitted) > SMTP_GIVE_UP ) {
548                 give_up = 1;
549         }
550
551         if (MyQItem->Retry == SMTP_RETRY_INTERVAL) {
552                 first_attempt = 1;
553         }
554
555         /*
556          * Now go through the instructions checking for stuff.
557          */
558         Msg = NewStrBufPlain(NULL, 1024);
559         It = GetNewHashPos(MyQItem->MailQEntries, 0);
560         while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
561         {
562                 MailQEntry *ThisItem = vQE;
563                 if ((ThisItem->Active && (ThisItem->Status == 5)) || /* failed now? */
564                     ((give_up == 1) && (ThisItem->Status != 2)) ||
565                     ((first_attempt == 1) && (ThisItem->Status != 2)))
566                         /* giving up after failed attempts... */
567                 {
568                         ++num_bounces;
569
570                         StrBufAppendBufPlain(Msg, HKEY(" "), 0);
571                         StrBufAppendBuf(Msg, ThisItem->Recipient, 0);
572                         StrBufAppendBufPlain(Msg, HKEY(": "), 0);
573                         if (ThisItem->AllStatusMessages != NULL)
574                                 StrBufAppendBuf(Msg, ThisItem->AllStatusMessages, 0);
575                         else
576                                 StrBufAppendBuf(Msg, ThisItem->StatusMessage, 0);
577                         StrBufAppendBufPlain(Msg, HKEY("\r\n"), 0);
578                 }
579         }
580         DeleteHashPos(&It);
581
582         /* Deliver the bounce if there's anything worth mentioning */
583         SMTPC_syslog(LOG_DEBUG, "num_bounces = %d\n", num_bounces);
584
585         if (num_bounces == 0) {
586                 FreeStrBuf(&Msg);
587                 return;
588         }
589
590         if ((StrLength(MyQItem->SenderRoom) == 0) && MyQItem->HaveRelay) {
591                 const char *RelayUrlStr = "[not found]";
592                 /* one message that relaying is broken is enough; no extra room error message. */
593                 StrBuf *RelayDetails = NewStrBuf();
594
595                 if (Relay != NULL)
596                         RelayUrlStr = ChrPtr(Relay->URL);
597
598                 StrBufPrintf(RelayDetails,
599                              "Relaying via %s failed permanently. \n Reason:\n%s\n Revalidate your relay configuration.",
600                              RelayUrlStr,
601                              ChrPtr(Msg));
602                 CtdlAideMessage(ChrPtr(RelayDetails), "Relaying Failed");
603                 FreeStrBuf(&RelayDetails);
604         }
605
606         boundary = NewStrBufPlain(HKEY("=_Citadel_Multipart_"));
607         StrBufAppendPrintf(boundary,
608                            "%s_%04x%04x",
609                            config.c_fqdn,
610                            getpid(),
611                            ++seq);
612
613         /* Start building our bounce message; go shopping for memory first. */
614         BounceMB = NewStrBufPlain(
615                 NULL,
616                 1024 + /* mime stuff.... */
617                 StrLength(Msg) +  /* the bounce information... */
618                 StrLength(OMsgTxt)); /* the original message */
619         if (BounceMB == NULL) {
620                 FreeStrBuf(&boundary);
621                 SMTPCM_syslog(LOG_ERR, "Failed to alloc() bounce message.\n");
622
623                 return;
624         }
625
626         bmsg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
627         if (bmsg == NULL) {
628                 FreeStrBuf(&boundary);
629                 FreeStrBuf(&BounceMB);
630                 SMTPCM_syslog(LOG_ERR, "Failed to alloc() bounce message.\n");
631
632                 return;
633         }
634         memset(bmsg, 0, sizeof(struct CtdlMessage));
635
636
637         StrBufAppendBufPlain(BounceMB, HKEY("Content-type: multipart/mixed; boundary=\""), 0);
638         StrBufAppendBuf(BounceMB, boundary, 0);
639         StrBufAppendBufPlain(BounceMB, HKEY("\"\r\n"), 0);
640         StrBufAppendBufPlain(BounceMB, HKEY("MIME-Version: 1.0\r\n"), 0);
641         StrBufAppendBufPlain(BounceMB, HKEY("X-Mailer: " CITADEL "\r\n"), 0);
642         StrBufAppendBufPlain(BounceMB, HKEY("\r\nThis is a multipart message in MIME format.\r\n\r\n"), 0);
643         StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
644         StrBufAppendBuf(BounceMB, boundary, 0);
645         StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
646         StrBufAppendBufPlain(BounceMB, HKEY("Content-type: text/plain\r\n\r\n"), 0);
647
648         if (give_up)
649                 StrBufAppendBufPlain(
650                         BounceMB,
651                         HKEY(
652                                 "A message you sent could not be delivered "
653                                 "to some or all of its recipients\n"
654                                 "due to prolonged unavailability "
655                                 "of its destination(s).\n"
656                                 "Giving up on the following addresses:\n\n"
657                                 ), 0);
658         else
659                 StrBufAppendBufPlain(
660                         BounceMB,
661                         HKEY(
662                                 "A message you sent could not be delivered "
663                                 "to some or all of its recipients.\n"
664                                 "The following addresses "
665                                 "were undeliverable:\n\n"
666                                 ), 0);
667
668         StrBufAppendBuf(BounceMB, Msg, 0);
669         FreeStrBuf(&Msg);
670
671         if (StrLength(MyQItem->SenderRoom) > 0)
672         {
673                 StrBufAppendBufPlain(
674                         BounceMB,
675                         HKEY("The message was originaly posted in: "), 0);
676                 StrBufAppendBuf(BounceMB, MyQItem->SenderRoom, 0);
677                 StrBufAppendBufPlain(
678                         BounceMB,
679                         HKEY("\n"), 0);
680         }
681
682         /* Attach the original message */
683         StrBufAppendBufPlain(BounceMB, HKEY("\r\n--"), 0);
684         StrBufAppendBuf(BounceMB, boundary, 0);
685         StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
686         StrBufAppendBufPlain(BounceMB,
687                              HKEY("Content-type: message/rfc822\r\n"), 0);
688         StrBufAppendBufPlain(BounceMB,
689                              HKEY("Content-Transfer-Encoding: 7bit\r\n"), 0);
690         StrBufAppendBufPlain(BounceMB,
691                              HKEY("Content-Disposition: inline\r\n"), 0);
692         StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
693         StrBufAppendBuf(BounceMB, OMsgTxt, 0);
694
695         /* Close the multipart MIME scope */
696         StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
697         StrBufAppendBuf(BounceMB, boundary, 0);
698         StrBufAppendBufPlain(BounceMB, HKEY("--\r\n"), 0);
699
700         bmsg->cm_magic = CTDLMESSAGE_MAGIC;
701         bmsg->cm_anon_type = MES_NORMAL;
702         bmsg->cm_format_type = FMT_RFC822;
703
704         CM_SetField(bmsg, eOriginalRoom, HKEY(MAILROOM));
705         CM_SetField(bmsg, eAuthor, HKEY("Citadel"));
706         CM_SetField(bmsg, eNodeName, CFG_KEY(c_nodename));
707         CM_SetField(bmsg, eMsgSubject, HKEY("Delivery Status Notification (Failure)"));
708         CM_SetAsFieldSB(bmsg, eMesageText, &BounceMB);
709
710         /* First try the user who sent the message */
711         if (StrLength(MyQItem->BounceTo) == 0) {
712                 SMTPCM_syslog(LOG_ERR, "No bounce address specified\n");
713         }
714         else {
715                 SMTPC_syslog(LOG_DEBUG, "bounce to user? <%s>\n",
716                        ChrPtr(MyQItem->BounceTo));
717         }
718
719         /* Can we deliver the bounce to the original sender? */
720         valid = validate_recipients(ChrPtr(MyQItem->BounceTo), NULL, 0);
721         if ((valid != NULL) && (valid->num_error == 0)) {
722                 CtdlSubmitMsg(bmsg, valid, "", QP_EADDR);
723                 successful_bounce = 1;
724         }
725
726         /* If not, post it in the Aide> room */
727         if (successful_bounce == 0) {
728                 CtdlSubmitMsg(bmsg, NULL, config.c_aideroom, QP_EADDR);
729         }
730
731         /* Free up the memory we used */
732         free_recipients(valid);
733         FreeStrBuf(&boundary);
734         CM_Free(bmsg);
735         SMTPCM_syslog(LOG_DEBUG, "Done processing bounces\n");
736 }
737
738 ParsedURL *LoadRelayUrls(OneQueItem *MyQItem,
739                          char *Author,
740                          char *Address)
741 {
742         int nRelays = 0;
743         ParsedURL *RelayUrls = NULL;
744         char mxbuf[SIZ];
745         ParsedURL **Url = &MyQItem->URL;
746
747         nRelays = get_hosts(mxbuf, "fallbackhost");
748         if (nRelays > 0) {
749                 StrBuf *All;
750                 StrBuf *One;
751                 const char *Pos = NULL;
752                 All = NewStrBufPlain(mxbuf, -1);
753                 One = NewStrBufPlain(NULL, StrLength(All) + 1);
754                 
755                 while ((Pos != StrBufNOTNULL) &&
756                        ((Pos == NULL) ||
757                         !IsEmptyStr(Pos)))
758                 {
759                         StrBufExtract_NextToken(One, All, &Pos, '|');
760                         if (!ParseURL(Url, One, DefaultMXPort)) {
761                                 SMTPC_syslog(LOG_DEBUG,
762                                              "Failed to parse: %s\n",
763                                              ChrPtr(One));
764                         }
765                         else {
766                                 (*Url)->IsRelay = 1;
767                                 MyQItem->HaveRelay = 1;
768                         }
769                 }
770                 FreeStrBuf(&All);
771                 FreeStrBuf(&One);
772         }
773         nRelays = get_hosts(mxbuf, "smarthost");
774         if (nRelays > 0) {
775                 char *User;
776                 StrBuf *All;
777                 StrBuf *One;
778                 const char *Pos = NULL;
779                 All = NewStrBufPlain(mxbuf, -1);
780                 One = NewStrBufPlain(NULL, StrLength(All) + 1);
781                 
782                 while ((Pos != StrBufNOTNULL) &&
783                        ((Pos == NULL) ||
784                         !IsEmptyStr(Pos)))
785                 {
786                         StrBufExtract_NextToken(One, All, &Pos, '|');
787                         User = strchr(ChrPtr(One), ' ');
788                         if (User != NULL) {
789                                 if (!strcmp(User + 1, Author) ||
790                                     !strcmp(User + 1, Address))
791                                         StrBufCutAt(One, 0, User);
792                                 else {
793                                         MyQItem->HaveRelay = 1;
794                                         continue;
795                                 }
796                         }
797                         if (!ParseURL(Url, One, DefaultMXPort)) {
798                                 SMTPC_syslog(LOG_DEBUG,
799                                              "Failed to parse: %s\n",
800                                              ChrPtr(One));
801                         }
802                         else {
803                                 ///if (!Url->IsIP)) // todo dupe me fork ipv6
804                                 (*Url)->IsRelay = 1;
805                                 MyQItem->HaveRelay = 1;
806                         }
807                 }
808                 FreeStrBuf(&All);
809                 FreeStrBuf(&One);
810         }
811         return RelayUrls;
812 }
813 /*
814  * smtp_do_procmsg()
815  *
816  * Called by smtp_do_queue() to handle an individual message.
817  */
818 void smtp_do_procmsg(long msgnum, void *userdata) {
819         time_t now;
820         int mynumsessions = num_sessions;
821         struct CtdlMessage *msg = NULL;
822         char *Author = NULL;
823         char *Address = NULL;
824         char *instr = NULL;
825         StrBuf *PlainQItem;
826         OneQueItem *MyQItem;
827         char *pch;
828         HashPos  *It;
829         void *vQE;
830         long len;
831         const char *Key;
832         int HaveBuffers = 0;
833         StrBuf *Msg =NULL;
834
835         if (mynumsessions > max_sessions_for_outbound_smtp) {
836                 SMTPC_syslog(LOG_INFO,
837                              "skipping because of num jobs %d > %d max_sessions_for_outbound_smtp",
838                              mynumsessions,
839                              max_sessions_for_outbound_smtp);
840         }
841
842         SMTPC_syslog(LOG_DEBUG, "smtp_do_procmsg(%ld)\n", msgnum);
843         ///strcpy(envelope_from, "");
844
845         msg = CtdlFetchMessage(msgnum, 1);
846         if (msg == NULL) {
847                 SMTPC_syslog(LOG_ERR, "tried %ld but no such message!\n",
848                        msgnum);
849                 return;
850         }
851
852         pch = instr = msg->cm_fields[eMesageText];
853
854         /* Strip out the headers (no not amd any other non-instruction) line */
855         while (pch != NULL) {
856                 pch = strchr(pch, '\n');
857                 if ((pch != NULL) &&
858                     ((*(pch + 1) == '\n') ||
859                      (*(pch + 1) == '\r')))
860                 {
861                         instr = pch + 2;
862                         pch = NULL;
863                 }
864         }
865         PlainQItem = NewStrBufPlain(instr, -1);
866         CM_Free(msg);
867         MyQItem = DeserializeQueueItem(PlainQItem, msgnum);
868         FreeStrBuf(&PlainQItem);
869
870         if (MyQItem == NULL) {
871                 SMTPC_syslog(LOG_ERR,
872                              "Msg No %ld: already in progress!\n",
873                              msgnum);
874                 return; /* s.b. else is already processing... */
875         }
876
877         /*
878          * Postpone delivery if we've already tried recently.
879          */
880         now = time(NULL);
881         if ((MyQItem->ReattemptWhen != 0) && 
882             (now < MyQItem->ReattemptWhen) &&
883             (run_queue_now == 0))
884         {
885                 SMTPC_syslog(LOG_DEBUG, 
886                              "Retry time not yet reached. %ld seconds left.",
887                              MyQItem->ReattemptWhen - now);
888
889                 It = GetNewHashPos(MyQItem->MailQEntries, 0);
890                 pthread_mutex_lock(&ActiveQItemsLock);
891                 {
892                         if (GetHashPosFromKey(ActiveQItems,
893                                               LKEY(MyQItem->MessageID),
894                                               It))
895                         {
896                                 DeleteEntryFromHash(ActiveQItems, It);
897                         }
898                 }
899                 pthread_mutex_unlock(&ActiveQItemsLock);
900                 ////FreeQueItem(&MyQItem); TODO: DeleteEntryFromHash frees this?
901                 DeleteHashPos(&It);
902                 return;
903         }
904
905         /*
906          * Bail out if there's no actual message associated with this
907          */
908         if (MyQItem->MessageID < 0L) {
909                 SMTPCM_syslog(LOG_ERR, "no 'msgid' directive found!\n");
910                 It = GetNewHashPos(MyQItem->MailQEntries, 0);
911                 pthread_mutex_lock(&ActiveQItemsLock);
912                 {
913                         if (GetHashPosFromKey(ActiveQItems,
914                                               LKEY(MyQItem->MessageID),
915                                               It))
916                         {
917                                 DeleteEntryFromHash(ActiveQItems, It);
918                         }
919                 }
920                 pthread_mutex_unlock(&ActiveQItemsLock);
921                 DeleteHashPos(&It);
922                 ////FreeQueItem(&MyQItem); TODO: DeleteEntryFromHash frees this?
923                 return;
924         }
925
926
927         It = GetNewHashPos(MyQItem->MailQEntries, 0);
928         while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
929         {
930                 MailQEntry *ThisItem = vQE;
931                 SMTPC_syslog(LOG_DEBUG, "SMTP Queue: Task: <%s> %d\n",
932                              ChrPtr(ThisItem->Recipient),
933                              ThisItem->Active);
934         }
935         DeleteHashPos(&It);
936
937         MyQItem->NotYetShutdownDeliveries = 
938                 MyQItem->ActiveDeliveries = CountActiveQueueEntries(MyQItem, 1);
939
940         /* failsafe against overload: 
941          * will we exceed the limit set? 
942          */
943         if ((MyQItem->ActiveDeliveries + mynumsessions > max_sessions_for_outbound_smtp) && 
944             /* if yes, did we reach more than half of the quota? */
945             ((mynumsessions * 2) > max_sessions_for_outbound_smtp) && 
946             /* if... would we ever fit into half of the quota?? */
947             (((MyQItem->ActiveDeliveries * 2)  < max_sessions_for_outbound_smtp)))
948         {
949                 /* abort delivery for another time. */
950                 SMTPC_syslog(LOG_INFO,
951                              "SMTP Queue: skipping because of num jobs %d + %ld > %d max_sessions_for_outbound_smtp",
952                              mynumsessions,
953                              MyQItem->ActiveDeliveries,
954                              max_sessions_for_outbound_smtp);
955
956                 It = GetNewHashPos(MyQItem->MailQEntries, 0);
957                 pthread_mutex_lock(&ActiveQItemsLock);
958                 {
959                         if (GetHashPosFromKey(ActiveQItems,
960                                               LKEY(MyQItem->MessageID),
961                                               It))
962                         {
963                                 DeleteEntryFromHash(ActiveQItems, It);
964                         }
965                 }
966                 pthread_mutex_unlock(&ActiveQItemsLock);
967
968                 return;
969         }
970
971
972         if (MyQItem->ActiveDeliveries > 0)
973         {
974                 ParsedURL *RelayUrls = NULL;
975                 int nActivated = 0;
976                 int n = MsgCount++;
977                 int m = MyQItem->ActiveDeliveries;
978                 int i = 1;
979
980                 It = GetNewHashPos(MyQItem->MailQEntries, 0);
981
982                 Msg = smtp_load_msg(MyQItem, n, &Author, &Address);
983                 RelayUrls = LoadRelayUrls(MyQItem, Author, Address);
984                 if ((RelayUrls == NULL) && MyQItem->HaveRelay) {
985
986                         while ((i <= m) &&
987                                (GetNextHashPos(MyQItem->MailQEntries,
988                                                It, &len, &Key, &vQE)))
989                         {
990                                 int KeepBuffers = (i == m);
991                                 MailQEntry *ThisItem = vQE;
992                                 StrBufPrintf(ThisItem->StatusMessage,
993                                              "No relay configured matching %s / %s", 
994                                              (Author != NULL)? Author : "",
995                                              (Address != NULL)? Address : "");
996                                 ThisItem->Status = 5;
997
998                                 nActivated++;
999
1000                                 if (i > 1) n = MsgCount++;
1001                                 syslog(LOG_INFO,
1002                                        "SMTPC: giving up on <%ld> <%s> %d / %d \n",
1003                                        MyQItem->MessageID,
1004                                        ChrPtr(ThisItem->Recipient),
1005                                        i,
1006                                        m);
1007                                 (*((int*) userdata)) ++;
1008                                 smtp_try_one_queue_entry(MyQItem,
1009                                                          ThisItem,
1010                                                          Msg,
1011                                                          KeepBuffers,
1012                                                          n,
1013                                                          RelayUrls);
1014
1015                                 if (KeepBuffers) HaveBuffers++;
1016
1017                                 i++;
1018                         }
1019                         if (Author != NULL) free (Author);
1020                         if (Address != NULL) free (Address);
1021                         DeleteHashPos(&It);
1022
1023                         return;
1024                 }
1025                 if (Author != NULL) free (Author);
1026                 if (Address != NULL) free (Address);
1027
1028                 while ((i <= m) &&
1029                        (GetNextHashPos(MyQItem->MailQEntries,
1030                                        It, &len, &Key, &vQE)))
1031                 {
1032                         MailQEntry *ThisItem = vQE;
1033
1034                         if (ThisItem->Active == 1)
1035                         {
1036                                 int KeepBuffers = (i == m);
1037
1038                                 nActivated++;
1039                                 if (nActivated % ndelay_count == 0)
1040                                         usleep(delay_msec);
1041
1042                                 if (i > 1) n = MsgCount++;
1043                                 syslog(LOG_DEBUG,
1044                                        "SMTPC: Trying <%ld> <%s> %d / %d \n",
1045                                        MyQItem->MessageID,
1046                                        ChrPtr(ThisItem->Recipient),
1047                                        i,
1048                                        m);
1049                                 (*((int*) userdata)) ++;
1050                                 smtp_try_one_queue_entry(MyQItem,
1051                                                          ThisItem,
1052                                                          Msg,
1053                                                          KeepBuffers,
1054                                                          n,
1055                                                          RelayUrls);
1056
1057                                 if (KeepBuffers) HaveBuffers++;
1058
1059                                 i++;
1060                         }
1061                 }
1062                 DeleteHashPos(&It);
1063         }
1064         else
1065         {
1066                 It = GetNewHashPos(MyQItem->MailQEntries, 0);
1067                 pthread_mutex_lock(&ActiveQItemsLock);
1068                 {
1069                         if (GetHashPosFromKey(ActiveQItems,
1070                                               LKEY(MyQItem->MessageID),
1071                                               It))
1072                         {
1073                                 DeleteEntryFromHash(ActiveQItems, It);
1074                         }
1075                         else
1076                         {
1077                                 long len;
1078                                 const char* Key;
1079                                 void *VData;
1080
1081                                 SMTPC_syslog(LOG_WARNING,
1082                                              "unable to find QItem with ID[%ld]",
1083                                              MyQItem->MessageID);
1084                                 while (GetNextHashPos(ActiveQItems,
1085                                                       It,
1086                                                       &len,
1087                                                       &Key,
1088                                                       &VData))
1089                                 {
1090                                         SMTPC_syslog(LOG_WARNING,
1091                                                      "have: ID[%ld]",
1092                                                      ((OneQueItem *)VData)->MessageID);
1093                                 }
1094                         }
1095
1096                 }
1097                 pthread_mutex_unlock(&ActiveQItemsLock);
1098                 DeleteHashPos(&It);
1099                 ////FreeQueItem(&MyQItem); TODO: DeleteEntryFromHash frees this?
1100
1101 // TODO: bounce & delete?
1102
1103         }
1104         if (!HaveBuffers) {
1105                 FreeStrBuf (&Msg);
1106 // TODO : free RelayUrls
1107         }
1108 }
1109
1110
1111
1112 /*
1113  * smtp_queue_thread()
1114  *
1115  * Run through the queue sending out messages.
1116  */
1117 void smtp_do_queue(void) {
1118         int num_processed = 0;
1119         int num_activated = 0;
1120
1121         pthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
1122         SMTPCM_syslog(LOG_INFO, "processing outbound queue");
1123
1124         if (CtdlGetRoom(&CC->room, SMTP_SPOOLOUT_ROOM) != 0) {
1125                 SMTPC_syslog(LOG_ERR, "Cannot find room <%s>", SMTP_SPOOLOUT_ROOM);
1126         }
1127         else {
1128                 num_processed = CtdlForEachMessage(MSGS_ALL,
1129                                                    0L,
1130                                                    NULL,
1131                                                    SPOOLMIME,
1132                                                    NULL,
1133                                                    smtp_do_procmsg,
1134                                                    &num_activated);
1135         }
1136         SMTPC_syslog(LOG_INFO,
1137                      "queue run completed; %d messages processed %d activated",
1138                      num_processed, num_activated);
1139
1140 }
1141
1142
1143
1144 /*
1145  * Initialize the SMTP outbound queue
1146  */
1147 void smtp_init_spoolout(void) {
1148         struct ctdlroom qrbuf;
1149
1150         /*
1151          * Create the room.  This will silently fail if the room already
1152          * exists, and that's perfectly ok, because we want it to exist.
1153          */
1154         CtdlCreateRoom(SMTP_SPOOLOUT_ROOM, 3, "", 0, 1, 0, VIEW_QUEUE);
1155
1156         /*
1157          * Make sure it's set to be a "system room" so it doesn't show up
1158          * in the <K>nown rooms list for Aides.
1159          */
1160         if (CtdlGetRoomLock(&qrbuf, SMTP_SPOOLOUT_ROOM) == 0) {
1161                 qrbuf.QRflags2 |= QR2_SYSTEM;
1162                 CtdlPutRoomLock(&qrbuf);
1163         }
1164 }
1165
1166
1167
1168
1169 /*****************************************************************************/
1170 /*                          SMTP UTILITY COMMANDS                            */
1171 /*****************************************************************************/
1172
1173 void cmd_smtp(char *argbuf) {
1174         char cmd[64];
1175         char node[256];
1176         char buf[1024];
1177         int i;
1178         int num_mxhosts;
1179
1180         if (CtdlAccessCheck(ac_aide)) return;
1181
1182         extract_token(cmd, argbuf, 0, '|', sizeof cmd);
1183
1184         if (!strcasecmp(cmd, "mx")) {
1185                 extract_token(node, argbuf, 1, '|', sizeof node);
1186                 num_mxhosts = getmx(buf, node);
1187                 cprintf("%d %d MX hosts listed for %s\n",
1188                         LISTING_FOLLOWS, num_mxhosts, node);
1189                 for (i=0; i<num_mxhosts; ++i) {
1190                         extract_token(node, buf, i, '|', sizeof node);
1191                         cprintf("%s\n", node);
1192                 }
1193                 cprintf("000\n");
1194                 return;
1195         }
1196
1197         else if (!strcasecmp(cmd, "runqueue")) {
1198                 run_queue_now = 1;
1199                 cprintf("%d All outbound SMTP will be retried now.\n", CIT_OK);
1200                 return;
1201         }
1202
1203         else {
1204                 cprintf("%d Invalid command.\n", ERROR + ILLEGAL_VALUE);
1205         }
1206
1207 }
1208
1209 int smtp_aftersave(struct CtdlMessage *msg,
1210                    recptypes *recps)
1211 {
1212         /* For internet mail, generate delivery instructions.
1213          * Yes, this is recursive.  Deal with it.  Infinite recursion does
1214          * not happen because the delivery instructions message does not
1215          * contain a recipient.
1216          */
1217         if ((recps != NULL) && (recps->num_internet > 0)) {
1218                 struct CtdlMessage *imsg = NULL;
1219                 char recipient[SIZ];
1220                 CitContext *CCC = MyContext();
1221                 StrBuf *SpoolMsg = NewStrBuf();
1222                 long nTokens;
1223                 int i;
1224
1225                 MSGM_syslog(LOG_DEBUG, "Generating delivery instructions\n");
1226
1227                 StrBufPrintf(SpoolMsg,
1228                              "Content-type: "SPOOLMIME"\n"
1229                              "\n"
1230                              "msgid|%s\n"
1231                              "submitted|%ld\n"
1232                              "bounceto|%s\n",
1233                              msg->cm_fields[eVltMsgNum],
1234                              (long)time(NULL),
1235                              recps->bounce_to);
1236
1237                 if (recps->envelope_from != NULL) {
1238                         StrBufAppendBufPlain(SpoolMsg, HKEY("envelope_from|"), 0);
1239                         StrBufAppendBufPlain(SpoolMsg, recps->envelope_from, -1, 0);
1240                         StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
1241                 }
1242                 if (recps->sending_room != NULL) {
1243                         StrBufAppendBufPlain(SpoolMsg, HKEY("source_room|"), 0);
1244                         StrBufAppendBufPlain(SpoolMsg, recps->sending_room, -1, 0);
1245                         StrBufAppendBufPlain(SpoolMsg, HKEY("\n"), 0);
1246                 }
1247
1248                 nTokens = num_tokens(recps->recp_internet, '|');
1249                 for (i = 0; i < nTokens; i++) {
1250                         long len;
1251                         len = extract_token(recipient, recps->recp_internet, i, '|', sizeof recipient);
1252                         if (len > 0) {
1253                                 StrBufAppendBufPlain(SpoolMsg, HKEY("remote|"), 0);
1254                                 StrBufAppendBufPlain(SpoolMsg, recipient, len, 0);
1255                                 StrBufAppendBufPlain(SpoolMsg, HKEY("|0||\n"), 0);
1256                         }
1257                 }
1258
1259                 imsg = malloc(sizeof(struct CtdlMessage));
1260                 memset(imsg, 0, sizeof(struct CtdlMessage));
1261                 imsg->cm_magic = CTDLMESSAGE_MAGIC;
1262                 imsg->cm_anon_type = MES_NORMAL;
1263                 imsg->cm_format_type = FMT_RFC822;
1264                 CM_SetField(imsg, eMsgSubject, HKEY("QMSG"));
1265                 CM_SetField(imsg, eAuthor, HKEY("Citadel"));
1266                 CM_SetField(imsg, eJournal, HKEY("do not journal"));
1267                 CM_SetAsFieldSB(imsg, eMesageText, &SpoolMsg);
1268                 CtdlSubmitMsg(imsg, NULL, SMTP_SPOOLOUT_ROOM, QP_EADDR);
1269                 CM_Free(imsg);
1270         }
1271         return 0;
1272 }
1273
1274 CTDL_MODULE_INIT(smtp_queu)
1275 {
1276         char *pstr;
1277
1278         if (!threading)
1279         {
1280                 pstr = getenv("CITSERVER_n_session_max");
1281                 if ((pstr != NULL) && (*pstr != '\0'))
1282                         max_sessions_for_outbound_smtp = atol(pstr); /* how many sessions might be active till we stop adding more smtp jobs */
1283
1284                 pstr = getenv("CITSERVER_smtp_n_delay_count");
1285                 if ((pstr != NULL) && (*pstr != '\0'))
1286                         ndelay_count = atol(pstr); /* every n queued messages we will sleep... */
1287
1288                 pstr = getenv("CITSERVER_smtp_delay");
1289                 if ((pstr != NULL) && (*pstr != '\0'))
1290                         delay_msec = atol(pstr) * 1000; /* this many seconds. */
1291
1292                 CtdlRegisterMessageHook(smtp_aftersave, EVT_AFTERSAVE);
1293
1294                 CtdlFillSystemContext(&smtp_queue_CC, "SMTP_Send");
1295                 ActiveQItems = NewHash(1, lFlathash);
1296                 pthread_mutex_init(&ActiveQItemsLock, NULL);
1297
1298                 QItemHandlers = NewHash(0, NULL);
1299
1300                 RegisterQItemHandler(HKEY("msgid"),             QItem_Handle_MsgID);
1301                 RegisterQItemHandler(HKEY("envelope_from"),     QItem_Handle_EnvelopeFrom);
1302                 RegisterQItemHandler(HKEY("retry"),             QItem_Handle_retry);
1303                 RegisterQItemHandler(HKEY("attempted"),         QItem_Handle_Attempted);
1304                 RegisterQItemHandler(HKEY("remote"),            QItem_Handle_Recipient);
1305                 RegisterQItemHandler(HKEY("bounceto"),          QItem_Handle_BounceTo);
1306                 RegisterQItemHandler(HKEY("source_room"),       QItem_Handle_SenderRoom);
1307                 RegisterQItemHandler(HKEY("submitted"),         QItem_Handle_Submitted);
1308
1309                 smtp_init_spoolout();
1310
1311                 CtdlRegisterEVCleanupHook(smtp_evq_cleanup);
1312
1313                 CtdlRegisterProtoHook(cmd_smtp, "SMTP", "SMTP utility commands");
1314                 CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER, PRIO_SEND + 10);
1315         }
1316
1317         /* return our Subversion id for the Log */
1318         return "smtpeventclient";
1319 }