* move serv_info into the session, here we can control its de/allocation the right...
[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         wprintf("<tr><td>");
133         wprintf("%ld<br />", msgnum);
134         wprintf(" <a href=\"javascript:DeleteQueueMsg(%ld,%ld);\">%s</a>", 
135                 msgnum, msgid, _("(Delete)")
136         );
137
138         wprintf("</td><td>");
139         if (submitted > 0) {
140                 webcit_fmt_date(buf, submitted, 1);
141                 wprintf("%s", buf);
142         }
143         else {
144                 wprintf("&nbsp;");
145         }
146
147         wprintf("</td><td>");
148         if (last_attempt > 0) {
149                 webcit_fmt_date(buf, last_attempt, 1);
150                 wprintf("%s", buf);
151         }
152         else {
153                 wprintf("&nbsp;");
154         }
155
156         wprintf("</td><td>");
157         escputs(sender);
158
159         wprintf("</td><td>");
160         wprintf("%s", recipients);
161         wprintf("</td></tr>\n");
162
163 }
164
165
166 void display_smtpqueue_inner_div(void) {
167         message_summary *Msg;
168         wcsession *WCC = WC;
169         int i;
170         int num_msgs;
171         StrBuf *Buf;
172
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->wc_roomname), "__CitadelSMTPspoolout__")) {
180
181                 num_msgs = load_msg_ptrs("MSGS ALL", 0);
182                 if (num_msgs > 0) {
183                         wprintf("<table class=\"mailbox_summary\" rules=rows "
184                                 "cellpadding=2 style=\"width:100%%;\">"
185                         );
186
187                         wprintf("<tr><td><b><i>");
188                         wprintf(_("Message ID"));
189                         wprintf("</i></b></td><td><b><i>");
190                         wprintf(_("Date/time submitted"));
191                         wprintf("</i></b></td><td><b><i>");
192                         wprintf(_("Last attempt"));
193                         wprintf("</i></b></td><td><b><i>");
194                         wprintf(_("Sender"));
195                         wprintf("</i></b></td><td><b><i>");
196                         wprintf(_("Recipients"));
197                         wprintf("</i></b></td></tr>\n");
198
199                         for (i=0; i<num_msgs; ++i) {
200                                 Msg = GetMessagePtrAt(i, WCC->summ);
201
202                                 display_queue_msg((Msg==NULL)? 0 : Msg->msgnum);
203                         }
204
205                         wprintf("</table>");
206
207                 }
208                 else {
209                         wprintf("<br /><br /><div align=\"center\">");
210                         wprintf(_("The queue is empty."));
211                         wprintf("</div><br /><br />");
212                 }
213         }
214         else {
215                 wprintf("<br /><br /><div align=\"center\">");
216                 wprintf(_("You do not have permission to view this resource."));
217                 wprintf("</div><br /><br />");
218         }
219
220 }
221
222 /**
223  * \brief display the outbound SMTP queue
224  */
225 void display_smtpqueue(void)
226 {
227         output_headers(1, 1, 2, 0, 0, 0);
228
229         wprintf("<script type=\"text/javascript\">                              \n"
230                 "function RefreshQueueDisplay() {                               \n"
231                 "       new Ajax.Updater('smtpqueue_inner_div',                 \n"
232                 "       'display_smtpqueue_inner_div', { method: 'get',         \n"
233                 "               parameters: Math.random() } );                  \n"
234                 "}                                                              \n"
235                 "                                                               \n"
236                 "function DeleteQueueMsg(msgnum1, msgnum2) {                                    \n"
237                 "       new Ajax.Request(                                                       \n"
238                 "               'ajax_servcmd', {                                               \n"
239                 "                       method: 'post',                                         \n"
240                 "                       parameters: 'g_cmd=DELE ' + msgnum1 + ',' + msgnum2,    \n"
241                 "                       onComplete: RefreshQueueDisplay()                       \n"
242                 "               }                                                               \n"
243                 "       );                                                                      \n"
244                 "}                                                                              \n"
245                 "                                                               \n"
246                 "</script>                                                      \n"
247         );
248
249         wprintf("<div id=\"banner\">\n");
250         wprintf("<h1>");
251         wprintf(_("View the outbound SMTP queue"));
252         wprintf("</h1>\n");
253         wprintf("</div>\n");
254
255         wprintf("<div id=\"content\" class=\"service\">\n");
256
257         wprintf("<div class=\"fix_scrollbar_bug\">"
258                 "<table class=\"smtpqueue_background\">"
259                 "<tr><td valign=top>\n");
260
261         wprintf("<div id=\"smtpqueue_inner_div\">");
262
263         display_smtpqueue_inner_div();
264
265         wprintf("</div>"
266                 "<div align=\"center\">"
267                 "<a href=\"javascript:RefreshQueueDisplay();\">%s</a>"
268                 "</div>"
269                 "</td></tr></table></div>\n", _("Refresh this page")
270         );
271         wDumpContent(1);
272
273 }
274
275 void 
276 InitModule_SMTP_QUEUE
277 (void)
278 {
279         WebcitAddUrlHandler(HKEY("display_smtpqueue"), display_smtpqueue, 0);
280         WebcitAddUrlHandler(HKEY("display_smtpqueue_inner_div"), display_smtpqueue_inner_div, 0);
281 }
282
283 /*@}*/