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 void display_smtpqueue_inner_div(void) {
166 message_summary *Msg = NULL;
171 SharedMessageStatus Stat;
173 memset(&Stat, 0, sizeof(SharedMessageStatus));
174 /* Check to see if we can go to the __CitadelSMTPspoolout__ room.
175 * If not, we don't have access to the queue.
177 Buf = NewStrBufPlain(HKEY("__CitadelSMTPspoolout__"));
180 if (!strcasecmp(ChrPtr(WCC->CurRoom.name), "__CitadelSMTPspoolout__")) {
182 Stat.maxload = 10000;
183 Stat.lowest_found = (-1);
184 Stat.highest_found = (-1);
185 // num_msgs = load_msg_ptrs("MSGS ALL|0|1", "SUBJ|;QMSG", &Stat, NULL);
186 num_msgs = load_msg_ptrs("MSGS ", NULL, &Stat, NULL);
188 wc_printf("<table class=\"mailbox_summary\" rules=rows "
189 "cellpadding=2 style=\"width:100%%;\">"
192 wc_printf("<tr><td><b><i>");
193 wc_printf(_("Message ID"));
194 wc_printf("</i></b></td><td><b><i>");
195 wc_printf(_("Date/time submitted"));
196 wc_printf("</i></b></td><td><b><i>");
197 wc_printf(_("Last attempt"));
198 wc_printf("</i></b></td><td><b><i>");
199 wc_printf(_("Sender"));
200 wc_printf("</i></b></td><td><b><i>");
201 wc_printf(_("Recipients"));
202 wc_printf("</i></b></td></tr>\n");
204 for (i=0; (i < num_msgs) && (i < Stat.maxload); ++i) {
205 Msg = GetMessagePtrAt(i, WCC->summ);
207 display_queue_msg(Msg->msgnum);
211 wc_printf("</table>");
215 wc_printf("<br><br><div align=\"center\">");
216 wc_printf(_("The queue is empty."));
217 wc_printf("</div><br><br>");
221 wc_printf("<br><br><div align=\"center\">");
222 wc_printf(_("You do not have permission to view this resource."));
223 wc_printf("</div><br><br>");
225 output_headers(0, 0, 0, 0, 0, 0);
230 * display the outbound SMTP queue
232 void display_smtpqueue(void)
234 output_headers(1, 1, 2, 0, 0, 0);
236 wc_printf("<div id=\"banner\">\n");
238 wc_printf(_("View the outbound SMTP queue"));
239 wc_printf("</h1>\n");
240 wc_printf("</div>\n");
242 wc_printf("<div id=\"content\" class=\"service\">\n");
244 wc_printf("<table class=\"smtpqueue_background\">"
245 "<tr><td valign=top>\n");
247 wc_printf("<div id=\"smtpqueue_inner_div\">"
248 "<div align=\"center\"><img src=\"static/webcit_icons/throbber.gif\"></div>"
250 "<div align=\"center\">"
251 "<a href=\"javascript:RefreshSMTPqueueDisplay();\">%s</a>"
253 "</td></tr></table>\n", _("Refresh this page")
256 StrBufAppendPrintf(WC->trailing_javascript, "RefreshSMTPqueueDisplay();\n");
271 typedef struct _mailq_entry {
273 StrBuf *StatusMessage;
276 * 0 = No delivery has yet been attempted
277 * 2 = Delivery was successful
278 * 4 = A transient error was experienced ... try again later
279 * 5 = Delivery to this address failed permanently. The error message
280 * should be placed in the fourth field so that a bounce message may
288 typedef struct queueitem {
293 HashList *MailQEntries;
294 /* copy of the currently parsed item in the MailQEntries list;
295 * if null add a new one.
298 time_t ReattemptWhen;
301 long ActiveDeliveries;
302 StrBuf *EnvelopeFrom;
305 ParsedURL *FallBackHost;
309 typedef void (*QItemHandler)(OneQueItem *Item, StrBuf *Line, const char **Pos);
312 void FreeMailQEntry(void *qv)
315 FreeStrBuf(&Q->Recipient);
316 FreeStrBuf(&Q->StatusMessage);
319 void FreeQueItem(OneQueItem **Item)
321 DeleteHash(&(*Item)->MailQEntries);
322 FreeStrBuf(&(*Item)->EnvelopeFrom);
323 FreeStrBuf(&(*Item)->BounceTo);
324 FreeURL(&(*Item)->URL);
328 void HFreeQueItem(void *Item)
330 FreeQueItem((OneQueItem**)&Item);
334 OneQueItem *DeserializeQueueItem(StrBuf *RawQItem, long QueMsgID)
337 const char *pLine = NULL;
341 Item = (OneQueItem*)malloc(sizeof(OneQueItem));
342 memset(Item, 0, sizeof(OneQueItem));
344 Item->MessageID = -1;
345 Item->QueMsgID = QueMsgID;
348 Line = NewStrBufPlain(NULL, 128);
349 while (pLine != StrBufNOTNULL) {
350 const char *pItemPart = NULL;
353 StrBufExtract_NextToken(Line, RawQItem, &pLine, '\n');
354 if (StrLength(Line) == 0) continue;
355 StrBufExtract_NextToken(Token, Line, &pItemPart, '|');
356 if (GetHash(QItemHandlers, SKEY(Token), &vHandler))
359 H = (QItemHandler) vHandler;
360 H(Item, Line, &pItemPart);
367 LKEY(Item->MessageID),
375 void tmplput_MailQID(StrBuf *Target, WCTemplputParams *TP)
377 OneQueItem *Item = (OneQueItem*) CTX;
378 StrBufAppendPrintf(Target, "%ld", Item->QueMsgID);;
380 void tmplput_MailQPayloadID(StrBuf *Target, WCTemplputParams *TP)
382 OneQueItem *Item = (OneQueItem*) CTX;
383 StrBufAppendPrintf(Target, "%ld", Item->MessageID);
385 void tmplput_MailQBounceTo(StrBuf *Target, WCTemplputParams *TP)
387 OneQueItem *Item = (OneQueItem*) CTX;
388 StrBufAppendTemplate(Target, TP, Item->BounceTo, 0);
390 void tmplput_MailQAttempted(StrBuf *Target, WCTemplputParams *TP)
393 OneQueItem *Item = (OneQueItem*) CTX;
394 webcit_fmt_date(datebuf, 64, Item->ReattemptWhen, DATEFMT_BRIEF);
395 StrBufAppendBufPlain(Target, datebuf, -1, 0);
397 void tmplput_MailQSubmitted(StrBuf *Target, WCTemplputParams *TP)
400 OneQueItem *Item = (OneQueItem*) CTX;
401 webcit_fmt_date(datebuf, 64, Item->Submitted, DATEFMT_BRIEF);
402 StrBufAppendBufPlain(Target, datebuf, -1, 0);
404 void tmplput_MailQEnvelopeFrom(StrBuf *Target, WCTemplputParams *TP)
406 OneQueItem *Item = (OneQueItem*) CTX;
407 StrBufAppendTemplate(Target, TP, Item->EnvelopeFrom, 0);
409 void tmplput_MailQRetry(StrBuf *Target, WCTemplputParams *TP)
412 OneQueItem *Item = (OneQueItem*) CTX;
414 if (Item->Retry == 0) {
415 StrBufAppendBufPlain(Target, _("First Attempt pending"), -1, 0);
418 webcit_fmt_date(datebuf, sizeof(datebuf), Item->Retry, DATEFMT_BRIEF);
419 StrBufAppendBufPlain(Target, datebuf, -1, 0);
423 void tmplput_MailQRCPT(StrBuf *Target, WCTemplputParams *TP)
425 MailQEntry *Entry = (MailQEntry*) CTX;
426 StrBufAppendTemplate(Target, TP, Entry->Recipient, 0);
428 void tmplput_MailQRCPTStatus(StrBuf *Target, WCTemplputParams *TP)
430 MailQEntry *Entry = (MailQEntry*) CTX;
431 StrBufAppendPrintf(Target, "%ld", Entry->Status);
433 void tmplput_MailQStatusMsg(StrBuf *Target, WCTemplputParams *TP)
435 MailQEntry *Entry = (MailQEntry*) CTX;
436 StrBufAppendTemplate(Target, TP, Entry->StatusMessage, 0);
439 HashList *iterate_get_Recipients(StrBuf *Target, WCTemplputParams *TP)
441 OneQueItem *Item = (OneQueItem*) CTX;
442 return Item->MailQEntries;
446 void NewMailQEntry(OneQueItem *Item)
448 Item->Current = (MailQEntry*) malloc(sizeof(MailQEntry));
449 memset(Item->Current, 0, sizeof(MailQEntry));
451 if (Item->MailQEntries == NULL)
452 Item->MailQEntries = NewHash(1, Flathash);
453 Item->Current->StatusMessage = NewStrBuf();
454 Item->Current->n = GetCount(Item->MailQEntries);
455 Put(Item->MailQEntries,
456 IKEY(Item->Current->n),
461 void QItem_Handle_MsgID(OneQueItem *Item, StrBuf *Line, const char **Pos)
463 Item->MessageID = StrBufExtractNext_long(Line, Pos, '|');
466 void QItem_Handle_EnvelopeFrom(OneQueItem *Item, StrBuf *Line, const char **Pos)
468 if (Item->EnvelopeFrom == NULL)
469 Item->EnvelopeFrom = NewStrBufPlain(NULL, StrLength(Line));
470 StrBufExtract_NextToken(Item->EnvelopeFrom, Line, Pos, '|');
473 void QItem_Handle_BounceTo(OneQueItem *Item, StrBuf *Line, const char **Pos)
475 if (Item->BounceTo == NULL)
476 Item->BounceTo = NewStrBufPlain(NULL, StrLength(Line));
477 StrBufExtract_NextToken(Item->BounceTo, Line, Pos, '|');
480 void QItem_Handle_Recipient(OneQueItem *Item, StrBuf *Line, const char **Pos)
482 if (Item->Current == NULL)
484 if (Item->Current->Recipient == NULL)
485 Item->Current->Recipient=NewStrBufPlain(NULL, StrLength(Line));
486 StrBufExtract_NextToken(Item->Current->Recipient, Line, Pos, '|');
487 Item->Current->Status = StrBufExtractNext_int(Line, Pos, '|');
488 StrBufExtract_NextToken(Item->Current->StatusMessage, Line, Pos, '|');
489 Item->Current = NULL; // TODO: is this always right?
493 void QItem_Handle_retry(OneQueItem *Item, StrBuf *Line, const char **Pos)
496 StrBufExtractNext_int(Line, Pos, '|');
501 void QItem_Handle_Submitted(OneQueItem *Item, StrBuf *Line, const char **Pos)
503 Item->Submitted = atol(*Pos);
507 void QItem_Handle_Attempted(OneQueItem *Item, StrBuf *Line, const char **Pos)
509 Item->ReattemptWhen = StrBufExtractNext_int(Line, Pos, '|');
519 void render_QUEUE(wc_mime_attachment *Mime, StrBuf *RawData, StrBuf *FoundCharset)
521 WCTemplputParams SubTP;
523 memset(&SubTP, 0, sizeof(WCTemplputParams));
524 SubTP.Filter.ContextType = CTX_MAILQITEM;
525 SubTP.Context = DeserializeQueueItem(Mime->Data, Mime->msgnum);
526 DoTemplate(HKEY("view_mailq_message"),NULL, &SubTP);
527 FreeQueItem ((OneQueItem**)&SubTP.Context);
531 ServerShutdownModule_SMTP_QUEUE
534 DeleteHash(&QItemHandlers);
537 ServerStartModule_SMTP_QUEUE
540 QItemHandlers = NewHash(0, NULL);
543 int qview_PrintPageHeader(SharedMessageStatus *Stat, void **ViewSpecific)
547 output_headers(1, 1, 1, 0, 0, 0);
551 output_headers(1, 1, 2, 0, 0, 0);
556 int qview_GetParamsGetServerCall(SharedMessageStatus *Stat,
566 DoTemplate(HKEY("aide_required"), NULL, NULL);
572 snprintf(cmd, len, "MSGS ALL|0|1");
573 snprintf(filter, flen, "SUBJ|QMSG");
574 DoTemplate(HKEY("view_mailq_header"), NULL, NULL);
582 int qview_LoadMsgFromServer(SharedMessageStatus *Stat,
584 message_summary* Msg,
591 /* Not (yet?) needed here? calview *c = (calview *) *ViewSpecific; */
592 read_message(WCC->WBuf, HKEY("view_mailq_message_bearer"), Msg->msgnum, NULL, &Mime);
598 int qview_RenderView_or_Tail(SharedMessageStatus *Stat,
603 WCTemplputParams SubTP;
605 if (GetCount(WCC->summ) == 0)
606 DoTemplate(HKEY("view_mailq_footer_empty"),NULL, &SubTP);
608 DoTemplate(HKEY("view_mailq_footer"),NULL, &SubTP);
612 int qview_Cleanup(void **ViewSpecific)
619 InitModule_SMTP_QUEUE
623 Put(QItemHandlers, HKEY("msgid"), QItem_Handle_MsgID, reference_free_handler);
624 Put(QItemHandlers, HKEY("envelope_from"), QItem_Handle_EnvelopeFrom, reference_free_handler);
625 Put(QItemHandlers, HKEY("retry"), QItem_Handle_retry, reference_free_handler);
626 Put(QItemHandlers, HKEY("attempted"), QItem_Handle_Attempted, reference_free_handler);
627 Put(QItemHandlers, HKEY("remote"), QItem_Handle_Recipient, reference_free_handler);
628 Put(QItemHandlers, HKEY("bounceto"), QItem_Handle_BounceTo, reference_free_handler);
629 Put(QItemHandlers, HKEY("submitted"), QItem_Handle_Submitted, reference_free_handler);
631 WebcitAddUrlHandler(HKEY("display_smtpqueue"), "", 0, display_smtpqueue, 0);
632 WebcitAddUrlHandler(HKEY("display_smtpqueue_inner_div"), "", 0, display_smtpqueue_inner_div, 0);
633 RegisterMimeRenderer(HKEY("application/x-citadel-delivery-list"), render_QUEUE, 1, 9000);
636 RegisterNamespace("MAILQ:ID", 0, 0, tmplput_MailQID, NULL, CTX_MAILQITEM);
637 RegisterNamespace("MAILQ:PAYLOAD:ID", 0, 0, tmplput_MailQPayloadID, NULL, CTX_MAILQITEM);
638 RegisterNamespace("MAILQ:BOUNCETO", 0, 1, tmplput_MailQBounceTo, NULL, CTX_MAILQITEM);
639 RegisterNamespace("MAILQ:ATTEMPTED", 0, 0, tmplput_MailQAttempted, NULL, CTX_MAILQITEM);
640 RegisterNamespace("MAILQ:SUBMITTED", 0, 0, tmplput_MailQSubmitted, NULL, CTX_MAILQITEM);
641 RegisterNamespace("MAILQ:ENVELOPEFROM", 0, 1, tmplput_MailQEnvelopeFrom, NULL, CTX_MAILQITEM);
642 RegisterNamespace("MAILQ:RETRY", 0, 0, tmplput_MailQRetry, NULL, CTX_MAILQITEM);
644 RegisterNamespace("MAILQ:RCPT:ADDR", 0, 1, tmplput_MailQRCPT, NULL, CTX_MAILQ_RCPT);
645 RegisterNamespace("MAILQ:RCPT:STATUS", 0, 0, tmplput_MailQRCPTStatus, NULL, CTX_MAILQ_RCPT);
646 RegisterNamespace("MAILQ:RCPT:STATUSMSG", 0, 1, tmplput_MailQStatusMsg, NULL, CTX_MAILQ_RCPT);
648 RegisterIterator("MAILQ:RCPT", 0, NULL, iterate_get_Recipients,
649 NULL, NULL, CTX_MAILQ_RCPT, CTX_MAILQITEM, IT_NOFLAG);
652 RegisterReadLoopHandlerset(
654 qview_GetParamsGetServerCall,
655 qview_PrintPageHeader,
656 NULL, /* TODO: is this right? */
658 qview_LoadMsgFromServer,
659 qview_RenderView_or_Tail,