HUGE PATCH. This moves all of mime_parser.c and all
[citadel.git] / webcit / smtpqueue.c
1 /* 
2  * $Id$
3  */
4 /**
5  * \defgroup SMTPqueue Display the outbound SMTP queue
6  * \ingroup CitadelConfig
7  */
8 /*@{*/
9 #include "webcit.h"
10
11 /**
12  * \brief display one message in the queue
13  */
14 void display_queue_msg(long msgnum)
15 {
16         char buf[1024];
17         char keyword[32];
18         int in_body = 0;
19         int is_delivery_list = 0;
20         time_t submitted = 0;
21         time_t attempted = 0;
22         time_t last_attempt = 0;
23         int number_of_attempts = 0;
24         char sender[256];
25         char recipients[65536];
26         char thisrecp[256];
27         char thisdsn[256];
28         long msgid = 0;
29         int len;
30
31         strcpy(sender, "");
32         strcpy(recipients, "");
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) == 0) && (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                                 extract_token(sender, buf, 1, '|', sizeof sender);
85
86                                 /* Strip off local hostname if it's our own */
87                                 char *atsign;
88                                 atsign = strchr(sender, '@');
89                                 if (atsign != NULL) {
90                                         ++atsign;
91                                         if (!strcasecmp(atsign, serv_info.serv_nodename)) {
92                                                 --atsign;
93                                                 *atsign = 0;
94                                         }
95                                 }
96                         }
97
98                         if (!strcasecmp(keyword, "remote")) {
99                                 int RcptLen;
100                                 int TRcptLen;
101                                 int TDsn;
102                                 int NLen;
103                                 extract_token(thisrecp, buf, 1, '|', sizeof thisrecp);
104                                 extract_token(thisdsn, buf, 3, '|', sizeof thisdsn);
105                                 RcptLen = strlen(recipients);
106                                 TRcptLen = strlen(thisrecp);
107                                 TDsn = strlen(thisdsn);
108                                 if ( RcptLen + TRcptLen + TDsn + 100
109                                    < sizeof recipients) {
110                                         if (!IsEmptyStr(recipients)) {
111                                                 // copy the \0 to be sure..
112                                                 memcpy (&recipients[RcptLen], "<br />\0",  7);
113                                                 RcptLen += 6;
114                                         }
115                                         NLen = stresc(&recipients[RcptLen], 
116                                                       sizeof recipients - RcptLen, 
117                                                       thisrecp, 1, 1);
118                                         if (NLen != -1)
119                                         {
120                                                 RcptLen += NLen;
121                                                 NLen = sizeof "<br />&nbsp;&nbsp;<i>";
122                                                 memcpy(recipients, "<br />&nbsp;&nbsp;<i>", 
123                                                        NLen);
124                                                 RcptLen += NLen - 1;
125                                                 NLen = stresc(&recipients[RcptLen], 
126                                                               sizeof recipients - RcptLen, 
127                                                               thisdsn, 1, 1);
128                                                 if (NLen != -1)
129                                                         memcpy (recipients, "</i>\0", 5);
130                                         } /// else bail out?
131                                 }
132
133                         }
134
135                 }
136
137         }
138
139         wprintf("<tr><td>");
140         wprintf("%ld<br />", msgnum);
141         wprintf(" <a href=\"javascript:DeleteQueueMsg(%ld,%ld);\">%s</a>", 
142                 msgnum, msgid, _("(Delete)")
143         );
144
145         wprintf("</td><td>");
146         if (submitted > 0) {
147                 webcit_fmt_date(buf, submitted, 1);
148                 wprintf("%s", buf);
149         }
150         else {
151                 wprintf("&nbsp;");
152         }
153
154         wprintf("</td><td>");
155         if (last_attempt > 0) {
156                 webcit_fmt_date(buf, last_attempt, 1);
157                 wprintf("%s", buf);
158         }
159         else {
160                 wprintf("&nbsp;");
161         }
162
163         wprintf("</td><td>");
164         escputs(sender);
165
166         wprintf("</td><td>");
167         wprintf("%s", recipients);
168         wprintf("</td></tr>\n");
169
170 }
171
172
173 void display_smtpqueue_inner_div(void) {
174         int i;
175         int num_msgs;
176
177         /* Check to see if we can go to the __CitadelSMTPspoolout__ room.
178          * If not, we don't have access to the queue.
179          */
180         gotoroom("__CitadelSMTPspoolout__");
181         if (!strcasecmp(WC->wc_roomname, "__CitadelSMTPspoolout__")) {
182
183                 num_msgs = load_msg_ptrs("MSGS ALL", 0);
184                 if (num_msgs > 0) {
185                         wprintf("<table class=\"mailbox_summary\" rules=rows "
186                                 "cellpadding=2 style=\"width:100%%;-moz-user-select:none;\">"
187                         );
188
189                         wprintf("<tr><td><b><i>");
190                         wprintf(_("Message ID"));
191                         wprintf("</i></b></td><td><b><i>");
192                         wprintf(_("Date/time submitted"));
193                         wprintf("</i></b></td><td><b><i>");
194                         wprintf(_("Last attempt"));
195                         wprintf("</i></b></td><td><b><i>");
196                         wprintf(_("Sender"));
197                         wprintf("</i></b></td><td><b><i>");
198                         wprintf(_("Recipients"));
199                         wprintf("</i></b></td></tr>\n");
200
201                         for (i=0; i<num_msgs; ++i) {
202                                 display_queue_msg(WC->msgarr[i]);
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
276
277
278 /*@}*/