c73e9a25613502cb228768a58cde1414679e8044
[citadel.git] / webcit / smtpqueue.c
1 /* 
2  * Display the outbound SMTP queue
3  */
4
5 #include "webcit.h"
6
7 /*
8  * display one message in the queue
9  */
10 void display_queue_msg(long msgnum)
11 {
12         char buf[1024];
13         char keyword[32];
14         int in_body = 0;
15         int is_delivery_list = 0;
16         time_t submitted = 0;
17         time_t attempted = 0;
18         time_t last_attempt = 0;
19         int number_of_attempts = 0;
20         char sender[256];
21         char recipients[65536];
22         int recipients_len = 0;
23         char thisrecp[256];
24         char thisdsn[256];
25         char thismsg[512];
26         int thismsg_len;
27         long msgid = 0;
28         int len;
29
30         strcpy(sender, "");
31         strcpy(recipients, "");
32         recipients_len = 0;
33
34         serv_printf("MSG2 %ld", msgnum);
35         serv_getln(buf, sizeof buf);
36         if (buf[0] != '1') return;
37
38         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
39
40                 if (!IsEmptyStr(buf)) {
41                         len = strlen(buf);
42                         if (buf[len - 1] == 13) {
43                                 buf[len - 1] = 0;
44                         }
45                 }
46
47                 if ( (IsEmptyStr(buf)) && (in_body == 0) ) {
48                         in_body = 1;
49                 }
50
51                 if ( (!in_body)
52                    && (!strncasecmp(buf, "Content-type: application/x-citadel-delivery-list", 49))
53                 ) {
54                         is_delivery_list = 1;
55                 }
56
57                 if ( (in_body) && (!is_delivery_list) ) {
58                         while (serv_getln(buf, sizeof buf), strcmp(buf, "000")) {
59                                 /* Not a delivery list; flush and return quietly. */
60                         }
61                         return;
62                 }
63
64                 if ( (in_body) && (is_delivery_list) ) {
65                         extract_token(keyword, buf, 0, '|', sizeof keyword);
66
67                         if (!strcasecmp(keyword, "msgid")) {
68                                 msgid = extract_long(buf, 1);
69                         }
70
71                         if (!strcasecmp(keyword, "submitted")) {
72                                 submitted = extract_long(buf, 1);
73                         }
74
75                         if (!strcasecmp(keyword, "attempted")) {
76                                 attempted = extract_long(buf, 1);
77                                 ++number_of_attempts;
78                                 if (attempted > last_attempt) {
79                                         last_attempt = attempted;
80                                 }
81                         }
82
83                         if (!strcasecmp(keyword, "bounceto")) {
84                                 char *atsign;
85                                 extract_token(sender, buf, 1, '|', sizeof sender);
86
87                                 /* Strip off local hostname if it's our own */
88                                 atsign = strchr(sender, '@');
89                                 if (atsign != NULL) {
90                                         ++atsign;
91                                         if (!strcasecmp(atsign, ChrPtr(WC->serv_info->serv_nodename))) {
92                                                 --atsign;
93                                                 *atsign = 0;
94                                         }
95                                 }
96                         }
97
98                         if (!strcasecmp(keyword, "remote")) {
99                                 thismsg[0] = 0;
100
101                                 extract_token(thisrecp, buf, 1, '|', sizeof thisrecp);
102                                 extract_token(thisdsn, buf, 3, '|', sizeof thisdsn);
103
104                                 if (!IsEmptyStr(thisrecp)) {
105                                         stresc(thismsg, sizeof thismsg, thisrecp, 1, 1);
106                                         if (!IsEmptyStr(thisdsn)) {
107                                                 strcat(thismsg, "<br />&nbsp;&nbsp;<i>");
108                                                 stresc(&thismsg[strlen(thismsg)], sizeof thismsg,
109                                                         thisdsn, 1, 1);
110                                                 strcat(thismsg, "</i>");
111                                         }
112                                         thismsg_len = strlen(thismsg);
113
114                                         if ((recipients_len + thismsg_len + 100) < sizeof recipients) {
115                                                 if (!IsEmptyStr(recipients)) {
116                                                         strcpy(&recipients[recipients_len], "<br />");
117                                                         recipients_len += 6;
118                                                 }
119                                                 strcpy(&recipients[recipients_len], thismsg);
120                                                 recipients_len += thismsg_len;
121                                         }
122                                 }
123
124                         }
125
126                 }
127
128         }
129
130         wc_printf("<tr><td>");
131         wc_printf("%ld<br />", msgnum);
132         wc_printf(" <a href=\"javascript:DeleteSMTPqueueMsg(%ld,%ld);\">%s</a>", 
133                 msgnum, msgid, _("(Delete)")
134         );
135
136         wc_printf("</td><td>");
137         if (submitted > 0) {
138                 webcit_fmt_date(buf, 1024, submitted, 1);
139                 wc_printf("%s", buf);
140         }
141         else {
142                 wc_printf("&nbsp;");
143         }
144
145         wc_printf("</td><td>");
146         if (last_attempt > 0) {
147                 webcit_fmt_date(buf, 1024, last_attempt, 1);
148                 wc_printf("%s", buf);
149         }
150         else {
151                 wc_printf("&nbsp;");
152         }
153
154         wc_printf("</td><td>");
155         escputs(sender);
156
157         wc_printf("</td><td>");
158         wc_printf("%s", recipients);
159         wc_printf("</td></tr>\n");
160
161 }
162
163
164 void display_smtpqueue_inner_div(void) {
165         message_summary *Msg = NULL;
166         wcsession *WCC = WC;
167         int i;
168         int num_msgs;
169         StrBuf *Buf;
170         SharedMessageStatus Stat;
171
172         memset(&Stat, 0, sizeof(SharedMessageStatus));
173         /* Check to see if we can go to the __CitadelSMTPspoolout__ room.
174          * If not, we don't have access to the queue.
175          */
176         Buf = NewStrBufPlain(HKEY("__CitadelSMTPspoolout__"));
177         gotoroom(Buf);
178         FreeStrBuf(&Buf);
179         if (!strcasecmp(ChrPtr(WCC->CurRoom.name), "__CitadelSMTPspoolout__")) {
180
181                 Stat.maxload = 10000;
182                 Stat.lowest_found = (-1);
183                 Stat.highest_found = (-1);
184                 num_msgs = load_msg_ptrs("MSGS ALL", &Stat, NULL);
185                 if (num_msgs > 0) {
186                         wc_printf("<table class=\"mailbox_summary\" rules=rows "
187                                 "cellpadding=2 style=\"width:100%%;\">"
188                         );
189
190                         wc_printf("<tr><td><b><i>");
191                         wc_printf(_("Message ID"));
192                         wc_printf("</i></b></td><td><b><i>");
193                         wc_printf(_("Date/time submitted"));
194                         wc_printf("</i></b></td><td><b><i>");
195                         wc_printf(_("Last attempt"));
196                         wc_printf("</i></b></td><td><b><i>");
197                         wc_printf(_("Sender"));
198                         wc_printf("</i></b></td><td><b><i>");
199                         wc_printf(_("Recipients"));
200                         wc_printf("</i></b></td></tr>\n");
201
202                         for (i=0; (i < num_msgs) && (i < Stat.maxload); ++i) {
203                                 Msg = GetMessagePtrAt(i, WCC->summ);
204                                 if (Msg != NULL) {
205                                         display_queue_msg(Msg->msgnum);
206                                 }
207                         }
208
209                         wc_printf("</table>");
210
211                 }
212                 else {
213                         wc_printf("<br /><br /><div align=\"center\">");
214                         wc_printf(_("The queue is empty."));
215                         wc_printf("</div><br /><br />");
216                 }
217         }
218         else {
219                 wc_printf("<br /><br /><div align=\"center\">");
220                 wc_printf(_("You do not have permission to view this resource."));
221                 wc_printf("</div><br /><br />");
222         }
223         output_headers(0, 0, 0, 0, 0, 0);
224         end_burst();
225 }
226
227 /*
228  * display the outbound SMTP queue
229  */
230 void display_smtpqueue(void)
231 {
232         output_headers(1, 1, 2, 0, 0, 0);
233
234         wc_printf("<div id=\"banner\">\n");
235         wc_printf("<h1>");
236         wc_printf(_("View the outbound SMTP queue"));
237         wc_printf("</h1>\n");
238         wc_printf("</div>\n");
239
240         wc_printf("<div id=\"content\" class=\"service\">\n");
241
242         wc_printf("<div class=\"fix_scrollbar_bug\">"
243                 "<table class=\"smtpqueue_background\">"
244                 "<tr><td valign=top>\n");
245
246         wc_printf("<div id=\"smtpqueue_inner_div\">"
247                 "<div align=\"center\"><img src=\"static/throbber.gif\"></div>"
248                 "</div>"
249                 "<div align=\"center\">"
250                 "<a href=\"javascript:RefreshSMTPqueueDisplay();\">%s</a>"
251                 "</div>"
252                 "</td></tr></table></div>\n", _("Refresh this page")
253         );
254
255         StrBufAppendPrintf(WC->trailing_javascript, "RefreshSMTPqueueDisplay();\n");
256
257         wDumpContent(1);
258
259 }
260
261 void 
262 InitModule_SMTP_QUEUE
263 (void)
264 {
265         WebcitAddUrlHandler(HKEY("display_smtpqueue"), "", 0, display_smtpqueue, 0);
266         WebcitAddUrlHandler(HKEY("display_smtpqueue_inner_div"), "", 0, display_smtpqueue_inner_div, 0);
267 }