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;
97 static const long MaxRetry = SMTP_RETRY_INTERVAL * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2;
99 int run_queue_now = 0; /* Set to 1 to ignore SMTP send retry times */
101 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
102 MailQEntry *MyQEntry,
104 /* KeepMsgText allows us to use MsgText as ours. */
107 ParsedURL *RelayUrls);
110 void smtp_evq_cleanup(void)
113 pthread_mutex_lock(&ActiveQItemsLock);
114 DeleteHash(&QItemHandlers);
115 DeleteHash(&ActiveQItems);
116 pthread_mutex_unlock(&ActiveQItemsLock);
117 pthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
118 /* citthread_mutex_destroy(&ActiveQItemsLock); TODO */
121 int DecreaseQReference(OneQueItem *MyQItem)
123 int IDestructQueItem;
125 pthread_mutex_lock(&ActiveQItemsLock);
126 MyQItem->ActiveDeliveries--;
127 IDestructQueItem = MyQItem->ActiveDeliveries == 0;
128 pthread_mutex_unlock(&ActiveQItemsLock);
129 return IDestructQueItem;
132 void RemoveQItem(OneQueItem *MyQItem)
139 pthread_mutex_lock(&ActiveQItemsLock);
140 It = GetNewHashPos(ActiveQItems, 0);
141 if (GetHashPosFromKey(ActiveQItems, LKEY(MyQItem->MessageID), It))
142 DeleteEntryFromHash(ActiveQItems, It);
146 "SMTP cleanup: unable to find QItem with ID[%ld]",
148 while (GetNextHashPos(ActiveQItems, It, &len, &Key, &VData))
150 "SMTP cleanup: have_: ID[%ld]",
151 ((OneQueItem *)VData)->MessageID);
153 pthread_mutex_unlock(&ActiveQItemsLock);
158 void FreeMailQEntry(void *qv)
161 FreeStrBuf(&Q->Recipient);
162 FreeStrBuf(&Q->StatusMessage);
165 void FreeQueItem(OneQueItem **Item)
167 DeleteHash(&(*Item)->MailQEntries);
168 FreeStrBuf(&(*Item)->EnvelopeFrom);
169 FreeStrBuf(&(*Item)->BounceTo);
170 FreeURL(&(*Item)->URL);
174 void HFreeQueItem(void *Item)
176 FreeQueItem((OneQueItem**)&Item);
179 /* inspect recipients with a status of:
180 * - 0 (no delivery yet attempted)
181 * - 3/4 (transient errors
182 * were experienced and it's time to try again)
184 int CountActiveQueueEntries(OneQueItem *MyQItem)
188 long ActiveDeliveries;
192 ActiveDeliveries = 0;
193 It = GetNewHashPos(MyQItem->MailQEntries, 0);
194 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
196 MailQEntry *ThisItem = vQE;
197 if ((ThisItem->Status == 0) ||
198 (ThisItem->Status == 3) ||
199 (ThisItem->Status == 4))
202 ThisItem->Active = 1;
205 ThisItem->Active = 0;
208 return ActiveDeliveries;
211 OneQueItem *DeserializeQueueItem(StrBuf *RawQItem, long QueMsgID)
214 const char *pLine = NULL;
219 Item = (OneQueItem*)malloc(sizeof(OneQueItem));
220 memset(Item, 0, sizeof(OneQueItem));
221 Item->Retry = SMTP_RETRY_INTERVAL;
222 Item->MessageID = -1;
223 Item->QueMsgID = QueMsgID;
226 Line = NewStrBufPlain(NULL, 128);
227 while (pLine != StrBufNOTNULL) {
228 const char *pItemPart = NULL;
231 StrBufExtract_NextToken(Line, RawQItem, &pLine, '\n');
232 if (StrLength(Line) == 0) continue;
233 StrBufExtract_NextToken(Token, Line, &pItemPart, '|');
234 if (GetHash(QItemHandlers, SKEY(Token), &vHandler))
237 H = (QItemHandler) vHandler;
238 H(Item, Line, &pItemPart);
244 if (Item->Retry >= MaxRetry)
247 pthread_mutex_lock(&ActiveQItemsLock);
248 if (GetHash(ActiveQItems,
249 LKEY(Item->MessageID),
252 /* WHOOPS. somebody else is already working on this. */
253 pthread_mutex_unlock(&ActiveQItemsLock);
258 /* mark our claim on this. */
260 LKEY(Item->MessageID),
263 pthread_mutex_unlock(&ActiveQItemsLock);
269 StrBuf *SerializeQueueItem(OneQueItem *MyQItem)
277 QMessage = NewStrBufPlain(NULL, SIZ);
278 StrBufPrintf(QMessage, "Content-type: %s\n", SPOOLMIME);
279 // "attempted|%ld\n" "retry|%ld\n",, (long)time(NULL), (long)retry );
280 StrBufAppendBufPlain(QMessage, HKEY("\nmsgid|"), 0);
281 StrBufAppendPrintf(QMessage, "%ld", MyQItem->MessageID);
283 StrBufAppendBufPlain(QMessage, HKEY("\nsubmitted|"), 0);
284 StrBufAppendPrintf(QMessage, "%ld", MyQItem->Submitted);
286 if (StrLength(MyQItem->BounceTo) > 0) {
287 StrBufAppendBufPlain(QMessage, HKEY("\nbounceto|"), 0);
288 StrBufAppendBuf(QMessage, MyQItem->BounceTo, 0);
291 if (StrLength(MyQItem->EnvelopeFrom) > 0) {
292 StrBufAppendBufPlain(QMessage, HKEY("\nenvelope_from|"), 0);
293 StrBufAppendBuf(QMessage, MyQItem->EnvelopeFrom, 0);
296 StrBufAppendBufPlain(QMessage, HKEY("\nretry|"), 0);
297 StrBufAppendPrintf(QMessage, "%ld",
300 StrBufAppendBufPlain(QMessage, HKEY("\nattempted|"), 0);
301 StrBufAppendPrintf(QMessage, "%ld",
302 MyQItem->ReattemptWhen);
304 It = GetNewHashPos(MyQItem->MailQEntries, 0);
305 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
307 MailQEntry *ThisItem = vQE;
309 if (!ThisItem->Active)
311 /* skip already sent ones from the spoolfile. */
314 StrBufAppendBufPlain(QMessage, HKEY("\nremote|"), 0);
315 StrBufAppendBuf(QMessage, ThisItem->Recipient, 0);
316 StrBufAppendBufPlain(QMessage, HKEY("|"), 0);
317 StrBufAppendPrintf(QMessage, "%d", ThisItem->Status);
318 StrBufAppendBufPlain(QMessage, HKEY("|"), 0);
319 StrBufAppendBuf(QMessage, ThisItem->StatusMessage, 0);
322 StrBufAppendBufPlain(QMessage, HKEY("\n"), 0);
330 void NewMailQEntry(OneQueItem *Item)
332 Item->Current = (MailQEntry*) malloc(sizeof(MailQEntry));
333 memset(Item->Current, 0, sizeof(MailQEntry));
335 if (Item->MailQEntries == NULL)
336 Item->MailQEntries = NewHash(1, Flathash);
337 Item->Current->StatusMessage = NewStrBuf();
338 Item->Current->n = GetCount(Item->MailQEntries);
339 Put(Item->MailQEntries,
340 IKEY(Item->Current->n),
345 void QItem_Handle_MsgID(OneQueItem *Item, StrBuf *Line, const char **Pos)
347 Item->MessageID = StrBufExtractNext_long(Line, Pos, '|');
350 void QItem_Handle_EnvelopeFrom(OneQueItem *Item, StrBuf *Line, const char **Pos)
352 if (Item->EnvelopeFrom == NULL)
353 Item->EnvelopeFrom = NewStrBufPlain(NULL, StrLength(Line));
354 StrBufExtract_NextToken(Item->EnvelopeFrom, Line, Pos, '|');
357 void QItem_Handle_BounceTo(OneQueItem *Item, StrBuf *Line, const char **Pos)
359 if (Item->BounceTo == NULL)
360 Item->BounceTo = NewStrBufPlain(NULL, StrLength(Line));
361 StrBufExtract_NextToken(Item->BounceTo, Line, Pos, '|');
364 void QItem_Handle_Recipient(OneQueItem *Item, StrBuf *Line, const char **Pos)
366 if (Item->Current == NULL)
368 if (Item->Current->Recipient == NULL)
369 Item->Current->Recipient=NewStrBufPlain(NULL, StrLength(Line));
370 StrBufExtract_NextToken(Item->Current->Recipient, Line, Pos, '|');
371 Item->Current->Status = StrBufExtractNext_int(Line, Pos, '|');
372 StrBufExtract_NextToken(Item->Current->StatusMessage, Line, Pos, '|');
373 Item->Current = NULL; // TODO: is this always right?
377 void QItem_Handle_retry(OneQueItem *Item, StrBuf *Line, const char **Pos)
380 StrBufExtractNext_int(Line, Pos, '|');
385 void QItem_Handle_Submitted(OneQueItem *Item, StrBuf *Line, const char **Pos)
387 Item->Submitted = atol(*Pos);
391 void QItem_Handle_Attempted(OneQueItem *Item, StrBuf *Line, const char **Pos)
393 Item->ReattemptWhen = StrBufExtractNext_int(Line, Pos, '|');
399 * this one has to have the context for loading the message via the redirect buffer...
401 StrBuf *smtp_load_msg(OneQueItem *MyQItem, int n)
406 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
407 CtdlOutputMsg(MyQItem->MessageID,
408 MT_RFC822, HEADERS_ALL,
410 (ESC_DOT|SUPPRESS_ENV_TO) );
412 SendMsg = CCC->redirect_buffer;
413 CCC->redirect_buffer = NULL;
414 if ((StrLength(SendMsg) > 0) &&
415 ChrPtr(SendMsg)[StrLength(SendMsg) - 1] != '\n') {
417 "SMTP client[%d]: Possible problem: message did not "
418 "correctly terminate. (expecting 0x10, got 0x%02x)\n",
419 MsgCount, //yes uncool, but best choice here...
420 ChrPtr(SendMsg)[StrLength(SendMsg) - 1] );
421 StrBufAppendBufPlain(SendMsg, HKEY("\r\n"), 0);
429 * smtp_do_bounce() is caled by smtp_do_procmsg() to scan a set of delivery
430 * instructions for "5" codes (permanent fatal errors) and produce/deliver
431 * a "bounce" message (delivery status notification).
433 void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt)
437 struct CtdlMessage *bmsg = NULL;
441 struct recptypes *valid;
448 int successful_bounce = 0;
452 syslog(LOG_DEBUG, "smtp_do_bounce() called\n");
454 if ( (ev_time() - MyQItem->Submitted) > SMTP_GIVE_UP ) {
455 give_up = 1;/// TODO: replace time by libevq timer get
459 * Now go through the instructions checking for stuff.
461 It = GetNewHashPos(MyQItem->MailQEntries, 0);
462 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
464 MailQEntry *ThisItem = vQE;
465 if ((ThisItem->Status == 5) || /* failed now? */
467 (ThisItem->Status != 2)))
468 /* giving up after failed attempts... */
470 if (num_bounces == 0)
471 Msg = NewStrBufPlain(NULL, 1024);
474 StrBufAppendBuf(Msg, ThisItem->Recipient, 0);
475 StrBufAppendBufPlain(Msg, HKEY(": "), 0);
476 StrBufAppendBuf(Msg, ThisItem->StatusMessage, 0);
477 StrBufAppendBufPlain(Msg, HKEY("\r\n"), 0);
482 /* Deliver the bounce if there's anything worth mentioning */
483 syslog(LOG_DEBUG, "num_bounces = %d\n", num_bounces);
485 if (num_bounces == 0) {
490 boundary = NewStrBufPlain(HKEY("=_Citadel_Multipart_"));
491 StrBufAppendPrintf(boundary,
497 /* Start building our bounce message; go shopping for memory first. */
498 BounceMB = NewStrBufPlain(
500 1024 + /* mime stuff.... */
501 StrLength(Msg) + /* the bounce information... */
502 StrLength(OMsgTxt)); /* the original message */
503 if (BounceMB == NULL) {
504 FreeStrBuf(&boundary);
505 syslog(LOG_ERR, "Failed to alloc() bounce message.\n");
510 bmsg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
512 FreeStrBuf(&boundary);
513 FreeStrBuf(&BounceMB);
514 syslog(LOG_ERR, "Failed to alloc() bounce message.\n");
518 memset(bmsg, 0, sizeof(struct CtdlMessage));
521 StrBufAppendBufPlain(BounceMB, HKEY("Content-type: multipart/mixed; boundary=\""), 0);
522 StrBufAppendBuf(BounceMB, boundary, 0);
523 StrBufAppendBufPlain(BounceMB, HKEY("\"\r\n"), 0);
524 StrBufAppendBufPlain(BounceMB, HKEY("MIME-Version: 1.0\r\n"), 0);
525 StrBufAppendBufPlain(BounceMB, HKEY("X-Mailer: " CITADEL "\r\n"), 0);
526 StrBufAppendBufPlain(BounceMB, HKEY("\r\nThis is a multipart message in MIME format.\r\n\r\n"), 0);
527 StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
528 StrBufAppendBuf(BounceMB, boundary, 0);
529 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
530 StrBufAppendBufPlain(BounceMB, HKEY("Content-type: text/plain\r\n\r\n"), 0);
533 StrBufAppendBufPlain(
536 "A message you sent could not be delivered "
537 "to some or all of its recipients\n"
538 "due to prolonged unavailability "
539 "of its destination(s).\n"
540 "Giving up on the following addresses:\n\n"
543 StrBufAppendBufPlain(
546 "A message you sent could not be delivered "
547 "to some or all of its recipients.\n"
548 "The following addresses "
549 "were undeliverable:\n\n"
552 StrBufAppendBuf(BounceMB, Msg, 0);
555 /* Attach the original message */
556 StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
557 StrBufAppendBuf(BounceMB, boundary, 0);
558 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
559 StrBufAppendBufPlain(BounceMB,
560 HKEY("Content-type: message/rfc822\r\n"), 0);
561 StrBufAppendBufPlain(BounceMB,
562 HKEY("Content-Transfer-Encoding: 7bit\r\n"), 0);
563 StrBufAppendBufPlain(BounceMB,
564 HKEY("Content-Disposition: inline\r\n"), 0);
565 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
566 StrBufAppendBuf(BounceMB, OMsgTxt, 0);
568 /* Close the multipart MIME scope */
569 StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
570 StrBufAppendBuf(BounceMB, boundary, 0);
571 StrBufAppendBufPlain(BounceMB, HKEY("--\r\n"), 0);
573 bmsg->cm_magic = CTDLMESSAGE_MAGIC;
574 bmsg->cm_anon_type = MES_NORMAL;
575 bmsg->cm_format_type = FMT_RFC822;
577 bmsg->cm_fields['O'] = strdup(MAILROOM);
578 bmsg->cm_fields['A'] = strdup("Citadel");
579 bmsg->cm_fields['N'] = strdup(config.c_nodename);
580 bmsg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
581 bmsg->cm_fields['M'] = SmashStrBuf(&BounceMB);
583 /* First try the user who sent the message */
584 if (StrLength(MyQItem->BounceTo) == 0)
585 syslog(LOG_ERR, "No bounce address specified\n");
587 syslog(LOG_DEBUG, "bounce to user? <%s>\n",
588 ChrPtr(MyQItem->BounceTo));
590 /* Can we deliver the bounce to the original sender? */
591 valid = validate_recipients(ChrPtr(MyQItem->BounceTo), NULL, 0);
592 if ((valid != NULL) && (valid->num_error == 0)) {
593 CtdlSubmitMsg(bmsg, valid, "", QP_EADDR);
594 successful_bounce = 1;
597 /* If not, post it in the Aide> room */
598 if (successful_bounce == 0) {
599 CtdlSubmitMsg(bmsg, NULL, config.c_aideroom, QP_EADDR);
602 /* Free up the memory we used */
603 free_recipients(valid);
604 FreeStrBuf(&boundary);
605 CtdlFreeMessage(bmsg);
606 syslog(LOG_DEBUG, "Done processing bounces\n");
612 * Called by smtp_do_queue() to handle an individual message.
614 void smtp_do_procmsg(long msgnum, void *userdata) {
615 struct CtdlMessage *msg = NULL;
625 ParsedURL *RelayUrls = NULL;
629 syslog(LOG_DEBUG, "SMTP Queue: smtp_do_procmsg(%ld)\n", msgnum);
630 ///strcpy(envelope_from, "");
632 msg = CtdlFetchMessage(msgnum, 1);
634 syslog(LOG_ERR, "SMTP Queue: tried %ld but no such message!\n",
639 pch = instr = msg->cm_fields['M'];
641 /* Strip out the headers (no not amd any other non-instruction) line */
642 while (pch != NULL) {
643 pch = strchr(pch, '\n');
644 if ((pch != NULL) && (*(pch + 1) == '\n')) {
649 PlainQItem = NewStrBufPlain(instr, -1);
650 CtdlFreeMessage(msg);
651 MyQItem = DeserializeQueueItem(PlainQItem, msgnum);
652 FreeStrBuf(&PlainQItem);
654 if (MyQItem == NULL) {
656 "SMTP Queue: Msg No %ld: already in progress!\n",
658 return; /* s.b. else is already processing... */
662 * Postpone delivery if we've already tried recently.
664 if ((MyQItem->ReattemptWhen != 0) &&
665 (time(NULL) < MyQItem->ReattemptWhen) &&
666 (run_queue_now == 0))
668 syslog(LOG_DEBUG, "SMTP client: Retry time not yet reached.\n");
670 It = GetNewHashPos(MyQItem->MailQEntries, 0);
671 pthread_mutex_lock(&ActiveQItemsLock);
673 if (GetHashPosFromKey(ActiveQItems,
674 LKEY(MyQItem->MessageID),
677 DeleteEntryFromHash(ActiveQItems, It);
680 pthread_mutex_unlock(&ActiveQItemsLock);
681 ////FreeQueItem(&MyQItem); TODO: DeleteEntryFromHash frees this?
687 * Bail out if there's no actual message associated with this
689 if (MyQItem->MessageID < 0L) {
690 syslog(LOG_ERR, "SMTP Queue: no 'msgid' directive found!\n");
691 It = GetNewHashPos(MyQItem->MailQEntries, 0);
692 pthread_mutex_lock(&ActiveQItemsLock);
694 if (GetHashPosFromKey(ActiveQItems,
695 LKEY(MyQItem->MessageID),
698 DeleteEntryFromHash(ActiveQItems, It);
701 pthread_mutex_unlock(&ActiveQItemsLock);
703 ////FreeQueItem(&MyQItem); TODO: DeleteEntryFromHash frees this?
709 ParsedURL **Url = &MyQItem->URL;
710 nRelays = get_hosts(mxbuf, "smarthost");
714 const char *Pos = NULL;
715 All = NewStrBufPlain(mxbuf, -1);
716 One = NewStrBufPlain(NULL, StrLength(All) + 1);
718 while ((Pos != StrBufNOTNULL) &&
722 StrBufExtract_NextToken(One, All, &Pos, '|');
723 if (!ParseURL(Url, One, 25))
725 "Failed to parse: %s\n",
728 ///if (!Url->IsIP)) // todo dupe me fork ipv6
736 Url = &MyQItem->FallBackHost;
737 nRelays = get_hosts(mxbuf, "fallbackhost");
741 const char *Pos = NULL;
742 All = NewStrBufPlain(mxbuf, -1);
743 One = NewStrBufPlain(NULL, StrLength(All) + 1);
745 while ((Pos != StrBufNOTNULL) &&
749 StrBufExtract_NextToken(One, All, &Pos, '|');
750 if (!ParseURL(Url, One, 25))
752 "Failed to parse: %s\n",
762 It = GetNewHashPos(MyQItem->MailQEntries, 0);
763 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
765 MailQEntry *ThisItem = vQE;
766 syslog(LOG_DEBUG, "SMTP Queue: Task: <%s> %d\n",
767 ChrPtr(ThisItem->Recipient),
772 MyQItem->ActiveDeliveries = CountActiveQueueEntries(MyQItem);
773 if (MyQItem->ActiveDeliveries > 0)
776 int m = MyQItem->ActiveDeliveries;
778 Msg = smtp_load_msg(MyQItem, n);
779 It = GetNewHashPos(MyQItem->MailQEntries, 0);
781 (GetNextHashPos(MyQItem->MailQEntries,
782 It, &len, &Key, &vQE)))
784 MailQEntry *ThisItem = vQE;
786 if (ThisItem->Active == 1)
788 int KeepBuffers = (i == m);
789 if (i > 1) n = MsgCount++;
791 "SMTPQ: Trying <%ld> <%s> %d / %d \n",
793 ChrPtr(ThisItem->Recipient),
796 smtp_try_one_queue_entry(MyQItem,
803 if (KeepBuffers) HaveBuffers = 1;
812 It = GetNewHashPos(MyQItem->MailQEntries, 0);
813 pthread_mutex_lock(&ActiveQItemsLock);
815 if (GetHashPosFromKey(ActiveQItems,
816 LKEY(MyQItem->MessageID),
819 DeleteEntryFromHash(ActiveQItems, It);
828 "SMTP cleanup: unable to find "
829 "QItem with ID[%ld]",
831 while (GetNextHashPos(ActiveQItems,
838 "SMTP cleanup: have: ID[%ld]",
839 ((OneQueItem *)VData)->MessageID);
844 pthread_mutex_unlock(&ActiveQItemsLock);
846 ////FreeQueItem(&MyQItem); TODO: DeleteEntryFromHash frees this?
848 // TODO: bounce & delete?
853 // TODO : free RelayUrls
860 * smtp_queue_thread()
862 * Run through the queue sending out messages.
864 void smtp_do_queue(void) {
865 static int is_running = 0;
866 int num_processed = 0;
869 return; /* Concurrency check - only one can run */
872 pthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
873 syslog(LOG_INFO, "SMTP client: processing outbound queue");
875 if (CtdlGetRoom(&CC->room, SMTP_SPOOLOUT_ROOM) != 0) {
876 syslog(LOG_ERR, "Cannot find room <%s>", SMTP_SPOOLOUT_ROOM);
879 num_processed = CtdlForEachMessage(MSGS_ALL,
888 "SMTP client: queue run completed; %d messages processed",
898 * Initialize the SMTP outbound queue
900 void smtp_init_spoolout(void) {
901 struct ctdlroom qrbuf;
904 * Create the room. This will silently fail if the room already
905 * exists, and that's perfectly ok, because we want it to exist.
907 CtdlCreateRoom(SMTP_SPOOLOUT_ROOM, 3, "", 0, 1, 0, VIEW_QUEUE);
910 * Make sure it's set to be a "system room" so it doesn't show up
911 * in the <K>nown rooms list for Aides.
913 if (CtdlGetRoomLock(&qrbuf, SMTP_SPOOLOUT_ROOM) == 0) {
914 qrbuf.QRflags2 |= QR2_SYSTEM;
915 CtdlPutRoomLock(&qrbuf);
922 /*****************************************************************************/
923 /* SMTP UTILITY COMMANDS */
924 /*****************************************************************************/
926 void cmd_smtp(char *argbuf) {
933 if (CtdlAccessCheck(ac_aide)) return;
935 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
937 if (!strcasecmp(cmd, "mx")) {
938 extract_token(node, argbuf, 1, '|', sizeof node);
939 num_mxhosts = getmx(buf, node);
940 cprintf("%d %d MX hosts listed for %s\n",
941 LISTING_FOLLOWS, num_mxhosts, node);
942 for (i=0; i<num_mxhosts; ++i) {
943 extract_token(node, buf, i, '|', sizeof node);
944 cprintf("%s\n", node);
950 else if (!strcasecmp(cmd, "runqueue")) {
952 cprintf("%d All outbound SMTP will be retried now.\n", CIT_OK);
957 cprintf("%d Invalid command.\n", ERROR + ILLEGAL_VALUE);
963 CTDL_MODULE_INIT(smtp_queu)
967 CtdlFillSystemContext(&smtp_queue_CC, "SMTP_Send");
968 ActiveQItems = NewHash(1, lFlathash);
969 pthread_mutex_init(&ActiveQItemsLock, NULL);
971 QItemHandlers = NewHash(0, NULL);
973 Put(QItemHandlers, HKEY("msgid"), QItem_Handle_MsgID, reference_free_handler);
974 Put(QItemHandlers, HKEY("envelope_from"), QItem_Handle_EnvelopeFrom, reference_free_handler);
975 Put(QItemHandlers, HKEY("retry"), QItem_Handle_retry, reference_free_handler);
976 Put(QItemHandlers, HKEY("attempted"), QItem_Handle_Attempted, reference_free_handler);
977 Put(QItemHandlers, HKEY("remote"), QItem_Handle_Recipient, reference_free_handler);
978 Put(QItemHandlers, HKEY("bounceto"), QItem_Handle_BounceTo, reference_free_handler);
979 Put(QItemHandlers, HKEY("submitted"), QItem_Handle_Submitted, reference_free_handler);
981 smtp_init_spoolout();
983 CtdlRegisterCleanupHook(smtp_evq_cleanup);
985 CtdlRegisterProtoHook(cmd_smtp, "SMTP", "SMTP utility commands");
986 CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER);
989 /* return our Subversion id for the Log */
990 return "smtpeventclient";