]> code.citadel.org Git - citadel.git/blobdiff - citadel/modules/smtp/serv_smtpqueue.c
Don't put functionpointer into the hash directly, some gcc warninglevels don't like...
[citadel.git] / citadel / modules / smtp / serv_smtpqueue.c
index 00b60f8fa85281f6fca198c92cb7fcc38e3ecb2e..4704d31ce75ef7267d833e97b2e5561c3c7712f9 100644 (file)
@@ -101,6 +101,15 @@ static const long MaxRetry = SMTP_RETRY_INTERVAL * 2 * 2 * 2 * 2 * 2 * 2 * 2 * 2
 int MsgCount            = 0;
 int run_queue_now       = 0;   /* Set to 1 to ignore SMTP send retry times */
 
+void RegisterQItemHandler(const char *Key, long Len, QItemHandler H)
+{
+       QItemHandlerStruct *HS = (QItemHandlerStruct*)malloc(sizeof(QItemHandlerStruct));
+       HS->H = H;
+       Put(QItemHandlers, Key, Len, HS, NULL);
+}
+
+
+
 void smtp_try_one_queue_entry(OneQueItem *MyQItem,
                              MailQEntry *MyQEntry,
                              StrBuf *MsgText,
@@ -270,9 +279,9 @@ OneQueItem *DeserializeQueueItem(StrBuf *RawQItem, long QueMsgID)
                StrBufExtract_NextToken(Token, Line, &pItemPart, '|');
                if (GetHash(QItemHandlers, SKEY(Token), &vHandler))
                {
-                       QItemHandler H;
-                       H = (QItemHandler) vHandler;
-                       H(Item, Line, &pItemPart);
+                       QItemHandlerStruct *HS;
+                       HS = (QItemHandlerStruct*) vHandler;
+                       HS->H(Item, Line, &pItemPart);
                }
        }
        FreeStrBuf(&Line);
@@ -341,7 +350,7 @@ StrBuf *SerializeQueueItem(OneQueItem *MyQItem)
 
        StrBufAppendBufPlain(QMessage, HKEY("\nattempted|"), 0);
        StrBufAppendPrintf(QMessage, "%ld",
-                          MyQItem->ReattemptWhen);
+                          ctdl_ev_now() + MyQItem->Retry);
 
        It = GetNewHashPos(MyQItem->MailQEntries, 0);
        while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
@@ -422,7 +431,10 @@ void QItem_Handle_retry(OneQueItem *Item, StrBuf *Line, const char **Pos)
 {
        Item->Retry =
                StrBufExtractNext_int(Line, Pos, '|');
-       Item->Retry *= 2;
+       if (Item->Retry == 0)
+               Item->Retry = SMTP_RETRY_INTERVAL;
+       else
+               Item->Retry *= 2;
 }
 
 
@@ -483,39 +495,46 @@ void smtpq_do_bounce(OneQueItem *MyQItem, StrBuf *OMsgTxt)
        StrBuf *Msg = NULL;
        StrBuf *BounceMB;
        struct recptypes *valid;
+       time_t now;
 
        HashPos *It;
        void *vQE;
        long len;
        const char *Key;
 
+       int first_attempt = 0;
        int successful_bounce = 0;
        int num_bounces = 0;
        int give_up = 0;
 
        syslog(LOG_DEBUG, "smtp_do_bounce() called\n");
 
-       if (!MyQItem->SendBounceMail)
+       if (MyQItem->SendBounceMail == 0)
                return;
 
-       if ( (ev_time() - MyQItem->Submitted) > SMTP_GIVE_UP ) {
-               give_up = 1;/// TODO: replace time by libevq timer get
+       now = ev_time();
+
+       if ( (now - MyQItem->Submitted) > SMTP_GIVE_UP ) {
+               give_up = 1;
+       }
+
+       if (MyQItem->Retry == SMTP_RETRY_INTERVAL) {
+               first_attempt = 1;
        }
 
        /*
         * Now go through the instructions checking for stuff.
         */
+       Msg = NewStrBufPlain(NULL, 1024);
        It = GetNewHashPos(MyQItem->MailQEntries, 0);
        while (GetNextHashPos(MyQItem->MailQEntries, It, &len, &Key, &vQE))
        {
                MailQEntry *ThisItem = vQE;
-               if ((ThisItem->Status == 5) || /* failed now? */
-                   ((give_up == 1) &&
-                    (ThisItem->Status != 2)))
+               if ((ThisItem->Active && (ThisItem->Status == 5)) || /* failed now? */
+                   ((give_up == 1) && (ThisItem->Status != 2)) ||
+                   ((first_attempt == 1) && (ThisItem->Status != 2)))
                        /* giving up after failed attempts... */
                {
-                       if (num_bounces == 0)
-                               Msg = NewStrBufPlain(NULL, 1024);
                        ++num_bounces;
 
                        StrBufAppendBuf(Msg, ThisItem->Recipient, 0);
@@ -1082,14 +1101,15 @@ CTDL_MODULE_INIT(smtp_queu)
 
                QItemHandlers = NewHash(0, NULL);
 
-               Put(QItemHandlers, HKEY("msgid"), QItem_Handle_MsgID, reference_free_handler);
-               Put(QItemHandlers, HKEY("envelope_from"), QItem_Handle_EnvelopeFrom, reference_free_handler);
-               Put(QItemHandlers, HKEY("retry"), QItem_Handle_retry, reference_free_handler);
-               Put(QItemHandlers, HKEY("attempted"), QItem_Handle_Attempted, reference_free_handler);
-               Put(QItemHandlers, HKEY("remote"), QItem_Handle_Recipient, reference_free_handler);
-               Put(QItemHandlers, HKEY("bounceto"), QItem_Handle_BounceTo, reference_free_handler);
-               Put(QItemHandlers, HKEY("source_room"), QItem_Handle_SenderRoom, reference_free_handler);
-               Put(QItemHandlers, HKEY("submitted"), QItem_Handle_Submitted, reference_free_handler);
+               RegisterQItemHandler(HKEY("msgid"),             QItem_Handle_MsgID);
+               RegisterQItemHandler(HKEY("envelope_from"),     QItem_Handle_EnvelopeFrom);
+               RegisterQItemHandler(HKEY("retry"),             QItem_Handle_retry);
+               RegisterQItemHandler(HKEY("attempted"),         QItem_Handle_Attempted);
+               RegisterQItemHandler(HKEY("remote"),            QItem_Handle_Recipient);
+               RegisterQItemHandler(HKEY("bounceto"),          QItem_Handle_BounceTo);
+               RegisterQItemHandler(HKEY("source_room"),       QItem_Handle_SenderRoom);
+               RegisterQItemHandler(HKEY("submitted"),         QItem_Handle_Submitted);
+
                smtp_init_spoolout();
 
                CtdlRegisterEVCleanupHook(smtp_evq_cleanup);