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-2009 by the citadel.org team
25 * This program is free software; you can redistribute it and/or modify
26 * it under the terms of the GNU General Public License as published by
27 * the Free Software Foundation; either version 3 of the License, or
28 * (at your option) any later version.
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.
35 * You should have received a copy of the GNU General Public License
36 * along with this program; if not, write to the Free Software
37 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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;
98 int run_queue_now = 0; /* Set to 1 to ignore SMTP send retry times */
100 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
101 MailQEntry *MyQEntry,
103 /* KeepMsgText allows us to use MsgText as ours. */
106 ParsedURL *RelayUrls);
109 void smtp_evq_cleanup(void)
112 pthread_mutex_lock(&ActiveQItemsLock);
113 DeleteHash(&QItemHandlers);
114 DeleteHash(&ActiveQItems);
115 pthread_mutex_unlock(&ActiveQItemsLock);
116 pthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
117 /* citthread_mutex_destroy(&ActiveQItemsLock); TODO */
120 int DecreaseQReference(OneQueItem *MyQItem)
122 int IDestructQueItem;
124 pthread_mutex_lock(&ActiveQItemsLock);
125 MyQItem->ActiveDeliveries--;
126 IDestructQueItem = MyQItem->ActiveDeliveries == 0;
127 pthread_mutex_unlock(&ActiveQItemsLock);
128 return IDestructQueItem;
131 void RemoveQItem(OneQueItem *MyQItem)
138 pthread_mutex_lock(&ActiveQItemsLock);
139 It = GetNewHashPos(ActiveQItems, 0);
140 if (GetHashPosFromKey(ActiveQItems, LKEY(MyQItem->MessageID), It))
141 DeleteEntryFromHash(ActiveQItems, It);
145 "SMTP cleanup: unable to find QItem with ID[%ld]",
147 while (GetNextHashPos(ActiveQItems, It, &len, &Key, &VData))
149 "SMTP cleanup: have_: ID[%ld]",
150 ((OneQueItem *)VData)->MessageID);
152 pthread_mutex_unlock(&ActiveQItemsLock);
157 void FreeMailQEntry(void *qv)
160 FreeStrBuf(&Q->Recipient);
161 FreeStrBuf(&Q->StatusMessage);
164 void FreeQueItem(OneQueItem **Item)
166 DeleteHash(&(*Item)->MailQEntries);
167 FreeStrBuf(&(*Item)->EnvelopeFrom);
168 FreeStrBuf(&(*Item)->BounceTo);
169 FreeURL(&(*Item)->URL);
173 void HFreeQueItem(void *Item)
175 FreeQueItem((OneQueItem**)&Item);
178 /* inspect recipients with a status of:
179 * - 0 (no delivery yet attempted)
180 * - 3/4 (transient errors
181 * were experienced and it's time to try again)
183 int CountActiveQueueEntries(OneQueItem *MyQItem)
187 long ActiveDeliveries;
191 ActiveDeliveries = 0;
192 It = GetNewHashPos(MyQItem->MailQEntries, 0);
193 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
195 MailQEntry *ThisItem = vQE;
196 if ((ThisItem->Status == 0) ||
197 (ThisItem->Status == 3) ||
198 (ThisItem->Status == 4))
201 ThisItem->Active = 1;
204 ThisItem->Active = 0;
207 return ActiveDeliveries;
210 OneQueItem *DeserializeQueueItem(StrBuf *RawQItem, long QueMsgID)
213 const char *pLine = NULL;
218 Item = (OneQueItem*)malloc(sizeof(OneQueItem));
219 memset(Item, 0, sizeof(OneQueItem));
220 Item->LastAttempt.retry = SMTP_RETRY_INTERVAL;
221 Item->MessageID = -1;
222 Item->QueMsgID = QueMsgID;
225 Line = NewStrBufPlain(NULL, 128);
226 while (pLine != StrBufNOTNULL) {
227 const char *pItemPart = NULL;
230 StrBufExtract_NextToken(Line, RawQItem, &pLine, '\n');
231 if (StrLength(Line) == 0) continue;
232 StrBufExtract_NextToken(Token, Line, &pItemPart, '|');
233 if (GetHash(QItemHandlers, SKEY(Token), &vHandler))
236 H = (QItemHandler) vHandler;
237 H(Item, Line, &pItemPart);
243 pthread_mutex_lock(&ActiveQItemsLock);
244 if (GetHash(ActiveQItems,
245 LKEY(Item->MessageID),
248 /* WHOOPS. somebody else is already working on this. */
249 pthread_mutex_unlock(&ActiveQItemsLock);
254 /* mark our claim on this. */
256 LKEY(Item->MessageID),
259 pthread_mutex_unlock(&ActiveQItemsLock);
265 StrBuf *SerializeQueueItem(OneQueItem *MyQItem)
273 QMessage = NewStrBufPlain(NULL, SIZ);
274 StrBufPrintf(QMessage, "Content-type: %s\n", SPOOLMIME);
275 // "attempted|%ld\n" "retry|%ld\n",, (long)time(NULL), (long)retry );
276 StrBufAppendBufPlain(QMessage, HKEY("\nmsgid|"), 0);
277 StrBufAppendPrintf(QMessage, "%ld", MyQItem->MessageID);
279 StrBufAppendBufPlain(QMessage, HKEY("\nsubmitted|"), 0);
280 StrBufAppendPrintf(QMessage, "%ld", MyQItem->Submitted);
282 if (StrLength(MyQItem->BounceTo) > 0) {
283 StrBufAppendBufPlain(QMessage, HKEY("\nbounceto|"), 0);
284 StrBufAppendBuf(QMessage, MyQItem->BounceTo, 0);
287 if (StrLength(MyQItem->EnvelopeFrom) > 0) {
288 StrBufAppendBufPlain(QMessage, HKEY("\nenvelope_from|"), 0);
289 StrBufAppendBuf(QMessage, MyQItem->EnvelopeFrom, 0);
292 It = GetNewHashPos(MyQItem->MailQEntries, 0);
293 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
295 MailQEntry *ThisItem = vQE;
298 if (!ThisItem->Active)
300 /* skip already sent ones from the spoolfile. */
304 for (i=0; i < ThisItem->nAttempts; i++) {
305 /* TODO: most probably
306 * there is just one retry/attempted per message!
308 StrBufAppendBufPlain(QMessage, HKEY("\nretry|"), 0);
309 StrBufAppendPrintf(QMessage, "%ld",
310 ThisItem->Attempts[i].retry);
312 StrBufAppendBufPlain(QMessage, HKEY("\nattempted|"), 0);
313 StrBufAppendPrintf(QMessage, "%ld",
314 ThisItem->Attempts[i].when);
316 StrBufAppendBufPlain(QMessage, HKEY("\nremote|"), 0);
317 StrBufAppendBuf(QMessage, ThisItem->Recipient, 0);
318 StrBufAppendBufPlain(QMessage, HKEY("|"), 0);
319 StrBufAppendPrintf(QMessage, "%d", ThisItem->Status);
320 StrBufAppendBufPlain(QMessage, HKEY("|"), 0);
321 StrBufAppendBuf(QMessage, ThisItem->StatusMessage, 0);
324 StrBufAppendBufPlain(QMessage, HKEY("\n"), 0);
332 void NewMailQEntry(OneQueItem *Item)
334 Item->Current = (MailQEntry*) malloc(sizeof(MailQEntry));
335 memset(Item->Current, 0, sizeof(MailQEntry));
337 if (Item->MailQEntries == NULL)
338 Item->MailQEntries = NewHash(1, Flathash);
339 Item->Current->StatusMessage = NewStrBuf();
340 Item->Current->n = GetCount(Item->MailQEntries);
341 Put(Item->MailQEntries,
342 IKEY(Item->Current->n),
347 void QItem_Handle_MsgID(OneQueItem *Item, StrBuf *Line, const char **Pos)
349 Item->MessageID = StrBufExtractNext_long(Line, Pos, '|');
352 void QItem_Handle_EnvelopeFrom(OneQueItem *Item, StrBuf *Line, const char **Pos)
354 if (Item->EnvelopeFrom == NULL)
355 Item->EnvelopeFrom = NewStrBufPlain(NULL, StrLength(Line));
356 StrBufExtract_NextToken(Item->EnvelopeFrom, Line, Pos, '|');
359 void QItem_Handle_BounceTo(OneQueItem *Item, StrBuf *Line, const char **Pos)
361 if (Item->BounceTo == NULL)
362 Item->BounceTo = NewStrBufPlain(NULL, StrLength(Line));
363 StrBufExtract_NextToken(Item->BounceTo, Line, Pos, '|');
366 void QItem_Handle_Recipient(OneQueItem *Item, StrBuf *Line, const char **Pos)
368 if (Item->Current == NULL)
370 if (Item->Current->Recipient == NULL)
371 Item->Current->Recipient=NewStrBufPlain(NULL, StrLength(Line));
372 StrBufExtract_NextToken(Item->Current->Recipient, Line, Pos, '|');
373 Item->Current->Status = StrBufExtractNext_int(Line, Pos, '|');
374 StrBufExtract_NextToken(Item->Current->StatusMessage, Line, Pos, '|');
375 Item->Current = NULL; // TODO: is this always right?
379 void QItem_Handle_retry(OneQueItem *Item, StrBuf *Line, const char **Pos)
381 if (Item->Current == NULL)
383 if (Item->Current->Attempts[Item->Current->nAttempts].retry != 0)
384 Item->Current->nAttempts++;
385 if (Item->Current->nAttempts > MaxAttempts) {
389 Item->Current->Attempts[Item->Current->nAttempts].retry =
390 StrBufExtractNext_int(Line, Pos, '|');
394 void QItem_Handle_Submitted(OneQueItem *Item, StrBuf *Line, const char **Pos)
396 Item->Submitted = atol(*Pos);
400 void QItem_Handle_Attempted(OneQueItem *Item, StrBuf *Line, const char **Pos)
402 if (Item->Current == NULL)
404 if (Item->Current->Attempts[Item->Current->nAttempts].when != 0)
405 Item->Current->nAttempts++;
406 if (Item->Current->nAttempts > MaxAttempts) {
411 Item->Current->Attempts[Item->Current->nAttempts].when =
412 StrBufExtractNext_int(Line, Pos, '|');
413 if (Item->Current->Attempts[Item->Current->nAttempts].when >
414 Item->LastAttempt.when)
416 Item->LastAttempt.when =
417 Item->Current->Attempts[Item->Current->nAttempts].when;
419 Item->LastAttempt.retry =
420 Item->Current->Attempts[
421 Item->Current->nAttempts
424 if (Item->LastAttempt.retry > SMTP_RETRY_MAX)
425 Item->LastAttempt.retry = SMTP_RETRY_MAX;
432 * this one has to have the context for loading the message via the redirect buffer...
434 StrBuf *smtp_load_msg(OneQueItem *MyQItem, int n)
439 CCC->redirect_buffer = NewStrBufPlain(NULL, SIZ);
440 CtdlOutputMsg(MyQItem->MessageID,
441 MT_RFC822, HEADERS_ALL,
443 (ESC_DOT|SUPPRESS_ENV_TO) );
445 SendMsg = CCC->redirect_buffer;
446 CCC->redirect_buffer = NULL;
447 if ((StrLength(SendMsg) > 0) &&
448 ChrPtr(SendMsg)[StrLength(SendMsg) - 1] != '\n') {
450 "SMTP client[%d]: Possible problem: message did not "
451 "correctly terminate. (expecting 0x10, got 0x%02x)\n",
452 MsgCount, //yes uncool, but best choice here...
453 ChrPtr(SendMsg)[StrLength(SendMsg) - 1] );
454 StrBufAppendBufPlain(SendMsg, HKEY("\r\n"), 0);
462 * smtp_do_bounce() is caled by smtp_do_procmsg() to scan a set of delivery
463 * instructions for "5" codes (permanent fatal errors) and produce/deliver
464 * a "bounce" message (delivery status notification).
466 void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt)
470 struct CtdlMessage *bmsg = NULL;
474 struct recptypes *valid;
481 int successful_bounce = 0;
485 syslog(LOG_DEBUG, "smtp_do_bounce() called\n");
487 if ( (ev_time() - MyQItem->Submitted) > SMTP_GIVE_UP ) {
488 give_up = 1;/// TODO: replace time by libevq timer get
492 * Now go through the instructions checking for stuff.
494 It = GetNewHashPos(MyQItem->MailQEntries, 0);
495 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
497 MailQEntry *ThisItem = vQE;
498 if ((ThisItem->Status == 5) || /* failed now? */
500 (ThisItem->Status != 2)))
501 /* giving up after failed attempts... */
503 if (num_bounces == 0)
504 Msg = NewStrBufPlain(NULL, 1024);
507 StrBufAppendBuf(Msg, ThisItem->Recipient, 0);
508 StrBufAppendBufPlain(Msg, HKEY(": "), 0);
509 StrBufAppendBuf(Msg, ThisItem->StatusMessage, 0);
510 StrBufAppendBufPlain(Msg, HKEY("\r\n"), 0);
515 /* Deliver the bounce if there's anything worth mentioning */
516 syslog(LOG_DEBUG, "num_bounces = %d\n", num_bounces);
518 if (num_bounces == 0) {
523 boundary = NewStrBufPlain(HKEY("=_Citadel_Multipart_"));
524 StrBufAppendPrintf(boundary,
530 /* Start building our bounce message; go shopping for memory first. */
531 BounceMB = NewStrBufPlain(
533 1024 + /* mime stuff.... */
534 StrLength(Msg) + /* the bounce information... */
535 StrLength(OMsgTxt)); /* the original message */
536 if (BounceMB == NULL) {
537 FreeStrBuf(&boundary);
538 syslog(LOG_ERR, "Failed to alloc() bounce message.\n");
543 bmsg = (struct CtdlMessage *) malloc(sizeof(struct CtdlMessage));
545 FreeStrBuf(&boundary);
546 FreeStrBuf(&BounceMB);
547 syslog(LOG_ERR, "Failed to alloc() bounce message.\n");
551 memset(bmsg, 0, sizeof(struct CtdlMessage));
554 StrBufAppendBufPlain(BounceMB, HKEY("Content-type: multipart/mixed; boundary=\""), 0);
555 StrBufAppendBuf(BounceMB, boundary, 0);
556 StrBufAppendBufPlain(BounceMB, HKEY("\"\r\n"), 0);
557 StrBufAppendBufPlain(BounceMB, HKEY("MIME-Version: 1.0\r\n"), 0);
558 StrBufAppendBufPlain(BounceMB, HKEY("X-Mailer: " CITADEL "\r\n"), 0);
559 StrBufAppendBufPlain(BounceMB, HKEY("\r\nThis is a multipart message in MIME format.\r\n\r\n"), 0);
560 StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
561 StrBufAppendBuf(BounceMB, boundary, 0);
562 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
563 StrBufAppendBufPlain(BounceMB, HKEY("Content-type: text/plain\r\n\r\n"), 0);
566 StrBufAppendBufPlain(
569 "A message you sent could not be delivered "
570 "to some or all of its recipients\n"
571 "due to prolonged unavailability "
572 "of its destination(s).\n"
573 "Giving up on the following addresses:\n\n"
576 StrBufAppendBufPlain(
579 "A message you sent could not be delivered "
580 "to some or all of its recipients.\n"
581 "The following addresses "
582 "were undeliverable:\n\n"
585 StrBufAppendBuf(BounceMB, Msg, 0);
588 /* Attach the original message */
589 StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
590 StrBufAppendBuf(BounceMB, boundary, 0);
591 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
592 StrBufAppendBufPlain(BounceMB,
593 HKEY("Content-type: message/rfc822\r\n"), 0);
594 StrBufAppendBufPlain(BounceMB,
595 HKEY("Content-Transfer-Encoding: 7bit\r\n"), 0);
596 StrBufAppendBufPlain(BounceMB,
597 HKEY("Content-Disposition: inline\r\n"), 0);
598 StrBufAppendBufPlain(BounceMB, HKEY("\r\n"), 0);
599 StrBufAppendBuf(BounceMB, OMsgTxt, 0);
601 /* Close the multipart MIME scope */
602 StrBufAppendBufPlain(BounceMB, HKEY("--"), 0);
603 StrBufAppendBuf(BounceMB, boundary, 0);
604 StrBufAppendBufPlain(BounceMB, HKEY("--\r\n"), 0);
606 bmsg->cm_magic = CTDLMESSAGE_MAGIC;
607 bmsg->cm_anon_type = MES_NORMAL;
608 bmsg->cm_format_type = FMT_RFC822;
610 bmsg->cm_fields['O'] = strdup(MAILROOM);
611 bmsg->cm_fields['A'] = strdup("Citadel");
612 bmsg->cm_fields['N'] = strdup(config.c_nodename);
613 bmsg->cm_fields['U'] = strdup("Delivery Status Notification (Failure)");
614 bmsg->cm_fields['M'] = SmashStrBuf(&BounceMB);
616 /* First try the user who sent the message */
617 if (StrLength(MyQItem->BounceTo) == 0)
618 syslog(LOG_ERR, "No bounce address specified\n");
620 syslog(LOG_DEBUG, "bounce to user? <%s>\n",
621 ChrPtr(MyQItem->BounceTo));
623 /* Can we deliver the bounce to the original sender? */
624 valid = validate_recipients(ChrPtr(MyQItem->BounceTo), NULL, 0);
625 if ((valid != NULL) && (valid->num_error == 0)) {
626 CtdlSubmitMsg(bmsg, valid, "", QP_EADDR);
627 successful_bounce = 1;
630 /* If not, post it in the Aide> room */
631 if (successful_bounce == 0) {
632 CtdlSubmitMsg(bmsg, NULL, config.c_aideroom, QP_EADDR);
635 /* Free up the memory we used */
636 free_recipients(valid);
637 FreeStrBuf(&boundary);
638 CtdlFreeMessage(bmsg);
639 syslog(LOG_DEBUG, "Done processing bounces\n");
645 * Called by smtp_do_queue() to handle an individual message.
647 void smtp_do_procmsg(long msgnum, void *userdata) {
648 struct CtdlMessage *msg = NULL;
658 ParsedURL *RelayUrls = NULL;
662 syslog(LOG_DEBUG, "SMTP Queue: smtp_do_procmsg(%ld)\n", msgnum);
663 ///strcpy(envelope_from, "");
665 msg = CtdlFetchMessage(msgnum, 1);
667 syslog(LOG_ERR, "SMTP Queue: tried %ld but no such message!\n",
672 pch = instr = msg->cm_fields['M'];
674 /* Strip out the headers (no not amd any other non-instruction) line */
675 while (pch != NULL) {
676 pch = strchr(pch, '\n');
677 if ((pch != NULL) && (*(pch + 1) == '\n')) {
682 PlainQItem = NewStrBufPlain(instr, -1);
683 CtdlFreeMessage(msg);
684 MyQItem = DeserializeQueueItem(PlainQItem, msgnum);
685 FreeStrBuf(&PlainQItem);
687 if (MyQItem == NULL) {
689 "SMTP Queue: Msg No %ld: already in progress!\n",
691 return; /* s.b. else is already processing... */
695 * Postpone delivery if we've already tried recently.
697 if (((time(NULL) - MyQItem->LastAttempt.when) <
698 MyQItem->LastAttempt.retry) &&
699 (run_queue_now == 0))
701 syslog(LOG_DEBUG, "SMTP client: Retry time not yet reached.\n");
703 It = GetNewHashPos(MyQItem->MailQEntries, 0);
704 pthread_mutex_lock(&ActiveQItemsLock);
706 if (GetHashPosFromKey(ActiveQItems,
707 LKEY(MyQItem->MessageID),
710 DeleteEntryFromHash(ActiveQItems, It);
713 pthread_mutex_unlock(&ActiveQItemsLock);
714 ////FreeQueItem(&MyQItem); TODO: DeleteEntryFromHash frees this?
720 * Bail out if there's no actual message associated with this
722 if (MyQItem->MessageID < 0L) {
723 syslog(LOG_ERR, "SMTP Queue: no 'msgid' directive found!\n");
724 It = GetNewHashPos(MyQItem->MailQEntries, 0);
725 pthread_mutex_lock(&ActiveQItemsLock);
727 if (GetHashPosFromKey(ActiveQItems,
728 LKEY(MyQItem->MessageID),
731 DeleteEntryFromHash(ActiveQItems, It);
734 pthread_mutex_unlock(&ActiveQItemsLock);
736 ////FreeQueItem(&MyQItem); TODO: DeleteEntryFromHash frees this?
742 ParsedURL **Url = &MyQItem->URL;
743 nRelays = get_hosts(mxbuf, "smarthost");
747 const char *Pos = NULL;
748 All = NewStrBufPlain(mxbuf, -1);
749 One = NewStrBufPlain(NULL, StrLength(All) + 1);
751 while ((Pos != StrBufNOTNULL) &&
755 StrBufExtract_NextToken(One, All, &Pos, '|');
756 if (!ParseURL(Url, One, 25))
758 "Failed to parse: %s\n",
761 ///if (!Url->IsIP)) // todo dupe me fork ipv6
769 Url = &MyQItem->FallBackHost;
770 nRelays = get_hosts(mxbuf, "fallbackhost");
774 const char *Pos = NULL;
775 All = NewStrBufPlain(mxbuf, -1);
776 One = NewStrBufPlain(NULL, StrLength(All) + 1);
778 while ((Pos != StrBufNOTNULL) &&
782 StrBufExtract_NextToken(One, All, &Pos, '|');
783 if (!ParseURL(Url, One, 25))
785 "Failed to parse: %s\n",
795 It = GetNewHashPos(MyQItem->MailQEntries, 0);
796 while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
798 MailQEntry *ThisItem = vQE;
799 syslog(LOG_DEBUG, "SMTP Queue: Task: <%s> %d\n",
800 ChrPtr(ThisItem->Recipient),
805 MyQItem->ActiveDeliveries = CountActiveQueueEntries(MyQItem);
806 if (MyQItem->ActiveDeliveries > 0)
809 int m = MyQItem->ActiveDeliveries;
811 Msg = smtp_load_msg(MyQItem, n);
812 It = GetNewHashPos(MyQItem->MailQEntries, 0);
814 (GetNextHashPos(MyQItem->MailQEntries,
815 It, &len, &Key, &vQE)))
817 MailQEntry *ThisItem = vQE;
819 if (ThisItem->Active == 1)
821 int KeepBuffers = (i == m);
822 if (i > 1) n = MsgCount++;
824 "SMTPQ: Trying <%ld> <%s> %d / %d \n",
826 ChrPtr(ThisItem->Recipient),
829 smtp_try_one_queue_entry(MyQItem,
836 if (KeepBuffers) HaveBuffers = 1;
845 It = GetNewHashPos(MyQItem->MailQEntries, 0);
846 pthread_mutex_lock(&ActiveQItemsLock);
848 if (GetHashPosFromKey(ActiveQItems,
849 LKEY(MyQItem->MessageID),
852 DeleteEntryFromHash(ActiveQItems, It);
861 "SMTP cleanup: unable to find "
862 "QItem with ID[%ld]",
864 while (GetNextHashPos(ActiveQItems,
871 "SMTP cleanup: have: ID[%ld]",
872 ((OneQueItem *)VData)->MessageID);
877 pthread_mutex_unlock(&ActiveQItemsLock);
879 ////FreeQueItem(&MyQItem); TODO: DeleteEntryFromHash frees this?
881 // TODO: bounce & delete?
886 // TODO : free RelayUrls
893 * smtp_queue_thread()
895 * Run through the queue sending out messages.
897 void smtp_do_queue(void) {
898 static int is_running = 0;
899 int num_processed = 0;
902 return; /* Concurrency check - only one can run */
905 pthread_setspecific(MyConKey, (void *)&smtp_queue_CC);
906 syslog(LOG_INFO, "SMTP client: processing outbound queue");
908 if (CtdlGetRoom(&CC->room, SMTP_SPOOLOUT_ROOM) != 0) {
909 syslog(LOG_ERR, "Cannot find room <%s>", SMTP_SPOOLOUT_ROOM);
912 num_processed = CtdlForEachMessage(MSGS_ALL,
921 "SMTP client: queue run completed; %d messages processed",
931 * Initialize the SMTP outbound queue
933 void smtp_init_spoolout(void) {
934 struct ctdlroom qrbuf;
937 * Create the room. This will silently fail if the room already
938 * exists, and that's perfectly ok, because we want it to exist.
940 CtdlCreateRoom(SMTP_SPOOLOUT_ROOM, 3, "", 0, 1, 0, VIEW_MAILBOX);
943 * Make sure it's set to be a "system room" so it doesn't show up
944 * in the <K>nown rooms list for Aides.
946 if (CtdlGetRoomLock(&qrbuf, SMTP_SPOOLOUT_ROOM) == 0) {
947 qrbuf.QRflags2 |= QR2_SYSTEM;
948 CtdlPutRoomLock(&qrbuf);
955 /*****************************************************************************/
956 /* SMTP UTILITY COMMANDS */
957 /*****************************************************************************/
959 void cmd_smtp(char *argbuf) {
966 if (CtdlAccessCheck(ac_aide)) return;
968 extract_token(cmd, argbuf, 0, '|', sizeof cmd);
970 if (!strcasecmp(cmd, "mx")) {
971 extract_token(node, argbuf, 1, '|', sizeof node);
972 num_mxhosts = getmx(buf, node);
973 cprintf("%d %d MX hosts listed for %s\n",
974 LISTING_FOLLOWS, num_mxhosts, node);
975 for (i=0; i<num_mxhosts; ++i) {
976 extract_token(node, buf, i, '|', sizeof node);
977 cprintf("%s\n", node);
983 else if (!strcasecmp(cmd, "runqueue")) {
985 cprintf("%d All outbound SMTP will be retried now.\n", CIT_OK);
990 cprintf("%d Invalid command.\n", ERROR + ILLEGAL_VALUE);
996 CTDL_MODULE_INIT(smtp_queu)
1000 CtdlFillSystemContext(&smtp_queue_CC, "SMTP_Send");
1001 ActiveQItems = NewHash(1, lFlathash);
1002 pthread_mutex_init(&ActiveQItemsLock, NULL);
1004 QItemHandlers = NewHash(0, NULL);
1006 Put(QItemHandlers, HKEY("msgid"), QItem_Handle_MsgID, reference_free_handler);
1007 Put(QItemHandlers, HKEY("envelope_from"), QItem_Handle_EnvelopeFrom, reference_free_handler);
1008 Put(QItemHandlers, HKEY("retry"), QItem_Handle_retry, reference_free_handler);
1009 Put(QItemHandlers, HKEY("attempted"), QItem_Handle_Attempted, reference_free_handler);
1010 Put(QItemHandlers, HKEY("remote"), QItem_Handle_Recipient, reference_free_handler);
1011 Put(QItemHandlers, HKEY("bounceto"), QItem_Handle_BounceTo, reference_free_handler);
1012 Put(QItemHandlers, HKEY("submitted"), QItem_Handle_Submitted, reference_free_handler);
1014 smtp_init_spoolout();
1016 CtdlRegisterCleanupHook(smtp_evq_cleanup);
1018 CtdlRegisterProtoHook(cmd_smtp, "SMTP", "SMTP utility commands");
1019 CtdlRegisterSessionHook(smtp_do_queue, EVT_TIMER);
1022 /* return our Subversion id for the Log */
1023 return "smtpeventclient";