Back to index

webcit  8.12-dfsg
smtpqueue.c
Go to the documentation of this file.
00001 /* 
00002  * Display the outbound SMTP queue
00003  */
00004 
00005 #include "webcit.h"
00006 HashList *QItemHandlers = NULL;
00007 
00008 
00009 
00010 typedef struct _mailq_entry {
00011        StrBuf *Recipient;
00012        StrBuf *StatusMessage;
00013        int Status;
00024        int n;
00025        int Active;
00026 }MailQEntry;
00027 
00028 typedef struct queueitem {
00029        long MessageID;
00030        long QueMsgID;
00031        long Submitted;
00032        int FailNow;
00033        HashList *MailQEntries;
00034 /* copy of the currently parsed item in the MailQEntries list;
00035  * if null add a new one.
00036  */
00037        MailQEntry *Current;
00038        time_t ReattemptWhen;
00039        time_t Retry;
00040 
00041        long ActiveDeliveries;
00042        StrBuf *EnvelopeFrom;
00043        StrBuf *BounceTo;
00044        StrBuf *SenderRoom;
00045        ParsedURL *URL;
00046        ParsedURL *FallBackHost;
00047 } OneQueItem;
00048 
00049 
00050 typedef void (*QItemHandler)(OneQueItem *Item, StrBuf *Line, const char **Pos);
00051 
00052 typedef struct __QItemHandlerStruct {
00053        QItemHandler H;
00054 } QItemHandlerStruct;
00055 
00056 void RegisterQItemHandler(const char *Key, long Len, QItemHandler H)
00057 {
00058        QItemHandlerStruct *HS = (QItemHandlerStruct*)malloc(sizeof(QItemHandlerStruct));
00059        HS->H = H;
00060        Put(QItemHandlers, Key, Len, HS, NULL);
00061 }
00062 
00063 void FreeMailQEntry(void *qv)
00064 {
00065        MailQEntry *Q = qv;
00066        FreeStrBuf(&Q->Recipient);
00067        FreeStrBuf(&Q->StatusMessage);
00068        free(Q);
00069 }
00070 void FreeQueItem(OneQueItem **Item)
00071 {
00072        DeleteHash(&(*Item)->MailQEntries);
00073        FreeStrBuf(&(*Item)->EnvelopeFrom);
00074        FreeStrBuf(&(*Item)->BounceTo);
00075        FreeStrBuf(&(*Item)->SenderRoom);
00076        FreeURL(&(*Item)->URL);
00077        free(*Item);
00078        Item = NULL;
00079 }
00080 void HFreeQueItem(void *Item)
00081 {
00082        FreeQueItem((OneQueItem**)&Item);
00083 }
00084 
00085 
00086 OneQueItem *DeserializeQueueItem(StrBuf *RawQItem, long QueMsgID)
00087 {
00088        OneQueItem *Item;
00089        const char *pLine = NULL;
00090        StrBuf *Line;
00091        StrBuf *Token;
00092        
00093        Item = (OneQueItem*)malloc(sizeof(OneQueItem));
00094        memset(Item, 0, sizeof(OneQueItem));
00095        Item->Retry = 0;
00096        Item->MessageID = -1;
00097        Item->QueMsgID = QueMsgID;
00098 
00099        Token = NewStrBuf();
00100        Line = NewStrBufPlain(NULL, 128);
00101        while (pLine != StrBufNOTNULL) {
00102               const char *pItemPart = NULL;
00103               void *vHandler;
00104 
00105               StrBufExtract_NextToken(Line, RawQItem, &pLine, '\n');
00106               if (StrLength(Line) == 0) continue;
00107               StrBufExtract_NextToken(Token, Line, &pItemPart, '|');
00108               if (GetHash(QItemHandlers, SKEY(Token), &vHandler))
00109               {
00110                      QItemHandlerStruct *HS;
00111                      HS = (QItemHandlerStruct*) vHandler;
00112                      HS->H(Item, Line, &pItemPart);
00113               }
00114        }
00115        FreeStrBuf(&Line);
00116        FreeStrBuf(&Token);
00117 /*
00118        Put(ActiveQItems,
00119            LKEY(Item->MessageID),
00120            Item,
00121            HFreeQueItem);
00122 */     
00123 
00124        return Item;
00125 }
00126 
00127 void tmplput_MailQID(StrBuf *Target, WCTemplputParams *TP)
00128 {
00129        OneQueItem *Item = (OneQueItem*) CTX;
00130        StrBufAppendPrintf(Target, "%ld", Item->QueMsgID);;
00131 }
00132 void tmplput_MailQPayloadID(StrBuf *Target, WCTemplputParams *TP)
00133 {
00134        OneQueItem *Item = (OneQueItem*) CTX;
00135        StrBufAppendPrintf(Target, "%ld", Item->MessageID);
00136 }
00137 void tmplput_MailQBounceTo(StrBuf *Target, WCTemplputParams *TP)
00138 {
00139        OneQueItem *Item = (OneQueItem*) CTX;
00140        StrBufAppendTemplate(Target, TP, Item->BounceTo, 0);
00141 }
00142 void tmplput_MailQAttempted(StrBuf *Target, WCTemplputParams *TP)
00143 {
00144         char datebuf[64];
00145        OneQueItem *Item = (OneQueItem*) CTX;
00146         webcit_fmt_date(datebuf, 64, Item->ReattemptWhen, DATEFMT_BRIEF);
00147         StrBufAppendBufPlain(Target, datebuf, -1, 0);
00148 }
00149 void tmplput_MailQSubmitted(StrBuf *Target, WCTemplputParams *TP)
00150 {
00151         char datebuf[64];
00152        OneQueItem *Item = (OneQueItem*) CTX;
00153         webcit_fmt_date(datebuf, 64, Item->Submitted, DATEFMT_BRIEF);
00154         StrBufAppendBufPlain(Target, datebuf, -1, 0);
00155 }
00156 void tmplput_MailQEnvelopeFrom(StrBuf *Target, WCTemplputParams *TP)
00157 {
00158        OneQueItem *Item = (OneQueItem*) CTX;
00159        StrBufAppendTemplate(Target, TP, Item->EnvelopeFrom, 0);
00160 }
00161 void tmplput_MailQSourceRoom(StrBuf *Target, WCTemplputParams *TP)
00162 {
00163        OneQueItem *Item = (OneQueItem*) CTX;
00164        StrBufAppendTemplate(Target, TP, Item->SenderRoom, 0);
00165 }
00166 
00167 int Conditional_MailQ_HaveSourceRoom(StrBuf *Target, WCTemplputParams *TP)
00168 {
00169        OneQueItem *Item = (OneQueItem*) CTX;
00170        return StrLength(Item->SenderRoom) > 0;
00171 }
00172 
00173 void tmplput_MailQRetry(StrBuf *Target, WCTemplputParams *TP)
00174 {
00175         char datebuf[64];
00176        OneQueItem *Item = (OneQueItem*) CTX;
00177 
00178        if (Item->Retry == 0) {
00179               StrBufAppendBufPlain(Target, _("First Attempt pending"), -1, 0);
00180        }
00181        else {
00182               webcit_fmt_date(datebuf, sizeof(datebuf), Item->Retry, DATEFMT_BRIEF);
00183               StrBufAppendBufPlain(Target, datebuf, -1, 0);
00184        }
00185 }
00186 
00187 void tmplput_MailQRCPT(StrBuf *Target, WCTemplputParams *TP)
00188 {
00189        MailQEntry *Entry = (MailQEntry*) CTX;
00190        StrBufAppendTemplate(Target, TP, Entry->Recipient, 0);
00191 }
00192 void tmplput_MailQRCPTStatus(StrBuf *Target, WCTemplputParams *TP)
00193 {
00194        MailQEntry *Entry = (MailQEntry*) CTX;
00195        StrBufAppendPrintf(Target, "%ld", Entry->Status);
00196 }
00197 void tmplput_MailQStatusMsg(StrBuf *Target, WCTemplputParams *TP)
00198 {
00199        MailQEntry *Entry = (MailQEntry*) CTX;
00200        StrBufAppendTemplate(Target, TP, Entry->StatusMessage, 0);
00201 }
00202 
00203 HashList *iterate_get_Recipients(StrBuf *Target, WCTemplputParams *TP)
00204 {
00205        OneQueItem *Item = (OneQueItem*) CTX;
00206        return Item->MailQEntries;
00207 }
00208 
00209 
00210 void NewMailQEntry(OneQueItem *Item)
00211 {
00212        Item->Current = (MailQEntry*) malloc(sizeof(MailQEntry));
00213        memset(Item->Current, 0, sizeof(MailQEntry));
00214 
00215        if (Item->MailQEntries == NULL)
00216               Item->MailQEntries = NewHash(1, Flathash);
00217        Item->Current->StatusMessage = NewStrBuf();
00218        Item->Current->n = GetCount(Item->MailQEntries);
00219        Put(Item->MailQEntries,
00220            IKEY(Item->Current->n),
00221            Item->Current,
00222            FreeMailQEntry);
00223 }
00224 
00225 void QItem_Handle_MsgID(OneQueItem *Item, StrBuf *Line, const char **Pos)
00226 {
00227        Item->MessageID = StrBufExtractNext_long(Line, Pos, '|');
00228 }
00229 
00230 void QItem_Handle_EnvelopeFrom(OneQueItem *Item, StrBuf *Line, const char **Pos)
00231 {
00232        if (Item->EnvelopeFrom == NULL)
00233               Item->EnvelopeFrom = NewStrBufPlain(NULL, StrLength(Line));
00234        StrBufExtract_NextToken(Item->EnvelopeFrom, Line, Pos, '|');
00235 }
00236 
00237 void QItem_Handle_BounceTo(OneQueItem *Item, StrBuf *Line, const char **Pos)
00238 {
00239        if (Item->BounceTo == NULL)
00240               Item->BounceTo = NewStrBufPlain(NULL, StrLength(Line));
00241        StrBufExtract_NextToken(Item->BounceTo, Line, Pos, '|');
00242 }
00243 
00244 void QItem_Handle_SenderRoom(OneQueItem *Item, StrBuf *Line, const char **Pos)
00245 {
00246        if (Item->SenderRoom == NULL)
00247               Item->SenderRoom = NewStrBufPlain(NULL, StrLength(Line));
00248        StrBufExtract_NextToken(Item->SenderRoom, Line, Pos, '|');
00249 }
00250 
00251 void QItem_Handle_Recipient(OneQueItem *Item, StrBuf *Line, const char **Pos)
00252 {
00253        if (Item->Current == NULL)
00254               NewMailQEntry(Item);
00255        if (Item->Current->Recipient == NULL)
00256               Item->Current->Recipient=NewStrBufPlain(NULL, StrLength(Line));
00257        StrBufExtract_NextToken(Item->Current->Recipient, Line, Pos, '|');
00258        Item->Current->Status = StrBufExtractNext_int(Line, Pos, '|');
00259        StrBufExtract_NextToken(Item->Current->StatusMessage, Line, Pos, '|');
00260        Item->Current = NULL; // TODO: is this always right?
00261 }
00262 
00263 
00264 void QItem_Handle_retry(OneQueItem *Item, StrBuf *Line, const char **Pos)
00265 {
00266        Item->Retry = StrBufExtractNext_int(Line, Pos, '|');
00267 }
00268 
00269 
00270 void QItem_Handle_Submitted(OneQueItem *Item, StrBuf *Line, const char **Pos)
00271 {
00272        Item->Submitted = atol(*Pos);
00273 
00274 }
00275 
00276 void QItem_Handle_Attempted(OneQueItem *Item, StrBuf *Line, const char **Pos)
00277 {
00278        Item->ReattemptWhen = StrBufExtractNext_int(Line, Pos, '|');
00279 }
00280 
00281 
00282 
00283 
00284 
00285 
00286 
00287 
00288 void render_QUEUE(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
00289 {
00290        WCTemplputParams SubTP;
00291 
00292        memset(&SubTP, 0, sizeof(WCTemplputParams));
00293        SubTP.Filter.ContextType = CTX_MAILQITEM;
00294        SubTP.Context = DeserializeQueueItem(Mime->Data, Mime->msgnum);
00295        DoTemplate(HKEY("view_mailq_message"),NULL, &SubTP);
00296        FreeQueItem ((OneQueItem**)&SubTP.Context);
00297 }
00298 
00299 void
00300 ServerShutdownModule_SMTP_QUEUE
00301 (void)
00302 {
00303        DeleteHash(&QItemHandlers);
00304 }
00305 void
00306 ServerStartModule_SMTP_QUEUE
00307 (void)
00308 {
00309        QItemHandlers = NewHash(0, NULL);
00310 }
00311 
00312 int qview_PrintPageHeader(SharedMessageStatus *Stat, void **ViewSpecific)
00313 {
00314        output_headers(1, 1, 1, 0, 0, 0);
00315        return 0;
00316 }
00317 
00318 int qview_GetParamsGetServerCall(SharedMessageStatus *Stat,
00319                              void **ViewSpecific,
00320                              long oper,
00321                              char *cmd,
00322                              long len,
00323                              char *filter,
00324                              long flen)
00325 {
00326        if (!WC->is_aide)
00327        {
00328               DoTemplate(HKEY("aide_required"), NULL, NULL);
00329               end_burst();
00330 
00331               return 300;
00332        }
00333        else {
00334               snprintf(cmd, len, "MSGS ALL|0|1");
00335               snprintf(filter, flen, "SUBJ|QMSG");
00336               DoTemplate(HKEY("view_mailq_header"), NULL, NULL);
00337               return 200;
00338        }
00339 }
00340 
00341 /*
00342  * Display task view
00343  */
00344 int qview_LoadMsgFromServer(SharedMessageStatus *Stat, 
00345                             void **ViewSpecific, 
00346                             message_summary* Msg, 
00347                             int is_new, 
00348                             int i)
00349 {
00350        wcsession *WCC = WC;
00351        const StrBuf *Mime;
00352 
00353         /* Not (yet?) needed here? calview *c = (calview *) *ViewSpecific; */
00354        read_message(WCC->WBuf, HKEY("view_mailq_message_bearer"), Msg->msgnum, NULL, &Mime);
00355 
00356         return 0;
00357 }
00358 
00359 
00360 int qview_RenderView_or_Tail(SharedMessageStatus *Stat, 
00361                           void **ViewSpecific, 
00362                           long oper)
00363 {
00364        wcsession *WCC = WC;
00365        WCTemplputParams SubTP;
00366 
00367        if (GetCount(WCC->summ) == 0)
00368               DoTemplate(HKEY("view_mailq_footer_empty"),NULL, &SubTP);
00369        else
00370               DoTemplate(HKEY("view_mailq_footer"),NULL, &SubTP);
00371        
00372        return 0;
00373 }
00374 int qview_Cleanup(void **ViewSpecific)
00375 {
00376        wDumpContent(1);
00377        return 0;
00378 }
00379 
00380 void 
00381 InitModule_SMTP_QUEUE
00382 (void)
00383 {
00384 
00385        RegisterQItemHandler(HKEY("msgid"),              QItem_Handle_MsgID);
00386        RegisterQItemHandler(HKEY("envelope_from"),      QItem_Handle_EnvelopeFrom);
00387        RegisterQItemHandler(HKEY("retry"),              QItem_Handle_retry);
00388        RegisterQItemHandler(HKEY("attempted"),          QItem_Handle_Attempted);
00389        RegisterQItemHandler(HKEY("remote"),             QItem_Handle_Recipient);
00390        RegisterQItemHandler(HKEY("bounceto"),           QItem_Handle_BounceTo);
00391        RegisterQItemHandler(HKEY("source_room"), QItem_Handle_SenderRoom);
00392        RegisterQItemHandler(HKEY("submitted"),          QItem_Handle_Submitted);
00393        RegisterMimeRenderer(HKEY("application/x-citadel-delivery-list"), render_QUEUE, 1, 9000);
00394        RegisterNamespace("MAILQ:ID", 0, 0, tmplput_MailQID, NULL, CTX_MAILQITEM);
00395        RegisterNamespace("MAILQ:PAYLOAD:ID", 0, 0, tmplput_MailQPayloadID, NULL, CTX_MAILQITEM);
00396        RegisterNamespace("MAILQ:BOUNCETO", 0, 1, tmplput_MailQBounceTo, NULL, CTX_MAILQITEM);
00397        RegisterNamespace("MAILQ:ATTEMPTED", 0, 0, tmplput_MailQAttempted, NULL, CTX_MAILQITEM);
00398        RegisterNamespace("MAILQ:SUBMITTED", 0, 0, tmplput_MailQSubmitted, NULL, CTX_MAILQITEM);
00399        RegisterNamespace("MAILQ:ENVELOPEFROM", 0, 1, tmplput_MailQEnvelopeFrom, NULL, CTX_MAILQITEM);
00400        RegisterNamespace("MAILQ:SRCROOM", 0, 1, tmplput_MailQSourceRoom, NULL, CTX_MAILQITEM);
00401        RegisterConditional(HKEY("COND:MAILQ:HAVESRCROOM"), 0, Conditional_MailQ_HaveSourceRoom,  CTX_MAILQITEM);
00402        RegisterNamespace("MAILQ:RETRY", 0, 0, tmplput_MailQRetry, NULL, CTX_MAILQITEM);
00403 
00404        RegisterNamespace("MAILQ:RCPT:ADDR", 0, 1, tmplput_MailQRCPT, NULL, CTX_MAILQ_RCPT);
00405        RegisterNamespace("MAILQ:RCPT:STATUS", 0, 0, tmplput_MailQRCPTStatus, NULL, CTX_MAILQ_RCPT);
00406        RegisterNamespace("MAILQ:RCPT:STATUSMSG", 0, 1, tmplput_MailQStatusMsg, NULL, CTX_MAILQ_RCPT);
00407 
00408        RegisterIterator("MAILQ:RCPT", 0, NULL, iterate_get_Recipients, 
00409                       NULL, NULL, CTX_MAILQ_RCPT, CTX_MAILQITEM, IT_NOFLAG);
00410 
00411 
00412        RegisterReadLoopHandlerset(
00413               VIEW_QUEUE,
00414               qview_GetParamsGetServerCall,
00415               qview_PrintPageHeader,
00416               NULL, /* TODO: is this right? */
00417               NULL,
00418               qview_LoadMsgFromServer,
00419               qview_RenderView_or_Tail,
00420               qview_Cleanup);
00421 
00422 }