2 * This module is an SMTP and ESMTP implementation for the Citadel system.
3 * It is compliant with all of the following:
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
20 * The VRFY and EXPN commands have been removed from this implementation
21 * because nobody uses these commands anymore, except for spammers.
23 * Copyright (c) 1998-2012 by the citadel.org team
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.
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.
49 #include <sys/types.h>
52 #if TIME_WITH_SYS_TIME
53 # include <sys/time.h>
57 # include <sys/time.h>
66 #include <sys/socket.h>
67 #include <netinet/in.h>
68 #include <arpa/inet.h>
69 #include <libcitadel.h>
72 #include "citserver.h"
79 #include "internet_addressing.h"
82 #include "clientsocket.h"
83 #include "locate_host.h"
84 #include "citadel_dirs.h"
86 #include "ctdl_module.h"
88 #include "smtpqueue.h"
89 #include "event_client.h"
92 struct CitContext smtp_queue_CC;
93 pthread_mutex_t ActiveQItemsLock;
94 HashList *ActiveQItems = NULL;
95 HashList *QItemHandlers = NULL;
96 int max_sessions_for_outbound_smtp = 500; /* how many sessions might be active till we stop adding more smtp jobs */
97 int ndelay_count = 50; /* every n queued messages we will sleep... */
98 int delay_msec = 5000; /* this many seconds. */
100 static const long MaxRetry = SMTP_RETRY_INTERVAL * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2;
102 int run_queue_now = 0; /* Set to 1 to ignore SMTP send retry times */
104 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
105 MailQEntry *MyQEntry,
107 /* KeepMsgText allows us to use MsgText as ours. */
110 ParsedURL *RelayUrls);
113 void smtp_evq_cleanup(void)
116 pthread_mutex_lock(&ActiveQItemsLock);
117 DeleteHash(&QItemHandlers);
118 DeleteHash(&ActiveQItems);
119 pthread_mutex_unlock(&ActiveQItemsLock);
120 pthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
121 /* citthread_mutex_destroy(&ActiveQItemsLock); TODO */
124 int DecreaseQReference(OneQueItem *MyQItem)
126 int IDestructQueItem;
128 pthread_mutex_lock(&ActiveQItemsLock);
129 MyQItem->ActiveDeliveries--;
130 IDestructQueItem = MyQItem->ActiveDeliveries == 0;
131 pthread_mutex_unlock(&ActiveQItemsLock);
132 return IDestructQueItem;
135 void RemoveQItem(OneQueItem *MyQItem)
142 pthread_mutex_lock(&ActiveQItemsLock);
143 It = GetNewHashPos(ActiveQItems, 0);
144 if (GetHashPosFromKey(ActiveQItems, LKEY(MyQItem->MessageID), It))
145 DeleteEntryFromHash(ActiveQItems, It);
149 "SMTP cleanup: unable to find QItem with ID[%ld]",
151 while (GetNextHashPos(ActiveQItems, It, &len, &Key, &VData))
153 "SMTP cleanup: have_: ID[%ld]",
154 ((OneQueItem *)VData)->MessageID);
156 pthread_mutex_unlock(&ActiveQItemsLock);
161 void FreeMailQEntry(void *qv)
164 FreeStrBuf(&Q->Recipient);
165 FreeStrBuf(&Q->StatusMessage);
168 void FreeQueItem(OneQueItem **Item)
170 DeleteHash(&(*Item)->MailQEntries);
171 FreeStrBuf(&(*Item)->EnvelopeFrom);
172 FreeStrBuf(&(*Item)->BounceTo);
173 FreeURL(&(*Item)->URL);
177 void HFreeQueItem(void *Item)
179 FreeQueItem((OneQueItem**)&Item);
182 /* inspect recipients with a status of:
183 * - 0 (no delivery yet attempted)
184 * - 3/4 (transient errors
185 * were experienced and it's time to try again)
187 int CountActiveQueueEntries(OneQueItem *MyQItem)
191 long ActiveDeliveries;
195 ActiveDeliveries = 0;
196 It = GetNewHashPos(MyQItem->MailQEntries, 0);
197 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
199 MailQEntry *ThisItem = vQE;
200 if ((ThisItem->Status == 0) ||
201 (ThisItem->Status == 3) ||
202 (ThisItem->Status == 4))
205 ThisItem->Active = 1;
208 ThisItem->Active = 0;
211 return ActiveDeliveries;
214 OneQueItem *DeserializeQueueItem(StrBuf *RawQItem, long QueMsgID)
217 const char *pLine = NULL;
222 Item = (OneQueItem*)malloc(sizeof(OneQueItem));
223 memset(Item, 0, sizeof(OneQueItem));
224 Item->Retry = SMTP_RETRY_INTERVAL;
225 Item->MessageID = -1;
226 Item->QueMsgID = QueMsgID;
229 Line = NewStrBufPlain(NULL, 128);
230 while (pLine != StrBufNOTNULL) {
231 const char *pItemPart = NULL;
234 StrBufExtract_NextToken(Line, RawQItem, &pLine, '\n');
235 if (StrLength(Line) == 0) continue;
236 StrBufExtract_NextToken(Token, Line, &pItemPart, '|');
237 if (GetHash(QItemHandlers, SKEY(Token), &vHandler))
240 H = (QItemHandler) vHandler;
241 H(Item, Line, &pItemPart);
247 if (Item->Retry >= MaxRetry)
250 pthread_mutex_lock(&ActiveQItemsLock);
251 if (GetHash(ActiveQItems,
252 LKEY(Item->MessageID),
255 /* WHOOPS. somebody else is already working on this. */
256 pthread_mutex_unlock(&ActiveQItemsLock);
261 /* mark our claim on this. */
263 LKEY(Item->MessageID),
266 pthread_mutex_unlock(&ActiveQItemsLock);
272 StrBuf *SerializeQueueItem(OneQueItem *MyQItem)
280 QMessage = NewStrBufPlain(NULL, SIZ);
281 StrBufPrintf(QMessage, "Content-type: %s\n", SPOOLMIME);
282 // "attempted|%ld\n" "retry|%ld\n",, (long)time(NULL), (long)retry );
283 StrBufAppendBufPlain(QMessage, HKEY("\nmsgid|"), 0);
284 StrBufAppendPrintf(QMessage, "%ld", MyQItem->MessageID);
286 StrBufAppendBufPlain(QMessage, HKEY("\nsubmitted|"), 0);
287 StrBufAppendPrintf(QMessage, "%ld", MyQItem->Submitted);
289 if (StrLength(MyQItem->BounceTo) > 0) {
290 StrBufAppendBufPlain(QMessage, HKEY("\nbounceto|"), 0);
291 StrBufAppendBuf(QMessage, MyQItem->BounceTo, 0);
294 if (StrLength(MyQItem->EnvelopeFrom) > 0) {
295 StrBufAppendBufPlain(QMessage, HKEY("\nenvelope_from|"), 0);
296 StrBufAppendBuf(QMessage, MyQItem->EnvelopeFrom, 0);
299 StrBufAppendBufPlain(QMessage, HKEY("\nretry|"), 0);
300 StrBufAppendPrintf(QMessage, "%ld",
303 StrBufAppendBufPlain(QMessage, HKEY("\nattempted|"), 0);
304 StrBufAppendPrintf(QMessage, "%ld",
305 MyQItem->ReattemptWhen);
307 It = GetNewHashPos(MyQItem->MailQEntries, 0);
308 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
310 MailQEntry *ThisItem = vQE;
312 if (!ThisItem->Active)
314 /* skip already sent ones from the spoolfile. */
317 StrBufAppendBufPlain(QMessage, HKEY("\nremote|"), 0);
318 StrBufAppendBuf(QMessage, ThisItem->Recipient, 0);
319 StrBufAppendBufPlain(QMessage, HKEY("|"), 0);
320 StrBufAppendPrintf(QMessage, "%d", ThisItem->Status);
321 StrBufAppendBufPlain(QMessage, HKEY("|"), 0);
322 StrBufAppendBuf(QMessage, ThisItem->StatusMessage, 0);
325 StrBufAppendBufPlain(QMessage, HKEY("\n"), 0);
333 void NewMailQEntry(OneQueItem *Item)
335 Item->Current = (MailQEntry*) malloc(sizeof(MailQEntry));
336 memset(Item->Current, 0, sizeof(MailQEntry));
338 if (Item->MailQEntries == NULL)
339 Item->MailQEntries = NewHash(1, Flathash);
340 Item->Current->StatusMessage = NewStrBuf();
341 Item->Current->n = GetCount(Item->MailQEntries);
342 Put(Item->MailQEntries,
343 IKEY(Item->Current->n),
348 void QItem_Handle_MsgID(OneQueItem *Item, StrBuf *Line, const char **Pos)
350 Item->MessageID = StrBufExtractNext_long(Line, Pos, '|');
353 void QItem_Handle_EnvelopeFrom(OneQueItem *Item, StrBuf *Line, const char **Pos)
355 if (Item->EnvelopeFrom == NULL)
356 Item->EnvelopeFrom = NewStrBufPlain(NULL, StrLength(Line));
357 StrBufExtract_NextToken(Item->EnvelopeFrom, Line, Pos, '|');
360 void QItem_Handle_BounceTo(OneQueItem *Item, StrBuf *Line, const char **Pos)
362 if (Item->BounceTo == NULL)
363 Item->BounceTo = NewStrBufPlain(NULL, StrLength(Line));
364 StrBufExtract_NextToken(Item->BounceTo, Line, Pos, '|');
367 void QItem_Handle_Recipient(OneQueItem *Item, StrBuf *Line, const char **Pos)
369 if (Item->Current == NULL)
371 if (Item->Current->Recipient == NULL)
372 Item->Current->Recipient=NewStrBufPlain(NULL, StrLength(Line));
373 StrBufExtract_NextToken(Item->Current->Recipient, Line, Pos, '|');
374 Item->Current->Status = StrBufExtractNext_int(Line, Pos, '|');
375 StrBufExtract_NextToken(Item->Current->StatusMessage, Line, Pos, '|');
376 Item->Current = NULL; // TODO: is this always right?
380 void QItem_Handle_retry(OneQueItem *Item, StrBuf *Line, const char **Pos)
383 StrBufExtractNext_int(Line, Pos, '|');
388 void QItem_Handle_Submitted(OneQueItem *Item, StrBuf *Line, const char **Pos)
390 Item->Submitted = atol(*Pos);
394 void QItem_Handle_Attempted(OneQueItem *Item, StrBuf *Line, const char **Pos)
396 Item->ReattemptWhen = StrBufExtractNext_int(Line, Pos, '|');
402 * this one has to have the context for loading the message via the redirect buffer...
404 StrBuf *smtp_load_msg(OneQueItem *MyQItem, int n)
409 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
410 CtdlOutputMsg(MyQItem->MessageID,
411 MT_RFC822, HEADERS_ALL,
413 (ESC_DOT|SUPPRESS_ENV_TO) );
415 SendMsg = CCC->redirect_buffer;
416 CCC->redirect_buffer = NULL;
417 if ((StrLength(SendMsg) > 0) &&
418 ChrPtr(SendMsg)[StrLength(SendMsg) - 1] != '\n') {
420 "SMTP client[%d]: Possible problem: message did not "
421 "correctly terminate. (expecting 0x10, got 0x%02x)\n",
422 MsgCount, //yes uncool, but best choice here...
423 ChrPtr(SendMsg)[StrLength(SendMsg) - 1] );
424 StrBufAppendBufPlain(SendMsg, HKEY("\r\n"), 0);
432 * smtp_do_bounce() is caled by smtp_do_procmsg() to scan a set of delivery
433 * instructions for "5" codes (permanent fatal errors) and produce/deliver
434 * a "bounce" message (delivery status notification).
436 void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt)
440 struct CtdlMessage *bmsg = NULL;
444 struct recptypes *valid;
451 int successful_bounce = 0;
455 syslog(LOG_DEBUG, "smtp_do_bounce() called\n");
457 if ( (ev_time() - MyQItem->Submitted) > SMTP_GIVE_UP ) {
458 give_up = 1;/// TODO: replace time by libevq timer get
462 * Now go through the instructions checking for stuff.
464 It = GetNewHashPos(MyQItem->MailQEntries, 0);
465 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
467 MailQEntry *ThisItem = vQE;
468 if ((ThisItem->Status == 5) || /* failed now? */
470 (ThisItem->Status != 2)))
471 /* giving up after failed attempts... */
473 if (num_bounces == 0)
474 Msg = NewStrBufPlain(NULL, 1024);
477 StrBufAppendBuf(Msg, ThisItem->Recipient, 0);
478 StrBufAppendBufPlain(Msg, HKEY(": "), 0);
479 StrBufAppendBuf(Msg, ThisItem->StatusMessage, 0);
480 StrBufAppendBufPlain(Msg, HKEY("\r\n"), 0);
485 /* Deliver the bounce if there's anything worth mentioning */
486 syslog(LOG_DEBUG, "num_bounces = %d\n", num_bounces);
488 if (num_bounces == 0) {
493 boundary = NewStrBufPlain(HKEY("=_Citadel_Multipart_"));
494 StrBufAppendPrintf(boundary,
500 /* Start building our bounce message; go shopping for memory first. */
501 BounceMB = NewStrBufPlain(
503 1024 + /* mime stuff.... */
504 StrLength(Msg) + /* the bounce information... */
505 StrLength(OMsgTxt)); /* the original message */
506 if (BounceMB == NULL) {
507 FreeStrBuf(&boundary);
508 syslog(LOG_ERR, "Failed to alloc() bounce message.\n");
513 bmsg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
515 FreeStrBuf(&boundary);
516 FreeStrBuf(&BounceMB);
517 syslog(LOG_ERR, "Failed to alloc() bounce message.\n");
521 memset(bmsg, 0, sizeof(struct CtdlMessage));
524 StrBufAppendBufPlain(BounceMB, HKEY("Content-type: multipart/mixed; boundary=\""), 0);
525 StrBufAppendBuf(BounceMB, boundary, 0);
526 StrBufAppendBufPlain(BounceMB, HKEY("\"\r\n"), 0);
527 StrBufAppendBufPlain(BounceMB, HKEY("MIME-Version: 1.0\r\n"), 0);
528 StrBufAppendBufPlain(BounceMB, HKEY("X-Mailer: " CITADEL "\r\n"), 0);
529 StrBufAppendBufPlain(BounceMB, HKEY("\r\nThis is a multipart message in MIME format.\r\n\r\n"), 0);
530 StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
531 StrBufAppendBuf(BounceMB, boundary, 0);
532 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
533 StrBufAppendBufPlain(BounceMB, HKEY("Content-type: text/plain\r\n\r\n"), 0);
536 StrBufAppendBufPlain(
539 "A message you sent could not be delivered "
540 "to some or all of its recipients\n"
541 "due to prolonged unavailability "
542 "of its destination(s).\n"
543 "Giving up on the following addresses:\n\n"
546 StrBufAppendBufPlain(
549 "A message you sent could not be delivered "
550 "to some or all of its recipients.\n"
551 "The following addresses "
552 "were undeliverable:\n\n"
555 StrBufAppendBuf(BounceMB, Msg, 0);
558 /* Attach the original message */
559 StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
560 StrBufAppendBuf(BounceMB, boundary, 0);
561 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
562 StrBufAppendBufPlain(BounceMB,
563 HKEY("Content-type: message/rfc822\r\n"), 0);
564 StrBufAppendBufPlain(BounceMB,
565 HKEY("Content-Transfer-Encoding: 7bit\r\n"), 0);
566 StrBufAppendBufPlain(BounceMB,
567 HKEY("Content-Disposition: inline\r\n"), 0);
568 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
569 StrBufAppendBuf(BounceMB, OMsgTxt, 0);
571 /* Close the multipart MIME scope */
572 StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
573 StrBufAppendBuf(BounceMB, boundary, 0);
574 StrBufAppendBufPlain(BounceMB, HKEY("--\r\n"), 0);
576 bmsg->cm_magic = CTDLMESSAGE_MAGIC;
577 bmsg->cm_anon_type = MES_NORMAL;
578 bmsg->cm_format_type = FMT_RFC822;
580 bmsg->cm_fields['O'] = strdup(MAILROOM);
581 bmsg->cm_fields['A'] = strdup("Citadel");
582 bmsg->cm_fields['N'] = strdup(config.c_nodename);
583 bmsg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
584 bmsg->cm_fields['M'] = SmashStrBuf(&BounceMB);
586 /* First try the user who sent the message */
587 if (StrLength(MyQItem->BounceTo) == 0)
588 syslog(LOG_ERR, "No bounce address specified\n");
590 syslog(LOG_DEBUG, "bounce to user? <%s>\n",
591 ChrPtr(MyQItem->BounceTo));
593 /* Can we deliver the bounce to the original sender? */
594 valid = validate_recipients(ChrPtr(MyQItem->BounceTo), NULL, 0);
595 if ((valid != NULL) && (valid->num_error == 0)) {
596 CtdlSubmitMsg(bmsg, valid, "", QP_EADDR);
597 successful_bounce = 1;
600 /* If not, post it in the Aide> room */
601 if (successful_bounce == 0) {
602 CtdlSubmitMsg(bmsg, NULL, config.c_aideroom, QP_EADDR);
605 /* Free up the memory we used */
606 free_recipients(valid);
607 FreeStrBuf(&boundary);
608 CtdlFreeMessage(bmsg);
609 syslog(LOG_DEBUG, "Done processing bounces\n");
615 * Called by smtp_do_queue() to handle an individual message.
617 void smtp_do_procmsg(long msgnum, void *userdata) {
618 int mynumsessions = num_sessions;
619 struct CtdlMessage *msg = NULL;
629 ParsedURL *RelayUrls = NULL;
633 if (mynumsessions > max_sessions_for_outbound_smtp) {
635 "SMTP Queue: skipping because of num jobs %d > %d max_sessions_for_outbound_smtp",
637 max_sessions_for_outbound_smtp);
640 syslog(LOG_DEBUG, "SMTP Queue: smtp_do_procmsg(%ld)\n", msgnum);
641 ///strcpy(envelope_from, "");
643 msg = CtdlFetchMessage(msgnum, 1);
645 syslog(LOG_ERR, "SMTP Queue: tried %ld but no such message!\n",
650 pch = instr = msg->cm_fields['M'];
652 /* Strip out the headers (no not amd any other non-instruction) line */
653 while (pch != NULL) {
654 pch = strchr(pch, '\n');
655 if ((pch != NULL) && (*(pch + 1) == '\n')) {
660 PlainQItem = NewStrBufPlain(instr, -1);
661 CtdlFreeMessage(msg);
662 MyQItem = DeserializeQueueItem(PlainQItem, msgnum);
663 FreeStrBuf(&PlainQItem);
665 if (MyQItem == NULL) {
667 "SMTP Queue: Msg No %ld: already in progress!\n",
669 return; /* s.b. else is already processing... */
673 * Postpone delivery if we've already tried recently.
675 if ((MyQItem->ReattemptWhen != 0) &&
676 (time(NULL) < MyQItem->ReattemptWhen) &&
677 (run_queue_now == 0))
679 syslog(LOG_DEBUG, "SMTP client: Retry time not yet reached.\n");
681 It = GetNewHashPos(MyQItem->MailQEntries, 0);
682 pthread_mutex_lock(&ActiveQItemsLock);
684 if (GetHashPosFromKey(ActiveQItems,
685 LKEY(MyQItem->MessageID),
688 DeleteEntryFromHash(ActiveQItems, It);
691 pthread_mutex_unlock(&ActiveQItemsLock);
692 ////FreeQueItem(&MyQItem); TODO: DeleteEntryFromHash frees this?
698 * Bail out if there's no actual message associated with this
700 if (MyQItem->MessageID < 0L) {
701 syslog(LOG_ERR, "SMTP Queue: no 'msgid' directive found!\n");
702 It = GetNewHashPos(MyQItem->MailQEntries, 0);
703 pthread_mutex_lock(&ActiveQItemsLock);
705 if (GetHashPosFromKey(ActiveQItems,
706 LKEY(MyQItem->MessageID),
709 DeleteEntryFromHash(ActiveQItems, It);
712 pthread_mutex_unlock(&ActiveQItemsLock);
714 ////FreeQueItem(&MyQItem); TODO: DeleteEntryFromHash frees this?
720 ParsedURL **Url = &MyQItem->URL;
721 nRelays = get_hosts(mxbuf, "smarthost");
725 const char *Pos = NULL;
726 All = NewStrBufPlain(mxbuf, -1);
727 One = NewStrBufPlain(NULL, StrLength(All) + 1);
729 while ((Pos != StrBufNOTNULL) &&
733 StrBufExtract_NextToken(One, All, &Pos, '|');
734 if (!ParseURL(Url, One, 25))
736 "Failed to parse: %s\n",
739 ///if (!Url->IsIP)) // todo dupe me fork ipv6
747 Url = &MyQItem->FallBackHost;
748 nRelays = get_hosts(mxbuf, "fallbackhost");
752 const char *Pos = NULL;
753 All = NewStrBufPlain(mxbuf, -1);
754 One = NewStrBufPlain(NULL, StrLength(All) + 1);
756 while ((Pos != StrBufNOTNULL) &&
760 StrBufExtract_NextToken(One, All, &Pos, '|');
761 if (!ParseURL(Url, One, 25))
763 "Failed to parse: %s\n",
773 It = GetNewHashPos(MyQItem->MailQEntries, 0);
774 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
776 MailQEntry *ThisItem = vQE;
777 syslog(LOG_DEBUG, "SMTP Queue: Task: <%s> %d\n",
778 ChrPtr(ThisItem->Recipient),
783 MyQItem->ActiveDeliveries = CountActiveQueueEntries(MyQItem);
785 /* failsafe against overload:
786 * will we exceed the limit set?
788 if ((MyQItem->ActiveDeliveries + mynumsessions > max_sessions_for_outbound_smtp) &&
789 /* if yes, did we reach more than half of the quota? */
790 ((mynumsessions * 2) > max_sessions_for_outbound_smtp) &&
791 /* if... would we ever fit into half of the quota?? */
792 (((MyQItem->ActiveDeliveries * 2) < max_sessions_for_outbound_smtp)))
794 /* abort delivery for another time. */
796 "SMTP Queue: skipping because of num jobs %d + %ld > %d max_sessions_for_outbound_smtp",
798 MyQItem->ActiveDeliveries,
799 max_sessions_for_outbound_smtp);
801 FreeQueItem(&MyQItem);
807 if (MyQItem->ActiveDeliveries > 0)
811 int m = MyQItem->ActiveDeliveries;
813 Msg = smtp_load_msg(MyQItem, n);
814 It = GetNewHashPos(MyQItem->MailQEntries, 0);
816 (GetNextHashPos(MyQItem->MailQEntries,
817 It, &len, &Key, &vQE)))
819 MailQEntry *ThisItem = vQE;
821 if (ThisItem->Active == 1)
823 int KeepBuffers = (i == m);
826 if (nActivated % ndelay_count == 0)
829 if (i > 1) n = MsgCount++;
831 "SMTPQ: Trying <%ld> <%s> %d / %d \n",
833 ChrPtr(ThisItem->Recipient),
836 smtp_try_one_queue_entry(MyQItem,
843 if (KeepBuffers) HaveBuffers = 1;
852 It = GetNewHashPos(MyQItem->MailQEntries, 0);
853 pthread_mutex_lock(&ActiveQItemsLock);
855 if (GetHashPosFromKey(ActiveQItems,
856 LKEY(MyQItem->MessageID),
859 DeleteEntryFromHash(ActiveQItems, It);
868 "SMTP cleanup: unable to find "
869 "QItem with ID[%ld]",
871 while (GetNextHashPos(ActiveQItems,
878 "SMTP cleanup: have: ID[%ld]",
879 ((OneQueItem *)VData)->MessageID);
884 pthread_mutex_unlock(&ActiveQItemsLock);
886 ////FreeQueItem(&MyQItem); TODO: DeleteEntryFromHash frees this?
888 // TODO: bounce & delete?
893 // TODO : free RelayUrls
900 * smtp_queue_thread()
902 * Run through the queue sending out messages.
904 void smtp_do_queue(void) {
905 static int is_running = 0;
906 int num_processed = 0;
909 return; /* Concurrency check - only one can run */
912 pthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
913 syslog(LOG_INFO, "SMTP client: processing outbound queue");
915 if (CtdlGetRoom(&CC->room, SMTP_SPOOLOUT_ROOM) != 0) {
916 syslog(LOG_ERR, "Cannot find room <%s>", SMTP_SPOOLOUT_ROOM);
919 num_processed = CtdlForEachMessage(MSGS_ALL,
928 "SMTP client: queue run completed; %d messages processed",
938 * Initialize the SMTP outbound queue
940 void smtp_init_spoolout(void) {
941 struct ctdlroom qrbuf;
944 * Create the room. This will silently fail if the room already
945 * exists, and that's perfectly ok, because we want it to exist.
947 CtdlCreateRoom(SMTP_SPOOLOUT_ROOM, 3, "", 0, 1, 0, VIEW_QUEUE);
950 * Make sure it's set to be a "system room" so it doesn't show up
951 * in the <K>nown rooms list for Aides.
953 if (CtdlGetRoomLock(&qrbuf, SMTP_SPOOLOUT_ROOM) == 0) {
954 qrbuf.QRflags2 |= QR2_SYSTEM;
955 CtdlPutRoomLock(&qrbuf);
962 /*****************************************************************************/
963 /* SMTP UTILITY COMMANDS */
964 /*****************************************************************************/
966 void cmd_smtp(char *argbuf) {
973 if (CtdlAccessCheck(ac_aide)) return;
975 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
977 if (!strcasecmp(cmd, "mx")) {
978 extract_token(node, argbuf, 1, '|', sizeof node);
979 num_mxhosts = getmx(buf, node);
980 cprintf("%d %d MX hosts listed for %s\n",
981 LISTING_FOLLOWS, num_mxhosts, node);
982 for (i=0; i<num_mxhosts; ++i) {
983 extract_token(node, buf, i, '|', sizeof node);
984 cprintf("%s\n", node);
990 else if (!strcasecmp(cmd, "runqueue")) {
992 cprintf("%d All outbound SMTP will be retried now.\n", CIT_OK);
997 cprintf("%d Invalid command.\n", ERROR + ILLEGAL_VALUE);
1003 CTDL_MODULE_INIT(smtp_queu)
1009 pstr = getenv("CITSERVER_n_session_max");
1010 if ((pstr != NULL) && (*pstr != '\0'))
1011 max_sessions_for_outbound_smtp = atol(pstr); /* how many sessions might be active till we stop adding more smtp jobs */
1013 pstr = getenv("CITSERVER_smtp_n_delay_count");
1014 if ((pstr != NULL) && (*pstr != '\0'))
1015 ndelay_count = atol(pstr); /* every n queued messages we will sleep... */
1017 pstr = getenv("CITSERVER_smtp_delay");
1018 if ((pstr != NULL) && (*pstr != '\0'))
1019 delay_msec = atol(pstr) * 1000; /* this many seconds. */
1024 CtdlFillSystemContext(&smtp_queue_CC, "SMTP_Send");
1025 ActiveQItems = NewHash(1, lFlathash);
1026 pthread_mutex_init(&ActiveQItemsLock, NULL);
1028 QItemHandlers = NewHash(0, NULL);
1030 Put(QItemHandlers, HKEY("msgid"), QItem_Handle_MsgID, reference_free_handler);
1031 Put(QItemHandlers, HKEY("envelope_from"), QItem_Handle_EnvelopeFrom, reference_free_handler);
1032 Put(QItemHandlers, HKEY("retry"), QItem_Handle_retry, reference_free_handler);
1033 Put(QItemHandlers, HKEY("attempted"), QItem_Handle_Attempted, reference_free_handler);
1034 Put(QItemHandlers, HKEY("remote"), QItem_Handle_Recipient, reference_free_handler);
1035 Put(QItemHandlers, HKEY("bounceto"), QItem_Handle_BounceTo, reference_free_handler);
1036 Put(QItemHandlers, HKEY("submitted"), QItem_Handle_Submitted, reference_free_handler);
1038 smtp_init_spoolout();
1040 CtdlRegisterCleanupHook(smtp_evq_cleanup);
1042 CtdlRegisterProtoHook(cmd_smtp, "SMTP", "SMTP utility commands");
1043 CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER);
1046 /* return our Subversion id for the Log */
1047 return "smtpeventclient";