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