2 * Display the outbound SMTP queue
6 HashList *QItemHandlers = NULL;
9 * display one message in the queue
11 void display_queue_msg(long msgnum)
16 int is_delivery_list = 0;
19 time_t last_attempt = 0;
20 int number_of_attempts = 0;
22 char recipients[65536];
23 int recipients_len = 0;
32 strcpy(recipients, "");
35 serv_printf("MSG2 %ld", msgnum);
36 serv_getln(buf, sizeof buf);
37 if (buf[0] != '1') return;
39 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
41 if (!IsEmptyStr(buf)) {
43 if (buf[len - 1] == 13) {
48 if ( (IsEmptyStr(buf)) && (in_body == 0) ) {
53 && (!strncasecmp(buf, "Content-type: application/x-citadel-delivery-list", 49))
58 if ( (in_body) && (!is_delivery_list) ) {
59 while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
60 /* Not a delivery list; flush and return quietly. */
65 if ( (in_body) && (is_delivery_list) ) {
66 extract_token(keyword, buf, 0, '|', sizeof keyword);
68 if (!strcasecmp(keyword, "msgid")) {
69 msgid = extract_long(buf, 1);
72 if (!strcasecmp(keyword, "submitted")) {
73 submitted = extract_long(buf, 1);
76 if (!strcasecmp(keyword, "attempted")) {
77 attempted = extract_long(buf, 1);
79 if (attempted > last_attempt) {
80 last_attempt = attempted;
84 if (!strcasecmp(keyword, "bounceto")) {
86 extract_token(sender, buf, 1, '|', sizeof sender);
88 /* Strip off local hostname if it's our own */
89 atsign = strchr(sender, '@');
92 if (!strcasecmp(atsign, ChrPtr(WC->serv_info->serv_nodename))) {
99 if (!strcasecmp(keyword, "remote")) {
102 extract_token(thisrecp, buf, 1, '|', sizeof thisrecp);
103 extract_token(thisdsn, buf, 3, '|', sizeof thisdsn);
105 if (!IsEmptyStr(thisrecp)) {
106 stresc(thismsg, sizeof thismsg, thisrecp, 1, 1);
107 if (!IsEmptyStr(thisdsn)) {
108 strcat(thismsg, "<br> <i>");
109 stresc(&thismsg[strlen(thismsg)], sizeof thismsg,
111 strcat(thismsg, "</i>");
113 thismsg_len = strlen(thismsg);
115 if ((recipients_len + thismsg_len + 100) < sizeof recipients) {
116 if (!IsEmptyStr(recipients)) {
117 strcpy(&recipients[recipients_len], "<br>");
120 strcpy(&recipients[recipients_len], thismsg);
121 recipients_len += thismsg_len;
131 wc_printf("<tr><td>");
132 wc_printf("%ld<br>", msgnum);
133 wc_printf(" <a href=\"javascript:DeleteSMTPqueueMsg(%ld,%ld);\">%s</a>",
134 msgnum, msgid, _("(Delete)")
137 wc_printf("</td><td>");
139 webcit_fmt_date(buf, 1024, submitted, 1);
140 wc_printf("%s", buf);
146 wc_printf("</td><td>");
147 if (last_attempt > 0) {
148 webcit_fmt_date(buf, 1024, last_attempt, 1);
149 wc_printf("%s", buf);
155 wc_printf("</td><td>");
158 wc_printf("</td><td>");
159 wc_printf("%s", recipients);
160 wc_printf("</td></tr>\n");
165 typedef struct _mailq_entry {
167 StrBuf *StatusMessage;
170 * 0 = No delivery has yet been attempted
171 * 2 = Delivery was successful
172 * 3 = Transient error like connection problem. Try next remote if available.
173 * 4 = A transient error was experienced ... try again later
174 * 5 = Delivery to this address failed permanently. The error message
175 * should be placed in the fourth field so that a bounce message may
183 typedef struct queueitem {
188 HashList *MailQEntries;
189 /* copy of the currently parsed item in the MailQEntries list;
190 * if null add a new one.
193 time_t ReattemptWhen;
196 long ActiveDeliveries;
197 StrBuf *EnvelopeFrom;
201 ParsedURL *FallBackHost;
205 typedef void (*QItemHandler)(OneQueItem *Item, StrBuf *Line, const char **Pos);
207 typedef struct __QItemHandlerStruct {
209 } QItemHandlerStruct;
211 void RegisterQItemHandler(const char *Key, long Len, QItemHandler H)
213 QItemHandlerStruct *HS = (QItemHandlerStruct*)malloc(sizeof(QItemHandlerStruct));
215 Put(QItemHandlers, Key, Len, HS, NULL);
218 void FreeMailQEntry(void *qv)
221 FreeStrBuf(&Q->Recipient);
222 FreeStrBuf(&Q->StatusMessage);
225 void FreeQueItem(OneQueItem **Item)
227 DeleteHash(&(*Item)->MailQEntries);
228 FreeStrBuf(&(*Item)->EnvelopeFrom);
229 FreeStrBuf(&(*Item)->BounceTo);
230 FreeStrBuf(&(*Item)->SenderRoom);
231 FreeURL(&(*Item)->URL);
235 void HFreeQueItem(void *Item)
237 FreeQueItem((OneQueItem**)&Item);
241 OneQueItem *DeserializeQueueItem(StrBuf *RawQItem, long QueMsgID)
244 const char *pLine = NULL;
248 Item = (OneQueItem*)malloc(sizeof(OneQueItem));
249 memset(Item, 0, sizeof(OneQueItem));
251 Item->MessageID = -1;
252 Item->QueMsgID = QueMsgID;
255 Line = NewStrBufPlain(NULL, 128);
256 while (pLine != StrBufNOTNULL) {
257 const char *pItemPart = NULL;
260 StrBufExtract_NextToken(Line, RawQItem, &pLine, '\n');
261 if (StrLength(Line) == 0) continue;
262 StrBufExtract_NextToken(Token, Line, &pItemPart, '|');
263 if (GetHash(QItemHandlers, SKEY(Token), &vHandler))
265 QItemHandlerStruct *HS;
266 HS = (QItemHandlerStruct*) vHandler;
267 HS->H(Item, Line, &pItemPart);
274 LKEY(Item->MessageID),
282 void tmplput_MailQID(StrBuf *Target, WCTemplputParams *TP)
284 OneQueItem *Item = (OneQueItem*) CTX;
285 StrBufAppendPrintf(Target, "%ld", Item->QueMsgID);;
287 void tmplput_MailQPayloadID(StrBuf *Target, WCTemplputParams *TP)
289 OneQueItem *Item = (OneQueItem*) CTX;
290 StrBufAppendPrintf(Target, "%ld", Item->MessageID);
292 void tmplput_MailQBounceTo(StrBuf *Target, WCTemplputParams *TP)
294 OneQueItem *Item = (OneQueItem*) CTX;
295 StrBufAppendTemplate(Target, TP, Item->BounceTo, 0);
297 void tmplput_MailQAttempted(StrBuf *Target, WCTemplputParams *TP)
300 OneQueItem *Item = (OneQueItem*) CTX;
301 webcit_fmt_date(datebuf, 64, Item->ReattemptWhen, DATEFMT_BRIEF);
302 StrBufAppendBufPlain(Target, datebuf, -1, 0);
304 void tmplput_MailQSubmitted(StrBuf *Target, WCTemplputParams *TP)
307 OneQueItem *Item = (OneQueItem*) CTX;
308 webcit_fmt_date(datebuf, 64, Item->Submitted, DATEFMT_BRIEF);
309 StrBufAppendBufPlain(Target, datebuf, -1, 0);
311 void tmplput_MailQEnvelopeFrom(StrBuf *Target, WCTemplputParams *TP)
313 OneQueItem *Item = (OneQueItem*) CTX;
314 StrBufAppendTemplate(Target, TP, Item->EnvelopeFrom, 0);
316 void tmplput_MailQSourceRoom(StrBuf *Target, WCTemplputParams *TP)
318 OneQueItem *Item = (OneQueItem*) CTX;
319 StrBufAppendTemplate(Target, TP, Item->SenderRoom, 0);
322 int Conditional_MailQ_HaveSourceRoom(StrBuf *Target, WCTemplputParams *TP)
324 OneQueItem *Item = (OneQueItem*) CTX;
325 return StrLength(Item->SenderRoom) > 0;
328 void tmplput_MailQRetry(StrBuf *Target, WCTemplputParams *TP)
331 OneQueItem *Item = (OneQueItem*) CTX;
333 if (Item->Retry == 0) {
334 StrBufAppendBufPlain(Target, _("First Attempt pending"), -1, 0);
337 webcit_fmt_date(datebuf, sizeof(datebuf), Item->Retry, DATEFMT_BRIEF);
338 StrBufAppendBufPlain(Target, datebuf, -1, 0);
342 void tmplput_MailQRCPT(StrBuf *Target, WCTemplputParams *TP)
344 MailQEntry *Entry = (MailQEntry*) CTX;
345 StrBufAppendTemplate(Target, TP, Entry->Recipient, 0);
347 void tmplput_MailQRCPTStatus(StrBuf *Target, WCTemplputParams *TP)
349 MailQEntry *Entry = (MailQEntry*) CTX;
350 StrBufAppendPrintf(Target, "%ld", Entry->Status);
352 void tmplput_MailQStatusMsg(StrBuf *Target, WCTemplputParams *TP)
354 MailQEntry *Entry = (MailQEntry*) CTX;
355 StrBufAppendTemplate(Target, TP, Entry->StatusMessage, 0);
358 HashList *iterate_get_Recipients(StrBuf *Target, WCTemplputParams *TP)
360 OneQueItem *Item = (OneQueItem*) CTX;
361 return Item->MailQEntries;
365 void NewMailQEntry(OneQueItem *Item)
367 Item->Current = (MailQEntry*) malloc(sizeof(MailQEntry));
368 memset(Item->Current, 0, sizeof(MailQEntry));
370 if (Item->MailQEntries == NULL)
371 Item->MailQEntries = NewHash(1, Flathash);
372 Item->Current->StatusMessage = NewStrBuf();
373 Item->Current->n = GetCount(Item->MailQEntries);
374 Put(Item->MailQEntries,
375 IKEY(Item->Current->n),
380 void QItem_Handle_MsgID(OneQueItem *Item, StrBuf *Line, const char **Pos)
382 Item->MessageID = StrBufExtractNext_long(Line, Pos, '|');
385 void QItem_Handle_EnvelopeFrom(OneQueItem *Item, StrBuf *Line, const char **Pos)
387 if (Item->EnvelopeFrom == NULL)
388 Item->EnvelopeFrom = NewStrBufPlain(NULL, StrLength(Line));
389 StrBufExtract_NextToken(Item->EnvelopeFrom, Line, Pos, '|');
392 void QItem_Handle_BounceTo(OneQueItem *Item, StrBuf *Line, const char **Pos)
394 if (Item->BounceTo == NULL)
395 Item->BounceTo = NewStrBufPlain(NULL, StrLength(Line));
396 StrBufExtract_NextToken(Item->BounceTo, Line, Pos, '|');
399 void QItem_Handle_SenderRoom(OneQueItem *Item, StrBuf *Line, const char **Pos)
401 if (Item->SenderRoom == NULL)
402 Item->SenderRoom = NewStrBufPlain(NULL, StrLength(Line));
403 StrBufExtract_NextToken(Item->SenderRoom, Line, Pos, '|');
406 void QItem_Handle_Recipient(OneQueItem *Item, StrBuf *Line, const char **Pos)
408 if (Item->Current == NULL)
410 if (Item->Current->Recipient == NULL)
411 Item->Current->Recipient=NewStrBufPlain(NULL, StrLength(Line));
412 StrBufExtract_NextToken(Item->Current->Recipient, Line, Pos, '|');
413 Item->Current->Status = StrBufExtractNext_int(Line, Pos, '|');
414 StrBufExtract_NextToken(Item->Current->StatusMessage, Line, Pos, '|');
415 Item->Current = NULL; // TODO: is this always right?
419 void QItem_Handle_retry(OneQueItem *Item, StrBuf *Line, const char **Pos)
421 Item->Retry = StrBufExtractNext_int(Line, Pos, '|');
425 void QItem_Handle_Submitted(OneQueItem *Item, StrBuf *Line, const char **Pos)
427 Item->Submitted = atol(*Pos);
431 void QItem_Handle_Attempted(OneQueItem *Item, StrBuf *Line, const char **Pos)
433 Item->ReattemptWhen = StrBufExtractNext_int(Line, Pos, '|');
443 void render_QUEUE(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
445 WCTemplputParams SubTP;
447 memset(&SubTP, 0, sizeof(WCTemplputParams));
448 SubTP.Filter.ContextType = CTX_MAILQITEM;
449 SubTP.Context = DeserializeQueueItem(Mime->Data, Mime->msgnum);
450 DoTemplate(HKEY("view_mailq_message"),NULL, &SubTP);
451 FreeQueItem ((OneQueItem**)&SubTP.Context);
455 ServerShutdownModule_SMTP_QUEUE
458 DeleteHash(&QItemHandlers);
461 ServerStartModule_SMTP_QUEUE
464 QItemHandlers = NewHash(0, NULL);
467 int qview_PrintPageHeader(SharedMessageStatus *Stat, void **ViewSpecific)
469 output_headers(1, 1, 1, 0, 0, 0);
473 int qview_GetParamsGetServerCall(SharedMessageStatus *Stat,
483 DoTemplate(HKEY("aide_required"), NULL, NULL);
489 snprintf(cmd, len, "MSGS ALL|0|1");
490 snprintf(filter, flen, "SUBJ|QMSG");
491 DoTemplate(HKEY("view_mailq_header"), NULL, NULL);
499 int qview_LoadMsgFromServer(SharedMessageStatus *Stat,
501 message_summary* Msg,
508 /* Not (yet?) needed here? calview *c = (calview *) *ViewSpecific; */
509 read_message(WCC->WBuf, HKEY("view_mailq_message_bearer"), Msg->msgnum, NULL, &Mime);
515 int qview_RenderView_or_Tail(SharedMessageStatus *Stat,
520 WCTemplputParams SubTP;
522 if (GetCount(WCC->summ) == 0)
523 DoTemplate(HKEY("view_mailq_footer_empty"),NULL, &SubTP);
525 DoTemplate(HKEY("view_mailq_footer"),NULL, &SubTP);
529 int qview_Cleanup(void **ViewSpecific)
536 InitModule_SMTP_QUEUE
540 RegisterQItemHandler(HKEY("msgid"), QItem_Handle_MsgID);
541 RegisterQItemHandler(HKEY("envelope_from"), QItem_Handle_EnvelopeFrom);
542 RegisterQItemHandler(HKEY("retry"), QItem_Handle_retry);
543 RegisterQItemHandler(HKEY("attempted"), QItem_Handle_Attempted);
544 RegisterQItemHandler(HKEY("remote"), QItem_Handle_Recipient);
545 RegisterQItemHandler(HKEY("bounceto"), QItem_Handle_BounceTo);
546 RegisterQItemHandler(HKEY("source_room"), QItem_Handle_SenderRoom);
547 RegisterQItemHandler(HKEY("submitted"), QItem_Handle_Submitted);
548 RegisterMimeRenderer(HKEY("application/x-citadel-delivery-list"), render_QUEUE, 1, 9000);
549 RegisterNamespace("MAILQ:ID", 0, 0, tmplput_MailQID, NULL, CTX_MAILQITEM);
550 RegisterNamespace("MAILQ:PAYLOAD:ID", 0, 0, tmplput_MailQPayloadID, NULL, CTX_MAILQITEM);
551 RegisterNamespace("MAILQ:BOUNCETO", 0, 1, tmplput_MailQBounceTo, NULL, CTX_MAILQITEM);
552 RegisterNamespace("MAILQ:ATTEMPTED", 0, 0, tmplput_MailQAttempted, NULL, CTX_MAILQITEM);
553 RegisterNamespace("MAILQ:SUBMITTED", 0, 0, tmplput_MailQSubmitted, NULL, CTX_MAILQITEM);
554 RegisterNamespace("MAILQ:ENVELOPEFROM", 0, 1, tmplput_MailQEnvelopeFrom, NULL, CTX_MAILQITEM);
555 RegisterNamespace("MAILQ:SRCROOM", 0, 1, tmplput_MailQSourceRoom, NULL, CTX_MAILQITEM);
556 RegisterConditional(HKEY("COND:MAILQ:HAVESRCROOM"), 0, Conditional_MailQ_HaveSourceRoom, CTX_MAILQITEM);
557 RegisterNamespace("MAILQ:RETRY", 0, 0, tmplput_MailQRetry, NULL, CTX_MAILQITEM);
559 RegisterNamespace("MAILQ:RCPT:ADDR", 0, 1, tmplput_MailQRCPT, NULL, CTX_MAILQ_RCPT);
560 RegisterNamespace("MAILQ:RCPT:STATUS", 0, 0, tmplput_MailQRCPTStatus, NULL, CTX_MAILQ_RCPT);
561 RegisterNamespace("MAILQ:RCPT:STATUSMSG", 0, 1, tmplput_MailQStatusMsg, NULL, CTX_MAILQ_RCPT);
563 RegisterIterator("MAILQ:RCPT", 0, NULL, iterate_get_Recipients,
564 NULL, NULL, CTX_MAILQ_RCPT, CTX_MAILQITEM, IT_NOFLAG);
567 RegisterReadLoopHandlerset(
569 qview_GetParamsGetServerCall,
570 qview_PrintPageHeader,
571 NULL, /* TODO: is this right? */
573 qview_LoadMsgFromServer,
574 qview_RenderView_or_Tail,